|
| 1 | +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project |
| 2 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +#include "videodec_impl.h" |
| 5 | + |
| 6 | +#include "common/alignment.h" |
| 7 | +#include "common/assert.h" |
| 8 | +#include "common/logging/log.h" |
| 9 | +#include "core/libraries/error_codes.h" |
| 10 | + |
| 11 | +// The av_err2str macro in libavutil/error.h does not play nice with C++ |
| 12 | +#ifdef av_err2str |
| 13 | +#undef av_err2str |
| 14 | +#include <string> |
| 15 | +av_always_inline std::string av_err2string(int errnum) { |
| 16 | + char errbuf[AV_ERROR_MAX_STRING_SIZE]; |
| 17 | + return av_make_error_string(errbuf, AV_ERROR_MAX_STRING_SIZE, errnum); |
| 18 | +} |
| 19 | +#define av_err2str(err) av_err2string(err).c_str() |
| 20 | +#endif // av_err2str |
| 21 | + |
| 22 | +namespace Libraries::Videodec { |
| 23 | + |
| 24 | +static inline void CopyNV12Data(u8* dst, const AVFrame& src) { |
| 25 | + std::memcpy(dst, src.data[0], src.width * src.height); |
| 26 | + std::memcpy(dst + (src.width * src.height), src.data[1], (src.width * src.height) / 2); |
| 27 | +} |
| 28 | + |
| 29 | +VdecDecoder::VdecDecoder(const OrbisVideodecConfigInfo& pCfgInfoIn, |
| 30 | + const OrbisVideodecResourceInfo& pRsrcInfoIn) { |
| 31 | + |
| 32 | + const AVCodec* codec = avcodec_find_decoder(AV_CODEC_ID_H264); |
| 33 | + ASSERT(codec); |
| 34 | + |
| 35 | + mCodecContext = avcodec_alloc_context3(codec); |
| 36 | + ASSERT(mCodecContext); |
| 37 | + mCodecContext->width = pCfgInfoIn.maxFrameWidth; |
| 38 | + mCodecContext->height = pCfgInfoIn.maxFrameHeight; |
| 39 | + |
| 40 | + avcodec_open2(mCodecContext, codec, nullptr); |
| 41 | +} |
| 42 | + |
| 43 | +VdecDecoder::~VdecDecoder() { |
| 44 | + avcodec_free_context(&mCodecContext); |
| 45 | + sws_freeContext(mSwsContext); |
| 46 | +} |
| 47 | + |
| 48 | +s32 VdecDecoder::Decode(const OrbisVideodecInputData& pInputDataIn, |
| 49 | + OrbisVideodecFrameBuffer& pFrameBufferInOut, |
| 50 | + OrbisVideodecPictureInfo& pPictureInfoOut) { |
| 51 | + pPictureInfoOut.thisSize = sizeof(OrbisVideodecPictureInfo); |
| 52 | + pPictureInfoOut.isValid = false; |
| 53 | + pPictureInfoOut.isErrorPic = true; |
| 54 | + |
| 55 | + if (!pInputDataIn.pAuData) { |
| 56 | + return ORBIS_VIDEODEC_ERROR_AU_POINTER; |
| 57 | + } |
| 58 | + if (pInputDataIn.auSize == 0) { |
| 59 | + return ORBIS_VIDEODEC_ERROR_AU_SIZE; |
| 60 | + } |
| 61 | + |
| 62 | + AVPacket* packet = av_packet_alloc(); |
| 63 | + if (!packet) { |
| 64 | + LOG_ERROR(Lib_Videodec, "Failed to allocate packet"); |
| 65 | + return ORBIS_VIDEODEC_ERROR_API_FAIL; |
| 66 | + } |
| 67 | + |
| 68 | + packet->data = (u8*)pInputDataIn.pAuData; |
| 69 | + packet->size = pInputDataIn.auSize; |
| 70 | + packet->pts = pInputDataIn.ptsData; |
| 71 | + packet->dts = pInputDataIn.dtsData; |
| 72 | + |
| 73 | + int ret = avcodec_send_packet(mCodecContext, packet); |
| 74 | + if (ret < 0) { |
| 75 | + LOG_ERROR(Lib_Videodec, "Error sending packet to decoder: {}", ret); |
| 76 | + av_packet_free(&packet); |
| 77 | + return ORBIS_VIDEODEC_ERROR_API_FAIL; |
| 78 | + } |
| 79 | + |
| 80 | + AVFrame* frame = av_frame_alloc(); |
| 81 | + if (frame == nullptr) { |
| 82 | + LOG_ERROR(Lib_Videodec, "Failed to allocate frame"); |
| 83 | + av_packet_free(&packet); |
| 84 | + return ORBIS_VIDEODEC_ERROR_API_FAIL; |
| 85 | + } |
| 86 | + |
| 87 | + while (true) { |
| 88 | + ret = avcodec_receive_frame(mCodecContext, frame); |
| 89 | + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 90 | + break; |
| 91 | + } else if (ret < 0) { |
| 92 | + LOG_ERROR(Lib_Videodec, "Error receiving frame from decoder: {}", ret); |
| 93 | + av_packet_free(&packet); |
| 94 | + av_frame_free(&frame); |
| 95 | + return ORBIS_VIDEODEC_ERROR_API_FAIL; |
| 96 | + } |
| 97 | + |
| 98 | + if (frame->format != AV_PIX_FMT_NV12) { |
| 99 | + AVFrame* nv12_frame = ConvertNV12Frame(*frame); |
| 100 | + ASSERT(nv12_frame); |
| 101 | + av_frame_free(&frame); |
| 102 | + frame = nv12_frame; |
| 103 | + } |
| 104 | + |
| 105 | + CopyNV12Data((u8*)pFrameBufferInOut.pFrameBuffer, *frame); |
| 106 | + |
| 107 | + pPictureInfoOut.codecType = 1; |
| 108 | + pPictureInfoOut.frameWidth = frame->width; |
| 109 | + pPictureInfoOut.frameHeight = frame->height; |
| 110 | + pPictureInfoOut.framePitch = frame->linesize[0]; |
| 111 | + |
| 112 | + pPictureInfoOut.isValid = true; |
| 113 | + pPictureInfoOut.isErrorPic = false; |
| 114 | + } |
| 115 | + |
| 116 | + av_packet_free(&packet); |
| 117 | + av_frame_free(&frame); |
| 118 | + return ORBIS_OK; |
| 119 | +} |
| 120 | + |
| 121 | +s32 VdecDecoder::Flush(OrbisVideodecFrameBuffer& pFrameBufferInOut, |
| 122 | + OrbisVideodecPictureInfo& pPictureInfoOut) { |
| 123 | + pPictureInfoOut.thisSize = sizeof(pPictureInfoOut); |
| 124 | + pPictureInfoOut.isValid = false; |
| 125 | + pPictureInfoOut.isErrorPic = true; |
| 126 | + |
| 127 | + AVFrame* frame = av_frame_alloc(); |
| 128 | + if (!frame) { |
| 129 | + LOG_ERROR(Lib_Videodec, "Failed to allocate frame"); |
| 130 | + return ORBIS_VIDEODEC_ERROR_API_FAIL; |
| 131 | + } |
| 132 | + |
| 133 | + while (true) { |
| 134 | + int ret = avcodec_receive_frame(mCodecContext, frame); |
| 135 | + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 136 | + break; |
| 137 | + } else if (ret < 0) { |
| 138 | + LOG_ERROR(Lib_Videodec, "Error receiving frame from decoder: {}", ret); |
| 139 | + av_frame_free(&frame); |
| 140 | + return ORBIS_VIDEODEC_ERROR_API_FAIL; |
| 141 | + } |
| 142 | + |
| 143 | + if (frame->format != AV_PIX_FMT_NV12) { |
| 144 | + AVFrame* nv12_frame = ConvertNV12Frame(*frame); |
| 145 | + ASSERT(nv12_frame); |
| 146 | + av_frame_free(&frame); |
| 147 | + frame = nv12_frame; |
| 148 | + } |
| 149 | + |
| 150 | + CopyNV12Data((u8*)pFrameBufferInOut.pFrameBuffer, *frame); |
| 151 | + |
| 152 | + pPictureInfoOut.codecType = 1; |
| 153 | + pPictureInfoOut.frameWidth = frame->width; |
| 154 | + pPictureInfoOut.frameHeight = frame->height; |
| 155 | + pPictureInfoOut.framePitch = frame->linesize[0]; |
| 156 | + |
| 157 | + pPictureInfoOut.isValid = true; |
| 158 | + pPictureInfoOut.isErrorPic = false; |
| 159 | + } |
| 160 | + |
| 161 | + av_frame_free(&frame); |
| 162 | + return ORBIS_OK; |
| 163 | +} |
| 164 | + |
| 165 | +s32 VdecDecoder::Reset() { |
| 166 | + avcodec_flush_buffers(mCodecContext); |
| 167 | + return ORBIS_OK; |
| 168 | +} |
| 169 | + |
| 170 | +AVFrame* VdecDecoder::ConvertNV12Frame(AVFrame& frame) { |
| 171 | + AVFrame* nv12_frame = av_frame_alloc(); |
| 172 | + nv12_frame->pts = frame.pts; |
| 173 | + nv12_frame->pkt_dts = frame.pkt_dts < 0 ? 0 : frame.pkt_dts; |
| 174 | + nv12_frame->format = AV_PIX_FMT_NV12; |
| 175 | + nv12_frame->width = frame.width; |
| 176 | + nv12_frame->height = frame.height; |
| 177 | + nv12_frame->sample_aspect_ratio = frame.sample_aspect_ratio; |
| 178 | + nv12_frame->crop_top = frame.crop_top; |
| 179 | + nv12_frame->crop_bottom = frame.crop_bottom; |
| 180 | + nv12_frame->crop_left = frame.crop_left; |
| 181 | + nv12_frame->crop_right = frame.crop_right; |
| 182 | + |
| 183 | + av_frame_get_buffer(nv12_frame, 0); |
| 184 | + |
| 185 | + if (mSwsContext == nullptr) { |
| 186 | + mSwsContext = sws_getContext(frame.width, frame.height, AVPixelFormat(frame.format), |
| 187 | + nv12_frame->width, nv12_frame->height, AV_PIX_FMT_NV12, |
| 188 | + SWS_FAST_BILINEAR, nullptr, nullptr, nullptr); |
| 189 | + } |
| 190 | + |
| 191 | + const auto res = sws_scale(mSwsContext, frame.data, frame.linesize, 0, frame.height, |
| 192 | + nv12_frame->data, nv12_frame->linesize); |
| 193 | + if (res < 0) { |
| 194 | + LOG_ERROR(Lib_Videodec, "Could not convert to NV12: {}", av_err2str(res)); |
| 195 | + return nullptr; |
| 196 | + } |
| 197 | + |
| 198 | + return nv12_frame; |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace Libraries::Videodec |
0 commit comments