0009-ffmpeg-add-mjpeg-decoder-support-and-fix-some-bug.patch 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. From fff5724489cd0a503d206c6f513b1d8572247484 Mon Sep 17 00:00:00 2001
  2. From: "sw.multimedia" <se.multimedia@starfivetech.com>
  3. Date: Wed, 25 May 2022 21:28:23 +0800
  4. Subject: [PATCH] ffmpeg: add mjpeg decoder support and fix some bug
  5. Signed-off-by: sw.multimedia <se.multimedia@starfivetech.com>
  6. ---
  7. configure | 1 +
  8. libavcodec/allcodecs.c | 1 +
  9. libavcodec/omx.c | 25 +++++++++++++++++++++---
  10. libavcodec/omxdec.c | 44 +++++++++++++++++++++++++++++++++++++-----
  11. libavformat/utils.c | 7 +++++++
  12. 5 files changed, 70 insertions(+), 8 deletions(-)
  13. diff --git a/configure b/configure
  14. index 80be074..ca465a1 100755
  15. --- a/configure
  16. +++ b/configure
  17. @@ -3095,6 +3095,7 @@ h264_v4l2m2m_decoder_select="h264_mp4toannexb_bsf"
  18. h264_v4l2m2m_encoder_deps="v4l2_m2m h264_v4l2_m2m"
  19. hevc_omx_encoder_deps="omx"
  20. hevc_omx_decoder_deps="omx"
  21. +mjpeg_omx_decoder_deps="omx"
  22. hevc_amf_encoder_deps="amf"
  23. hevc_cuvid_decoder_deps="cuvid"
  24. hevc_cuvid_decoder_select="hevc_mp4toannexb_bsf"
  25. diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
  26. index 0c7a18d..69627df 100644
  27. --- a/libavcodec/allcodecs.c
  28. +++ b/libavcodec/allcodecs.c
  29. @@ -817,6 +817,7 @@ extern AVCodec ff_libkvazaar_encoder;
  30. extern AVCodec ff_mjpeg_cuvid_decoder;
  31. extern AVCodec ff_mjpeg_qsv_encoder;
  32. extern AVCodec ff_mjpeg_qsv_decoder;
  33. +extern AVCodec ff_mjpeg_omx_decoder;
  34. extern AVCodec ff_mjpeg_vaapi_encoder;
  35. extern AVCodec ff_mp3_mf_encoder;
  36. extern AVCodec ff_mpeg1_cuvid_decoder;
  37. diff --git a/libavcodec/omx.c b/libavcodec/omx.c
  38. index 86e32a8..023db9c 100644
  39. --- a/libavcodec/omx.c
  40. +++ b/libavcodec/omx.c
  41. @@ -42,6 +42,7 @@
  42. #include "avcodec.h"
  43. #include "h264.h"
  44. +#include "hevc.h"
  45. #include "internal.h"
  46. #include "profiles.h"
  47. @@ -474,9 +475,11 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  48. in_port_params.format.video.nFrameWidth = avctx->width;
  49. in_port_params.format.video.nFrameHeight = avctx->height;
  50. if (avctx->framerate.den > 0 && avctx->framerate.num > 0)
  51. - in_port_params.format.video.xFramerate = (1LL << 16) * avctx->framerate.num / avctx->framerate.den;
  52. + //in_port_params.format.video.xFramerate = (1LL << 16) * avctx->framerate.num / avctx->framerate.den;
  53. + in_port_params.format.video.xFramerate = avctx->framerate.num / avctx->framerate.den;
  54. else
  55. - in_port_params.format.video.xFramerate = (1LL << 16) * avctx->time_base.den / avctx->time_base.num;
  56. + //in_port_params.format.video.xFramerate = (1LL << 16) * avctx->time_base.den / avctx->time_base.num;
  57. + in_port_params.format.video.xFramerate = avctx->time_base.den / avctx->time_base.num;
  58. err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &in_port_params);
  59. CHECK(err);
  60. @@ -721,7 +724,23 @@ static av_cold int omx_encode_init(AVCodecContext *avctx)
  61. }
  62. if (nals[H264_NAL_SPS] && nals[H264_NAL_PPS])
  63. break;
  64. - } else {
  65. + } else if (avctx->codec->id == AV_CODEC_ID_HEVC) {
  66. + // For H.265, the extradata can be returned in two separate buffers
  67. + // (the videocore encoder on raspberry pi does this);
  68. + // therefore check that we have got both SPS and PPS before continuing.
  69. + int nals[128] = { 0 };
  70. + int i;
  71. + for (i = 0; i + 4 < avctx->extradata_size; i++) {
  72. + if (!avctx->extradata[i + 0] &&
  73. + !avctx->extradata[i + 1] &&
  74. + !avctx->extradata[i + 2] &&
  75. + avctx->extradata[i + 3] == 1) {
  76. + nals[(avctx->extradata[i + 4] & 0x7E) >> 1]++;
  77. + }
  78. + }
  79. + if (nals[HEVC_NAL_SPS] && nals[HEVC_NAL_PPS] && nals[HEVC_NAL_VPS])
  80. + break;
  81. + } else {
  82. if (avctx->extradata_size > 0)
  83. break;
  84. }
  85. diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
  86. index 8d00b86..32f39f3 100644
  87. --- a/libavcodec/omxdec.c
  88. +++ b/libavcodec/omxdec.c
  89. @@ -106,6 +106,7 @@ static const struct {
  90. { OMX_COLOR_FormatYUV420Planar, AV_PIX_FMT_YUV420P },
  91. { OMX_COLOR_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12 },
  92. { OMX_COLOR_FormatYUV420PackedSemiPlanar, AV_PIX_FMT_NV21 },
  93. + { OMX_COLOR_FormatYUV444Interleaved, AV_PIX_FMT_YUV444P },
  94. { 0 }
  95. };
  96. @@ -366,6 +367,7 @@ static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, O
  97. dec_out_height = out_port_params.format.video.nFrameHeight;
  98. dec_pix_fmt = out_port_params.format.video.eColorFormat;
  99. + av_log(s->avctx, AV_LOG_VERBOSE, "w:%d, h:%d, fmt:%d\n", dec_out_width, dec_out_height, dec_pix_fmt);
  100. }
  101. }
  102. break;
  103. @@ -535,9 +537,11 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  104. in_port_params.format.video.nFrameHeight = avctx->height;
  105. if (avctx->framerate.den > 0 && avctx->framerate.num > 0)
  106. - in_port_params.format.video.xFramerate = (1LL << 16) * avctx->framerate.num / avctx->framerate.den;
  107. + //in_port_params.format.video.xFramerate = (1LL << 16) * avctx->framerate.num / avctx->framerate.den;
  108. + in_port_params.format.video.xFramerate = avctx->framerate.num / avctx->framerate.den;
  109. else
  110. - in_port_params.format.video.xFramerate = (1LL << 16) * avctx->time_base.den / avctx->time_base.num;
  111. + //in_port_params.format.video.xFramerate = (1LL << 16) * avctx->time_base.den / avctx->time_base.num;
  112. + in_port_params.format.video.xFramerate = avctx->time_base.den / avctx->time_base.num;
  113. err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &in_port_params);
  114. CHECK(err);
  115. @@ -567,6 +571,8 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  116. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
  117. else if (avctx->codec->id == AV_CODEC_ID_HEVC)
  118. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingHEVC;
  119. + else if (avctx->codec->id == AV_CODEC_ID_MJPEG)
  120. + out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMJPEG;
  121. err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
  122. CHECK(err);
  123. @@ -703,6 +709,9 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
  124. case AV_CODEC_ID_HEVC:
  125. role = "video_decoder.hevc";
  126. break;
  127. + case AV_CODEC_ID_MJPEG:
  128. + role = "video_decoder.mjpeg";
  129. + break;
  130. default:
  131. return AVERROR(ENOSYS);
  132. }
  133. @@ -852,11 +861,13 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
  134. !pkt || had_partial);
  135. if (!buffer) {
  136. - /*eos is sent wait for vpu evnet_bufferflag to get all frames*/
  137. -+ if(s->eos_sent && !evnet_bufferflag){}
  138. + /*eos is sent wait for vpu evnet_bufferflag to get all frames
  139. + mjpeg: sent a frame, then wait for a decoder frame
  140. + */
  141. + if((s->eos_sent && !evnet_bufferflag) || (avctx->codec_id == AV_CODEC_ID_MJPEG )) {
  142. continue;
  143. }
  144. - break;
  145. + break;
  146. }
  147. //if (!buffer)
  148. // break;
  149. @@ -924,6 +935,7 @@ static av_cold int omx_decode_end(AVCodecContext *avctx)
  150. #define OFFSET(x) offsetof(OMXCodecContext, x)
  151. #define VDE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  152. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  153. +#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  154. static const AVOption options[] = {
  155. { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  156. { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  157. @@ -980,6 +992,7 @@ AVCodec ff_h264_omx_decoder = {
  158. .capabilities = AV_CODEC_CAP_DELAY,
  159. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  160. .priv_class = &omx_h264dec_class,
  161. + .bsfs = "h264_mp4toannexb",
  162. };
  163. static const AVClass omx_hevcdec_class = {
  164. @@ -1001,4 +1014,25 @@ AVCodec ff_hevc_omx_decoder = {
  165. .capabilities = AV_CODEC_CAP_DELAY,
  166. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  167. .priv_class = &omx_hevcdec_class,
  168. + .bsfs = "hevc_mp4toannexb",
  169. +};
  170. +
  171. +static const AVClass omx_mjpegdec_class = {
  172. + .class_name = "mjpeg_omx",
  173. + .item_name = av_default_item_name,
  174. + .version = LIBAVUTIL_VERSION_INT,
  175. +};
  176. +AVCodec ff_mjpeg_omx_decoder = {
  177. + .name = "mjpeg_omx",
  178. + .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL mjpeg video decoder"),
  179. + .type = AVMEDIA_TYPE_VIDEO,
  180. + .id = AV_CODEC_ID_MJPEG,
  181. + .priv_data_size = sizeof(OMXCodecContext),
  182. + .init = omx_decode_init,
  183. + .decode = omx_decode_frame,
  184. + .close = omx_decode_end,
  185. + .capabilities = AV_CODEC_CAP_DR1,
  186. + .max_lowres = 3,
  187. + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  188. + .priv_class = &omx_mjpegdec_class,
  189. };
  190. diff --git a/libavformat/utils.c b/libavformat/utils.c
  191. index 262ff5f..4867810 100644
  192. --- a/libavformat/utils.c
  193. +++ b/libavformat/utils.c
  194. @@ -219,6 +219,13 @@ static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st,
  195. return avcodec_find_decoder_by_name("hevc");
  196. #endif
  197. +#if CONFIG_MJPEG_DECODER
  198. + /* Other parts of the code assume this decoder to be used for mjpeg,
  199. + * so force it if possible. */
  200. + if (codec_id == AV_CODEC_ID_MJPEG)
  201. + return avcodec_find_decoder_by_name("mjpeg");
  202. +#endif
  203. +
  204. codec = find_decoder(s, st, codec_id);
  205. if (!codec)
  206. return NULL;
  207. --
  208. 2.17.1