FFmpeg
nvdec_mpeg4.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 Part 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 "internal.h"
25 #include "mpeg4videodec.h"
26 #include "mpeg4videodefs.h"
27 #include "nvdec.h"
28 #include "decode.h"
29 #include "hwaccel_internal.h"
30 
31 static int nvdec_mpeg4_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
32 {
33  Mpeg4DecContext *m = avctx->priv_data;
34  MpegEncContext *s = &m->m;
35 
37  CUVIDPICPARAMS *pp = &ctx->pic_params;
38  CUVIDMPEG4PICPARAMS *ppc = &pp->CodecSpecific.mpeg4;
39  FrameDecodeData *fdd;
40  NVDECFrame *cf;
41  AVFrame *cur_frame = s->cur_pic.ptr->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  .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  s->pict_type == AV_PICTURE_TYPE_S,
61 
62  .CodecSpecific.mpeg4 = {
63  .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_pic.ptr ? s->last_pic.ptr->f : NULL),
64  .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_pic.ptr ? s->next_pic.ptr->f : NULL),
65 
66  .video_object_layer_width = s->width,
67  .video_object_layer_height = s->height,
68  .vop_time_increment_bitcount = m->time_increment_bits,
69  .top_field_first = s->top_field_first,
70  .resync_marker_disable = !m->resync_marker,
71  .quant_type = s->mpeg_quant,
72  .quarter_sample = s->quarter_sample,
73  .short_video_header = avctx->codec->id == AV_CODEC_ID_H263,
74  .divx_flags = s->divx_packed ? 5 : 0,
75 
76  .vop_coding_type = s->pict_type - AV_PICTURE_TYPE_I,
77  .vop_coded = 1,
78  .vop_rounding_type = s->no_rounding,
79  .alternate_vertical_scan_flag = s->alternate_scan,
80  .interlaced = !s->progressive_sequence,
81  .vop_fcode_forward = s->f_code,
82  .vop_fcode_backward = s->b_code,
83  .trd = { s->pp_time, s->pp_field_time >> 1 },
84  .trb = { s->pb_time, s->pb_field_time >> 1 },
85 
86  .gmc_enabled = s->pict_type == AV_PICTURE_TYPE_S &&
88  }
89  };
90 
91  for (i = 0; i < 64; ++i) {
92  int n = s->idsp.idct_permutation[i];
93  ppc->QuantMatrixIntra[i] = s->intra_matrix[n];
94  ppc->QuantMatrixInter[i] = s->inter_matrix[n];
95  }
96 
97  // We need to pass the full frame buffer and not just the slice
99 }
100 
101 static int nvdec_mpeg4_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
102 {
103  return 0;
104 }
105 
107  AVBufferRef *hw_frames_ctx)
108 {
109  // Each frame can at most have one P and one B reference
110  return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2, 0);
111 }
112 
114  .p.name = "mpeg4_nvdec",
115  .p.type = AVMEDIA_TYPE_VIDEO,
116  .p.id = AV_CODEC_ID_MPEG4,
117  .p.pix_fmt = AV_PIX_FMT_CUDA,
118  .start_frame = nvdec_mpeg4_start_frame,
119  .end_frame = ff_nvdec_simple_end_frame,
120  .decode_slice = nvdec_mpeg4_decode_slice,
121  .frame_params = nvdec_mpeg4_frame_params,
122  .init = ff_nvdec_decode_init,
123  .uninit = ff_nvdec_decode_uninit,
124  .priv_data_size = sizeof(NVDECContext),
125 };
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:260
Mpeg4DecContext::vol_sprite_usage
int vol_sprite_usage
Definition: mpeg4videodec.h:40
ff_mpeg4_nvdec_hwaccel
const FFHWAccel ff_mpeg4_nvdec_hwaccel
Definition: nvdec_mpeg4.c:113
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
mpeg4videodec.h
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
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
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:389
AVFrame::width
int width
Definition: frame.h:461
internal.h
nvdec_mpeg4_start_frame
static int nvdec_mpeg4_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_mpeg4.c:31
nvdec_mpeg4_decode_slice
static int nvdec_mpeg4_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: nvdec_mpeg4.c:101
FFHWAccel
Definition: hwaccel_internal.h:34
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:460
ff_nvdec_start_frame
int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: nvdec.c:561
s
#define s(width, name)
Definition: cbs_vp9.c:198
NVDECFrame
Definition: nvdec.h:44
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
ff_nvdec_simple_end_frame
int ff_nvdec_simple_end_frame(AVCodecContext *avctx)
Definition: nvdec.c:663
NULL
#define NULL
Definition: coverity.c:32
GMC_SPRITE
#define GMC_SPRITE
Definition: mpeg4videodefs.h:50
hwaccel_internal.h
ff_nvdec_decode_init
int ff_nvdec_decode_init(AVCodecContext *avctx)
Definition: nvdec.c:327
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
Mpeg4DecContext
Definition: mpeg4videodec.h:34
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:130
NVDECFrame::idx
unsigned int idx
Definition: nvdec.h:45
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: codec_id.h:56
size
int size
Definition: twinvq_data.h:10344
nvdec.h
ff_nvdec_decode_uninit
int ff_nvdec_decode_uninit(AVCodecContext *avctx)
Definition: nvdec.c:259
AVFrame::private_ref
AVBufferRef * private_ref
AVBufferRef for internal use by a single libav* library.
Definition: frame.h:785
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
AVHWAccel::name
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2105
Mpeg4DecContext::time_increment_bits
int time_increment_bits
number of bits to represent the fractional part of time
Definition: mpeg4videodec.h:38
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avcodec.h
mpeg4videodefs.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AVFrame::height
int height
Definition: frame.h:461
Mpeg4DecContext::m
MpegEncContext m
Definition: mpeg4videodec.h:35
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
Mpeg4DecContext::resync_marker
int resync_marker
could this stream contain resync markers
Definition: mpeg4videodec.h:55
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
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
FrameDecodeData::hwaccel_priv
void * hwaccel_priv
Per-frame private data for hwaccels.
Definition: decode.h:51
nvdec_mpeg4_frame_params
static int nvdec_mpeg4_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: nvdec_mpeg4.c:106
NVDECContext
Definition: nvdec.h:52
AV_PICTURE_TYPE_S
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:282
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:73