本文记录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”文件并存储于相同的目录下。

生成的文件如下图所示。

 
 

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

最简单的基于FFmpeg的直播系统开发移动端例子:IOS 视频解码器的更多相关文章

  1. 简单red5+obs推流实现直播系统开发,具体设置介绍

    前言:随便搞搞,先放一张效果图,

  2. 最简单的基于FFmpeg的内存读写的例子:内存播放器

    ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...

  3. (转)最简单的基于FFmpeg的内存读写的例子:内存播放器

    ffmpeg内存播放解码 目录(?)[+] ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章 ...

  4. 最简单的基于FFmpeg的推流器(以推送RTMP为例)

    ===================================================== 最简单的基于FFmpeg的推流器系列文章列表: <最简单的基于FFmpeg的推流器(以 ...

  5. 最简单的基于FFmpeg的AVDevice例子(屏幕录制)

    =====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...

  6. 最简单的基于FFmpeg的AVDevice例子(读取摄像头)

    =====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...

  7. [转载] 最简单的基于FFmpeg的AVDevice例子(读取摄像头)

    =====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...

  8. 100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/8652605 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] ...

  9. 最简单的基于FFmpeg的AVDevice例子(读取摄像头)【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/39702113 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[- ...

随机推荐

  1. 怀疑安装MySQL之后,导致OrCAD Capture、Allegro就打不开

    记得在异常出现之前,只安装了MySQL,之后OrCAD Capture.Allegro就打不开了. Capture.exe - 系统错误 allegro.exe - 系统错误 我尝试在Cadence的 ...

  2. 在uniapp或者vue中单行文字或者符号无法换行的终极解决方案

    在VUE开发过程中,会出现比较诡异的情况. 比如常规的英文或中文显示都是很正常的,但是当出现了一些中文符号(比如,!等等)在文末的时候,总是会超出view的显示区域. 那么在遇到上面这种问题我们记得检 ...

  3. C#开启线程的四种方式

    如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! 1.异步委托开启线程 public class Pro ...

  4. 关于keytool和jarsigner工具签名的使用小结

    在我们日常Android应用开发中,我们都要对我们开发的apk做签名处理,或者加固,增强我们apk的安全性,防止被逆向反编译,在apk签名这块,我们一般采用JDK自动工具来签名,下面就对相关工具做个简 ...

  5. Java9第四篇-Reactive Stream API响应式编程

    我计划在后续的一段时间内,写一系列关于java 9的文章,虽然java 9 不像Java 8或者Java 11那样的核心java版本,但是还是有很多的特性值得关注.期待您能关注我,我将把java 9 ...

  6. ansible使用shell模块在受控机上执行命令(ansible2.9.5)

    一,ansible的shell模块和command模块的区别? shell模块:在远程主机上执行主控端发出的shell/python脚本 command模块:不能调用shell指令,没有bash的环境 ...

  7. Python函数的定义和参数

    函数的初识: 以功能为导向,一个函数就是一个功能.随调随用. 优点: 减少代码重复性. 增强代码的可读性. 函数的结构: def function_name(): 函数体 ​ def: 关键字,定义函 ...

  8. apktool重新打包添加签名

    一.生成apk apktool b 反编译后项目目录 -o 新apk名称.apk 二.生成签名 keytool -genkeypair -alias 新apk名称.apk -keyalg RSA -v ...

  9. spring cloud gateway整合sentinel作网关限流

    说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentine ...

  10. ORA-12609报错分析

    问题:监控不断告警ORA-12609 Wed 10/14/2020 10:40 AM 12CRAC1-ALERT中出现ORA错误,请检查 171- nt OS err code: 0 172- Cli ...