aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/utils.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2012-05-10 02:14:44 +0200
committerMichael Niedermayer <michaelni@gmx.at>2012-05-10 02:25:41 +0200
commit61930bd0d7154b6f3f8c8d2398c056c20e921652 (patch)
tree54108723e1482e5d20a152f7a46c67ea51441965 /libavcodec/utils.c
parent35894ebbf9bef18a31dbb90a8a8818fbdc85a184 (diff)
parentc8b4a3999bc7f3732a537cdec6475918a65d6e78 (diff)
downloadffmpeg-61930bd0d7154b6f3f8c8d2398c056c20e921652.tar.gz
Merge remote-tracking branch 'qatar/master'
* qatar/master: (27 commits) libxvid: Give more suitable names to libxvid-related files. libxvid: Separate libxvid encoder from libxvid rate control code. jpeglsdec: Remove write-only variable in ff_jpegls_decode_lse(). fate: cosmetics: lowercase some comments fate: Give more consistent names to some RealVideo/RealAudio tests. lavfi: add avfilter_get_audio_buffer_ref_from_arrays(). lavfi: add extended_data to AVFilterBuffer. lavc: check that extended_data is properly set in avcodec_encode_audio2(). lavc: pad last audio frame with silence when needed. samplefmt: add a function for filling a buffer with silence. samplefmt: add a function for copying audio samples. lavr: do not try to copy to uninitialized output audio data. lavr: make avresample_read() with NULL output discard samples. fate: split idroq audio and video into separate tests fate: improve dependencies fate: add convenient shorthands for ea-vp6, libavcodec, libavutil tests fate: split some combined tests into separate audio and video tests fate: fix dependencies for probe tests mips: intreadwrite: fix inline asm for gcc 4.8 mips: intreadwrite: remove unnecessary inline asm ... Conflicts: cmdutils.h configure doc/APIchanges doc/filters.texi ffmpeg.c ffplay.c libavcodec/internal.h libavcodec/jpeglsdec.c libavcodec/libschroedingerdec.c libavcodec/libxvid.c libavcodec/libxvid_rc.c libavcodec/utils.c libavcodec/version.h libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.h tests/Makefile tests/fate/aac.mak tests/fate/audio.mak tests/fate/demux.mak tests/fate/ea.mak tests/fate/image.mak tests/fate/libavutil.mak tests/fate/lossless-audio.mak tests/fate/lossless-video.mak tests/fate/microsoft.mak tests/fate/qt.mak tests/fate/real.mak tests/fate/screen.mak tests/fate/video.mak tests/fate/voice.mak tests/fate/vqf.mak tests/ref/fate/ea-mad tests/ref/fate/ea-tqi Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec/utils.c')
-rw-r--r--libavcodec/utils.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 79b355472e..c915de6a06 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1001,11 +1001,59 @@ int ff_alloc_packet(AVPacket *avpkt, int size)
return ff_alloc_packet2(NULL, avpkt, size);
}
+/**
+ * Pad last frame with silence.
+ */
+static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
+{
+ AVFrame *frame = NULL;
+ uint8_t *buf = NULL;
+ int ret;
+
+ if (!(frame = avcodec_alloc_frame()))
+ return AVERROR(ENOMEM);
+ *frame = *src;
+
+ if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
+ s->frame_size, s->sample_fmt, 0)) < 0)
+ goto fail;
+
+ if (!(buf = av_malloc(ret))) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ frame->nb_samples = s->frame_size;
+ if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
+ buf, ret, 0)) < 0)
+ goto fail;
+ if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
+ src->nb_samples, s->channels, s->sample_fmt)) < 0)
+ goto fail;
+ if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
+ frame->nb_samples - src->nb_samples,
+ s->channels, s->sample_fmt)) < 0)
+ goto fail;
+
+ *dst = frame;
+
+ return 0;
+
+fail:
+ if (frame->extended_data != frame->data)
+ av_freep(&frame->extended_data);
+ av_freep(&buf);
+ av_freep(&frame);
+ return ret;
+}
+
int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
AVPacket *avpkt,
const AVFrame *frame,
int *got_packet_ptr)
{
+ AVFrame tmp;
+ AVFrame *padded_frame = NULL;
int ret;
AVPacket user_pkt = *avpkt;
int needs_realloc = !user_pkt.data;
@@ -1018,12 +1066,38 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
return 0;
}
+ /* ensure that extended_data is properly set */
+ if (frame && !frame->extended_data) {
+ if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
+ avctx->channels > AV_NUM_DATA_POINTERS) {
+ av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
+ "with more than %d channels, but extended_data is not set.\n",
+ AV_NUM_DATA_POINTERS);
+ return AVERROR(EINVAL);
+ }
+ av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
+
+ tmp = *frame;
+ tmp.extended_data = tmp.data;
+ frame = &tmp;
+ }
+
/* check for valid frame size */
if (frame) {
if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
if (frame->nb_samples > avctx->frame_size)
return AVERROR(EINVAL);
} else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
+ if (frame->nb_samples < avctx->frame_size &&
+ !avctx->internal->last_audio_frame) {
+ ret = pad_last_frame(avctx, &padded_frame, frame);
+ if (ret < 0)
+ return ret;
+
+ frame = padded_frame;
+ avctx->internal->last_audio_frame = 1;
+ }
+
if (frame->nb_samples != avctx->frame_size)
return AVERROR(EINVAL);
}
@@ -1084,6 +1158,13 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
here to simplify things */
avpkt->flags |= AV_PKT_FLAG_KEY;
+ if (padded_frame) {
+ av_freep(&padded_frame->data[0]);
+ if (padded_frame->extended_data != padded_frame->data)
+ av_freep(&padded_frame->extended_data);
+ av_freep(&padded_frame);
+ }
+
return ret;
}