diff options
author | Anton Khirnov <anton@khirnov.net> | 2014-07-27 03:12:25 +0400 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2014-07-27 03:15:39 +0400 |
commit | 22e9fe06ebb64cd8e901a95a82b0c3e7e00df611 (patch) | |
tree | f4e677cdc4be18fcca506de2b345db8061ce7c8a /doc/examples | |
parent | a98cadef7fe1e6fedeb916bb95dfe61161dd0034 (diff) |
doc/examples/muxing: add alloc_audio_frame() and use it to simplify code.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/muxing.c | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/doc/examples/muxing.c b/doc/examples/muxing.c index 4410f82805..66027bfc30 100644 --- a/doc/examples/muxing.c +++ b/doc/examples/muxing.c @@ -178,9 +178,38 @@ static void add_stream(OutputStream *ost, AVFormatContext *oc, /**************************************************************/ /* audio output */ +static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt, + uint64_t channel_layout, + int sample_rate, int nb_samples) +{ + AVFrame *frame = av_frame_alloc(); + int ret; + + if (!frame) { + fprintf(stderr, "Error allocating an audio frame\n"); + exit(1); + } + + frame->format = sample_fmt; + frame->channel_layout = channel_layout; + frame->sample_rate = sample_rate; + frame->nb_samples = nb_samples; + + if (nb_samples) { + ret = av_frame_get_buffer(frame, 0); + if (ret < 0) { + fprintf(stderr, "Error allocating an audio buffer\n"); + exit(1); + } + } + + return frame; +} + static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg) { AVCodecContext *c; + int nb_samples; int ret; AVDictionary *opt = NULL; @@ -201,27 +230,15 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A /* increment frequency by 110 Hz per second */ ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate; - ost->frame = av_frame_alloc(); - if (!ost->frame) - exit(1); - - ost->frame->sample_rate = c->sample_rate; - ost->frame->format = AV_SAMPLE_FMT_S16; - ost->frame->channel_layout = c->channel_layout; - if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) - ost->frame->nb_samples = 10000; + nb_samples = 10000; else - ost->frame->nb_samples = c->frame_size; + nb_samples = c->frame_size; - ost->tmp_frame = av_frame_alloc(); - if (!ost->frame) - exit(1); - - ost->tmp_frame->sample_rate = c->sample_rate; - ost->tmp_frame->format = c->sample_fmt; - ost->tmp_frame->channel_layout = c->channel_layout; - ost->tmp_frame->nb_samples = ost->frame->nb_samples; + ost->frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout, + c->sample_rate, nb_samples); + ost->tmp_frame = alloc_audio_frame(c->sample_fmt, c->channel_layout, + c->sample_rate, ost->frame->nb_samples); /* create resampler context */ if (c->sample_fmt != AV_SAMPLE_FMT_S16) { @@ -245,17 +262,6 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A exit(1); } } - - ret = av_frame_get_buffer(ost->frame, 0); - if (ret < 0) { - fprintf(stderr, "Could not allocate an audio frame.\n"); - exit(1); - } - ret = av_frame_get_buffer(ost->tmp_frame, 0); - if (ret < 0) { - fprintf(stderr, "Could not allocate an audio frame.\n"); - exit(1); - } } /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and |