mirror of
https://git.ffmpeg.org/ffmpeg.git
synced 2024-12-25 00:32:31 +00:00
fe6037fd04
There are two types of MPVPictures: Three (cur_pic, last_pic, next_pic) that are directly part of MpegEncContext and an array of MPVPictures that are separately allocated and are mostly accessed via pointers (cur|last|next)_pic_ptr; they are also used to store AVFrames in the encoder (necessary due to B-frames). As the name implies, each of the former is directly associated with one of the _ptr pointers: They actually share the same underlying buffers, but the ones that are part of the context can have their data pointers offset and their linesize doubled for field pictures. Up until now, each of these had their own references; in particular, there was an underlying av_frame_ref() to sync cur_pic and cur_pic_ptr etc. This is wasteful. This commit changes this relationship: cur_pic, last_pic and next_pic now become MPVWorkPictures; this structure does not have an AVFrame at all any more, but only the cached values of data and linesize. It also contains a pointer to the corresponding MPVPicture, establishing a more natural relationsship between the two. This already means that creating the context-pictures from the pointers can no longer fail. What has not been changed is the fact that the MPVPicture* pointers are not ownership pointers and that the MPVPictures are part of an array of MPVPictures that is owned by a single AVCodecContext. Doing so will be done in a latter commit. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
134 lines
5.0 KiB
C
134 lines
5.0 KiB
C
/*
|
|
* MPEG-1/2 HW decode acceleration through NVDEC
|
|
*
|
|
* Copyright (c) 2017 Philip Langdale
|
|
*
|
|
* This file is part of FFmpeg.
|
|
*
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#include "config_components.h"
|
|
|
|
#include "avcodec.h"
|
|
#include "hwaccel_internal.h"
|
|
#include "internal.h"
|
|
#include "mpegutils.h"
|
|
#include "mpegvideo.h"
|
|
#include "nvdec.h"
|
|
#include "decode.h"
|
|
|
|
static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
|
|
{
|
|
MpegEncContext *s = avctx->priv_data;
|
|
|
|
NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
|
|
CUVIDPICPARAMS *pp = &ctx->pic_params;
|
|
CUVIDMPEG2PICPARAMS *ppc = &pp->CodecSpecific.mpeg2;
|
|
FrameDecodeData *fdd;
|
|
NVDECFrame *cf;
|
|
AVFrame *cur_frame = s->cur_pic.ptr->f;
|
|
|
|
int ret, i;
|
|
|
|
ret = ff_nvdec_start_frame(avctx, cur_frame);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
fdd = (FrameDecodeData*)cur_frame->private_ref->data;
|
|
cf = (NVDECFrame*)fdd->hwaccel_priv;
|
|
|
|
*pp = (CUVIDPICPARAMS) {
|
|
.PicWidthInMbs = (cur_frame->width + 15) / 16,
|
|
.FrameHeightInMbs = (cur_frame->height + 15) / 16,
|
|
.CurrPicIdx = cf->idx,
|
|
|
|
.field_pic_flag = s->picture_structure != PICT_FRAME,
|
|
.bottom_field_flag = s->picture_structure == PICT_BOTTOM_FIELD,
|
|
.second_field = s->picture_structure != PICT_FRAME && !s->first_field,
|
|
|
|
.intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I,
|
|
.ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
|
|
s->pict_type == AV_PICTURE_TYPE_P,
|
|
|
|
.CodecSpecific.mpeg2 = {
|
|
.ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_pic.ptr ? s->last_pic.ptr->f : NULL),
|
|
.BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_pic.ptr ? s->next_pic.ptr->f : NULL),
|
|
|
|
.picture_coding_type = s->pict_type,
|
|
.full_pel_forward_vector = s->full_pel[0],
|
|
.full_pel_backward_vector = s->full_pel[1],
|
|
.f_code = { { s->mpeg_f_code[0][0],
|
|
s->mpeg_f_code[0][1] },
|
|
{ s->mpeg_f_code[1][0],
|
|
s->mpeg_f_code[1][1] } },
|
|
.intra_dc_precision = s->intra_dc_precision,
|
|
.frame_pred_frame_dct = s->frame_pred_frame_dct,
|
|
.concealment_motion_vectors = s->concealment_motion_vectors,
|
|
.q_scale_type = s->q_scale_type,
|
|
.intra_vlc_format = s->intra_vlc_format,
|
|
.alternate_scan = s->alternate_scan,
|
|
.top_field_first = s->top_field_first,
|
|
}
|
|
};
|
|
|
|
for (i = 0; i < 64; ++i) {
|
|
int n = s->idsp.idct_permutation[i];
|
|
ppc->QuantMatrixIntra[i] = s->intra_matrix[n];
|
|
ppc->QuantMatrixInter[i] = s->inter_matrix[n];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int nvdec_mpeg12_frame_params(AVCodecContext *avctx,
|
|
AVBufferRef *hw_frames_ctx)
|
|
{
|
|
// Each frame can at most have one P and one B reference
|
|
return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2, 0);
|
|
}
|
|
|
|
#if CONFIG_MPEG2_NVDEC_HWACCEL
|
|
const FFHWAccel ff_mpeg2_nvdec_hwaccel = {
|
|
.p.name = "mpeg2_nvdec",
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
.p.id = AV_CODEC_ID_MPEG2VIDEO,
|
|
.p.pix_fmt = AV_PIX_FMT_CUDA,
|
|
.start_frame = nvdec_mpeg12_start_frame,
|
|
.end_frame = ff_nvdec_simple_end_frame,
|
|
.decode_slice = ff_nvdec_simple_decode_slice,
|
|
.frame_params = nvdec_mpeg12_frame_params,
|
|
.init = ff_nvdec_decode_init,
|
|
.uninit = ff_nvdec_decode_uninit,
|
|
.priv_data_size = sizeof(NVDECContext),
|
|
};
|
|
#endif
|
|
|
|
#if CONFIG_MPEG1_NVDEC_HWACCEL
|
|
const FFHWAccel ff_mpeg1_nvdec_hwaccel = {
|
|
.p.name = "mpeg1_nvdec",
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
.p.id = AV_CODEC_ID_MPEG1VIDEO,
|
|
.p.pix_fmt = AV_PIX_FMT_CUDA,
|
|
.start_frame = nvdec_mpeg12_start_frame,
|
|
.end_frame = ff_nvdec_simple_end_frame,
|
|
.decode_slice = ff_nvdec_simple_decode_slice,
|
|
.frame_params = nvdec_mpeg12_frame_params,
|
|
.init = ff_nvdec_decode_init,
|
|
.uninit = ff_nvdec_decode_uninit,
|
|
.priv_data_size = sizeof(NVDECContext),
|
|
};
|
|
#endif
|