X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fapi-example.c;h=93d6c2253cf1cd57e9d7b7e904654a67d9c0fc09;hb=2b98377935384ecd22c2cd26106b9e03a6c9f598;hp=cde8dc352483087ffe4fe64fb7e8943871800e3f;hpb=11f6181af18a7deab3dc97caf864a00a2a017961;p=ffmpeg diff --git a/libavcodec/api-example.c b/libavcodec/api-example.c index cde8dc35248..93d6c2253cf 100644 --- a/libavcodec/api-example.c +++ b/libavcodec/api-example.c @@ -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 - * avcodec API use example. + * 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 @@ -37,6 +38,7 @@ #include "libavcodec/avcodec.h" #include "libavutil/mathematics.h" +#include "libavutil/samplefmt.h" #define INBUF_SIZE 4096 #define AUDIO_INBUF_SIZE 20480 @@ -64,7 +66,7 @@ static 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; @@ -72,7 +74,7 @@ static 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); } @@ -117,11 +119,11 @@ static void audio_decode_example(const char *outfilename, const char *filename) { AVCodec *codec; AVCodecContext *c= NULL; - int out_size, len; + int len; FILE *f, *outfile; - uint8_t *outbuf; uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; AVPacket avpkt; + AVFrame *decoded_frame = NULL; av_init_packet(&avpkt); @@ -134,16 +136,14 @@ static 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); @@ -157,41 +157,53 @@ static void audio_decode_example(const char *outfilename, const char *filename) /* decode until eof */ avpkt.data = inbuf; - avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); + avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); - while (avpkt.size > 0) { - out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; - len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); - 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); - } - 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; - } + } 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); } /* @@ -215,7 +227,7 @@ static 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 */ @@ -230,7 +242,7 @@ static 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); } @@ -346,7 +358,7 @@ static 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) @@ -357,7 +369,7 @@ static 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); } @@ -444,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();