History

avcodec/encode: Enable encoders to control padding of last frame

Some audio codecs work with atomic units that decode to a fixed number of audio samples with this number being so small that it is common to put multiple of these atoms into one packet. In these cases it makes no sense to pad the last frame to the big frame_size, so allow encoders to set the number of samples that they want the last frame to be padded to instead.

Andreas Rheinhardt committed 4 years ago

Commit 017d9761 parent dc7b6642 files · +17 −6Browse files

Unified · Split

2 files
  1. libavcodec/encode.c +11 −6
  2. libavcodec/internal.h +6 −0
libavcodec/encode.c+11−6
@@ -127,12 +127,12 @@ static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt) · 126 unmodified lines
127127 /**
128128 * Pad last frame with silence.
129129 */
130−static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
130+static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
131131 {
132132 int ret;
133133
134134 frame->format = src->format;
135− frame->nb_samples = s->frame_size;
135+ frame->nb_samples = out_samples;
136136 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
137137 if (ret < 0)
138138 goto fail;
@@ -406,10 +406,15 @@ static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src) · 267 unmodified lines
406406 if (src->nb_samples < avctx->frame_size) {
407407 avctx->internal->last_audio_frame = 1;
408408 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
409− ret = pad_last_frame(avctx, dst, src);
410− if (ret < 0)
411− return ret;
412− goto finish;
409+ int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
410+ int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
411+
412+ if (out_samples != src->nb_samples) {
413+ ret = pad_last_frame(avctx, dst, src, out_samples);
414+ if (ret < 0)
415+ return ret;
416+ goto finish;
417+ }
413418 }
414419 }
415420 }
libavcodec/internal.h+6−0
@@ -62,6 +62,12 @@ typedef struct AVCodecInternal { · 61 unmodified lines
6262 */
6363 int last_audio_frame;
6464
65+ /**
66+ * Audio encoders can set this flag during init to indicate that they
67+ * want the small last frame to be padded to a multiple of pad_samples.
68+ */
69+ int pad_samples;
70+
6571 AVBufferRef *pool;
6672
6773 void *thread_ctx;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t