boolXDecode::Send(const AVPacket* pkt) { unique_lock<mutex> lock(mux_); if (!c_) returnfalse; auto re = avcodec_send_packet(c_, pkt); if (re != 0) returnfalse;
returntrue; }
boolXDecode::Recv(AVFrame* frame) { unique_lock<mutex> lock(mux_); if (!c_) returnfalse; auto f = frame; if (c_->hw_device_ctx) { f = av_frame_alloc(); } auto re = avcodec_receive_frame(c_, f); if (re == 0) { if (c_->hw_device_ctx) { re = av_hwframe_transfer_data(frame, f, 0); av_frame_free(&f); if (re != 0) { PrintErr(re); returnfalse; } } returntrue; } if (c_->hw_device_ctx) av_frame_free(&f); returnfalse; }
boolXDecode::InitHW(int type) { unique_lock<mutex> lock(mux_); if (!c_) returnfalse;
AVBufferRef *ctx = nullptr; auto re = av_hwdevice_ctx_create(&ctx, (AVHWDeviceType)type, NULL, NULL, 0); if (re != 0) { PrintErr(re); returnfalse; } c_->hw_device_ctx = ctx; cout << "硬件加速: " << type << endl;
unique_lock<mutex> lock(mux_); if (!c_) return res;
int ret = avcodec_send_packet(c_, NULL); while (ret >= 0) { auto frame = av_frame_alloc(); ret = avcodec_receive_frame(c_, frame); if (ret < 0) { av_frame_free(&frame); break; } res.push_back(frame); }
return res; }
Send()
作用:函数用于向解码器发送编码数据包。
1 2 3 4 5 6 7 8 9 10 11
boolXDecode::Send(const AVPacket* pkt) { unique_lock<mutex> lock(mux_); if (!c_) returnfalse; // 将数据包pkt发送到解码器c_ auto re = avcodec_send_packet(c_, pkt); if (re != 0) returnfalse;
unique_lock<mutex> lock(mux_); if (!c_) return res;
// 取出缓冲中的数据 int ret = avcodec_send_packet(c_, NULL); while (ret >= 0) { auto frame = av_frame_alloc(); ret = avcodec_receive_frame(c_, frame); if (ret < 0) { av_frame_free(&frame); break; } res.push_back(frame); }