FFmpeg在Windows上通過dshow編解碼方式設置爲mjpeg並實時顯示測試代碼

Windows上默認的內置攝像頭一般支持兩種編解碼格式:rawvideo和mjpeg。在調用FFmpeg接口時默認的採用rawvideo。這裏通過DirectShow實現爲mjpeg進行編解碼。

通過命令行調用FFmpeg可執行文件:

(1). 可獲取Windows上連接的視頻設備,命令如下:

ffmpeg.exe -list_devices true -f dshow -i dummy

(2). 可獲取指定視頻設備支持的編解碼格式和video size,命令如下:

ffmpeg.exe -f dshow -list_options true -i video="Integrated Webcam"

這裏通過C++實現獲取連接的視頻設備,代碼如下:

AVFormatContext* format_context = avformat_alloc_context();
AVDictionary* dict = nullptr;
av_dict_set(&dict, "list_devices", "true", 0);
AVInputFormat* input_format = av_find_input_format("dshow");
avformat_open_input(&format_context, "", input_format, &dict);
avformat_close_input(&format_context);
av_dict_free(&dict);

這裏通過C++實現支持的編解碼格式和video size,代碼如下:

AVFormatContext* format_context = avformat_alloc_context();
AVDictionary* dict = nullptr;
av_dict_set(&dict, "list_options", "true", 0);
AVInputFormat* input_format = av_find_input_format("dshow");
avformat_open_input(&format_context, "video=Integrated Webcam", input_format, &dict); // video=video device name
avformat_close_input(&format_context);
av_dict_free(&dict);

設置編解碼格式通過av_dict_set好像不能成功,這裏是通過AVFormatContext的video_codec_id進行設置,通過av_dict_set設置幀率好像也不能成功。通過調用OpenCV的接口進行實時顯示,代碼如下:

#include "funset.hpp"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <memory>
#include <fstream>
#include <thread>
#include "common.hpp"

#ifdef __cplusplus
extern "C" {
#endif

#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/mem.h>
#include <libavutil/imgutils.h>

#ifdef __cplusplus
}
#endif

#include <opencv2/opencv.hpp>

#ifdef _MSC_VER
int test_ffmpeg_decode_dshow()
{
	avdevice_register_all();

	AVCodecID id = AV_CODEC_ID_MJPEG;
	AVCodec* encoder_id = avcodec_find_encoder(id);
	AVCodec* decoder_id = avcodec_find_decoder(id);
	if (!encoder_id || !decoder_id) {
		fprintf(stderr, "codec not found: %d\n", id);
		return -1;
	}

	AVFormatContext* format_context = avformat_alloc_context();
	format_context->video_codec_id = id; // 指定編解碼格式

	AVInputFormat* input_format = av_find_input_format("dshow");
	AVDictionary* dict = nullptr;
	//if (av_dict_set(&dict, "vcodec"/*"input_format"*/, "mjpeg", 0) < 0) fprintf(stderr, "fail to av_dict_set: line: %d\n", __LINE__); // 通過av_dict_set設置編解碼格式好像不起作用
	if (av_dict_set(&dict, "video_size", "320x240", 0) < 0) fprintf(stderr, "fail to av_dict_set: line: %d\n", __LINE__);
	//if (av_dict_set(&dict, "r", "25", 0) < 0) fprintf(stderr, "fail to av_dict_set: line: %d\n", __LINE__); // 通過av_dict_set設置幀率好像不起作用
	int ret = avformat_open_input(&format_context, "video=Integrated Webcam", input_format, &dict);
	if (ret != 0) {
		fprintf(stderr, "fail to avformat_open_input: %d\n", ret);
		return -1;
	}

	ret = avformat_find_stream_info(format_context, nullptr);
	if (ret < 0) {
		fprintf(stderr, "fail to get stream information: %d\n", ret);
		return -1;
	}

	int video_stream_index = -1;
	for (unsigned int i = 0; i < format_context->nb_streams; ++i) {
		const AVStream* stream = format_context->streams[i];
		if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
			video_stream_index = i;
			fprintf(stdout, "type of the encoded data: %d, dimensions of the video frame in pixels: width: %d, height: %d, pixel format: %d\n",
				stream->codecpar->codec_id, stream->codecpar->width, stream->codecpar->height, stream->codecpar->format);
		}
	}

	if (video_stream_index == -1) {
		fprintf(stderr, "no video stream\n");
		return -1;
	}

	fprintf(stdout, "frame rate: %f\n", av_q2d(format_context->streams[video_stream_index]->r_frame_rate));

	AVCodecParameters* codecpar = format_context->streams[video_stream_index]->codecpar;
	const AVCodec* codec = avcodec_find_decoder(codecpar->codec_id);
	if (!codec) {
		fprintf(stderr, "fail to avcodec_find_decoder\n");
		return -1;
	}

	if (codecpar->codec_id != id) {
		fprintf(stderr, "this test code only support mjpeg encode: %d\n", codecpar->codec_id);
		return -1;
	}

	AVCodecContext* codec_context = avcodec_alloc_context3(codec);
	if (!codec_context) {
		fprintf(stderr, "fail to avcodec_alloc_context3\n");
		return -1;
	}

	codec_context->pix_fmt = AVPixelFormat(codecpar->format);
	codec_context->height = codecpar->height;
	codec_context->width = codecpar->width;
	codec_context->thread_count = 16;
	ret = avcodec_open2(codec_context, codec, nullptr);
	if (ret != 0) {
		fprintf(stderr, "fail to avcodec_open2: %d\n", ret);
		return -1;
	}

	AVPixelFormat dst_pixel_format = AV_PIX_FMT_BGR24;
	AVFrame* frame = av_frame_alloc();
	AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));
	SwsContext* sws_context = sws_getContext(codec_context->width, codec_context->height, codec_context->pix_fmt, codec_context->width, codec_context->height, dst_pixel_format, 0, nullptr, nullptr, nullptr);
	if (!frame || !packet || !sws_context) {
		fprintf(stderr, "fail to alloc\n");
		return -1;
	}

	uint8_t *bgr_data[4];
	int bgr_linesize[4];
	av_image_alloc(bgr_data, bgr_linesize, codec_context->width, codec_context->height, dst_pixel_format, 1);
	cv::Mat mat(codec_context->height, codec_context->width, CV_8UC3);
	const char* winname = "dshow mjpeg video";
	cv::namedWindow(winname);

	while (1) {
		ret = av_read_frame(format_context, packet);
		if (ret >= 0 && packet->stream_index == video_stream_index && packet->size > 0) {
			ret = avcodec_send_packet(codec_context, packet);
			if (ret < 0) {
				fprintf(stderr, "##### fail to avcodec_send_packet: %d\n", ret);
				av_packet_unref(packet);
				continue;
			}

			ret = avcodec_receive_frame(codec_context, frame);
			if (ret < 0) {
				fprintf(stderr, "##### fail to avcodec_receive_frame: %d\n", ret);
				av_packet_unref(packet);
				continue;
			}

			sws_scale(sws_context, frame->data, frame->linesize, 0, codec_context->height, bgr_data, bgr_linesize);
			mat.data = bgr_data[0];
			cv::imshow(winname, mat);
		} else if (ret < 0 || packet->size <= 0) {
			fprintf(stderr, "##### fail to av_read_frame: %d, packet size: %d\n", ret, packet->size);
			continue;
		}

		av_packet_unref(packet);

		int key = cv::waitKey(30);
		if (key == 27) break;
	}

	cv::destroyWindow(winname);
	sws_freeContext(sws_context);
	av_frame_free(&frame);
	av_freep(packet);
	av_freep(&bgr_data[0]);
	avformat_close_input(&format_context);
	av_dict_free(&dict);
}

	fprintf(stdout, "test finish\n");
	return 0;
}
#else
int test_ffmpeg_decode_dshow()
{
	fprintf(stderr, "Error: this test code only support windows platform\n");
	return -1;
}
#endif

執行結果如下:

GitHubhttps://github.com/fengbingchun/OpenCV_Test

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章