在以下代码中:
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;
}
我在avcodec_send_packet中收到错误-22。该文件的格式是自定义的,但是帧的格式与我以前从IP摄像机(H264)获得的格式相同。我从ffmpeg来源的hw_decode.c中启发了代码。我错过了什么?