0002-libavcodec-amfenc-reconfig-when-bitrate-change.patch 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. From 8d061adb7b00fc765b8001307c025437ef1cad88 Mon Sep 17 00:00:00 2001
  2. From: 21pages <sunboeasy@gmail.com>
  3. Date: Thu, 5 Sep 2024 16:32:16 +0800
  4. Subject: [PATCH 2/5] libavcodec/amfenc: reconfig when bitrate change
  5. Signed-off-by: 21pages <sunboeasy@gmail.com>
  6. ---
  7. libavcodec/amfenc.c | 20 ++++++++++++++++++++
  8. libavcodec/amfenc.h | 1 +
  9. 2 files changed, 21 insertions(+)
  10. diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
  11. index a47aea6108..f70f0109f6 100644
  12. --- a/libavcodec/amfenc.c
  13. +++ b/libavcodec/amfenc.c
  14. @@ -275,6 +275,7 @@ static int amf_init_context(AVCodecContext *avctx)
  15. ctx->hwsurfaces_in_queue = 0;
  16. ctx->hwsurfaces_in_queue_max = 16;
  17. + ctx->av_bitrate = avctx->bit_rate;
  18. // configure AMF logger
  19. // the return of these functions indicates old state and do not affect behaviour
  20. @@ -640,6 +641,23 @@ static void amf_release_buffer_with_frame_ref(AMFBuffer *frame_ref_storage_buffe
  21. frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
  22. }
  23. +static int reconfig_encoder(AVCodecContext *avctx)
  24. +{
  25. + AmfContext *ctx = avctx->priv_data;
  26. + AMF_RESULT res = AMF_OK;
  27. +
  28. + if (ctx->av_bitrate != avctx->bit_rate) {
  29. + av_log(ctx, AV_LOG_INFO, "change bitrate from %d to %d\n", ctx->av_bitrate, avctx->bit_rate);
  30. + ctx->av_bitrate = avctx->bit_rate;
  31. + if (avctx->codec->id == AV_CODEC_ID_H264) {
  32. + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_TARGET_BITRATE, avctx->bit_rate);
  33. + } else if (avctx->codec->id == AV_CODEC_ID_HEVC) {
  34. + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE, avctx->bit_rate);
  35. + }
  36. + }
  37. + return 0;
  38. +}
  39. +
  40. int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  41. {
  42. AmfContext *ctx = avctx->priv_data;
  43. @@ -653,6 +671,8 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  44. int query_output_data_flag = 0;
  45. AMF_RESULT res_resubmit;
  46. + reconfig_encoder(avctx);
  47. +
  48. if (!ctx->encoder)
  49. return AVERROR(EINVAL);
  50. diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h
  51. index 320c66919e..481e0fb75d 100644
  52. --- a/libavcodec/amfenc.h
  53. +++ b/libavcodec/amfenc.h
  54. @@ -115,6 +115,7 @@ typedef struct AmfContext {
  55. int max_b_frames;
  56. int qvbr_quality_level;
  57. int hw_high_motion_quality_boost;
  58. + int64_t av_bitrate;
  59. // HEVC - specific options
  60. --
  61. 2.43.0.windows.1