本文记录IOS平台下基于FFmpeg的视频解码器。该示例C语言的源代码来自于《最简单的基于FFMPEG+SDL的视频播放器》。相关的概念就不再重复记录了。

源代码

项目的目录结构如图所示。

C代码位于ViewController.m文件中,内容如下所示。

  1.  
    /**
  2.  
    * 最简单的基于FFmpeg的视频解码器-IOS
  3.  
    * Simplest FFmpeg IOS Decoder
  4.  
    *
  5.  
    * 雷霄骅 Lei Xiaohua
  6.  
    * leixiaohua1020@126.com
  7.  
    * 中国传媒大学/数字电视技术
  8.  
    * Communication University of China / Digital TV Technology
  9.  
    * http://blog.csdn.net/leixiaohua1020
  10.  
    *
  11.  
    * 本程序是IOS平台下最简单的基于FFmpeg的视频解码器。
  12.  
    * 它可以将输入的视频数据解码成YUV像素数据。
  13.  
    *
  14.  
    * This software is the simplest decoder based on FFmpeg in IOS.
  15.  
    * It can decode video stream to raw YUV data.
  16.  
    *
  17.  
    */
  18.  
     
  19.  
    #import "ViewController.h"
  20.  
    #include <libavcodec/avcodec.h>
  21.  
    #include <libavformat/avformat.h>
  22.  
    #include <libavutil/imgutils.h>
  23.  
    #include <libswscale/swscale.h>
  24.  
     
  25.  
    @interface ViewController ()
  26.  
     
  27.  
    @end
  28.  
     
  29.  
    @implementation ViewController
  30.  
     
  31.  
    - (void)viewDidLoad {
  32.  
    [super viewDidLoad];
  33.  
    // Do any additional setup after loading the view, typically from a nib.
  34.  
    }
  35.  
     
  36.  
    - (void)didReceiveMemoryWarning {
  37.  
    [super didReceiveMemoryWarning];
  38.  
    // Dispose of any resources that can be recreated.
  39.  
    }
  40.  
     
  41.  
    - (IBAction)clickDecodeButton:(id)sender {
  42.  
    AVFormatContext *pFormatCtx;
  43.  
    int i, videoindex;
  44.  
    AVCodecContext *pCodecCtx;
  45.  
    AVCodec *pCodec;
  46.  
    AVFrame *pFrame,*pFrameYUV;
  47.  
    uint8_t *out_buffer;
  48.  
    AVPacket *packet;
  49.  
    int y_size;
  50.  
    int ret, got_picture;
  51.  
    struct SwsContext *img_convert_ctx;
  52.  
    FILE *fp_yuv;
  53.  
    int frame_cnt;
  54.  
    clock_t time_start, time_finish;
  55.  
    double time_duration = 0.0;
  56.  
     
  57.  
    char input_str_full[500]={0};
  58.  
    char output_str_full[500]={0};
  59.  
    char info[1000]={0};
  60.  
     
  61.  
    NSString *input_str= [NSString stringWithFormat:@"resource.bundle/%@",self.inputurl.text];
  62.  
    NSString *output_str= [NSString stringWithFormat:@"resource.bundle/%@",self.outputurl.text];
  63.  
     
  64.  
    NSString *input_nsstr=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:input_str];
  65.  
    NSString *output_nsstr=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:output_str];
  66.  
     
  67.  
    sprintf(input_str_full,"%s",[input_nsstr UTF8String]);
  68.  
    sprintf(output_str_full,"%s",[output_nsstr UTF8String]);
  69.  
     
  70.  
    printf("Input Path:%s\n",input_str_full);
  71.  
    printf("Output Path:%s\n",output_str_full);
  72.  
     
  73.  
    av_register_all();
  74.  
    avformat_network_init();
  75.  
    pFormatCtx = avformat_alloc_context();
  76.  
     
  77.  
    if(avformat_open_input(&pFormatCtx,input_str_full,NULL,NULL)!=0){
  78.  
    printf("Couldn't open input stream.\n");
  79.  
    return ;
  80.  
    }
  81.  
    if(avformat_find_stream_info(pFormatCtx,NULL)<0){
  82.  
    printf("Couldn't find stream information.\n");
  83.  
    return;
  84.  
    }
  85.  
    videoindex=-1;
  86.  
    for(i=0; i<pFormatCtx->nb_streams; i++)
  87.  
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
  88.  
    videoindex=i;
  89.  
    break;
  90.  
    }
  91.  
    if(videoindex==-1){
  92.  
    printf("Couldn't find a video stream.\n");
  93.  
    return;
  94.  
    }
  95.  
    pCodecCtx=pFormatCtx->streams[videoindex]->codec;
  96.  
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  97.  
    if(pCodec==NULL){
  98.  
    printf("Couldn't find Codec.\n");
  99.  
    return;
  100.  
    }
  101.  
    if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
  102.  
    printf("Couldn't open codec.\n");
  103.  
    return;
  104.  
    }
  105.  
     
  106.  
    pFrame=av_frame_alloc();
  107.  
    pFrameYUV=av_frame_alloc();
  108.  
    out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height,1));
  109.  
    av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,
  110.  
    AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);
  111.  
    packet=(AVPacket *)av_malloc(sizeof(AVPacket));
  112.  
     
  113.  
    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
  114.  
    pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  115.  
     
  116.  
     
  117.  
    sprintf(info, "[Input ]%s\n", [input_str UTF8String]);
  118.  
    sprintf(info, "%s[Output ]%s\n",info,[output_str UTF8String]);
  119.  
    sprintf(info, "%s[Format ]%s\n",info, pFormatCtx->iformat->name);
  120.  
    sprintf(info, "%s[Codec ]%s\n",info, pCodecCtx->codec->name);
  121.  
    sprintf(info, "%s[Resolution]%dx%d\n",info, pCodecCtx->width,pCodecCtx->height);
  122.  
     
  123.  
     
  124.  
    fp_yuv=fopen(output_str_full,"wb+");
  125.  
    if(fp_yuv==NULL){
  126.  
    printf("Cannot open output file.\n");
  127.  
    return;
  128.  
    }
  129.  
     
  130.  
    frame_cnt=0;
  131.  
    time_start = clock();
  132.  
     
  133.  
    while(av_read_frame(pFormatCtx, packet)>=0){
  134.  
    if(packet->stream_index==videoindex){
  135.  
    ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  136.  
    if(ret < 0){
  137.  
    printf("Decode Error.\n");
  138.  
    return;
  139.  
    }
  140.  
    if(got_picture){
  141.  
    sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
  142.  
    pFrameYUV->data, pFrameYUV->linesize);
  143.  
     
  144.  
    y_size=pCodecCtx->width*pCodecCtx->height;
  145.  
    fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y
  146.  
    fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U
  147.  
    fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V
  148.  
    //Output info
  149.  
    char pictype_str[10]={0};
  150.  
    switch(pFrame->pict_type){
  151.  
    case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
  152.  
    case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
  153.  
    case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
  154.  
    default:sprintf(pictype_str,"Other");break;
  155.  
    }
  156.  
    printf("Frame Index: %5d. Type:%s\n",frame_cnt,pictype_str);
  157.  
    frame_cnt++;
  158.  
    }
  159.  
    }
  160.  
    av_free_packet(packet);
  161.  
    }
  162.  
    //flush decoder
  163.  
    //FIX: Flush Frames remained in Codec
  164.  
    while (1) {
  165.  
    ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  166.  
    if (ret < 0)
  167.  
    break;
  168.  
    if (!got_picture)
  169.  
    break;
  170.  
    sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
  171.  
    pFrameYUV->data, pFrameYUV->linesize);
  172.  
    int y_size=pCodecCtx->width*pCodecCtx->height;
  173.  
    fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y
  174.  
    fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U
  175.  
    fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V
  176.  
    //Output info
  177.  
    char pictype_str[10]={0};
  178.  
    switch(pFrame->pict_type){
  179.  
    case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
  180.  
    case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
  181.  
    case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
  182.  
    default:sprintf(pictype_str,"Other");break;
  183.  
    }
  184.  
    printf("Frame Index: %5d. Type:%s\n",frame_cnt,pictype_str);
  185.  
    frame_cnt++;
  186.  
    }
  187.  
    time_finish = clock();
  188.  
    time_duration=(double)(time_finish - time_start);
  189.  
     
  190.  
    sprintf(info, "%s[Time ]%fus\n",info,time_duration);
  191.  
    sprintf(info, "%s[Count ]%d\n",info,frame_cnt);
  192.  
     
  193.  
    sws_freeContext(img_convert_ctx);
  194.  
     
  195.  
    fclose(fp_yuv);
  196.  
     
  197.  
    av_frame_free(&pFrameYUV);
  198.  
    av_frame_free(&pFrame);
  199.  
    avcodec_close(pCodecCtx);
  200.  
    avformat_close_input(&pFormatCtx);
  201.  
     
  202.  
    NSString * info_ns = [NSString stringWithFormat:@"%s", info];
  203.  
    self.infomation.text=info_ns;
  204.  
     
  205.  
    }
  206.  
     
  207.  
     
  208.  
    @end

运行结果

App在手机上运行后的结果如下图所示。单击“Decode”,将会把位于resource.bundle中的“sintel.mov”文件解码为“sintel.yuv”文件并存储于相同的目录下。

生成的文件如下图所示。

 
 

本文转载自网络,感谢原作者的分享,转载仅为分享干货知识,如有侵权欢迎联系作者进行删除处理。

最新文章

  1. winserver2008 DNS 很详细
  2. 将事件绑定在html标签中和js动态绑定的区别
  3. IE请求访问的设置
  4. my Highcharts
  5. Flash Attribute
  6. TCP四种定时器--学习笔记
  7. SQL Server 表字段值转换成字段名称(二)
  8. 解决Fetching android sdk component information加载过久问题
  9. windows MySQL 安装
  10. Sql Server的艺术(六) SQL 子查询,创建使用返回多行的子查询,子查询创建视图
  11. Nginx技术进阶详讲
  12. Codeforces Goodbye 2018
  13. mysql的事务和数据库锁的关系
  14. Maven笔记 #01# 入门
  15. less中使用calc
  16. hdu-1026(bfs+优先队列)
  17. Spring Boot 2 实践记录之 组合注解原理
  18. sql 恢复数据库
  19. css平移动画的实现
  20. 短信api接口

热门文章

  1. opencv 中从cv::line和resize()函数
  2. Jmeter之连接数据库
  3. 脚手架安装react
  4. Java安全之Commons Collections1分析(一)
  5. SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理|前后端分离(下)----筑基后期
  6. 十一长假我肝了这本超硬核PDF,现决定开源!!
  7. Spring Boot 系列:最新版优雅停机详解
  8. day23 Pyhton学习 昨日回顾.re模块.序列化模块
  9. CentOS7克隆多个虚拟机
  10. 【C/C++】用C语言编写爬虫—爬虫程序优化要点