History

lavc: pad last audio frame with silence when needed.

Anton Khirnov committed 14 years ago

Commit a5117a21 parent 6d7f6176 files · +80 −16Browse files

Unified · Split

6 files
  1. avconv.c +0 −8
  2. doc/APIchanges +6 −0
  3. libavcodec/avcodec.h +3 −7
  4. libavcodec/internal.h +6 −0
  5. libavcodec/utils.c +64 −0
  6. libavcodec/version.h +1 −1
avconv.c+0−8
@@ -2048,14 +2048,6 @@ static void flush_encoders(void) · 2047 unmodified lines
20482048
20492049 av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
20502050
2051− /* pad last frame with silence if needed */
2052− if (!(enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME)) {
2053− frame_bytes = enc->frame_size * enc->channels *
2054− av_get_bytes_per_sample(enc->sample_fmt);
2055− if (allocated_audio_buf_size < frame_bytes)
2056− exit_program(1);
2057− generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes);
2058− }
20592051 encode_audio_frame(os, ost, audio_buf, frame_bytes);
20602052 } else {
20612053 /* flush encoder with NULL frames until it is done
doc/APIchanges+6−0
@@ -13,6 +13,12 @@ libavutil: 2011-04-18 · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2012-xx-xx - xxxxxxx - lavc 54.13.1
17+ For audio formats with fixed frame size, the last frame
18+ no longer needs to be padded with silence, libavcodec
19+ will handle this internally (effectively all encoders
20+ behave as if they had CODEC_CAP_SMALL_LAST_FRAME set).
21+
1622 2012-xx-xx - xxxxxxx - lavc 54.13.0 - avcodec.h
1723 Add sample_rate and channel_layout fields to AVFrame.
1824
libavcodec/avcodec.h+3−7
@@ -3860,15 +3860,11 @@ int attribute_deprecated avcodec_encode_audio(AVCodecContext *avctx, · 3859 unmodified lines
38603860 * @param[in] frame AVFrame containing the raw audio data to be encoded.
38613861 * May be NULL when flushing an encoder that has the
38623862 * CODEC_CAP_DELAY capability set.
3863− * There are 2 codec capabilities that affect the allowed
3864− * values of frame->nb_samples.
3865− * If CODEC_CAP_SMALL_LAST_FRAME is set, then only the final
3866− * frame may be smaller than avctx->frame_size, and all other
3867− * frames must be equal to avctx->frame_size.
38683863 * If CODEC_CAP_VARIABLE_FRAME_SIZE is set, then each frame
38693864 * can have any number of samples.
3870− * If neither is set, frame->nb_samples must be equal to
3871− * avctx->frame_size for all frames.
3865+ * If it is not set, frame->nb_samples must be equal to
3866+ * avctx->frame_size for all frames except the last.
3867+ * The final frame may be smaller than avctx->frame_size.
38723868 * @param[out] got_packet_ptr This field is set to 1 by libavcodec if the
38733869 * output packet is non-empty, and to 0 if it is
38743870 * empty. If the function returns an error, the
libavcodec/internal.h+6−0
@@ -70,6 +70,12 @@ typedef struct AVCodecInternal { · 69 unmodified lines
7070 */
7171 int sample_count;
7272 #endif
73+
74+ /**
75+ * An audio frame with less than required samples has been submitted and
76+ * padded with silence. Reject all subsequent frames.
77+ */
78+ int last_audio_frame;
7379 } AVCodecInternal;
7480
7581 struct AVCodecDefault {
libavcodec/utils.c+64−0
@@ -857,11 +857,58 @@ int ff_alloc_packet(AVPacket *avpkt, int size) · 856 unmodified lines
857857 }
858858 }
859859
860+/**
861+ * Pad last frame with silence.
862+ */
863+static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
864+{
865+ AVFrame *frame = NULL;
866+ uint8_t *buf = NULL;
867+ int ret;
868+
869+ if (!(frame = avcodec_alloc_frame()))
870+ return AVERROR(ENOMEM);
871+ *frame = *src;
872+
873+ if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
874+ s->frame_size, s->sample_fmt, 0)) < 0)
875+ goto fail;
876+
877+ if (!(buf = av_malloc(ret))) {
878+ ret = AVERROR(ENOMEM);
879+ goto fail;
880+ }
881+
882+ frame->nb_samples = s->frame_size;
883+ if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
884+ buf, ret, 0)) < 0)
885+ goto fail;
886+ if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
887+ src->nb_samples, s->channels, s->sample_fmt)) < 0)
888+ goto fail;
889+ if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
890+ frame->nb_samples - src->nb_samples,
891+ s->channels, s->sample_fmt)) < 0)
892+ goto fail;
893+
894+ *dst = frame;
895+
896+ return 0;
897+
898+fail:
899+ if (frame->extended_data != frame->data)
900+ av_freep(&frame->extended_data);
901+ av_freep(&buf);
902+ av_freep(&frame);
903+ return ret;
904+}
905+
860906 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
861907 AVPacket *avpkt,
862908 const AVFrame *frame,
863909 int *got_packet_ptr)
864910 {
911+ AVFrame *padded_frame = NULL;
865912 int ret;
866913 int user_packet = !!avpkt->data;
867914
@@ -879,6 +926,16 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, · 11 unmodified lines
879926 if (frame->nb_samples > avctx->frame_size)
880927 return AVERROR(EINVAL);
881928 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
929+ if (frame->nb_samples < avctx->frame_size &&
930+ !avctx->internal->last_audio_frame) {
931+ ret = pad_last_frame(avctx, &padded_frame, frame);
932+ if (ret < 0)
933+ return ret;
934+
935+ frame = padded_frame;
936+ avctx->internal->last_audio_frame = 1;
937+ }
938+
882939 if (frame->nb_samples != avctx->frame_size)
883940 return AVERROR(EINVAL);
884941 }
@@ -919,6 +976,13 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx, · 34 unmodified lines
919976 here to simplify things */
920977 avpkt->flags |= AV_PKT_FLAG_KEY;
921978
979+ if (padded_frame) {
980+ av_freep(&padded_frame->data[0]);
981+ if (padded_frame->extended_data != padded_frame->data)
982+ av_freep(&padded_frame->extended_data);
983+ av_freep(&padded_frame);
984+ }
985+
922986 return ret;
923987 }
924988
libavcodec/version.h+1−1
@@ -28,7 +28,7 @@ · 27 unmodified lines
2828
2929 #define LIBAVCODEC_VERSION_MAJOR 54
3030 #define LIBAVCODEC_VERSION_MINOR 13
31−#define LIBAVCODEC_VERSION_MICRO 0
31+#define LIBAVCODEC_VERSION_MICRO 1
3232
3333 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3434 LIBAVCODEC_VERSION_MINOR, \

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