Merge commit 'a0f2946068c62e18cb05ac25c0df3d86077251a6'
[ffmpeg.git] / libavcodec / vdpau_h264.c
1 /*
2  * MPEG-4 Part 10 / AVC / H.264 HW decode acceleration through VDPAU
3  *
4  * Copyright (c) 2008 NVIDIA
5  * Copyright (c) 2013 RĂ©mi Denis-Courmont
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <vdpau/vdpau.h>
25
26 #include "avcodec.h"
27 #include "internal.h"
28 #include "h264.h"
29 #include "mpegutils.h"
30 #include "vdpau.h"
31 #include "vdpau_internal.h"
32
33 static int32_t h264_foc(int foc)
34 {
35     if (foc == INT_MAX)
36         foc = 0;
37     return foc;
38 }
39
40 static void vdpau_h264_clear_rf(VdpReferenceFrameH264 *rf)
41 {
42     rf->surface             = VDP_INVALID_HANDLE;
43     rf->is_long_term        = VDP_FALSE;
44     rf->top_is_reference    = VDP_FALSE;
45     rf->bottom_is_reference = VDP_FALSE;
46     rf->field_order_cnt[0]  = 0;
47     rf->field_order_cnt[1]  = 0;
48     rf->frame_idx           = 0;
49 }
50
51 static void vdpau_h264_set_rf(VdpReferenceFrameH264 *rf, H264Picture *pic,
52                               int pic_structure)
53 {
54     VdpVideoSurface surface = ff_vdpau_get_surface_id(pic->f);
55
56     if (pic_structure == 0)
57         pic_structure = pic->reference;
58
59     rf->surface             = surface;
60     rf->is_long_term        = pic->reference && pic->long_ref;
61     rf->top_is_reference    = (pic_structure & PICT_TOP_FIELD)    != 0;
62     rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0;
63     rf->field_order_cnt[0]  = h264_foc(pic->field_poc[0]);
64     rf->field_order_cnt[1]  = h264_foc(pic->field_poc[1]);
65     rf->frame_idx           = pic->long_ref ? pic->pic_id : pic->frame_num;
66 }
67
68 static void vdpau_h264_set_reference_frames(AVCodecContext *avctx)
69 {
70     H264Context * const h = avctx->priv_data;
71     struct vdpau_picture_context *pic_ctx = h->cur_pic_ptr->hwaccel_picture_private;
72     VdpPictureInfoH264 *info = &pic_ctx->info.h264;
73     int list;
74
75     VdpReferenceFrameH264 *rf = &info->referenceFrames[0];
76 #define H264_RF_COUNT FF_ARRAY_ELEMS(info->referenceFrames)
77
78     for (list = 0; list < 2; ++list) {
79         H264Picture **lp = list ? h->long_ref : h->short_ref;
80         int i, ls    = list ? 16          : h->short_ref_count;
81
82         for (i = 0; i < ls; ++i) {
83             H264Picture *pic = lp[i];
84             VdpReferenceFrameH264 *rf2;
85             VdpVideoSurface surface_ref;
86             int pic_frame_idx;
87
88             if (!pic || !pic->reference)
89                 continue;
90             pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
91             surface_ref = ff_vdpau_get_surface_id(pic->f);
92
93             rf2 = &info->referenceFrames[0];
94             while (rf2 != rf) {
95                 if ((rf2->surface      == surface_ref)   &&
96                     (rf2->is_long_term == pic->long_ref) &&
97                     (rf2->frame_idx    == pic_frame_idx))
98                     break;
99                 ++rf2;
100             }
101             if (rf2 != rf) {
102                 rf2->top_is_reference    |= (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
103                 rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
104                 continue;
105             }
106
107             if (rf >= &info->referenceFrames[H264_RF_COUNT])
108                 continue;
109
110             vdpau_h264_set_rf(rf, pic, pic->reference);
111             ++rf;
112         }
113     }
114
115     for (; rf < &info->referenceFrames[H264_RF_COUNT]; ++rf)
116         vdpau_h264_clear_rf(rf);
117 }
118
119 static int vdpau_h264_start_frame(AVCodecContext *avctx,
120                                   const uint8_t *buffer, uint32_t size)
121 {
122     H264Context * const h = avctx->priv_data;
123     H264Picture *pic = h->cur_pic_ptr;
124     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
125     VdpPictureInfoH264 *info = &pic_ctx->info.h264;
126 #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
127     VdpPictureInfoH264Predictive *info2 = &pic_ctx->info.h264_predictive;
128 #endif
129
130     /* init VdpPictureInfoH264 */
131     info->slice_count                            = 0;
132     info->field_order_cnt[0]                     = h264_foc(pic->field_poc[0]);
133     info->field_order_cnt[1]                     = h264_foc(pic->field_poc[1]);
134     info->is_reference                           = h->nal_ref_idc != 0;
135     info->frame_num                              = h->frame_num;
136     info->field_pic_flag                         = h->picture_structure != PICT_FRAME;
137     info->bottom_field_flag                      = h->picture_structure == PICT_BOTTOM_FIELD;
138     info->num_ref_frames                         = h->sps.ref_frame_count;
139     info->mb_adaptive_frame_field_flag           = h->sps.mb_aff && !info->field_pic_flag;
140     info->constrained_intra_pred_flag            = h->pps.constrained_intra_pred;
141     info->weighted_pred_flag                     = h->pps.weighted_pred;
142     info->weighted_bipred_idc                    = h->pps.weighted_bipred_idc;
143     info->frame_mbs_only_flag                    = h->sps.frame_mbs_only_flag;
144     info->transform_8x8_mode_flag                = h->pps.transform_8x8_mode;
145     info->chroma_qp_index_offset                 = h->pps.chroma_qp_index_offset[0];
146     info->second_chroma_qp_index_offset          = h->pps.chroma_qp_index_offset[1];
147     info->pic_init_qp_minus26                    = h->pps.init_qp - 26;
148     info->num_ref_idx_l0_active_minus1           = h->pps.ref_count[0] - 1;
149     info->num_ref_idx_l1_active_minus1           = h->pps.ref_count[1] - 1;
150     info->log2_max_frame_num_minus4              = h->sps.log2_max_frame_num - 4;
151     info->pic_order_cnt_type                     = h->sps.poc_type;
152     info->log2_max_pic_order_cnt_lsb_minus4      = h->sps.poc_type ? 0 : h->sps.log2_max_poc_lsb - 4;
153     info->delta_pic_order_always_zero_flag       = h->sps.delta_pic_order_always_zero_flag;
154     info->direct_8x8_inference_flag              = h->sps.direct_8x8_inference_flag;
155 #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
156     info2->qpprime_y_zero_transform_bypass_flag  = h->sps.transform_bypass;
157     info2->separate_colour_plane_flag            = h->sps.residual_color_transform_flag;
158 #endif
159     info->entropy_coding_mode_flag               = h->pps.cabac;
160     info->pic_order_present_flag                 = h->pps.pic_order_present;
161     info->deblocking_filter_control_present_flag = h->pps.deblocking_filter_parameters_present;
162     info->redundant_pic_cnt_present_flag         = h->pps.redundant_pic_cnt_present;
163
164     memcpy(info->scaling_lists_4x4, h->pps.scaling_matrix4,
165            sizeof(info->scaling_lists_4x4));
166     memcpy(info->scaling_lists_8x8[0], h->pps.scaling_matrix8[0],
167            sizeof(info->scaling_lists_8x8[0]));
168     memcpy(info->scaling_lists_8x8[1], h->pps.scaling_matrix8[3],
169            sizeof(info->scaling_lists_8x8[1]));
170
171     vdpau_h264_set_reference_frames(avctx);
172
173     return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
174 }
175
176 static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
177
178 static int vdpau_h264_decode_slice(AVCodecContext *avctx,
179                                    const uint8_t *buffer, uint32_t size)
180 {
181     H264Context *h = avctx->priv_data;
182     H264Picture *pic = h->cur_pic_ptr;
183     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
184     int val;
185
186     val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
187     if (val)
188         return val;
189
190     val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
191     if (val)
192         return val;
193
194     pic_ctx->info.h264.slice_count++;
195     return 0;
196 }
197
198 static int vdpau_h264_end_frame(AVCodecContext *avctx)
199 {
200     H264Context *h = avctx->priv_data;
201     H264SliceContext *sl = &h->slice_ctx[0];
202     H264Picture *pic = h->cur_pic_ptr;
203     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
204     int val;
205
206     val = ff_vdpau_common_end_frame(avctx, pic->f, pic_ctx);
207     if (val < 0)
208         return val;
209
210     ff_h264_draw_horiz_band(h, sl, 0, h->avctx->height);
211     return 0;
212 }
213
214 static int vdpau_h264_init(AVCodecContext *avctx)
215 {
216     VdpDecoderProfile profile;
217     uint32_t level = avctx->level;
218
219     switch (avctx->profile & ~FF_PROFILE_H264_INTRA) {
220     case FF_PROFILE_H264_BASELINE:
221         profile = VDP_DECODER_PROFILE_H264_BASELINE;
222         break;
223     case FF_PROFILE_H264_CONSTRAINED_BASELINE:
224 #ifdef VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE
225         profile = VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE;
226         break;
227 #endif
228     case FF_PROFILE_H264_MAIN:
229         profile = VDP_DECODER_PROFILE_H264_MAIN;
230         break;
231     case FF_PROFILE_H264_HIGH:
232         profile = VDP_DECODER_PROFILE_H264_HIGH;
233         break;
234 #ifdef VDP_DECODER_PROFILE_H264_EXTENDED
235     case FF_PROFILE_H264_EXTENDED:
236         profile = VDP_DECODER_PROFILE_H264_EXTENDED;
237         break;
238 #endif
239     case FF_PROFILE_H264_HIGH_10:
240         /* XXX: High 10 can be treated as High so long as only 8-bits per
241          * formats are supported. */
242         profile = VDP_DECODER_PROFILE_H264_HIGH;
243         break;
244 #ifdef VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE
245     case FF_PROFILE_H264_HIGH_422:
246     case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
247     case FF_PROFILE_H264_CAVLC_444:
248         profile = VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE;
249         break;
250 #endif
251     default:
252         return AVERROR(ENOTSUP);
253     }
254
255     if ((avctx->profile & FF_PROFILE_H264_INTRA) && avctx->level == 11)
256         level = VDP_DECODER_LEVEL_H264_1b;
257
258     return ff_vdpau_common_init(avctx, profile, level);
259 }
260
261 AVHWAccel ff_h264_vdpau_hwaccel = {
262     .name           = "h264_vdpau",
263     .type           = AVMEDIA_TYPE_VIDEO,
264     .id             = AV_CODEC_ID_H264,
265     .pix_fmt        = AV_PIX_FMT_VDPAU,
266     .start_frame    = vdpau_h264_start_frame,
267     .end_frame      = vdpau_h264_end_frame,
268     .decode_slice   = vdpau_h264_decode_slice,
269     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
270     .init           = vdpau_h264_init,
271     .uninit         = ff_vdpau_common_uninit,
272     .priv_data_size = sizeof(VDPAUContext),
273 };