Page 1 of 1

971130 - I get invalid argument error in avcodec_send_packet

Posted: Sat Feb 23, 2019 10:21 am
by hamidi
In the following code:

Code: Select all

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;
}
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?