diff options
Diffstat (limited to 'doc/examples/encode_audio.c')
-rw-r--r-- | doc/examples/encode_audio.c | 67 |
1 files changed, 38 insertions, 29 deletions
diff --git a/doc/examples/encode_audio.c b/doc/examples/encode_audio.c index a32fcc9004..d1ef105d9d 100644 --- a/doc/examples/encode_audio.c +++ b/doc/examples/encode_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,12 +31,12 @@ #include <stdio.h> #include <stdlib.h> -#include "libavcodec/avcodec.h" +#include <libavcodec/avcodec.h> -#include "libavutil/channel_layout.h" -#include "libavutil/common.h" -#include "libavutil/frame.h" -#include "libavutil/samplefmt.h" +#include <libavutil/channel_layout.h> +#include <libavutil/common.h> +#include <libavutil/frame.h> +#include <libavutil/samplefmt.h> /* check that a given sample format is supported by the encoder */ static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt) @@ -60,7 +62,8 @@ static int select_sample_rate(const AVCodec *codec) p = codec->supported_samplerates; while (*p) { - best_samplerate = FFMAX(*p, best_samplerate); + if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate)) + best_samplerate = *p; p++; } return best_samplerate; @@ -97,7 +100,7 @@ static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, /* send the frame for encoding */ ret = avcodec_send_frame(ctx, frame); if (ret < 0) { - fprintf(stderr, "error sending the frame to the encoder\n"); + fprintf(stderr, "Error sending the frame to the encoder\n"); exit(1); } @@ -108,7 +111,7 @@ static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) return; else if (ret < 0) { - fprintf(stderr, "error encoding audio frame\n"); + fprintf(stderr, "Error encoding audio frame\n"); exit(1); } @@ -141,11 +144,15 @@ int main(int argc, char **argv) /* find the MP2 encoder */ codec = avcodec_find_encoder(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); + } /* put sample parameters */ c->bit_rate = 64000; @@ -153,7 +160,7 @@ int main(int argc, char **argv) /* check that the encoder supports s16 pcm input */ c->sample_fmt = AV_SAMPLE_FMT_S16; if (!check_sample_fmt(codec, c->sample_fmt)) { - fprintf(stderr, "encoder does not support %s", + fprintf(stderr, "Encoder does not support sample format %s", av_get_sample_fmt_name(c->sample_fmt)); exit(1); } @@ -165,13 +172,13 @@ int main(int argc, char **argv) /* 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, "wb"); if (!f) { - fprintf(stderr, "could not open %s\n", filename); + fprintf(stderr, "Could not open %s\n", filename); exit(1); } @@ -185,7 +192,7 @@ int main(int argc, char **argv) /* frame containing input raw audio */ frame = av_frame_alloc(); if (!frame) { - fprintf(stderr, "could not allocate audio frame\n"); + fprintf(stderr, "Could not allocate audio frame\n"); exit(1); } @@ -196,14 +203,14 @@ int main(int argc, char **argv) /* allocate the data buffers */ ret = av_frame_get_buffer(frame, 0); if (ret < 0) { - fprintf(stderr, "could not allocate audio data buffers\n"); + fprintf(stderr, "Could not allocate audio data buffers\n"); exit(1); } /* encode a single tone sound */ t = 0; tincr = 2 * M_PI * 440.0 / c->sample_rate; - for(i=0;i<200;i++) { + for (i = 0; i < 200; i++) { /* make sure the frame is writable -- makes a copy if the encoder * kept a reference internally */ ret = av_frame_make_writable(frame); @@ -229,4 +236,6 @@ int main(int argc, char **argv) av_frame_free(&frame); av_packet_free(&pkt); avcodec_free_context(&c); + + return 0; } |