The simplest mobile-based ffmpeg example: IOS Video Decoder

Source: Internet
Author: User
<span id="Label3"></p>This document records the ffmpeg-based video decoder under the iOS platform. The source code for the example C language comes from the simplest ffmpeg+sdl-based video player. The related concepts are no longer duplicated.<br><br>Source<p><p>The directory structure of the Project.</p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p>The C code is in the VIEWCONTROLLER.M file, as shown in the Following.<br><pre name="code" class="objc">/** * Simplest FFmpeg based video Decoder-ios * simplest FFmpeg IOS Decoder * * Lei hua Lei Xiaohua * [email protected] * Communication university/digital TV Technology * Communication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program is the simplest under the iOS platform based on FFmpeg Video Decoder. * It can decode the input video data into YUV pixel Data. * * This software are 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 <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 the can be recreated.} -(ibaction) clickdecodebutton: (id) Sender {avformatcontext*pformatctx; inti, 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= (uint8_t *) av_malloc (avpicture_get_size (pix_fmt_yuv420p, pcodecctx->width, pCodecCtx->height)); Avpicture_fill ((avpicture *) pframeyuv, out_buffer, pix_fmt_yuv420p, pcodecctx->width, pCodecCtx->height); packet= (avpacket *) av_malloc (sizeof (avpacket)); Img_convert_ctx = Sws_getcontext (pcodecctx->width, pcodecctx->height, pcodecctx->pix_fmt, pcodecctx->width, pcodecctx->height, 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_d Ecode_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-&gt ; 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"); Case av_picture_type_p:sprintf (pictype_str, "P"); Case av_picture_type_b:sprintf (pictype_str, "B"); default:sprintf (pictype_str, "other"); } 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"); Case av_picture_type_p:sprintf (pictype_str, "P"); Case av_picture_type_b:sprintf (pictype_str, "B"); default:sprintf (pictype_str, "other"); } 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</pre><pre name="code" class="objc"><pre name="code" class="objc"></pre></pre><p><p><br></p></p>Run results<p><p>The app runs on the phone as shown in the Results. Clicking "Decode" will decode the "sintel.mov" file in Resource.bundle to "sintel.yuv" file and store it in the same directory.</p></p><p style="text-align: center;"><p style="text-align: center;"><br></p></p>The resulting file is as Shown.<br><br>Download<br><strong><strong>simplest ffmpeg mobile</strong></strong><br><br><p><p><strong>Project Home</strong></p></p><p><p>Github:https://github.com/leixiaohua1020/simplest_ffmpeg_mobile</p></p><p><p>Open Source China: https://git.oschina.net/leixiaohua1020/simplest_ffmpeg_mobile<br></p></p><p><p><br></p></p><p><p>CSDN project: http://download.csdn.net/detail/leixiaohua1020/8924391</p></p><p><p><br></p></p>This solution contains various examples of using FFMPEG to process multimedia on the mobile side:<br> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"> [Android] <br>Simplest_android_player: Android interface-based Video player <br>Simplest_ffmpeg_android_helloworld: ffmpeg-based HelloWorld program under Android platform <br>Simplest_ffmpeg_android_decoder: The simplest ffmpeg-based video decoder on the Android platform <br>Simplest_ffmpeg_android_decoder_onelib: The simplest ffmpeg-based video decoder on the Android Platform-library edition <br>Simplest_ffmpeg_android_streamer: The simplest ffmpeg-based push-to-flow device under the Android platform <br>Simplest_ffmpeg_android_transcoder: FFmpeg command-line tool ported under Android platform <br>Simplest_sdl_android_helloworld: the simplest program for porting SDL to the Android platform <br>[IOS] <br>Simplest_ios_player: Video player based on iOS interface <br>HelloWorld program based on FFmpeg under the Simplest_ffmpeg_ios_helloworld:ios platform <br> <span style="color:#ff0000;">The simplest ffmpeg-based video decoder under the Simplest_ffmpeg_ios_decoder:ios platform</span> <br>The simplest ffmpeg based on the Simplest_ffmpeg_ios_streamer:ios platform <br>FFMPEG.C command-line tool ported under Simplest_ffmpeg_ios_transcoder:ios Platform <br>Simplest_sdl_ios_helloworld: The simplest program to migrate SDL to the iOS platform </blockquote> </blockquote><br><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>The simplest mobile-based ffmpeg example: IOS Video Decoder</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.