00001 /*
00002 * Copyright (c) 2001 Fabrice Bellard
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a copy
00005 * of this software and associated documentation files (the "Software"), to deal
00006 * in the Software without restriction, including without limitation the rights
00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 * copies of the Software, and to permit persons to whom the Software is
00009 * furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included in
00012 * all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 * THE SOFTWARE.
00021 */
00022
00033 #include <math.h>
00034
00035 #include <libavutil/opt.h>
00036 #include <libavcodec/avcodec.h>
00037 #include <libavutil/channel_layout.h>
00038 #include <libavutil/common.h>
00039 #include <libavutil/imgutils.h>
00040 #include <libavutil/mathematics.h>
00041 #include <libavutil/samplefmt.h>
00042
00043 #define INBUF_SIZE 4096
00044 #define AUDIO_INBUF_SIZE 20480
00045 #define AUDIO_REFILL_THRESH 4096
00046
00047 /* check that a given sample format is supported by the encoder */
00048 static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
00049 {
00050 const enum AVSampleFormat *p = codec->sample_fmts;
00051
00052 while (*p != AV_SAMPLE_FMT_NONE) {
00053 if (*p == sample_fmt)
00054 return 1;
00055 p++;
00056 }
00057 return 0;
00058 }
00059
00060 /* just pick the highest supported samplerate */
00061 static int select_sample_rate(AVCodec *codec)
00062 {
00063 const int *p;
00064 int best_samplerate = 0;
00065
00066 if (!codec->supported_samplerates)
00067 return 44100;
00068
00069 p = codec->supported_samplerates;
00070 while (*p) {
00071 best_samplerate = FFMAX(*p, best_samplerate);
00072 p++;
00073 }
00074 return best_samplerate;
00075 }
00076
00077 /* select layout with the highest channel count */
00078 static int select_channel_layout(AVCodec *codec)
00079 {
00080 const uint64_t *p;
00081 uint64_t best_ch_layout = 0;
00082 int best_nb_channells = 0;
00083
00084 if (!codec->channel_layouts)
00085 return AV_CH_LAYOUT_STEREO;
00086
00087 p = codec->channel_layouts;
00088 while (*p) {
00089 int nb_channels = av_get_channel_layout_nb_channels(*p);
00090
00091 if (nb_channels > best_nb_channells) {
00092 best_ch_layout = *p;
00093 best_nb_channells = nb_channels;
00094 }
00095 p++;
00096 }
00097 return best_ch_layout;
00098 }
00099
00100 /*
00101 * Audio encoding example
00102 */
00103 static void audio_encode_example(const char *filename)
00104 {
00105 AVCodec *codec;
00106 AVCodecContext *c= NULL;
00107 AVFrame *frame;
00108 AVPacket pkt;
00109 int i, j, k, ret, got_output;
00110 int buffer_size;
00111 FILE *f;
00112 uint16_t *samples;
00113 float t, tincr;
00114
00115 printf("Encode audio file %s\n", filename);
00116
00117 /* find the MP2 encoder */
00118 codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
00119 if (!codec) {
00120 fprintf(stderr, "Codec not found\n");
00121 exit(1);
00122 }
00123
00124 c = avcodec_alloc_context3(codec);
00125 if (!c) {
00126 fprintf(stderr, "Could not allocate audio codec context\n");
00127 exit(1);
00128 }
00129
00130 /* put sample parameters */
00131 c->bit_rate = 64000;
00132
00133 /* check that the encoder supports s16 pcm input */
00134 c->sample_fmt = AV_SAMPLE_FMT_S16;
00135 if (!check_sample_fmt(codec, c->sample_fmt)) {
00136 fprintf(stderr, "Encoder does not support sample format %s",
00137 av_get_sample_fmt_name(c->sample_fmt));
00138 exit(1);
00139 }
00140
00141 /* select other audio parameters supported by the encoder */
00142 c->sample_rate = select_sample_rate(codec);
0