0004-videotoolbox-changing-bitrate.patch 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. From d74de94b49efcf7a0b25673ace6016938d1b9272 Mon Sep 17 00:00:00 2001
  2. From: 21pages <sunboeasy@gmail.com>
  3. Date: Tue, 10 Dec 2024 14:12:01 +0800
  4. Subject: [PATCH 3/5] videotoolbox changing bitrate
  5. Signed-off-by: 21pages <sunboeasy@gmail.com>
  6. ---
  7. libavcodec/videotoolboxenc.c | 40 ++++++++++++++++++++++++++++++++++++
  8. 1 file changed, 40 insertions(+)
  9. diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
  10. index da7b291b03..3c866177f5 100644
  11. --- a/libavcodec/videotoolboxenc.c
  12. +++ b/libavcodec/videotoolboxenc.c
  13. @@ -279,6 +279,8 @@ typedef struct VTEncContext {
  14. int max_slice_bytes;
  15. int power_efficient;
  16. int max_ref_frames;
  17. +
  18. + int last_bit_rate;
  19. } VTEncContext;
  20. static void vtenc_free_buf_node(BufNode *info)
  21. @@ -1180,6 +1182,7 @@ static int vtenc_create_encoder(AVCodecContext *avctx,
  22. int64_t one_second_value = 0;
  23. void *nums[2];
  24. + vtctx->last_bit_rate = bit_rate;
  25. int status = VTCompressionSessionCreate(kCFAllocatorDefault,
  26. avctx->width,
  27. avctx->height,
  28. @@ -2638,6 +2641,42 @@ out:
  29. return status;
  30. }
  31. +static void update_config(AVCodecContext *avctx)
  32. +{
  33. + VTEncContext *vtctx = avctx->priv_data;
  34. +
  35. + if (avctx->codec_id != AV_CODEC_ID_PRORES) {
  36. + if (avctx->bit_rate != vtctx->last_bit_rate) {
  37. + av_log(avctx, AV_LOG_INFO, "Setting bit rate to %d\n", avctx->bit_rate);
  38. + vtctx->last_bit_rate = avctx->bit_rate;
  39. + SInt32 bit_rate = avctx->bit_rate;
  40. + CFNumberRef bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
  41. + kCFNumberSInt32Type,
  42. + &bit_rate);
  43. + if (!bit_rate_num) return;
  44. +
  45. + if (vtctx->constant_bit_rate) {
  46. + int status = VTSessionSetProperty(vtctx->session,
  47. + compat_keys.kVTCompressionPropertyKey_ConstantBitRate,
  48. + bit_rate_num);
  49. + if (status == kVTPropertyNotSupportedErr) {
  50. + av_log(avctx, AV_LOG_ERROR, "Error: -constant_bit_rate true is not supported by the encoder.\n");
  51. + }
  52. + } else {
  53. + int status = VTSessionSetProperty(vtctx->session,
  54. + kVTCompressionPropertyKey_AverageBitRate,
  55. + bit_rate_num);
  56. + if (status) {
  57. + av_log(avctx, AV_LOG_ERROR, "Error: cannot set average bit rate: %d\n", status);
  58. + }
  59. + }
  60. +
  61. + CFRelease(bit_rate_num);
  62. + }
  63. + }
  64. +}
  65. +
  66. +
  67. static av_cold int vtenc_frame(
  68. AVCodecContext *avctx,
  69. AVPacket *pkt,
  70. @@ -2650,6 +2689,7 @@ static av_cold int vtenc_frame(
  71. CMSampleBufferRef buf = NULL;
  72. ExtraSEI sei = {0};
  73. + update_config(avctx);
  74. if (frame) {
  75. status = vtenc_send_frame(avctx, vtctx, frame);
  76. --
  77. 2.43.0.windows.1