encode_audio.c
Go to the documentation of this file.
1 /*
2  * copyright (c) 2001 Fabrice Bellard
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #include "libavcodec/avcodec.h"
33 
35 #include "libavutil/common.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/samplefmt.h"
38 
39 /* check that a given sample format is supported by the encoder */
40 static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
41 {
42  const enum AVSampleFormat *p = codec->sample_fmts;
43 
44  while (*p != AV_SAMPLE_FMT_NONE) {
45  if (*p == sample_fmt)
46  return 1;
47  p++;
48  }
49  return 0;
50 }
51 
52 /* just pick the highest supported samplerate */
53 static int select_sample_rate(const AVCodec *codec)
54 {
55  const int *p;
56  int best_samplerate = 0;
57 
58  if (!codec->supported_samplerates)
59  return 44100;
60 
61  p = codec->supported_samplerates;
62  while (*p) {
63  best_samplerate = FFMAX(*p, best_samplerate);
64  p++;
65  }
66  return best_samplerate;
67 }
68 
69 /* select layout with the highest channel count */
70 static int select_channel_layout(const AVCodec *codec)
71 {
72  const uint64_t *p;
73  uint64_t best_ch_layout = 0;
74  int best_nb_channels = 0;
75 
76  if (!codec->channel_layouts)
77  return AV_CH_LAYOUT_STEREO;
78 
79  p = codec->channel_layouts;
80  while (*p) {
81  int nb_channels = av_get_channel_layout_nb_channels(*p);
82 
83  if (nb_channels > best_nb_channels) {
84  best_ch_layout = *p;
85  best_nb_channels = nb_channels;
86  }
87  p++;
88  }
89  return best_ch_layout;
90 }
91 
92 static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
93  FILE *output)
94 {
95  int ret;
96 
97  /* send the frame for encoding */
98  ret = avcodec_send_frame(ctx, frame);
99  if (ret < 0) {
100  fprintf(stderr, "error sending the frame to the encoder\n");
101  exit(1);
102  }
103 
104  /* read all the available output packets (in general there may be any
105  * number of them */
106  while (ret >= 0) {
107  ret = avcodec_receive_packet(ctx, pkt);
108  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
109  return;
110  else if (ret < 0) {
111  fprintf(stderr, "error encoding audio frame\n");
112  exit(1);
113  }
114 
115  fwrite(pkt->data, 1, pkt->size, output);
116  av_packet_unref(pkt);
117  }
118 }
119 
120 int main(int argc, char **argv)
121 {
122  const char *filename;
123  const AVCodec *codec;
124  AVCodecContext *c= NULL;
125  AVFrame *frame;
126  AVPacket *pkt;
127  int i, j, k, ret;
128  FILE *f;
129  uint16_t *samples;
130  float t, tincr;
131 
132  if (argc <= 1) {
133  fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
134  return 0;
135  }
136  filename = argv[1];
137 
138  /* register all the codecs */
140 
141  /* find the MP2 encoder */
143  if (!codec) {
144  fprintf(stderr, "codec not found\n");
145  exit(1);
146  }
147 
148  c = avcodec_alloc_context3(codec);
149 
150  /* put sample parameters */
151  c->bit_rate = 64000;
152 
153  /* check that the encoder supports s16 pcm input */
155  if (!check_sample_fmt(codec, c->sample_fmt)) {
156  fprintf(stderr, "encoder does not support %s",
158  exit(1);
159  }
160 
161  /* select other audio parameters supported by the encoder */
162  c->sample_rate = select_sample_rate(codec);
165 
166  /* open it */
167  if (avcodec_open2(c, codec, NULL) < 0) {
168  fprintf(stderr, "could not open codec\n");
169  exit(1);
170  }
171 
172  f = fopen(filename, "wb");
173  if (!f) {
174  fprintf(stderr, "could not open %s\n", filename);
175  exit(1);
176  }
177 
178  /* packet for holding encoded output */
179  pkt = av_packet_alloc();
180  if (!pkt) {
181  fprintf(stderr, "could not allocate the packet\n");
182  exit(1);
183  }
184 
185  /* frame containing input raw audio */
186  frame = av_frame_alloc();
187  if (!frame) {
188  fprintf(stderr, "could not allocate audio frame\n");
189  exit(1);
190  }
191 
192  frame->nb_samples = c->frame_size;
193  frame->format = c->sample_fmt;
194  frame->channel_layout = c->channel_layout;
195 
196  /* allocate the data buffers */
197  ret = av_frame_get_buffer(frame, 0);
198  if (ret < 0) {
199  fprintf(stderr, "could not allocate audio data buffers\n");
200  exit(1);
201  }
202 
203  /* encode a single tone sound */
204  t = 0;
205  tincr = 2 * M_PI * 440.0 / c->sample_rate;
206  for(i=0;i<200;i++) {
207  /* make sure the frame is writable -- makes a copy if the encoder
208  * kept a reference internally */
209  ret = av_frame_make_writable(frame);
210  if (ret < 0)
211  exit(1);
212  samples = (uint16_t*)frame->data[0];
213 
214  for (j = 0; j < c->frame_size; j++) {
215  samples[2*j] = (int)(sin(t) * 10000);
216 
217  for (k = 1; k < c->channels; k++)
218  samples[2*j + k] = samples[2*j];
219  t += tincr;
220  }
221  encode(c, frame, pkt, f);
222  }
223 
224  /* flush the encoder */
225  encode(c, NULL, pkt, f);
226 
227  fclose(f);
228 
229  av_frame_free(&frame);
230  av_packet_free(&pkt);
232 }
This structure describes decoded (raw) audio or video data.
Definition: frame.h:147
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
int size
Definition: avcodec.h:1154
int main(int argc, char **argv)
Definition: encode_audio.c:120
#define AV_CH_LAYOUT_STEREO
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
AVCodec.
Definition: avcodec.h:2842
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1860
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
uint8_t * data
Definition: avcodec.h:1153
#define AVERROR_EOF
End of file.
Definition: error.h:51
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
Definition: encode_audio.c:40
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
Definition: avcodec.h:2866
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
#define FFMAX(a, b)
Definition: common.h:64
reference-counted frame API
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1903
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:309
int bit_rate
the average bitrate
Definition: avcodec.h:1265
audio channel layout utility functions
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
static int select_channel_layout(const AVCodec *codec)
Definition: encode_audio.c:70
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:207
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1872
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
int sample_rate
samples per second
Definition: avcodec.h:1852
main external API structure.
Definition: avcodec.h:1216
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
Definition: encode_audio.c:92
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:153
common internal and external API header
signed 16 bits
Definition: samplefmt.h:63
static int select_sample_rate(const AVCodec *codec)
Definition: encode_audio.c:53
int channels
number of audio channels
Definition: avcodec.h:1853
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
Definition: avcodec.h:2864
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:2865
This structure stores compressed data.
Definition: avcodec.h:1130
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:200