=====================================================
The simplest example of a ffmpeg-based mobile Sample series article List:
The simplest mobile-based example of FFmpeg: Android HelloWorld
The simplest ffmpeg-based mobile sample: Android Video Decoder
Simplest FFmpeg-based mobile sample: Android Video Decoder-Single library version
Simplest FFmpeg-based mobile sample: Android push-to-stream
The simplest ffmpeg-based mobile sample: Android video transcoding
Simplest FFmpeg-based mobile sample attachment: Android comes with player
Simplest FFmpeg-based mobile sample attachment: SDL Android HelloWorld
The simplest ffmpeg-based mobile sample: IOS HelloWorld
The simplest ffmpeg-based mobile sample: IOS Video Decoder
Simplest FFmpeg-based mobile sample: IOS push-to-stream
The simplest ffmpeg-based mobile sample: IOS video transcoding
Simplest FFmpeg-based mobile sample attachment: iOS comes with player
Simplest FFmpeg-based mobile sample: Windows Phone HelloWorld
=====================================================
This document records the FFmpeg-based video decoder under the iOS platform.
The source code for the demo sample C language is from the simplest ffmpeg+sdl based video player.
The related concepts are no longer recorded repeatedly.
Source
The folder structure of the project is seen.
The C code is located in the Viewcontroller.m file, as seen in the following sections.
/** * Simplest FFmpeg based video decoder-ios * simplest FFmpeg IOS Decoder * * Lei hua Lei xiaohua * [email protected] * Communication University/Digital TV Technology * COMMU Nication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program is the simplest ffmpeg-based video decoding under the iOS platform Manager * It is capable of decoding 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 <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 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= (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"); 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
Execution results
The results of the app's execution on the phone, for example, are seen. Click "Decode". The "Sintel.mov" file located in Resource.bundle will be decoded to "SINTEL.YUV" file and stored in the same folder.
The resulting file is, for example, seen.
Download
simplest ffmpeg mobile
Project Home
Github:https://github.com/leixiaohua1020/simplest_ffmpeg_mobile
Open source China: https://git.oschina.net/leixiaohua1020/simplest_ffmpeg_mobile
sourceforge:https://sourceforge.net/projects/simplestffmpegmobile/
CSDN Project: http://download.csdn.net/detail/leixiaohua1020/8924391
This solution includes various examples of using FFMPEG to process multimedia on the mobile side:
[Android]
Simplest_android_player: Android interface-based video player
Simplest_ffmpeg_android_helloworld: FFmpeg-based HelloWorld program under Android platform
Simplest_ffmpeg_android_decoder: The simplest ffmpeg-based video decoder on the Android platform
Simplest_ffmpeg_android_decoder_onelib: The simplest ffmpeg-based video decoder on the Android Platform-Library edition
Simplest_ffmpeg_android_streamer: The simplest ffmpeg-based push-to-flow device under the Android platform
Simplest_ffmpeg_android_transcoder: FFmpeg command-line tool ported under Android platform
Simplest_sdl_android_helloworld: The simplest program for porting SDL to the Android platform
[IOS]
Simplest_ios_player: Video player based on iOS interface
HelloWorld program based on FFmpeg under the Simplest_ffmpeg_ios_helloworld:ios platform
The simplest ffmpeg-based video decoder on the Simplest_ffmpeg_ios_decoder:ios platform
The simplest ffmpeg based on the Simplest_ffmpeg_ios_streamer:ios platform
FFMPEG.C command-line tool ported under Simplest_ffmpeg_ios_transcoder:ios Platform
Simplest_sdl_ios_helloworld: The simplest program to migrate SDL to the iOS platform
The simplest ffmpeg-based mobile sample: IOS Video Decoder