FFmpeg  4.4.5
nvdec_mpeg12.c
Go to the documentation of this file.
1 /*
2  * MPEG-1/2 HW decode acceleration through NVDEC
3  *
4  * Copyright (c) 2017 Philip Langdale
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avcodec.h"
24 #include "mpegvideo.h"
25 #include "nvdec.h"
26 #include "decode.h"
27 
28 static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
29 {
30  MpegEncContext *s = avctx->priv_data;
31 
33  CUVIDPICPARAMS *pp = &ctx->pic_params;
34  CUVIDMPEG2PICPARAMS *ppc = &pp->CodecSpecific.mpeg2;
35  FrameDecodeData *fdd;
36  NVDECFrame *cf;
37  AVFrame *cur_frame = s->current_picture.f;
38 
39  int ret, i;
40 
41  ret = ff_nvdec_start_frame(avctx, cur_frame);
42  if (ret < 0)
43  return ret;
44 
45  fdd = (FrameDecodeData*)cur_frame->private_ref->data;
46  cf = (NVDECFrame*)fdd->hwaccel_priv;
47 
48  *pp = (CUVIDPICPARAMS) {
49  .PicWidthInMbs = (cur_frame->width + 15) / 16,
50  .FrameHeightInMbs = (cur_frame->height + 15) / 16,
51  .CurrPicIdx = cf->idx,
52 
53  .field_pic_flag = s->picture_structure != PICT_FRAME,
54  .bottom_field_flag = s->picture_structure == PICT_BOTTOM_FIELD,
55  .second_field = s->picture_structure != PICT_FRAME && !s->first_field,
56 
57  .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I,
58  .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
59  s->pict_type == AV_PICTURE_TYPE_P,
60 
61  .CodecSpecific.mpeg2 = {
62  .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f),
63  .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_picture.f),
64 
65  .picture_coding_type = s->pict_type,
66  .full_pel_forward_vector = s->full_pel[0],
67  .full_pel_backward_vector = s->full_pel[1],
68  .f_code = { { s->mpeg_f_code[0][0],
69  s->mpeg_f_code[0][1] },
70  { s->mpeg_f_code[1][0],
71  s->mpeg_f_code[1][1] } },
72  .intra_dc_precision = s->intra_dc_precision,
73  .frame_pred_frame_dct = s->frame_pred_frame_dct,
74  .concealment_motion_vectors = s->concealment_motion_vectors,
75  .q_scale_type = s->q_scale_type,
76  .intra_vlc_format = s->intra_vlc_format,
77  .alternate_scan = s->alternate_scan,
78  .top_field_first = s->top_field_first,
79  }
80  };
81 
82  for (i = 0; i < 64; ++i) {
83  int n = s->idsp.idct_permutation[i];
84  ppc->QuantMatrixIntra[i] = s->intra_matrix[n];
85  ppc->QuantMatrixInter[i] = s->inter_matrix[n];
86  }
87 
88  return 0;
89 }
90 
92  AVBufferRef *hw_frames_ctx)
93 {
94  // Each frame can at most have one P and one B reference
95  return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2, 0);
96 }
97 
98 #if CONFIG_MPEG2_NVDEC_HWACCEL
100  .name = "mpeg2_nvdec",
101  .type = AVMEDIA_TYPE_VIDEO,
103  .pix_fmt = AV_PIX_FMT_CUDA,
104  .start_frame = nvdec_mpeg12_start_frame,
105  .end_frame = ff_nvdec_simple_end_frame,
106  .decode_slice = ff_nvdec_simple_decode_slice,
107  .frame_params = nvdec_mpeg12_frame_params,
108  .init = ff_nvdec_decode_init,
109  .uninit = ff_nvdec_decode_uninit,
110  .priv_data_size = sizeof(NVDECContext),
111 };
112 #endif
113 
114 #if CONFIG_MPEG1_NVDEC_HWACCEL
116  .name = "mpeg1_nvdec",
117  .type = AVMEDIA_TYPE_VIDEO,
119  .pix_fmt = AV_PIX_FMT_CUDA,
120  .start_frame = nvdec_mpeg12_start_frame,
121  .end_frame = ff_nvdec_simple_end_frame,
122  .decode_slice = ff_nvdec_simple_decode_slice,
123  .frame_params = nvdec_mpeg12_frame_params,
124  .init = ff_nvdec_decode_init,
125  .uninit = ff_nvdec_decode_uninit,
126  .priv_data_size = sizeof(NVDECContext),
127 };
128 #endif
uint8_t
Libavcodec external API header.
#define s(width, name)
Definition: cbs_vp9.c:257
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:50
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
const AVHWAccel ff_mpeg1_nvdec_hwaccel
const AVHWAccel ff_mpeg2_nvdec_hwaccel
int i
Definition: input.c:407
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
#define PICT_FRAME
Definition: mpegutils.h:39
mpegvideo header.
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size, int supports_444)
Definition: nvdec.c:697
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:665
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec.c:675
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:330
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition: nvdec.c:749
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:559
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:262
static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_mpeg12.c:28
static int nvdec_mpeg12_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: nvdec_mpeg12.c:91
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:235
static char buffer[20]
Definition: seek.c:32
A reference to a data buffer.
Definition: buffer.h:84
uint8_t * data
The data buffer.
Definition: buffer.h:92
main external API structure.
Definition: avcodec.h:536
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:571
void * priv_data
Definition: avcodec.h:563
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:180
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int width
Definition: frame.h:376
int height
Definition: frame.h:376
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:697
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2444
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:34
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:52
MpegEncContext.
Definition: mpegvideo.h:81
unsigned int idx
Definition: nvdec.h:45
AVFormatContext * ctx
Definition: movenc.c:48
int size