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