最简单的基于FFmpeg的直播系统开发移动端例子:IOS 视频解码器
本文记录IOS平台下基于FFmpeg的视频解码器。该示例C语言的源代码来自于《最简单的基于FFMPEG+SDL的视频播放器》。相关的概念就不再重复记录了。
源代码
项目的目录结构如图所示。
C代码位于ViewController.m文件中,内容如下所示。
- /**
- * 最简单的基于FFmpeg的视频解码器-IOS
- * Simplest FFmpeg IOS Decoder
- *
- * 雷霄骅 Lei Xiaohua
- * leixiaohua1020@126.com
- * 中国传媒大学/数字电视技术
- * Communication University of China / Digital TV Technology
- * http://blog.csdn.net/leixiaohua1020
- *
- * 本程序是IOS平台下最简单的基于FFmpeg的视频解码器。
- * 它可以将输入的视频数据解码成YUV像素数据。
- *
- * This software is the simplest decoder based on FFmpeg in IOS.
- * It can decode video stream to raw YUV data.
- *
- */
- #import "ViewController.h"
- #include <libavcodec/avcodec.h>
- #include <libavformat/avformat.h>
- #include <libavutil/imgutils.h>
- #include <libswscale/swscale.h>
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)clickDecodeButton:(id)sender {
- AVFormatContext *pFormatCtx;
- int i, videoindex;
- AVCodecContext *pCodecCtx;
- AVCodec *pCodec;
- AVFrame *pFrame,*pFrameYUV;
- uint8_t *out_buffer;
- AVPacket *packet;
- int y_size;
- int ret, got_picture;
- struct SwsContext *img_convert_ctx;
- FILE *fp_yuv;
- int frame_cnt;
- clock_t time_start, time_finish;
- double time_duration = 0.0;
- char input_str_full[500]={0};
- char output_str_full[500]={0};
- char info[1000]={0};
- NSString *input_str= [NSString stringWithFormat:@"resource.bundle/%@",self.inputurl.text];
- NSString *output_str= [NSString stringWithFormat:@"resource.bundle/%@",self.outputurl.text];
- NSString *input_nsstr=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:input_str];
- NSString *output_nsstr=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:output_str];
- sprintf(input_str_full,"%s",[input_nsstr UTF8String]);
- sprintf(output_str_full,"%s",[output_nsstr UTF8String]);
- printf("Input Path:%s\n",input_str_full);
- printf("Output Path:%s\n",output_str_full);
- av_register_all();
- avformat_network_init();
- pFormatCtx = avformat_alloc_context();
- if(avformat_open_input(&pFormatCtx,input_str_full,NULL,NULL)!=0){
- printf("Couldn't open input stream.\n");
- return ;
- }
- if(avformat_find_stream_info(pFormatCtx,NULL)<0){
- printf("Couldn't find stream information.\n");
- return;
- }
- videoindex=-1;
- for(i=0; i<pFormatCtx->nb_streams; i++)
- if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
- videoindex=i;
- break;
- }
- if(videoindex==-1){
- printf("Couldn't find a video stream.\n");
- return;
- }
- pCodecCtx=pFormatCtx->streams[videoindex]->codec;
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL){
- printf("Couldn't find Codec.\n");
- return;
- }
- if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
- printf("Couldn't open codec.\n");
- return;
- }
- pFrame=av_frame_alloc();
- pFrameYUV=av_frame_alloc();
- out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height,1));
- av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,
- AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);
- packet=(AVPacket *)av_malloc(sizeof(AVPacket));
- img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
- pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
- sprintf(info, "[Input ]%s\n", [input_str UTF8String]);
- sprintf(info, "%s[Output ]%s\n",info,[output_str UTF8String]);
- sprintf(info, "%s[Format ]%s\n",info, pFormatCtx->iformat->name);
- sprintf(info, "%s[Codec ]%s\n",info, pCodecCtx->codec->name);
- sprintf(info, "%s[Resolution]%dx%d\n",info, pCodecCtx->width,pCodecCtx->height);
- fp_yuv=fopen(output_str_full,"wb+");
- if(fp_yuv==NULL){
- printf("Cannot open output file.\n");
- return;
- }
- frame_cnt=0;
- time_start = clock();
- while(av_read_frame(pFormatCtx, packet)>=0){
- if(packet->stream_index==videoindex){
- ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
- if(ret < 0){
- printf("Decode Error.\n");
- return;
- }
- if(got_picture){
- sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
- pFrameYUV->data, pFrameYUV->linesize);
- y_size=pCodecCtx->width*pCodecCtx->height;
- fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y
- fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U
- fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V
- //Output info
- char pictype_str[10]={0};
- switch(pFrame->pict_type){
- case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
- case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
- case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
- default:sprintf(pictype_str,"Other");break;
- }
- printf("Frame Index: %5d. Type:%s\n",frame_cnt,pictype_str);
- frame_cnt++;
- }
- }
- av_free_packet(packet);
- }
- //flush decoder
- //FIX: Flush Frames remained in Codec
- while (1) {
- ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
- if (ret < 0)
- break;
- if (!got_picture)
- break;
- sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
- pFrameYUV->data, pFrameYUV->linesize);
- int y_size=pCodecCtx->width*pCodecCtx->height;
- fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y
- fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U
- fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V
- //Output info
- char pictype_str[10]={0};
- switch(pFrame->pict_type){
- case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
- case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
- case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
- default:sprintf(pictype_str,"Other");break;
- }
- printf("Frame Index: %5d. Type:%s\n",frame_cnt,pictype_str);
- frame_cnt++;
- }
- time_finish = clock();
- time_duration=(double)(time_finish - time_start);
- sprintf(info, "%s[Time ]%fus\n",info,time_duration);
- sprintf(info, "%s[Count ]%d\n",info,frame_cnt);
- sws_freeContext(img_convert_ctx);
- fclose(fp_yuv);
- av_frame_free(&pFrameYUV);
- av_frame_free(&pFrame);
- avcodec_close(pCodecCtx);
- avformat_close_input(&pFormatCtx);
- NSString * info_ns = [NSString stringWithFormat:@"%s", info];
- self.infomation.text=info_ns;
- }
- @end
运行结果
App在手机上运行后的结果如下图所示。单击“Decode”,将会把位于resource.bundle中的“sintel.mov”文件解码为“sintel.yuv”文件并存储于相同的目录下。
生成的文件如下图所示。
本文转载自网络,感谢原作者的分享,转载仅为分享干货知识,如有侵权欢迎联系作者进行删除处理。
最简单的基于FFmpeg的直播系统开发移动端例子:IOS 视频解码器的更多相关文章
- 简单red5+obs推流实现直播系统开发,具体设置介绍
前言:随便搞搞,先放一张效果图,
- 最简单的基于FFmpeg的内存读写的例子:内存播放器
===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...
- (转)最简单的基于FFmpeg的内存读写的例子:内存播放器
ffmpeg内存播放解码 目录(?)[+] ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章 ...
- 最简单的基于FFmpeg的推流器(以推送RTMP为例)
===================================================== 最简单的基于FFmpeg的推流器系列文章列表: <最简单的基于FFmpeg的推流器(以 ...
- 最简单的基于FFmpeg的AVDevice例子(屏幕录制)
=====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...
- 最简单的基于FFmpeg的AVDevice例子(读取摄像头)
=====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...
- [转载] 最简单的基于FFmpeg的AVDevice例子(读取摄像头)
=====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...
- 100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)【转】
转自:http://blog.csdn.net/leixiaohua1020/article/details/8652605 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] ...
- 最简单的基于FFmpeg的AVDevice例子(读取摄像头)【转】
转自:http://blog.csdn.net/leixiaohua1020/article/details/39702113 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[- ...
随机推荐
- 《C++primerplus》第11章练习题
1.修改程序清单11.5(随机漫步),使之以特定的格式将结果写入文件中. //vector.h -- Vector Class #ifndef _VECTOR_H_ #define _VECTOR_H ...
- C语言编程入门之--第六章C语言控制语句
导读:本章带读者理解什么是控制语句,然后逐个讲解C语言常用的控制语句,含有控制语句的代码量多起来后就要注意写代码的风格了,本章末节都是练习题,大量的练习才能掌握好控制语句的使用. 6.1 什么是控制语 ...
- CentOS7 系统安全的设置
一.禁止root远程直接登录 # 创建普通用户,并设置密码 useradd bluceli #新建账户 passwd bluceli #设置密码 # 不允许root远程直接登录 vim /etc/ss ...
- Fiddler抓包工具 请求图标为一个锁的图标的设置
第一步,Fiddler抓包的数据 前面的都是一个锁的图标,的设置方法, 然后 点击打开 按此设置图一 在图二, 图三. 其他默认就好
- java 的 callback
Java 本身没有回调这一说,但是面向对象可以模拟出来. 1. 回调接口对象 ICommand package com.git.Cmder; public interface ICommand { v ...
- 使用Python学习win32库进行内存读写
前言: 上一周,在52的精华帖中,看到有位大佬用Python制作了鬼泣5的修改器,看完才知道,原来Python也可以对内存进行操作,出于对技术的好奇,看完以后,决定自己也尝试一下. 要用到的工具: C ...
- 2020已经过去五分之四了,你确定还不来了解一下JS的rAF?
不会吧,不会吧,现在都2020年了不会还真人有人不知道JS的rAF吧??? rAF 简介 rAF是requestAnimationFrame的简称: 我们先从字面意思上理解requestAnimati ...
- 发布MeteoInfo 1.2.3
提升了对GeoTiff格式数据的读取能力(多个tiles).当然还有MeteoInfoLab功能的提升.下载地址:http://yun.baidu.com/share/link?shareid=669 ...
- Flask之WTF
Flask-WTF是什么? 是一个关于表单的扩展库,可以自动生成表单的HTML代码和验证提交的表单数据,并且提供跨站请求伪造(Cross-Site Request Forgery)保护的功能,使用非常 ...
- 微信小程序项目wx-store代码详解
这篇文章会很长,非常长,特别长,无敌长. 真的是挤牙膏般的项目进度,差不多是8月底有开始这个项目的想法,时至今日都1个多月了,抛去频繁的加班时间,王者时间,羽毛球时间...见缝插针的写这个项目,我竟然 ...