0030-Omxdec-fix-decode-process-stuck-in-some-hls-cases.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. From 81d10d2e614c4c5fd0c7a23c47196c7b1d552a20 Mon Sep 17 00:00:00 2001
  2. From: Som Qin <som.qin@starfivetech.com>
  3. Date: Tue, 29 Aug 2023 21:18:17 +0800
  4. Subject: [PATCH 1/2] Omxdec :fix decode process stuck in some hls cases.
  5. Signed-off-by: Som Qin <som.qin@starfivetech.com>
  6. ---
  7. libavcodec/omxdec.c | 45 ++++++++++++++++++++++++++++-----------------
  8. 1 file changed, 28 insertions(+), 17 deletions(-)
  9. diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
  10. index b38e3b9..18bd954 100755
  11. --- a/libavcodec/omxdec.c
  12. +++ b/libavcodec/omxdec.c
  13. @@ -356,6 +356,7 @@ typedef struct OMXCodecContext {
  14. int extradata_sent;
  15. int has_cleanup;
  16. + int has_disable_outport;
  17. uint8_t *output_buf;
  18. int output_buf_size;
  19. @@ -392,11 +393,16 @@ static void append_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond,
  20. static OMX_BUFFERHEADERTYPE *get_buffer(pthread_mutex_t *mutex, pthread_cond_t *cond,
  21. int* array_size, OMX_BUFFERHEADERTYPE **array,
  22. - int wait)
  23. + int wait_sec)
  24. {
  25. + struct timespec abstime;
  26. OMX_BUFFERHEADERTYPE *buffer;
  27. pthread_mutex_lock(mutex);
  28. - if (wait) {
  29. + if (wait_sec > 0 && !*array_size) {
  30. + clock_gettime(CLOCK_REALTIME, &abstime);
  31. + abstime.tv_sec += wait_sec;
  32. + pthread_cond_timedwait(cond, mutex, &abstime);
  33. + } else if (wait_sec < 0) {
  34. while (!*array_size)
  35. {
  36. pthread_cond_wait(cond, mutex);
  37. @@ -445,6 +451,7 @@ static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, O
  38. av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" disabled\n", (uint32_t) data2);
  39. if (data2 == 1) {
  40. pthread_mutex_lock(&s->disableEVnt_mutex);
  41. + s->has_disable_outport = 1;
  42. pthread_cond_broadcast(&s->disableEVnt_cond);
  43. pthread_mutex_unlock(&s->disableEVnt_mutex);
  44. }
  45. @@ -903,9 +910,11 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  46. err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
  47. CHECK(err);
  48. + s->has_disable_outport = 0;
  49. OMX_SendCommand(s->handle, OMX_CommandPortDisable, 1, NULL);
  50. pthread_mutex_lock(&s->disableEVnt_mutex);
  51. - pthread_cond_wait(&s->disableEVnt_cond, &s->disableEVnt_mutex);
  52. + if(!s->has_disable_outport)
  53. + pthread_cond_wait(&s->disableEVnt_cond, &s->disableEVnt_mutex);
  54. pthread_mutex_unlock(&s->disableEVnt_mutex);
  55. err = OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
  56. @@ -958,7 +967,7 @@ static av_cold void cleanup(OMXCodecContext *s)
  57. OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateLoaded, NULL);
  58. for (i = 0; i < s->num_in_buffers; i++) {
  59. buffer = get_buffer(&s->input_mutex, &s->input_cond,
  60. - &s->num_free_in_buffers, s->free_in_buffers, 1);
  61. + &s->num_free_in_buffers, s->free_in_buffers, -1);
  62. if (s->input_zerocopy)
  63. buffer->pBuffer = NULL;
  64. OMX_FreeBuffer(s->handle, s->in_port, buffer);
  65. @@ -1209,7 +1218,7 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
  66. int ret = 0;
  67. OMX_BUFFERHEADERTYPE* buffer;
  68. OMX_ERRORTYPE err;
  69. - int had_partial = 0;
  70. + //int had_partial = 0;
  71. AVFrame *avframe = data;
  72. @@ -1221,26 +1230,28 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
  73. pkt->size, pkt->pts, pkt->dts, pkt->duration);
  74. av_log(avctx, AV_LOG_VERBOSE, "avctx->pts_correction_last_pts: %ld avctx->pts_correction_last_dts: %ld\n",
  75. avctx->pts_correction_last_pts, avctx->pts_correction_last_dts);
  76. - if (pkt->dts != AV_NOPTS_VALUE) {
  77. - OMXDecodeQueuePush(&s->decode_pts_queue, pkt->dts);
  78. - } else {
  79. - if (OMXDecodeQueueEmpty(&s->decode_pts_queue)) {
  80. - OMXDecodeQueuePush(&s->decode_pts_queue, 0);
  81. - } else {
  82. - OMXDecodeQueuePush(&s->decode_pts_queue, s->decode_pts_queue.tail->val + pkt->duration);
  83. - }
  84. - }
  85. +
  86. if (pkt->size) {
  87. //VPU init and fill buffer slow, so empty buf sleep to send before get vpu fill buf.
  88. // if(!s->first_get_outbuffer)
  89. // av_usleep(100000);
  90. buffer = get_buffer(&s->input_mutex, &s->input_cond,
  91. - &s->num_free_in_buffers, s->free_in_buffers, 1);
  92. + &s->num_free_in_buffers, s->free_in_buffers, 10);
  93. if (!buffer) {
  94. av_log(avctx, AV_LOG_ERROR, "get_buffer NULL\n");
  95. - return AVERROR(ENOMEM);
  96. + return AVERROR(EAGAIN);
  97. + }
  98. +
  99. + if (pkt->dts != AV_NOPTS_VALUE) {
  100. + OMXDecodeQueuePush(&s->decode_pts_queue, pkt->dts);
  101. + } else {
  102. + if (OMXDecodeQueueEmpty(&s->decode_pts_queue)) {
  103. + OMXDecodeQueuePush(&s->decode_pts_queue, 0);
  104. + } else {
  105. + OMXDecodeQueuePush(&s->decode_pts_queue, s->decode_pts_queue.tail->val + pkt->duration);
  106. + }
  107. }
  108. //cpy the extradata
  109. @@ -1280,7 +1291,7 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
  110. // packet, or get EOS.
  111. buffer = get_buffer(&s->output_mutex, &s->output_cond,
  112. &s->num_done_out_buffers, s->done_out_buffers,
  113. - !pkt || had_partial);
  114. + 0);
  115. if (!buffer) {
  116. break;
  117. --
  118. 2.25.1