apiexample.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:12k
源码类别:

Windows CE

开发平台:

C/C++

  1. /**
  2.  * @file apiexample.c
  3.  * avcodec API use example.
  4.  *
  5.  * Note that this library only handles codecs (mpeg, mpeg4, etc...),
  6.  * not file formats (avi, vob, etc...). See library 'libavformat' for the
  7.  * format handling 
  8.  */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #ifdef HAVE_AV_CONFIG_H
  14. #undef HAVE_AV_CONFIG_H
  15. #endif
  16. #include "avcodec.h"
  17. #define INBUF_SIZE 4096
  18. /*
  19.  * Audio encoding example 
  20.  */
  21. void audio_encode_example(const char *filename)
  22. {
  23.     AVCodec *codec;
  24.     AVCodecContext *c= NULL;
  25.     int frame_size, i, j, out_size, outbuf_size;
  26.     FILE *f;
  27.     short *samples;
  28.     float t, tincr;
  29.     uint8_t *outbuf;
  30.     printf("Audio encodingn");
  31.     /* find the MP2 encoder */
  32.     codec = avcodec_find_encoder(CODEC_ID_MP2);
  33.     if (!codec) {
  34.         fprintf(stderr, "codec not foundn");
  35.         exit(1);
  36.     }
  37.     c= avcodec_alloc_context();
  38.     
  39.     /* put sample parameters */
  40.     c->bit_rate = 64000;
  41.     c->sample_rate = 44100;
  42.     c->channels = 2;
  43.     /* open it */
  44.     if (avcodec_open(c, codec) < 0) {
  45.         fprintf(stderr, "could not open codecn");
  46.         exit(1);
  47.     }
  48.     
  49.     /* the codec gives us the frame size, in samples */
  50.     frame_size = c->frame_size;
  51.     samples = malloc(frame_size * 2 * c->channels);
  52.     outbuf_size = 10000;
  53.     outbuf = malloc(outbuf_size);
  54.     f = fopen(filename, "wb");
  55.     if (!f) {
  56.         fprintf(stderr, "could not open %sn", filename);
  57.         exit(1);
  58.     }
  59.         
  60.     /* encode a single tone sound */
  61.     t = 0;
  62.     tincr = 2 * M_PI * 440.0 / c->sample_rate;
  63.     for(i=0;i<200;i++) {
  64.         for(j=0;j<frame_size;j++) {
  65.             samples[2*j] = (int)(sin(t) * 10000);
  66.             samples[2*j+1] = samples[2*j];
  67.             t += tincr;
  68.         }
  69.         /* encode the samples */
  70.         out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
  71.         fwrite(outbuf, 1, out_size, f);
  72.     }
  73.     fclose(f);
  74.     free(outbuf);
  75.     free(samples);
  76.     avcodec_close(c);
  77.     av_free(c);
  78. }
  79. /*
  80.  * Audio decoding. 
  81.  */
  82. void audio_decode_example(const char *outfilename, const char *filename)
  83. {
  84.     AVCodec *codec;
  85.     AVCodecContext *c= NULL;
  86.     int out_size, size, len;
  87.     FILE *f, *outfile;
  88.     uint8_t *outbuf;
  89.     uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
  90.     printf("Audio decodingn");
  91.     
  92.     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
  93.     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  94.     /* find the mpeg audio decoder */
  95.     codec = avcodec_find_decoder(CODEC_ID_MP2);
  96.     if (!codec) {
  97.         fprintf(stderr, "codec not foundn");
  98.         exit(1);
  99.     }
  100.     c= avcodec_alloc_context();
  101.     /* open it */
  102.     if (avcodec_open(c, codec) < 0) {
  103.         fprintf(stderr, "could not open codecn");
  104.         exit(1);
  105.     }
  106.     
  107.     outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
  108.     f = fopen(filename, "rb");
  109.     if (!f) {
  110.         fprintf(stderr, "could not open %sn", filename);
  111.         exit(1);
  112.     }
  113.     outfile = fopen(outfilename, "wb");
  114.     if (!outfile) {
  115.         av_free(c);
  116.         exit(1);
  117.     }
  118.         
  119.     /* decode until eof */
  120.     inbuf_ptr = inbuf;
  121.     for(;;) {
  122.         size = fread(inbuf, 1, INBUF_SIZE, f);
  123.         if (size == 0)
  124.             break;
  125.         inbuf_ptr = inbuf;
  126.         while (size > 0) {
  127.             len = avcodec_decode_audio(c, (short *)outbuf, &out_size, 
  128.                                        inbuf_ptr, size);
  129.             if (len < 0) {
  130.                 fprintf(stderr, "Error while decodingn");
  131.                 exit(1);
  132.             }
  133.             if (out_size > 0) {
  134.                 /* if a frame has been decoded, output it */
  135.                 fwrite(outbuf, 1, out_size, outfile);
  136.             }
  137.             size -= len;
  138.             inbuf_ptr += len;
  139.         }
  140.     }
  141.     fclose(outfile);
  142.     fclose(f);
  143.     free(outbuf);
  144.     avcodec_close(c);
  145.     av_free(c);
  146. }
  147. /*
  148.  * Video encoding example 
  149.  */
  150. void video_encode_example(const char *filename)
  151. {
  152.     AVCodec *codec;
  153.     AVCodecContext *c= NULL;
  154.     int i, out_size, size, x, y, outbuf_size;
  155.     FILE *f;
  156.     AVFrame *picture;
  157.     uint8_t *outbuf, *picture_buf;
  158.     printf("Video encodingn");
  159.     /* find the mpeg1 video encoder */
  160.     codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
  161.     if (!codec) {
  162.         fprintf(stderr, "codec not foundn");
  163.         exit(1);
  164.     }
  165.     c= avcodec_alloc_context();
  166.     picture= avcodec_alloc_frame();
  167.     
  168.     /* put sample parameters */
  169.     c->bit_rate = 400000;
  170.     /* resolution must be a multiple of two */
  171.     c->width = 352;  
  172.     c->height = 288;
  173.     /* frames per second */
  174.     c->time_base= (AVRational){1,25};
  175.     c->gop_size = 10; /* emit one intra frame every ten frames */
  176.     c->max_b_frames=1;
  177.     c->pix_fmt = PIX_FMT_YUV420P;
  178.     /* open it */
  179.     if (avcodec_open(c, codec) < 0) {
  180.         fprintf(stderr, "could not open codecn");
  181.         exit(1);
  182.     }
  183.     
  184.     /* the codec gives us the frame size, in samples */
  185.     f = fopen(filename, "wb");
  186.     if (!f) {
  187.         fprintf(stderr, "could not open %sn", filename);
  188.         exit(1);
  189.     }
  190.     
  191.     /* alloc image and output buffer */
  192.     outbuf_size = 100000;
  193.     outbuf = malloc(outbuf_size);
  194.     size = c->width * c->height;
  195.     picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
  196.     
  197.     picture->data[0] = picture_buf;
  198.     picture->data[1] = picture->data[0] + size;
  199.     picture->data[2] = picture->data[1] + size / 4;
  200.     picture->linesize[0] = c->width;
  201.     picture->linesize[1] = c->width / 2;
  202.     picture->linesize[2] = c->width / 2;
  203.     /* encode 1 second of video */
  204.     for(i=0;i<25;i++) {
  205.         fflush(stdout);
  206.         /* prepare a dummy image */
  207.         /* Y */
  208.         for(y=0;y<c->height;y++) {
  209.             for(x=0;x<c->width;x++) {
  210.                 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  211.             }
  212.         }
  213.         /* Cb and Cr */
  214.         for(y=0;y<c->height/2;y++) {
  215.             for(x=0;x<c->width/2;x++) {
  216.                 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  217.                 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  218.             }
  219.         }
  220.         /* encode the image */
  221.         out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
  222.         printf("encoding frame %3d (size=%5d)n", i, out_size);
  223.         fwrite(outbuf, 1, out_size, f);
  224.     }
  225.     /* get the delayed frames */
  226.     for(; out_size; i++) {
  227.         fflush(stdout);
  228.         
  229.         out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
  230.         printf("write frame %3d (size=%5d)n", i, out_size);
  231.         fwrite(outbuf, 1, out_size, f);
  232.     }
  233.     /* add sequence end code to have a real mpeg file */
  234.     outbuf[0] = 0x00;
  235.     outbuf[1] = 0x00;
  236.     outbuf[2] = 0x01;
  237.     outbuf[3] = 0xb7;
  238.     fwrite(outbuf, 1, 4, f);
  239.     fclose(f);
  240.     free(picture_buf);
  241.     free(outbuf);
  242.     avcodec_close(c);
  243.     av_free(c);
  244.     av_free(picture);
  245.     printf("n");
  246. }
  247. /*
  248.  * Video decoding example 
  249.  */
  250. void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename) 
  251. {
  252.     FILE *f;
  253.     int i;
  254.     f=fopen(filename,"w");
  255.     fprintf(f,"P5n%d %dn%dn",xsize,ysize,255);
  256.     for(i=0;i<ysize;i++)
  257.         fwrite(buf + i * wrap,1,xsize,f);
  258.     fclose(f);
  259. }
  260. void video_decode_example(const char *outfilename, const char *filename)
  261. {
  262.     AVCodec *codec;
  263.     AVCodecContext *c= NULL;
  264.     int frame, size, got_picture, len;
  265.     FILE *f;
  266.     AVFrame *picture;
  267.     uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
  268.     char buf[1024];
  269.     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
  270.     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  271.     printf("Video decodingn");
  272.     /* find the mpeg1 video decoder */
  273.     codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
  274.     if (!codec) {
  275.         fprintf(stderr, "codec not foundn");
  276.         exit(1);
  277.     }
  278.     c= avcodec_alloc_context();
  279.     picture= avcodec_alloc_frame();
  280.     if(codec->capabilities&CODEC_CAP_TRUNCATED)
  281.         c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
  282.     /* for some codecs, such as msmpeg4 and mpeg4, width and height
  283.        MUST be initialized there because these info are not available
  284.        in the bitstream */
  285.     /* open it */
  286.     if (avcodec_open(c, codec) < 0) {
  287.         fprintf(stderr, "could not open codecn");
  288.         exit(1);
  289.     }
  290.     
  291.     /* the codec gives us the frame size, in samples */
  292.     f = fopen(filename, "rb");
  293.     if (!f) {
  294.         fprintf(stderr, "could not open %sn", filename);
  295.         exit(1);
  296.     }
  297.     
  298.     frame = 0;
  299.     for(;;) {
  300.         size = fread(inbuf, 1, INBUF_SIZE, f);
  301.         if (size == 0)
  302.             break;
  303.         /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
  304.            and this is the only method to use them because you cannot
  305.            know the compressed data size before analysing it. 
  306.            BUT some other codecs (msmpeg4, mpeg4) are inherently frame
  307.            based, so you must call them with all the data for one
  308.            frame exactly. You must also initialize 'width' and
  309.            'height' before initializing them. */
  310.         /* NOTE2: some codecs allow the raw parameters (frame size,
  311.            sample rate) to be changed at any frame. We handle this, so
  312.            you should also take care of it */
  313.         /* here, we use a stream based decoder (mpeg1video), so we
  314.            feed decoder and see if it could decode a frame */
  315.         inbuf_ptr = inbuf;
  316.         while (size > 0) {
  317.             len = avcodec_decode_video(c, picture, &got_picture, 
  318.                                        inbuf_ptr, size);
  319.             if (len < 0) {
  320.                 fprintf(stderr, "Error while decoding frame %dn", frame);
  321.                 exit(1);
  322.             }
  323.             if (got_picture) {
  324.                 printf("saving frame %3dn", frame);
  325.                 fflush(stdout);
  326.                 /* the picture is allocated by the decoder. no need to
  327.                    free it */
  328.                 snprintf(buf, sizeof(buf), outfilename, frame);
  329.                 pgm_save(picture->data[0], picture->linesize[0], 
  330.                          c->width, c->height, buf);
  331.                 frame++;
  332.             }
  333.             size -= len;
  334.             inbuf_ptr += len;
  335.         }
  336.     }
  337.     /* some codecs, such as MPEG, transmit the I and P frame with a
  338.        latency of one frame. You must do the following to have a
  339.        chance to get the last frame of the video */
  340.     len = avcodec_decode_video(c, picture, &got_picture, 
  341.                                NULL, 0);
  342.     if (got_picture) {
  343.         printf("saving last frame %3dn", frame);
  344.         fflush(stdout);
  345.         
  346.         /* the picture is allocated by the decoder. no need to
  347.            free it */
  348.         snprintf(buf, sizeof(buf), outfilename, frame);
  349.         pgm_save(picture->data[0], picture->linesize[0], 
  350.                  c->width, c->height, buf);
  351.         frame++;
  352.     }
  353.         
  354.     fclose(f);
  355.     avcodec_close(c);
  356.     av_free(c);
  357.     av_free(picture);
  358.     printf("n");
  359. }
  360. int main(int argc, char **argv)
  361. {
  362.     const char *filename;
  363.     /* must be called before using avcodec lib */
  364.     avcodec_init();
  365.     /* register all the codecs (you can also register only the codec
  366.        you wish to have smaller code */
  367.     avcodec_register_all();
  368.     if (argc <= 1) {
  369.         audio_encode_example("/tmp/test.mp2");
  370.         audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
  371.         video_encode_example("/tmp/test.mpg");
  372.         filename = "/tmp/test.mpg";
  373.     } else {
  374.         filename = argv[1];
  375.     }
  376.     //    audio_decode_example("/tmp/test.sw", filename);
  377.     video_decode_example("/tmp/test%d.pgm", filename);
  378.     return 0;
  379. }