]> git.sesse.net Git - ffmpeg/blob - libavcodec/api-example.c
62750804baf5646df4a68d41d1b7d9ac178c1070
[ffmpeg] / libavcodec / api-example.c
1 /*
2  * copyright (c) 2001 Fabrice Bellard
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * libavcodec API use example.
24  *
25  * @example libavcodec/api-example.c
26  * Note that this library only handles codecs (mpeg, mpeg4, etc...),
27  * not file formats (avi, vob, etc...). See library 'libavformat' for the
28  * format handling
29  */
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #ifdef HAVE_AV_CONFIG_H
36 #undef HAVE_AV_CONFIG_H
37 #endif
38
39 #include "libavcodec/avcodec.h"
40 #include "libavutil/audioconvert.h"
41 #include "libavutil/common.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/samplefmt.h"
45
46 #define INBUF_SIZE 4096
47 #define AUDIO_INBUF_SIZE 20480
48 #define AUDIO_REFILL_THRESH 4096
49
50 /* check that a given sample format is supported by the encoder */
51 static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
52 {
53     const enum AVSampleFormat *p = codec->sample_fmts;
54
55     while (*p != AV_SAMPLE_FMT_NONE) {
56         if (*p == sample_fmt)
57             return 1;
58         p++;
59     }
60     return 0;
61 }
62
63 /* just pick the highest supported samplerate */
64 static int select_sample_rate(AVCodec *codec)
65 {
66     const int *p;
67     int best_samplerate = 0;
68
69     if (!codec->supported_samplerates)
70         return 44100;
71
72     p = codec->supported_samplerates;
73     while (*p) {
74         best_samplerate = FFMAX(*p, best_samplerate);
75         p++;
76     }
77     return best_samplerate;
78 }
79
80 /* select layout with the highest channel count */
81 static int select_channel_layout(AVCodec *codec)
82 {
83     const uint64_t *p;
84     uint64_t best_ch_layout = 0;
85     int best_nb_channells   = 0;
86
87     if (!codec->channel_layouts)
88         return AV_CH_LAYOUT_STEREO;
89
90     p = codec->channel_layouts;
91     while (*p) {
92         int nb_channels = av_get_channel_layout_nb_channels(*p);
93
94         if (nb_channels > best_nb_channells) {
95             best_ch_layout    = *p;
96             best_nb_channells = nb_channels;
97         }
98         p++;
99     }
100     return best_ch_layout;
101 }
102
103 /*
104  * Audio encoding example
105  */
106 static void audio_encode_example(const char *filename)
107 {
108     AVCodec *codec;
109     AVCodecContext *c= NULL;
110     AVFrame *frame;
111     AVPacket pkt;
112     int i, j, k, ret, got_output;
113     int buffer_size;
114     FILE *f;
115     uint16_t *samples;
116     float t, tincr;
117
118     printf("Audio encoding\n");
119
120     /* find the MP2 encoder */
121     codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
122     if (!codec) {
123         fprintf(stderr, "codec not found\n");
124         exit(1);
125     }
126
127     c = avcodec_alloc_context3(codec);
128
129     /* put sample parameters */
130     c->bit_rate = 64000;
131
132     /* check that the encoder supports s16 pcm input */
133     c->sample_fmt = AV_SAMPLE_FMT_S16;
134     if (!check_sample_fmt(codec, c->sample_fmt)) {
135         fprintf(stderr, "encoder does not support %s",
136                 av_get_sample_fmt_name(c->sample_fmt));
137         exit(1);
138     }
139
140     /* select other audio parameters supported by the encoder */
141     c->sample_rate    = select_sample_rate(codec);
142     c->channel_layout = select_channel_layout(codec);
143     c->channels       = av_get_channel_layout_nb_channels(c->channel_layout);
144
145     /* open it */
146     if (avcodec_open2(c, codec, NULL) < 0) {
147         fprintf(stderr, "could not open codec\n");
148         exit(1);
149     }
150
151     f = fopen(filename, "wb");
152     if (!f) {
153         fprintf(stderr, "could not open %s\n", filename);
154         exit(1);
155     }
156
157     /* frame containing input raw audio */
158     frame = avcodec_alloc_frame();
159     if (!frame) {
160         fprintf(stderr, "could not allocate audio frame\n");
161         exit(1);
162     }
163
164     frame->nb_samples     = c->frame_size;
165     frame->format         = c->sample_fmt;
166     frame->channel_layout = c->channel_layout;
167
168     /* the codec gives us the frame size, in samples,
169      * we calculate the size of the samples buffer in bytes */
170     buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
171                                              c->sample_fmt, 0);
172     samples = av_malloc(buffer_size);
173     if (!samples) {
174         fprintf(stderr, "could not allocate %d bytes for samples buffer\n",
175                 buffer_size);
176         exit(1);
177     }
178     /* setup the data pointers in the AVFrame */
179     ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
180                                    (const uint8_t*)samples, buffer_size, 0);
181     if (ret < 0) {
182         fprintf(stderr, "could not setup audio frame\n");
183         exit(1);
184     }
185
186     /* encode a single tone sound */
187     t = 0;
188     tincr = 2 * M_PI * 440.0 / c->sample_rate;
189     for(i=0;i<200;i++) {
190         av_init_packet(&pkt);
191         pkt.data = NULL; // packet data will be allocated by the encoder
192         pkt.size = 0;
193
194         for (j = 0; j < c->frame_size; j++) {
195             samples[2*j] = (int)(sin(t) * 10000);
196
197             for (k = 1; k < c->channels; k++)
198                 samples[2*j + k] = samples[2*j];
199             t += tincr;
200         }
201         /* encode the samples */
202         ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
203         if (ret < 0) {
204             fprintf(stderr, "error encoding audio frame\n");
205             exit(1);
206         }
207         if (got_output) {
208             fwrite(pkt.data, 1, pkt.size, f);
209             av_free_packet(&pkt);
210         }
211     }
212     fclose(f);
213
214     av_freep(&samples);
215     avcodec_free_frame(&frame);
216     avcodec_close(c);
217     av_free(c);
218 }
219
220 /*
221  * Audio decoding.
222  */
223 static void audio_decode_example(const char *outfilename, const char *filename)
224 {
225     AVCodec *codec;
226     AVCodecContext *c= NULL;
227     int len;
228     FILE *f, *outfile;
229     uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
230     AVPacket avpkt;
231     AVFrame *decoded_frame = NULL;
232
233     av_init_packet(&avpkt);
234
235     printf("Audio decoding\n");
236
237     /* find the mpeg audio decoder */
238     codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
239     if (!codec) {
240         fprintf(stderr, "codec not found\n");
241         exit(1);
242     }
243
244     c = avcodec_alloc_context3(codec);
245
246     /* open it */
247     if (avcodec_open2(c, codec, NULL) < 0) {
248         fprintf(stderr, "could not open codec\n");
249         exit(1);
250     }
251
252     f = fopen(filename, "rb");
253     if (!f) {
254         fprintf(stderr, "could not open %s\n", filename);
255         exit(1);
256     }
257     outfile = fopen(outfilename, "wb");
258     if (!outfile) {
259         av_free(c);
260         exit(1);
261     }
262
263     /* decode until eof */
264     avpkt.data = inbuf;
265     avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
266
267     while (avpkt.size > 0) {
268         int got_frame = 0;
269
270         if (!decoded_frame) {
271             if (!(decoded_frame = avcodec_alloc_frame())) {
272                 fprintf(stderr, "out of memory\n");
273                 exit(1);
274             }
275         } else
276             avcodec_get_frame_defaults(decoded_frame);
277
278         len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
279         if (len < 0) {
280             fprintf(stderr, "Error while decoding\n");
281             exit(1);
282         }
283         if (got_frame) {
284             /* if a frame has been decoded, output it */
285             int data_size = av_samples_get_buffer_size(NULL, c->channels,
286                                                        decoded_frame->nb_samples,
287                                                        c->sample_fmt, 1);
288             fwrite(decoded_frame->data[0], 1, data_size, outfile);
289         }
290         avpkt.size -= len;
291         avpkt.data += len;
292         if (avpkt.size < AUDIO_REFILL_THRESH) {
293             /* Refill the input buffer, to avoid trying to decode
294              * incomplete frames. Instead of this, one could also use
295              * a parser, or use a proper container format through
296              * libavformat. */
297             memmove(inbuf, avpkt.data, avpkt.size);
298             avpkt.data = inbuf;
299             len = fread(avpkt.data + avpkt.size, 1,
300                         AUDIO_INBUF_SIZE - avpkt.size, f);
301             if (len > 0)
302                 avpkt.size += len;
303         }
304     }
305
306     fclose(outfile);
307     fclose(f);
308
309     avcodec_close(c);
310     av_free(c);
311     avcodec_free_frame(&decoded_frame);
312 }
313
314 /*
315  * Video encoding example
316  */
317 static void video_encode_example(const char *filename)
318 {
319     AVCodec *codec;
320     AVCodecContext *c= NULL;
321     int i, ret, x, y, got_output;
322     FILE *f;
323     AVFrame *picture;
324     AVPacket pkt;
325     uint8_t endcode[] = { 0, 0, 1, 0xb7 };
326
327     printf("Video encoding\n");
328
329     /* find the mpeg1 video encoder */
330     codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
331     if (!codec) {
332         fprintf(stderr, "codec not found\n");
333         exit(1);
334     }
335
336     c = avcodec_alloc_context3(codec);
337     picture= avcodec_alloc_frame();
338
339     /* put sample parameters */
340     c->bit_rate = 400000;
341     /* resolution must be a multiple of two */
342     c->width = 352;
343     c->height = 288;
344     /* frames per second */
345     c->time_base= (AVRational){1,25};
346     c->gop_size = 10; /* emit one intra frame every ten frames */
347     c->max_b_frames=1;
348     c->pix_fmt = PIX_FMT_YUV420P;
349
350     /* open it */
351     if (avcodec_open2(c, codec, NULL) < 0) {
352         fprintf(stderr, "could not open codec\n");
353         exit(1);
354     }
355
356     f = fopen(filename, "wb");
357     if (!f) {
358         fprintf(stderr, "could not open %s\n", filename);
359         exit(1);
360     }
361
362     ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height,
363                          c->pix_fmt, 32);
364     if (ret < 0) {
365         fprintf(stderr, "could not alloc raw picture buffer\n");
366         exit(1);
367     }
368     picture->format = c->pix_fmt;
369     picture->width  = c->width;
370     picture->height = c->height;
371
372     /* encode 1 second of video */
373     for(i=0;i<25;i++) {
374         av_init_packet(&pkt);
375         pkt.data = NULL;    // packet data will be allocated by the encoder
376         pkt.size = 0;
377
378         fflush(stdout);
379         /* prepare a dummy image */
380         /* Y */
381         for(y=0;y<c->height;y++) {
382             for(x=0;x<c->width;x++) {
383                 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
384             }
385         }
386
387         /* Cb and Cr */
388         for(y=0;y<c->height/2;y++) {
389             for(x=0;x<c->width/2;x++) {
390                 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
391                 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
392             }
393         }
394
395         picture->pts = i;
396
397         /* encode the image */
398         ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
399         if (ret < 0) {
400             fprintf(stderr, "error encoding frame\n");
401             exit(1);
402         }
403
404         if (got_output) {
405             printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
406             fwrite(pkt.data, 1, pkt.size, f);
407             av_free_packet(&pkt);
408         }
409     }
410
411     /* get the delayed frames */
412     for (got_output = 1; got_output; i++) {
413         fflush(stdout);
414
415         ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
416         if (ret < 0) {
417             fprintf(stderr, "error encoding frame\n");
418             exit(1);
419         }
420
421         if (got_output) {
422             printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
423             fwrite(pkt.data, 1, pkt.size, f);
424             av_free_packet(&pkt);
425         }
426     }
427
428     /* add sequence end code to have a real mpeg file */
429     fwrite(endcode, 1, sizeof(endcode), f);
430     fclose(f);
431
432     avcodec_close(c);
433     av_free(c);
434     av_freep(&picture->data[0]);
435     avcodec_free_frame(&picture);
436     printf("\n");
437 }
438
439 /*
440  * Video decoding example
441  */
442
443 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
444                      char *filename)
445 {
446     FILE *f;
447     int i;
448
449     f=fopen(filename,"w");
450     fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
451     for(i=0;i<ysize;i++)
452         fwrite(buf + i * wrap,1,xsize,f);
453     fclose(f);
454 }
455
456 static void video_decode_example(const char *outfilename, const char *filename)
457 {
458     AVCodec *codec;
459     AVCodecContext *c= NULL;
460     int frame, got_picture, len;
461     FILE *f;
462     AVFrame *picture;
463     uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
464     char buf[1024];
465     AVPacket avpkt;
466
467     av_init_packet(&avpkt);
468
469     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
470     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
471
472     printf("Video decoding\n");
473
474     /* find the mpeg1 video decoder */
475     codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
476     if (!codec) {
477         fprintf(stderr, "codec not found\n");
478         exit(1);
479     }
480
481     c = avcodec_alloc_context3(codec);
482     picture= avcodec_alloc_frame();
483
484     if(codec->capabilities&CODEC_CAP_TRUNCATED)
485         c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
486
487     /* For some codecs, such as msmpeg4 and mpeg4, width and height
488        MUST be initialized there because this information is not
489        available in the bitstream. */
490
491     /* open it */
492     if (avcodec_open2(c, codec, NULL) < 0) {
493         fprintf(stderr, "could not open codec\n");
494         exit(1);
495     }
496
497     /* the codec gives us the frame size, in samples */
498
499     f = fopen(filename, "rb");
500     if (!f) {
501         fprintf(stderr, "could not open %s\n", filename);
502         exit(1);
503     }
504
505     frame = 0;
506     for(;;) {
507         avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
508         if (avpkt.size == 0)
509             break;
510
511         /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
512            and this is the only method to use them because you cannot
513            know the compressed data size before analysing it.
514
515            BUT some other codecs (msmpeg4, mpeg4) are inherently frame
516            based, so you must call them with all the data for one
517            frame exactly. You must also initialize 'width' and
518            'height' before initializing them. */
519
520         /* NOTE2: some codecs allow the raw parameters (frame size,
521            sample rate) to be changed at any frame. We handle this, so
522            you should also take care of it */
523
524         /* here, we use a stream based decoder (mpeg1video), so we
525            feed decoder and see if it could decode a frame */
526         avpkt.data = inbuf;
527         while (avpkt.size > 0) {
528             len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
529             if (len < 0) {
530                 fprintf(stderr, "Error while decoding frame %d\n", frame);
531                 exit(1);
532             }
533             if (got_picture) {
534                 printf("saving frame %3d\n", frame);
535                 fflush(stdout);
536
537                 /* the picture is allocated by the decoder. no need to
538                    free it */
539                 snprintf(buf, sizeof(buf), outfilename, frame);
540                 pgm_save(picture->data[0], picture->linesize[0],
541                          c->width, c->height, buf);
542                 frame++;
543             }
544             avpkt.size -= len;
545             avpkt.data += len;
546         }
547     }
548
549     /* some codecs, such as MPEG, transmit the I and P frame with a
550        latency of one frame. You must do the following to have a
551        chance to get the last frame of the video */
552     avpkt.data = NULL;
553     avpkt.size = 0;
554     len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
555     if (got_picture) {
556         printf("saving last frame %3d\n", frame);
557         fflush(stdout);
558
559         /* the picture is allocated by the decoder. no need to
560            free it */
561         snprintf(buf, sizeof(buf), outfilename, frame);
562         pgm_save(picture->data[0], picture->linesize[0],
563                  c->width, c->height, buf);
564         frame++;
565     }
566
567     fclose(f);
568
569     avcodec_close(c);
570     av_free(c);
571     avcodec_free_frame(&picture);
572     printf("\n");
573 }
574
575 int main(int argc, char **argv)
576 {
577     const char *filename;
578
579     /* register all the codecs */
580     avcodec_register_all();
581
582     if (argc <= 1) {
583         audio_encode_example("/tmp/test.mp2");
584         audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
585
586         video_encode_example("/tmp/test.mpg");
587         filename = "/tmp/test.mpg";
588     } else {
589         filename = argv[1];
590     }
591
592     //    audio_decode_example("/tmp/test.sw", filename);
593     video_decode_example("/tmp/test%d.pgm", filename);
594
595     return 0;
596 }