35 #include <libavutil/opt.h>
36 #include <libavcodec/avcodec.h>
37 #include <libavutil/channel_layout.h>
38 #include <libavutil/common.h>
39 #include <libavutil/imgutils.h>
40 #include <libavutil/mathematics.h>
41 #include <libavutil/samplefmt.h>
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");
175 fprintf(stderr,
"Could not allocate %d bytes for samples buffer\n",
181 (
const uint8_t*)samples, buffer_size, 0);
183 fprintf(stderr,
"Could not setup audio frame\n");
196 samples[2*j] = (int)(sin(t) * 10000);
199 samples[2*j + k] = samples[2*j];
205 fprintf(stderr,
"Error encoding audio frame\n");
215 for (got_output = 1; got_output; i++) {
218 fprintf(stderr,
"Error encoding frame\n");
250 printf(
"Decode audio file %s to %s\n", filename, outfilename);
255 fprintf(stderr,
"Codec not found\n");
261 fprintf(stderr,
"Could not allocate audio codec context\n");
267 fprintf(stderr,
"Could not open codec\n");
271 f = fopen(filename,
"rb");
273 fprintf(stderr,
"Could not open %s\n", filename);
276 outfile = fopen(outfilename,
"wb");
286 while (avpkt.
size > 0) {
289 if (!decoded_frame) {
291 fprintf(stderr,
"Could not allocate audio frame\n");
299 fprintf(stderr,
"Error while decoding\n");
307 fwrite(decoded_frame->
data[0], 1, data_size, outfile);
318 memmove(inbuf, avpkt.
data, avpkt.
size);
320 len = fread(avpkt.
data + avpkt.
size, 1,
342 int i,
ret, x,
y, got_output;
346 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
348 printf(
"Encode video file %s\n", filename);
353 fprintf(stderr,
"Codec not found\n");
359 fprintf(stderr,
"Could not allocate video codec context\n");
379 fprintf(stderr,
"Could not open codec\n");
383 f = fopen(filename,
"wb");
385 fprintf(stderr,
"Could not open %s\n", filename);
391 fprintf(stderr,
"Could not allocate video frame\n");
403 fprintf(stderr,
"Could not allocate raw picture buffer\n");
416 for(y=0;y<c->
height;y++) {
417 for(x=0;x<c->
width;x++) {
418 frame->
data[0][y * frame->
linesize[0] + x] = x + y + i * 3;
423 for(y=0;y<c->
height/2;y++) {
424 for(x=0;x<c->
width/2;x++) {
425 frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
426 frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
435 fprintf(stderr,
"Error encoding frame\n");
440 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
447 for (got_output = 1; got_output; i++) {
452 fprintf(stderr,
"Error encoding frame\n");
457 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
464 fwrite(endcode, 1,
sizeof(endcode), f);
484 f=fopen(filename,
"w");
485 fprintf(f,
"P5\n%d %d\n%d\n",xsize,ysize,255);
487 fwrite(buf + i * wrap,1,xsize,f);
499 fprintf(stderr,
"Error while decoding frame %d\n", *frame_count);
503 printf(
"Saving %sframe %3d\n", last ?
"last " :
"", *frame_count);
507 snprintf(buf,
sizeof(buf), outfilename, *frame_count);
534 printf(
"Decode video file %s to %s\n", filename, outfilename);
539 fprintf(stderr,
"Codec not found\n");
545 fprintf(stderr,
"Could not allocate video codec context\n");
558 fprintf(stderr,
"Could not open codec\n");
562 f = fopen(filename,
"rb");
564 fprintf(stderr,
"Could not open %s\n", filename);
570 fprintf(stderr,
"Could not allocate video frame\n");
596 while (avpkt.
size > 0)
616 int main(
int argc,
char **argv)
618 const char *output_type;
624 printf(
"usage: %s output_type\n"
625 "API example program to decode/encode a media stream with libavcodec.\n"
626 "This program generates a synthetic stream and encodes it to a file\n"
627 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
628 "The encoded stream is then decoded and written to a raw data output.\n"
629 "output_type must be choosen between 'h264', 'mp2', 'mpg'.\n",
633 output_type = argv[1];
635 if (!strcmp(output_type,
"h264")) {
637 }
else if (!strcmp(output_type,
"mp2")) {
640 }
else if (!strcmp(output_type,
"mpg")) {
644 fprintf(stderr,
"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",
void av_free_packet(AVPacket *pkt)
Free a packet.
This structure describes decoded (raw) audio or video data.
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.
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
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...
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
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...
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
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.
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
enum AVSampleFormat sample_fmt
audio sample format
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.
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.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
#define CODEC_FLAG_TRUNCATED
int width
width and height of the video frame
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
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.
static void video_encode_example(const char *filename, int codec_id)
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int capabilities
Codec capabilities.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define AUDIO_REFILL_THRESH
uint64_t channel_layout
Audio channel layout.
uint64_t channel_layout
Channel layout of the audio data.
static void video_decode_example(const char *outfilename, const char *filename)
#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
int width
picture width / height.
static void audio_encode_example(const char *filename)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
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.
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching 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.
AVSampleFormat
Audio Sample Formats.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
int sample_rate
samples per second
main external API structure.
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
rational number numerator/denominator
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
static int select_sample_rate(AVCodec *codec)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static void audio_decode_example(const char *outfilename, const char *filename)
int channels
number of audio channels
void avcodec_free_frame(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int main(int argc, char **argv)
This structure stores compressed data.
static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
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.
static int select_channel_layout(AVCodec *codec)