43 #define INBUF_SIZE 4096
44 #define AUDIO_INBUF_SIZE 20480
45 #define AUDIO_REFILL_THRESH 4096
64 int best_samplerate = 0;
71 best_samplerate =
FFMAX(*p, best_samplerate);
74 return best_samplerate;
81 uint64_t best_ch_layout = 0;
82 int best_nb_channels = 0;
91 if (nb_channels > best_nb_channels) {
97 return best_ch_layout;
109 int i, j, k,
ret, got_output;
115 printf(
"Encode audio file %s\n", filename);
120 fprintf(stderr,
"Codec not found\n");
126 fprintf(stderr,
"Could not allocate audio codec context\n");
136 fprintf(stderr,
"Encoder does not support sample format %s",
148 fprintf(stderr,
"Could not open codec\n");
152 f = fopen(filename,
"wb");
154 fprintf(stderr,
"Could not open %s\n", filename);
161 fprintf(stderr,
"Could not allocate audio frame\n");
173 if (buffer_size < 0) {
174 fprintf(stderr,
"Could not get sample buffer size\n");
179 fprintf(stderr,
"Could not allocate %d bytes for samples buffer\n",
185 (
const uint8_t*)samples, buffer_size, 0);
187 fprintf(stderr,
"Could not setup audio frame\n");
194 for (i = 0; i < 200; i++) {
200 samples[2*j] = (int)(sin(t) * 10000);
203 samples[2*j + k] = samples[2*j];
209 fprintf(stderr,
"Error encoding audio frame\n");
219 for (got_output = 1; got_output; i++) {
222 fprintf(stderr,
"Error encoding frame\n");
254 printf(
"Decode audio file %s to %s\n", filename, outfilename);
259 fprintf(stderr,
"Codec not found\n");
265 fprintf(stderr,
"Could not allocate audio codec context\n");
271 fprintf(stderr,
"Could not open codec\n");
275 f = fopen(filename,
"rb");
277 fprintf(stderr,
"Could not open %s\n", filename);
280 outfile = fopen(outfilename,
"wb");
290 while (avpkt.
size > 0) {
294 if (!decoded_frame) {
296 fprintf(stderr,
"Could not allocate audio frame\n");
303 fprintf(stderr,
"Error while decoding\n");
311 fprintf(stderr,
"Failed to calculate data size\n");
316 fwrite(decoded_frame->
data[ch] + data_size*i, 1, data_size, outfile);
327 memmove(inbuf, avpkt.
data, avpkt.
size);
329 len = fread(avpkt.
data + avpkt.
size, 1,
351 int i,
ret, x,
y, got_output;
355 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
357 printf(
"Encode video file %s\n", filename);
362 fprintf(stderr,
"Codec not found\n");
368 fprintf(stderr,
"Could not allocate video codec context\n");
394 fprintf(stderr,
"Could not open codec\n");
398 f = fopen(filename,
"wb");
400 fprintf(stderr,
"Could not open %s\n", filename);
406 fprintf(stderr,
"Could not allocate video frame\n");
418 fprintf(stderr,
"Could not allocate raw picture buffer\n");
423 for (i = 0; i < 25; i++) {
431 for (y = 0; y < c->
height; y++) {
432 for (x = 0; x < c->
width; x++) {
433 frame->
data[0][y * frame->
linesize[0] + x] = x + y + i * 3;
438 for (y = 0; y < c->
height/2; y++) {
439 for (x = 0; x < c->
width/2; x++) {
440 frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
441 frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
450 fprintf(stderr,
"Error encoding frame\n");
455 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
462 for (got_output = 1; got_output; i++) {
467 fprintf(stderr,
"Error encoding frame\n");
472 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
479 fwrite(endcode, 1,
sizeof(endcode), f);
499 f = fopen(filename,
"w");
500 fprintf(f,
"P5\n%d %d\n%d\n", xsize, ysize, 255);
501 for (i = 0; i < ysize; i++)
502 fwrite(buf + i * wrap, 1, xsize, f);
514 fprintf(stderr,
"Error while decoding frame %d\n", *frame_count);
518 printf(
"Saving %sframe %3d\n", last ?
"last " :
"", *frame_count);
522 snprintf(buf,
sizeof(buf), outfilename, *frame_count);
549 printf(
"Decode video file %s to %s\n", filename, outfilename);
554 fprintf(stderr,
"Codec not found\n");
560 fprintf(stderr,
"Could not allocate video codec context\n");
573 fprintf(stderr,
"Could not open codec\n");
577 f = fopen(filename,
"rb");
579 fprintf(stderr,
"Could not open %s\n", filename);
585 fprintf(stderr,
"Could not allocate video frame\n");
611 while (avpkt.
size > 0)
631 int main(
int argc,
char **argv)
633 const char *output_type;
639 printf(
"usage: %s output_type\n"
640 "API example program to decode/encode a media stream with libavcodec.\n"
641 "This program generates a synthetic stream and encodes it to a file\n"
642 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
643 "The encoded stream is then decoded and written to a raw data output.\n"
644 "output_type must be chosen between 'h264', 'mp2', 'mpg'.\n",
648 output_type = argv[1];
650 if (!strcmp(output_type,
"h264")) {
652 }
else if (!strcmp(output_type,
"mp2")) {
655 }
else if (!strcmp(output_type,
"mpg")) {
659 fprintf(stderr,
"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",
static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
void av_free_packet(AVPacket *pkt)
Free a packet.
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.
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
#define CODEC_CAP_TRUNCATED
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static void video_decode_example(const char *outfilename, const char *filename)
int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
#define AV_CH_LAYOUT_STEREO
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align)
Fill AVFrame audio data and linesize pointers.
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
static int select_sample_rate(AVCodec *codec)
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
#define CODEC_FLAG_TRUNCATED
int width
width and height of the video frame
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int capabilities
Codec capabilities.
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 AUDIO_REFILL_THRESH
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
uint64_t channel_layout
Channel layout of the audio data.
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int bit_rate
the average bitrate
audio channel layout utility functions
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
int width
picture width / height.
static void audio_encode_example(const char *filename)
int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
static void video_encode_example(const char *filename, int codec_id)
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.
static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
AVSampleFormat
Audio sample formats.
int main(int argc, char **argv)
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
rational number numerator/denominator
static void audio_decode_example(const char *outfilename, const char *filename)
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal and external API header
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
int channels
number of audio channels
static int select_channel_layout(AVCodec *codec)
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
This structure stores compressed data.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.