diff mbox series

[FFmpeg-devel,2/2] avcodec/amd: fix pict_type, match it with amf & ffmpeg. add sps pps when frame->keyframe

Message ID tencent_96E76966496F26A4A8420E92D7B875D18506@qq.com
State New
Headers show
Series [FFmpeg-devel,1/2] avcodec/qsv: add sps pps header when frame->key_frame is true | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

reito Nov. 16, 2023, 4:06 a.m. UTC
The frame->key_frame currently doesn't set extra header into frames when
using amf encoders.
The frame->pict_type is not set to amf picture type, now we force it.

Signed-off-by: reito <cnschwarzer@qq.com>
---
 libavcodec/amfenc.c | 52 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_SKIP);
+                    break;
+                default:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE,
AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_NONE);
+                    break;
+            }
+            // Keyframe overrides previous assignment.
+            if (frame->key_frame) {
+                av_log(ctx, AV_LOG_DEBUG, "IDR,PPS and SPS set - h265\n");
+                AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_HEVC_INSERT_HEADER, 1);
+                AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE,
AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_IDR);
+            }
+        }
+
         // submit surface
         res = ctx->encoder->pVtbl->SubmitInput(ctx->encoder,
(AMFData*)surface);
         if (res == AMF_INPUT_FULL) { // handle full queue

Comments

Dmitrii Ovchinnikov Nov. 30, 2023, 10:58 a.m. UTC | #1
The idea itself looks good to me, but the patch is corrupted.

Regarding the patch:
1)
>>// Keyframe overrides previous assignment.
It might be better instead of:
setting the frame type -> if key frame then overwrite
to do:
check for key frame and if it's not, set other PICTURE_TYPE
to avoid unnecessary overwriting
(use AV_FRAME_FLAG_KEY instead of deprecated key_frame,
as already noted under the second patch)
2) The patch includes logic for AV_CODEC_ID_H264 and AV_CODEC_ID_HEVC,
but not for AV_CODEC_ID_AV1.( FORCE_INSERT_SEQUENCE_HEADER
 AMF_VIDEO_ENCODER_AV1_FORCE_FRAME_TYPE )
diff mbox series

Patch

diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index 061859f85c..076fecb760 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -696,6 +696,58 @@  int ff_amf_receive_packet(AVCodecContext *avctx,
AVPacket *avpkt)
             break;
         }
 
+        // Override Picture Type for Frame
+        if (avctx->codec->id == AV_CODEC_ID_H264) {
+            switch (frame->pict_type) {
+                case AV_PICTURE_TYPE_I:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_I);
+                    break;
+                case AV_PICTURE_TYPE_P:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_P);
+                    break;
+                case AV_PICTURE_TYPE_B:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_B);
+                    break;
+                case AV_PICTURE_TYPE_S:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_SKIP);
+                    break;
+                default:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_NONE);
+                    break;
+            }
+            // Keyframe overrides previous assignment.
+            if (frame->key_frame) {
+                av_log(ctx, AV_LOG_DEBUG, "IDR,AUD,PPS and SPS set -
h264\n");
+                AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_INSERT_SPS, 1);
+                AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_INSERT_PPS, 1);
+                AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
+            }
+        } else if (avctx->codec->id == AV_CODEC_ID_HEVC) {
+            switch (frame->pict_type) {
+                case AV_PICTURE_TYPE_I:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE,
AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_I);
+                    break;
+                case AV_PICTURE_TYPE_P:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE,
AMF_VIDEO_ENCODER_HEVC_PICTURE_TYPE_P);
+                    break;
+                case AV_PICTURE_TYPE_B:
+                    av_log(ctx, AV_LOG_WARNING, "Ignoring B-Frame,
unsupported by AMD AMF H.265 Encoder.");
+                    break;
+                case AV_PICTURE_TYPE_S:
+                    AMF_ASSIGN_PROPERTY_INT64(res, surface,
AMF_VIDEO_ENCODER_HEVC_FORCE_PICTURE_TYPE,