35 #ifdef HAVE_AV_CONFIG_H 36 #undef HAVE_AV_CONFIG_H 46 #define INBUF_SIZE 4096 47 #define AUDIO_INBUF_SIZE 20480 48 #define AUDIO_REFILL_THRESH 4096 67 int best_samplerate = 0;
74 best_samplerate =
FFMAX(*p, best_samplerate);
77 return best_samplerate;
84 uint64_t best_ch_layout = 0;
85 int best_nb_channels = 0;
94 if (nb_channels > best_nb_channels) {
100 return best_ch_layout;
112 int i, j, k, ret, got_output;
118 printf(
"Audio encoding\n");
123 fprintf(stderr,
"codec not found\n");
135 fprintf(stderr,
"encoder does not support %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");
174 fprintf(stderr,
"could not allocate %d bytes for samples buffer\n",
180 (
const uint8_t*)samples, buffer_size, 0);
182 fprintf(stderr,
"could not setup audio frame\n");
195 samples[2*j] = (int)(sin(t) * 10000);
198 samples[2*j + k] = samples[2*j];
204 fprintf(stderr,
"error encoding audio frame\n");
234 printf(
"Audio decoding\n");
239 fprintf(stderr,
"codec not found\n");
247 fprintf(stderr,
"could not open codec\n");
251 f = fopen(filename,
"rb");
253 fprintf(stderr,
"could not open %s\n", filename);
256 outfile = fopen(outfilename,
"wb");
266 while (avpkt.
size > 0) {
269 if (!decoded_frame) {
271 fprintf(stderr,
"out of memory\n");
278 fprintf(stderr,
"Error while decoding\n");
286 fwrite(decoded_frame->
data[0], 1, data_size, outfile);
295 memmove(inbuf, avpkt.
data, avpkt.
size);
297 len = fread(avpkt.
data + avpkt.
size, 1,
318 int i, ret, x, y, got_output;
322 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
324 printf(
"Video encoding\n");
329 fprintf(stderr,
"codec not found\n");
349 fprintf(stderr,
"could not open codec\n");
353 f = fopen(filename,
"wb");
355 fprintf(stderr,
"could not open %s\n", filename);
362 fprintf(stderr,
"could not alloc raw picture buffer\n");
378 for(y=0;y<c->
height;y++) {
379 for(x=0;x<c->
width;x++) {
380 picture->
data[0][y * picture->
linesize[0] + x] = x + y + i * 3;
385 for(y=0;y<c->
height/2;y++) {
386 for(x=0;x<c->
width/2;x++) {
387 picture->
data[1][y * picture->
linesize[1] + x] = 128 + y + i * 2;
388 picture->
data[2][y * picture->
linesize[2] + x] = 64 + x + i * 5;
397 fprintf(stderr,
"error encoding frame\n");
402 printf(
"encoding frame %3d (size=%5d)\n", i, pkt.
size);
409 for (got_output = 1; got_output; i++) {
414 fprintf(stderr,
"error encoding frame\n");
419 printf(
"encoding frame %3d (size=%5d)\n", i, pkt.
size);
426 fwrite(endcode, 1,
sizeof(endcode), f);
439 static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
445 f=fopen(filename,
"w");
446 fprintf(f,
"P5\n%d %d\n%d\n",xsize,ysize,255);
448 fwrite(buf + i * wrap,1,xsize,f);
456 int frame, got_picture,
len;
468 printf(
"Video decoding\n");
473 fprintf(stderr,
"codec not found\n");
489 fprintf(stderr,
"could not open codec\n");
495 f = fopen(filename,
"rb");
497 fprintf(stderr,
"could not open %s\n", filename);
523 while (avpkt.
size > 0) {
526 fprintf(stderr,
"Error while decoding frame %d\n", frame);
530 printf(
"saving frame %3d\n", frame);
535 snprintf(buf,
sizeof(buf), outfilename, frame);
552 printf(
"saving last frame %3d\n", frame);
557 snprintf(buf,
sizeof(buf), outfilename, frame);
570 int main(
int argc,
char **argv)
572 const char *filename;
582 filename =
"/tmp/test.mpg";
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
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.
static void video_decode_example(const char *outfilename, const char *filename)
static void video_encode_example(const char *filename)
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...
attribute_deprecated int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
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...
attribute_deprecated int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define AV_CH_LAYOUT_STEREO
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
static void audio_decode_example(const char *outfilename, const char *filename)
int main(int argc, char **argv)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
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 audio frame data and linesize.
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).
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
static int select_channel_layout(AVCodec *codec)
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
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(). ...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int capabilities
Codec capabilities.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int flags
AV_CODEC_FLAG_*.
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.
uint64_t channel_layout
Audio channel layout.
uint64_t channel_layout
Channel layout of the audio data.
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 int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
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
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.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
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
attribute_deprecated int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
#define AUDIO_REFILL_THRESH
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define AV_CODEC_CAP_TRUNCATED
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
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)
static int select_sample_rate(AVCodec *codec)
common internal and external API header
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int channels
number of audio channels
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
static void audio_encode_example(const char *filename)
#define AV_CODEC_FLAG_TRUNCATED
Input bitstream might be truncated at a random location instead of only at frame boundaries.
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
attribute_deprecated int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame