音视频流::ffplay::tutorial01

原创:

http://blog.chinaunix.net/uid-24203478-id-3035890.html

 

所用的版本为ffmpeg 0.6.3

使用编译命令:
gcc -o tutorial01 tutorial01.c -lavutil -lavformat -lavcodec -lswscale

源程序如下:

  1. #include <libavcodec/avcodec.h>    
  2. #include <libavformat/avformat.h>    
  3. #include <libswscale/swscale.h>    
  4.     
  5. #include <stdio.h>    
  6.     
  7. #define debug() fprintf(stderr,"%s#%i\n",__func__,__LINE__)    
  8.     
  9. static struct SwsContext *img_convert_ctx;    
  10.     
  11. void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)    
  12. {    
  13.     FILE  *pFile;    
  14.     char   szFilename[32];    
  15.     int    y;    
  16.      
  17.     sprintf(szFilename, "frame%d.ppm", iFrame);    
  18.     pFile = fopen(szFilename, "wb");    
  19.     if (pFile == NULL) {    
  20.         return;    
  21.     }    
  22.     
  23.     fprintf(pFile, "P6\n%d %d\n255\n", width, height);    
  24.     
  25.     for (y = 0; y < height; y++) {    
  26.         fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);    
  27.     }    
  28.     
  29.     fclose(pFile);    
  30. }    
  31.     
  32. int main(int argc, char *argv[])    
  33. {    
  34.     AVFormatContext  *pFormatCtx;    
  35.     int       i, videoStream;    
  36.     AVCodecContext   *pCodecCtx;    
  37.     AVCodec      *pCodec;    
  38.     AVFrame      *pFrame;    
  39.     AVFrame      *pFrameRGB;    
  40.     AVPacket      packet;    
  41.     int       frameFinished;    
  42.     int               numBytes;    
  43.     uint8_t       *buffer;    
  44.     
  45.     if (argc < 2) {    
  46.         printf("Please provide a movie file\n");    
  47.         return -1;    
  48.     }    
  49.     fprintf(stderr,"ARG OK.\n");    
  50.     debug();    
  51.     av_register_all();    
  52.     fprintf(stderr,"av_register_all() OK.\n");    
  53.     debug();    
  54.         
  55.     pFormatCtx = avformat_alloc_context(); // 分配空间    
  56.     fprintf(stderr,"avformat_alloc_context() OK.\n");    
  57.     debug();    
  58.     if (av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL) != 0) {    
  59.     //if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) { // 打开输入文件    
  60.         return -1;    
  61.     }    
  62.     fprintf(stderr,"avformat_open_input() OK.\n");    
  63.     debug();    
  64.     
  65.     if (av_find_stream_info(pFormatCtx) < 0) {    
  66.         return -1;    
  67.     }    
  68.     fprintf(stderr,"av_find_stream_info() OK.\n");    
  69.     debug();    
  70.     
  71.     dump_format(pFormatCtx, 0, argv[1], 0);    
  72.     fprintf(stderr,"dump_format() OK.\n");    
  73.     
  74.     debug();    
  75.     videoStream = -1;    
  76.     for(i=0; i<pFormatCtx->nb_streams; i++) {    
  77.         if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {    
  78.         videoStream = i;    
  79.         break;    
  80.         }    
  81.     }    
  82.     fprintf(stderr,"find CODEC_TYPE_VIDEO OK.\n");    
  83.     debug();    
  84.     if(videoStream == -1) {    
  85.         return -1;    
  86.     }    
  87.     debug();    
  88.     pCodecCtx=pFormatCtx->streams[videoStream]->codec;    
  89.     img_convert_ctx = sws_getContext(    
  90.             pCodecCtx->width,     
  91.             pCodecCtx->height,    
  92.             pCodecCtx->pix_fmt,     
  93.             pCodecCtx->width,     
  94.             pCodecCtx->height,     
  95.             PIX_FMT_RGB24,     
  96.             SWS_BICUBIC,     
  97.             NULL, NULL, NULL);    
  98.     fprintf(stderr,"sws_getContext() OK.\n");    
  99.     
  100.     debug();    
  101.     pCodec = avcodec_find_decoder(pCodecCtx->codec_id);    
  102.     if (pCodec == NULL) {    
  103.         fprintf(stderr, "Unsupported codec!\n");    
  104.         return -1;    
  105.     }    
  106.     fprintf(stderr,"avcodec_find_decoder() OK.\n");    
  107.     
  108.     debug();    
  109.     if (avcodec_open(pCodecCtx, pCodec) < 0) {    
  110.         return -1;    
  111.     }    
  112.     fprintf(stderr,"avcodec_open() OK.\n");    
  113.     debug();    
  114.     
  115.     pFrame = avcodec_alloc_frame();    
  116.     fprintf(stderr,"avcodec_alloc_frame() OK.\n");    
  117.     debug();    
  118.     
  119.     pFrameRGB = avcodec_alloc_frame();    
  120.     if (pFrameRGB == NULL) {    
  121.         return -1;    
  122.     }    
  123.     fprintf(stderr,"avcodec_alloc_frame() OK.\n");    
  124.     debug();    
  125.     
  126.     numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,    
  127.                           pCodecCtx->height);    
  128.     fprintf(stderr,"avpicture_get_size() OK.\n");    
  129.     debug();    
  130.     buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));    
  131.     
  132.     debug();    
  133.     avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,    
  134.                pCodecCtx->width, pCodecCtx->height);    
  135.     fprintf(stderr,"avpicture_fill() OK.\n");    
  136.     debug();    i = 0;    
  137.     while(av_read_frame(pFormatCtx, &packet) >= 0) {    
  138.         if (packet.stream_index == videoStream) {    
  139.             avcodec_decode_video(pCodecCtx, pFrame,     
  140.                          &frameFinished,    
  141.                          packet.data,     
  142.                          packet.size);    
  143.             if(frameFinished) {    
  144.                 sws_scale(img_convert_ctx,     
  145.                       (const uint8_t **)pFrame->data,     
  146.                       pFrame->linesize, 0,     
  147.                       pCodecCtx->height,     
  148.                       pFrameRGB->data,     
  149.                       pFrameRGB->linesize);    
  150.                 if (++i <= 5) {    
  151.                     SaveFrame(pFrameRGB, pCodecCtx->width,pCodecCtx->height,i);    
  152.                 }    
  153.             }    
  154.         }    
  155.         av_free_packet(&packet);    
  156.     }    
  157.     debug();    
  158.     
  159.     av_free(buffer);    
  160.     av_free(pFrameRGB);    
  161.     
  162.     av_free(pFrame);    
  163.     
  164.     avcodec_close(pCodecCtx);    
  165.     
  166.     av_close_input_file(pFormatCtx);    
  167.     
  168.     return 0;    
  169. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值