0014-ffmpeg-add-pixel-foramt.patch 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. From 474232dff26df2188fe37287aeb7834f6d3853d1 Mon Sep 17 00:00:00 2001
  2. From: "arvin.zhu" <arvin.zhu@starfivetech.com>
  3. Date: Thu, 29 Sep 2022 16:05:09 +0800
  4. Subject: [PATCH] ffmpeg: add pixel foramt
  5. add pixel format option for codecs of VPU
  6. Signed-off-by: arvin.zhu <arvin.zhu@starfivetech.com>
  7. ---
  8. libavcodec/omx.c | 19 ++++++++++++++++---
  9. libavcodec/omxdec.c | 43 +++++++++++++++++++++++++++++++++++++------
  10. 2 files changed, 53 insertions(+), 9 deletions(-)
  11. diff --git a/libavcodec/omx.c b/libavcodec/omx.c
  12. index 1c4179f..6976884 100755
  13. --- a/libavcodec/omx.c
  14. +++ b/libavcodec/omx.c
  15. @@ -456,18 +456,31 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  16. break;
  17. }
  18. }
  19. +
  20. if (s->color_format == 0) {
  21. av_log(avctx, AV_LOG_ERROR, "No supported pixel formats (%d formats available)\n", i);
  22. return AVERROR_UNKNOWN;
  23. }
  24. -
  25. + av_log(avctx, AV_LOG_VERBOSE, "OMX setting pixel_format:%s\n", av_get_pix_fmt_name(avctx->pix_fmt));
  26. in_port_params.bEnabled = OMX_TRUE;
  27. in_port_params.bPopulated = OMX_FALSE;
  28. in_port_params.eDomain = OMX_PortDomainVideo;
  29. in_port_params.format.video.pNativeRender = NULL;
  30. in_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
  31. - in_port_params.format.video.eColorFormat = s->color_format;
  32. + switch (avctx->pix_fmt) {
  33. + case AV_PIX_FMT_NV12:
  34. + in_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  35. + break;
  36. + case AV_PIX_FMT_NV21:
  37. + in_port_params.format.video.eColorFormat = OMX_COLOR_FormatYVU420SemiPlanar;
  38. + break;
  39. + case AV_PIX_FMT_YUV420P:
  40. + out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  41. + break;
  42. + default:
  43. + return AVERROR_OPTION_NOT_FOUND;
  44. + }
  45. s->stride = avctx->width;
  46. s->plane_size = avctx->height;
  47. // If specific codecs need to manually override the stride/plane_size,
  48. @@ -986,7 +999,7 @@ static const AVOption options_hevc[] = {
  49. };
  50. static const enum AVPixelFormat omx_encoder_pix_fmts[] = {
  51. - AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
  52. + AV_PIX_FMT_YUV420P, AV_PIX_FMT_NV12, AV_PIX_FMT_NV21, AV_PIX_FMT_NONE
  53. };
  54. static const AVClass omx_mpeg4enc_class = {
  55. diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
  56. index 20298b8..074ff4a 100755
  57. --- a/libavcodec/omxdec.c
  58. +++ b/libavcodec/omxdec.c
  59. @@ -481,7 +481,7 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  60. //OMX_VIDEO_PARAM_BITRATETYPE vid_param_bitrate = { 0 };
  61. OMX_ERRORTYPE err;
  62. int i;
  63. -
  64. +av_log(avctx, AV_LOG_VERBOSE, "OMX avctx pixel_format:%s\n", av_get_pix_fmt_name(avctx->pix_fmt));
  65. s->version.s.nVersionMajor = 1;
  66. s->version.s.nVersionMinor = 1;
  67. s->version.s.nRevision = 2;
  68. @@ -571,16 +571,46 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  69. out_port_params.format.video.nBitrate = avctx->bit_rate;
  70. out_port_params.format.video.xFramerate = in_port_params.format.video.xFramerate;
  71. out_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
  72. + av_log(avctx, AV_LOG_VERBOSE, "OMX setting pixel_format:%s\n", s->pixel_format);
  73. if (avctx->codec->id == AV_CODEC_ID_MPEG4)
  74. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
  75. - else if (avctx->codec->id == AV_CODEC_ID_H264)
  76. + else if (avctx->codec->id == AV_CODEC_ID_H264) {
  77. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
  78. - else if (avctx->codec->id == AV_CODEC_ID_HEVC)
  79. +
  80. + switch (av_get_pix_fmt(s->pixel_format)) {
  81. + case AV_PIX_FMT_NV12:
  82. + out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  83. + break;
  84. + case AV_PIX_FMT_NV21:
  85. + out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYVU420SemiPlanar;
  86. + break;
  87. + case AV_PIX_FMT_YUV420P:
  88. + out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  89. + break;
  90. + default:
  91. + return AVERROR_OPTION_NOT_FOUND;
  92. + }
  93. + }
  94. + else if (avctx->codec->id == AV_CODEC_ID_HEVC) {
  95. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingHEVC;
  96. +
  97. + switch (av_get_pix_fmt(s->pixel_format)) {
  98. + case AV_PIX_FMT_NV12:
  99. + out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  100. + break;
  101. + case AV_PIX_FMT_NV21:
  102. + out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYVU420SemiPlanar;
  103. + break;
  104. + case AV_PIX_FMT_YUV420P:
  105. + out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  106. + break;
  107. + default:
  108. + return AVERROR_OPTION_NOT_FOUND;
  109. + }
  110. + }
  111. else if (avctx->codec->id == AV_CODEC_ID_MJPEG){
  112. out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingMJPEG;
  113. - av_log(avctx, AV_LOG_VERBOSE, "OMX_pixel_format:%s\n", s->pixel_format);
  114. switch (av_get_pix_fmt(s->pixel_format)) {
  115. case AV_PIX_FMT_NV12:
  116. out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  117. @@ -610,7 +640,7 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
  118. out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV444Planar;
  119. break;
  120. default:
  121. - out_port_params.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  122. + return AVERROR_OPTION_NOT_FOUND;
  123. }
  124. }
  125. @@ -991,6 +1021,7 @@ static const AVOption options[] = {
  126. { "baseline", "", 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_H264_BASELINE }, 0, 0, VE, "profile" },
  127. { "main", "", 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_H264_MAIN }, 0, 0, VE, "profile" },
  128. { "high", "", 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_H264_HIGH }, 0, 0, VE, "profile" },
  129. + { "omx_pix_fmt", "Set the decoding pixel format for h264_omx decoder. The following formats are supported: yuv420p, nv12, nv21.", OFFSET(pixel_format), AV_OPT_TYPE_STRING, { .str = "yuv420p" }, 0, 0, VD },
  130. { NULL }
  131. };
  132. @@ -998,6 +1029,7 @@ static const AVOption options_hevc[] = {
  133. { "omx_libname", "OpenMAX library name", OFFSET(libname), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  134. { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  135. { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1, VE },
  136. + { "omx_pix_fmt", "Set the decoding pixel format for hevc_omx decoder. The following formats are supported: yuv420p, nv12, nv21.", OFFSET(pixel_format), AV_OPT_TYPE_STRING, { .str = "yuv420p" }, 0, 0, VD },
  137. { NULL },
  138. };
  139. @@ -1006,7 +1038,6 @@ static const AVOption options_mjpeg[] = {
  140. { "omx_libprefix", "OpenMAX library prefix", OFFSET(libprefix), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VDE },
  141. { "zerocopy", "Try to avoid copying input frames if possible", OFFSET(input_zerocopy), AV_OPT_TYPE_INT, { .i64 = CONFIG_OMX_RPI }, 0, 1, VD },
  142. { "omx_pix_fmt", "Set the decoding pixel format for mjpeg_omx decoder. The following formats are supported: yuv420p, nv12, nv21, nv16, yuv422p, yuyv422, yvyu422, uyvy422, yuv444p.", OFFSET(pixel_format), AV_OPT_TYPE_STRING, { .str = "yuv420p" }, 0, 0, VD },
  143. -
  144. { NULL },
  145. };
  146. --
  147. 2.17.1