FFmpeg  2.1.1
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_malloc(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  for (i = 0; i < sh->num_entry_point_offsets; i++) {
606  int val = 0;
607  for (j = 0; j < segments; j++) {
608  val <<= 16;
609  val += get_bits(gb, 16);
610  }
611  if (rest) {
612  val <<= rest;
613  val += get_bits(gb, rest);
614  }
615  sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
616  }
617  if (s->threads_number > 1 && (s->pps->num_tile_rows > 1 || s->pps->num_tile_columns > 1)) {
618  s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
619  s->threads_number = 1;
620  } else
621  s->enable_parallel_tiles = 0;
622  } else
623  s->enable_parallel_tiles = 0;
624  }
625 
627  int length = get_ue_golomb_long(gb);
628  for (i = 0; i < length; i++)
629  skip_bits(gb, 8); // slice_header_extension_data_byte
630  }
631 
632  // Inferred parameters
633  sh->slice_qp = 26 + s->pps->pic_init_qp_minus26 + sh->slice_qp_delta;
635 
637 
638  if (!s->pps->cu_qp_delta_enabled_flag)
639  s->HEVClc->qp_y = ((s->sh.slice_qp + 52 + 2 * s->sps->qp_bd_offset) %
640  (52 + s->sps->qp_bd_offset)) - s->sps->qp_bd_offset;
641 
642  s->slice_initialized = 1;
643 
644  return 0;
645 }
646 
647 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
648 
649 #define SET_SAO(elem, value) \
650 do { \
651  if (!sao_merge_up_flag && !sao_merge_left_flag) \
652  sao->elem = value; \
653  else if (sao_merge_left_flag) \
654  sao->elem = CTB(s->sao, rx-1, ry).elem; \
655  else if (sao_merge_up_flag) \
656  sao->elem = CTB(s->sao, rx, ry-1).elem; \
657  else \
658  sao->elem = 0; \
659 } while (0)
660 
661 static void hls_sao_param(HEVCContext *s, int rx, int ry)
662 {
663  HEVCLocalContext *lc = s->HEVClc;
664  int sao_merge_left_flag = 0;
665  int sao_merge_up_flag = 0;
666  int shift = s->sps->bit_depth - FFMIN(s->sps->bit_depth, 10);
667  SAOParams *sao = &CTB(s->sao, rx, ry);
668  int c_idx, i;
669 
672  if (rx > 0) {
673  if (lc->ctb_left_flag)
674  sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
675  }
676  if (ry > 0 && !sao_merge_left_flag) {
677  if (lc->ctb_up_flag)
678  sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
679  }
680  }
681 
682  for (c_idx = 0; c_idx < 3; c_idx++) {
683  if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
684  sao->type_idx[c_idx] = SAO_NOT_APPLIED;
685  continue;
686  }
687 
688  if (c_idx == 2) {
689  sao->type_idx[2] = sao->type_idx[1];
690  sao->eo_class[2] = sao->eo_class[1];
691  } else {
692  SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
693  }
694 
695  if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
696  continue;
697 
698  for (i = 0; i < 4; i++)
699  SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
700 
701  if (sao->type_idx[c_idx] == SAO_BAND) {
702  for (i = 0; i < 4; i++) {
703  if (sao->offset_abs[c_idx][i]) {
704  SET_SAO(offset_sign[c_idx][i], ff_hevc_sao_offset_sign_decode(s));
705  } else {
706  sao->offset_sign[c_idx][i] = 0;
707  }
708  }
709  SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
710  } else if (c_idx != 2) {
711  SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
712  }
713 
714  // Inferred parameters
715  sao->offset_val[c_idx][0] = 0;
716  for (i = 0; i < 4; i++) {
717  sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i] << shift;
718  if (sao->type_idx[c_idx] == SAO_EDGE) {
719  if (i > 1)
720  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
721  } else if (sao->offset_sign[c_idx][i]) {
722  sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
723  }
724  }
725  }
726 }
727 
728 #undef SET_SAO
729 #undef CTB
730 
731 
732 static void hls_transform_unit(HEVCContext *s, int x0, int y0,
733  int xBase, int yBase, int cb_xBase, int cb_yBase,
734  int log2_cb_size, int log2_trafo_size,
735  int trafo_depth, int blk_idx)
736 {
737  HEVCLocalContext *lc = s->HEVClc;
738 
739  if (lc->cu.pred_mode == MODE_INTRA) {
740  int trafo_size = 1 << log2_trafo_size;
741  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
742 
743  s->hpc.intra_pred(s, x0, y0, log2_trafo_size, 0);
744  if (log2_trafo_size > 2) {
745  trafo_size = trafo_size << (s->sps->hshift[1] - 1);
746  ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
747  s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 1);
748  s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 2);
749  } else if (blk_idx == 3) {
750  trafo_size = trafo_size << (s->sps->hshift[1]);
751  ff_hevc_set_neighbour_available(s, xBase, yBase, trafo_size, trafo_size);
752  s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 1);
753  s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 2);
754  }
755  }
756 
757  if (lc->tt.cbf_luma ||
758  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
759  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
760  int scan_idx = SCAN_DIAG;
761  int scan_idx_c = SCAN_DIAG;
762 
765  if (lc->tu.cu_qp_delta != 0)
766  if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
767  lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
768  lc->tu.is_cu_qp_delta_coded = 1;
769  ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
770  }
771 
772  if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
773  if (lc->tu.cur_intra_pred_mode >= 6 &&
774  lc->tu.cur_intra_pred_mode <= 14) {
775  scan_idx = SCAN_VERT;
776  } else if (lc->tu.cur_intra_pred_mode >= 22 &&
777  lc->tu.cur_intra_pred_mode <= 30) {
778  scan_idx = SCAN_HORIZ;
779  }
780 
781  if (lc->pu.intra_pred_mode_c >= 6 &&
782  lc->pu.intra_pred_mode_c <= 14) {
783  scan_idx_c = SCAN_VERT;
784  } else if (lc->pu.intra_pred_mode_c >= 22 &&
785  lc->pu.intra_pred_mode_c <= 30) {
786  scan_idx_c = SCAN_HORIZ;
787  }
788  }
789 
790  if (lc->tt.cbf_luma)
791  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
792  if (log2_trafo_size > 2) {
793  if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0))
794  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
795  if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0))
796  ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
797  } else if (blk_idx == 3) {
798  if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], xBase, yBase))
799  ff_hevc_hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
800  if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], xBase, yBase))
801  ff_hevc_hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
802  }
803  }
804 }
805 
806 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
807 {
808  int cb_size = 1 << log2_cb_size;
809  int log2_min_pu_size = s->sps->log2_min_pu_size;
810 
811  int min_pu_width = s->sps->min_pu_width;
812  int x_end = FFMIN(x0 + cb_size, s->sps->width);
813  int y_end = FFMIN(y0 + cb_size, s->sps->height);
814  int i, j;
815 
816  for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
817  for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
818  s->is_pcm[i + j * min_pu_width] = 2;
819 }
820 
821 static void hls_transform_tree(HEVCContext *s, int x0, int y0,
822  int xBase, int yBase, int cb_xBase, int cb_yBase,
823  int log2_cb_size, int log2_trafo_size,
824  int trafo_depth, int blk_idx)
825 {
826  HEVCLocalContext *lc = s->HEVClc;
827  uint8_t split_transform_flag;
828 
829  if (trafo_depth > 0 && log2_trafo_size == 2) {
830  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
831  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase);
832  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
833  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase);
834  } else {
835  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
836  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) = 0;
837  }
838 
839  if (lc->cu.intra_split_flag) {
840  if (trafo_depth == 1)
841  lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
842  } else {
844  }
845 
846  lc->tt.cbf_luma = 1;
847 
849  lc->cu.pred_mode == MODE_INTER &&
850  lc->cu.part_mode != PART_2Nx2N && trafo_depth == 0);
851 
852  if (log2_trafo_size <= s->sps->log2_max_trafo_size &&
853  log2_trafo_size > s->sps->log2_min_tb_size &&
854  trafo_depth < lc->cu.max_trafo_depth &&
855  !(lc->cu.intra_split_flag && trafo_depth == 0)) {
856  split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
857  } else {
858  split_transform_flag = (log2_trafo_size > s->sps->log2_max_trafo_size ||
859  (lc->cu.intra_split_flag && (trafo_depth == 0)) ||
860  lc->tt.inter_split_flag);
861  }
862 
863  if (log2_trafo_size > 2) {
864  if (trafo_depth == 0 ||
865  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase)) {
866  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
867  ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
868  }
869 
870  if (trafo_depth == 0 || SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase)) {
871  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
872  ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
873  }
874  }
875 
876  if (split_transform_flag) {
877  int x1 = x0 + ((1 << log2_trafo_size) >> 1);
878  int y1 = y0 + ((1 << log2_trafo_size) >> 1);
879 
880  hls_transform_tree(s, x0, y0, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
881  log2_trafo_size - 1, trafo_depth + 1, 0);
882  hls_transform_tree(s, x1, y0, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
883  log2_trafo_size - 1, trafo_depth + 1, 1);
884  hls_transform_tree(s, x0, y1, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
885  log2_trafo_size - 1, trafo_depth + 1, 2);
886  hls_transform_tree(s, x1, y1, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
887  log2_trafo_size - 1, trafo_depth + 1, 3);
888  } else {
889  int min_tu_size = 1 << s->sps->log2_min_tb_size;
890  int log2_min_tu_size = s->sps->log2_min_tb_size;
891  int min_tu_width = s->sps->min_tb_width;
892 
893  if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
894  SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
895  SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
896  lc->tt.cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
897  }
898 
899  hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
900  log2_cb_size, log2_trafo_size, trafo_depth, blk_idx);
901 
902  // TODO: store cbf_luma somewhere else
903  if (lc->tt.cbf_luma) {
904  int i, j;
905  for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
906  for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
907  int x_tu = (x0 + j) >> log2_min_tu_size;
908  int y_tu = (y0 + i) >> log2_min_tu_size;
909  s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
910  }
911  }
913  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size,
917  set_deblocking_bypass(s, x0, y0, log2_trafo_size);
918  }
919  }
920 }
921 
922 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
923 {
924  //TODO: non-4:2:0 support
925  HEVCLocalContext *lc = s->HEVClc;
926  GetBitContext gb;
927  int cb_size = 1 << log2_cb_size;
928  int stride0 = s->frame->linesize[0];
929  uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->sps->pixel_shift)];
930  int stride1 = s->frame->linesize[1];
931  uint8_t *dst1 = &s->frame->data[1][(y0 >> s->sps->vshift[1]) * stride1 + ((x0 >> s->sps->hshift[1]) << s->sps->pixel_shift)];
932  int stride2 = s->frame->linesize[2];
933  uint8_t *dst2 = &s->frame->data[2][(y0 >> s->sps->vshift[2]) * stride2 + ((x0 >> s->sps->hshift[2]) << s->sps->pixel_shift)];
934 
935  int length = cb_size * cb_size * s->sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->sps->pcm.bit_depth;
936  const uint8_t *pcm = skip_bytes(&s->HEVClc->cc, (length + 7) >> 3);
937  int ret;
938 
939  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
942 
943  ret = init_get_bits(&gb, pcm, length);
944  if (ret < 0)
945  return ret;
946 
947  s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->sps->pcm.bit_depth);
948  s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
949  s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
950  return 0;
951 }
952 
953 /**
954  * 8.5.3.2.2.1 Luma sample interpolation process
955  *
956  * @param s HEVC decoding context
957  * @param dst target buffer for block data at block position
958  * @param dststride stride of the dst buffer
959  * @param ref reference picture buffer at origin (0, 0)
960  * @param mv motion vector (relative to block position) to get pixel data from
961  * @param x_off horizontal position of block from origin (0, 0)
962  * @param y_off vertical position of block from origin (0, 0)
963  * @param block_w width of block
964  * @param block_h height of block
965  */
966 static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride,
967  AVFrame *ref, const Mv *mv, int x_off, int y_off,
968  int block_w, int block_h)
969 {
970  HEVCLocalContext *lc = s->HEVClc;
971  uint8_t *src = ref->data[0];
972  ptrdiff_t srcstride = ref->linesize[0];
973  int pic_width = s->sps->width;
974  int pic_height = s->sps->height;
975 
976  int mx = mv->x & 3;
977  int my = mv->y & 3;
978  int extra_left = ff_hevc_qpel_extra_before[mx];
979  int extra_top = ff_hevc_qpel_extra_before[my];
980 
981  x_off += mv->x >> 2;
982  y_off += mv->y >> 2;
983  src += y_off * srcstride + (x_off << s->sps->pixel_shift);
984 
985  if (x_off < extra_left || y_off < extra_top ||
986  x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
987  y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
988  int offset = extra_top * srcstride + (extra_left << s->sps->pixel_shift);
989 
990  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, srcstride, src - offset, srcstride,
991  block_w + ff_hevc_qpel_extra[mx], block_h + ff_hevc_qpel_extra[my],
992  x_off - extra_left, y_off - extra_top,
993  pic_width, pic_height);
994  src = lc->edge_emu_buffer + offset;
995  }
996  s->hevcdsp.put_hevc_qpel[my][mx](dst, dststride, src, srcstride, block_w,
997  block_h, lc->mc_buffer);
998 }
999 
1000 /**
1001  * 8.5.3.2.2.2 Chroma sample interpolation process
1002  *
1003  * @param s HEVC decoding context
1004  * @param dst1 target buffer for block data at block position (U plane)
1005  * @param dst2 target buffer for block data at block position (V plane)
1006  * @param dststride stride of the dst1 and dst2 buffers
1007  * @param ref reference picture buffer at origin (0, 0)
1008  * @param mv motion vector (relative to block position) to get pixel data from
1009  * @param x_off horizontal position of block from origin (0, 0)
1010  * @param y_off vertical position of block from origin (0, 0)
1011  * @param block_w width of block
1012  * @param block_h height of block
1013  */
1014 static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2, ptrdiff_t dststride, AVFrame *ref,
1015  const Mv *mv, int x_off, int y_off, int block_w, int block_h)
1016 {
1017  HEVCLocalContext *lc = s->HEVClc;
1018  uint8_t *src1 = ref->data[1];
1019  uint8_t *src2 = ref->data[2];
1020  ptrdiff_t src1stride = ref->linesize[1];
1021  ptrdiff_t src2stride = ref->linesize[2];
1022  int pic_width = s->sps->width >> 1;
1023  int pic_height = s->sps->height >> 1;
1024 
1025  int mx = mv->x & 7;
1026  int my = mv->y & 7;
1027 
1028  x_off += mv->x >> 3;
1029  y_off += mv->y >> 3;
1030  src1 += y_off * src1stride + (x_off << s->sps->pixel_shift);
1031  src2 += y_off * src2stride + (x_off << s->sps->pixel_shift);
1032 
1033  if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1034  x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1035  y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1036  int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->sps->pixel_shift));
1037  int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->sps->pixel_shift));
1038 
1039  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1stride, src1 - offset1, src1stride,
1040  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1041  x_off - EPEL_EXTRA_BEFORE,
1042  y_off - EPEL_EXTRA_BEFORE,
1043  pic_width, pic_height);
1044 
1045  src1 = lc->edge_emu_buffer + offset1;
1046  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1047  block_w, block_h, mx, my, lc->mc_buffer);
1048 
1049  s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2stride, src2 - offset2, src2stride,
1050  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1051  x_off - EPEL_EXTRA_BEFORE,
1052  y_off - EPEL_EXTRA_BEFORE,
1053  pic_width, pic_height);
1054  src2 = lc->edge_emu_buffer + offset2;
1055  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1056  block_w, block_h, mx, my,
1057  lc->mc_buffer);
1058  } else {
1059  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1060  block_w, block_h, mx, my,
1061  lc->mc_buffer);
1062  s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1063  block_w, block_h, mx, my,
1064  lc->mc_buffer);
1065  }
1066 }
1067 
1069  const Mv *mv, int y0, int height)
1070 {
1071  int y = (mv->y >> 2) + y0 + height + 9;
1072 
1073  if (s->threads_type == FF_THREAD_FRAME )
1074  ff_thread_await_progress(&ref->tf, y, 0);
1075 }
1076 
1077 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1078  int nPbW, int nPbH,
1079  int log2_cb_size, int partIdx)
1080 {
1081 #define POS(c_idx, x, y) \
1082  &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1083  (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
1084  HEVCLocalContext *lc = s->HEVClc;
1085  int merge_idx = 0;
1086  struct MvField current_mv = {{{ 0 }}};
1087 
1088  int min_pu_width = s->sps->min_pu_width;
1089 
1090  MvField *tab_mvf = s->ref->tab_mvf;
1091  RefPicList *refPicList = s->ref->refPicList;
1092  HEVCFrame *ref0, *ref1;
1093 
1094  int tmpstride = MAX_PB_SIZE;
1095 
1096  uint8_t *dst0 = POS(0, x0, y0);
1097  uint8_t *dst1 = POS(1, x0, y0);
1098  uint8_t *dst2 = POS(2, x0, y0);
1099  int log2_min_cb_size = s->sps->log2_min_cb_size;
1100  int min_cb_width = s->sps->min_cb_width;
1101  int x_cb = x0 >> log2_min_cb_size;
1102  int y_cb = y0 >> log2_min_cb_size;
1103  int ref_idx[2];
1104  int mvp_flag[2];
1105  int x_pu, y_pu;
1106  int i, j;
1107 
1108  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
1109  if (s->sh.max_num_merge_cand > 1)
1110  merge_idx = ff_hevc_merge_idx_decode(s);
1111  else
1112  merge_idx = 0;
1113 
1114  ff_hevc_luma_mv_merge_mode(s, x0, y0, 1 << log2_cb_size, 1 << log2_cb_size,
1115  log2_cb_size, partIdx, merge_idx, &current_mv);
1116  x_pu = x0 >> s->sps->log2_min_pu_size;
1117  y_pu = y0 >> s->sps->log2_min_pu_size;
1118 
1119  for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1120  for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1121  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1122  } else { /* MODE_INTER */
1124  if (lc->pu.merge_flag) {
1125  if (s->sh.max_num_merge_cand > 1)
1126  merge_idx = ff_hevc_merge_idx_decode(s);
1127  else
1128  merge_idx = 0;
1129 
1130  ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1131  partIdx, merge_idx, &current_mv);
1132  x_pu = x0 >> s->sps->log2_min_pu_size;
1133  y_pu = y0 >> s->sps->log2_min_pu_size;
1134 
1135  for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1136  for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1137  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1138  } else {
1139  enum InterPredIdc inter_pred_idc = PRED_L0;
1140  ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1141  if (s->sh.slice_type == B_SLICE)
1142  inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1143 
1144  if (inter_pred_idc != PRED_L1) {
1145  if (s->sh.nb_refs[L0]) {
1146  ref_idx[0] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1147  current_mv.ref_idx[0] = ref_idx[0];
1148  }
1149  current_mv.pred_flag[0] = 1;
1150  ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1151  mvp_flag[0] = ff_hevc_mvp_lx_flag_decode(s);
1152  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1153  partIdx, merge_idx, &current_mv, mvp_flag[0], 0);
1154  current_mv.mv[0].x += lc->pu.mvd.x;
1155  current_mv.mv[0].y += lc->pu.mvd.y;
1156  }
1157 
1158  if (inter_pred_idc != PRED_L0) {
1159  if (s->sh.nb_refs[L1]) {
1160  ref_idx[1] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1161  current_mv.ref_idx[1] = ref_idx[1];
1162  }
1163 
1164  if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1165  lc->pu.mvd.x = 0;
1166  lc->pu.mvd.y = 0;
1167  } else {
1168  ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1169  }
1170 
1171  current_mv.pred_flag[1] = 1;
1172  mvp_flag[1] = ff_hevc_mvp_lx_flag_decode(s);
1173  ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1174  partIdx, merge_idx, &current_mv, mvp_flag[1], 1);
1175  current_mv.mv[1].x += lc->pu.mvd.x;
1176  current_mv.mv[1].y += lc->pu.mvd.y;
1177  }
1178 
1179  x_pu = x0 >> s->sps->log2_min_pu_size;
1180  y_pu = y0 >> s->sps->log2_min_pu_size;
1181 
1182  for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1183  for(j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1184  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1185  }
1186  }
1187 
1188  if (current_mv.pred_flag[0]) {
1189  ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1190  if (!ref0)
1191  return;
1192  hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1193  }
1194  if (current_mv.pred_flag[1]) {
1195  ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1196  if (!ref1)
1197  return;
1198  hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1199  }
1200 
1201  if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
1202  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1203  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1204 
1205  luma_mc(s, tmp, tmpstride, ref0->frame,
1206  &current_mv.mv[0], x0, y0, nPbW, nPbH);
1207 
1208  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1209  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1211  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1212  s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1213  dst0, s->frame->linesize[0], tmp,
1214  tmpstride, nPbW, nPbH);
1215  } else {
1216  s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1217  }
1218  chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1219  &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1220 
1221  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1222  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1224  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1225  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1226  dst1, s->frame->linesize[1], tmp, tmpstride,
1227  nPbW / 2, nPbH / 2);
1229  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1230  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1231  dst2, s->frame->linesize[2], tmp2, tmpstride,
1232  nPbW / 2, nPbH / 2);
1233  } else {
1234  s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1235  s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1236  }
1237  } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1238  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1239  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1240 
1241  if (!ref1)
1242  return;
1243 
1244  luma_mc(s, tmp, tmpstride, ref1->frame,
1245  &current_mv.mv[1], x0, y0, nPbW, nPbH);
1246 
1247  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1248  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1250  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1251  s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1252  dst0, s->frame->linesize[0], tmp, tmpstride,
1253  nPbW, nPbH);
1254  } else {
1255  s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1256  }
1257 
1258  chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
1259  &current_mv.mv[1], x0/2, y0/2, nPbW/2, nPbH/2);
1260 
1261  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1262  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1264  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1265  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1266  dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1268  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1269  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1270  dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1271  } else {
1272  s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1273  s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1274  }
1275  } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1276  DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1277  DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1278  DECLARE_ALIGNED(16, int16_t, tmp3[MAX_PB_SIZE * MAX_PB_SIZE]);
1279  DECLARE_ALIGNED(16, int16_t, tmp4[MAX_PB_SIZE * MAX_PB_SIZE]);
1280  HEVCFrame *ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1281  HEVCFrame *ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1282 
1283  if (!ref0 || !ref1)
1284  return;
1285 
1286  luma_mc(s, tmp, tmpstride, ref0->frame,
1287  &current_mv.mv[0], x0, y0, nPbW, nPbH);
1288  luma_mc(s, tmp2, tmpstride, ref1->frame,
1289  &current_mv.mv[1], x0, y0, nPbW, nPbH);
1290 
1291  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1292  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1294  s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1295  s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1296  s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1297  s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1298  dst0, s->frame->linesize[0],
1299  tmp, tmp2, tmpstride, nPbW, nPbH);
1300  } else {
1301  s->hevcdsp.put_weighted_pred_avg(dst0, s->frame->linesize[0],
1302  tmp, tmp2, tmpstride, nPbW, nPbH);
1303  }
1304 
1305  chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1306  &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1307  chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
1308  &current_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1309 
1310  if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1311  (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1313  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1314  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1315  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1316  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1317  dst1, s->frame->linesize[1], tmp, tmp3,
1318  tmpstride, nPbW / 2, nPbH / 2);
1320  s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1321  s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1322  s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1323  s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1324  dst2, s->frame->linesize[2], tmp2, tmp4,
1325  tmpstride, nPbW / 2, nPbH / 2);
1326  } else {
1327  s->hevcdsp.put_weighted_pred_avg(dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbW/2, nPbH/2);
1328  s->hevcdsp.put_weighted_pred_avg(dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbW/2, nPbH/2);
1329  }
1330  }
1331 }
1332 
1333 /**
1334  * 8.4.1
1335  */
1336 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1337  int prev_intra_luma_pred_flag)
1338 {
1339  HEVCLocalContext *lc = s->HEVClc;
1340  int x_pu = x0 >> s->sps->log2_min_pu_size;
1341  int y_pu = y0 >> s->sps->log2_min_pu_size;
1342  int min_pu_width = s->sps->min_pu_width;
1343  int size_in_pus = pu_size >> s->sps->log2_min_pu_size;
1344  int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
1345  int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
1346 
1347  int cand_up = (lc->ctb_up_flag || y0b) ?
1348  s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1349  int cand_left = (lc->ctb_left_flag || x0b) ?
1350  s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1351 
1352  int y_ctb = (y0 >> (s->sps->log2_ctb_size)) << (s->sps->log2_ctb_size);
1353 
1354  MvField *tab_mvf = s->ref->tab_mvf;
1355  int intra_pred_mode;
1356  int candidate[3];
1357  int i, j;
1358 
1359  // intra_pred_mode prediction does not cross vertical CTB boundaries
1360  if ((y0 - 1) < y_ctb)
1361  cand_up = INTRA_DC;
1362 
1363  if (cand_left == cand_up) {
1364  if (cand_left < 2) {
1365  candidate[0] = INTRA_PLANAR;
1366  candidate[1] = INTRA_DC;
1367  candidate[2] = INTRA_ANGULAR_26;
1368  } else {
1369  candidate[0] = cand_left;
1370  candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1371  candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1372  }
1373  } else {
1374  candidate[0] = cand_left;
1375  candidate[1] = cand_up;
1376  if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1377  candidate[2] = INTRA_PLANAR;
1378  } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1379  candidate[2] = INTRA_DC;
1380  } else {
1381  candidate[2] = INTRA_ANGULAR_26;
1382  }
1383  }
1384 
1385  if (prev_intra_luma_pred_flag) {
1386  intra_pred_mode = candidate[lc->pu.mpm_idx];
1387  } else {
1388  if (candidate[0] > candidate[1])
1389  FFSWAP(uint8_t, candidate[0], candidate[1]);
1390  if (candidate[0] > candidate[2])
1391  FFSWAP(uint8_t, candidate[0], candidate[2]);
1392  if (candidate[1] > candidate[2])
1393  FFSWAP(uint8_t, candidate[1], candidate[2]);
1394 
1395  intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1396  for (i = 0; i < 3; i++)
1397  if (intra_pred_mode >= candidate[i])
1398  intra_pred_mode++;
1399  }
1400 
1401  /* write the intra prediction units into the mv array */
1402  if (!size_in_pus)
1403  size_in_pus = 1;
1404  for (i = 0; i < size_in_pus; i++) {
1405  memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1406  intra_pred_mode, size_in_pus);
1407 
1408  for (j = 0; j < size_in_pus; j++) {
1409  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].is_intra = 1;
1410  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[0] = 0;
1411  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[1] = 0;
1412  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[0] = 0;
1413  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[1] = 0;
1414  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].x = 0;
1415  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].y = 0;
1416  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].x = 0;
1417  tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].y = 0;
1418  }
1419  }
1420 
1421  return intra_pred_mode;
1422 }
1423 
1424 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1425  int log2_cb_size, int ct_depth)
1426 {
1427  int length = (1 << log2_cb_size) >> s->sps->log2_min_cb_size;
1428  int x_cb = x0 >> s->sps->log2_min_cb_size;
1429  int y_cb = y0 >> s->sps->log2_min_cb_size;
1430  int y;
1431 
1432  for (y = 0; y < length; y++)
1433  memset(&s->tab_ct_depth[(y_cb + y) * s->sps->min_cb_width + x_cb],
1434  ct_depth, length);
1435 }
1436 
1437 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1438  int log2_cb_size)
1439 {
1440  HEVCLocalContext *lc = s->HEVClc;
1441  static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1442  uint8_t prev_intra_luma_pred_flag[4];
1443  int split = lc->cu.part_mode == PART_NxN;
1444  int pb_size = (1 << log2_cb_size) >> split;
1445  int side = split + 1;
1446  int chroma_mode;
1447  int i, j;
1448 
1449  for (i = 0; i < side; i++)
1450  for (j = 0; j < side; j++)
1451  prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1452 
1453  for (i = 0; i < side; i++) {
1454  for (j = 0; j < side; j++) {
1455  if (prev_intra_luma_pred_flag[2 * i + j])
1457  else
1459 
1460  lc->pu.intra_pred_mode[2 * i + j] =
1461  luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1462  prev_intra_luma_pred_flag[2 * i + j]);
1463  }
1464  }
1465 
1466  chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1467  if (chroma_mode != 4) {
1468  if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1469  lc->pu.intra_pred_mode_c = 34;
1470  else
1471  lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
1472  } else {
1473  lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
1474  }
1475 }
1476 
1478  int x0, int y0,
1479  int log2_cb_size)
1480 {
1481  HEVCLocalContext *lc = s->HEVClc;
1482  int pb_size = 1 << log2_cb_size;
1483  int size_in_pus = pb_size >> s->sps->log2_min_pu_size;
1484  int min_pu_width = s->sps->min_pu_width;
1485  MvField *tab_mvf = s->ref->tab_mvf;
1486  int x_pu = x0 >> s->sps->log2_min_pu_size;
1487  int y_pu = y0 >> s->sps->log2_min_pu_size;
1488  int j, k;
1489 
1490  if (size_in_pus == 0)
1491  size_in_pus = 1;
1492  for (j = 0; j < size_in_pus; j++) {
1493  memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
1494  for (k = 0; k < size_in_pus; k++)
1495  tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
1496  }
1497 }
1498 
1499 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
1500 {
1501  int cb_size = 1 << log2_cb_size;
1502  HEVCLocalContext *lc = s->HEVClc;
1503  int log2_min_cb_size = s->sps->log2_min_cb_size;
1504  int length = cb_size >> log2_min_cb_size;
1505  int min_cb_width = s->sps->min_cb_width;
1506  int x_cb = x0 >> log2_min_cb_size;
1507  int y_cb = y0 >> log2_min_cb_size;
1508  int x, y;
1509 
1510  lc->cu.x = x0;
1511  lc->cu.y = y0;
1512  lc->cu.rqt_root_cbf = 1;
1513 
1514  lc->cu.pred_mode = MODE_INTRA;
1515  lc->cu.part_mode = PART_2Nx2N;
1516  lc->cu.intra_split_flag = 0;
1517  lc->cu.pcm_flag = 0;
1518  SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
1519  for (x = 0; x < 4; x++)
1520  lc->pu.intra_pred_mode[x] = 1;
1523  if (lc->cu.cu_transquant_bypass_flag)
1524  set_deblocking_bypass(s, x0, y0, log2_cb_size);
1525  } else
1526  lc->cu.cu_transquant_bypass_flag = 0;
1527 
1528  if (s->sh.slice_type != I_SLICE) {
1529  uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
1530 
1531  lc->cu.pred_mode = MODE_SKIP;
1532  x = y_cb * min_cb_width + x_cb;
1533  for (y = 0; y < length; y++) {
1534  memset(&s->skip_flag[x], skip_flag, length);
1535  x += min_cb_width;
1536  }
1537  lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
1538  }
1539 
1540  if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
1541  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
1542  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1543 
1545  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1548  } else {
1549  if (s->sh.slice_type != I_SLICE)
1551  if (lc->cu.pred_mode != MODE_INTRA ||
1552  log2_cb_size == s->sps->log2_min_cb_size) {
1553  lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
1554  lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
1555  lc->cu.pred_mode == MODE_INTRA;
1556  }
1557 
1558  if (lc->cu.pred_mode == MODE_INTRA) {
1559  if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
1560  log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
1561  log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
1563  }
1564  if (lc->cu.pcm_flag) {
1565  int ret;
1566  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1567  ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
1569  set_deblocking_bypass(s, x0, y0, log2_cb_size);
1570 
1571  if (ret < 0)
1572  return ret;
1573  } else {
1574  intra_prediction_unit(s, x0, y0, log2_cb_size);
1575  }
1576  } else {
1577  intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1578  switch (lc->cu.part_mode) {
1579  case PART_2Nx2N:
1580  hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
1581  break;
1582  case PART_2NxN:
1583  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
1584  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size/2, log2_cb_size, 1);
1585  break;
1586  case PART_Nx2N:
1587  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
1588  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
1589  break;
1590  case PART_2NxnU:
1591  hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
1592  hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
1593  break;
1594  case PART_2NxnD:
1595  hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
1596  hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
1597  break;
1598  case PART_nLx2N:
1599  hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size,0);
1600  hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
1601  break;
1602  case PART_nRx2N:
1603  hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size,0);
1604  hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size/4, cb_size, log2_cb_size, 1);
1605  break;
1606  case PART_NxN:
1607  hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
1608  hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
1609  hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
1610  hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
1611  break;
1612  }
1613  }
1614 
1615  if (!lc->cu.pcm_flag) {
1616  if (lc->cu.pred_mode != MODE_INTRA &&
1617  !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
1619  }
1620  if (lc->cu.rqt_root_cbf) {
1621  lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
1624  hls_transform_tree(s, x0, y0, x0, y0, x0, y0, log2_cb_size,
1625  log2_cb_size, 0, 0);
1626  } else {
1628  ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1631  }
1632  }
1633  }
1634 
1636  ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
1637 
1638  x = y_cb * min_cb_width + x_cb;
1639  for (y = 0; y < length; y++) {
1640  memset(&s->qp_y_tab[x], lc->qp_y, length);
1641  x += min_cb_width;
1642  }
1643 
1644  set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
1645 
1646  return 0;
1647 }
1648 
1649 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
1650  int log2_cb_size, int cb_depth)
1651 {
1652  HEVCLocalContext *lc = s->HEVClc;
1653  const int cb_size = 1 << log2_cb_size;
1654  int ret;
1655 
1656  lc->ct.depth = cb_depth;
1657  if ((x0 + cb_size <= s->sps->width) &&
1658  (y0 + cb_size <= s->sps->height) &&
1659  log2_cb_size > s->sps->log2_min_cb_size) {
1660  SAMPLE(s->split_cu_flag, x0, y0) =
1661  ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
1662  } else {
1663  SAMPLE(s->split_cu_flag, x0, y0) =
1664  (log2_cb_size > s->sps->log2_min_cb_size);
1665  }
1666  if (s->pps->cu_qp_delta_enabled_flag &&
1667  log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
1668  lc->tu.is_cu_qp_delta_coded = 0;
1669  lc->tu.cu_qp_delta = 0;
1670  }
1671 
1672  if (SAMPLE(s->split_cu_flag, x0, y0)) {
1673  const int cb_size_split = cb_size >> 1;
1674  const int x1 = x0 + cb_size_split;
1675  const int y1 = y0 + cb_size_split;
1676  int more_data = 0;
1677 
1678  more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
1679  if (more_data < 0)
1680  return more_data;
1681 
1682  if (more_data && x1 < s->sps->width)
1683  more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
1684  if (more_data && y1 < s->sps->height)
1685  more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
1686  if (more_data && x1 < s->sps->width &&
1687  y1 < s->sps->height) {
1688  return hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
1689  }
1690  if (more_data)
1691  return ((x1 + cb_size_split) < s->sps->width ||
1692  (y1 + cb_size_split) < s->sps->height);
1693  else
1694  return 0;
1695  } else {
1696  ret = hls_coding_unit(s, x0, y0, log2_cb_size);
1697  if (ret < 0)
1698  return ret;
1699  if ((!((x0 + cb_size) %
1700  (1 << (s->sps->log2_ctb_size))) ||
1701  (x0 + cb_size >= s->sps->width)) &&
1702  (!((y0 + cb_size) %
1703  (1 << (s->sps->log2_ctb_size))) ||
1704  (y0 + cb_size >= s->sps->height))) {
1705  int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
1706  return !end_of_slice_flag;
1707  } else {
1708  return 1;
1709  }
1710  }
1711 
1712  return 0;
1713 }
1714 
1715 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb, int ctb_addr_ts)
1716 {
1717  HEVCLocalContext *lc = s->HEVClc;
1718  int ctb_size = 1 << s->sps->log2_ctb_size;
1719  int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1720  int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
1721 
1722  int tile_left_boundary;
1723  int tile_up_boundary;
1724  int slice_left_boundary;
1725  int slice_up_boundary;
1726 
1727  s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
1728 
1730  if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
1731  lc->first_qp_group = 1;
1732  lc->end_of_tiles_x = s->sps->width;
1733  } else if (s->pps->tiles_enabled_flag) {
1734  if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
1735  int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
1736  lc->start_of_tiles_x = x_ctb;
1737  lc->end_of_tiles_x = x_ctb + (s->pps->column_width[idxX] << s->sps->log2_ctb_size);
1738  lc->first_qp_group = 1;
1739  }
1740  } else {
1741  lc->end_of_tiles_x = s->sps->width;
1742  }
1743 
1744  lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
1745 
1746  if (s->pps->tiles_enabled_flag) {
1747  tile_left_boundary = ((x_ctb > 0) &&
1748  (s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]]));
1749  slice_left_boundary = ((x_ctb > 0) &&
1750  (s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - 1]));
1751  tile_up_boundary = ((y_ctb > 0) &&
1752  (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]]));
1753  slice_up_boundary = ((y_ctb > 0) &&
1754  (s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width]));
1755  } else {
1756  tile_left_boundary =
1757  tile_up_boundary = 1;
1758  slice_left_boundary = ctb_addr_in_slice > 0;
1759  slice_up_boundary = ctb_addr_in_slice >= s->sps->ctb_width;
1760  }
1761  lc->slice_or_tiles_left_boundary = (!slice_left_boundary) + (!tile_left_boundary << 1);
1762  lc->slice_or_tiles_up_boundary = (!slice_up_boundary + (!tile_up_boundary << 1));
1763  lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && tile_left_boundary);
1764  lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && tile_up_boundary);
1765  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]]));
1766  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]]));
1767 }
1768 
1769 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
1770 {
1771  HEVCContext *s = avctxt->priv_data;
1772  int ctb_size = 1 << s->sps->log2_ctb_size;
1773  int more_data = 1;
1774  int x_ctb = 0;
1775  int y_ctb = 0;
1776  int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
1777 
1778  while (more_data && ctb_addr_ts < s->sps->ctb_size) {
1779  int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1780 
1781  x_ctb = (ctb_addr_rs % ((s->sps->width + (ctb_size - 1)) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
1782  y_ctb = (ctb_addr_rs / ((s->sps->width + (ctb_size - 1)) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
1783  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
1784 
1785  ff_hevc_cabac_init(s, ctb_addr_ts);
1786 
1787  hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
1788 
1789  s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
1790  s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
1792 
1793  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
1794  if (more_data < 0)
1795  return more_data;
1796 
1797  ctb_addr_ts++;
1798  ff_hevc_save_states(s, ctb_addr_ts);
1799  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
1800  }
1801 
1802  if (x_ctb + ctb_size >= s->sps->width &&
1803  y_ctb + ctb_size >= s->sps->height)
1804  ff_hevc_hls_filter(s, x_ctb, y_ctb);
1805 
1806  return ctb_addr_ts;
1807 }
1808 
1810 {
1811  int arg[2];
1812  int ret[2];
1813 
1814  arg[0] = 0;
1815  arg[1] = 1;
1816 
1817  s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
1818  return ret[0];
1819 }
1820 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
1821 {
1822  HEVCContext *s1 = avctxt->priv_data, *s;
1823  HEVCLocalContext *lc;
1824  int ctb_size = 1<< s1->sps->log2_ctb_size;
1825  int more_data = 1;
1826  int *ctb_row_p = input_ctb_row;
1827  int ctb_row = ctb_row_p[job];
1828  int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->sps->width + ctb_size - 1) >> s1->sps->log2_ctb_size);
1829  int ctb_addr_ts = s1->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
1830  int thread = ctb_row % s1->threads_number;
1831  int ret;
1832 
1833  s = s1->sList[self_id];
1834  lc = s->HEVClc;
1835 
1836  if(ctb_row) {
1837  ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
1838 
1839  if (ret < 0)
1840  return ret;
1841  ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
1842  }
1843 
1844  while(more_data && ctb_addr_ts < s->sps->ctb_size) {
1845  int x_ctb = (ctb_addr_rs % s->sps->ctb_width) << s->sps->log2_ctb_size;
1846  int y_ctb = (ctb_addr_rs / s->sps->ctb_width) << s->sps->log2_ctb_size;
1847 
1848  hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
1849 
1850  ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
1851 
1852  if (avpriv_atomic_int_get(&s1->wpp_err)){
1853  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
1854  return 0;
1855  }
1856 
1857  ff_hevc_cabac_init(s, ctb_addr_ts);
1858  hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
1859  more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
1860 
1861  if (more_data < 0)
1862  return more_data;
1863 
1864  ctb_addr_ts++;
1865 
1866  ff_hevc_save_states(s, ctb_addr_ts);
1867  ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
1868  ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
1869 
1870  if (!more_data && (x_ctb+ctb_size) < s->sps->width && ctb_row != s->sh.num_entry_point_offsets) {
1871  avpriv_atomic_int_set(&s1->wpp_err, 1);
1872  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
1873  return 0;
1874  }
1875 
1876  if ((x_ctb+ctb_size) >= s->sps->width && (y_ctb+ctb_size) >= s->sps->height ) {
1877  ff_hevc_hls_filter(s, x_ctb, y_ctb);
1878  ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
1879  return ctb_addr_ts;
1880  }
1881  ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1882  x_ctb+=ctb_size;
1883 
1884  if(x_ctb >= s->sps->width) {
1885  break;
1886  }
1887  }
1888  ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
1889 
1890  return 0;
1891 }
1892 
1893 static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
1894 {
1895  HEVCLocalContext *lc = s->HEVClc;
1896  int *ret = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
1897  int *arg = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
1898  int offset;
1899  int startheader, cmpt = 0;
1900  int i, j, res = 0;
1901 
1902 
1903  if (!s->sList[1]) {
1905 
1906 
1907  for (i = 1; i < s->threads_number; i++) {
1908  s->sList[i] = av_malloc(sizeof(HEVCContext));
1909  memcpy(s->sList[i], s, sizeof(HEVCContext));
1910  s->HEVClcList[i] = av_malloc(sizeof(HEVCLocalContext));
1911  s->HEVClcList[i]->edge_emu_buffer = av_malloc((MAX_PB_SIZE + 7) * s->frame->linesize[0]);
1912  s->sList[i]->HEVClc = s->HEVClcList[i];
1913  }
1914  }
1915 
1916  offset = (lc->gb.index >> 3);
1917 
1918  for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < s->skipped_bytes; j++) {
1919  if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
1920  startheader--;
1921  cmpt++;
1922  }
1923  }
1924 
1925  for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
1926  offset += (s->sh.entry_point_offset[i - 1] - cmpt);
1927  for (j = 0, cmpt = 0, startheader = offset
1928  + s->sh.entry_point_offset[i]; j < s->skipped_bytes; j++) {
1929  if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
1930  startheader--;
1931  cmpt++;
1932  }
1933  }
1934  s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
1935  s->sh.offset[i - 1] = offset;
1936 
1937  }
1938  if (s->sh.num_entry_point_offsets != 0) {
1939  offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
1940  s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
1941  s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
1942 
1943  }
1944  s->data = nal;
1945 
1946  for (i = 1; i < s->threads_number; i++) {
1947  s->sList[i]->HEVClc->first_qp_group = 1;
1948  s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
1949  memcpy(s->sList[i], s, sizeof(HEVCContext));
1950  s->sList[i]->HEVClc = s->HEVClcList[i];
1951  }
1952 
1954  ff_reset_entries(s->avctx);
1955 
1956  for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
1957  arg[i] = i;
1958  ret[i] = 0;
1959  }
1960 
1962  s->avctx->execute2(s->avctx, (void *) hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
1963 
1964  for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
1965  res += ret[i];
1966  av_free(ret);
1967  av_free(arg);
1968  return res;
1969 }
1970 
1971 /**
1972  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
1973  * 0 if the unit should be skipped, 1 otherwise
1974  */
1976 {
1977  GetBitContext *gb = &s->HEVClc->gb;
1978  int nuh_layer_id;
1979 
1980  if (get_bits1(gb) != 0)
1981  return AVERROR_INVALIDDATA;
1982 
1983  s->nal_unit_type = get_bits(gb, 6);
1984 
1985  nuh_layer_id = get_bits(gb, 6);
1986  s->temporal_id = get_bits(gb, 3) - 1;
1987  if (s->temporal_id < 0)
1988  return AVERROR_INVALIDDATA;
1989 
1991  "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
1992  s->nal_unit_type, nuh_layer_id, s->temporal_id);
1993 
1994  return nuh_layer_id == 0;
1995 }
1996 
1998 {
1999  int min_pu_size = 1 << s->sps->log2_min_pu_size;
2000  int x, y, c_idx;
2001 
2002  for (c_idx = 0; c_idx < 3; c_idx++) {
2003  ptrdiff_t stride = s->frame->linesize[c_idx];
2004  int hshift = s->sps->hshift[c_idx];
2005  int vshift = s->sps->vshift[c_idx];
2006  for (y = 0; y < s->sps->min_pu_height; y++) {
2007  for (x = 0; x < s->sps->min_pu_width; x++) {
2008  if (s->is_pcm[y * s->sps->min_pu_width + x]) {
2009  int n;
2010  int len = min_pu_size >> hshift;
2011  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)];
2012  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)];
2013  for (n = 0; n < (min_pu_size >> vshift); n++) {
2014  memcpy(dst, src, len);
2015  src += stride;
2016  dst += stride;
2017  }
2018  }
2019  }
2020  }
2021  }
2022 }
2023 
2025 {
2026  HEVCLocalContext *lc = s->HEVClc;
2027  int ret;
2028 
2029  memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2030  memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2031  memset(s->cbf_luma, 0, s->sps->min_tb_width * s->sps->min_tb_height);
2032  memset(s->is_pcm, 0, s->sps->min_pu_width * s->sps->min_pu_height);
2033 
2034  lc->start_of_tiles_x = 0;
2035  s->is_decoded = 0;
2036 
2037  if (s->pps->tiles_enabled_flag)
2038  lc->end_of_tiles_x = s->pps->column_width[0] << s->sps->log2_ctb_size;
2039 
2040  ret = ff_hevc_set_new_ref(s, s->sps->sao_enabled ? &s->sao_frame : &s->frame,
2041  s->poc);
2042  if (ret < 0)
2043  goto fail;
2044 
2046  (MAX_PB_SIZE + 7) * s->ref->frame->linesize[0]);
2047  if (!lc->edge_emu_buffer) {
2048  ret = AVERROR(ENOMEM);
2049  goto fail;
2050  }
2051 
2052  ret = ff_hevc_frame_rps(s);
2053  if (ret < 0) {
2054  av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2055  goto fail;
2056  }
2057 
2059  ret = ff_hevc_output_frame(s, s->output_frame, 0);
2060  if (ret < 0)
2061  goto fail;
2062 
2064 
2065  return 0;
2066 fail:
2067  if (s->ref && s->threads_type == FF_THREAD_FRAME)
2068  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2069  s->ref = NULL;
2070  return ret;
2071 }
2072 
2073 static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
2074 {
2075  HEVCLocalContext *lc = s->HEVClc;
2076  GetBitContext *gb = &lc->gb;
2077  int ctb_addr_ts;
2078  int ret;
2079 
2080  ret = init_get_bits8(gb, nal, length);
2081  if (ret < 0)
2082  return ret;
2083 
2084  ret = hls_nal_unit(s);
2085  if (ret < 0) {
2086  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
2087  s->nal_unit_type);
2089  return ret;
2090  return 0;
2091  } else if (!ret)
2092  return 0;
2093 
2094  switch (s->nal_unit_type) {
2095  case NAL_VPS:
2096  ret = ff_hevc_decode_nal_vps(s);
2097  if (ret < 0)
2098  return ret;
2099  break;
2100  case NAL_SPS:
2101  ret = ff_hevc_decode_nal_sps(s);
2102  if (ret < 0)
2103  return ret;
2104  break;
2105  case NAL_PPS:
2106  ret = ff_hevc_decode_nal_pps(s);
2107  if (ret < 0)
2108  return ret;
2109  break;
2110  case NAL_SEI_PREFIX:
2111  case NAL_SEI_SUFFIX:
2112  ret = ff_hevc_decode_nal_sei(s);
2113  if (ret < 0)
2114  return ret;
2115  break;
2116  case NAL_TRAIL_R:
2117  case NAL_TRAIL_N:
2118  case NAL_TSA_N:
2119  case NAL_TSA_R:
2120  case NAL_STSA_N:
2121  case NAL_STSA_R:
2122  case NAL_BLA_W_LP:
2123  case NAL_BLA_W_RADL:
2124  case NAL_BLA_N_LP:
2125  case NAL_IDR_W_RADL:
2126  case NAL_IDR_N_LP:
2127  case NAL_CRA_NUT:
2128  case NAL_RADL_N:
2129  case NAL_RADL_R:
2130  case NAL_RASL_N:
2131  case NAL_RASL_R:
2132  ret = hls_slice_header(s);
2133  if (ret < 0)
2134  return ret;
2135 
2136  if (s->max_ra == INT_MAX) {
2137  if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
2138  s->max_ra = s->poc;
2139  } else {
2140  if (IS_IDR(s))
2141  s->max_ra = INT_MIN;
2142  }
2143  }
2144 
2145  if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
2146  s->poc <= s->max_ra) {
2147  s->is_decoded = 0;
2148  break;
2149  } else {
2150  if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
2151  s->max_ra = INT_MIN;
2152  }
2153 
2154  if (s->sh.first_slice_in_pic_flag) {
2155  ret = hevc_frame_start(s);
2156  if (ret < 0)
2157  return ret;
2158  } else if (!s->ref) {
2159  av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2160  return AVERROR_INVALIDDATA;
2161  }
2162 
2163  if (!s->sh.dependent_slice_segment_flag &&
2164  s->sh.slice_type != I_SLICE) {
2165  ret = ff_hevc_slice_rpl(s);
2166  if (ret < 0) {
2168  "Error constructing the reference lists for the current slice.\n");
2170  return ret;
2171  }
2172  }
2173 
2174  if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2175  ctb_addr_ts = hls_slice_data_wpp(s, nal, length);
2176  else
2177  ctb_addr_ts = hls_slice_data(s);
2178 
2179  if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
2180  s->is_decoded = 1;
2183  s->sps->sao_enabled)
2184  restore_tqb_pixels(s);
2185  }
2186 
2187  if (ctb_addr_ts < 0)
2188  return ctb_addr_ts;
2189  break;
2190  case NAL_EOS_NUT:
2191  case NAL_EOB_NUT:
2192  s->seq_decode = (s->seq_decode + 1) & 0xff;
2193  s->max_ra = INT_MAX;
2194  break;
2195  case NAL_AUD:
2196  case NAL_FD_NUT:
2197  break;
2198  default:
2199  av_log(s->avctx, AV_LOG_INFO,
2200  "Skipping NAL unit %d\n", s->nal_unit_type);
2201  }
2202 
2203  return 0;
2204 }
2205 
2206 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
2207  between these functions would be nice. */
2209  HEVCNAL *nal)
2210 {
2211  int i, si, di;
2212  uint8_t *dst;
2213 
2214  s->skipped_bytes = 0;
2215 #define STARTCODE_TEST \
2216  if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
2217  if (src[i + 2] != 3) { \
2218  /* startcode, so we must be past the end */ \
2219  length = i; \
2220  } \
2221  break; \
2222  }
2223 #if HAVE_FAST_UNALIGNED
2224 #define FIND_FIRST_ZERO \
2225  if (i > 0 && !src[i]) \
2226  i--; \
2227  while (src[i]) \
2228  i++
2229 #if HAVE_FAST_64BIT
2230  for (i = 0; i + 1 < length; i += 9) {
2231  if (!((~AV_RN64A(src + i) &
2232  (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
2233  0x8000800080008080ULL))
2234  continue;
2235  FIND_FIRST_ZERO;
2237  i -= 7;
2238  }
2239 #else
2240  for (i = 0; i + 1 < length; i += 5) {
2241  if (!((~AV_RN32A(src + i) &
2242  (AV_RN32A(src + i) - 0x01000101U)) &
2243  0x80008080U))
2244  continue;
2245  FIND_FIRST_ZERO;
2247  i -= 3;
2248  }
2249 #endif
2250 #else
2251  for (i = 0; i + 1 < length; i += 2) {
2252  if (src[i])
2253  continue;
2254  if (i > 0 && src[i - 1] == 0)
2255  i--;
2257  }
2258 #endif
2259 
2260  if (i >= length - 1) { // no escaped 0
2261  nal->data = src;
2262  nal->size = length;
2263  return length;
2264  }
2265 
2267  length + FF_INPUT_BUFFER_PADDING_SIZE);
2268  if (!nal->rbsp_buffer)
2269  return AVERROR(ENOMEM);
2270 
2271  dst = nal->rbsp_buffer;
2272 
2273  memcpy(dst, src, i);
2274  si = di = i;
2275  while (si + 2 < length) {
2276  // remove escapes (very rare 1:2^22)
2277  if (src[si + 2] > 3) {
2278  dst[di++] = src[si++];
2279  dst[di++] = src[si++];
2280  } else if (src[si] == 0 && src[si + 1] == 0) {
2281  if (src[si + 2] == 3) { // escape
2282  dst[di++] = 0;
2283  dst[di++] = 0;
2284  si += 3;
2285 
2286  s->skipped_bytes++;
2287  if (s->skipped_bytes_pos_size < s->skipped_bytes) {
2288  s->skipped_bytes_pos_size *= 2;
2291  sizeof(*s->skipped_bytes_pos));
2292  if (!s->skipped_bytes_pos)
2293  return AVERROR(ENOMEM);
2294  }
2295  if (s->skipped_bytes_pos)
2296  s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
2297  continue;
2298  } else // next start code
2299  goto nsc;
2300  }
2301 
2302  dst[di++] = src[si++];
2303  }
2304  while (si < length)
2305  dst[di++] = src[si++];
2306 nsc:
2307 
2308  memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2309 
2310  nal->data = dst;
2311  nal->size = di;
2312  return si;
2313 }
2314 
2315 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2316 {
2317  int i, consumed, ret = 0;
2318 
2319  s->ref = NULL;
2320  s->eos = 0;
2321 
2322  /* split the input packet into NAL units, so we know the upper bound on the
2323  * number of slices in the frame */
2324  s->nb_nals = 0;
2325  while (length >= 4) {
2326  HEVCNAL *nal;
2327  int extract_length = 0;
2328 
2329  if (s->is_nalff) {
2330  int i;
2331  for (i = 0; i < s->nal_length_size; i++)
2332  extract_length = (extract_length << 8) | buf[i];
2333  buf += s->nal_length_size;
2334  length -= s->nal_length_size;
2335 
2336  if (extract_length > length) {
2337  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
2338  ret = AVERROR_INVALIDDATA;
2339  goto fail;
2340  }
2341  } else {
2342  /* search start code */
2343  while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
2344  ++buf;
2345  --length;
2346  if (length < 4) {
2347  av_log(s->avctx, AV_LOG_ERROR, "No start code is found.\n");
2348  ret = AVERROR_INVALIDDATA;
2349  goto fail;
2350  }
2351  }
2352 
2353  buf += 3;
2354  length -= 3;
2355  }
2356 
2357  if (!s->is_nalff)
2358  extract_length = length;
2359 
2360  if (s->nals_allocated < s->nb_nals + 1) {
2361  int new_size = s->nals_allocated + 1;
2362  HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));
2363  if (!tmp) {
2364  ret = AVERROR(ENOMEM);
2365  goto fail;
2366  }
2367  s->nals = tmp;
2368  memset(s->nals + s->nals_allocated, 0, (new_size - s->nals_allocated) * sizeof(*tmp));
2369  av_reallocp_array(&s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
2372  s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size
2374  s->nals_allocated = new_size;
2375  }
2378  nal = &s->nals[s->nb_nals];
2379 
2380  consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
2381 
2385 
2386 
2387  if (consumed < 0) {
2388  ret = consumed;
2389  goto fail;
2390  }
2391 
2392  ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);
2393  if (ret < 0)
2394  goto fail;
2395  hls_nal_unit(s);
2396 
2397  if (s->nal_unit_type == NAL_EOS_NUT ||
2398  s->nal_unit_type == NAL_EOB_NUT)
2399  s->eos = 1;
2400 
2401  buf += consumed;
2402  length -= consumed;
2403  }
2404 
2405  /* parse the NAL units */
2406  for (i = 0; i < s->nb_nals; i++) {
2407  int ret;
2408  s->skipped_bytes = s->skipped_bytes_nal[i];
2410 
2411  ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
2412  if (ret < 0) {
2414  "Error parsing NAL unit #%d.\n", i);
2416  goto fail;
2417  }
2418  }
2419 
2420 fail:
2421  if (s->ref && s->threads_type == FF_THREAD_FRAME)
2422  ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2423 
2424  return ret;
2425 }
2426 
2427 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2428 {
2429  int i;
2430  for (i = 0; i < 16; i++)
2431  av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2432 }
2433 
2435 {
2436  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2437  int pixel_shift;
2438  int i, j;
2439 
2440  if (!desc)
2441  return AVERROR(EINVAL);
2442 
2443  pixel_shift = desc->comp[0].depth_minus1 > 7;
2444 
2445  av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2446  s->poc);
2447 
2448  /* the checksums are LE, so we have to byteswap for >8bpp formats
2449  * on BE arches */
2450 #if HAVE_BIGENDIAN
2451  if (pixel_shift && !s->checksum_buf) {
2453  FFMAX3(frame->linesize[0], frame->linesize[1],
2454  frame->linesize[2]));
2455  if (!s->checksum_buf)
2456  return AVERROR(ENOMEM);
2457  }
2458 #endif
2459 
2460  for (i = 0; frame->data[i]; i++) {
2461  int width = s->avctx->coded_width;
2462  int height = s->avctx->coded_height;
2463  int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
2464  int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2465  uint8_t md5[16];
2466 
2467  av_md5_init(s->md5_ctx);
2468  for (j = 0; j < h; j++) {
2469  const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2470 #if HAVE_BIGENDIAN
2471  if (pixel_shift) {
2472  s->dsp.bswap16_buf((uint16_t*)s->checksum_buf,
2473  (const uint16_t*)src, w);
2474  src = s->checksum_buf;
2475  }
2476 #endif
2477  av_md5_update(s->md5_ctx, src, w << pixel_shift);
2478  }
2479  av_md5_final(s->md5_ctx, md5);
2480 
2481  if (!memcmp(md5, s->md5[i], 16)) {
2482  av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2483  print_md5(s->avctx, AV_LOG_DEBUG, md5);
2484  av_log (s->avctx, AV_LOG_DEBUG, "; ");
2485  } else {
2486  av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2487  print_md5(s->avctx, AV_LOG_ERROR, md5);
2488  av_log (s->avctx, AV_LOG_ERROR, " != ");
2489  print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2490  av_log (s->avctx, AV_LOG_ERROR, "\n");
2491  return AVERROR_INVALIDDATA;
2492  }
2493  }
2494 
2495  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2496 
2497  return 0;
2498 }
2499 
2500 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
2501  AVPacket *avpkt)
2502 {
2503  int ret;
2504  HEVCContext *s = avctx->priv_data;
2505 
2506  if (!avpkt->size) {
2507  ret = ff_hevc_output_frame(s, data, 1);
2508  if (ret < 0)
2509  return ret;
2510 
2511  *got_output = ret;
2512  return 0;
2513  }
2514 
2515  s->ref = NULL;
2516  ret = decode_nal_units(s, avpkt->data, avpkt->size);
2517  if (ret < 0)
2518  return ret;
2519 
2520  /* verify the SEI checksum */
2521  if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
2522  avctx->err_recognition & AV_EF_EXPLODE &&
2523  s->is_md5) {
2524  ret = verify_md5(s, s->ref->frame);
2525  if (ret < 0) {
2526  ff_hevc_unref_frame(s, s->ref, ~0);
2527  return ret;
2528  }
2529  }
2530  s->is_md5 = 0;
2531 
2532  if (s->is_decoded) {
2533  av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
2534  s->is_decoded = 0;
2535  }
2536 
2537  if (s->output_frame->buf[0]) {
2538  av_frame_move_ref(data, s->output_frame);
2539  *got_output = 1;
2540  }
2541 
2542  return avpkt->size;
2543 }
2544 
2546 {
2547  int ret;
2548 
2549  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
2550  if (ret < 0)
2551  return ret;
2552 
2553  dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
2554  if (!dst->tab_mvf_buf)
2555  goto fail;
2556  dst->tab_mvf = src->tab_mvf;
2557 
2558  dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
2559  if (!dst->rpl_tab_buf)
2560  goto fail;
2561  dst->rpl_tab = src->rpl_tab;
2562 
2563  dst->rpl_buf = av_buffer_ref(src->rpl_buf);
2564  if (!dst->rpl_buf)
2565  goto fail;
2566 
2567  dst->poc = src->poc;
2568  dst->ctb_count = src->ctb_count;
2569  dst->window = src->window;
2570  dst->flags = src->flags;
2571  dst->sequence = src->sequence;
2572 
2573  return 0;
2574 fail:
2575  ff_hevc_unref_frame(s, dst, ~0);
2576  return AVERROR(ENOMEM);
2577 }
2578 
2580 {
2581  HEVCContext *s = avctx->priv_data;
2582  HEVCLocalContext *lc = s->HEVClc;
2583  int i;
2584 
2585  pic_arrays_free(s);
2586 
2587  av_freep(&lc->edge_emu_buffer);
2588  av_freep(&s->md5_ctx);
2589 
2590  for(i=0; i < s->nals_allocated; i++) {
2592  }
2596 
2597  av_freep(&s->cabac_state);
2598 
2599  av_frame_free(&s->tmp_frame);
2601 
2602  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2603  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2604  av_frame_free(&s->DPB[i].frame);
2605  }
2606 
2607  for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
2608  av_freep(&s->vps_list[i]);
2609  for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
2610  av_buffer_unref(&s->sps_list[i]);
2611  for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
2612  av_buffer_unref(&s->pps_list[i]);
2613 
2615  av_freep(&s->sh.offset);
2616  av_freep(&s->sh.size);
2617 
2618  for (i = 1; i < s->threads_number; i++) {
2619  lc = s->HEVClcList[i];
2620  if (lc) {
2621  av_freep(&lc->edge_emu_buffer);
2622 
2623  av_freep(&s->HEVClcList[i]);
2624  av_freep(&s->sList[i]);
2625  }
2626  }
2627  av_freep(&s->HEVClcList[0]);
2628 
2629  for (i = 0; i < s->nals_allocated; i++)
2630  av_freep(&s->nals[i].rbsp_buffer);
2631  av_freep(&s->nals);
2632  s->nals_allocated = 0;
2633 
2634  return 0;
2635 }
2636 
2638 {
2639  HEVCContext *s = avctx->priv_data;
2640  int i;
2641 
2642  s->avctx = avctx;
2643 
2644  s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
2645  if (!s->HEVClc)
2646  goto fail;
2647  s->HEVClcList[0] = s->HEVClc;
2648  s->sList[0] = s;
2649 
2651  if (!s->cabac_state)
2652  goto fail;
2653 
2654  s->tmp_frame = av_frame_alloc();
2655  if (!s->tmp_frame)
2656  goto fail;
2657 
2659  if (!s->output_frame)
2660  goto fail;
2661 
2662  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2663  s->DPB[i].frame = av_frame_alloc();
2664  if (!s->DPB[i].frame)
2665  goto fail;
2666  s->DPB[i].tf.f = s->DPB[i].frame;
2667  }
2668 
2669  s->max_ra = INT_MAX;
2670 
2671  s->md5_ctx = av_md5_alloc();
2672  if (!s->md5_ctx)
2673  goto fail;
2674 
2675  ff_dsputil_init(&s->dsp, avctx);
2676 
2677  s->context_initialized = 1;
2678 
2679  return 0;
2680 fail:
2681  hevc_decode_free(avctx);
2682  return AVERROR(ENOMEM);
2683 }
2684 
2686  const AVCodecContext *src)
2687 {
2688  HEVCContext *s = dst->priv_data;
2689  HEVCContext *s0 = src->priv_data;
2690  int i, ret;
2691 
2692  if (!s->context_initialized) {
2693  ret = hevc_init_context(dst);
2694  if (ret < 0)
2695  return ret;
2696  }
2697 
2698  for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2699  ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2700  if (s0->DPB[i].frame->buf[0]) {
2701  ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
2702  if (ret < 0)
2703  return ret;
2704  }
2705  }
2706 
2707  for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
2708  av_buffer_unref(&s->sps_list[i]);
2709  if (s0->sps_list[i]) {
2710  s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
2711  if (!s->sps_list[i])
2712  return AVERROR(ENOMEM);
2713  }
2714  }
2715 
2716  for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
2717  av_buffer_unref(&s->pps_list[i]);
2718  if (s0->pps_list[i]) {
2719  s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
2720  if (!s->pps_list[i])
2721  return AVERROR(ENOMEM);
2722  }
2723  }
2724 
2725  s->seq_decode = s0->seq_decode;
2726  s->seq_output = s0->seq_output;
2727  s->pocTid0 = s0->pocTid0;
2728  s->max_ra = s0->max_ra;
2729 
2730  s->is_nalff = s0->is_nalff;
2732 
2733  s->threads_number = s0->threads_number;
2734  s->threads_type = s0->threads_type;
2735 
2736  if (s0->eos) {
2737  s->seq_decode = (s->seq_decode + 1) & 0xff;
2738  s->max_ra = INT_MAX;
2739  }
2740 
2741  return 0;
2742 }
2743 
2745 {
2746  AVCodecContext *avctx = s->avctx;
2747  GetByteContext gb;
2748  int ret;
2749 
2750  bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
2751 
2752  if (avctx->extradata_size > 3 &&
2753  (avctx->extradata[0] || avctx->extradata[1] ||
2754  avctx->extradata[2] > 1)) {
2755  /* It seems the extradata is encoded as hvcC format.
2756  * Temporarily, we support configurationVersion==0 until 14496-15 3rd finalized.
2757  * When finalized, configurationVersion will be 1 and we can recognize hvcC by
2758  * checking if avctx->extradata[0]==1 or not. */
2759  int i, j, num_arrays;
2760  int nal_len_size;
2761 
2762  s->is_nalff = 1;
2763 
2764  bytestream2_skip(&gb, 21);
2765  nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
2766  num_arrays = bytestream2_get_byte(&gb);
2767 
2768  /* nal units in the hvcC always have length coded with 2 bytes,
2769  * so put a fake nal_length_size = 2 while parsing them */
2770  s->nal_length_size = 2;
2771 
2772  /* Decode nal units from hvcC. */
2773  for (i = 0; i < num_arrays; i++) {
2774  int type = bytestream2_get_byte(&gb) & 0x3f;
2775  int cnt = bytestream2_get_be16(&gb);
2776 
2777  for (j = 0; j < cnt; j++) {
2778  // +2 for the nal size field
2779  int nalsize = bytestream2_peek_be16(&gb) + 2;
2780  if (bytestream2_get_bytes_left(&gb) < nalsize) {
2782  "Invalid NAL unit size in extradata.\n");
2783  return AVERROR_INVALIDDATA;
2784  }
2785 
2786  ret = decode_nal_units(s, gb.buffer, nalsize);
2787  if (ret < 0) {
2788  av_log(avctx, AV_LOG_ERROR,
2789  "Decoding nal unit %d %d from hvcC failed\n", type, i);
2790  return ret;
2791  }
2792  bytestream2_skip(&gb, nalsize);
2793  }
2794  }
2795 
2796  /* Now store right nal length size, that will be used to parse all other nals */
2797  s->nal_length_size = nal_len_size;
2798  } else {
2799  s->is_nalff = 0;
2800  ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
2801  if (ret < 0)
2802  return ret;
2803  }
2804  return 0;
2805 }
2806 
2808 {
2809  HEVCContext *s = avctx->priv_data;
2810  int ret;
2811 
2813 
2814  avctx->internal->allocate_progress = 1;
2815 
2816  ret = hevc_init_context(avctx);
2817  if (ret < 0)
2818  return ret;
2819 
2820  s->enable_parallel_tiles = 0;
2821 
2822  if(avctx->active_thread_type & FF_THREAD_SLICE)
2823  s->threads_number = avctx->thread_count;
2824  else
2825  s->threads_number = 1;
2826 
2827  if (avctx->extradata_size > 0 && avctx->extradata) {
2828  ret = hevc_decode_extradata(s);
2829  if (ret < 0) {
2830  hevc_decode_free(avctx);
2831  return ret;
2832  }
2833  }
2834 
2835  if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
2837  else
2839 
2840  return 0;
2841 }
2842 
2844 {
2845  HEVCContext *s = avctx->priv_data;
2846  int ret;
2847 
2848  memset(s, 0, sizeof(*s));
2849 
2850  ret = hevc_init_context(avctx);
2851  if (ret < 0)
2852  return ret;
2853 
2854  return 0;
2855 }
2856 
2858 {
2859  HEVCContext *s = avctx->priv_data;
2860  ff_hevc_flush_dpb(s);
2861  s->max_ra = INT_MAX;
2862 }
2863 
2864 #define OFFSET(x) offsetof(HEVCContext, x)
2865 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
2866 static const AVOption options[] = {
2867  { "strict-displaywin", "stricly apply default display window size", OFFSET(strict_def_disp_win),
2868  AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
2869  { NULL },
2870 };
2871 
2872 static const AVClass hevc_decoder_class = {
2873  .class_name = "HEVC decoder",
2874  .item_name = av_default_item_name,
2875  .option = options,
2876  .version = LIBAVUTIL_VERSION_INT,
2877 };
2878 
2880  .name = "hevc",
2881  .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
2882  .type = AVMEDIA_TYPE_VIDEO,
2883  .id = AV_CODEC_ID_HEVC,
2884  .priv_data_size = sizeof(HEVCContext),
2885  .priv_class = &hevc_decoder_class,
2893 };
int nals_allocated
Definition: hevc.h:859
VPS * vps_list[MAX_VPS_COUNT]
Definition: hevc.h:783
static int hls_coding_quadtree(HEVCContext *s, int x0, int y0, int log2_cb_size, int cb_depth)
Definition: hevc.c:1649
uint8_t ctb_up_flag
Definition: hevc.h:739
AVFrame * frame
Definition: hevc.h:688
unsigned int log2_min_cb_size
Definition: hevc.h:427
#define POS(c_idx, x, y)
VPS * vps
Definition: hevc.h:780
const char const char void * val
Definition: avisynth_c.h:671
TransformTree tt
Definition: hevc.h:731
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2678
#define SAMPLE_CTB(tab, x, y)
Definition: hevc.h:79
Definition: h264.h:112
HEVCPredContext hpc
Definition: hevc.h:810
const char * s
Definition: avisynth_c.h:668
#define AVERROR_PATCHWELCOME
static int shift(int a, int b)
Definition: sonic.c:78
int pic_order_cnt_lsb
Definition: hevc.h:534
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:105
static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref, const Mv *mv, int y0, int height)
Definition: hevc.c:1068
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
HEVCFrame * ref
Definition: hevc.h:798
static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:1499
Definition: hevc.h:623
void(* put_hevc_epel[2][2])(int16_t *dst, ptrdiff_t dststride, uint8_t *src, ptrdiff_t srcstride, int width, int height, int mx, int my, int16_t *mcbuffer)
Definition: hevcdsp.h:50
Definition: h264.h:113
int ctb_height
Definition: hevc.h:441
uint8_t is_cu_qp_delta_coded
Definition: hevc.h:662
AVOption.
Definition: opt.h:253
void(* emulated_edge_mc)(uint8_t *dst, ptrdiff_t dst_stride, const uint8_t *src, ptrdiff_t src_stride, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:61
#define av_always_inline
Definition: attributes.h:41
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:183
int pps_id
address (in raster order) of the first block in the current slice segment
Definition: hevc.h:525
static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
Definition: hevc.c:2545
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1324
void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth)
Definition: hevcpred.c:38
VideoDSPContext vdsp
Definition: hevc.h:812
static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
Definition: hevc.c:2073
Definition: hevc.h:106
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
AVFrame * f
Definition: thread.h:36
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
int ff_hevc_sao_band_position_decode(HEVCContext *s)
Definition: hevc_cabac.c:615
int vps_id
Definition: hevc.h:376
int16_t x
horizontal component of motion vector
Definition: hevc.h:619
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size)
Definition: hevc_cabac.c:725
uint8_t * cabac_state
Definition: hevc.h:771
uint8_t nb_refs
Definition: hevc.h:268
MvField * tab_mvf
Definition: hevc.h:690
int pic_init_qp_minus26
Definition: hevc.h:465
int bs_width
Definition: hevc.h:805
uint8_t intra_split_flag
IntraSplitFlag.
Definition: hevc.h:613
#define MAX_PPS_COUNT
Definition: h264.h:43
VUI vui
Definition: hevc.h:400
AVFrame * sao_frame
Definition: hevc.h:777
#define MAX_REFS
Definition: hevc.h:40
int rem_intra_luma_pred_mode
Definition: hevc.h:641
int vshift[3]
Definition: hevc.h:451
void(* put_hevc_qpel[4][4])(int16_t *dst, ptrdiff_t dststride, uint8_t *src, ptrdiff_t srcstride, int width, int height, int16_t *mcbuffer)
Definition: hevcdsp.h:47
static void pic_arrays_free(HEVCContext *s)
NOTE: Each function hls_foo correspond to the function foo in the specification (HLS stands for High ...
Definition: hevc.c:54
int size
Definition: avcodec.h:1064
unsigned int slice_addr
Definition: hevc.h:530
int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:848
int nb_nals
Definition: hevc.h:858
void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc_cabac.c:1401
uint8_t weighted_bipred_flag
Definition: hevc.h:477
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:88
#define DECLARE_ALIGNED(n, t, v)
Definition: avcodec.h:5102
static void 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)
Definition: hevc.c:732
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1517
int tc_offset
Definition: hevc.h:680
#define STARTCODE_TEST
void(* put_unweighted_pred)(uint8_t *dst, ptrdiff_t dststride, int16_t *src, ptrdiff_t srcstride, int width, int height)
Definition: hevcdsp.h:53
Clip a signed integer value into the amin-amax range.
Definition: avcodec.h:1429
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
PredictionUnit pu
Definition: hevc.h:749
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:131
uint8_t seq_loop_filter_across_slices_enabled_flag
Definition: hevc.h:490
uint8_t cabac_init_present_flag
Definition: hevc.h:461
int16_t chroma_offset_l1[16][2]
Definition: hevc.h:592
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
Definition: pthread.c:1147
uint8_t cbf_cb[MAX_TRANSFORM_DEPTH][MAX_CU_SIZE *MAX_CU_SIZE]
Definition: hevc.h:649
static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output, AVPacket *avpkt)
Definition: hevc.c:2500
int x
Definition: hevc.h:602
void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int part_idx, int merge_idx, MvField *mv, int mvp_lx_flag, int LX)
Definition: hevc_mvs.c:620
#define EPEL_EXTRA
Definition: hevc.h:73
AVCodec ff_hevc_decoder
Definition: hevc.c:2879
int min_cb_height
Definition: hevc.h:444
int * ctb_addr_ts_to_rs
CtbAddrTSToRS.
Definition: hevc.h:517
#define IS_IDR(s)
Definition: hevc.h:82
int num_ref_idx_l0_default_active
num_ref_idx_l0_default_active_minus1 + 1
Definition: hevc.h:463
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
Definition: pthread.c:1136
int * skipped_bytes_pos
Definition: hevc.h:848
struct HEVCFrame * ref[MAX_REFS]
Definition: hevc.h:272
Definition: hevc.h:129
uint8_t dependent_slice_segment_flag
Definition: hevc.h:537
CABACContext cc
Definition: hevc.h:730
ShortTermRPS slice_rps
Definition: hevc.h:542
ShortTermRPS st_rps[MAX_SHORT_TERM_RPS_COUNT]
Definition: hevc.h:407
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
static int hls_slice_header(HEVCContext *s)
Definition: hevc.c:283
int width
Definition: hevc.h:438
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:68
uint16_t seq_decode
Sequence counters for decoded and output frames, so that old frames are output first after a POC rese...
Definition: hevc.h:842
static void intra_prediction_unit_default_value(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:1477
Definition: hevc.h:197
void av_md5_update(struct AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Definition: md5.c:148
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
int ff_hevc_decode_nal_sei(HEVCContext *s)
Definition: hevc_sei.c:123
uint8_t threads_type
Definition: hevc.h:765
int ff_hevc_frame_nb_refs(HEVCContext *s)
Get the number of candidate references for the current frame.
Definition: hevc_refs.c:465
int qp_bd_offset
Definition: hevc.h:453
int pixel_shift
Definition: hevc.h:387
uint8_t entropy_coding_sync_enabled_flag
Definition: hevc.h:483
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
int max_ra
Definition: hevc.h:804
#define FFMAX3(a, b, c)
Definition: avcodec.h:924
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
int output_width
Definition: hevc.h:381
AVBufferPool * rpl_tab_pool
candidate references for the current frame
Definition: hevc.h:788
CodingTree ct
Definition: hevc.h:747
static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
Definition: hevc.c:1769
static int verify_md5(HEVCContext *s, AVFrame *frame)
Definition: hevc.c:2434
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: utils.c:153
#define EPEL_EXTRA_AFTER
Definition: hevc.h:72
#define L0
Definition: hevc.h:68
int chroma_format_idc
Definition: hevc.h:377
uint8_t disable_dbf
Definition: hevc.h:494
static uint8_t * res
Definition: ffhash.c:43
InterPredIdc
Definition: hevc.h:195
unsigned int log2_max_trafo_size
Definition: hevc.h:430
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice
Definition: hevc.h:528
Definition: hevc.h:198
int is_nalff
this flag is != 0 if bitstream is encapsulated as a format defined in 14496-15
Definition: hevc.h:867
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length, HEVCNAL *nal)
Definition: hevc.c:2208
int end_of_tiles_x
Definition: hevc.h:743
uint8_t
int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s)
Definition: hevc_cabac.c:780
Definition: hevc.h:203
int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size)
Definition: hevc_cabac.c:884
float delta
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
Definition: hevc_cabac.c:707
uint8_t ctb_up_right_flag
Definition: hevc.h:740
LongTermRPS long_term_rps
Definition: hevc.h:544
const uint8_t * data
Definition: hevc.h:720
int poc[32]
Definition: hevc.h:266
int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
Compute POC of the current frame and return it.
Definition: hevc_refs.c:442
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
#define SAMPLE(tab, x, y)
Value of the luma sample at position (x, y) in the 2D array tab.
Definition: hevc.h:78
void(* put_pcm)(uint8_t *_dst, ptrdiff_t _stride, int size, GetBitContext *gb, int pcm_bit_depth)
Definition: hevcdsp.h:31
int min_tb_width
Definition: hevc.h:445
int depth
ctDepth
Definition: hevc.h:598
uint8_t * rbsp_buffer
Definition: hevc.h:716
int num_entry_point_offsets
Definition: hevc.h:576
void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0, int log2_trafo_size, enum ScanType scan_idx, int c_idx)
Definition: hevc_cabac.c:1058
#define OFFSET(x)
Definition: hevc.c:2864
AVFrame * output_frame
Definition: hevc.h:779
SAOParams * sao
Definition: hevc.h:794
int num_ref_idx_l1_default_active
num_ref_idx_l1_default_active_minus1 + 1
Definition: hevc.h:464
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int wpp_err
Definition: hevc.h:846
unsigned int log2_min_pcm_cb_size
Definition: hevc.h:420
static int hls_nal_unit(HEVCContext *s)
Definition: hevc.c:1975
AVCodecContext * avctx
Definition: hevc.h:758
static av_cold int hevc_decode_init(AVCodecContext *avctx)
Definition: hevc.c:2807
int min_cb_width
Definition: hevc.h:443
int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
Definition: hevc_ps.c:72
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:652
#define avpriv_atomic_int_set
Definition: atomic_gcc.h:35
const char data[16]
Definition: mxf.c:68
struct HEVCSPS::@63 temporal_layer[MAX_SUB_LAYERS]
void ff_hevc_flush_dpb(HEVCContext *s)
Drop all frames currently in DPB.
Definition: hevc_refs.c:73
HEVCNAL * nals
Definition: hevc.h:857
ThreadFrame tf
Definition: hevc.h:689
int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:767
uint8_t first_slice_in_pic_flag
Definition: hevc.h:536
uint8_t bit_depth_chroma
Definition: hevc.h:419
const uint8_t * buffer
Definition: bytestream.h:34
uint8_t ctb_up_left_flag
Definition: hevc.h:741
void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0, int log2_trafo_size, int slice_or_tiles_up_boundary, int slice_or_tiles_left_boundary)
Definition: hevc_filter.c:551
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1190
void ff_hevc_clear_refs(HEVCContext *s)
Mark all frames in DPB as unused for reference.
Definition: hevc_refs.c:65
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:3273
uint8_t threads_number
Definition: hevc.h:766
void(* put_weighted_pred_avg)(uint8_t *dst, ptrdiff_t dststride, int16_t *src1, int16_t *src2, ptrdiff_t srcstride, int width, int height)
Definition: hevcdsp.h:56
int max_num_merge_cand
5 - 5_minus_max_num_merge_cand
Definition: hevc.h:570
uint8_t cbf_cr[MAX_TRANSFORM_DEPTH][MAX_CU_SIZE *MAX_CU_SIZE]
Definition: hevc.h:650
int8_t * qp_y_tab
Definition: hevc.h:814
uint8_t loop_filter_disable_flag
Definition: hevc.h:422
uint8_t pic_output_flag
Definition: hevc.h:538
uint8_t * tab_ct_depth
Definition: hevc.h:823
uint8_t cu_transquant_bypass_flag
Definition: hevc.h:615
#define FFSWAP(type, a, b)
Definition: avcodec.h:928
int * skipped_bytes_pos_size_nal
Definition: hevc.h:853
const OptionDef options[]
Definition: ffserver.c:4682
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
uint8_t transquant_bypass_enable_flag
Definition: hevc.h:479
uint8_t used[32]
Definition: hevc.h:267
int temporal_id
temporal_id_plus1 - 1
Definition: hevc.h:797
uint8_t first_qp_group
Definition: hevc.h:727
HEVCDSPContext hevcdsp
Definition: hevc.h:811
int ctb_count
Definition: hevc.h:693
uint8_t no_output_of_prior_pics_flag
Definition: hevc.h:548
static AVFrame * frame
Definition: demuxing.c:51
HEVCLocalContext * HEVClcList[MAX_NB_THREADS]
Definition: hevc.h:762
const uint8_t ff_hevc_qpel_extra_after[4]
Definition: hevc.c:41
int slice_idx
number of the slice being currently decoded
Definition: hevc.h:802
int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH)
Definition: hevc_cabac.c:817
uint8_t intra_pred_mode[4]
Definition: hevc.h:642
const HEVCSPS * sps
Definition: hevc.h:781
void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
Definition: hevc_refs.c:30
uint8_t colour_plane_id
RPS coded in the slice header itself is stored here.
Definition: hevc.h:539
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread.c:684
int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx)
Definition: hevc_cabac.c:827
uint16_t depth_minus1
number of bits in the component minus 1
Definition: pixdesc.h:45
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1427
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
uint8_t slice_initialized
1 if the independent slice segment header was successfully parsed
Definition: hevc.h:774
unsigned int log2_max_poc_lsb
Definition: hevc.h:390
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:77
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: dsputil.h:206
int min_pu_height
Definition: hevc.h:448
Definition: hevc.h:92
static void restore_tqb_pixels(HEVCContext *s)
Definition: hevc.c:1997
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:769
Definition: h264.h:111
AVBufferRef * rpl_tab_buf
Definition: hevc.h:700
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:47
#define avpriv_atomic_int_get
Definition: atomic_gcc.h:28
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:162
uint8_t rpl_modification_flag[2]
Definition: hevc.h:547
int * size
Definition: hevc.h:575
#define CTB(tab, x, y)
Definition: hevc.c:647
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2608
static void hevc_decode_flush(AVCodecContext *avctx)
Definition: hevc.c:2857
#define PAR
Definition: ac3dec.c:1434
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2615
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:110
RefPicList * refPicList
Definition: hevc.h:691
int16_t luma_offset_l0[16]
Definition: hevc.h:588
int ff_hevc_mvp_lx_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:843
static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride, AVFrame *ref, const Mv *mv, int x_off, int y_off, int block_w, int block_h)
8.5.3.2.2.1 Luma sample interpolation process
Definition: hevc.c:966
int bs_height
Definition: hevc.h:806
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
int ff_hevc_slice_rpl(HEVCContext *s)
Construct the reference picture list(s) for the current slice.
Definition: hevc_refs.c:224
#define s0
Definition: regdef.h:37
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:152
const char * arg
Definition: jacosubdec.c:69
unsigned int log2_ctb_size
Definition: hevc.h:431
int ** skipped_bytes_pos_nal
Definition: hevc.h:852
int tc_offset
tc_offset_div2 * 2
Definition: hevc.h:568
const ShortTermRPS * short_term_rps
Definition: hevc.h:543
uint8_t merge_flag
Definition: hevc.h:644
void(* weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag, uint8_t *dst, ptrdiff_t dststride, int16_t *src, ptrdiff_t srcstride, int width, int height)
Definition: hevcdsp.h:58
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
void ff_init_cabac_states(void)
Definition: cabac.c:137
#define EPEL_EXTRA_BEFORE
Definition: hevc.h:71
struct AVMD5 * md5_ctx
Definition: hevc.h:862
uint8_t cbf_luma
Definition: hevc.h:651
int8_t slice_qp
Definition: hevc.h:578
uint8_t max_trafo_depth
MaxTrafoDepth.
Definition: hevc.h:614
uint16_t sequence
A sequence counter, so that old frames are output first after a POC reset.
Definition: hevc.h:707
static av_cold int hevc_init_context(AVCodecContext *avctx)
Definition: hevc.c:2637
uint8_t slice_temporal_mvp_enabled_flag
Definition: hevc.h:549
uint8_t * vertical_bs
Definition: hevc.h:817
static char * split(char *message, char delim)
Definition: af_channelmap.c:82
uint8_t tiles_enabled_flag
Definition: hevc.h:482
int eo_class[3]
sao_eo_class
Definition: hevc.h:671
int16_t luma_weight_l0[16]
Definition: hevc.h:583
static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: ffv1dec.c:1023
static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
Definition: hevc.c:2315
int * col_idxX
Definition: hevc.h:514
struct HEVCContext * sList[MAX_NB_THREADS]
Definition: hevc.h:760
goto fail
Definition: avfilter.c:963
int slice_qp_delta
Definition: hevc.h:563
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
static int pic_arrays_init(HEVCContext *s)
Definition: hevc.c:83
#define SAMPLE_CBF(tab, x, y)
Definition: hevc.h:80
int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s)
Definition: hevc_cabac.c:790
#define FF_ARRAY_ELEMS(a)
Definition: avcodec.h:929
uint8_t is_intra
Definition: hevc.h:627
uint8_t lists_modification_present_flag
Definition: hevc.h:501
static void intra_prediction_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:1437
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
AVBufferRef * tab_mvf_buf
Definition: hevc.h:699
uint8_t type_idx[3]
sao_type_idx
Definition: hevc.h:675
int ff_hevc_pred_mode_decode(HEVCContext *s)
Definition: hevc_cabac.c:702
int ff_hevc_sao_eo_class_decode(HEVCContext *s)
Definition: hevc_cabac.c:640
int beta_offset
beta_offset_div2 * 2
Definition: hevc.h:567
uint8_t * data
The data buffer.
Definition: buffer.h:89
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2476
int ff_hevc_sao_offset_sign_decode(HEVCContext *s)
Definition: hevc_cabac.c:635
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:351
int max_transform_hierarchy_depth_inter
Definition: hevc.h:434
void(* weighted_pred_avg)(uint8_t denom, int16_t wl0Flag, int16_t wl1Flag, int16_t ol0Flag, int16_t ol1Flag, uint8_t *dst, ptrdiff_t dststride, int16_t *src1, int16_t *src2, ptrdiff_t srcstride, int width, int height)
Definition: hevcdsp.h:60
int16_t mc_buffer[(64+7)*64]
Definition: hevc.h:724
int edge_emu_buffer_size
Definition: hevc.h:746
int rbsp_buffer_size
Definition: hevc.h:717
static int hevc_frame_start(HEVCContext *s)
Definition: hevc.c:2024
float y
int slice_cr_qp_offset
Definition: hevc.h:565
int offset_abs[3][4]
sao_offset_abs
Definition: hevc.h:666
int num_tile_columns
num_tile_columns_minus1 + 1
Definition: hevc.h:485
int * column_width
ColumnWidth.
Definition: hevc.h:510
ret
Definition: avfilter.c:961
int output_height
Definition: hevc.h:381
int width
picture width / height.
Definition: avcodec.h:1314
Definition: hevc.h:127
uint8_t * tab_ipm
Definition: hevc.h:825
int ff_hevc_sao_type_idx_decode(HEVCContext *s)
Definition: hevc_cabac.c:605
int size
Definition: hevc.h:719
#define AV_RN32A(p)
Definition: intreadwrite.h:518
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
uint8_t * edge_emu_buffer
Definition: hevc.h:745
int hshift[3]
Definition: hevc.h:450
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: hevc.h:784
int ff_hevc_end_of_slice_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:647
uint8_t cu_qp_delta_enabled_flag
Definition: hevc.h:470
#define FFMIN(a, b)
Definition: avcodec.h:925
uint8_t used_by_curr_pic_lt_sps_flag[32]
Definition: hevc.h:414
int8_t qp_y
Definition: hevc.h:733
const uint8_t ff_hevc_qpel_extra_before[4]
Definition: hevc.c:40
#define HEVC_CONTEXTS
Definition: hevc.h:64
Context Adaptive Binary Arithmetic Coder inline functions.
int ctb_width
Definition: hevc.h:440
int16_t chroma_weight_l0[16][2]
Definition: hevc.h:584
HEVCPPS * pps
Definition: hevc.h:782
int height
Definition: hevc.h:439
int n
Definition: avisynth_c.h:588
int ff_alloc_entries(AVCodecContext *avctx, int count)
Definition: pthread.c:1163
uint8_t output_flag_present_flag
Definition: hevc.h:478
static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
Definition: hevc.c:2843
static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
Definition: hevc.c:142
uint16_t seq_output
Definition: hevc.h:843
int skipped_bytes
Definition: hevc.h:847
int mpm_idx
Definition: hevc.h:640
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:203
int16_t luma_offset_l1[16]
Definition: hevc.h:591
void ff_hevc_set_qPy(HEVCContext *s, int xC, int yC, int xBase, int yBase, int log2_cb_size)
Definition: hevc_filter.c:152
int offset_val[3][5]
SaoOffsetVal.
Definition: hevc.h:673
int16_t chroma_offset_l0[16][2]
Definition: hevc.h:589
int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s)
Definition: hevc_cabac.c:697
void ff_reset_entries(AVCodecContext *avctx)
Definition: pthread.c:1189
AVBufferRef * pps_list[MAX_PPS_COUNT]
Definition: hevc.h:785
static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb, int ctb_addr_ts)
Definition: hevc.c:1715
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2596
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:498
int ff_hevc_output_frame(HEVCContext *s, AVFrame *frame, int flush)
Find next frame in output order and put a reference to it in frame.
Definition: hevc_refs.c:150
Definition: hevc.h:241
static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size, int prev_intra_luma_pred_flag)
8.4.1
Definition: hevc.c:1336
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1938
int sps_id
seq_parameter_set_id
Definition: hevc.h:457
Definition: hevc.h:93
uint8_t pic_slice_level_chroma_qp_offsets_present_flag
Definition: hevc.h:475
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:85
static const int8_t mv[256][2]
Definition: 4xm.c:73
HEVCFrame DPB[32]
Definition: hevc.h:799
Definition: hevc.h:375
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:157
enum AVPixelFormat pix_fmt
Definition: hevc.h:388
RefPicListTab ** rpl_tab
Definition: hevc.h:692
int context_initialized
Definition: hevc.h:866
int slice_cb_qp_offset
Definition: hevc.h:564
void ff_hevc_dsp_init(HEVCDSPContext *hevcdsp, int bit_depth)
Definition: hevcdsp.c:115
static int width
Definition: utils.c:158
#define AV_LOG_INFO
Standard information.
Definition: avcodec.h:4158
#define SHIFT_CTB_WPP
Definition: hevc.h:43
int ff_hevc_merge_idx_decode(HEVCContext *s)
Definition: hevc_cabac.c:801
Definition: hevc.h:456
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:65
AVS_Value src
Definition: avisynth_c.h:523
Definition: hevc.h:715
int * ctb_addr_rs_to_ts
CtbAddrRSToTS.
Definition: hevc.h:516
static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0, int log2_cb_size, int ct_depth)
Definition: hevc.c:1424
int ff_hevc_merge_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:812
int max_sub_layers
Definition: hevc.h:393
unsigned int log2_min_pu_size
Definition: hevc.h:432
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:436
int8_t pred_flag[2]
Definition: hevc.h:626
uint8_t md5[3][16]
Definition: hevc.h:863
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:57
main external API structure.
Definition: avcodec.h:1146
uint8_t is_md5
Definition: hevc.h:864
uint8_t sao_enabled
Definition: hevc.h:410
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
enum PredMode pred_mode
PredMode.
Definition: hevc.h:605
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2607
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:373
int num_extra_slice_header_bits
Definition: hevc.h:503
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2480
int16_t y
vertical component of motion vector
Definition: hevc.h:620
int height
Definition: hevc.h:769
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
int * skipped_bytes_nal
Definition: hevc.h:851
uint8_t num_long_term_ref_pics_sps
Definition: hevc.h:415
void av_md5_init(struct AVMD5 *ctx)
Initialize MD5 hashing.
Definition: md5.c:138
TransformUnit tu
Definition: hevc.h:736
void * buf
Definition: avisynth_c.h:594
int extradata_size
Definition: avcodec.h:1255
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:366
uint8_t ctb_left_flag
Definition: hevc.h:738
int y
Definition: hevc.h:603
uint8_t deblocking_filter_control_present_flag
Definition: hevc.h:492
int cu_qp_delta
Definition: hevc.h:658
uint8_t * is_pcm
Definition: hevc.h:829
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
uint8_t * checksum_buf
used on BE to byteswap the lines for checksumming
Definition: hevc.h:835
uint8_t sps_temporal_mvp_enabled_flag
Definition: hevc.h:424
unsigned int nb_st_rps
Definition: hevc.h:406
int coded_height
Definition: avcodec.h:1324
AVFrame * tmp_frame
Definition: hevc.h:778
uint8_t cabac_init_flag
Definition: hevc.h:556
Describe the class of an AVClass context structure.
Definition: log.h:50
int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth)
Definition: hevc_cabac.c:894
int num_tile_rows
num_tile_rows_minus1 + 1
Definition: hevc.h:486
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:81
static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:922
int poc
Definition: hevc.h:694
void av_buffer_pool_uninit(AVBufferPool **pool)
Mark the pool as being available for freeing.
Definition: buffer.c:236
DSPContext dsp
Definition: hevc.h:813
uint8_t * data
Definition: avcodec.h:1063
AVFrame * frame
Definition: hevc.h:776
void(* intra_pred)(struct HEVCContext *s, int x0, int y0, int log2_size, int c_idx)
Definition: hevcpred.h:32
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:405
int enable_parallel_tiles
Definition: hevc.h:845
void ff_hevc_luma_mv_merge_mode(HEVCContext *s, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int part_idx, int merge_idx, MvField *mv)
Definition: hevc_mvs.c:529
int checksum_buf_size
Definition: hevc.h:836
DBParams * deblock
Definition: hevc.h:795
GetBitContext gb
Definition: hevc.h:729
unsigned int log2_min_tb_size
Definition: hevc.h:429
int poc
Definition: hevc.h:800
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
enum PartMode part_mode
PartMode.
Definition: hevc.h:606
uint16_t lt_ref_pic_poc_lsb_sps[32]
Definition: hevc.h:413
static int hevc_decode_extradata(HEVCContext *s)
Definition: hevc.c:2744
#define s1
Definition: regdef.h:38
enum NALUnitType nal_unit_type
Definition: hevc.h:796
int start_of_tiles_x
Definition: hevc.h:742
void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts)
Definition: hevc_cabac.c:555
Definition: hevc.h:618
void av_md5_final(struct AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:183
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:78
HEVCWindow window
Definition: hevc.h:697
int ff_hevc_frame_rps(HEVCContext *s)
Construct the reference picture sets for the current frame.
Definition: hevc_refs.c:383
int * tile_id
TileId.
Definition: hevc.h:518
#define type
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread.c:666
int16_t luma_weight_l1[16]
Definition: hevc.h:586
int16_t chroma_log2_weight_denom
Definition: hevc.h:581
int tc_offset
tc_offset_div2 * 2
Definition: hevc.h:496
int pocTid0
Definition: hevc.h:801
uint8_t flags
A combination of HEVC_FRAME_FLAG_*.
Definition: hevc.h:712
HEVCLocalContext * HEVClc
Definition: hevc.h:763
int ff_hevc_cu_qp_delta_abs(HEVCContext *s)
Definition: hevc_cabac.c:672
void * priv_data
Definition: avcodec.h:1182
#define MAX_PB_SIZE
Definition: hevc.h:59
static void * av_malloc_array(size_t nmemb, size_t size)
Definition: mem.h:93
uint8_t level
Definition: svq3.c:146
uint8_t * split_cu_flag
Definition: hevc.h:815
uint8_t intra_pred_mode_c
Definition: hevc.h:645
static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
Definition: hevc.c:1893
int eos
current packet contains an EOS/EOB NAL
Definition: hevc.h:803
int skipped_bytes_pos_size
Definition: hevc.h:849
void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
Definition: hevc_filter.c:682
void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts)
Definition: hevc_cabac.c:504
int max_transform_hierarchy_depth_intra
coded frame dimension in various units
Definition: hevc.h:435
Mv mv[2]
Definition: hevc.h:624
uint8_t * skip_flag
Definition: hevc.h:822
int8_t ref_idx[2]
Definition: hevc.h:625
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:815
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:206
uint8_t slice_or_tiles_up_boundary
Definition: hevc.h:753
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:811
Definition: hevc.h:182
uint8_t weighted_pred_flag
Definition: hevc.h:476
uint8_t * horizontal_bs
Definition: hevc.h:816
#define IS_BLA(s)
Definition: hevc.h:83
unsigned int nb_refs[2]
Definition: hevc.h:551
int32_t * tab_slice_address
Definition: hevc.h:819
uint8_t disable_deblocking_filter_flag
slice_header_disable_deblocking_filter_flag
Definition: hevc.h:557
uint8_t * filter_slice_edges
Definition: hevc.h:832
uint8_t slice_header_extension_present_flag
Definition: hevc.h:504
uint8_t collocated_list
Definition: hevc.h:559
int ff_hevc_sao_merge_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:600
int nal_length_size
Number of bytes used for nal length (1, 2 or 4)
Definition: hevc.h:871
#define L1
Definition: hevc.h:69
int slice_ctb_addr_rs
Definition: hevc.h:594
AVBufferPool * tab_mvf_pool
Definition: hevc.h:787
Definition: hevc.h:196
static int hevc_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: hevc.c:2685
DSP utils.
AVRational sar
Definition: hevc.h:290
uint8_t slice_loop_filter_across_slices_enabled_flag
Definition: hevc.h:558
struct HEVCSPS::@64 pcm
uint8_t inter_split_flag
Definition: hevc.h:654
#define SET_SAO(elem, value)
Definition: hevc.c:649
int ff_hevc_decode_nal_sps(HEVCContext *s)
Definition: hevc_ps.c:596
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:91
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:122
int ff_hevc_mpm_idx_decode(HEVCContext *s)
Definition: hevc_cabac.c:772
unsigned int collocated_ref_idx
Definition: hevc.h:561
uint8_t pcm_flag
Definition: hevc.h:610
#define AVERROR_INVALIDDATA
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2636
CodingUnit cu
Definition: hevc.h:748
int ff_hevc_decode_nal_pps(HEVCContext *s)
Definition: hevc_ps.c:961
int ff_hevc_decode_nal_vps(HEVCContext *s)
Definition: hevc_ps.c:321
int len
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:2656
int min_pu_width
Definition: hevc.h:447
int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
Definition: hevc_refs.c:118
int beta_offset
Definition: hevc.h:679
static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
Definition: hevc.c:806
void ff_hevc_set_neighbour_available(HEVCContext *s, int x0, int y0, int nPbW, int nPbH)
Definition: hevc_mvs.c:41
const uint8_t ff_hevc_qpel_extra[4]
Definition: hevc.c:42
unsigned int list_entry_lx[2][32]
Definition: hevc.h:545
uint8_t luma_log2_weight_denom
Definition: hevc.h:580
int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth)
Definition: hevc_cabac.c:889
int16_t chroma_weight_l1[16][2]
Definition: hevc.h:585
uint8_t slice_or_tiles_left_boundary
Definition: hevc.h:752
#define AVERROR(e)
uint8_t long_term_ref_pics_present_flag
Definition: hevc.h:412
static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
Definition: hevc.c:231
static const AVClass hevc_decoder_class
Definition: hevc.c:2872
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
static void hls_sao_param(HEVCContext *s, int rx, int ry)
Definition: hevc.c:661
Definition: hevc.h:242
int diff_cu_qp_delta_depth
Definition: hevc.h:471
Definition: hevc.h:128
#define AV_EF_CRCCHECK
verify embedded CRCs
Definition: avcodec.h:2477
int cur_intra_pred_mode
Definition: hevc.h:661
int num_reorder_pics
Definition: hevc.h:396
static int init_thread_copy(AVCodecContext *avctx)
Definition: alac.c:620
uint8_t * data
Definition: hevc.h:855
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
int ff_hevc_sao_offset_abs_decode(HEVCContext *s)
Definition: hevc_cabac.c:625
static void print_md5(void *log_ctx, int level, uint8_t md5[16])
Definition: hevc.c:2427
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
Definition: pthread.c:702
AVBufferRef * rpl_buf
Definition: hevc.h:701
int is_decoded
Definition: hevc.h:808
const char int length
Definition: avisynth_c.h:668
uint8_t deblocking_filter_override_enabled_flag
Definition: hevc.h:493
int bit_depth
Definition: hevc.h:386
enum SliceType slice_type
Definition: hevc.h:532
int beta_offset
beta_offset_div2 * 2
Definition: hevc.h:495
int min_tb_height
Definition: hevc.h:446
uint8_t * cbf_luma
Definition: hevc.h:828
SliceHeader sh
Definition: hevc.h:793
static av_cold int hevc_decode_free(AVCodecContext *avctx)
Definition: hevc.c:2579
exp golomb vlc stuff
int pcm_enabled_flag
Definition: hevc.h:391
int width
Definition: hevc.h:768
This structure stores compressed data.
Definition: avcodec.h:1040
int * offset
Definition: hevc.h:574
uint8_t mvd_l1_zero_flag
Definition: hevc.h:554
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
static int hls_slice_data(HEVCContext *s)
Definition: hevc.c:1809
for(j=16;j >0;--j)
uint8_t separate_colour_plane_flag
output (i.e. cropped) values
Definition: hevc.h:378
static void 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)
Definition: hevc.c:821
int end_of_tiles_y
Definition: hevc.h:744
uint8_t rqt_root_cbf
Definition: hevc.h:608
int * entry_point_offset
Definition: hevc.h:573
int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb)
Definition: hevc_cabac.c:657
void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
Definition: hevc_filter.c:675
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
#define AV_RN64A(p)
Definition: intreadwrite.h:522
static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
Definition: hevc.c:1820
uint8_t slice_sample_adaptive_offset_flag[3]
Definition: hevc.h:553
static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2, ptrdiff_t dststride, AVFrame *ref, const Mv *mv, int x_off, int y_off, int block_w, int block_h)
8.5.3.2.2.2 Chroma sample interpolation process
Definition: hevc.c:1014
uint8_t dependent_slice_segments_enabled_flag
Definition: hevc.h:481
int offset_sign[3][4]
sao_offset_sign
Definition: hevc.h:667
int ff_hevc_pcm_flag_decode(HEVCContext *s)
Definition: hevc_cabac.c:762
static void hls_prediction_unit(HEVCContext *s, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int partIdx)
Definition: hevc.c:1077