58 int best_samplerate = 0;
65 if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
69 return best_samplerate;
76 uint64_t best_ch_layout = 0;
77 int best_nb_channels = 0;
86 if (nb_channels > best_nb_channels) {
92 return best_ch_layout;
103 fprintf(stderr,
"Error sending the frame to the encoder\n");
114 fprintf(stderr,
"Error encoding audio frame\n");
118 fwrite(pkt->
data, 1, pkt->
size, output);
123 int main(
int argc,
char **argv)
125 const char *filename;
136 fprintf(stderr,
"Usage: %s <output file>\n", argv[0]);
144 fprintf(stderr,
"Codec not found\n");
150 fprintf(stderr,
"Could not allocate audio codec context\n");
160 fprintf(stderr,
"Encoder does not support sample format %s",
172 fprintf(stderr,
"Could not open codec\n");
176 f = fopen(filename,
"wb");
178 fprintf(stderr,
"Could not open %s\n", filename);
185 fprintf(stderr,
"could not allocate the packet\n");
192 fprintf(stderr,
"Could not allocate audio frame\n");
203 fprintf(stderr,
"Could not allocate audio data buffers\n");
210 for (i = 0; i < 200; i++) {
216 samples = (uint16_t*)frame->
data[0];
219 samples[2*j] = (
int)(sin(t) * 10000);
222 samples[2*j + k] = samples[2*j];
This structure describes decoded (raw) audio or video data.
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int64_t bit_rate
the average bitrate
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
int main(int argc, char **argv)
#define AV_CH_LAYOUT_STEREO
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
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
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AVERROR_EOF
End of file.
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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.
reference-counted frame API
uint64_t channel_layout
Audio channel layout.
uint64_t channel_layout
Channel layout of the audio data.
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)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
int frame_size
Number of samples per channel in an audio frame.
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
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
main external API structure.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
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.
common internal and external API header
static int select_sample_rate(const AVCodec *codec)
int channels
number of audio channels
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
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
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame