一、ffmpeg视频解码流程
1. 初始化ffmpeg环境
av_register_all();
2. 打开文件
nRet = avformat_open_input(&pFormatCtx, filePath, NULL, NULL);
3. 获取视频文件流信息
nRet = avformat_find_stream_info(pFormatCtx, NULL);
4. 查找流解码器信息
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
5. 解码器解码流数据
nRet = avcodec_open2(pCodecCtx, pCodec, NULL);
6. 按顺序读取帧数据
while(av_read_frame(pFormatCtx, packet) >= 0)
7. 将帧数据解码成图像数据
nRet = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
8. 对图像进行缩放
sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
9. 关闭文件
avformat_close_input(&pFormatCtx);
二、SDL显示流程
1. 初始化sdl环境
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)
2. 创建显示窗口
SDL_Window* hWnd = SDL_CreateWindow("My first ffmpeg video application", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, pCodecCtx->width, pCodecCtx->height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
3. 创建渲染器
SDL_Renderer* render = SDL_CreateRenderer(hWnd, -1, 0 );
4. 创建纹理
SDL_Texture* txtture = SDL_CreateTexture(render, pixFormat, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);
5. 更新纹理
SDL_UpdateYUVTexture(txtture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]
,pFrameYUV->data[1], pFrameYUV->linesize[1]
,pFrameYUV->data[2], pFrameYUV->linesize[2]);
6. 清空
SDL_RenderClear(render);
7. 拷贝纹理
SDL_RenderCopy(render, txtture, NULL, &rect1);
8. 刷新
SDL_RenderPresent(render);
完整代码:
