最简单的基于FFMPEG+SDL的视频播放器:拆分

=====================================================

最简单的基于FFmpeg的视频播放器系列文章列表:

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

最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)

最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器

最简单的基于FFMPEG的Helloworld程序

=====================================================

本文补充记录《最简单的基于FFMPEG+SDL的视频播放器》中的两个例子:FFmpeg视频解码器和SDL像素数据播放器。这两个部分是从视频播放器中拆分出来的两个例子。FFmpeg视频解码器实现了视频数据到YUV数据的解码,而SDL像素数据播放器实现了YUV数据的显示。简而言之,原先的FFmpeg+SDL视频播放器实现了:

视频数据->YUV->显示器

FFmpeg视频解码器实现了:

视频数据->YUV

SDL像素数据播放器实现了:

YUV->显示器

FFmpeg视频解码器源代码/** * 最简单的基于FFmpeg的视频解码器 * Simplest FFmpeg Decoder * * 雷霄骅 Lei Xiaohua * leixiaohua1020@126.com * 中国传媒大学/数字电视技术 * Communication University of China / Digital TV Technology * * * * 本程序实现了视频文件解码为YUV数据。它使用了libavcodec和 * libavformat。是最简单的FFmpeg视频解码方面的教程。 * 通过学习本例子可以了解FFmpeg的解码流程。 * This software is a simplest decoder based on FFmpeg. * It decodes video to YUV pixel data. * It uses libavcodec and libavformat. * Suitable for beginner of FFmpeg. * */#include <stdio.h>#define __STDC_CONSTANT_MACROS#ifdef _WIN32//Windowsextern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"#include "libswscale/swscale.h"};#else//Linux…#ifdef __cplusplusextern "C"{#endif#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#include <libswscale/swscale.h>#ifdef __cplusplus};#endif#endifint main(int argc, char* argv[]){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;char filepath[]="Titanic.mkv";FILE *fp_yuv=fopen("output.yuv","wb+");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");return -1;}if(avformat_find_stream_info(pFormatCtx,NULL)<0){printf("Couldn’t find stream information.\n");return -1;}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("Didn’t find a video stream.\n");return -1;}pCodecCtx=pFormatCtx->streams[videoindex]->codec;pCodec=avcodec_find_decoder(pCodecCtx->codec_id);if(pCodec==NULL){printf("Codec not found.\n");return -1;}if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){printf("Could not open codec.\n");return -1;}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));//Output Info—————————–printf("————— File Information —————-\n");av_dump_format(pFormatCtx,0,filepath,0);printf("————————————————-\n");img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); 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 -1;}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); //Yfwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //Ufwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //Vprintf("Succeed to decode 1 frame!\n");}}av_free_packet(packet);}//flush decoder//FIX: Flush Frames remained in Codecwhile (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); //Ufwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //Vprintf("Flush Decoder: Succeed to decode 1 frame!\n");}sws_freeContext(img_convert_ctx);fclose(fp_yuv);av_frame_free(&pFrameYUV);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;}运行结果程序运行后,会解码下面的视频文件。想想我的影子,我会在你身后给你一个拥抱;

最简单的基于FFMPEG+SDL的视频播放器:拆分

相关文章:

你感兴趣的文章:

标签云: