History

lavc: use a separate field for exporting audio encoder padding

Currently, the amount of padding inserted at the beginning by some audio encoders, is exported through AVCodecContext.delay. However - the term 'delay' is heavily overloaded and can have multiple different meanings even in the case of audio encoding. - this field has entirely different meanings, depending on whether the codec context is used for encoding or decoding (and has yet another different meaning for video), preventing generic handling of the codec context. Therefore, add a new field -- AVCodecContext.initial_padding. It could conceivably be used for decoding as well at a later point.

Anton Khirnov committed 12 years ago

Commit 2df0c321 parent c80a81622 files · +63 −40Browse files

Unified · Split

22 files
  1. doc/APIchanges +4 −0
  2. libavcodec/aacenc.c +1 −1
  3. libavcodec/ac3enc.c +1 −1
  4. libavcodec/audio_frame_queue.c +2 −2
  5. libavcodec/avcodec.h +18 −10
  6. libavcodec/g722enc.c +2 −2
  7. libavcodec/libfaac.c +1 −1
  8. libavcodec/libfdk-aacenc.c +1 −1
  9. libavcodec/libmp3lame.c +1 −1
  10. libavcodec/libopencore-amr.c +2 −2
  11. libavcodec/libopusenc.c +2 −2
  12. libavcodec/libspeexenc.c +1 −1
  13. libavcodec/libtwolame.c +2 −2
  14. libavcodec/libvo-aacenc.c +1 −1
  15. libavcodec/libvo-amrwbenc.c +2 −2
  16. libavcodec/libvorbis.c +2 −2
  17. libavcodec/mpegaudioenc.c +2 −2
  18. libavcodec/nellymoserenc.c +1 −1
  19. libavcodec/ra144enc.c +1 −1
  20. libavcodec/utils.c +9 −0
  21. libavcodec/version.h +5 −2
  22. libavcodec/wmaenc.c +2 −3
doc/APIchanges+4−0
@@ -13,6 +13,10 @@ libavutil: 2014-08-09 · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2014-10-13 - xxxxxxx - lavc 55.03.0 - avcodec.h
17+ Add AVCodecContext.initial_padding. Deprecate the use of AVCodecContext.delay
18+ for audio encoding.
19+
1620 2014-09-xx - xxxxxxx - lavu 54.04.0 - pixdesc.h
1721 Add API to return the name of frame and context color properties.
1822
libavcodec/aacenc.c+1−1
@@ -777,7 +777,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) · 776 unmodified lines
777777 for (i = 0; i < 428; i++)
778778 ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
779779
780− avctx->delay = 1024;
780+ avctx->initial_padding = 1024;
781781 ff_af_queue_init(avctx, &s->afq);
782782
783783 return 0;
libavcodec/ac3enc.c+1−1
@@ -2436,7 +2436,7 @@ av_cold int ff_ac3_encode_init(AVCodecContext *avctx) · 2435 unmodified lines
24362436 return ret;
24372437
24382438 avctx->frame_size = AC3_BLOCK_SIZE * s->num_blocks;
2439− avctx->delay = AC3_BLOCK_SIZE;
2439+ avctx->initial_padding = AC3_BLOCK_SIZE;
24402440
24412441 s->bitstream_mode = avctx->audio_service_type;
24422442 if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
libavcodec/audio_frame_queue.c+2−2
@@ -29,8 +29,8 @@ av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq) · 28 unmodified lines
2929 {
3030 afq->avctx = avctx;
3131 afq->next_pts = AV_NOPTS_VALUE;
32− afq->remaining_delay = avctx->delay;
33− afq->remaining_samples = avctx->delay;
32+ afq->remaining_delay = avctx->initial_padding;
33+ afq->remaining_samples = avctx->initial_padding;
3434 afq->frame_queue = NULL;
3535 }
3636
libavcodec/avcodec.h+18−10
@@ -1191,16 +1191,7 @@ typedef struct AVCodecContext { · 1190 unmodified lines
11911191 * encoded input.
11921192 *
11931193 * Audio:
1194− * For encoding, this is the number of "priming" samples added by the
1195− * encoder to the beginning of the stream. The decoded output will be
1196− * delayed by this many samples relative to the input to the encoder (or
1197− * more, if the decoder adds its own padding).
1198− * The timestamps on the output packets are adjusted by the encoder so
1199− * that they always refer to the first sample of the data actually
1200− * contained in the packet, including any added padding.
1201− * E.g. if the timebase is 1/samplerate and the timestamp of the first
1202− * input sample is 0, the timestamp of the first output packet will be
1203− * -delay.
1194+ * For encoding, this field is unused (see initial_padding).
12041195 *
12051196 * For decoding, this is the number of samples the decoder needs to
12061197 * output before the decoder's output is valid. When seeking, you should
@@ -2780,6 +2771,23 @@ typedef struct AVCodecContext { · 1573 unmodified lines
27802771 * use AVOptions to set this field.
27812772 */
27822773 int side_data_only_packets;
2774+
2775+ /**
2776+ * Audio only. The number of "priming" samples (padding) inserted by the
2777+ * encoder at the beginning of the audio. I.e. this number of leading
2778+ * decoded samples must be discarded by the caller to get the original audio
2779+ * without leading padding.
2780+ *
2781+ * - decoding: unused
2782+ * - encoding: Set by libavcodec. The timestamps on the output packets are
2783+ * adjusted by the encoder so that they always refer to the
2784+ * first sample of the data actually contained in the packet,
2785+ * including any added padding. E.g. if the timebase is
2786+ * 1/samplerate and the timestamp of the first input sample is
2787+ * 0, the timestamp of the first output packet will be
2788+ * -initial_padding.
2789+ */
2790+ int initial_padding;
27832791 } AVCodecContext;
27842792
27852793 /**
libavcodec/g722enc.c+2−2
@@ -106,7 +106,7 @@ static av_cold int g722_encode_init(AVCodecContext * avctx) · 105 unmodified lines
106106 a common packet size for VoIP applications */
107107 avctx->frame_size = 320;
108108 }
109− avctx->delay = 22;
109+ avctx->initial_padding = 22;
110110
111111 if (avctx->trellis) {
112112 /* validate trellis */
@@ -375,7 +375,7 @@ static int g722_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 262 unmodified lines
375375 }
376376
377377 if (frame->pts != AV_NOPTS_VALUE)
378− avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
378+ avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
379379 *got_packet_ptr = 1;
380380 return 0;
381381 }
libavcodec/libfaac.c+1−1
@@ -157,7 +157,7 @@ static av_cold int Faac_encode_init(AVCodecContext *avctx) · 156 unmodified lines
157157 goto error;
158158 }
159159
160− avctx->delay = FAAC_DELAY_SAMPLES;
160+ avctx->initial_padding = FAAC_DELAY_SAMPLES;
161161 ff_af_queue_init(avctx, &s->afq);
162162
163163 return 0;
libavcodec/libfdk-aacenc.c+1−1
@@ -286,7 +286,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) · 285 unmodified lines
286286 }
287287
288288 avctx->frame_size = info.frameLength;
289− avctx->delay = info.encoderDelay;
289+ avctx->initial_padding = info.encoderDelay;
290290 ff_af_queue_init(avctx, &s->afq);
291291
292292 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
libavcodec/libmp3lame.c+1−1
@@ -137,7 +137,7 @@ static av_cold int mp3lame_encode_init(AVCodecContext *avctx) · 136 unmodified lines
137137 }
138138
139139 /* get encoder delay */
140− avctx->delay = lame_get_encoder_delay(s->gfp) + 528 + 1;
140+ avctx->initial_padding = lame_get_encoder_delay(s->gfp) + 528 + 1;
141141 ff_af_queue_init(avctx, &s->afq);
142142
143143 avctx->frame_size = lame_get_framesize(s->gfp);
libavcodec/libopencore-amr.c+2−2
@@ -200,7 +200,7 @@ static av_cold int amr_nb_encode_init(AVCodecContext *avctx) · 199 unmodified lines
200200 }
201201
202202 avctx->frame_size = 160;
203− avctx->delay = 50;
203+ avctx->initial_padding = 50;
204204 ff_af_queue_init(avctx, &s->afq);
205205
206206 s->enc_state = Encoder_Interface_init(s->enc_dtx);
@@ -250,7 +250,7 @@ static int amr_nb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 43 unmodified lines
250250 return AVERROR(ENOMEM);
251251 memcpy(flush_buf, samples, frame->nb_samples * sizeof(*flush_buf));
252252 samples = flush_buf;
253− if (frame->nb_samples < avctx->frame_size - avctx->delay)
253+ if (frame->nb_samples < avctx->frame_size - avctx->initial_padding)
254254 s->enc_last_frame = -1;
255255 }
256256 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) {
libavcodec/libopusenc.c+2−2
@@ -87,7 +87,7 @@ static void libopus_write_header(AVCodecContext *avctx, int stream_count, · 86 unmodified lines
8787 bytestream_put_buffer(&p, "OpusHead", 8);
8888 bytestream_put_byte(&p, 1); /* Version */
8989 bytestream_put_byte(&p, channels);
90− bytestream_put_le16(&p, avctx->delay); /* Lookahead samples at 48kHz */
90+ bytestream_put_le16(&p, avctx->initial_padding); /* Lookahead samples at 48kHz */
9191 bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
9292 bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
9393
@@ -277,7 +277,7 @@ static int av_cold libopus_encode_init(AVCodecContext *avctx) · 183 unmodified lines
277277 goto fail;
278278 }
279279
280− ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->delay));
280+ ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding));
281281 if (ret != OPUS_OK)
282282 av_log(avctx, AV_LOG_WARNING,
283283 "Unable to get number of lookahead samples: %s\n",
libavcodec/libspeexenc.c+1−1
@@ -235,7 +235,7 @@ static av_cold int encode_init(AVCodecContext *avctx) · 234 unmodified lines
235235 s->header.frames_per_packet = s->frames_per_packet;
236236
237237 /* set encoding delay */
238− speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->delay);
238+ speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->initial_padding);
239239 ff_af_queue_init(avctx, &s->afq);
240240
241241 /* create header packet bytes from header struct */
libavcodec/libtwolame.c+2−2
@@ -60,7 +60,7 @@ static av_cold int twolame_encode_init(AVCodecContext *avctx) · 59 unmodified lines
6060 int ret;
6161
6262 avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME;
63− avctx->delay = 512 - 32 + 1;
63+ avctx->initial_padding = 512 - 32 + 1;
6464
6565 s->glopts = twolame_init();
6666 if (!s->glopts)
@@ -151,7 +151,7 @@ static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 84 unmodified lines
151151 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
152152 if (frame) {
153153 if (frame->pts != AV_NOPTS_VALUE)
154− avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
154+ avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
155155 } else {
156156 avpkt->pts = s->next_pts;
157157 }
libavcodec/libvo-aacenc.c+1−1
@@ -61,7 +61,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) · 60 unmodified lines
6161 int index, ret;
6262
6363 avctx->frame_size = FRAME_SIZE;
64− avctx->delay = ENC_DELAY;
64+ avctx->initial_padding = ENC_DELAY;
6565 s->last_frame = 2;
6666 ff_af_queue_init(avctx, &s->afq);
6767
libavcodec/libvo-amrwbenc.c+2−2
@@ -93,7 +93,7 @@ static av_cold int amr_wb_encode_init(AVCodecContext *avctx) · 92 unmodified lines
9393 s->last_bitrate = avctx->bit_rate;
9494
9595 avctx->frame_size = 320;
96− avctx->delay = 80;
96+ avctx->initial_padding = 80;
9797
9898 s->state = E_IF_init();
9999
@@ -131,7 +131,7 @@ static int amr_wb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 31 unmodified lines
131131 }
132132
133133 if (frame->pts != AV_NOPTS_VALUE)
134− avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
134+ avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
135135
136136 avpkt->size = size;
137137 *got_packet_ptr = 1;
libavcodec/libvorbis.c+2−2
@@ -322,8 +322,8 @@ static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 321 unmodified lines
322322 if (duration > 0) {
323323 /* we do not know encoder delay until we get the first packet from
324324 * libvorbis, so we have to update the AudioFrameQueue counts */
325− if (!avctx->delay) {
326− avctx->delay = duration;
325+ if (!avctx->initial_padding) {
326+ avctx->initial_padding = duration;
327327 s->afq.remaining_delay += duration;
328328 s->afq.remaining_samples += duration;
329329 }
libavcodec/mpegaudioenc.c+2−2
@@ -84,7 +84,7 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) · 83 unmodified lines
8484 bitrate = bitrate / 1000;
8585 s->nb_channels = channels;
8686 avctx->frame_size = MPA_FRAME_SIZE;
87− avctx->delay = 512 - 32 + 1;
87+ avctx->initial_padding = 512 - 32 + 1;
8888
8989 /* encoding freq */
9090 s->lsf = 0;
@@ -735,7 +735,7 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 644 unmodified lines
735735 encode_frame(s, bit_alloc, padding);
736736
737737 if (frame->pts != AV_NOPTS_VALUE)
738− avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
738+ avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
739739
740740 avpkt->size = put_bits_count(&s->pb) / 8;
741741 *got_packet_ptr = 1;
libavcodec/nellymoserenc.c+1−1
@@ -165,7 +165,7 @@ static av_cold int encode_init(AVCodecContext *avctx) · 164 unmodified lines
165165 }
166166
167167 avctx->frame_size = NELLY_SAMPLES;
168− avctx->delay = NELLY_BUF_LEN;
168+ avctx->initial_padding = NELLY_BUF_LEN;
169169 ff_af_queue_init(avctx, &s->afq);
170170 s->avctx = avctx;
171171 if ((ret = ff_mdct_init(&s->mdct_ctx, 8, 0, 32768.0)) < 0)
libavcodec/ra144enc.c+1−1
@@ -56,7 +56,7 @@ static av_cold int ra144_encode_init(AVCodecContext * avctx) · 55 unmodified lines
5656 return -1;
5757 }
5858 avctx->frame_size = NBLOCKS * BLOCKSIZE;
59− avctx->delay = avctx->frame_size;
59+ avctx->initial_padding = avctx->frame_size;
6060 avctx->bit_rate = 8000;
6161 ractx = avctx->priv_data;
6262 ractx->lpc_coef[0] = ractx->lpc_tables[0];
libavcodec/utils.c+9−0
@@ -1240,6 +1240,11 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code · 1239 unmodified lines
12401240 }
12411241 }
12421242
1243+#if FF_API_AUDIOENC_DELAY
1244+ if (av_codec_is_encoder(avctx->codec))
1245+ avctx->delay = avctx->initial_padding;
1246+#endif
1247+
12431248 if (av_codec_is_decoder(avctx->codec)) {
12441249 /* validate channel layout from the decoder */
12451250 if (avctx->channel_layout) {
@@ -1447,6 +1452,10 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, · 201 unmodified lines
14471452 end:
14481453 av_frame_free(&padded_frame);
14491454
1455+#if FF_API_AUDIOENC_DELAY
1456+ avctx->delay = avctx->initial_padding;
1457+#endif
1458+
14501459 return ret;
14511460 }
14521461
libavcodec/version.h+5−2
@@ -29,8 +29,8 @@ · 28 unmodified lines
2929 #include "libavutil/version.h"
3030
3131 #define LIBAVCODEC_VERSION_MAJOR 56
32−#define LIBAVCODEC_VERSION_MINOR 2
33−#define LIBAVCODEC_VERSION_MICRO 2
32+#define LIBAVCODEC_VERSION_MINOR 3
33+#define LIBAVCODEC_VERSION_MICRO 0
3434
3535 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3636 LIBAVCODEC_VERSION_MINOR, \
@@ -153,5 +153,8 @@ · 116 unmodified lines
153153 #ifndef FF_API_AFD
154154 #define FF_API_AFD (LIBAVCODEC_VERSION_MAJOR < 57)
155155 #endif
156+#ifndef FF_API_AUDIOENC_DELAY
157+#define FF_API_AUDIOENC_DELAY (LIBAVCODEC_VERSION_MAJOR < 58)
158+#endif
156159
157160 #endif /* AVCODEC_VERSION_H */
libavcodec/wmaenc.c+2−3
@@ -92,8 +92,7 @@ static av_cold int encode_init(AVCodecContext *avctx) · 91 unmodified lines
9292 avctx->block_align = block_align;
9393 avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate /
9494 s->frame_len;
95− avctx->frame_size =
96− avctx->delay = s->frame_len;
95+ avctx->frame_size = avctx->initial_padding = s->frame_len;
9796
9897 return 0;
9998 }
@@ -420,7 +419,7 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt, · 320 unmodified lines
420419 flush_put_bits(&s->pb);
421420
422421 if (frame->pts != AV_NOPTS_VALUE)
423− avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
422+ avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
424423
425424 avpkt->size = avctx->block_align;
426425 *got_packet_ptr = 1;

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