35 #ifdef HAVE_AV_CONFIG_H
36 #undef HAVE_AV_CONFIG_H
43 #define INBUF_SIZE 4096
44 #define AUDIO_INBUF_SIZE 20480
45 #define AUDIO_REFILL_THRESH 4096
54 int frame_size, i, j, out_size, outbuf_size;
60 printf(
"Audio encoding\n");
65 fprintf(stderr,
"codec not found\n");
77 if (avcodec_open(c, codec) < 0) {
78 fprintf(stderr,
"could not open codec\n");
86 outbuf =
malloc(outbuf_size);
88 f = fopen(filename,
"wb");
90 fprintf(stderr,
"could not open %s\n", filename);
98 for(j=0;j<frame_size;j++) {
99 samples[2*j] = (int)(sin(t) * 10000);
100 samples[2*j+1] = samples[2*j];
104 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
105 fwrite(outbuf, 1, out_size, f);
130 printf(
"Audio decoding\n");
135 fprintf(stderr,
"codec not found\n");
142 if (avcodec_open(c, codec) < 0) {
143 fprintf(stderr,
"could not open codec\n");
147 f = fopen(filename,
"rb");
149 fprintf(stderr,
"could not open %s\n", filename);
152 outfile = fopen(outfilename,
"wb");
162 while (avpkt.
size > 0) {
165 if (!decoded_frame) {
167 fprintf(stderr,
"out of memory\n");
175 fprintf(stderr,
"Error while decoding\n");
183 fwrite(decoded_frame->
data[0], 1, data_size, outfile);
192 memmove(inbuf, avpkt.
data, avpkt.
size);
194 len = fread(avpkt.
data + avpkt.
size, 1,
216 int i, out_size,
size, x, y, outbuf_size;
219 uint8_t *outbuf, *picture_buf;
221 printf(
"Video encoding\n");
226 fprintf(stderr,
"codec not found\n");
245 if (avcodec_open(c, codec) < 0) {
246 fprintf(stderr,
"could not open codec\n");
250 f = fopen(filename,
"wb");
252 fprintf(stderr,
"could not open %s\n", filename);
257 outbuf_size = 100000;
258 outbuf =
malloc(outbuf_size);
260 picture_buf =
malloc((size * 3) / 2);
262 picture->
data[0] = picture_buf;
264 picture->
data[2] = picture->
data[1] + size / 4;
274 for(y=0;y<c->
height;y++) {
275 for(x=0;x<c->
width;x++) {
276 picture->
data[0][y * picture->
linesize[0] + x] = x + y + i * 3;
281 for(y=0;y<c->
height/2;y++) {
282 for(x=0;x<c->
width/2;x++) {
283 picture->
data[1][y * picture->
linesize[1] + x] = 128 + y + i * 2;
284 picture->
data[2][y * picture->
linesize[2] + x] = 64 + x + i * 5;
290 printf(
"encoding frame %3d (size=%5d)\n", i, out_size);
291 fwrite(outbuf, 1, out_size, f);
295 for(; out_size; i++) {
299 printf(
"write frame %3d (size=%5d)\n", i, out_size);
300 fwrite(outbuf, 1, out_size, f);
308 fwrite(outbuf, 1, 4, f);
323 static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
329 f=fopen(filename,
"w");
330 fprintf(f,
"P5\n%d %d\n%d\n",xsize,ysize,255);
332 fwrite(buf + i * wrap,1,xsize,f);
340 int frame, got_picture,
len;
352 printf(
"Video decoding\n");
357 fprintf(stderr,
"codec not found\n");
372 if (avcodec_open(c, codec) < 0) {
373 fprintf(stderr,
"could not open codec\n");
379 f = fopen(filename,
"rb");
381 fprintf(stderr,
"could not open %s\n", filename);
407 while (avpkt.
size > 0) {
410 fprintf(stderr,
"Error while decoding frame %d\n", frame);
414 printf(
"saving frame %3d\n", frame);
419 snprintf(buf,
sizeof(buf), outfilename, frame);
436 printf(
"saving last frame %3d\n", frame);
441 snprintf(buf,
sizeof(buf), outfilename, frame);
455 int main(
int argc,
char **argv)
457 const char *filename;
470 filename =
"/tmp/test.mpg";
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
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.
AVCodec * avcodec_find_encoder(enum CodecID id)
Find a registered encoder with a matching codec ID.
static void audio_decode_example(const char *outfilename, 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.
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...
int main(int argc, char **argv)
AVCodec * avcodec_find_decoder(enum CodecID id)
Find a registered decoder with a matching codec ID.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
enum AVSampleFormat sample_fmt
audio sample format
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
AVCodecContext * avcodec_alloc_context3(AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define CODEC_CAP_TRUNCATED
int capabilities
Codec capabilities.
#define CODEC_FLAG_TRUNCATED
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
struct AVRational AVRational
rational number numerator/denominator
int bit_rate
the average bitrate
static void audio_encode_example(const char *filename)
int width
picture width / height.
static void video_decode_example(const char *outfilename, const char *filename)
int frame_size
Samples per packet, initialized when calling 'init'.
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
main external API structure.
static void video_encode_example(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 avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVFrame *pict)
Encode a video frame from pict into buf.
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
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
static void avcodec_init(void)
#define AUDIO_REFILL_THRESH
int channels
number of audio channels
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
int nb_samples
number of audio samples (per channel) described by this frame