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