Commit | Line | Data |
---|---|---|
2ba45a60 DM |
1 | /* |
2 | * HEVC Parameter Set decoding | |
3 | * | |
4 | * Copyright (C) 2012 - 2103 Guillaume Martres | |
5 | * Copyright (C) 2012 - 2103 Mickael Raulet | |
6 | * Copyright (C) 2012 - 2013 Gildas Cocherel | |
7 | * Copyright (C) 2013 Vittorio Giovara | |
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/imgutils.h" | |
27 | #include "golomb.h" | |
28 | #include "hevc.h" | |
29 | ||
30 | static const uint8_t default_scaling_list_intra[] = { | |
31 | 16, 16, 16, 16, 17, 18, 21, 24, | |
32 | 16, 16, 16, 16, 17, 19, 22, 25, | |
33 | 16, 16, 17, 18, 20, 22, 25, 29, | |
34 | 16, 16, 18, 21, 24, 27, 31, 36, | |
35 | 17, 17, 20, 24, 30, 35, 41, 47, | |
36 | 18, 19, 22, 27, 35, 44, 54, 65, | |
37 | 21, 22, 25, 31, 41, 54, 70, 88, | |
38 | 24, 25, 29, 36, 47, 65, 88, 115 | |
39 | }; | |
40 | ||
41 | static const uint8_t default_scaling_list_inter[] = { | |
42 | 16, 16, 16, 16, 17, 18, 20, 24, | |
43 | 16, 16, 16, 17, 18, 20, 24, 25, | |
44 | 16, 16, 17, 18, 20, 24, 25, 28, | |
45 | 16, 17, 18, 20, 24, 25, 28, 33, | |
46 | 17, 18, 20, 24, 25, 28, 33, 41, | |
47 | 18, 20, 24, 25, 28, 33, 41, 54, | |
48 | 20, 24, 25, 28, 33, 41, 54, 71, | |
49 | 24, 25, 28, 33, 41, 54, 71, 91 | |
50 | }; | |
51 | ||
52 | static const AVRational vui_sar[] = { | |
53 | { 0, 1 }, | |
54 | { 1, 1 }, | |
55 | { 12, 11 }, | |
56 | { 10, 11 }, | |
57 | { 16, 11 }, | |
58 | { 40, 33 }, | |
59 | { 24, 11 }, | |
60 | { 20, 11 }, | |
61 | { 32, 11 }, | |
62 | { 80, 33 }, | |
63 | { 18, 11 }, | |
64 | { 15, 11 }, | |
65 | { 64, 33 }, | |
66 | { 160, 99 }, | |
67 | { 4, 3 }, | |
68 | { 3, 2 }, | |
69 | { 2, 1 }, | |
70 | }; | |
71 | ||
72 | int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps, | |
73 | const HEVCSPS *sps, int is_slice_header) | |
74 | { | |
75 | HEVCLocalContext *lc = s->HEVClc; | |
76 | uint8_t rps_predict = 0; | |
77 | int delta_poc; | |
78 | int k0 = 0; | |
79 | int k1 = 0; | |
80 | int k = 0; | |
81 | int i; | |
82 | ||
83 | GetBitContext *gb = &lc->gb; | |
84 | ||
85 | if (rps != sps->st_rps && sps->nb_st_rps) | |
86 | rps_predict = get_bits1(gb); | |
87 | ||
88 | if (rps_predict) { | |
89 | const ShortTermRPS *rps_ridx; | |
90 | int delta_rps; | |
91 | unsigned abs_delta_rps; | |
92 | uint8_t use_delta_flag = 0; | |
93 | uint8_t delta_rps_sign; | |
94 | ||
95 | if (is_slice_header) { | |
96 | unsigned int delta_idx = get_ue_golomb_long(gb) + 1; | |
97 | if (delta_idx > sps->nb_st_rps) { | |
98 | av_log(s->avctx, AV_LOG_ERROR, | |
99 | "Invalid value of delta_idx in slice header RPS: %d > %d.\n", | |
100 | delta_idx, sps->nb_st_rps); | |
101 | return AVERROR_INVALIDDATA; | |
102 | } | |
103 | rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx]; | |
104 | } else | |
105 | rps_ridx = &sps->st_rps[rps - sps->st_rps - 1]; | |
106 | ||
107 | delta_rps_sign = get_bits1(gb); | |
108 | abs_delta_rps = get_ue_golomb_long(gb) + 1; | |
109 | if (abs_delta_rps < 1 || abs_delta_rps > 32768) { | |
110 | av_log(s->avctx, AV_LOG_ERROR, | |
111 | "Invalid value of abs_delta_rps: %d\n", | |
112 | abs_delta_rps); | |
113 | return AVERROR_INVALIDDATA; | |
114 | } | |
115 | delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps; | |
116 | for (i = 0; i <= rps_ridx->num_delta_pocs; i++) { | |
117 | int used = rps->used[k] = get_bits1(gb); | |
118 | ||
119 | if (!used) | |
120 | use_delta_flag = get_bits1(gb); | |
121 | ||
122 | if (used || use_delta_flag) { | |
123 | if (i < rps_ridx->num_delta_pocs) | |
124 | delta_poc = delta_rps + rps_ridx->delta_poc[i]; | |
125 | else | |
126 | delta_poc = delta_rps; | |
127 | rps->delta_poc[k] = delta_poc; | |
128 | if (delta_poc < 0) | |
129 | k0++; | |
130 | else | |
131 | k1++; | |
132 | k++; | |
133 | } | |
134 | } | |
135 | ||
136 | rps->num_delta_pocs = k; | |
137 | rps->num_negative_pics = k0; | |
138 | // sort in increasing order (smallest first) | |
139 | if (rps->num_delta_pocs != 0) { | |
140 | int used, tmp; | |
141 | for (i = 1; i < rps->num_delta_pocs; i++) { | |
142 | delta_poc = rps->delta_poc[i]; | |
143 | used = rps->used[i]; | |
144 | for (k = i - 1; k >= 0; k--) { | |
145 | tmp = rps->delta_poc[k]; | |
146 | if (delta_poc < tmp) { | |
147 | rps->delta_poc[k + 1] = tmp; | |
148 | rps->used[k + 1] = rps->used[k]; | |
149 | rps->delta_poc[k] = delta_poc; | |
150 | rps->used[k] = used; | |
151 | } | |
152 | } | |
153 | } | |
154 | } | |
155 | if ((rps->num_negative_pics >> 1) != 0) { | |
156 | int used; | |
157 | k = rps->num_negative_pics - 1; | |
158 | // flip the negative values to largest first | |
159 | for (i = 0; i < rps->num_negative_pics >> 1; i++) { | |
160 | delta_poc = rps->delta_poc[i]; | |
161 | used = rps->used[i]; | |
162 | rps->delta_poc[i] = rps->delta_poc[k]; | |
163 | rps->used[i] = rps->used[k]; | |
164 | rps->delta_poc[k] = delta_poc; | |
165 | rps->used[k] = used; | |
166 | k--; | |
167 | } | |
168 | } | |
169 | } else { | |
170 | unsigned int prev, nb_positive_pics; | |
171 | rps->num_negative_pics = get_ue_golomb_long(gb); | |
172 | nb_positive_pics = get_ue_golomb_long(gb); | |
173 | ||
174 | if (rps->num_negative_pics >= MAX_REFS || | |
175 | nb_positive_pics >= MAX_REFS) { | |
176 | av_log(s->avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n"); | |
177 | return AVERROR_INVALIDDATA; | |
178 | } | |
179 | ||
180 | rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics; | |
181 | if (rps->num_delta_pocs) { | |
182 | prev = 0; | |
183 | for (i = 0; i < rps->num_negative_pics; i++) { | |
184 | delta_poc = get_ue_golomb_long(gb) + 1; | |
185 | prev -= delta_poc; | |
186 | rps->delta_poc[i] = prev; | |
187 | rps->used[i] = get_bits1(gb); | |
188 | } | |
189 | prev = 0; | |
190 | for (i = 0; i < nb_positive_pics; i++) { | |
191 | delta_poc = get_ue_golomb_long(gb) + 1; | |
192 | prev += delta_poc; | |
193 | rps->delta_poc[rps->num_negative_pics + i] = prev; | |
194 | rps->used[rps->num_negative_pics + i] = get_bits1(gb); | |
195 | } | |
196 | } | |
197 | } | |
198 | return 0; | |
199 | } | |
200 | ||
201 | ||
202 | static int decode_profile_tier_level(HEVCContext *s, PTLCommon *ptl) | |
203 | { | |
204 | int i; | |
205 | HEVCLocalContext *lc = s->HEVClc; | |
206 | GetBitContext *gb = &lc->gb; | |
207 | ||
208 | if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 16 + 16 + 12) | |
209 | return -1; | |
210 | ||
211 | ptl->profile_space = get_bits(gb, 2); | |
212 | ptl->tier_flag = get_bits1(gb); | |
213 | ptl->profile_idc = get_bits(gb, 5); | |
214 | if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN) | |
215 | av_log(s->avctx, AV_LOG_DEBUG, "Main profile bitstream\n"); | |
216 | else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10) | |
217 | av_log(s->avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n"); | |
218 | else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_STILL_PICTURE) | |
219 | av_log(s->avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n"); | |
220 | else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT) | |
221 | av_log(s->avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n"); | |
222 | else | |
223 | av_log(s->avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc); | |
224 | ||
225 | for (i = 0; i < 32; i++) | |
226 | ptl->profile_compatibility_flag[i] = get_bits1(gb); | |
227 | ptl->progressive_source_flag = get_bits1(gb); | |
228 | ptl->interlaced_source_flag = get_bits1(gb); | |
229 | ptl->non_packed_constraint_flag = get_bits1(gb); | |
230 | ptl->frame_only_constraint_flag = get_bits1(gb); | |
231 | ||
232 | skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15] | |
233 | skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31] | |
234 | skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43] | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
239 | static int parse_ptl(HEVCContext *s, PTL *ptl, int max_num_sub_layers) | |
240 | { | |
241 | int i; | |
242 | HEVCLocalContext *lc = s->HEVClc; | |
243 | GetBitContext *gb = &lc->gb; | |
244 | if (decode_profile_tier_level(s, &ptl->general_ptl) < 0 || | |
245 | get_bits_left(gb) < 8 + 8*2) { | |
246 | av_log(s->avctx, AV_LOG_ERROR, "PTL information too short\n"); | |
247 | return -1; | |
248 | } | |
249 | ||
250 | ptl->general_ptl.level_idc = get_bits(gb, 8); | |
251 | ||
252 | for (i = 0; i < max_num_sub_layers - 1; i++) { | |
253 | ptl->sub_layer_profile_present_flag[i] = get_bits1(gb); | |
254 | ptl->sub_layer_level_present_flag[i] = get_bits1(gb); | |
255 | } | |
256 | ||
257 | if (max_num_sub_layers - 1> 0) | |
258 | for (i = max_num_sub_layers - 1; i < 8; i++) | |
259 | skip_bits(gb, 2); // reserved_zero_2bits[i] | |
260 | for (i = 0; i < max_num_sub_layers - 1; i++) { | |
261 | if (ptl->sub_layer_profile_present_flag[i] && | |
262 | decode_profile_tier_level(s, &ptl->sub_layer_ptl[i]) < 0) { | |
263 | av_log(s->avctx, AV_LOG_ERROR, | |
264 | "PTL information for sublayer %i too short\n", i); | |
265 | return -1; | |
266 | } | |
267 | if (ptl->sub_layer_level_present_flag[i]) { | |
268 | if (get_bits_left(gb) < 8) { | |
269 | av_log(s->avctx, AV_LOG_ERROR, | |
270 | "Not enough data for sublayer %i level_idc\n", i); | |
271 | return -1; | |
272 | } else | |
273 | ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8); | |
274 | } | |
275 | } | |
276 | ||
277 | return 0; | |
278 | } | |
279 | ||
280 | static void decode_sublayer_hrd(HEVCContext *s, unsigned int nb_cpb, | |
281 | int subpic_params_present) | |
282 | { | |
283 | GetBitContext *gb = &s->HEVClc->gb; | |
284 | int i; | |
285 | ||
286 | for (i = 0; i < nb_cpb; i++) { | |
287 | get_ue_golomb_long(gb); // bit_rate_value_minus1 | |
288 | get_ue_golomb_long(gb); // cpb_size_value_minus1 | |
289 | ||
290 | if (subpic_params_present) { | |
291 | get_ue_golomb_long(gb); // cpb_size_du_value_minus1 | |
292 | get_ue_golomb_long(gb); // bit_rate_du_value_minus1 | |
293 | } | |
294 | skip_bits1(gb); // cbr_flag | |
295 | } | |
296 | } | |
297 | ||
298 | static int decode_hrd(HEVCContext *s, int common_inf_present, | |
299 | int max_sublayers) | |
300 | { | |
301 | GetBitContext *gb = &s->HEVClc->gb; | |
302 | int nal_params_present = 0, vcl_params_present = 0; | |
303 | int subpic_params_present = 0; | |
304 | int i; | |
305 | ||
306 | if (common_inf_present) { | |
307 | nal_params_present = get_bits1(gb); | |
308 | vcl_params_present = get_bits1(gb); | |
309 | ||
310 | if (nal_params_present || vcl_params_present) { | |
311 | subpic_params_present = get_bits1(gb); | |
312 | ||
313 | if (subpic_params_present) { | |
314 | skip_bits(gb, 8); // tick_divisor_minus2 | |
315 | skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1 | |
316 | skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag | |
317 | skip_bits(gb, 5); // dpb_output_delay_du_length_minus1 | |
318 | } | |
319 | ||
320 | skip_bits(gb, 4); // bit_rate_scale | |
321 | skip_bits(gb, 4); // cpb_size_scale | |
322 | ||
323 | if (subpic_params_present) | |
324 | skip_bits(gb, 4); // cpb_size_du_scale | |
325 | ||
326 | skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1 | |
327 | skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1 | |
328 | skip_bits(gb, 5); // dpb_output_delay_length_minus1 | |
329 | } | |
330 | } | |
331 | ||
332 | for (i = 0; i < max_sublayers; i++) { | |
333 | int low_delay = 0; | |
334 | unsigned int nb_cpb = 1; | |
335 | int fixed_rate = get_bits1(gb); | |
336 | ||
337 | if (!fixed_rate) | |
338 | fixed_rate = get_bits1(gb); | |
339 | ||
340 | if (fixed_rate) | |
341 | get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1 | |
342 | else | |
343 | low_delay = get_bits1(gb); | |
344 | ||
345 | if (!low_delay) { | |
346 | nb_cpb = get_ue_golomb_long(gb) + 1; | |
347 | if (nb_cpb < 1 || nb_cpb > 32) { | |
348 | av_log(s->avctx, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb); | |
349 | return AVERROR_INVALIDDATA; | |
350 | } | |
351 | } | |
352 | ||
353 | if (nal_params_present) | |
354 | decode_sublayer_hrd(s, nb_cpb, subpic_params_present); | |
355 | if (vcl_params_present) | |
356 | decode_sublayer_hrd(s, nb_cpb, subpic_params_present); | |
357 | } | |
358 | return 0; | |
359 | } | |
360 | ||
361 | int ff_hevc_decode_nal_vps(HEVCContext *s) | |
362 | { | |
363 | int i,j; | |
364 | GetBitContext *gb = &s->HEVClc->gb; | |
365 | int vps_id = 0; | |
366 | HEVCVPS *vps; | |
367 | AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps)); | |
368 | ||
369 | if (!vps_buf) | |
370 | return AVERROR(ENOMEM); | |
371 | vps = (HEVCVPS*)vps_buf->data; | |
372 | ||
373 | av_log(s->avctx, AV_LOG_DEBUG, "Decoding VPS\n"); | |
374 | ||
375 | vps_id = get_bits(gb, 4); | |
376 | if (vps_id >= MAX_VPS_COUNT) { | |
377 | av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id); | |
378 | goto err; | |
379 | } | |
380 | ||
381 | if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits | |
382 | av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n"); | |
383 | goto err; | |
384 | } | |
385 | ||
386 | vps->vps_max_layers = get_bits(gb, 6) + 1; | |
387 | vps->vps_max_sub_layers = get_bits(gb, 3) + 1; | |
388 | vps->vps_temporal_id_nesting_flag = get_bits1(gb); | |
389 | ||
390 | if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits | |
391 | av_log(s->avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n"); | |
392 | goto err; | |
393 | } | |
394 | ||
395 | if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) { | |
396 | av_log(s->avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n", | |
397 | vps->vps_max_sub_layers); | |
398 | goto err; | |
399 | } | |
400 | ||
401 | if (parse_ptl(s, &vps->ptl, vps->vps_max_sub_layers) < 0) | |
402 | goto err; | |
403 | ||
404 | vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb); | |
405 | ||
406 | i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1; | |
407 | for (; i < vps->vps_max_sub_layers; i++) { | |
408 | vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1; | |
409 | vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb); | |
410 | vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1; | |
411 | ||
412 | if (vps->vps_max_dec_pic_buffering[i] > MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) { | |
413 | av_log(s->avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n", | |
414 | vps->vps_max_dec_pic_buffering[i] - 1); | |
415 | goto err; | |
416 | } | |
417 | if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) { | |
418 | av_log(s->avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n", | |
419 | vps->vps_num_reorder_pics[i]); | |
420 | if (s->avctx->err_recognition & AV_EF_EXPLODE) | |
421 | goto err; | |
422 | } | |
423 | } | |
424 | ||
425 | vps->vps_max_layer_id = get_bits(gb, 6); | |
426 | vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1; | |
427 | if ((vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) { | |
428 | av_log(s->avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n"); | |
429 | goto err; | |
430 | } | |
431 | ||
432 | for (i = 1; i < vps->vps_num_layer_sets; i++) | |
433 | for (j = 0; j <= vps->vps_max_layer_id; j++) | |
434 | skip_bits(gb, 1); // layer_id_included_flag[i][j] | |
435 | ||
436 | vps->vps_timing_info_present_flag = get_bits1(gb); | |
437 | if (vps->vps_timing_info_present_flag) { | |
438 | vps->vps_num_units_in_tick = get_bits_long(gb, 32); | |
439 | vps->vps_time_scale = get_bits_long(gb, 32); | |
440 | vps->vps_poc_proportional_to_timing_flag = get_bits1(gb); | |
441 | if (vps->vps_poc_proportional_to_timing_flag) | |
442 | vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1; | |
443 | vps->vps_num_hrd_parameters = get_ue_golomb_long(gb); | |
444 | for (i = 0; i < vps->vps_num_hrd_parameters; i++) { | |
445 | int common_inf_present = 1; | |
446 | ||
447 | get_ue_golomb_long(gb); // hrd_layer_set_idx | |
448 | if (i) | |
449 | common_inf_present = get_bits1(gb); | |
450 | decode_hrd(s, common_inf_present, vps->vps_max_sub_layers); | |
451 | } | |
452 | } | |
453 | get_bits1(gb); /* vps_extension_flag */ | |
454 | ||
455 | if (get_bits_left(gb) < 0) { | |
456 | av_log(s->avctx, AV_LOG_ERROR, | |
457 | "Overread VPS by %d bits\n", -get_bits_left(gb)); | |
458 | goto err; | |
459 | } | |
460 | ||
461 | av_buffer_unref(&s->vps_list[vps_id]); | |
462 | s->vps_list[vps_id] = vps_buf; | |
463 | return 0; | |
464 | ||
465 | err: | |
466 | av_buffer_unref(&vps_buf); | |
467 | return AVERROR_INVALIDDATA; | |
468 | } | |
469 | ||
470 | static void decode_vui(HEVCContext *s, HEVCSPS *sps) | |
471 | { | |
472 | VUI *vui = &sps->vui; | |
473 | GetBitContext *gb = &s->HEVClc->gb; | |
474 | GetBitContext backup; | |
475 | int sar_present, alt = 0; | |
476 | ||
477 | av_log(s->avctx, AV_LOG_DEBUG, "Decoding VUI\n"); | |
478 | ||
479 | sar_present = get_bits1(gb); | |
480 | if (sar_present) { | |
481 | uint8_t sar_idx = get_bits(gb, 8); | |
482 | if (sar_idx < FF_ARRAY_ELEMS(vui_sar)) | |
483 | vui->sar = vui_sar[sar_idx]; | |
484 | else if (sar_idx == 255) { | |
485 | vui->sar.num = get_bits(gb, 16); | |
486 | vui->sar.den = get_bits(gb, 16); | |
487 | } else | |
488 | av_log(s->avctx, AV_LOG_WARNING, | |
489 | "Unknown SAR index: %u.\n", sar_idx); | |
490 | } | |
491 | ||
492 | vui->overscan_info_present_flag = get_bits1(gb); | |
493 | if (vui->overscan_info_present_flag) | |
494 | vui->overscan_appropriate_flag = get_bits1(gb); | |
495 | ||
496 | vui->video_signal_type_present_flag = get_bits1(gb); | |
497 | if (vui->video_signal_type_present_flag) { | |
498 | vui->video_format = get_bits(gb, 3); | |
499 | vui->video_full_range_flag = get_bits1(gb); | |
500 | vui->colour_description_present_flag = get_bits1(gb); | |
501 | if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P) | |
502 | sps->pix_fmt = AV_PIX_FMT_YUVJ420P; | |
503 | if (vui->colour_description_present_flag) { | |
504 | vui->colour_primaries = get_bits(gb, 8); | |
505 | vui->transfer_characteristic = get_bits(gb, 8); | |
506 | vui->matrix_coeffs = get_bits(gb, 8); | |
507 | ||
508 | // Set invalid values to "unspecified" | |
509 | if (vui->colour_primaries >= AVCOL_PRI_NB) | |
510 | vui->colour_primaries = AVCOL_PRI_UNSPECIFIED; | |
511 | if (vui->transfer_characteristic >= AVCOL_TRC_NB) | |
512 | vui->transfer_characteristic = AVCOL_TRC_UNSPECIFIED; | |
513 | if (vui->matrix_coeffs >= AVCOL_SPC_NB) | |
514 | vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED; | |
515 | } | |
516 | } | |
517 | ||
518 | vui->chroma_loc_info_present_flag = get_bits1(gb); | |
519 | if (vui->chroma_loc_info_present_flag) { | |
520 | vui->chroma_sample_loc_type_top_field = get_ue_golomb_long(gb); | |
521 | vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb); | |
522 | } | |
523 | ||
524 | vui->neutra_chroma_indication_flag = get_bits1(gb); | |
525 | vui->field_seq_flag = get_bits1(gb); | |
526 | vui->frame_field_info_present_flag = get_bits1(gb); | |
527 | ||
528 | if (get_bits_left(gb) >= 68 && show_bits_long(gb, 21) == 0x100000) { | |
529 | vui->default_display_window_flag = 0; | |
530 | av_log(s->avctx, AV_LOG_WARNING, "Invalid default display window\n"); | |
531 | } else | |
532 | vui->default_display_window_flag = get_bits1(gb); | |
533 | // Backup context in case an alternate header is detected | |
534 | memcpy(&backup, gb, sizeof(backup)); | |
535 | ||
536 | if (vui->default_display_window_flag) { | |
537 | //TODO: * 2 is only valid for 420 | |
538 | vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * 2; | |
539 | vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * 2; | |
540 | vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * 2; | |
541 | vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * 2; | |
542 | ||
543 | if (s->apply_defdispwin && | |
544 | s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) { | |
545 | av_log(s->avctx, AV_LOG_DEBUG, | |
546 | "discarding vui default display window, " | |
547 | "original values are l:%u r:%u t:%u b:%u\n", | |
548 | vui->def_disp_win.left_offset, | |
549 | vui->def_disp_win.right_offset, | |
550 | vui->def_disp_win.top_offset, | |
551 | vui->def_disp_win.bottom_offset); | |
552 | ||
553 | vui->def_disp_win.left_offset = | |
554 | vui->def_disp_win.right_offset = | |
555 | vui->def_disp_win.top_offset = | |
556 | vui->def_disp_win.bottom_offset = 0; | |
557 | } | |
558 | } | |
559 | ||
560 | vui->vui_timing_info_present_flag = get_bits1(gb); | |
561 | ||
562 | if (vui->vui_timing_info_present_flag) { | |
563 | if( get_bits_left(gb) < 66) { | |
564 | // The alternate syntax seem to have timing info located | |
565 | // at where def_disp_win is normally located | |
566 | av_log(s->avctx, AV_LOG_WARNING, | |
567 | "Strange VUI timing information, retrying...\n"); | |
568 | vui->default_display_window_flag = 0; | |
569 | memset(&vui->def_disp_win, 0, sizeof(vui->def_disp_win)); | |
570 | memcpy(gb, &backup, sizeof(backup)); | |
571 | alt = 1; | |
572 | } | |
573 | vui->vui_num_units_in_tick = get_bits_long(gb, 32); | |
574 | vui->vui_time_scale = get_bits_long(gb, 32); | |
575 | if (alt) { | |
576 | av_log(s->avctx, AV_LOG_INFO, "Retry got %i/%i fps\n", | |
577 | vui->vui_time_scale, vui->vui_num_units_in_tick); | |
578 | } | |
579 | vui->vui_poc_proportional_to_timing_flag = get_bits1(gb); | |
580 | if (vui->vui_poc_proportional_to_timing_flag) | |
581 | vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb); | |
582 | vui->vui_hrd_parameters_present_flag = get_bits1(gb); | |
583 | if (vui->vui_hrd_parameters_present_flag) | |
584 | decode_hrd(s, 1, sps->max_sub_layers); | |
585 | } | |
586 | ||
587 | vui->bitstream_restriction_flag = get_bits1(gb); | |
588 | if (vui->bitstream_restriction_flag) { | |
589 | vui->tiles_fixed_structure_flag = get_bits1(gb); | |
590 | vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb); | |
591 | vui->restricted_ref_pic_lists_flag = get_bits1(gb); | |
592 | vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb); | |
593 | vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb); | |
594 | vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb); | |
595 | vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb); | |
596 | vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb); | |
597 | } | |
598 | } | |
599 | ||
600 | static void set_default_scaling_list_data(ScalingList *sl) | |
601 | { | |
602 | int matrixId; | |
603 | ||
604 | for (matrixId = 0; matrixId < 6; matrixId++) { | |
605 | // 4x4 default is 16 | |
606 | memset(sl->sl[0][matrixId], 16, 16); | |
607 | sl->sl_dc[0][matrixId] = 16; // default for 16x16 | |
608 | sl->sl_dc[1][matrixId] = 16; // default for 32x32 | |
609 | } | |
610 | memcpy(sl->sl[1][0], default_scaling_list_intra, 64); | |
611 | memcpy(sl->sl[1][1], default_scaling_list_intra, 64); | |
612 | memcpy(sl->sl[1][2], default_scaling_list_intra, 64); | |
613 | memcpy(sl->sl[1][3], default_scaling_list_inter, 64); | |
614 | memcpy(sl->sl[1][4], default_scaling_list_inter, 64); | |
615 | memcpy(sl->sl[1][5], default_scaling_list_inter, 64); | |
616 | memcpy(sl->sl[2][0], default_scaling_list_intra, 64); | |
617 | memcpy(sl->sl[2][1], default_scaling_list_intra, 64); | |
618 | memcpy(sl->sl[2][2], default_scaling_list_intra, 64); | |
619 | memcpy(sl->sl[2][3], default_scaling_list_inter, 64); | |
620 | memcpy(sl->sl[2][4], default_scaling_list_inter, 64); | |
621 | memcpy(sl->sl[2][5], default_scaling_list_inter, 64); | |
622 | memcpy(sl->sl[3][0], default_scaling_list_intra, 64); | |
623 | memcpy(sl->sl[3][1], default_scaling_list_intra, 64); | |
624 | memcpy(sl->sl[3][2], default_scaling_list_intra, 64); | |
625 | memcpy(sl->sl[3][3], default_scaling_list_inter, 64); | |
626 | memcpy(sl->sl[3][4], default_scaling_list_inter, 64); | |
627 | memcpy(sl->sl[3][5], default_scaling_list_inter, 64); | |
628 | } | |
629 | ||
630 | static int scaling_list_data(HEVCContext *s, ScalingList *sl, HEVCSPS *sps) | |
631 | { | |
632 | GetBitContext *gb = &s->HEVClc->gb; | |
633 | uint8_t scaling_list_pred_mode_flag; | |
634 | int32_t scaling_list_dc_coef[2][6]; | |
635 | int size_id, matrix_id, pos; | |
636 | int i; | |
637 | ||
638 | for (size_id = 0; size_id < 4; size_id++) | |
639 | for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) { | |
640 | scaling_list_pred_mode_flag = get_bits1(gb); | |
641 | if (!scaling_list_pred_mode_flag) { | |
642 | unsigned int delta = get_ue_golomb_long(gb); | |
643 | /* Only need to handle non-zero delta. Zero means default, | |
644 | * which should already be in the arrays. */ | |
645 | if (delta) { | |
646 | // Copy from previous array. | |
647 | if (matrix_id < delta) { | |
648 | av_log(s->avctx, AV_LOG_ERROR, | |
649 | "Invalid delta in scaling list data: %d.\n", delta); | |
650 | return AVERROR_INVALIDDATA; | |
651 | } | |
652 | ||
653 | memcpy(sl->sl[size_id][matrix_id], | |
654 | sl->sl[size_id][matrix_id - delta], | |
655 | size_id > 0 ? 64 : 16); | |
656 | if (size_id > 1) | |
657 | sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta]; | |
658 | } | |
659 | } else { | |
660 | int next_coef, coef_num; | |
661 | int32_t scaling_list_delta_coef; | |
662 | ||
663 | next_coef = 8; | |
664 | coef_num = FFMIN(64, 1 << (4 + (size_id << 1))); | |
665 | if (size_id > 1) { | |
666 | scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8; | |
667 | next_coef = scaling_list_dc_coef[size_id - 2][matrix_id]; | |
668 | sl->sl_dc[size_id - 2][matrix_id] = next_coef; | |
669 | } | |
670 | for (i = 0; i < coef_num; i++) { | |
671 | if (size_id == 0) | |
672 | pos = 4 * ff_hevc_diag_scan4x4_y[i] + | |
673 | ff_hevc_diag_scan4x4_x[i]; | |
674 | else | |
675 | pos = 8 * ff_hevc_diag_scan8x8_y[i] + | |
676 | ff_hevc_diag_scan8x8_x[i]; | |
677 | ||
678 | scaling_list_delta_coef = get_se_golomb(gb); | |
679 | next_coef = (next_coef + scaling_list_delta_coef + 256) % 256; | |
680 | sl->sl[size_id][matrix_id][pos] = next_coef; | |
681 | } | |
682 | } | |
683 | } | |
684 | ||
685 | if (sps->chroma_format_idc == 3) { | |
686 | for (i = 0; i < 64; i++) { | |
687 | sl->sl[3][1][i] = sl->sl[2][1][i]; | |
688 | sl->sl[3][2][i] = sl->sl[2][2][i]; | |
689 | sl->sl[3][4][i] = sl->sl[2][4][i]; | |
690 | sl->sl[3][5][i] = sl->sl[2][5][i]; | |
691 | } | |
692 | sl->sl_dc[1][1] = sl->sl_dc[0][1]; | |
693 | sl->sl_dc[1][2] = sl->sl_dc[0][2]; | |
694 | sl->sl_dc[1][4] = sl->sl_dc[0][4]; | |
695 | sl->sl_dc[1][5] = sl->sl_dc[0][5]; | |
696 | } | |
697 | ||
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
702 | int ff_hevc_decode_nal_sps(HEVCContext *s) | |
703 | { | |
704 | const AVPixFmtDescriptor *desc; | |
705 | GetBitContext *gb = &s->HEVClc->gb; | |
706 | int ret = 0; | |
707 | unsigned int sps_id = 0; | |
708 | int log2_diff_max_min_transform_block_size; | |
709 | int bit_depth_chroma, start, vui_present, sublayer_ordering_info; | |
710 | int i; | |
711 | ||
712 | HEVCSPS *sps; | |
713 | AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps)); | |
714 | ||
715 | if (!sps_buf) | |
716 | return AVERROR(ENOMEM); | |
717 | sps = (HEVCSPS*)sps_buf->data; | |
718 | ||
719 | av_log(s->avctx, AV_LOG_DEBUG, "Decoding SPS\n"); | |
720 | ||
721 | // Coded parameters | |
722 | ||
723 | sps->vps_id = get_bits(gb, 4); | |
724 | if (sps->vps_id >= MAX_VPS_COUNT) { | |
725 | av_log(s->avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id); | |
726 | ret = AVERROR_INVALIDDATA; | |
727 | goto err; | |
728 | } | |
729 | ||
730 | if (!s->vps_list[sps->vps_id]) { | |
731 | av_log(s->avctx, AV_LOG_ERROR, "VPS %d does not exist\n", | |
732 | sps->vps_id); | |
733 | ret = AVERROR_INVALIDDATA; | |
734 | goto err; | |
735 | } | |
736 | ||
737 | sps->max_sub_layers = get_bits(gb, 3) + 1; | |
738 | if (sps->max_sub_layers > MAX_SUB_LAYERS) { | |
739 | av_log(s->avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n", | |
740 | sps->max_sub_layers); | |
741 | ret = AVERROR_INVALIDDATA; | |
742 | goto err; | |
743 | } | |
744 | ||
745 | skip_bits1(gb); // temporal_id_nesting_flag | |
746 | ||
747 | if (parse_ptl(s, &sps->ptl, sps->max_sub_layers) < 0) | |
748 | goto err; | |
749 | ||
750 | sps_id = get_ue_golomb_long(gb); | |
751 | if (sps_id >= MAX_SPS_COUNT) { | |
752 | av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", sps_id); | |
753 | ret = AVERROR_INVALIDDATA; | |
754 | goto err; | |
755 | } | |
756 | ||
757 | sps->chroma_format_idc = get_ue_golomb_long(gb); | |
758 | if (!(sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2 || sps->chroma_format_idc == 3)) { | |
759 | avpriv_report_missing_feature(s->avctx, "chroma_format_idc != {1, 2, 3}\n"); | |
760 | ret = AVERROR_PATCHWELCOME; | |
761 | goto err; | |
762 | } | |
763 | ||
764 | if (sps->chroma_format_idc == 3) | |
765 | sps->separate_colour_plane_flag = get_bits1(gb); | |
766 | ||
767 | if (sps->separate_colour_plane_flag) | |
768 | sps->chroma_format_idc = 0; | |
769 | ||
770 | sps->width = get_ue_golomb_long(gb); | |
771 | sps->height = get_ue_golomb_long(gb); | |
772 | if ((ret = av_image_check_size(sps->width, | |
773 | sps->height, 0, s->avctx)) < 0) | |
774 | goto err; | |
775 | ||
776 | if (get_bits1(gb)) { // pic_conformance_flag | |
777 | //TODO: * 2 is only valid for 420 | |
778 | sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * 2; | |
779 | sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * 2; | |
780 | sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * 2; | |
781 | sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * 2; | |
782 | ||
783 | if (s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) { | |
784 | av_log(s->avctx, AV_LOG_DEBUG, | |
785 | "discarding sps conformance window, " | |
786 | "original values are l:%u r:%u t:%u b:%u\n", | |
787 | sps->pic_conf_win.left_offset, | |
788 | sps->pic_conf_win.right_offset, | |
789 | sps->pic_conf_win.top_offset, | |
790 | sps->pic_conf_win.bottom_offset); | |
791 | ||
792 | sps->pic_conf_win.left_offset = | |
793 | sps->pic_conf_win.right_offset = | |
794 | sps->pic_conf_win.top_offset = | |
795 | sps->pic_conf_win.bottom_offset = 0; | |
796 | } | |
797 | sps->output_window = sps->pic_conf_win; | |
798 | } | |
799 | ||
800 | sps->bit_depth = get_ue_golomb_long(gb) + 8; | |
801 | bit_depth_chroma = get_ue_golomb_long(gb) + 8; | |
802 | if (bit_depth_chroma != sps->bit_depth) { | |
803 | av_log(s->avctx, AV_LOG_ERROR, | |
804 | "Luma bit depth (%d) is different from chroma bit depth (%d), " | |
805 | "this is unsupported.\n", | |
806 | sps->bit_depth, bit_depth_chroma); | |
807 | ret = AVERROR_INVALIDDATA; | |
808 | goto err; | |
809 | } | |
810 | ||
811 | switch (sps->bit_depth) { | |
812 | case 8: | |
813 | if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P; | |
814 | if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P; | |
815 | if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P; | |
816 | break; | |
817 | case 9: | |
818 | if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9; | |
819 | if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9; | |
820 | if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9; | |
821 | break; | |
822 | case 10: | |
823 | if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10; | |
824 | if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10; | |
825 | if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10; | |
826 | break; | |
827 | case 12: | |
828 | if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12; | |
829 | if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12; | |
830 | if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12; | |
831 | break; | |
832 | default: | |
833 | av_log(s->avctx, AV_LOG_ERROR, | |
834 | "4:2:0, 4:2:2, 4:4:4 supports are currently specified for 8, 10 and 12 bits.\n"); | |
835 | ret = AVERROR_PATCHWELCOME; | |
836 | goto err; | |
837 | } | |
838 | ||
839 | desc = av_pix_fmt_desc_get(sps->pix_fmt); | |
840 | if (!desc) { | |
841 | ret = AVERROR(EINVAL); | |
842 | goto err; | |
843 | } | |
844 | ||
845 | sps->hshift[0] = sps->vshift[0] = 0; | |
846 | sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w; | |
847 | sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h; | |
848 | ||
849 | sps->pixel_shift = sps->bit_depth > 8; | |
850 | ||
851 | sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4; | |
852 | if (sps->log2_max_poc_lsb > 16) { | |
853 | av_log(s->avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n", | |
854 | sps->log2_max_poc_lsb - 4); | |
855 | ret = AVERROR_INVALIDDATA; | |
856 | goto err; | |
857 | } | |
858 | ||
859 | sublayer_ordering_info = get_bits1(gb); | |
860 | start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1; | |
861 | for (i = start; i < sps->max_sub_layers; i++) { | |
862 | sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1; | |
863 | sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb); | |
864 | sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1; | |
865 | if (sps->temporal_layer[i].max_dec_pic_buffering > MAX_DPB_SIZE) { | |
866 | av_log(s->avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n", | |
867 | sps->temporal_layer[i].max_dec_pic_buffering - 1); | |
868 | ret = AVERROR_INVALIDDATA; | |
869 | goto err; | |
870 | } | |
871 | if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) { | |
872 | av_log(s->avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n", | |
873 | sps->temporal_layer[i].num_reorder_pics); | |
874 | if (s->avctx->err_recognition & AV_EF_EXPLODE || | |
875 | sps->temporal_layer[i].num_reorder_pics > MAX_DPB_SIZE - 1) { | |
876 | ret = AVERROR_INVALIDDATA; | |
877 | goto err; | |
878 | } | |
879 | sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1; | |
880 | } | |
881 | } | |
882 | ||
883 | if (!sublayer_ordering_info) { | |
884 | for (i = 0; i < start; i++) { | |
885 | sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering; | |
886 | sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics; | |
887 | sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase; | |
888 | } | |
889 | } | |
890 | ||
891 | sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3; | |
892 | sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb); | |
893 | sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2; | |
894 | log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb); | |
895 | sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size + | |
896 | sps->log2_min_tb_size; | |
897 | ||
898 | if (sps->log2_min_tb_size >= sps->log2_min_cb_size) { | |
899 | av_log(s->avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size"); | |
900 | ret = AVERROR_INVALIDDATA; | |
901 | goto err; | |
902 | } | |
903 | sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb); | |
904 | sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb); | |
905 | ||
906 | sps->scaling_list_enable_flag = get_bits1(gb); | |
907 | if (sps->scaling_list_enable_flag) { | |
908 | set_default_scaling_list_data(&sps->scaling_list); | |
909 | ||
910 | if (get_bits1(gb)) { | |
911 | ret = scaling_list_data(s, &sps->scaling_list, sps); | |
912 | if (ret < 0) | |
913 | goto err; | |
914 | } | |
915 | } | |
916 | ||
917 | sps->amp_enabled_flag = get_bits1(gb); | |
918 | sps->sao_enabled = get_bits1(gb); | |
919 | ||
920 | sps->pcm_enabled_flag = get_bits1(gb); | |
921 | if (sps->pcm_enabled_flag) { | |
922 | sps->pcm.bit_depth = get_bits(gb, 4) + 1; | |
923 | sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1; | |
924 | sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3; | |
925 | sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size + | |
926 | get_ue_golomb_long(gb); | |
927 | if (sps->pcm.bit_depth > sps->bit_depth) { | |
928 | av_log(s->avctx, AV_LOG_ERROR, | |
929 | "PCM bit depth (%d) is greater than normal bit depth (%d)\n", | |
930 | sps->pcm.bit_depth, sps->bit_depth); | |
931 | ret = AVERROR_INVALIDDATA; | |
932 | goto err; | |
933 | } | |
934 | ||
935 | sps->pcm.loop_filter_disable_flag = get_bits1(gb); | |
936 | } | |
937 | ||
938 | sps->nb_st_rps = get_ue_golomb_long(gb); | |
939 | if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) { | |
940 | av_log(s->avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n", | |
941 | sps->nb_st_rps); | |
942 | ret = AVERROR_INVALIDDATA; | |
943 | goto err; | |
944 | } | |
945 | for (i = 0; i < sps->nb_st_rps; i++) { | |
946 | if ((ret = ff_hevc_decode_short_term_rps(s, &sps->st_rps[i], | |
947 | sps, 0)) < 0) | |
948 | goto err; | |
949 | } | |
950 | ||
951 | sps->long_term_ref_pics_present_flag = get_bits1(gb); | |
952 | if (sps->long_term_ref_pics_present_flag) { | |
953 | sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb); | |
f6fa7814 DM |
954 | if (sps->num_long_term_ref_pics_sps > 31U) { |
955 | av_log(0, AV_LOG_ERROR, "num_long_term_ref_pics_sps %d is out of range.\n", | |
956 | sps->num_long_term_ref_pics_sps); | |
957 | goto err; | |
958 | } | |
2ba45a60 DM |
959 | for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) { |
960 | sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb); | |
961 | sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb); | |
962 | } | |
963 | } | |
964 | ||
965 | sps->sps_temporal_mvp_enabled_flag = get_bits1(gb); | |
966 | sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb); | |
967 | sps->vui.sar = (AVRational){0, 1}; | |
968 | vui_present = get_bits1(gb); | |
969 | if (vui_present) | |
970 | decode_vui(s, sps); | |
971 | ||
972 | if (get_bits1(gb)) { // sps_extension_flag | |
973 | int sps_extension_flag[1]; | |
974 | for (i = 0; i < 1; i++) | |
975 | sps_extension_flag[i] = get_bits1(gb); | |
976 | skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7); | |
977 | if (sps_extension_flag[0]) { | |
978 | int extended_precision_processing_flag; | |
979 | int high_precision_offsets_enabled_flag; | |
980 | int cabac_bypass_alignment_enabled_flag; | |
981 | ||
982 | sps->transform_skip_rotation_enabled_flag = get_bits1(gb); | |
983 | sps->transform_skip_context_enabled_flag = get_bits1(gb); | |
984 | sps->implicit_rdpcm_enabled_flag = get_bits1(gb); | |
985 | ||
986 | sps->explicit_rdpcm_enabled_flag = get_bits1(gb); | |
987 | ||
988 | extended_precision_processing_flag = get_bits1(gb); | |
989 | if (extended_precision_processing_flag) | |
990 | av_log(s->avctx, AV_LOG_WARNING, | |
991 | "extended_precision_processing_flag not yet implemented\n"); | |
992 | ||
993 | sps->intra_smoothing_disabled_flag = get_bits1(gb); | |
994 | high_precision_offsets_enabled_flag = get_bits1(gb); | |
995 | if (high_precision_offsets_enabled_flag) | |
996 | av_log(s->avctx, AV_LOG_WARNING, | |
997 | "high_precision_offsets_enabled_flag not yet implemented\n"); | |
998 | ||
999 | sps->persistent_rice_adaptation_enabled_flag = get_bits1(gb); | |
1000 | ||
1001 | cabac_bypass_alignment_enabled_flag = get_bits1(gb); | |
1002 | if (cabac_bypass_alignment_enabled_flag) | |
1003 | av_log(s->avctx, AV_LOG_WARNING, | |
1004 | "cabac_bypass_alignment_enabled_flag not yet implemented\n"); | |
1005 | } | |
1006 | } | |
1007 | if (s->apply_defdispwin) { | |
1008 | sps->output_window.left_offset += sps->vui.def_disp_win.left_offset; | |
1009 | sps->output_window.right_offset += sps->vui.def_disp_win.right_offset; | |
1010 | sps->output_window.top_offset += sps->vui.def_disp_win.top_offset; | |
1011 | sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset; | |
1012 | } | |
1013 | if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) && | |
1014 | !(s->avctx->flags & CODEC_FLAG_UNALIGNED)) { | |
1015 | sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift)); | |
1016 | av_log(s->avctx, AV_LOG_WARNING, "Reducing left output window to %d " | |
1017 | "chroma samples to preserve alignment.\n", | |
1018 | sps->output_window.left_offset); | |
1019 | } | |
1020 | sps->output_width = sps->width - | |
1021 | (sps->output_window.left_offset + sps->output_window.right_offset); | |
1022 | sps->output_height = sps->height - | |
1023 | (sps->output_window.top_offset + sps->output_window.bottom_offset); | |
1024 | if (sps->output_width <= 0 || sps->output_height <= 0) { | |
1025 | av_log(s->avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n", | |
1026 | sps->output_width, sps->output_height); | |
1027 | if (s->avctx->err_recognition & AV_EF_EXPLODE) { | |
1028 | ret = AVERROR_INVALIDDATA; | |
1029 | goto err; | |
1030 | } | |
1031 | av_log(s->avctx, AV_LOG_WARNING, | |
1032 | "Displaying the whole video surface.\n"); | |
f6fa7814 DM |
1033 | memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win)); |
1034 | memset(&sps->output_window, 0, sizeof(sps->output_window)); | |
2ba45a60 DM |
1035 | sps->output_width = sps->width; |
1036 | sps->output_height = sps->height; | |
1037 | } | |
1038 | ||
1039 | // Inferred parameters | |
1040 | sps->log2_ctb_size = sps->log2_min_cb_size + | |
1041 | sps->log2_diff_max_min_coding_block_size; | |
1042 | sps->log2_min_pu_size = sps->log2_min_cb_size - 1; | |
1043 | ||
1044 | sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size; | |
1045 | sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size; | |
1046 | sps->ctb_size = sps->ctb_width * sps->ctb_height; | |
1047 | ||
1048 | sps->min_cb_width = sps->width >> sps->log2_min_cb_size; | |
1049 | sps->min_cb_height = sps->height >> sps->log2_min_cb_size; | |
1050 | sps->min_tb_width = sps->width >> sps->log2_min_tb_size; | |
1051 | sps->min_tb_height = sps->height >> sps->log2_min_tb_size; | |
1052 | sps->min_pu_width = sps->width >> sps->log2_min_pu_size; | |
1053 | sps->min_pu_height = sps->height >> sps->log2_min_pu_size; | |
1054 | sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1; | |
1055 | ||
1056 | sps->qp_bd_offset = 6 * (sps->bit_depth - 8); | |
1057 | ||
1058 | if (sps->width & ((1 << sps->log2_min_cb_size) - 1) || | |
1059 | sps->height & ((1 << sps->log2_min_cb_size) - 1)) { | |
1060 | av_log(s->avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n"); | |
1061 | goto err; | |
1062 | } | |
1063 | ||
1064 | if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) { | |
1065 | av_log(s->avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size); | |
1066 | goto err; | |
1067 | } | |
1068 | if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) { | |
1069 | av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n", | |
1070 | sps->max_transform_hierarchy_depth_inter); | |
1071 | goto err; | |
1072 | } | |
1073 | if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) { | |
1074 | av_log(s->avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n", | |
1075 | sps->max_transform_hierarchy_depth_intra); | |
1076 | goto err; | |
1077 | } | |
1078 | if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) { | |
1079 | av_log(s->avctx, AV_LOG_ERROR, | |
1080 | "max transform block size out of range: %d\n", | |
1081 | sps->log2_max_trafo_size); | |
1082 | goto err; | |
1083 | } | |
1084 | ||
1085 | if (get_bits_left(gb) < 0) { | |
1086 | av_log(s->avctx, AV_LOG_ERROR, | |
1087 | "Overread SPS by %d bits\n", -get_bits_left(gb)); | |
1088 | goto err; | |
1089 | } | |
1090 | ||
1091 | if (s->avctx->debug & FF_DEBUG_BITSTREAM) { | |
1092 | av_log(s->avctx, AV_LOG_DEBUG, | |
1093 | "Parsed SPS: id %d; coded wxh: %dx%d; " | |
1094 | "cropped wxh: %dx%d; pix_fmt: %s.\n", | |
1095 | sps_id, sps->width, sps->height, | |
1096 | sps->output_width, sps->output_height, | |
1097 | av_get_pix_fmt_name(sps->pix_fmt)); | |
1098 | } | |
1099 | ||
1100 | /* check if this is a repeat of an already parsed SPS, then keep the | |
1101 | * original one. | |
1102 | * otherwise drop all PPSes that depend on it */ | |
1103 | if (s->sps_list[sps_id] && | |
1104 | !memcmp(s->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) { | |
1105 | av_buffer_unref(&sps_buf); | |
1106 | } else { | |
1107 | for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) { | |
1108 | if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == sps_id) | |
1109 | av_buffer_unref(&s->pps_list[i]); | |
1110 | } | |
1111 | if (s->sps_list[sps_id] && s->sps == (HEVCSPS*)s->sps_list[sps_id]->data) { | |
1112 | av_buffer_unref(&s->current_sps); | |
1113 | s->current_sps = av_buffer_ref(s->sps_list[sps_id]); | |
1114 | if (!s->current_sps) | |
1115 | s->sps = NULL; | |
1116 | } | |
1117 | av_buffer_unref(&s->sps_list[sps_id]); | |
1118 | s->sps_list[sps_id] = sps_buf; | |
1119 | } | |
1120 | ||
1121 | return 0; | |
1122 | ||
1123 | err: | |
1124 | av_buffer_unref(&sps_buf); | |
1125 | return ret; | |
1126 | } | |
1127 | ||
1128 | static void hevc_pps_free(void *opaque, uint8_t *data) | |
1129 | { | |
1130 | HEVCPPS *pps = (HEVCPPS*)data; | |
1131 | ||
1132 | av_freep(&pps->column_width); | |
1133 | av_freep(&pps->row_height); | |
1134 | av_freep(&pps->col_bd); | |
1135 | av_freep(&pps->row_bd); | |
1136 | av_freep(&pps->col_idxX); | |
1137 | av_freep(&pps->ctb_addr_rs_to_ts); | |
1138 | av_freep(&pps->ctb_addr_ts_to_rs); | |
1139 | av_freep(&pps->tile_pos_rs); | |
1140 | av_freep(&pps->tile_id); | |
1141 | av_freep(&pps->min_tb_addr_zs_tab); | |
1142 | ||
1143 | av_freep(&pps); | |
1144 | } | |
1145 | ||
1146 | static int pps_range_extensions(HEVCContext *s, HEVCPPS *pps, HEVCSPS *sps) { | |
1147 | GetBitContext *gb = &s->HEVClc->gb; | |
1148 | int i; | |
1149 | ||
1150 | if (pps->transform_skip_enabled_flag) { | |
1151 | pps->log2_max_transform_skip_block_size = get_ue_golomb_long(gb) + 2; | |
1152 | } | |
1153 | pps->cross_component_prediction_enabled_flag = get_bits1(gb); | |
1154 | pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb); | |
1155 | if (pps->chroma_qp_offset_list_enabled_flag) { | |
1156 | pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_long(gb); | |
1157 | pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_long(gb); | |
1158 | if (pps->chroma_qp_offset_list_len_minus1 && pps->chroma_qp_offset_list_len_minus1 >= 5) { | |
1159 | av_log(s->avctx, AV_LOG_ERROR, | |
1160 | "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n"); | |
1161 | return AVERROR_INVALIDDATA; | |
1162 | } | |
1163 | for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) { | |
1164 | pps->cb_qp_offset_list[i] = get_se_golomb_long(gb); | |
1165 | if (pps->cb_qp_offset_list[i]) { | |
1166 | av_log(s->avctx, AV_LOG_WARNING, | |
1167 | "cb_qp_offset_list not tested yet.\n"); | |
1168 | } | |
1169 | pps->cr_qp_offset_list[i] = get_se_golomb_long(gb); | |
1170 | if (pps->cr_qp_offset_list[i]) { | |
1171 | av_log(s->avctx, AV_LOG_WARNING, | |
1172 | "cb_qp_offset_list not tested yet.\n"); | |
1173 | } | |
1174 | } | |
1175 | } | |
1176 | pps->log2_sao_offset_scale_luma = get_ue_golomb_long(gb); | |
1177 | pps->log2_sao_offset_scale_chroma = get_ue_golomb_long(gb); | |
1178 | ||
1179 | return(0); | |
1180 | } | |
1181 | ||
1182 | int ff_hevc_decode_nal_pps(HEVCContext *s) | |
1183 | { | |
1184 | GetBitContext *gb = &s->HEVClc->gb; | |
1185 | HEVCSPS *sps = NULL; | |
1186 | int pic_area_in_ctbs; | |
1187 | int log2_diff_ctb_min_tb_size; | |
1188 | int i, j, x, y, ctb_addr_rs, tile_id; | |
1189 | int ret = 0; | |
1190 | unsigned int pps_id = 0; | |
1191 | ||
1192 | AVBufferRef *pps_buf; | |
1193 | HEVCPPS *pps = av_mallocz(sizeof(*pps)); | |
1194 | ||
1195 | if (!pps) | |
1196 | return AVERROR(ENOMEM); | |
1197 | ||
1198 | pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps), | |
1199 | hevc_pps_free, NULL, 0); | |
1200 | if (!pps_buf) { | |
1201 | av_freep(&pps); | |
1202 | return AVERROR(ENOMEM); | |
1203 | } | |
1204 | ||
1205 | av_log(s->avctx, AV_LOG_DEBUG, "Decoding PPS\n"); | |
1206 | ||
1207 | // Default values | |
1208 | pps->loop_filter_across_tiles_enabled_flag = 1; | |
1209 | pps->num_tile_columns = 1; | |
1210 | pps->num_tile_rows = 1; | |
1211 | pps->uniform_spacing_flag = 1; | |
1212 | pps->disable_dbf = 0; | |
1213 | pps->beta_offset = 0; | |
1214 | pps->tc_offset = 0; | |
1215 | pps->log2_max_transform_skip_block_size = 2; | |
1216 | ||
1217 | // Coded parameters | |
1218 | pps_id = get_ue_golomb_long(gb); | |
1219 | if (pps_id >= MAX_PPS_COUNT) { | |
1220 | av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id); | |
1221 | ret = AVERROR_INVALIDDATA; | |
1222 | goto err; | |
1223 | } | |
1224 | pps->sps_id = get_ue_golomb_long(gb); | |
1225 | if (pps->sps_id >= MAX_SPS_COUNT) { | |
1226 | av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id); | |
1227 | ret = AVERROR_INVALIDDATA; | |
1228 | goto err; | |
1229 | } | |
1230 | if (!s->sps_list[pps->sps_id]) { | |
1231 | av_log(s->avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id); | |
1232 | ret = AVERROR_INVALIDDATA; | |
1233 | goto err; | |
1234 | } | |
1235 | sps = (HEVCSPS *)s->sps_list[pps->sps_id]->data; | |
1236 | ||
1237 | pps->dependent_slice_segments_enabled_flag = get_bits1(gb); | |
1238 | pps->output_flag_present_flag = get_bits1(gb); | |
1239 | pps->num_extra_slice_header_bits = get_bits(gb, 3); | |
1240 | ||
1241 | pps->sign_data_hiding_flag = get_bits1(gb); | |
1242 | ||
1243 | pps->cabac_init_present_flag = get_bits1(gb); | |
1244 | ||
1245 | pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1; | |
1246 | pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1; | |
1247 | ||
1248 | pps->pic_init_qp_minus26 = get_se_golomb(gb); | |
1249 | ||
1250 | pps->constrained_intra_pred_flag = get_bits1(gb); | |
1251 | pps->transform_skip_enabled_flag = get_bits1(gb); | |
1252 | ||
1253 | pps->cu_qp_delta_enabled_flag = get_bits1(gb); | |
1254 | pps->diff_cu_qp_delta_depth = 0; | |
1255 | if (pps->cu_qp_delta_enabled_flag) | |
1256 | pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb); | |
1257 | ||
092a9121 DM |
1258 | if (pps->diff_cu_qp_delta_depth < 0 || |
1259 | pps->diff_cu_qp_delta_depth > sps->log2_diff_max_min_coding_block_size) { | |
1260 | av_log(s->avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n", | |
1261 | pps->diff_cu_qp_delta_depth); | |
1262 | ret = AVERROR_INVALIDDATA; | |
1263 | goto err; | |
1264 | } | |
1265 | ||
2ba45a60 DM |
1266 | pps->cb_qp_offset = get_se_golomb(gb); |
1267 | if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) { | |
1268 | av_log(s->avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n", | |
1269 | pps->cb_qp_offset); | |
1270 | ret = AVERROR_INVALIDDATA; | |
1271 | goto err; | |
1272 | } | |
1273 | pps->cr_qp_offset = get_se_golomb(gb); | |
1274 | if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) { | |
1275 | av_log(s->avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n", | |
1276 | pps->cr_qp_offset); | |
1277 | ret = AVERROR_INVALIDDATA; | |
1278 | goto err; | |
1279 | } | |
1280 | pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb); | |
1281 | ||
1282 | pps->weighted_pred_flag = get_bits1(gb); | |
1283 | pps->weighted_bipred_flag = get_bits1(gb); | |
1284 | ||
1285 | pps->transquant_bypass_enable_flag = get_bits1(gb); | |
1286 | pps->tiles_enabled_flag = get_bits1(gb); | |
1287 | pps->entropy_coding_sync_enabled_flag = get_bits1(gb); | |
1288 | ||
1289 | if (pps->tiles_enabled_flag) { | |
1290 | pps->num_tile_columns = get_ue_golomb_long(gb) + 1; | |
1291 | pps->num_tile_rows = get_ue_golomb_long(gb) + 1; | |
1292 | if (pps->num_tile_columns == 0 || | |
1293 | pps->num_tile_columns >= sps->width) { | |
1294 | av_log(s->avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n", | |
1295 | pps->num_tile_columns - 1); | |
1296 | ret = AVERROR_INVALIDDATA; | |
1297 | goto err; | |
1298 | } | |
1299 | if (pps->num_tile_rows == 0 || | |
1300 | pps->num_tile_rows >= sps->height) { | |
1301 | av_log(s->avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n", | |
1302 | pps->num_tile_rows - 1); | |
1303 | ret = AVERROR_INVALIDDATA; | |
1304 | goto err; | |
1305 | } | |
1306 | ||
1307 | pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width)); | |
1308 | pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height)); | |
1309 | if (!pps->column_width || !pps->row_height) { | |
1310 | ret = AVERROR(ENOMEM); | |
1311 | goto err; | |
1312 | } | |
1313 | ||
1314 | pps->uniform_spacing_flag = get_bits1(gb); | |
1315 | if (!pps->uniform_spacing_flag) { | |
1316 | uint64_t sum = 0; | |
1317 | for (i = 0; i < pps->num_tile_columns - 1; i++) { | |
1318 | pps->column_width[i] = get_ue_golomb_long(gb) + 1; | |
1319 | sum += pps->column_width[i]; | |
1320 | } | |
1321 | if (sum >= sps->ctb_width) { | |
1322 | av_log(s->avctx, AV_LOG_ERROR, "Invalid tile widths.\n"); | |
1323 | ret = AVERROR_INVALIDDATA; | |
1324 | goto err; | |
1325 | } | |
1326 | pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum; | |
1327 | ||
1328 | sum = 0; | |
1329 | for (i = 0; i < pps->num_tile_rows - 1; i++) { | |
1330 | pps->row_height[i] = get_ue_golomb_long(gb) + 1; | |
1331 | sum += pps->row_height[i]; | |
1332 | } | |
1333 | if (sum >= sps->ctb_height) { | |
1334 | av_log(s->avctx, AV_LOG_ERROR, "Invalid tile heights.\n"); | |
1335 | ret = AVERROR_INVALIDDATA; | |
1336 | goto err; | |
1337 | } | |
1338 | pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum; | |
1339 | } | |
1340 | pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb); | |
1341 | } | |
1342 | ||
1343 | pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb); | |
1344 | ||
1345 | pps->deblocking_filter_control_present_flag = get_bits1(gb); | |
1346 | if (pps->deblocking_filter_control_present_flag) { | |
1347 | pps->deblocking_filter_override_enabled_flag = get_bits1(gb); | |
1348 | pps->disable_dbf = get_bits1(gb); | |
1349 | if (!pps->disable_dbf) { | |
1350 | pps->beta_offset = get_se_golomb(gb) * 2; | |
1351 | pps->tc_offset = get_se_golomb(gb) * 2; | |
1352 | if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) { | |
1353 | av_log(s->avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n", | |
1354 | pps->beta_offset/2); | |
1355 | ret = AVERROR_INVALIDDATA; | |
1356 | goto err; | |
1357 | } | |
1358 | if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) { | |
1359 | av_log(s->avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n", | |
1360 | pps->tc_offset/2); | |
1361 | ret = AVERROR_INVALIDDATA; | |
1362 | goto err; | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | pps->scaling_list_data_present_flag = get_bits1(gb); | |
1368 | if (pps->scaling_list_data_present_flag) { | |
1369 | set_default_scaling_list_data(&pps->scaling_list); | |
1370 | ret = scaling_list_data(s, &pps->scaling_list, sps); | |
1371 | if (ret < 0) | |
1372 | goto err; | |
1373 | } | |
1374 | pps->lists_modification_present_flag = get_bits1(gb); | |
1375 | pps->log2_parallel_merge_level = get_ue_golomb_long(gb) + 2; | |
1376 | if (pps->log2_parallel_merge_level > sps->log2_ctb_size) { | |
1377 | av_log(s->avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n", | |
1378 | pps->log2_parallel_merge_level - 2); | |
1379 | ret = AVERROR_INVALIDDATA; | |
1380 | goto err; | |
1381 | } | |
1382 | ||
1383 | pps->slice_header_extension_present_flag = get_bits1(gb); | |
1384 | ||
1385 | if (get_bits1(gb)) { // pps_extension_present_flag | |
1386 | int pps_range_extensions_flag = get_bits1(gb); | |
1387 | /* int pps_extension_7bits = */ get_bits(gb, 7); | |
1388 | if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps_range_extensions_flag) { | |
f6fa7814 DM |
1389 | if ((ret = pps_range_extensions(s, pps, sps)) < 0) |
1390 | goto err; | |
2ba45a60 DM |
1391 | } |
1392 | } | |
1393 | ||
1394 | // Inferred parameters | |
1395 | pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd)); | |
1396 | pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd)); | |
1397 | pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX)); | |
1398 | if (!pps->col_bd || !pps->row_bd || !pps->col_idxX) { | |
1399 | ret = AVERROR(ENOMEM); | |
1400 | goto err; | |
1401 | } | |
1402 | ||
1403 | if (pps->uniform_spacing_flag) { | |
1404 | if (!pps->column_width) { | |
1405 | pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width)); | |
1406 | pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height)); | |
1407 | } | |
1408 | if (!pps->column_width || !pps->row_height) { | |
1409 | ret = AVERROR(ENOMEM); | |
1410 | goto err; | |
1411 | } | |
1412 | ||
1413 | for (i = 0; i < pps->num_tile_columns; i++) { | |
1414 | pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns - | |
1415 | (i * sps->ctb_width) / pps->num_tile_columns; | |
1416 | } | |
1417 | ||
1418 | for (i = 0; i < pps->num_tile_rows; i++) { | |
1419 | pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows - | |
1420 | (i * sps->ctb_height) / pps->num_tile_rows; | |
1421 | } | |
1422 | } | |
1423 | ||
1424 | pps->col_bd[0] = 0; | |
1425 | for (i = 0; i < pps->num_tile_columns; i++) | |
1426 | pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i]; | |
1427 | ||
1428 | pps->row_bd[0] = 0; | |
1429 | for (i = 0; i < pps->num_tile_rows; i++) | |
1430 | pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i]; | |
1431 | ||
1432 | for (i = 0, j = 0; i < sps->ctb_width; i++) { | |
1433 | if (i > pps->col_bd[j]) | |
1434 | j++; | |
1435 | pps->col_idxX[i] = j; | |
1436 | } | |
1437 | ||
1438 | /** | |
1439 | * 6.5 | |
1440 | */ | |
1441 | pic_area_in_ctbs = sps->ctb_width * sps->ctb_height; | |
1442 | ||
1443 | pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts)); | |
1444 | pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs)); | |
1445 | pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id)); | |
1446 | pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab)); | |
1447 | if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs || | |
1448 | !pps->tile_id || !pps->min_tb_addr_zs_tab) { | |
1449 | ret = AVERROR(ENOMEM); | |
1450 | goto err; | |
1451 | } | |
1452 | ||
1453 | for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) { | |
1454 | int tb_x = ctb_addr_rs % sps->ctb_width; | |
1455 | int tb_y = ctb_addr_rs / sps->ctb_width; | |
1456 | int tile_x = 0; | |
1457 | int tile_y = 0; | |
1458 | int val = 0; | |
1459 | ||
1460 | for (i = 0; i < pps->num_tile_columns; i++) { | |
1461 | if (tb_x < pps->col_bd[i + 1]) { | |
1462 | tile_x = i; | |
1463 | break; | |
1464 | } | |
1465 | } | |
1466 | ||
1467 | for (i = 0; i < pps->num_tile_rows; i++) { | |
1468 | if (tb_y < pps->row_bd[i + 1]) { | |
1469 | tile_y = i; | |
1470 | break; | |
1471 | } | |
1472 | } | |
1473 | ||
1474 | for (i = 0; i < tile_x; i++) | |
1475 | val += pps->row_height[tile_y] * pps->column_width[i]; | |
1476 | for (i = 0; i < tile_y; i++) | |
1477 | val += sps->ctb_width * pps->row_height[i]; | |
1478 | ||
1479 | val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] + | |
1480 | tb_x - pps->col_bd[tile_x]; | |
1481 | ||
1482 | pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val; | |
1483 | pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs; | |
1484 | } | |
1485 | ||
1486 | for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++) | |
1487 | for (i = 0; i < pps->num_tile_columns; i++, tile_id++) | |
1488 | for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++) | |
1489 | for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++) | |
1490 | pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id; | |
1491 | ||
1492 | pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs)); | |
1493 | if (!pps->tile_pos_rs) { | |
1494 | ret = AVERROR(ENOMEM); | |
1495 | goto err; | |
1496 | } | |
1497 | ||
1498 | for (j = 0; j < pps->num_tile_rows; j++) | |
1499 | for (i = 0; i < pps->num_tile_columns; i++) | |
1500 | pps->tile_pos_rs[j * pps->num_tile_columns + i] = pps->row_bd[j] * sps->ctb_width + pps->col_bd[i]; | |
1501 | ||
1502 | log2_diff_ctb_min_tb_size = sps->log2_ctb_size - sps->log2_min_tb_size; | |
1503 | pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1]; | |
1504 | for (y = 0; y < sps->tb_mask+2; y++) { | |
1505 | pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1; | |
1506 | pps->min_tb_addr_zs_tab[y] = -1; | |
1507 | } | |
1508 | for (y = 0; y < sps->tb_mask+1; y++) { | |
1509 | for (x = 0; x < sps->tb_mask+1; x++) { | |
1510 | int tb_x = x >> log2_diff_ctb_min_tb_size; | |
1511 | int tb_y = y >> log2_diff_ctb_min_tb_size; | |
1512 | int ctb_addr_rs = sps->ctb_width * tb_y + tb_x; | |
1513 | int val = pps->ctb_addr_rs_to_ts[ctb_addr_rs] << | |
1514 | (log2_diff_ctb_min_tb_size * 2); | |
1515 | for (i = 0; i < log2_diff_ctb_min_tb_size; i++) { | |
1516 | int m = 1 << i; | |
1517 | val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0); | |
1518 | } | |
1519 | pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | if (get_bits_left(gb) < 0) { | |
1524 | av_log(s->avctx, AV_LOG_ERROR, | |
1525 | "Overread PPS by %d bits\n", -get_bits_left(gb)); | |
1526 | goto err; | |
1527 | } | |
1528 | ||
1529 | av_buffer_unref(&s->pps_list[pps_id]); | |
1530 | s->pps_list[pps_id] = pps_buf; | |
1531 | ||
1532 | return 0; | |
1533 | ||
1534 | err: | |
1535 | av_buffer_unref(&pps_buf); | |
1536 | return ret; | |
1537 | } |