diff options
Diffstat (limited to 'doc/examples/decode_audio.c')
-rw-r--r-- | doc/examples/decode_audio.c | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/doc/examples/decode_audio.c b/doc/examples/decode_audio.c index 4378281dbd..738a188aef 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. */ /** @@ -62,21 +64,25 @@ 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); } 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"); @@ -90,11 +96,12 @@ int main(int argc, char **argv) avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); while (avpkt.size > 0) { + int i, ch; int got_frame = 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); } } @@ -106,13 +113,20 @@ int main(int argc, char **argv) } 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); + int data_size = av_get_bytes_per_sample(c->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<decoded_frame->nb_samples; i++) + for (ch=0; ch<c->channels; ch++) + fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile); } avpkt.size -= len; avpkt.data += len; + avpkt.dts = + avpkt.pts = AV_NOPTS_VALUE; if (avpkt.size < AUDIO_REFILL_THRESH) { /* Refill the input buffer, to avoid trying to decode * incomplete frames. Instead of this, one could also use |