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;
95 int main(
int argc,
char **argv)
102 int i, j, k, ret, got_output;
108 fprintf(stderr,
"Usage: %s <output file>\n", argv[0]);
119 fprintf(stderr,
"Codec not found\n");
125 fprintf(stderr,
"Could not allocate audio codec context\n");
135 fprintf(stderr,
"Encoder does not support sample format %s",
147 fprintf(stderr,
"Could not open codec\n");
151 f = fopen(filename,
"wb");
153 fprintf(stderr,
"Could not open %s\n", filename);
160 fprintf(stderr,
"Could not allocate audio frame\n");
171 fprintf(stderr,
"Could not allocate audio data buffers\n");
178 for (i = 0; i < 200; i++) {
188 samples = (uint16_t*)frame->
data[0];
191 samples[2*j] = (
int)(sin(t) * 10000);
194 samples[2*j + k] = samples[2*j];
200 fprintf(stderr,
"Error encoding audio frame\n");
210 for (got_output = 1; got_output; i++) {
213 fprintf(stderr,
"Error encoding frame\n");
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
attribute_deprecated int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
int main(int argc, char **argv)
#define AV_CH_LAYOUT_STEREO
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
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.
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
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
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 ...
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