FFmpeg
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 "config_components.h"
24 
25 #include "avcodec.h"
26 #include "hwaccel_internal.h"
27 #include "internal.h"
28 #include "mpegvideo.h"
29 #include "nvdec.h"
30 #include "decode.h"
31 
32 static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
33 {
34  MpegEncContext *s = avctx->priv_data;
35 
37  CUVIDPICPARAMS *pp = &ctx->pic_params;
38  CUVIDMPEG2PICPARAMS *ppc = &pp->CodecSpecific.mpeg2;
39  FrameDecodeData *fdd;
40  NVDECFrame *cf;
41  AVFrame *cur_frame = s->current_picture.f;
42 
43  int ret, i;
44 
45  ret = ff_nvdec_start_frame(avctx, cur_frame);
46  if (ret < 0)
47  return ret;
48 
49  fdd = (FrameDecodeData*)cur_frame->private_ref->data;
50  cf = (NVDECFrame*)fdd->hwaccel_priv;
51 
52  *pp = (CUVIDPICPARAMS) {
53  .PicWidthInMbs = (cur_frame->width + 15) / 16,
54  .FrameHeightInMbs = (cur_frame->height + 15) / 16,
55  .CurrPicIdx = cf->idx,
56 
57  .field_pic_flag = s->picture_structure != PICT_FRAME,
58  .bottom_field_flag = s->picture_structure == PICT_BOTTOM_FIELD,
59  .second_field = s->picture_structure != PICT_FRAME && !s->first_field,
60 
61  .intra_pic_flag = s->pict_type == AV_PICTURE_TYPE_I,
62  .ref_pic_flag = s->pict_type == AV_PICTURE_TYPE_I ||
63  s->pict_type == AV_PICTURE_TYPE_P,
64 
65  .CodecSpecific.mpeg2 = {
66  .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f),
67  .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_picture.f),
68 
69  .picture_coding_type = s->pict_type,
70  .full_pel_forward_vector = s->full_pel[0],
71  .full_pel_backward_vector = s->full_pel[1],
72  .f_code = { { s->mpeg_f_code[0][0],
73  s->mpeg_f_code[0][1] },
74  { s->mpeg_f_code[1][0],
75  s->mpeg_f_code[1][1] } },
76  .intra_dc_precision = s->intra_dc_precision,
77  .frame_pred_frame_dct = s->frame_pred_frame_dct,
78  .concealment_motion_vectors = s->concealment_motion_vectors,
79  .q_scale_type = s->q_scale_type,
80  .intra_vlc_format = s->intra_vlc_format,
81  .alternate_scan = s->alternate_scan,
82  .top_field_first = s->top_field_first,
83  }
84  };
85 
86  for (i = 0; i < 64; ++i) {
87  int n = s->idsp.idct_permutation[i];
88  ppc->QuantMatrixIntra[i] = s->intra_matrix[n];
89  ppc->QuantMatrixInter[i] = s->inter_matrix[n];
90  }
91 
92  return 0;
93 }
94 
96  AVBufferRef *hw_frames_ctx)
97 {
98  // Each frame can at most have one P and one B reference
99  return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2, 0);
100 }
101 
102 #if CONFIG_MPEG2_NVDEC_HWACCEL
104  .p.name = "mpeg2_nvdec",
105  .p.type = AVMEDIA_TYPE_VIDEO,
106  .p.id = AV_CODEC_ID_MPEG2VIDEO,
107  .p.pix_fmt = AV_PIX_FMT_CUDA,
108  .start_frame = nvdec_mpeg12_start_frame,
109  .end_frame = ff_nvdec_simple_end_frame,
110  .decode_slice = ff_nvdec_simple_decode_slice,
111  .frame_params = nvdec_mpeg12_frame_params,
112  .init = ff_nvdec_decode_init,
113  .uninit = ff_nvdec_decode_uninit,
114  .priv_data_size = sizeof(NVDECContext),
115 };
116 #endif
117 
118 #if CONFIG_MPEG1_NVDEC_HWACCEL
120  .p.name = "mpeg1_nvdec",
121  .p.type = AVMEDIA_TYPE_VIDEO,
122  .p.id = AV_CODEC_ID_MPEG1VIDEO,
123  .p.pix_fmt = AV_PIX_FMT_CUDA,
124  .start_frame = nvdec_mpeg12_start_frame,
125  .end_frame = ff_nvdec_simple_end_frame,
126  .decode_slice = ff_nvdec_simple_decode_slice,
127  .frame_params = nvdec_mpeg12_frame_params,
128  .init = ff_nvdec_decode_init,
129  .uninit = ff_nvdec_decode_uninit,
130  .priv_data_size = sizeof(NVDECContext),
131 };
132 #endif
PICT_FRAME
#define PICT_FRAME
Definition: mpegutils.h:38
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:253
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_nvdec_get_ref_idx
int ff_nvdec_get_ref_idx(AVFrame *frame)
Definition: nvdec.c:747
FFHWAccel::p
AVHWAccel p
The public AVHWAccel.
Definition: hwaccel_internal.h:38
FrameDecodeData
This struct stores per-frame lavc-internal data and is attached to it via private_ref.
Definition: decode.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::width
int width
Definition: frame.h:412
internal.h
PICT_BOTTOM_FIELD
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:37
mpegvideo.h
FFHWAccel
Definition: hwaccel_internal.h:34
ff_nvdec_start_frame
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:561
nvdec_mpeg12_frame_params
static int nvdec_mpeg12_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: nvdec_mpeg12.c:95
ff_mpeg1_nvdec_hwaccel
const struct FFHWAccel ff_mpeg1_nvdec_hwaccel
s
#define s(width, name)
Definition: cbs_vp9.c:198
NVDECFrame
Definition: nvdec.h:44
ctx
AVFormatContext * ctx
Definition: movenc.c:48
decode.h
ff_nvdec_simple_end_frame
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:663
hwaccel_internal.h
ff_nvdec_decode_init
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:325
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:476
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:124
NVDECFrame::idx
unsigned int idx
Definition: nvdec.h:45
size
int size
Definition: twinvq_data.h:10344
nvdec.h
ff_nvdec_decode_uninit
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:257
AVFrame::private_ref
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:797
AVHWAccel::name
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2135
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
ff_mpeg2_nvdec_hwaccel
const struct FFHWAccel ff_mpeg2_nvdec_hwaccel
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AVFrame::height
int height
Definition: frame.h:412
ff_nvdec_simple_decode_slice
int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec.c:673
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ff_nvdec_frame_params
int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size, int supports_444)
Definition: nvdec.c:695
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
nvdec_mpeg12_start_frame
static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_mpeg12.c:32
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
FrameDecodeData::hwaccel_priv
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:51
NVDECContext
Definition: nvdec.h:52
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:67