FFmpeg + Opencv 解码和显示

来源:互联网 发布:redis 安装配置 linux 编辑:程序博客网 时间:2024/05/20 00:52



利用FFmpeg开源编解码库实现解码,转换为Mat格式后,使用Opencv显示


/*** 抽取ffmpeg中的函数接口实现视频解码* 再通过Opencv中的函数接口播放,同时开启两个视频进行操作*/#define __STDC_CONSTANT_MACROS#include <stdio.h>#include <Windows.h>#include <process.h>// Opencv#include <opencv/cv.h>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>extern "C"{#include "libavutil/avutil.h"#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"//新版里的图像转换结构需要引入的头文件#include "libswscale/swscale.h"};using namespace cv;//char* filename = "cuc_ieschool.flv";char* filename = "video0.avi";DWORD WINAPI ffmpeg_decoder(LPVOID pM);HANDLE g_semphore = {NULL};cv::Mat pCvMat;bool g_cvShow = true; //是否显示视频int main(){g_semphore = CreateSemaphore(NULL, 0, 1, NULL);HANDLE handle_video = CreateThread(NULL, 0, ffmpeg_decoder, (LPVOID)filename, 0, NULL);//////////////////////////////////////////////////////////////////////////#if 1cv::Mat pCvMatCopy = pCvMat;namedWindow("cvRGB", 1);int i = 0;while (g_cvShow){cv::imshow("cvRGB", pCvMatCopy);printf("%d",i);++i;}cvDestroyWindow("cvRGB");#endif/////////////////////////////////////////////////////////////////////////WaitForSingleObject(g_semphore, INFINITE);CloseHandle(g_semphore);CloseHandle(handle_video);getchar();return 0;}//ffmpeg解码DWORD WINAPI ffmpeg_decoder(LPVOID pM){AVCodec *pCodec; //解码器指针AVCodecContext* pCodecCtx; //ffmpeg解码类的类成员AVFrame* pAvFrame; //多媒体帧,保存解码后的数据帧AVFormatContext* pFormatCtx; //保存视频流的信息av_register_all(); //注册库中所有可用的文件格式和编码器pFormatCtx = avformat_alloc_context();char *filename = (char *)pM;//convert value typeif (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) { //检查文件头部printf("Can't find the stream!\n");}if (av_find_stream_info(pFormatCtx) < 0) { //查找流信息printf("Can't find the stream information !\n");}int videoindex = -1;for (int i=0; i < pFormatCtx->nb_streams; ++i) //遍历各个流,找到第一个视频流,并记录该流的编码信息{if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {videoindex = i;break;}}if (videoindex == -1) {printf("Don't find a video stream !\n");return -1;}pCodecCtx = pFormatCtx->streams[videoindex]->codec; //得到一个指向视频流的上下文指针pCodec = avcodec_find_decoder(pCodecCtx->codec_id); //到该格式的解码器if (pCodec == NULL) {printf("Cant't find the decoder !\n"); //寻找解码器return -1;}if (avcodec_open2(pCodecCtx,pCodec,NULL) < 0) { //打开解码器printf("Can't open the decoder !\n");return -1;}pAvFrame = avcodec_alloc_frame(); //分配帧存储空间AVFrame* pFrameBGR = avcodec_alloc_frame(); //存储解码后转换的RGB数据// 保存BGR,opencv中是按BGR来保存的int size = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);uint8_t *out_buffer = (uint8_t *)av_malloc(size);avpicture_fill((AVPicture *)pFrameBGR, out_buffer, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); // allocator memory for BGR bufferAVPacket* packet = (AVPacket*)malloc(sizeof(AVPacket)); //output the video infoprintf("-----------输出文件信息---------\n");av_dump_format(pFormatCtx, 0, filename, 0);printf("------------------------------");struct SwsContext *img_convert_ctx;img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL); //opencv,声明Mat对象,作为局部变量//cv::Mat pCvMat;pCvMat.create(cv::Size(pCodecCtx->width, pCodecCtx->height), CV_8UC3);int ret;int got_picture;//cvNamedWindow("RGB", 1);for (;;){if(av_read_frame(pFormatCtx, packet)>=0){if(packet->stream_index==videoindex){ret = avcodec_decode_video2(pCodecCtx, pAvFrame, &got_picture, packet);if(ret < 0){printf("Decode Error.(解码错误)\n");return -1;}if (got_picture){//YUV to RGBsws_scale(img_convert_ctx, (const uint8_t* const*)pAvFrame->data, pAvFrame->linesize, 0, pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize);//convert yuv to BGR24memcpy(pCvMat.data, out_buffer, size);//imshow("RGB", pCvMat);//waitKey(1);}}av_free_packet(packet);}else {break;}}g_cvShow = false;av_free(out_buffer);av_free(pFrameBGR);av_free(pAvFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);sws_freeContext(img_convert_ctx);//cvDestroyWindow("RGB");ReleaseSemaphore(g_semphore, 1, NULL);}









0 0
原创粉丝点击