2 * Copyright (c) 2001 Fabrice Bellard
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * libavcodec API use example.
27 * @example decoding_encoding.c
28 * Note that libavcodec only handles codecs (MPEG, MPEG-4, etc...),
29 * not file formats (AVI, VOB, MP4, MOV, MKV, MXF, FLV, MPEG-TS, MPEG-PS, etc...).
30 * See library 'libavformat' for the format handling
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
47 /* check that a given sample format is supported by the encoder */
48 static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
50 const enum AVSampleFormat *p = codec->sample_fmts;
52 while (*p != AV_SAMPLE_FMT_NONE) {
60 /* just pick the highest supported samplerate */
61 static int select_sample_rate(AVCodec *codec)
64 int best_samplerate = 0;
66 if (!codec->supported_samplerates)
69 p = codec->supported_samplerates;
71 best_samplerate = FFMAX(*p, best_samplerate);
74 return best_samplerate;
77 /* select layout with the highest channel count */
78 static int select_channel_layout(AVCodec *codec)
81 uint64_t best_ch_layout = 0;
82 int best_nb_channels = 0;
84 if (!codec->channel_layouts)
85 return AV_CH_LAYOUT_STEREO;
87 p = codec->channel_layouts;
89 int nb_channels = av_get_channel_layout_nb_channels(*p);
91 if (nb_channels > best_nb_channels) {
93 best_nb_channels = nb_channels;
97 return best_ch_layout;
101 * Audio encoding example
103 static void audio_encode_example(const char *filename)
106 AVCodecContext *c= NULL;
109 int i, j, k, ret, got_output;
115 printf("Encode audio file %s\n", filename);
117 /* find the MP2 encoder */
118 codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
120 fprintf(stderr, "Codec not found\n");
124 c = avcodec_alloc_context3(codec);
126 fprintf(stderr, "Could not allocate audio codec context\n");
130 /* put sample parameters */
133 /* check that the encoder supports s16 pcm input */
134 c->sample_fmt = AV_SAMPLE_FMT_S16;
135 if (!check_sample_fmt(codec, c->sample_fmt)) {
136 fprintf(stderr, "Encoder does not support sample format %s",
137 av_get_sample_fmt_name(c->sample_fmt));
141 /* select other audio parameters supported by the encoder */
142 c->sample_rate = select_sample_rate(codec);
143 c->channel_layout = select_channel_layout(codec);
144 c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
147 if (avcodec_open2(c, codec, NULL) < 0) {
148 fprintf(stderr, "Could not open codec\n");
152 f = fopen(filename, "wb");
154 fprintf(stderr, "Could not open %s\n", filename);
158 /* frame containing input raw audio */
159 frame = av_frame_alloc();
161 fprintf(stderr, "Could not allocate audio frame\n");
165 frame->nb_samples = c->frame_size;
166 frame->format = c->sample_fmt;
167 frame->channel_layout = c->channel_layout;
169 /* the codec gives us the frame size, in samples,
170 * we calculate the size of the samples buffer in bytes */
171 buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
173 if (buffer_size < 0) {
174 fprintf(stderr, "Could not get sample buffer size\n");
177 samples = av_malloc(buffer_size);
179 fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
183 /* setup the data pointers in the AVFrame */
184 ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
185 (const uint8_t*)samples, buffer_size, 0);
187 fprintf(stderr, "Could not setup audio frame\n");
191 /* encode a single tone sound */
193 tincr = 2 * M_PI * 440.0 / c->sample_rate;
194 for (i = 0; i < 200; i++) {
195 av_init_packet(&pkt);
196 pkt.data = NULL; // packet data will be allocated by the encoder
199 for (j = 0; j < c->frame_size; j++) {
200 samples[2*j] = (int)(sin(t) * 10000);
202 for (k = 1; k < c->channels; k++)
203 samples[2*j + k] = samples[2*j];
206 /* encode the samples */
207 ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
209 fprintf(stderr, "Error encoding audio frame\n");
213 fwrite(pkt.data, 1, pkt.size, f);
214 av_packet_unref(&pkt);
218 /* get the delayed frames */
219 for (got_output = 1; got_output; i++) {
220 ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
222 fprintf(stderr, "Error encoding frame\n");
227 fwrite(pkt.data, 1, pkt.size, f);
228 av_packet_unref(&pkt);
234 av_frame_free(&frame);
235 avcodec_free_context(&c);
241 static void audio_decode_example(const char *outfilename, const char *filename)
244 AVCodecContext *c= NULL;
247 uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
249 AVFrame *decoded_frame = NULL;
251 av_init_packet(&avpkt);
253 printf("Decode audio file %s to %s\n", filename, outfilename);
255 /* find the MPEG audio decoder */
256 codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
258 fprintf(stderr, "Codec not found\n");
262 c = avcodec_alloc_context3(codec);
264 fprintf(stderr, "Could not allocate audio codec context\n");
269 if (avcodec_open2(c, codec, NULL) < 0) {
270 fprintf(stderr, "Could not open codec\n");
274 f = fopen(filename, "rb");
276 fprintf(stderr, "Could not open %s\n", filename);
279 outfile = fopen(outfilename, "wb");
285 /* decode until eof */
287 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
289 while (avpkt.size > 0) {
293 if (!decoded_frame) {
294 if (!(decoded_frame = av_frame_alloc())) {
295 fprintf(stderr, "Could not allocate audio frame\n");
300 len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
302 fprintf(stderr, "Error while decoding\n");
306 /* if a frame has been decoded, output it */
307 int data_size = av_get_bytes_per_sample(c->sample_fmt);
309 /* This should not occur, checking just for paranoia */
310 fprintf(stderr, "Failed to calculate data size\n");
313 for (i=0; i<decoded_frame->nb_samples; i++)
314 for (ch=0; ch<c->channels; ch++)
315 fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
320 avpkt.pts = AV_NOPTS_VALUE;
321 if (avpkt.size < AUDIO_REFILL_THRESH) {
322 /* Refill the input buffer, to avoid trying to decode
323 * incomplete frames. Instead of this, one could also use
324 * a parser, or use a proper container format through
326 memmove(inbuf, avpkt.data, avpkt.size);
328 len = fread(avpkt.data + avpkt.size, 1,
329 AUDIO_INBUF_SIZE - avpkt.size, f);
338 avcodec_free_context(&c);
339 av_frame_free(&decoded_frame);
343 * Video encoding example
345 static void video_encode_example(const char *filename, int codec_id)
348 AVCodecContext *c= NULL;
349 int i, ret, x, y, got_output;
353 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
355 printf("Encode video file %s\n", filename);
357 /* find the video encoder */
358 codec = avcodec_find_encoder(codec_id);
360 fprintf(stderr, "Codec not found\n");
364 c = avcodec_alloc_context3(codec);
366 fprintf(stderr, "Could not allocate video codec context\n");
370 /* put sample parameters */
371 c->bit_rate = 400000;
372 /* resolution must be a multiple of two */
375 /* frames per second */
376 c->time_base = (AVRational){1,25};
377 /* emit one intra frame every ten frames
378 * check frame pict_type before passing frame
379 * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
380 * then gop_size is ignored and the output of encoder
381 * will always be I frame irrespective to gop_size
385 c->pix_fmt = AV_PIX_FMT_YUV420P;
387 if (codec_id == AV_CODEC_ID_H264)
388 av_opt_set(c->priv_data, "preset", "slow", 0);
391 if (avcodec_open2(c, codec, NULL) < 0) {
392 fprintf(stderr, "Could not open codec\n");
396 f = fopen(filename, "wb");
398 fprintf(stderr, "Could not open %s\n", filename);
402 frame = av_frame_alloc();
404 fprintf(stderr, "Could not allocate video frame\n");
407 frame->format = c->pix_fmt;
408 frame->width = c->width;
409 frame->height = c->height;
411 /* the image can be allocated by any means and av_image_alloc() is
412 * just the most convenient way if av_malloc() is to be used */
413 ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
416 fprintf(stderr, "Could not allocate raw picture buffer\n");
420 /* encode 1 second of video */
421 for (i = 0; i < 25; i++) {
422 av_init_packet(&pkt);
423 pkt.data = NULL; // packet data will be allocated by the encoder
427 /* prepare a dummy image */
429 for (y = 0; y < c->height; y++) {
430 for (x = 0; x < c->width; x++) {
431 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
436 for (y = 0; y < c->height/2; y++) {
437 for (x = 0; x < c->width/2; x++) {
438 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
439 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
445 /* encode the image */
446 ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
448 fprintf(stderr, "Error encoding frame\n");
453 printf("Write frame %3d (size=%5d)\n", i, pkt.size);
454 fwrite(pkt.data, 1, pkt.size, f);
455 av_packet_unref(&pkt);
459 /* get the delayed frames */
460 for (got_output = 1; got_output; i++) {
463 ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
465 fprintf(stderr, "Error encoding frame\n");
470 printf("Write frame %3d (size=%5d)\n", i, pkt.size);
471 fwrite(pkt.data, 1, pkt.size, f);
472 av_packet_unref(&pkt);
476 /* add sequence end code to have a real MPEG file */
477 fwrite(endcode, 1, sizeof(endcode), f);
480 avcodec_free_context(&c);
481 av_freep(&frame->data[0]);
482 av_frame_free(&frame);
487 * Video decoding example
490 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
496 f = fopen(filename,"w");
497 fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
498 for (i = 0; i < ysize; i++)
499 fwrite(buf + i * wrap, 1, xsize, f);
503 static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
504 AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
509 len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
511 fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
515 printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
518 /* the picture is allocated by the decoder, no need to free it */
519 snprintf(buf, sizeof(buf), outfilename, *frame_count);
520 pgm_save(frame->data[0], frame->linesize[0],
521 frame->width, frame->height, buf);
531 static void video_decode_example(const char *outfilename, const char *filename)
534 AVCodecContext *c= NULL;
538 uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
541 av_init_packet(&avpkt);
543 /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
544 memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
546 printf("Decode video file %s to %s\n", filename, outfilename);
548 /* find the MPEG-1 video decoder */
549 codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
551 fprintf(stderr, "Codec not found\n");
555 c = avcodec_alloc_context3(codec);
557 fprintf(stderr, "Could not allocate video codec context\n");
561 if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
562 c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
564 /* For some codecs, such as msmpeg4 and mpeg4, width and height
565 MUST be initialized there because this information is not
566 available in the bitstream. */
569 if (avcodec_open2(c, codec, NULL) < 0) {
570 fprintf(stderr, "Could not open codec\n");
574 f = fopen(filename, "rb");
576 fprintf(stderr, "Could not open %s\n", filename);
580 frame = av_frame_alloc();
582 fprintf(stderr, "Could not allocate video frame\n");
588 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
592 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
593 and this is the only method to use them because you cannot
594 know the compressed data size before analysing it.
596 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
597 based, so you must call them with all the data for one
598 frame exactly. You must also initialize 'width' and
599 'height' before initializing them. */
601 /* NOTE2: some codecs allow the raw parameters (frame size,
602 sample rate) to be changed at any frame. We handle this, so
603 you should also take care of it */
605 /* here, we use a stream based decoder (mpeg1video), so we
606 feed decoder and see if it could decode a frame */
608 while (avpkt.size > 0)
609 if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
613 /* Some codecs, such as MPEG, transmit the I- and P-frame with a
614 latency of one frame. You must do the following to have a
615 chance to get the last frame of the video. */
618 decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
622 avcodec_free_context(&c);
623 av_frame_free(&frame);
627 int main(int argc, char **argv)
629 const char *output_type;
631 /* register all the codecs */
632 avcodec_register_all();
635 printf("usage: %s output_type\n"
636 "API example program to decode/encode a media stream with libavcodec.\n"
637 "This program generates a synthetic stream and encodes it to a file\n"
638 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
639 "The encoded stream is then decoded and written to a raw data output.\n"
640 "output_type must be chosen between 'h264', 'mp2', 'mpg'.\n",
644 output_type = argv[1];
646 if (!strcmp(output_type, "h264")) {
647 video_encode_example("test.h264", AV_CODEC_ID_H264);
648 } else if (!strcmp(output_type, "mp2")) {
649 audio_encode_example("test.mp2");
650 audio_decode_example("test.pcm", "test.mp2");
651 } else if (!strcmp(output_type, "mpg")) {
652 video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO);
653 video_decode_example("test%02d.pgm", "test.mpg");
655 fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",