[FFmpeg-devel] [PATCH] examples/muxing: reuse global audio frame

Michael Niedermayer michaelni at gmx.at
Thu Jan 9 02:41:31 CET 2014


On Thu, Jan 09, 2014 at 01:09:45AM +0100, Stefano Sabatini wrote:
> On date Thursday 2014-01-09 00:43:27 +0100, Michael Niedermayer encoded:
> > On Wed, Jan 08, 2014 at 03:45:13PM +0100, Stefano Sabatini wrote:
> > > Simplify logic, avoid multiple unnecessary alloc/free operations.
> > > ---
> > >  doc/examples/muxing.c | 22 +++++++++++++---------
> > >  1 file changed, 13 insertions(+), 9 deletions(-)
> > 
> > simplify and 9->13 lines hmm
> 
> The added lines are comments and a missing check. Updated.
> -- 
> FFmpeg = Fancy and Formidable Miracolous Power Elegant God

>  muxing.c |   20 +++++++++++++-------
>  1 file changed, 13 insertions(+), 7 deletions(-)
> 8f7e8fa526d32afebf13f36703825303f923f438  0005-examples-muxing-reuse-global-audio-frame.patch
> From f914b1b800d71f01638a6c47c4a0cc604345a2f9 Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefasab at gmail.com>
> Date: Wed, 8 Jan 2014 15:42:14 +0100
> Subject: [PATCH] examples/muxing: reuse global audio frame
> 
> Simplify logic, avoid multiple unnecessary alloc/free operations.
> ---
>  doc/examples/muxing.c | 20 +++++++++++++-------
>  1 file changed, 13 insertions(+), 7 deletions(-)
> 
> diff --git a/doc/examples/muxing.c b/doc/examples/muxing.c
> index e5128e8..8edef00 100644
> --- a/doc/examples/muxing.c
> +++ b/doc/examples/muxing.c
> @@ -123,6 +123,7 @@ static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
>  
>  static float t, tincr, tincr2;
>  
> +AVFrame *audio_frame;
>  static uint8_t **src_samples_data;
>  static int       src_samples_linesize;
>  static int       src_nb_samples;
> @@ -141,6 +142,13 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
>  
>      c = st->codec;
>  
> +    /* allocate and init a re-usable frame */
> +    audio_frame = av_frame_alloc();
> +    if (!audio_frame) {
> +        fprintf(stderr, "Could not allocate audio frame\n");
> +        exit(1);
> +    }
> +
>      /* open it */
>      ret = avcodec_open2(c, codec, NULL);
>      if (ret < 0) {
> @@ -225,7 +233,6 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
>  {
>      AVCodecContext *c;
>      AVPacket pkt = { 0 }; // data and size must be 0;
> -    AVFrame *frame = av_frame_alloc();
>      int got_packet, ret, dst_nb_samples;
>  
>      av_init_packet(&pkt);
> @@ -261,18 +268,18 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
>          dst_nb_samples = src_nb_samples;
>      }
>  
> -    frame->nb_samples = dst_nb_samples;
> -    avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
> +    audio_frame->nb_samples = dst_nb_samples;
> +    avcodec_fill_audio_frame(audio_frame, c->channels, c->sample_fmt,
>                               dst_samples_data[0], dst_samples_size, 0);
>  
> -    ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
> +    ret = avcodec_encode_audio2(c, &pkt, audio_frame, &got_packet);
>      if (ret < 0) {
>          fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
>          exit(1);
>      }
>  
>      if (!got_packet)
> -        goto freeframe;
> +        return;
>  
>      pkt.stream_index = st->index;
>  
> @@ -283,8 +290,6 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
>                  av_err2str(ret));
>          exit(1);
>      }
> -freeframe:
> -    av_frame_free(&frame);
>  }
>  
>  static void close_audio(AVFormatContext *oc, AVStream *st)

> @@ -296,6 +301,7 @@ static void close_audio(AVFormatContext *oc, AVStream *st)
>      }
>      av_free(src_samples_data[0]);
>      av_free(src_samples_data);
> +    av_free(audio_frame);

av_frame_free()

otherwise should be ok

[....]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Awnsering whenever a program halts or runs forever is
On a turing machine, in general impossible (turings halting problem).
On any real computer, always possible as a real computer has a finite number
of states N, and will either halt in less than N cycles or never halt.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140109/66d06128/attachment.asc>


More information about the ffmpeg-devel mailing list