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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. diff --git a/configure b/configure
  14. index e3976df..80be074 100755
  15. --- a/configure
  16. +++ b/configure
  17. @@ -3093,6 +3093,8 @@ h264_vaapi_encoder_select="cbs_h264 vaapi_encode"
  18. h264_v4l2m2m_decoder_deps="v4l2_m2m h264_v4l2_m2m"
  19. h264_v4l2m2m_decoder_select="h264_mp4toannexb_bsf"
  20. h264_v4l2m2m_encoder_deps="v4l2_m2m h264_v4l2_m2m"
  21. +hevc_omx_encoder_deps="omx"
  22. +hevc_omx_decoder_deps="omx"
  23. hevc_amf_encoder_deps="amf"
  24. hevc_cuvid_decoder_deps="cuvid"
  25. hevc_cuvid_decoder_select="hevc_mp4toannexb_bsf"
  26. diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
  27. index e42fa30..0c7a18d 100644
  28. --- a/libavcodec/allcodecs.c
  29. +++ b/libavcodec/allcodecs.c
  30. @@ -793,6 +793,8 @@ extern AVCodec ff_h264_mf_encoder;
  31. extern AVCodec ff_h264_nvenc_encoder;
  32. extern AVCodec ff_h264_omx_encoder;
  33. extern AVCodec ff_h264_omx_decoder;
  34. +extern AVCodec ff_hevc_omx_encoder;
  35. +extern AVCodec ff_hevc_omx_decoder;
  36. extern AVCodec ff_h264_qsv_encoder;
  37. extern AVCodec ff_h264_v4l2m2m_encoder;
  38. extern AVCodec ff_h264_vaapi_encoder;
  39. diff --git a/libavcodec/omx.c b/libavcodec/omx.c
  40. index 0a6a308..86e32a8 100644
  41. --- a/libavcodec/omx.c
  42. +++ b/libavcodec/omx.c
  43. @@ -43,6 +43,7 @@
  44. #include "avcodec.h"
  45. #include "h264.h"
  46. #include "internal.h"
  47. +#include "profiles.h"
  48. #ifdef OMX_SKIP64BIT
  49. static OMX_TICKS to_omx_ticks(int64_t value)
  50. @@ -501,6 +502,8 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  51. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
  52. else if (avctx->codec->id == AV_CODEC_ID_H264)
  53. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
  54. + else if (avctx->codec->id == AV_CODEC_ID_HEVC)
  55. + out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingHEVC;
  56. err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
  57. CHECK(err);
  58. @@ -666,6 +669,9 @@ static av_cold int omx_encode_init(AVCodecContext *avctx)
  59. case AV_CODEC_ID_H264:
  60. role = "video_encoder.avc";
  61. break;
  62. + case AV_CODEC_ID_HEVC:
  63. + role = "video_encoder.hevc";
  64. + break;
  65. default:
  66. return AVERROR(ENOSYS);
  67. }
  68. @@ -941,6 +947,13 @@ static const AVOption options[] = {
  69. { NULL }
  70. };
  71. +static const AVOption options_hevc[] = {
  72. + { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  73. + { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  74. + { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1, VE },
  75. + { NULL },
  76. +};
  77. +
  78. static const enum AVPixelFormat omx_encoder_pix_fmts[] = {
  79. AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
  80. };
  81. @@ -986,3 +999,25 @@ AVCodec ff_h264_omx_encoder = {
  82. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  83. .priv_class = &omx_h264enc_class,
  84. };
  85. +
  86. +static const AVClass omx_hevcenc_class = {
  87. + .class_name = "hevc_omx",
  88. + .item_name = av_default_item_name,
  89. + .option = options_hevc,
  90. + .version = LIBAVUTIL_VERSION_INT,
  91. +};
  92. +AVCodec ff_hevc_omx_encoder = {
  93. + .name = "hevc_omx",
  94. + .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL HEVC video encoder"),
  95. + .type = AVMEDIA_TYPE_VIDEO,
  96. + .id = AV_CODEC_ID_HEVC,
  97. + .priv_data_size = sizeof(OMXCodecContext),
  98. + .init = omx_encode_init,
  99. + .encode2 = omx_encode_frame,
  100. + .close = omx_encode_end,
  101. + .pix_fmts = omx_encoder_pix_fmts,
  102. + .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
  103. + .capabilities = AV_CODEC_CAP_DELAY,
  104. + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  105. + .priv_class = &omx_hevcenc_class,
  106. +};
  107. diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
  108. index 7f76ec3..96a2829 100644
  109. --- a/libavcodec/omxdec.c
  110. +++ b/libavcodec/omxdec.c
  111. @@ -44,6 +44,7 @@
  112. #include "avcodec.h"
  113. #include "h264.h"
  114. #include "internal.h"
  115. +#include "profiles.h"
  116. #ifdef OMX_SKIP64BIT
  117. static OMX_TICKS to_omx_ticks(int64_t value)
  118. @@ -494,6 +495,8 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  119. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
  120. else if (avctx->codec->id == AV_CODEC_ID_H264)
  121. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
  122. + else if (avctx->codec->id == AV_CODEC_ID_HEVC)
  123. + out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingHEVC;
  124. err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
  125. CHECK(err);
  126. @@ -627,6 +630,9 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
  127. case AV_CODEC_ID_H264:
  128. role = "video_decoder.avc";
  129. break;
  130. + case AV_CODEC_ID_HEVC:
  131. + role = "video_decoder.hevc";
  132. + break;
  133. default:
  134. return AVERROR(ENOSYS);
  135. }
  136. @@ -733,7 +739,7 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
  137. buffer->nFilledLen = pkt->size;
  138. }
  139. - /* reduce memcpy. point it addr*/
  140. + /* avoid memcpy. point it addr*/
  141. //buffer->pAppPrivate = pkt;
  142. //buffer->pBuffer = pkt->data;
  143. //buffer->nFilledLen = pkt->size;
  144. @@ -851,6 +857,12 @@ static const AVOption options[] = {
  145. { NULL }
  146. };
  147. +static const AVOption options_hevc[] = {
  148. + { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  149. + { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  150. + { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1, VE },
  151. + { NULL },
  152. +};
  153. static const AVClass omx_mpeg4dec_class = {
  154. .class_name = "mpeg4_omx",
  155. @@ -891,3 +903,24 @@ AVCodec ff_h264_omx_decoder = {
  156. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  157. .priv_class = &omx_h264dec_class,
  158. };
  159. +
  160. +static const AVClass omx_hevcdec_class = {
  161. + .class_name = "hevc_omx",
  162. + .item_name = av_default_item_name,
  163. + .option = options_hevc,
  164. + .version = LIBAVUTIL_VERSION_INT,
  165. +};
  166. +AVCodec ff_hevc_omx_decoder = {
  167. + .name = "hevc_omx",
  168. + .long_name = NULL_IF_CONFIG_SMALL("OpenMAX IL HEVC video decoder"),
  169. + .type = AVMEDIA_TYPE_VIDEO,
  170. + .id = AV_CODEC_ID_HEVC,
  171. + .priv_data_size = sizeof(OMXCodecContext),
  172. + .init = omx_decode_init,
  173. + .decode = omx_decode_frame,
  174. + .close = omx_decode_end,
  175. + .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
  176. + .capabilities = AV_CODEC_CAP_DELAY,
  177. + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  178. + .priv_class = &omx_hevcdec_class,
  179. +};
  180. diff --git a/libavformat/utils.c b/libavformat/utils.c
  181. index 75e5350..262ff5f 100644
  182. --- a/libavformat/utils.c
  183. +++ b/libavformat/utils.c
  184. @@ -212,6 +212,13 @@ static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st,
  185. return avcodec_find_decoder_by_name("h264");
  186. #endif
  187. +#if CONFIG_HEVC_DECODER
  188. + /* Other parts of the code assume this decoder to be used for h265,
  189. + * so force it if possible. */
  190. + if (codec_id == AV_CODEC_ID_HEVC)
  191. + return avcodec_find_decoder_by_name("hevc");
  192. +#endif
  193. +
  194. codec = find_decoder(s, st, codec_id);
  195. if (!codec)
  196. return NULL;
  197. --
  198. 2.17.1