35 #ifdef HAVE_AV_CONFIG_H
36 #undef HAVE_AV_CONFIG_H
39 #include "libavcodec/avcodec.h"
40 #include "libavutil/channel_layout.h"
41 #include "libavutil/common.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/samplefmt.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");
235 printf(
"Audio decoding\n");
240 fprintf(stderr,
"codec not found\n");
248 fprintf(stderr,
"could not open codec\n");
252 f = fopen(filename,
"rb");
254 fprintf(stderr,
"could not open %s\n", filename);
257 outfile = fopen(outfilename,
"wb");
267 while (avpkt.
size > 0) {
270 if (!decoded_frame) {
272 fprintf(stderr,
"out of memory\n");
279 fprintf(stderr,
"Error while decoding\n");
287 fwrite(decoded_frame->
data[0], 1, data_size, outfile);
296 memmove(inbuf, avpkt.
data, avpkt.
size);
298 len = fread(avpkt.
data + avpkt.
size, 1,
320 int i, ret, x, y, got_output;
324 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
326 printf(
"Video encoding\n");
331 fprintf(stderr,
"codec not found\n");
351 fprintf(stderr,
"could not open codec\n");
355 f = fopen(filename,
"wb");
357 fprintf(stderr,
"could not open %s\n", filename);
364 fprintf(stderr,
"could not alloc raw picture buffer\n");
380 for(y=0;y<c->
height;y++) {
381 for(x=0;x<c->
width;x++) {
382 picture->
data[0][y * picture->
linesize[0] + x] = x + y + i * 3;
387 for(y=0;y<c->
height/2;y++) {
388 for(x=0;x<c->
width/2;x++) {
389 picture->
data[1][y * picture->
linesize[1] + x] = 128 + y + i * 2;
390 picture->
data[2][y * picture->
linesize[2] + x] = 64 + x + i * 5;
399 fprintf(stderr,
"error encoding frame\n");
404 printf(
"encoding frame %3d (size=%5d)\n", i, pkt.
size);
411 for (got_output = 1; got_output; i++) {
416 fprintf(stderr,
"error encoding frame\n");
421 printf(
"encoding frame %3d (size=%5d)\n", i, pkt.
size);
428 fwrite(endcode, 1,
sizeof(endcode), f);
442 static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
448 f=fopen(filename,
"w");
449 fprintf(f,
"P5\n%d %d\n%d\n",xsize,ysize,255);
451 fwrite(buf + i * wrap,1,xsize,f);
459 int frame, got_picture,
len;
471 printf(
"Video decoding\n");
476 fprintf(stderr,
"codec not found\n");
492 fprintf(stderr,
"could not open codec\n");
498 f = fopen(filename,
"rb");
500 fprintf(stderr,
"could not open %s\n", filename);
526 while (avpkt.
size > 0) {
529 fprintf(stderr,
"Error while decoding frame %d\n", frame);
533 printf(
"saving frame %3d\n", frame);
538 snprintf(buf,
sizeof(buf), outfilename, frame);
555 printf(
"saving last frame %3d\n", frame);
560 snprintf(buf,
sizeof(buf), outfilename, frame);
574 int main(
int argc,
char **argv)
576 const char *filename;
586 filename =
"/tmp/test.mpg";
void av_free_packet(AVPacket *pkt)
Free a packet.
This structure describes decoded (raw) audio or video data.
static void video_decode_example(const char *outfilename, const char *filename)
static void video_encode_example(const char *filename)
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...
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...
static void audio_decode_example(const char *outfilename, const char *filename)
int main(int argc, char **argv)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
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 audio frame data and linesize.
enum AVSampleFormat sample_fmt
audio sample format
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.
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)
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
static int select_channel_layout(AVCodec *codec)
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(). ...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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.
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
int width
picture width / height.
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 ...
static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
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.
int sample_rate
samples per second
main external API structure.
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
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.
#define AUDIO_REFILL_THRESH
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.
int channels
number of audio channels
static void audio_encode_example(const char *filename)
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
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
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.