0021-FFmpeg-omxdec-set-pts-of-avframe-and-fix-some-compil.patch 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. From 888f1780682a6a73b4004bec4d51c450ddaac710 Mon Sep 17 00:00:00 2001
  2. From: "arvin.zhu" <arvin.zhu@starfivetech.com>
  3. Date: Wed, 23 Nov 2022 15:08:36 +0800
  4. Subject: [PATCH] FFmpeg:omxdec: set pts of avframe and fix some compilation
  5. warnings
  6. set pts of avframe from video container and fix some compilation warnings
  7. Signed-off-by: arvin.zhu <arvin.zhu@starfivetech.com>
  8. ---
  9. libavcodec/omxdec.c | 129 +++++++++++++++++++++++++++++++++++++++-----
  10. 1 file changed, 116 insertions(+), 13 deletions(-)
  11. diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
  12. index 4974833..1bb08b1 100755
  13. --- a/libavcodec/omxdec.c
  14. +++ b/libavcodec/omxdec.c
  15. @@ -96,13 +96,96 @@ typedef struct OMXContext {
  16. void (*host_init)(void);
  17. } OMXContext;
  18. -static const struct {
  19. +typedef struct OMXDecodeQueueNode {
  20. + int64_t val;
  21. + struct OMXDecodeQueueNode* next;
  22. +} OMXDecodeQueueNode;
  23. +
  24. +typedef struct OMXDecodeQueue {
  25. + OMXDecodeQueueNode* head;
  26. + OMXDecodeQueueNode* tail;
  27. +} OMXDecodeQueue;
  28. +
  29. +static av_cold void OMXDecodeQueueInit(OMXDecodeQueue* pq)
  30. +{
  31. + assert(pq);
  32. + pq->head = pq->tail = NULL;
  33. +}
  34. +
  35. +static av_cold void OMXDecodeQueueDestory(OMXDecodeQueue* pq)
  36. +{
  37. + OMXDecodeQueueNode* cur = pq->head;
  38. + assert(pq);
  39. + while (cur)
  40. + {
  41. + OMXDecodeQueueNode* next = cur->next;
  42. + av_free(cur);
  43. + cur = next;
  44. + }
  45. + pq->tail = pq->head = NULL;
  46. +}
  47. +
  48. +static void OMXDecodeQueuePush(OMXDecodeQueue* pq, int64_t x)
  49. +{
  50. + OMXDecodeQueueNode* newNode = (OMXDecodeQueueNode*)malloc(sizeof(OMXDecodeQueueNode));
  51. + if (NULL == newNode)
  52. + {
  53. + av_log(NULL, AV_LOG_ERROR, "malloc queue error\n");
  54. + exit(-1);
  55. + }
  56. + assert(pq);
  57. + newNode->val = x;
  58. + newNode->next = NULL;
  59. +
  60. + if (pq->tail == NULL)
  61. + {
  62. + assert(pq->head == NULL);
  63. + pq->head = pq->tail = newNode;
  64. + }
  65. + else
  66. + {
  67. + pq->tail->next = newNode;
  68. + pq->tail = newNode;
  69. + }
  70. +}
  71. +
  72. +static void OMXDecodeQueuePop(OMXDecodeQueue* pq)
  73. +{
  74. + assert(pq);
  75. + assert(pq->head && pq->tail);
  76. + if (pq->head->next == NULL)
  77. + {
  78. + av_free(pq->head);
  79. + pq->head = pq->tail = NULL;
  80. + }
  81. + else
  82. + {
  83. + OMXDecodeQueueNode* next = pq->head->next;
  84. + av_free(pq->head);
  85. + pq->head = next;
  86. + }
  87. +}
  88. +
  89. +static int OMXDecodeQueueEmpty(OMXDecodeQueue* pq)
  90. +{
  91. + assert(pq);
  92. +
  93. + return pq->head == NULL;
  94. +}
  95. +
  96. +static int64_t OMXDecodeQueueFront(OMXDecodeQueue* pq)
  97. +{
  98. + assert(pq);
  99. + assert(pq->head);
  100. +
  101. + return pq->head->val;
  102. +}
  103. +
  104. +static const struct {
  105. int color_format;
  106. enum AVPixelFormat pix_fmt;
  107. -
  108. } color_formats[] = {
  109. -
  110. { OMX_COLOR_FormatYUV420Planar, AV_PIX_FMT_YUV420P },
  111. { OMX_COLOR_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12 },
  112. { OMX_COLOR_FormatYVU420SemiPlanar, AV_PIX_FMT_NV21 },
  113. @@ -286,6 +369,8 @@ typedef struct OMXCodecContext {
  114. int h;
  115. } crop;
  116. + OMXDecodeQueue decode_pts_queue;
  117. + int decode_flag;
  118. int input_zerocopy;
  119. int profile;
  120. char *pixel_format; /**< Set by a private option. */
  121. @@ -491,6 +576,10 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  122. //OMX_VIDEO_PARAM_PORTFORMATTYPE video_port_format = { 0 };
  123. //OMX_VIDEO_PARAM_BITRATETYPE vid_param_bitrate = { 0 };
  124. OMX_ERRORTYPE err;
  125. + OMX_CONFIG_SCALEFACTORTYPE ScaleConfig;
  126. + OMX_CONFIG_MIRRORTYPE MirrorConfig;
  127. + OMX_CONFIG_ROTATIONTYPE RotatConfig;
  128. + OMX_CONFIG_RECTTYPE RectConfig;
  129. int i;
  130. s->version.s.nVersionMajor = 1;
  131. s->version.s.nVersionMinor = 1;
  132. @@ -594,7 +683,6 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  133. /* Set Scale config setting*/
  134. if (s->scale_width || s->scale_height) {
  135. av_log(avctx, AV_LOG_TRACE, "mjpeg decoder: scaling width: %d scaling height: %d .\n", s->scale_width, s->scale_height);
  136. - OMX_CONFIG_SCALEFACTORTYPE ScaleConfig;
  137. INIT_STRUCT(ScaleConfig);
  138. ScaleConfig.nPortIndex = 1;
  139. OMX_GetConfig(s->handle, OMX_IndexConfigCommonScale, &ScaleConfig);
  140. @@ -665,7 +753,6 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  141. return AVERROR_INVALIDDATA;
  142. }
  143. av_log(avctx, AV_LOG_TRACE, "mjpeg decoder: mirror\n");
  144. - OMX_CONFIG_MIRRORTYPE MirrorConfig;
  145. INIT_STRUCT(MirrorConfig);
  146. MirrorConfig.nPortIndex = 1;
  147. OMX_GetConfig(s->handle, OMX_IndexConfigCommonMirror, &MirrorConfig);
  148. @@ -680,7 +767,6 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  149. return AVERROR_INVALIDDATA;
  150. }
  151. av_log(avctx, AV_LOG_TRACE, "mjpeg decoder: rotation\n");
  152. - OMX_CONFIG_ROTATIONTYPE RotatConfig;
  153. INIT_STRUCT(RotatConfig);
  154. RotatConfig.nPortIndex = 1;
  155. OMX_GetConfig(s->handle, OMX_IndexConfigCommonRotate, &RotatConfig);
  156. @@ -726,7 +812,6 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  157. return AVERROR_INVALIDDATA;
  158. }
  159. - OMX_CONFIG_RECTTYPE RectConfig;
  160. INIT_STRUCT(RectConfig);
  161. RectConfig.nPortIndex = 1;
  162. OMX_GetConfig(s->handle, OMX_IndexConfigCommonOutputCrop, &RectConfig);
  163. @@ -869,6 +954,8 @@ static av_cold void cleanup(OMXCodecContext *s)
  164. pthread_mutex_destroy(&s->output_mutex);
  165. s->mutex_cond_inited = 0;
  166. }
  167. + OMXDecodeQueueDestory(&s->decode_pts_queue);
  168. + av_freep(&s->decode_pts_queue);
  169. av_freep(&s->in_buffer_headers);
  170. av_freep(&s->out_buffer_headers);
  171. av_freep(&s->free_in_buffers);
  172. @@ -883,7 +970,10 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
  173. const char *role;
  174. //OMX_BUFFERHEADERTYPE *buffer;
  175. //OMX_ERRORTYPE err;
  176. + OMXDecodeQueueInit(&s->decode_pts_queue);
  177. + av_log(avctx, AV_LOG_VERBOSE, "avctx->time_base: %d/%d \n", avctx->time_base.num, avctx->time_base.den);
  178. + av_log(avctx, AV_LOG_VERBOSE, "avctx->framerate: %d/%d \n", avctx->framerate.num, avctx->framerate.den);
  179. s->omx_context = omx_init(avctx, s->libname, s->libprefix);
  180. if (!s->omx_context)
  181. return AVERROR_ENCODER_NOT_FOUND;
  182. @@ -898,6 +988,7 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
  183. s->avctx = avctx;
  184. s->state = OMX_StateLoaded;
  185. s->error = OMX_ErrorNone;
  186. + s->decode_flag = 0;
  187. switch (avctx->codec->id) {
  188. case AV_CODEC_ID_MPEG4:
  189. @@ -990,6 +1081,15 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
  190. uint8_t *dst[4];
  191. int linesize[4];
  192. + av_log(avctx, AV_LOG_VERBOSE, "s->decode_flag: %d\n", s->decode_flag);
  193. + av_log(avctx, AV_LOG_VERBOSE, "avctx->time_base: %d/%d \n", avctx->time_base.num, avctx->time_base.den);
  194. + av_log(avctx, AV_LOG_VERBOSE, "avctx->pkt_timebase: %d/%d \n", avctx->pkt_timebase.num, avctx->pkt_timebase.den);
  195. + av_log(avctx, AV_LOG_VERBOSE, "avctx->framerate: %d/%d \n", avctx->framerate.num, avctx->framerate.den);
  196. + av_log(avctx, AV_LOG_VERBOSE, "avpkt->size: %d avpkt->pts: %ld avpkt->dts: %ld avpkt->duration: %ld\n",
  197. + pkt->size, pkt->pts, pkt->dts, pkt->duration);
  198. + av_log(avctx, AV_LOG_VERBOSE, "avctx->pts_correction_last_pts: %ld avctx->pts_correction_last_dts: %ld\n",
  199. + avctx->pts_correction_last_pts, avctx->pts_correction_last_dts);
  200. + OMXDecodeQueuePush(&s->decode_pts_queue, pkt->dts);
  201. if (pkt->size) {
  202. //VPU init and fill buffer slow, so empty buf sleep to send before get vpu fill buf.
  203. @@ -1107,12 +1207,15 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
  204. av_image_copy(avframe->data, avframe->linesize, (const uint8_t**)dst, linesize,
  205. avctx->pix_fmt, avctx->width, avctx->height);
  206. -
  207. - //avframe->pts = buffer->nTimeStamp;
  208. - //avframe->pkt_dts = AV_NOPTS_VALUE;
  209. - //avframe->pict_type= AV_PICTURE_TYPE_I;
  210. - //avframe->key_frame= 1;
  211. -
  212. + if (pkt->pts) {
  213. + if (OMXDecodeQueueEmpty(&s->decode_pts_queue) != 0){
  214. + av_log(avctx, AV_LOG_ERROR, "The queue of decode pts is empty.\n");
  215. + return AVERROR_INVALIDDATA;
  216. + }
  217. + avframe->pts = OMXDecodeQueueFront(&s->decode_pts_queue);
  218. + OMXDecodeQueuePop(&s->decode_pts_queue);
  219. + }
  220. + s->decode_flag += 1;
  221. *got_packet = 1;
  222. /*
  223. --
  224. 2.17.1