Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code:
int CplayerDlg::play()
{
	FILE *fp = fopen("video_files/1010.brf", "rb");
	ASSERT(fp);

	RecordFrame rf;
	RecordHeader hdr;
	ASSERT(fread(&rf, sizeof(rf), 1, fp) == 1);
	ASSERT(rf.frameType == FRAME_TYPE_HEADER);
	ASSERT(fread(&hdr, sizeof(hdr), 1, fp) == 1);
	GetDlgItem(IDC_DIM)->SetWindowText(format("%dx%d", hdr.width, hdr.height));
	GetDlgItem(IDC_CODEC_ID)->SetWindowText(format("%d", hdr.codecId));
	GetDlgItem(IDC_PIXEL_FORMAT)->SetWindowText(format("%d", hdr.pixelFormat));
	GetDlgItem(IDC_TIMEBASE)->SetWindowText(format("%d/%d", hdr.timebaseNum, hdr.timebaseDen));

	AVHWDeviceType type = av_hwdevice_find_type_by_name("dxva2");
	ASSERT(type != AV_HWDEVICE_TYPE_NONE);
	AVCodec *decoder = avcodec_find_decoder(AV_CODEC_ID_H264);
	ASSERT(decoder);
	for (int i = 0;; i++)
	{
		const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
		ASSERT(config);
		if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
			config->device_type == type)
		{
			hw_pix_fmt = config->pix_fmt;
			break;
		}
	}
	AVCodecContext *decoder_ctx = avcodec_alloc_context3(decoder);
	ASSERT(decoder_ctx);
	decoder_ctx->get_format = get_hw_format;
	ASSERT(!av_hwdevice_ctx_create(&hw_device_ctx, type, NULL, NULL, 0));
	decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
	ASSERT(!avcodec_open2(decoder_ctx, decoder, NULL));
	AVPacket packet;
	av_init_packet(&packet);
	char buf[100000];
	AVFrame *frame = av_frame_alloc();
	ASSERT(frame);
	while (true)
	{
		ASSERT(fread(&rf, sizeof(rf), 1, fp) == 1);
		ASSERT(rf.frameType == FRAME_TYPE_STREAM);
		ASSERT(fread(buf, rf.frameSize, 1, fp) == 1);
		packet.data = (uint8_t *) buf;
		packet.size = rf.frameSize;
		packet.flags = rf.flags;
		packet.dts = packet.pts = rf.timestamp;
		ASSERT(!avcodec_send_packet(decoder_ctx, &packet));
		ASSERT(!avcodec_receive_frame(decoder_ctx, frame));
	}
	fclose(fp);
	av_frame_free(&frame);
	return 0;
}


What I have tried:

I get error -22 in avcodec_send_packet. The format of the file is custom, but the format of frames is the same as what I've got previously from an IP camera (H264). I inspired the code from hw_decode.c from ffmpeg sources. What I've missed?
Posted
Updated 19-Feb-19 0:28am
v2
Comments
Richard MacCutchan 19-Feb-19 13:32pm    
What does error -22 mean?
ilostmyid2 23-Feb-19 0:40am    
Invalid argument
Richard MacCutchan 23-Feb-19 3:51am    
Looking at the documentation (FFmpeg: AVPacket Struct Reference[^]), I suspect your buffer pointer is not a valid FFmpeg: AVBufferRef Struct Reference[^].
ilostmyid2 6-Mar-19 9:33am    
Before I come here to see your comment, I tried using FFmpegInvoke wrapper in a C# project with the same work and this time it worked!
My final goal was to convert the code to C# after getting successful result from it. Now that C# code is working, I don't care about the C++ code.
ilostmyid2 6-Mar-19 9:35am    
But then, another issue occurs. I get the following error while decoding packets:
[h264 @ 007ecf00] error while decoding MB 68 3, bytestream -6
[h264 @ 007ecf00] concealing 3341 DC, 3341 AC, 3341 MV errors in I frame
[h264 @ 007ecf00] error while decoding MB 67 4, bytestream -9
[h264 @ 007ecf00] concealing 3262 DC, 3262 AC, 3262 MV errors in I frame
....
This happens when I call FFmpegInvoke.avcodec_send_packet, not when I call FFmpegInvoke.avcodec_receive_frame.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900