diff options
Diffstat (limited to 'doc/examples/decode_audio.c')
-rw-r--r-- | doc/examples/decode_audio.c | 64 |
1 files changed, 38 insertions, 26 deletions
diff --git a/doc/examples/decode_audio.c b/doc/examples/decode_audio.c index d2150a67c1..8ad9f06fa2 100644 --- a/doc/examples/decode_audio.c +++ b/doc/examples/decode_audio.c @@ -1,21 +1,23 @@ /* - * copyright (c) 2001 Fabrice Bellard + * Copyright (c) 2001 Fabrice Bellard * - * This file is part of Libav. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * 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. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * 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 Libav; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ /** @@ -29,10 +31,10 @@ #include <stdlib.h> #include <string.h> -#include "libavutil/frame.h" -#include "libavutil/mem.h" +#include <libavutil/frame.h> +#include <libavutil/mem.h> -#include "libavcodec/avcodec.h" +#include <libavcodec/avcodec.h> #define AUDIO_INBUF_SIZE 20480 #define AUDIO_REFILL_THRESH 4096 @@ -49,11 +51,17 @@ static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, exit(1); } if (got_frame) { + int i, ch; /* if a frame has been decoded, output it */ - int data_size = av_samples_get_buffer_size(NULL, dec_ctx->channels, - frame->nb_samples, - dec_ctx->sample_fmt, 1); - fwrite(frame->data[0], 1, data_size, outfile); + int data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt); + if (data_size < 0) { + /* This should not occur, checking just for paranoia */ + fprintf(stderr, "Failed to calculate data size\n"); + exit(1); + } + for (i = 0; i < frame->nb_samples; i++) + for (ch = 0; ch < dec_ctx->channels; ch++) + fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile); } pkt->size -= len; pkt->data += len; @@ -89,27 +97,31 @@ int main(int argc, char **argv) /* find the MPEG audio decoder */ codec = avcodec_find_decoder(AV_CODEC_ID_MP2); if (!codec) { - fprintf(stderr, "codec not found\n"); + fprintf(stderr, "Codec not found\n"); exit(1); } parser = av_parser_init(codec->id); if (!parser) { - fprintf(stderr, "parser not found\n"); + fprintf(stderr, "Parser not found\n"); exit(1); } c = avcodec_alloc_context3(codec); + if (!c) { + fprintf(stderr, "Could not allocate audio codec context\n"); + exit(1); + } /* open it */ if (avcodec_open2(c, codec, NULL) < 0) { - fprintf(stderr, "could not open codec\n"); + fprintf(stderr, "Could not open codec\n"); exit(1); } f = fopen(filename, "rb"); if (!f) { - fprintf(stderr, "could not open %s\n", filename); + fprintf(stderr, "Could not open %s\n", filename); exit(1); } outfile = fopen(outfilename, "wb"); @@ -125,7 +137,7 @@ int main(int argc, char **argv) while (data_size > 0) { if (!decoded_frame) { if (!(decoded_frame = av_frame_alloc())) { - fprintf(stderr, "out of memory\n"); + fprintf(stderr, "Could not allocate audio frame\n"); exit(1); } } |