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.
Unified · Split
| @@ -13,6 +13,10 @@ libavutil: 2014-08-09 · 12 unmodified lines | ||
| 13 | 13 | |
| 14 | 14 | API changes, most recent first: |
| 15 | 15 | |
| 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 | + | |
| 16 | 20 | 2014-09-xx - xxxxxxx - lavu 54.04.0 - pixdesc.h |
| 17 | 21 | Add API to return the name of frame and context color properties. |
| 18 | 22 | |
| @@ -777,7 +777,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) · 776 unmodified lines | ||
| 777 | 777 | for (i = 0; i < 428; i++) |
| 778 | 778 | ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i])); |
| 779 | 779 | |
| 780 | − avctx->delay = 1024; | |
| 780 | + avctx->initial_padding = 1024; | |
| 781 | 781 | ff_af_queue_init(avctx, &s->afq); |
| 782 | 782 | |
| 783 | 783 | return 0; |
| @@ -2436,7 +2436,7 @@ av_cold int ff_ac3_encode_init(AVCodecContext *avctx) · 2435 unmodified lines | ||
| 2436 | 2436 | return ret; |
| 2437 | 2437 | |
| 2438 | 2438 | avctx->frame_size = AC3_BLOCK_SIZE * s->num_blocks; |
| 2439 | − avctx->delay = AC3_BLOCK_SIZE; | |
| 2439 | + avctx->initial_padding = AC3_BLOCK_SIZE; | |
| 2440 | 2440 | |
| 2441 | 2441 | s->bitstream_mode = avctx->audio_service_type; |
| 2442 | 2442 | if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE) |
| @@ -29,8 +29,8 @@ av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq) · 28 unmodified lines | ||
| 29 | 29 | { |
| 30 | 30 | afq->avctx = avctx; |
| 31 | 31 | 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; | |
| 34 | 34 | afq->frame_queue = NULL; |
| 35 | 35 | } |
| 36 | 36 | |
| @@ -1191,16 +1191,7 @@ typedef struct AVCodecContext { · 1190 unmodified lines | ||
| 1191 | 1191 | * encoded input. |
| 1192 | 1192 | * |
| 1193 | 1193 | * 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). | |
| 1204 | 1195 | * |
| 1205 | 1196 | * For decoding, this is the number of samples the decoder needs to |
| 1206 | 1197 | * output before the decoder's output is valid. When seeking, you should |
| @@ -2780,6 +2771,23 @@ typedef struct AVCodecContext { · 1573 unmodified lines | ||
| 2780 | 2771 | * use AVOptions to set this field. |
| 2781 | 2772 | */ |
| 2782 | 2773 | 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; | |
| 2783 | 2791 | } AVCodecContext; |
| 2784 | 2792 | |
| 2785 | 2793 | /** |
| @@ -106,7 +106,7 @@ static av_cold int g722_encode_init(AVCodecContext * avctx) · 105 unmodified lines | ||
| 106 | 106 | a common packet size for VoIP applications */ |
| 107 | 107 | avctx->frame_size = 320; |
| 108 | 108 | } |
| 109 | − avctx->delay = 22; | |
| 109 | + avctx->initial_padding = 22; | |
| 110 | 110 | |
| 111 | 111 | if (avctx->trellis) { |
| 112 | 112 | /* validate trellis */ |
| @@ -375,7 +375,7 @@ static int g722_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 262 unmodified lines | ||
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | 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); | |
| 379 | 379 | *got_packet_ptr = 1; |
| 380 | 380 | return 0; |
| 381 | 381 | } |
| @@ -157,7 +157,7 @@ static av_cold int Faac_encode_init(AVCodecContext *avctx) · 156 unmodified lines | ||
| 157 | 157 | goto error; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | − avctx->delay = FAAC_DELAY_SAMPLES; | |
| 160 | + avctx->initial_padding = FAAC_DELAY_SAMPLES; | |
| 161 | 161 | ff_af_queue_init(avctx, &s->afq); |
| 162 | 162 | |
| 163 | 163 | return 0; |
| @@ -286,7 +286,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) · 285 unmodified lines | ||
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | avctx->frame_size = info.frameLength; |
| 289 | − avctx->delay = info.encoderDelay; | |
| 289 | + avctx->initial_padding = info.encoderDelay; | |
| 290 | 290 | ff_af_queue_init(avctx, &s->afq); |
| 291 | 291 | |
| 292 | 292 | if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { |
| @@ -137,7 +137,7 @@ static av_cold int mp3lame_encode_init(AVCodecContext *avctx) · 136 unmodified lines | ||
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | /* 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; | |
| 141 | 141 | ff_af_queue_init(avctx, &s->afq); |
| 142 | 142 | |
| 143 | 143 | avctx->frame_size = lame_get_framesize(s->gfp); |
| @@ -200,7 +200,7 @@ static av_cold int amr_nb_encode_init(AVCodecContext *avctx) · 199 unmodified lines | ||
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | avctx->frame_size = 160; |
| 203 | − avctx->delay = 50; | |
| 203 | + avctx->initial_padding = 50; | |
| 204 | 204 | ff_af_queue_init(avctx, &s->afq); |
| 205 | 205 | |
| 206 | 206 | 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 | ||
| 250 | 250 | return AVERROR(ENOMEM); |
| 251 | 251 | memcpy(flush_buf, samples, frame->nb_samples * sizeof(*flush_buf)); |
| 252 | 252 | samples = flush_buf; |
| 253 | − if (frame->nb_samples < avctx->frame_size - avctx->delay) | |
| 253 | + if (frame->nb_samples < avctx->frame_size - avctx->initial_padding) | |
| 254 | 254 | s->enc_last_frame = -1; |
| 255 | 255 | } |
| 256 | 256 | if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) { |
| @@ -87,7 +87,7 @@ static void libopus_write_header(AVCodecContext *avctx, int stream_count, · 86 unmodified lines | ||
| 87 | 87 | bytestream_put_buffer(&p, "OpusHead", 8); |
| 88 | 88 | bytestream_put_byte(&p, 1); /* Version */ |
| 89 | 89 | 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 */ | |
| 91 | 91 | bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */ |
| 92 | 92 | bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */ |
| 93 | 93 | |
| @@ -277,7 +277,7 @@ static int av_cold libopus_encode_init(AVCodecContext *avctx) · 183 unmodified lines | ||
| 277 | 277 | goto fail; |
| 278 | 278 | } |
| 279 | 279 | |
| 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)); | |
| 281 | 281 | if (ret != OPUS_OK) |
| 282 | 282 | av_log(avctx, AV_LOG_WARNING, |
| 283 | 283 | "Unable to get number of lookahead samples: %s\n", |
| @@ -235,7 +235,7 @@ static av_cold int encode_init(AVCodecContext *avctx) · 234 unmodified lines | ||
| 235 | 235 | s->header.frames_per_packet = s->frames_per_packet; |
| 236 | 236 | |
| 237 | 237 | /* 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); | |
| 239 | 239 | ff_af_queue_init(avctx, &s->afq); |
| 240 | 240 | |
| 241 | 241 | /* create header packet bytes from header struct */ |
| @@ -60,7 +60,7 @@ static av_cold int twolame_encode_init(AVCodecContext *avctx) · 59 unmodified lines | ||
| 60 | 60 | int ret; |
| 61 | 61 | |
| 62 | 62 | avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME; |
| 63 | − avctx->delay = 512 - 32 + 1; | |
| 63 | + avctx->initial_padding = 512 - 32 + 1; | |
| 64 | 64 | |
| 65 | 65 | s->glopts = twolame_init(); |
| 66 | 66 | if (!s->glopts) |
| @@ -151,7 +151,7 @@ static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 84 unmodified lines | ||
| 151 | 151 | avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); |
| 152 | 152 | if (frame) { |
| 153 | 153 | 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); | |
| 155 | 155 | } else { |
| 156 | 156 | avpkt->pts = s->next_pts; |
| 157 | 157 | } |
| @@ -61,7 +61,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx) · 60 unmodified lines | ||
| 61 | 61 | int index, ret; |
| 62 | 62 | |
| 63 | 63 | avctx->frame_size = FRAME_SIZE; |
| 64 | − avctx->delay = ENC_DELAY; | |
| 64 | + avctx->initial_padding = ENC_DELAY; | |
| 65 | 65 | s->last_frame = 2; |
| 66 | 66 | ff_af_queue_init(avctx, &s->afq); |
| 67 | 67 | |
| @@ -93,7 +93,7 @@ static av_cold int amr_wb_encode_init(AVCodecContext *avctx) · 92 unmodified lines | ||
| 93 | 93 | s->last_bitrate = avctx->bit_rate; |
| 94 | 94 | |
| 95 | 95 | avctx->frame_size = 320; |
| 96 | − avctx->delay = 80; | |
| 96 | + avctx->initial_padding = 80; | |
| 97 | 97 | |
| 98 | 98 | s->state = E_IF_init(); |
| 99 | 99 | |
| @@ -131,7 +131,7 @@ static int amr_wb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 31 unmodified lines | ||
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | 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); | |
| 135 | 135 | |
| 136 | 136 | avpkt->size = size; |
| 137 | 137 | *got_packet_ptr = 1; |
| @@ -322,8 +322,8 @@ static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 321 unmodified lines | ||
| 322 | 322 | if (duration > 0) { |
| 323 | 323 | /* we do not know encoder delay until we get the first packet from |
| 324 | 324 | * 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; | |
| 327 | 327 | s->afq.remaining_delay += duration; |
| 328 | 328 | s->afq.remaining_samples += duration; |
| 329 | 329 | } |
| @@ -84,7 +84,7 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) · 83 unmodified lines | ||
| 84 | 84 | bitrate = bitrate / 1000; |
| 85 | 85 | s->nb_channels = channels; |
| 86 | 86 | avctx->frame_size = MPA_FRAME_SIZE; |
| 87 | − avctx->delay = 512 - 32 + 1; | |
| 87 | + avctx->initial_padding = 512 - 32 + 1; | |
| 88 | 88 | |
| 89 | 89 | /* encoding freq */ |
| 90 | 90 | s->lsf = 0; |
| @@ -735,7 +735,7 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 644 unmodified lines | ||
| 735 | 735 | encode_frame(s, bit_alloc, padding); |
| 736 | 736 | |
| 737 | 737 | 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); | |
| 739 | 739 | |
| 740 | 740 | avpkt->size = put_bits_count(&s->pb) / 8; |
| 741 | 741 | *got_packet_ptr = 1; |
| @@ -165,7 +165,7 @@ static av_cold int encode_init(AVCodecContext *avctx) · 164 unmodified lines | ||
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | avctx->frame_size = NELLY_SAMPLES; |
| 168 | − avctx->delay = NELLY_BUF_LEN; | |
| 168 | + avctx->initial_padding = NELLY_BUF_LEN; | |
| 169 | 169 | ff_af_queue_init(avctx, &s->afq); |
| 170 | 170 | s->avctx = avctx; |
| 171 | 171 | if ((ret = ff_mdct_init(&s->mdct_ctx, 8, 0, 32768.0)) < 0) |
| @@ -56,7 +56,7 @@ static av_cold int ra144_encode_init(AVCodecContext * avctx) · 55 unmodified lines | ||
| 56 | 56 | return -1; |
| 57 | 57 | } |
| 58 | 58 | avctx->frame_size = NBLOCKS * BLOCKSIZE; |
| 59 | − avctx->delay = avctx->frame_size; | |
| 59 | + avctx->initial_padding = avctx->frame_size; | |
| 60 | 60 | avctx->bit_rate = 8000; |
| 61 | 61 | ractx = avctx->priv_data; |
| 62 | 62 | ractx->lpc_coef[0] = ractx->lpc_tables[0]; |
| @@ -1240,6 +1240,11 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code · 1239 unmodified lines | ||
| 1240 | 1240 | } |
| 1241 | 1241 | } |
| 1242 | 1242 | |
| 1243 | +#if FF_API_AUDIOENC_DELAY | |
| 1244 | + if (av_codec_is_encoder(avctx->codec)) | |
| 1245 | + avctx->delay = avctx->initial_padding; | |
| 1246 | +#endif | |
| 1247 | + | |
| 1243 | 1248 | if (av_codec_is_decoder(avctx->codec)) { |
| 1244 | 1249 | /* validate channel layout from the decoder */ |
| 1245 | 1250 | if (avctx->channel_layout) { |
| @@ -1447,6 +1452,10 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, · 201 unmodified lines | ||
| 1447 | 1452 | end: |
| 1448 | 1453 | av_frame_free(&padded_frame); |
| 1449 | 1454 | |
| 1455 | +#if FF_API_AUDIOENC_DELAY | |
| 1456 | + avctx->delay = avctx->initial_padding; | |
| 1457 | +#endif | |
| 1458 | + | |
| 1450 | 1459 | return ret; |
| 1451 | 1460 | } |
| 1452 | 1461 | |
| @@ -29,8 +29,8 @@ · 28 unmodified lines | ||
| 29 | 29 | #include "libavutil/version.h" |
| 30 | 30 | |
| 31 | 31 | #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 | |
| 34 | 34 | |
| 35 | 35 | #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ |
| 36 | 36 | LIBAVCODEC_VERSION_MINOR, \ |
| @@ -153,5 +153,8 @@ · 116 unmodified lines | ||
| 153 | 153 | #ifndef FF_API_AFD |
| 154 | 154 | #define FF_API_AFD (LIBAVCODEC_VERSION_MAJOR < 57) |
| 155 | 155 | #endif |
| 156 | +#ifndef FF_API_AUDIOENC_DELAY | |
| 157 | +#define FF_API_AUDIOENC_DELAY (LIBAVCODEC_VERSION_MAJOR < 58) | |
| 158 | +#endif | |
| 156 | 159 | |
| 157 | 160 | #endif /* AVCODEC_VERSION_H */ |
| @@ -92,8 +92,7 @@ static av_cold int encode_init(AVCodecContext *avctx) · 91 unmodified lines | ||
| 92 | 92 | avctx->block_align = block_align; |
| 93 | 93 | avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate / |
| 94 | 94 | s->frame_len; |
| 95 | − avctx->frame_size = | |
| 96 | − avctx->delay = s->frame_len; | |
| 95 | + avctx->frame_size = avctx->initial_padding = s->frame_len; | |
| 97 | 96 | |
| 98 | 97 | return 0; |
| 99 | 98 | } |
| @@ -420,7 +419,7 @@ static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt, · 320 unmodified lines | ||
| 420 | 419 | flush_put_bits(&s->pb); |
| 421 | 420 | |
| 422 | 421 | 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); | |
| 424 | 423 | |
| 425 | 424 | avpkt->size = avctx->block_align; |
| 426 | 425 | *got_packet_ptr = 1; |