]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/api-example.c
lagarith: pad RGB buffer by 1 byte.
[ffmpeg] / libavcodec / api-example.c
index 7f682cde30a208406faeab5dd4a8edae743818dd..93d6c2253cf1cd57e9d7b7e904654a67d9c0fc09 100644 (file)
@@ -1,27 +1,28 @@
 /*
  * copyright (c) 2001 Fabrice Bellard
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
- * @file libavcodec/apiexample.c
- * avcodec API use example.
+ * @file
+ * libavcodec API use example.
  *
+ * @example libavcodec/api-example.c
  * Note that this library only handles codecs (mpeg, mpeg4, etc...),
  * not file formats (avi, vob, etc...). See library 'libavformat' for the
  * format handling
 #undef HAVE_AV_CONFIG_H
 #endif
 
-#include "avcodec.h"
+#include "libavcodec/avcodec.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/samplefmt.h"
 
 #define INBUF_SIZE 4096
+#define AUDIO_INBUF_SIZE 20480
+#define AUDIO_REFILL_THRESH 4096
 
 /*
  * Audio encoding example
  */
-void audio_encode_example(const char *filename)
+static void audio_encode_example(const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
@@ -62,7 +66,7 @@ void audio_encode_example(const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
 
     /* put sample parameters */
     c->bit_rate = 64000;
@@ -70,7 +74,7 @@ void audio_encode_example(const char *filename)
     c->channels = 2;
 
     /* open it */
-    if (avcodec_open(c, codec) < 0) {
+    if (avcodec_open2(c, codec, NULL) < 0) {
         fprintf(stderr, "could not open codec\n");
         exit(1);
     }
@@ -111,14 +115,17 @@ void audio_encode_example(const char *filename)
 /*
  * Audio decoding.
  */
-void audio_decode_example(const char *outfilename, const char *filename)
+static void audio_decode_example(const char *outfilename, const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
-    int out_size, size, len;
+    int len;
     FILE *f, *outfile;
-    uint8_t *outbuf;
-    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
+    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
+    AVPacket avpkt;
+    AVFrame *decoded_frame = NULL;
+
+    av_init_packet(&avpkt);
 
     printf("Audio decoding\n");
 
@@ -129,16 +136,14 @@ void audio_decode_example(const char *outfilename, const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
 
     /* open it */
-    if (avcodec_open(c, codec) < 0) {
+    if (avcodec_open2(c, codec, NULL) < 0) {
         fprintf(stderr, "could not open codec\n");
         exit(1);
     }
 
-    outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
-
     f = fopen(filename, "rb");
     if (!f) {
         fprintf(stderr, "could not open %s\n", filename);
@@ -151,42 +156,60 @@ void audio_decode_example(const char *outfilename, const char *filename)
     }
 
     /* decode until eof */
-    inbuf_ptr = inbuf;
-    for(;;) {
-        size = fread(inbuf, 1, INBUF_SIZE, f);
-        if (size == 0)
-            break;
+    avpkt.data = inbuf;
+    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
 
-        inbuf_ptr = inbuf;
-        while (size > 0) {
-            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-            len = avcodec_decode_audio2(c, (short *)outbuf, &out_size,
-                                       inbuf_ptr, size);
-            if (len < 0) {
-                fprintf(stderr, "Error while decoding\n");
+    while (avpkt.size > 0) {
+        int got_frame = 0;
+
+        if (!decoded_frame) {
+            if (!(decoded_frame = avcodec_alloc_frame())) {
+                fprintf(stderr, "out of memory\n");
                 exit(1);
             }
-            if (out_size > 0) {
-                /* if a frame has been decoded, output it */
-                fwrite(outbuf, 1, out_size, outfile);
-            }
-            size -= len;
-            inbuf_ptr += len;
+        } else
+            avcodec_get_frame_defaults(decoded_frame);
+
+        len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
+        if (len < 0) {
+            fprintf(stderr, "Error while decoding\n");
+            exit(1);
+        }
+        if (got_frame) {
+            /* if a frame has been decoded, output it */
+            int data_size = av_samples_get_buffer_size(NULL, c->channels,
+                                                       decoded_frame->nb_samples,
+                                                       c->sample_fmt, 1);
+            fwrite(decoded_frame->data[0], 1, data_size, outfile);
+        }
+        avpkt.size -= len;
+        avpkt.data += len;
+        if (avpkt.size < AUDIO_REFILL_THRESH) {
+            /* Refill the input buffer, to avoid trying to decode
+             * incomplete frames. Instead of this, one could also use
+             * a parser, or use a proper container format through
+             * libavformat. */
+            memmove(inbuf, avpkt.data, avpkt.size);
+            avpkt.data = inbuf;
+            len = fread(avpkt.data + avpkt.size, 1,
+                        AUDIO_INBUF_SIZE - avpkt.size, f);
+            if (len > 0)
+                avpkt.size += len;
         }
     }
 
     fclose(outfile);
     fclose(f);
-    free(outbuf);
 
     avcodec_close(c);
     av_free(c);
+    av_free(decoded_frame);
 }
 
 /*
  * Video encoding example
  */
-void video_encode_example(const char *filename)
+static void video_encode_example(const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
@@ -204,7 +227,7 @@ void video_encode_example(const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
     picture= avcodec_alloc_frame();
 
     /* put sample parameters */
@@ -219,7 +242,7 @@ void video_encode_example(const char *filename)
     c->pix_fmt = PIX_FMT_YUV420P;
 
     /* open it */
-    if (avcodec_open(c, codec) < 0) {
+    if (avcodec_open2(c, codec, NULL) < 0) {
         fprintf(stderr, "could not open codec\n");
         exit(1);
     }
@@ -297,7 +320,8 @@ void video_encode_example(const char *filename)
  * Video decoding example
  */
 
-void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
+static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
+                     char *filename)
 {
     FILE *f;
     int i;
@@ -309,15 +333,18 @@ void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
     fclose(f);
 }
 
-void video_decode_example(const char *outfilename, const char *filename)
+static void video_decode_example(const char *outfilename, const char *filename)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
-    int frame, size, got_picture, len;
+    int frame, got_picture, len;
     FILE *f;
     AVFrame *picture;
-    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
+    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
     char buf[1024];
+    AVPacket avpkt;
+
+    av_init_packet(&avpkt);
 
     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
@@ -331,7 +358,7 @@ void video_decode_example(const char *outfilename, const char *filename)
         exit(1);
     }
 
-    c= avcodec_alloc_context();
+    c = avcodec_alloc_context3(codec);
     picture= avcodec_alloc_frame();
 
     if(codec->capabilities&CODEC_CAP_TRUNCATED)
@@ -342,7 +369,7 @@ void video_decode_example(const char *outfilename, const char *filename)
        available in the bitstream. */
 
     /* open it */
-    if (avcodec_open(c, codec) < 0) {
+    if (avcodec_open2(c, codec, NULL) < 0) {
         fprintf(stderr, "could not open codec\n");
         exit(1);
     }
@@ -357,8 +384,8 @@ void video_decode_example(const char *outfilename, const char *filename)
 
     frame = 0;
     for(;;) {
-        size = fread(inbuf, 1, INBUF_SIZE, f);
-        if (size == 0)
+        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
+        if (avpkt.size == 0)
             break;
 
         /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
@@ -376,10 +403,9 @@ void video_decode_example(const char *outfilename, const char *filename)
 
         /* here, we use a stream based decoder (mpeg1video), so we
            feed decoder and see if it could decode a frame */
-        inbuf_ptr = inbuf;
-        while (size > 0) {
-            len = avcodec_decode_video(c, picture, &got_picture,
-                                       inbuf_ptr, size);
+        avpkt.data = inbuf;
+        while (avpkt.size > 0) {
+            len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
             if (len < 0) {
                 fprintf(stderr, "Error while decoding frame %d\n", frame);
                 exit(1);
@@ -395,16 +421,17 @@ void video_decode_example(const char *outfilename, const char *filename)
                          c->width, c->height, buf);
                 frame++;
             }
-            size -= len;
-            inbuf_ptr += len;
+            avpkt.size -= len;
+            avpkt.data += len;
         }
     }
 
     /* some codecs, such as MPEG, transmit the I and P frame with a
        latency of one frame. You must do the following to have a
        chance to get the last frame of the video */
-    len = avcodec_decode_video(c, picture, &got_picture,
-                               NULL, 0);
+    avpkt.data = NULL;
+    avpkt.size = 0;
+    len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
     if (got_picture) {
         printf("saving last frame %3d\n", frame);
         fflush(stdout);
@@ -429,9 +456,6 @@ int main(int argc, char **argv)
 {
     const char *filename;
 
-    /* must be called before using avcodec lib */
-    avcodec_init();
-
     /* register all the codecs */
     avcodec_register_all();