One weeks of effort finally took care of the video playback, the use of FFmpeg decoding video, the decoded data through opengles rendering playback. Find out what you want to know and complete your own study plan, a little excitement. Tomorrow is "51", holiday three days, more happy.
This article realizes the video file playback is before oneself writes the article the actual combat Ffmpeg--ios platform uses the ffmpeg to convert the video file to the YUV file, the actual combat Opengles--ios platform uses Opengles to render the YUV picture On the basis of improved synthesis. Not many interpretations, directly on the code, clearly clear.
NSString *path = [[NSBundle mainbundle] Pathforresource:@"NWN"OfType:@"mp4"]; Const Char*filepath=[path utf8string]; Av_register_all (); Avformat_network_init (); Pformatctx=Avformat_alloc_context (); if(Avformat_open_input (&pformatctx,filepath,null,null)! =0) {printf ("couldn ' t open input stream.\n"); Exit (1); } /** Av_find_stream_info (): FFmpeg version update does not have this function, with Avformat_find_stream_info This function, you can see the version of the update changes*/ //if (Av_find_stream_info (pformatctx) <0) if(Avformat_find_stream_info (Pformatctx, NULL) <0) {printf ("couldn ' t find stream information.\n"); Exit (1); } Videoindex=-1; for(intI=0; i<pformatctx->nb_streams; i++) if(pformatctx->streams[i]->codec->codec_type==Avmedia_type_video) {Videoindex=i; Break; } if(videoindex==-1) {printf ("didn ' t find a video stream.\n"); Exit (1); } pcodecctx=pformatctx->streams[videoindex]->codec; Pcodec=avcodec_find_decoder (pcodecctx->codec_id); if(pcodec==NULL) {printf ("Codec not found.\n"); Exit (1); } /** Avcodec_open (): FFmpeg version update does not have this function, with Avformat_find_stream_info This function, you can see the version of the update changes * AVCODEC_OPEN2 (avcodecconte XT *avctx, const AVCODEC *CODEC, avdictionary **options)*/ //if (Avcodec_open (Pcodecctx, Pcodec) <0) if(Avcodec_open2 (Pcodecctx, Pcodec, NULL) <0) {printf ("Could not open codec.\n"); Exit (1); } avframe*pframe,*PFRAMEYUV; Pframe=Avcodec_alloc_frame (); PFRAMEYUV=Avcodec_alloc_frame (); uint8_t*Out_buffer; Out_buffer=NewUint8_t[avpicture_get_size (pix_fmt_yuv420p, Pcodecctx->width, pcodecctx->height)]; Avpicture_fill ((Avpicture*) PFRAMEYUV, Out_buffer, pix_fmt_yuv420p, Pcodecctx->width, pcodecctx->height); intret, got_picture; intY_size = Pcodecctx->width * pcodecctx->height; Avpacket*packet= (Avpacket *) malloc (sizeof(Avpacket)); Av_new_packet (packet, y_size); printf ("Video infomation:\n"); Av_dump_format (Pformatctx,0, filepath,0); 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"); Exit (1); } if(got_picture) {Char*buf = (Char*) malloc (pframe->width * pframe->height *3/2); Avpicture*PICT; intW, H; Char*y, *u, *v; PICT= (Avpicture *) pframe;//the frame here is the decoded avframe .W = pframe->width; H= pframe->height; Y=buf; U= y + W *h; V= U + w * H/4; for(intI=0; i) memcpy (y+ W * I, pict->data[0] + pict->linesize[0] *I, W); for(intI=0; i2; i++) memcpy (U+ w/2* I, pict->data[1] + pict->linesize[1] * I, w/2); for(intI=0; i
2; i++) memcpy (v+ w/2* I, pict->data[2] + pict->linesize[2] * I, w/2); [MyView setvideosize:pframe->width height:pframe->height]; [MyView displayyuv420pdata:buf Width:pframe->width height:pframe->height]; //Use Opengles to render YUV free (BUF); }} av_free_packet (packet); } delete[] Out_buffer; Av_free (PFRAMEYUV); Avcodec_close (PCODECCTX); Avformat_close_input (&PFORMATCTX);
This week's learning task is completed, after writing this blog will come home from work, haha. 51 Long vacation, I want to rest, list oneself next study plan. Next week I want to do a local audio file decoding playback, as well as local video files in the audio synchronized with the video playback. What is done now is the processing of local audio and video files, because local files are easier to handle, which also makes them fast to learn. About the network audio and video real-time streaming synchronization and playback, this involves the content will be more complex, effective learning + time planning, I am sure to play through the development of the player.
The complete engineering FFMPEGDECODE_OPENGLESRENDERYUV of this paper is: http://pan.baidu.com/s/1dDvpECh
Actual combat ffmpeg + Opengles--ios video decoding and playback on the platform