[PATCH] libavcodec/libfdk-aacnc: send encoder delay/padding in packet side data
--- libavcodec/libfdk-aacenc.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c index 54549de473..55d10990e4 100644 --- a/libavcodec/libfdk-aacenc.c +++ b/libavcodec/libfdk-aacenc.c @@ -21,6 +21,7 @@ #include "libavutil/channel_layout.h" #include "libavutil/common.h" +#include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "avcodec.h" #include "audio_frame_queue.h" @@ -46,6 +47,7 @@ typedef struct AACContext { int latm; int header_period; int vbr; + int delay_sent; AudioFrameQueue afq; } AACContext; @@ -368,7 +370,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, int out_buffer_identifier = OUT_BITSTREAM_DATA; int out_buffer_size, out_buffer_element_size; void *in_ptr, *out_ptr; - int ret; + int ret, discard_padding; uint8_t dummy_buf[1]; AACENC_ERROR err; @@ -428,6 +430,29 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, &avpkt->duration); + discard_padding = avctx->frame_size - avpkt->duration; + // Check if subtraction resulted in an overflow + if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) { + av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n"); + av_packet_unref(avpkt); + av_free(avpkt); + return AVERROR(EINVAL); + } + if ((!s->delay_sent && avctx->initial_padding > 0) || discard_padding > 0) { + uint8_t *side_data = + av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10); + if (!side_data) { + av_packet_unref(avpkt); + av_free(avpkt); + return AVERROR(ENOMEM); + } + if (!s->delay_sent) { + AV_WL32(side_data, avctx->initial_padding); + s->delay_sent = 1; + } + AV_WL32(side_data + 4, discard_padding); + } + avpkt->size = out_args.numOutBytes; *got_packet_ptr = 1; return 0; -- 2.39.2.637.g21b0678d19-goog
On Fri, Feb 24, 2023 at 3:14 PM JonHGee <jonhgee@gmail.com> wrote: --- libavcodec/libfdk-aacenc.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c index 54549de473..55d10990e4 100644 --- a/libavcodec/libfdk-aacenc.c +++ b/libavcodec/libfdk-aacenc.c @@ -21,6 +21,7 @@ #include "libavutil/channel_layout.h" #include "libavutil/common.h" +#include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "avcodec.h" #include "audio_frame_queue.h" @@ -46,6 +47,7 @@ typedef struct AACContext { int latm; int header_period; int vbr; + int delay_sent; AudioFrameQueue afq; } AACContext; @@ -368,7 +370,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, int out_buffer_identifier = OUT_BITSTREAM_DATA; int out_buffer_size, out_buffer_element_size; void *in_ptr, *out_ptr; - int ret; + int ret, discard_padding; uint8_t dummy_buf[1]; AACENC_ERROR err; @@ -428,6 +430,29 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, &avpkt->duration); + discard_padding = avctx->frame_size - avpkt->duration; + // Check if subtraction resulted in an overflow + if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) { + av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n"); + av_packet_unref(avpkt); + av_free(avpkt); + return AVERROR(EINVAL); + } + if ((!s->delay_sent && avctx->initial_padding > 0) || discard_padding > 0) { + uint8_t *side_data = + av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10); + if (!side_data) { + av_packet_unref(avpkt); + av_free(avpkt); + return AVERROR(ENOMEM); + } + if (!s->delay_sent) { + AV_WL32(side_data, avctx->initial_padding); + s->delay_sent = 1; + } + AV_WL32(side_data + 4, discard_padding); + } + avpkt->size = out_args.numOutBytes; *got_packet_ptr = 1; return 0; -- 2.39.2.637.g21b0678d19-goog
Ping?
On Fri, 24 Feb 2023, JonHGee wrote:
--- libavcodec/libfdk-aacenc.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c index 54549de473..55d10990e4 100644 --- a/libavcodec/libfdk-aacenc.c +++ b/libavcodec/libfdk-aacenc.c @@ -21,6 +21,7 @@
#include "libavutil/channel_layout.h" #include "libavutil/common.h" +#include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "avcodec.h" #include "audio_frame_queue.h" @@ -46,6 +47,7 @@ typedef struct AACContext { int latm; int header_period; int vbr; + int delay_sent;
AudioFrameQueue afq; } AACContext; @@ -368,7 +370,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, int out_buffer_identifier = OUT_BITSTREAM_DATA; int out_buffer_size, out_buffer_element_size; void *in_ptr, *out_ptr; - int ret; + int ret, discard_padding; uint8_t dummy_buf[1]; AACENC_ERROR err;
@@ -428,6 +430,29 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, &avpkt->duration);
+ discard_padding = avctx->frame_size - avpkt->duration; + // Check if subtraction resulted in an overflow + if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) { + av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n"); + av_packet_unref(avpkt); + av_free(avpkt);
I don't think we can or should free the avpkt pointer here (which is allocated by the caller).
+ return AVERROR(EINVAL); + } + if ((!s->delay_sent && avctx->initial_padding > 0) || discard_padding > 0) { + uint8_t *side_data = + av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10); + if (!side_data) { + av_packet_unref(avpkt); + av_free(avpkt); + return AVERROR(ENOMEM); + } + if (!s->delay_sent) { + AV_WL32(side_data, avctx->initial_padding); + s->delay_sent = 1; + } + AV_WL32(side_data + 4, discard_padding); + } +
Other than that, this seems reasonable. There's a bunch of stylistic choices that I'd do slightly differently here, but this seems to match existing code in libavcodec/libmp3lame.c, so it's probably ok to keep it matching. // Martin
Signed-off-by: JonHGee <JonHGee@gmail.com> --- libavcodec/libfdk-aacenc.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c index 54549de473..954ddab07f 100644 --- a/libavcodec/libfdk-aacenc.c +++ b/libavcodec/libfdk-aacenc.c @@ -21,6 +21,7 @@ #include "libavutil/channel_layout.h" #include "libavutil/common.h" +#include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "avcodec.h" #include "audio_frame_queue.h" @@ -46,6 +47,7 @@ typedef struct AACContext { int latm; int header_period; int vbr; + int delay_sent; AudioFrameQueue afq; } AACContext; @@ -368,7 +370,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, int out_buffer_identifier = OUT_BITSTREAM_DATA; int out_buffer_size, out_buffer_element_size; void *in_ptr, *out_ptr; - int ret; + int ret, discard_padding; uint8_t dummy_buf[1]; AACENC_ERROR err; @@ -428,6 +430,27 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, &avpkt->duration); + discard_padding = avctx->frame_size - avpkt->duration; + // Check if subtraction resulted in an overflow + if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) { + av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n"); + av_packet_unref(avpkt); + return AVERROR(EINVAL); + } + if ((!s->delay_sent && avctx->initial_padding > 0) || discard_padding > 0) { + uint8_t *side_data = + av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10); + if (!side_data) { + av_packet_unref(avpkt); + return AVERROR(ENOMEM); + } + if (!s->delay_sent) { + AV_WL32(side_data, avctx->initial_padding); + s->delay_sent = 1; + } + AV_WL32(side_data + 4, discard_padding); + } + avpkt->size = out_args.numOutBytes; *got_packet_ptr = 1; return 0; -- 2.39.2.722.g9855ee24e9-goog
On Tue, 28 Feb 2023, JonHGee wrote:
Signed-off-by: JonHGee <JonHGee@gmail.com> --- libavcodec/libfdk-aacenc.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c index 54549de473..954ddab07f 100644 --- a/libavcodec/libfdk-aacenc.c +++ b/libavcodec/libfdk-aacenc.c @@ -21,6 +21,7 @@
#include "libavutil/channel_layout.h" #include "libavutil/common.h" +#include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "avcodec.h" #include "audio_frame_queue.h" @@ -46,6 +47,7 @@ typedef struct AACContext { int latm; int header_period; int vbr; + int delay_sent;
AudioFrameQueue afq; } AACContext; @@ -368,7 +370,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, int out_buffer_identifier = OUT_BITSTREAM_DATA; int out_buffer_size, out_buffer_element_size; void *in_ptr, *out_ptr; - int ret; + int ret, discard_padding; uint8_t dummy_buf[1]; AACENC_ERROR err;
@@ -428,6 +430,27 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts, &avpkt->duration);
+ discard_padding = avctx->frame_size - avpkt->duration; + // Check if subtraction resulted in an overflow + if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) { + av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n"); + av_packet_unref(avpkt); + return AVERROR(EINVAL); + } + if ((!s->delay_sent && avctx->initial_padding > 0) || discard_padding > 0) { + uint8_t *side_data = + av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10); + if (!side_data) { + av_packet_unref(avpkt); + return AVERROR(ENOMEM); + } + if (!s->delay_sent) { + AV_WL32(side_data, avctx->initial_padding); + s->delay_sent = 1; + } + AV_WL32(side_data + 4, discard_padding); + } + avpkt->size = out_args.numOutBytes; *got_packet_ptr = 1; return 0; -- 2.39.2.722.g9855ee24e9-goog
LGTM, pushed. // Martin
participants (3)
-
Jonathan Gee -
JonHGee -
Martin Storsjö