FFmpeg解码-Opencv数据显示-双线程调度

来源:互联网 发布:00后直播软件 编辑:程序博客网 时间:2024/05/20 04:11


大致想法是:使用ffmpeg实现解码,解码后的数据转化为 Mat,调用 opencv中的函数显示,同时开启两个线程解码显示两路视频


<span style="font-size:18px;">/*** 抽取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[2] = {"720p.m2v", "cuc_ieschool.avi" };DWORD WINAPI ffmpeg_decoder1(LPVOID pM){AVCodec *pCodec; //解码器指针AVCodecContext* pCodecCtx; //ffmpeg解码类的类成员AVFrame* pAvFrame; //多媒体帧,保存解码后的数据帧AVFormatContext* pFormatCtx; //保存视频流的信息av_register_all(); //注册库中所有可用的文件格式和编码器pFormatCtx = avformat_alloc_context();char *filename = (char *)pM;if (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);AVPacket* packet = (AVPacket*)malloc(sizeof(AVPacket));printf("-----------输出文件信息---------\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); //opencvcv::Mat pCvMat;pCvMat.create(cv::Size(pCodecCtx->width, pCodecCtx->height), CV_8UC3);int ret;int got_picture;cvNamedWindow("RGB1", 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);memcpy(pCvMat.data, out_buffer, size);imshow("RGB1", pCvMat);waitKey(1);}}av_free_packet(packet);}else {break;}}av_free(out_buffer);av_free(pFrameBGR);av_free(pAvFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);sws_freeContext(img_convert_ctx);cvDestroyWindow("RGB1");}DWORD WINAPI ffmpeg_decoder2(LPVOID pM){AVCodec *pCodec; //解码器指针AVCodecContext* pCodecCtx; //ffmpeg解码类的类成员AVFrame* pAvFrame; //多媒体帧,保存解码后的数据帧AVFormatContext* pFormatCtx; //保存视频流的信息av_register_all(); //注册库中所有可用的文件格式和编码器pFormatCtx = avformat_alloc_context();char *filename = (char *)pM;if (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);AVPacket* packet = (AVPacket*)malloc(sizeof(AVPacket));printf("-----------输出文件信息---------\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); //opencvcv::Mat pCvMat;pCvMat.create(cv::Size(pCodecCtx->width, pCodecCtx->height), CV_8UC3);int ret;int got_picture;cvNamedWindow("RGB2", 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);memcpy(pCvMat.data, out_buffer, size);imshow("RGB2", pCvMat);waitKey(1);}}av_free_packet(packet);}else {break;}}av_free(out_buffer);av_free(pFrameBGR);av_free(pAvFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);sws_freeContext(img_convert_ctx);cvDestroyWindow("RGB2");}int main(){HANDLE handle_video1 = CreateThread(NULL, 0, ffmpeg_decoder1, (LPVOID)filename[0], 0, NULL);HANDLE handle_video2 = CreateThread(NULL, 0, ffmpeg_decoder2, (LPVOID)filename[1], 0, NULL);CloseHandle(handle_video1);CloseHandle(handle_video2);while(1);return 0;}</span>


0 0
原创粉丝点击