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
127/**127/**
128 * Pad last frame with silence.128 * Pad last frame with silence.
129 */129 */
130static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)130static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
131{131{
132 int ret;132 int ret;
133 133
134 frame->format = src->format;134 frame->format = src->format;
135 frame->nb_samples = s->frame_size;135 frame->nb_samples = out_samples;
136 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);136 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
137 if (ret < 0)137 if (ret < 0)
138 goto fail;138 goto fail;
@@ -406,10 +406,15 @@ static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src) · 267 unmodified lines
406 if (src->nb_samples < avctx->frame_size) {406 if (src->nb_samples < avctx->frame_size) {
407 avctx->internal->last_audio_frame = 1;407 avctx->internal->last_audio_frame = 1;
408 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {408 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
409 ret = pad_last_frame(avctx, dst, src);409 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
410 if (ret < 0)410 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
411 return ret;411
412 goto finish;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 }
413 }418 }
414 }419 }
415 }420 }
libavcodec/internal.h+6−0
@@ -62,6 +62,12 @@ typedef struct AVCodecInternal { · 61 unmodified lines
62 */62 */
63 int last_audio_frame;63 int last_audio_frame;
64 64
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
65 AVBufferRef *pool;71 AVBufferRef *pool;
66 72
67 void *thread_ctx;73 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