0022-omxdec-optimize-with-omx-zero-copy-feature.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. From 640ea14c3ca4536dce22a24153ac37d752009412 Mon Sep 17 00:00:00 2001
  2. From: Som Qin <som.qin@starfivetech.com>
  3. Date: Fri, 4 Aug 2023 11:38:04 +0800
  4. Subject: [PATCH 1/8] omxdec optimize with omx zero copy feature
  5. Signed-off-by: Som Qin <som.qin@starfivetech.com>
  6. ---
  7. libavcodec/omxdec.c | 63 ++++++++++++++++++++++++++++++++++-----------
  8. 1 file changed, 48 insertions(+), 15 deletions(-)
  9. diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
  10. index 1bb08b1..c518542 100755
  11. --- a/libavcodec/omxdec.c
  12. +++ b/libavcodec/omxdec.c
  13. @@ -342,6 +342,8 @@ typedef struct OMXCodecContext {
  14. pthread_cond_t input_cond;
  15. pthread_mutex_t output_mutex;
  16. pthread_cond_t output_cond;
  17. + pthread_mutex_t disableEVnt_mutex;
  18. + pthread_cond_t disableEVnt_cond;
  19. pthread_mutex_t state_mutex;
  20. pthread_cond_t state_cond;
  21. @@ -419,7 +421,7 @@ static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, O
  22. OMX_PARAM_PORTDEFINITIONTYPE out_port_params = { 0 };
  23. OMX_PORT_PARAM_TYPE video_port_params = { 0 };
  24. OMX_ERRORTYPE err;
  25. - int i;
  26. + int i, num;
  27. switch (event) {
  28. case OMX_EventError:
  29. @@ -438,6 +440,11 @@ static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, O
  30. pthread_mutex_unlock(&s->state_mutex);
  31. } else if (data1 == OMX_CommandPortDisable) {
  32. av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" disabled\n", (uint32_t) data2);
  33. + if (data2 == 1) {
  34. + pthread_mutex_lock(&s->disableEVnt_mutex);
  35. + pthread_cond_broadcast(&s->disableEVnt_cond);
  36. + pthread_mutex_unlock(&s->disableEVnt_mutex);
  37. + }
  38. } else if (data1 == OMX_CommandPortEnable) {
  39. av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" enabled\n", (uint32_t) data2);
  40. } else {
  41. @@ -470,6 +477,32 @@ static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, O
  42. dec_pix_fmt = out_port_params.format.video.eColorFormat;
  43. av_log(s->avctx, AV_LOG_VERBOSE, "w:%d, h:%d, fmt:%d\n", dec_out_width, dec_out_height, dec_pix_fmt);
  44. + if (!s->num_out_buffers) {
  45. + s->num_out_buffers = out_port_params.nBufferCountActual;
  46. + s->out_buffer_headers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_out_buffers);
  47. + s->done_out_buffers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_out_buffers);
  48. +
  49. + if (!s->out_buffer_headers || !s->done_out_buffers)
  50. + return AVERROR(ENOMEM);
  51. +
  52. + OMX_SendCommand(s->handle, OMX_CommandPortEnable, 1, NULL);
  53. +
  54. + for (num = 0; num < s->num_out_buffers && err == OMX_ErrorNone; num++)
  55. + err = OMX_AllocateBuffer(s->handle, &s->out_buffer_headers[num], s->out_port, s, out_port_params.nBufferSize);
  56. +
  57. + if (err != OMX_ErrorNone) {
  58. + av_log(s->avctx, AV_LOG_ERROR, "err %x (%d) on line %d\n", err, err, __LINE__);
  59. + return AVERROR_UNKNOWN;
  60. + }
  61. + s->num_out_buffers = num;
  62. +
  63. + for (num = 0; num < s->num_out_buffers && err == OMX_ErrorNone; num++)
  64. + err = OMX_FillThisBuffer(s->handle, s->out_buffer_headers[num]);
  65. + if (err != OMX_ErrorNone) {
  66. + for (; num < s->num_out_buffers; num++)
  67. + s->done_out_buffers[s->num_done_out_buffers++] = s->out_buffer_headers[num];
  68. + }
  69. + }
  70. }
  71. }
  72. break;
  73. @@ -866,15 +899,18 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  74. CHECK(err);
  75. err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
  76. CHECK(err);
  77. - s->num_out_buffers = out_port_params.nBufferCountActual;
  78. +
  79. + OMX_SendCommand(s->handle, OMX_CommandPortDisable, 1, NULL);
  80. + pthread_mutex_lock(&s->disableEVnt_mutex);
  81. + pthread_cond_wait(&s->disableEVnt_cond, &s->disableEVnt_mutex);
  82. + pthread_mutex_unlock(&s->disableEVnt_mutex);
  83. err = OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
  84. CHECK(err);
  85. s->in_buffer_headers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_in_buffers);
  86. s->free_in_buffers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_in_buffers);
  87. - s->out_buffer_headers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_out_buffers);
  88. - s->done_out_buffers = av_mallocz(sizeof(OMX_BUFFERHEADERTYPE*) * s->num_out_buffers);
  89. - if (!s->in_buffer_headers || !s->free_in_buffers || !s->out_buffer_headers || !s->done_out_buffers)
  90. +
  91. + if (!s->in_buffer_headers || !s->free_in_buffers)
  92. return AVERROR(ENOMEM);
  93. for (i = 0; i < s->num_in_buffers && err == OMX_ErrorNone; i++) {
  94. @@ -887,10 +923,7 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  95. }
  96. CHECK(err);
  97. s->num_in_buffers = i;
  98. - for (i = 0; i < s->num_out_buffers && err == OMX_ErrorNone; i++)
  99. - err = OMX_AllocateBuffer(s->handle, &s->out_buffer_headers[i], s->out_port, s, out_port_params.nBufferSize);
  100. - CHECK(err);
  101. - s->num_out_buffers = i;
  102. +
  103. if (wait_for_state(s, OMX_StateIdle) < 0) {
  104. av_log(avctx, AV_LOG_ERROR, "Didn't get OMX_StateIdle\n");
  105. return AVERROR_UNKNOWN;
  106. @@ -901,12 +934,7 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  107. av_log(avctx, AV_LOG_ERROR, "Didn't get OMX_StateExecuting\n");
  108. return AVERROR_UNKNOWN;
  109. }
  110. - for (i = 0; i < s->num_out_buffers && err == OMX_ErrorNone; i++)
  111. - err = OMX_FillThisBuffer(s->handle, s->out_buffer_headers[i]);
  112. - if (err != OMX_ErrorNone) {
  113. - for (; i < s->num_out_buffers; i++)
  114. - s->done_out_buffers[s->num_done_out_buffers++] = s->out_buffer_headers[i];
  115. - }
  116. +
  117. for (i = 0; i < s->num_in_buffers; i++)
  118. s->free_in_buffers[s->num_free_in_buffers++] = s->in_buffer_headers[i];
  119. return err != OMX_ErrorNone ? AVERROR_UNKNOWN : 0;
  120. @@ -952,6 +980,8 @@ static av_cold void cleanup(OMXCodecContext *s)
  121. pthread_mutex_destroy(&s->input_mutex);
  122. pthread_cond_destroy(&s->output_cond);
  123. pthread_mutex_destroy(&s->output_mutex);
  124. + pthread_cond_destroy(&s->disableEVnt_cond);
  125. + pthread_mutex_destroy(&s->disableEVnt_mutex);
  126. s->mutex_cond_inited = 0;
  127. }
  128. OMXDecodeQueueDestory(&s->decode_pts_queue);
  129. @@ -984,6 +1014,8 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
  130. pthread_cond_init(&s->input_cond, NULL);
  131. pthread_mutex_init(&s->output_mutex, NULL);
  132. pthread_cond_init(&s->output_cond, NULL);
  133. + pthread_mutex_init(&s->disableEVnt_mutex, NULL);
  134. + pthread_cond_init(&s->disableEVnt_cond, NULL);
  135. s->mutex_cond_inited = 1;
  136. s->avctx = avctx;
  137. s->state = OMX_StateLoaded;
  138. @@ -1009,6 +1041,7 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
  139. if ((ret = find_component(s->omx_context, avctx, role, s->component_name, sizeof(s->component_name))) < 0)
  140. goto fail;
  141. + strcat(s->component_name, ".internal");
  142. av_log(avctx, AV_LOG_INFO, "Using %s\n", s->component_name);
  143. --
  144. 2.25.1