FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hevc.c
Go to the documentation of this file.
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/internal.h"
30 #include "libavutil/md5.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 
34 #include "bytestream.h"
35 #include "cabac_functions.h"
36 #include "dsputil.h"
37 #include "golomb.h"
38 #include "hevc.h"
39 
40 const uint8_t ff_hevc_qpel_extra_before[4] = { 0, 3, 3, 2 };
41 const uint8_t ff_hevc_qpel_extra_after[4] = { 0, 3, 4, 4 };
42 const uint8_t ff_hevc_qpel_extra[4] = { 0, 6, 7, 6 };
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() */
55 {
56  av_freep(&s->sao);
57  av_freep(&s->deblock);
59 
60  av_freep(&s->skip_flag);
62 
63  av_freep(&s->tab_ipm);
64  av_freep(&s->cbf_luma);
65  av_freep(&s->is_pcm);
66 
67  av_freep(&s->qp_y_tab);
70 
72  av_freep(&s->vertical_bs);
73 
75  av_freep(&s->sh.size);
76  av_freep(&s->sh.offset);
77 
80 }
81 
82 /* allocate arrays that depend on frame dimensions */
84 {
85  int log2_min_cb_size = s->sps->log2_min_cb_size;
86  int width = s->sps->width;
87  int height = s->sps->height;
88  int pic_size = width * height;
89  int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
90  ((height >> log2_min_cb_size) + 1);
91  int ctb_count = s->sps->ctb_width * s->sps->ctb_height;
92  int min_pu_width = width >> s->sps->log2_min_pu_size;
93  int pic_height_in_min_pu = height >> s->sps->log2_min_pu_size;
94  int pic_size_in_min_pu = min_pu_width * pic_height_in_min_pu;
95  int pic_width_in_min_tu = width >> s->sps->log2_min_tb_size;
96  int pic_height_in_min_tu = height >> s->sps->log2_min_tb_size;
97 
98  s->bs_width = width >> 3;
99  s->bs_height = height >> 3;
100 
101  s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
102  s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
103  s->split_cu_flag = av_malloc(pic_size);
104  if (!s->sao || !s->deblock || !s->split_cu_flag)
105  goto fail;
106 
107  s->skip_flag = av_malloc(pic_size_in_ctb);
109  if (!s->skip_flag || !s->tab_ct_depth)
110  goto fail;
111 
112  s->tab_ipm = av_mallocz(pic_size_in_min_pu);
113  s->cbf_luma = av_malloc(pic_width_in_min_tu * pic_height_in_min_tu);
114  s->is_pcm = av_malloc(pic_size_in_min_pu);
115  if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
116  goto fail;
117 
118  s->filter_slice_edges = av_malloc(ctb_count);
119  s->tab_slice_address = av_malloc(pic_size_in_ctb * sizeof(*s->tab_slice_address));
120  s->qp_y_tab = av_malloc(pic_size_in_ctb * sizeof(*s->qp_y_tab));
121  if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
122  goto fail;
123 
124  s->horizontal_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
125  s->vertical_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
126  if (!s->horizontal_bs || !s->vertical_bs)
127  goto fail;
128 
129  s->tab_mvf_pool = av_buffer_pool_init(pic_size_in_min_pu * sizeof(MvField),
131  s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
133  if (!s->tab_mvf_pool || !s->rpl_tab_pool)
134  goto fail;
135 
136  return 0;
137 fail:
138  pic_arrays_free(s);
139  return AVERROR(ENOMEM);
140 }
141 
143 {
144  int i = 0;
145  int j = 0;
146  uint8_t luma_weight_l0_flag[16];
147  uint8_t chroma_weight_l0_flag[16];
148  uint8_t luma_weight_l1_flag[16];
149  uint8_t chroma_weight_l1_flag[16];
150 
152  if (s->sps->chroma_format_idc != 0) {
153  int delta = get_se_golomb(gb);
155  }
156 
157  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
158  luma_weight_l0_flag[i] = get_bits1(gb);
159  if (!luma_weight_l0_flag[i]) {
160  s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
161  s->sh.luma_offset_l0[i] = 0;
162  }
163  }
164  if (s->sps->chroma_format_idc != 0) { // FIXME: invert "if" and "for"
165  for (i = 0; i < s->sh.nb_refs[L0]; i++)
166  chroma_weight_l0_flag[i] = get_bits1(gb);
167  } else {
168  for (i = 0; i < s->sh.nb_refs[L0]; i++)
169  chroma_weight_l0_flag[i] = 0;
170  }
171  for (i = 0; i < s->sh.nb_refs[L0]; i++) {
172  if (luma_weight_l0_flag[i]) {
173  int delta_luma_weight_l0 = get_se_golomb(gb);
174  s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
175  s->sh.luma_offset_l0[i] = get_se_golomb(gb);
176  }
177  if (chroma_weight_l0_flag[i]) {
178  for (j = 0; j < 2; j++) {
179  int delta_chroma_weight_l0 = get_se_golomb(gb);
180  int delta_chroma_offset_l0 = get_se_golomb(gb);
181  s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
182  s->sh.chroma_offset_l0[i][j] = av_clip_c((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
183  >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
184  }
185  } else {
186  s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
187  s->sh.chroma_offset_l0[i][0] = 0;
188  s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
189  s->sh.chroma_offset_l0[i][1] = 0;
190  }
191  }
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_c((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 }
230 
232 {
233  const HEVCSPS *sps = s->sps;
234  int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
235  int prev_delta_msb = 0;
236  int nb_sps = 0, nb_sh;
237  int i;
238 
239  rps->nb_refs = 0;
241  return 0;
242 
243  if (sps->num_long_term_ref_pics_sps > 0)
244  nb_sps = get_ue_golomb_long(gb);
245  nb_sh = get_ue_golomb_long(gb);
246 
247  if (nb_sh + nb_sps > FF_ARRAY_ELEMS(rps->poc))
248  return AVERROR_INVALIDDATA;
249 
250  rps->nb_refs = nb_sh + nb_sps;
251 
252  for (i = 0; i < rps->nb_refs; i++) {
253  uint8_t delta_poc_msb_present;
254 
255  if (i < nb_sps) {
256  uint8_t lt_idx_sps = 0;
257 
258  if (sps->num_long_term_ref_pics_sps > 1)
259  lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
260 
261  rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
262  rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
263  } else {
264  rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
265  rps->used[i] = get_bits1(gb);
266  }
267 
268  delta_poc_msb_present = get_bits1(gb);
269  if (delta_poc_msb_present) {
270  int delta = get_ue_golomb_long(gb);
271 
272  if (i && i != nb_sps)
273  delta += prev_delta_msb;
274 
275  rps->poc[i] += s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
276  prev_delta_msb = delta;
277  }
278  }
279 
280  return 0;
281 }
282 
284 {
285  GetBitContext *gb = &s->HEVClc->gb;
286  SliceHeader *sh = &s->sh;
287  int i, j, ret;
288 
289  // Coded parameters
291  if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
292  s->seq_decode = (s->seq_decode + 1) & 0xff;
293  s->max_ra = INT_MAX;
294  if (IS_IDR(s))
296  }
297  if (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
299 
300  sh->pps_id = get_ue_golomb_long(gb);
301  if (sh->pps_id >= MAX_PPS_COUNT || !s->pps_list[sh->pps_id]) {
302  av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
303  return AVERROR_INVALIDDATA;
304  }
305  s->pps = (HEVCPPS*)s->pps_list[sh->pps_id]->data;
306 
307  if (s->sps != (HEVCSPS*)s->sps_list[s->pps->sps_id]->data) {
308  s->sps = (HEVCSPS*)s->sps_list[s->pps->sps_id]->data;
309  s->vps = s->vps_list[s->sps->vps_id];
310 
311  pic_arrays_free(s);
312  ret = pic_arrays_init(s);
313  if (ret < 0) {
314  s->sps = NULL;
315  return AVERROR(ENOMEM);
316  }
317 
318  s->width = s->sps->width;
319  s->height = s->sps->height;
320 
321  s->avctx->coded_width = s->sps->width;
322  s->avctx->coded_height = s->sps->height;
323  s->avctx->width = s->sps->output_width;
324  s->avctx->height = s->sps->output_height;
325  s->avctx->pix_fmt = s->sps->pix_fmt;
326  s->avctx->sample_aspect_ratio = s->sps->vui.sar;
328 
329  if (s->sps->chroma_format_idc == 0 || s->sps->separate_colour_plane_flag) {
331  "TODO: s->sps->chroma_format_idc == 0 || "
332  "s->sps->separate_colour_plane_flag\n");
333  return AVERROR_PATCHWELCOME;
334  }
335 
338  ff_videodsp_init (&s->vdsp, s->sps->bit_depth);
339 
340  if (s->sps->sao_enabled) {
342  ret = ff_get_buffer(s->avctx, s->tmp_frame, 0);
343  if (ret < 0)
344  return ret;
345  s->frame = s->tmp_frame;
346  }
347  }
348 
350  if (!sh->first_slice_in_pic_flag) {
351  int slice_address_length;
352 
355 
356  slice_address_length = av_ceil_log2(s->sps->ctb_width *
357  s->sps->ctb_height);
358  sh->slice_segment_addr = get_bits(gb, slice_address_length);
359  if (sh->slice_segment_addr >= s->sps->ctb_width * s->sps->ctb_height) {
360  av_log(s->avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
361  sh->slice_segment_addr);
362  return AVERROR_INVALIDDATA;
363  }
364 
365  if (!sh->dependent_slice_segment_flag) {
366  sh->slice_addr = sh->slice_segment_addr;
367  s->slice_idx++;
368  }
369  } else {
370  sh->slice_segment_addr = sh->slice_addr = 0;
371  s->slice_idx = 0;
372  s->slice_initialized = 0;
373  }
374 
375  if (!sh->dependent_slice_segment_flag) {
376  s->slice_initialized = 0;
377 
378  for (i = 0; i < s->pps->num_extra_slice_header_bits; i++)
379  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
380 
381  sh->slice_type = get_ue_golomb_long(gb);
382  if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
383  sh->slice_type == B_SLICE)) {
384  av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
385  sh->slice_type);
386  return AVERROR_INVALIDDATA;
387  }
388 
390  sh->pic_output_flag = get_bits1(gb);
391 
393  sh->colour_plane_id = get_bits(gb, 2);
394 
395  if (!IS_IDR(s)) {
396  int short_term_ref_pic_set_sps_flag;
397  int poc;
398 
401  if (!sh->first_slice_in_pic_flag && poc != s->poc) {
403  "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
405  return AVERROR_INVALIDDATA;
406  poc = s->poc;
407  }
408  s->poc = poc;
409 
410  short_term_ref_pic_set_sps_flag = get_bits1(gb);
411  if (!short_term_ref_pic_set_sps_flag) {
412  ret = ff_hevc_decode_short_term_rps(s, &sh->slice_rps, s->sps, 1);
413  if (ret < 0)
414  return ret;
415 
416  sh->short_term_rps = &sh->slice_rps;
417  } else {
418  int numbits, rps_idx;
419 
420  if (!s->sps->nb_st_rps) {
421  av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
422  return AVERROR_INVALIDDATA;
423  }
424 
425  numbits = av_ceil_log2(s->sps->nb_st_rps);
426  rps_idx = (numbits > 0) ? get_bits(gb, numbits) : 0;
427  sh->short_term_rps = &s->sps->st_rps[rps_idx];
428  }
429 
430  ret = decode_lt_rps(s, &sh->long_term_rps, gb);
431  if (ret < 0) {
432  av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
434  return AVERROR_INVALIDDATA;
435  }
436 
439  else
441  } else {
442  s->sh.short_term_rps = NULL;
443  s->poc = 0;
444  }
445 
446  /* 8.3.1 */
447  if (s->temporal_id == 0 &&
448  s->nal_unit_type != NAL_TRAIL_N &&
449  s->nal_unit_type != NAL_TSA_N &&
450  s->nal_unit_type != NAL_STSA_N &&
451  s->nal_unit_type != NAL_TRAIL_N &&
452  s->nal_unit_type != NAL_RADL_N &&
453  s->nal_unit_type != NAL_RADL_R &&
455  s->pocTid0 = s->poc;
456 
457  if (s->sps->sao_enabled) {
461  } else {
465  }
466 
467  sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
468  if (sh->slice_type == P_SLICE || sh->slice_type == B_SLICE) {
469  int nb_refs;
470 
472  if (sh->slice_type == B_SLICE)
474 
475  if (get_bits1(gb)) { // num_ref_idx_active_override_flag
476  sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
477  if (sh->slice_type == B_SLICE)
478  sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
479  }
480  if (sh->nb_refs[L0] > MAX_REFS || sh->nb_refs[L1] > MAX_REFS) {
481  av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
482  sh->nb_refs[L0], sh->nb_refs[L1]);
483  return AVERROR_INVALIDDATA;
484  }
485 
486  sh->rpl_modification_flag[0] = 0;
487  sh->rpl_modification_flag[1] = 0;
488  nb_refs = ff_hevc_frame_nb_refs(s);
489  if (!nb_refs) {
490  av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
491  return AVERROR_INVALIDDATA;
492  }
493 
494  if (s->pps->lists_modification_present_flag && nb_refs > 1) {
495  sh->rpl_modification_flag[0] = get_bits1(gb);
496  if (sh->rpl_modification_flag[0]) {
497  for (i = 0; i < sh->nb_refs[L0]; i++)
498  sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
499  }
500 
501  if (sh->slice_type == B_SLICE) {
502  sh->rpl_modification_flag[1] = get_bits1(gb);
503  if (sh->rpl_modification_flag[1] == 1)
504  for (i = 0; i < sh->nb_refs[L1]; i++)
505  sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
506  }
507  }
508 
509  if (sh->slice_type == B_SLICE)
510  sh->mvd_l1_zero_flag = get_bits1(gb);
511 
513  sh->cabac_init_flag = get_bits1(gb);
514  else
515  sh->cabac_init_flag = 0;
516 
517  sh->collocated_ref_idx = 0;
519  sh->collocated_list = L0;
520  if (sh->slice_type == B_SLICE)
521  sh->collocated_list = !get_bits1(gb);
522 
523  if (sh->nb_refs[sh->collocated_list] > 1) {
525  if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
527  "Invalid collocated_ref_idx: %d.\n", sh->collocated_ref_idx);
528  return AVERROR_INVALIDDATA;
529  }
530  }
531  }
532 
533  if ((s->pps->weighted_pred_flag && sh->slice_type == P_SLICE) ||
534  (s->pps->weighted_bipred_flag && sh->slice_type == B_SLICE)) {
535  pred_weight_table(s, gb);
536  }
537 
539  if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
541  "Invalid number of merging MVP candidates: %d.\n",
542  sh->max_num_merge_cand);
543  return AVERROR_INVALIDDATA;
544  }
545  }
546 
547  sh->slice_qp_delta = get_se_golomb(gb);
551  } else {
552  sh->slice_cb_qp_offset = 0;
553  sh->slice_cr_qp_offset = 0;
554  }
555 
557  int deblocking_filter_override_flag = 0;
558 
560  deblocking_filter_override_flag = get_bits1(gb);
561 
562  if (deblocking_filter_override_flag) {
565  sh->beta_offset = get_se_golomb(gb) * 2;
566  sh->tc_offset = get_se_golomb(gb) * 2;
567  }
568  } else {
570  sh->beta_offset = s->pps->beta_offset;
571  sh->tc_offset = s->pps->tc_offset;
572  }
573  } else {
575  sh->beta_offset = 0;
576  sh->tc_offset = 0;
577  }
578 
584  } else {
586  }
587  } else if (!s->slice_initialized) {
588  av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
589  return AVERROR_INVALIDDATA;
590  }
591 
592  sh->num_entry_point_offsets = 0;
595  if (sh->num_entry_point_offsets > 0) {
596  int offset_len = get_ue_golomb_long(gb) + 1;
597  int segments = offset_len >> 4;
598  int rest = (offset_len & 15);
600  av_freep(&sh->offset);
601  av_freep(&sh->size);
602  sh->entry_point_offset = av_malloc(sh->num_entry_point_offsets * sizeof(int));
603  sh->offset = av_malloc(sh->num_entry_point_offsets * sizeof(int));
604  sh->size = av_malloc(sh->num_entry_point_offsets * sizeof(int));
605  if (!sh->entry_point_offset || !sh->offset || !sh->size) {
606  sh->num_entry_point_offsets = 0;
607  av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
608  return AVERROR(ENOMEM);
609  }
610  for (i = 0; i < sh->num_entry_point_offsets; i++) {
611  int val = 0;
612  for (j = 0; j < segments; j++) {
613  val <<= 16;
614  val += get_bits(gb, 16);
615  }
616  if (rest) {
617  val <<= rest;
618  val += get_bits(gb, rest);
619  }
620  sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
621  }
622  if (s->threads_number > 1 && (s->pps->num_tile_rows > 1 || s->pps->num_tile_columns > 1)) {
623  s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
624  s->threads_number = 1;
625  } else
626  s->enable_parallel_tiles = 0;
627  } else
628  s->enable_parallel_tiles = 0;
629  }
630 
632  int length = get_ue_golomb_long(gb);
633  for (i = 0; i < length; i++)
634  skip_bits(gb, 8); // slice_header_extension_data_byte
635  }
636 
637  // Inferred parameters
638  sh->slice_qp = 26U + s->pps->pic_init_qp_minus26 + sh->slice_qp_delta;
639  if (sh->slice_qp > 51 ||
640  sh->slice_qp < -s->sps->qp_bd_offset) {
642  "The slice_qp %d is outside the valid range "
643  "[%d, 51].\n",
644  sh->slice_qp,
645  -s->sps->qp_bd_offset);
646  return AVERROR_INVALIDDATA;
647  }
648 
650 
652 
653  if (!s->pps->cu_qp_delta_enabled_flag)
654  s->HEVClc->qp_y = FFUMOD(s->sh.slice_qp + 52 + 2 * s->sps->qp_bd_offset,
655  52 + s->sps->qp_bd_offset) - s->sps->qp_bd_offset;
656 
657  s->slice_initialized = 1;
658 
659  return 0;
660 }
661 
662 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
663 
664 #define SET_SAO(elem, value) \
665 do { \
666  if (!sao_merge_up_flag && !sao_merge_left_flag) \
667  sao->elem = value; \
668  else if (sao_merge_left_flag) \
669  sao->elem = CTB(s->sao, rx-1, ry).elem; \
670  else if (sao_merge_up_flag) \
671  sao->elem = CTB(s->sao, rx, ry-1).elem; \
672  else \
673  sao->elem = 0; \
674 } while (0)
675 
676 static void hls_sao_param(HEVCContext *s, int rx, int ry)
677 {
678  HEVCLocalContext *lc = s->HEVClc;
679  int sao_merge_left_flag = 0;
680  int sao_merge_up_flag = 0;
681  int shift = s->sps->bit_depth - FFMIN(s->sps->bit_depth, 10);
682  SAOParams *sao = &CTB(s->sao, rx, ry);
683  int c_idx, i;
684 
687  if (rx > 0) {
688  if (lc->ctb_left_flag)
689  sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
690  }
691  if (ry > 0 && !sao_merge_left_flag) {
692  if (lc->ctb_up_flag)
693  sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
694  }
695  }
696 
697  for (c_idx = 0; c_idx < 3; c_idx++) {
698  if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
699  sao->type_idx[c_idx] = SAO_NOT_APPLIED;
700  continue;
701  }
702 
703  if (c_idx == 2) {
704  sao->type_idx[2] = sao->type_idx[1];
705  sao->eo_class[2] = sao->eo_class[1];
706  } else {
707  SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
708  }
709 
710  if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
711  continue;
712 
713  for (i = 0; i < 4; i++)
714  SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
715 
716  if (sao->type_idx[c_idx] == SAO_BAND) {
717  for (i = 0; i < 4; i++) {
718  if (sao->offset_abs[c_idx][i]) {
719  SET_SAO(offset_sign[c_idx][i], ff_hevc_sao_offset_sign_decode(s));
720  } else {
721  sao->offset_sign[c_idx][i] = 0;
722  }
723  }
724  SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
725  } else if (c_idx != 2) {
726  SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
727  }
728 
729  // Inferred parameters
730  sao->offset_val[c_idx][0] = 0;
731  for (i = 0; i < 4; i++) {
732  sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i] << shift;
733  if (sao->type_idx[c_idx] == SAO_EDGE) {
734  if (i > 1)
735  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
736  } else if (sao->offset_sign[c_idx][i]) {
737  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
738  }
739  }
740  }
741 }
742 
743 #undef SET_SAO
744 #undef CTB
745 
746 
747 static void hls_transform_unit(HEVCContext *s, int x0, int y0,
748  int xBase, int yBase, int cb_xBase, int cb_yBase,
749  int log2_cb_size, int log2_trafo_size,
750  int trafo_depth, int blk_idx)
751 {
752  HEVCLocalContext *lc = s->HEVClc;
753 
754  if (lc->cu.pred_mode == MODE_INTRA) {
755  int trafo_size = 1 << log2_trafo_size;
756  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
757 
758  s->hpc.intra_pred(s, x0, y0, log2_trafo_size, 0);
759  if (log2_trafo_size > 2) {
760  trafo_size = trafo_size << (s->sps->hshift[1] - 1);
761  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
762  s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 1);
763  s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 2);
764  } else if (blk_idx == 3) {
765  trafo_size = trafo_size << (s->sps->hshift[1]);
766  ff_hevc_set_neighbour_available(s, xBase, yBase, trafo_size, trafo_size);
767  s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 1);
768  s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 2);
769  }
770  }
771 
772  if (lc->tt.cbf_luma ||
773  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
774  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
775  int scan_idx = SCAN_DIAG;
776  int scan_idx_c = SCAN_DIAG;
777 
780  if (lc->tu.cu_qp_delta != 0)
781  if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
782  lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
783  lc->tu.is_cu_qp_delta_coded = 1;
784  ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
785  }
786 
787  if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
788  if (lc->tu.cur_intra_pred_mode >= 6 &&
789  lc->tu.cur_intra_pred_mode <= 14) {
790  scan_idx = SCAN_VERT;
791  } else if (lc->tu.cur_intra_pred_mode >= 22 &&
792  lc->tu.cur_intra_pred_mode <= 30) {
793  scan_idx = SCAN_HORIZ;
794  }
795 
796  if (lc->pu.intra_pred_mode_c >= 6 &&
797  lc->pu.intra_pred_mode_c <= 14) {
798  scan_idx_c = SCAN_VERT;
799  } else if (lc->pu.intra_pred_mode_c >= 22 &&
800  lc->pu.intra_pred_mode_c <= 30) {
801  scan_idx_c = SCAN_HORIZ;
802  }
803  }
804 
805  if (lc->tt.cbf_luma)
806  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
807  if (log2_trafo_size > 2) {
808  if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0))
809  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
810  if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0))
811  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
812  } else if (blk_idx == 3) {
813  if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], xBase, yBase))
814  ff_hevc_hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
815  if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], xBase, yBase))
816  ff_hevc_hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
817  }
818  }
819 }
820 
821 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
822 {
823  int cb_size = 1 << log2_cb_size;
824  int log2_min_pu_size = s->sps->log2_min_pu_size;
825 
826  int min_pu_width = s->sps->min_pu_width;
827  int x_end = FFMIN(x0 + cb_size, s->sps->width);
828  int y_end = FFMIN(y0 + cb_size, s->sps->height);
829  int i, j;
830 
831  for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
832  for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
833  s->is_pcm[i + j * min_pu_width] = 2;
834 }
835 
836 static void hls_transform_tree(HEVCContext *s, int x0, int y0,
837  int xBase, int yBase, int cb_xBase, int cb_yBase,
838  int log2_cb_size, int log2_trafo_size,
839  int trafo_depth, int blk_idx)
840 {
841  HEVCLocalContext *lc = s->HEVClc;
842  uint8_t split_transform_flag;
843 
844  if (trafo_depth > 0 && log2_trafo_size == 2) {
845  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
846  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase);
847  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
848  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase);
849  } else {
850  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
851  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) = 0;
852  }
853 
854  if (lc->cu.intra_split_flag) {
855  if (trafo_depth == 1)
856  lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
857  } else {
859  }
860 
861  lc->tt.cbf_luma = 1;
862 
864  lc->cu.pred_mode == MODE_INTER &&
865  lc->cu.part_mode != PART_2Nx2N && trafo_depth == 0);
866 
867  if (log2_trafo_size <= s->sps->log2_max_trafo_size &&
868  log2_trafo_size > s->sps->log2_min_tb_size &&
869  trafo_depth < lc->cu.max_trafo_depth &&
870  !(lc->cu.intra_split_flag && trafo_depth == 0)) {
871  split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
872  } else {
873  split_transform_flag = (log2_trafo_size > s->sps->log2_max_trafo_size ||
874  (lc->cu.intra_split_flag && (trafo_depth == 0)) ||
875  lc->tt.inter_split_flag);
876  }
877 
878  if (log2_trafo_size > 2) {
879  if (trafo_depth == 0 ||
880  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase)) {
881  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
882  ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
883  }
884 
885  if (trafo_depth == 0 || SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase)) {
886  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
887  ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
888  }
889  }
890 
891  if (split_transform_flag) {
892  int x1 = x0 + ((1 << log2_trafo_size) >> 1);
893  int y1 = y0 + ((1 << log2_trafo_size) >> 1);
894 
895  hls_transform_tree(s, x0, y0, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
896  log2_trafo_size - 1, trafo_depth + 1, 0);
897  hls_transform_tree(s, x1, y0, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
898  log2_trafo_size - 1, trafo_depth + 1, 1);
899  hls_transform_tree(s, x0, y1, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
900  log2_trafo_size - 1, trafo_depth + 1, 2);
901  hls_transform_tree(s, x1, y1, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
902  log2_trafo_size - 1, trafo_depth + 1, 3);
903  } else {
904  int min_tu_size = 1 << s->sps->log2_min_tb_size;
905  int log2_min_tu_size = s->sps->log2_min_tb_size;
906  int min_tu_width = s->sps->min_tb_width;
907 
908  if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
909  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
910  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
911  lc->tt.cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
912  }
913 
914  hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
915  log2_cb_size, log2_trafo_size, trafo_depth, blk_idx);
916 
917  // TODO: store cbf_luma somewhere else
918  if (lc->tt.cbf_luma) {
919  int i, j;
920  for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
921  for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
922  int x_tu = (x0 + j) >> log2_min_tu_size;
923  int y_tu = (y0 + i) >> log2_min_tu_size;
924  s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
925  }
926  }
928  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size,
932  set_deblocking_bypass(s, x0, y0, log2_trafo_size);
933  }
934  }
935 }
936 
937 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
938 {
939  //TODO: non-4:2:0 support
940  HEVCLocalContext *lc = s->HEVClc;
941  GetBitContext gb;
942  int cb_size = 1 << log2_cb_size;
943  int stride0 = s->frame->linesize[0];
944  uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->sps->pixel_shift)];
945  int stride1 = s->frame->linesize[1];
946  uint8_t *dst1 = &s->frame->data[1][(y0 >> s->sps->vshift[1]) * stride1 + ((x0 >> s->sps->hshift[1]) << s->sps->pixel_shift)];
947  int stride2 = s->frame->linesize[2];
948  uint8_t *dst2 = &s->frame->data[2][(y0 >> s->sps->vshift[2]) * stride2 + ((x0 >> s->sps->hshift[2]) << s->sps->pixel_shift)];
949 
950  int length = cb_size * cb_size * s->sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->sps->pcm.bit_depth;
951  const uint8_t *pcm = skip_bytes(&s->HEVClc->cc, (length + 7) >> 3);
952  int ret;
953 
954  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
957 
958  ret = init_get_bits(&gb, pcm, length);
959  if (ret < 0)
960  return ret;
961 
962  s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->sps->pcm.bit_depth);
963  s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
964  s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
965  return 0;
966 }
967 
968 /**
969  * 8.5.3.2.2.1 Luma sample interpolation process
970  *
971  * @param s HEVC decoding context
972  * @param dst target buffer for block data at block position
973  * @param dststride stride of the dst buffer
974  * @param ref reference picture buffer at origin (0, 0)
975  * @param mv motion vector (relative to block position) to get pixel data from
976  * @param x_off horizontal position of block from origin (0, 0)
977  * @param y_off vertical position of block from origin (0, 0)
978  * @param block_w width of block
979  * @param block_h height of block
980  */
981 static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride,
982  AVFrame *ref, const Mv *mv, int x_off, int y_off,
983  int block_w, int block_h)
984 {
985  HEVCLocalContext *lc = s->HEVClc;
986  uint8_t *src = ref->data[0];
987  ptrdiff_t srcstride = ref->linesize[0];
988  int pic_width = s->sps->width;
989  int pic_height = s->sps->height;
990 
991  int mx = mv->x & 3;
992  int my = mv->y & 3;
993  int extra_left = ff_hevc_qpel_extra_before[mx];
994  int extra_top = ff_hevc_qpel_extra_before[my];
995 
996  x_off += mv->x >> 2;
997  y_off += mv->y >> 2;
998  src += y_off * srcstride + (x_off << s->sps->pixel_shift);
999 
1000  if (x_off < extra_left || y_off < extra_top ||
1001  x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
1002  y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
1003  int offset = extra_top * srcstride + (extra_left << s->sps->pixel_shift);
1004 
1005  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, srcstride, src - offset, srcstride,
1006  block_w + ff_hevc_qpel_extra[mx], block_h + ff_hevc_qpel_extra[my],
1007  x_off - extra_left, y_off - extra_top,
1008  pic_width, pic_height);
1009  src = lc->edge_emu_buffer + offset;
1010  }
1011  s->hevcdsp.put_hevc_qpel[my][mx](dst, dststride, src, srcstride, block_w,
1012  block_h, lc->mc_buffer);
1013 }
1014 
1015 /**
1016  * 8.5.3.2.2.2 Chroma sample interpolation process
1017  *
1018  * @param s HEVC decoding context
1019  * @param dst1 target buffer for block data at block position (U plane)
1020  * @param dst2 target buffer for block data at block position (V plane)
1021  * @param dststride stride of the dst1 and dst2 buffers
1022  * @param ref reference picture buffer at origin (0, 0)
1023  * @param mv motion vector (relative to block position) to get pixel data from
1024  * @param x_off horizontal position of block from origin (0, 0)
1025  * @param y_off vertical position of block from origin (0, 0)
1026  * @param block_w width of block
1027  * @param block_h height of block
1028  */
1029 static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2, ptrdiff_t dststride, AVFrame *ref,
1030  const Mv *mv, int x_off, int y_off, int block_w, int block_h)
1031 {
1032  HEVCLocalContext *lc = s->HEVClc;
1033  uint8_t *src1 = ref->data[1];
1034  uint8_t *src2 = ref->data[2];
1035  ptrdiff_t src1stride = ref->linesize[1];
1036  ptrdiff_t src2stride = ref->linesize[2];
1037  int pic_width = s->sps->width >> 1;
1038  int pic_height = s->sps->height >> 1;
1039 
1040  int mx = mv->x & 7;
1041  int my = mv->y & 7;
1042 
1043  x_off += mv->x >> 3;
1044  y_off += mv->y >> 3;
1045  src1 += y_off * src1stride + (x_off << s->sps->pixel_shift);
1046  src2 += y_off * src2stride + (x_off << s->sps->pixel_shift);
1047 
1048  if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1049  x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1050  y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1051  int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->sps->pixel_shift));
1052  int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->sps->pixel_shift));
1053 
1054  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1stride, src1 - offset1, src1stride,
1055  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1056  x_off - EPEL_EXTRA_BEFORE,
1057  y_off - EPEL_EXTRA_BEFORE,
1058  pic_width, pic_height);
1059 
1060  src1 = lc->edge_emu_buffer + offset1;
1061  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1062  block_w, block_h, mx, my, lc->mc_buffer);
1063 
1064  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2stride, src2 - offset2, src2stride,
1065  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1066  x_off - EPEL_EXTRA_BEFORE,
1067  y_off - EPEL_EXTRA_BEFORE,
1068  pic_width, pic_height);
1069  src2 = lc->edge_emu_buffer + offset2;
1070  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1071  block_w, block_h, mx, my,
1072  lc->mc_buffer);
1073  } else {
1074  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1075  block_w, block_h, mx, my,
1076  lc->mc_buffer);
1077  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1078  block_w, block_h, mx, my,
1079  lc->mc_buffer);
1080  }
1081 }
1082 
1084  const Mv *mv, int y0, int height)
1085 {
1086  int y = (mv->y >> 2) + y0 + height + 9;
1087 
1088  if (s->threads_type == FF_THREAD_FRAME )
1089  ff_thread_await_progress(&ref->tf, y, 0);
1090 }
1091 
1092 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1093  int nPbW, int nPbH,
1094  int log2_cb_size, int partIdx)
1095 {
1096 #define POS(c_idx, x, y) \
1097  &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1098  (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
1099  HEVCLocalContext *lc = s->HEVClc;
1100  int merge_idx = 0;
1101  struct MvField current_mv = {{{ 0 }}};
1102 
1103  int min_pu_width = s->sps->min_pu_width;
1104 
1105  MvField *tab_mvf = s->ref->tab_mvf;
1106  RefPicList *refPicList = s->ref->refPicList;
1107  HEVCFrame *ref0, *ref1;
1108 
1109  int tmpstride = MAX_PB_SIZE;
1110 
1111  uint8_t *dst0 = POS(0, x0, y0);
1112  uint8_t *dst1 = POS(1, x0, y0);
1113  uint8_t *dst2 = POS(2, x0, y0);
1114  int log2_min_cb_size = s->sps->log2_min_cb_size;
1115  int min_cb_width = s->sps->min_cb_width;
1116  int x_cb = x0 >> log2_min_cb_size;
1117  int y_cb = y0 >> log2_min_cb_size;
1118  int ref_idx[2];
1119  int mvp_flag[2];
1120  int x_pu, y_pu;
1121  int i, j;
1122 
1123  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
1124  if (s->sh.max_num_merge_cand > 1)
1125  merge_idx = ff_hevc_merge_idx_decode(s);
1126  else
1127  merge_idx = 0;
1128 
1129  ff_hevc_luma_mv_merge_mode(s, x0, y0, 1 << log2_cb_size, 1 << log2_cb_size,
1130  log2_cb_size, partIdx, merge_idx, &current_mv);
1131  x_pu = x0 >> s->sps->log2_min_pu_size;
1132  y_pu = y0 >> s->sps->log2_min_pu_size;
1133 
1134  for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1135  for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1136  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1137  } else { /* MODE_INTER */
1139  if (lc->pu.merge_flag) {
1140  if (s->sh.max_num_merge_cand > 1)
1141  merge_idx = ff_hevc_merge_idx_decode(s);
1142  else
1143  merge_idx = 0;
1144 
1145  ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1146  partIdx, merge_idx, &current_mv);
1147  x_pu = x0 >> s->sps->log2_min_pu_size;
1148  y_pu = y0 >> s->sps->log2_min_pu_size;
1149 
1150  for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1151  for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1152  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1153  } else {
1154  enum InterPredIdc inter_pred_idc = PRED_L0;
1155  ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1156  if (s->sh.slice_type == B_SLICE)
1157  inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1158 
1159  if (inter_pred_idc != PRED_L1) {
1160  if (s->sh.nb_refs[L0]) {
1161  ref_idx[0] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1162  current_mv.ref_idx[0] = ref_idx[0];
1163  }
1164  current_mv.pred_flag[0] = 1;
1165  ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1166  mvp_flag[0] = ff_hevc_mvp_lx_flag_decode(s);
1167  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1168  partIdx, merge_idx, &current_mv, mvp_flag[0], 0);
1169  current_mv.mv[0].x += lc->pu.mvd.x;
1170  current_mv.mv[0].y += lc->pu.mvd.y;
1171  }
1172 
1173  if (inter_pred_idc != PRED_L0) {
1174  if (s->sh.nb_refs[L1]) {
1175  ref_idx[1] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1176  current_mv.ref_idx[1] = ref_idx[1];
1177  }
1178 
1179  if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1180  lc->pu.mvd.x = 0;
1181  lc->pu.mvd.y = 0;
1182  } else {
1183  ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1184  }
1185 
1186  current_mv.pred_flag[1] = 1;
1187  mvp_flag[1] = ff_hevc_mvp_lx_flag_decode(s);
1188  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1189  partIdx, merge_idx, &current_mv, mvp_flag[1], 1);
1190  current_mv.mv[1].x += lc->pu.mvd.x;
1191  current_mv.mv[1].y += lc->pu.mvd.y;
1192  }
1193 
1194  x_pu = x0 >> s->sps->log2_min_pu_size;
1195  y_pu = y0 >> s->sps->log2_min_pu_size;
1196 
1197  for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1198  for(j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1199  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1200  }
1201  }
1202 
1203  if (current_mv.pred_flag[0]) {
1204  ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1205  if (!ref0)
1206  return;
1207  hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1208  }
1209  if (current_mv.pred_flag[1]) {
1210  ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1211  if (!ref1)
1212  return;
1213  hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1214  }
1215 
1216  if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
1217  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1218  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1219 
1220  luma_mc(s, tmp, tmpstride, ref0->frame,
1221  &current_mv.mv[0], x0, y0, nPbW, nPbH);
1222 
1223  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1224  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1226  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1227  s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1228  dst0, s->frame->linesize[0], tmp,
1229  tmpstride, nPbW, nPbH);
1230  } else {
1231  s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1232  }
1233  chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1234  &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1235 
1236  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1237  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1239  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1240  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1241  dst1, s->frame->linesize[1], tmp, tmpstride,
1242  nPbW / 2, nPbH / 2);
1244  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1245  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1246  dst2, s->frame->linesize[2], tmp2, tmpstride,
1247  nPbW / 2, nPbH / 2);
1248  } else {
1249  s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1250  s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1251  }
1252  } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1253  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1254  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1255 
1256  if (!ref1)
1257  return;
1258 
1259  luma_mc(s, tmp, tmpstride, ref1->frame,
1260  &current_mv.mv[1], x0, y0, nPbW, nPbH);
1261 
1262  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1263  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1265  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1266  s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1267  dst0, s->frame->linesize[0], tmp, tmpstride,
1268  nPbW, nPbH);
1269  } else {
1270  s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1271  }
1272 
1273  chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
1274  &current_mv.mv[1], x0/2, y0/2, nPbW/2, nPbH/2);
1275 
1276  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1277  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1279  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1280  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1281  dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1283  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1284  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1285  dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1286  } else {
1287  s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1288  s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1289  }
1290  } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1291  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1292  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1293  DECLARE_ALIGNED(16, int16_t, tmp3[MAX_PB_SIZE * MAX_PB_SIZE]);
1294  DECLARE_ALIGNED(16, int16_t, tmp4[MAX_PB_SIZE * MAX_PB_SIZE]);
1295  HEVCFrame *ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1296  HEVCFrame *ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1297 
1298  if (!ref0 || !ref1)
1299  return;
1300 
1301  luma_mc(s, tmp, tmpstride, ref0->frame,
1302  &current_mv.mv[0], x0, y0, nPbW, nPbH);
1303  luma_mc(s, tmp2, tmpstride, ref1->frame,
1304  &current_mv.mv[1], x0, y0, nPbW, nPbH);
1305 
1306  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1307  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1309  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1310  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1311  s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1312  s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1313  dst0, s->frame->linesize[0],
1314  tmp, tmp2, tmpstride, nPbW, nPbH);
1315  } else {
1316  s->hevcdsp.put_weighted_pred_avg(dst0, s->frame->linesize[0],
1317  tmp, tmp2, tmpstride, nPbW, nPbH);
1318  }
1319 
1320  chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1321  &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1322  chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
1323  &current_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1324 
1325  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1326  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1328  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1329  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1330  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1331  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1332  dst1, s->frame->linesize[1], tmp, tmp3,
1333  tmpstride, nPbW / 2, nPbH / 2);
1335  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1336  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1337  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1338  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1339  dst2, s->frame->linesize[2], tmp2, tmp4,
1340  tmpstride, nPbW / 2, nPbH / 2);
1341  } else {
1342  s->hevcdsp.put_weighted_pred_avg(dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbW/2, nPbH/2);
1343  s->hevcdsp.put_weighted_pred_avg(dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbW/2, nPbH/2);
1344  }
1345  }
1346 }
1347 
1348 /**
1349  * 8.4.1
1350  */
1351 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1352  int prev_intra_luma_pred_flag)
1353 {
1354  HEVCLocalContext *lc = s->HEVClc;
1355  int x_pu = x0 >> s->sps->log2_min_pu_size;
1356  int y_pu = y0 >> s->sps->log2_min_pu_size;
1357  int min_pu_width = s->sps->min_pu_width;
1358  int size_in_pus = pu_size >> s->sps->log2_min_pu_size;
1359  int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
1360  int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
1361 
1362  int cand_up = (lc->ctb_up_flag || y0b) ?
1363  s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1364  int cand_left = (lc->ctb_left_flag || x0b) ?
1365  s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1366 
1367  int y_ctb = (y0 >> (s->sps->log2_ctb_size)) << (s->sps->log2_ctb_size);
1368 
1369  MvField *tab_mvf = s->ref->tab_mvf;
1370  int intra_pred_mode;
1371  int candidate[3];
1372  int i, j;
1373 
1374  // intra_pred_mode prediction does not cross vertical CTB boundaries
1375  if ((y0 - 1) < y_ctb)
1376  cand_up = INTRA_DC;
1377 
1378  if (cand_left == cand_up) {
1379  if (cand_left < 2) {
1380  candidate[0] = INTRA_PLANAR;
1381  candidate[1] = INTRA_DC;
1382  candidate[2] = INTRA_ANGULAR_26;
1383  } else {
1384  candidate[0] = cand_left;
1385  candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1386  candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1387  }
1388  } else {
1389  candidate[0] = cand_left;
1390  candidate[1] = cand_up;
1391  if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1392  candidate[2] = INTRA_PLANAR;
1393  } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1394  candidate[2] = INTRA_DC;
1395  } else {
1396  candidate[2] = INTRA_ANGULAR_26;
1397  }
1398  }
1399 
1400  if (prev_intra_luma_pred_flag) {
1401  intra_pred_mode = candidate[lc->pu.mpm_idx];
1402  } else {
1403  if (candidate[0] > candidate[1])
1404  FFSWAP(uint8_t, candidate[0], candidate[1]);
1405  if (candidate[0] > candidate[2])
1406  FFSWAP(uint8_t, candidate[0], candidate[2]);
1407  if (candidate[1] > candidate[2])
1408  FFSWAP(uint8_t, candidate[1], candidate[2]);
1409 
1410  intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1411  for (i = 0; i < 3; i++)
1412  if (intra_pred_mode >= candidate[i])
1413  intra_pred_mode++;
1414  }
1415 
1416  /* write the intra prediction units into the mv array */
1417  if (!size_in_pus)
1418  size_in_pus = 1;
1419  for (i = 0; i < size_in_pus; i++) {
1420  memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1421  intra_pred_mode, size_in_pus);
1422 
1423  for (j = 0; j < size_in_pus; j++) {
1424  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].is_intra = 1;
1425  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[0] = 0;
1426  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[1] = 0;
1427  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[0] = 0;
1428  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[1] = 0;
1429  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].x = 0;
1430  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].y = 0;
1431  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].x = 0;
1432  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].y = 0;
1433  }
1434  }
1435 
1436  return intra_pred_mode;
1437 }
1438 
1439 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1440  int log2_cb_size, int ct_depth)
1441 {
1442  int length = (1 << log2_cb_size) >> s->sps->log2_min_cb_size;
1443  int x_cb = x0 >> s->sps->log2_min_cb_size;
1444  int y_cb = y0 >> s->sps->log2_min_cb_size;
1445  int y;
1446 
1447  for (y = 0; y < length; y++)
1448  memset(&s->tab_ct_depth[(y_cb + y) * s->sps->min_cb_width + x_cb],
1449  ct_depth, length);
1450 }
1451 
1452 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1453  int log2_cb_size)
1454 {
1455  HEVCLocalContext *lc = s->HEVClc;
1456  static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1457  uint8_t prev_intra_luma_pred_flag[4];
1458  int split = lc->cu.part_mode == PART_NxN;
1459  int pb_size = (1 << log2_cb_size) >> split;
1460  int side = split + 1;
1461  int chroma_mode;
1462  int i, j;
1463 
1464  for (i = 0; i < side; i++)
1465  for (j = 0; j < side; j++)
1466  prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1467 
1468  for (i = 0; i < side; i++) {
1469  for (j = 0; j < side; j++) {
1470  if (prev_intra_luma_pred_flag[2 * i + j])
1472  else
1474 
1475  lc->pu.intra_pred_mode[2 * i + j] =
1476  luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1477  prev_intra_luma_pred_flag[2 * i + j]);
1478  }
1479  }
1480 
1481  chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1482  if (chroma_mode != 4) {
1483  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1484  lc->pu.intra_pred_mode_c = 34;
1485  else
1486  lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
1487  } else {
1488  lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
1489  }
1490 }
1491 
1493  int x0, int y0,
1494  int log2_cb_size)
1495 {
1496  HEVCLocalContext *lc = s->HEVClc;
1497  int pb_size = 1 << log2_cb_size;
1498  int size_in_pus = pb_size >> s->sps->log2_min_pu_size;
1499  int min_pu_width = s->sps->min_pu_width;
1500  MvField *tab_mvf = s->ref->tab_mvf;
1501  int x_pu = x0 >> s->sps->log2_min_pu_size;
1502  int y_pu = y0 >> s->sps->log2_min_pu_size;
1503  int j, k;
1504 
1505  if (size_in_pus == 0)
1506  size_in_pus = 1;
1507  for (j = 0; j < size_in_pus; j++) {
1508  memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
1509  for (k = 0; k < size_in_pus; k++)
1510  tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
1511  }
1512 }
1513 
1514 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
1515 {
1516  int cb_size = 1 << log2_cb_size;
1517  HEVCLocalContext *lc = s->HEVClc;
1518  int log2_min_cb_size = s->sps->log2_min_cb_size;
1519  int length = cb_size >> log2_min_cb_size;
1520  int min_cb_width = s->sps->min_cb_width;
1521  int x_cb = x0 >> log2_min_cb_size;
1522  int y_cb = y0 >> log2_min_cb_size;
1523  int x, y;
1524 
1525  lc->cu.x = x0;
1526  lc->cu.y = y0;
1527  lc->cu.rqt_root_cbf = 1;
1528 
1529  lc->cu.pred_mode = MODE_INTRA;
1530  lc->cu.part_mode = PART_2Nx2N;
1531  lc->cu.intra_split_flag = 0;
1532  lc->cu.pcm_flag = 0;
1533  SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
1534  for (x = 0; x < 4; x++)
1535  lc->pu.intra_pred_mode[x] = 1;
1538  if (lc->cu.cu_transquant_bypass_flag)
1539  set_deblocking_bypass(s, x0, y0, log2_cb_size);
1540  } else
1541  lc->cu.cu_transquant_bypass_flag = 0;
1542 
1543  if (s->sh.slice_type != I_SLICE) {
1544  uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
1545 
1546  lc->cu.pred_mode = MODE_SKIP;
1547  x = y_cb * min_cb_width + x_cb;
1548  for (y = 0; y < length; y++) {
1549  memset(&s->skip_flag[x], skip_flag, length);
1550  x += min_cb_width;
1551  }
1552  lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
1553  }
1554 
1555  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
1556  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
1557  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1558 
1560  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1563  } else {
1564  if (s->sh.slice_type != I_SLICE)
1566  if (lc->cu.pred_mode != MODE_INTRA ||
1567  log2_cb_size == s->sps->log2_min_cb_size) {
1568  lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
1569  lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
1570  lc->cu.pred_mode == MODE_INTRA;
1571  }
1572 
1573  if (lc->cu.pred_mode == MODE_INTRA) {
1574  if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
1575  log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
1576  log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
1578  }
1579  if (lc->cu.pcm_flag) {
1580  int ret;
1581  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1582  ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
1584  set_deblocking_bypass(s, x0, y0, log2_cb_size);
1585 
1586  if (ret < 0)
1587  return ret;
1588  } else {
1589  intra_prediction_unit(s, x0, y0, log2_cb_size);
1590  }
1591  } else {
1592  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1593  switch (lc->cu.part_mode) {
1594  case PART_2Nx2N:
1595  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
1596  break;
1597  case PART_2NxN:
1598  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
1599  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size/2, log2_cb_size, 1);
1600  break;
1601  case PART_Nx2N:
1602  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
1603  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
1604  break;
1605  case PART_2NxnU:
1606  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
1607  hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
1608  break;
1609  case PART_2NxnD:
1610  hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
1611  hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
1612  break;
1613  case PART_nLx2N:
1614  hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size,0);
1615  hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
1616  break;
1617  case PART_nRx2N:
1618  hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size,0);
1619  hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size/4, cb_size, log2_cb_size, 1);
1620  break;
1621  case PART_NxN:
1622  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
1623  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
1624  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
1625  hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
1626  break;
1627  }
1628  }
1629 
1630  if (!lc->cu.pcm_flag) {
1631  if (lc->cu.pred_mode != MODE_INTRA &&
1632  !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
1634  }
1635  if (lc->cu.rqt_root_cbf) {
1636  lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
1639  hls_transform_tree(s, x0, y0, x0, y0, x0, y0, log2_cb_size,
1640  log2_cb_size, 0, 0);
1641  } else {
1643  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1646  }
1647  }
1648  }
1649 
1651  ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
1652 
1653  x = y_cb * min_cb_width + x_cb;
1654  for (y = 0; y < length; y++) {
1655  memset(&s->qp_y_tab[x], lc->qp_y, length);
1656  x += min_cb_width;
1657  }
1658 
1659  set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
1660 
1661  return 0;
1662 }
1663 
1664 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
1665  int log2_cb_size, int cb_depth)
1666 {
1667  HEVCLocalContext *lc = s->HEVClc;
1668  const int cb_size = 1 << log2_cb_size;
1669  int ret;
1670 
1671  lc->ct.depth = cb_depth;
1672  if ((x0 + cb_size <= s->sps->width) &&
1673  (y0 + cb_size <= s->sps->height) &&
1674  log2_cb_size > s->sps->log2_min_cb_size) {
1675  SAMPLE(s->split_cu_flag, x0, y0) =
1676  ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
1677  } else {
1678  SAMPLE(s->split_cu_flag, x0, y0) =
1679  (log2_cb_size > s->sps->log2_min_cb_size);
1680  }
1681  if (s->pps->cu_qp_delta_enabled_flag &&
1682  log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
1683  lc->tu.is_cu_qp_delta_coded = 0;
1684  lc->tu.cu_qp_delta = 0;
1685  }
1686 
1687  if (SAMPLE(s->split_cu_flag, x0, y0)) {
1688  const int cb_size_split = cb_size >> 1;
1689  const int x1 = x0 + cb_size_split;
1690  const int y1 = y0 + cb_size_split;
1691  int more_data = 0;
1692 
1693  more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
1694  if (more_data < 0)
1695  return more_data;
1696 
1697  if (more_data && x1 < s->sps->width)
1698  more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
1699  if (more_data && y1 < s->sps->height)
1700  more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
1701  if (more_data && x1 < s->sps->width &&
1702  y1 < s->sps->height) {
1703  return hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
1704  }
1705  if (more_data)
1706  return ((x1 + cb_size_split) < s->sps->width ||
1707  (y1 + cb_size_split) < s->sps->height);
1708  else
1709  return 0;
1710  } else {
1711  ret = hls_coding_unit(s, x0, y0, log2_cb_size);
1712  if (ret < 0)
1713  return ret;
1714  if ((!((x0 + cb_size) %
1715  (1 << (s->sps->log2_ctb_size))) ||
1716  (x0 + cb_size >= s->sps->width)) &&
1717  (!((y0 + cb_size) %
1718  (1 << (s->sps->log2_ctb_size))) ||
1719  (y0 + cb_size >= s->sps->height))) {
1720  int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
1721  return !end_of_slice_flag;
1722  } else {
1723  return 1;
1724  }
1725  }
1726 
1727  return 0;
1728 }
1729 
1730 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb, int ctb_addr_ts)
1731 {
1732  HEVCLocalContext *lc = s->HEVClc;
1733  int ctb_size = 1 << s->sps->log2_ctb_size;
1734  int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1735  int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
1736 
1737  int tile_left_boundary;
1738  int tile_up_boundary;
1739  int slice_left_boundary;
1740  int slice_up_boundary;
1741 
1742  s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
1743 
1745  if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
1746  lc->first_qp_group = 1;
1747  lc->end_of_tiles_x = s->sps->width;
1748  } else if (s->pps->tiles_enabled_flag) {
1749  if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
1750  int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
1751  lc->start_of_tiles_x = x_ctb;
1752  lc->end_of_tiles_x = x_ctb + (s->pps->column_width[idxX] << s->sps->log2_ctb_size);
1753  lc->first_qp_group = 1;
1754  }
1755  } else {
1756  lc->end_of_tiles_x = s->sps->width;
1757  }
1758 
1759  lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
1760 
1761  if (s->pps->tiles_enabled_flag) {
1762  tile_left_boundary = ((x_ctb > 0) &&
1763  (s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]]));
1764  slice_left_boundary = ((x_ctb > 0) &&
1765  (s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - 1]));
1766  tile_up_boundary = ((y_ctb > 0) &&
1767  (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]]));
1768  slice_up_boundary = ((y_ctb > 0) &&
1769  (s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width]));
1770  } else {
1771  tile_left_boundary =
1772  tile_up_boundary = 1;
1773  slice_left_boundary = ctb_addr_in_slice > 0;
1774  slice_up_boundary = ctb_addr_in_slice >= s->sps->ctb_width;
1775  }
1776  lc->slice_or_tiles_left_boundary = (!slice_left_boundary) + (!tile_left_boundary << 1);
1777  lc->slice_or_tiles_up_boundary = (!slice_up_boundary + (!tile_up_boundary << 1));
1778  lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && tile_left_boundary);
1779  lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && tile_up_boundary);
1780  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]]));
1781  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]]));
1782 }
1783 
1784 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
1785 {
1786  HEVCContext *s = avctxt->priv_data;
1787  int ctb_size = 1 << s->sps->log2_ctb_size;
1788  int more_data = 1;
1789  int x_ctb = 0;
1790  int y_ctb = 0;
1791  int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
1792 
1793  if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
1794  av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
1795  return AVERROR_INVALIDDATA;
1796  }
1797 
1798  while (more_data && ctb_addr_ts < s->sps->ctb_size) {
1799  int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1800 
1801  x_ctb = (ctb_addr_rs % ((s->sps->width + (ctb_size - 1)) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
1802  y_ctb = (ctb_addr_rs / ((s->sps->width + (ctb_size - 1)) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
1803  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
1804 
1805  ff_hevc_cabac_init(s, ctb_addr_ts);
1806 
1807  hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
1808 
1809  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
1810  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
1812 
1813  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
1814  if (more_data < 0)
1815  return more_data;
1816 
1817  ctb_addr_ts++;
1818  ff_hevc_save_states(s, ctb_addr_ts);
1819  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
1820  }
1821 
1822  if (x_ctb + ctb_size >= s->sps->width &&
1823  y_ctb + ctb_size >= s->sps->height)
1824  ff_hevc_hls_filter(s, x_ctb, y_ctb);
1825 
1826  return ctb_addr_ts;
1827 }
1828 
1830 {
1831  int arg[2];
1832  int ret[2];
1833 
1834  arg[0] = 0;
1835  arg[1] = 1;
1836 
1837  s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
1838  return ret[0];
1839 }
1840 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
1841 {
1842  HEVCContext *s1 = avctxt->priv_data, *s;
1843  HEVCLocalContext *lc;
1844  int ctb_size = 1<< s1->sps->log2_ctb_size;
1845  int more_data = 1;
1846  int *ctb_row_p = input_ctb_row;
1847  int ctb_row = ctb_row_p[job];
1848  int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->sps->width + ctb_size - 1) >> s1->sps->log2_ctb_size);
1849  int ctb_addr_ts = s1->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
1850  int thread = ctb_row % s1->threads_number;
1851  int ret;
1852 
1853  s = s1->sList[self_id];
1854  lc = s->HEVClc;
1855 
1856  if(ctb_row) {
1857  ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
1858 
1859  if (ret < 0)
1860  return ret;
1861  ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
1862  }
1863 
1864  while(more_data && ctb_addr_ts < s->sps->ctb_size) {
1865  int x_ctb = (ctb_addr_rs % s->sps->ctb_width) << s->sps->log2_ctb_size;
1866  int y_ctb = (ctb_addr_rs / s->sps->ctb_width) << s->sps->log2_ctb_size;
1867 
1868  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
1869 
1870  ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
1871 
1872  if (avpriv_atomic_int_get(&s1->wpp_err)){
1873  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
1874  return 0;
1875  }
1876 
1877  ff_hevc_cabac_init(s, ctb_addr_ts);
1878  hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
1879  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
1880 
1881  if (more_data < 0)
1882  return more_data;
1883 
1884  ctb_addr_ts++;
1885 
1886  ff_hevc_save_states(s, ctb_addr_ts);
1887  ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
1888  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
1889 
1890  if (!more_data && (x_ctb+ctb_size) < s->sps->width && ctb_row != s->sh.num_entry_point_offsets) {
1891  avpriv_atomic_int_set(&s1->wpp_err, 1);
1892  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
1893  return 0;
1894  }
1895 
1896  if ((x_ctb+ctb_size) >= s->sps->width && (y_ctb+ctb_size) >= s->sps->height ) {
1897  ff_hevc_hls_filter(s, x_ctb, y_ctb);
1898  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
1899  return ctb_addr_ts;
1900  }
1901  ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1902  x_ctb+=ctb_size;
1903 
1904  if(x_ctb >= s->sps->width) {
1905  break;
1906  }
1907  }
1908  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
1909 
1910  return 0;
1911 }
1912 
1913 static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
1914 {
1915  HEVCLocalContext *lc = s->HEVClc;
1916  int *ret = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
1917  int *arg = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
1918  int offset;
1919  int startheader, cmpt = 0;
1920  int i, j, res = 0;
1921 
1922 
1923  if (!s->sList[1]) {
1925 
1926 
1927  for (i = 1; i < s->threads_number; i++) {
1928  s->sList[i] = av_malloc(sizeof(HEVCContext));
1929  memcpy(s->sList[i], s, sizeof(HEVCContext));
1930  s->HEVClcList[i] = av_malloc(sizeof(HEVCLocalContext));
1931  s->HEVClcList[i]->edge_emu_buffer = av_malloc((MAX_PB_SIZE + 7) * s->frame->linesize[0]);
1932  s->sList[i]->HEVClc = s->HEVClcList[i];
1933  }
1934  }
1935 
1936  offset = (lc->gb.index >> 3);
1937 
1938  for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < s->skipped_bytes; j++) {
1939  if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
1940  startheader--;
1941  cmpt++;
1942  }
1943  }
1944 
1945  for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
1946  offset += (s->sh.entry_point_offset[i - 1] - cmpt);
1947  for (j = 0, cmpt = 0, startheader = offset
1948  + s->sh.entry_point_offset[i]; j < s->skipped_bytes; j++) {
1949  if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
1950  startheader--;
1951  cmpt++;
1952  }
1953  }
1954  s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
1955  s->sh.offset[i - 1] = offset;
1956 
1957  }
1958  if (s->sh.num_entry_point_offsets != 0) {
1959  offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
1960  s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
1961  s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
1962 
1963  }
1964  s->data = nal;
1965 
1966  for (i = 1; i < s->threads_number; i++) {
1967  s->sList[i]->HEVClc->first_qp_group = 1;
1968  s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
1969  memcpy(s->sList[i], s, sizeof(HEVCContext));
1970  s->sList[i]->HEVClc = s->HEVClcList[i];
1971  }
1972 
1974  ff_reset_entries(s->avctx);
1975 
1976  for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
1977  arg[i] = i;
1978  ret[i] = 0;
1979  }
1980 
1982  s->avctx->execute2(s->avctx, (void *) hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
1983 
1984  for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
1985  res += ret[i];
1986  av_free(ret);
1987  av_free(arg);
1988  return res;
1989 }
1990 
1991 /**
1992  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
1993  * 0 if the unit should be skipped, 1 otherwise
1994  */
1996 {
1997  GetBitContext *gb = &s->HEVClc->gb;
1998  int nuh_layer_id;
1999 
2000  if (get_bits1(gb) != 0)
2001  return AVERROR_INVALIDDATA;
2002 
2003  s->nal_unit_type = get_bits(gb, 6);
2004 
2005  nuh_layer_id = get_bits(gb, 6);
2006  s->temporal_id = get_bits(gb, 3) - 1;
2007  if (s->temporal_id < 0)
2008  return AVERROR_INVALIDDATA;
2009 
2011  "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
2012  s->nal_unit_type, nuh_layer_id, s->temporal_id);
2013 
2014  return nuh_layer_id == 0;
2015 }
2016 
2018 {
2019  int min_pu_size = 1 << s->sps->log2_min_pu_size;
2020  int x, y, c_idx;
2021 
2022  for (c_idx = 0; c_idx < 3; c_idx++) {
2023  ptrdiff_t stride = s->frame->linesize[c_idx];
2024  int hshift = s->sps->hshift[c_idx];
2025  int vshift = s->sps->vshift[c_idx];
2026  for (y = 0; y < s->sps->min_pu_height; y++) {
2027  for (x = 0; x < s->sps->min_pu_width; x++) {
2028  if (s->is_pcm[y * s->sps->min_pu_width + x]) {
2029  int n;
2030  int len = min_pu_size >> hshift;
2031  uint8_t *src = &s->frame->data[c_idx][((y << s->sps->log2_min_pu_size) >> vshift) * stride + (((x << s->sps->log2_min_pu_size) >> hshift) << s->sps->pixel_shift)];
2032  uint8_t *dst = &s->sao_frame->data[c_idx][((y << s->sps->log2_min_pu_size) >> vshift) * stride + (((x << s->sps->log2_min_pu_size) >> hshift) << s->sps->pixel_shift)];
2033  for (n = 0; n < (min_pu_size >> vshift); n++) {
2034  memcpy(dst, src, len);
2035  src += stride;
2036  dst += stride;
2037  }
2038  }
2039  }
2040  }
2041  }
2042 }
2043 
2045 {
2046  HEVCLocalContext *lc = s->HEVClc;
2047  int ret;
2048 
2049  memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2050  memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2051  memset(s->cbf_luma, 0, s->sps->min_tb_width * s->sps->min_tb_height);
2052  memset(s->is_pcm, 0, s->sps->min_pu_width * s->sps->min_pu_height);
2053 
2054  lc->start_of_tiles_x = 0;
2055  s->is_decoded = 0;
2056 
2057  if (s->pps->tiles_enabled_flag)
2058  lc->end_of_tiles_x = s->pps->column_width[0] << s->sps->log2_ctb_size;
2059 
2060  ret = ff_hevc_set_new_ref(s, s->sps->sao_enabled ? &s->sao_frame : &s->frame,
2061  s->poc);
2062  if (ret < 0)
2063  goto fail;
2064 
2066  (MAX_PB_SIZE + 7) * s->ref->frame->linesize[0]);
2067  if (!lc->edge_emu_buffer) {
2068  ret = AVERROR(ENOMEM);
2069  goto fail;
2070  }
2071 
2072  ret = ff_hevc_frame_rps(s);
2073  if (ret < 0) {
2074  av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2075  goto fail;
2076  }
2077 
2079  ret = ff_hevc_output_frame(s, s->output_frame, 0);
2080  if (ret < 0)
2081  goto fail;
2082 
2084 
2085  return 0;
2086 fail:
2087  if (s->ref && s->threads_type == FF_THREAD_FRAME)
2088  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2089  s->ref = NULL;
2090  return ret;
2091 }
2092 
2093 static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
2094 {
2095  HEVCLocalContext *lc = s->HEVClc;
2096  GetBitContext *gb = &lc->gb;
2097  int ctb_addr_ts;
2098  int ret;
2099 
2100  ret = init_get_bits8(gb, nal, length);
2101  if (ret < 0)
2102  return ret;
2103 
2104  ret = hls_nal_unit(s);
2105  if (ret < 0) {
2106  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
2107  s->nal_unit_type);
2109  return ret;
2110  return 0;
2111  } else if (!ret)
2112  return 0;
2113 
2114  switch (s->nal_unit_type) {
2115  case NAL_VPS:
2116  ret = ff_hevc_decode_nal_vps(s);
2117  if (ret < 0)
2118  return ret;
2119  break;
2120  case NAL_SPS:
2121  ret = ff_hevc_decode_nal_sps(s);
2122  if (ret < 0)
2123  return ret;
2124  break;
2125  case NAL_PPS:
2126  ret = ff_hevc_decode_nal_pps(s);
2127  if (ret < 0)
2128  return ret;
2129  break;
2130  case NAL_SEI_PREFIX:
2131  case NAL_SEI_SUFFIX:
2132  ret = ff_hevc_decode_nal_sei(s);
2133  if (ret < 0)
2134  return ret;
2135  break;
2136  case NAL_TRAIL_R:
2137  case NAL_TRAIL_N:
2138  case NAL_TSA_N:
2139  case NAL_TSA_R:
2140  case NAL_STSA_N:
2141  case NAL_STSA_R:
2142  case NAL_BLA_W_LP:
2143  case NAL_BLA_W_RADL:
2144  case NAL_BLA_N_LP:
2145  case NAL_IDR_W_RADL:
2146  case NAL_IDR_N_LP:
2147  case NAL_CRA_NUT:
2148  case NAL_RADL_N:
2149  case NAL_RADL_R:
2150  case NAL_RASL_N:
2151  case NAL_RASL_R:
2152  ret = hls_slice_header(s);
2153  if (ret < 0)
2154  return ret;
2155 
2156  if (s->max_ra == INT_MAX) {
2157  if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
2158  s->max_ra = s->poc;
2159  } else {
2160  if (IS_IDR(s))
2161  s->max_ra = INT_MIN;
2162  }
2163  }
2164 
2165  if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
2166  s->poc <= s->max_ra) {
2167  s->is_decoded = 0;
2168  break;
2169  } else {
2170  if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
2171  s->max_ra = INT_MIN;
2172  }
2173 
2174  if (s->sh.first_slice_in_pic_flag) {
2175  ret = hevc_frame_start(s);
2176  if (ret < 0)
2177  return ret;
2178  } else if (!s->ref) {
2179  av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2180  return AVERROR_INVALIDDATA;
2181  }
2182 
2183  if (!s->sh.dependent_slice_segment_flag &&
2184  s->sh.slice_type != I_SLICE) {
2185  ret = ff_hevc_slice_rpl(s);
2186  if (ret < 0) {
2188  "Error constructing the reference lists for the current slice.\n");
2190  return ret;
2191  }
2192  }
2193 
2194  if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2195  ctb_addr_ts = hls_slice_data_wpp(s, nal, length);
2196  else
2197  ctb_addr_ts = hls_slice_data(s);
2198 
2199  if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
2200  s->is_decoded = 1;
2203  s->sps->sao_enabled)
2204  restore_tqb_pixels(s);
2205  }
2206 
2207  if (ctb_addr_ts < 0)
2208  return ctb_addr_ts;
2209  break;
2210  case NAL_EOS_NUT:
2211  case NAL_EOB_NUT:
2212  s->seq_decode = (s->seq_decode + 1) & 0xff;
2213  s->max_ra = INT_MAX;
2214  break;
2215  case NAL_AUD:
2216  case NAL_FD_NUT:
2217  break;
2218  default:
2219  av_log(s->avctx, AV_LOG_INFO,
2220  "Skipping NAL unit %d\n", s->nal_unit_type);
2221  }
2222 
2223  return 0;
2224 }
2225 
2226 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
2227  between these functions would be nice. */
2229  HEVCNAL *nal)
2230 {
2231  int i, si, di;
2232  uint8_t *dst;
2233 
2234  s->skipped_bytes = 0;
2235 #define STARTCODE_TEST \
2236  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
2237  if (src[i + 2] != 3) { \
2238  /* startcode, so we must be past the end */ \
2239  length = i; \
2240  } \
2241  break; \
2242  }
2243 #if HAVE_FAST_UNALIGNED
2244 #define FIND_FIRST_ZERO \
2245  if (i > 0 && !src[i]) \
2246  i--; \
2247  while (src[i]) \
2248  i++
2249 #if HAVE_FAST_64BIT
2250  for (i = 0; i + 1 < length; i += 9) {
2251  if (!((~AV_RN64A(src + i) &
2252  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
2253  0x8000800080008080ULL))
2254  continue;
2255  FIND_FIRST_ZERO;
2257  i -= 7;
2258  }
2259 #else
2260  for (i = 0; i + 1 < length; i += 5) {
2261  if (!((~AV_RN32A(src + i) &
2262  (AV_RN32A(src + i) - 0x01000101U)) &
2263  0x80008080U))
2264  continue;
2265  FIND_FIRST_ZERO;
2267  i -= 3;
2268  }
2269 #endif
2270 #else
2271  for (i = 0; i + 1 < length; i += 2) {
2272  if (src[i])
2273  continue;
2274  if (i > 0 && src[i - 1] == 0)
2275  i--;
2277  }
2278 #endif
2279 
2280  if (i >= length - 1) { // no escaped 0
2281  nal->data = src;
2282  nal->size = length;
2283  return length;
2284  }
2285 
2287  length + FF_INPUT_BUFFER_PADDING_SIZE);
2288  if (!nal->rbsp_buffer)
2289  return AVERROR(ENOMEM);
2290 
2291  dst = nal->rbsp_buffer;
2292 
2293  memcpy(dst, src, i);
2294  si = di = i;
2295  while (si + 2 < length) {
2296  // remove escapes (very rare 1:2^22)
2297  if (src[si + 2] > 3) {
2298  dst[di++] = src[si++];
2299  dst[di++] = src[si++];
2300  } else if (src[si] == 0 && src[si + 1] == 0) {
2301  if (src[si + 2] == 3) { // escape
2302  dst[di++] = 0;
2303  dst[di++] = 0;
2304  si += 3;
2305 
2306  s->skipped_bytes++;
2307  if (s->skipped_bytes_pos_size < s->skipped_bytes) {
2308  s->skipped_bytes_pos_size *= 2;
2311  sizeof(*s->skipped_bytes_pos));
2312  if (!s->skipped_bytes_pos)
2313  return AVERROR(ENOMEM);
2314  }
2315  if (s->skipped_bytes_pos)
2316  s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
2317  continue;
2318  } else // next start code
2319  goto nsc;
2320  }
2321 
2322  dst[di++] = src[si++];
2323  }
2324  while (si < length)
2325  dst[di++] = src[si++];
2326 nsc:
2327 
2328  memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2329 
2330  nal->data = dst;
2331  nal->size = di;
2332  return si;
2333 }
2334 
2335 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2336 {
2337  int i, consumed, ret = 0;
2338 
2339  s->ref = NULL;
2340  s->eos = 0;
2341 
2342  /* split the input packet into NAL units, so we know the upper bound on the
2343  * number of slices in the frame */
2344  s->nb_nals = 0;
2345  while (length >= 4) {
2346  HEVCNAL *nal;
2347  int extract_length = 0;
2348 
2349  if (s->is_nalff) {
2350  int i;
2351  for (i = 0; i < s->nal_length_size; i++)
2352  extract_length = (extract_length << 8) | buf[i];
2353  buf += s->nal_length_size;
2354  length -= s->nal_length_size;
2355 
2356  if (extract_length > length) {
2357  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
2358  ret = AVERROR_INVALIDDATA;
2359  goto fail;
2360  }
2361  } else {
2362  /* search start code */
2363  while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
2364  ++buf;
2365  --length;
2366  if (length < 4) {
2367  av_log(s->avctx, AV_LOG_ERROR, "No start code is found.\n");
2368  ret = AVERROR_INVALIDDATA;
2369  goto fail;
2370  }
2371  }
2372 
2373  buf += 3;
2374  length -= 3;
2375  }
2376 
2377  if (!s->is_nalff)
2378  extract_length = length;
2379 
2380  if (s->nals_allocated < s->nb_nals + 1) {
2381  int new_size = s->nals_allocated + 1;
2382  HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));
2383  if (!tmp) {
2384  ret = AVERROR(ENOMEM);
2385  goto fail;
2386  }
2387  s->nals = tmp;
2388  memset(s->nals + s->nals_allocated, 0, (new_size - s->nals_allocated) * sizeof(*tmp));
2389  av_reallocp_array(&s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
2392  s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size
2394  s->nals_allocated = new_size;
2395  }
2398  nal = &s->nals[s->nb_nals];
2399 
2400  consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
2401 
2405 
2406 
2407  if (consumed < 0) {
2408  ret = consumed;
2409  goto fail;
2410  }
2411 
2412  ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);
2413  if (ret < 0)
2414  goto fail;
2415  hls_nal_unit(s);
2416 
2417  if (s->nal_unit_type == NAL_EOS_NUT ||
2418  s->nal_unit_type == NAL_EOB_NUT)
2419  s->eos = 1;
2420 
2421  buf += consumed;
2422  length -= consumed;
2423  }
2424 
2425  /* parse the NAL units */
2426  for (i = 0; i < s->nb_nals; i++) {
2427  int ret;
2428  s->skipped_bytes = s->skipped_bytes_nal[i];
2430 
2431  ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
2432  if (ret < 0) {
2434  "Error parsing NAL unit #%d.\n", i);
2436  goto fail;
2437  }
2438  }
2439 
2440 fail:
2441  if (s->ref && s->threads_type == FF_THREAD_FRAME)
2442  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2443 
2444  return ret;
2445 }
2446 
2447 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2448 {
2449  int i;
2450  for (i = 0; i < 16; i++)
2451  av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2452 }
2453 
2455 {
2456  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2457  int pixel_shift;
2458  int i, j;
2459 
2460  if (!desc)
2461  return AVERROR(EINVAL);
2462 
2463  pixel_shift = desc->comp[0].depth_minus1 > 7;
2464 
2465  av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2466  s->poc);
2467 
2468  /* the checksums are LE, so we have to byteswap for >8bpp formats
2469  * on BE arches */
2470 #if HAVE_BIGENDIAN
2471  if (pixel_shift && !s->checksum_buf) {
2473  FFMAX3(frame->linesize[0], frame->linesize[1],
2474  frame->linesize[2]));
2475  if (!s->checksum_buf)
2476  return AVERROR(ENOMEM);
2477  }
2478 #endif
2479 
2480  for (i = 0; frame->data[i]; i++) {
2481  int width = s->avctx->coded_width;
2482  int height = s->avctx->coded_height;
2483  int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
2484  int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2485  uint8_t md5[16];
2486 
2487  av_md5_init(s->md5_ctx);
2488  for (j = 0; j < h; j++) {
2489  const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2490 #if HAVE_BIGENDIAN
2491  if (pixel_shift) {
2492  s->dsp.bswap16_buf((uint16_t*)s->checksum_buf,
2493  (const uint16_t*)src, w);
2494  src = s->checksum_buf;
2495  }
2496 #endif
2497  av_md5_update(s->md5_ctx, src, w << pixel_shift);
2498  }
2499  av_md5_final(s->md5_ctx, md5);
2500 
2501  if (!memcmp(md5, s->md5[i], 16)) {
2502  av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2503  print_md5(s->avctx, AV_LOG_DEBUG, md5);
2504  av_log (s->avctx, AV_LOG_DEBUG, "; ");
2505  } else {
2506  av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2507  print_md5(s->avctx, AV_LOG_ERROR, md5);
2508  av_log (s->avctx, AV_LOG_ERROR, " != ");
2509  print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2510  av_log (s->avctx, AV_LOG_ERROR, "\n");
2511  return AVERROR_INVALIDDATA;
2512  }
2513  }
2514 
2515  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2516 
2517  return 0;
2518 }
2519 
2520 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
2521  AVPacket *avpkt)
2522 {
2523  int ret;
2524  HEVCContext *s = avctx->priv_data;
2525 
2526  if (!avpkt->size) {
2527  ret = ff_hevc_output_frame(s, data, 1);
2528  if (ret < 0)
2529  return ret;
2530 
2531  *got_output = ret;
2532  return 0;
2533  }
2534 
2535  s->ref = NULL;
2536  ret = decode_nal_units(s, avpkt->data, avpkt->size);
2537  if (ret < 0)
2538  return ret;
2539 
2540  /* verify the SEI checksum */
2541  if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
2542  avctx->err_recognition & AV_EF_EXPLODE &&
2543  s->is_md5) {
2544  ret = verify_md5(s, s->ref->frame);
2545  if (ret < 0) {
2546  ff_hevc_unref_frame(s, s->ref, ~0);
2547  return ret;
2548  }
2549  }
2550  s->is_md5 = 0;
2551 
2552  if (s->is_decoded) {
2553  av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
2554  s->is_decoded = 0;
2555  }
2556 
2557  if (s->output_frame->buf[0]) {
2558  av_frame_move_ref(data, s->output_frame);
2559  *got_output = 1;
2560  }
2561 
2562  return avpkt->size;
2563 }
2564 
2566 {
2567  int ret;
2568 
2569  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
2570  if (ret < 0)
2571  return ret;
2572 
2573  dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
2574  if (!dst->tab_mvf_buf)
2575  goto fail;
2576  dst->tab_mvf = src->tab_mvf;
2577 
2578  dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
2579  if (!dst->rpl_tab_buf)
2580  goto fail;
2581  dst->rpl_tab = src->rpl_tab;
2582 
2583  dst->rpl_buf = av_buffer_ref(src->rpl_buf);
2584  if (!dst->rpl_buf)
2585  goto fail;
2586 
2587  dst->poc = src->poc;
2588  dst->ctb_count = src->ctb_count;
2589  dst->window = src->window;
2590  dst->flags = src->flags;
2591  dst->sequence = src->sequence;
2592 
2593  return 0;
2594 fail:
2595  ff_hevc_unref_frame(s, dst, ~0);
2596  return AVERROR(ENOMEM);
2597 }
2598 
2600 {
2601  HEVCContext *s = avctx->priv_data;
2602  HEVCLocalContext *lc = s->HEVClc;
2603  int i;
2604 
2605  pic_arrays_free(s);
2606 
2607  if (lc)
2608  av_freep(&lc->edge_emu_buffer);
2609  av_freep(&s->md5_ctx);
2610 
2611  for(i=0; i < s->nals_allocated; i++) {
2613  }
2617 
2618  av_freep(&s->cabac_state);
2619 
2620  av_frame_free(&s->tmp_frame);
2622 
2623  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2624  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2625  av_frame_free(&s->DPB[i].frame);
2626  }
2627 
2628  for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
2629  av_freep(&s->vps_list[i]);
2630  for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
2631  av_buffer_unref(&s->sps_list[i]);
2632  for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
2633  av_buffer_unref(&s->pps_list[i]);
2634 
2636  av_freep(&s->sh.offset);
2637  av_freep(&s->sh.size);
2638 
2639  for (i = 1; i < s->threads_number; i++) {
2640  lc = s->HEVClcList[i];
2641  if (lc) {
2642  av_freep(&lc->edge_emu_buffer);
2643 
2644  av_freep(&s->HEVClcList[i]);
2645  av_freep(&s->sList[i]);
2646  }
2647  }
2648  if (s->HEVClc == s->HEVClcList[0])
2649  s->HEVClc = NULL;
2650  av_freep(&s->HEVClcList[0]);
2651 
2652  for (i = 0; i < s->nals_allocated; i++)
2653  av_freep(&s->nals[i].rbsp_buffer);
2654  av_freep(&s->nals);
2655  s->nals_allocated = 0;
2656 
2657  return 0;
2658 }
2659 
2661 {
2662  HEVCContext *s = avctx->priv_data;
2663  int i;
2664 
2665  s->avctx = avctx;
2666 
2667  s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
2668  if (!s->HEVClc)
2669  goto fail;
2670  s->HEVClcList[0] = s->HEVClc;
2671  s->sList[0] = s;
2672 
2674  if (!s->cabac_state)
2675  goto fail;
2676 
2677  s->tmp_frame = av_frame_alloc();
2678  if (!s->tmp_frame)
2679  goto fail;
2680 
2682  if (!s->output_frame)
2683  goto fail;
2684 
2685  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2686  s->DPB[i].frame = av_frame_alloc();
2687  if (!s->DPB[i].frame)
2688  goto fail;
2689  s->DPB[i].tf.f = s->DPB[i].frame;
2690  }
2691 
2692  s->max_ra = INT_MAX;
2693 
2694  s->md5_ctx = av_md5_alloc();
2695  if (!s->md5_ctx)
2696  goto fail;
2697 
2698  ff_dsputil_init(&s->dsp, avctx);
2699 
2700  s->context_initialized = 1;
2701 
2702  return 0;
2703 fail:
2704  hevc_decode_free(avctx);
2705  return AVERROR(ENOMEM);
2706 }
2707 
2709  const AVCodecContext *src)
2710 {
2711  HEVCContext *s = dst->priv_data;
2712  HEVCContext *s0 = src->priv_data;
2713  int i, ret;
2714 
2715  if (!s->context_initialized) {
2716  ret = hevc_init_context(dst);
2717  if (ret < 0)
2718  return ret;
2719  }
2720 
2721  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2722  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2723  if (s0->DPB[i].frame->buf[0]) {
2724  ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
2725  if (ret < 0)
2726  return ret;
2727  }
2728  }
2729 
2730  for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
2731  av_buffer_unref(&s->sps_list[i]);
2732  if (s0->sps_list[i]) {
2733  s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
2734  if (!s->sps_list[i])
2735  return AVERROR(ENOMEM);
2736  }
2737  }
2738 
2739  for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
2740  av_buffer_unref(&s->pps_list[i]);
2741  if (s0->pps_list[i]) {
2742  s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
2743  if (!s->pps_list[i])
2744  return AVERROR(ENOMEM);
2745  }
2746  }
2747 
2748  s->seq_decode = s0->seq_decode;
2749  s->seq_output = s0->seq_output;
2750  s->pocTid0 = s0->pocTid0;
2751  s->max_ra = s0->max_ra;
2752 
2753  s->is_nalff = s0->is_nalff;
2755 
2756  s->threads_number = s0->threads_number;
2757  s->threads_type = s0->threads_type;
2758 
2759  if (s0->eos) {
2760  s->seq_decode = (s->seq_decode + 1) & 0xff;
2761  s->max_ra = INT_MAX;
2762  }
2763 
2764  return 0;
2765 }
2766 
2768 {
2769  AVCodecContext *avctx = s->avctx;
2770  GetByteContext gb;
2771  int ret;
2772 
2773  bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
2774 
2775  if (avctx->extradata_size > 3 &&
2776  (avctx->extradata[0] || avctx->extradata[1] ||
2777  avctx->extradata[2] > 1)) {
2778  /* It seems the extradata is encoded as hvcC format.
2779  * Temporarily, we support configurationVersion==0 until 14496-15 3rd finalized.
2780  * When finalized, configurationVersion will be 1 and we can recognize hvcC by
2781  * checking if avctx->extradata[0]==1 or not. */
2782  int i, j, num_arrays;
2783  int nal_len_size;
2784 
2785  s->is_nalff = 1;
2786 
2787  bytestream2_skip(&gb, 21);
2788  nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
2789  num_arrays = bytestream2_get_byte(&gb);
2790 
2791  /* nal units in the hvcC always have length coded with 2 bytes,
2792  * so put a fake nal_length_size = 2 while parsing them */
2793  s->nal_length_size = 2;
2794 
2795  /* Decode nal units from hvcC. */
2796  for (i = 0; i < num_arrays; i++) {
2797  int type = bytestream2_get_byte(&gb) & 0x3f;
2798  int cnt = bytestream2_get_be16(&gb);
2799 
2800  for (j = 0; j < cnt; j++) {
2801  // +2 for the nal size field
2802  int nalsize = bytestream2_peek_be16(&gb) + 2;
2803  if (bytestream2_get_bytes_left(&gb) < nalsize) {
2805  "Invalid NAL unit size in extradata.\n");
2806  return AVERROR_INVALIDDATA;
2807  }
2808 
2809  ret = decode_nal_units(s, gb.buffer, nalsize);
2810  if (ret < 0) {
2811  av_log(avctx, AV_LOG_ERROR,
2812  "Decoding nal unit %d %d from hvcC failed\n", type, i);
2813  return ret;
2814  }
2815  bytestream2_skip(&gb, nalsize);
2816  }
2817  }
2818 
2819  /* Now store right nal length size, that will be used to parse all other nals */
2820  s->nal_length_size = nal_len_size;
2821  } else {
2822  s->is_nalff = 0;
2823  ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
2824  if (ret < 0)
2825  return ret;
2826  }
2827  return 0;
2828 }
2829 
2831 {
2832  HEVCContext *s = avctx->priv_data;
2833  int ret;
2834 
2836 
2837  avctx->internal->allocate_progress = 1;
2838 
2839  ret = hevc_init_context(avctx);
2840  if (ret < 0)
2841  return ret;
2842 
2843  s->enable_parallel_tiles = 0;
2844 
2845  if(avctx->active_thread_type & FF_THREAD_SLICE)
2846  s->threads_number = avctx->thread_count;
2847  else
2848  s->threads_number = 1;
2849 
2850  if (avctx->extradata_size > 0 && avctx->extradata) {
2851  ret = hevc_decode_extradata(s);
2852  if (ret < 0) {
2853  hevc_decode_free(avctx);
2854  return ret;
2855  }
2856  }
2857 
2858  if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
2860  else
2862 
2863  return 0;
2864 }
2865 
2867 {
2868  HEVCContext *s = avctx->priv_data;
2869  int ret;
2870 
2871  memset(s, 0, sizeof(*s));
2872 
2873  ret = hevc_init_context(avctx);
2874  if (ret < 0)
2875  return ret;
2876 
2877  return 0;
2878 }
2879 
2881 {
2882  HEVCContext *s = avctx->priv_data;
2883  ff_hevc_flush_dpb(s);
2884  s->max_ra = INT_MAX;
2885 }
2886 
2887 #define OFFSET(x) offsetof(HEVCContext, x)
2888 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
2889 static const AVOption options[] = {
2890  { "strict-displaywin", "stricly apply default display window size", OFFSET(strict_def_disp_win),
2891  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
2892  { NULL },
2893 };
2894 
2895 static const AVClass hevc_decoder_class = {
2896  .class_name = "HEVC decoder",
2897  .item_name = av_default_item_name,
2898  .option = options,
2899  .version = LIBAVUTIL_VERSION_INT,
2900 };
2901 
2903  .name = "hevc",
2904  .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
2905  .type = AVMEDIA_TYPE_VIDEO,
2906  .id = AV_CODEC_ID_HEVC,
2907  .priv_data_size = sizeof(HEVCContext),
2908  .priv_class = &hevc_decoder_class,
2916 };