1 /*
2 * HEVC video Decoder
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Mickael Raulet
6 * Copyright (C) 2012 - 2013 Gildas Cocherel
7 * Copyright (C) 2012 - 2013 Wassim Hamidouche
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/attributes.h"
29 #include "libavutil/common.h"
30 #include "libavutil/display.h"
31 #include "libavutil/film_grain_params.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mastering_display_metadata.h"
34 #include "libavutil/md5.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/stereo3d.h"
38 #include "libavutil/timecode.h"
39
40 #include "bswapdsp.h"
41 #include "bytestream.h"
42 #include "cabac_functions.h"
43 #include "codec_internal.h"
44 #include "golomb.h"
45 #include "hevc.h"
46 #include "hevc_data.h"
47 #include "hevc_parse.h"
48 #include "hevcdec.h"
49 #include "hwconfig.h"
50 #include "internal.h"
51 #include "profiles.h"
52 #include "thread.h"
53 #include "threadframe.h"
54
55 static const uint8_t hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
56
57 /**
58 * NOTE: Each function hls_foo correspond to the function foo in the
59 * specification (HLS stands for High Level Syntax).
60 */
61
62 /**
63 * Section 5.7
64 */
65
66 /* free everything allocated by pic_arrays_init() */
pic_arrays_free(HEVCContext * s)67 static void pic_arrays_free(HEVCContext *s)
68 {
69 av_freep(&s->sao);
70 av_freep(&s->deblock);
71
72 av_freep(&s->skip_flag);
73 av_freep(&s->tab_ct_depth);
74
75 av_freep(&s->tab_ipm);
76 av_freep(&s->cbf_luma);
77 av_freep(&s->is_pcm);
78
79 av_freep(&s->qp_y_tab);
80 av_freep(&s->tab_slice_address);
81 av_freep(&s->filter_slice_edges);
82
83 av_freep(&s->horizontal_bs);
84 av_freep(&s->vertical_bs);
85
86 av_freep(&s->sh.entry_point_offset);
87 av_freep(&s->sh.size);
88 av_freep(&s->sh.offset);
89
90 av_buffer_pool_uninit(&s->tab_mvf_pool);
91 av_buffer_pool_uninit(&s->rpl_tab_pool);
92 }
93
94 /* allocate arrays that depend on frame dimensions */
pic_arrays_init(HEVCContext * s,const HEVCSPS * sps)95 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
96 {
97 int log2_min_cb_size = sps->log2_min_cb_size;
98 int width = sps->width;
99 int height = sps->height;
100 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
101 ((height >> log2_min_cb_size) + 1);
102 int ctb_count = sps->ctb_width * sps->ctb_height;
103 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
104
105 s->bs_width = (width >> 2) + 1;
106 s->bs_height = (height >> 2) + 1;
107
108 s->sao = av_calloc(ctb_count, sizeof(*s->sao));
109 s->deblock = av_calloc(ctb_count, sizeof(*s->deblock));
110 if (!s->sao || !s->deblock)
111 goto fail;
112
113 s->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
114 s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
115 if (!s->skip_flag || !s->tab_ct_depth)
116 goto fail;
117
118 s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
119 s->tab_ipm = av_mallocz(min_pu_size);
120 s->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
121 if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
122 goto fail;
123
124 s->filter_slice_edges = av_mallocz(ctb_count);
125 s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
126 sizeof(*s->tab_slice_address));
127 s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
128 sizeof(*s->qp_y_tab));
129 if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
130 goto fail;
131
132 s->horizontal_bs = av_calloc(s->bs_width, s->bs_height);
133 s->vertical_bs = av_calloc(s->bs_width, s->bs_height);
134 if (!s->horizontal_bs || !s->vertical_bs)
135 goto fail;
136
137 s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
138 av_buffer_allocz);
139 s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
140 av_buffer_allocz);
141 if (!s->tab_mvf_pool || !s->rpl_tab_pool)
142 goto fail;
143
144 return 0;
145
146 fail:
147 pic_arrays_free(s);
148 return AVERROR(ENOMEM);
149 }
150
pred_weight_table(HEVCContext * s,GetBitContext * gb)151 static int pred_weight_table(HEVCContext *s, GetBitContext *gb)
152 {
153 int i = 0;
154 int j = 0;
155 uint8_t luma_weight_l0_flag[16];
156 uint8_t chroma_weight_l0_flag[16];
157 uint8_t luma_weight_l1_flag[16];
158 uint8_t chroma_weight_l1_flag[16];
159 int luma_log2_weight_denom;
160
161 luma_log2_weight_denom = get_ue_golomb_long(gb);
162 if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
163 av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
164 return AVERROR_INVALIDDATA;
165 }
166 s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
167 if (s->ps.sps->chroma_format_idc != 0) {
168 int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
169 if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
170 av_log(s->avctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
171 return AVERROR_INVALIDDATA;
172 }
173 s->sh.chroma_log2_weight_denom = chroma_log2_weight_denom;
174 }
175
176 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
177 luma_weight_l0_flag[i] = get_bits1(gb);
178 if (!luma_weight_l0_flag[i]) {
179 s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
180 s->sh.luma_offset_l0[i] = 0;
181 }
182 }
183 if (s->ps.sps->chroma_format_idc != 0) {
184 for (i = 0; i < s->sh.nb_refs[L0]; i++)
185 chroma_weight_l0_flag[i] = get_bits1(gb);
186 } else {
187 for (i = 0; i < s->sh.nb_refs[L0]; i++)
188 chroma_weight_l0_flag[i] = 0;
189 }
190 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
191 if (luma_weight_l0_flag[i]) {
192 int delta_luma_weight_l0 = get_se_golomb(gb);
193 if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
194 return AVERROR_INVALIDDATA;
195 s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
196 s->sh.luma_offset_l0[i] = get_se_golomb(gb);
197 }
198 if (chroma_weight_l0_flag[i]) {
199 for (j = 0; j < 2; j++) {
200 int delta_chroma_weight_l0 = get_se_golomb(gb);
201 int delta_chroma_offset_l0 = get_se_golomb(gb);
202
203 if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
204 || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
205 return AVERROR_INVALIDDATA;
206 }
207
208 s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
209 s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
210 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
211 }
212 } else {
213 s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
214 s->sh.chroma_offset_l0[i][0] = 0;
215 s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
216 s->sh.chroma_offset_l0[i][1] = 0;
217 }
218 }
219 if (s->sh.slice_type == HEVC_SLICE_B) {
220 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
221 luma_weight_l1_flag[i] = get_bits1(gb);
222 if (!luma_weight_l1_flag[i]) {
223 s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
224 s->sh.luma_offset_l1[i] = 0;
225 }
226 }
227 if (s->ps.sps->chroma_format_idc != 0) {
228 for (i = 0; i < s->sh.nb_refs[L1]; i++)
229 chroma_weight_l1_flag[i] = get_bits1(gb);
230 } else {
231 for (i = 0; i < s->sh.nb_refs[L1]; i++)
232 chroma_weight_l1_flag[i] = 0;
233 }
234 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
235 if (luma_weight_l1_flag[i]) {
236 int delta_luma_weight_l1 = get_se_golomb(gb);
237 if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
238 return AVERROR_INVALIDDATA;
239 s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
240 s->sh.luma_offset_l1[i] = get_se_golomb(gb);
241 }
242 if (chroma_weight_l1_flag[i]) {
243 for (j = 0; j < 2; j++) {
244 int delta_chroma_weight_l1 = get_se_golomb(gb);
245 int delta_chroma_offset_l1 = get_se_golomb(gb);
246
247 if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
248 || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
249 return AVERROR_INVALIDDATA;
250 }
251
252 s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
253 s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
254 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
255 }
256 } else {
257 s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
258 s->sh.chroma_offset_l1[i][0] = 0;
259 s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
260 s->sh.chroma_offset_l1[i][1] = 0;
261 }
262 }
263 }
264 return 0;
265 }
266
decode_lt_rps(HEVCContext * s,LongTermRPS * rps,GetBitContext * gb)267 static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
268 {
269 const HEVCSPS *sps = s->ps.sps;
270 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
271 int prev_delta_msb = 0;
272 unsigned int nb_sps = 0, nb_sh;
273 int i;
274
275 rps->nb_refs = 0;
276 if (!sps->long_term_ref_pics_present_flag)
277 return 0;
278
279 if (sps->num_long_term_ref_pics_sps > 0)
280 nb_sps = get_ue_golomb_long(gb);
281 nb_sh = get_ue_golomb_long(gb);
282
283 if (nb_sps > sps->num_long_term_ref_pics_sps)
284 return AVERROR_INVALIDDATA;
285 if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
286 return AVERROR_INVALIDDATA;
287
288 rps->nb_refs = nb_sh + nb_sps;
289
290 for (i = 0; i < rps->nb_refs; i++) {
291
292 if (i < nb_sps) {
293 uint8_t lt_idx_sps = 0;
294
295 if (sps->num_long_term_ref_pics_sps > 1)
296 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
297
298 rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
299 rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
300 } else {
301 rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
302 rps->used[i] = get_bits1(gb);
303 }
304
305 rps->poc_msb_present[i] = get_bits1(gb);
306 if (rps->poc_msb_present[i]) {
307 int64_t delta = get_ue_golomb_long(gb);
308 int64_t poc;
309
310 if (i && i != nb_sps)
311 delta += prev_delta_msb;
312
313 poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
314 if (poc != (int32_t)poc)
315 return AVERROR_INVALIDDATA;
316 rps->poc[i] = poc;
317 prev_delta_msb = delta;
318 }
319 }
320
321 return 0;
322 }
323
export_stream_params(HEVCContext * s,const HEVCSPS * sps)324 static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
325 {
326 AVCodecContext *avctx = s->avctx;
327 const HEVCParamSets *ps = &s->ps;
328 const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
329 const HEVCWindow *ow = &sps->output_window;
330 unsigned int num = 0, den = 0;
331
332 avctx->pix_fmt = sps->pix_fmt;
333 avctx->coded_width = sps->width;
334 avctx->coded_height = sps->height;
335 avctx->width = sps->width - ow->left_offset - ow->right_offset;
336 avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
337 avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
338 avctx->profile = sps->ptl.general_ptl.profile_idc;
339 avctx->level = sps->ptl.general_ptl.level_idc;
340
341 ff_set_sar(avctx, sps->vui.sar);
342
343 if (sps->vui.video_signal_type_present_flag)
344 avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
345 : AVCOL_RANGE_MPEG;
346 else
347 avctx->color_range = AVCOL_RANGE_MPEG;
348
349 if (sps->vui.colour_description_present_flag) {
350 avctx->color_primaries = sps->vui.colour_primaries;
351 avctx->color_trc = sps->vui.transfer_characteristic;
352 avctx->colorspace = sps->vui.matrix_coeffs;
353 } else {
354 avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
355 avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
356 avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
357 }
358
359 avctx->chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
360 if (sps->chroma_format_idc == 1) {
361 if (sps->vui.chroma_loc_info_present_flag) {
362 if (sps->vui.chroma_sample_loc_type_top_field <= 5)
363 avctx->chroma_sample_location = sps->vui.chroma_sample_loc_type_top_field + 1;
364 } else
365 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
366 }
367
368 if (vps->vps_timing_info_present_flag) {
369 num = vps->vps_num_units_in_tick;
370 den = vps->vps_time_scale;
371 } else if (sps->vui.vui_timing_info_present_flag) {
372 num = sps->vui.vui_num_units_in_tick;
373 den = sps->vui.vui_time_scale;
374 }
375
376 if (num != 0 && den != 0)
377 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
378 num, den, 1 << 30);
379 }
380
export_stream_params_from_sei(HEVCContext * s)381 static int export_stream_params_from_sei(HEVCContext *s)
382 {
383 AVCodecContext *avctx = s->avctx;
384
385 if (s->sei.a53_caption.buf_ref)
386 s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
387
388 if (s->sei.alternative_transfer.present &&
389 av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
390 s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
391 avctx->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
392 }
393
394 if (s->sei.film_grain_characteristics.present)
395 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
396
397 return 0;
398 }
399
get_format(HEVCContext * s,const HEVCSPS * sps)400 static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
401 {
402 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
403 CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
404 CONFIG_HEVC_NVDEC_HWACCEL + \
405 CONFIG_HEVC_VAAPI_HWACCEL + \
406 CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
407 CONFIG_HEVC_VDPAU_HWACCEL)
408 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
409
410 switch (sps->pix_fmt) {
411 case AV_PIX_FMT_YUV420P:
412 case AV_PIX_FMT_YUVJ420P:
413 #if CONFIG_HEVC_DXVA2_HWACCEL
414 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
415 #endif
416 #if CONFIG_HEVC_D3D11VA_HWACCEL
417 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
418 *fmt++ = AV_PIX_FMT_D3D11;
419 #endif
420 #if CONFIG_HEVC_VAAPI_HWACCEL
421 *fmt++ = AV_PIX_FMT_VAAPI;
422 #endif
423 #if CONFIG_HEVC_VDPAU_HWACCEL
424 *fmt++ = AV_PIX_FMT_VDPAU;
425 #endif
426 #if CONFIG_HEVC_NVDEC_HWACCEL
427 *fmt++ = AV_PIX_FMT_CUDA;
428 #endif
429 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
430 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
431 #endif
432 break;
433 case AV_PIX_FMT_YUV420P10:
434 #if CONFIG_HEVC_DXVA2_HWACCEL
435 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
436 #endif
437 #if CONFIG_HEVC_D3D11VA_HWACCEL
438 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
439 *fmt++ = AV_PIX_FMT_D3D11;
440 #endif
441 #if CONFIG_HEVC_VAAPI_HWACCEL
442 *fmt++ = AV_PIX_FMT_VAAPI;
443 #endif
444 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
445 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
446 #endif
447 #if CONFIG_HEVC_VDPAU_HWACCEL
448 *fmt++ = AV_PIX_FMT_VDPAU;
449 #endif
450 #if CONFIG_HEVC_NVDEC_HWACCEL
451 *fmt++ = AV_PIX_FMT_CUDA;
452 #endif
453 break;
454 case AV_PIX_FMT_YUV444P:
455 #if CONFIG_HEVC_VDPAU_HWACCEL
456 *fmt++ = AV_PIX_FMT_VDPAU;
457 #endif
458 #if CONFIG_HEVC_NVDEC_HWACCEL
459 *fmt++ = AV_PIX_FMT_CUDA;
460 #endif
461 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
462 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
463 #endif
464 break;
465 case AV_PIX_FMT_YUV422P:
466 case AV_PIX_FMT_YUV422P10LE:
467 #if CONFIG_HEVC_VAAPI_HWACCEL
468 *fmt++ = AV_PIX_FMT_VAAPI;
469 #endif
470 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
471 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
472 #endif
473 break;
474 case AV_PIX_FMT_YUV444P10:
475 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
476 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
477 #endif
478 case AV_PIX_FMT_YUV420P12:
479 case AV_PIX_FMT_YUV444P12:
480 #if CONFIG_HEVC_VDPAU_HWACCEL
481 *fmt++ = AV_PIX_FMT_VDPAU;
482 #endif
483 #if CONFIG_HEVC_NVDEC_HWACCEL
484 *fmt++ = AV_PIX_FMT_CUDA;
485 #endif
486 break;
487 }
488
489 *fmt++ = sps->pix_fmt;
490 *fmt = AV_PIX_FMT_NONE;
491
492 return ff_thread_get_format(s->avctx, pix_fmts);
493 }
494
set_sps(HEVCContext * s,const HEVCSPS * sps,enum AVPixelFormat pix_fmt)495 static int set_sps(HEVCContext *s, const HEVCSPS *sps,
496 enum AVPixelFormat pix_fmt)
497 {
498 int ret, i;
499
500 pic_arrays_free(s);
501 s->ps.sps = NULL;
502 s->ps.vps = NULL;
503
504 if (!sps)
505 return 0;
506
507 ret = pic_arrays_init(s, sps);
508 if (ret < 0)
509 goto fail;
510
511 export_stream_params(s, sps);
512
513 s->avctx->pix_fmt = pix_fmt;
514
515 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
516 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
517 ff_videodsp_init (&s->vdsp, sps->bit_depth);
518
519 for (i = 0; i < 3; i++) {
520 av_freep(&s->sao_pixel_buffer_h[i]);
521 av_freep(&s->sao_pixel_buffer_v[i]);
522 }
523
524 if (sps->sao_enabled && !s->avctx->hwaccel) {
525 int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
526 int c_idx;
527
528 for(c_idx = 0; c_idx < c_count; c_idx++) {
529 int w = sps->width >> sps->hshift[c_idx];
530 int h = sps->height >> sps->vshift[c_idx];
531 s->sao_pixel_buffer_h[c_idx] =
532 av_malloc((w * 2 * sps->ctb_height) <<
533 sps->pixel_shift);
534 s->sao_pixel_buffer_v[c_idx] =
535 av_malloc((h * 2 * sps->ctb_width) <<
536 sps->pixel_shift);
537 if (!s->sao_pixel_buffer_h[c_idx] ||
538 !s->sao_pixel_buffer_v[c_idx])
539 goto fail;
540 }
541 }
542
543 s->ps.sps = sps;
544 s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
545
546 return 0;
547
548 fail:
549 pic_arrays_free(s);
550 for (i = 0; i < 3; i++) {
551 av_freep(&s->sao_pixel_buffer_h[i]);
552 av_freep(&s->sao_pixel_buffer_v[i]);
553 }
554 s->ps.sps = NULL;
555 return ret;
556 }
557
hls_slice_header(HEVCContext * s)558 static int hls_slice_header(HEVCContext *s)
559 {
560 GetBitContext *gb = &s->HEVClc->gb;
561 SliceHeader *sh = &s->sh;
562 int i, ret;
563
564 // Coded parameters
565 sh->first_slice_in_pic_flag = get_bits1(gb);
566 if (s->ref && sh->first_slice_in_pic_flag) {
567 av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
568 return 1; // This slice will be skipped later, do not corrupt state
569 }
570
571 if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
572 s->seq_decode = (s->seq_decode + 1) & 0xff;
573 s->max_ra = INT_MAX;
574 if (IS_IDR(s))
575 ff_hevc_clear_refs(s);
576 }
577 sh->no_output_of_prior_pics_flag = 0;
578 if (IS_IRAP(s))
579 sh->no_output_of_prior_pics_flag = get_bits1(gb);
580
581 sh->pps_id = get_ue_golomb_long(gb);
582 if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
583 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
584 return AVERROR_INVALIDDATA;
585 }
586 if (!sh->first_slice_in_pic_flag &&
587 s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
588 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
589 return AVERROR_INVALIDDATA;
590 }
591 s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
592 if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
593 sh->no_output_of_prior_pics_flag = 1;
594
595 if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
596 const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
597 const HEVCSPS *last_sps = s->ps.sps;
598 enum AVPixelFormat pix_fmt;
599
600 if (last_sps && IS_IRAP(s) && s->nal_unit_type != HEVC_NAL_CRA_NUT) {
601 if (sps->width != last_sps->width || sps->height != last_sps->height ||
602 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering !=
603 last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
604 sh->no_output_of_prior_pics_flag = 0;
605 }
606 ff_hevc_clear_refs(s);
607
608 ret = set_sps(s, sps, sps->pix_fmt);
609 if (ret < 0)
610 return ret;
611
612 pix_fmt = get_format(s, sps);
613 if (pix_fmt < 0)
614 return pix_fmt;
615 s->avctx->pix_fmt = pix_fmt;
616
617 s->seq_decode = (s->seq_decode + 1) & 0xff;
618 s->max_ra = INT_MAX;
619 }
620
621 ret = export_stream_params_from_sei(s);
622 if (ret < 0)
623 return ret;
624
625 sh->dependent_slice_segment_flag = 0;
626 if (!sh->first_slice_in_pic_flag) {
627 int slice_address_length;
628
629 if (s->ps.pps->dependent_slice_segments_enabled_flag)
630 sh->dependent_slice_segment_flag = get_bits1(gb);
631
632 slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
633 s->ps.sps->ctb_height);
634 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
635 if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
636 av_log(s->avctx, AV_LOG_ERROR,
637 "Invalid slice segment address: %u.\n",
638 sh->slice_segment_addr);
639 return AVERROR_INVALIDDATA;
640 }
641
642 if (!sh->dependent_slice_segment_flag) {
643 sh->slice_addr = sh->slice_segment_addr;
644 s->slice_idx++;
645 }
646 } else {
647 sh->slice_segment_addr = sh->slice_addr = 0;
648 s->slice_idx = 0;
649 s->slice_initialized = 0;
650 }
651
652 if (!sh->dependent_slice_segment_flag) {
653 s->slice_initialized = 0;
654
655 for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
656 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
657
658 sh->slice_type = get_ue_golomb_long(gb);
659 if (!(sh->slice_type == HEVC_SLICE_I ||
660 sh->slice_type == HEVC_SLICE_P ||
661 sh->slice_type == HEVC_SLICE_B)) {
662 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
663 sh->slice_type);
664 return AVERROR_INVALIDDATA;
665 }
666 if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
667 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
668 return AVERROR_INVALIDDATA;
669 }
670
671 // when flag is not present, picture is inferred to be output
672 sh->pic_output_flag = 1;
673 if (s->ps.pps->output_flag_present_flag)
674 sh->pic_output_flag = get_bits1(gb);
675
676 if (s->ps.sps->separate_colour_plane_flag)
677 sh->colour_plane_id = get_bits(gb, 2);
678
679 if (!IS_IDR(s)) {
680 int poc, pos;
681
682 sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
683 poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
684 if (!sh->first_slice_in_pic_flag && poc != s->poc) {
685 av_log(s->avctx, AV_LOG_WARNING,
686 "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
687 if (s->avctx->err_recognition & AV_EF_EXPLODE)
688 return AVERROR_INVALIDDATA;
689 poc = s->poc;
690 }
691 s->poc = poc;
692
693 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
694 pos = get_bits_left(gb);
695 if (!sh->short_term_ref_pic_set_sps_flag) {
696 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
697 if (ret < 0)
698 return ret;
699
700 sh->short_term_rps = &sh->slice_rps;
701 } else {
702 int numbits, rps_idx;
703
704 if (!s->ps.sps->nb_st_rps) {
705 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
706 return AVERROR_INVALIDDATA;
707 }
708
709 numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
710 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
711 sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
712 }
713 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
714
715 pos = get_bits_left(gb);
716 ret = decode_lt_rps(s, &sh->long_term_rps, gb);
717 if (ret < 0) {
718 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
719 if (s->avctx->err_recognition & AV_EF_EXPLODE)
720 return AVERROR_INVALIDDATA;
721 }
722 sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
723
724 if (s->ps.sps->sps_temporal_mvp_enabled_flag)
725 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
726 else
727 sh->slice_temporal_mvp_enabled_flag = 0;
728 } else {
729 s->sh.short_term_rps = NULL;
730 s->poc = 0;
731 }
732
733 /* 8.3.1 */
734 if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
735 s->nal_unit_type != HEVC_NAL_TRAIL_N &&
736 s->nal_unit_type != HEVC_NAL_TSA_N &&
737 s->nal_unit_type != HEVC_NAL_STSA_N &&
738 s->nal_unit_type != HEVC_NAL_RADL_N &&
739 s->nal_unit_type != HEVC_NAL_RADL_R &&
740 s->nal_unit_type != HEVC_NAL_RASL_N &&
741 s->nal_unit_type != HEVC_NAL_RASL_R)
742 s->pocTid0 = s->poc;
743
744 if (s->ps.sps->sao_enabled) {
745 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
746 if (s->ps.sps->chroma_format_idc) {
747 sh->slice_sample_adaptive_offset_flag[1] =
748 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
749 }
750 } else {
751 sh->slice_sample_adaptive_offset_flag[0] = 0;
752 sh->slice_sample_adaptive_offset_flag[1] = 0;
753 sh->slice_sample_adaptive_offset_flag[2] = 0;
754 }
755
756 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
757 if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
758 int nb_refs;
759
760 sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
761 if (sh->slice_type == HEVC_SLICE_B)
762 sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
763
764 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
765 sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
766 if (sh->slice_type == HEVC_SLICE_B)
767 sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
768 }
769 if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
770 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
771 sh->nb_refs[L0], sh->nb_refs[L1]);
772 return AVERROR_INVALIDDATA;
773 }
774
775 sh->rpl_modification_flag[0] = 0;
776 sh->rpl_modification_flag[1] = 0;
777 nb_refs = ff_hevc_frame_nb_refs(s);
778 if (!nb_refs) {
779 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
780 return AVERROR_INVALIDDATA;
781 }
782
783 if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
784 sh->rpl_modification_flag[0] = get_bits1(gb);
785 if (sh->rpl_modification_flag[0]) {
786 for (i = 0; i < sh->nb_refs[L0]; i++)
787 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
788 }
789
790 if (sh->slice_type == HEVC_SLICE_B) {
791 sh->rpl_modification_flag[1] = get_bits1(gb);
792 if (sh->rpl_modification_flag[1] == 1)
793 for (i = 0; i < sh->nb_refs[L1]; i++)
794 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
795 }
796 }
797
798 if (sh->slice_type == HEVC_SLICE_B)
799 sh->mvd_l1_zero_flag = get_bits1(gb);
800
801 if (s->ps.pps->cabac_init_present_flag)
802 sh->cabac_init_flag = get_bits1(gb);
803 else
804 sh->cabac_init_flag = 0;
805
806 sh->collocated_ref_idx = 0;
807 if (sh->slice_temporal_mvp_enabled_flag) {
808 sh->collocated_list = L0;
809 if (sh->slice_type == HEVC_SLICE_B)
810 sh->collocated_list = !get_bits1(gb);
811
812 if (sh->nb_refs[sh->collocated_list] > 1) {
813 sh->collocated_ref_idx = get_ue_golomb_long(gb);
814 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
815 av_log(s->avctx, AV_LOG_ERROR,
816 "Invalid collocated_ref_idx: %d.\n",
817 sh->collocated_ref_idx);
818 return AVERROR_INVALIDDATA;
819 }
820 }
821 }
822
823 if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
824 (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
825 int ret = pred_weight_table(s, gb);
826 if (ret < 0)
827 return ret;
828 }
829
830 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
831 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
832 av_log(s->avctx, AV_LOG_ERROR,
833 "Invalid number of merging MVP candidates: %d.\n",
834 sh->max_num_merge_cand);
835 return AVERROR_INVALIDDATA;
836 }
837 }
838
839 sh->slice_qp_delta = get_se_golomb(gb);
840
841 if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
842 sh->slice_cb_qp_offset = get_se_golomb(gb);
843 sh->slice_cr_qp_offset = get_se_golomb(gb);
844 if (sh->slice_cb_qp_offset < -12 || sh->slice_cb_qp_offset > 12 ||
845 sh->slice_cr_qp_offset < -12 || sh->slice_cr_qp_offset > 12) {
846 av_log(s->avctx, AV_LOG_ERROR, "Invalid slice cx qp offset.\n");
847 return AVERROR_INVALIDDATA;
848 }
849 } else {
850 sh->slice_cb_qp_offset = 0;
851 sh->slice_cr_qp_offset = 0;
852 }
853
854 if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
855 sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
856 else
857 sh->cu_chroma_qp_offset_enabled_flag = 0;
858
859 if (s->ps.pps->deblocking_filter_control_present_flag) {
860 int deblocking_filter_override_flag = 0;
861
862 if (s->ps.pps->deblocking_filter_override_enabled_flag)
863 deblocking_filter_override_flag = get_bits1(gb);
864
865 if (deblocking_filter_override_flag) {
866 sh->disable_deblocking_filter_flag = get_bits1(gb);
867 if (!sh->disable_deblocking_filter_flag) {
868 int beta_offset_div2 = get_se_golomb(gb);
869 int tc_offset_div2 = get_se_golomb(gb) ;
870 if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
871 tc_offset_div2 < -6 || tc_offset_div2 > 6) {
872 av_log(s->avctx, AV_LOG_ERROR,
873 "Invalid deblock filter offsets: %d, %d\n",
874 beta_offset_div2, tc_offset_div2);
875 return AVERROR_INVALIDDATA;
876 }
877 sh->beta_offset = beta_offset_div2 * 2;
878 sh->tc_offset = tc_offset_div2 * 2;
879 }
880 } else {
881 sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
882 sh->beta_offset = s->ps.pps->beta_offset;
883 sh->tc_offset = s->ps.pps->tc_offset;
884 }
885 } else {
886 sh->disable_deblocking_filter_flag = 0;
887 sh->beta_offset = 0;
888 sh->tc_offset = 0;
889 }
890
891 if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
892 (sh->slice_sample_adaptive_offset_flag[0] ||
893 sh->slice_sample_adaptive_offset_flag[1] ||
894 !sh->disable_deblocking_filter_flag)) {
895 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
896 } else {
897 sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
898 }
899 } else if (!s->slice_initialized) {
900 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
901 return AVERROR_INVALIDDATA;
902 }
903
904 sh->num_entry_point_offsets = 0;
905 if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
906 unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
907 // It would be possible to bound this tighter but this here is simpler
908 if (num_entry_point_offsets > get_bits_left(gb)) {
909 av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
910 return AVERROR_INVALIDDATA;
911 }
912
913 sh->num_entry_point_offsets = num_entry_point_offsets;
914 if (sh->num_entry_point_offsets > 0) {
915 int offset_len = get_ue_golomb_long(gb) + 1;
916
917 if (offset_len < 1 || offset_len > 32) {
918 sh->num_entry_point_offsets = 0;
919 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
920 return AVERROR_INVALIDDATA;
921 }
922
923 av_freep(&sh->entry_point_offset);
924 av_freep(&sh->offset);
925 av_freep(&sh->size);
926 sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
927 sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
928 sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
929 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
930 sh->num_entry_point_offsets = 0;
931 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
932 return AVERROR(ENOMEM);
933 }
934 for (i = 0; i < sh->num_entry_point_offsets; i++) {
935 unsigned val = get_bits_long(gb, offset_len);
936 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
937 }
938 if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
939 s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
940 s->threads_number = 1;
941 } else
942 s->enable_parallel_tiles = 0;
943 } else
944 s->enable_parallel_tiles = 0;
945 }
946
947 if (s->ps.pps->slice_header_extension_present_flag) {
948 unsigned int length = get_ue_golomb_long(gb);
949 if (length*8LL > get_bits_left(gb)) {
950 av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
951 return AVERROR_INVALIDDATA;
952 }
953 for (i = 0; i < length; i++)
954 skip_bits(gb, 8); // slice_header_extension_data_byte
955 }
956
957 // Inferred parameters
958 sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
959 if (sh->slice_qp > 51 ||
960 sh->slice_qp < -s->ps.sps->qp_bd_offset) {
961 av_log(s->avctx, AV_LOG_ERROR,
962 "The slice_qp %d is outside the valid range "
963 "[%d, 51].\n",
964 sh->slice_qp,
965 -s->ps.sps->qp_bd_offset);
966 return AVERROR_INVALIDDATA;
967 }
968
969 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
970
971 if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
972 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
973 return AVERROR_INVALIDDATA;
974 }
975
976 if (get_bits_left(gb) < 0) {
977 av_log(s->avctx, AV_LOG_ERROR,
978 "Overread slice header by %d bits\n", -get_bits_left(gb));
979 return AVERROR_INVALIDDATA;
980 }
981
982 s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
983
984 if (!s->ps.pps->cu_qp_delta_enabled_flag)
985 s->HEVClc->qp_y = s->sh.slice_qp;
986
987 s->slice_initialized = 1;
988 s->HEVClc->tu.cu_qp_offset_cb = 0;
989 s->HEVClc->tu.cu_qp_offset_cr = 0;
990
991 return 0;
992 }
993
994 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
995
996 #define SET_SAO(elem, value) \
997 do { \
998 if (!sao_merge_up_flag && !sao_merge_left_flag) \
999 sao->elem = value; \
1000 else if (sao_merge_left_flag) \
1001 sao->elem = CTB(s->sao, rx-1, ry).elem; \
1002 else if (sao_merge_up_flag) \
1003 sao->elem = CTB(s->sao, rx, ry-1).elem; \
1004 else \
1005 sao->elem = 0; \
1006 } while (0)
1007
hls_sao_param(HEVCContext * s,int rx,int ry)1008 static void hls_sao_param(HEVCContext *s, int rx, int ry)
1009 {
1010 HEVCLocalContext *lc = s->HEVClc;
1011 int sao_merge_left_flag = 0;
1012 int sao_merge_up_flag = 0;
1013 SAOParams *sao = &CTB(s->sao, rx, ry);
1014 int c_idx, i;
1015
1016 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
1017 s->sh.slice_sample_adaptive_offset_flag[1]) {
1018 if (rx > 0) {
1019 if (lc->ctb_left_flag)
1020 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
1021 }
1022 if (ry > 0 && !sao_merge_left_flag) {
1023 if (lc->ctb_up_flag)
1024 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
1025 }
1026 }
1027
1028 for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
1029 int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
1030 s->ps.pps->log2_sao_offset_scale_chroma;
1031
1032 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
1033 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
1034 continue;
1035 }
1036
1037 if (c_idx == 2) {
1038 sao->type_idx[2] = sao->type_idx[1];
1039 sao->eo_class[2] = sao->eo_class[1];
1040 } else {
1041 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
1042 }
1043
1044 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
1045 continue;
1046
1047 for (i = 0; i < 4; i++)
1048 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
1049
1050 if (sao->type_idx[c_idx] == SAO_BAND) {
1051 for (i = 0; i < 4; i++) {
1052 if (sao->offset_abs[c_idx][i]) {
1053 SET_SAO(offset_sign[c_idx][i],
1054 ff_hevc_sao_offset_sign_decode(s));
1055 } else {
1056 sao->offset_sign[c_idx][i] = 0;
1057 }
1058 }
1059 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
1060 } else if (c_idx != 2) {
1061 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
1062 }
1063
1064 // Inferred parameters
1065 sao->offset_val[c_idx][0] = 0;
1066 for (i = 0; i < 4; i++) {
1067 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
1068 if (sao->type_idx[c_idx] == SAO_EDGE) {
1069 if (i > 1)
1070 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1071 } else if (sao->offset_sign[c_idx][i]) {
1072 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1073 }
1074 sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
1075 }
1076 }
1077 }
1078
1079 #undef SET_SAO
1080 #undef CTB
1081
hls_cross_component_pred(HEVCContext * s,int idx)1082 static int hls_cross_component_pred(HEVCContext *s, int idx) {
1083 HEVCLocalContext *lc = s->HEVClc;
1084 int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
1085
1086 if (log2_res_scale_abs_plus1 != 0) {
1087 int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
1088 lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
1089 (1 - 2 * res_scale_sign_flag);
1090 } else {
1091 lc->tu.res_scale_val = 0;
1092 }
1093
1094
1095 return 0;
1096 }
1097
hls_transform_unit(HEVCContext * s,int x0,int y0,int xBase,int yBase,int cb_xBase,int cb_yBase,int log2_cb_size,int log2_trafo_size,int blk_idx,int cbf_luma,int * cbf_cb,int * cbf_cr)1098 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
1099 int xBase, int yBase, int cb_xBase, int cb_yBase,
1100 int log2_cb_size, int log2_trafo_size,
1101 int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
1102 {
1103 HEVCLocalContext *lc = s->HEVClc;
1104 const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
1105 int i;
1106
1107 if (lc->cu.pred_mode == MODE_INTRA) {
1108 int trafo_size = 1 << log2_trafo_size;
1109 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1110
1111 s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
1112 }
1113
1114 if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1115 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1116 int scan_idx = SCAN_DIAG;
1117 int scan_idx_c = SCAN_DIAG;
1118 int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1119 (s->ps.sps->chroma_format_idc == 2 &&
1120 (cbf_cb[1] || cbf_cr[1]));
1121
1122 if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1123 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
1124 if (lc->tu.cu_qp_delta != 0)
1125 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1126 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1127 lc->tu.is_cu_qp_delta_coded = 1;
1128
1129 if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
1130 lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
1131 av_log(s->avctx, AV_LOG_ERROR,
1132 "The cu_qp_delta %d is outside the valid range "
1133 "[%d, %d].\n",
1134 lc->tu.cu_qp_delta,
1135 -(26 + s->ps.sps->qp_bd_offset / 2),
1136 (25 + s->ps.sps->qp_bd_offset / 2));
1137 return AVERROR_INVALIDDATA;
1138 }
1139
1140 ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
1141 }
1142
1143 if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1144 !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
1145 int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
1146 if (cu_chroma_qp_offset_flag) {
1147 int cu_chroma_qp_offset_idx = 0;
1148 if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
1149 cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
1150 av_log(s->avctx, AV_LOG_ERROR,
1151 "cu_chroma_qp_offset_idx not yet tested.\n");
1152 }
1153 lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1154 lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1155 } else {
1156 lc->tu.cu_qp_offset_cb = 0;
1157 lc->tu.cu_qp_offset_cr = 0;
1158 }
1159 lc->tu.is_cu_chroma_qp_offset_coded = 1;
1160 }
1161
1162 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1163 if (lc->tu.intra_pred_mode >= 6 &&
1164 lc->tu.intra_pred_mode <= 14) {
1165 scan_idx = SCAN_VERT;
1166 } else if (lc->tu.intra_pred_mode >= 22 &&
1167 lc->tu.intra_pred_mode <= 30) {
1168 scan_idx = SCAN_HORIZ;
1169 }
1170
1171 if (lc->tu.intra_pred_mode_c >= 6 &&
1172 lc->tu.intra_pred_mode_c <= 14) {
1173 scan_idx_c = SCAN_VERT;
1174 } else if (lc->tu.intra_pred_mode_c >= 22 &&
1175 lc->tu.intra_pred_mode_c <= 30) {
1176 scan_idx_c = SCAN_HORIZ;
1177 }
1178 }
1179
1180 lc->tu.cross_pf = 0;
1181
1182 if (cbf_luma)
1183 ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1184 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1185 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1186 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1187 lc->tu.cross_pf = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
1188 (lc->cu.pred_mode == MODE_INTER ||
1189 (lc->tu.chroma_mode_c == 4)));
1190
1191 if (lc->tu.cross_pf) {
1192 hls_cross_component_pred(s, 0);
1193 }
1194 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1195 if (lc->cu.pred_mode == MODE_INTRA) {
1196 ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1197 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
1198 }
1199 if (cbf_cb[i])
1200 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1201 log2_trafo_size_c, scan_idx_c, 1);
1202 else
1203 if (lc->tu.cross_pf) {
1204 ptrdiff_t stride = s->frame->linesize[1];
1205 int hshift = s->ps.sps->hshift[1];
1206 int vshift = s->ps.sps->vshift[1];
1207 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1208 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1209 int size = 1 << log2_trafo_size_c;
1210
1211 uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
1212 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1213 for (i = 0; i < (size * size); i++) {
1214 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1215 }
1216 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1217 }
1218 }
1219
1220 if (lc->tu.cross_pf) {
1221 hls_cross_component_pred(s, 1);
1222 }
1223 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1224 if (lc->cu.pred_mode == MODE_INTRA) {
1225 ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1226 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
1227 }
1228 if (cbf_cr[i])
1229 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1230 log2_trafo_size_c, scan_idx_c, 2);
1231 else
1232 if (lc->tu.cross_pf) {
1233 ptrdiff_t stride = s->frame->linesize[2];
1234 int hshift = s->ps.sps->hshift[2];
1235 int vshift = s->ps.sps->vshift[2];
1236 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1237 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1238 int size = 1 << log2_trafo_size_c;
1239
1240 uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
1241 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1242 for (i = 0; i < (size * size); i++) {
1243 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1244 }
1245 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1246 }
1247 }
1248 } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
1249 int trafo_size_h = 1 << (log2_trafo_size + 1);
1250 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1251 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1252 if (lc->cu.pred_mode == MODE_INTRA) {
1253 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1254 trafo_size_h, trafo_size_v);
1255 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
1256 }
1257 if (cbf_cb[i])
1258 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1259 log2_trafo_size, scan_idx_c, 1);
1260 }
1261 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1262 if (lc->cu.pred_mode == MODE_INTRA) {
1263 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1264 trafo_size_h, trafo_size_v);
1265 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
1266 }
1267 if (cbf_cr[i])
1268 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1269 log2_trafo_size, scan_idx_c, 2);
1270 }
1271 }
1272 } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1273 if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
1274 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1275 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1276 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
1277 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
1278 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
1279 if (s->ps.sps->chroma_format_idc == 2) {
1280 ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
1281 trafo_size_h, trafo_size_v);
1282 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
1283 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
1284 }
1285 } else if (blk_idx == 3) {
1286 int trafo_size_h = 1 << (log2_trafo_size + 1);
1287 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1288 ff_hevc_set_neighbour_available(s, xBase, yBase,
1289 trafo_size_h, trafo_size_v);
1290 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1291 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1292 if (s->ps.sps->chroma_format_idc == 2) {
1293 ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
1294 trafo_size_h, trafo_size_v);
1295 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
1296 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
1297 }
1298 }
1299 }
1300
1301 return 0;
1302 }
1303
set_deblocking_bypass(HEVCContext * s,int x0,int y0,int log2_cb_size)1304 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1305 {
1306 int cb_size = 1 << log2_cb_size;
1307 int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1308
1309 int min_pu_width = s->ps.sps->min_pu_width;
1310 int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1311 int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1312 int i, j;
1313
1314 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1315 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1316 s->is_pcm[i + j * min_pu_width] = 2;
1317 }
1318
hls_transform_tree(HEVCContext * s,int x0,int y0,int xBase,int yBase,int cb_xBase,int cb_yBase,int log2_cb_size,int log2_trafo_size,int trafo_depth,int blk_idx,const int * base_cbf_cb,const int * base_cbf_cr)1319 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1320 int xBase, int yBase, int cb_xBase, int cb_yBase,
1321 int log2_cb_size, int log2_trafo_size,
1322 int trafo_depth, int blk_idx,
1323 const int *base_cbf_cb, const int *base_cbf_cr)
1324 {
1325 HEVCLocalContext *lc = s->HEVClc;
1326 uint8_t split_transform_flag;
1327 int cbf_cb[2];
1328 int cbf_cr[2];
1329 int ret;
1330
1331 cbf_cb[0] = base_cbf_cb[0];
1332 cbf_cb[1] = base_cbf_cb[1];
1333 cbf_cr[0] = base_cbf_cr[0];
1334 cbf_cr[1] = base_cbf_cr[1];
1335
1336 if (lc->cu.intra_split_flag) {
1337 if (trafo_depth == 1) {
1338 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1339 if (s->ps.sps->chroma_format_idc == 3) {
1340 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1341 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1342 } else {
1343 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1344 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1345 }
1346 }
1347 } else {
1348 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1349 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1350 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1351 }
1352
1353 if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1354 log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1355 trafo_depth < lc->cu.max_trafo_depth &&
1356 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1357 split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1358 } else {
1359 int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1360 lc->cu.pred_mode == MODE_INTER &&
1361 lc->cu.part_mode != PART_2Nx2N &&
1362 trafo_depth == 0;
1363
1364 split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1365 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1366 inter_split;
1367 }
1368
1369 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1370 if (trafo_depth == 0 || cbf_cb[0]) {
1371 cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1372 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1373 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1374 }
1375 }
1376
1377 if (trafo_depth == 0 || cbf_cr[0]) {
1378 cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1379 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1380 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1381 }
1382 }
1383 }
1384
1385 if (split_transform_flag) {
1386 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1387 const int x1 = x0 + trafo_size_split;
1388 const int y1 = y0 + trafo_size_split;
1389
1390 #define SUBDIVIDE(x, y, idx) \
1391 do { \
1392 ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1393 log2_trafo_size - 1, trafo_depth + 1, idx, \
1394 cbf_cb, cbf_cr); \
1395 if (ret < 0) \
1396 return ret; \
1397 } while (0)
1398
1399 SUBDIVIDE(x0, y0, 0);
1400 SUBDIVIDE(x1, y0, 1);
1401 SUBDIVIDE(x0, y1, 2);
1402 SUBDIVIDE(x1, y1, 3);
1403
1404 #undef SUBDIVIDE
1405 } else {
1406 int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1407 int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1408 int min_tu_width = s->ps.sps->min_tb_width;
1409 int cbf_luma = 1;
1410
1411 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1412 cbf_cb[0] || cbf_cr[0] ||
1413 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1414 cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1415 }
1416
1417 ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1418 log2_cb_size, log2_trafo_size,
1419 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1420 if (ret < 0)
1421 return ret;
1422 // TODO: store cbf_luma somewhere else
1423 if (cbf_luma) {
1424 int i, j;
1425 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1426 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1427 int x_tu = (x0 + j) >> log2_min_tu_size;
1428 int y_tu = (y0 + i) >> log2_min_tu_size;
1429 s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1430 }
1431 }
1432 if (!s->sh.disable_deblocking_filter_flag) {
1433 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1434 if (s->ps.pps->transquant_bypass_enable_flag &&
1435 lc->cu.cu_transquant_bypass_flag)
1436 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1437 }
1438 }
1439 return 0;
1440 }
1441
hls_pcm_sample(HEVCContext * s,int x0,int y0,int log2_cb_size)1442 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1443 {
1444 HEVCLocalContext *lc = s->HEVClc;
1445 GetBitContext gb;
1446 int cb_size = 1 << log2_cb_size;
1447 ptrdiff_t stride0 = s->frame->linesize[0];
1448 ptrdiff_t stride1 = s->frame->linesize[1];
1449 ptrdiff_t stride2 = s->frame->linesize[2];
1450 uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1451 uint8_t *dst1 = &s->frame->data[1][(y0 >> s->ps.sps->vshift[1]) * stride1 + ((x0 >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
1452 uint8_t *dst2 = &s->frame->data[2][(y0 >> s->ps.sps->vshift[2]) * stride2 + ((x0 >> s->ps.sps->hshift[2]) << s->ps.sps->pixel_shift)];
1453
1454 int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
1455 (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
1456 ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
1457 s->ps.sps->pcm.bit_depth_chroma;
1458 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1459 int ret;
1460
1461 if (!s->sh.disable_deblocking_filter_flag)
1462 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1463
1464 ret = init_get_bits(&gb, pcm, length);
1465 if (ret < 0)
1466 return ret;
1467
1468 s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1469 if (s->ps.sps->chroma_format_idc) {
1470 s->hevcdsp.put_pcm(dst1, stride1,
1471 cb_size >> s->ps.sps->hshift[1],
1472 cb_size >> s->ps.sps->vshift[1],
1473 &gb, s->ps.sps->pcm.bit_depth_chroma);
1474 s->hevcdsp.put_pcm(dst2, stride2,
1475 cb_size >> s->ps.sps->hshift[2],
1476 cb_size >> s->ps.sps->vshift[2],
1477 &gb, s->ps.sps->pcm.bit_depth_chroma);
1478 }
1479
1480 return 0;
1481 }
1482
1483 /**
1484 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1485 *
1486 * @param s HEVC decoding context
1487 * @param dst target buffer for block data at block position
1488 * @param dststride stride of the dst buffer
1489 * @param ref reference picture buffer at origin (0, 0)
1490 * @param mv motion vector (relative to block position) to get pixel data from
1491 * @param x_off horizontal position of block from origin (0, 0)
1492 * @param y_off vertical position of block from origin (0, 0)
1493 * @param block_w width of block
1494 * @param block_h height of block
1495 * @param luma_weight weighting factor applied to the luma prediction
1496 * @param luma_offset additive offset applied to the luma prediction value
1497 */
1498
luma_mc_uni(HEVCContext * s,uint8_t * dst,ptrdiff_t dststride,AVFrame * ref,const Mv * mv,int x_off,int y_off,int block_w,int block_h,int luma_weight,int luma_offset)1499 static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1500 AVFrame *ref, const Mv *mv, int x_off, int y_off,
1501 int block_w, int block_h, int luma_weight, int luma_offset)
1502 {
1503 HEVCLocalContext *lc = s->HEVClc;
1504 uint8_t *src = ref->data[0];
1505 ptrdiff_t srcstride = ref->linesize[0];
1506 int pic_width = s->ps.sps->width;
1507 int pic_height = s->ps.sps->height;
1508 int mx = mv->x & 3;
1509 int my = mv->y & 3;
1510 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1511 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1512 int idx = hevc_pel_weight[block_w];
1513
1514 x_off += mv->x >> 2;
1515 y_off += mv->y >> 2;
1516 src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1517
1518 if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1519 x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1520 y_off >= pic_height - block_h - QPEL_EXTRA_AFTER ||
1521 ref == s->frame) {
1522 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1523 int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1524 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1525
1526 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1527 edge_emu_stride, srcstride,
1528 block_w + QPEL_EXTRA,
1529 block_h + QPEL_EXTRA,
1530 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1531 pic_width, pic_height);
1532 src = lc->edge_emu_buffer + buf_offset;
1533 srcstride = edge_emu_stride;
1534 }
1535
1536 if (!weight_flag)
1537 s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1538 block_h, mx, my, block_w);
1539 else
1540 s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1541 block_h, s->sh.luma_log2_weight_denom,
1542 luma_weight, luma_offset, mx, my, block_w);
1543 }
1544
1545 /**
1546 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1547 *
1548 * @param s HEVC decoding context
1549 * @param dst target buffer for block data at block position
1550 * @param dststride stride of the dst buffer
1551 * @param ref0 reference picture0 buffer at origin (0, 0)
1552 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1553 * @param x_off horizontal position of block from origin (0, 0)
1554 * @param y_off vertical position of block from origin (0, 0)
1555 * @param block_w width of block
1556 * @param block_h height of block
1557 * @param ref1 reference picture1 buffer at origin (0, 0)
1558 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1559 * @param current_mv current motion vector structure
1560 */
luma_mc_bi(HEVCContext * s,uint8_t * dst,ptrdiff_t dststride,AVFrame * ref0,const Mv * mv0,int x_off,int y_off,int block_w,int block_h,AVFrame * ref1,const Mv * mv1,struct MvField * current_mv)1561 static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1562 AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1563 int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
1564 {
1565 HEVCLocalContext *lc = s->HEVClc;
1566 ptrdiff_t src0stride = ref0->linesize[0];
1567 ptrdiff_t src1stride = ref1->linesize[0];
1568 int pic_width = s->ps.sps->width;
1569 int pic_height = s->ps.sps->height;
1570 int mx0 = mv0->x & 3;
1571 int my0 = mv0->y & 3;
1572 int mx1 = mv1->x & 3;
1573 int my1 = mv1->y & 3;
1574 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1575 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1576 int x_off0 = x_off + (mv0->x >> 2);
1577 int y_off0 = y_off + (mv0->y >> 2);
1578 int x_off1 = x_off + (mv1->x >> 2);
1579 int y_off1 = y_off + (mv1->y >> 2);
1580 int idx = hevc_pel_weight[block_w];
1581
1582 uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1583 uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1584
1585 if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1586 x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1587 y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1588 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1589 int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1590 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1591
1592 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1593 edge_emu_stride, src0stride,
1594 block_w + QPEL_EXTRA,
1595 block_h + QPEL_EXTRA,
1596 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1597 pic_width, pic_height);
1598 src0 = lc->edge_emu_buffer + buf_offset;
1599 src0stride = edge_emu_stride;
1600 }
1601
1602 if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1603 x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1604 y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1605 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1606 int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1607 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1608
1609 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1610 edge_emu_stride, src1stride,
1611 block_w + QPEL_EXTRA,
1612 block_h + QPEL_EXTRA,
1613 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1614 pic_width, pic_height);
1615 src1 = lc->edge_emu_buffer2 + buf_offset;
1616 src1stride = edge_emu_stride;
1617 }
1618
1619 s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1620 block_h, mx0, my0, block_w);
1621 if (!weight_flag)
1622 s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1623 block_h, mx1, my1, block_w);
1624 else
1625 s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1626 block_h, s->sh.luma_log2_weight_denom,
1627 s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1628 s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1629 s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1630 s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1631 mx1, my1, block_w);
1632
1633 }
1634
1635 /**
1636 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1637 *
1638 * @param s HEVC decoding context
1639 * @param dst1 target buffer for block data at block position (U plane)
1640 * @param dst2 target buffer for block data at block position (V plane)
1641 * @param dststride stride of the dst1 and dst2 buffers
1642 * @param ref reference picture buffer at origin (0, 0)
1643 * @param mv motion vector (relative to block position) to get pixel data from
1644 * @param x_off horizontal position of block from origin (0, 0)
1645 * @param y_off vertical position of block from origin (0, 0)
1646 * @param block_w width of block
1647 * @param block_h height of block
1648 * @param chroma_weight weighting factor applied to the chroma prediction
1649 * @param chroma_offset additive offset applied to the chroma prediction value
1650 */
1651
chroma_mc_uni(HEVCContext * s,uint8_t * dst0,ptrdiff_t dststride,uint8_t * src0,ptrdiff_t srcstride,int reflist,int x_off,int y_off,int block_w,int block_h,struct MvField * current_mv,int chroma_weight,int chroma_offset)1652 static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
1653 ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
1654 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
1655 {
1656 HEVCLocalContext *lc = s->HEVClc;
1657 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1658 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1659 const Mv *mv = ¤t_mv->mv[reflist];
1660 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1661 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1662 int idx = hevc_pel_weight[block_w];
1663 int hshift = s->ps.sps->hshift[1];
1664 int vshift = s->ps.sps->vshift[1];
1665 intptr_t mx = av_mod_uintp2(mv->x, 2 + hshift);
1666 intptr_t my = av_mod_uintp2(mv->y, 2 + vshift);
1667 intptr_t _mx = mx << (1 - hshift);
1668 intptr_t _my = my << (1 - vshift);
1669 int emu = src0 == s->frame->data[1] || src0 == s->frame->data[2];
1670
1671 x_off += mv->x >> (2 + hshift);
1672 y_off += mv->y >> (2 + vshift);
1673 src0 += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1674
1675 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1676 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1677 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER ||
1678 emu) {
1679 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1680 int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
1681 int buf_offset0 = EPEL_EXTRA_BEFORE *
1682 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1683 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1684 edge_emu_stride, srcstride,
1685 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1686 x_off - EPEL_EXTRA_BEFORE,
1687 y_off - EPEL_EXTRA_BEFORE,
1688 pic_width, pic_height);
1689
1690 src0 = lc->edge_emu_buffer + buf_offset0;
1691 srcstride = edge_emu_stride;
1692 }
1693 if (!weight_flag)
1694 s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1695 block_h, _mx, _my, block_w);
1696 else
1697 s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1698 block_h, s->sh.chroma_log2_weight_denom,
1699 chroma_weight, chroma_offset, _mx, _my, block_w);
1700 }
1701
1702 /**
1703 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1704 *
1705 * @param s HEVC decoding context
1706 * @param dst target buffer for block data at block position
1707 * @param dststride stride of the dst buffer
1708 * @param ref0 reference picture0 buffer at origin (0, 0)
1709 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1710 * @param x_off horizontal position of block from origin (0, 0)
1711 * @param y_off vertical position of block from origin (0, 0)
1712 * @param block_w width of block
1713 * @param block_h height of block
1714 * @param ref1 reference picture1 buffer at origin (0, 0)
1715 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1716 * @param current_mv current motion vector structure
1717 * @param cidx chroma component(cb, cr)
1718 */
chroma_mc_bi(HEVCContext * s,uint8_t * dst0,ptrdiff_t dststride,AVFrame * ref0,AVFrame * ref1,int x_off,int y_off,int block_w,int block_h,struct MvField * current_mv,int cidx)1719 static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
1720 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
1721 {
1722 HEVCLocalContext *lc = s->HEVClc;
1723 uint8_t *src1 = ref0->data[cidx+1];
1724 uint8_t *src2 = ref1->data[cidx+1];
1725 ptrdiff_t src1stride = ref0->linesize[cidx+1];
1726 ptrdiff_t src2stride = ref1->linesize[cidx+1];
1727 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1728 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1729 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1730 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1731 Mv *mv0 = ¤t_mv->mv[0];
1732 Mv *mv1 = ¤t_mv->mv[1];
1733 int hshift = s->ps.sps->hshift[1];
1734 int vshift = s->ps.sps->vshift[1];
1735
1736 intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
1737 intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
1738 intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
1739 intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
1740 intptr_t _mx0 = mx0 << (1 - hshift);
1741 intptr_t _my0 = my0 << (1 - vshift);
1742 intptr_t _mx1 = mx1 << (1 - hshift);
1743 intptr_t _my1 = my1 << (1 - vshift);
1744
1745 int x_off0 = x_off + (mv0->x >> (2 + hshift));
1746 int y_off0 = y_off + (mv0->y >> (2 + vshift));
1747 int x_off1 = x_off + (mv1->x >> (2 + hshift));
1748 int y_off1 = y_off + (mv1->y >> (2 + vshift));
1749 int idx = hevc_pel_weight[block_w];
1750 src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1751 src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1752
1753 if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1754 x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1755 y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1756 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1757 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1758 int buf_offset1 = EPEL_EXTRA_BEFORE *
1759 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1760
1761 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1762 edge_emu_stride, src1stride,
1763 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1764 x_off0 - EPEL_EXTRA_BEFORE,
1765 y_off0 - EPEL_EXTRA_BEFORE,
1766 pic_width, pic_height);
1767
1768 src1 = lc->edge_emu_buffer + buf_offset1;
1769 src1stride = edge_emu_stride;
1770 }
1771
1772 if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1773 x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1774 y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1775 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1776 int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1777 int buf_offset1 = EPEL_EXTRA_BEFORE *
1778 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1779
1780 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1781 edge_emu_stride, src2stride,
1782 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1783 x_off1 - EPEL_EXTRA_BEFORE,
1784 y_off1 - EPEL_EXTRA_BEFORE,
1785 pic_width, pic_height);
1786
1787 src2 = lc->edge_emu_buffer2 + buf_offset1;
1788 src2stride = edge_emu_stride;
1789 }
1790
1791 s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1792 block_h, _mx0, _my0, block_w);
1793 if (!weight_flag)
1794 s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1795 src2, src2stride, lc->tmp,
1796 block_h, _mx1, _my1, block_w);
1797 else
1798 s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1799 src2, src2stride, lc->tmp,
1800 block_h,
1801 s->sh.chroma_log2_weight_denom,
1802 s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1803 s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1804 s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1805 s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1806 _mx1, _my1, block_w);
1807 }
1808
hevc_await_progress(HEVCContext * s,HEVCFrame * ref,const Mv * mv,int y0,int height)1809 static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1810 const Mv *mv, int y0, int height)
1811 {
1812 if (s->threads_type == FF_THREAD_FRAME ) {
1813 int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1814
1815 ff_thread_await_progress(&ref->tf, y, 0);
1816 }
1817 }
1818
hevc_luma_mv_mvp_mode(HEVCContext * s,int x0,int y0,int nPbW,int nPbH,int log2_cb_size,int part_idx,int merge_idx,MvField * mv)1819 static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1820 int nPbH, int log2_cb_size, int part_idx,
1821 int merge_idx, MvField *mv)
1822 {
1823 HEVCLocalContext *lc = s->HEVClc;
1824 enum InterPredIdc inter_pred_idc = PRED_L0;
1825 int mvp_flag;
1826
1827 ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1828 mv->pred_flag = 0;
1829 if (s->sh.slice_type == HEVC_SLICE_B)
1830 inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1831
1832 if (inter_pred_idc != PRED_L1) {
1833 if (s->sh.nb_refs[L0])
1834 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1835
1836 mv->pred_flag = PF_L0;
1837 ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1838 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1839 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1840 part_idx, merge_idx, mv, mvp_flag, 0);
1841 mv->mv[0].x += lc->pu.mvd.x;
1842 mv->mv[0].y += lc->pu.mvd.y;
1843 }
1844
1845 if (inter_pred_idc != PRED_L0) {
1846 if (s->sh.nb_refs[L1])
1847 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1848
1849 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1850 AV_ZERO32(&lc->pu.mvd);
1851 } else {
1852 ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1853 }
1854
1855 mv->pred_flag += PF_L1;
1856 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1857 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1858 part_idx, merge_idx, mv, mvp_flag, 1);
1859 mv->mv[1].x += lc->pu.mvd.x;
1860 mv->mv[1].y += lc->pu.mvd.y;
1861 }
1862 }
1863
hls_prediction_unit(HEVCContext * s,int x0,int y0,int nPbW,int nPbH,int log2_cb_size,int partIdx,int idx)1864 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1865 int nPbW, int nPbH,
1866 int log2_cb_size, int partIdx, int idx)
1867 {
1868 #define POS(c_idx, x, y) \
1869 &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1870 (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1871 HEVCLocalContext *lc = s->HEVClc;
1872 int merge_idx = 0;
1873 struct MvField current_mv = {{{ 0 }}};
1874
1875 int min_pu_width = s->ps.sps->min_pu_width;
1876
1877 MvField *tab_mvf = s->ref->tab_mvf;
1878 RefPicList *refPicList = s->ref->refPicList;
1879 HEVCFrame *ref0 = NULL, *ref1 = NULL;
1880 uint8_t *dst0 = POS(0, x0, y0);
1881 uint8_t *dst1 = POS(1, x0, y0);
1882 uint8_t *dst2 = POS(2, x0, y0);
1883 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1884 int min_cb_width = s->ps.sps->min_cb_width;
1885 int x_cb = x0 >> log2_min_cb_size;
1886 int y_cb = y0 >> log2_min_cb_size;
1887 int x_pu, y_pu;
1888 int i, j;
1889
1890 int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1891
1892 if (!skip_flag)
1893 lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1894
1895 if (skip_flag || lc->pu.merge_flag) {
1896 if (s->sh.max_num_merge_cand > 1)
1897 merge_idx = ff_hevc_merge_idx_decode(s);
1898 else
1899 merge_idx = 0;
1900
1901 ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1902 partIdx, merge_idx, ¤t_mv);
1903 } else {
1904 hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1905 partIdx, merge_idx, ¤t_mv);
1906 }
1907
1908 x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1909 y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1910
1911 for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1912 for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1913 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1914
1915 if (current_mv.pred_flag & PF_L0) {
1916 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1917 if (!ref0 || !ref0->frame->data[0])
1918 return;
1919 hevc_await_progress(s, ref0, ¤t_mv.mv[0], y0, nPbH);
1920 }
1921 if (current_mv.pred_flag & PF_L1) {
1922 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1923 if (!ref1 || !ref1->frame->data[0])
1924 return;
1925 hevc_await_progress(s, ref1, ¤t_mv.mv[1], y0, nPbH);
1926 }
1927
1928 if (current_mv.pred_flag == PF_L0) {
1929 int x0_c = x0 >> s->ps.sps->hshift[1];
1930 int y0_c = y0 >> s->ps.sps->vshift[1];
1931 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1932 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1933
1934 luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
1935 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1936 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1937 s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1938
1939 if (s->ps.sps->chroma_format_idc) {
1940 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1941 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1942 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1943 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1944 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1945 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1946 }
1947 } else if (current_mv.pred_flag == PF_L1) {
1948 int x0_c = x0 >> s->ps.sps->hshift[1];
1949 int y0_c = y0 >> s->ps.sps->vshift[1];
1950 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1951 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1952
1953 luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1954 ¤t_mv.mv[1], x0, y0, nPbW, nPbH,
1955 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1956 s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1957
1958 if (s->ps.sps->chroma_format_idc) {
1959 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1960 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1961 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1962
1963 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1964 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1965 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1966 }
1967 } else if (current_mv.pred_flag == PF_BI) {
1968 int x0_c = x0 >> s->ps.sps->hshift[1];
1969 int y0_c = y0 >> s->ps.sps->vshift[1];
1970 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1971 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1972
1973 luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1974 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1975 ref1->frame, ¤t_mv.mv[1], ¤t_mv);
1976
1977 if (s->ps.sps->chroma_format_idc) {
1978 chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1979 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 0);
1980
1981 chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1982 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 1);
1983 }
1984 }
1985 }
1986
1987 /**
1988 * 8.4.1
1989 */
luma_intra_pred_mode(HEVCContext * s,int x0,int y0,int pu_size,int prev_intra_luma_pred_flag)1990 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1991 int prev_intra_luma_pred_flag)
1992 {
1993 HEVCLocalContext *lc = s->HEVClc;
1994 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1995 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1996 int min_pu_width = s->ps.sps->min_pu_width;
1997 int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1998 int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1999 int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
2000
2001 int cand_up = (lc->ctb_up_flag || y0b) ?
2002 s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
2003 int cand_left = (lc->ctb_left_flag || x0b) ?
2004 s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
2005
2006 int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
2007
2008 MvField *tab_mvf = s->ref->tab_mvf;
2009 int intra_pred_mode;
2010 int candidate[3];
2011 int i, j;
2012
2013 // intra_pred_mode prediction does not cross vertical CTB boundaries
2014 if ((y0 - 1) < y_ctb)
2015 cand_up = INTRA_DC;
2016
2017 if (cand_left == cand_up) {
2018 if (cand_left < 2) {
2019 candidate[0] = INTRA_PLANAR;
2020 candidate[1] = INTRA_DC;
2021 candidate[2] = INTRA_ANGULAR_26;
2022 } else {
2023 candidate[0] = cand_left;
2024 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
2025 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
2026 }
2027 } else {
2028 candidate[0] = cand_left;
2029 candidate[1] = cand_up;
2030 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
2031 candidate[2] = INTRA_PLANAR;
2032 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
2033 candidate[2] = INTRA_DC;
2034 } else {
2035 candidate[2] = INTRA_ANGULAR_26;
2036 }
2037 }
2038
2039 if (prev_intra_luma_pred_flag) {
2040 intra_pred_mode = candidate[lc->pu.mpm_idx];
2041 } else {
2042 if (candidate[0] > candidate[1])
2043 FFSWAP(uint8_t, candidate[0], candidate[1]);
2044 if (candidate[0] > candidate[2])
2045 FFSWAP(uint8_t, candidate[0], candidate[2]);
2046 if (candidate[1] > candidate[2])
2047 FFSWAP(uint8_t, candidate[1], candidate[2]);
2048
2049 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
2050 for (i = 0; i < 3; i++)
2051 if (intra_pred_mode >= candidate[i])
2052 intra_pred_mode++;
2053 }
2054
2055 /* write the intra prediction units into the mv array */
2056 if (!size_in_pus)
2057 size_in_pus = 1;
2058 for (i = 0; i < size_in_pus; i++) {
2059 memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
2060 intra_pred_mode, size_in_pus);
2061
2062 for (j = 0; j < size_in_pus; j++) {
2063 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
2064 }
2065 }
2066
2067 return intra_pred_mode;
2068 }
2069
set_ct_depth(HEVCContext * s,int x0,int y0,int log2_cb_size,int ct_depth)2070 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
2071 int log2_cb_size, int ct_depth)
2072 {
2073 int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
2074 int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
2075 int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
2076 int y;
2077
2078 for (y = 0; y < length; y++)
2079 memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
2080 ct_depth, length);
2081 }
2082
2083 static const uint8_t tab_mode_idx[] = {
2084 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
2085 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
2086
intra_prediction_unit(HEVCContext * s,int x0,int y0,int log2_cb_size)2087 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
2088 int log2_cb_size)
2089 {
2090 HEVCLocalContext *lc = s->HEVClc;
2091 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
2092 uint8_t prev_intra_luma_pred_flag[4];
2093 int split = lc->cu.part_mode == PART_NxN;
2094 int pb_size = (1 << log2_cb_size) >> split;
2095 int side = split + 1;
2096 int chroma_mode;
2097 int i, j;
2098
2099 for (i = 0; i < side; i++)
2100 for (j = 0; j < side; j++)
2101 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
2102
2103 for (i = 0; i < side; i++) {
2104 for (j = 0; j < side; j++) {
2105 if (prev_intra_luma_pred_flag[2 * i + j])
2106 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
2107 else
2108 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
2109
2110 lc->pu.intra_pred_mode[2 * i + j] =
2111 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
2112 prev_intra_luma_pred_flag[2 * i + j]);
2113 }
2114 }
2115
2116 if (s->ps.sps->chroma_format_idc == 3) {
2117 for (i = 0; i < side; i++) {
2118 for (j = 0; j < side; j++) {
2119 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2120 if (chroma_mode != 4) {
2121 if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
2122 lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2123 else
2124 lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2125 } else {
2126 lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2127 }
2128 }
2129 }
2130 } else if (s->ps.sps->chroma_format_idc == 2) {
2131 int mode_idx;
2132 lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2133 if (chroma_mode != 4) {
2134 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2135 mode_idx = 34;
2136 else
2137 mode_idx = intra_chroma_table[chroma_mode];
2138 } else {
2139 mode_idx = lc->pu.intra_pred_mode[0];
2140 }
2141 lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2142 } else if (s->ps.sps->chroma_format_idc != 0) {
2143 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2144 if (chroma_mode != 4) {
2145 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2146 lc->pu.intra_pred_mode_c[0] = 34;
2147 else
2148 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2149 } else {
2150 lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2151 }
2152 }
2153 }
2154
intra_prediction_unit_default_value(HEVCContext * s,int x0,int y0,int log2_cb_size)2155 static void intra_prediction_unit_default_value(HEVCContext *s,
2156 int x0, int y0,
2157 int log2_cb_size)
2158 {
2159 HEVCLocalContext *lc = s->HEVClc;
2160 int pb_size = 1 << log2_cb_size;
2161 int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
2162 int min_pu_width = s->ps.sps->min_pu_width;
2163 MvField *tab_mvf = s->ref->tab_mvf;
2164 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
2165 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
2166 int j, k;
2167
2168 if (size_in_pus == 0)
2169 size_in_pus = 1;
2170 for (j = 0; j < size_in_pus; j++)
2171 memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2172 if (lc->cu.pred_mode == MODE_INTRA)
2173 for (j = 0; j < size_in_pus; j++)
2174 for (k = 0; k < size_in_pus; k++)
2175 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2176 }
2177
hls_coding_unit(HEVCContext * s,int x0,int y0,int log2_cb_size)2178 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2179 {
2180 int cb_size = 1 << log2_cb_size;
2181 HEVCLocalContext *lc = s->HEVClc;
2182 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2183 int length = cb_size >> log2_min_cb_size;
2184 int min_cb_width = s->ps.sps->min_cb_width;
2185 int x_cb = x0 >> log2_min_cb_size;
2186 int y_cb = y0 >> log2_min_cb_size;
2187 int idx = log2_cb_size - 2;
2188 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2189 int x, y, ret;
2190
2191 lc->cu.x = x0;
2192 lc->cu.y = y0;
2193 lc->cu.pred_mode = MODE_INTRA;
2194 lc->cu.part_mode = PART_2Nx2N;
2195 lc->cu.intra_split_flag = 0;
2196
2197 SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2198 for (x = 0; x < 4; x++)
2199 lc->pu.intra_pred_mode[x] = 1;
2200 if (s->ps.pps->transquant_bypass_enable_flag) {
2201 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
2202 if (lc->cu.cu_transquant_bypass_flag)
2203 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2204 } else
2205 lc->cu.cu_transquant_bypass_flag = 0;
2206
2207 if (s->sh.slice_type != HEVC_SLICE_I) {
2208 uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2209
2210 x = y_cb * min_cb_width + x_cb;
2211 for (y = 0; y < length; y++) {
2212 memset(&s->skip_flag[x], skip_flag, length);
2213 x += min_cb_width;
2214 }
2215 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2216 } else {
2217 x = y_cb * min_cb_width + x_cb;
2218 for (y = 0; y < length; y++) {
2219 memset(&s->skip_flag[x], 0, length);
2220 x += min_cb_width;
2221 }
2222 }
2223
2224 if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2225 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2226 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2227
2228 if (!s->sh.disable_deblocking_filter_flag)
2229 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2230 } else {
2231 int pcm_flag = 0;
2232
2233 if (s->sh.slice_type != HEVC_SLICE_I)
2234 lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2235 if (lc->cu.pred_mode != MODE_INTRA ||
2236 log2_cb_size == s->ps.sps->log2_min_cb_size) {
2237 lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2238 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2239 lc->cu.pred_mode == MODE_INTRA;
2240 }
2241
2242 if (lc->cu.pred_mode == MODE_INTRA) {
2243 if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2244 log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2245 log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2246 pcm_flag = ff_hevc_pcm_flag_decode(s);
2247 }
2248 if (pcm_flag) {
2249 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2250 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2251 if (s->ps.sps->pcm.loop_filter_disable_flag)
2252 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2253
2254 if (ret < 0)
2255 return ret;
2256 } else {
2257 intra_prediction_unit(s, x0, y0, log2_cb_size);
2258 }
2259 } else {
2260 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2261 switch (lc->cu.part_mode) {
2262 case PART_2Nx2N:
2263 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2264 break;
2265 case PART_2NxN:
2266 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2267 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2268 break;
2269 case PART_Nx2N:
2270 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2271 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2272 break;
2273 case PART_2NxnU:
2274 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2275 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2276 break;
2277 case PART_2NxnD:
2278 hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2279 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2280 break;
2281 case PART_nLx2N:
2282 hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2283 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2284 break;
2285 case PART_nRx2N:
2286 hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2287 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2288 break;
2289 case PART_NxN:
2290 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2291 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2292 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2293 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2294 break;
2295 }
2296 }
2297
2298 if (!pcm_flag) {
2299 int rqt_root_cbf = 1;
2300
2301 if (lc->cu.pred_mode != MODE_INTRA &&
2302 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2303 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2304 }
2305 if (rqt_root_cbf) {
2306 const static int cbf[2] = { 0 };
2307 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2308 s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2309 s->ps.sps->max_transform_hierarchy_depth_inter;
2310 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2311 log2_cb_size,
2312 log2_cb_size, 0, 0, cbf, cbf);
2313 if (ret < 0)
2314 return ret;
2315 } else {
2316 if (!s->sh.disable_deblocking_filter_flag)
2317 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2318 }
2319 }
2320 }
2321
2322 if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2323 ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2324
2325 x = y_cb * min_cb_width + x_cb;
2326 for (y = 0; y < length; y++) {
2327 memset(&s->qp_y_tab[x], lc->qp_y, length);
2328 x += min_cb_width;
2329 }
2330
2331 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2332 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2333 lc->qPy_pred = lc->qp_y;
2334 }
2335
2336 set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2337
2338 return 0;
2339 }
2340
hls_coding_quadtree(HEVCContext * s,int x0,int y0,int log2_cb_size,int cb_depth)2341 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2342 int log2_cb_size, int cb_depth)
2343 {
2344 HEVCLocalContext *lc = s->HEVClc;
2345 const int cb_size = 1 << log2_cb_size;
2346 int ret;
2347 int split_cu;
2348
2349 lc->ct_depth = cb_depth;
2350 if (x0 + cb_size <= s->ps.sps->width &&
2351 y0 + cb_size <= s->ps.sps->height &&
2352 log2_cb_size > s->ps.sps->log2_min_cb_size) {
2353 split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2354 } else {
2355 split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2356 }
2357 if (s->ps.pps->cu_qp_delta_enabled_flag &&
2358 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2359 lc->tu.is_cu_qp_delta_coded = 0;
2360 lc->tu.cu_qp_delta = 0;
2361 }
2362
2363 if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2364 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2365 lc->tu.is_cu_chroma_qp_offset_coded = 0;
2366 }
2367
2368 if (split_cu) {
2369 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2370 const int cb_size_split = cb_size >> 1;
2371 const int x1 = x0 + cb_size_split;
2372 const int y1 = y0 + cb_size_split;
2373
2374 int more_data = 0;
2375
2376 more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2377 if (more_data < 0)
2378 return more_data;
2379
2380 if (more_data && x1 < s->ps.sps->width) {
2381 more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2382 if (more_data < 0)
2383 return more_data;
2384 }
2385 if (more_data && y1 < s->ps.sps->height) {
2386 more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2387 if (more_data < 0)
2388 return more_data;
2389 }
2390 if (more_data && x1 < s->ps.sps->width &&
2391 y1 < s->ps.sps->height) {
2392 more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2393 if (more_data < 0)
2394 return more_data;
2395 }
2396
2397 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2398 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2399 lc->qPy_pred = lc->qp_y;
2400
2401 if (more_data)
2402 return ((x1 + cb_size_split) < s->ps.sps->width ||
2403 (y1 + cb_size_split) < s->ps.sps->height);
2404 else
2405 return 0;
2406 } else {
2407 ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2408 if (ret < 0)
2409 return ret;
2410 if ((!((x0 + cb_size) %
2411 (1 << (s->ps.sps->log2_ctb_size))) ||
2412 (x0 + cb_size >= s->ps.sps->width)) &&
2413 (!((y0 + cb_size) %
2414 (1 << (s->ps.sps->log2_ctb_size))) ||
2415 (y0 + cb_size >= s->ps.sps->height))) {
2416 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2417 return !end_of_slice_flag;
2418 } else {
2419 return 1;
2420 }
2421 }
2422
2423 return 0;
2424 }
2425
hls_decode_neighbour(HEVCContext * s,int x_ctb,int y_ctb,int ctb_addr_ts)2426 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2427 int ctb_addr_ts)
2428 {
2429 HEVCLocalContext *lc = s->HEVClc;
2430 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2431 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2432 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2433
2434 s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2435
2436 if (s->ps.pps->entropy_coding_sync_enabled_flag) {
2437 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2438 lc->first_qp_group = 1;
2439 lc->end_of_tiles_x = s->ps.sps->width;
2440 } else if (s->ps.pps->tiles_enabled_flag) {
2441 if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2442 int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2443 lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2444 lc->first_qp_group = 1;
2445 }
2446 } else {
2447 lc->end_of_tiles_x = s->ps.sps->width;
2448 }
2449
2450 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2451
2452 lc->boundary_flags = 0;
2453 if (s->ps.pps->tiles_enabled_flag) {
2454 if (x_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2455 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2456 if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2457 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2458 if (y_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]])
2459 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2460 if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2461 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2462 } else {
2463 if (ctb_addr_in_slice <= 0)
2464 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2465 if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2466 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2467 }
2468
2469 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2470 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2471 lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->ps.sps->ctb_width]]));
2472 lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
2473 }
2474
hls_decode_entry(AVCodecContext * avctxt,void * isFilterThread)2475 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2476 {
2477 HEVCContext *s = avctxt->priv_data;
2478 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2479 int more_data = 1;
2480 int x_ctb = 0;
2481 int y_ctb = 0;
2482 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2483 int ret;
2484
2485 if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2486 av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2487 return AVERROR_INVALIDDATA;
2488 }
2489
2490 if (s->sh.dependent_slice_segment_flag) {
2491 int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2492 if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2493 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2494 return AVERROR_INVALIDDATA;
2495 }
2496 }
2497
2498 while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2499 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2500
2501 x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2502 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2503 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2504
2505 ret = ff_hevc_cabac_init(s, ctb_addr_ts, 0);
2506 if (ret < 0) {
2507 s->tab_slice_address[ctb_addr_rs] = -1;
2508 return ret;
2509 }
2510
2511 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2512
2513 s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2514 s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2515 s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2516
2517 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2518 if (more_data < 0) {
2519 s->tab_slice_address[ctb_addr_rs] = -1;
2520 return more_data;
2521 }
2522
2523
2524 ctb_addr_ts++;
2525 ff_hevc_save_states(s, ctb_addr_ts);
2526 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2527 }
2528
2529 if (x_ctb + ctb_size >= s->ps.sps->width &&
2530 y_ctb + ctb_size >= s->ps.sps->height)
2531 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2532
2533 return ctb_addr_ts;
2534 }
2535
hls_slice_data(HEVCContext * s)2536 static int hls_slice_data(HEVCContext *s)
2537 {
2538 int arg[2];
2539 int ret[2];
2540
2541 arg[0] = 0;
2542 arg[1] = 1;
2543
2544 s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2545 return ret[0];
2546 }
hls_decode_entry_wpp(AVCodecContext * avctxt,void * input_ctb_row,int job,int self_id)2547 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2548 {
2549 HEVCContext *s1 = avctxt->priv_data, *s;
2550 HEVCLocalContext *lc;
2551 int ctb_size = 1<< s1->ps.sps->log2_ctb_size;
2552 int more_data = 1;
2553 int *ctb_row_p = input_ctb_row;
2554 int ctb_row = ctb_row_p[job];
2555 int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->ps.sps->width + ctb_size - 1) >> s1->ps.sps->log2_ctb_size);
2556 int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2557 int thread = ctb_row % s1->threads_number;
2558 int ret;
2559
2560 s = s1->sList[self_id];
2561 lc = s->HEVClc;
2562
2563 if(ctb_row) {
2564 ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2565 if (ret < 0)
2566 goto error;
2567 ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2568 }
2569
2570 while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2571 int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2572 int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2573
2574 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2575
2576 ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2577
2578 if (atomic_load(&s1->wpp_err)) {
2579 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2580 return 0;
2581 }
2582
2583 ret = ff_hevc_cabac_init(s, ctb_addr_ts, thread);
2584 if (ret < 0)
2585 goto error;
2586 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2587 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2588
2589 if (more_data < 0) {
2590 ret = more_data;
2591 goto error;
2592 }
2593
2594 ctb_addr_ts++;
2595
2596 ff_hevc_save_states(s, ctb_addr_ts);
2597 ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2598 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2599
2600 if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2601 atomic_store(&s1->wpp_err, 1);
2602 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2603 return 0;
2604 }
2605
2606 if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2607 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2608 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2609 return ctb_addr_ts;
2610 }
2611 ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2612 x_ctb+=ctb_size;
2613
2614 if(x_ctb >= s->ps.sps->width) {
2615 break;
2616 }
2617 }
2618 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2619
2620 return 0;
2621 error:
2622 s->tab_slice_address[ctb_addr_rs] = -1;
2623 atomic_store(&s1->wpp_err, 1);
2624 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2625 return ret;
2626 }
2627
hls_slice_data_wpp(HEVCContext * s,const H2645NAL * nal)2628 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2629 {
2630 const uint8_t *data = nal->data;
2631 int length = nal->size;
2632 HEVCLocalContext *lc = s->HEVClc;
2633 int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2634 int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2635 int64_t offset;
2636 int64_t startheader, cmpt = 0;
2637 int i, j, res = 0;
2638
2639 if (!ret || !arg) {
2640 av_free(ret);
2641 av_free(arg);
2642 return AVERROR(ENOMEM);
2643 }
2644
2645 if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * s->ps.sps->ctb_width >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
2646 av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2647 s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2648 s->ps.sps->ctb_width, s->ps.sps->ctb_height
2649 );
2650 res = AVERROR_INVALIDDATA;
2651 goto error;
2652 }
2653
2654 ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2655
2656 for (i = 1; i < s->threads_number; i++) {
2657 if (s->sList[i] && s->HEVClcList[i])
2658 continue;
2659 av_freep(&s->sList[i]);
2660 av_freep(&s->HEVClcList[i]);
2661 s->sList[i] = av_malloc(sizeof(HEVCContext));
2662 s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2663 if (!s->sList[i] || !s->HEVClcList[i]) {
2664 res = AVERROR(ENOMEM);
2665 goto error;
2666 }
2667 memcpy(s->sList[i], s, sizeof(HEVCContext));
2668 s->sList[i]->HEVClc = s->HEVClcList[i];
2669 }
2670
2671 offset = (lc->gb.index >> 3);
2672
2673 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2674 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2675 startheader--;
2676 cmpt++;
2677 }
2678 }
2679
2680 for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2681 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2682 for (j = 0, cmpt = 0, startheader = offset
2683 + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2684 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2685 startheader--;
2686 cmpt++;
2687 }
2688 }
2689 s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2690 s->sh.offset[i - 1] = offset;
2691
2692 }
2693 if (s->sh.num_entry_point_offsets != 0) {
2694 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2695 if (length < offset) {
2696 av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2697 res = AVERROR_INVALIDDATA;
2698 goto error;
2699 }
2700 s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2701 s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2702
2703 }
2704 s->data = data;
2705
2706 for (i = 1; i < s->threads_number; i++) {
2707 s->sList[i]->HEVClc->first_qp_group = 1;
2708 s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2709 memcpy(s->sList[i], s, sizeof(HEVCContext));
2710 s->sList[i]->HEVClc = s->HEVClcList[i];
2711 }
2712
2713 atomic_store(&s->wpp_err, 0);
2714 ff_reset_entries(s->avctx);
2715
2716 for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2717 arg[i] = i;
2718 ret[i] = 0;
2719 }
2720
2721 if (s->ps.pps->entropy_coding_sync_enabled_flag)
2722 s->avctx->execute2(s->avctx, hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2723
2724 for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2725 res += ret[i];
2726 error:
2727 av_free(ret);
2728 av_free(arg);
2729 return res;
2730 }
2731
set_side_data(HEVCContext * s)2732 static int set_side_data(HEVCContext *s)
2733 {
2734 AVFrame *out = s->ref->frame;
2735 int ret;
2736
2737 if (s->sei.frame_packing.present &&
2738 s->sei.frame_packing.arrangement_type >= 3 &&
2739 s->sei.frame_packing.arrangement_type <= 5 &&
2740 s->sei.frame_packing.content_interpretation_type > 0 &&
2741 s->sei.frame_packing.content_interpretation_type < 3) {
2742 AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2743 if (!stereo)
2744 return AVERROR(ENOMEM);
2745
2746 switch (s->sei.frame_packing.arrangement_type) {
2747 case 3:
2748 if (s->sei.frame_packing.quincunx_subsampling)
2749 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2750 else
2751 stereo->type = AV_STEREO3D_SIDEBYSIDE;
2752 break;
2753 case 4:
2754 stereo->type = AV_STEREO3D_TOPBOTTOM;
2755 break;
2756 case 5:
2757 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2758 break;
2759 }
2760
2761 if (s->sei.frame_packing.content_interpretation_type == 2)
2762 stereo->flags = AV_STEREO3D_FLAG_INVERT;
2763
2764 if (s->sei.frame_packing.arrangement_type == 5) {
2765 if (s->sei.frame_packing.current_frame_is_frame0_flag)
2766 stereo->view = AV_STEREO3D_VIEW_LEFT;
2767 else
2768 stereo->view = AV_STEREO3D_VIEW_RIGHT;
2769 }
2770 }
2771
2772 if (s->sei.display_orientation.present &&
2773 (s->sei.display_orientation.anticlockwise_rotation ||
2774 s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
2775 double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
2776 AVFrameSideData *rotation = av_frame_new_side_data(out,
2777 AV_FRAME_DATA_DISPLAYMATRIX,
2778 sizeof(int32_t) * 9);
2779 if (!rotation)
2780 return AVERROR(ENOMEM);
2781
2782 /* av_display_rotation_set() expects the angle in the clockwise
2783 * direction, hence the first minus.
2784 * The below code applies the flips after the rotation, yet
2785 * the H.2645 specs require flipping to be applied first.
2786 * Because of R O(phi) = O(-phi) R (where R is flipping around
2787 * an arbitatry axis and O(phi) is the proper rotation by phi)
2788 * we can create display matrices as desired by negating
2789 * the degree once for every flip applied. */
2790 angle = -angle * (1 - 2 * !!s->sei.display_orientation.hflip)
2791 * (1 - 2 * !!s->sei.display_orientation.vflip);
2792 av_display_rotation_set((int32_t *)rotation->data, angle);
2793 av_display_matrix_flip((int32_t *)rotation->data,
2794 s->sei.display_orientation.hflip,
2795 s->sei.display_orientation.vflip);
2796 }
2797
2798 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2799 // so the side data persists for the entire coded video sequence.
2800 if (s->sei.mastering_display.present > 0 &&
2801 IS_IRAP(s) && s->no_rasl_output_flag) {
2802 s->sei.mastering_display.present--;
2803 }
2804 if (s->sei.mastering_display.present) {
2805 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
2806 const int mapping[3] = {2, 0, 1};
2807 const int chroma_den = 50000;
2808 const int luma_den = 10000;
2809 int i;
2810 AVMasteringDisplayMetadata *metadata =
2811 av_mastering_display_metadata_create_side_data(out);
2812 if (!metadata)
2813 return AVERROR(ENOMEM);
2814
2815 for (i = 0; i < 3; i++) {
2816 const int j = mapping[i];
2817 metadata->display_primaries[i][0].num = s->sei.mastering_display.display_primaries[j][0];
2818 metadata->display_primaries[i][0].den = chroma_den;
2819 metadata->display_primaries[i][1].num = s->sei.mastering_display.display_primaries[j][1];
2820 metadata->display_primaries[i][1].den = chroma_den;
2821 }
2822 metadata->white_point[0].num = s->sei.mastering_display.white_point[0];
2823 metadata->white_point[0].den = chroma_den;
2824 metadata->white_point[1].num = s->sei.mastering_display.white_point[1];
2825 metadata->white_point[1].den = chroma_den;
2826
2827 metadata->max_luminance.num = s->sei.mastering_display.max_luminance;
2828 metadata->max_luminance.den = luma_den;
2829 metadata->min_luminance.num = s->sei.mastering_display.min_luminance;
2830 metadata->min_luminance.den = luma_den;
2831 metadata->has_luminance = 1;
2832 metadata->has_primaries = 1;
2833
2834 av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
2835 av_log(s->avctx, AV_LOG_DEBUG,
2836 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
2837 av_q2d(metadata->display_primaries[0][0]),
2838 av_q2d(metadata->display_primaries[0][1]),
2839 av_q2d(metadata->display_primaries[1][0]),
2840 av_q2d(metadata->display_primaries[1][1]),
2841 av_q2d(metadata->display_primaries[2][0]),
2842 av_q2d(metadata->display_primaries[2][1]),
2843 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
2844 av_log(s->avctx, AV_LOG_DEBUG,
2845 "min_luminance=%f, max_luminance=%f\n",
2846 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
2847 }
2848 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2849 // so the side data persists for the entire coded video sequence.
2850 if (s->sei.content_light.present > 0 &&
2851 IS_IRAP(s) && s->no_rasl_output_flag) {
2852 s->sei.content_light.present--;
2853 }
2854 if (s->sei.content_light.present) {
2855 AVContentLightMetadata *metadata =
2856 av_content_light_metadata_create_side_data(out);
2857 if (!metadata)
2858 return AVERROR(ENOMEM);
2859 metadata->MaxCLL = s->sei.content_light.max_content_light_level;
2860 metadata->MaxFALL = s->sei.content_light.max_pic_average_light_level;
2861
2862 av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
2863 av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
2864 metadata->MaxCLL, metadata->MaxFALL);
2865 }
2866
2867 if (s->sei.a53_caption.buf_ref) {
2868 HEVCSEIA53Caption *a53 = &s->sei.a53_caption;
2869
2870 AVFrameSideData *sd = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_A53_CC, a53->buf_ref);
2871 if (!sd)
2872 av_buffer_unref(&a53->buf_ref);
2873 a53->buf_ref = NULL;
2874 }
2875
2876 for (int i = 0; i < s->sei.unregistered.nb_buf_ref; i++) {
2877 HEVCSEIUnregistered *unreg = &s->sei.unregistered;
2878
2879 if (unreg->buf_ref[i]) {
2880 AVFrameSideData *sd = av_frame_new_side_data_from_buf(out,
2881 AV_FRAME_DATA_SEI_UNREGISTERED,
2882 unreg->buf_ref[i]);
2883 if (!sd)
2884 av_buffer_unref(&unreg->buf_ref[i]);
2885 unreg->buf_ref[i] = NULL;
2886 }
2887 }
2888 s->sei.unregistered.nb_buf_ref = 0;
2889
2890 if (s->sei.timecode.present) {
2891 uint32_t *tc_sd;
2892 char tcbuf[AV_TIMECODE_STR_SIZE];
2893 AVFrameSideData *tcside = av_frame_new_side_data(out, AV_FRAME_DATA_S12M_TIMECODE,
2894 sizeof(uint32_t) * 4);
2895 if (!tcside)
2896 return AVERROR(ENOMEM);
2897
2898 tc_sd = (uint32_t*)tcside->data;
2899 tc_sd[0] = s->sei.timecode.num_clock_ts;
2900
2901 for (int i = 0; i < tc_sd[0]; i++) {
2902 int drop = s->sei.timecode.cnt_dropped_flag[i];
2903 int hh = s->sei.timecode.hours_value[i];
2904 int mm = s->sei.timecode.minutes_value[i];
2905 int ss = s->sei.timecode.seconds_value[i];
2906 int ff = s->sei.timecode.n_frames[i];
2907
2908 tc_sd[i + 1] = av_timecode_get_smpte(s->avctx->framerate, drop, hh, mm, ss, ff);
2909 av_timecode_make_smpte_tc_string2(tcbuf, s->avctx->framerate, tc_sd[i + 1], 0, 0);
2910 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
2911 }
2912
2913 s->sei.timecode.num_clock_ts = 0;
2914 }
2915
2916 if (s->sei.film_grain_characteristics.present) {
2917 HEVCSEIFilmGrainCharacteristics *fgc = &s->sei.film_grain_characteristics;
2918 AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(out);
2919 if (!fgp)
2920 return AVERROR(ENOMEM);
2921
2922 fgp->type = AV_FILM_GRAIN_PARAMS_H274;
2923 fgp->seed = s->ref->poc; /* no poc_offset in HEVC */
2924
2925 fgp->codec.h274.model_id = fgc->model_id;
2926 if (fgc->separate_colour_description_present_flag) {
2927 fgp->codec.h274.bit_depth_luma = fgc->bit_depth_luma;
2928 fgp->codec.h274.bit_depth_chroma = fgc->bit_depth_chroma;
2929 fgp->codec.h274.color_range = fgc->full_range + 1;
2930 fgp->codec.h274.color_primaries = fgc->color_primaries;
2931 fgp->codec.h274.color_trc = fgc->transfer_characteristics;
2932 fgp->codec.h274.color_space = fgc->matrix_coeffs;
2933 } else {
2934 const HEVCSPS *sps = s->ps.sps;
2935 const VUI *vui = &sps->vui;
2936 fgp->codec.h274.bit_depth_luma = sps->bit_depth;
2937 fgp->codec.h274.bit_depth_chroma = sps->bit_depth_chroma;
2938 if (vui->video_signal_type_present_flag)
2939 fgp->codec.h274.color_range = vui->video_full_range_flag + 1;
2940 else
2941 fgp->codec.h274.color_range = AVCOL_RANGE_UNSPECIFIED;
2942 if (vui->colour_description_present_flag) {
2943 fgp->codec.h274.color_primaries = vui->colour_primaries;
2944 fgp->codec.h274.color_trc = vui->transfer_characteristic;
2945 fgp->codec.h274.color_space = vui->matrix_coeffs;
2946 } else {
2947 fgp->codec.h274.color_primaries = AVCOL_PRI_UNSPECIFIED;
2948 fgp->codec.h274.color_trc = AVCOL_TRC_UNSPECIFIED;
2949 fgp->codec.h274.color_space = AVCOL_SPC_UNSPECIFIED;
2950 }
2951 }
2952 fgp->codec.h274.blending_mode_id = fgc->blending_mode_id;
2953 fgp->codec.h274.log2_scale_factor = fgc->log2_scale_factor;
2954
2955 memcpy(&fgp->codec.h274.component_model_present, &fgc->comp_model_present_flag,
2956 sizeof(fgp->codec.h274.component_model_present));
2957 memcpy(&fgp->codec.h274.num_intensity_intervals, &fgc->num_intensity_intervals,
2958 sizeof(fgp->codec.h274.num_intensity_intervals));
2959 memcpy(&fgp->codec.h274.num_model_values, &fgc->num_model_values,
2960 sizeof(fgp->codec.h274.num_model_values));
2961 memcpy(&fgp->codec.h274.intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
2962 sizeof(fgp->codec.h274.intensity_interval_lower_bound));
2963 memcpy(&fgp->codec.h274.intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
2964 sizeof(fgp->codec.h274.intensity_interval_upper_bound));
2965 memcpy(&fgp->codec.h274.comp_model_value, &fgc->comp_model_value,
2966 sizeof(fgp->codec.h274.comp_model_value));
2967
2968 fgc->present = fgc->persistence_flag;
2969 }
2970
2971 if (s->sei.dynamic_hdr_plus.info) {
2972 AVBufferRef *info_ref = av_buffer_ref(s->sei.dynamic_hdr_plus.info);
2973 if (!info_ref)
2974 return AVERROR(ENOMEM);
2975
2976 if (!av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DYNAMIC_HDR_PLUS, info_ref)) {
2977 av_buffer_unref(&info_ref);
2978 return AVERROR(ENOMEM);
2979 }
2980 }
2981
2982 if (s->rpu_buf) {
2983 AVFrameSideData *rpu = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DOVI_RPU_BUFFER, s->rpu_buf);
2984 if (!rpu)
2985 return AVERROR(ENOMEM);
2986
2987 s->rpu_buf = NULL;
2988 }
2989
2990 if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
2991 return ret;
2992
2993 if (s->sei.dynamic_hdr_vivid.info) {
2994 AVBufferRef *info_ref = av_buffer_ref(s->sei.dynamic_hdr_vivid.info);
2995 if (!info_ref)
2996 return AVERROR(ENOMEM);
2997
2998 if (!av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DYNAMIC_HDR_VIVID, info_ref)) {
2999 av_buffer_unref(&info_ref);
3000 return AVERROR(ENOMEM);
3001 }
3002 }
3003
3004 return 0;
3005 }
3006
hevc_frame_start(HEVCContext * s)3007 static int hevc_frame_start(HEVCContext *s)
3008 {
3009 HEVCLocalContext *lc = s->HEVClc;
3010 int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
3011 ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
3012 int ret;
3013
3014 memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
3015 memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
3016 memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
3017 memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
3018 memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
3019
3020 s->is_decoded = 0;
3021 s->first_nal_type = s->nal_unit_type;
3022
3023 s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
3024
3025 if (s->ps.pps->tiles_enabled_flag)
3026 lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
3027
3028 ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
3029 if (ret < 0)
3030 goto fail;
3031
3032 ret = ff_hevc_frame_rps(s);
3033 if (ret < 0) {
3034 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
3035 goto fail;
3036 }
3037
3038 s->ref->frame->key_frame = IS_IRAP(s);
3039
3040 s->ref->needs_fg = s->sei.film_grain_characteristics.present &&
3041 !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
3042 !s->avctx->hwaccel;
3043
3044 if (s->ref->needs_fg) {
3045 s->ref->frame_grain->format = s->ref->frame->format;
3046 s->ref->frame_grain->width = s->ref->frame->width;
3047 s->ref->frame_grain->height = s->ref->frame->height;
3048 if ((ret = ff_thread_get_buffer(s->avctx, s->ref->frame_grain, 0)) < 0)
3049 goto fail;
3050 }
3051
3052 ret = set_side_data(s);
3053 if (ret < 0)
3054 goto fail;
3055
3056 s->frame->pict_type = 3 - s->sh.slice_type;
3057
3058 if (!IS_IRAP(s))
3059 ff_hevc_bump_frame(s);
3060
3061 av_frame_unref(s->output_frame);
3062 ret = ff_hevc_output_frame(s, s->output_frame, 0);
3063 if (ret < 0)
3064 goto fail;
3065
3066 if (!s->avctx->hwaccel)
3067 ff_thread_finish_setup(s->avctx);
3068
3069 return 0;
3070
3071 fail:
3072 if (s->ref)
3073 ff_hevc_unref_frame(s, s->ref, ~0);
3074 s->ref = NULL;
3075 return ret;
3076 }
3077
hevc_frame_end(HEVCContext * s)3078 static int hevc_frame_end(HEVCContext *s)
3079 {
3080 HEVCFrame *out = s->ref;
3081 const AVFrameSideData *sd;
3082 int ret;
3083
3084 if (out->needs_fg) {
3085 sd = av_frame_get_side_data(out->frame, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
3086 av_assert0(out->frame_grain->buf[0] && sd);
3087 ret = ff_h274_apply_film_grain(out->frame_grain, out->frame, &s->h274db,
3088 (AVFilmGrainParams *) sd->data);
3089
3090 if (ret < 0) {
3091 av_log(s->avctx, AV_LOG_WARNING, "Failed synthesizing film "
3092 "grain, ignoring: %s\n", av_err2str(ret));
3093 out->needs_fg = 0;
3094 }
3095 }
3096
3097 return 0;
3098 }
3099
decode_nal_unit(HEVCContext * s,const H2645NAL * nal)3100 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
3101 {
3102 HEVCLocalContext *lc = s->HEVClc;
3103 GetBitContext *gb = &lc->gb;
3104 int ctb_addr_ts, ret;
3105
3106 *gb = nal->gb;
3107 s->nal_unit_type = nal->type;
3108 s->temporal_id = nal->temporal_id;
3109
3110 switch (s->nal_unit_type) {
3111 case HEVC_NAL_VPS:
3112 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
3113 ret = s->avctx->hwaccel->decode_params(s->avctx,
3114 nal->type,
3115 nal->raw_data,
3116 nal->raw_size);
3117 if (ret < 0)
3118 goto fail;
3119 }
3120 ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
3121 if (ret < 0)
3122 goto fail;
3123 break;
3124 case HEVC_NAL_SPS:
3125 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
3126 ret = s->avctx->hwaccel->decode_params(s->avctx,
3127 nal->type,
3128 nal->raw_data,
3129 nal->raw_size);
3130 if (ret < 0)
3131 goto fail;
3132 }
3133 ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
3134 s->apply_defdispwin);
3135 if (ret < 0)
3136 goto fail;
3137 break;
3138 case HEVC_NAL_PPS:
3139 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
3140 ret = s->avctx->hwaccel->decode_params(s->avctx,
3141 nal->type,
3142 nal->raw_data,
3143 nal->raw_size);
3144 if (ret < 0)
3145 goto fail;
3146 }
3147 ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
3148 if (ret < 0)
3149 goto fail;
3150 break;
3151 case HEVC_NAL_SEI_PREFIX:
3152 case HEVC_NAL_SEI_SUFFIX:
3153 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
3154 ret = s->avctx->hwaccel->decode_params(s->avctx,
3155 nal->type,
3156 nal->raw_data,
3157 nal->raw_size);
3158 if (ret < 0)
3159 goto fail;
3160 }
3161 ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
3162 if (ret < 0)
3163 goto fail;
3164 break;
3165 case HEVC_NAL_TRAIL_R:
3166 case HEVC_NAL_TRAIL_N:
3167 case HEVC_NAL_TSA_N:
3168 case HEVC_NAL_TSA_R:
3169 case HEVC_NAL_STSA_N:
3170 case HEVC_NAL_STSA_R:
3171 case HEVC_NAL_BLA_W_LP:
3172 case HEVC_NAL_BLA_W_RADL:
3173 case HEVC_NAL_BLA_N_LP:
3174 case HEVC_NAL_IDR_W_RADL:
3175 case HEVC_NAL_IDR_N_LP:
3176 case HEVC_NAL_CRA_NUT:
3177 case HEVC_NAL_RADL_N:
3178 case HEVC_NAL_RADL_R:
3179 case HEVC_NAL_RASL_N:
3180 case HEVC_NAL_RASL_R:
3181 ret = hls_slice_header(s);
3182 if (ret < 0)
3183 return ret;
3184 if (ret == 1) {
3185 ret = AVERROR_INVALIDDATA;
3186 goto fail;
3187 }
3188
3189
3190 if (
3191 (s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
3192 (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
3193 (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s))) {
3194 break;
3195 }
3196
3197 if (s->sh.first_slice_in_pic_flag) {
3198 if (s->max_ra == INT_MAX) {
3199 if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
3200 s->max_ra = s->poc;
3201 } else {
3202 if (IS_IDR(s))
3203 s->max_ra = INT_MIN;
3204 }
3205 }
3206
3207 if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
3208 s->poc <= s->max_ra) {
3209 s->is_decoded = 0;
3210 break;
3211 } else {
3212 if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
3213 s->max_ra = INT_MIN;
3214 }
3215
3216 s->overlap ++;
3217 ret = hevc_frame_start(s);
3218 if (ret < 0)
3219 return ret;
3220 } else if (!s->ref) {
3221 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
3222 goto fail;
3223 }
3224
3225 if (s->nal_unit_type != s->first_nal_type) {
3226 av_log(s->avctx, AV_LOG_ERROR,
3227 "Non-matching NAL types of the VCL NALUs: %d %d\n",
3228 s->first_nal_type, s->nal_unit_type);
3229 return AVERROR_INVALIDDATA;
3230 }
3231
3232 if (!s->sh.dependent_slice_segment_flag &&
3233 s->sh.slice_type != HEVC_SLICE_I) {
3234 ret = ff_hevc_slice_rpl(s);
3235 if (ret < 0) {
3236 av_log(s->avctx, AV_LOG_WARNING,
3237 "Error constructing the reference lists for the current slice.\n");
3238 goto fail;
3239 }
3240 }
3241
3242 if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
3243 ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
3244 if (ret < 0)
3245 goto fail;
3246 }
3247
3248 if (s->avctx->hwaccel) {
3249 ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
3250 if (ret < 0)
3251 goto fail;
3252 } else {
3253 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
3254 ctb_addr_ts = hls_slice_data_wpp(s, nal);
3255 else
3256 ctb_addr_ts = hls_slice_data(s);
3257 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
3258 ret = hevc_frame_end(s);
3259 if (ret < 0)
3260 goto fail;
3261 s->is_decoded = 1;
3262 }
3263
3264 if (ctb_addr_ts < 0) {
3265 ret = ctb_addr_ts;
3266 goto fail;
3267 }
3268 }
3269 break;
3270 case HEVC_NAL_EOS_NUT:
3271 case HEVC_NAL_EOB_NUT:
3272 s->seq_decode = (s->seq_decode + 1) & 0xff;
3273 s->max_ra = INT_MAX;
3274 break;
3275 case HEVC_NAL_AUD:
3276 case HEVC_NAL_FD_NUT:
3277 case HEVC_NAL_UNSPEC62:
3278 break;
3279 default:
3280 av_log(s->avctx, AV_LOG_INFO,
3281 "Skipping NAL unit %d\n", s->nal_unit_type);
3282 }
3283
3284 return 0;
3285 fail:
3286 if (s->avctx->err_recognition & AV_EF_EXPLODE)
3287 return ret;
3288 return 0;
3289 }
3290
decode_nal_units(HEVCContext * s,const uint8_t * buf,int length)3291 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3292 {
3293 int i, ret = 0;
3294 int eos_at_start = 1;
3295
3296 s->ref = NULL;
3297 s->last_eos = s->eos;
3298 s->eos = 0;
3299 s->overlap = 0;
3300
3301 /* split the input packet into NAL units, so we know the upper bound on the
3302 * number of slices in the frame */
3303 ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
3304 s->nal_length_size, s->avctx->codec_id, 1, 0);
3305 if (ret < 0) {
3306 av_log(s->avctx, AV_LOG_ERROR,
3307 "Error splitting the input into NAL units.\n");
3308 return ret;
3309 }
3310
3311 for (i = 0; i < s->pkt.nb_nals; i++) {
3312 if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3313 s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3314 if (eos_at_start) {
3315 s->last_eos = 1;
3316 } else {
3317 s->eos = 1;
3318 }
3319 } else {
3320 eos_at_start = 0;
3321 }
3322 }
3323
3324 /*
3325 * Check for RPU delimiter.
3326 *
3327 * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
3328 *
3329 * We have to do this check here an create the rpu buffer, since RPUs are appended
3330 * to the end of an AU; they are the last non-EOB/EOS NAL in the AU.
3331 */
3332 if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
3333 s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
3334 && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
3335 H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
3336 if (s->rpu_buf) {
3337 av_buffer_unref(&s->rpu_buf);
3338 av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
3339 }
3340
3341 s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
3342 if (!s->rpu_buf)
3343 return AVERROR(ENOMEM);
3344 memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
3345
3346 ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2);
3347 if (ret < 0) {
3348 av_buffer_unref(&s->rpu_buf);
3349 av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
3350 /* ignore */
3351 }
3352 }
3353
3354 /* decode the NAL units */
3355 for (i = 0; i < s->pkt.nb_nals; i++) {
3356 H2645NAL *nal = &s->pkt.nals[i];
3357
3358 if (s->avctx->skip_frame >= AVDISCARD_ALL ||
3359 (s->avctx->skip_frame >= AVDISCARD_NONREF
3360 && ff_hevc_nal_is_nonref(nal->type)) || nal->nuh_layer_id > 0)
3361 continue;
3362
3363 ret = decode_nal_unit(s, nal);
3364 if (ret >= 0 && s->overlap > 2)
3365 ret = AVERROR_INVALIDDATA;
3366 if (ret < 0) {
3367 av_log(s->avctx, AV_LOG_WARNING,
3368 "Error parsing NAL unit #%d.\n", i);
3369 goto fail;
3370 }
3371 }
3372
3373 fail:
3374 if (s->ref && s->threads_type == FF_THREAD_FRAME)
3375 ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
3376
3377 return ret;
3378 }
3379
print_md5(void * log_ctx,int level,uint8_t md5[16])3380 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
3381 {
3382 int i;
3383 for (i = 0; i < 16; i++)
3384 av_log(log_ctx, level, "%02"PRIx8, md5[i]);
3385 }
3386
verify_md5(HEVCContext * s,AVFrame * frame)3387 static int verify_md5(HEVCContext *s, AVFrame *frame)
3388 {
3389 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3390 int pixel_shift;
3391 int i, j;
3392
3393 if (!desc)
3394 return AVERROR(EINVAL);
3395
3396 pixel_shift = desc->comp[0].depth > 8;
3397
3398 av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
3399 s->poc);
3400
3401 /* the checksums are LE, so we have to byteswap for >8bpp formats
3402 * on BE arches */
3403 #if HAVE_BIGENDIAN
3404 if (pixel_shift && !s->checksum_buf) {
3405 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3406 FFMAX3(frame->linesize[0], frame->linesize[1],
3407 frame->linesize[2]));
3408 if (!s->checksum_buf)
3409 return AVERROR(ENOMEM);
3410 }
3411 #endif
3412
3413 for (i = 0; frame->data[i]; i++) {
3414 int width = s->avctx->coded_width;
3415 int height = s->avctx->coded_height;
3416 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3417 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3418 uint8_t md5[16];
3419
3420 av_md5_init(s->md5_ctx);
3421 for (j = 0; j < h; j++) {
3422 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3423 #if HAVE_BIGENDIAN
3424 if (pixel_shift) {
3425 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3426 (const uint16_t *) src, w);
3427 src = s->checksum_buf;
3428 }
3429 #endif
3430 av_md5_update(s->md5_ctx, src, w << pixel_shift);
3431 }
3432 av_md5_final(s->md5_ctx, md5);
3433
3434 if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3435 av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
3436 print_md5(s->avctx, AV_LOG_DEBUG, md5);
3437 av_log (s->avctx, AV_LOG_DEBUG, "; ");
3438 } else {
3439 av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
3440 print_md5(s->avctx, AV_LOG_ERROR, md5);
3441 av_log (s->avctx, AV_LOG_ERROR, " != ");
3442 print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
3443 av_log (s->avctx, AV_LOG_ERROR, "\n");
3444 return AVERROR_INVALIDDATA;
3445 }
3446 }
3447
3448 av_log(s->avctx, AV_LOG_DEBUG, "\n");
3449
3450 return 0;
3451 }
3452
hevc_decode_extradata(HEVCContext * s,uint8_t * buf,int length,int first)3453 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3454 {
3455 int ret, i;
3456
3457 ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3458 &s->nal_length_size, s->avctx->err_recognition,
3459 s->apply_defdispwin, s->avctx);
3460 if (ret < 0)
3461 return ret;
3462
3463 /* export stream parameters from the first SPS */
3464 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3465 if (first && s->ps.sps_list[i]) {
3466 const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3467 export_stream_params(s, sps);
3468 break;
3469 }
3470 }
3471
3472 /* export stream parameters from SEI */
3473 ret = export_stream_params_from_sei(s);
3474 if (ret < 0)
3475 return ret;
3476
3477 return 0;
3478 }
3479
hevc_decode_frame(AVCodecContext * avctx,AVFrame * rframe,int * got_output,AVPacket * avpkt)3480 static int hevc_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
3481 int *got_output, AVPacket *avpkt)
3482 {
3483 int ret;
3484 uint8_t *sd;
3485 size_t sd_size;
3486 HEVCContext *s = avctx->priv_data;
3487
3488 if (!avpkt->size) {
3489 ret = ff_hevc_output_frame(s, rframe, 1);
3490 if (ret < 0)
3491 return ret;
3492
3493 *got_output = ret;
3494 return 0;
3495 }
3496
3497 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
3498 if (sd && sd_size > 0) {
3499 ret = hevc_decode_extradata(s, sd, sd_size, 0);
3500 if (ret < 0)
3501 return ret;
3502 }
3503
3504 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
3505 if (sd && sd_size > 0)
3506 ff_dovi_update_cfg(&s->dovi_ctx, (AVDOVIDecoderConfigurationRecord *) sd);
3507
3508 s->ref = NULL;
3509 ret = decode_nal_units(s, avpkt->data, avpkt->size);
3510 if (ret < 0)
3511 return ret;
3512
3513 if (avctx->hwaccel) {
3514 if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
3515 av_log(avctx, AV_LOG_ERROR,
3516 "hardware accelerator failed to decode picture\n");
3517 ff_hevc_unref_frame(s, s->ref, ~0);
3518 return ret;
3519 }
3520 } else {
3521 /* verify the SEI checksum */
3522 if (avctx->err_recognition & AV_EF_CRCCHECK && s->ref && s->is_decoded &&
3523 s->sei.picture_hash.is_md5) {
3524 ret = verify_md5(s, s->ref->frame);
3525 if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
3526 ff_hevc_unref_frame(s, s->ref, ~0);
3527 return ret;
3528 }
3529 }
3530 }
3531 s->sei.picture_hash.is_md5 = 0;
3532
3533 if (s->is_decoded) {
3534 av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3535 s->is_decoded = 0;
3536 }
3537
3538 if (s->output_frame->buf[0]) {
3539 av_frame_move_ref(rframe, s->output_frame);
3540 *got_output = 1;
3541 }
3542
3543 return avpkt->size;
3544 }
3545
hevc_ref_frame(HEVCContext * s,HEVCFrame * dst,HEVCFrame * src)3546 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
3547 {
3548 int ret;
3549
3550 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
3551 if (ret < 0)
3552 return ret;
3553
3554 if (src->needs_fg) {
3555 ret = av_frame_ref(dst->frame_grain, src->frame_grain);
3556 if (ret < 0)
3557 return ret;
3558 dst->needs_fg = 1;
3559 }
3560
3561 dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
3562 if (!dst->tab_mvf_buf)
3563 goto fail;
3564 dst->tab_mvf = src->tab_mvf;
3565
3566 dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
3567 if (!dst->rpl_tab_buf)
3568 goto fail;
3569 dst->rpl_tab = src->rpl_tab;
3570
3571 dst->rpl_buf = av_buffer_ref(src->rpl_buf);
3572 if (!dst->rpl_buf)
3573 goto fail;
3574
3575 dst->poc = src->poc;
3576 dst->ctb_count = src->ctb_count;
3577 dst->flags = src->flags;
3578 dst->sequence = src->sequence;
3579
3580 if (src->hwaccel_picture_private) {
3581 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
3582 if (!dst->hwaccel_priv_buf)
3583 goto fail;
3584 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
3585 }
3586
3587 return 0;
3588 fail:
3589 ff_hevc_unref_frame(s, dst, ~0);
3590 return AVERROR(ENOMEM);
3591 }
3592
hevc_decode_free(AVCodecContext * avctx)3593 static av_cold int hevc_decode_free(AVCodecContext *avctx)
3594 {
3595 HEVCContext *s = avctx->priv_data;
3596 int i;
3597
3598 pic_arrays_free(s);
3599
3600 ff_dovi_ctx_unref(&s->dovi_ctx);
3601 av_buffer_unref(&s->rpu_buf);
3602
3603 av_freep(&s->md5_ctx);
3604
3605 av_freep(&s->cabac_state);
3606
3607 for (i = 0; i < 3; i++) {
3608 av_freep(&s->sao_pixel_buffer_h[i]);
3609 av_freep(&s->sao_pixel_buffer_v[i]);
3610 }
3611 av_frame_free(&s->output_frame);
3612
3613 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3614 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3615 av_frame_free(&s->DPB[i].frame);
3616 av_frame_free(&s->DPB[i].frame_grain);
3617 }
3618
3619 ff_hevc_ps_uninit(&s->ps);
3620
3621 av_freep(&s->sh.entry_point_offset);
3622 av_freep(&s->sh.offset);
3623 av_freep(&s->sh.size);
3624
3625 if (s->HEVClcList && s->sList) {
3626 for (i = 1; i < s->threads_number; i++) {
3627 av_freep(&s->HEVClcList[i]);
3628 av_freep(&s->sList[i]);
3629 }
3630 }
3631 av_freep(&s->HEVClc);
3632 av_freep(&s->HEVClcList);
3633 av_freep(&s->sList);
3634
3635 ff_h2645_packet_uninit(&s->pkt);
3636
3637 ff_hevc_reset_sei(&s->sei);
3638
3639 return 0;
3640 }
3641
hevc_init_context(AVCodecContext * avctx)3642 static av_cold int hevc_init_context(AVCodecContext *avctx)
3643 {
3644 HEVCContext *s = avctx->priv_data;
3645 int i;
3646
3647 s->avctx = avctx;
3648
3649 s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3650 s->HEVClcList = av_mallocz(sizeof(HEVCLocalContext*) * s->threads_number);
3651 s->sList = av_mallocz(sizeof(HEVCContext*) * s->threads_number);
3652 if (!s->HEVClc || !s->HEVClcList || !s->sList)
3653 return AVERROR(ENOMEM);
3654 s->HEVClcList[0] = s->HEVClc;
3655 s->sList[0] = s;
3656
3657 s->cabac_state = av_malloc(HEVC_CONTEXTS);
3658 if (!s->cabac_state)
3659 return AVERROR(ENOMEM);
3660
3661 s->output_frame = av_frame_alloc();
3662 if (!s->output_frame)
3663 return AVERROR(ENOMEM);
3664
3665 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3666 s->DPB[i].frame = av_frame_alloc();
3667 if (!s->DPB[i].frame)
3668 return AVERROR(ENOMEM);
3669 s->DPB[i].tf.f = s->DPB[i].frame;
3670
3671 s->DPB[i].frame_grain = av_frame_alloc();
3672 if (!s->DPB[i].frame_grain)
3673 return AVERROR(ENOMEM);
3674 }
3675
3676 s->max_ra = INT_MAX;
3677
3678 s->md5_ctx = av_md5_alloc();
3679 if (!s->md5_ctx)
3680 return AVERROR(ENOMEM);
3681
3682 ff_bswapdsp_init(&s->bdsp);
3683
3684 s->dovi_ctx.logctx = avctx;
3685 s->eos = 0;
3686
3687 ff_hevc_reset_sei(&s->sei);
3688
3689 return 0;
3690 }
3691
3692 #if HAVE_THREADS
hevc_update_thread_context(AVCodecContext * dst,const AVCodecContext * src)3693 static int hevc_update_thread_context(AVCodecContext *dst,
3694 const AVCodecContext *src)
3695 {
3696 HEVCContext *s = dst->priv_data;
3697 HEVCContext *s0 = src->priv_data;
3698 int i, ret;
3699
3700 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3701 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3702 if (s0->DPB[i].frame->buf[0]) {
3703 ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
3704 if (ret < 0)
3705 return ret;
3706 }
3707 }
3708
3709 if (s->ps.sps != s0->ps.sps)
3710 s->ps.sps = NULL;
3711 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
3712 ret = av_buffer_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
3713 if (ret < 0)
3714 return ret;
3715 }
3716
3717 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3718 ret = av_buffer_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
3719 if (ret < 0)
3720 return ret;
3721 }
3722
3723 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
3724 ret = av_buffer_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
3725 if (ret < 0)
3726 return ret;
3727 }
3728
3729 if (s->ps.sps != s0->ps.sps)
3730 if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3731 return ret;
3732
3733 s->seq_decode = s0->seq_decode;
3734 s->seq_output = s0->seq_output;
3735 s->pocTid0 = s0->pocTid0;
3736 s->max_ra = s0->max_ra;
3737 s->eos = s0->eos;
3738 s->no_rasl_output_flag = s0->no_rasl_output_flag;
3739
3740 s->is_nalff = s0->is_nalff;
3741 s->nal_length_size = s0->nal_length_size;
3742
3743 s->threads_number = s0->threads_number;
3744 s->threads_type = s0->threads_type;
3745
3746 if (s0->eos) {
3747 s->seq_decode = (s->seq_decode + 1) & 0xff;
3748 s->max_ra = INT_MAX;
3749 }
3750
3751 ret = av_buffer_replace(&s->sei.a53_caption.buf_ref, s0->sei.a53_caption.buf_ref);
3752 if (ret < 0)
3753 return ret;
3754
3755 for (i = 0; i < s->sei.unregistered.nb_buf_ref; i++)
3756 av_buffer_unref(&s->sei.unregistered.buf_ref[i]);
3757 s->sei.unregistered.nb_buf_ref = 0;
3758
3759 if (s0->sei.unregistered.nb_buf_ref) {
3760 ret = av_reallocp_array(&s->sei.unregistered.buf_ref,
3761 s0->sei.unregistered.nb_buf_ref,
3762 sizeof(*s->sei.unregistered.buf_ref));
3763 if (ret < 0)
3764 return ret;
3765
3766 for (i = 0; i < s0->sei.unregistered.nb_buf_ref; i++) {
3767 s->sei.unregistered.buf_ref[i] = av_buffer_ref(s0->sei.unregistered.buf_ref[i]);
3768 if (!s->sei.unregistered.buf_ref[i])
3769 return AVERROR(ENOMEM);
3770 s->sei.unregistered.nb_buf_ref++;
3771 }
3772 }
3773
3774 ret = av_buffer_replace(&s->sei.dynamic_hdr_plus.info, s0->sei.dynamic_hdr_plus.info);
3775 if (ret < 0)
3776 return ret;
3777
3778 ret = av_buffer_replace(&s->rpu_buf, s0->rpu_buf);
3779 if (ret < 0)
3780 return ret;
3781
3782 ret = ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
3783 if (ret < 0)
3784 return ret;
3785
3786 ret = av_buffer_replace(&s->sei.dynamic_hdr_vivid.info, s0->sei.dynamic_hdr_vivid.info);
3787 if (ret < 0)
3788 return ret;
3789
3790 s->sei.frame_packing = s0->sei.frame_packing;
3791 s->sei.display_orientation = s0->sei.display_orientation;
3792 s->sei.mastering_display = s0->sei.mastering_display;
3793 s->sei.content_light = s0->sei.content_light;
3794 s->sei.alternative_transfer = s0->sei.alternative_transfer;
3795
3796 ret = export_stream_params_from_sei(s);
3797 if (ret < 0)
3798 return ret;
3799
3800 return 0;
3801 }
3802 #endif
3803
hevc_decode_init(AVCodecContext * avctx)3804 static av_cold int hevc_decode_init(AVCodecContext *avctx)
3805 {
3806 HEVCContext *s = avctx->priv_data;
3807 int ret;
3808
3809 if (avctx->active_thread_type & FF_THREAD_SLICE) {
3810 s->threads_number = avctx->thread_count;
3811 ret = ff_slice_thread_init_progress(avctx);
3812 if (ret < 0)
3813 return ret;
3814 } else
3815 s->threads_number = 1;
3816
3817 if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3818 s->threads_type = FF_THREAD_FRAME;
3819 else
3820 s->threads_type = FF_THREAD_SLICE;
3821
3822 ret = hevc_init_context(avctx);
3823 if (ret < 0)
3824 return ret;
3825
3826 s->enable_parallel_tiles = 0;
3827 s->sei.picture_timing.picture_struct = 0;
3828 s->eos = 1;
3829
3830 atomic_init(&s->wpp_err, 0);
3831
3832 if (!avctx->internal->is_copy) {
3833 if (avctx->extradata_size > 0 && avctx->extradata) {
3834 ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
3835 if (ret < 0) {
3836 return ret;
3837 }
3838 }
3839 }
3840
3841 return 0;
3842 }
3843
hevc_decode_flush(AVCodecContext * avctx)3844 static void hevc_decode_flush(AVCodecContext *avctx)
3845 {
3846 HEVCContext *s = avctx->priv_data;
3847 ff_hevc_flush_dpb(s);
3848 ff_hevc_reset_sei(&s->sei);
3849 ff_dovi_ctx_flush(&s->dovi_ctx);
3850 av_buffer_unref(&s->rpu_buf);
3851 s->max_ra = INT_MAX;
3852 s->eos = 1;
3853 }
3854
3855 #define OFFSET(x) offsetof(HEVCContext, x)
3856 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3857
3858 static const AVOption options[] = {
3859 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3860 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3861 { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3862 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3863 { NULL },
3864 };
3865
3866 static const AVClass hevc_decoder_class = {
3867 .class_name = "HEVC decoder",
3868 .item_name = av_default_item_name,
3869 .option = options,
3870 .version = LIBAVUTIL_VERSION_INT,
3871 };
3872
3873 const FFCodec ff_hevc_decoder = {
3874 .p.name = "hevc",
3875 .p.long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3876 .p.type = AVMEDIA_TYPE_VIDEO,
3877 .p.id = AV_CODEC_ID_HEVC,
3878 .priv_data_size = sizeof(HEVCContext),
3879 .p.priv_class = &hevc_decoder_class,
3880 .init = hevc_decode_init,
3881 .close = hevc_decode_free,
3882 FF_CODEC_DECODE_CB(hevc_decode_frame),
3883 .flush = hevc_decode_flush,
3884 .update_thread_context = ONLY_IF_THREADS_ENABLED(hevc_update_thread_context),
3885 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3886 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
3887 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING |
3888 FF_CODEC_CAP_ALLOCATE_PROGRESS | FF_CODEC_CAP_INIT_CLEANUP,
3889 .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
3890 .hw_configs = (const AVCodecHWConfigInternal *const []) {
3891 #if CONFIG_HEVC_DXVA2_HWACCEL
3892 HWACCEL_DXVA2(hevc),
3893 #endif
3894 #if CONFIG_HEVC_D3D11VA_HWACCEL
3895 HWACCEL_D3D11VA(hevc),
3896 #endif
3897 #if CONFIG_HEVC_D3D11VA2_HWACCEL
3898 HWACCEL_D3D11VA2(hevc),
3899 #endif
3900 #if CONFIG_HEVC_NVDEC_HWACCEL
3901 HWACCEL_NVDEC(hevc),
3902 #endif
3903 #if CONFIG_HEVC_VAAPI_HWACCEL
3904 HWACCEL_VAAPI(hevc),
3905 #endif
3906 #if CONFIG_HEVC_VDPAU_HWACCEL
3907 HWACCEL_VDPAU(hevc),
3908 #endif
3909 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
3910 HWACCEL_VIDEOTOOLBOX(hevc),
3911 #endif
3912 NULL
3913 },
3914 };
3915