0006-add-hevc-decoder-and-encoder-support.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. From 0adf789b7c3060e241b82e6afdfc2f7975f1ee39 Mon Sep 17 00:00:00 2001
  2. From: "sw.multimedia" <se.multimedia@starfivetech.com>
  3. Date: Wed, 25 May 2022 20:28:21 +0800
  4. Subject: [PATCH 2/8] add hevc decoder and encoder support
  5. Signed-off-by: sw.multimedia <se.multimedia@starfivetech.com>
  6. ---
  7. configure | 2 ++
  8. libavcodec/allcodecs.c | 2 ++
  9. libavcodec/omx.c | 35 +++++++++++++++++++++++++++++++++++
  10. libavcodec/omxdec.c | 35 ++++++++++++++++++++++++++++++++++-
  11. libavformat/utils.c | 7 +++++++
  12. 5 files changed, 80 insertions(+), 1 deletion(-)
  13. --- a/configure
  14. +++ b/configure
  15. @@ -3185,6 +3185,8 @@
  16. h264_v4l2m2m_decoder_deps="v4l2_m2m h264_v4l2_m2m"
  17. 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. hevc_amf_encoder_deps="amf"
  22. hevc_cuvid_decoder_deps="cuvid"
  23. hevc_cuvid_decoder_select="hevc_mp4toannexb_bsf"
  24. --- a/libavcodec/allcodecs.c
  25. +++ b/libavcodec/allcodecs.c
  26. @@ -824,6 +824,8 @@
  27. extern const FFCodec ff_h264_nvenc_encoder;
  28. extern const FFCodec ff_h264_omx_encoder;
  29. extern const FFCodec ff_h264_omx_decoder;
  30. +extern const FFCodec ff_hevc_omx_encoder;
  31. +extern const FFCodec ff_hevc_omx_decoder;
  32. extern const FFCodec ff_h264_qsv_encoder;
  33. extern const FFCodec ff_h264_v4l2m2m_encoder;
  34. extern const FFCodec ff_h264_vaapi_encoder;
  35. --- a/libavcodec/omx.c
  36. +++ b/libavcodec/omx.c
  37. @@ -44,7 +44,8 @@
  38. #include "codec_internal.h"
  39. #include "h264.h"
  40. #include "pthread_internal.h"
  41. -
  42. +#include "profiles.h"
  43. +
  44. #ifdef OMX_SKIP64BIT
  45. static OMX_TICKS to_omx_ticks(int64_t value)
  46. {
  47. @@ -508,6 +509,8 @@
  48. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
  49. else if (avctx->codec->id == AV_CODEC_ID_H264)
  50. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
  51. + else if (avctx->codec->id == AV_CODEC_ID_HEVC)
  52. + out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingHEVC;
  53. err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
  54. CHECK(err);
  55. @@ -666,6 +669,9 @@
  56. case AV_CODEC_ID_H264:
  57. role = "video_encoder.avc";
  58. break;
  59. + case AV_CODEC_ID_HEVC:
  60. + role = "video_encoder.hevc";
  61. + break;
  62. default:
  63. return AVERROR(ENOSYS);
  64. }
  65. @@ -926,6 +932,13 @@
  66. { NULL }
  67. };
  68. +static const AVOption options_hevc[] = {
  69. + { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  70. + { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  71. + { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1, VE },
  72. + { NULL },
  73. +};
  74. +
  75. static const enum AVPixelFormat omx_encoder_pix_fmts[] = {
  76. AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
  77. };
  78. @@ -971,3 +984,25 @@
  79. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  80. .p.priv_class = &omx_h264enc_class,
  81. };
  82. +
  83. +static const AVClass omx_hevcenc_class = {
  84. + .class_name = "hevc_omx",
  85. + .item_name = av_default_item_name,
  86. + .option = options_hevc,
  87. + .version = LIBAVUTIL_VERSION_INT,
  88. +};
  89. +AVCodec ff_hevc_omx_encoder = {
  90. + .name = "hevc_omx",
  91. + .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL HEVC video encoder"),
  92. + .type = AVMEDIA_TYPE_VIDEO,
  93. + .id = AV_CODEC_ID_HEVC,
  94. + .priv_data_size = sizeof(OMXCodecContext),
  95. + .init = omx_encode_init,
  96. + .encode2 = omx_encode_frame,
  97. + .close = omx_encode_end,
  98. + .pix_fmts = omx_encoder_pix_fmts,
  99. + .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
  100. + .capabilities = AV_CODEC_CAP_DELAY,
  101. + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  102. + .priv_class = &omx_hevcenc_class,
  103. +};
  104. --- a/libavcodec/omxdec.c
  105. +++ b/libavcodec/omxdec.c
  106. @@ -44,6 +44,7 @@
  107. #include "avcodec.h"
  108. #include "h264.h"
  109. #include "internal.h"
  110. +#include "profiles.h"
  111. #ifdef OMX_SKIP64BIT
  112. static OMX_TICKS to_omx_ticks(int64_t value)
  113. @@ -494,6 +495,8 @@
  114. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
  115. else if (avctx->codec->id == AV_CODEC_ID_H264)
  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. err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
  120. CHECK(err);
  121. @@ -627,6 +630,9 @@
  122. case AV_CODEC_ID_H264:
  123. role = "video_decoder.avc";
  124. break;
  125. + case AV_CODEC_ID_HEVC:
  126. + role = "video_decoder.hevc";
  127. + break;
  128. default:
  129. return AVERROR(ENOSYS);
  130. }
  131. @@ -733,7 +739,7 @@
  132. buffer->nFilledLen = pkt->size;
  133. }
  134. - /* reduce memcpy. point it addr*/
  135. + /* avoid memcpy. point it addr*/
  136. //buffer->pAppPrivate = pkt;
  137. //buffer->pBuffer = pkt->data;
  138. //buffer->nFilledLen = pkt->size;
  139. @@ -851,6 +857,12 @@
  140. { NULL }
  141. };
  142. +static const AVOption options_hevc[] = {
  143. + { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  144. + { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  145. + { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1, VE },
  146. + { NULL },
  147. +};
  148. static const AVClass omx_mpeg4dec_class = {
  149. .class_name = "mpeg4_omx",
  150. @@ -891,3 +903,24 @@
  151. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  152. .priv_class = &omx_h264dec_class,
  153. };
  154. +
  155. +static const AVClass omx_hevcdec_class = {
  156. + .class_name = "hevc_omx",
  157. + .item_name = av_default_item_name,
  158. + .option = options_hevc,
  159. + .version = LIBAVUTIL_VERSION_INT,
  160. +};
  161. +AVCodec ff_hevc_omx_decoder = {
  162. + .name = "hevc_omx",
  163. + .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL HEVC video decoder"),
  164. + .type = AVMEDIA_TYPE_VIDEO,
  165. + .id = AV_CODEC_ID_HEVC,
  166. + .priv_data_size = sizeof(OMXCodecContext),
  167. + .init = omx_decode_init,
  168. + .decode = omx_decode_frame,
  169. + .close = omx_decode_end,
  170. + .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
  171. + .capabilities = AV_CODEC_CAP_DELAY,
  172. + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  173. + .priv_class = &omx_hevcdec_class,
  174. +};
  175. --- a/libavformat/demux.c
  176. +++ b/libavformat/demux.c
  177. @@ -77,6 +77,13 @@
  178. return avcodec_find_decoder_by_name("h264");
  179. #endif
  180. +#if CONFIG_HEVC_DECODER
  181. + /* Other parts of the code assume this decoder to be used for h265,
  182. + * so force it if possible. */
  183. + if (codec_id == AV_CODEC_ID_HEVC)
  184. + return avcodec_find_decoder_by_name("hevc");
  185. +#endif
  186. +
  187. codec = ff_find_decoder(s, st, codec_id);
  188. if (!codec)
  189. return NULL;