音视频流::ffplay::tutorial02

转自:

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

 

所用的版本为ffmpeg 0.6.3

totorial02需要安装sdl。用如下命令安装
sudo apt-get install libsdl1.2-dev
安装完后,编译totorial02.c的命令为:
gcc -o tutorial02 tutorial02.c -lavformat -lavcodec -lz -lm -lswscale `sdl-config --cflags --libs`

totorial02.c源程序如下:

  1. // tutorial02.c  
  2. // A pedagogical video player that will stream through every video frame as fast as it can.  
  3. //  
  4. // Code based on FFplay, Copyright (c) 2003 Fabrice Bellard,   
  5. // and a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)  
  6. // Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1  
  7. // Use  
  8. //  
  9. // gcc -o tutorial02 tutorial02.c -lavformat -lavcodec -lz -lm -lswscale `sdl-config --cflags --libs`  
  10. // to build (assuming libavformat and libavcodec are correctly installed,   
  11. // and assuming you have sdl-config. Please refer to SDL docs for your installation.)  
  12. //  
  13. // Run using  
  14. // tutorial02 myvideofile.mpg  
  15. //  
  16. // to play the video stream on your screen.  
  17.   
  18. #include <libavcodec/avcodec.h>    
  19. #include <libavformat/avformat.h>    
  20. #include <libswscale/swscale.h>    
  21.     
  22. #include <SDL.h>    
  23. #include <SDL_thread.h>    
  24.     
  25. #include <stdio.h>    
  26.     
  27. int main(int argc, char *argv[]) {    
  28.     AVFormatContext *pFormatCtx;    
  29.     int     i, videoStream;    
  30.     AVCodecContext  *pCodecCtx;    
  31.     AVCodec     *pCodec;    
  32.     AVFrame     *pFrame;    
  33.     AVPacket    packet;    
  34.     int     frameFinished;    
  35.     float       aspect_ratio;    
  36.     struct SwsContext *img_convert_ctx;    
  37.     
  38.     SDL_Overlay *bmp;    
  39.     SDL_Surface *screen;    
  40.     SDL_Rect    rect;    
  41.     SDL_Event   event;    
  42.     
  43.     if (argc < 2) {    
  44.         fprintf(stderr, "Usage: test <file>\n");    
  45.         exit(1);    
  46.     }    
  47.     
  48.     av_register_all();    
  49.     
  50.     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {    
  51.         fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());    
  52.         exit(1);    
  53.     }    
  54.     
  55.     if (av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL) != 0) {    
  56.         return -1;    
  57.     }    
  58.     
  59.     if (av_find_stream_info(pFormatCtx) < 0) {    
  60.         return -1;    
  61.     }    
  62.     
  63.     dump_format(pFormatCtx, 0, argv[1], 0);    
  64.     
  65.     videoStream = -1;    
  66.     for (i=0 ; i<pFormatCtx->nb_streams ; i++) {    
  67.         if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {    
  68.             videoStream = i;    
  69.             break;    
  70.         }    
  71.     }    
  72.     if (videoStream == -1) {    
  73.         return -1;    
  74.     }    
  75.     
  76.     pCodecCtx = pFormatCtx->streams[videoStream]->codec;    
  77.     img_convert_ctx = sws_getContext(    
  78.             pCodecCtx->width,     
  79.             pCodecCtx->height,    
  80.             pCodecCtx->pix_fmt,     
  81.             pCodecCtx->width,     
  82.             pCodecCtx->height,     
  83.             PIX_FMT_YUV420P,     
  84.             //PIX_FMT_RGB24,     
  85.             SWS_BICUBIC,     
  86.             NULL, NULL, NULL);    
  87.     
  88.     
  89.     pCodec = avcodec_find_decoder(pCodecCtx->codec_id);    
  90.     if (pCodec == NULL) {    
  91.         fprintf(stderr, "Unsupportd codec!\n");    
  92.         return -1;    
  93.     }    
  94.     
  95.     if (avcodec_open(pCodecCtx, pCodec) < 0) {    
  96.         return -1;    
  97.     }    
  98.     
  99.     pFrame = avcodec_alloc_frame();    
  100.     
  101.     screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 24, 0);    
  102.     
  103.     if (!screen) {    
  104.         fprintf(stderr, "SDL: could not set video mode - exiting\n");    
  105.         exit(1);    
  106.     }    
  107.     
  108.     bmp = SDL_CreateYUVOverlay(pCodecCtx->width,    
  109.                        pCodecCtx->height,    
  110.                    SDL_YV12_OVERLAY,    
  111.                    screen);    
  112.     while(av_read_frame(pFormatCtx, &packet) >= 0) {    
  113.         if (packet.stream_index == videoStream) {    
  114.             avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,    
  115.                          packet.data, packet.size);    
  116.     
  117.             if (frameFinished) {    
  118.                 SDL_LockYUVOverlay(bmp);    
  119.     
  120.                 AVPicture pict;    
  121.                 pict.data[0] = bmp->pixels[0];    
  122.                 pict.data[1] = bmp->pixels[2];    
  123.                 pict.data[2] = bmp->pixels[1];    
  124.     
  125.                 pict.linesize[0] = bmp->pitches[0];    
  126.                 pict.linesize[1] = bmp->pitches[2];    
  127.                 pict.linesize[2] = bmp->pitches[1];    
  128.     
  129.                 //img_convert(&pict, PIX_FMT_YUV420P,    
  130.                 //      (AVPicture*)pFrame, pCodecCtx->pix_fmt,    
  131.                 //      pCodecCtx->width, pCodecCtx->height);    
  132.                 sws_scale(img_convert_ctx,     
  133.                       pFrame->data,     
  134.                       //(const uint8_t **)pFrame->data,     
  135.                       pFrame->linesize, 0,     
  136.                       pCodecCtx->height,     
  137.                       pict.data,     
  138.                       pict.linesize);    
  139.     
  140.                 SDL_UnlockYUVOverlay(bmp);    
  141.     
  142.                 rect.x = 0;    
  143.                 rect.y = 0;    
  144.                 rect.w = pCodecCtx->width;    
  145.                 rect.h = pCodecCtx->height;    
  146.                 SDL_DisplayYUVOverlay(bmp, &rect);    
  147.             }    
  148.         }    
  149.     
  150.         av_free_packet(&packet);    
  151.         SDL_PollEvent(&event);    
  152.         switch(event.type) {    
  153.             case SDL_QUIT:    
  154.                 SDL_Quit();    
  155.                 exit(0);    
  156.                 break;    
  157.             default:    
  158.                 break;    
  159.         }    
  160.         SDL_Delay(100);    
  161.     }    
  162.     
  163.     av_free(pFrame);    
  164.     
  165.     avcodec_close(pCodecCtx);    
  166.     
  167.     av_close_input_file(pFormatCtx);    
  168.     
  169.     return 0;    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值