/src/gpac/src/media_tools/av_parsers.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * GPAC - Multimedia Framework C SDK |
3 | | * |
4 | | * Authors: Jean Le Feuvre, Romain Bouqueau, Cyril Concolato |
5 | | * Copyright (c) Telecom ParisTech 2000-2024 |
6 | | * All rights reserved |
7 | | * |
8 | | * This file is part of GPAC / Media Tools sub-project |
9 | | * |
10 | | * GPAC is free software; you can redistribute it and/or modify |
11 | | * it under the terms of the GNU Lesser General Public License as published by |
12 | | * the Free Software Foundation; either version 2, or (at your option) |
13 | | * any later version. |
14 | | * |
15 | | * GPAC is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public |
21 | | * License along with this library; see the file COPYING. If not, write to |
22 | | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
23 | | * |
24 | | */ |
25 | | |
26 | | #include <gpac/internal/media_dev.h> |
27 | | #include <gpac/constants.h> |
28 | | #include <gpac/mpeg4_odf.h> |
29 | | #include <gpac/maths.h> |
30 | | #include <gpac/avparse.h> |
31 | | |
32 | | #ifndef GPAC_DISABLE_OGG |
33 | | #include <gpac/internal/ogg.h> |
34 | | #endif |
35 | | |
36 | | //uncomment/define globally to remove all bitstream parsing logging from code (this will break inspect mode analyze=bs) |
37 | | //#define GPAC_DISABLE_AVPARSE_LOGS |
38 | | |
39 | | #ifndef GPAC_DISABLE_AVPARSE_LOGS |
40 | | void gf_bs_log_idx(GF_BitStream *bs, u32 nBits, const char *fname, s64 val, s32 idx1, s32 idx2, s32 idx3); |
41 | | |
42 | 487k | #define gf_bs_log(_bs, _nBits, _fname, _val) gf_bs_log_idx(_bs, _nBits, _fname, _val, -1, -1, -1) |
43 | | |
44 | | static u32 gf_bs_read_int_log_idx3(GF_BitStream *bs, u32 nBits, const char *fname, s32 idx1, s32 idx2, s32 idx3) |
45 | 492M | { |
46 | 492M | u32 val = gf_bs_read_int(bs, nBits); |
47 | 492M | gf_bs_log_idx(bs, nBits, fname, val, idx1, idx2, idx3); |
48 | 492M | return val; |
49 | 492M | } |
50 | | |
51 | 42.5M | #define gf_bs_read_int_log(_bs, _nBits, _fname) gf_bs_read_int_log_idx3(_bs, _nBits, _fname, -1, -1, -1) |
52 | 319M | #define gf_bs_read_int_log_idx(_bs, _nBits, _fname, _idx) gf_bs_read_int_log_idx3(_bs, _nBits, _fname, _idx, -1, -1) |
53 | 129M | #define gf_bs_read_int_log_idx2(_bs, _nBits, _fname, _idx1, _idx2) gf_bs_read_int_log_idx3(_bs, _nBits, _fname, (s32) _idx1, (s32) _idx2, -1) |
54 | | |
55 | | |
56 | | #else |
57 | | |
58 | | #define gf_bs_log(_bs, _nBits, _fname, _val) |
59 | | #define gf_bs_log_idx(_bs, _nBits, _fname, _val, _idx1, _idx2, _idx3) |
60 | | |
61 | | #define gf_bs_read_int_log(_bs, _nbb, _f) gf_bs_read_int(_bs, _nbb) |
62 | | #define gf_bs_read_int_log_idx(_bs, _nbb, _f, _idx) gf_bs_read_int(_bs, _nbb) |
63 | | #define gf_bs_read_int_log_idx2(_bs, _nbb, _f, _idx1, _idx2) gf_bs_read_int(_bs, _nbb) |
64 | | #define gf_bs_read_int_log_idx3(_bs, _nbb, _f, _idx1, _idx2, _idx3) gf_bs_read_int(_bs, _nbb) |
65 | | |
66 | | #endif |
67 | | |
68 | | |
69 | | |
70 | | |
71 | | static const struct { |
72 | | u32 w, h; |
73 | | } std_par[] = |
74 | | { |
75 | | { 4, 3}, {3, 2}, {16, 9}, {5, 3}, {5, 4}, {8, 5}, {2, 1}, {1, 1}, |
76 | | {0, 0}, |
77 | | }; |
78 | | |
79 | | GF_EXPORT |
80 | | void gf_media_reduce_aspect_ratio(u32 *width, u32 *height) |
81 | 0 | { |
82 | 0 | u32 i = 0; |
83 | 0 | u32 w = *width; |
84 | 0 | u32 h = *height; |
85 | 0 | while (std_par[i].w) { |
86 | 0 | if (std_par[i].w * h == std_par[i].h * w) { |
87 | 0 | *width = std_par[i].w; |
88 | 0 | *height = std_par[i].h; |
89 | 0 | return; |
90 | 0 | } |
91 | 0 | i++; |
92 | 0 | } |
93 | | //not standard one, reduce (brute force) |
94 | 0 | i = 2; |
95 | 0 | while (1) { |
96 | 0 | if ((w <= i) || (h <= i)) |
97 | 0 | return; |
98 | | |
99 | 0 | if (!(w % i) && !(h % i)) { |
100 | 0 | *width = w / i; |
101 | 0 | *height = h / i; |
102 | 0 | } |
103 | 0 | i += 1; |
104 | 0 | } |
105 | 0 | } |
106 | | |
107 | | GF_EXPORT |
108 | | void gf_media_get_reduced_frame_rate(u32 *timescale, u32 *sample_dur) |
109 | 11 | { |
110 | 11 | u32 res; |
111 | 11 | if (!*sample_dur) return; |
112 | 11 | res = *timescale / *sample_dur; |
113 | 11 | if (res * (*sample_dur) == *timescale) { |
114 | 0 | *timescale = res; |
115 | 0 | *sample_dur = 1; |
116 | 0 | } |
117 | 11 | else if ((double)((u64)*timescale * 1001 - (u64)(res + 1) * (u64)*sample_dur * 1000) / ((u64)(res + 1) * (u64)*sample_dur * 1000) < 0.001) { |
118 | 0 | *timescale = (res + 1) * 1000; |
119 | 0 | *sample_dur = 1001; |
120 | 0 | } |
121 | 11 | } |
122 | | |
123 | | struct __m4v_profile |
124 | | { |
125 | | u32 value; |
126 | | const char *name; |
127 | | } M4VProfiles[] = { |
128 | | {0x00, "Reserved (0x00) Profile"}, |
129 | | {0x01, "Simple Profile @ Level 1"}, |
130 | | {0x02, "Simple Profile @ Level 2"}, |
131 | | {0x03, "Simple Profile @ Level 3"}, |
132 | | {0x08, "Simple Profile @ Level 0"}, |
133 | | {0x10, "Simple Scalable Profile @ Level 0"}, |
134 | | {0x11, "Simple Scalable Profile @ Level 1"}, |
135 | | {0x12, "Simple Scalable Profile @ Level 2"}, |
136 | | {0x21, "Core Profile @ Level 1"}, |
137 | | {0x22, "Core Profile @ Level 2"}, |
138 | | {0x32, "Main Profile @ Level 2"}, |
139 | | {0x33, "Main Profile @ Level 3"}, |
140 | | {0x34, "Main Profile @ Level 4"}, |
141 | | {0x42, "N-bit Profile @ Level 2"}, |
142 | | {0x51, "Scalable Texture Profile @ Level 1"}, |
143 | | {0x61, "Simple Face Animation Profile @ Level 1"}, |
144 | | {0x62, "Simple Face Animation Profile @ Level 2"}, |
145 | | {0x63, "Simple FBA Profile @ Level 1"}, |
146 | | {0x64, "Simple FBA Profile @ Level 2"}, |
147 | | {0x71, "Basic Animated Texture Profile @ Level 1"}, |
148 | | {0x72, "Basic Animated Texture Profile @ Level 2"}, |
149 | | {0x7F, "AVC/H264 Profile"}, |
150 | | {0x81, "Hybrid Profile @ Level 1"}, |
151 | | {0x82, "Hybrid Profile @ Level 2"}, |
152 | | {0x91, "Advanced Real Time Simple Profile @ Level 1"}, |
153 | | {0x92, "Advanced Real Time Simple Profile @ Level 2"}, |
154 | | {0x93, "Advanced Real Time Simple Profile @ Level 3"}, |
155 | | {0x94, "Advanced Real Time Simple Profile @ Level 4"}, |
156 | | {0xA1, "Core Scalable Profile @ Level1"}, |
157 | | {0xA2, "Core Scalable Profile @ Level2"}, |
158 | | {0xA3, "Core Scalable Profile @ Level3"}, |
159 | | {0xB1, "Advanced Coding Efficiency Profile @ Level 1"}, |
160 | | {0xB2, "Advanced Coding Efficiency Profile @ Level 2"}, |
161 | | {0xB3, "Advanced Coding Efficiency Profile @ Level 3"}, |
162 | | {0xB4, "Advanced Coding Efficiency Profile @ Level 4"}, |
163 | | {0xC1, "Advanced Core Profile @ Level 1"}, |
164 | | {0xC2, "Advanced Core Profile @ Level 2"}, |
165 | | {0xD1, "Advanced Scalable Texture @ Level1"}, |
166 | | {0xD2, "Advanced Scalable Texture @ Level2"}, |
167 | | {0xE1, "Simple Studio Profile @ Level 1"}, |
168 | | {0xE2, "Simple Studio Profile @ Level 2"}, |
169 | | {0xE3, "Simple Studio Profile @ Level 3"}, |
170 | | {0xE4, "Simple Studio Profile @ Level 4"}, |
171 | | {0xE5, "Core Studio Profile @ Level 1"}, |
172 | | {0xE6, "Core Studio Profile @ Level 2"}, |
173 | | {0xE7, "Core Studio Profile @ Level 3"}, |
174 | | {0xE8, "Core Studio Profile @ Level 4"}, |
175 | | {0xF0, "Advanced Simple Profile @ Level 0"}, |
176 | | {0xF1, "Advanced Simple Profile @ Level 1"}, |
177 | | {0xF2, "Advanced Simple Profile @ Level 2"}, |
178 | | {0xF3, "Advanced Simple Profile @ Level 3"}, |
179 | | {0xF4, "Advanced Simple Profile @ Level 4"}, |
180 | | {0xF5, "Advanced Simple Profile @ Level 5"}, |
181 | | {0xF7, "Advanced Simple Profile @ Level 3b"}, |
182 | | {0xF8, "Fine Granularity Scalable Profile @ Level 0"}, |
183 | | {0xF9, "Fine Granularity Scalable Profile @ Level 1"}, |
184 | | {0xFA, "Fine Granularity Scalable Profile @ Level 2"}, |
185 | | {0xFB, "Fine Granularity Scalable Profile @ Level 3"}, |
186 | | {0xFC, "Fine Granularity Scalable Profile @ Level 4"}, |
187 | | {0xFD, "Fine Granularity Scalable Profile @ Level 5"}, |
188 | | {0xFE, "Not part of MPEG-4 Visual profiles"}, |
189 | | {0xFF, "No visual capability required"} |
190 | | }; |
191 | | |
192 | | GF_EXPORT |
193 | | const char *gf_m4v_get_profile_name(u8 video_pl) |
194 | 0 | { |
195 | 0 | u32 i, count = GF_ARRAY_LENGTH(M4VProfiles); |
196 | 0 | for (i=0; i<count; i++) { |
197 | 0 | if ((u32)video_pl == M4VProfiles[i].value) |
198 | 0 | return M4VProfiles[i].name; |
199 | 0 | } |
200 | 0 | return "ISO Reserved Profile"; |
201 | 0 | } |
202 | | |
203 | | |
204 | | #ifndef GPAC_DISABLE_AV_PARSERS |
205 | | |
206 | 61.7M | #define MPEG12_START_CODE_PREFIX 0x000001 |
207 | | #define MPEG12_PICTURE_START_CODE 0x00000100 |
208 | 0 | #define MPEG12_SLICE_MIN_START 0x00000101 |
209 | 0 | #define MPEG12_SLICE_MAX_START 0x000001af |
210 | | #define MPEG12_USER_DATA_START_CODE 0x000001b2 |
211 | | #define MPEG12_SEQUENCE_START_CODE 0x000001b3 |
212 | | #define MPEG12_SEQUENCE_ERR_START_CODE 0x000001b4 |
213 | | #define MPEG12_EXT_START_CODE 0x000001b5 |
214 | | #define MPEG12_SEQUENCE_END_START_CODE 0x000001b7 |
215 | | #define MPEG12_GOP_START_CODE 0x000001b8 |
216 | | |
217 | | s32 gf_mv12_next_start_code(unsigned char *pbuffer, u32 buflen, u32 *optr, u32 *scode) |
218 | 3.74M | { |
219 | 3.74M | u32 value; |
220 | 3.74M | u32 offset; |
221 | | |
222 | 3.74M | if (buflen < 4) return -1; |
223 | 61.7M | for (offset = 0; offset < buflen - 3; offset++, pbuffer++) { |
224 | | #ifdef GPAC_BIG_ENDIAN |
225 | | value = *(u32 *)pbuffer >> 8; |
226 | | #else |
227 | 61.7M | value = (pbuffer[0] << 16) | (pbuffer[1] << 8) | (pbuffer[2] << 0); |
228 | 61.7M | #endif |
229 | | |
230 | 61.7M | if (value == MPEG12_START_CODE_PREFIX) { |
231 | 3.67M | *optr = offset; |
232 | 3.67M | *scode = (value << 8) | pbuffer[3]; |
233 | 3.67M | return 0; |
234 | 3.67M | } |
235 | 61.7M | } |
236 | 57.6k | return -1; |
237 | 3.73M | } |
238 | | |
239 | | s32 gf_mv12_next_slice_start(unsigned char *pbuffer, u32 startoffset, u32 buflen, u32 *slice_offset) |
240 | 0 | { |
241 | 0 | u32 slicestart, code; |
242 | 0 | while (gf_mv12_next_start_code(pbuffer + startoffset, buflen - startoffset, &slicestart, &code) >= 0) { |
243 | 0 | if ((code >= MPEG12_SLICE_MIN_START) && (code <= MPEG12_SLICE_MAX_START)) { |
244 | 0 | *slice_offset = slicestart + startoffset; |
245 | 0 | return 0; |
246 | 0 | } |
247 | 0 | startoffset += slicestart + 4; |
248 | 0 | } |
249 | 0 | return -1; |
250 | 0 | } |
251 | | |
252 | | |
253 | | /* |
254 | | MPEG-4 video (14496-2) |
255 | | */ |
256 | | |
257 | | struct __tag_m4v_parser |
258 | | { |
259 | | GF_BitStream *bs; |
260 | | Bool mpeg12, step_mode; |
261 | | u32 current_object_type; |
262 | | u32 force_next_obj_type; |
263 | | u64 current_object_start; |
264 | | u32 tc_dec, prev_tc_dec, tc_disp, prev_tc_disp; |
265 | | }; |
266 | | |
267 | | GF_EXPORT |
268 | | GF_M4VParser *gf_m4v_parser_new(u8 *data, u64 data_size, Bool mpeg12video) |
269 | 269k | { |
270 | 269k | GF_M4VParser *tmp; |
271 | 269k | if (!data || !data_size) return NULL; |
272 | 269k | GF_SAFEALLOC(tmp, GF_M4VParser); |
273 | 269k | if (!tmp) return NULL; |
274 | 269k | tmp->bs = gf_bs_new(data, data_size, GF_BITSTREAM_READ); |
275 | 269k | tmp->mpeg12 = mpeg12video; |
276 | 269k | return tmp; |
277 | 269k | } |
278 | | |
279 | | GF_M4VParser *gf_m4v_parser_bs_new(GF_BitStream *bs, Bool mpeg12video) |
280 | 508 | { |
281 | 508 | GF_M4VParser *tmp; |
282 | 508 | GF_SAFEALLOC(tmp, GF_M4VParser); |
283 | 508 | if (!tmp) return NULL; |
284 | 508 | tmp->bs = bs; |
285 | 508 | tmp->mpeg12 = mpeg12video; |
286 | 508 | return tmp; |
287 | 508 | } |
288 | | |
289 | | GF_EXPORT |
290 | | void gf_m4v_parser_del(GF_M4VParser *m4v) |
291 | 270k | { |
292 | 270k | gf_bs_del(m4v->bs); |
293 | 270k | gf_free(m4v); |
294 | 270k | } |
295 | | |
296 | | GF_EXPORT |
297 | | void gf_m4v_parser_del_no_bs(GF_M4VParser *m4v) |
298 | 342 | { |
299 | 342 | gf_free(m4v); |
300 | 342 | } |
301 | | |
302 | | GF_EXPORT |
303 | | void gf_m4v_parser_set_inspect(GF_M4VParser *m4v) |
304 | 259k | { |
305 | 259k | if (m4v) m4v->step_mode = 1; |
306 | 259k | } |
307 | | GF_EXPORT |
308 | | u32 gf_m4v_parser_get_obj_type(GF_M4VParser *m4v) |
309 | 2.14M | { |
310 | 2.14M | if (m4v) return m4v->current_object_type; |
311 | 0 | return 0; |
312 | 2.14M | } |
313 | | |
314 | 25.4M | #define M4V_CACHE_SIZE 4096 |
315 | | s32 M4V_LoadObject(GF_M4VParser *m4v) |
316 | 23.0M | { |
317 | 23.0M | u32 v, bpos, found; |
318 | 23.0M | char m4v_cache[M4V_CACHE_SIZE]; |
319 | 23.0M | u64 end, cache_start, load_size; |
320 | 23.0M | if (!m4v) return 0; |
321 | 23.0M | if (m4v->force_next_obj_type) { |
322 | 100k | m4v->current_object_type = m4v->force_next_obj_type - 1; |
323 | 100k | m4v->force_next_obj_type = 0; |
324 | 100k | return (s32)m4v->current_object_type; |
325 | 100k | } |
326 | | |
327 | 22.9M | bpos = 0; |
328 | 22.9M | found = 0; |
329 | 22.9M | load_size = 0; |
330 | 22.9M | end = 0; |
331 | 22.9M | cache_start = 0; |
332 | 22.9M | v = 0xffffffff; |
333 | 365M | while (!end) { |
334 | | /*refill cache*/ |
335 | 365M | if (bpos == (u32)load_size) { |
336 | 23.4M | if (!gf_bs_available(m4v->bs)) break; |
337 | 22.8M | load_size = gf_bs_available(m4v->bs); |
338 | 22.8M | if (load_size > M4V_CACHE_SIZE) load_size = M4V_CACHE_SIZE; |
339 | 22.8M | bpos = 0; |
340 | 22.8M | cache_start = gf_bs_get_position(m4v->bs); |
341 | 22.8M | gf_bs_read_data(m4v->bs, m4v_cache, (u32)load_size); |
342 | 22.8M | } |
343 | 364M | v = ((v << 8) & 0xFFFFFF00) | ((u8)m4v_cache[bpos]); |
344 | 364M | bpos++; |
345 | 364M | if ((v & 0xFFFFFF00) == 0x00000100) { |
346 | 22.3M | end = cache_start + bpos - 4; |
347 | 22.3M | found = 1; |
348 | 22.3M | break; |
349 | 22.3M | } |
350 | 364M | } |
351 | 22.9M | if (!found) return -1; |
352 | 22.3M | m4v->current_object_start = end; |
353 | 22.3M | gf_bs_seek(m4v->bs, end + 3); |
354 | 22.3M | m4v->current_object_type = gf_bs_read_u8(m4v->bs); |
355 | 22.3M | return (s32)m4v->current_object_type; |
356 | 22.9M | } |
357 | | |
358 | | |
359 | | GF_EXPORT |
360 | | void gf_m4v_rewrite_pl(u8 **o_data, u32 *o_dataLen, u8 PL) |
361 | 0 | { |
362 | 0 | u32 pos = 0; |
363 | 0 | unsigned char *data = (unsigned char *)*o_data; |
364 | 0 | u32 dataLen = *o_dataLen; |
365 | |
|
366 | 0 | while (pos + 4 < dataLen) { |
367 | 0 | if (!data[pos] && !data[pos + 1] && (data[pos + 2] == 0x01) && (data[pos + 3] == M4V_VOS_START_CODE)) { |
368 | 0 | data[pos + 4] = PL; |
369 | 0 | return; |
370 | 0 | } |
371 | 0 | pos++; |
372 | 0 | } |
373 | | /*emulate VOS at beggining*/ |
374 | 0 | (*o_data) = (char *)gf_malloc(sizeof(char)*(dataLen + 5)); |
375 | 0 | (*o_data)[0] = 0; |
376 | 0 | (*o_data)[1] = 0; |
377 | 0 | (*o_data)[2] = 1; |
378 | 0 | (*o_data)[3] = (char)M4V_VOS_START_CODE; |
379 | 0 | (*o_data)[4] = PL; |
380 | 0 | memcpy((*o_data + 5), data, sizeof(char)*dataLen); |
381 | 0 | gf_free(data); |
382 | 0 | (*o_dataLen) = dataLen + 5; |
383 | 0 | } |
384 | | |
385 | | static GF_Err M4V_Reset(GF_M4VParser *m4v, u64 start) |
386 | 746k | { |
387 | 746k | gf_bs_seek(m4v->bs, start); |
388 | | |
389 | 746k | gf_fatal_assert(start < (u64)1<<31); |
390 | 746k | m4v->current_object_start = (u32)start; |
391 | 746k | m4v->current_object_type = 0; |
392 | 746k | return GF_OK; |
393 | 746k | } |
394 | | |
395 | | void gf_m4v_parser_reset(GF_M4VParser *m4v, u8 sc_type) |
396 | 524k | { |
397 | 524k | m4v->current_object_start = 0; |
398 | 524k | m4v->current_object_type = 0; |
399 | 524k | m4v->force_next_obj_type = sc_type; |
400 | 524k | } |
401 | | static GF_Err gf_m4v_parse_config_mpeg12(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi) |
402 | 4.04k | { |
403 | 4.04k | unsigned char p[4]; |
404 | 4.04k | u32 ext_type; |
405 | 4.04k | s32 o_type; |
406 | 4.04k | u8 go, par; |
407 | | |
408 | 4.04k | if (!m4v || !dsi) return GF_BAD_PARAM; |
409 | | |
410 | 4.04k | memset(dsi, 0, sizeof(GF_M4VDecSpecInfo)); |
411 | 4.04k | dsi->VideoPL = 0; |
412 | | |
413 | 4.04k | go = 1; |
414 | 51.8k | while (go) { |
415 | 47.7k | o_type = M4V_LoadObject(m4v); |
416 | 47.7k | switch (o_type) { |
417 | 6.97k | case M2V_SEQ_START_CODE: |
418 | 6.97k | dsi->RAP_stream = 1; |
419 | 6.97k | gf_bs_read_data(m4v->bs, (char *)p, 4); |
420 | 6.97k | dsi->width = (p[0] << 4) | ((p[1] >> 4) & 0xf); |
421 | 6.97k | dsi->height = ((p[1] & 0xf) << 8) | p[2]; |
422 | | |
423 | 6.97k | dsi->VideoPL = GF_CODECID_MPEG1; |
424 | 6.97k | par = (p[3] >> 4) & 0xf; |
425 | 6.97k | switch (par) { |
426 | 82 | case 2: |
427 | 82 | dsi->par_num = dsi->height / 3; |
428 | 82 | dsi->par_den = dsi->width / 4; |
429 | 82 | break; |
430 | 973 | case 3: |
431 | 973 | dsi->par_num = dsi->height / 9; |
432 | 973 | dsi->par_den = dsi->width / 16; |
433 | 973 | break; |
434 | 1.34k | case 4: |
435 | 1.34k | dsi->par_num = dsi->height / 2; |
436 | 1.34k | dsi->par_den = dsi->width / 21; |
437 | 1.34k | break; |
438 | 4.57k | default: |
439 | 4.57k | dsi->par_den = dsi->par_num = 0; |
440 | 4.57k | break; |
441 | 6.97k | } |
442 | 6.97k | switch (p[3] & 0xf) { |
443 | 397 | case 0: |
444 | 397 | break; |
445 | 4 | case 1: |
446 | 4 | dsi->fps = 24000.0 / 1001.0; |
447 | 4 | break; |
448 | 1.03k | case 2: |
449 | 1.03k | dsi->fps = 24.0; |
450 | 1.03k | break; |
451 | 165 | case 3: |
452 | 165 | dsi->fps = 25.0; |
453 | 165 | break; |
454 | 2 | case 4: |
455 | 2 | dsi->fps = 30000.0 / 1001.0; |
456 | 2 | break; |
457 | 0 | case 5: |
458 | 0 | dsi->fps = 30.0; |
459 | 0 | break; |
460 | 539 | case 6: |
461 | 539 | dsi->fps = 50.0; |
462 | 539 | break; |
463 | 136 | case 7: |
464 | 136 | dsi->fps = ((60.0*1000.0) / 1001.0); |
465 | 136 | break; |
466 | 338 | case 8: |
467 | 338 | dsi->fps = 60.0; |
468 | 338 | break; |
469 | 507 | case 9: |
470 | 507 | dsi->fps = 1; |
471 | 507 | break; |
472 | 1.55k | case 10: |
473 | 1.55k | dsi->fps = 5; |
474 | 1.55k | break; |
475 | 1.19k | case 11: |
476 | 1.19k | dsi->fps = 10; |
477 | 1.19k | break; |
478 | 1.00k | case 12: |
479 | 1.00k | dsi->fps = 12; |
480 | 1.00k | break; |
481 | 70 | case 13: |
482 | 70 | dsi->fps = 15; |
483 | 70 | break; |
484 | 6.97k | } |
485 | 6.97k | break; |
486 | 6.97k | case M2V_EXT_START_CODE: |
487 | 47 | gf_bs_read_data(m4v->bs, (char *)p, 4); |
488 | 47 | ext_type = ((p[0] >> 4) & 0xf); |
489 | 47 | if (ext_type == 1) { |
490 | 0 | dsi->VideoPL = (p[0]&0xf) | ((p[1] >> 4) & 0xf); |
491 | 0 | dsi->progresive = (p[1] & 0x8) ? 1 : 0; |
492 | 0 | dsi->chroma_fmt = (p[1]>>1) & 0x3; |
493 | 0 | dsi->height = ((p[1] & 0x1) << 13) | ((p[2] & 0x80) << 5) | (dsi->height & 0x0fff); |
494 | 0 | dsi->width = (((p[2] >> 5) & 0x3) << 12) | (dsi->width & 0x0fff); |
495 | 0 | } |
496 | 47 | break; |
497 | 3.94k | case M2V_PIC_START_CODE: |
498 | 3.94k | if (dsi->width) go = 0; |
499 | 3.94k | break; |
500 | 34.8k | default: |
501 | 34.8k | break; |
502 | | /*EOS*/ |
503 | 34.8k | case -1: |
504 | 1.93k | go = 0; |
505 | 1.93k | m4v->current_object_start = gf_bs_get_position(m4v->bs); |
506 | 1.93k | break; |
507 | 47.7k | } |
508 | 47.7k | } |
509 | 4.04k | return GF_OK; |
510 | 4.04k | } |
511 | | |
512 | | |
513 | | static const struct { |
514 | | u32 w, h; |
515 | | } m4v_sar[6] = { { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, { 40, 33 } }; |
516 | | |
517 | | static u8 m4v_get_sar_idx(u32 w, u32 h) |
518 | 0 | { |
519 | 0 | u32 i; |
520 | 0 | for (i = 0; i < 6; i++) { |
521 | 0 | if ((m4v_sar[i].w == w) && (m4v_sar[i].h == h)) return i; |
522 | 0 | } |
523 | 0 | return 0xF; |
524 | 0 | } |
525 | | |
526 | | static void gf_m4v_parse_vol(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi) |
527 | 167k | { |
528 | 167k | u8 verid, par; |
529 | 167k | s32 clock_rate; |
530 | 167k | u8 vpl = dsi->VideoPL; |
531 | | |
532 | 167k | memset(dsi, 0, sizeof(GF_M4VDecSpecInfo)); |
533 | 167k | dsi->VideoPL = vpl; |
534 | | |
535 | 167k | verid = 0; |
536 | 167k | dsi->RAP_stream = gf_bs_read_int(m4v->bs, 1); |
537 | 167k | dsi->objectType = gf_bs_read_int(m4v->bs, 8); |
538 | 167k | if (gf_bs_read_int(m4v->bs, 1)) { |
539 | 15.1k | verid = gf_bs_read_int(m4v->bs, 4); |
540 | 15.1k | gf_bs_read_int(m4v->bs, 3); |
541 | 15.1k | } |
542 | 167k | par = gf_bs_read_int(m4v->bs, 4); |
543 | 167k | if (par == 0xF) { |
544 | 543 | dsi->par_num = gf_bs_read_int(m4v->bs, 8); |
545 | 543 | dsi->par_den = gf_bs_read_int(m4v->bs, 8); |
546 | 167k | } else if (par<6) { |
547 | 12.9k | dsi->par_num = m4v_sar[par].w; |
548 | 12.9k | dsi->par_den = m4v_sar[par].h; |
549 | 12.9k | } |
550 | 167k | if (gf_bs_read_int(m4v->bs, 1)) { |
551 | 13.2k | dsi->chroma_fmt = gf_bs_read_int(m4v->bs, 2); |
552 | 13.2k | gf_bs_read_int(m4v->bs, 1); |
553 | 13.2k | if (gf_bs_read_int(m4v->bs, 1)) gf_bs_read_int(m4v->bs, 79); |
554 | 13.2k | } |
555 | 167k | dsi->has_shape = gf_bs_read_int(m4v->bs, 2); |
556 | 167k | if (dsi->has_shape && (verid!=1) ) gf_bs_read_int(m4v->bs, 4); |
557 | 167k | gf_bs_read_int(m4v->bs, 1); |
558 | | /*clock rate*/ |
559 | 167k | dsi->clock_rate = gf_bs_read_int(m4v->bs, 16); |
560 | | /*marker*/ |
561 | 167k | gf_bs_read_int(m4v->bs, 1); |
562 | | |
563 | 167k | clock_rate = dsi->clock_rate-1; |
564 | 167k | if (clock_rate >= 65536) clock_rate = 65535; |
565 | 167k | if (clock_rate > 0) { |
566 | 1.77M | for (dsi->NumBitsTimeIncrement = 1; dsi->NumBitsTimeIncrement < 16; dsi->NumBitsTimeIncrement++) { |
567 | 1.68M | if (clock_rate == 1) break; |
568 | 1.60M | clock_rate = (clock_rate >> 1); |
569 | 1.60M | } |
570 | 162k | } else { |
571 | | /*fix from vivien for divX*/ |
572 | 5.03k | dsi->NumBitsTimeIncrement = 1; |
573 | 5.03k | } |
574 | | /*fixed FPS stream*/ |
575 | 167k | dsi->time_increment = 0; |
576 | 167k | if (gf_bs_read_int(m4v->bs, 1)) { |
577 | 57.2k | dsi->time_increment = gf_bs_read_int(m4v->bs, dsi->NumBitsTimeIncrement); |
578 | 57.2k | } |
579 | 167k | if (!dsi->has_shape) { |
580 | 143k | gf_bs_read_int(m4v->bs, 1); //marker bit |
581 | 143k | dsi->width = gf_bs_read_int(m4v->bs, 13); |
582 | 143k | gf_bs_read_int(m4v->bs, 1); //marker bit |
583 | 143k | dsi->height = gf_bs_read_int(m4v->bs, 13); |
584 | 143k | gf_bs_read_int(m4v->bs, 1); //marker bit |
585 | 143k | dsi->progresive = !gf_bs_read_int(m4v->bs, 1); |
586 | 143k | } else { |
587 | 24.3k | dsi->width = dsi->height = 0; |
588 | 24.3k | } |
589 | 167k | gf_bs_align(m4v->bs); |
590 | 167k | } |
591 | | |
592 | | static GF_Err gf_m4v_parse_config_mpeg4(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi) |
593 | 89.7k | { |
594 | 89.7k | s32 o_type; |
595 | 89.7k | u8 go; |
596 | | |
597 | 89.7k | if (!m4v || !dsi) return GF_BAD_PARAM; |
598 | | |
599 | 89.7k | memset(dsi, 0, sizeof(GF_M4VDecSpecInfo)); |
600 | | |
601 | 89.7k | go = 1; |
602 | 17.4M | while (go) { |
603 | 17.4M | o_type = M4V_LoadObject(m4v); |
604 | 17.4M | switch (o_type) { |
605 | | /*vosh*/ |
606 | 4.24k | case M4V_VOS_START_CODE: |
607 | 4.24k | dsi->VideoPL = (u8)gf_bs_read_u8(m4v->bs); |
608 | 4.24k | break; |
609 | | |
610 | 19.7k | case M4V_VOL_START_CODE: |
611 | 19.7k | gf_m4v_parse_vol(m4v, dsi); |
612 | | /*shape will be done later*/ |
613 | 19.7k | gf_bs_align(m4v->bs); |
614 | 19.7k | break; |
615 | | |
616 | 24.0k | case M4V_VOP_START_CODE: |
617 | 25.3k | case M4V_GOV_START_CODE: |
618 | 25.3k | go = 0; |
619 | 25.3k | break; |
620 | | /*EOS*/ |
621 | 64.4k | case -1: |
622 | 64.4k | m4v->current_object_start = gf_bs_get_position(m4v->bs); |
623 | 64.4k | return GF_EOS; |
624 | | /*don't interest us*/ |
625 | 8.69M | case M4V_UDTA_START_CODE: |
626 | 17.3M | default: |
627 | 17.3M | break; |
628 | 17.4M | } |
629 | 17.4M | } |
630 | 25.3k | return GF_OK; |
631 | 89.7k | } |
632 | | |
633 | | GF_EXPORT |
634 | | GF_Err gf_m4v_parse_config(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi) |
635 | 93.7k | { |
636 | 93.7k | if (m4v->mpeg12) { |
637 | 4.04k | return gf_m4v_parse_config_mpeg12(m4v, dsi); |
638 | 4.04k | } |
639 | 89.7k | else { |
640 | 89.7k | return gf_m4v_parse_config_mpeg4(m4v, dsi); |
641 | 89.7k | } |
642 | 93.7k | } |
643 | | |
644 | | static GF_Err gf_m4v_parse_frame_mpeg12(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi, u8 *frame_type, u32 *time_inc, u64 *size, u64 *start, Bool *is_coded) |
645 | 970k | { |
646 | 970k | u8 go, hasVOP, firstObj, val; |
647 | 970k | s32 o_type; |
648 | | |
649 | 970k | if (!m4v || !size || !start || !frame_type) return GF_BAD_PARAM; |
650 | | |
651 | 970k | *size = 0; |
652 | 970k | firstObj = 1; |
653 | 970k | hasVOP = 0; |
654 | 970k | *is_coded = GF_FALSE; |
655 | 970k | *frame_type = 0; |
656 | | |
657 | 970k | if (!m4v->step_mode) |
658 | 328k | M4V_Reset(m4v, m4v->current_object_start); |
659 | | |
660 | 970k | m4v->current_object_type = (u32)-1; |
661 | 970k | go = 1; |
662 | 1.81M | while (go) { |
663 | 1.80M | o_type = M4V_LoadObject(m4v); |
664 | 1.80M | switch (o_type) { |
665 | 499k | case M2V_PIC_START_CODE: |
666 | | /*done*/ |
667 | 499k | if (hasVOP) { |
668 | 7.28k | go = 0; |
669 | 7.28k | break; |
670 | 7.28k | } |
671 | 492k | if (firstObj) { |
672 | 488k | *start = m4v->current_object_start; |
673 | 488k | firstObj = 0; |
674 | 488k | } |
675 | 492k | hasVOP = 1; |
676 | 492k | *is_coded = 1; |
677 | | |
678 | | /*val = */gf_bs_read_u8(m4v->bs); |
679 | 492k | val = gf_bs_read_u8(m4v->bs); |
680 | 492k | *frame_type = ((val >> 3) & 0x7); |
681 | 492k | break; |
682 | 191 | case M2V_GOP_START_CODE: |
683 | 191 | if (firstObj) { |
684 | 146 | *start = m4v->current_object_start; |
685 | 146 | firstObj = 0; |
686 | 146 | } |
687 | 191 | if (hasVOP) go = 0; |
688 | 191 | break; |
689 | | |
690 | 14.3k | case M2V_SEQ_START_CODE: |
691 | 14.3k | if (firstObj) { |
692 | 11.2k | *start = m4v->current_object_start; |
693 | 11.2k | firstObj = 0; |
694 | 11.2k | } |
695 | 14.3k | if (hasVOP) { |
696 | 82 | go = 0; |
697 | 82 | break; |
698 | 82 | } |
699 | | |
700 | | /**/ |
701 | 14.2k | break; |
702 | | |
703 | 919k | default: |
704 | 919k | break; |
705 | | |
706 | 919k | case -1: |
707 | 375k | *size = gf_bs_get_position(m4v->bs) - *start; |
708 | 375k | return GF_EOS; |
709 | 1.80M | } |
710 | 1.43M | if (m4v->step_mode) |
711 | 587k | return GF_OK; |
712 | 1.43M | } |
713 | 7.40k | *size = m4v->current_object_start - *start; |
714 | 7.40k | return GF_OK; |
715 | 970k | } |
716 | | |
717 | | static GF_Err gf_m4v_parse_frame_mpeg4(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi, u8 *frame_type, u32 *time_inc, u64 *size, u64 *start, Bool *is_coded) |
718 | 2.16M | { |
719 | 2.16M | u8 go, hasVOP, firstObj, secs; |
720 | 2.16M | s32 o_type; |
721 | 2.16M | u32 vop_inc = 0; |
722 | | |
723 | 2.16M | if (!m4v || !size || !start || !frame_type) return GF_BAD_PARAM; |
724 | | |
725 | 2.16M | *size = 0; |
726 | 2.16M | firstObj = 1; |
727 | 2.16M | hasVOP = 0; |
728 | 2.16M | *is_coded = 0; |
729 | 2.16M | m4v->current_object_type = (u32)-1; |
730 | 2.16M | *frame_type = 0; |
731 | 2.16M | *start = 0; |
732 | | |
733 | 2.16M | if (!m4v->step_mode) |
734 | 418k | M4V_Reset(m4v, m4v->current_object_start); |
735 | | |
736 | 2.16M | go = 1; |
737 | 4.14M | while (go) { |
738 | 3.72M | o_type = M4V_LoadObject(m4v); |
739 | 3.72M | switch (o_type) { |
740 | 1.11M | case M4V_VOP_START_CODE: |
741 | | /*done*/ |
742 | 1.11M | if (hasVOP) { |
743 | 298k | go = 0; |
744 | 298k | break; |
745 | 298k | } |
746 | 820k | if (firstObj) { |
747 | 756k | *start = m4v->current_object_start; |
748 | 756k | firstObj = 0; |
749 | 756k | } |
750 | 820k | hasVOP = 1; |
751 | | |
752 | | /*coding type*/ |
753 | 820k | *frame_type = 1 + gf_bs_read_int(m4v->bs, 2); |
754 | | /*modulo time base*/ |
755 | 820k | secs = 0; |
756 | 1.22M | while (gf_bs_read_int(m4v->bs, 1) != 0) |
757 | 407k | secs++; |
758 | | /*no support for B frames in parsing*/ |
759 | 820k | secs += (dsi->enh_layer || *frame_type!=2) ? m4v->tc_dec : m4v->tc_disp; |
760 | | /*marker*/ |
761 | 820k | gf_bs_read_int(m4v->bs, 1); |
762 | | /*vop_time_inc*/ |
763 | 820k | if (dsi->NumBitsTimeIncrement) |
764 | 685k | vop_inc = gf_bs_read_int(m4v->bs, dsi->NumBitsTimeIncrement); |
765 | | |
766 | 820k | m4v->prev_tc_dec = m4v->tc_dec; |
767 | 820k | m4v->prev_tc_disp = m4v->tc_disp; |
768 | 820k | if (dsi->enh_layer || *frame_type!=2) { |
769 | 525k | m4v->tc_disp = m4v->tc_dec; |
770 | 525k | m4v->tc_dec = secs; |
771 | 525k | } |
772 | 820k | *time_inc = secs * dsi->clock_rate + vop_inc; |
773 | | /*marker*/ |
774 | 820k | gf_bs_read_int(m4v->bs, 1); |
775 | | /*coded*/ |
776 | 820k | *is_coded = gf_bs_read_int(m4v->bs, 1); |
777 | 820k | gf_bs_align(m4v->bs); |
778 | 820k | break; |
779 | 30.1k | case M4V_GOV_START_CODE: |
780 | 30.1k | if (firstObj) { |
781 | 15.4k | *start = m4v->current_object_start; |
782 | 15.4k | firstObj = 0; |
783 | 15.4k | } |
784 | 30.1k | if (hasVOP) go = 0; |
785 | 30.1k | break; |
786 | | |
787 | 198k | case M4V_VOL_START_CODE: |
788 | 198k | if (m4v->step_mode) |
789 | 148k | gf_m4v_parse_vol(m4v, dsi); |
790 | 323k | case M4V_VOS_START_CODE: |
791 | 323k | if (hasVOP) { |
792 | 107k | go = 0; |
793 | 107k | } |
794 | 215k | else if (firstObj) { |
795 | 205k | *start = m4v->current_object_start; |
796 | 205k | firstObj = 0; |
797 | 205k | } |
798 | 323k | break; |
799 | | |
800 | 1.08M | case M4V_VO_START_CODE: |
801 | 2.04M | default: |
802 | 2.04M | break; |
803 | | |
804 | 2.04M | case -1: |
805 | 209k | *size = gf_bs_get_position(m4v->bs) - *start; |
806 | 209k | return GF_EOS; |
807 | 3.72M | } |
808 | 3.51M | if (m4v->step_mode) |
809 | 1.54M | return GF_OK; |
810 | 3.51M | } |
811 | 412k | if (m4v->current_object_start < *start) return GF_NON_COMPLIANT_BITSTREAM; |
812 | 412k | *size = m4v->current_object_start - *start; |
813 | 412k | return GF_OK; |
814 | 412k | } |
815 | | |
816 | | GF_EXPORT |
817 | | GF_Err gf_m4v_parse_frame(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi, u8 *frame_type, u32 *time_inc, u64 *size, u64 *start, Bool *is_coded) |
818 | 3.14M | { |
819 | 3.14M | if (m4v->mpeg12) { |
820 | 970k | return gf_m4v_parse_frame_mpeg12(m4v, dsi, frame_type, time_inc, size, start, is_coded); |
821 | 970k | } |
822 | 2.16M | else { |
823 | 2.16M | return gf_m4v_parse_frame_mpeg4(m4v, dsi, frame_type, time_inc, size, start, is_coded); |
824 | 2.16M | } |
825 | 3.14M | } |
826 | | |
827 | | GF_Err gf_m4v_rewrite_par(u8 **o_data, u32 *o_dataLen, s32 par_n, s32 par_d) |
828 | 0 | { |
829 | 0 | u64 start, end, size; |
830 | 0 | GF_BitStream *mod; |
831 | 0 | GF_M4VParser *m4v; |
832 | 0 | Bool go = 1; |
833 | |
|
834 | 0 | m4v = gf_m4v_parser_new(*o_data, *o_dataLen, 0); |
835 | 0 | mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
836 | |
|
837 | 0 | start = 0; |
838 | 0 | while (go) { |
839 | 0 | u32 type = M4V_LoadObject(m4v); |
840 | |
|
841 | 0 | end = gf_bs_get_position(m4v->bs) - 4; |
842 | 0 | size = end - start; |
843 | | /*store previous object*/ |
844 | 0 | if (size) { |
845 | 0 | assert (size < (u64)1<<31); |
846 | 0 | gf_bs_write_data(mod, *o_data + start, (u32)size); |
847 | 0 | start = end; |
848 | 0 | } |
849 | |
|
850 | 0 | switch (type) { |
851 | 0 | case M4V_VOL_START_CODE: |
852 | 0 | gf_bs_write_int(mod, 0, 8); |
853 | 0 | gf_bs_write_int(mod, 0, 8); |
854 | 0 | gf_bs_write_int(mod, 1, 8); |
855 | 0 | gf_bs_write_int(mod, M4V_VOL_START_CODE, 8); |
856 | 0 | gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 1), 1); |
857 | 0 | gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 8), 8); |
858 | 0 | start = gf_bs_read_int(m4v->bs, 1); |
859 | 0 | gf_bs_write_int(mod, (u32)start, 1); |
860 | 0 | if (start) { |
861 | 0 | gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 7), 7); |
862 | 0 | } |
863 | 0 | start = gf_bs_read_int(m4v->bs, 4); |
864 | 0 | if (start == 0xF) { |
865 | 0 | gf_bs_read_int(m4v->bs, 8); |
866 | 0 | gf_bs_read_int(m4v->bs, 8); |
867 | 0 | } |
868 | 0 | if ((par_n >= 0) && (par_d >= 0)) { |
869 | 0 | u8 par = m4v_get_sar_idx(par_n, par_d); |
870 | 0 | gf_bs_write_int(mod, par, 4); |
871 | 0 | if (par == 0xF) { |
872 | 0 | gf_bs_write_int(mod, par_n, 8); |
873 | 0 | gf_bs_write_int(mod, par_d, 8); |
874 | 0 | } |
875 | 0 | } |
876 | 0 | else { |
877 | 0 | gf_bs_write_int(mod, 0x0, 4); |
878 | 0 | } |
879 | 0 | case -1: |
880 | 0 | go = 0; |
881 | 0 | break; |
882 | 0 | default: |
883 | 0 | break; |
884 | 0 | } |
885 | 0 | } |
886 | 0 | while (gf_bs_bits_available(m4v->bs)) { |
887 | 0 | u32 b = gf_bs_read_int(m4v->bs, 1); |
888 | 0 | gf_bs_write_int(mod, b, 1); |
889 | 0 | } |
890 | |
|
891 | 0 | gf_m4v_parser_del(m4v); |
892 | 0 | gf_free(*o_data); |
893 | 0 | gf_bs_get_content(mod, o_data, o_dataLen); |
894 | 0 | gf_bs_del(mod); |
895 | 0 | return GF_OK; |
896 | 0 | } |
897 | | |
898 | | GF_EXPORT |
899 | | u64 gf_m4v_get_object_start(GF_M4VParser *m4v) |
900 | 236k | { |
901 | 236k | return m4v->current_object_start; |
902 | 236k | } |
903 | | |
904 | | #if 0 //unused |
905 | | Bool gf_m4v_is_valid_object_type(GF_M4VParser *m4v) |
906 | | { |
907 | | return ((s32)m4v->current_object_type == -1) ? 0 : 1; |
908 | | } |
909 | | #endif |
910 | | |
911 | | |
912 | | GF_EXPORT |
913 | | GF_Err gf_m4v_get_config(u8 *rawdsi, u32 rawdsi_size, GF_M4VDecSpecInfo *dsi) |
914 | 3.84k | { |
915 | 3.84k | GF_Err e; |
916 | 3.84k | GF_M4VParser *vparse; |
917 | 3.84k | if (!rawdsi || !rawdsi_size) return GF_NON_COMPLIANT_BITSTREAM; |
918 | 3.84k | vparse = gf_m4v_parser_new(rawdsi, rawdsi_size, 0); |
919 | 3.84k | e = gf_m4v_parse_config(vparse, dsi); |
920 | 3.84k | dsi->next_object_start = (u32)vparse->current_object_start; |
921 | 3.84k | gf_m4v_parser_del(vparse); |
922 | 3.84k | return e < 0 ? e : GF_OK; |
923 | 3.84k | } |
924 | | |
925 | | GF_EXPORT |
926 | | GF_Err gf_mpegv12_get_config(u8 *rawdsi, u32 rawdsi_size, GF_M4VDecSpecInfo *dsi) |
927 | 0 | { |
928 | 0 | GF_Err e; |
929 | 0 | GF_M4VParser *vparse; |
930 | 0 | if (!rawdsi || !rawdsi_size) return GF_NON_COMPLIANT_BITSTREAM; |
931 | 0 | vparse = gf_m4v_parser_new(rawdsi, rawdsi_size, GF_TRUE); |
932 | 0 | e = gf_m4v_parse_config(vparse, dsi); |
933 | 0 | dsi->next_object_start = (u32)vparse->current_object_start; |
934 | 0 | gf_m4v_parser_del(vparse); |
935 | 0 | return e; |
936 | 0 | } |
937 | | |
938 | | #endif |
939 | | |
940 | | |
941 | | /* |
942 | | AAC parser |
943 | | */ |
944 | | |
945 | | struct __m4a_oti |
946 | | { |
947 | | u32 type; |
948 | | const char *name; |
949 | | } M4AObjectTypes[] = { |
950 | | {0, "MPEG-4 Audio Reserved"}, |
951 | | {1, "MPEG-4 Audio AAC Main"}, |
952 | | {2, "MPEG-4 Audio AAC LC"}, |
953 | | {3, "MPEG-4 Audio AAC SSR"}, |
954 | | {4, "MPEG-4 Audio AAC LTP"}, |
955 | | {5, "MPEG-4 Audio SBR"}, |
956 | | {6, "MPEG-4 Audio AAC Scalable"}, |
957 | | {7, "MPEG-4 Audio TwinVQ"}, |
958 | | {8, "MPEG-4 Audio CELP"}, |
959 | | {9, "MPEG-4 Audio HVXC"}, |
960 | | {10, "MPEG-4 Audio Reserved"}, |
961 | | {11, "MPEG-4 Audio Reserved"}, |
962 | | {12, "MPEG-4 Audio TTSI"}, |
963 | | {13, "MPEG-4 Audio Main synthetic"}, |
964 | | {14, "MPEG-4 Audio Wavetable synthesis"}, |
965 | | {15, "MPEG-4 Audio General MIDI"}, |
966 | | {16, "MPEG-4 Audio Algorithmic Synthesis and Audio FX"}, |
967 | | {17, "MPEG-4 Audio ER AAC LC"}, |
968 | | {18, "MPEG-4 Audio Reserved"}, |
969 | | {19, "MPEG-4 Audio ER AAC LTP"}, |
970 | | {20, "MPEG-4 Audio ER AAC scalable"}, |
971 | | {21, "MPEG-4 Audio ER TwinVQ"}, |
972 | | {22, "MPEG-4 Audio ER BSAC"}, |
973 | | {23, "MPEG-4 Audio ER AAC LD"}, |
974 | | {24, "MPEG-4 Audio ER CELP"}, |
975 | | {25, "MPEG-4 Audio ER HVXC"}, |
976 | | {26, "MPEG-4 Audio ER HILN"}, |
977 | | {27, "MPEG-4 Audio ER Parametric"}, |
978 | | {28, "MPEG-4 Audio SSC"}, |
979 | | {29, "MPEG-4 Audio ParametricStereo"}, |
980 | | {30, "MPEG-4 Audio Reserved"}, |
981 | | {31, "MPEG-4 Audio Reserved"}, |
982 | | {32, "MPEG-1 Audio Layer-1"}, |
983 | | {33, "MPEG-1 Audio Layer-2"}, |
984 | | {34, "MPEG-1 Audio Layer-3"}, |
985 | | {35, "MPEG-4 Audio DST"}, |
986 | | {36, "MPEG-4 Audio ALS"}, |
987 | | {37, "MPEG-4 Audio SLS"}, |
988 | | {42, "MPEG Audio xHE-AAC"}, |
989 | | }; |
990 | | |
991 | | GF_EXPORT |
992 | | const char *gf_m4a_object_type_name(u32 objectType) |
993 | 0 | { |
994 | 0 | u32 i, count = GF_ARRAY_LENGTH(M4AObjectTypes); |
995 | 0 | for (i=0; i<count; i++) { |
996 | 0 | if (objectType==M4AObjectTypes[i].type) |
997 | 0 | return M4AObjectTypes[i].name; |
998 | 0 | } |
999 | 0 | return "MPEG-4 Audio Unknown"; |
1000 | 0 | } |
1001 | | |
1002 | | struct __m4a_profile |
1003 | | { |
1004 | | u32 value; |
1005 | | const char *name; |
1006 | | } M4AProfiles[] = { |
1007 | | {0x00, "ISO Reserved (0x00)"}, |
1008 | | {0x01, "Main Audio Profile @ Level 1"}, |
1009 | | {0x02, "Main Audio Profile @ Level 2"}, |
1010 | | {0x03, "Main Audio Profile @ Level 3"}, |
1011 | | {0x04, "Main Audio Profile @ Level 4"}, |
1012 | | {0x05, "Scalable Audio Profile @ Level 1"}, |
1013 | | {0x06, "Scalable Audio Profile @ Level 2"}, |
1014 | | {0x07, "Scalable Audio Profile @ Level 3"}, |
1015 | | {0x08, "Scalable Audio Profile @ Level 4"}, |
1016 | | {0x09, "Speech Audio Profile @ Level 1"}, |
1017 | | {0x0A, "Speech Audio Profile @ Level 2"}, |
1018 | | {0x0B, "Synthetic Audio Profile @ Level 1"}, |
1019 | | {0x0C, "Synthetic Audio Profile @ Level 2"}, |
1020 | | {0x0D, "Synthetic Audio Profile @ Level 3"}, |
1021 | | {0x0E, "High Quality Audio Profile @ Level 1"}, |
1022 | | {0x0F, "High Quality Audio Profile @ Level 2"}, |
1023 | | {0x10, "High Quality Audio Profile @ Level 3"}, |
1024 | | {0x11, "High Quality Audio Profile @ Level 4"}, |
1025 | | {0x12, "High Quality Audio Profile @ Level 5"}, |
1026 | | {0x13, "High Quality Audio Profile @ Level 6"}, |
1027 | | {0x14, "High Quality Audio Profile @ Level 7"}, |
1028 | | {0x15, "High Quality Audio Profile @ Level 8"}, |
1029 | | {0x16, "Low Delay Audio Profile @ Level 1"}, |
1030 | | {0x17, "Low Delay Audio Profile @ Level 2"}, |
1031 | | {0x18, "Low Delay Audio Profile @ Level 3"}, |
1032 | | {0x19, "Low Delay Audio Profile @ Level 4"}, |
1033 | | {0x1A, "Low Delay Audio Profile @ Level 5"}, |
1034 | | {0x1B, "Low Delay Audio Profile @ Level 6"}, |
1035 | | {0x1C, "Low Delay Audio Profile @ Level 7"}, |
1036 | | {0x1D, "Low Delay Audio Profile @ Level 8"}, |
1037 | | {0x1E, "Natural Audio Profile @ Level 1"}, |
1038 | | {0x1F, "Natural Audio Profile @ Level 2"}, |
1039 | | {0x20, "Natural Audio Profile @ Level 3"}, |
1040 | | {0x21, "Natural Audio Profile @ Level 4"}, |
1041 | | {0x22, "Mobile Audio Internetworking Profile @ Level 1"}, |
1042 | | {0x23, "Mobile Audio Internetworking Profile @ Level 2"}, |
1043 | | {0x24, "Mobile Audio Internetworking Profile @ Level 3"}, |
1044 | | {0x25, "Mobile Audio Internetworking Profile @ Level 4"}, |
1045 | | {0x26, "Mobile Audio Internetworking Profile @ Level 5"}, |
1046 | | {0x27, "Mobile Audio Internetworking Profile @ Level 6"}, |
1047 | | {0x28, "AAC Profile @ Level 1"}, |
1048 | | {0x29, "AAC Profile @ Level 2"}, |
1049 | | {0x2A, "AAC Profile @ Level 4"}, |
1050 | | {0x2B, "AAC Profile @ Level 5"}, |
1051 | | {0x2C, "High Efficiency AAC Profile @ Level 2"}, |
1052 | | {0x2D, "High Efficiency AAC Profile @ Level 3"}, |
1053 | | {0x2E, "High Efficiency AAC Profile @ Level 4"}, |
1054 | | {0x2F, "High Efficiency AAC Profile @ Level 5"}, |
1055 | | {0x30, "High Efficiency AAC v2 Profile @ Level 2"}, |
1056 | | {0x31, "High Efficiency AAC v2 Profile @ Level 3"}, |
1057 | | {0x32, "High Efficiency AAC v2 Profile @ Level 4"}, |
1058 | | {0x33, "High Efficiency AAC v2 Profile @ Level 5"}, |
1059 | | {0x34, "Low Delay AAC Profile"}, |
1060 | | {0x35, "Baseline MPEG Surround Profile @ Level 1"}, |
1061 | | {0x36, "Baseline MPEG Surround Profile @ Level 2"}, |
1062 | | {0x37, "Baseline MPEG Surround Profile @ Level 3"}, |
1063 | | {0x38, "Baseline MPEG Surround Profile @ Level 4"}, |
1064 | | {0x39, "Baseline MPEG Surround Profile @ Level 5"}, |
1065 | | {0x3A, "Baseline MPEG Surround Profile @ Level 6"}, |
1066 | | {0x3B, "High Definition AAC Profile @ Level 1"}, |
1067 | | {0x3C, "ALS Simple Profile @ Level 1"}, |
1068 | | {0x50, "AAC Profile @ Level 6"}, |
1069 | | {0x51, "AAC Profile @ Level 7"}, |
1070 | | {0x52, "High Efficiency AAC Profile @ Level 6"}, |
1071 | | {0x53, "High Efficiency AAC Profile @ Level 7"}, |
1072 | | {0x54, "High Efficiency AAC v2 Profile @ Level 6"}, |
1073 | | {0x55, "High Efficiency AAC v2 Profile @ Level 7"}, |
1074 | | {0x56, "Extended High Efficiency AAC Profile @ Level 6"}, |
1075 | | {0x57, "Extended High Efficiency AAC Profile @ Level 7"}, |
1076 | | {0xFE, "Not part of MPEG-4 audio profiles"}, |
1077 | | {0xFF, "No audio capability required"} |
1078 | | }; |
1079 | | |
1080 | | GF_EXPORT |
1081 | | const char *gf_m4a_get_profile_name(u8 audio_pl) |
1082 | 0 | { |
1083 | 0 | u32 i, count = GF_ARRAY_LENGTH(M4AProfiles); |
1084 | 0 | for (i=0; i<count; i++) { |
1085 | 0 | if ((u32) audio_pl==M4AProfiles[i].value) |
1086 | 0 | return M4AProfiles[i].name; |
1087 | 0 | } |
1088 | 0 | return "ISO Reserved / User Private"; |
1089 | 0 | } |
1090 | | |
1091 | | #ifndef GPAC_DISABLE_AV_PARSERS |
1092 | | |
1093 | | GF_EXPORT |
1094 | | u32 gf_m4a_get_profile(GF_M4ADecSpecInfo *cfg) |
1095 | 703k | { |
1096 | 703k | switch (cfg->base_object_type) { |
1097 | 21.9k | case 2: /*AAC LC*/ |
1098 | 21.9k | if (cfg->nb_chan <= 2) |
1099 | 10.5k | return (cfg->base_sr <= 24000) ? 0x28 : 0x29; /*LC@L1 or LC@L2*/ |
1100 | 11.4k | if (cfg->nb_chan <= 5) |
1101 | 1.67k | return (cfg->base_sr <= 48000) ? 0x2A : 0x2B; /*LC@L4 or LC@L5*/ |
1102 | 9.75k | return (cfg->base_sr <= 48000) ? 0x50 : 0x51; /*LC@L4 or LC@L5*/ |
1103 | 43.3k | case 5: /*HE-AAC - SBR*/ |
1104 | 43.3k | if (cfg->nb_chan <= 2) |
1105 | 13.7k | return (cfg->base_sr <= 24000) ? 0x2C : 0x2D; /*HE@L2 or HE@L3*/ |
1106 | 29.6k | if (cfg->nb_chan <= 5) |
1107 | 17.7k | return (cfg->base_sr <= 48000) ? 0x2E : 0x2F; /*HE@L4 or HE@L5*/ |
1108 | 11.9k | return (cfg->base_sr <= 48000) ? 0x52 : 0x53; /*HE@L6 or HE@L7*/ |
1109 | 21.3k | case 29: /*HE-AACv2 - SBR+PS*/ |
1110 | 21.3k | if (cfg->nb_chan <= 2) |
1111 | 15.8k | return (cfg->base_sr <= 24000) ? 0x30 : 0x31; /*HE-AACv2@L2 or HE-AACv2@L3*/ |
1112 | 5.47k | if (cfg->nb_chan <= 5) |
1113 | 707 | return (cfg->base_sr <= 48000) ? 0x32 : 0x33; /*HE-AACv2@L4 or HE-AACv2@L5*/ |
1114 | 4.76k | return (cfg->base_sr <= 48000) ? 0x54 : 0x55; /*HE-AACv2@L6 or HE-AACv2@L7*/ |
1115 | | /*default to HQ*/ |
1116 | 617k | default: |
1117 | 617k | if (cfg->nb_chan <= 2) return (cfg->base_sr < 24000) ? 0x0E : 0x0F; /*HQ@L1 or HQ@L2*/ |
1118 | 229k | return 0x10; /*HQ@L3*/ |
1119 | 703k | } |
1120 | 703k | } |
1121 | | |
1122 | | GF_EXPORT |
1123 | | GF_Err gf_m4a_parse_program_config_element(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg) |
1124 | 253k | { |
1125 | 253k | u32 i; |
1126 | | |
1127 | 253k | cfg->program_config_element_present = 1; |
1128 | 253k | cfg->cpe_channels = 0; |
1129 | | |
1130 | 253k | cfg->element_instance_tag = gf_bs_read_int_log(bs, 4, "element_instance_tag"); |
1131 | 253k | cfg->object_type = gf_bs_read_int_log(bs, 2, "object_type"); |
1132 | 253k | cfg->sampling_frequency_index = gf_bs_read_int_log(bs, 4, "sampling_frequency_index"); |
1133 | 253k | cfg->num_front_channel_elements = gf_bs_read_int_log(bs, 4, "num_front_channel_elements"); |
1134 | 253k | cfg->num_side_channel_elements = gf_bs_read_int_log(bs, 4, "num_side_channel_elements"); |
1135 | 253k | cfg->num_back_channel_elements = gf_bs_read_int_log(bs, 4, "num_back_channel_elements"); |
1136 | 253k | cfg->num_lfe_channel_elements = gf_bs_read_int_log(bs, 2, "num_lfe_channel_elements"); |
1137 | 253k | cfg->num_assoc_data_elements = gf_bs_read_int_log(bs, 3, "num_assoc_data_elements"); |
1138 | 253k | cfg->num_valid_cc_elements = gf_bs_read_int_log(bs, 4, "num_valid_cc_elements"); |
1139 | 253k | cfg->mono_mixdown_present = (Bool)gf_bs_read_int_log(bs, 1, "mono_mixdown_present"); |
1140 | 253k | if (cfg->mono_mixdown_present) { |
1141 | 20.3k | cfg->mono_mixdown_element_number = gf_bs_read_int_log(bs, 4, "mono_mixdown_element_number"); |
1142 | 20.3k | } |
1143 | 253k | cfg->stereo_mixdown_present = gf_bs_read_int_log(bs, 1, "stereo_mixdown_present"); |
1144 | 253k | if (cfg->stereo_mixdown_present) { |
1145 | 20.7k | cfg->stereo_mixdown_element_number = gf_bs_read_int_log(bs, 4, "stereo_mixdown_element_number"); |
1146 | 20.7k | } |
1147 | 253k | cfg->matrix_mixdown_idx_present = gf_bs_read_int_log(bs, 1, "matrix_mixdown_idx_present"); |
1148 | 253k | if (cfg->matrix_mixdown_idx_present) { |
1149 | 23.7k | cfg->matrix_mixdown_idx = gf_bs_read_int_log(bs, 2, "matrix_mixdown_idx"); |
1150 | 23.7k | cfg->pseudo_surround_enable = gf_bs_read_int_log(bs, 1, "pseudo_surround_enable"); |
1151 | 23.7k | } |
1152 | 2.20M | for (i = 0; i < cfg->num_front_channel_elements; i++) { |
1153 | 1.95M | cfg->front_element_is_cpe[i] = gf_bs_read_int_log_idx(bs, 1, "front_element_is_cpe", i); |
1154 | 1.95M | cfg->front_element_tag_select[i] = gf_bs_read_int_log_idx(bs, 4, "front_element_tag_select", i); |
1155 | 1.95M | if (cfg->front_element_is_cpe[i]) cfg->cpe_channels++; |
1156 | 1.95M | } |
1157 | 2.22M | for (i = 0; i < cfg->num_side_channel_elements; i++) { |
1158 | 1.96M | cfg->side_element_is_cpe[i] = gf_bs_read_int_log_idx(bs, 1, "side_element_is_cpe", i); |
1159 | 1.96M | cfg->side_element_tag_select[i] = gf_bs_read_int_log_idx(bs, 4, "side_element_tag_select", i); |
1160 | 1.96M | if (cfg->side_element_is_cpe[i]) cfg->cpe_channels++; |
1161 | 1.96M | } |
1162 | 2.72M | for (i = 0; i < cfg->num_back_channel_elements; i++) { |
1163 | 2.47M | cfg->back_element_is_cpe[i] = gf_bs_read_int_log_idx(bs, 1, "back_element_is_cpe", i); |
1164 | 2.47M | cfg->back_element_tag_select[i] = gf_bs_read_int_log_idx(bs, 4, "back_element_tag_select", i); |
1165 | 2.47M | if (cfg->back_element_is_cpe[i]) cfg->cpe_channels++; |
1166 | 2.47M | } |
1167 | 722k | for (i = 0; i < cfg->num_lfe_channel_elements; i++) { |
1168 | 469k | cfg->lfe_element_tag_select[i] = gf_bs_read_int_log_idx(bs, 4, "lfe_element_tag_select", i); |
1169 | 469k | } |
1170 | 845k | for (i = 0; i < cfg->num_assoc_data_elements; i++) { |
1171 | 591k | cfg->assoc_data_element_tag_select[i] = gf_bs_read_int_log_idx(bs, 4, "assoc_data_element_tag_select", i); |
1172 | 591k | } |
1173 | | |
1174 | 1.84M | for (i = 0; i < cfg->num_valid_cc_elements; i++) { |
1175 | 1.59M | cfg->cc_element_is_ind_sw[i] = gf_bs_read_int_log_idx(bs, 1, "cc_element_is_ind_sw", i); |
1176 | 1.59M | cfg->valid_cc_element_tag_select[i] = gf_bs_read_int_log_idx(bs, 4, "valid_cc_element_tag_select", i); |
1177 | 1.59M | } |
1178 | 253k | gf_bs_align(bs); |
1179 | 253k | cfg->comment_field_bytes = gf_bs_read_int_log(bs, 8, "comment_field_bytes"); |
1180 | 253k | if (gf_bs_available(bs) < cfg->comment_field_bytes) return GF_NON_COMPLIANT_BITSTREAM; |
1181 | 252k | gf_bs_read_data(bs, (char *)cfg->comments, cfg->comment_field_bytes); |
1182 | | |
1183 | 252k | cfg->nb_chan = cfg->num_front_channel_elements + cfg->num_back_channel_elements + cfg->num_side_channel_elements + cfg->num_lfe_channel_elements; |
1184 | 252k | cfg->nb_chan += cfg->cpe_channels; |
1185 | | |
1186 | 252k | return GF_OK; |
1187 | 253k | } |
1188 | | |
1189 | | GF_EXPORT |
1190 | | GF_Err gf_m4a_parse_config(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg, Bool size_known) |
1191 | 698k | { |
1192 | 698k | u32 audio_obj_type; |
1193 | 698k | memset(cfg, 0, sizeof(GF_M4ADecSpecInfo)); |
1194 | 698k | cfg->base_object_type = gf_bs_read_int_log(bs, 5, "base_object_type"); |
1195 | | /*extended object type*/ |
1196 | 698k | if (cfg->base_object_type == 31) { |
1197 | 12.6k | cfg->base_object_type = 32 + gf_bs_read_int_log(bs, 6, "extended_base_object_type"); |
1198 | 12.6k | } |
1199 | 698k | cfg->base_sr_index = gf_bs_read_int_log(bs, 4, "base_samplerate_index"); |
1200 | 698k | if (cfg->base_sr_index == 0x0F) { |
1201 | 29.4k | cfg->base_sr = gf_bs_read_int_log(bs, 24, "base_samplerate"); |
1202 | 29.4k | } |
1203 | 669k | else { |
1204 | 669k | cfg->base_sr = GF_M4ASampleRates[cfg->base_sr_index]; |
1205 | 669k | } |
1206 | | |
1207 | 698k | cfg->chan_cfg = gf_bs_read_int_log(bs, 4, "channel_configuration"); |
1208 | 698k | if (cfg->chan_cfg) { |
1209 | 498k | cfg->nb_chan = GF_M4ANumChannels[cfg->chan_cfg - 1]; |
1210 | 498k | } |
1211 | | |
1212 | 698k | audio_obj_type = cfg->base_object_type; |
1213 | 698k | if (cfg->base_object_type == 5 || cfg->base_object_type == 29) { |
1214 | 64.7k | if (cfg->base_object_type == 29) { |
1215 | 21.3k | cfg->has_ps = 1; |
1216 | 21.3k | cfg->nb_chan = 1; |
1217 | 21.3k | } |
1218 | 64.7k | cfg->has_sbr = GF_TRUE; |
1219 | 64.7k | cfg->sbr_sr_index = gf_bs_read_int_log(bs, 4, "sbr_samplerate_index"); |
1220 | 64.7k | if (cfg->sbr_sr_index == 0x0F) { |
1221 | 3.88k | cfg->sbr_sr = gf_bs_read_int_log(bs, 24, "sbr_samplerate"); |
1222 | 3.88k | } |
1223 | 60.8k | else { |
1224 | 60.8k | cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index]; |
1225 | 60.8k | } |
1226 | 64.7k | cfg->sbr_object_type = gf_bs_read_int_log(bs, 5, "sbr_object_type"); |
1227 | 64.7k | if (cfg->sbr_object_type==31) |
1228 | 890 | cfg->sbr_object_type = 32 + gf_bs_read_int_log(bs, 6, "audioObjectTypeExt"); |
1229 | 64.7k | audio_obj_type = cfg->sbr_object_type; |
1230 | 64.7k | if (cfg->sbr_object_type==22) { |
1231 | 3.10k | /*ext_chan_cfg = */gf_bs_read_int_log(bs, 4, "channel_configuration"); |
1232 | 3.10k | } |
1233 | 64.7k | } |
1234 | | |
1235 | | /*object cfg*/ |
1236 | 698k | switch (audio_obj_type) { |
1237 | 30.7k | case 1: |
1238 | 53.2k | case 2: |
1239 | 79.8k | case 3: |
1240 | 94.3k | case 4: |
1241 | 105k | case 6: |
1242 | 117k | case 7: |
1243 | 128k | case 17: |
1244 | 138k | case 19: |
1245 | 153k | case 20: |
1246 | 164k | case 21: |
1247 | 181k | case 22: |
1248 | 213k | case 23: |
1249 | 215k | case 42: |
1250 | 215k | { |
1251 | 215k | Bool ext_flag; |
1252 | 215k | gf_bs_read_int_log(bs, 1, "frame_length_flag"); |
1253 | 215k | if (gf_bs_read_int_log(bs, 1, "depends_on_core_coder")) |
1254 | 77.9k | gf_bs_read_int_log(bs, 14, "delay"); |
1255 | 215k | ext_flag = gf_bs_read_int_log(bs, 1, "extension_flag"); |
1256 | | |
1257 | 215k | if (!cfg->chan_cfg) { |
1258 | 41.7k | GF_Err e = gf_m4a_parse_program_config_element(bs, cfg); |
1259 | 41.7k | if(e) return e; |
1260 | 41.7k | } |
1261 | | |
1262 | 215k | if ((cfg->base_object_type == 6) || (cfg->base_object_type == 20)) { |
1263 | 25.5k | gf_bs_read_int_log(bs, 3, "layerN"); |
1264 | 25.5k | } |
1265 | 215k | if (ext_flag) { |
1266 | 68.3k | if (cfg->base_object_type == 22) { |
1267 | 6.63k | gf_bs_read_int_log(bs, 5, "numOfSubFrame"); |
1268 | 6.63k | gf_bs_read_int_log(bs, 11, "layer_length"); |
1269 | 6.63k | } |
1270 | 68.3k | if ((cfg->base_object_type == 17) |
1271 | 68.3k | || (cfg->base_object_type == 19) |
1272 | 68.3k | || (cfg->base_object_type == 20) |
1273 | 68.3k | || (cfg->base_object_type == 23) |
1274 | 68.3k | ) { |
1275 | 13.3k | gf_bs_read_int_log(bs, 1, "aacSectionDataResilienceFlag"); |
1276 | 13.3k | gf_bs_read_int_log(bs, 1, "aacScalefactorDataResilienceFlag"); |
1277 | 13.3k | gf_bs_read_int_log(bs, 1, "aacSpectralDataResilienceFlag"); |
1278 | 13.3k | } |
1279 | 68.3k | gf_bs_read_int_log(bs, 1, "extensionFlag3"); |
1280 | 68.3k | } |
1281 | 215k | } |
1282 | 0 | break; |
1283 | 698k | } |
1284 | | /*ER cfg*/ |
1285 | 698k | switch (audio_obj_type) { |
1286 | 11.6k | case 17: |
1287 | 20.9k | case 19: |
1288 | 35.9k | case 20: |
1289 | 47.1k | case 21: |
1290 | 64.6k | case 22: |
1291 | 96.8k | case 23: |
1292 | 111k | case 24: |
1293 | 125k | case 25: |
1294 | 145k | case 26: |
1295 | 160k | case 27: |
1296 | 160k | { |
1297 | 160k | u32 epConfig = gf_bs_read_int_log(bs, 2, "epConfig"); |
1298 | 160k | if ((epConfig == 2) || (epConfig == 3)) { |
1299 | 42.4k | } |
1300 | 160k | if (epConfig == 3) { |
1301 | 22.9k | gf_bs_read_int_log(bs, 1, "directMapping"); |
1302 | 22.9k | } |
1303 | 160k | } |
1304 | 160k | break; |
1305 | 698k | } |
1306 | | |
1307 | 698k | if (size_known && (cfg->base_object_type != 5) && (cfg->base_object_type != 29)) { |
1308 | 13.0k | while (gf_bs_available(bs) >= 2) { |
1309 | 0 | u32 sync = gf_bs_peek_bits(bs, 11, 0); |
1310 | 0 | if (sync == 0x2b7) { |
1311 | 0 | gf_bs_read_int_log(bs, 11, "syncExtensionType"); |
1312 | 0 | cfg->sbr_object_type = gf_bs_read_int_log(bs, 5, "extensionAudioObjectType "); |
1313 | 0 | cfg->has_sbr = gf_bs_read_int_log(bs, 1, "sbrPresentFlag"); |
1314 | 0 | if (cfg->has_sbr) { |
1315 | 0 | cfg->sbr_sr_index = gf_bs_read_int_log(bs, 4, "extensionSamplingFrequencyIndex"); |
1316 | 0 | if (cfg->sbr_sr_index == 0x0F) { |
1317 | 0 | cfg->sbr_sr = gf_bs_read_int_log(bs, 24, "extensionSamplingFrequency"); |
1318 | 0 | } |
1319 | 0 | else { |
1320 | 0 | cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index]; |
1321 | 0 | } |
1322 | 0 | } |
1323 | 0 | } |
1324 | 0 | else if (sync == 0x548) { |
1325 | 0 | gf_bs_read_int_log(bs, 11, "syncExtensionType"); |
1326 | 0 | cfg->has_ps = gf_bs_read_int_log(bs, 1, "hasParametricStereo"); |
1327 | 0 | if (cfg->has_ps) |
1328 | 0 | cfg->nb_chan = 1; |
1329 | 0 | } |
1330 | 0 | else { |
1331 | 0 | break; |
1332 | 0 | } |
1333 | 0 | } |
1334 | 13.0k | } |
1335 | 698k | cfg->audioPL = gf_m4a_get_profile(cfg); |
1336 | 698k | return GF_OK; |
1337 | 698k | } |
1338 | | |
1339 | | GF_EXPORT |
1340 | | GF_Err gf_m4a_get_config(u8 *dsi, u32 dsi_size, GF_M4ADecSpecInfo *cfg) |
1341 | 0 | { |
1342 | 0 | GF_BitStream *bs; |
1343 | 0 | if (!dsi || !dsi_size || (dsi_size < 2)) return GF_NON_COMPLIANT_BITSTREAM; |
1344 | 0 | bs = gf_bs_new(dsi, dsi_size, GF_BITSTREAM_READ); |
1345 | 0 | gf_m4a_parse_config(bs, cfg, GF_TRUE); |
1346 | 0 | gf_bs_del(bs); |
1347 | 0 | return GF_OK; |
1348 | 0 | } |
1349 | | |
1350 | | u32 gf_latm_get_value(GF_BitStream *bs) |
1351 | 457k | { |
1352 | 457k | u32 i, tmp, value = 0; |
1353 | 457k | u32 bytesForValue = gf_bs_read_int(bs, 2); |
1354 | 1.43M | for (i = 0; i <= bytesForValue; i++) { |
1355 | 978k | value <<= 8; |
1356 | 978k | tmp = gf_bs_read_int(bs, 8); |
1357 | 978k | value += tmp; |
1358 | 978k | } |
1359 | 457k | return value; |
1360 | 457k | } |
1361 | | |
1362 | | GF_EXPORT |
1363 | | u32 gf_m4a_get_channel_cfg(u32 nb_chan) |
1364 | 8.46k | { |
1365 | 8.46k | u32 i, count = sizeof(GF_M4ANumChannels) / sizeof(u32); |
1366 | 52.6k | for (i = 0; i < count; i++) { |
1367 | 52.6k | if (GF_M4ANumChannels[i] == nb_chan) return i + 1; |
1368 | 52.6k | } |
1369 | 0 | return 0; |
1370 | 8.46k | } |
1371 | | |
1372 | | GF_EXPORT |
1373 | | GF_Err gf_m4a_write_program_config_element_bs(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg) |
1374 | 5.11k | { |
1375 | 5.11k | u32 i; |
1376 | 5.11k | gf_bs_write_int(bs, cfg->element_instance_tag, 4); |
1377 | 5.11k | gf_bs_write_int(bs, cfg->object_type, 2); |
1378 | 5.11k | gf_bs_write_int(bs, cfg->sampling_frequency_index, 4); |
1379 | 5.11k | gf_bs_write_int(bs, cfg->num_front_channel_elements, 4); |
1380 | 5.11k | gf_bs_write_int(bs, cfg->num_side_channel_elements, 4); |
1381 | 5.11k | gf_bs_write_int(bs, cfg->num_back_channel_elements, 4); |
1382 | 5.11k | gf_bs_write_int(bs, cfg->num_lfe_channel_elements, 2); |
1383 | 5.11k | gf_bs_write_int(bs, cfg->num_assoc_data_elements, 3); |
1384 | 5.11k | gf_bs_write_int(bs, cfg->num_valid_cc_elements, 4); |
1385 | 5.11k | gf_bs_write_int(bs, cfg->mono_mixdown_present, 1); |
1386 | 5.11k | if (cfg->mono_mixdown_present) { |
1387 | 1.55k | gf_bs_write_int(bs, cfg->mono_mixdown_element_number, 4); |
1388 | 1.55k | } |
1389 | 5.11k | gf_bs_write_int(bs, cfg->stereo_mixdown_present, 1); |
1390 | 5.11k | if (cfg->stereo_mixdown_present) { |
1391 | 3.46k | gf_bs_write_int(bs, cfg->stereo_mixdown_element_number, 4); |
1392 | 3.46k | } |
1393 | 5.11k | gf_bs_write_int(bs, cfg->matrix_mixdown_idx_present, 1); |
1394 | 5.11k | if (cfg->matrix_mixdown_idx_present) { |
1395 | 2.44k | gf_bs_write_int(bs, cfg->matrix_mixdown_idx, 2); |
1396 | 2.44k | gf_bs_write_int(bs, cfg->pseudo_surround_enable, 1); |
1397 | 2.44k | } |
1398 | 39.5k | for (i = 0; i < cfg->num_front_channel_elements; i++) { |
1399 | 34.4k | gf_bs_write_int(bs, cfg->front_element_is_cpe[i], 1); |
1400 | 34.4k | gf_bs_write_int(bs, cfg->front_element_tag_select[i], 4); |
1401 | 34.4k | } |
1402 | 18.2k | for (i = 0; i < cfg->num_side_channel_elements; i++) { |
1403 | 13.1k | gf_bs_write_int(bs, cfg->side_element_is_cpe[i], 1); |
1404 | 13.1k | gf_bs_write_int(bs, cfg->side_element_tag_select[i], 4); |
1405 | 13.1k | } |
1406 | 35.8k | for (i = 0; i < cfg->num_back_channel_elements; i++) { |
1407 | 30.6k | gf_bs_write_int(bs, cfg->back_element_is_cpe[i], 1); |
1408 | 30.6k | gf_bs_write_int(bs, cfg->back_element_tag_select[i], 4); |
1409 | 30.6k | } |
1410 | 13.0k | for (i = 0; i < cfg->num_lfe_channel_elements; i++) { |
1411 | 7.94k | gf_bs_write_int(bs, cfg->lfe_element_tag_select[i], 4); |
1412 | 7.94k | } |
1413 | 14.8k | for (i = 0; i < cfg->num_assoc_data_elements; i++) { |
1414 | 9.75k | gf_bs_write_int(bs, cfg->assoc_data_element_tag_select[i], 4); |
1415 | 9.75k | } |
1416 | | |
1417 | 47.5k | for (i = 0; i < cfg->num_valid_cc_elements; i++) { |
1418 | 42.4k | gf_bs_write_int(bs, cfg->cc_element_is_ind_sw[i], 1); |
1419 | 42.4k | gf_bs_write_int(bs, cfg->valid_cc_element_tag_select[i], 4); |
1420 | 42.4k | } |
1421 | 5.11k | gf_bs_align(bs); |
1422 | 5.11k | gf_bs_write_int(bs, cfg->comment_field_bytes, 8); |
1423 | 5.11k | gf_bs_write_data(bs, (char *)cfg->comments, cfg->comment_field_bytes); |
1424 | 5.11k | return GF_OK; |
1425 | 5.11k | } |
1426 | | |
1427 | | GF_EXPORT |
1428 | | GF_Err gf_m4a_write_config_bs(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg) |
1429 | 13.5k | { |
1430 | 13.5k | if (!cfg->base_sr_index) { |
1431 | 6.51k | if (!cfg->base_sr) return GF_BAD_PARAM; |
1432 | 6.51k | while (GF_M4ASampleRates[cfg->base_sr_index]) { |
1433 | 6.51k | if (GF_M4ASampleRates[cfg->base_sr_index] == cfg->base_sr) |
1434 | 6.51k | break; |
1435 | 0 | cfg->base_sr_index++; |
1436 | 0 | } |
1437 | 6.51k | } |
1438 | 13.5k | if (cfg->sbr_sr && !cfg->sbr_sr_index) { |
1439 | 550 | while (GF_M4ASampleRates[cfg->sbr_sr_index]) { |
1440 | 550 | if (GF_M4ASampleRates[cfg->sbr_sr_index] == cfg->sbr_sr) |
1441 | 550 | break; |
1442 | 0 | cfg->sbr_sr_index++; |
1443 | 0 | } |
1444 | 550 | } |
1445 | | /*extended object type*/ |
1446 | 13.5k | if (cfg->base_object_type >= 32) { |
1447 | 276 | gf_bs_write_int(bs, 31, 5); |
1448 | 276 | gf_bs_write_int(bs, cfg->base_object_type - 32, 6); |
1449 | 276 | } |
1450 | 13.3k | else { |
1451 | 13.3k | gf_bs_write_int(bs, cfg->base_object_type, 5); |
1452 | 13.3k | } |
1453 | 13.5k | gf_bs_write_int(bs, cfg->base_sr_index, 4); |
1454 | 13.5k | if (cfg->base_sr_index == 0x0F) { |
1455 | 0 | gf_bs_write_int(bs, cfg->base_sr, 24); |
1456 | 0 | } |
1457 | | |
1458 | 13.5k | if (cfg->program_config_element_present) { |
1459 | 5.11k | gf_bs_write_int(bs, 0, 4); |
1460 | 8.46k | } else { |
1461 | 8.46k | cfg->chan_cfg = gf_m4a_get_channel_cfg(cfg->nb_chan); |
1462 | 8.46k | if (!cfg->chan_cfg) { |
1463 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AAC] Cannot write decoder config, ProgramConfigElement is missing and channel configuration is not a predefined one !\n")); |
1464 | 0 | return GF_BAD_PARAM; |
1465 | 0 | } |
1466 | 8.46k | gf_bs_write_int(bs, cfg->chan_cfg, 4); |
1467 | 8.46k | } |
1468 | | |
1469 | 13.5k | if (cfg->base_object_type == 5 || cfg->base_object_type == 29) { |
1470 | 1.81k | if (cfg->base_object_type == 29) { |
1471 | 493 | cfg->has_ps = 1; |
1472 | 493 | cfg->nb_chan = 1; |
1473 | 493 | } |
1474 | 1.81k | cfg->has_sbr = 1; |
1475 | 1.81k | gf_bs_write_int(bs, cfg->sbr_sr_index, 4); |
1476 | 1.81k | if (cfg->sbr_sr_index == 0x0F) { |
1477 | 892 | gf_bs_write_int(bs, cfg->sbr_sr, 24); |
1478 | 892 | } |
1479 | 1.81k | gf_bs_write_int(bs, cfg->sbr_object_type, 5); |
1480 | 1.81k | } |
1481 | | |
1482 | | /*object cfg*/ |
1483 | 13.5k | switch (cfg->base_object_type) { |
1484 | 3.06k | case 1: |
1485 | 3.82k | case 2: |
1486 | 6.24k | case 3: |
1487 | 6.62k | case 4: |
1488 | 6.94k | case 6: |
1489 | 7.02k | case 7: |
1490 | 7.05k | case 17: |
1491 | 7.16k | case 19: |
1492 | 7.20k | case 20: |
1493 | 7.23k | case 21: |
1494 | 7.25k | case 22: |
1495 | 7.33k | case 23: |
1496 | 7.59k | case 42: |
1497 | 7.59k | { |
1498 | | /*frame length flag*/ |
1499 | 7.59k | gf_bs_write_int(bs, 0, 1); |
1500 | | /*depends on core coder*/ |
1501 | 7.59k | gf_bs_write_int(bs, 0, 1); |
1502 | | /*ext flag*/ |
1503 | 7.59k | gf_bs_write_int(bs, 0, 1); |
1504 | | |
1505 | 7.59k | if (cfg->program_config_element_present) { |
1506 | 5.11k | gf_m4a_write_program_config_element_bs(bs, cfg); |
1507 | 5.11k | } |
1508 | | |
1509 | 7.59k | if ((cfg->base_object_type == 6) || (cfg->base_object_type == 20)) { |
1510 | 357 | gf_bs_write_int(bs, 0, 3); |
1511 | 357 | } |
1512 | 7.59k | } |
1513 | 7.59k | break; |
1514 | 13.5k | } |
1515 | | /*ER cfg - not supported*/ |
1516 | | |
1517 | | /*implicit sbr/ps signaling not written here, cf reframe_adts*/ |
1518 | 13.5k | return GF_OK; |
1519 | 13.5k | } |
1520 | | |
1521 | | GF_EXPORT |
1522 | | GF_Err gf_m4a_write_config(GF_M4ADecSpecInfo *cfg, u8 **dsi, u32 *dsi_size) |
1523 | 8.60k | { |
1524 | 8.60k | GF_BitStream *bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
1525 | 8.60k | gf_m4a_write_config_bs(bs, cfg); |
1526 | 8.60k | gf_bs_get_content(bs, dsi, dsi_size); |
1527 | 8.60k | gf_bs_del(bs); |
1528 | 8.60k | return GF_OK; |
1529 | 8.60k | } |
1530 | | |
1531 | | |
1532 | | /*AV1 parsing*/ |
1533 | | |
1534 | | static u32 av1_read_ns(GF_BitStream *bs, u32 n, const char *fname) |
1535 | 482k | { |
1536 | 482k | u32 v, res; |
1537 | 482k | Bool extra_bit; |
1538 | 482k | int w = (u32)(log(n) / log(2)) + 1; |
1539 | 482k | u32 m = (1 << w) - n; |
1540 | 482k | gf_assert(w < 32); |
1541 | 482k | v = gf_bs_read_int(bs, w - 1); |
1542 | 482k | if (v < m) { |
1543 | 411k | if (fname) { |
1544 | 408k | gf_bs_log(bs, w-1, fname, v); |
1545 | 408k | } |
1546 | 411k | return v; |
1547 | 411k | } |
1548 | 71.4k | extra_bit = gf_bs_read_int(bs, 1); |
1549 | 71.4k | res = (v << 1) - m + extra_bit; |
1550 | 71.4k | if (fname) { |
1551 | 66.7k | gf_bs_log(bs, w, fname, res); |
1552 | 66.7k | } |
1553 | 71.4k | return res; |
1554 | 482k | } |
1555 | | |
1556 | | static void av1_color_config(GF_BitStream *bs, AV1State *state) |
1557 | 99.6k | { |
1558 | 99.6k | state->config->high_bitdepth = gf_bs_read_int_log(bs, 1, "high_bitdepth"); |
1559 | 99.6k | state->bit_depth = 8; |
1560 | 99.6k | if (state->config->seq_profile == 2 && state->config->high_bitdepth) { |
1561 | 6.94k | state->config->twelve_bit = gf_bs_read_int_log(bs, 1, "twelve_bit"); |
1562 | 6.94k | state->bit_depth = state->config->twelve_bit ? 12 : 10; |
1563 | 6.94k | } |
1564 | 92.7k | else if (state->config->seq_profile <= 2) { |
1565 | 78.5k | state->bit_depth = state->config->high_bitdepth ? 10 : 8; |
1566 | 78.5k | } |
1567 | | |
1568 | 99.6k | state->config->monochrome = GF_FALSE; |
1569 | 99.6k | if (state->config->seq_profile == 1) { |
1570 | 3.30k | state->config->monochrome = GF_FALSE; |
1571 | 3.30k | } |
1572 | 96.3k | else { |
1573 | 96.3k | state->config->monochrome = gf_bs_read_int_log(bs, 1, "monochrome"); |
1574 | 96.3k | } |
1575 | | /*NumPlanes = mono_chrome ? 1 : 3;*/ |
1576 | 99.6k | state->color_description_present_flag = gf_bs_read_int_log(bs, 1, "color_description_present_flag"); |
1577 | 99.6k | if (state->color_description_present_flag) { |
1578 | 10.4k | state->color_primaries = gf_bs_read_int_log(bs, 8, "color_primaries"); |
1579 | 10.4k | state->transfer_characteristics = gf_bs_read_int_log(bs, 8, "transfer_characteristics"); |
1580 | 10.4k | state->matrix_coefficients = gf_bs_read_int_log(bs, 8, "matrix_coefficients"); |
1581 | 10.4k | } |
1582 | 89.1k | else { |
1583 | 89.1k | state->color_primaries = 2/*CP_UNSPECIFIED*/; |
1584 | 89.1k | state->transfer_characteristics = 2/*TC_UNSPECIFIED*/; |
1585 | 89.1k | state->matrix_coefficients = 2/*MC_UNSPECIFIED*/; |
1586 | 89.1k | } |
1587 | 99.6k | if (state->config->monochrome) { |
1588 | 40.0k | state->color_range = gf_bs_read_int_log(bs, 1, "color_range"); |
1589 | 40.0k | state->config->chroma_subsampling_x = GF_TRUE; |
1590 | 40.0k | state->config->chroma_subsampling_y = GF_TRUE; |
1591 | 40.0k | state->config->chroma_sample_position = 0/*CSP_UNKNOWN*/; |
1592 | 40.0k | state->separate_uv_delta_q = 0; |
1593 | 40.0k | return; |
1594 | 40.0k | } |
1595 | 59.6k | else if (state->color_primaries == 0/*CP_BT_709*/ && |
1596 | 59.6k | state->transfer_characteristics == 13/*TC_SRGB*/ && |
1597 | 59.6k | state->matrix_coefficients == 0/*MC_IDENTITY*/) { |
1598 | 1 | state->color_range = GF_TRUE; |
1599 | 1 | state->config->chroma_subsampling_x = GF_FALSE; |
1600 | 1 | state->config->chroma_subsampling_y = GF_FALSE; |
1601 | 1 | } |
1602 | 59.6k | else { |
1603 | 59.6k | state->config->chroma_subsampling_x = GF_FALSE; |
1604 | 59.6k | state->config->chroma_subsampling_y = GF_FALSE; |
1605 | | |
1606 | 59.6k | state->color_range = gf_bs_read_int_log(bs, 1, "color_range"); |
1607 | 59.6k | if (state->config->seq_profile == 0) { |
1608 | 36.4k | state->config->chroma_subsampling_x = GF_TRUE; |
1609 | 36.4k | state->config->chroma_subsampling_y = GF_TRUE; |
1610 | 36.4k | } |
1611 | 23.1k | else if (state->config->seq_profile == 1) { |
1612 | 3.30k | state->config->chroma_subsampling_x = GF_FALSE; |
1613 | 3.30k | state->config->chroma_subsampling_y = GF_FALSE; |
1614 | 3.30k | } |
1615 | 19.8k | else { |
1616 | 19.8k | if (state->bit_depth == 12) { |
1617 | 6.45k | state->config->chroma_subsampling_x = gf_bs_read_int_log(bs, 1, "chroma_subsampling_x"); |
1618 | 6.45k | if (state->config->chroma_subsampling_x) |
1619 | 776 | state->config->chroma_subsampling_y = gf_bs_read_int_log(bs, 1, "chroma_subsampling_y"); |
1620 | 5.67k | else |
1621 | 5.67k | state->config->chroma_subsampling_y = GF_FALSE; |
1622 | 6.45k | } |
1623 | 13.4k | else { |
1624 | 13.4k | state->config->chroma_subsampling_x = GF_TRUE; |
1625 | 13.4k | state->config->chroma_subsampling_y = GF_FALSE; |
1626 | 13.4k | } |
1627 | 19.8k | } |
1628 | 59.6k | if (state->config->chroma_subsampling_x && state->config->chroma_subsampling_y) { |
1629 | 36.7k | state->config->chroma_sample_position = gf_bs_read_int_log(bs, 2, "chroma_sample_position"); |
1630 | 36.7k | } |
1631 | 59.6k | } |
1632 | 59.6k | state->separate_uv_delta_q = gf_bs_read_int_log(bs, 1, "separate_uv_delta_q"); |
1633 | 59.6k | } |
1634 | | |
1635 | | |
1636 | | static u32 av1_uvlc(GF_BitStream *bs, const char *fname) |
1637 | 1.53k | { |
1638 | 1.53k | u32 res; |
1639 | 1.53k | u8 leadingZeros = 0; |
1640 | 6.43k | while (1) { |
1641 | 6.43k | Bool done = gf_bs_read_int(bs, 1); |
1642 | 6.43k | if (done) |
1643 | 1.48k | break; |
1644 | 4.94k | leadingZeros++; |
1645 | | //avoid calls to bs_available or bs_is_overflow - see #2698 for poc |
1646 | 4.94k | if (leadingZeros >= 32) { |
1647 | 45 | return 0xFFFFFFFF; |
1648 | 45 | } |
1649 | 4.94k | } |
1650 | 1.48k | res = gf_bs_read_int(bs, leadingZeros) + ((u32)1 << leadingZeros) - 1; |
1651 | 1.48k | gf_bs_log(bs, 2*leadingZeros, fname, res); |
1652 | 1.48k | return res; |
1653 | 1.53k | } |
1654 | | |
1655 | 3.91k | static void timing_info(GF_BitStream *bs, AV1State *state) { |
1656 | 3.91k | u32 time_scale = 0; |
1657 | 3.91k | u32 num_units_in_display_tick = gf_bs_read_int_log(bs, 32, "num_units_in_display_tick"); |
1658 | 3.91k | if (num_units_in_display_tick == 0) { |
1659 | 52 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] num_units_in_display_tick must be greater than 0.\n")); |
1660 | 52 | } |
1661 | 3.91k | time_scale = gf_bs_read_int_log(bs, 32, "time_scale"); |
1662 | 3.91k | if (time_scale == 0) { |
1663 | 1.32k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] time_scale must be greater than 0.\n")); |
1664 | 1.32k | } |
1665 | 3.91k | state->equal_picture_interval = gf_bs_read_int_log(bs, 1, "equal_picture_interval"); |
1666 | 3.91k | if (state->equal_picture_interval) { |
1667 | 1.53k | u32 num_ticks_per_picture_minus_1 = av1_uvlc(bs, "num_ticks_per_picture_minus_1"); |
1668 | 1.53k | state->tb_num = time_scale; |
1669 | 1.53k | state->tb_den = (num_ticks_per_picture_minus_1 + 1)*num_units_in_display_tick; |
1670 | 1.53k | } |
1671 | 2.37k | else { |
1672 | 2.37k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] VFR not supported.\n")); |
1673 | | //TODO: upload num_units_in_display_tick (eq. to the POC in H264), compute delta between frames, set it as dts_inc in gf_import_aom_av1() |
1674 | 2.37k | } |
1675 | 3.91k | } |
1676 | | |
1677 | 1.58k | static void decoder_model_info(AV1State *state, GF_BitStream *bs) { |
1678 | 1.58k | state->buffer_delay_length = 1 + gf_bs_read_int_log(bs, 5, "buffer_delay_length_minus1"); |
1679 | 1.58k | gf_bs_read_int_log(bs, 32, "num_units_in_decoding_tick"); |
1680 | 1.58k | state->buffer_removal_time_length = gf_bs_read_int_log(bs, 5, "buffer_removal_time_length"); |
1681 | 1.58k | state->frame_presentation_time_length = 1 + gf_bs_read_int_log(bs, 5, "frame_presentation_time_length_minus1"); |
1682 | 1.58k | } |
1683 | | |
1684 | 2.63k | static void operating_parameters_info(GF_BitStream *bs, const u8 idx, const u8 buffer_delay_length_minus_1) { |
1685 | 2.63k | const u8 n = buffer_delay_length_minus_1 + 1; |
1686 | 2.63k | gf_bs_read_int_log(bs, n, "decoder_buffer_delay"); |
1687 | 2.63k | gf_bs_read_int_log(bs, n, "encoder_buffer_delay"); |
1688 | 2.63k | gf_bs_read_int_log(bs, 1, "low_delay_mode_flag"); |
1689 | 2.63k | } |
1690 | | |
1691 | | static void av1_parse_sequence_header_obu(GF_BitStream *bs, AV1State *state) |
1692 | 99.6k | { |
1693 | 99.6k | u8 buffer_delay_length_minus_1 = 0; |
1694 | 99.6k | state->frame_state.seen_seq_header = GF_TRUE; |
1695 | 99.6k | state->config->seq_profile = gf_bs_read_int_log(bs, 3, "seq_profile"); |
1696 | 99.6k | state->still_picture = gf_bs_read_int_log(bs, 1, "still_picture"); |
1697 | 99.6k | state->reduced_still_picture_header = gf_bs_read_int_log(bs, 1, "reduced_still_picture_header"); |
1698 | 99.6k | if (state->reduced_still_picture_header) { |
1699 | | //timing_info_present_flag = GF_FALSE; |
1700 | | //initial_display_delay_present_flag = GF_FALSE; |
1701 | 65.9k | state->operating_points_count = 1; |
1702 | 65.9k | state->config->seq_level_idx_0 = gf_bs_read_int_log(bs, 5, "seq_level_idx_0"); |
1703 | 65.9k | } |
1704 | 33.7k | else { |
1705 | 33.7k | u8 i = 0; |
1706 | 33.7k | Bool initial_display_delay_present_flag; |
1707 | 33.7k | Bool timing_info_present_flag = gf_bs_read_int_log(bs, 1, "timing_info_present_flag"); |
1708 | 33.7k | if (timing_info_present_flag) { |
1709 | 3.91k | timing_info(bs, state); |
1710 | 3.91k | state->decoder_model_info_present_flag = gf_bs_read_int_log(bs, 1, "decoder_model_info_present_flag"); |
1711 | 3.91k | if (state->decoder_model_info_present_flag) { |
1712 | 1.58k | decoder_model_info(state, bs); |
1713 | 1.58k | } |
1714 | 3.91k | } |
1715 | 29.8k | else { |
1716 | 29.8k | state->decoder_model_info_present_flag = GF_FALSE; |
1717 | 29.8k | } |
1718 | 33.7k | initial_display_delay_present_flag = gf_bs_read_int_log(bs, 1, "initial_display_delay_present_flag"); |
1719 | 33.7k | state->operating_points_count = 1 + gf_bs_read_int_log(bs, 5, "operating_points_count_minus1"); |
1720 | 173k | for (i = 0; i < state->operating_points_count; i++) { |
1721 | 140k | u8 seq_level_idx_i, seq_tier = 0; |
1722 | | |
1723 | 140k | state->operating_point_idc[i] = gf_bs_read_int_log_idx(bs, 12, "operating_point_idc", i); |
1724 | | |
1725 | 140k | seq_level_idx_i = gf_bs_read_int_log_idx(bs, 5, "seq_level_idx", i); |
1726 | 140k | if (i == 0) state->config->seq_level_idx_0 = seq_level_idx_i; |
1727 | | |
1728 | 140k | if (seq_level_idx_i > 7) { |
1729 | 52.6k | seq_tier = gf_bs_read_int_log_idx(bs, 1, "seq_tier", i); |
1730 | 52.6k | } |
1731 | 140k | if (i == 0) state->config->seq_tier_0 = seq_tier; |
1732 | | |
1733 | 140k | if (state->decoder_model_info_present_flag) { |
1734 | 7.82k | state->decoder_model_present_for_this_op[i] = gf_bs_read_int_log_idx(bs, 1, "decoder_model_present_for_this_op", i); |
1735 | 7.82k | if (state->decoder_model_present_for_this_op[i]) { |
1736 | 2.63k | operating_parameters_info(bs, i, buffer_delay_length_minus_1); |
1737 | 2.63k | } |
1738 | 7.82k | } |
1739 | 132k | else { |
1740 | 132k | state->decoder_model_present_for_this_op[i] = 0; |
1741 | 132k | } |
1742 | 140k | if (initial_display_delay_present_flag) { |
1743 | 45.1k | state->initial_display_delay_present_for_this_op[i] = gf_bs_read_int_log_idx(bs, 1, "initial_display_delay_present_for_this_op", i); |
1744 | 45.1k | if (state->initial_display_delay_present_for_this_op[i]) { |
1745 | 17.8k | state->initial_display_delay_minus1_for_this_op[i] = gf_bs_read_int_log_idx(bs, 4, "initial_display_delay_minus1", i); |
1746 | | // the procedure to compute the initial_presentation_delay (IPD) measured in ISOBMFF samples |
1747 | | // from the initial_display_delay (IDD) measured in decoded frames is a bit complex, |
1748 | | // and cannot be computed until samples have been formed |
1749 | | // except when the number of operating points is 1 (usual, non scalable case) |
1750 | | // and for the special case where IDD = 1 (low delay encodes) |
1751 | | // because that first frame is necessarily contained in the first sample, so IPD = 1 |
1752 | 17.8k | if (state->operating_points_count == 1 |
1753 | 17.8k | && state->initial_display_delay_minus1_for_this_op[i] == 0) { |
1754 | 297 | state->config->initial_presentation_delay_present = GF_TRUE; |
1755 | 297 | state->config->initial_presentation_delay_minus_one = 0; |
1756 | 297 | } |
1757 | 17.8k | } |
1758 | 45.1k | } |
1759 | 140k | } |
1760 | 33.7k | } |
1761 | | |
1762 | | //operatingPoint = av1_choose_operating_point(bs); |
1763 | 99.6k | state->OperatingPointIdc = 0;//TODO: operating_point_idc[operatingPoint]; |
1764 | | |
1765 | 99.6k | state->frame_width_bits_minus_1 = gf_bs_read_int_log(bs, 4, "frame_width_bits_minus1"); |
1766 | 99.6k | state->frame_height_bits_minus_1 = gf_bs_read_int_log(bs, 4, "frame_height_bits_minus1"); |
1767 | 99.6k | state->width = gf_bs_read_int_log(bs, state->frame_width_bits_minus_1 + 1, "width_minus1") + 1; |
1768 | 99.6k | state->height = gf_bs_read_int_log(bs, state->frame_height_bits_minus_1 + 1, "height_minus1") + 1; |
1769 | 99.6k | state->sequence_width = state->width; |
1770 | 99.6k | state->sequence_height = state->height; |
1771 | 99.6k | state->frame_id_numbers_present_flag = GF_FALSE; |
1772 | 99.6k | if (!state->reduced_still_picture_header) { |
1773 | 33.7k | state->frame_id_numbers_present_flag = gf_bs_read_int_log(bs, 1, "frame_id_numbers_present_flag"); |
1774 | 33.7k | } |
1775 | 99.6k | if (state->frame_id_numbers_present_flag) { |
1776 | 10.3k | state->delta_frame_id_length_minus_2 = gf_bs_read_int_log(bs, 4, "delta_frame_id_length_minus2"); |
1777 | 10.3k | state->additional_frame_id_length_minus_1 = gf_bs_read_int_log(bs, 3, "additional_frame_id_length_minus1"); |
1778 | 10.3k | } |
1779 | 99.6k | state->use_128x128_superblock = gf_bs_read_int_log(bs, 1, "use_128x128_superblock"); |
1780 | 99.6k | gf_bs_read_int_log(bs, 1, "enable_filter_intra"); |
1781 | 99.6k | gf_bs_read_int_log(bs, 1, "enable_intra_edge_filter"); |
1782 | 99.6k | if (state->reduced_still_picture_header) { |
1783 | | /*enable_interintra_compound = 0; |
1784 | | enable_masked_compound = 0; |
1785 | | enable_dual_filter = 0; |
1786 | | enable_jnt_comp = 0; |
1787 | | enable_ref_frame_mvs = 0;*/ |
1788 | 65.9k | state->enable_warped_motion = 0; |
1789 | 65.9k | state->enable_order_hint = GF_FALSE; |
1790 | 65.9k | state->OrderHintBits = 0; |
1791 | 65.9k | state->seq_force_integer_mv = 2/*SELECT_INTEGER_MV*/; |
1792 | 65.9k | state->seq_force_screen_content_tools = 2/*SELECT_SCREEN_CONTENT_TOOLS*/; |
1793 | 65.9k | } |
1794 | 33.7k | else { |
1795 | 33.7k | Bool seq_choose_screen_content_tools; |
1796 | 33.7k | gf_bs_read_int_log(bs, 1, "enable_interintra_compound"); |
1797 | 33.7k | gf_bs_read_int_log(bs, 1, "enable_masked_compound"); |
1798 | 33.7k | state->enable_warped_motion = gf_bs_read_int_log(bs, 1, "enable_warped_motion"); |
1799 | 33.7k | gf_bs_read_int_log(bs, 1, "enable_dual_filter"); |
1800 | 33.7k | state->enable_order_hint = gf_bs_read_int_log(bs, 1, "enable_order_hint"); |
1801 | 33.7k | if (state->enable_order_hint) { |
1802 | 18.9k | gf_bs_read_int_log(bs, 1, "enable_jnt_comp"); |
1803 | 18.9k | state->enable_ref_frame_mvs = gf_bs_read_int_log(bs, 1, "enable_ref_frame_mvs"); |
1804 | 18.9k | } |
1805 | 14.7k | else { |
1806 | 14.7k | /*enable_jnt_comp = 0*/; |
1807 | 14.7k | /*enable_ref_frame_mvs = 0*/; |
1808 | 14.7k | } |
1809 | 33.7k | seq_choose_screen_content_tools = gf_bs_read_int_log(bs, 1, "seq_choose_screen_content_tools"); |
1810 | 33.7k | state->seq_force_screen_content_tools = 0; |
1811 | 33.7k | if (seq_choose_screen_content_tools) { |
1812 | 9.65k | state->seq_force_screen_content_tools = 2/*SELECT_SCREEN_CONTENT_TOOLS*/; |
1813 | 9.65k | } |
1814 | 24.0k | else { |
1815 | 24.0k | state->seq_force_screen_content_tools = gf_bs_read_int_log(bs, 1, "seq_force_screen_content_tools"); |
1816 | 24.0k | } |
1817 | | |
1818 | 33.7k | state->seq_force_integer_mv = 0; |
1819 | 33.7k | if (state->seq_force_screen_content_tools > 0) { |
1820 | 18.8k | const Bool seq_choose_integer_mv = gf_bs_read_int_log(bs, 1, "seq_choose_integer_mv"); |
1821 | 18.8k | if (seq_choose_integer_mv) { |
1822 | 6.38k | state->seq_force_integer_mv = 2/*SELECT_INTEGER_MV*/; |
1823 | 6.38k | } |
1824 | 12.4k | else { |
1825 | 12.4k | state->seq_force_integer_mv = gf_bs_read_int_log(bs, 1, "seq_force_integer_mv"); |
1826 | 12.4k | } |
1827 | 18.8k | } |
1828 | 14.8k | else { |
1829 | 14.8k | state->seq_force_integer_mv = 2/*SELECT_INTEGER_MV*/; |
1830 | 14.8k | } |
1831 | 33.7k | if (state->enable_order_hint) { |
1832 | 18.9k | u8 order_hint_bits_minus_1 = gf_bs_read_int_log(bs, 3, "order_hint_bits_minus1"); |
1833 | 18.9k | state->OrderHintBits = order_hint_bits_minus_1 + 1; |
1834 | 18.9k | } |
1835 | 14.7k | else { |
1836 | 14.7k | state->OrderHintBits = 0; |
1837 | 14.7k | } |
1838 | 33.7k | } |
1839 | | |
1840 | 99.6k | state->enable_superres = gf_bs_read_int_log(bs, 1, "enable_superres"); |
1841 | 99.6k | state->enable_cdef = gf_bs_read_int_log(bs, 1, "enable_cdef"); |
1842 | 99.6k | state->enable_restoration = gf_bs_read_int_log(bs, 1, "enable_restoration"); |
1843 | 99.6k | av1_color_config(bs, state); |
1844 | 99.6k | state->film_grain_params_present = gf_bs_read_int_log(bs, 1, "film_grain_params_present"); |
1845 | 99.6k | } |
1846 | | |
1847 | | |
1848 | | |
1849 | 4.58k | #define IVF_FILE_HEADER_SIZE 32 |
1850 | | |
1851 | | Bool gf_media_probe_ivf(GF_BitStream *bs) |
1852 | 4.49k | { |
1853 | 4.49k | u32 dw = 0; |
1854 | 4.49k | if (gf_bs_available(bs) < IVF_FILE_HEADER_SIZE) return GF_FALSE; |
1855 | | |
1856 | 4.49k | dw = gf_bs_peek_bits(bs, 32, 0); |
1857 | 4.49k | if (dw != GF_4CC('D', 'K', 'I', 'F')) { |
1858 | 4.37k | return GF_FALSE; |
1859 | 4.37k | } |
1860 | 118 | return GF_TRUE; |
1861 | 4.49k | } |
1862 | | |
1863 | | GF_Err gf_media_parse_ivf_file_header(GF_BitStream *bs, u32 *width, u32 *height, u32 *codec_fourcc, u32 *timebase_num, u32 *timebase_den, u32 *num_frames) |
1864 | 46 | { |
1865 | 46 | u32 dw = 0; |
1866 | | |
1867 | 46 | if (!width || !height || !codec_fourcc || !timebase_den || !timebase_num || !num_frames) { |
1868 | 0 | gf_assert(0); |
1869 | 0 | return GF_BAD_PARAM; |
1870 | 0 | } |
1871 | | |
1872 | 46 | if (gf_bs_available(bs) < IVF_FILE_HEADER_SIZE) { |
1873 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Not enough bytes available ("LLU").\n", gf_bs_available(bs))); |
1874 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
1875 | 0 | } |
1876 | | |
1877 | 46 | dw = gf_bs_read_u32(bs); |
1878 | 46 | if (dw != GF_4CC('D', 'K', 'I', 'F')) { |
1879 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[IVF] Invalid signature\n")); |
1880 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
1881 | 0 | } |
1882 | | |
1883 | 46 | dw = gf_bs_read_u16_le(bs); |
1884 | 46 | if (dw != 0) { |
1885 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Wrong IVF version. 0 expected, got %u\n", dw)); |
1886 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
1887 | 0 | } |
1888 | | |
1889 | 46 | dw = gf_bs_read_u16_le(bs); //length of header in bytes |
1890 | 46 | if (dw != IVF_FILE_HEADER_SIZE) { |
1891 | 2 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Wrong IVF header length. Expected 32 bytes, got %u\n", dw)); |
1892 | 2 | return GF_NON_COMPLIANT_BITSTREAM; |
1893 | 2 | } |
1894 | | |
1895 | 44 | *codec_fourcc = gf_bs_read_u32(bs); |
1896 | | |
1897 | 44 | *width = gf_bs_read_u16_le(bs); |
1898 | 44 | *height = gf_bs_read_u16_le(bs); |
1899 | | |
1900 | 44 | *timebase_num = gf_bs_read_u32_le(bs); |
1901 | 44 | *timebase_den = gf_bs_read_u32_le(bs); |
1902 | | |
1903 | 44 | *num_frames = gf_bs_read_u32_le(bs); |
1904 | 44 | gf_bs_read_u32_le(bs); //skip unused |
1905 | | |
1906 | 44 | return GF_OK; |
1907 | 46 | } |
1908 | | |
1909 | | GF_Err gf_media_parse_ivf_frame_header(GF_BitStream *bs, u64 *frame_size, u64 *pts) |
1910 | 2.23k | { |
1911 | 2.23k | if (!frame_size) return GF_BAD_PARAM; |
1912 | 2.23k | if (gf_bs_available(bs) < 12) |
1913 | 0 | return GF_BUFFER_TOO_SMALL; |
1914 | | |
1915 | 2.23k | *frame_size = gf_bs_read_u32_le(bs); |
1916 | 2.23k | if (*frame_size > 256 * 1024 * 1024) { |
1917 | 345 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Wrong frame size %u\n", *frame_size)); |
1918 | 345 | *frame_size = 0; |
1919 | 345 | return GF_NON_COMPLIANT_BITSTREAM; |
1920 | 345 | } |
1921 | | |
1922 | 1.88k | *pts = gf_bs_read_u64_le(bs); |
1923 | | |
1924 | 1.88k | return GF_OK; |
1925 | 2.23k | } |
1926 | | |
1927 | | GF_Err gf_vp9_parse_superframe(GF_BitStream *bs, u64 ivf_frame_size, u32 *num_frames_in_superframe, u32 frame_sizes[VP9_MAX_FRAMES_IN_SUPERFRAME], u32 *superframe_index_size) |
1928 | 73 | { |
1929 | 73 | u32 byte, bytes_per_framesize; |
1930 | 73 | u64 pos = gf_bs_get_position(bs), i = 0; |
1931 | 73 | GF_Err e; |
1932 | | |
1933 | 73 | gf_assert(bs && num_frames_in_superframe); |
1934 | | |
1935 | | /*initialize like there is no superframe*/ |
1936 | 73 | memset(frame_sizes, 0, VP9_MAX_FRAMES_IN_SUPERFRAME * sizeof(frame_sizes[0])); |
1937 | 73 | *num_frames_in_superframe = 1; |
1938 | 73 | frame_sizes[0] = (u32)ivf_frame_size; |
1939 | 73 | *superframe_index_size = 0; |
1940 | | |
1941 | 73 | e = gf_bs_seek(bs, pos + ivf_frame_size - 1); |
1942 | 73 | if (e) return e; |
1943 | | |
1944 | 73 | gf_bs_mark_overflow(bs, GF_TRUE); |
1945 | | |
1946 | 73 | byte = gf_bs_read_u8(bs); |
1947 | 73 | if ((byte & 0xe0) != 0xc0) |
1948 | 67 | goto exit; /*no superframe*/ |
1949 | | |
1950 | 6 | bytes_per_framesize = 1 + ((byte & 0x18) >> 3); |
1951 | 6 | *num_frames_in_superframe = (u32)(1 + (byte & 0x7)); |
1952 | | |
1953 | | /*superframe_index()*/ |
1954 | 6 | *superframe_index_size = 2 + bytes_per_framesize * *num_frames_in_superframe; |
1955 | 6 | gf_bs_seek(bs, pos + ivf_frame_size - *superframe_index_size); |
1956 | 6 | byte = gf_bs_read_u8(bs); |
1957 | 6 | if ((byte & 0xe0) != 0xc0) { |
1958 | 3 | e = GF_NON_COMPLIANT_BITSTREAM; |
1959 | 3 | goto exit; /*no superframe*/ |
1960 | 3 | } |
1961 | 3 | frame_sizes[0] = 0; |
1962 | 27 | for (i = 0; i < *num_frames_in_superframe; ++i) { |
1963 | 24 | gf_bs_read_data(bs, (char*)(frame_sizes + i), bytes_per_framesize); |
1964 | 24 | } |
1965 | | |
1966 | 73 | exit: |
1967 | 73 | gf_bs_seek(bs, pos); |
1968 | | |
1969 | 73 | if (gf_bs_is_overflow(bs)) e = GF_NON_COMPLIANT_BITSTREAM; |
1970 | | |
1971 | 73 | return e; |
1972 | 3 | } |
1973 | | |
1974 | | |
1975 | | static Bool vp9_frame_sync_code(GF_BitStream *bs) |
1976 | 18 | { |
1977 | 18 | u8 val = gf_bs_read_int_log(bs, 8, "syncbyte1"); |
1978 | 18 | if (val != 0x49) |
1979 | 18 | return GF_FALSE; |
1980 | | |
1981 | 0 | val = gf_bs_read_int_log(bs, 8, "syncbyte2"); |
1982 | 0 | if (val != 0x83) |
1983 | 0 | return GF_FALSE; |
1984 | | |
1985 | 0 | val = gf_bs_read_int_log(bs, 8, "syncbyte3"); |
1986 | 0 | if (val != 0x42) |
1987 | 0 | return GF_FALSE; |
1988 | | |
1989 | 0 | return GF_TRUE; |
1990 | 0 | } |
1991 | | |
1992 | | typedef enum { |
1993 | | CS_UNKNOWN = 0, |
1994 | | CS_BT_601 = 1, |
1995 | | CS_BT_709 = 2, |
1996 | | CS_SMPTE_170 = 3, |
1997 | | CS_SMPTE_240 = 4, |
1998 | | CS_BT_2020 = 5, |
1999 | | CS_RESERVED = 6, |
2000 | | CS_RGB = 7, |
2001 | | } VP9_color_space; |
2002 | | |
2003 | | static const int VP9_CS_to_23001_8_colour_primaries[] = { 2/*unspecified*/, 5, 1, 6, 7, 9, -1/*reserved*/, 1 }; |
2004 | | static const int VP9_CS_to_23001_8_transfer_characteristics[] = { 2/*unspecified*/, 5, 1, 6, 7, 9, -1/*reserved*/, 13 }; |
2005 | | static const int VP9_CS_to_23001_8_matrix_coefficients[] = { 2/*unspecified*/, 6, 1, -1, -1, 9, -1/*reserved*/, 0 }; |
2006 | | |
2007 | | static GF_Err vp9_color_config(GF_BitStream *bs, GF_VPConfig *vp9_cfg) |
2008 | 0 | { |
2009 | 0 | VP9_color_space color_space; |
2010 | |
|
2011 | 0 | if (vp9_cfg->profile >= 2) { |
2012 | 0 | Bool ten_or_twelve_bit = gf_bs_read_int_log(bs, 1, "ten_or_twelve_bit"); |
2013 | 0 | vp9_cfg->bit_depth = ten_or_twelve_bit ? 12 : 10; |
2014 | 0 | } |
2015 | 0 | else { |
2016 | 0 | vp9_cfg->bit_depth = 8; |
2017 | 0 | } |
2018 | |
|
2019 | 0 | color_space = gf_bs_read_int_log(bs, 3, "color_space"); |
2020 | 0 | vp9_cfg->colour_primaries = VP9_CS_to_23001_8_colour_primaries[color_space]; |
2021 | 0 | vp9_cfg->transfer_characteristics = VP9_CS_to_23001_8_transfer_characteristics[color_space]; |
2022 | 0 | vp9_cfg->matrix_coefficients = VP9_CS_to_23001_8_matrix_coefficients[color_space]; |
2023 | 0 | if (color_space != CS_RGB) { |
2024 | 0 | vp9_cfg->video_fullRange_flag = gf_bs_read_int_log(bs, 1, "video_fullRange_flag"); |
2025 | 0 | if (vp9_cfg->profile == 1 || vp9_cfg->profile == 3) { |
2026 | 0 | u8 subsampling_x, subsampling_y, subsampling_xy_to_chroma_subsampling[2][2] = { {3, 0}, {2, 0} }; |
2027 | 0 | subsampling_x = gf_bs_read_int_log(bs, 1, "subsampling_x"); |
2028 | 0 | subsampling_y = gf_bs_read_int_log(bs, 1, "subsampling_x"); |
2029 | 0 | vp9_cfg->chroma_subsampling = subsampling_xy_to_chroma_subsampling[subsampling_x][subsampling_y]; |
2030 | 0 | Bool reserved_zero = gf_bs_read_int_log(bs, 1, "reserved_zero"); |
2031 | 0 | if (reserved_zero) { |
2032 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VP9] color config reserved zero (1) is not zero.\n")); |
2033 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2034 | 0 | } |
2035 | 0 | } |
2036 | 0 | else { |
2037 | 0 | vp9_cfg->chroma_subsampling = 0; |
2038 | 0 | } |
2039 | 0 | } |
2040 | 0 | else { |
2041 | 0 | vp9_cfg->video_fullRange_flag = GF_TRUE; |
2042 | 0 | if (vp9_cfg->profile == 1 || vp9_cfg->profile == 3) { |
2043 | 0 | vp9_cfg->chroma_subsampling = 3; |
2044 | 0 | Bool reserved_zero = gf_bs_read_int_log(bs, 1, "reserved_zero"); |
2045 | 0 | if (reserved_zero) { |
2046 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VP9] color config reserved zero (2) is not zero.\n")); |
2047 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2048 | 0 | } |
2049 | 0 | } |
2050 | 0 | } |
2051 | | |
2052 | 0 | return GF_OK; |
2053 | 0 | } |
2054 | | |
2055 | | static void vp9_compute_image_size(int FrameWidth, int FrameHeight, int *Sb64Cols, int *Sb64Rows) |
2056 | 36 | { |
2057 | 36 | int MiCols = (FrameWidth + 7) >> 3; |
2058 | 36 | int MiRows = (FrameHeight + 7) >> 3; |
2059 | 36 | *Sb64Cols = (MiCols + 7) >> 3; |
2060 | 36 | *Sb64Rows = (MiRows + 7) >> 3; |
2061 | 36 | } |
2062 | | |
2063 | | static void vp9_frame_size(GF_BitStream *bs, int *FrameWidth, int *FrameHeight, int *Sb64Cols, int *Sb64Rows) |
2064 | 23 | { |
2065 | 23 | int frame_width_minus_1 = gf_bs_read_int_log(bs, 16, "frame_width_minus_1"); |
2066 | 23 | int frame_height_minus_1 = gf_bs_read_int_log(bs, 16, "frame_height_minus_1"); |
2067 | 23 | if (frame_width_minus_1 + 1 != *FrameWidth || frame_height_minus_1 + 1 != *FrameHeight) { |
2068 | 11 | if (*FrameWidth || *FrameHeight) |
2069 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[VP9] inconsistent frame dimensions: previous was %dx%d, new one is %dx%d.\n", *FrameWidth, *FrameHeight, frame_width_minus_1 + 1, frame_height_minus_1 + 1)); |
2070 | 11 | } |
2071 | 23 | *FrameWidth = frame_width_minus_1 + 1; |
2072 | 23 | *FrameHeight = frame_height_minus_1 + 1; |
2073 | 23 | vp9_compute_image_size(*FrameWidth, *FrameHeight, Sb64Cols, Sb64Rows); |
2074 | 23 | } |
2075 | | |
2076 | | static void vp9_render_size(GF_BitStream *bs, int FrameWidth, int FrameHeight, int *renderWidth, int *renderHeight) |
2077 | 36 | { |
2078 | 36 | Bool render_and_frame_size_different = gf_bs_read_int_log(bs, 1, "render_and_frame_size_different"); |
2079 | 36 | if (render_and_frame_size_different == 1) { |
2080 | 29 | int render_width_minus_1 = gf_bs_read_int_log(bs, 16, "render_width_minus_1"); |
2081 | 29 | int render_height_minus_1 = gf_bs_read_int_log(bs, 16, "render_height_minus_1"); |
2082 | 29 | *renderWidth = render_width_minus_1 + 1; |
2083 | 29 | *renderHeight = render_height_minus_1 + 1; |
2084 | 29 | } |
2085 | 7 | else { |
2086 | 7 | *renderWidth = FrameWidth; |
2087 | 7 | *renderHeight = FrameHeight; |
2088 | 7 | } |
2089 | 36 | } |
2090 | | |
2091 | 0 | static s64 vp9_s(GF_BitStream *bs, int n, const char *fname, u32 idx) { |
2092 | 0 | s64 value = gf_bs_read_int(bs, n); |
2093 | 0 | Bool sign = gf_bs_read_int(bs, 1); |
2094 | 0 | if (sign) value = -value; |
2095 | 0 | gf_bs_log_idx(bs, n+1, fname, value, idx, -1, -1); |
2096 | 0 | return value; |
2097 | 0 | } |
2098 | | |
2099 | | static void vp9_loop_filter_params(GF_BitStream *bs) |
2100 | 36 | { |
2101 | 36 | /*loop_filter_level = */gf_bs_read_int_log(bs, 6, "loop_filter_level"); |
2102 | 36 | /*loop_filter_sharpness = */gf_bs_read_int_log(bs, 3, "loop_filter_sharpness"); |
2103 | 36 | Bool loop_filter_delta_enabled = gf_bs_read_int_log(bs, 1, "loop_filter_delta_enabled"); |
2104 | 36 | if (loop_filter_delta_enabled == 1) { |
2105 | 9 | Bool loop_filter_delta_update = gf_bs_read_int_log(bs, 1, "loop_filter_delta_update"); |
2106 | 9 | if (loop_filter_delta_update == GF_TRUE) { |
2107 | 0 | int i; |
2108 | 0 | for (i = 0; i < 4; i++) { |
2109 | 0 | Bool update_ref_delta = gf_bs_read_int_log_idx(bs, 1, "update_ref_delta", i); |
2110 | 0 | if (update_ref_delta == GF_TRUE) |
2111 | 0 | vp9_s(bs, 6, "loop_filter_ref_deltas", i); |
2112 | 0 | } |
2113 | 0 | for (i = 0; i < 2; i++) { |
2114 | 0 | Bool update_mode_delta = gf_bs_read_int_log_idx(bs, 1, "update_mode_delta", i); |
2115 | 0 | if (update_mode_delta == GF_TRUE) |
2116 | 0 | vp9_s(bs, 6, "loop_filter_mode_deltas", i); |
2117 | 0 | } |
2118 | 0 | } |
2119 | 9 | } |
2120 | 36 | } |
2121 | | |
2122 | 108 | static void vp9_delta_q(GF_BitStream *bs) { |
2123 | 108 | Bool delta_coded = gf_bs_read_int_log(bs, 1, "delta_coded"); |
2124 | 108 | if (delta_coded) { |
2125 | 25 | gf_bs_read_int_log(bs, 4, "delta_q"); |
2126 | 25 | } |
2127 | 108 | } |
2128 | | |
2129 | | static void vp9_quantization_params(GF_BitStream *bs) |
2130 | 36 | { |
2131 | 36 | /*base_q_idx = */gf_bs_read_int_log(bs, 8, "base_q_idx"); |
2132 | 36 | vp9_delta_q(bs); // delta_q_y_dc |
2133 | 36 | vp9_delta_q(bs); // delta_q_uv_dc |
2134 | 36 | vp9_delta_q(bs); // delta_q_uv_ac |
2135 | 36 | } |
2136 | | |
2137 | 0 | #define VP9_MAX_SEGMENTS 8 |
2138 | 0 | #define VP9_SEG_LVL_MAX 4 |
2139 | | static const int segmentation_feature_bits[VP9_SEG_LVL_MAX] = { 8, 6, 2, 0 }; |
2140 | | static const int segmentation_feature_signed[VP9_SEG_LVL_MAX] = { 1, 1, 0, 0 }; |
2141 | | |
2142 | 188 | #define VP9_MIN_TILE_WIDTH_B64 4 |
2143 | 119 | #define VP9_MAX_TILE_WIDTH_B64 64 |
2144 | | |
2145 | | static void vp9_read_prob(GF_BitStream *bs) |
2146 | 44 | { |
2147 | 44 | Bool prob_coded = gf_bs_read_int_log(bs, 1, "prob_coded"); |
2148 | 44 | if (prob_coded) { |
2149 | 16 | gf_bs_read_int_log(bs, 8, "prob"); |
2150 | 16 | } |
2151 | 44 | } |
2152 | | |
2153 | | static void vp9_segmentation_params(GF_BitStream *bs) |
2154 | 36 | { |
2155 | 36 | Bool segmentation_enabled = gf_bs_read_int_log(bs, 1, "segmentation_enabled"); |
2156 | 36 | if (segmentation_enabled == 1) { |
2157 | 5 | int i; |
2158 | 5 | Bool segmentation_update_map = gf_bs_read_int_log(bs, 1, "segmentation_update_map"); |
2159 | 5 | if (segmentation_update_map) { |
2160 | 40 | for (i = 0; i < 7; i++) { |
2161 | 35 | vp9_read_prob(bs); |
2162 | 35 | } |
2163 | 5 | Bool segmentation_temporal_update = gf_bs_read_int_log(bs, 1, "segmentation_temporal_update"); |
2164 | 20 | for (i = 0; i < 3; i++) { |
2165 | 15 | if (segmentation_temporal_update) { |
2166 | 9 | vp9_read_prob(bs); |
2167 | 9 | } |
2168 | 15 | } |
2169 | 5 | } |
2170 | 5 | Bool segmentation_update_data = gf_bs_read_int_log(bs, 1, "segmentation_update_data"); |
2171 | 5 | if (segmentation_update_data == 1) { |
2172 | 0 | /*segmentation_abs_or_delta_update =*/ gf_bs_read_int_log(bs, 1, "segmentation_abs_or_delta_update"); |
2173 | 0 | for (i = 0; i < VP9_MAX_SEGMENTS; i++) { |
2174 | 0 | int j; |
2175 | 0 | for (j = 0; j < VP9_SEG_LVL_MAX; j++) { |
2176 | | /*feature_value = 0*/ |
2177 | 0 | Bool feature_enabled = gf_bs_read_int_log(bs, 1, "feature_enabled"); |
2178 | | /*FeatureEnabled[i][j] = feature_enabled*/ |
2179 | 0 | if (feature_enabled) { |
2180 | 0 | int bits_to_read = segmentation_feature_bits[j]; |
2181 | 0 | /*feature_value =*/ gf_bs_read_int_log(bs, bits_to_read, "feature_value"); |
2182 | 0 | if (segmentation_feature_signed[j] == 1) { |
2183 | 0 | /*Bool feature_sign = */gf_bs_read_int_log(bs, 1, "feature_sign"); |
2184 | | /*if (feature_sign == 1) |
2185 | | feature_value *= -1*/ |
2186 | 0 | } |
2187 | 0 | } |
2188 | | /*FeatureData[i][j] = feature_value*/ |
2189 | 0 | } |
2190 | 0 | } |
2191 | 0 | } |
2192 | 5 | } |
2193 | 36 | } |
2194 | | |
2195 | 36 | static int calc_min_log2_tile_cols(int Sb64Cols) { |
2196 | 36 | int minLog2 = 0; |
2197 | 119 | while ((VP9_MAX_TILE_WIDTH_B64 << minLog2) < Sb64Cols) |
2198 | 83 | minLog2++; |
2199 | | |
2200 | 36 | return minLog2; |
2201 | 36 | } |
2202 | | |
2203 | 36 | static int calc_max_log2_tile_cols(int Sb64Cols) { |
2204 | 36 | int maxLog2 = 1; |
2205 | 188 | while ((Sb64Cols >> maxLog2) >= VP9_MIN_TILE_WIDTH_B64) |
2206 | 152 | maxLog2++; |
2207 | | |
2208 | 36 | return maxLog2 - 1; |
2209 | 36 | } |
2210 | | |
2211 | | static void vp9_tile_info(GF_BitStream *bs, int Sb64Cols) |
2212 | 36 | { |
2213 | 36 | Bool tile_rows_log2; |
2214 | 36 | int minLog2TileCols = calc_min_log2_tile_cols(Sb64Cols); |
2215 | 36 | int maxLog2TileCols = calc_max_log2_tile_cols(Sb64Cols); |
2216 | 36 | int tile_cols_log2 = minLog2TileCols; |
2217 | 78 | while (tile_cols_log2 < maxLog2TileCols) { |
2218 | 51 | Bool increment_tile_cols_log2 = gf_bs_read_int_log(bs, 1, "increment_tile_cols_log2"); |
2219 | 51 | if (increment_tile_cols_log2) |
2220 | 42 | tile_cols_log2++; |
2221 | 9 | else |
2222 | 9 | break; |
2223 | 51 | } |
2224 | 36 | tile_rows_log2 = gf_bs_read_int_log(bs, 1, "tile_rows_log2"); |
2225 | 36 | if (tile_rows_log2) { |
2226 | 14 | /*Bool increment_tile_rows_log2 = */gf_bs_read_int_log(bs, 1, "increment_tile_rows_log2"); |
2227 | | //tile_rows_log2 += increment_tile_rows_log2; |
2228 | 14 | } |
2229 | 36 | } |
2230 | | |
2231 | | static void vp9_frame_size_with_refs(GF_BitStream *bs, u8 refresh_frame_flags, u8 * ref_frame_idx, int * RefFrameWidth, int *RefFrameHeight, |
2232 | | int *FrameWidth, int *FrameHeight, int *RenderWidth, int *RenderHeight, int *Sb64Cols, int *Sb64Rows) |
2233 | 36 | { |
2234 | 36 | Bool found_ref; |
2235 | 36 | int i; |
2236 | 111 | for (i = 0; i < 3; i++) { |
2237 | 88 | found_ref = gf_bs_read_int_log(bs, 1, "found_ref"); |
2238 | 88 | if (found_ref) { |
2239 | 13 | *FrameWidth = RefFrameWidth [ref_frame_idx[i]]; |
2240 | 13 | *FrameHeight = RefFrameHeight[ref_frame_idx[i]]; |
2241 | 13 | break; |
2242 | 13 | } |
2243 | 88 | } |
2244 | 36 | if (found_ref == 0) { |
2245 | 23 | vp9_frame_size(bs, FrameWidth, FrameHeight, Sb64Cols, Sb64Rows); |
2246 | 23 | } |
2247 | 13 | else { |
2248 | 13 | vp9_compute_image_size(*FrameWidth, *FrameHeight, Sb64Cols, Sb64Rows); |
2249 | 13 | } |
2250 | | |
2251 | 36 | vp9_render_size(bs, *FrameWidth, *FrameHeight, RenderWidth, RenderHeight); |
2252 | 36 | } |
2253 | | |
2254 | | static void vp9_read_interpolation_filter(GF_BitStream *bs) |
2255 | 36 | { |
2256 | 36 | Bool is_filter_switchable = gf_bs_read_int_log(bs, 1, "is_filter_switchable"); |
2257 | 36 | if (!is_filter_switchable) { |
2258 | 9 | /*raw_interpolation_filter = */gf_bs_read_int_log(bs, 2, "raw_interpolation_filter"); |
2259 | 9 | } |
2260 | 36 | } |
2261 | | |
2262 | | |
2263 | 54 | #define VP9_KEY_FRAME 0 |
2264 | | |
2265 | | GF_Err gf_vp9_parse_sample(GF_BitStream *bs, GF_VPConfig *vp9_cfg, Bool *key_frame, u32 *FrameWidth, u32 *FrameHeight, u32 *renderWidth, u32 *renderHeight) |
2266 | 84 | { |
2267 | 84 | Bool FrameIsIntra = GF_FALSE, profile_low_bit, profile_high_bit, show_existing_frame = GF_FALSE, frame_type = GF_FALSE, show_frame = GF_FALSE, error_resilient_mode = GF_FALSE; |
2268 | 84 | /*u8 frame_context_idx = 0, reset_frame_context = 0, frame_marker = 0*/; |
2269 | 84 | int Sb64Cols = 0, Sb64Rows = 0, i; |
2270 | 84 | u8 refresh_frame_flags = 0; |
2271 | | |
2272 | 84 | gf_assert(bs && key_frame); |
2273 | | |
2274 | | /*uncompressed header*/ |
2275 | 84 | /*frame_marker = */gf_bs_read_int_log(bs, 2, "frame_marker"); |
2276 | 84 | profile_low_bit = gf_bs_read_int_log(bs, 1, "profile_low_bit"); |
2277 | 84 | profile_high_bit = gf_bs_read_int_log(bs, 1, "profile_high_bit"); |
2278 | 84 | vp9_cfg->profile = (profile_high_bit << 1) + profile_low_bit; |
2279 | 84 | if (vp9_cfg->profile == 3) { |
2280 | 13 | Bool reserved_zero = gf_bs_read_int_log(bs, 1, "reserved_zero"); |
2281 | 13 | if (reserved_zero) { |
2282 | 6 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VP9] uncompressed header reserved zero is not zero.\n")); |
2283 | 6 | return GF_NON_COMPLIANT_BITSTREAM; |
2284 | 6 | } |
2285 | 13 | } |
2286 | | |
2287 | 78 | show_existing_frame = gf_bs_read_int_log(bs, 1, "show_existing_frame"); |
2288 | 78 | if (show_existing_frame == GF_TRUE) { |
2289 | 24 | /*frame_to_show_map_idx = */gf_bs_read_int_log(bs, 3, "frame_to_show_map_idx"); |
2290 | 24 | return GF_OK; |
2291 | 24 | } |
2292 | | |
2293 | 54 | frame_type = gf_bs_read_int_log(bs, 1, "frame_type"); |
2294 | 54 | show_frame = gf_bs_read_int_log(bs, 1, "show_frame"); |
2295 | 54 | error_resilient_mode = gf_bs_read_int_log(bs, 1, "error_resilient_mode"); |
2296 | 54 | if (frame_type == VP9_KEY_FRAME) { |
2297 | 18 | if (!vp9_frame_sync_code(bs)) |
2298 | 18 | return GF_NON_COMPLIANT_BITSTREAM; |
2299 | 0 | if (vp9_color_config(bs, vp9_cfg) != GF_OK) |
2300 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2301 | 0 | vp9_frame_size(bs, FrameWidth, FrameHeight, &Sb64Cols, &Sb64Rows); |
2302 | 0 | vp9_render_size(bs, *FrameWidth, *FrameHeight, renderWidth, renderHeight); |
2303 | 0 | refresh_frame_flags = 0xFF; |
2304 | 0 | *key_frame = GF_TRUE; |
2305 | 0 | FrameIsIntra = GF_TRUE; |
2306 | 0 | } |
2307 | 36 | else { |
2308 | 36 | Bool intra_only = GF_FALSE; |
2309 | 36 | *key_frame = GF_FALSE; |
2310 | | |
2311 | 36 | if (show_frame == GF_FALSE) { |
2312 | 0 | intra_only = gf_bs_read_int_log(bs, 1, "intra_only"); |
2313 | 0 | } |
2314 | 36 | FrameIsIntra = intra_only; |
2315 | | |
2316 | 36 | if (error_resilient_mode == GF_FALSE) { |
2317 | 7 | /*reset_frame_context = */gf_bs_read_int_log(bs, 2, "reset_frame_context"); |
2318 | 7 | } |
2319 | | |
2320 | 36 | if (intra_only == GF_TRUE) { |
2321 | 0 | if (!vp9_frame_sync_code(bs)) |
2322 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2323 | | |
2324 | 0 | if (vp9_cfg->profile > 0) { |
2325 | 0 | if (vp9_color_config(bs, vp9_cfg) != GF_OK) |
2326 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2327 | 0 | } |
2328 | 0 | else { |
2329 | 0 | u8 color_space = CS_BT_601; |
2330 | 0 | vp9_cfg->colour_primaries = VP9_CS_to_23001_8_colour_primaries[color_space]; |
2331 | 0 | vp9_cfg->transfer_characteristics = VP9_CS_to_23001_8_transfer_characteristics[color_space]; |
2332 | 0 | vp9_cfg->matrix_coefficients = VP9_CS_to_23001_8_matrix_coefficients[color_space]; |
2333 | 0 | vp9_cfg->chroma_subsampling = 0; |
2334 | 0 | vp9_cfg->bit_depth = 8; |
2335 | 0 | } |
2336 | 0 | refresh_frame_flags = gf_bs_read_int_log(bs, 8, "refresh_frame_flags"); |
2337 | 0 | vp9_frame_size(bs, FrameWidth, FrameHeight, &Sb64Cols, &Sb64Rows); |
2338 | 0 | vp9_render_size(bs, *FrameWidth, *FrameHeight, renderWidth, renderHeight); |
2339 | 0 | } |
2340 | 36 | else { |
2341 | 36 | refresh_frame_flags = gf_bs_read_int_log(bs, 8, "refresh_frame_flags"); |
2342 | 36 | u8 ref_frame_idx[3]; |
2343 | 144 | for (i = 0; i < 3; i++) { |
2344 | 108 | ref_frame_idx[i] = gf_bs_read_int_log_idx(bs, 3, "ref_frame_idx", i); |
2345 | 108 | /*ref_frame_sign_bias[LAST_FRAME + i] = */gf_bs_read_int_log_idx(bs, 1, "ref_frame_sign_bias", i); |
2346 | 108 | } |
2347 | 36 | vp9_frame_size_with_refs(bs, refresh_frame_flags, ref_frame_idx, vp9_cfg->RefFrameWidth, vp9_cfg->RefFrameHeight, FrameWidth, FrameHeight, renderWidth, renderHeight, &Sb64Cols, &Sb64Rows); |
2348 | 36 | /*allow_high_precision_mv = */gf_bs_read_int_log(bs, 1, "allow_high_precision_mv"); |
2349 | 36 | vp9_read_interpolation_filter(bs); |
2350 | 36 | } |
2351 | 36 | } |
2352 | | |
2353 | 36 | if (error_resilient_mode == 0) { |
2354 | 7 | /*refresh_frame_context = */gf_bs_read_int_log(bs, 1, "refresh_frame_context"); |
2355 | 7 | /*frame_parallel_decoding_mode = */gf_bs_read_int_log(bs, 1, "frame_parallel_decoding_mode"); |
2356 | 7 | } |
2357 | | |
2358 | 36 | /*frame_context_idx = */gf_bs_read_int_log(bs, 2, "frame_context_idx"); |
2359 | 36 | if (FrameIsIntra || error_resilient_mode) { |
2360 | | /*setup_past_independence + save_probs ...*/ |
2361 | | //frame_context_idx = 0; |
2362 | 29 | } |
2363 | | |
2364 | 36 | vp9_loop_filter_params(bs); |
2365 | 36 | vp9_quantization_params(bs); |
2366 | 36 | vp9_segmentation_params(bs); |
2367 | 36 | vp9_tile_info(bs, Sb64Cols); |
2368 | | |
2369 | 36 | /*header_size_in_bytes = */gf_bs_read_int_log(bs, 16, "header_size_in_bytes"); |
2370 | | |
2371 | | /*Reference frame update process (8.10 - partial)*/ |
2372 | 324 | for (i = 0; i < VP9_NUM_REF_FRAMES; i++) { |
2373 | 288 | if ((refresh_frame_flags >> i) & 1) { |
2374 | 133 | vp9_cfg->RefFrameWidth[i] = *FrameWidth; |
2375 | 133 | vp9_cfg->RefFrameHeight[i] = *FrameHeight; |
2376 | 133 | } |
2377 | 288 | } |
2378 | | |
2379 | 36 | return GF_OK; |
2380 | 54 | } |
2381 | | |
2382 | | GF_EXPORT |
2383 | | GF_Err gf_av1_parse_obu_header(GF_BitStream *bs, ObuType *obu_type, Bool *obu_extension_flag, Bool *obu_has_size_field, u8 *temporal_id, u8 *spatial_id) |
2384 | 1.62M | { |
2385 | 1.62M | Bool forbidden = gf_bs_read_int(bs, 1); |
2386 | 1.62M | if (forbidden) { |
2387 | 2.49k | return GF_NON_COMPLIANT_BITSTREAM; |
2388 | 2.49k | } |
2389 | | |
2390 | 1.62M | *obu_type = gf_bs_read_int(bs, 4); |
2391 | 1.62M | *obu_extension_flag = gf_bs_read_int(bs, 1); |
2392 | 1.62M | *obu_has_size_field = gf_bs_read_int(bs, 1); |
2393 | 1.62M | if (gf_bs_read_int(bs, 1) /*obu_reserved_1bit*/) { |
2394 | 4.61k | return GF_NON_COMPLIANT_BITSTREAM; |
2395 | 4.61k | } |
2396 | 1.62M | if (*obu_extension_flag) { |
2397 | 235k | *temporal_id = gf_bs_read_int(bs, 3); |
2398 | 235k | *spatial_id = gf_bs_read_int(bs, 2); |
2399 | 235k | /*extension_header_reserved_3bits = */gf_bs_read_int(bs, 3); |
2400 | 235k | } |
2401 | | |
2402 | 1.62M | return GF_OK; |
2403 | 1.62M | } |
2404 | | |
2405 | | #endif // GPAC_DISABLE_AV_PARSERS |
2406 | | |
2407 | | GF_EXPORT |
2408 | | const char *gf_av1_get_obu_name(ObuType obu_type) |
2409 | 288k | { |
2410 | 288k | switch (obu_type) { |
2411 | 24.9k | case OBU_SEQUENCE_HEADER: return "seq_header"; |
2412 | 0 | case OBU_TEMPORAL_DELIMITER: return "delimiter"; |
2413 | 125 | case OBU_FRAME_HEADER: return "frame_header"; |
2414 | 2.19k | case OBU_TILE_GROUP: return "tile_group"; |
2415 | 252k | case OBU_METADATA: return "metadata"; |
2416 | 2.76k | case OBU_FRAME: return "frame"; |
2417 | 0 | case OBU_REDUNDANT_FRAME_HEADER: return "redundant_frame_header"; |
2418 | 13 | case OBU_TILE_LIST: return "tile_list"; |
2419 | 0 | case OBU_PADDING: return "padding"; |
2420 | 5.63k | case OBU_RESERVED_0: |
2421 | 5.63k | case OBU_RESERVED_9: |
2422 | 5.64k | case OBU_RESERVED_10: |
2423 | 5.65k | case OBU_RESERVED_11: |
2424 | 5.65k | case OBU_RESERVED_12: |
2425 | 5.66k | case OBU_RESERVED_13: |
2426 | 5.67k | case OBU_RESERVED_14: |
2427 | 5.67k | return "reserved"; |
2428 | 0 | default: return "unknown"; |
2429 | 288k | } |
2430 | 288k | } |
2431 | | |
2432 | 710k | Bool av1_is_obu_header(ObuType obu_type) { |
2433 | 710k | switch (obu_type) { |
2434 | 80.5k | case OBU_SEQUENCE_HEADER: |
2435 | 563k | case OBU_METADATA: |
2436 | | // TODO add check based on the metadata type |
2437 | 563k | return GF_TRUE; |
2438 | 146k | default: |
2439 | 146k | return GF_FALSE; |
2440 | 710k | } |
2441 | 710k | } |
2442 | | |
2443 | | #ifndef GPAC_DISABLE_AV_PARSERS |
2444 | | |
2445 | | static Bool av1_is_obu_frame(AV1State *state, ObuType obu_type) |
2446 | 319k | { |
2447 | 319k | switch (obu_type) { |
2448 | 9 | case OBU_PADDING: |
2449 | 240 | case OBU_REDUNDANT_FRAME_HEADER: |
2450 | 240 | return GF_FALSE; |
2451 | 13.2k | case OBU_TEMPORAL_DELIMITER: |
2452 | 13.2k | return state->keep_temporal_delim ? GF_TRUE : GF_FALSE; |
2453 | 305k | default: |
2454 | 305k | return GF_TRUE; |
2455 | 319k | } |
2456 | 319k | } |
2457 | | |
2458 | | GF_EXPORT |
2459 | 1.91M | u64 gf_av1_leb128_read(GF_BitStream *bs, u8 *opt_Leb128Bytes) { |
2460 | 1.91M | u64 value = 0; |
2461 | 1.91M | u8 Leb128Bytes = 0, i = 0; |
2462 | 1.95M | for (i = 0; i < 8; i++) { |
2463 | 1.95M | u8 leb128_byte = gf_bs_read_u8(bs); |
2464 | 1.95M | value |= ( ((u64) (leb128_byte & 0x7f)) << (i * 7)); |
2465 | 1.95M | Leb128Bytes += 1; |
2466 | 1.95M | if (!(leb128_byte & 0x80)) { |
2467 | 1.91M | break; |
2468 | 1.91M | } |
2469 | 1.95M | } |
2470 | | |
2471 | 1.91M | if (opt_Leb128Bytes) { |
2472 | 9.08k | *opt_Leb128Bytes = Leb128Bytes; |
2473 | 9.08k | } |
2474 | 1.91M | return value; |
2475 | 1.91M | } |
2476 | | |
2477 | | u32 gf_av1_leb128_size(u64 value) |
2478 | 1.97k | { |
2479 | 1.97k | u32 gf_av1_leb128_size = 0; |
2480 | 2.90k | do { |
2481 | 2.90k | ++gf_av1_leb128_size; |
2482 | 2.90k | } while ((value >>= 7) != 0); |
2483 | | |
2484 | 1.97k | return gf_av1_leb128_size; |
2485 | 1.97k | } |
2486 | | |
2487 | | u64 gf_av1_leb128_write(GF_BitStream *bs, u64 value) |
2488 | 987 | { |
2489 | 987 | u32 i, leb_size = gf_av1_leb128_size(value); |
2490 | 2.44k | for (i = 0; i < leb_size; ++i) { |
2491 | 1.45k | u8 byte = value & 0x7f; |
2492 | 1.45k | value >>= 7; |
2493 | 1.45k | if (value != 0) byte |= 0x80; //more bytes follow |
2494 | 1.45k | gf_bs_write_u8(bs, byte); |
2495 | 1.45k | } |
2496 | | |
2497 | 987 | return leb_size; |
2498 | 987 | } |
2499 | | |
2500 | 230k | #define OBU_BLOCK_SIZE 4096 |
2501 | | static void av1_add_obu_internal(GF_BitStream *bs, u64 pos, u64 obu_length, ObuType obu_type, GF_List **obu_list, AV1State *state) |
2502 | 643k | { |
2503 | 643k | char block[OBU_BLOCK_SIZE]; |
2504 | 643k | Bool has_size_field = 0, obu_extension_flag = 0; |
2505 | 643k | u8 temporal_id, spatial_id; |
2506 | 643k | GF_AV1_OBUArrayEntry *a = NULL; |
2507 | | |
2508 | 643k | if (state && state->mem_mode) { |
2509 | 228k | if (!state->bs) { |
2510 | 213 | state->bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
2511 | 228k | } else { |
2512 | 228k | gf_bs_reassign_buffer(state->bs, state->frame_obus, state->frame_obus_alloc); |
2513 | | //make sure we don't attempt at freeing this buffer while assigned to the bitstream - cf gf_av1_reset_state |
2514 | 228k | state->frame_obus = NULL; |
2515 | 228k | } |
2516 | 228k | } |
2517 | 415k | else { |
2518 | 415k | GF_SAFEALLOC(a, GF_AV1_OBUArrayEntry); |
2519 | 415k | if (!a) { |
2520 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] Failed to allocate OBU\n")); |
2521 | 0 | return; |
2522 | 0 | } |
2523 | 415k | } |
2524 | | |
2525 | 643k | gf_bs_seek(bs, pos); |
2526 | 643k | gf_av1_parse_obu_header(bs, &obu_type, &obu_extension_flag, &has_size_field, &temporal_id, &spatial_id); |
2527 | 643k | gf_bs_seek(bs, pos); |
2528 | | |
2529 | 643k | if (has_size_field) { |
2530 | 642k | if (a) { |
2531 | 414k | a->obu = gf_malloc((size_t)obu_length); |
2532 | 414k | gf_bs_read_data(bs, a->obu, (u32)obu_length); |
2533 | 414k | a->obu_length = obu_length; |
2534 | 414k | } |
2535 | 227k | else { |
2536 | 227k | u32 remain = (u32)obu_length; |
2537 | 456k | while (remain) { |
2538 | 228k | u32 block_size = OBU_BLOCK_SIZE; |
2539 | 228k | if (block_size > remain) block_size = remain; |
2540 | 228k | gf_bs_read_data(bs, block, block_size); |
2541 | 228k | gf_bs_write_data(state->bs, block, block_size); |
2542 | 228k | remain -= block_size; |
2543 | 228k | } |
2544 | 227k | return; |
2545 | 227k | } |
2546 | 642k | } |
2547 | 987 | else { |
2548 | 987 | u8 i, hdr_size = obu_extension_flag ? 2 : 1; |
2549 | 987 | const u32 leb_size = (u32)gf_av1_leb128_size(obu_length); |
2550 | 987 | const u64 obu_size = obu_length - hdr_size; |
2551 | | |
2552 | 987 | if (a) { |
2553 | 227 | a->obu = gf_malloc((size_t)obu_length + leb_size); |
2554 | 227 | a->obu_length = obu_length + leb_size; |
2555 | 679 | for (i = 0; i < hdr_size; ++i) { |
2556 | 452 | a->obu[i] = gf_bs_read_u8(bs); |
2557 | | /*add size field flag*/ |
2558 | 452 | if (i == 0) a->obu[0] |= 0x02; |
2559 | 452 | } |
2560 | 227 | { |
2561 | 227 | u32 out_size = 0; |
2562 | 227 | u8 *output = NULL; |
2563 | 227 | GF_BitStream *bsLeb128 = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
2564 | | /*write size field*/ |
2565 | 227 | gf_av1_leb128_write(bsLeb128, obu_size); |
2566 | 227 | gf_assert(gf_bs_get_position(bsLeb128) == leb_size); |
2567 | 227 | gf_bs_get_content(bsLeb128, &output, &out_size); |
2568 | 227 | gf_bs_del(bsLeb128); |
2569 | 227 | memcpy(a->obu + hdr_size, output, out_size); |
2570 | 227 | gf_free(output); |
2571 | 227 | } |
2572 | 227 | gf_bs_read_data(bs, a->obu + hdr_size + leb_size, (u32)(obu_size)); |
2573 | 227 | gf_assert(gf_bs_get_position(bs) == pos + obu_length); |
2574 | 227 | } |
2575 | 760 | else { |
2576 | 760 | u32 remain; |
2577 | 1.70k | for (i = 0; i < hdr_size; ++i) { |
2578 | 943 | u8 hdr_b = gf_bs_read_u8(bs); |
2579 | 943 | if (i == 0) hdr_b |= 0x02; /*add size field flag*/ |
2580 | 943 | gf_bs_write_u8(state->bs, hdr_b); |
2581 | 943 | } |
2582 | | /*add size field */ |
2583 | 760 | gf_av1_leb128_write(state->bs, obu_size); |
2584 | 760 | remain = (u32)obu_length - hdr_size; |
2585 | 2.63k | while (remain) { |
2586 | 1.87k | u32 block_size = OBU_BLOCK_SIZE; |
2587 | 1.87k | if (block_size > remain) block_size = remain; |
2588 | 1.87k | gf_bs_read_data(bs, block, block_size); |
2589 | 1.87k | gf_bs_write_data(state->bs, block, block_size); |
2590 | 1.87k | remain -= block_size; |
2591 | 1.87k | } |
2592 | 760 | gf_assert(gf_bs_get_position(bs) == pos + obu_length); |
2593 | 760 | return; |
2594 | 760 | } |
2595 | 987 | } |
2596 | 415k | if (!obu_list) { |
2597 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] internal error, no OBU list cannot add\n")); |
2598 | 0 | gf_free(a->obu); |
2599 | 0 | gf_free(a); |
2600 | 0 | return; |
2601 | 0 | } |
2602 | 415k | a->obu_type = obu_type; |
2603 | 415k | if (! *obu_list) |
2604 | 13.4k | *obu_list = gf_list_new(); |
2605 | 415k | gf_list_add(*obu_list, a); |
2606 | 415k | } |
2607 | | |
2608 | | static void av1_populate_state_from_obu(GF_BitStream *bs, u64 pos, u64 obu_length, ObuType obu_type, AV1State *state) |
2609 | 374k | { |
2610 | 374k | if (av1_is_obu_header(obu_type)) { |
2611 | 337k | av1_add_obu_internal(bs, pos, obu_length, obu_type, &state->frame_state.header_obus, NULL); |
2612 | 337k | } |
2613 | 374k | if (!state->skip_frames && av1_is_obu_frame(state, obu_type)) { |
2614 | 305k | if (!state->mem_mode) { |
2615 | 77.2k | av1_add_obu_internal(bs, pos, obu_length, obu_type, &state->frame_state.frame_obus, NULL); |
2616 | 77.2k | } |
2617 | 228k | else { |
2618 | 228k | av1_add_obu_internal(bs, pos, obu_length, obu_type, NULL, state); |
2619 | 228k | } |
2620 | 305k | } |
2621 | 374k | } |
2622 | | |
2623 | | GF_Err aom_av1_parse_temporal_unit_from_section5(GF_BitStream *bs, AV1State *state) |
2624 | 90.5k | { |
2625 | 90.5k | if (!state) return GF_BAD_PARAM; |
2626 | 90.5k | state->has_temporal_delim = 0; |
2627 | | //if some OBUs are written, we had a TL before but had to stop parsing due to not enough space |
2628 | 90.5k | Bool first_obu = state->has_frame_data ? GF_FALSE : GF_TRUE; |
2629 | | |
2630 | 463k | while (1) { |
2631 | 463k | GF_Err e; |
2632 | 463k | if (!gf_bs_available(bs)) |
2633 | 67.5k | return state->unframed ? GF_BUFFER_TOO_SMALL : GF_OK; |
2634 | | |
2635 | 395k | u64 pos = gf_bs_get_position(bs), obu_size = 0; |
2636 | | |
2637 | 395k | e = gf_av1_parse_obu(bs, &state->obu_type, &obu_size, NULL, state); |
2638 | 395k | if (e) |
2639 | 8.51k | return e; |
2640 | | |
2641 | 387k | if (obu_size != gf_bs_get_position(bs) - pos) { |
2642 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] OBU (Section 5) frame size "LLU" different from consumed bytes "LLU".\n", obu_size, gf_bs_get_position(bs) - pos)); |
2643 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2644 | 0 | } |
2645 | | |
2646 | 387k | if (state->obu_type == OBU_TEMPORAL_DELIMITER) { |
2647 | 29.9k | if (!first_obu) { |
2648 | | // seek back |
2649 | 14.5k | gf_bs_seek(bs, pos); |
2650 | 14.5k | break; |
2651 | 14.5k | } |
2652 | 15.4k | state->has_temporal_delim = 1; |
2653 | 15.4k | state->has_frame_data = 0; |
2654 | 357k | } else { |
2655 | 357k | state->has_frame_data = 1; |
2656 | 357k | } |
2657 | 372k | first_obu = GF_FALSE; |
2658 | | |
2659 | 372k | GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[AV1] Section5 OBU detected (size "LLU")\n", obu_size)); |
2660 | 372k | av1_populate_state_from_obu(bs, pos, obu_size, state->obu_type, state); |
2661 | 372k | } |
2662 | | |
2663 | 14.5k | return GF_OK; |
2664 | 90.5k | } |
2665 | | |
2666 | | Bool gf_media_aom_probe_annexb(GF_BitStream *bs) |
2667 | 4.37k | { |
2668 | 4.37k | Bool res = GF_TRUE; |
2669 | 4.37k | u64 init_pos = gf_bs_get_position(bs); |
2670 | 4.37k | u64 sz = gf_av1_leb128_read(bs, NULL); |
2671 | 4.37k | if (!sz) res = GF_FALSE; |
2672 | 4.49k | while (sz > 0) { |
2673 | 3.90k | u8 Leb128Bytes = 0; |
2674 | 3.90k | u64 frame_unit_size = gf_av1_leb128_read(bs, &Leb128Bytes); |
2675 | | |
2676 | 3.90k | if (!frame_unit_size) { |
2677 | 151 | res = GF_FALSE; |
2678 | 151 | break; |
2679 | 151 | } |
2680 | | |
2681 | 3.75k | if (sz < Leb128Bytes + frame_unit_size) { |
2682 | 2.36k | res = GF_FALSE; |
2683 | 2.36k | break; |
2684 | 2.36k | } |
2685 | 1.38k | sz -= Leb128Bytes + frame_unit_size; |
2686 | | |
2687 | 1.91k | while (frame_unit_size > 0) { |
2688 | 1.79k | ObuType obu_type; |
2689 | 1.79k | u64 pos, obu_length = gf_av1_leb128_read(bs, &Leb128Bytes); |
2690 | 1.79k | if (frame_unit_size < Leb128Bytes + obu_length) { |
2691 | 769 | res = GF_FALSE; |
2692 | 769 | break; |
2693 | 769 | } |
2694 | 1.02k | pos = gf_bs_get_position(bs); |
2695 | 1.02k | frame_unit_size -= Leb128Bytes; |
2696 | | |
2697 | 1.02k | u8 tid, sid; |
2698 | 1.02k | Bool extflag, has_size; |
2699 | 1.02k | GF_Err e = gf_av1_parse_obu_header(bs, &obu_type, &extflag, &has_size, &tid, &sid); |
2700 | 1.02k | if (e) { |
2701 | 199 | res = GF_FALSE; |
2702 | 199 | break; |
2703 | 199 | } |
2704 | | |
2705 | 824 | if (has_size) { |
2706 | 278 | obu_length = (u32)gf_av1_leb128_read(bs, NULL); |
2707 | 278 | } |
2708 | 546 | else { |
2709 | 546 | if (obu_length >= 1 + extflag) { |
2710 | 415 | obu_length = obu_length - 1 - extflag; |
2711 | 415 | } |
2712 | 131 | else { |
2713 | 131 | res = GF_FALSE; |
2714 | 131 | break; |
2715 | 131 | } |
2716 | 546 | } |
2717 | 693 | u32 hdr_size = (u32)(gf_bs_get_position(bs) - pos); |
2718 | 693 | obu_length += hdr_size; |
2719 | | |
2720 | 693 | if (frame_unit_size < obu_length) { |
2721 | 162 | res = GF_FALSE; |
2722 | 162 | break; |
2723 | 162 | } |
2724 | 531 | frame_unit_size -= obu_length; |
2725 | 531 | gf_bs_skip_bytes(bs, obu_length - hdr_size); |
2726 | 531 | } |
2727 | 1.38k | if (!res) break; |
2728 | 1.38k | } |
2729 | 4.37k | gf_bs_seek(bs, init_pos); |
2730 | 4.37k | return res; |
2731 | 4.37k | } |
2732 | | |
2733 | | GF_Err aom_av1_parse_temporal_unit_from_annexb(GF_BitStream *bs, AV1State *state) |
2734 | 1.51k | { |
2735 | 1.51k | GF_Err e; |
2736 | 1.51k | u64 tupos; |
2737 | 1.51k | u64 tusize, sz; |
2738 | 1.51k | if (!bs || !state) return GF_BAD_PARAM; |
2739 | | |
2740 | 1.51k | gf_bs_mark_overflow(bs, GF_TRUE); |
2741 | 1.51k | tusize = sz = gf_av1_leb128_read(bs, NULL); |
2742 | 1.51k | tupos = gf_bs_get_position(bs); |
2743 | 1.51k | if (!sz) { |
2744 | 234 | GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[AV1] temporal unit size is 0, likely not annex B\n")); |
2745 | 234 | return GF_NON_COMPLIANT_BITSTREAM; |
2746 | 234 | } |
2747 | | |
2748 | 1.28k | GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[AV1] Annex B temporal unit detected (size "LLU") ***** \n", sz)); |
2749 | 1.53k | while (sz > 0) { |
2750 | 1.34k | u8 Leb128Bytes = 0; |
2751 | 1.34k | u64 frame_unit_size = gf_av1_leb128_read(bs, &Leb128Bytes); |
2752 | | |
2753 | 1.34k | if (gf_bs_is_overflow(bs)) { |
2754 | 0 | return GF_BUFFER_TOO_SMALL; |
2755 | 0 | } |
2756 | 1.34k | if (sz < Leb128Bytes + frame_unit_size) { |
2757 | 499 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] Annex B sz("LLU") < Leb128Bytes("LLU") + frame_unit_size("LLU")\n", sz, Leb128Bytes, frame_unit_size)); |
2758 | 499 | return GF_NON_COMPLIANT_BITSTREAM; |
2759 | 499 | } |
2760 | 844 | GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[AV1] Annex B frame unit detected (size "LLU")\n", frame_unit_size)); |
2761 | 844 | sz -= Leb128Bytes + frame_unit_size; |
2762 | | |
2763 | 2.29k | while (frame_unit_size > 0) { |
2764 | 2.04k | u64 pos, obu_size = gf_av1_leb128_read(bs, &Leb128Bytes); |
2765 | | |
2766 | 2.04k | if (gf_bs_is_overflow(bs)) { |
2767 | 0 | return GF_BUFFER_TOO_SMALL; |
2768 | 0 | } |
2769 | 2.04k | if (frame_unit_size < Leb128Bytes + obu_size) { |
2770 | 149 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] Annex B frame_unit_size("LLU") < Leb128Bytes("LLU") + obu_length("LLU")\n", frame_unit_size, Leb128Bytes, obu_size)); |
2771 | 149 | return GF_NON_COMPLIANT_BITSTREAM; |
2772 | 149 | } |
2773 | 1.89k | GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[AV1] Annex B OBU detected (size "LLU")\n", obu_size)); |
2774 | 1.89k | pos = gf_bs_get_position(bs); |
2775 | 1.89k | frame_unit_size -= Leb128Bytes; |
2776 | | |
2777 | 1.89k | e = gf_av1_parse_obu(bs, &state->obu_type, &obu_size, NULL, state); |
2778 | 1.89k | if (e) return e; |
2779 | | |
2780 | 1.45k | if (obu_size != gf_bs_get_position(bs) - pos) { |
2781 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Annex B frame size "LLU" different from consumed bytes "LLU".\n", obu_size, gf_bs_get_position(bs) - pos)); |
2782 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2783 | 0 | } |
2784 | | |
2785 | 1.45k | av1_populate_state_from_obu(bs, pos, obu_size, state->obu_type, state); |
2786 | 1.45k | if (frame_unit_size < obu_size) { |
2787 | 6 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] Annex B frame_unit_size("LLU") < OBU size ("LLU")\n", frame_unit_size, obu_size)); |
2788 | 6 | return GF_NON_COMPLIANT_BITSTREAM; |
2789 | 6 | } |
2790 | 1.45k | frame_unit_size -= obu_size; |
2791 | 1.45k | } |
2792 | 844 | } |
2793 | 188 | gf_assert(sz == 0); |
2794 | 188 | if (tusize != gf_bs_get_position(bs) - tupos) { |
2795 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Annex B TU size "LLU" different from consumed bytes "LLU".\n", tusize, gf_bs_get_position(bs) - tupos)); |
2796 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2797 | 0 | } |
2798 | 188 | return GF_OK; |
2799 | 188 | } |
2800 | | |
2801 | | GF_Err aom_av1_parse_temporal_unit_from_ivf(GF_BitStream *bs, AV1State *state) |
2802 | 1.51k | { |
2803 | 1.51k | u64 frame_size, pts_ignored; |
2804 | 1.51k | GF_Err e; |
2805 | 1.51k | if (gf_bs_available(bs)<12) return GF_EOS; |
2806 | 1.51k | e = gf_media_parse_ivf_frame_header(bs, &frame_size, &pts_ignored); |
2807 | 1.51k | if (e) return e; |
2808 | 1.41k | GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[AV1] IVF frame detected (size "LLU")\n", frame_size)); |
2809 | | |
2810 | 1.41k | if (gf_bs_available(bs) < frame_size) return GF_EOS; |
2811 | | |
2812 | 88 | while (frame_size > 0) { |
2813 | 74 | u64 obu_size = 0, pos = gf_bs_get_position(bs); |
2814 | | |
2815 | 74 | e = gf_av1_parse_obu(bs, &state->obu_type, &obu_size, NULL, state); |
2816 | 74 | if (e != GF_OK) |
2817 | 18 | return e; |
2818 | | |
2819 | 56 | if (obu_size != gf_bs_get_position(bs) - pos) { |
2820 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] IVF frame size "LLU" different from consumed bytes "LLU".\n", obu_size, gf_bs_get_position(bs) - pos)); |
2821 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2822 | 0 | } |
2823 | | |
2824 | 56 | av1_populate_state_from_obu(bs, pos, obu_size, state->obu_type, state); |
2825 | | |
2826 | 56 | frame_size -= obu_size; |
2827 | 56 | } |
2828 | 14 | return GF_OK; |
2829 | 32 | } |
2830 | | |
2831 | 1.33M | #define AV1_NUM_REF_FRAMES 8 |
2832 | 42.6k | #define AV1_ALL_FRAMES ((1 << AV1_NUM_REF_FRAMES) - 1) |
2833 | | |
2834 | 6.92k | #define AV1_SUPERRES_DENOM_MIN 9 |
2835 | | #define AV1_SUPERRES_DENOM_BITS 3 |
2836 | 87.1k | #define AV1_SUPERRES_NUM 8 |
2837 | | |
2838 | 901k | #define AV1_REFS_PER_FRAME 7 |
2839 | 94.3k | #define AV1_PRIMARY_REF_NONE 7 |
2840 | | |
2841 | 47.0k | #define MAX_TILE_WIDTH 4096 |
2842 | 47.0k | #define MAX_TILE_AREA (4096 * 2304) |
2843 | | |
2844 | | static u32 aom_av1_tile_log2(u32 blkSize, u32 target) |
2845 | 278k | { |
2846 | 278k | u32 k; |
2847 | 507k | for (k = 0; (blkSize << k) < target; k++) { |
2848 | 228k | } |
2849 | 278k | return k; |
2850 | 278k | } |
2851 | | |
2852 | 10.7k | static u64 aom_av1_le(GF_BitStream *bs, u32 n, const char *name) { |
2853 | 10.7k | u32 i = 0; |
2854 | 10.7k | u64 t = 0; |
2855 | 31.4k | for (i = 0; i < n; i++) { |
2856 | 20.6k | u32 byte = gf_bs_read_int(bs, 8); |
2857 | 20.6k | t += (byte << (i * 8)); |
2858 | 20.6k | } |
2859 | 10.7k | gf_bs_log(bs, n*8, name, t); |
2860 | 10.7k | return t; |
2861 | 10.7k | } |
2862 | | |
2863 | | |
2864 | | static void av1_parse_tile_info(GF_BitStream *bs, AV1State *state) |
2865 | 47.0k | { |
2866 | 47.0k | u32 i; |
2867 | 47.0k | u32 MiCols = 2 * ((state->width + 7) >> 3); |
2868 | 47.0k | u32 MiRows = 2 * ((state->height + 7) >> 3); |
2869 | 47.0k | u32 sbCols = state->use_128x128_superblock ? ((MiCols + 31) >> 5) : ((MiCols + 15) >> 4); |
2870 | 47.0k | u32 sbRows = state->use_128x128_superblock ? ((MiRows + 31) >> 5) : ((MiRows + 15) >> 4); |
2871 | 47.0k | u32 sbShift = state->use_128x128_superblock ? 5 : 4; |
2872 | 47.0k | u32 sbSize = sbShift + 2; |
2873 | 47.0k | u32 maxTileWidthSb = MAX_TILE_WIDTH >> sbSize; |
2874 | 47.0k | u32 maxTileAreaSb = MAX_TILE_AREA >> (2 * sbSize); |
2875 | 47.0k | u32 minLog2tileCols = aom_av1_tile_log2(maxTileWidthSb, sbCols); |
2876 | 47.0k | u32 maxLog2tileCols = aom_av1_tile_log2(1, MIN(sbCols, AV1_MAX_TILE_COLS)); |
2877 | 47.0k | u32 maxLog2tileRows = aom_av1_tile_log2(1, MIN(sbRows, AV1_MAX_TILE_ROWS)); |
2878 | 47.0k | u32 minLog2Tiles = MAX(minLog2tileCols, aom_av1_tile_log2(maxTileAreaSb, sbRows * sbCols)); |
2879 | 47.0k | Bool uniform_tile_spacing_flag = gf_bs_read_int_log(bs, 1, "uniform_tile_spacing_flag"); |
2880 | 47.0k | if (uniform_tile_spacing_flag) { |
2881 | 23.6k | u32 startSb, tileWidthSb, tileHeightSb, minLog2tileRows; |
2882 | 23.6k | state->tileColsLog2 = minLog2tileCols; |
2883 | 37.7k | while (state->tileColsLog2 < maxLog2tileCols) { |
2884 | 15.7k | Bool increment_tile_cols_log2 = gf_bs_read_int_log(bs, 1, "increment_tile_cols_log2"); |
2885 | 15.7k | if (increment_tile_cols_log2 == 1) |
2886 | 14.0k | state->tileColsLog2++; |
2887 | 1.65k | else |
2888 | 1.65k | break; |
2889 | 15.7k | } |
2890 | | |
2891 | 23.6k | tileWidthSb = (sbCols + (1 << state->tileColsLog2) - 1) >> state->tileColsLog2; |
2892 | 23.6k | i = 0; |
2893 | 148k | for (startSb = 0; startSb < sbCols; startSb += tileWidthSb) { |
2894 | 124k | i += 1; |
2895 | 124k | } |
2896 | 23.6k | state->tileCols = i; |
2897 | 23.6k | minLog2tileRows = MAX((int)(minLog2Tiles - state->tileColsLog2), 0); |
2898 | 23.6k | state->tileRowsLog2 = minLog2tileRows; |
2899 | 30.8k | while (state->tileRowsLog2 < maxLog2tileRows) { |
2900 | 14.5k | Bool increment_tile_rows_log2 = gf_bs_read_int_log(bs, 1, "increment_tile_rows_log2"); |
2901 | 14.5k | if (increment_tile_rows_log2 == 1) |
2902 | 7.21k | state->tileRowsLog2++; |
2903 | 7.37k | else |
2904 | 7.37k | break; |
2905 | 14.5k | } |
2906 | | |
2907 | 23.6k | tileHeightSb = (sbRows + (1 << state->tileRowsLog2) - 1) >> state->tileRowsLog2; |
2908 | 23.6k | i = 0; |
2909 | 115k | for (startSb = 0; startSb < sbRows; startSb += tileHeightSb) { |
2910 | 92.2k | i += 1; |
2911 | 92.2k | } |
2912 | 23.6k | state->tileRows = i; |
2913 | 23.6k | } |
2914 | 23.3k | else { |
2915 | 23.3k | u32 startSb, maxTileHeightSb, widestTileSb; |
2916 | 23.3k | widestTileSb = 0; |
2917 | 23.3k | startSb = 0; |
2918 | 157k | for (i = 0; startSb < sbCols; i++) { |
2919 | 134k | u32 maxWidth = MIN((int)(sbCols - startSb), maxTileWidthSb); |
2920 | 134k | u32 width_in_sbs_minus_1 = av1_read_ns(bs, maxWidth, "width_in_sbs_minus_1"); |
2921 | 134k | u32 sizeSb = width_in_sbs_minus_1 + 1; |
2922 | 134k | widestTileSb = MAX(sizeSb, widestTileSb); |
2923 | 134k | startSb += sizeSb; |
2924 | 134k | } |
2925 | 23.3k | if (!widestTileSb) { |
2926 | 920 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] widest tile is 0, broken bitstream\n")); |
2927 | 920 | return; |
2928 | 920 | } |
2929 | 22.4k | state->tileCols = i; |
2930 | 22.4k | state->tileColsLog2 = aom_av1_tile_log2(1, state->tileCols); |
2931 | | |
2932 | 22.4k | if (minLog2Tiles > 0) |
2933 | 3.84k | maxTileAreaSb = (sbRows * sbCols) >> (minLog2Tiles + 1); |
2934 | 18.6k | else |
2935 | 18.6k | maxTileAreaSb = sbRows * sbCols; |
2936 | 22.4k | maxTileHeightSb = MAX(maxTileAreaSb / widestTileSb, 1); |
2937 | | |
2938 | 22.4k | startSb = 0; |
2939 | 363k | for (i = 0; startSb < sbRows; i++) { |
2940 | 340k | u32 maxHeight = MIN((int)(sbRows - startSb), maxTileHeightSb); |
2941 | 340k | u32 height_in_sbs_minus_1 = av1_read_ns(bs, maxHeight, "height_in_sbs_minus_1"); |
2942 | 340k | u32 sizeSb = height_in_sbs_minus_1 + 1; |
2943 | 340k | startSb += sizeSb; |
2944 | 340k | } |
2945 | | |
2946 | 22.4k | state->tileRows = i; |
2947 | 22.4k | state->tileRowsLog2 = aom_av1_tile_log2(1, state->tileRows); |
2948 | 22.4k | } |
2949 | 46.0k | if (state->tileColsLog2 > 0 || state->tileRowsLog2 > 0) { |
2950 | 11.8k | gf_bs_read_int_log(bs, state->tileRowsLog2 + state->tileColsLog2, "context_update_tile_id"); |
2951 | 11.8k | state->tile_size_bytes = gf_bs_read_int_log(bs, 2, "tile_size_bytes_minus1") + 1; |
2952 | 11.8k | } |
2953 | 46.0k | } |
2954 | | |
2955 | | static void superres_params(GF_BitStream *bs, AV1State *state) |
2956 | 47.0k | { |
2957 | 47.0k | u32 SuperresDenom; |
2958 | 47.0k | Bool use_superres; |
2959 | | |
2960 | 47.0k | if (state->enable_superres) { |
2961 | 23.0k | use_superres = gf_bs_read_int_log(bs, 1, "use_superres"); |
2962 | 23.0k | } |
2963 | 23.9k | else { |
2964 | 23.9k | use_superres = GF_FALSE; |
2965 | 23.9k | } |
2966 | 47.0k | if (use_superres) { |
2967 | 6.92k | u8 coded_denom = gf_bs_read_int_log(bs, AV1_SUPERRES_DENOM_BITS, "coded_denom"); |
2968 | 6.92k | SuperresDenom = coded_denom + AV1_SUPERRES_DENOM_MIN; |
2969 | 6.92k | } |
2970 | 40.0k | else { |
2971 | 40.0k | SuperresDenom = AV1_SUPERRES_NUM; |
2972 | 40.0k | } |
2973 | 47.0k | state->UpscaledWidth = state->width; |
2974 | 47.0k | state->width = (state->UpscaledWidth * AV1_SUPERRES_NUM + (SuperresDenom / 2)) / SuperresDenom; |
2975 | 47.0k | } |
2976 | | |
2977 | | static void av1_frame_size(GF_BitStream *bs, AV1State *state, Bool frame_size_override_flag) |
2978 | 44.0k | { |
2979 | 44.0k | if (frame_size_override_flag) { |
2980 | 12.4k | u32 frame_width_minus_1, frame_height_minus_1; |
2981 | 12.4k | u8 n = state->frame_width_bits_minus_1 + 1; |
2982 | 12.4k | frame_width_minus_1 = gf_bs_read_int_log(bs, n, "frame_width_minus_1"); |
2983 | 12.4k | n = state->frame_height_bits_minus_1 + 1; |
2984 | 12.4k | frame_height_minus_1 = gf_bs_read_int_log(bs, n, "frame_height_minus_1"); |
2985 | 12.4k | state->width = frame_width_minus_1 + 1; |
2986 | 12.4k | state->height = frame_height_minus_1 + 1; |
2987 | 31.6k | } else { |
2988 | 31.6k | state->width = state->sequence_width; |
2989 | 31.6k | state->height = state->sequence_height; |
2990 | 31.6k | } |
2991 | 44.0k | superres_params(bs, state); |
2992 | | //compute_image_size(); //no bits |
2993 | 44.0k | } |
2994 | | |
2995 | | static void av1_render_size(GF_BitStream *bs) |
2996 | 44.0k | { |
2997 | 44.0k | Bool render_and_frame_size_different = gf_bs_read_int_log(bs, 1, "render_and_frame_size_different_flag"); |
2998 | 44.0k | if (render_and_frame_size_different == GF_TRUE) { |
2999 | 15.5k | gf_bs_read_int_log(bs, 16, "render_width_minus_1"); |
3000 | 15.5k | gf_bs_read_int_log(bs, 16, "render_height_minus_1"); |
3001 | | //RenderWidth = render_width_minus_1 + 1; |
3002 | | //RenderHeight = render_height_minus_1 + 1; |
3003 | 15.5k | } |
3004 | 28.5k | else { |
3005 | | //RenderWidth = UpscaledWidth; |
3006 | | //RenderHeight = FrameHeight; |
3007 | 28.5k | } |
3008 | 44.0k | } |
3009 | | |
3010 | | static void read_interpolation_filter(GF_BitStream *bs) |
3011 | 28.1k | { |
3012 | 28.1k | Bool is_filter_switchable = gf_bs_read_int_log(bs, 1, "is_filter_switchable"); |
3013 | 28.1k | if (!is_filter_switchable) { |
3014 | 15.9k | /*interpolation_filter =*/ gf_bs_read_int_log(bs, 2, "interpolation_filter"); |
3015 | 15.9k | } |
3016 | 28.1k | } |
3017 | | |
3018 | | static void frame_size_with_refs(GF_BitStream *bs, AV1State *state, Bool frame_size_override_flag, s8 *ref_frame_idx) |
3019 | 3.71k | { |
3020 | 3.71k | Bool found_ref = GF_FALSE; |
3021 | 3.71k | u32 i = 0; |
3022 | 13.1k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
3023 | 12.4k | found_ref = gf_bs_read_int_log_idx(bs, 1, "found_ref", i); |
3024 | 12.4k | if (found_ref == 1) { |
3025 | 2.93k | state->UpscaledWidth = state->RefUpscaledWidth[ref_frame_idx[i]]; |
3026 | 2.93k | state->width = state->UpscaledWidth; |
3027 | 2.93k | state->height = state->RefFrameHeight[ref_frame_idx[i]]; |
3028 | 2.93k | break; |
3029 | 2.93k | } |
3030 | 12.4k | } |
3031 | 3.71k | if (found_ref == 0) { |
3032 | 775 | av1_frame_size(bs, state, frame_size_override_flag); |
3033 | 775 | av1_render_size(bs); |
3034 | 775 | } |
3035 | 2.93k | else { |
3036 | 2.93k | superres_params(bs, state); |
3037 | | //compute_image_size(); |
3038 | 2.93k | } |
3039 | 3.71k | } |
3040 | | |
3041 | | static s32 av1_delta_q(GF_BitStream *bs, const char *name_flag, const char *name) |
3042 | 139k | { |
3043 | 139k | Bool delta_coded = gf_bs_read_int_log(bs, 1, name_flag); |
3044 | 139k | s32 delta_q = 0; |
3045 | 139k | if (delta_coded) { |
3046 | 82.9k | u32 signMask = 1 << (7 - 1); |
3047 | 82.9k | delta_q = gf_bs_read_int_log(bs, 7, name); |
3048 | 82.9k | if (delta_q & signMask) |
3049 | 30.3k | delta_q = delta_q - 2 * signMask; |
3050 | 82.9k | } |
3051 | 139k | return delta_q; |
3052 | 139k | } |
3053 | | |
3054 | | static u8 Segmentation_Feature_Bits[] = { 8,6,6,6,6,3,0,0 }; |
3055 | | static u8 Segmentation_Feature_Signed[] = { 1, 1, 1, 1, 1, 0, 0, 0 }; |
3056 | | |
3057 | | static u8 av1_get_qindex(Bool ignoreDeltaQ, u32 segmentId, u32 base_q_idx, u32 delta_q_present, u32 CurrentQIndex, Bool segmentation_enabled, u8 *features_SEG_LVL_ALT_Q_enabled, s32 *features_SEG_LVL_ALT_Q) |
3058 | 376k | { |
3059 | | //If seg_feature_active_idx( segmentId, SEG_LVL_ALT_Q ) is equal to 1 the following ordered steps apply: |
3060 | 376k | if (segmentation_enabled && features_SEG_LVL_ALT_Q_enabled[segmentId]) { |
3061 | | //Set the variable data equal to FeatureData[ segmentId ][ SEG_LVL_ALT_Q ]. |
3062 | 20.7k | s32 data = features_SEG_LVL_ALT_Q[segmentId]; |
3063 | 20.7k | s32 qindex = base_q_idx + data; |
3064 | | //If ignoreDeltaQ is equal to 0 and delta_q_present is equal to 1, set qindex equal to CurrentQIndex + data. |
3065 | 20.7k | if ((ignoreDeltaQ == 0) && (delta_q_present == 1)) qindex = CurrentQIndex + data; |
3066 | | //Return Clip3( 0, 255, qindex ). |
3067 | 20.7k | if (qindex < 0) return 0; |
3068 | 20.7k | else if (qindex > 255) return 255; |
3069 | 6.26k | else return (u8)qindex; |
3070 | 20.7k | } |
3071 | | //Otherwise, if ignoreDeltaQ is equal to 0 and delta_q_present is equal to 1, return CurrentQIndex. |
3072 | 355k | if ((ignoreDeltaQ == 0) && (delta_q_present == 1)) return CurrentQIndex; |
3073 | | //otherwise |
3074 | 355k | return base_q_idx; |
3075 | 355k | } |
3076 | | |
3077 | | enum { |
3078 | | AV1_RESTORE_NONE = 0, |
3079 | | AV1_RESTORE_SWITCHABLE, |
3080 | | AV1_RESTORE_WIENER, |
3081 | | AV1_RESTORE_SGRPROJ |
3082 | | }; |
3083 | | |
3084 | 196k | #define AV1_GMC_IDENTITY 0 |
3085 | 328k | #define AV1_GMC_TRANSLATION 1 |
3086 | 227k | #define AV1_GMC_ROTZOOM 2 |
3087 | 118k | #define AV1_GMC_AFFINE 3 |
3088 | | |
3089 | 526k | #define AV1_LAST_FRAME 1 |
3090 | 8.64k | #define AV1_LAST2_FRAME 2 |
3091 | 8.64k | #define AV1_LAST3_FRAME 3 |
3092 | 8.64k | #define AV1_GOLDEN_FRAME 4 |
3093 | 14.3k | #define AV1_BWDREF_FRAME 5 |
3094 | 13.5k | #define AV1_ALTREF2_FRAME 6 |
3095 | 882k | #define AV1_ALTREF_FRAME 7 |
3096 | | |
3097 | 299k | #define GM_ABS_ALPHA_BITS 12 |
3098 | 299k | #define GM_ALPHA_PREC_BITS 15 |
3099 | 9.48k | #define GM_ABS_TRANS_ONLY_BITS 9 |
3100 | 9.48k | #define GM_TRANS_ONLY_PREC_BITS 3 |
3101 | 117k | #define GM_ABS_TRANS_BITS 12 |
3102 | 117k | #define GM_TRANS_PREC_BITS 6 |
3103 | 1.51M | #define WARPEDMODEL_PREC_BITS 16 |
3104 | | |
3105 | | |
3106 | | static u32 av1_decode_subexp(GF_BitStream *bs, s32 numSyms) |
3107 | 299k | { |
3108 | 299k | s32 i = 0; |
3109 | 299k | s32 mk = 0; |
3110 | 299k | s32 k = 3; |
3111 | 532k | while (1) { |
3112 | 532k | s32 b2 = i ? k + i - 1 : k; |
3113 | 532k | s32 a = 1 << b2; |
3114 | 532k | if (numSyms <= mk + 3 * a) { |
3115 | 7.59k | s32 subexp_final_bits = av1_read_ns(bs, numSyms - mk, NULL); |
3116 | 7.59k | return subexp_final_bits + mk; |
3117 | 7.59k | } |
3118 | 524k | else { |
3119 | 524k | s32 subexp_more_bits = gf_bs_read_int(bs, 1); |
3120 | 524k | if (subexp_more_bits) { |
3121 | 233k | i++; |
3122 | 233k | mk += a; |
3123 | 233k | } |
3124 | 291k | else { |
3125 | 291k | s32 subexp_bits = gf_bs_read_int(bs, b2); |
3126 | 291k | return subexp_bits + mk; |
3127 | 291k | } |
3128 | 524k | } |
3129 | 532k | } |
3130 | 299k | } |
3131 | | |
3132 | | static GFINLINE s32 inverse_recenter(s32 r, u32 v) |
3133 | 299k | { |
3134 | 299k | if ((s64)v > (s64)(2 * r)) |
3135 | 16.8k | return v; |
3136 | 282k | else if (v & 1) |
3137 | 95.2k | return r - ((v + 1) >> 1); |
3138 | 186k | else |
3139 | 186k | return r + (v >> 1); |
3140 | 299k | } |
3141 | | |
3142 | | static s32 av1_decode_unsigned_subexp_with_ref(GF_BitStream *bs, s32 mx, s32 r) |
3143 | 299k | { |
3144 | 299k | u32 v = av1_decode_subexp(bs, mx); |
3145 | 299k | if ((r < 0) && (-(-r << 1) <= mx)) { |
3146 | 15.1k | return inverse_recenter(r, v); |
3147 | 15.1k | } |
3148 | 283k | else if ((r << 1) <= mx) { |
3149 | 269k | return inverse_recenter(r, v); |
3150 | 269k | } |
3151 | 14.8k | else { |
3152 | 14.8k | return mx - 1 - inverse_recenter(mx - 1 - r, v); |
3153 | 14.8k | } |
3154 | 299k | } |
3155 | | static s16 av1_decode_signed_subexp_with_ref(GF_BitStream *bs, s32 low, s32 high, s32 r) |
3156 | 299k | { |
3157 | 299k | s16 x = av1_decode_unsigned_subexp_with_ref(bs, high - low, r - low); |
3158 | 299k | return x + low; |
3159 | 299k | } |
3160 | | |
3161 | | static void av1_read_global_param(AV1State *state, GF_BitStream *bs, u8 type, u8 ref, u8 idx) |
3162 | 299k | { |
3163 | 299k | u8 absBits = GM_ABS_ALPHA_BITS; |
3164 | 299k | u8 precBits = GM_ALPHA_PREC_BITS; |
3165 | 299k | if (idx < 2) { |
3166 | 126k | if (type == AV1_GMC_TRANSLATION) { |
3167 | 9.48k | absBits = GM_ABS_TRANS_ONLY_BITS - (!state->frame_state.allow_high_precision_mv ? 1 : 0); |
3168 | 9.48k | precBits = GM_TRANS_ONLY_PREC_BITS - (!state->frame_state.allow_high_precision_mv ? 1 : 0); |
3169 | 9.48k | } |
3170 | 117k | else { |
3171 | 117k | absBits = GM_ABS_TRANS_BITS; |
3172 | 117k | precBits = GM_TRANS_PREC_BITS; |
3173 | 117k | } |
3174 | 126k | } |
3175 | 299k | s32 precDiff = WARPEDMODEL_PREC_BITS - precBits; |
3176 | 299k | s32 round = (idx % 3) == 2 ? (1 << WARPEDMODEL_PREC_BITS) : 0; |
3177 | 299k | s32 sub = (idx % 3) == 2 ? (1 << precBits) : 0; |
3178 | 299k | s32 mx = (1 << absBits); |
3179 | 299k | s32 r = (state->PrevGmParams.coefs[ref][idx] >> precDiff) - sub; |
3180 | 299k | s32 val = av1_decode_signed_subexp_with_ref(bs, -mx, mx + 1, r); |
3181 | | |
3182 | 299k | if (val < 0) { |
3183 | 111k | val = -val; |
3184 | 111k | state->GmParams.coefs[ref][idx] = (-(val << precDiff) + round); |
3185 | 111k | } |
3186 | 187k | else { |
3187 | 187k | state->GmParams.coefs[ref][idx] = (val << precDiff) + round; |
3188 | 187k | } |
3189 | 299k | } |
3190 | | |
3191 | | static s32 av1_get_relative_dist(s32 a, s32 b, AV1State *state) |
3192 | 248k | { |
3193 | 248k | if (!state->enable_order_hint) |
3194 | 0 | return 0; |
3195 | 248k | s32 diff = a - b; |
3196 | 248k | s32 m = 1 << (state->OrderHintBits - 1); |
3197 | 248k | diff = (diff & (m - 1)) - (diff & m); |
3198 | 248k | return diff; |
3199 | 248k | } |
3200 | | |
3201 | | static void av1_setup_past_independence(AV1State *state) |
3202 | 33.3k | { |
3203 | 33.3k | u32 ref, i; |
3204 | 266k | for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ref++) { |
3205 | 1.63M | for (i = 0; i <= 5; i++) { |
3206 | 1.40M | state->PrevGmParams.coefs[ref][i] = ((i % 3 == 2) ? 1 << WARPEDMODEL_PREC_BITS : 0); |
3207 | 1.40M | } |
3208 | 233k | } |
3209 | 33.3k | } |
3210 | | |
3211 | | static void av1_load_previous(AV1State *state, u8 primary_ref_frame, s8 *ref_frame_idx) |
3212 | 13.6k | { |
3213 | 13.6k | s8 prevFrame = ref_frame_idx[primary_ref_frame]; |
3214 | 13.6k | if (prevFrame < 0) { |
3215 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] load_previous: prevFrame reference index %d is invalid\n", prevFrame)); |
3216 | 0 | } |
3217 | 13.6k | else { |
3218 | 13.6k | state->PrevGmParams = state->SavedGmParams[prevFrame]; |
3219 | | // load_loop_filter_params( prevFrame ) |
3220 | | // load_segmentation_params( prevFrame ) |
3221 | 13.6k | } |
3222 | 13.6k | } |
3223 | | |
3224 | | static void av1_decode_frame_wrapup(AV1State *state) |
3225 | 40.8k | { |
3226 | 40.8k | u32 i; |
3227 | 367k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3228 | 326k | if ((state->frame_state.refresh_frame_flags >> i) & 1) { |
3229 | 244k | state->RefOrderHint[i] = state->frame_state.order_hint; |
3230 | 244k | state->SavedGmParams[i] = state->GmParams; |
3231 | 244k | state->RefFrameType[i] = state->frame_state.frame_type; |
3232 | 244k | state->RefUpscaledWidth[i] = state->UpscaledWidth; |
3233 | 244k | state->RefFrameHeight[i] = state->height; |
3234 | 244k | } |
3235 | 326k | } |
3236 | 40.8k | state->frame_state.seen_frame_header = GF_FALSE; |
3237 | | //Otherwise (show_existing_frame is equal to 1), if frame_type is equal to KEY_FRAME, the reference frame loading process as specified in section 7.21 is invoked |
3238 | 40.8k | if ((state->frame_state.show_existing_frame) && (state->frame_state.frame_type == AV1_KEY_FRAME)) { |
3239 | 1.15k | state->frame_state.order_hint = state->RefOrderHint[state->frame_state.frame_to_show_map_idx]; |
3240 | | //OrderHints[ j + LAST_FRAME ] is set equal to SavedOrderHints[state->frame_to_show_map_idx ][ j + LAST_FRAME ] for j = 0..REFS_PER_FRAME-1. |
3241 | | |
3242 | | //gm_params[ ref ][ j ] is set equal to SavedGmParams[ frame_to_show_map_idx ][ ref ][ j ] for ref = LAST_FRAME..ALTREF_FRAME, for j = 0..5. |
3243 | 1.15k | state->GmParams = state->SavedGmParams[state->frame_state.frame_to_show_map_idx]; |
3244 | | |
3245 | 1.15k | } |
3246 | 40.8k | } |
3247 | | |
3248 | | static s32 find_latest_forward(u32 curFrameHint, u8 *shiftedOrderHints, u8 *usedFrame) |
3249 | 26.3k | { |
3250 | 26.3k | u32 i; |
3251 | 26.3k | s32 ref = -1; |
3252 | 26.3k | s32 latestOrderHint = 0; |
3253 | 237k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3254 | 210k | s32 hint = shiftedOrderHints[i]; |
3255 | 210k | if (!usedFrame[i] && ((u32)hint < curFrameHint) && (ref < 0 || hint >= latestOrderHint)) { |
3256 | 72.1k | ref = i; |
3257 | 72.1k | latestOrderHint = hint; |
3258 | 72.1k | } |
3259 | 210k | } |
3260 | 26.3k | return ref; |
3261 | 26.3k | } |
3262 | | |
3263 | | //see 7.8 of AV1 spec |
3264 | | static void av1_set_frame_refs(AV1State *state, u8 last_frame_idx, u8 gold_frame_idx, s8 *ref_frame_idx) |
3265 | 8.64k | { |
3266 | 8.64k | u32 i; |
3267 | 8.64k | u8 usedFrame[AV1_NUM_REF_FRAMES]; |
3268 | 8.64k | u8 shiftedOrderHints[AV1_NUM_REF_FRAMES]; |
3269 | | |
3270 | 69.1k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) |
3271 | 60.4k | ref_frame_idx[i] = -1; |
3272 | | |
3273 | 8.64k | ref_frame_idx[AV1_LAST_FRAME - AV1_LAST_FRAME] = last_frame_idx; |
3274 | 8.64k | ref_frame_idx[AV1_GOLDEN_FRAME - AV1_LAST_FRAME] = gold_frame_idx; |
3275 | | |
3276 | 77.7k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3277 | 69.1k | usedFrame[i] = 0; |
3278 | 69.1k | } |
3279 | | |
3280 | 8.64k | usedFrame[last_frame_idx] = 1; |
3281 | 8.64k | usedFrame[gold_frame_idx] = 1; |
3282 | 8.64k | u32 curFrameHint = 1 << (state->OrderHintBits - 1); |
3283 | | |
3284 | 77.7k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3285 | 69.1k | shiftedOrderHints[i] = curFrameHint + av1_get_relative_dist(state->RefOrderHint[i], state->frame_state.order_hint, state); |
3286 | 69.1k | } |
3287 | | |
3288 | 8.64k | u8 lastOrderHint = shiftedOrderHints[last_frame_idx]; |
3289 | 8.64k | u8 goldOrderHint = shiftedOrderHints[gold_frame_idx]; |
3290 | | |
3291 | | //It is a requirement of bitstream conformance that lastOrderHint is strictly less than curFrameHint. |
3292 | 8.64k | if (lastOrderHint >= curFrameHint) { |
3293 | 4.04k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] non conformant bitstream detected while setting up frame refs: lastOrderHint(%d) shall be stricly less than curFrameHint(%d)\n", lastOrderHint, curFrameHint)); |
3294 | 4.04k | } |
3295 | | //It is a requirement of bitstream conformance that goldOrderHint is strictly less than curFrameHint. |
3296 | 8.64k | if (goldOrderHint >= curFrameHint) { |
3297 | 4.15k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] non conformant bitstream detected while setting up frame refs: goldOrderHint(%d) shall be stricly less than curFrameHint(%d)\n", lastOrderHint, curFrameHint)); |
3298 | 4.15k | } |
3299 | | |
3300 | | //find_latest_backward() { |
3301 | 8.64k | s32 ref = -1; |
3302 | 8.64k | s32 latestOrderHint = 0; |
3303 | 77.7k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3304 | 69.1k | s32 hint = shiftedOrderHints[i]; |
3305 | 69.1k | if (!usedFrame[i] && ((u32)hint >= curFrameHint) && (ref < 0 || hint >= latestOrderHint)) { |
3306 | 19.9k | ref = i; |
3307 | 19.9k | latestOrderHint = hint; |
3308 | 19.9k | } |
3309 | 69.1k | } |
3310 | 8.64k | if (ref >= 0) { |
3311 | 6.25k | ref_frame_idx[AV1_ALTREF_FRAME - AV1_LAST_FRAME] = ref; |
3312 | 6.25k | usedFrame[ref] = 1; |
3313 | 6.25k | } |
3314 | | //find_earliest_backward() for BWDREF_FRAME |
3315 | 8.64k | ref = -1; |
3316 | 8.64k | s32 earliestOrderHint = 0; |
3317 | 77.7k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3318 | 69.1k | s32 hint = shiftedOrderHints[i]; |
3319 | 69.1k | if (!usedFrame[i] && ((u32)hint >= curFrameHint) && (ref < 0 || hint < earliestOrderHint)) { |
3320 | 6.47k | ref = i; |
3321 | 6.47k | earliestOrderHint = hint; |
3322 | 6.47k | } |
3323 | 69.1k | } |
3324 | 8.64k | if (ref >= 0) { |
3325 | 5.67k | ref_frame_idx[AV1_BWDREF_FRAME - AV1_LAST_FRAME] = ref; |
3326 | 5.67k | usedFrame[ref] = 1; |
3327 | 5.67k | } |
3328 | | |
3329 | | //find_earliest_backward() for ALTREF2_FRAME |
3330 | 8.64k | ref = -1; |
3331 | 8.64k | earliestOrderHint = 0; |
3332 | 77.7k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3333 | 69.1k | s32 hint = shiftedOrderHints[i]; |
3334 | 69.1k | if (!usedFrame[i] && ((u32)hint >= curFrameHint) && (ref < 0 || hint < earliestOrderHint)) { |
3335 | 6.19k | ref = i; |
3336 | 6.19k | earliestOrderHint = hint; |
3337 | 6.19k | } |
3338 | 69.1k | } |
3339 | 8.64k | if (ref >= 0) { |
3340 | 4.90k | ref_frame_idx[AV1_ALTREF2_FRAME - AV1_LAST_FRAME] = ref; |
3341 | 4.90k | usedFrame[ref] = 1; |
3342 | 4.90k | } |
3343 | | |
3344 | | //The remaining references are set to be forward references in anti-chronological order as follows: |
3345 | | |
3346 | 8.64k | const u8 Ref_Frame_List[AV1_REFS_PER_FRAME - 2] = { |
3347 | 8.64k | AV1_LAST2_FRAME, AV1_LAST3_FRAME, AV1_BWDREF_FRAME, AV1_ALTREF2_FRAME, AV1_ALTREF_FRAME |
3348 | 8.64k | }; |
3349 | | |
3350 | 51.8k | for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) { |
3351 | 43.2k | u8 refFrame = Ref_Frame_List[i]; |
3352 | 43.2k | if (ref_frame_idx[refFrame - AV1_LAST_FRAME] < 0) { |
3353 | 26.3k | s32 last_ref = find_latest_forward(curFrameHint, shiftedOrderHints, usedFrame); |
3354 | 26.3k | if (last_ref >= 0) { |
3355 | 22.8k | ref_frame_idx[refFrame - AV1_LAST_FRAME] = last_ref; |
3356 | 22.8k | usedFrame[last_ref] = 1; |
3357 | 22.8k | } |
3358 | 26.3k | } |
3359 | 43.2k | } |
3360 | | //Finally, any remaining references are set to the reference frame with smallest output order as follows: |
3361 | 8.64k | ref = -1; |
3362 | 77.7k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3363 | 69.1k | s32 hint = shiftedOrderHints[i]; |
3364 | 69.1k | if (ref < 0 || hint < earliestOrderHint) { |
3365 | 14.2k | ref = i; |
3366 | 14.2k | earliestOrderHint = hint; |
3367 | 14.2k | } |
3368 | 69.1k | } |
3369 | 69.1k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
3370 | 60.4k | if (ref_frame_idx[i] < 0) { |
3371 | 3.50k | ref_frame_idx[i] = ref; |
3372 | 3.50k | } |
3373 | 60.4k | } |
3374 | 8.64k | } |
3375 | | |
3376 | | |
3377 | | static void av1_parse_uncompressed_header(GF_BitStream *bs, AV1State *state) |
3378 | 48.5k | { |
3379 | 48.5k | Bool error_resilient_mode = GF_FALSE, allow_screen_content_tools = GF_FALSE, force_integer_mv = GF_FALSE; |
3380 | 48.5k | Bool /*use_ref_frame_mvs = GF_FALSE,*/ FrameIsIntra = GF_FALSE, frame_size_override_flag = GF_FALSE; |
3381 | 48.5k | Bool disable_cdf_update = GF_FALSE; |
3382 | 48.5k | u8 showable_frame; |
3383 | 48.5k | u8 primary_ref_frame; |
3384 | 48.5k | u16 idLen = 0; |
3385 | 48.5k | u32 idx; |
3386 | 48.5k | s8 ref_frame_idx[AV1_REFS_PER_FRAME]; |
3387 | 48.5k | AV1StateFrame *frame_state = &state->frame_state; |
3388 | | |
3389 | 48.5k | if (state->frame_id_numbers_present_flag) { |
3390 | 8.94k | idLen = (state->additional_frame_id_length_minus_1 + state->delta_frame_id_length_minus_2 + 3); |
3391 | 8.94k | } |
3392 | 48.5k | frame_state->refresh_frame_flags = 0; |
3393 | | |
3394 | 48.5k | showable_frame = 0; |
3395 | 48.5k | if (state->reduced_still_picture_header) { |
3396 | 14.9k | frame_state->key_frame = GF_TRUE; |
3397 | 14.9k | FrameIsIntra = GF_TRUE; |
3398 | 14.9k | frame_state->frame_type = AV1_KEY_FRAME; |
3399 | 14.9k | frame_state->show_frame = GF_TRUE; |
3400 | 14.9k | frame_state->show_existing_frame = 0; |
3401 | 14.9k | } |
3402 | 33.6k | else { |
3403 | 33.6k | frame_state->show_existing_frame = gf_bs_read_int_log(bs, 1, "show_existing_frame"); |
3404 | 33.6k | if (frame_state->show_existing_frame == GF_TRUE) { |
3405 | 1.53k | frame_state->frame_to_show_map_idx = gf_bs_read_int_log(bs, 3, "frame_to_show_map_idx"); |
3406 | 1.53k | frame_state->frame_type = state->RefFrameType[frame_state->frame_to_show_map_idx]; |
3407 | 1.53k | frame_state->order_hint = state->RefOrderHint[frame_state->frame_to_show_map_idx]; |
3408 | 1.53k | if (state->decoder_model_info_present_flag && !state->equal_picture_interval) { |
3409 | 58 | gf_bs_read_int_log(bs, state->frame_presentation_time_length, "frame_presentation_time"); |
3410 | 58 | } |
3411 | | |
3412 | 1.53k | frame_state->refresh_frame_flags = 0; |
3413 | 1.53k | if (state->frame_id_numbers_present_flag) { |
3414 | 209 | gf_bs_read_int_log(bs, idLen, "display_frame_id"); |
3415 | 209 | } |
3416 | 1.53k | if (frame_state->frame_type == AV1_KEY_FRAME) { |
3417 | 830 | frame_state->refresh_frame_flags = AV1_ALL_FRAMES; |
3418 | 830 | } |
3419 | | /* |
3420 | | if (film_grain_params_present) { |
3421 | | load_grain_params(frame_to_show_map_idx) |
3422 | | }*/ |
3423 | 1.53k | return; |
3424 | 1.53k | } |
3425 | 32.1k | frame_state->frame_type = gf_bs_read_int_log(bs, 2, "frame_type"); |
3426 | 32.1k | FrameIsIntra = (frame_state->frame_type == AV1_INTRA_ONLY_FRAME || frame_state->frame_type == AV1_KEY_FRAME); |
3427 | 32.1k | frame_state->show_frame = gf_bs_read_int_log(bs, 1, "show_frame"); |
3428 | 32.1k | if (frame_state->is_first_frame) { |
3429 | 545 | frame_state->key_frame = frame_state->seen_seq_header && frame_state->show_frame && frame_state->frame_type == AV1_KEY_FRAME && frame_state->seen_frame_header; |
3430 | 545 | } |
3431 | 32.1k | if (frame_state->show_frame && state->decoder_model_info_present_flag && !state->equal_picture_interval) { |
3432 | 2.82k | gf_bs_read_int_log(bs, state->frame_presentation_time_length, "frame_presentation_time"); |
3433 | 2.82k | } |
3434 | 32.1k | if (frame_state->show_frame) { |
3435 | 22.3k | showable_frame = frame_state->frame_type != AV1_KEY_FRAME; |
3436 | | |
3437 | 22.3k | } |
3438 | 9.80k | else { |
3439 | 9.80k | showable_frame = gf_bs_read_int_log(bs, 1, "showable_frame"); |
3440 | 9.80k | } |
3441 | 32.1k | if (frame_state->frame_type == AV1_SWITCH_FRAME || (frame_state->frame_type == AV1_KEY_FRAME && frame_state->show_frame)) |
3442 | 8.01k | error_resilient_mode = GF_TRUE; |
3443 | 24.1k | else |
3444 | 24.1k | error_resilient_mode = gf_bs_read_int_log(bs, 1, "error_resilient_mode"); |
3445 | 32.1k | } |
3446 | | |
3447 | 47.0k | if ((frame_state->frame_type == AV1_KEY_FRAME) && frame_state->show_frame) { |
3448 | 16.0k | u32 i; |
3449 | 144k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3450 | 128k | state->RefValid[i] = 0; |
3451 | 128k | state->RefOrderHint[i] = 0; |
3452 | 128k | } |
3453 | 128k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
3454 | 112k | state->OrderHints[AV1_LAST_FRAME + i] = 0; |
3455 | 112k | } |
3456 | 16.0k | } |
3457 | | |
3458 | 47.0k | disable_cdf_update = gf_bs_read_int_log(bs, 1, "disable_cdf_update"); |
3459 | 47.0k | if (state->seq_force_screen_content_tools == 2/*SELECT_SCREEN_CONTENT_TOOLS*/) { |
3460 | 28.9k | allow_screen_content_tools = gf_bs_read_int_log(bs, 1, "allow_screen_content_tools"); |
3461 | 28.9k | } |
3462 | 18.0k | else { |
3463 | 18.0k | allow_screen_content_tools = state->seq_force_screen_content_tools; |
3464 | 18.0k | } |
3465 | 47.0k | if (allow_screen_content_tools) { |
3466 | 24.2k | if (state->seq_force_integer_mv == 2/*SELECT_INTEGER_MV*/) { |
3467 | 16.5k | force_integer_mv = gf_bs_read_int_log(bs, 1, "force_integer_mv"); |
3468 | 16.5k | } |
3469 | 7.64k | else { |
3470 | 7.64k | force_integer_mv = state->seq_force_integer_mv; |
3471 | 7.64k | } |
3472 | 24.2k | } |
3473 | 22.8k | else { |
3474 | 22.8k | force_integer_mv = 0; |
3475 | 22.8k | } |
3476 | 47.0k | if (FrameIsIntra) { |
3477 | 18.8k | force_integer_mv = 1; |
3478 | 18.8k | } |
3479 | 47.0k | if (state->frame_id_numbers_present_flag) { |
3480 | 8.74k | gf_bs_read_int_log(bs, idLen, "current_frame_id"); |
3481 | 8.74k | } |
3482 | 47.0k | if (frame_state->frame_type == AV1_SWITCH_FRAME) |
3483 | 6.83k | frame_size_override_flag = GF_TRUE; |
3484 | 40.1k | else if (state->reduced_still_picture_header) |
3485 | 14.9k | frame_size_override_flag = GF_FALSE; |
3486 | 25.2k | else |
3487 | 25.2k | frame_size_override_flag = gf_bs_read_int_log(bs, 1, "frame_size_override_flag"); |
3488 | | |
3489 | 47.0k | frame_state->order_hint = gf_bs_read_int_log(bs, state->OrderHintBits, "order_hint"); |
3490 | 47.0k | if (FrameIsIntra || error_resilient_mode) { |
3491 | 31.9k | primary_ref_frame = AV1_PRIMARY_REF_NONE; |
3492 | 31.9k | } |
3493 | 15.0k | else { |
3494 | 15.0k | primary_ref_frame = gf_bs_read_int_log(bs, 3, "primary_ref_frame"); |
3495 | 15.0k | } |
3496 | | |
3497 | 47.0k | if (state->decoder_model_info_present_flag) { |
3498 | 7.81k | u8 buffer_removal_time_present_flag = gf_bs_read_int_log(bs, 1, "buffer_removal_time_present_flag"); |
3499 | 7.81k | if (buffer_removal_time_present_flag) { |
3500 | 2.94k | u32 opNum; |
3501 | 13.8k | for (opNum = 0; opNum < state->operating_points_count; opNum++) { |
3502 | 10.8k | if (state->decoder_model_present_for_this_op[opNum]) { |
3503 | 4.97k | u8 opPtIdc = state->operating_point_idc[opNum]; |
3504 | 4.97k | u8 inTemporalLayer = (opPtIdc >> state->temporal_id) & 1; |
3505 | 4.97k | u8 inSpatialLayer = (opPtIdc >> (state->spatial_id + 8)) & 1; |
3506 | 4.97k | if (opPtIdc == 0 || (inTemporalLayer && inSpatialLayer)) { |
3507 | 2.40k | gf_bs_read_int_log_idx(bs, state->buffer_removal_time_length, "buffer_removal_time", opNum); |
3508 | 2.40k | } |
3509 | 4.97k | } |
3510 | 10.8k | } |
3511 | 2.94k | } |
3512 | 7.81k | } |
3513 | | |
3514 | 47.0k | if (frame_state->frame_type == AV1_SWITCH_FRAME || (frame_state->frame_type == AV1_KEY_FRAME && frame_state->show_frame)) { |
3515 | 22.9k | frame_state->refresh_frame_flags = AV1_ALL_FRAMES; |
3516 | 22.9k | } |
3517 | 24.1k | else { |
3518 | 24.1k | frame_state->refresh_frame_flags = gf_bs_read_int_log(bs, 8, "refresh_frame_flags"); |
3519 | 24.1k | } |
3520 | 47.0k | if (!FrameIsIntra || frame_state->refresh_frame_flags != AV1_ALL_FRAMES) { |
3521 | 30.9k | if (error_resilient_mode && state->enable_order_hint) { |
3522 | 8.58k | u32 i = 0; |
3523 | 77.2k | for (i = 0; i < AV1_NUM_REF_FRAMES; i++) { |
3524 | 68.6k | u8 ref_order_hint = gf_bs_read_int_log_idx(bs, state->OrderHintBits, "ref_order_hint", i); |
3525 | 68.6k | if (ref_order_hint != state->RefOrderHint[i]) { |
3526 | 42.7k | state->RefValid[i] = 0; |
3527 | 42.7k | } |
3528 | 68.6k | state->RefOrderHint[i] = ref_order_hint; |
3529 | 68.6k | } |
3530 | 8.58k | } |
3531 | 30.9k | } |
3532 | | |
3533 | 47.0k | u8 allow_intrabc = 0; |
3534 | 47.0k | if (frame_state->frame_type == AV1_KEY_FRAME) { |
3535 | 17.2k | av1_frame_size(bs, state, frame_size_override_flag); |
3536 | 17.2k | av1_render_size(bs); |
3537 | 17.2k | if (allow_screen_content_tools && state->UpscaledWidth == state->width) { |
3538 | 5.13k | allow_intrabc = gf_bs_read_int_log(bs, 1, "allow_intrabc"); |
3539 | 5.13k | } |
3540 | 17.2k | } |
3541 | 29.7k | else { |
3542 | 29.7k | if (frame_state->frame_type == AV1_INTRA_ONLY_FRAME) { |
3543 | 1.60k | av1_frame_size(bs, state, frame_size_override_flag); |
3544 | 1.60k | av1_render_size(bs); |
3545 | 1.60k | if (allow_screen_content_tools && state->UpscaledWidth == state->width) { |
3546 | 430 | allow_intrabc = gf_bs_read_int_log(bs, 1, "allow_intrabc"); |
3547 | 430 | } |
3548 | 1.60k | } |
3549 | 28.1k | else { |
3550 | 28.1k | u32 i = 0; |
3551 | 28.1k | Bool frame_refs_short_signaling = GF_FALSE; |
3552 | 28.1k | if (state->enable_order_hint) { |
3553 | 18.6k | frame_refs_short_signaling = gf_bs_read_int_log(bs, 1, "frame_refs_short_signaling"); |
3554 | 18.6k | if (frame_refs_short_signaling) { |
3555 | 8.64k | u8 last_frame_idx = gf_bs_read_int_log(bs, 3, "last_frame_idx"); |
3556 | 8.64k | u8 gold_frame_idx = gf_bs_read_int_log(bs, 3, "gold_frame_idx"); |
3557 | 8.64k | av1_set_frame_refs(state, last_frame_idx, gold_frame_idx, ref_frame_idx); |
3558 | 8.64k | } |
3559 | 18.6k | } |
3560 | 224k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
3561 | 196k | if (!frame_refs_short_signaling) |
3562 | 136k | ref_frame_idx[i] = gf_bs_read_int_log_idx(bs, 3, "ref_frame_idx", i); |
3563 | | |
3564 | 196k | if (state->frame_id_numbers_present_flag) { |
3565 | 55.3k | u32 n = state->delta_frame_id_length_minus_2 + 2; |
3566 | 55.3k | /*delta_frame_id_minus_1 =*/ gf_bs_read_int_log_idx(bs, n, "delta_frame_id_minus1", i); |
3567 | | //DeltaFrameId = delta_frame_id_minus_1 + 1; |
3568 | | //expectedFrameId[i] = ((current_frame_id + (1 << idLen) - DeltaFrameId) % (1 << idLen)); |
3569 | 55.3k | } |
3570 | 196k | } |
3571 | 28.1k | if (frame_size_override_flag && !error_resilient_mode) { |
3572 | 3.71k | frame_size_with_refs(bs, state, frame_size_override_flag, ref_frame_idx); |
3573 | 3.71k | } |
3574 | 24.4k | else { |
3575 | 24.4k | av1_frame_size(bs, state, frame_size_override_flag); |
3576 | 24.4k | av1_render_size(bs); |
3577 | 24.4k | } |
3578 | 28.1k | frame_state->allow_high_precision_mv = 0; |
3579 | 28.1k | if (!force_integer_mv) { |
3580 | 21.2k | frame_state->allow_high_precision_mv = gf_bs_read_int_log(bs, 1, "allow_high_precision_mv"); |
3581 | 21.2k | } |
3582 | | |
3583 | 28.1k | read_interpolation_filter(bs); |
3584 | | |
3585 | 28.1k | gf_bs_read_int_log(bs, 1, "is_motion_mode_switchable"); |
3586 | 28.1k | if (!(error_resilient_mode || !state->enable_ref_frame_mvs)) { |
3587 | 2.60k | gf_bs_read_int_log(bs, 1, "use_ref_frame_mvs"); |
3588 | 2.60k | } |
3589 | 28.1k | } |
3590 | 29.7k | } |
3591 | | |
3592 | 47.0k | if (!FrameIsIntra) { |
3593 | 28.1k | u32 i; |
3594 | 224k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
3595 | 196k | u8 refFrame = AV1_LAST_FRAME + i; |
3596 | 196k | u8 ridx = ref_frame_idx[i]; |
3597 | 196k | if (ridx >= 0) { |
3598 | 196k | u8 hint = state->RefOrderHint[ridx]; |
3599 | 196k | state->OrderHints[refFrame] = hint; |
3600 | | /* if ( !enable_order_hint ) { |
3601 | | RefFrameSignBias[ refFrame ] = 0; |
3602 | | } else { |
3603 | | RefFrameSignBias[ refFrame ] = get_relative_dist( hint, OrderHint) > 0; |
3604 | | } |
3605 | | */ |
3606 | 196k | } |
3607 | | |
3608 | 196k | } |
3609 | 28.1k | } |
3610 | | |
3611 | 47.0k | if (!(state->reduced_still_picture_header || disable_cdf_update)) |
3612 | 16.5k | gf_bs_read_int_log(bs, 1, "disable_frame_end_update_cdf"); |
3613 | | |
3614 | 47.0k | if (primary_ref_frame == AV1_PRIMARY_REF_NONE) { |
3615 | | //init_non_coeff_cdfs(); |
3616 | 33.3k | av1_setup_past_independence(state); |
3617 | 33.3k | } |
3618 | 13.6k | else { |
3619 | | //load_cdfs(ref_frame_idx[primary_ref_frame]); |
3620 | 13.6k | av1_load_previous(state, primary_ref_frame, ref_frame_idx); |
3621 | 13.6k | } |
3622 | | |
3623 | 47.0k | av1_parse_tile_info(bs, state); |
3624 | | //quantization_params( ): |
3625 | 47.0k | u8 base_q_idx = gf_bs_read_int_log(bs, 8, "base_q_idx"); |
3626 | 47.0k | s32 DeltaQUDc = 0; |
3627 | 47.0k | s32 DeltaQUAc = 0; |
3628 | 47.0k | s32 DeltaQVDc = 0; |
3629 | 47.0k | s32 DeltaQVAc = 0; |
3630 | 47.0k | s32 DeltaQYDc = av1_delta_q(bs, "DeltaQYDc_coded", "DeltaQYDc"); |
3631 | 47.0k | if (!state->config || !state->config->monochrome) { |
3632 | 34.3k | u8 diff_uv_delta = 0; |
3633 | 34.3k | if (state->separate_uv_delta_q) |
3634 | 19.0k | diff_uv_delta = gf_bs_read_int_log(bs, 1, "diff_uv_delta"); |
3635 | | |
3636 | 34.3k | DeltaQUDc = av1_delta_q(bs, "DeltaQUDc_coded", "DeltaQUDc"); |
3637 | 34.3k | DeltaQUAc = av1_delta_q(bs, "DeltaQUAc_coded", "DeltaQUAc"); |
3638 | 34.3k | if (diff_uv_delta) { |
3639 | 11.9k | DeltaQVDc = av1_delta_q(bs, "DeltaQVDc_coded", "DeltaQVDc"); |
3640 | 11.9k | DeltaQVAc = av1_delta_q(bs, "DeltaQVAc_coded", "DeltaQVAc"); |
3641 | 11.9k | } |
3642 | 34.3k | } |
3643 | 47.0k | if (gf_bs_read_int_log(bs, 1, "using_qmatrix")) { |
3644 | 27.9k | gf_bs_read_int_log(bs, 4, "qm_y"); |
3645 | 27.9k | gf_bs_read_int_log(bs, 4, "qm_u"); |
3646 | 27.9k | if (state->separate_uv_delta_q) { |
3647 | 12.8k | gf_bs_read_int_log(bs, 4, "qm_v"); |
3648 | 12.8k | } |
3649 | 27.9k | } |
3650 | | |
3651 | 47.0k | u8 seg_features_SEG_LVL_ALT_Q_enabled[8] = { 0,0,0,0,0,0,0,0 }; |
3652 | 47.0k | s32 seg_features_SEG_LVL_ALT_Q[8] = { 0,0,0,0,0,0,0,0 }; |
3653 | | |
3654 | | //segmentation_params( ): |
3655 | 47.0k | u8 segmentation_enabled = gf_bs_read_int_log(bs, 1, "segmentation_enabled"); |
3656 | 47.0k | if (segmentation_enabled) { |
3657 | | /*u8 segmentation_temporal_update = 0;*/ |
3658 | 15.3k | u8 segmentation_update_data = 1; |
3659 | 15.3k | if (primary_ref_frame != AV1_PRIMARY_REF_NONE) { |
3660 | 4.60k | u8 segmentation_update_map = gf_bs_read_int_log(bs, 1, "segmentation_update_map"); |
3661 | 4.60k | if (segmentation_update_map == 1) |
3662 | 1.72k | gf_bs_read_int_log(bs, 1, "segmentation_temporal_update"); |
3663 | 4.60k | segmentation_update_data = gf_bs_read_int_log(bs, 1, "segmentation_update_data"); |
3664 | 4.60k | } |
3665 | 15.3k | if (segmentation_update_data == 1) { |
3666 | 12.2k | u32 i, j; |
3667 | 110k | for (i = 0; i < 8/*=MAX_SEGMENTS*/; i++) { |
3668 | 882k | for (j = 0; j < 8 /*=SEG_LVL_MAX*/; j++) { |
3669 | 784k | if (/*feature_enabled = */gf_bs_read_int_log_idx2(bs, 1, "feature_enabled", i, j) == 1) { |
3670 | 261k | s32 val; |
3671 | 261k | u32 bitsToRead = Segmentation_Feature_Bits[j]; |
3672 | | //this is SEG_LVL_ALT_Q |
3673 | 261k | if (!j) seg_features_SEG_LVL_ALT_Q_enabled[i] = 1; |
3674 | | |
3675 | 261k | if (Segmentation_Feature_Signed[j] == 1) { |
3676 | 166k | val = gf_bs_read_int_log_idx2(bs, 1 + bitsToRead, "signed_feature_value", i, j); |
3677 | 166k | } |
3678 | 94.3k | else { |
3679 | 94.3k | val = gf_bs_read_int_log_idx2(bs, bitsToRead, "feature_value", i, j); |
3680 | 94.3k | } |
3681 | 261k | if (!j) seg_features_SEG_LVL_ALT_Q[i] = val; |
3682 | 261k | } |
3683 | 784k | } |
3684 | 98.0k | } |
3685 | | //ignore all init steps |
3686 | 12.2k | } |
3687 | | |
3688 | 15.3k | } |
3689 | | |
3690 | | //delta_q_params(): |
3691 | | /*u8 delta_q_res = 0;*/ |
3692 | 47.0k | u8 delta_q_present = 0; |
3693 | 47.0k | if (base_q_idx > 0) { |
3694 | 41.3k | delta_q_present = gf_bs_read_int_log(bs, 1, "delta_q_present"); |
3695 | 41.3k | } |
3696 | 47.0k | if (delta_q_present) { |
3697 | 11.0k | gf_bs_read_int_log(bs, 2, "delta_q_res"); |
3698 | 11.0k | } |
3699 | | |
3700 | | //delta_lf_params(): |
3701 | 47.0k | u8 delta_lf_present = 0; |
3702 | | /*u8 delta_lf_res = 0; |
3703 | | u8 delta_lf_multi = 0;*/ |
3704 | 47.0k | if (delta_q_present) { |
3705 | 11.0k | if (!allow_intrabc) { |
3706 | 10.8k | delta_lf_present = gf_bs_read_int_log(bs, 1, "delta_lf_present"); |
3707 | 10.8k | } |
3708 | 11.0k | if (delta_lf_present) { |
3709 | 3.71k | gf_bs_read_int_log(bs, 2, "delta_lf_res"); |
3710 | 3.71k | gf_bs_read_int_log(bs, 1, "delta_lf_multi"); |
3711 | 3.71k | } |
3712 | 11.0k | } |
3713 | | |
3714 | | //init lossless stuff! |
3715 | 47.0k | u8 CodedLossless = 1; |
3716 | 423k | for (idx = 0; idx < 8; idx++) { |
3717 | 376k | u8 qindex = av1_get_qindex(GF_TRUE, idx, base_q_idx, delta_q_present, 0/*CurrentQIndex always ignored at this level of parsin*/, segmentation_enabled, seg_features_SEG_LVL_ALT_Q_enabled, seg_features_SEG_LVL_ALT_Q); |
3718 | 376k | Bool LosslessArray = (qindex == 0) && (DeltaQYDc == 0) && (DeltaQUAc == 0) && (DeltaQUDc == 0) && (DeltaQVAc == 0) && (DeltaQVDc == 0); |
3719 | 376k | if (!LosslessArray) |
3720 | 348k | CodedLossless = 0; |
3721 | 376k | } |
3722 | 47.0k | Bool AllLossless = CodedLossless && (state->width == state->UpscaledWidth); |
3723 | | |
3724 | | //loop_filter_params(): |
3725 | 47.0k | if (!CodedLossless && !allow_intrabc) { |
3726 | 43.2k | u8 loop_filter_level_0 = gf_bs_read_int_log(bs, 6, "loop_filter_level_0"); |
3727 | 43.2k | u8 loop_filter_level_1 = gf_bs_read_int_log(bs, 6, "loop_filter_level_1"); |
3728 | 43.2k | if (!state->config->monochrome) { |
3729 | 32.8k | if (loop_filter_level_0 || loop_filter_level_1) { |
3730 | 27.3k | gf_bs_read_int_log(bs, 6, "loop_filter_level_2"); |
3731 | 27.3k | gf_bs_read_int_log(bs, 6, "loop_filter_level_3"); |
3732 | 27.3k | } |
3733 | 32.8k | } |
3734 | 43.2k | gf_bs_read_int_log(bs, 3, "loop_filter_sharpness"); |
3735 | 43.2k | u8 loop_filter_delta_enabled = gf_bs_read_int_log(bs, 1, "loop_filter_delta_enabled"); |
3736 | 43.2k | if (loop_filter_delta_enabled == 1) { |
3737 | 10.0k | u8 loop_filter_delta_update = gf_bs_read_int_log(bs, 1, "loop_filter_delta_update"); |
3738 | 10.0k | if (loop_filter_delta_update) { |
3739 | 7.39k | u32 i; |
3740 | 66.5k | for (i = 0; i < 8/*TOTAL_REFS_PER_FRAME*/; i++) { |
3741 | 59.1k | u8 update_ref_delta = gf_bs_read_int_log_idx(bs, 1, "update_ref_delta", i); |
3742 | 59.1k | if (update_ref_delta == 1) { |
3743 | 29.1k | gf_bs_read_int_log_idx(bs, 1 + 6, "loop_filter_ref_deltas", i); |
3744 | 29.1k | } |
3745 | 59.1k | } |
3746 | 22.1k | for (i = 0; i < 2; i++) { |
3747 | 14.7k | u8 update_mode_delta = gf_bs_read_int_log_idx(bs, 1, "update_mode_delta", i); |
3748 | 14.7k | if (update_mode_delta) { |
3749 | 6.14k | gf_bs_read_int_log_idx(bs, 1 + 6, "loop_filter_mode_deltas", i); |
3750 | 6.14k | } |
3751 | 14.7k | } |
3752 | 7.39k | } |
3753 | 10.0k | } |
3754 | 43.2k | } |
3755 | | //cdef_params( ): |
3756 | 47.0k | if (!CodedLossless && !allow_intrabc && state->enable_cdef) { |
3757 | 17.9k | gf_bs_read_int_log(bs, 2, "cdef_damping_minus_3"); |
3758 | 17.9k | u8 cdef_bits = gf_bs_read_int_log(bs, 2, "cdef_bits"); |
3759 | 17.9k | u32 i, num_cd = 1 << cdef_bits; |
3760 | 55.7k | for (i = 0; i < num_cd; i++) { |
3761 | 37.8k | gf_bs_read_int_log_idx(bs, 4, "cdef_y_pri_strength", i); |
3762 | 37.8k | gf_bs_read_int_log_idx(bs, 2, "cdef_y_sec_strength", i); |
3763 | 37.8k | if (!state->config->monochrome) { |
3764 | 27.1k | gf_bs_read_int_log_idx(bs, 4, "cdef_uv_pri_strength", i); |
3765 | 27.1k | gf_bs_read_int_log_idx(bs, 2, "cdef_uv_sec_strength", i); |
3766 | 27.1k | } |
3767 | 37.8k | } |
3768 | 17.9k | } |
3769 | | |
3770 | | //lr_params( ) : |
3771 | 47.0k | if (!AllLossless && !allow_intrabc && state->enable_restoration) { |
3772 | 20.7k | u32 i, nb_planes = state->config->monochrome ? 1 : 3; |
3773 | 20.7k | u8 UsesLr = 0; |
3774 | 20.7k | u8 usesChromaLr = 0; |
3775 | 71.9k | for (i = 0; i < nb_planes; i++) { |
3776 | 51.1k | u8 lr_type = gf_bs_read_int_log_idx(bs, 2, "lr_type", i); |
3777 | | //FrameRestorationType[i] = Remap_Lr_Type[lr_type] |
3778 | 51.1k | if (lr_type != AV1_RESTORE_NONE) { |
3779 | 22.6k | UsesLr = 1; |
3780 | 22.6k | if (i > 0) { |
3781 | 11.4k | usesChromaLr = 1; |
3782 | 11.4k | } |
3783 | 22.6k | } |
3784 | 51.1k | } |
3785 | 20.7k | if (UsesLr) { |
3786 | 12.5k | if (state->use_128x128_superblock) { |
3787 | 2.90k | gf_bs_read_int_log(bs, 1, "lr_unit_shift_minus_1"); |
3788 | 2.90k | } |
3789 | 9.65k | else { |
3790 | 9.65k | u8 lr_unit_shift = gf_bs_read_int_log(bs, 1, "lr_unit_shift"); |
3791 | 9.65k | if (lr_unit_shift) { |
3792 | 4.71k | gf_bs_read_int_log(bs, 1, "lr_unit_extra_shift"); |
3793 | | //lr_unit_shift += lr_unit_extra_shift; |
3794 | 4.71k | } |
3795 | 9.65k | } |
3796 | 12.5k | if (state->config->chroma_subsampling_x && state->config->chroma_subsampling_y && usesChromaLr) { |
3797 | 556 | gf_bs_read_int_log(bs, 1, "lr_uv_shift"); |
3798 | 556 | } |
3799 | 12.5k | } |
3800 | 20.7k | } |
3801 | | //read_tx_mode(): |
3802 | 47.0k | if (CodedLossless == 1) { |
3803 | 3.35k | } |
3804 | 43.6k | else { |
3805 | 43.6k | gf_bs_read_int_log(bs, 1, "tx_mode_select"); |
3806 | 43.6k | } |
3807 | | |
3808 | | //frame_reference_mode( ): |
3809 | 47.0k | u8 reference_select = 0; |
3810 | 47.0k | if (FrameIsIntra) { |
3811 | 18.8k | } |
3812 | 28.1k | else { |
3813 | 28.1k | reference_select = gf_bs_read_int_log(bs, 1, "reference_select"); |
3814 | 28.1k | } |
3815 | | |
3816 | | //skip_mode_params( ): |
3817 | 47.0k | u8 skipModeAllowed = 0; |
3818 | 47.0k | if (FrameIsIntra || !reference_select || !state->enable_order_hint) { |
3819 | 37.3k | } |
3820 | 9.67k | else { |
3821 | 9.67k | u32 i; |
3822 | 9.67k | s32 forwardIdx = -1; |
3823 | 9.67k | s32 backwardIdx = -1; |
3824 | 9.67k | s32 forwardHint = 0; |
3825 | 9.67k | s32 backwardHint = 0; |
3826 | 77.3k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
3827 | 67.6k | u8 refHint = state->RefOrderHint[ref_frame_idx[i]]; |
3828 | 67.6k | if (av1_get_relative_dist(refHint, frame_state->order_hint, state) < 0) { |
3829 | 35.8k | if (forwardIdx < 0 || av1_get_relative_dist(refHint, forwardHint, state) > 0) { |
3830 | 9.05k | forwardIdx = i; |
3831 | 9.05k | forwardHint = refHint; |
3832 | 9.05k | } |
3833 | 35.8k | } |
3834 | 31.8k | else if (av1_get_relative_dist(refHint, frame_state->order_hint, state) > 0) { |
3835 | 12.8k | if (backwardIdx < 0 || av1_get_relative_dist(refHint, backwardHint, state) < 0) { |
3836 | 4.24k | backwardIdx = i; |
3837 | 4.24k | backwardHint = refHint; |
3838 | 4.24k | } |
3839 | 12.8k | } |
3840 | 67.6k | } |
3841 | 9.67k | if (forwardIdx < 0) { |
3842 | 2.39k | skipModeAllowed = 0; |
3843 | 2.39k | } |
3844 | 7.28k | else if (backwardIdx >= 0) { |
3845 | 2.02k | skipModeAllowed = 1; |
3846 | | //SkipModeFrame[0] = AV1_LAST_FRAME + MIN(forwardIdx, backwardIdx); |
3847 | | //SkipModeFrame[1] = AV1_LAST_FRAME + MAX(forwardIdx, backwardIdx); |
3848 | 2.02k | } |
3849 | 5.25k | else { |
3850 | 5.25k | s32 secondForwardIdx = -1; |
3851 | 5.25k | s32 secondForwardHint = 0; |
3852 | 42.0k | for (i = 0; i < AV1_REFS_PER_FRAME; i++) { |
3853 | 36.7k | u8 refHint = state->RefOrderHint[ref_frame_idx[i]]; |
3854 | 36.7k | if (av1_get_relative_dist(refHint, forwardHint, state) < 0) { |
3855 | 7.33k | if (secondForwardIdx < 0 || av1_get_relative_dist(refHint, secondForwardHint, state) > 0) { |
3856 | 2.64k | secondForwardIdx = i; |
3857 | 2.64k | secondForwardHint = refHint; |
3858 | 2.64k | } |
3859 | 7.33k | } |
3860 | 36.7k | } |
3861 | 5.25k | if (secondForwardIdx < 0) { |
3862 | 3.12k | skipModeAllowed = 0; |
3863 | 3.12k | } |
3864 | 2.13k | else { |
3865 | 2.13k | skipModeAllowed = 1; |
3866 | | //SkipModeFrame[ 0 ] = LAST_FRAME + Min(forwardIdx, secondForwardIdx) |
3867 | | //SkipModeFrame[ 1 ] = LAST_FRAME + Max(forwardIdx, secondForwardIdx) |
3868 | 2.13k | } |
3869 | 5.25k | } |
3870 | 9.67k | } |
3871 | 47.0k | if (skipModeAllowed) { |
3872 | 4.15k | gf_bs_read_int_log(bs, 1, "skip_mode_present"); |
3873 | 4.15k | } |
3874 | | |
3875 | | |
3876 | 47.0k | if (FrameIsIntra || error_resilient_mode || !state->enable_warped_motion) { |
3877 | | |
3878 | 42.7k | } |
3879 | 4.25k | else { |
3880 | 4.25k | gf_bs_read_int_log(bs, 1, "allow_warped_motion"); |
3881 | 4.25k | } |
3882 | | |
3883 | 47.0k | gf_bs_read_int_log(bs, 1, "reduced_tx"); |
3884 | | |
3885 | | //global_motion_params( ) |
3886 | 47.0k | u32 ref; |
3887 | 376k | for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ref++) { |
3888 | 329k | u32 i; |
3889 | 2.30M | for (i = 0; i < 6; i++) { |
3890 | 1.97M | state->GmParams.coefs[ref][i] = ((i % 3 == 2) ? 1 << WARPEDMODEL_PREC_BITS : 0); |
3891 | 1.97M | } |
3892 | 329k | } |
3893 | 47.0k | if (!FrameIsIntra) { |
3894 | 28.1k | u32 refs; |
3895 | 224k | for (refs = AV1_LAST_FRAME; refs <= AV1_ALTREF_FRAME; refs++) { |
3896 | 196k | u8 type = AV1_GMC_IDENTITY; |
3897 | 196k | Bool is_global = gf_bs_read_int_log_idx(bs, 1, "is_global", refs); |
3898 | 196k | if (is_global) { |
3899 | 63.3k | Bool is_rot_zoom = gf_bs_read_int_log_idx(bs, 1, "is_rot_zoom", refs); |
3900 | 63.3k | if (is_rot_zoom) { |
3901 | 31.0k | type = AV1_GMC_ROTZOOM; |
3902 | 31.0k | } |
3903 | 32.3k | else { |
3904 | 32.3k | Bool is_trans = gf_bs_read_int_log_idx(bs, 1, "is_translation", refs); |
3905 | 32.3k | type = is_trans ? AV1_GMC_TRANSLATION : AV1_GMC_AFFINE; |
3906 | | |
3907 | 32.3k | } |
3908 | 63.3k | } |
3909 | | |
3910 | 196k | if (type >= AV1_GMC_ROTZOOM) { |
3911 | 58.5k | av1_read_global_param(state, bs, type, refs, 2); |
3912 | 58.5k | av1_read_global_param(state, bs, type, refs, 3); |
3913 | 58.5k | if (type == AV1_GMC_AFFINE) { |
3914 | 27.5k | av1_read_global_param(state, bs, type, refs, 4); |
3915 | 27.5k | av1_read_global_param(state, bs, type, refs, 5); |
3916 | 27.5k | } |
3917 | 31.0k | else { |
3918 | 31.0k | state->GmParams.coefs[refs][4] = -state->GmParams.coefs[refs][3]; |
3919 | 31.0k | state->GmParams.coefs[refs][5] = state->GmParams.coefs[refs][2]; |
3920 | | |
3921 | 31.0k | } |
3922 | 58.5k | } |
3923 | 196k | if (type >= AV1_GMC_TRANSLATION) { |
3924 | 63.3k | av1_read_global_param(state, bs, type, refs, 0); |
3925 | 63.3k | av1_read_global_param(state, bs, type, refs, 1); |
3926 | 63.3k | } |
3927 | 196k | } |
3928 | 28.1k | } |
3929 | | |
3930 | | //film_grain_params() |
3931 | 47.0k | if (!state->film_grain_params_present || (!state->frame_state.show_frame && !showable_frame)) { |
3932 | 21.8k | } |
3933 | 25.1k | else { |
3934 | 25.1k | u8 apply_grain = gf_bs_read_int_log(bs, 1, "apply_grain"); |
3935 | 25.1k | if (apply_grain) { |
3936 | 7.04k | gf_bs_read_int_log(bs, 16, "grain_seed"); |
3937 | 7.04k | u8 update_grain = 1; |
3938 | 7.04k | if (state->frame_state.frame_type == AV1_INTER_FRAME) { |
3939 | 1.45k | update_grain = gf_bs_read_int_log(bs, 1, "update_grain"); |
3940 | 1.45k | } |
3941 | 7.04k | if (!update_grain) { |
3942 | 1.00k | gf_bs_read_int_log(bs, 3, "film_grain_params_ref_idx"); |
3943 | 1.00k | } |
3944 | 6.04k | else { |
3945 | 6.04k | u32 i, num_y_points = gf_bs_read_int_log(bs, 4, "num_y_points"); |
3946 | 28.6k | for (i = 0; i < num_y_points; i++) { |
3947 | 22.6k | gf_bs_read_int_log_idx(bs, 8, "point_y_value", i); |
3948 | 22.6k | gf_bs_read_int_log_idx(bs, 8, "point_y_scaling", i); |
3949 | 22.6k | } |
3950 | 6.04k | u8 chroma_scaling_from_luma = 0; |
3951 | 6.04k | if (!state->config->monochrome) |
3952 | 4.62k | chroma_scaling_from_luma = gf_bs_read_int_log(bs, 1, "chroma_scaling_from_luma"); |
3953 | | |
3954 | 6.04k | u8 num_cb_points = 0; |
3955 | 6.04k | u8 num_cr_points = 0; |
3956 | 6.04k | if (state->config->monochrome || chroma_scaling_from_luma || |
3957 | 6.04k | ((state->config->chroma_subsampling_x == 1) && (state->config->chroma_subsampling_y == 1) && (num_y_points == 0)) |
3958 | 6.04k | ) { |
3959 | 3.13k | } |
3960 | 2.91k | else { |
3961 | 2.91k | num_cb_points = gf_bs_read_int_log(bs, 4, "num_cb_points"); |
3962 | 6.33k | for (i = 0; i < num_cb_points; i++) { |
3963 | 3.42k | gf_bs_read_int_log_idx(bs, 8, "point_cb_value", i); |
3964 | 3.42k | gf_bs_read_int_log_idx(bs, 8, "point_cb_scaling", i); |
3965 | 3.42k | } |
3966 | 2.91k | num_cr_points = gf_bs_read_int_log(bs, 4, "num_cr_points"); |
3967 | 12.5k | for (i = 0; i < num_cr_points; i++) { |
3968 | 9.64k | gf_bs_read_int_log_idx(bs, 8, "point_cr_value", i); |
3969 | 9.64k | gf_bs_read_int_log_idx(bs, 8, "point_cr_scaling", i); |
3970 | 9.64k | } |
3971 | 2.91k | } |
3972 | 6.04k | gf_bs_read_int_log(bs, 2, "grain_scaling_minus_8"); |
3973 | 6.04k | u8 ar_coeff_lag = gf_bs_read_int_log(bs, 2, "ar_coeff_lag"); |
3974 | 6.04k | u16 numPosLuma = 2 * ar_coeff_lag * (ar_coeff_lag + 1); |
3975 | 6.04k | u16 numPosChroma = numPosLuma; |
3976 | 6.04k | if (num_y_points) { |
3977 | 4.85k | numPosChroma = numPosLuma + 1; |
3978 | 55.7k | for (i = 0; i < numPosLuma; i++) { |
3979 | 50.9k | gf_bs_read_int_log_idx(bs, 8, "ar_coeffs_y_plus_128", i); |
3980 | 50.9k | } |
3981 | 4.85k | } |
3982 | 6.04k | if (chroma_scaling_from_luma || num_cb_points) { |
3983 | 44.0k | for (i = 0; i < numPosChroma; i++) { |
3984 | 41.6k | gf_bs_read_int_log_idx(bs, 8, "ar_coeffs_cb_plus_128", i); |
3985 | 41.6k | } |
3986 | 2.42k | } |
3987 | 6.04k | if (chroma_scaling_from_luma || num_cr_points) { |
3988 | 48.5k | for (i = 0; i < numPosChroma; i++) { |
3989 | 46.0k | gf_bs_read_int_log_idx(bs, 8, "ar_coeffs_cr_plus_128", i); |
3990 | 46.0k | } |
3991 | 2.49k | } |
3992 | 6.04k | gf_bs_read_int_log(bs, 2, "ar_coeff_shift_minus_6"); |
3993 | 6.04k | gf_bs_read_int_log(bs, 2, "grain_scale_shift"); |
3994 | 6.04k | if (num_cb_points) { |
3995 | 976 | gf_bs_read_int_log(bs, 8, "cb_mult"); |
3996 | 976 | gf_bs_read_int_log(bs, 8, "cb_luma_mult"); |
3997 | 976 | gf_bs_read_int_log(bs, 9, "cb_offset"); |
3998 | 976 | } |
3999 | 6.04k | if (num_cr_points) { |
4000 | 1.04k | gf_bs_read_int_log(bs, 8, "cr_mult"); |
4001 | 1.04k | gf_bs_read_int_log(bs, 8, "cr_luma_mult"); |
4002 | 1.04k | gf_bs_read_int_log(bs, 9, "cr_offset"); |
4003 | 1.04k | } |
4004 | 6.04k | gf_bs_read_int_log(bs, 1, "overlap_flag"); |
4005 | 6.04k | gf_bs_read_int_log(bs, 1, "clip_to_restricted_range"); |
4006 | 6.04k | } |
4007 | 7.04k | } |
4008 | 25.1k | } |
4009 | | |
4010 | | //end of uncompressed header !! |
4011 | 47.0k | } |
4012 | | |
4013 | | GF_EXPORT |
4014 | | void gf_av1_init_state(AV1State *state) |
4015 | 56.5k | { |
4016 | 56.5k | if (!state) return; |
4017 | 56.5k | memset(state, 0, sizeof(AV1State)); |
4018 | 56.5k | state->color_primaries = 2; |
4019 | 56.5k | state->transfer_characteristics = 2; |
4020 | 56.5k | state->matrix_coefficients = 2; |
4021 | 56.5k | } |
4022 | | |
4023 | | GF_EXPORT |
4024 | | void gf_av1_reset_state(AV1State *state, Bool is_destroy) |
4025 | 68.0k | { |
4026 | 68.0k | GF_List *l1, *l2; |
4027 | | |
4028 | 68.0k | if (state->frame_state.header_obus) { |
4029 | 148k | while (gf_list_count(state->frame_state.header_obus)) { |
4030 | 127k | GF_AV1_OBUArrayEntry *a = (GF_AV1_OBUArrayEntry*)gf_list_pop_back(state->frame_state.header_obus); |
4031 | 127k | if (a->obu) gf_free(a->obu); |
4032 | 127k | gf_free(a); |
4033 | 127k | } |
4034 | 20.3k | } |
4035 | | |
4036 | 68.0k | if (state->frame_state.frame_obus) { |
4037 | 84.4k | while (gf_list_count(state->frame_state.frame_obus)) { |
4038 | 77.2k | GF_AV1_OBUArrayEntry *a = (GF_AV1_OBUArrayEntry*)gf_list_pop_back(state->frame_state.frame_obus); |
4039 | 77.2k | if (a->obu) gf_free(a->obu); |
4040 | 77.2k | gf_free(a); |
4041 | 77.2k | } |
4042 | 7.17k | } |
4043 | 68.0k | l1 = state->frame_state.frame_obus; |
4044 | 68.0k | l2 = state->frame_state.header_obus; |
4045 | 68.0k | memset(&state->frame_state, 0, sizeof(AV1StateFrame)); |
4046 | 68.0k | state->frame_state.is_first_frame = GF_TRUE; |
4047 | 68.0k | state->has_frame_data = 0; |
4048 | | |
4049 | 68.0k | if (is_destroy) { |
4050 | 53.6k | gf_list_del(l1); |
4051 | 53.6k | gf_list_del(l2); |
4052 | 53.6k | if (state->bs) { |
4053 | | //cf issues #1893 and #2604: |
4054 | | //state->frame_obus is either: |
4055 | | //- NULL, in which case there is a valid buffer in bs, freed by bs_del |
4056 | | //- not NULL, in which case the internal buffer of bs is NULL and we must free the buffer |
4057 | | |
4058 | 213 | if (state->frame_obus) { |
4059 | 166 | gf_free(state->frame_obus); |
4060 | 166 | state->frame_obus = NULL; |
4061 | 166 | state->frame_obus_alloc = 0; |
4062 | 166 | } |
4063 | 213 | gf_bs_del(state->bs); |
4064 | 213 | state->bs = NULL; |
4065 | 213 | } |
4066 | 53.6k | } |
4067 | 14.3k | else { |
4068 | 14.3k | state->frame_state.frame_obus = l1; |
4069 | 14.3k | state->frame_state.header_obus = l2; |
4070 | 14.3k | if (state->bs) |
4071 | 11.4k | gf_bs_seek(state->bs, 0); |
4072 | 14.3k | } |
4073 | 68.0k | } |
4074 | | |
4075 | | static GF_Err av1_parse_tile_group(GF_BitStream *bs, AV1State *state, u64 obu_start, u64 obu_size) |
4076 | 42.6k | { |
4077 | 42.6k | u32 TileNum, tg_start = 0, tg_end = 0; |
4078 | 42.6k | u32 numTiles = state->tileCols * state->tileRows; |
4079 | 42.6k | Bool tile_start_and_end_present_flag = GF_FALSE; |
4080 | 42.6k | GF_Err e = GF_OK; |
4081 | 42.6k | if (numTiles > 1) |
4082 | 10.7k | tile_start_and_end_present_flag = gf_bs_read_int_log(bs, 1, "tile_start_and_end_present_flag"); |
4083 | | |
4084 | 42.6k | if (numTiles == 1 || !tile_start_and_end_present_flag) { |
4085 | 41.8k | tg_start = 0; |
4086 | 41.8k | tg_end = numTiles - 1; |
4087 | | /*state->frame_state.tg[0].start_idx = 0; |
4088 | | state->frame_state.tg[0].end_idx = numTiles - 1;*/ |
4089 | 41.8k | } |
4090 | 839 | else { |
4091 | 839 | u32 tileBits = state->tileColsLog2 + state->tileRowsLog2; |
4092 | 839 | /*state->frame_state.tg[state->frame_state.tg_idx].start_idx*/ tg_start = gf_bs_read_int_log(bs, tileBits, "tg_start"); |
4093 | 839 | /*state->frame_state.tg[state->frame_state.tg_idx].end_idx*/ tg_end = gf_bs_read_int_log(bs, tileBits, "tg_end"); |
4094 | 839 | } |
4095 | | /*state->frame_state.tg_idx++;*/ |
4096 | | |
4097 | 42.6k | gf_bs_align(bs); |
4098 | | |
4099 | 42.6k | if (tg_end >= GF_ARRAY_LENGTH(state->frame_state.tiles)) |
4100 | 2.55k | return GF_NON_COMPLIANT_BITSTREAM; |
4101 | | |
4102 | 40.1k | state->frame_state.nb_tiles_in_obu = 0; |
4103 | 73.4k | for (TileNum = tg_start; TileNum <= tg_end; TileNum++) { |
4104 | 41.8k | u32 tile_start_offset, tile_size; |
4105 | | /*u32 tileRow = TileNum / state->tileCols; |
4106 | | u32 tileCol = TileNum % state->tileCols;*/ |
4107 | 41.8k | Bool lastTile = TileNum == tg_end; |
4108 | 41.8k | u64 pos = gf_bs_get_position(bs); |
4109 | 41.8k | if (lastTile) { |
4110 | 31.0k | tile_start_offset = (u32)(pos - obu_start); |
4111 | 31.0k | tile_size = (u32)(obu_size - (pos - obu_start)); |
4112 | 31.0k | } |
4113 | 10.7k | else { |
4114 | 10.7k | u64 tile_size_minus_1 = aom_av1_le(bs, state->tile_size_bytes, "tile_size_minus_1"); |
4115 | 10.7k | pos = gf_bs_get_position(bs); |
4116 | 10.7k | tile_start_offset = (u32)(pos - obu_start); |
4117 | 10.7k | tile_size = (u32)(tile_size_minus_1 + 1/* + state->tile_size_bytes*/); |
4118 | 10.7k | } |
4119 | | |
4120 | | |
4121 | 41.8k | if (tile_start_offset + tile_size > obu_size) { |
4122 | 8.50k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] Error parsing tile group, tile %d start %d + size %d exceeds OBU length %d\n", TileNum, tile_start_offset, tile_size, obu_size)); |
4123 | 8.50k | e = GF_NON_COMPLIANT_BITSTREAM; |
4124 | 8.50k | break; |
4125 | 8.50k | } |
4126 | | |
4127 | 33.3k | state->frame_state.tiles[state->frame_state.nb_tiles_in_obu].obu_start_offset = tile_start_offset; |
4128 | 33.3k | state->frame_state.tiles[state->frame_state.nb_tiles_in_obu].size = tile_size; |
4129 | 33.3k | gf_bs_skip_bytes(bs, tile_size); |
4130 | 33.3k | state->frame_state.nb_tiles_in_obu++; |
4131 | 33.3k | } |
4132 | 40.1k | if (tg_end == numTiles - 1) { |
4133 | 39.3k | av1_decode_frame_wrapup(state); |
4134 | 39.3k | } |
4135 | 40.1k | return e; |
4136 | 42.6k | } |
4137 | | |
4138 | | static void av1_parse_frame_header(GF_BitStream *bs, AV1State *state) |
4139 | 49.5k | { |
4140 | 49.5k | AV1StateFrame *frame_state = &state->frame_state; |
4141 | 49.5k | if (frame_state->seen_frame_header == GF_FALSE) { |
4142 | 48.5k | u64 pos = gf_bs_get_position(bs); |
4143 | 48.5k | state->frame_state.show_existing_frame = GF_FALSE; |
4144 | 48.5k | frame_state->seen_frame_header = GF_TRUE; |
4145 | 48.5k | av1_parse_uncompressed_header(bs, state); |
4146 | 48.5k | state->frame_state.is_first_frame = GF_FALSE; |
4147 | 48.5k | state->frame_state.uncompressed_header_bytes = (u32) (gf_bs_get_position(bs) - pos); |
4148 | | |
4149 | 48.5k | if (state->frame_state.show_existing_frame) { |
4150 | 1.53k | av1_decode_frame_wrapup(state); |
4151 | 1.53k | frame_state->seen_frame_header = GF_FALSE; |
4152 | 1.53k | } |
4153 | 47.0k | else { |
4154 | | //TileNum = 0; |
4155 | 47.0k | frame_state->seen_frame_header = GF_TRUE; |
4156 | 47.0k | } |
4157 | 48.5k | } |
4158 | 49.5k | } |
4159 | | |
4160 | | static GF_Err av1_parse_frame(GF_BitStream *bs, AV1State *state, u64 obu_start, u64 obu_size) |
4161 | 36.6k | { |
4162 | 36.6k | av1_parse_frame_header(bs, state); |
4163 | | //byte alignment |
4164 | 36.6k | { |
4165 | 36.6k | u32 nbBits = gf_bs_align(bs); |
4166 | 36.6k | gf_bs_log_idx(bs, nbBits, "alignment", 0, -1, -1, -1); |
4167 | 36.6k | } |
4168 | 36.6k | return av1_parse_tile_group(bs, state, obu_start, obu_size); |
4169 | 36.6k | } |
4170 | | |
4171 | | static void av1_parse_obu_metadata(AV1State *state, GF_BitStream *bs) |
4172 | 677k | { |
4173 | 677k | u32 metadata_type = (u32)gf_av1_leb128_read(bs, NULL); |
4174 | | |
4175 | 677k | switch (metadata_type) { |
4176 | 14 | case OBU_METADATA_TYPE_ITUT_T35: |
4177 | 14 | break; |
4178 | 171 | case OBU_METADATA_TYPE_HDR_CLL: |
4179 | 171 | gf_bs_read_data(bs, state->clli_data, 4); |
4180 | 171 | state->clli_valid = 1; |
4181 | 171 | break; |
4182 | 11.8k | case OBU_METADATA_TYPE_HDR_MDCV: |
4183 | 11.8k | gf_bs_read_data(bs, state->mdcv_data, 24); |
4184 | 11.8k | state->mdcv_valid = 1; |
4185 | 11.8k | break; |
4186 | 665k | default: |
4187 | 665k | break; |
4188 | 677k | } |
4189 | 677k | } |
4190 | | |
4191 | | GF_EXPORT |
4192 | | GF_Err gf_av1_parse_obu(GF_BitStream *bs, ObuType *obu_type, u64 *obu_size, u32 *obu_hdr_size, AV1State *state) |
4193 | 984k | { |
4194 | 984k | GF_Err e = GF_OK; |
4195 | 984k | u32 i, hdr_size; |
4196 | 984k | u64 pos = gf_bs_get_position(bs); |
4197 | | |
4198 | 984k | if (!bs || !obu_type || !state) |
4199 | 0 | return GF_BAD_PARAM; |
4200 | | |
4201 | 984k | gf_bs_mark_overflow(bs, GF_TRUE); |
4202 | | |
4203 | 984k | state->obu_extension_flag = state->obu_has_size_field = 0; |
4204 | 984k | state->temporal_id = state->spatial_id = 0; |
4205 | 984k | state->frame_state.uncompressed_header_bytes = 0; |
4206 | 984k | e = gf_av1_parse_obu_header(bs, obu_type, &state->obu_extension_flag, &state->obu_has_size_field, &state->temporal_id, &state->spatial_id); |
4207 | 984k | if (gf_bs_is_overflow(bs)) e = GF_NON_COMPLIANT_BITSTREAM; |
4208 | 984k | if (e) |
4209 | 6.98k | return e; |
4210 | | |
4211 | | //at this point obu_size is either zero or the size of the containing buffer (likely the Temporal Unit) |
4212 | 977k | if (state->obu_has_size_field) { |
4213 | 969k | *obu_size = (u32)gf_av1_leb128_read(bs, NULL); |
4214 | 969k | } |
4215 | 7.74k | else { |
4216 | 7.74k | if (*obu_size >= 1 + state->obu_extension_flag) { |
4217 | 1.10k | *obu_size = *obu_size - 1 - state->obu_extension_flag; |
4218 | 1.10k | } |
4219 | 6.63k | else { |
4220 | 6.63k | GF_LOG(state->config ? GF_LOG_WARNING : GF_LOG_DEBUG, GF_LOG_CODING, ("[AV1] computed OBU size "LLD" (input value = "LLU"). Skipping.\n", *obu_size - 1 - state->obu_extension_flag, *obu_size)); |
4221 | 6.63k | return GF_NON_COMPLIANT_BITSTREAM; |
4222 | 6.63k | } |
4223 | 7.74k | } |
4224 | 970k | hdr_size = (u32)(gf_bs_get_position(bs) - pos); |
4225 | 970k | if (gf_bs_is_overflow(bs) || (gf_bs_available(bs) < *obu_size) ) { |
4226 | 3.33k | gf_bs_seek(bs, pos); |
4227 | 3.33k | return GF_BUFFER_TOO_SMALL; |
4228 | 3.33k | } |
4229 | | //gpac's internal obu_size includes the header + the payload |
4230 | 967k | *obu_size += hdr_size; |
4231 | 967k | if (obu_hdr_size) *obu_hdr_size = hdr_size; |
4232 | 967k | if (!*obu_size) |
4233 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
4234 | | |
4235 | 967k | if (*obu_type != OBU_SEQUENCE_HEADER && *obu_type != OBU_TEMPORAL_DELIMITER && |
4236 | 967k | state->OperatingPointIdc != 0 && state->obu_extension_flag == 1) |
4237 | 0 | { |
4238 | 0 | u32 inTemporalLayer = (state->OperatingPointIdc >> state->temporal_id) & 1; |
4239 | 0 | u32 inSpatialLayer = (state->OperatingPointIdc >> (state->spatial_id + 8)) & 1; |
4240 | 0 | if (!inTemporalLayer || !inSpatialLayer) { |
4241 | 0 | *obu_type = -1; |
4242 | 0 | gf_bs_seek(bs, pos + *obu_size); |
4243 | 0 | return GF_OK; |
4244 | 0 | } |
4245 | 0 | } |
4246 | | |
4247 | 967k | e = GF_OK; |
4248 | | |
4249 | | /* for AVIF a1lx */ |
4250 | 4.66M | for (i = state->spatial_id; i < 4; i++) { |
4251 | 3.69M | state->layer_size[i] = (u32) (pos + *obu_size); |
4252 | 3.69M | } |
4253 | | |
4254 | 967k | switch (*obu_type) { |
4255 | 99.6k | case OBU_SEQUENCE_HEADER: |
4256 | 99.6k | if (state->config) { |
4257 | 99.6k | av1_parse_sequence_header_obu(bs, state); |
4258 | 99.6k | if (gf_bs_is_overflow(bs) || (gf_bs_get_position(bs) > pos + *obu_size)) { |
4259 | 4.18k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Sequence header parsing consumed too many bytes !\n")); |
4260 | 4.18k | e = GF_NON_COMPLIANT_BITSTREAM; |
4261 | 4.18k | } |
4262 | 99.6k | } |
4263 | 99.6k | gf_bs_seek(bs, pos + *obu_size); |
4264 | 99.6k | break; |
4265 | | |
4266 | 677k | case OBU_METADATA: |
4267 | 677k | av1_parse_obu_metadata(state, bs); |
4268 | 677k | gf_bs_seek(bs, pos + *obu_size); |
4269 | 677k | if (gf_bs_is_overflow(bs)) e = GF_NON_COMPLIANT_BITSTREAM; |
4270 | 677k | break; |
4271 | | |
4272 | 1.71k | case OBU_FRAME_HEADER: |
4273 | 12.9k | case OBU_REDUNDANT_FRAME_HEADER: |
4274 | 12.9k | if (state->config) { |
4275 | 12.9k | av1_parse_frame_header(bs, state); |
4276 | 12.9k | if (gf_bs_is_overflow(bs) || (gf_bs_get_position(bs) > pos + *obu_size)) { |
4277 | 4.29k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Frame header parsing consumed too many bytes !\n")); |
4278 | 4.29k | e = GF_NON_COMPLIANT_BITSTREAM; |
4279 | 4.29k | } |
4280 | 12.9k | } |
4281 | 12.9k | gf_bs_seek(bs, pos + *obu_size); |
4282 | 12.9k | break; |
4283 | 36.6k | case OBU_FRAME: |
4284 | 36.6k | e = av1_parse_frame(bs, state, pos, *obu_size); |
4285 | 36.6k | if (gf_bs_is_overflow(bs) || (gf_bs_get_position(bs) != pos + *obu_size)) { |
4286 | 13.2k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Frame parsing did not consume the right number of bytes !\n")); |
4287 | 13.2k | e = GF_NON_COMPLIANT_BITSTREAM; |
4288 | 13.2k | } |
4289 | 36.6k | gf_bs_seek(bs, pos + *obu_size); |
4290 | 36.6k | break; |
4291 | 6.00k | case OBU_TILE_GROUP: |
4292 | 6.00k | if (state->config) { |
4293 | 6.00k | e = av1_parse_tile_group(bs, state, pos, *obu_size); |
4294 | 6.00k | if (gf_bs_is_overflow(bs) || (gf_bs_get_position(bs) != pos + *obu_size)) { |
4295 | 834 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Tile group parsing did not consume the right number of bytes !\n")); |
4296 | 834 | e = GF_NON_COMPLIANT_BITSTREAM; |
4297 | 834 | } |
4298 | 6.00k | } |
4299 | 6.00k | gf_bs_seek(bs, pos + *obu_size); |
4300 | 6.00k | break; |
4301 | 65.5k | case OBU_TEMPORAL_DELIMITER: |
4302 | 65.5k | state->frame_state.seen_frame_header = GF_FALSE; |
4303 | | // fallthru |
4304 | 66.6k | case OBU_PADDING: |
4305 | 66.6k | gf_bs_seek(bs, pos + *obu_size); |
4306 | 66.6k | break; |
4307 | 67.5k | default: |
4308 | 67.5k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] unknown OBU type %u (size "LLU"). Skipping.\n", *obu_type, *obu_size)); |
4309 | 67.5k | gf_bs_seek(bs, pos + *obu_size); |
4310 | 67.5k | break; |
4311 | 967k | } |
4312 | 967k | return e; |
4313 | 967k | } |
4314 | | |
4315 | | |
4316 | | GF_EXPORT |
4317 | | GF_Err gf_media_prores_parse_bs(GF_BitStream *bs, GF_ProResFrameInfo *prores_frame) |
4318 | 0 | { |
4319 | 0 | u32 i, j; |
4320 | 0 | u64 start, pos; |
4321 | 0 | memset(prores_frame, 0, sizeof(GF_ProResFrameInfo)); |
4322 | |
|
4323 | 0 | start = gf_bs_get_position(bs); |
4324 | 0 | if (gf_bs_available(bs) < 10) |
4325 | 0 | return GF_BUFFER_TOO_SMALL; |
4326 | | |
4327 | 0 | prores_frame->frame_size = gf_bs_read_u32(bs); |
4328 | 0 | prores_frame->frame_identifier = gf_bs_read_u32(bs); |
4329 | 0 | if (prores_frame->frame_identifier != GF_4CC('i','c','p','f')) { |
4330 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[ProRes] Invalid frame identifier, expected \"icpf\" got \"%s\"\n", gf_4cc_to_str(prores_frame->frame_identifier) )); |
4331 | 0 | gf_bs_seek(bs, start); |
4332 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
4333 | 0 | } |
4334 | | /*parse frame header*/ |
4335 | 0 | pos = gf_bs_get_position(bs); |
4336 | 0 | prores_frame->frame_hdr_size = gf_bs_read_u16(bs); |
4337 | 0 | if (gf_bs_available(bs) + 2 < prores_frame->frame_hdr_size) { |
4338 | 0 | gf_bs_seek(bs, start); |
4339 | 0 | return GF_BUFFER_TOO_SMALL; |
4340 | 0 | } |
4341 | 0 | gf_bs_read_u8(bs); |
4342 | 0 | prores_frame->version = gf_bs_read_u8(bs); |
4343 | 0 | prores_frame->encoder_id = gf_bs_read_u32(bs); |
4344 | 0 | prores_frame->width = gf_bs_read_u16(bs); |
4345 | 0 | prores_frame->height = gf_bs_read_u16(bs); |
4346 | 0 | prores_frame->chroma_format = gf_bs_read_int(bs, 2); |
4347 | 0 | gf_bs_read_int(bs, 2); |
4348 | 0 | prores_frame->interlaced_mode = gf_bs_read_int(bs, 2); |
4349 | 0 | gf_bs_read_int(bs, 2); |
4350 | 0 | prores_frame->aspect_ratio_information = gf_bs_read_int(bs, 4); |
4351 | 0 | prores_frame->framerate_code = gf_bs_read_int(bs, 4); |
4352 | 0 | prores_frame->color_primaries = gf_bs_read_u8(bs); |
4353 | 0 | prores_frame->transfer_characteristics = gf_bs_read_u8(bs); |
4354 | 0 | prores_frame->matrix_coefficients = gf_bs_read_u8(bs); |
4355 | 0 | gf_bs_read_int(bs, 4); |
4356 | 0 | prores_frame->alpha_channel_type = gf_bs_read_int(bs, 4); |
4357 | 0 | gf_bs_read_int(bs, 14); |
4358 | 0 | prores_frame->load_luma_quant_matrix = gf_bs_read_int(bs, 1); |
4359 | 0 | prores_frame->load_chroma_quant_matrix = gf_bs_read_int(bs, 1); |
4360 | 0 | if (prores_frame->load_luma_quant_matrix) { |
4361 | 0 | for (i=0; i<8; i++) { |
4362 | 0 | for (j=0; j<8; j++) { |
4363 | 0 | prores_frame->luma_quant_matrix[i][j] = gf_bs_read_u8(bs); |
4364 | 0 | } |
4365 | 0 | } |
4366 | 0 | } |
4367 | 0 | if (prores_frame->load_chroma_quant_matrix) { |
4368 | 0 | for (i=0; i<8; i++) { |
4369 | 0 | for (j=0; j<8; j++) { |
4370 | 0 | prores_frame->chroma_quant_matrix[i][j] = gf_bs_read_u8(bs); |
4371 | 0 | } |
4372 | 0 | } |
4373 | 0 | } |
4374 | 0 | pos = gf_bs_get_position(bs) - pos; |
4375 | 0 | if (pos != prores_frame->frame_hdr_size) { |
4376 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[ProRes] Invalid frame header size, expected %d got %d\n", prores_frame->frame_hdr_size, (u32) pos)); |
4377 | 0 | gf_bs_seek(bs, start); |
4378 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
4379 | 0 | } |
4380 | 0 | prores_frame->nb_pic = ((prores_frame->interlaced_mode==1) || (prores_frame->interlaced_mode==2)) ? 2 : 1; |
4381 | 0 | gf_bs_seek(bs, start); |
4382 | |
|
4383 | 0 | return GF_OK; |
4384 | 0 | } |
4385 | | |
4386 | | #endif /*GPAC_DISABLE_AV_PARSERS*/ |
4387 | | |
4388 | | GF_EXPORT |
4389 | | u8 gf_mp3_version(u32 hdr) |
4390 | 13.6M | { |
4391 | 13.6M | return ((hdr >> 19) & 0x3); |
4392 | 13.6M | } |
4393 | | |
4394 | | GF_EXPORT |
4395 | | const char *gf_mp3_version_name(u32 hdr) |
4396 | 0 | { |
4397 | 0 | u32 v = gf_mp3_version(hdr); |
4398 | 0 | switch (v) { |
4399 | 0 | case 0: |
4400 | 0 | return "MPEG-2.5"; |
4401 | 0 | case 1: |
4402 | 0 | return "Reserved"; |
4403 | 0 | case 2: |
4404 | 0 | return "MPEG-2"; |
4405 | 0 | case 3: |
4406 | 0 | return "MPEG-1"; |
4407 | 0 | default: |
4408 | 0 | return "Unknown"; |
4409 | 0 | } |
4410 | 0 | } |
4411 | | |
4412 | | #ifndef GPAC_DISABLE_AV_PARSERS |
4413 | | |
4414 | | GF_EXPORT |
4415 | | u8 gf_mp3_layer(u32 hdr) |
4416 | 9.10M | { |
4417 | 9.10M | return 4 - (((hdr >> 17) & 0x3)); |
4418 | 9.10M | } |
4419 | | |
4420 | | GF_EXPORT |
4421 | | u8 gf_mp3_num_channels(u32 hdr) |
4422 | 39.3k | { |
4423 | 39.3k | if (((hdr >> 6) & 0x3) == 3) return 1; |
4424 | 25.9k | return 2; |
4425 | 39.3k | } |
4426 | | |
4427 | | GF_EXPORT |
4428 | | u16 gf_mp3_sampling_rate(u32 hdr) |
4429 | 4.54M | { |
4430 | 4.54M | u16 res; |
4431 | | /* extract the necessary fields from the MP3 header */ |
4432 | 4.54M | u8 version = gf_mp3_version(hdr); |
4433 | 4.54M | u8 sampleRateIndex = (hdr >> 10) & 0x3; |
4434 | | |
4435 | 4.54M | switch (sampleRateIndex) { |
4436 | 1.14M | case 0: |
4437 | 1.14M | res = 44100; |
4438 | 1.14M | break; |
4439 | 2.87M | case 1: |
4440 | 2.87M | res = 48000; |
4441 | 2.87M | break; |
4442 | 530k | case 2: |
4443 | 530k | res = 32000; |
4444 | 530k | break; |
4445 | 44 | default: |
4446 | 44 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] Samplerate index not valid\n")); |
4447 | 44 | return 0; |
4448 | 4.54M | } |
4449 | | /*reserved or MPEG-1*/ |
4450 | 4.54M | if (version & 1) return res; |
4451 | | |
4452 | | /*MPEG-2*/ |
4453 | 811k | res /= 2; |
4454 | | /*MPEG-2.5*/ |
4455 | 811k | if (version == 0) res /= 2; |
4456 | 811k | return res; |
4457 | 4.54M | } |
4458 | | |
4459 | | GF_EXPORT |
4460 | | u16 gf_mp3_window_size(u32 hdr) |
4461 | 81.0k | { |
4462 | 81.0k | u8 version = gf_mp3_version(hdr); |
4463 | 81.0k | u8 layer = gf_mp3_layer(hdr); |
4464 | | |
4465 | 81.0k | if (layer == 3) { |
4466 | 35.3k | if (version == 3) return 1152; |
4467 | 35.1k | return 576; |
4468 | 35.3k | } |
4469 | 45.6k | if (layer == 2) return 1152; |
4470 | 45.0k | return 384; |
4471 | 45.6k | } |
4472 | | |
4473 | | GF_EXPORT |
4474 | | u8 gf_mp3_object_type_indication(u32 hdr) |
4475 | 16.9k | { |
4476 | 16.9k | switch (gf_mp3_version(hdr)) { |
4477 | 9.47k | case 3: |
4478 | 9.47k | return GF_CODECID_MPEG_AUDIO; |
4479 | 7.37k | case 2: |
4480 | 7.47k | case 0: |
4481 | 7.47k | return GF_CODECID_MPEG2_PART3; |
4482 | 0 | default: |
4483 | 0 | return 0x00; |
4484 | 16.9k | } |
4485 | 16.9k | } |
4486 | | |
4487 | | /*aligned bitrate parsing with libMAD*/ |
4488 | | |
4489 | | static |
4490 | | u32 const bitrate_table[5][15] = { |
4491 | | /* MPEG-1 */ |
4492 | | { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */ |
4493 | | 256000, 288000, 320000, 352000, 384000, 416000, 448000 |
4494 | | }, |
4495 | | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */ |
4496 | | 128000, 160000, 192000, 224000, 256000, 320000, 384000 |
4497 | | }, |
4498 | | { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */ |
4499 | | 112000, 128000, 160000, 192000, 224000, 256000, 320000 |
4500 | | }, |
4501 | | |
4502 | | /* MPEG-2 LSF */ |
4503 | | { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */ |
4504 | | 128000, 144000, 160000, 176000, 192000, 224000, 256000 |
4505 | | }, |
4506 | | { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */ |
4507 | | 64000, 80000, 96000, 112000, 128000, 144000, 160000 |
4508 | | } /* II & III */ |
4509 | | }; |
4510 | | |
4511 | | |
4512 | | u32 gf_mp3_bit_rate(u32 hdr) |
4513 | 4.50M | { |
4514 | 4.50M | u8 version = gf_mp3_version(hdr); |
4515 | 4.50M | u8 layer = gf_mp3_layer(hdr); |
4516 | 4.50M | u8 bitRateIndex = (hdr >> 12) & 0xF; |
4517 | 4.50M | u32 lidx; |
4518 | | /*MPEG-1*/ |
4519 | 4.50M | if (version & 1) { |
4520 | 3.71M | if (!layer) { |
4521 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] layer index not valid\n")); |
4522 | 0 | return 0; |
4523 | 0 | } |
4524 | 3.71M | lidx = layer - 1; |
4525 | 3.71M | } |
4526 | | /*MPEG-2/2.5*/ |
4527 | 791k | else { |
4528 | 791k | lidx = 3 + (layer >> 1); |
4529 | 791k | } |
4530 | 4.50M | if (lidx>4) { |
4531 | 15 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] layer index not valid\n")); |
4532 | 15 | return 0; |
4533 | 15 | } |
4534 | 4.50M | if (bitRateIndex>14) { |
4535 | 29 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] Bitrate index not valid\n")); |
4536 | 29 | return 0; |
4537 | 29 | } |
4538 | 4.50M | return bitrate_table[lidx][bitRateIndex]; |
4539 | 4.50M | } |
4540 | | |
4541 | | |
4542 | | |
4543 | | GF_EXPORT |
4544 | | u16 gf_mp3_frame_size(u32 hdr) |
4545 | 4.48M | { |
4546 | 4.48M | u8 version = gf_mp3_version(hdr); |
4547 | 4.48M | u8 layer = gf_mp3_layer(hdr); |
4548 | 4.48M | u32 pad = ((hdr >> 9) & 0x1) ? 1 : 0; |
4549 | 4.48M | u32 bitrate = gf_mp3_bit_rate(hdr); |
4550 | 4.48M | u32 samplerate = gf_mp3_sampling_rate(hdr); |
4551 | | |
4552 | 4.48M | u32 frameSize = 0; |
4553 | 4.48M | if (!samplerate || !bitrate) return 0; |
4554 | | |
4555 | 4.48M | if (layer == 1) { |
4556 | 3.83M | frameSize = ((12 * bitrate / samplerate) + pad) * 4; |
4557 | 3.83M | } |
4558 | 647k | else { |
4559 | 647k | u32 slots_per_frame = 144; |
4560 | 647k | if ((layer == 3) && !(version & 1)) slots_per_frame = 72; |
4561 | 647k | frameSize = (slots_per_frame * bitrate / samplerate) + pad; |
4562 | 647k | } |
4563 | 4.48M | return (u16)frameSize; |
4564 | 4.48M | } |
4565 | | |
4566 | | |
4567 | | GF_EXPORT |
4568 | | u32 gf_mp3_get_next_header(FILE* in) |
4569 | 23.4k | { |
4570 | 23.4k | u8 b, state = 0; |
4571 | 23.4k | u32 dropped = 0; |
4572 | 23.4k | unsigned char bytes[4]; |
4573 | 23.4k | bytes[0] = bytes[1] = bytes[2] = bytes[3] = 0; |
4574 | | |
4575 | 2.18M | while (1) { |
4576 | 2.18M | if (gf_fread(&b, 1, in) == 0) return 0; |
4577 | | |
4578 | 2.18M | if (state == 3) { |
4579 | 23.4k | bytes[state] = b; |
4580 | 23.4k | return GF_4CC((u32)bytes[0], bytes[1], bytes[2], bytes[3]); |
4581 | 23.4k | } |
4582 | 2.16M | if (state == 2) { |
4583 | 386k | if (((b & 0xF0) == 0) || ((b & 0xF0) == 0xF0) || ((b & 0x0C) == 0x0C)) { |
4584 | 363k | if (bytes[1] == 0xFF) state = 1; |
4585 | 4.86k | else state = 0; |
4586 | 363k | } |
4587 | 23.4k | else { |
4588 | 23.4k | bytes[state] = b; |
4589 | 23.4k | state = 3; |
4590 | 23.4k | } |
4591 | 386k | } |
4592 | 2.16M | if (state == 1) { |
4593 | 421k | if (((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) { |
4594 | 385k | bytes[state] = b; |
4595 | 385k | state = 2; |
4596 | 385k | } |
4597 | 35.9k | else { |
4598 | 35.9k | state = 0; |
4599 | 35.9k | } |
4600 | 421k | } |
4601 | | |
4602 | 2.16M | if (state == 0) { |
4603 | 1.75M | if (b == 0xFF) { |
4604 | 63.3k | bytes[state] = b; |
4605 | 63.3k | state = 1; |
4606 | 63.3k | } |
4607 | 1.69M | else { |
4608 | 1.69M | if ((dropped == 0) && ((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) { |
4609 | 908 | bytes[0] = (u8)0xFF; |
4610 | 908 | bytes[1] = b; |
4611 | 908 | state = 2; |
4612 | 908 | } |
4613 | 1.69M | else { |
4614 | 1.69M | dropped++; |
4615 | 1.69M | } |
4616 | 1.69M | } |
4617 | 1.75M | } |
4618 | 2.16M | } |
4619 | 0 | return 0; |
4620 | 23.4k | } |
4621 | | |
4622 | | GF_EXPORT |
4623 | | u32 gf_mp3_get_next_header_mem(const u8 *buffer, u32 size, u32 *pos) |
4624 | 2.24M | { |
4625 | 2.24M | u32 cur; |
4626 | 2.24M | u8 b, state = 0; |
4627 | 2.24M | u32 dropped = 0; |
4628 | 2.24M | unsigned char bytes[4]; |
4629 | 2.24M | bytes[0] = bytes[1] = bytes[2] = bytes[3] = 0; |
4630 | | |
4631 | 2.24M | cur = 0; |
4632 | 2.24M | *pos = 0; |
4633 | 755M | while (cur < size) { |
4634 | 755M | b = (u8)buffer[cur]; |
4635 | 755M | cur++; |
4636 | | |
4637 | 755M | if (state == 3) { |
4638 | 2.22M | u32 val; |
4639 | 2.22M | bytes[state] = b; |
4640 | 2.22M | val = GF_4CC((u32)bytes[0], bytes[1], bytes[2], bytes[3]); |
4641 | 2.22M | if (gf_mp3_frame_size(val)) { |
4642 | 2.22M | *pos = dropped; |
4643 | 2.22M | return val; |
4644 | 2.22M | } |
4645 | 0 | state = 0; |
4646 | 0 | dropped = cur; |
4647 | 0 | } |
4648 | 752M | if (state == 2) { |
4649 | 25.5M | if (((b & 0xF0) == 0) || ((b & 0xF0) == 0xF0) || ((b & 0x0C) == 0x0C)) { |
4650 | 23.2M | if (bytes[1] == 0xFF) { |
4651 | 22.6M | state = 1; |
4652 | 22.6M | dropped += 1; |
4653 | 22.6M | } |
4654 | 657k | else { |
4655 | 657k | state = 0; |
4656 | 657k | dropped = cur; |
4657 | 657k | } |
4658 | 23.2M | } |
4659 | 2.23M | else { |
4660 | 2.23M | bytes[state] = b; |
4661 | 2.23M | state = 3; |
4662 | 2.23M | } |
4663 | 25.5M | } |
4664 | 752M | if (state == 1) { |
4665 | 37.6M | if (((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) { |
4666 | 25.5M | bytes[state] = b; |
4667 | 25.5M | state = 2; |
4668 | 25.5M | } |
4669 | 12.1M | else { |
4670 | 12.1M | state = 0; |
4671 | 12.1M | dropped = cur; |
4672 | 12.1M | } |
4673 | 37.6M | } |
4674 | | |
4675 | 752M | if (state == 0) { |
4676 | 725M | if (b == 0xFF) { |
4677 | 15.0M | bytes[state] = b; |
4678 | 15.0M | state = 1; |
4679 | 15.0M | } |
4680 | 710M | else { |
4681 | 710M | dropped++; |
4682 | 710M | } |
4683 | 725M | } |
4684 | 752M | } |
4685 | 12.7k | return 0; |
4686 | 2.24M | } |
4687 | | |
4688 | | #endif /*GPAC_DISABLE_AV_PARSERS*/ |
4689 | | |
4690 | | Bool gf_avcc_use_extensions(u8 profile_idc) |
4691 | 204k | { |
4692 | 204k | switch (profile_idc) { |
4693 | 167 | case 66: |
4694 | 6.93k | case 77: |
4695 | 12.4k | case 88: |
4696 | 12.4k | return GF_FALSE; |
4697 | 192k | default: |
4698 | 192k | return GF_TRUE; |
4699 | 204k | } |
4700 | 204k | } |
4701 | | GF_EXPORT |
4702 | | const char *gf_avc_get_profile_name(u8 video_prof) |
4703 | 0 | { |
4704 | 0 | switch (video_prof) { |
4705 | 0 | case 0x42: |
4706 | 0 | return "Baseline"; |
4707 | 0 | case 0x4D: |
4708 | 0 | return "Main"; |
4709 | 0 | case 0x53: |
4710 | 0 | return "Scalable Baseline"; |
4711 | 0 | case 0x56: |
4712 | 0 | return "Scalable High"; |
4713 | 0 | case 0x58: |
4714 | 0 | return "Extended"; |
4715 | 0 | case 0x64: |
4716 | 0 | return "High"; |
4717 | 0 | case 0x6E: |
4718 | 0 | return "High 10"; |
4719 | 0 | case 0x7A: |
4720 | 0 | return "High 4:2:2"; |
4721 | 0 | case 0x90: |
4722 | 0 | case 0xF4: |
4723 | 0 | return "High 4:4:4"; |
4724 | 0 | default: |
4725 | 0 | return "Unknown"; |
4726 | 0 | } |
4727 | 0 | } |
4728 | | |
4729 | | GF_EXPORT |
4730 | | const char *gf_hevc_get_profile_name(u8 video_prof) |
4731 | 0 | { |
4732 | 0 | switch (video_prof) { |
4733 | 0 | case 0x01: |
4734 | 0 | return "Main"; |
4735 | 0 | case 0x02: |
4736 | 0 | return "Main 10"; |
4737 | 0 | case 0x03: |
4738 | 0 | return "Main Still Picture"; |
4739 | 0 | default: |
4740 | 0 | return "Unknown"; |
4741 | 0 | } |
4742 | 0 | } |
4743 | | GF_EXPORT |
4744 | | const char *gf_avc_hevc_get_chroma_format_name(u8 chroma_format) |
4745 | 0 | { |
4746 | 0 | switch (chroma_format) { |
4747 | 0 | case 1: |
4748 | 0 | return "YUV 4:2:0"; |
4749 | 0 | case 2: |
4750 | 0 | return "YUV 4:2:2"; |
4751 | 0 | case 3: |
4752 | 0 | return "YUV 4:4:4"; |
4753 | 0 | default: |
4754 | 0 | return "Unknown"; |
4755 | 0 | } |
4756 | 0 | } |
4757 | | |
4758 | | u32 gf_bs_read_ue_log_idx3(GF_BitStream *bs, const char *fname, s32 idx1, s32 idx2, s32 idx3) |
4759 | 30.9M | { |
4760 | 30.9M | u32 val=0, code; |
4761 | 30.9M | s32 nb_lead = -1; |
4762 | 30.9M | u32 bits = 0; |
4763 | 417M | for (code=0; !code; nb_lead++) { |
4764 | 396M | if (nb_lead>=32) { |
4765 | 10.2M | break; |
4766 | 10.2M | } |
4767 | 386M | code = gf_bs_read_int(bs, 1); |
4768 | 386M | bits++; |
4769 | 386M | } |
4770 | | |
4771 | 30.9M | if (nb_lead>=32) { |
4772 | 10.2M | if (gf_bs_is_overflow(bs)<2) { |
4773 | | //gf_bs_read_int keeps returning 0 on EOS, so if no more bits available, rbsp was truncated otherwise code is broken in rbsp) |
4774 | | //we only test once nb_lead>=32 to avoid testing at each bit read |
4775 | 190k | if (!gf_bs_available(bs)) { |
4776 | 164k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[Core] exp-golomb read failed, not enough bits in bitstream !\n")); |
4777 | 164k | } else { |
4778 | 26.8k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[Core] corrupted exp-golomb code, %d leading zeros, max 31 allowed !\n", nb_lead)); |
4779 | 26.8k | } |
4780 | 190k | gf_bs_mark_overflow(bs, GF_FALSE); |
4781 | 190k | } |
4782 | 10.2M | return 0; |
4783 | 10.2M | } |
4784 | | |
4785 | 20.7M | if (nb_lead) { |
4786 | 8.42M | u32 leads=1; |
4787 | 8.42M | val = gf_bs_read_int(bs, nb_lead); |
4788 | 8.42M | leads <<= nb_lead; |
4789 | 8.42M | leads -= 1; |
4790 | 8.42M | val += leads; |
4791 | | // val += (1 << nb_lead) - 1; |
4792 | 8.42M | bits += nb_lead; |
4793 | 8.42M | } |
4794 | | |
4795 | 20.7M | if (fname) { |
4796 | 13.6M | gf_bs_log_idx(bs, bits, fname, val, idx1, idx2, idx3); |
4797 | 13.6M | } |
4798 | 20.7M | return val; |
4799 | 30.9M | } |
4800 | | |
4801 | 8.63M | #define gf_bs_read_ue_log_idx2(_bs, _fname, _idx1, _idx2) gf_bs_read_ue_log_idx3(_bs, _fname, (s32) _idx1, (s32) _idx2, -1) |
4802 | 5.08M | #define gf_bs_read_ue_log_idx(_bs, _fname, _idx) gf_bs_read_ue_log_idx3(_bs, _fname, (s32) _idx, -1, -1) |
4803 | 16.4M | #define gf_bs_read_ue_log(_bs, _fname) gf_bs_read_ue_log_idx3(_bs, _fname, -1, -1, -1) |
4804 | | |
4805 | | |
4806 | | u32 gf_bs_read_ue(GF_BitStream *bs) |
4807 | 7.96M | { |
4808 | 7.96M | return gf_bs_read_ue_log(bs, NULL); |
4809 | 7.96M | } |
4810 | | |
4811 | | s32 gf_bs_read_se(GF_BitStream *bs) |
4812 | 7.93M | { |
4813 | 7.93M | u32 v = gf_bs_read_ue(bs); |
4814 | 7.93M | if ((v & 0x1) == 0) return (s32)(0 - (v >> 1)); |
4815 | 1.28M | return (v + 1) >> 1; |
4816 | 7.93M | } |
4817 | | |
4818 | | s32 gf_bs_read_se_log_idx2(GF_BitStream *bs, const char *fname, s32 idx1, s32 idx2) |
4819 | 6.05M | { |
4820 | 6.05M | s32 res = gf_bs_read_se(bs); |
4821 | 6.05M | if (fname) |
4822 | 6.05M | gf_bs_log_idx(bs, -1, fname, res, idx1, idx2, -1); |
4823 | 6.05M | return res; |
4824 | 6.05M | } |
4825 | 1.89M | #define gf_bs_read_se_log_idx(_bs, _fname, _idx) gf_bs_read_se_log_idx2(_bs, _fname, (s32) _idx, -1) |
4826 | 1.69M | #define gf_bs_read_se_log(_bs, _fname) gf_bs_read_se_log_idx2(_bs, _fname, -1, -1) |
4827 | | |
4828 | | |
4829 | | |
4830 | 0 | void gf_bs_write_ue(GF_BitStream *bs, u32 num) { |
4831 | 0 | s32 length = 1; |
4832 | 0 | s32 temp = ++num; |
4833 | |
|
4834 | 0 | while (temp != 1) { |
4835 | 0 | temp >>= 1; |
4836 | 0 | length += 2; |
4837 | 0 | } |
4838 | |
|
4839 | 0 | gf_bs_write_int(bs, 0, length >> 1); |
4840 | 0 | gf_bs_write_int(bs, num, (length + 1) >> 1); |
4841 | 0 | } |
4842 | | |
4843 | 0 | static u32 gf_get_ue_nb_bits(u32 num) { |
4844 | 0 | s32 length = 1; |
4845 | 0 | s32 temp = ++num; |
4846 | |
|
4847 | 0 | while (temp != 1) { |
4848 | 0 | temp >>= 1; |
4849 | 0 | length += 2; |
4850 | 0 | } |
4851 | |
|
4852 | 0 | return (length >> 1) + ( (length + 1) >> 1); |
4853 | 0 | } |
4854 | | |
4855 | | void gf_bs_write_se(GF_BitStream *bs, s32 num) |
4856 | 0 | { |
4857 | 0 | u32 v; |
4858 | 0 | if (num <= 0) |
4859 | 0 | v = (-1 * num) << 1; |
4860 | 0 | else |
4861 | 0 | v = (num << 1) - 1; |
4862 | |
|
4863 | 0 | gf_bs_write_ue(bs, v); |
4864 | 0 | } |
4865 | | |
4866 | | #ifndef GPAC_DISABLE_AV_PARSERS |
4867 | | |
4868 | | GF_EXPORT |
4869 | | u32 gf_media_nalu_next_start_code(const u8 *data, u32 data_len, u32 *sc_size) |
4870 | 2.54M | { |
4871 | 2.54M | u32 avail = data_len; |
4872 | 2.54M | const u8 *cur = data; |
4873 | | |
4874 | 12.7M | while (cur) { |
4875 | 12.7M | u32 v, bpos; |
4876 | 12.7M | u8 *next_zero = memchr(cur, 0, avail); |
4877 | 12.7M | if (!next_zero) return data_len; |
4878 | | |
4879 | 12.7M | v = 0xffffff00; |
4880 | 12.7M | bpos = (u32)(next_zero - data) + 1; |
4881 | 28.4M | while (1) { |
4882 | 28.4M | u8 cval; |
4883 | 28.4M | if (bpos == (u32)data_len) |
4884 | 2.97k | return data_len; |
4885 | | |
4886 | 28.4M | cval = data[bpos]; |
4887 | 28.4M | v = ((v << 8) & 0xFFFFFF00) | ((u32)cval); |
4888 | 28.4M | bpos++; |
4889 | 28.4M | if (v == 0x00000001) { |
4890 | 530k | *sc_size = 4; |
4891 | 530k | return bpos - 4; |
4892 | 530k | } |
4893 | 27.8M | else if ((v & 0x00FFFFFF) == 0x00000001) { |
4894 | 2.00M | *sc_size = 3; |
4895 | 2.00M | return bpos - 3; |
4896 | 2.00M | } |
4897 | 25.8M | if (cval) |
4898 | 10.1M | break; |
4899 | 25.8M | } |
4900 | 10.1M | if (bpos >= data_len) |
4901 | 1.03k | break; |
4902 | 10.1M | cur = data + bpos; |
4903 | 10.1M | avail = data_len - bpos; |
4904 | 10.1M | } |
4905 | 1.03k | return data_len; |
4906 | 2.54M | } |
4907 | | |
4908 | | Bool gf_avc_slice_is_intra(AVCState *avc) |
4909 | 334k | { |
4910 | 334k | switch (avc->s_info.slice_type) { |
4911 | 26.5k | case GF_AVC_TYPE_I: |
4912 | 26.9k | case GF_AVC_TYPE2_I: |
4913 | 36.2k | case GF_AVC_TYPE_SI: |
4914 | 37.8k | case GF_AVC_TYPE2_SI: |
4915 | 37.8k | return 1; |
4916 | 296k | default: |
4917 | 296k | return 0; |
4918 | 334k | } |
4919 | 334k | } |
4920 | | |
4921 | | #if 0 //unused |
4922 | | Bool gf_avc_slice_is_IDR(AVCState *avc) |
4923 | | { |
4924 | | if (avc->sei.recovery_point.valid) |
4925 | | { |
4926 | | avc->sei.recovery_point.valid = 0; |
4927 | | return 1; |
4928 | | } |
4929 | | if (avc->s_info.nal_unit_type != GF_AVC_NALU_IDR_SLICE) |
4930 | | return 0; |
4931 | | return gf_avc_slice_is_intra(avc); |
4932 | | } |
4933 | | #endif |
4934 | | |
4935 | | static const struct { |
4936 | | u32 w, h; |
4937 | | } avc_hevc_sar[] = { |
4938 | | { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, |
4939 | | { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 }, |
4940 | | { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 }, |
4941 | | { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 }, |
4942 | | { 2, 1 } |
4943 | | }; |
4944 | | |
4945 | | |
4946 | | /*ISO 14496-10 (N11084) E.1.2*/ |
4947 | | static s32 avc_parse_hrd_parameters(GF_BitStream *bs, AVC_HRD *hrd) |
4948 | 69.4k | { |
4949 | 69.4k | int i, cpb_cnt_minus1; |
4950 | | |
4951 | 69.4k | cpb_cnt_minus1 = gf_bs_read_ue_log(bs, "cpb_cnt_minus1"); |
4952 | 69.4k | if (cpb_cnt_minus1 > 31) { |
4953 | 1.25k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] invalid cpb_cnt_minus1 value: %d (expected in [0;31])\n", cpb_cnt_minus1)); |
4954 | 1.25k | return -1; |
4955 | 1.25k | } |
4956 | 68.2k | gf_bs_read_int_log(bs, 4, "bit_rate_scale"); |
4957 | 68.2k | gf_bs_read_int_log(bs, 4, "cpb_size_scale"); |
4958 | | |
4959 | | /*for( SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ ) {*/ |
4960 | 205k | for (i = 0; i <= cpb_cnt_minus1; i++) { |
4961 | 137k | gf_bs_read_ue_log_idx(bs, "bit_rate_value_minus1", i); |
4962 | 137k | gf_bs_read_ue_log_idx(bs, "cpb_size_value_minus1", i); |
4963 | 137k | gf_bs_read_int_log_idx(bs, 1, "cbr_flag", i); |
4964 | 137k | } |
4965 | 68.2k | gf_bs_read_int_log(bs, 5, "initial_cpb_removal_delay_length_minus1"); |
4966 | 68.2k | hrd->cpb_removal_delay_length_minus1 = gf_bs_read_int_log(bs, 5, "cpb_removal_delay_length_minus1"); |
4967 | 68.2k | hrd->dpb_output_delay_length_minus1 = gf_bs_read_int_log(bs, 5, "dpb_output_delay_length_minus1"); |
4968 | 68.2k | hrd->time_offset_length = gf_bs_read_int_log(bs, 5, "time_offset_length"); |
4969 | 68.2k | return 0; |
4970 | 69.4k | } |
4971 | | |
4972 | | /*returns the nal_size without emulation prevention bytes*/ |
4973 | | u32 gf_media_nalu_emulation_bytes_add_count(u8 *buffer, u32 nal_size) |
4974 | 2.27k | { |
4975 | 2.27k | u32 i = 0, emulation_bytes_count = 0; |
4976 | 2.27k | u8 num_zero = 0; |
4977 | | |
4978 | 1.19M | while (i < nal_size) { |
4979 | | /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003 |
4980 | | other than the following sequences shall not occur at any byte-aligned position: |
4981 | | \96 0x00000300 |
4982 | | \96 0x00000301 |
4983 | | \96 0x00000302 |
4984 | | \96 0x00000303" |
4985 | | */ |
4986 | 1.19M | if (num_zero == 2 && (u8)buffer[i] < 0x04) { |
4987 | | /*emulation code found*/ |
4988 | 140k | num_zero = 0; |
4989 | 140k | emulation_bytes_count++; |
4990 | 140k | if (!buffer[i]) |
4991 | 137k | num_zero = 1; |
4992 | 140k | } |
4993 | 1.05M | else { |
4994 | 1.05M | if (!buffer[i]) |
4995 | 216k | num_zero++; |
4996 | 840k | else |
4997 | 840k | num_zero = 0; |
4998 | 1.05M | } |
4999 | 1.19M | i++; |
5000 | 1.19M | } |
5001 | 2.27k | return emulation_bytes_count; |
5002 | 2.27k | } |
5003 | | |
5004 | | u32 gf_media_nalu_add_emulation_bytes(const u8 *buffer_src, u8 *buffer_dst, u32 nal_size) |
5005 | 1.10k | { |
5006 | 1.10k | u32 i = 0, emulation_bytes_count = 0; |
5007 | 1.10k | u8 num_zero = 0; |
5008 | | |
5009 | 216k | while (i < nal_size) { |
5010 | | /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003 |
5011 | | other than the following sequences shall not occur at any byte-aligned position: |
5012 | | 0x00000300 |
5013 | | 0x00000301 |
5014 | | 0x00000302 |
5015 | | 0x00000303" |
5016 | | */ |
5017 | 215k | if (num_zero == 2 && (u8)buffer_src[i] < 0x04) { |
5018 | | /*add emulation code*/ |
5019 | 27.0k | num_zero = 0; |
5020 | 27.0k | buffer_dst[i + emulation_bytes_count] = 0x03; |
5021 | 27.0k | emulation_bytes_count++; |
5022 | 27.0k | if (!buffer_src[i]) |
5023 | 25.9k | num_zero = 1; |
5024 | 27.0k | } |
5025 | 188k | else { |
5026 | 188k | if (!buffer_src[i]) |
5027 | 39.8k | num_zero++; |
5028 | 148k | else |
5029 | 148k | num_zero = 0; |
5030 | 188k | } |
5031 | 215k | buffer_dst[i + emulation_bytes_count] = buffer_src[i]; |
5032 | 215k | i++; |
5033 | 215k | } |
5034 | 1.10k | return nal_size + emulation_bytes_count; |
5035 | 1.10k | } |
5036 | | |
5037 | | /*returns the nal_size without emulation prevention bytes*/ |
5038 | | u32 gf_media_nalu_emulation_bytes_remove_count(const u8 *buffer, u32 nal_size) |
5039 | 0 | { |
5040 | 0 | u32 i = 0, emulation_bytes_count = 0; |
5041 | 0 | u8 num_zero = 0; |
5042 | 0 | if (!buffer || !nal_size) return 0; |
5043 | | |
5044 | 0 | while (i < nal_size) |
5045 | 0 | { |
5046 | | /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003 |
5047 | | other than the following sequences shall not occur at any byte-aligned position: |
5048 | | \96 0x00000300 |
5049 | | \96 0x00000301 |
5050 | | \96 0x00000302 |
5051 | | \96 0x00000303" |
5052 | | */ |
5053 | 0 | if (num_zero == 2 |
5054 | 0 | && buffer[i] == 0x03 |
5055 | 0 | && i + 1 < nal_size /*next byte is readable*/ |
5056 | 0 | && (u8)buffer[i + 1] < 0x04) |
5057 | 0 | { |
5058 | | /*emulation code found*/ |
5059 | 0 | num_zero = 0; |
5060 | 0 | emulation_bytes_count++; |
5061 | 0 | i++; |
5062 | 0 | } |
5063 | |
|
5064 | 0 | if (!buffer[i]) |
5065 | 0 | num_zero++; |
5066 | 0 | else |
5067 | 0 | num_zero = 0; |
5068 | |
|
5069 | 0 | i++; |
5070 | 0 | } |
5071 | |
|
5072 | 0 | return emulation_bytes_count; |
5073 | 0 | } |
5074 | | |
5075 | | /*nal_size is updated to allow better error detection*/ |
5076 | | GF_EXPORT |
5077 | | u32 gf_media_nalu_remove_emulation_bytes(const u8 *buffer_src, u8 *buffer_dst, u32 nal_size) |
5078 | 0 | { |
5079 | 0 | u32 i = 0, emulation_bytes_count = 0; |
5080 | 0 | u8 num_zero = 0; |
5081 | |
|
5082 | 0 | while (i < nal_size) |
5083 | 0 | { |
5084 | | /*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003 |
5085 | | other than the following sequences shall not occur at any byte-aligned position: |
5086 | | 0x00000300 |
5087 | | 0x00000301 |
5088 | | 0x00000302 |
5089 | | 0x00000303" |
5090 | | */ |
5091 | 0 | if (num_zero == 2 |
5092 | 0 | && buffer_src[i] == 0x03 |
5093 | 0 | && i + 1 < nal_size /*next byte is readable*/ |
5094 | 0 | && (u8)buffer_src[i + 1] < 0x04) |
5095 | 0 | { |
5096 | | /*emulation code found*/ |
5097 | 0 | num_zero = 0; |
5098 | 0 | emulation_bytes_count++; |
5099 | 0 | i++; |
5100 | 0 | } |
5101 | |
|
5102 | 0 | buffer_dst[i - emulation_bytes_count] = buffer_src[i]; |
5103 | |
|
5104 | 0 | if (!buffer_src[i]) |
5105 | 0 | num_zero++; |
5106 | 0 | else |
5107 | 0 | num_zero = 0; |
5108 | |
|
5109 | 0 | i++; |
5110 | 0 | } |
5111 | |
|
5112 | 0 | return nal_size - emulation_bytes_count; |
5113 | 0 | } |
5114 | | |
5115 | 66.5k | #define AVC_SPS_BROKEN {\ |
5116 | 66.5k | memset(sps, 0, sizeof(AVC_SPS)); \ |
5117 | 66.5k | return -1;\ |
5118 | 66.5k | } |
5119 | | |
5120 | | static s32 gf_avc_read_sps_bs_internal(GF_BitStream *bs, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos, u32 nal_hdr) |
5121 | 257k | { |
5122 | 257k | AVC_SPS *sps; |
5123 | 257k | s32 mb_width, mb_height, sps_id = -1; |
5124 | 257k | u32 profile_idc, level_idc, pcomp, i, chroma_format_idc, cl = 0, cr = 0, ct = 0, cb = 0, luma_bd, chroma_bd; |
5125 | 257k | u8 separate_colour_plane_flag = 0; |
5126 | | |
5127 | 257k | if (!vui_flag_pos) { |
5128 | 257k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
5129 | 257k | } |
5130 | | |
5131 | 257k | if (!bs) { |
5132 | 0 | return -1; |
5133 | 0 | } |
5134 | | |
5135 | 257k | if (!nal_hdr) { |
5136 | 117k | gf_bs_read_int_log(bs, 1, "forbidden_zero_bit"); |
5137 | 117k | gf_bs_read_int_log(bs, 2, "nal_ref_idc"); |
5138 | 117k | gf_bs_read_int_log(bs, 5, "nal_unit_type"); |
5139 | 117k | } |
5140 | 257k | profile_idc = gf_bs_read_int_log(bs, 8, "profile_idc"); |
5141 | | |
5142 | 257k | pcomp = gf_bs_read_int_log(bs, 8, "profile_compatibility"); |
5143 | | /*sanity checks*/ |
5144 | 257k | if (pcomp & 0x3) |
5145 | 63.6k | return -1; |
5146 | | |
5147 | 193k | level_idc = gf_bs_read_int_log(bs, 8, "level_idc"); |
5148 | | |
5149 | | /*SubsetSps is used to be sure that AVC SPS are not going to be scratched |
5150 | | by subset SPS. According to the SVC standard, subset SPS can have the same sps_id |
5151 | | than its base layer, but it does not refer to the same SPS. */ |
5152 | 193k | sps_id = gf_bs_read_ue_log(bs, "sps_id") + GF_SVC_SSPS_ID_SHIFT * subseq_sps; |
5153 | 193k | if ((sps_id < 0) || (sps_id >= 32)) { |
5154 | 5.96k | return -1; |
5155 | 5.96k | } |
5156 | | |
5157 | 187k | luma_bd = chroma_bd = 0; |
5158 | 187k | sps = &avc->sps[sps_id]; |
5159 | 187k | chroma_format_idc = sps->ChromaArrayType = 1; |
5160 | 187k | sps->state |= subseq_sps ? AVC_SUBSPS_PARSED : AVC_SPS_PARSED; |
5161 | | |
5162 | | /*High Profile and SVC*/ |
5163 | 187k | switch (profile_idc) { |
5164 | 40.7k | case 100: |
5165 | 42.7k | case 110: |
5166 | 46.3k | case 122: |
5167 | 47.1k | case 244: |
5168 | 47.5k | case 44: |
5169 | | /*sanity checks: note1 from 7.4.2.1.1 of iso/iec 14496-10-N11084*/ |
5170 | 47.5k | if (pcomp & 0xE0) |
5171 | 1.60k | AVC_SPS_BROKEN |
5172 | 55.0k | case 83: |
5173 | 55.7k | case 86: |
5174 | 62.1k | case 118: |
5175 | 62.8k | case 128: |
5176 | 62.8k | chroma_format_idc = gf_bs_read_ue_log(bs, "chroma_format_idc"); |
5177 | 62.8k | sps->ChromaArrayType = chroma_format_idc; |
5178 | 62.8k | if (chroma_format_idc == 3) { |
5179 | 8.44k | separate_colour_plane_flag = gf_bs_read_int_log(bs, 1, "separate_colour_plane_flag"); |
5180 | | /* |
5181 | | Depending on the value of separate_colour_plane_flag, the value of the variable ChromaArrayType is assigned as follows. |
5182 | | \96 If separate_colour_plane_flag is equal to 0, ChromaArrayType is set equal to chroma_format_idc. |
5183 | | \96 Otherwise (separate_colour_plane_flag is equal to 1), ChromaArrayType is set equal to 0. |
5184 | | */ |
5185 | 8.44k | if (separate_colour_plane_flag) sps->ChromaArrayType = 0; |
5186 | 8.44k | } |
5187 | 62.8k | luma_bd = gf_bs_read_ue_log(bs, "luma_bit_depth"); |
5188 | 62.8k | chroma_bd = gf_bs_read_ue_log(bs, "chroma_bit_depth"); |
5189 | 62.8k | /*qpprime_y_zero_transform_bypass_flag = */ gf_bs_read_int_log(bs, 1, "qpprime_y_zero_transform_bypass_flag"); |
5190 | | /*seq_scaling_matrix_present_flag*/ |
5191 | 62.8k | if (gf_bs_read_int_log(bs, 1, "seq_scaling_matrix_present_flag")) { |
5192 | 12.9k | u32 k; |
5193 | 116k | for (k = 0; k < 8; k++) { |
5194 | 103k | if (gf_bs_read_int_log_idx(bs, 1, "seq_scaling_list_present_flag", k)) { |
5195 | 67.0k | u32 z, last = 8, next = 8; |
5196 | 67.0k | u32 sl = k < 6 ? 16 : 64; |
5197 | 1.85M | for (z = 0; z < sl; z++) { |
5198 | 1.79M | if (next) { |
5199 | 1.74M | s32 delta = gf_bs_read_se(bs); |
5200 | 1.74M | next = (last + delta + 256) % 256; |
5201 | 1.74M | } |
5202 | 1.79M | last = next ? next : last; |
5203 | 1.79M | } |
5204 | 67.0k | } |
5205 | 103k | } |
5206 | 12.9k | } |
5207 | 62.8k | break; |
5208 | 187k | } |
5209 | | |
5210 | 186k | sps->profile_idc = profile_idc; |
5211 | 186k | sps->level_idc = level_idc; |
5212 | 186k | sps->prof_compat = pcomp; |
5213 | 186k | sps->log2_max_frame_num = gf_bs_read_ue_log(bs, "log2_max_frame_num") + 4; |
5214 | 186k | if (sps->log2_max_frame_num>16) { |
5215 | 3.14k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[avc-h264] invalid SPS: log2_max_frame_num_minus4 shall be less than 12, but is %d\n", sps->log2_max_frame_num-4)); |
5216 | 3.14k | AVC_SPS_BROKEN |
5217 | 3.14k | } |
5218 | 182k | sps->poc_type = gf_bs_read_ue_log(bs, "poc_type"); |
5219 | 182k | if (sps->poc_type>2) { |
5220 | 7.97k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[avc-h264] invalid SPS: pic_order_cnt_type shall be less than 2, but is %d\n", sps->poc_type)); |
5221 | 7.97k | AVC_SPS_BROKEN |
5222 | 7.97k | } |
5223 | 175k | sps->chroma_format = chroma_format_idc; |
5224 | 175k | sps->luma_bit_depth_m8 = luma_bd; |
5225 | 175k | sps->chroma_bit_depth_m8 = chroma_bd; |
5226 | | |
5227 | 175k | if (sps->poc_type == 0) { |
5228 | 142k | sps->log2_max_poc_lsb = gf_bs_read_ue_log(bs, "log2_max_poc_lsb") + 4; |
5229 | | //log2_max_poc_lsb shall be in the range of 0 to 12, inclusive |
5230 | 142k | if (sps->log2_max_poc_lsb>16) { |
5231 | 2.42k | AVC_SPS_BROKEN |
5232 | 2.42k | } |
5233 | 142k | } |
5234 | 32.4k | else if (sps->poc_type == 1) { |
5235 | 27.5k | sps->delta_pic_order_always_zero_flag = gf_bs_read_int_log(bs, 1, "delta_pic_order_always_zero_flag"); |
5236 | 27.5k | sps->offset_for_non_ref_pic = gf_bs_read_se_log(bs, "offset_for_non_ref_pic"); |
5237 | 27.5k | sps->offset_for_top_to_bottom_field = gf_bs_read_se_log(bs, "offset_for_top_to_bottom_field"); |
5238 | 27.5k | sps->poc_cycle_length = gf_bs_read_ue_log(bs, "poc_cycle_length"); |
5239 | 27.5k | if (sps->poc_cycle_length > GF_ARRAY_LENGTH(sps->offset_for_ref_frame)) { |
5240 | 91 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[avc-h264] offset_for_ref_frame overflow from poc_cycle_length\n")); |
5241 | 91 | AVC_SPS_BROKEN |
5242 | 91 | } |
5243 | 89.1k | for (i = 0; i < sps->poc_cycle_length; i++) |
5244 | 61.6k | sps->offset_for_ref_frame[i] = gf_bs_read_se_log_idx(bs, "offset_for_ref_frame", i); |
5245 | 27.4k | } |
5246 | 172k | if (sps->poc_type > 2) { |
5247 | 0 | AVC_SPS_BROKEN |
5248 | 0 | } |
5249 | 172k | sps->max_num_ref_frames = gf_bs_read_ue_log(bs, "max_num_ref_frames"); |
5250 | 172k | sps->gaps_in_frame_num_value_allowed_flag = gf_bs_read_int_log(bs, 1, "gaps_in_frame_num_value_allowed_flag"); |
5251 | 172k | mb_width = gf_bs_read_ue_log(bs, "pic_width_in_mbs_minus1") + 1; |
5252 | 172k | mb_height = gf_bs_read_ue_log(bs, "pic_height_in_map_units_minus1") + 1; |
5253 | | //5.1 level max frame size in MBs is 36864, we set our limit at 16k x 16x pixels (eg 1 M MBs) for fuzzed stream detection |
5254 | 172k | if ( (u64) mb_width * (u64) mb_height > 1000000) { |
5255 | 834 | AVC_SPS_BROKEN |
5256 | 834 | } |
5257 | | |
5258 | 171k | sps->frame_mbs_only_flag = gf_bs_read_int_log(bs, 1, "frame_mbs_only_flag"); |
5259 | | |
5260 | 171k | sps->width = mb_width * 16; |
5261 | 171k | sps->height = (2 - sps->frame_mbs_only_flag) * mb_height * 16; |
5262 | | |
5263 | 171k | if (!sps->frame_mbs_only_flag) sps->mb_adaptive_frame_field_flag = gf_bs_read_int_log(bs, 1, "mb_adaptive_frame_field_flag"); |
5264 | 171k | gf_bs_read_int_log(bs, 1, "direct_8x8_inference_flag"); |
5265 | | |
5266 | 171k | if (gf_bs_read_int_log(bs, 1, "frame_cropping_flag")) { |
5267 | 50.4k | int CropUnitX, CropUnitY, SubWidthC = -1, SubHeightC = -1; |
5268 | | |
5269 | 50.4k | if (chroma_format_idc == 1) { |
5270 | 24.7k | SubWidthC = 2; SubHeightC = 2; |
5271 | 24.7k | } |
5272 | 25.7k | else if (chroma_format_idc == 2) { |
5273 | 1.21k | SubWidthC = 2; SubHeightC = 1; |
5274 | 1.21k | } |
5275 | 24.4k | else if ((chroma_format_idc == 3) && (separate_colour_plane_flag == 0)) { |
5276 | 876 | SubWidthC = 1; SubHeightC = 1; |
5277 | 876 | } |
5278 | | |
5279 | 50.4k | if (sps->ChromaArrayType == 0) { |
5280 | 20.3k | gf_assert(SubWidthC == -1); |
5281 | 20.3k | CropUnitX = 1; |
5282 | 20.3k | CropUnitY = 2 - sps->frame_mbs_only_flag; |
5283 | 20.3k | } |
5284 | 30.1k | else { |
5285 | 30.1k | CropUnitX = SubWidthC; |
5286 | 30.1k | CropUnitY = SubHeightC * (2 - sps->frame_mbs_only_flag); |
5287 | 30.1k | } |
5288 | | |
5289 | 50.4k | cl = gf_bs_read_ue_log(bs, "frame_crop_left_offset"); |
5290 | 50.4k | cr = gf_bs_read_ue_log(bs, "frame_crop_right_offset"); |
5291 | 50.4k | ct = gf_bs_read_ue_log(bs, "frame_crop_top_offset"); |
5292 | 50.4k | cb = gf_bs_read_ue_log(bs, "frame_crop_bottom_offset"); |
5293 | | |
5294 | 50.4k | sps->width -= CropUnitX * (cl + cr); |
5295 | 50.4k | sps->height -= CropUnitY * (ct + cb); |
5296 | 50.4k | cl *= CropUnitX; |
5297 | 50.4k | cr *= CropUnitX; |
5298 | 50.4k | ct *= CropUnitY; |
5299 | 50.4k | cb *= CropUnitY; |
5300 | 50.4k | } |
5301 | 171k | sps->crop.left = cl; |
5302 | 171k | sps->crop.right = cr; |
5303 | 171k | sps->crop.top = ct; |
5304 | 171k | sps->crop.bottom = cb; |
5305 | | |
5306 | 171k | if (vui_flag_pos) { |
5307 | 0 | *vui_flag_pos = (u32)gf_bs_get_bit_offset(bs); |
5308 | 0 | } |
5309 | | /*vui_parameters_present_flag*/ |
5310 | 171k | sps->vui_parameters_present_flag = gf_bs_read_int_log(bs, 1, "vui_parameters_present_flag"); |
5311 | 171k | if (sps->vui_parameters_present_flag) { |
5312 | 83.9k | sps->vui.aspect_ratio_info_present_flag = gf_bs_read_int_log(bs, 1, "aspect_ratio_info_present_flag"); |
5313 | 83.9k | if (sps->vui.aspect_ratio_info_present_flag) { |
5314 | 57.9k | s32 aspect_ratio_idc = gf_bs_read_int_log(bs, 8, "aspect_ratio_idc"); |
5315 | 57.9k | if (aspect_ratio_idc == 255) { |
5316 | 40.2k | sps->vui.par_num = gf_bs_read_int_log(bs, 16, "aspect_ratio_num"); |
5317 | 40.2k | sps->vui.par_den = gf_bs_read_int_log(bs, 16, "aspect_ratio_den"); |
5318 | 40.2k | } |
5319 | 17.7k | else if (aspect_ratio_idc < GF_ARRAY_LENGTH(avc_hevc_sar) ) { |
5320 | 1.57k | sps->vui.par_num = avc_hevc_sar[aspect_ratio_idc].w; |
5321 | 1.57k | sps->vui.par_den = avc_hevc_sar[aspect_ratio_idc].h; |
5322 | 1.57k | } |
5323 | 16.1k | else { |
5324 | 16.1k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] Unknown aspect_ratio_idc: your video may have a wrong aspect ratio. Contact the GPAC team!\n")); |
5325 | 16.1k | } |
5326 | 57.9k | } |
5327 | 83.9k | sps->vui.overscan_info_present_flag = gf_bs_read_int_log(bs, 1, "overscan_info_present_flag"); |
5328 | 83.9k | if (sps->vui.overscan_info_present_flag) |
5329 | 22.5k | gf_bs_read_int_log(bs, 1, "overscan_appropriate_flag"); |
5330 | | |
5331 | | /* default values */ |
5332 | 83.9k | sps->vui.video_format = 5; |
5333 | 83.9k | sps->vui.colour_primaries = 2; |
5334 | 83.9k | sps->vui.transfer_characteristics = 2; |
5335 | 83.9k | sps->vui.matrix_coefficients = 2; |
5336 | | //When the chroma_sample_loc_type_top_field and chroma_sample_loc_type_bottom_field are not present, the values of chroma_sample_loc_type_top_field and chroma_sample_loc_type_bottom_field shall be inferred to be equal to 0. |
5337 | 83.9k | sps->vui.chroma_sample_loc_type_top_field = sps->vui.chroma_sample_loc_type_bottom_field = 0; |
5338 | | |
5339 | | /* now read values if possible */ |
5340 | 83.9k | sps->vui.video_signal_type_present_flag = gf_bs_read_int_log(bs, 1, "video_signal_type_present_flag"); |
5341 | 83.9k | if (sps->vui.video_signal_type_present_flag) { |
5342 | 42.4k | sps->vui.video_format = gf_bs_read_int_log(bs, 3, "video_format"); |
5343 | 42.4k | sps->vui.video_full_range_flag = gf_bs_read_int_log(bs, 1, "video_full_range_flag"); |
5344 | 42.4k | sps->vui.colour_description_present_flag = gf_bs_read_int_log(bs, 1, "colour_description_present_flag"); |
5345 | 42.4k | if (sps->vui.colour_description_present_flag) { |
5346 | 33.5k | sps->vui.colour_primaries = gf_bs_read_int_log(bs, 8, "colour_primaries"); |
5347 | 33.5k | sps->vui.transfer_characteristics = gf_bs_read_int_log(bs, 8, "transfer_characteristics"); |
5348 | 33.5k | sps->vui.matrix_coefficients = gf_bs_read_int_log(bs, 8, "matrix_coefficients"); |
5349 | 33.5k | } |
5350 | 42.4k | } |
5351 | | |
5352 | 83.9k | sps->vui.chroma_location_info_present_flag = gf_bs_read_int_log(bs, 1, "chroma_location_info_present_flag"); |
5353 | 83.9k | if (sps->vui.chroma_location_info_present_flag) { |
5354 | 21.8k | sps->vui.chroma_sample_loc_type_top_field = gf_bs_read_ue_log(bs, "chroma_sample_location_type_top_field"); |
5355 | 21.8k | sps->vui.chroma_sample_loc_type_bottom_field = gf_bs_read_ue_log(bs, "chroma_sample_location_type_bottom_field"); |
5356 | 21.8k | } |
5357 | | |
5358 | 83.9k | sps->vui.timing_info_present_flag = gf_bs_read_int_log(bs, 1, "timing_info_present_flag"); |
5359 | 83.9k | if (sps->vui.timing_info_present_flag) { |
5360 | 25.3k | sps->vui.num_units_in_tick = gf_bs_read_int_log(bs, 32, "num_units_in_tick"); |
5361 | 25.3k | sps->vui.time_scale = gf_bs_read_int_log(bs, 32, "time_scale"); |
5362 | 25.3k | sps->vui.fixed_frame_rate_flag = gf_bs_read_int_log(bs, 1, "fixed_frame_rate_flag"); |
5363 | 25.3k | } |
5364 | | |
5365 | 83.9k | sps->vui.nal_hrd_parameters_present_flag = gf_bs_read_int_log(bs, 1, "nal_hrd_parameters_present_flag"); |
5366 | 83.9k | if (sps->vui.nal_hrd_parameters_present_flag) |
5367 | 44.3k | if (avc_parse_hrd_parameters(bs, &sps->vui.hrd)<0) |
5368 | 905 | AVC_SPS_BROKEN |
5369 | | |
5370 | 83.0k | sps->vui.vcl_hrd_parameters_present_flag = gf_bs_read_int_log(bs, 1, "vcl_hrd_parameters_present_flag"); |
5371 | 83.0k | if (sps->vui.vcl_hrd_parameters_present_flag) |
5372 | 25.0k | if (avc_parse_hrd_parameters(bs, &sps->vui.hrd)<0) |
5373 | 349 | AVC_SPS_BROKEN |
5374 | | |
5375 | 82.7k | if (sps->vui.nal_hrd_parameters_present_flag || sps->vui.vcl_hrd_parameters_present_flag) |
5376 | 47.7k | sps->vui.low_delay_hrd_flag = gf_bs_read_int_log(bs, 1, "low_delay_hrd_flag"); |
5377 | | |
5378 | 82.7k | sps->vui.pic_struct_present_flag = gf_bs_read_int_log(bs, 1, "pic_struct_present_flag"); |
5379 | 82.7k | } |
5380 | | /*end of seq_parameter_set_data*/ |
5381 | | |
5382 | 170k | if (subseq_sps) { |
5383 | 45.7k | if ((profile_idc == 83) || (profile_idc == 86)) { |
5384 | 7.98k | u8 extended_spatial_scalability_idc; |
5385 | | /*parsing seq_parameter_set_svc_extension*/ |
5386 | | |
5387 | 7.98k | gf_bs_read_int_log(bs, 1, "inter_layer_deblocking_filter_control_present_flag"); |
5388 | 7.98k | extended_spatial_scalability_idc = gf_bs_read_int_log(bs, 2, "extended_spatial_scalability_idc"); |
5389 | 7.98k | if (sps->ChromaArrayType == 1 || sps->ChromaArrayType == 2) { |
5390 | 3.63k | gf_bs_read_int_log(bs, 1, "chroma_phase_x_plus1_flag"); |
5391 | 3.63k | } |
5392 | 7.98k | if (sps->ChromaArrayType == 1) { |
5393 | 3.08k | gf_bs_read_int_log(bs, 2, "chroma_phase_y_plus1"); |
5394 | 3.08k | } |
5395 | 7.98k | if (extended_spatial_scalability_idc == 1) { |
5396 | 59 | if (sps->ChromaArrayType > 0) { |
5397 | 31 | gf_bs_read_int_log(bs, 1, "seq_ref_layer_chroma_phase_x_plus1_flag"); |
5398 | 31 | gf_bs_read_int_log(bs, 2, "seq_ref_layer_chroma_phase_y_plus1"); |
5399 | 31 | } |
5400 | 59 | gf_bs_read_se_log(bs, "seq_scaled_ref_layer_left_offset"); |
5401 | 59 | gf_bs_read_se_log(bs, "seq_scaled_ref_layer_top_offset"); |
5402 | 59 | gf_bs_read_se_log(bs, "seq_scaled_ref_layer_right_offset"); |
5403 | 59 | gf_bs_read_se_log(bs, "seq_scaled_ref_layer_bottom_offset"); |
5404 | 59 | } |
5405 | 7.98k | if (gf_bs_read_int_log(bs, 1, "seq_tcoeff_level_prediction_flag")) { |
5406 | 2.04k | gf_bs_read_int_log(bs, 1, "adaptive_tcoeff_level_prediction_flag"); |
5407 | 2.04k | } |
5408 | 7.98k | gf_bs_read_int_log(bs, 1, "slice_header_restriction_flag"); |
5409 | | |
5410 | 7.98k | if (gf_bs_read_int_log(bs, 1, "svc_vui_parameters_present")) { |
5411 | 1.65k | u32 vui_ext_num_entries_minus1 = gf_bs_read_ue_log(bs, "vui_ext_num_entries_minus1"); |
5412 | | |
5413 | 644k | for (i = 0; i <= vui_ext_num_entries_minus1; i++) { |
5414 | 642k | u8 vui_ext_nal_hrd_parameters_present_flag, vui_ext_vcl_hrd_parameters_present_flag, vui_ext_timing_info_present_flag; |
5415 | 642k | gf_bs_read_int_log(bs, 3, "vui_ext_dependency_id"); |
5416 | 642k | gf_bs_read_int_log(bs, 4, "vui_ext_quality_id"); |
5417 | 642k | gf_bs_read_int_log(bs, 3, "vui_ext_temporal_id"); |
5418 | 642k | vui_ext_timing_info_present_flag = gf_bs_read_int_log(bs, 1, "vui_ext_timing_info_present_flag"); |
5419 | 642k | if (vui_ext_timing_info_present_flag) { |
5420 | 1.30k | gf_bs_read_int_log(bs, 32, "vui_ext_num_units_in_tick"); |
5421 | 1.30k | gf_bs_read_int_log(bs, 32, "vui_ext_time_scale"); |
5422 | 1.30k | gf_bs_read_int_log(bs, 1, "vui_ext_fixed_frame_rate_flag"); |
5423 | 1.30k | } |
5424 | 642k | vui_ext_nal_hrd_parameters_present_flag = gf_bs_read_int_log(bs, 1, "vui_ext_nal_hrd_parameters_present_flag"); |
5425 | 642k | if (vui_ext_nal_hrd_parameters_present_flag) { |
5426 | | //hrd_parameters( ) |
5427 | 999 | } |
5428 | 642k | vui_ext_vcl_hrd_parameters_present_flag = gf_bs_read_int_log(bs, 1, "vui_ext_vcl_hrd_parameters_present_flag"); |
5429 | 642k | if (vui_ext_vcl_hrd_parameters_present_flag) { |
5430 | | //hrd_parameters( ) |
5431 | 862 | } |
5432 | 642k | if (vui_ext_nal_hrd_parameters_present_flag || vui_ext_vcl_hrd_parameters_present_flag) { |
5433 | 1.30k | gf_bs_read_int_log(bs, 1, "vui_ext_low_delay_hrd_flag"); |
5434 | 1.30k | } |
5435 | 642k | gf_bs_read_int_log(bs, 1, "vui_ext_pic_struct_present_flag"); |
5436 | 642k | } |
5437 | 1.65k | } |
5438 | 7.98k | } |
5439 | 37.7k | else if ((profile_idc == 118) || (profile_idc == 128)) { |
5440 | 2.61k | GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[avc-h264] MVC parsing not implemented - skipping parsing end of Subset SPS\n")); |
5441 | 2.61k | return sps_id; |
5442 | 2.61k | } |
5443 | | |
5444 | 43.1k | if (gf_bs_read_int_log(bs, 1, "additional_extension2")) { |
5445 | 18.0k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] skipping parsing end of Subset SPS (additional_extension2)\n")); |
5446 | 18.0k | return sps_id; |
5447 | 18.0k | } |
5448 | 43.1k | } |
5449 | 149k | if (gf_bs_is_overflow(bs)) |
5450 | 49.2k | AVC_SPS_BROKEN |
5451 | 100k | return sps_id; |
5452 | 149k | } |
5453 | | |
5454 | | GF_EXPORT |
5455 | | s32 gf_avc_read_sps_bs(GF_BitStream *bs, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos) |
5456 | 117k | { |
5457 | 117k | return gf_avc_read_sps_bs_internal(bs, avc, subseq_sps, vui_flag_pos, 0); |
5458 | 117k | } |
5459 | | |
5460 | | GF_EXPORT |
5461 | | s32 gf_avc_read_sps(const u8 *sps_data, u32 sps_size, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos) |
5462 | 18.7k | { |
5463 | 18.7k | s32 sps_id = -1; |
5464 | 18.7k | GF_BitStream *bs; |
5465 | 18.7k | char *sps_data_without_emulation_bytes = NULL; |
5466 | 18.7k | u32 sps_data_without_emulation_bytes_size = 0; |
5467 | | |
5468 | 18.7k | if (vui_flag_pos) { |
5469 | | /*SPS still contains emulation bytes*/ |
5470 | 0 | sps_data_without_emulation_bytes = gf_malloc(sps_size * sizeof(char)); |
5471 | 0 | sps_data_without_emulation_bytes_size = gf_media_nalu_remove_emulation_bytes(sps_data, sps_data_without_emulation_bytes, sps_size); |
5472 | 0 | bs = gf_bs_new(sps_data_without_emulation_bytes, sps_data_without_emulation_bytes_size, GF_BITSTREAM_READ); |
5473 | |
|
5474 | 0 | *vui_flag_pos = 0; |
5475 | 0 | } |
5476 | 18.7k | else { |
5477 | 18.7k | bs = gf_bs_new(sps_data, sps_size, GF_BITSTREAM_READ); |
5478 | 18.7k | } |
5479 | | |
5480 | 18.7k | if (!bs) { |
5481 | 0 | sps_id = -1; |
5482 | 0 | goto exit; |
5483 | 0 | } |
5484 | | |
5485 | 18.7k | sps_id = gf_avc_read_sps_bs(bs, avc, subseq_sps, vui_flag_pos); |
5486 | | |
5487 | 18.7k | exit: |
5488 | 18.7k | gf_bs_del(bs); |
5489 | 18.7k | if (sps_data_without_emulation_bytes) gf_free(sps_data_without_emulation_bytes); |
5490 | 18.7k | return sps_id; |
5491 | 18.7k | } |
5492 | | |
5493 | 52.1k | #define AVC_PPS_BROKEN {\ |
5494 | 52.1k | memset(pps, 0, sizeof(AVC_PPS)); \ |
5495 | 52.1k | return -1;\ |
5496 | 52.1k | } |
5497 | | |
5498 | | static s32 gf_avc_read_pps_bs_internal(GF_BitStream *bs, AVCState *avc, u32 nal_hdr) |
5499 | 143k | { |
5500 | 143k | s32 pps_id; |
5501 | 143k | AVC_PPS *pps; |
5502 | | |
5503 | 143k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
5504 | | |
5505 | 143k | if (!nal_hdr) { |
5506 | 60.8k | gf_bs_read_int_log(bs, 1, "forbidden_zero_bit"); |
5507 | 60.8k | gf_bs_read_int_log(bs, 2, "nal_ref_idc"); |
5508 | 60.8k | gf_bs_read_int_log(bs, 5, "nal_unit_type"); |
5509 | 60.8k | } |
5510 | 143k | pps_id = gf_bs_read_ue_log(bs, "pps_id"); |
5511 | 143k | if ((pps_id<0) || (pps_id >= 255)) { |
5512 | 9.61k | return -1; |
5513 | 9.61k | } |
5514 | 133k | pps = &avc->pps[pps_id]; |
5515 | 133k | pps->id = pps_id; |
5516 | | |
5517 | 133k | if (!pps->status) pps->status = 1; |
5518 | 133k | pps->sps_id = gf_bs_read_ue_log(bs, "sps_id"); |
5519 | 133k | if ((pps->sps_id<0) || (pps->sps_id >= 32)) { |
5520 | 5.02k | AVC_PPS_BROKEN |
5521 | 5.02k | } |
5522 | | /*sps_id may be refer to regular SPS or subseq sps, depending on the coded slice referring to the pps*/ |
5523 | 128k | if (!avc->sps[pps->sps_id].state |
5524 | 128k | && ((pps->sps_id + GF_SVC_SSPS_ID_SHIFT<32) && !avc->sps[pps->sps_id + GF_SVC_SSPS_ID_SHIFT].state) |
5525 | 128k | ) { |
5526 | 24.5k | AVC_PPS_BROKEN |
5527 | 24.5k | } |
5528 | 104k | avc->pps_active_idx = pps->id; /*set active sps*/ |
5529 | 104k | avc->sps_active_idx = pps->sps_id; /*set active sps*/ |
5530 | 104k | pps->entropy_coding_mode_flag = gf_bs_read_int_log(bs, 1, "entropy_coding_mode_flag"); |
5531 | 104k | pps->pic_order_present = gf_bs_read_int_log(bs, 1, "pic_order_present"); |
5532 | 104k | pps->slice_group_count = gf_bs_read_ue_log(bs, "slice_group_count_minus1") + 1; |
5533 | 104k | if (pps->slice_group_count > 1) { |
5534 | 17.1k | u32 iGroup; |
5535 | 17.1k | pps->mb_slice_group_map_type = gf_bs_read_ue_log(bs, "mb_slice_group_map_type"); |
5536 | 17.1k | if (pps->mb_slice_group_map_type == 0) { |
5537 | 49.8k | for (iGroup = 0; iGroup <= pps->slice_group_count - 1; iGroup++) |
5538 | 41.2k | gf_bs_read_ue_log_idx(bs, "run_length_minus1", iGroup); |
5539 | 8.60k | } |
5540 | 8.57k | else if (pps->mb_slice_group_map_type == 2) { |
5541 | 222k | for (iGroup = 0; iGroup < pps->slice_group_count - 1; iGroup++) { |
5542 | 222k | gf_bs_read_ue_log_idx(bs, "top_left", iGroup); |
5543 | 222k | gf_bs_read_ue_log_idx(bs, "bottom_right", iGroup); |
5544 | 222k | } |
5545 | 141 | } |
5546 | 8.43k | else if (pps->mb_slice_group_map_type == 3 || pps->mb_slice_group_map_type == 4 || pps->mb_slice_group_map_type == 5) { |
5547 | 1.53k | gf_bs_read_int_log(bs, 1, "slice_group_change_direction_flag"); |
5548 | 1.53k | gf_bs_read_ue_log(bs, "slice_group_change_rate_minus1"); |
5549 | 1.53k | } |
5550 | 6.89k | else if (pps->mb_slice_group_map_type == 6) { |
5551 | 1.07k | u32 i; |
5552 | 1.07k | pps->pic_size_in_map_units_minus1 = gf_bs_read_ue_log(bs, "pic_size_in_map_units_minus1"); |
5553 | 5.38k | for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++) { |
5554 | 4.31k | gf_bs_read_int_log_idx(bs, (u32)ceil(log(pps->slice_group_count) / log(2)), "slice_group_id", i); |
5555 | 4.31k | } |
5556 | 1.07k | } |
5557 | 17.1k | } |
5558 | 104k | pps->num_ref_idx_l0_default_active_minus1 = gf_bs_read_ue_log(bs, "num_ref_idx_l0_default_active_minus1"); |
5559 | 104k | pps->num_ref_idx_l1_default_active_minus1 = gf_bs_read_ue_log(bs, "num_ref_idx_l1_default_active_minus1"); |
5560 | | |
5561 | | /* |
5562 | | if ((pps->ref_count[0] > 32) || (pps->ref_count[1] > 32)) goto exit; |
5563 | | */ |
5564 | | |
5565 | 104k | pps->weighted_pred_flag = gf_bs_read_int_log(bs, 1, "weighted_pred_flag"); |
5566 | 104k | gf_bs_read_int_log(bs, 2, "weighted_bipred_idc"); |
5567 | 104k | gf_bs_read_se_log(bs, "init_qp_minus26"); |
5568 | 104k | gf_bs_read_se_log(bs, "init_qs_minus26"); |
5569 | 104k | gf_bs_read_se_log(bs, "chroma_qp_index_offset"); |
5570 | 104k | pps->deblocking_filter_control_present_flag = gf_bs_read_int_log(bs, 1, "deblocking_filter_control_present_flag"); |
5571 | 104k | gf_bs_read_int_log(bs, 1, "constrained_intra_pred"); |
5572 | 104k | pps->redundant_pic_cnt_present = gf_bs_read_int_log(bs, 1, "redundant_pic_cnt_present"); |
5573 | | |
5574 | 104k | if (gf_bs_is_overflow(bs)) |
5575 | 22.5k | AVC_PPS_BROKEN |
5576 | 81.8k | return pps_id; |
5577 | 104k | } |
5578 | | |
5579 | | GF_EXPORT |
5580 | | s32 gf_avc_read_pps_bs(GF_BitStream *bs, AVCState *avc) |
5581 | 60.8k | { |
5582 | 60.8k | return gf_avc_read_pps_bs_internal(bs, avc, 0); |
5583 | 60.8k | } |
5584 | | |
5585 | | GF_EXPORT |
5586 | | s32 gf_avc_read_pps(const u8 *pps_data, u32 pps_size, AVCState *avc) |
5587 | 0 | { |
5588 | 0 | GF_BitStream *bs; |
5589 | 0 | s32 pps_id; |
5590 | | |
5591 | | /*PPS still contains emulation bytes*/ |
5592 | 0 | bs = gf_bs_new(pps_data, pps_size, GF_BITSTREAM_READ); |
5593 | 0 | if (!bs) { |
5594 | 0 | return -1; |
5595 | 0 | } |
5596 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
5597 | 0 | pps_id = gf_avc_read_pps_bs(bs, avc); |
5598 | 0 | gf_bs_del(bs); |
5599 | 0 | return pps_id; |
5600 | 0 | } |
5601 | | |
5602 | | #if 0 //unused |
5603 | | |
5604 | | s32 gf_avc_read_sps_ext(const char *spse_data, u32 spse_size) |
5605 | | { |
5606 | | GF_BitStream *bs; |
5607 | | s32 sps_id; |
5608 | | |
5609 | | bs = gf_bs_new(spse_data, spse_size, GF_BITSTREAM_READ); |
5610 | | sps_id = gf_avc_read_sps_ext_bs(bs); |
5611 | | |
5612 | | gf_bs_del(bs); |
5613 | | return sps_id; |
5614 | | } |
5615 | | #endif |
5616 | | |
5617 | | static s32 SVC_ReadNal_header_extension(GF_BitStream *bs, SVC_NALUHeader *NalHeader) |
5618 | 67.5k | { |
5619 | 67.5k | gf_bs_read_int_log(bs, 1, "reserved_one_bit"); |
5620 | 67.5k | NalHeader->idr_pic_flag = gf_bs_read_int_log(bs, 1, "idr_flag"); |
5621 | 67.5k | NalHeader->priority_id = gf_bs_read_int_log(bs, 6, "priority_id"); |
5622 | 67.5k | gf_bs_read_int_log(bs, 1, "no_inter_layer_pred_flag"); |
5623 | 67.5k | NalHeader->dependency_id = gf_bs_read_int_log(bs, 3, "DependencyId"); |
5624 | 67.5k | NalHeader->quality_id = gf_bs_read_int_log(bs, 4, "quality_id"); |
5625 | 67.5k | NalHeader->temporal_id = gf_bs_read_int_log(bs, 3, "temporal_id"); |
5626 | 67.5k | gf_bs_read_int_log(bs, 1, "use_ref_base_pic_flag"); |
5627 | 67.5k | gf_bs_read_int_log(bs, 1, "discardable_flag"); |
5628 | 67.5k | gf_bs_read_int_log(bs, 1, "output_flag"); |
5629 | 67.5k | gf_bs_read_int_log(bs, 2, "reserved_three_2bits"); |
5630 | 67.5k | return 1; |
5631 | 67.5k | } |
5632 | | |
5633 | 133k | static void avc_ref_pic_list_modification(GF_BitStream *bs, u32 slice_type, Bool is_mvc) { |
5634 | 133k | if (slice_type % 5 != 2 && slice_type % 5 != 4) { |
5635 | 120k | if (gf_bs_read_int_log(bs, 1, "ref_pic_list_modification_flag_l0")) { |
5636 | 28.6k | u32 idx=0, modification_of_pic_nums_idc; |
5637 | 977k | do { |
5638 | 977k | modification_of_pic_nums_idc = gf_bs_read_ue_log_idx(bs, "modification_of_pic_nums_idc", idx); |
5639 | 977k | if (modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1) { |
5640 | 745k | gf_bs_read_ue_log_idx(bs, "abs_diff_pic_num_minus1", idx); |
5641 | 745k | } |
5642 | 232k | else if (modification_of_pic_nums_idc == 2) { |
5643 | 63.3k | gf_bs_read_ue_log_idx(bs, "long_term_pic_num", idx); |
5644 | 63.3k | } |
5645 | 168k | else if (is_mvc && ((modification_of_pic_nums_idc == 4) || (modification_of_pic_nums_idc == 5))) { |
5646 | 0 | gf_bs_read_ue_log_idx(bs, "abs_diff_view_idx_minus1", idx); |
5647 | 0 | } |
5648 | 977k | idx++; |
5649 | 977k | } while ((modification_of_pic_nums_idc != 3) && gf_bs_available(bs)); |
5650 | 28.6k | } |
5651 | 120k | } |
5652 | 133k | if (slice_type % 5 == 1) { |
5653 | 4.61k | if (gf_bs_read_int_log(bs, 1, "ref_pic_list_modification_flag_l1")) { |
5654 | 2.06k | u32 idx=0, modification_of_pic_nums_idc; |
5655 | 230k | do { |
5656 | 230k | modification_of_pic_nums_idc = gf_bs_read_ue_log_idx(bs, "modification_of_pic_nums_idc", idx); |
5657 | 230k | if (modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1) { |
5658 | 148k | gf_bs_read_ue_log_idx(bs, "abs_diff_pic_num_minus1", idx); |
5659 | 148k | } |
5660 | 81.9k | else if (modification_of_pic_nums_idc == 2) { |
5661 | 32.2k | gf_bs_read_ue_log_idx(bs, "long_term_pic_num", idx); |
5662 | 32.2k | } |
5663 | 49.6k | else if (is_mvc && ((modification_of_pic_nums_idc == 4) || (modification_of_pic_nums_idc == 5))) { |
5664 | 0 | gf_bs_read_ue_log_idx(bs, "abs_diff_view_idx_minus1", idx); |
5665 | 0 | } |
5666 | 230k | idx++; |
5667 | 230k | } while ((modification_of_pic_nums_idc != 3) && gf_bs_available(bs)); |
5668 | 2.06k | } |
5669 | 4.61k | } |
5670 | 133k | } |
5671 | | |
5672 | 19.6k | static void avc_pred_weight_table(GF_BitStream *bs, u32 slice_type, u32 ChromaArrayType, u32 num_ref_idx_l0_active_minus1, u32 num_ref_idx_l1_active_minus1) { |
5673 | 19.6k | u32 i, j; |
5674 | 19.6k | gf_bs_read_ue_log(bs, "luma_log2_weight_denom"); |
5675 | 19.6k | if (ChromaArrayType != 0) { |
5676 | 10.5k | gf_bs_read_ue_log(bs, "chroma_log2_weight_denom"); |
5677 | 10.5k | } |
5678 | 2.20M | for (i = 0; i <= num_ref_idx_l0_active_minus1; i++) { |
5679 | 2.18M | if (gf_bs_read_int_log_idx(bs, 1, "luma_weight_l0_flag", i)) { |
5680 | 30.1k | gf_bs_read_se_log_idx(bs, "luma_weight_l0", i); |
5681 | 30.1k | gf_bs_read_se_log_idx(bs, "luma_offset_l0", i); |
5682 | 30.1k | } |
5683 | 2.18M | if (ChromaArrayType != 0) { |
5684 | 615k | if (gf_bs_read_int_log_idx(bs, 1, "chroma_weight_l0_flag", i)) |
5685 | 41.3k | for (j = 0; j < 2; j++) { |
5686 | 27.5k | gf_bs_read_se_log_idx2(bs, "chroma_weight_l0", i, j); |
5687 | 27.5k | gf_bs_read_se_log_idx2(bs, "chroma_offset_l0", i, j); |
5688 | 27.5k | } |
5689 | 615k | } |
5690 | 2.18M | } |
5691 | 19.6k | if (slice_type % 5 == 1) { |
5692 | 0 | for (i = 0; i <= num_ref_idx_l1_active_minus1; i++) { |
5693 | 0 | if (gf_bs_read_int_log_idx(bs, 1, "luma_weight_l1_flag", i)) { |
5694 | 0 | gf_bs_read_se_log_idx(bs, "luma_weight_l1", i); |
5695 | 0 | gf_bs_read_se_log_idx(bs, "luma_offset_l1", i); |
5696 | 0 | } |
5697 | 0 | if (ChromaArrayType != 0) { |
5698 | 0 | if (gf_bs_read_int_log_idx(bs, 1, "chroma_weight_l1_flag", i)) { |
5699 | 0 | for (j = 0; j < 2; j++) { |
5700 | 0 | gf_bs_read_se_log_idx2(bs, "chroma_weight_l1", i, j); |
5701 | 0 | gf_bs_read_se_log_idx2(bs, "chroma_offset_l1", i, j); |
5702 | 0 | } |
5703 | 0 | } |
5704 | 0 | } |
5705 | 0 | } |
5706 | 0 | } |
5707 | 19.6k | } |
5708 | | |
5709 | 112k | static void dec_ref_pic_marking(GF_BitStream *bs, Bool IdrPicFlag) { |
5710 | 112k | if (IdrPicFlag) { |
5711 | 91.9k | gf_bs_read_int_log(bs, 1, "no_output_of_prior_pics_flag"); |
5712 | 91.9k | gf_bs_read_int_log(bs, 1, "long_term_reference_flag"); |
5713 | 91.9k | } |
5714 | 20.2k | else { |
5715 | 20.2k | if (gf_bs_read_int_log(bs, 1, "adaptive_ref_pic_marking_mode_flag")) { |
5716 | 10.1k | u32 idx=0, memory_management_control_operation; |
5717 | 19.1k | do { |
5718 | 19.1k | memory_management_control_operation = gf_bs_read_ue_log_idx(bs, "memory_management_control_operation", idx); |
5719 | 19.1k | if (memory_management_control_operation == 1 || memory_management_control_operation == 3) |
5720 | 781 | gf_bs_read_ue_log_idx(bs, "difference_of_pic_nums_minus1", idx); |
5721 | 19.1k | if (memory_management_control_operation == 2) |
5722 | 5.45k | gf_bs_read_ue_log_idx(bs, "long_term_pic_num", idx); |
5723 | 19.1k | if (memory_management_control_operation == 3 || memory_management_control_operation == 6) |
5724 | 486 | gf_bs_read_ue_log_idx(bs, "long_term_frame_idx", idx); |
5725 | 19.1k | if (memory_management_control_operation == 4) |
5726 | 119 | gf_bs_read_ue_log_idx(bs, "max_long_term_frame_idx_plus1", idx); |
5727 | 19.1k | idx++; |
5728 | 19.1k | } while (memory_management_control_operation != 0); |
5729 | 10.1k | } |
5730 | 20.2k | } |
5731 | 112k | } |
5732 | | |
5733 | | static s32 avc_parse_slice(GF_BitStream *bs, AVCState *avc, Bool svc_idr_flag, AVCSliceInfo *si) |
5734 | 368k | { |
5735 | 368k | s32 pps_id, num_ref_idx_l0_active_minus1 = 0, num_ref_idx_l1_active_minus1 = 0; |
5736 | | |
5737 | | /*s->current_picture.reference= h->nal_ref_idc != 0;*/ |
5738 | 368k | gf_bs_read_ue_log(bs, "first_mb_in_slice"); |
5739 | 368k | si->slice_type = gf_bs_read_ue_log(bs, "slice_type"); |
5740 | 368k | if (si->slice_type > 9) return -1; |
5741 | | |
5742 | 361k | pps_id = gf_bs_read_ue_log(bs, "pps_id"); |
5743 | 361k | if ((pps_id<0) || (pps_id >= 255)) return -1; |
5744 | 359k | si->pps = &avc->pps[pps_id]; |
5745 | 359k | if (!si->pps->slice_group_count) return -2; |
5746 | 288k | if (si->pps->sps_id>=32) return -1; |
5747 | 288k | si->sps = &avc->sps[si->pps->sps_id]; |
5748 | 288k | if (!si->sps->log2_max_frame_num) return -2; |
5749 | 133k | avc->sps_active_idx = si->pps->sps_id; |
5750 | 133k | avc->pps_active_idx = pps_id; |
5751 | | |
5752 | 133k | si->frame_num = gf_bs_read_int_log(bs, si->sps->log2_max_frame_num, "frame_num"); |
5753 | | |
5754 | 133k | si->field_pic_flag = 0; |
5755 | 133k | si->bottom_field_flag = 0; |
5756 | 133k | if (!si->sps->frame_mbs_only_flag) { |
5757 | 48.9k | si->field_pic_flag = gf_bs_read_int_log(bs, 1, "field_pic_flag"); |
5758 | 48.9k | if (si->field_pic_flag) |
5759 | 14.2k | si->bottom_field_flag = gf_bs_read_int_log(bs, 1, "bottom_field_flag"); |
5760 | 48.9k | } |
5761 | | |
5762 | 133k | if ((si->nal_unit_type == GF_AVC_NALU_IDR_SLICE) || svc_idr_flag) |
5763 | 93.2k | si->idr_pic_id = gf_bs_read_ue_log(bs, "idr_pic_id"); |
5764 | | |
5765 | 133k | if (si->sps->poc_type == 0) { |
5766 | 78.8k | si->poc_lsb = gf_bs_read_int_log(bs, si->sps->log2_max_poc_lsb, "poc_lsb"); |
5767 | 78.8k | if (si->pps->pic_order_present && !si->field_pic_flag) { |
5768 | 41.3k | si->delta_poc_bottom = gf_bs_read_se_log(bs, "delta_poc_bottom"); |
5769 | 41.3k | } |
5770 | 78.8k | } |
5771 | 54.2k | else if ((si->sps->poc_type == 1) && !si->sps->delta_pic_order_always_zero_flag) { |
5772 | 6.09k | si->delta_poc[0] = gf_bs_read_se_log(bs, "delta_poc0"); |
5773 | 6.09k | if ((si->pps->pic_order_present == 1) && !si->field_pic_flag) |
5774 | 3.46k | si->delta_poc[1] = gf_bs_read_se_log(bs, "delta_poc1"); |
5775 | 6.09k | } |
5776 | | |
5777 | 133k | if (si->pps->redundant_pic_cnt_present) { |
5778 | 31.6k | si->redundant_pic_cnt = gf_bs_read_ue_log(bs, "redundant_pic_cnt"); |
5779 | 31.6k | } |
5780 | | |
5781 | 133k | if (si->slice_type % 5 == GF_AVC_TYPE_B) { |
5782 | 4.61k | gf_bs_read_int_log(bs, 1, "direct_spatial_mv_pred_flag"); |
5783 | 4.61k | } |
5784 | | |
5785 | 133k | num_ref_idx_l0_active_minus1 = si->pps->num_ref_idx_l0_default_active_minus1; |
5786 | 133k | num_ref_idx_l1_active_minus1 = si->pps->num_ref_idx_l1_default_active_minus1; |
5787 | | |
5788 | 133k | if (si->slice_type % 5 == GF_AVC_TYPE_P || si->slice_type % 5 == GF_AVC_TYPE_SP || si->slice_type % 5 == GF_AVC_TYPE_B) { |
5789 | 120k | Bool num_ref_idx_active_override_flag = gf_bs_read_int_log(bs, 1, "num_ref_idx_active_override_flag"); |
5790 | 120k | if (num_ref_idx_active_override_flag) { |
5791 | 30.7k | num_ref_idx_l0_active_minus1 = gf_bs_read_ue_log(bs, "num_ref_idx_l0_active_minus1"); |
5792 | 30.7k | if (si->slice_type % 5 == GF_AVC_TYPE_B) { |
5793 | 2.19k | num_ref_idx_l1_active_minus1 = gf_bs_read_ue_log(bs, "num_ref_idx_l1_active_minus1"); |
5794 | 2.19k | } |
5795 | 30.7k | } |
5796 | 120k | } |
5797 | | |
5798 | 133k | if (si->nal_unit_type == 20 || si->nal_unit_type == 21) { |
5799 | 0 | avc_ref_pic_list_modification(bs, si->slice_type, GF_TRUE); |
5800 | 133k | } else { |
5801 | 133k | avc_ref_pic_list_modification(bs, si->slice_type, GF_FALSE); |
5802 | 133k | } |
5803 | | |
5804 | 133k | if ((si->pps->weighted_pred_flag && (si->slice_type % 5 == GF_AVC_TYPE_P || si->slice_type % 5 == GF_AVC_TYPE_SP)) |
5805 | 133k | || (si->pps->weighted_bipred_idc == 1 && si->slice_type % 5 == GF_AVC_TYPE_B)) { |
5806 | 19.6k | avc_pred_weight_table(bs, si->slice_type, si->sps->ChromaArrayType, num_ref_idx_l0_active_minus1, num_ref_idx_l1_active_minus1); |
5807 | 19.6k | } |
5808 | | |
5809 | 133k | if (si->nal_ref_idc != 0) { |
5810 | 112k | dec_ref_pic_marking(bs, (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE)); |
5811 | 112k | } |
5812 | | |
5813 | 133k | if (si->pps->entropy_coding_mode_flag && si->slice_type % 5 != GF_AVC_TYPE_I && si->slice_type % 5 != GF_AVC_TYPE_SI) { |
5814 | 39.7k | gf_bs_read_ue_log(bs, "cabac_init_idc"); |
5815 | 39.7k | } |
5816 | | |
5817 | | /*slice_qp_delta = */gf_bs_read_se(bs); |
5818 | 133k | if (si->slice_type % 5 == GF_AVC_TYPE_SP || si->slice_type % 5 == GF_AVC_TYPE_SI) { |
5819 | 53.1k | if (si->slice_type % 5 == GF_AVC_TYPE_SP) { |
5820 | 51.4k | gf_bs_read_int_log(bs, 1, "sp_for_switch_flag"); |
5821 | 51.4k | } |
5822 | 53.1k | gf_bs_read_se_log(bs, "slice_qs_delta"); |
5823 | 53.1k | } |
5824 | | |
5825 | 133k | if (si->pps->deblocking_filter_control_present_flag) { |
5826 | 109k | if (gf_bs_read_ue_log(bs, "disable_deblocking_filter_idc") != 1) { |
5827 | 104k | gf_bs_read_se_log(bs, "slice_alpha_c0_offset_div2"); |
5828 | 104k | gf_bs_read_se_log(bs, "slice_beta_offset_div2"); |
5829 | 104k | } |
5830 | 109k | } |
5831 | | |
5832 | 133k | if (si->pps->slice_group_count > 1 && si->pps->mb_slice_group_map_type >= 3 && si->pps->mb_slice_group_map_type <= 5) { |
5833 | 72 | gf_bs_read_int_log(bs, (u32)ceil(log1p((si->pps->pic_size_in_map_units_minus1 + 1) / (si->pps->slice_group_change_rate_minus1 + 1) ) / log(2)), "slice_group_change_cycle"); |
5834 | 72 | } |
5835 | 133k | return 0; |
5836 | 288k | } |
5837 | | |
5838 | | |
5839 | | static s32 svc_parse_slice(GF_BitStream *bs, AVCState *avc, AVCSliceInfo *si) |
5840 | 60.5k | { |
5841 | 60.5k | s32 pps_id; |
5842 | | |
5843 | | /*s->current_picture.reference= h->nal_ref_idc != 0;*/ |
5844 | 60.5k | gf_bs_read_ue_log(bs, "first_mb_in_slice"); |
5845 | 60.5k | si->slice_type = gf_bs_read_ue_log(bs, "slice_type"); |
5846 | 60.5k | if (si->slice_type > 9) return -1; |
5847 | | |
5848 | 54.6k | pps_id = gf_bs_read_ue_log(bs, "pps_id"); |
5849 | 54.6k | if ((pps_id<0) || (pps_id >= 255)) |
5850 | 2.82k | return -1; |
5851 | 51.7k | si->pps = &avc->pps[pps_id]; |
5852 | 51.7k | si->pps->id = pps_id; |
5853 | 51.7k | if (!si->pps->slice_group_count) |
5854 | 20.1k | return -2; |
5855 | 31.6k | if (si->pps->sps_id + GF_SVC_SSPS_ID_SHIFT>=32) |
5856 | 561 | return -1; |
5857 | 31.1k | si->sps = &avc->sps[si->pps->sps_id + GF_SVC_SSPS_ID_SHIFT]; |
5858 | 31.1k | if (!si->sps->log2_max_frame_num) |
5859 | 12.7k | return -2; |
5860 | | |
5861 | 18.3k | si->frame_num = gf_bs_read_int_log(bs, si->sps->log2_max_frame_num, "frame_num"); |
5862 | | |
5863 | 18.3k | si->field_pic_flag = 0; |
5864 | 18.3k | if (si->sps->frame_mbs_only_flag) { |
5865 | | /*s->picture_structure= PICT_FRAME;*/ |
5866 | 16.4k | } |
5867 | 1.93k | else { |
5868 | 1.93k | si->field_pic_flag = gf_bs_read_int_log(bs, 1, "field_pic_flag"); |
5869 | 1.93k | if (si->field_pic_flag) si->bottom_field_flag = gf_bs_read_int_log(bs, 1, "bottom_field_flag"); |
5870 | 1.93k | } |
5871 | 18.3k | if (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE || si->svc_nalhdr.idr_pic_flag) |
5872 | 8.05k | si->idr_pic_id = gf_bs_read_ue_log(bs, "idr_pic_id"); |
5873 | | |
5874 | 18.3k | if (si->sps->poc_type == 0) { |
5875 | 16.1k | si->poc_lsb = gf_bs_read_int_log(bs, si->sps->log2_max_poc_lsb, "poc_lsb"); |
5876 | 16.1k | if (si->pps->pic_order_present && !si->field_pic_flag) { |
5877 | 6.03k | si->delta_poc_bottom = gf_bs_read_se_log(bs, "delta_poc_bottom"); |
5878 | 6.03k | } |
5879 | 16.1k | } |
5880 | 2.16k | else if ((si->sps->poc_type == 1) && !si->sps->delta_pic_order_always_zero_flag) { |
5881 | 240 | si->delta_poc[0] = gf_bs_read_se_log(bs, "delta_poc0"); |
5882 | 240 | if ((si->pps->pic_order_present == 1) && !si->field_pic_flag) |
5883 | 158 | si->delta_poc[1] = gf_bs_read_se_log(bs, "delta_poc1"); |
5884 | 240 | } |
5885 | 18.3k | if (si->pps->redundant_pic_cnt_present) { |
5886 | 2.61k | si->redundant_pic_cnt = gf_bs_read_ue_log(bs, "redundant_pic_cnt"); |
5887 | 2.61k | } |
5888 | 18.3k | return 0; |
5889 | 31.1k | } |
5890 | | |
5891 | | |
5892 | | static s32 avc_parse_recovery_point_sei(GF_BitStream *bs, AVCState *avc) |
5893 | 644 | { |
5894 | 644 | AVCSeiRecoveryPoint *rp = &avc->sei.recovery_point; |
5895 | | |
5896 | 644 | rp->frame_cnt = gf_bs_read_ue_log(bs, "frame_cnt"); |
5897 | 644 | rp->exact_match_flag = gf_bs_read_int_log(bs, 1, "exact_match_flag"); |
5898 | 644 | rp->broken_link_flag = gf_bs_read_int_log(bs, 1, "broken_link_flag"); |
5899 | 644 | rp->changing_slice_group_idc = gf_bs_read_int_log(bs, 2, "changing_slice_group_idc"); |
5900 | 644 | rp->valid = 1; |
5901 | | |
5902 | 644 | return 0; |
5903 | 644 | } |
5904 | | |
5905 | | /*for interpretation see ISO 14496-10 N.11084, table D-1*/ |
5906 | | static s32 avc_parse_pic_timing_sei(GF_BitStream *bs, AVCState *avc) |
5907 | 4.83k | { |
5908 | 4.83k | int sps_id = avc->sps_active_idx; |
5909 | 4.83k | const char NumClockTS[] = { 1, 1, 1, 2, 2, 3, 3, 2, 3 }; |
5910 | 4.83k | AVCSeiPicTiming *pt = &avc->sei.pic_timing; |
5911 | | |
5912 | 4.83k | if (sps_id < 0) { |
5913 | | /*sps_active_idx equals -1 when no sps has been detected. In this case SEI should not be decoded.*/ |
5914 | 0 | gf_assert(0); |
5915 | 0 | return 1; |
5916 | 0 | } |
5917 | 4.83k | if (avc->sps[sps_id].vui.nal_hrd_parameters_present_flag || avc->sps[sps_id].vui.vcl_hrd_parameters_present_flag) { /*CpbDpbDelaysPresentFlag, see 14496-10(2003) E.11*/ |
5918 | 1.76k | gf_bs_read_int_log(bs, 1 + avc->sps[sps_id].vui.hrd.cpb_removal_delay_length_minus1, "cpb_removal_delay_minus1"); |
5919 | 1.76k | gf_bs_read_int_log(bs, 1 + avc->sps[sps_id].vui.hrd.dpb_output_delay_length_minus1, "dpb_output_delay_minus1"); |
5920 | 1.76k | } |
5921 | | |
5922 | | /*ISO 14496-10 (2003), D.8.2: we need to get pic_struct in order to know if we display top field first or bottom field first*/ |
5923 | 4.83k | if (avc->sps[sps_id].vui.pic_struct_present_flag) { |
5924 | 2.54k | int i; |
5925 | 2.54k | pt->pic_struct = gf_bs_read_int_log(bs, 4, "pic_struct"); |
5926 | 2.54k | if (pt->pic_struct > 8) { |
5927 | 374 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[avc-h264] invalid pic_struct value %d\n", pt->pic_struct)); |
5928 | 374 | return 1; |
5929 | 374 | } |
5930 | | |
5931 | 6.90k | for (i = 0; i < NumClockTS[pt->pic_struct]; i++) { |
5932 | 4.73k | if (gf_bs_read_int_log_idx(bs, 1, "clock_timestamp_flag", i)) { |
5933 | 3.74k | Bool full_timestamp_flag; |
5934 | 3.74k | gf_bs_read_int_log_idx(bs, 2, "ct_type", i); |
5935 | 3.74k | gf_bs_read_int_log_idx(bs, 1, "nuit_field_based_flag", i); |
5936 | 3.74k | gf_bs_read_int_log_idx(bs, 5, "counting_type", i); |
5937 | 3.74k | full_timestamp_flag = gf_bs_read_int_log_idx(bs, 1, "full_timestamp_flag", i); |
5938 | 3.74k | gf_bs_read_int_log_idx(bs, 1, "discontinuity_flag", i); |
5939 | 3.74k | gf_bs_read_int_log_idx(bs, 1, "cnt_dropped_flag", i); |
5940 | 3.74k | gf_bs_read_int_log_idx(bs, 8, "n_frames", i); |
5941 | 3.74k | if (full_timestamp_flag) { |
5942 | 1.07k | gf_bs_read_int_log_idx(bs, 6, "seconds_value", i); |
5943 | 1.07k | gf_bs_read_int_log_idx(bs, 6, "minutes_value", i); |
5944 | 1.07k | gf_bs_read_int_log_idx(bs, 5, "hours_value", i); |
5945 | 1.07k | } |
5946 | 2.67k | else { |
5947 | 2.67k | if (gf_bs_read_int_log_idx(bs, 1, "seconds_flag", i)) { |
5948 | 1.62k | gf_bs_read_int_log_idx(bs, 6, "seconds_value", i); |
5949 | 1.62k | if (gf_bs_read_int_log_idx(bs, 1, "minutes_flag", i)) { |
5950 | 1.46k | gf_bs_read_int_log_idx(bs, 6, "minutes_value", i); |
5951 | 1.46k | if (gf_bs_read_int_log_idx(bs, 1, "hours_flag", i)) { |
5952 | 900 | gf_bs_read_int_log_idx(bs, 5, "hours_value", i); |
5953 | 900 | } |
5954 | 1.46k | } |
5955 | 1.62k | } |
5956 | 2.67k | if (avc->sps[sps_id].vui.hrd.time_offset_length > 0) |
5957 | 463 | gf_bs_read_int_log_idx(bs, avc->sps[sps_id].vui.hrd.time_offset_length, "time_offset", i); |
5958 | 2.67k | } |
5959 | 3.74k | } |
5960 | 4.73k | } |
5961 | 2.16k | } |
5962 | | |
5963 | 4.46k | return 0; |
5964 | 4.83k | } |
5965 | | |
5966 | | |
5967 | | static void avc_parse_itu_t_t35_sei(GF_BitStream* bs, AVCSeiItuTT35DolbyVision *dovi) |
5968 | 170 | { |
5969 | 170 | u8 itu_t_t35_country_code = gf_bs_read_u8(bs); |
5970 | 170 | u16 terminal_provider_code = gf_bs_read_u16(bs); |
5971 | 170 | u32 user_id = gf_bs_read_u32(bs); |
5972 | 170 | u8 data_type_code = gf_bs_read_u8(bs); |
5973 | 170 | if (itu_t_t35_country_code == 0xB5 && terminal_provider_code == 0x31 && user_id == 0x47413934 && (data_type_code == 0x8 || data_type_code == 0x9)) { |
5974 | 0 | dovi->rpu_flag = GF_TRUE; |
5975 | 0 | } |
5976 | 170 | } |
5977 | | |
5978 | | static void avc_compute_poc(AVCSliceInfo *si) |
5979 | 173k | { |
5980 | 173k | enum { |
5981 | 173k | AVC_PIC_FRAME, |
5982 | 173k | AVC_PIC_FIELD_TOP, |
5983 | 173k | AVC_PIC_FIELD_BOTTOM, |
5984 | 173k | } pic_type; |
5985 | 173k | s32 field_poc[2] = { 0,0 }; |
5986 | 173k | s32 max_frame_num; |
5987 | | |
5988 | 173k | if (!si->sps) return; |
5989 | | |
5990 | 168k | max_frame_num = 1 << (si->sps->log2_max_frame_num); |
5991 | | |
5992 | | /* picture type */ |
5993 | 168k | if (si->sps->frame_mbs_only_flag || !si->field_pic_flag) pic_type = AVC_PIC_FRAME; |
5994 | 20.9k | else if (si->bottom_field_flag) pic_type = AVC_PIC_FIELD_BOTTOM; |
5995 | 5.16k | else pic_type = AVC_PIC_FIELD_TOP; |
5996 | | |
5997 | | /* frame_num_offset */ |
5998 | 168k | if (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE) { |
5999 | 86.8k | si->poc_lsb_prev = 0; |
6000 | 86.8k | si->poc_msb_prev = 0; |
6001 | 86.8k | si->frame_num_offset = 0; |
6002 | 86.8k | si->frame_num_offset_prev = 0; |
6003 | 86.8k | si->frame_num_prev = 0; |
6004 | 86.8k | } |
6005 | 81.2k | else { |
6006 | 81.2k | if (si->frame_num < si->frame_num_prev) |
6007 | 9.13k | si->frame_num_offset = si->frame_num_offset_prev + max_frame_num; |
6008 | 72.1k | else |
6009 | 72.1k | si->frame_num_offset = si->frame_num_offset_prev; |
6010 | 81.2k | } |
6011 | | |
6012 | | /*ISO 14496-10 N.11084 8.2.1.1*/ |
6013 | 168k | if (si->sps->poc_type == 0) |
6014 | 113k | { |
6015 | 113k | const u32 max_poc_lsb = 1 << (si->sps->log2_max_poc_lsb); |
6016 | | |
6017 | | /*ISO 14496-10 N.11084 eq (8-3)*/ |
6018 | 113k | if ((si->poc_lsb < si->poc_lsb_prev) && |
6019 | 113k | (si->poc_lsb_prev - si->poc_lsb >= max_poc_lsb / 2)) |
6020 | 4.62k | si->poc_msb = si->poc_msb_prev + max_poc_lsb; |
6021 | 109k | else if ((si->poc_lsb > si->poc_lsb_prev) && |
6022 | 109k | (si->poc_lsb - si->poc_lsb_prev > max_poc_lsb / 2)) |
6023 | 15.8k | si->poc_msb = si->poc_msb_prev - max_poc_lsb; |
6024 | 93.2k | else |
6025 | 93.2k | si->poc_msb = si->poc_msb_prev; |
6026 | | |
6027 | | /*ISO 14496-10 N.11084 eq (8-4)*/ |
6028 | 113k | if (pic_type != AVC_PIC_FIELD_BOTTOM) |
6029 | 106k | field_poc[0] = si->poc_msb + si->poc_lsb; |
6030 | | |
6031 | | /*ISO 14496-10 N.11084 eq (8-5)*/ |
6032 | 113k | if (pic_type != AVC_PIC_FIELD_TOP) { |
6033 | 111k | if (!si->field_pic_flag) |
6034 | 104k | field_poc[1] = field_poc[0] + si->delta_poc_bottom; |
6035 | 6.91k | else |
6036 | 6.91k | field_poc[1] = si->poc_msb + si->poc_lsb; |
6037 | 111k | } |
6038 | 113k | } |
6039 | | /*ISO 14496-10 N.11084 8.2.1.2*/ |
6040 | 54.3k | else if (si->sps->poc_type == 1) |
6041 | 44.2k | { |
6042 | 44.2k | u32 i; |
6043 | 44.2k | s32 abs_frame_num, expected_delta_per_poc_cycle, expected_poc; |
6044 | | |
6045 | 44.2k | if (si->sps->poc_cycle_length) |
6046 | 41.6k | abs_frame_num = si->frame_num_offset + si->frame_num; |
6047 | 2.56k | else |
6048 | 2.56k | abs_frame_num = 0; |
6049 | | |
6050 | 44.2k | if (!si->nal_ref_idc && (abs_frame_num > 0)) abs_frame_num--; |
6051 | | |
6052 | 44.2k | expected_delta_per_poc_cycle = 0; |
6053 | 145k | for (i = 0; i < si->sps->poc_cycle_length; i++) |
6054 | 101k | expected_delta_per_poc_cycle += si->sps->offset_for_ref_frame[i]; |
6055 | | |
6056 | 44.2k | if (abs_frame_num > 0) { |
6057 | 32.9k | const u32 poc_cycle_cnt = (abs_frame_num - 1) / si->sps->poc_cycle_length; |
6058 | 32.9k | const u32 frame_num_in_poc_cycle = (abs_frame_num - 1) % si->sps->poc_cycle_length; |
6059 | | |
6060 | 32.9k | expected_poc = poc_cycle_cnt * expected_delta_per_poc_cycle; |
6061 | 95.7k | for (i = 0; i <= frame_num_in_poc_cycle; i++) |
6062 | 62.8k | expected_poc += si->sps->offset_for_ref_frame[i]; |
6063 | 32.9k | } |
6064 | 11.2k | else { |
6065 | 11.2k | expected_poc = 0; |
6066 | 11.2k | } |
6067 | | |
6068 | 44.2k | if (!si->nal_ref_idc) expected_poc += si->sps->offset_for_non_ref_pic; |
6069 | | |
6070 | 44.2k | field_poc[0] = expected_poc + si->delta_poc[0]; |
6071 | 44.2k | field_poc[1] = field_poc[0] + si->sps->offset_for_top_to_bottom_field; |
6072 | 44.2k | if (pic_type == AVC_PIC_FRAME) field_poc[1] += si->delta_poc[1]; |
6073 | 44.2k | } |
6074 | | /*ISO 14496-10 N.11084 8.2.1.3*/ |
6075 | 10.1k | else if (si->sps->poc_type == 2) |
6076 | 10.1k | { |
6077 | 10.1k | int poc; |
6078 | 10.1k | if (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE) { |
6079 | 4.53k | poc = 0; |
6080 | 4.53k | } |
6081 | 5.60k | else { |
6082 | 5.60k | const int abs_frame_num = si->frame_num_offset + si->frame_num; |
6083 | 5.60k | poc = 2 * abs_frame_num; |
6084 | 5.60k | if (!si->nal_ref_idc) poc -= 1; |
6085 | 5.60k | } |
6086 | 10.1k | field_poc[0] = poc; |
6087 | 10.1k | field_poc[1] = poc; |
6088 | 10.1k | } |
6089 | | |
6090 | | /*ISO 14496-10 N.11084 eq (8-1)*/ |
6091 | 168k | if (pic_type == AVC_PIC_FRAME) |
6092 | 147k | si->poc = MIN(field_poc[0], field_poc[1]); |
6093 | 20.9k | else if (pic_type == AVC_PIC_FIELD_TOP) |
6094 | 5.16k | si->poc = field_poc[0]; |
6095 | 15.7k | else |
6096 | 15.7k | si->poc = field_poc[1]; |
6097 | 168k | } |
6098 | | |
6099 | | GF_EXPORT |
6100 | | s32 gf_avc_parse_nalu(GF_BitStream *bs, AVCState *avc) |
6101 | 831k | { |
6102 | 831k | u8 idr_flag; |
6103 | 831k | s32 slice, ret; |
6104 | 831k | u32 nal_hdr; |
6105 | 831k | AVCSliceInfo n_state; |
6106 | | |
6107 | 831k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
6108 | 831k | if (!gf_bs_available(bs)) return -1; |
6109 | 831k | gf_bs_mark_overflow(bs, GF_TRUE); |
6110 | | |
6111 | 831k | nal_hdr = gf_bs_read_u8(bs); |
6112 | | |
6113 | 831k | slice = 0; |
6114 | 831k | memcpy(&n_state, &avc->s_info, sizeof(AVCSliceInfo)); |
6115 | 831k | avc->last_nal_type_parsed = n_state.nal_unit_type = nal_hdr & 0x1F; |
6116 | 831k | n_state.nal_ref_idc = (nal_hdr >> 5) & 0x3; |
6117 | | |
6118 | 831k | idr_flag = 0; |
6119 | | |
6120 | 831k | switch (n_state.nal_unit_type) { |
6121 | 8.98k | case GF_AVC_NALU_ACCESS_UNIT: |
6122 | 10.1k | case GF_AVC_NALU_END_OF_SEQ: |
6123 | 10.6k | case GF_AVC_NALU_END_OF_STREAM: |
6124 | 10.6k | ret = 1; |
6125 | 10.6k | break; |
6126 | | |
6127 | 64.1k | case GF_AVC_NALU_SVC_SLICE: |
6128 | 64.1k | SVC_ReadNal_header_extension(bs, &n_state.svc_nalhdr); |
6129 | 64.1k | if (gf_bs_is_overflow(bs)) return -1; |
6130 | | |
6131 | | // slice buffer - read the info and compare. |
6132 | 60.5k | /*ret = */svc_parse_slice(bs, avc, &n_state); |
6133 | 60.5k | if (avc->s_info.nal_ref_idc) { |
6134 | 39.7k | n_state.poc_lsb_prev = avc->s_info.poc_lsb; |
6135 | 39.7k | n_state.poc_msb_prev = avc->s_info.poc_msb; |
6136 | 39.7k | } |
6137 | 60.5k | avc_compute_poc(&n_state); |
6138 | | |
6139 | 60.5k | if (avc->s_info.poc != n_state.poc) { |
6140 | 16.1k | memcpy(&avc->s_info, &n_state, sizeof(AVCSliceInfo)); |
6141 | 16.1k | return 1; |
6142 | 16.1k | } |
6143 | 44.3k | memcpy(&avc->s_info, &n_state, sizeof(AVCSliceInfo)); |
6144 | 44.3k | return 0; |
6145 | | |
6146 | 3.41k | case GF_AVC_NALU_SVC_PREFIX_NALU: |
6147 | 3.41k | SVC_ReadNal_header_extension(bs, &avc->s_info.svc_nalhdr); |
6148 | 3.41k | if (gf_bs_is_overflow(bs)) return -1; |
6149 | 3.03k | return 0; |
6150 | | |
6151 | 139k | case GF_AVC_NALU_IDR_SLICE: |
6152 | 335k | case GF_AVC_NALU_NON_IDR_SLICE: |
6153 | 343k | case GF_AVC_NALU_DP_A_SLICE: |
6154 | 352k | case GF_AVC_NALU_DP_B_SLICE: |
6155 | 368k | case GF_AVC_NALU_DP_C_SLICE: |
6156 | 368k | slice = 1; |
6157 | | /* slice buffer - read the info and compare.*/ |
6158 | 368k | ret = avc_parse_slice(bs, avc, idr_flag, &n_state); |
6159 | 368k | if (gf_bs_is_overflow(bs)) ret = -1; |
6160 | 368k | if (ret < 0) return ret; |
6161 | 113k | ret = 0; |
6162 | 113k | if ( |
6163 | 113k | ((avc->s_info.nal_unit_type > GF_AVC_NALU_IDR_SLICE) || (avc->s_info.nal_unit_type < GF_AVC_NALU_NON_IDR_SLICE)) |
6164 | 113k | && (avc->s_info.nal_unit_type != GF_AVC_NALU_SVC_SLICE) |
6165 | 113k | ) { |
6166 | 46.6k | break; |
6167 | 46.6k | } |
6168 | 66.9k | if (avc->s_info.frame_num != n_state.frame_num) { |
6169 | 28.4k | ret = 1; |
6170 | 28.4k | break; |
6171 | 28.4k | } |
6172 | | |
6173 | 38.4k | if (avc->s_info.field_pic_flag != n_state.field_pic_flag) { |
6174 | 1.19k | ret = 1; |
6175 | 1.19k | break; |
6176 | 1.19k | } |
6177 | 37.2k | if ((avc->s_info.nal_ref_idc != n_state.nal_ref_idc) && |
6178 | 37.2k | (!avc->s_info.nal_ref_idc || !n_state.nal_ref_idc)) { |
6179 | 1.30k | ret = 1; |
6180 | 1.30k | break; |
6181 | 1.30k | } |
6182 | 35.9k | if (!avc->s_info.sps) |
6183 | 573 | return -1; |
6184 | | |
6185 | 35.3k | if (avc->s_info.sps->poc_type == n_state.sps->poc_type) { |
6186 | 33.2k | if (!avc->s_info.sps->poc_type) { |
6187 | 15.8k | if (!n_state.bottom_field_flag && (avc->s_info.poc_lsb != n_state.poc_lsb)) { |
6188 | 3.87k | ret = 1; |
6189 | 3.87k | break; |
6190 | 3.87k | } |
6191 | 11.9k | if (avc->s_info.delta_poc_bottom != n_state.delta_poc_bottom) { |
6192 | 549 | ret = 1; |
6193 | 549 | break; |
6194 | 549 | } |
6195 | 11.9k | } |
6196 | 17.4k | else if (avc->s_info.sps->poc_type == 1) { |
6197 | 12.1k | if (avc->s_info.delta_poc[0] != n_state.delta_poc[0]) { |
6198 | 810 | ret = 1; |
6199 | 810 | break; |
6200 | 810 | } |
6201 | 11.3k | if (avc->s_info.delta_poc[1] != n_state.delta_poc[1]) { |
6202 | 509 | ret = 1; |
6203 | 509 | break; |
6204 | 509 | } |
6205 | 11.3k | } |
6206 | 33.2k | } |
6207 | | |
6208 | 29.6k | if (n_state.nal_unit_type == GF_AVC_NALU_IDR_SLICE) { |
6209 | 21.5k | if (avc->s_info.nal_unit_type != GF_AVC_NALU_IDR_SLICE) { /*IdrPicFlag differs in value*/ |
6210 | 5.56k | ret = 1; |
6211 | 5.56k | break; |
6212 | 5.56k | } |
6213 | 15.9k | else if (avc->s_info.idr_pic_id != n_state.idr_pic_id) { /*both IDR and idr_pic_id differs*/ |
6214 | 125 | ret = 1; |
6215 | 125 | break; |
6216 | 125 | } |
6217 | 21.5k | } |
6218 | 23.9k | break; |
6219 | 107k | case GF_AVC_NALU_SEQ_PARAM: |
6220 | 107k | avc->last_ps_idx = gf_avc_read_sps_bs_internal(bs, avc, 0, NULL, nal_hdr); |
6221 | 107k | if (gf_bs_is_overflow(bs)) return -1; |
6222 | 101k | if (avc->last_ps_idx < 0) return -1; |
6223 | 62.4k | avc->last_sps_idx = avc->last_ps_idx; |
6224 | 62.4k | return 0; |
6225 | | |
6226 | 82.7k | case GF_AVC_NALU_PIC_PARAM: |
6227 | 82.7k | avc->last_ps_idx = gf_avc_read_pps_bs_internal(bs, avc, nal_hdr); |
6228 | 82.7k | if (gf_bs_is_overflow(bs)) return -1; |
6229 | 70.5k | if (avc->last_ps_idx < 0) return -1; |
6230 | 56.3k | return 0; |
6231 | 31.9k | case GF_AVC_NALU_SVC_SUBSEQ_PARAM: |
6232 | 31.9k | avc->last_ps_idx = gf_avc_read_sps_bs_internal(bs, avc, 1, NULL, nal_hdr); |
6233 | 31.9k | if (gf_bs_is_overflow(bs)) return -1; |
6234 | 25.8k | if (avc->last_ps_idx < 0) return -1; |
6235 | 20.6k | return 0; |
6236 | 6.84k | case GF_AVC_NALU_SEQ_PARAM_EXT: |
6237 | 6.84k | avc->last_ps_idx = (s32) gf_bs_read_ue(bs); |
6238 | 6.84k | if (gf_bs_is_overflow(bs)) return -1; |
6239 | 6.84k | if (avc->last_ps_idx < 0) return -1; |
6240 | 6.62k | return 0; |
6241 | | |
6242 | 20.0k | case GF_AVC_NALU_SEI: |
6243 | 22.7k | case GF_AVC_NALU_FILLER_DATA: |
6244 | 22.7k | return 0; |
6245 | | |
6246 | 133k | default: |
6247 | 133k | if (avc->s_info.nal_unit_type <= GF_AVC_NALU_IDR_SLICE) ret = 1; |
6248 | | //To detect change of AU when multiple sps and pps in stream |
6249 | 46.7k | else if ((nal_hdr & 0x1F) == GF_AVC_NALU_SEI && avc->s_info.nal_unit_type == GF_AVC_NALU_SVC_SLICE) |
6250 | 0 | ret = 1; |
6251 | 46.7k | else if ((nal_hdr & 0x1F) == GF_AVC_NALU_SEQ_PARAM && avc->s_info.nal_unit_type == GF_AVC_NALU_SVC_SLICE) |
6252 | 0 | ret = 1; |
6253 | 46.7k | else |
6254 | 46.7k | ret = 0; |
6255 | 133k | break; |
6256 | 831k | } |
6257 | | |
6258 | | /* save _prev values */ |
6259 | 256k | if (ret && avc->s_info.sps) { |
6260 | 136k | n_state.frame_num_offset_prev = avc->s_info.frame_num_offset; |
6261 | 136k | if ((avc->s_info.sps->poc_type != 2) || (avc->s_info.nal_ref_idc != 0)) |
6262 | 134k | n_state.frame_num_prev = avc->s_info.frame_num; |
6263 | 136k | if (avc->s_info.nal_ref_idc) { |
6264 | 76.8k | n_state.poc_lsb_prev = avc->s_info.poc_lsb; |
6265 | 76.8k | n_state.poc_msb_prev = avc->s_info.poc_msb; |
6266 | 76.8k | } |
6267 | 136k | } |
6268 | 256k | if (slice) |
6269 | 112k | avc_compute_poc(&n_state); |
6270 | 256k | memcpy(&avc->s_info, &n_state, sizeof(AVCSliceInfo)); |
6271 | 256k | return ret; |
6272 | 831k | } |
6273 | | |
6274 | | |
6275 | | u32 gf_avc_reformat_sei(u8 *buffer, u32 nal_size, Bool isobmf_rewrite, AVCState *avc) |
6276 | 17.5k | { |
6277 | 17.5k | u32 ptype, psize, hdr, var; |
6278 | 17.5k | u32 start; |
6279 | 17.5k | GF_BitStream *bs; |
6280 | 17.5k | GF_BitStream *bs_dest = NULL; |
6281 | 17.5k | u8 nhdr; |
6282 | 17.5k | Bool sei_removed = GF_FALSE; |
6283 | 17.5k | char store; |
6284 | | |
6285 | 17.5k | hdr = buffer[0]; |
6286 | 17.5k | if ((hdr & 0x1F) != GF_AVC_NALU_SEI) return 0; |
6287 | | |
6288 | 17.5k | if (isobmf_rewrite) bs_dest = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
6289 | | |
6290 | 17.5k | bs = gf_bs_new(buffer, nal_size, GF_BITSTREAM_READ); |
6291 | 17.5k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
6292 | | |
6293 | 17.5k | nhdr = gf_bs_read_int(bs, 8); |
6294 | 17.5k | if (bs_dest) gf_bs_write_int(bs_dest, nhdr, 8); |
6295 | | |
6296 | | /*parse SEI*/ |
6297 | 233k | while (gf_bs_available(bs)) { |
6298 | 233k | Bool do_copy; |
6299 | 233k | ptype = 0; |
6300 | 298k | while (1) { |
6301 | 298k | u8 v = gf_bs_read_int(bs, 8); |
6302 | 298k | ptype += v; |
6303 | 298k | if (v != 0xFF) break; |
6304 | 298k | } |
6305 | | |
6306 | 233k | psize = 0; |
6307 | 252k | while (1) { |
6308 | 252k | u8 v = gf_bs_read_int(bs, 8); |
6309 | 252k | psize += v; |
6310 | 252k | if (v != 0xFF) break; |
6311 | 252k | } |
6312 | | |
6313 | 233k | start = (u32)gf_bs_get_position(bs); |
6314 | | |
6315 | 233k | do_copy = 1; |
6316 | | |
6317 | 233k | if (start + psize >= nal_size) { |
6318 | 15.0k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] SEI user message type %d size error (%d but %d remain), keeping full SEI untouched\n", ptype, psize, nal_size - start)); |
6319 | 15.0k | if (bs_dest) gf_bs_del(bs_dest); |
6320 | 15.0k | gf_bs_del(bs); |
6321 | 15.0k | return nal_size; |
6322 | 15.0k | } |
6323 | 218k | switch (ptype) { |
6324 | | /*remove SEI messages forbidden in MP4*/ |
6325 | 358 | case 3: /*filler data*/ |
6326 | 13.5k | case 10: /*sub_seq info*/ |
6327 | 15.7k | case 11: /*sub_seq_layer char*/ |
6328 | 19.7k | case 12: /*sub_seq char*/ |
6329 | 19.7k | do_copy = 0; |
6330 | 19.7k | sei_removed = GF_TRUE; |
6331 | 19.7k | break; |
6332 | 1.05k | case 5: /*user unregistered */ |
6333 | 1.05k | store = buffer[start + psize]; |
6334 | 1.05k | buffer[start + psize] = 0; |
6335 | 1.05k | GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[avc-h264] SEI user message %s\n", buffer + start + 16)); |
6336 | 1.05k | buffer[start + psize] = store; |
6337 | 1.05k | break; |
6338 | | |
6339 | 644 | case 6: /*recovery point*/ |
6340 | 644 | avc_parse_recovery_point_sei(bs, avc); |
6341 | 644 | break; |
6342 | | |
6343 | 4.83k | case 1: /*pic_timing*/ |
6344 | 4.83k | avc_parse_pic_timing_sei(bs, avc); |
6345 | 4.83k | break; |
6346 | | |
6347 | 136k | case 0: /*buffering period*/ |
6348 | 143k | case 2: /*pan scan rect*/ |
6349 | 146k | case 4: /*user registered ITU t35*/ |
6350 | 146k | case 7: /*def_rec_pic_marking_repetition*/ |
6351 | 146k | case 8: /*spare_pic*/ |
6352 | 146k | case 9: /*scene info*/ |
6353 | 146k | case 13: /*full frame freeze*/ |
6354 | 148k | case 14: /*full frame freeze release*/ |
6355 | 148k | case 15: /*full frame snapshot*/ |
6356 | 152k | case 16: /*progressive refinement segment start*/ |
6357 | 152k | case 17: /*progressive refinement segment end*/ |
6358 | 153k | case 18: /*motion constrained slice group*/ |
6359 | 191k | default: /*add all unknown SEIs*/ |
6360 | 191k | break; |
6361 | 218k | } |
6362 | | |
6363 | 218k | if (do_copy && bs_dest) { |
6364 | 198k | var = ptype; |
6365 | 256k | while (var >= 255) { |
6366 | 58.5k | gf_bs_write_int(bs_dest, 0xFF, 8); |
6367 | 58.5k | var -= 255; |
6368 | 58.5k | } |
6369 | 198k | gf_bs_write_int(bs_dest, var, 8); |
6370 | | |
6371 | 198k | var = psize; |
6372 | 200k | while (var >= 255) { |
6373 | 1.90k | gf_bs_write_int(bs_dest, 0xFF, 8); |
6374 | 1.90k | var -= 255; |
6375 | 1.90k | } |
6376 | 198k | gf_bs_write_int(bs_dest, var, 8); |
6377 | 198k | gf_bs_seek(bs, start); |
6378 | | |
6379 | | //bs_read_data does not skip EPB, read byte per byte |
6380 | 198k | var = psize; |
6381 | 3.84M | while (var) { |
6382 | 3.65M | gf_bs_write_u8(bs_dest, gf_bs_read_u8(bs)); |
6383 | 3.65M | var--; |
6384 | 3.65M | } |
6385 | 198k | } |
6386 | 19.7k | else { |
6387 | 19.7k | gf_bs_seek(bs, start); |
6388 | | |
6389 | | //bs_skip_bytes does not skip EPB, skip byte per byte |
6390 | 264k | while (psize) { |
6391 | 244k | gf_bs_read_u8(bs); |
6392 | 244k | psize--; |
6393 | 244k | } |
6394 | 19.7k | } |
6395 | | |
6396 | 218k | if (gf_bs_available(bs) <= 2) { |
6397 | 2.44k | var = gf_bs_read_int(bs, 8); |
6398 | 2.44k | if (var != 0x80) { |
6399 | 82 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] SEI user message has less than 2 bytes remaining but no end of sei found, keeping full SEI untouched\n")); |
6400 | 82 | if (bs_dest) gf_bs_del(bs_dest); |
6401 | 82 | gf_bs_del(bs); |
6402 | 82 | return nal_size; |
6403 | 82 | } |
6404 | 2.36k | if (bs_dest) gf_bs_write_int(bs_dest, 0x80, 8); |
6405 | 2.36k | break; |
6406 | 2.44k | } |
6407 | 218k | } |
6408 | 2.36k | gf_bs_del(bs); |
6409 | | //we cannot compare final size and original size since original may have EPB and final does not yet have them |
6410 | 2.36k | if (bs_dest && sei_removed) { |
6411 | 2.27k | u8 *dst_no_epb = NULL; |
6412 | 2.27k | u32 dst_no_epb_size = 0; |
6413 | 2.27k | gf_bs_get_content(bs_dest, &dst_no_epb, &dst_no_epb_size); |
6414 | 2.27k | if (dst_no_epb) { |
6415 | 2.27k | u32 nb_bytes_add = gf_media_nalu_emulation_bytes_add_count(dst_no_epb, dst_no_epb_size); |
6416 | | //if result fits into source buffer, reformat |
6417 | | //otherwise ignore and return source (happens in some fuzzing cases, cf issue 1903) |
6418 | 2.27k | if (dst_no_epb_size + nb_bytes_add <= nal_size) |
6419 | 1.10k | nal_size = gf_media_nalu_add_emulation_bytes(dst_no_epb, buffer, dst_no_epb_size); |
6420 | | |
6421 | 2.27k | gf_free(dst_no_epb); |
6422 | 2.27k | } |
6423 | 2.27k | } |
6424 | 2.36k | if (bs_dest) gf_bs_del(bs_dest); |
6425 | 2.36k | return nal_size; |
6426 | 17.5k | } |
6427 | | |
6428 | | static u8 avc_hevc_get_sar_idx(u32 w, u32 h) |
6429 | 0 | { |
6430 | 0 | u32 i, count = GF_ARRAY_LENGTH(avc_hevc_sar); |
6431 | 0 | for (i = 0; i < count; i++) { |
6432 | 0 | if ((avc_hevc_sar[i].w == w) && (avc_hevc_sar[i].h == h)) |
6433 | 0 | return i; |
6434 | 0 | } |
6435 | 0 | return 0xFF; |
6436 | 0 | } |
6437 | | |
6438 | | static void sub_layer_hrd_parameters(GF_BitStream *bs, int subLayerId, u32 cpb_cnt, Bool sub_pic_hrd_params_present_flag, u32 idx1, u32 idx2) |
6439 | 7.95k | { |
6440 | 7.95k | u32 i; |
6441 | 7.95k | if (!gf_bs_available(bs)) return; |
6442 | | |
6443 | 80.5k | for (i = 0; i <= cpb_cnt; i++) { |
6444 | 72.5k | gf_bs_read_ue_log_idx3(bs, "bit_rate_value_minus1", idx1, idx2, i); |
6445 | 72.5k | gf_bs_read_ue_log_idx3(bs, "cpb_size_value_minus1", idx1, idx2, i); |
6446 | 72.5k | if (sub_pic_hrd_params_present_flag) { |
6447 | 13.7k | gf_bs_read_ue_log_idx3(bs, "cpb_size_du_value_minus1", idx1, idx2, i); |
6448 | 13.7k | gf_bs_read_ue_log_idx3(bs, "bit_rate_du_value_minus1", idx1, idx2, i); |
6449 | 13.7k | } |
6450 | 72.5k | gf_bs_read_int_log_idx3(bs, 1, "cbr_flag", idx1, idx2, i); |
6451 | 72.5k | } |
6452 | 7.90k | } |
6453 | | |
6454 | | static void hevc_parse_hrd_parameters(GF_BitStream *bs, Bool commonInfPresentFlag, int maxNumSubLayersMinus1, u32 idx) |
6455 | 35.0k | { |
6456 | 35.0k | int i; |
6457 | 35.0k | Bool nal_hrd_parameters_present_flag = GF_FALSE; |
6458 | 35.0k | Bool vcl_hrd_parameters_present_flag = GF_FALSE; |
6459 | 35.0k | Bool sub_pic_hrd_params_present_flag = GF_FALSE; |
6460 | | |
6461 | 35.0k | if (commonInfPresentFlag) { |
6462 | 1.61k | nal_hrd_parameters_present_flag = gf_bs_read_int_log_idx(bs, 1, "nal_hrd_parameters_present_flag", idx); |
6463 | 1.61k | vcl_hrd_parameters_present_flag = gf_bs_read_int_log_idx(bs, 1, "vcl_hrd_parameters_present_flag", idx); |
6464 | 1.61k | if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) { |
6465 | 1.48k | sub_pic_hrd_params_present_flag = gf_bs_read_int_log_idx(bs, 1, "sub_pic_hrd_params_present_flag", idx); |
6466 | 1.48k | if (sub_pic_hrd_params_present_flag) { |
6467 | 41 | gf_bs_read_int_log_idx(bs, 8, "tick_divisor_minus2", idx); |
6468 | 41 | gf_bs_read_int_log_idx(bs, 5, "du_cpb_removal_delay_increment_length_minus1", idx); |
6469 | 41 | gf_bs_read_int_log_idx(bs, 1, "sub_pic_cpb_params_in_pic_timing_sei_flag", idx); |
6470 | 41 | gf_bs_read_int_log_idx(bs, 5, "dpb_output_delay_du_length_minus1", idx); |
6471 | 41 | } |
6472 | 1.48k | gf_bs_read_int_log_idx(bs, 4, "bit_rate_scale", idx); |
6473 | 1.48k | gf_bs_read_int_log_idx(bs, 4, "cpb_size_scale", idx); |
6474 | 1.48k | if (sub_pic_hrd_params_present_flag) { |
6475 | 41 | gf_bs_read_int_log_idx(bs, 4, "cpb_size_du_scale", idx); |
6476 | 41 | } |
6477 | 1.48k | gf_bs_read_int_log_idx(bs, 5, "initial_cpb_removal_delay_length_minus1", idx); |
6478 | 1.48k | gf_bs_read_int_log_idx(bs, 5, "au_cpb_removal_delay_length_minus1", idx); |
6479 | 1.48k | gf_bs_read_int_log_idx(bs, 5, "dpb_output_delay_length_minus1", idx); |
6480 | 1.48k | } |
6481 | 1.61k | } |
6482 | 103k | for (i = 0; i <= maxNumSubLayersMinus1; i++) { |
6483 | 68.6k | Bool fixed_pic_rate_general_flag_i = gf_bs_read_int_log_idx(bs, 1, "fixed_pic_rate_general_flag", idx); |
6484 | 68.6k | Bool fixed_pic_rate_within_cvs_flag_i = GF_TRUE; |
6485 | 68.6k | Bool low_delay_hrd_flag_i = GF_FALSE; |
6486 | 68.6k | u32 cpb_cnt_minus1_i = 0; |
6487 | 68.6k | if (!fixed_pic_rate_general_flag_i) { |
6488 | 63.7k | fixed_pic_rate_within_cvs_flag_i = gf_bs_read_int_log_idx(bs, 1, "fixed_pic_rate_within_cvs_flag", idx); |
6489 | 63.7k | } |
6490 | 68.6k | if (fixed_pic_rate_within_cvs_flag_i) |
6491 | 6.26k | gf_bs_read_ue_log_idx(bs, "elemental_duration_in_tc_minus1", idx); |
6492 | 62.4k | else |
6493 | 62.4k | low_delay_hrd_flag_i = gf_bs_read_int_log_idx(bs, 1, "low_delay_hrd_flag", idx); |
6494 | 68.6k | if (!low_delay_hrd_flag_i) { |
6495 | 66.2k | cpb_cnt_minus1_i = gf_bs_read_ue_log_idx(bs, "cpb_cnt_minus1", idx); |
6496 | 66.2k | } |
6497 | 68.6k | if (nal_hrd_parameters_present_flag) { |
6498 | 1.55k | sub_layer_hrd_parameters(bs, i, cpb_cnt_minus1_i, sub_pic_hrd_params_present_flag, idx, i); |
6499 | 1.55k | } |
6500 | 68.6k | if (vcl_hrd_parameters_present_flag) { |
6501 | 6.39k | sub_layer_hrd_parameters(bs, i, cpb_cnt_minus1_i, sub_pic_hrd_params_present_flag, idx, i); |
6502 | 6.39k | } |
6503 | 68.6k | } |
6504 | 35.0k | } |
6505 | | |
6506 | | static void avc_hevc_vvc_rewrite_vui(GF_VUIInfo *vui_info, GF_BitStream *orig, GF_BitStream *mod, GF_CodecID codec) |
6507 | 0 | { |
6508 | | /* VUI present flag*/ |
6509 | 0 | Bool vui_present_flag = gf_bs_read_int(orig, 1); |
6510 | | |
6511 | | /*setup default values*/ |
6512 | 0 | Bool aspect_ratio_info_present_flag = 0; |
6513 | 0 | s32 aspect_ratio_idc = -1; |
6514 | 0 | u32 ar_n=0, ar_d=0; |
6515 | 0 | Bool overscan_info_present_flag = 0; |
6516 | 0 | u32 overscan_info = 0; |
6517 | 0 | u32 video_signal_type_present_flag = 0; |
6518 | 0 | u32 video_format = 5; |
6519 | 0 | u32 video_full_range_flag = 0; |
6520 | 0 | u32 colour_description_present_flag = 0; |
6521 | 0 | u32 colour_primaries = 2; |
6522 | 0 | u32 transfer_characteristics = 2; |
6523 | 0 | u32 matrix_coefficients = 2; |
6524 | | //HEVC |
6525 | 0 | Bool neutral_chroma_indication_flag = GF_FALSE; |
6526 | 0 | Bool field_seq_flag = GF_FALSE; |
6527 | 0 | Bool frame_field_info_present_flag = GF_FALSE; |
6528 | 0 | Bool default_display_window_flag = GF_FALSE; |
6529 | 0 | u32 def_disp_win_left_offset = 0; |
6530 | 0 | u32 def_disp_win_right_offset = 0; |
6531 | 0 | u32 def_disp_win_top_offset = 0; |
6532 | 0 | u32 def_disp_win_bottom_offset = 0; |
6533 | | //AVC & HEVC |
6534 | 0 | Bool timing_info_present_flag = GF_FALSE; |
6535 | 0 | u32 num_units_in_tick = 0; |
6536 | 0 | u32 time_scale = 0; |
6537 | | //AVC |
6538 | 0 | Bool fixed_frame_rate_flag=GF_FALSE; |
6539 | | //HEVC |
6540 | 0 | Bool poc_proportional_to_timing_flag = GF_FALSE; |
6541 | 0 | u32 vui_num_ticks_poc_diff_one_minus1 = 0; |
6542 | | //VVC |
6543 | 0 | Bool progressive_source_flag = 1; |
6544 | 0 | Bool interlaced_source_flag = 0; |
6545 | 0 | Bool non_packed_constraint_flag = 0; |
6546 | 0 | Bool non_projected_constraint_flag = 0; |
6547 | 0 | Bool aspect_ratio_constant_flag = 1; |
6548 | 0 | u32 vui_start_pos = 0; |
6549 | 0 | u32 orig_vvc_payload_size = 0; |
6550 | 0 | Bool vui_chroma_loc_info_present_flag = 0; |
6551 | 0 | u32 chroma_loc1=0, chroma_loc2 = 0; |
6552 | 0 | u32 final_vvc_payload_size = 8; //4 first bits + 4 flags (ar, overscan and colour desc, chroma pos) |
6553 | 0 | u32 mod_vui_start_pos = 0; |
6554 | | |
6555 | | //if VUI is present, read all SAR and overscan values |
6556 | 0 | if (vui_present_flag) { /* VUI found in input bitstream */ |
6557 | 0 | if (codec == GF_CODECID_VVC) { |
6558 | | //align |
6559 | 0 | orig_vvc_payload_size = 8 * ( 1 + gf_bs_read_ue(orig) ); |
6560 | 0 | gf_bs_align(orig); |
6561 | 0 | vui_start_pos = gf_bs_get_bit_offset(orig); |
6562 | |
|
6563 | 0 | progressive_source_flag = gf_bs_read_int(orig, 1); |
6564 | 0 | interlaced_source_flag = gf_bs_read_int(orig, 1); |
6565 | 0 | non_packed_constraint_flag = gf_bs_read_int(orig, 1); |
6566 | 0 | non_projected_constraint_flag = gf_bs_read_int(orig, 1); |
6567 | 0 | } |
6568 | 0 | aspect_ratio_info_present_flag = gf_bs_read_int(orig, 1); |
6569 | |
|
6570 | 0 | if (aspect_ratio_info_present_flag) { |
6571 | 0 | if (codec == GF_CODECID_VVC) { |
6572 | 0 | aspect_ratio_constant_flag = gf_bs_read_int(orig, 1); |
6573 | 0 | } |
6574 | 0 | aspect_ratio_idc = gf_bs_read_int(orig, 8); /*aspect_ratio_idc*/ |
6575 | 0 | if (aspect_ratio_idc == 255) { |
6576 | 0 | ar_n = gf_bs_read_int(orig, 16); /*sar_width*/ |
6577 | 0 | ar_d = gf_bs_read_int(orig, 16); /*sar_height*/ |
6578 | 0 | } |
6579 | 0 | } |
6580 | | |
6581 | | /*overscan_info_present_flag */ |
6582 | 0 | overscan_info_present_flag = gf_bs_read_int(orig, 1); |
6583 | 0 | if(overscan_info_present_flag) { |
6584 | 0 | overscan_info = gf_bs_read_int(orig, 1); |
6585 | 0 | } |
6586 | | |
6587 | | /* read all video signal related flags first */ |
6588 | 0 | video_signal_type_present_flag = gf_bs_read_int(orig, 1); |
6589 | |
|
6590 | 0 | if (video_signal_type_present_flag) { |
6591 | 0 | if (codec != GF_CODECID_VVC) { |
6592 | 0 | video_format = gf_bs_read_int(orig, 3); |
6593 | 0 | video_full_range_flag = gf_bs_read_int(orig, 1); |
6594 | 0 | colour_description_present_flag = gf_bs_read_int(orig, 1); |
6595 | 0 | } else { |
6596 | 0 | colour_description_present_flag = 1; |
6597 | 0 | } |
6598 | |
|
6599 | 0 | if (colour_description_present_flag) { |
6600 | 0 | colour_primaries = gf_bs_read_int(orig, 8); |
6601 | 0 | transfer_characteristics = gf_bs_read_int(orig, 8); |
6602 | 0 | matrix_coefficients = gf_bs_read_int(orig, 8); |
6603 | 0 | if (codec == GF_CODECID_VVC) { |
6604 | 0 | video_full_range_flag = gf_bs_read_int(orig, 1); |
6605 | 0 | } |
6606 | 0 | } |
6607 | 0 | } |
6608 | |
|
6609 | 0 | if (codec == GF_CODECID_VVC) { |
6610 | 0 | vui_chroma_loc_info_present_flag = gf_bs_read_int(orig, 1); |
6611 | 0 | if (vui_chroma_loc_info_present_flag) { |
6612 | 0 | if (progressive_source_flag && !interlaced_source_flag) { |
6613 | 0 | chroma_loc1 = gf_bs_read_ue(orig); |
6614 | 0 | } else { |
6615 | 0 | chroma_loc1 = gf_bs_read_ue(orig); |
6616 | 0 | chroma_loc2 = gf_bs_read_ue(orig); |
6617 | 0 | } |
6618 | 0 | } |
6619 | | //LAST bit read for VVC |
6620 | 0 | } else { //AVC, HEVC |
6621 | 0 | vui_chroma_loc_info_present_flag = gf_bs_read_int(orig, 1); |
6622 | 0 | if (vui_chroma_loc_info_present_flag) { |
6623 | 0 | chroma_loc1 = gf_bs_read_ue(orig); //chroma_sample_loc_type_top_field |
6624 | 0 | chroma_loc2 = gf_bs_read_ue(orig); //chroma_sample_loc_type_bottom_field |
6625 | 0 | } |
6626 | |
|
6627 | 0 | if (codec == GF_CODECID_HEVC) { |
6628 | 0 | neutral_chroma_indication_flag = gf_bs_read_int(orig, 1); |
6629 | 0 | field_seq_flag = gf_bs_read_int(orig, 1); |
6630 | 0 | frame_field_info_present_flag = gf_bs_read_int(orig, 1); |
6631 | 0 | default_display_window_flag = gf_bs_read_int(orig, 1); |
6632 | 0 | if (default_display_window_flag) { |
6633 | 0 | def_disp_win_left_offset = gf_bs_read_ue(orig); |
6634 | 0 | def_disp_win_right_offset = gf_bs_read_ue(orig); |
6635 | 0 | def_disp_win_top_offset = gf_bs_read_ue(orig); |
6636 | 0 | def_disp_win_bottom_offset = gf_bs_read_ue(orig); |
6637 | 0 | } |
6638 | 0 | } |
6639 | |
|
6640 | 0 | timing_info_present_flag = gf_bs_read_int(orig, 1); |
6641 | 0 | if (timing_info_present_flag) { |
6642 | 0 | num_units_in_tick = gf_bs_read_int(orig, 32); |
6643 | 0 | time_scale = gf_bs_read_int(orig, 32); |
6644 | 0 | if (codec == GF_CODECID_AVC) { |
6645 | 0 | fixed_frame_rate_flag = gf_bs_read_int(orig, 1); |
6646 | | |
6647 | | //LAST bit read for AVC |
6648 | 0 | } else if (codec == GF_CODECID_HEVC) { |
6649 | 0 | poc_proportional_to_timing_flag = gf_bs_read_int(orig, 1); |
6650 | 0 | if (poc_proportional_to_timing_flag) |
6651 | 0 | /*vui_num_ticks_poc_diff_one_minus1 = */gf_bs_read_ue(orig); |
6652 | 0 | if (/*vui_hrd_parameters_present_flag = */gf_bs_read_int(orig, 1)) |
6653 | 0 | hevc_parse_hrd_parameters(orig, GF_TRUE, 0/*assumes max_sub_layers_minus1=0*/, 0); |
6654 | | |
6655 | | //LAST bit read for HEVC |
6656 | 0 | } |
6657 | 0 | } |
6658 | 0 | } |
6659 | 0 | } |
6660 | | |
6661 | | //recompute values |
6662 | | //no change |
6663 | 0 | if ((vui_info->ar_num<0) || (vui_info->ar_den<0)) { |
6664 | 0 | } |
6665 | | //remove par |
6666 | 0 | else if ((vui_info->ar_num==0) || (vui_info->ar_den==0)) { |
6667 | 0 | aspect_ratio_info_present_flag = 0; |
6668 | 0 | } |
6669 | | //set par |
6670 | 0 | else { |
6671 | 0 | aspect_ratio_info_present_flag = 1; |
6672 | 0 | } |
6673 | | |
6674 | | //add par size |
6675 | 0 | if (aspect_ratio_info_present_flag) { |
6676 | 0 | ar_n = vui_info->ar_num; |
6677 | 0 | ar_d = vui_info->ar_den; |
6678 | 0 | aspect_ratio_idc = avc_hevc_get_sar_idx((u32) ar_n, (u32) ar_d); |
6679 | 0 | if (codec == GF_CODECID_VVC) { |
6680 | 0 | final_vvc_payload_size += 9; |
6681 | 0 | if (aspect_ratio_idc==0xFF) |
6682 | 0 | final_vvc_payload_size += 32; |
6683 | 0 | } |
6684 | 0 | } |
6685 | |
|
6686 | 0 | if (vui_info->remove_video_info) { |
6687 | 0 | video_signal_type_present_flag = 0; |
6688 | 0 | } |
6689 | | /* correct the values of each flags */ |
6690 | 0 | else if ((vui_info->fullrange==0) && (vui_info->video_format==5) && (vui_info->color_prim==2) && (vui_info->color_tfc==2) && (vui_info->color_matrix==2)) { |
6691 | 0 | video_signal_type_present_flag = 0; /* all default, nothing to write*/ |
6692 | 0 | } else { |
6693 | 0 | video_signal_type_present_flag = 1; |
6694 | 0 | video_format = (vui_info->video_format < 0) ? video_format : vui_info->video_format; |
6695 | 0 | video_full_range_flag = (vui_info->fullrange < 0) ? video_full_range_flag : vui_info->fullrange; |
6696 | 0 | if ((vui_info->color_prim==2) && (vui_info->color_tfc==2) && (vui_info->color_matrix==2)) { |
6697 | 0 | colour_description_present_flag = 0; |
6698 | 0 | } else { |
6699 | 0 | colour_description_present_flag = 1; |
6700 | 0 | colour_primaries = (vui_info->color_prim < 0) ? colour_primaries : vui_info->color_prim; |
6701 | 0 | transfer_characteristics = (vui_info->color_tfc < 0) ? transfer_characteristics : vui_info->color_tfc; |
6702 | 0 | matrix_coefficients = (vui_info->color_matrix < 0) ? matrix_coefficients : vui_info->color_matrix; |
6703 | 0 | } |
6704 | 0 | if ((colour_primaries==2) && (transfer_characteristics==2) && (matrix_coefficients==2)) { |
6705 | 0 | colour_description_present_flag = 0; |
6706 | 0 | if ((video_format==5) && (video_full_range_flag==0)) |
6707 | 0 | video_signal_type_present_flag = 0; |
6708 | 0 | } |
6709 | |
|
6710 | 0 | if (codec == GF_CODECID_VVC) { |
6711 | 0 | if (!video_full_range_flag && !colour_description_present_flag) { |
6712 | 0 | video_signal_type_present_flag = 0; |
6713 | 0 | } else { |
6714 | 0 | final_vvc_payload_size += 25; |
6715 | 0 | } |
6716 | 0 | } |
6717 | 0 | } |
6718 | |
|
6719 | 0 | if ((codec == GF_CODECID_VVC) && vui_chroma_loc_info_present_flag) { |
6720 | 0 | if (progressive_source_flag && !interlaced_source_flag) { |
6721 | 0 | final_vvc_payload_size += gf_get_ue_nb_bits(chroma_loc1); |
6722 | 0 | } else { |
6723 | 0 | final_vvc_payload_size += gf_get_ue_nb_bits(chroma_loc1); |
6724 | 0 | final_vvc_payload_size += gf_get_ue_nb_bits(chroma_loc2); |
6725 | 0 | } |
6726 | 0 | } |
6727 | | //remove VUI timing |
6728 | 0 | if (vui_info->remove_vui_timing_info) |
6729 | 0 | timing_info_present_flag = 0; |
6730 | | |
6731 | | //always rewrite VUI |
6732 | 0 | gf_bs_write_int(mod, 1, 1); |
6733 | 0 | if (codec == GF_CODECID_VVC) { |
6734 | 0 | while (final_vvc_payload_size%8) |
6735 | 0 | final_vvc_payload_size++; |
6736 | 0 | final_vvc_payload_size/=8; |
6737 | |
|
6738 | 0 | gf_bs_write_ue(mod, final_vvc_payload_size-1); |
6739 | 0 | gf_bs_align(mod); |
6740 | 0 | mod_vui_start_pos = gf_bs_get_bit_offset(mod); |
6741 | 0 | final_vvc_payload_size *= 8; |
6742 | |
|
6743 | 0 | gf_bs_write_int(mod, progressive_source_flag, 1); |
6744 | 0 | gf_bs_write_int(mod, interlaced_source_flag, 1); |
6745 | 0 | gf_bs_write_int(mod, non_packed_constraint_flag, 1); |
6746 | 0 | gf_bs_write_int(mod, non_projected_constraint_flag, 1); |
6747 | 0 | } |
6748 | |
|
6749 | 0 | gf_bs_write_int(mod, aspect_ratio_info_present_flag, 1); |
6750 | 0 | if (aspect_ratio_info_present_flag) { |
6751 | 0 | if (codec == GF_CODECID_VVC) |
6752 | 0 | gf_bs_write_int(mod, aspect_ratio_constant_flag, 1); |
6753 | |
|
6754 | 0 | gf_bs_write_int(mod, aspect_ratio_idc, 8); |
6755 | 0 | if (aspect_ratio_idc == 255) { |
6756 | 0 | gf_bs_write_int(mod, ar_n, 16); |
6757 | 0 | gf_bs_write_int(mod, ar_d, 16); |
6758 | 0 | } |
6759 | 0 | if (vui_info->update) { |
6760 | 0 | vui_info->ar_num = ar_n; |
6761 | 0 | vui_info->ar_den = ar_d; |
6762 | 0 | } |
6763 | 0 | } |
6764 | 0 | gf_bs_write_int(mod, overscan_info_present_flag, 1); |
6765 | 0 | if (overscan_info_present_flag) { |
6766 | 0 | gf_bs_write_int(mod, overscan_info, 1); |
6767 | 0 | } |
6768 | |
|
6769 | 0 | gf_bs_write_int(mod, video_signal_type_present_flag, 1); |
6770 | 0 | if (video_signal_type_present_flag) { |
6771 | 0 | if (codec != GF_CODECID_VVC) { |
6772 | 0 | gf_bs_write_int(mod, video_format, 3); |
6773 | 0 | gf_bs_write_int(mod, video_full_range_flag, 1); |
6774 | 0 | gf_bs_write_int(mod, colour_description_present_flag, 1); |
6775 | 0 | } else { |
6776 | 0 | colour_description_present_flag = 1; |
6777 | 0 | } |
6778 | |
|
6779 | 0 | if (colour_description_present_flag) { |
6780 | 0 | gf_bs_write_int(mod, colour_primaries, 8); |
6781 | 0 | gf_bs_write_int(mod, transfer_characteristics, 8); |
6782 | 0 | gf_bs_write_int(mod, matrix_coefficients, 8); |
6783 | 0 | if (codec == GF_CODECID_VVC) |
6784 | 0 | gf_bs_write_int(mod, video_full_range_flag, 1); |
6785 | 0 | } |
6786 | |
|
6787 | 0 | if (vui_info->update) { |
6788 | 0 | vui_info->video_format = video_format; |
6789 | 0 | vui_info->fullrange = video_full_range_flag; |
6790 | 0 | if (colour_description_present_flag) { |
6791 | 0 | vui_info->color_prim = colour_primaries; |
6792 | 0 | vui_info->color_tfc = transfer_characteristics; |
6793 | 0 | vui_info->color_matrix = matrix_coefficients; |
6794 | 0 | } |
6795 | 0 | } |
6796 | 0 | } |
6797 | |
|
6798 | 0 | if (codec == GF_CODECID_VVC) { |
6799 | | //write vui_chroma_loc_info_present_flag |
6800 | 0 | gf_bs_write_int(mod, vui_chroma_loc_info_present_flag, 1); |
6801 | 0 | if (vui_chroma_loc_info_present_flag) { |
6802 | 0 | if (progressive_source_flag && !interlaced_source_flag) { |
6803 | 0 | gf_bs_write_ue(mod, chroma_loc1); |
6804 | 0 | } else { |
6805 | 0 | gf_bs_write_ue(mod, chroma_loc1); |
6806 | 0 | gf_bs_write_ue(mod, chroma_loc2); |
6807 | 0 | } |
6808 | 0 | } |
6809 | | //we don't copy over vui extension (they're not supposed to be present), but we must parse them |
6810 | 0 | if (vui_present_flag) { |
6811 | | //we are byte aligned for vui_paramaters |
6812 | 0 | Bool more_data_in_payload = GF_TRUE; |
6813 | 0 | vui_start_pos = gf_bs_get_bit_offset(orig) - vui_start_pos; |
6814 | 0 | if (gf_bs_is_align(orig) && (vui_start_pos == orig_vvc_payload_size)) |
6815 | 0 | more_data_in_payload = GF_FALSE; |
6816 | |
|
6817 | 0 | u32 nb_bits = orig_vvc_payload_size - vui_start_pos; |
6818 | 0 | if (more_data_in_payload) { |
6819 | 0 | if (nb_bits<8) { |
6820 | 0 | u32 val = gf_bs_peek_bits(orig, nb_bits, 0); |
6821 | 0 | u32 bit_pos = 1<<(nb_bits-1); |
6822 | 0 | if (val == bit_pos) |
6823 | 0 | more_data_in_payload = 0; |
6824 | 0 | } |
6825 | 0 | } |
6826 | 0 | if (more_data_in_payload) { |
6827 | 0 | while (nb_bits) { |
6828 | 0 | nb_bits--; |
6829 | 0 | gf_bs_read_int(orig, 1); //vui_reserved_payload_extension_data |
6830 | | //load next 32 bits, if only 1 at MSB and 0 afterwards, done |
6831 | 0 | if (nb_bits<32) { |
6832 | 0 | u32 val = gf_bs_peek_bits(orig, nb_bits, 0); |
6833 | 0 | u32 bit_pos = 1<<(nb_bits-1); |
6834 | 0 | if (val == bit_pos) |
6835 | 0 | break; |
6836 | 0 | } |
6837 | 0 | } |
6838 | | |
6839 | | //then byte alignment of vui_payload |
6840 | 0 | gf_bs_read_int(orig, 1); //vui_payload_bit_equal_to_one |
6841 | 0 | gf_bs_align(orig); |
6842 | 0 | } |
6843 | 0 | } |
6844 | 0 | mod_vui_start_pos = gf_bs_get_bit_offset(mod) - mod_vui_start_pos; |
6845 | | //check if we need explicit align |
6846 | 0 | if (!gf_bs_is_align(mod) || (mod_vui_start_pos != final_vvc_payload_size)) { |
6847 | 0 | gf_bs_write_int(mod, 1, 1); //vui_payload_bit_equal_to_one |
6848 | 0 | gf_bs_align(mod); |
6849 | 0 | } |
6850 | | //VVC done |
6851 | 0 | return; |
6852 | 0 | } |
6853 | | |
6854 | | //AVC and HEVC |
6855 | 0 | gf_bs_write_int(mod, vui_chroma_loc_info_present_flag, 1); |
6856 | 0 | if (vui_chroma_loc_info_present_flag) { |
6857 | 0 | gf_bs_write_ue(mod, chroma_loc1); //chroma_sample_loc_type_top_field |
6858 | 0 | gf_bs_write_ue(mod, chroma_loc2); //chroma_sample_loc_type_bottom_field |
6859 | 0 | } |
6860 | |
|
6861 | 0 | if (codec == GF_CODECID_HEVC) { |
6862 | 0 | gf_bs_write_int(mod, neutral_chroma_indication_flag, 1); |
6863 | 0 | gf_bs_write_int(mod, field_seq_flag, 1); |
6864 | 0 | gf_bs_write_int(mod, frame_field_info_present_flag, 1); |
6865 | 0 | gf_bs_write_int(mod, default_display_window_flag, 1); |
6866 | 0 | if (default_display_window_flag) { |
6867 | 0 | gf_bs_write_ue(mod, def_disp_win_left_offset); |
6868 | 0 | gf_bs_write_ue(mod, def_disp_win_right_offset); |
6869 | 0 | gf_bs_write_ue(mod, def_disp_win_top_offset); |
6870 | 0 | gf_bs_write_ue(mod, def_disp_win_bottom_offset); |
6871 | 0 | } |
6872 | 0 | } |
6873 | |
|
6874 | 0 | gf_bs_write_int(mod, timing_info_present_flag, 1); |
6875 | 0 | if (timing_info_present_flag) { |
6876 | 0 | gf_bs_write_int(mod, num_units_in_tick, 32); |
6877 | 0 | gf_bs_write_int(mod, time_scale, 32); |
6878 | 0 | if (codec == GF_CODECID_AVC) { |
6879 | 0 | gf_bs_write_int(mod, fixed_frame_rate_flag, 1); |
6880 | 0 | } else if (codec == GF_CODECID_HEVC) { |
6881 | 0 | gf_bs_write_int(mod, poc_proportional_to_timing_flag, 1); |
6882 | 0 | if (poc_proportional_to_timing_flag) |
6883 | 0 | gf_bs_write_ue(mod, vui_num_ticks_poc_diff_one_minus1); |
6884 | 0 | } |
6885 | 0 | } |
6886 | | |
6887 | | /*no VUI in input bitstream but we just inserted one, set all remaining vui flags to 0*/ |
6888 | 0 | if (!vui_present_flag) { |
6889 | 0 | if (codec == GF_CODECID_AVC) { |
6890 | 0 | gf_bs_write_int(mod, 0, 1); /*nal_hrd_parameters_present*/ |
6891 | 0 | gf_bs_write_int(mod, 0, 1); /*vcl_hrd_parameters_present*/ |
6892 | 0 | gf_bs_write_int(mod, 0, 1); /*pic_struct_present*/ |
6893 | 0 | gf_bs_write_int(mod, 0, 1); /*bitstream_restriction*/ |
6894 | 0 | } else if (codec == GF_CODECID_HEVC) { |
6895 | 0 | if (timing_info_present_flag) { |
6896 | 0 | gf_bs_write_int(mod, 0, 1); /*vui_hrd_parameters_present_flag*/ |
6897 | 0 | } |
6898 | 0 | gf_bs_write_int(mod, 0, 1); /*bitstream_restriction*/ |
6899 | 0 | } |
6900 | 0 | } |
6901 | | /*otherwise we copy over the bits from the input bitstream*/ |
6902 | 0 | } |
6903 | | |
6904 | | GF_Err gf_avc_change_vui(GF_AVCConfig *avcc, GF_VUIInfo *vui_info) |
6905 | 0 | { |
6906 | 0 | AVCState avc; |
6907 | 0 | u32 i, bit_offset, flag; |
6908 | 0 | s32 idx; |
6909 | 0 | GF_AVCConfigSlot *slc; |
6910 | |
|
6911 | 0 | if (!avcc) |
6912 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
6913 | | |
6914 | 0 | memset(&avc, 0, sizeof(AVCState)); |
6915 | 0 | avc.sps_active_idx = -1; |
6916 | |
|
6917 | 0 | i=0; |
6918 | 0 | while ((slc = (GF_AVCConfigSlot *)gf_list_enum(avcc->sequenceParameterSets, &i))) { |
6919 | 0 | GF_BitStream *orig, *mod; |
6920 | 0 | u8 *no_emulation_buf = NULL; |
6921 | 0 | u32 no_emulation_buf_size = 0, emulation_bytes = 0; |
6922 | 0 | idx = gf_avc_read_sps(slc->data, slc->size, &avc, 0, &bit_offset); |
6923 | 0 | if (idx<0) { |
6924 | 0 | continue; |
6925 | 0 | } |
6926 | | |
6927 | | /*SPS still contains emulation bytes*/ |
6928 | 0 | no_emulation_buf = gf_malloc((slc->size - 1) * sizeof(char)); |
6929 | 0 | no_emulation_buf_size = gf_media_nalu_remove_emulation_bytes(slc->data + 1, no_emulation_buf, slc->size - 1); |
6930 | |
|
6931 | 0 | orig = gf_bs_new(no_emulation_buf, no_emulation_buf_size, GF_BITSTREAM_READ); |
6932 | 0 | gf_bs_read_data(orig, no_emulation_buf, no_emulation_buf_size); |
6933 | 0 | gf_bs_seek(orig, 0); |
6934 | 0 | mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
6935 | | |
6936 | | /*copy over till vui flag*/ |
6937 | 0 | while (bit_offset > 8/*bit_offset doesn't take care of the first byte (NALU type)*/) { |
6938 | 0 | flag = gf_bs_read_int(orig, 1); |
6939 | 0 | gf_bs_write_int(mod, flag, 1); |
6940 | 0 | bit_offset--; |
6941 | 0 | } |
6942 | |
|
6943 | 0 | avc_hevc_vvc_rewrite_vui(vui_info, orig, mod, GF_CODECID_AVC); |
6944 | | |
6945 | | /*finally copy over remaining*/ |
6946 | 0 | while (gf_bs_bits_available(orig)) { |
6947 | 0 | flag = gf_bs_read_int(orig, 1); |
6948 | 0 | gf_bs_write_int(mod, flag, 1); |
6949 | 0 | } |
6950 | 0 | gf_bs_del(orig); |
6951 | 0 | orig = NULL; |
6952 | 0 | gf_free(no_emulation_buf); |
6953 | | |
6954 | | /*set anti-emulation*/ |
6955 | 0 | gf_bs_get_content(mod, &no_emulation_buf, &flag); |
6956 | 0 | emulation_bytes = gf_media_nalu_emulation_bytes_add_count(no_emulation_buf, flag); |
6957 | 0 | if (flag+emulation_bytes+1>slc->size) |
6958 | 0 | slc->data = (char*)gf_realloc(slc->data, flag+emulation_bytes+1); |
6959 | 0 | slc->size = gf_media_nalu_add_emulation_bytes(no_emulation_buf, slc->data + 1, flag) + 1; |
6960 | |
|
6961 | 0 | gf_bs_del(mod); |
6962 | 0 | gf_free(no_emulation_buf); |
6963 | 0 | } |
6964 | 0 | return GF_OK; |
6965 | 0 | } |
6966 | | |
6967 | | GF_EXPORT |
6968 | | GF_Err gf_avc_change_par(GF_AVCConfig *avcc, s32 ar_n, s32 ar_d) |
6969 | 0 | { |
6970 | 0 | GF_VUIInfo vuii; |
6971 | 0 | memset(&vuii, 0, sizeof(GF_VUIInfo)); |
6972 | 0 | vuii.ar_num = ar_n; |
6973 | 0 | vuii.ar_den = ar_d; |
6974 | 0 | vuii.fullrange = -1; |
6975 | 0 | vuii.video_format = -1; |
6976 | 0 | vuii.color_prim = -1; |
6977 | 0 | vuii.color_tfc = -1; |
6978 | 0 | vuii.color_matrix = -1; |
6979 | 0 | return gf_avc_change_vui(avcc, &vuii); |
6980 | 0 | } |
6981 | | |
6982 | | GF_EXPORT |
6983 | | GF_Err gf_avc_change_color(GF_AVCConfig *avcc, s32 fullrange, s32 vidformat, s32 colorprim, s32 transfer, s32 colmatrix) |
6984 | 0 | { |
6985 | 0 | GF_VUIInfo vuii; |
6986 | 0 | memset(&vuii, 0, sizeof(GF_VUIInfo)); |
6987 | 0 | vuii.ar_num = -1; |
6988 | 0 | vuii.ar_den = -1; |
6989 | 0 | vuii.fullrange = fullrange; |
6990 | 0 | vuii.video_format = vidformat; |
6991 | 0 | vuii.color_prim = colorprim; |
6992 | 0 | vuii.color_tfc = transfer; |
6993 | 0 | vuii.color_matrix = colmatrix; |
6994 | 0 | return gf_avc_change_vui(avcc, &vuii); |
6995 | 0 | } |
6996 | | |
6997 | | |
6998 | | GF_EXPORT |
6999 | | GF_Err gf_avc_get_sps_info(u8 *sps_data, u32 sps_size, u32 *sps_id, u32 *width, u32 *height, s32 *par_n, s32 *par_d) |
7000 | 0 | { |
7001 | 0 | AVCState avc; |
7002 | 0 | s32 idx; |
7003 | 0 | memset(&avc, 0, sizeof(AVCState)); |
7004 | 0 | avc.sps_active_idx = -1; |
7005 | |
|
7006 | 0 | idx = gf_avc_read_sps(sps_data, sps_size, &avc, 0, NULL); |
7007 | 0 | if (idx < 0) { |
7008 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
7009 | 0 | } |
7010 | 0 | if (sps_id) *sps_id = idx; |
7011 | |
|
7012 | 0 | if (width) *width = avc.sps[idx].width; |
7013 | 0 | if (height) *height = avc.sps[idx].height; |
7014 | 0 | if (par_n) *par_n = avc.sps[idx].vui.par_num ? avc.sps[idx].vui.par_num : (u32)-1; |
7015 | 0 | if (par_d) *par_d = avc.sps[idx].vui.par_den ? avc.sps[idx].vui.par_den : (u32)-1; |
7016 | |
|
7017 | 0 | return GF_OK; |
7018 | 0 | } |
7019 | | |
7020 | | GF_EXPORT |
7021 | | GF_Err gf_avc_get_pps_info(u8 *pps_data, u32 pps_size, u32 *pps_id, u32 *sps_id) |
7022 | 0 | { |
7023 | 0 | GF_BitStream *bs; |
7024 | 0 | GF_Err e = GF_OK; |
7025 | |
|
7026 | 0 | bs = gf_bs_new(pps_data, pps_size, GF_BITSTREAM_READ); |
7027 | 0 | if (!bs) { |
7028 | 0 | e = GF_NON_COMPLIANT_BITSTREAM; |
7029 | 0 | goto exit; |
7030 | 0 | } |
7031 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
7032 | 0 | /*nal hdr*/ gf_bs_read_int(bs, 8); |
7033 | |
|
7034 | 0 | *pps_id = gf_bs_read_ue(bs); |
7035 | 0 | *sps_id = gf_bs_read_ue(bs); |
7036 | |
|
7037 | 0 | exit: |
7038 | 0 | gf_bs_del(bs); |
7039 | 0 | return e; |
7040 | 0 | } |
7041 | | |
7042 | | /********** |
7043 | | HEVC parsing |
7044 | | **********/ |
7045 | | |
7046 | | Bool gf_hevc_slice_is_intra(HEVCState *hevc) |
7047 | 161k | { |
7048 | 161k | switch (hevc->s_info.nal_unit_type) { |
7049 | 1.46k | case GF_HEVC_NALU_SLICE_BLA_W_LP: |
7050 | 2.75k | case GF_HEVC_NALU_SLICE_BLA_W_DLP: |
7051 | 9.82k | case GF_HEVC_NALU_SLICE_BLA_N_LP: |
7052 | 12.1k | case GF_HEVC_NALU_SLICE_IDR_W_DLP: |
7053 | 14.2k | case GF_HEVC_NALU_SLICE_IDR_N_LP: |
7054 | 15.1k | case GF_HEVC_NALU_SLICE_CRA: |
7055 | 15.1k | return GF_TRUE; |
7056 | 146k | default: |
7057 | 146k | return GF_FALSE; |
7058 | 161k | } |
7059 | 161k | } |
7060 | | |
7061 | | Bool gf_hevc_slice_is_IDR(HEVCState *hevc) |
7062 | 323k | { |
7063 | 323k | if (hevc->sei.recovery_point.valid) |
7064 | 0 | { |
7065 | 0 | hevc->sei.recovery_point.valid = 0; |
7066 | 0 | return GF_TRUE; |
7067 | 0 | } |
7068 | 323k | switch (hevc->s_info.nal_unit_type) { |
7069 | 4.55k | case GF_HEVC_NALU_SLICE_IDR_W_DLP: |
7070 | 8.80k | case GF_HEVC_NALU_SLICE_IDR_N_LP: |
7071 | 8.80k | return GF_TRUE; |
7072 | 314k | default: |
7073 | 314k | return GF_FALSE; |
7074 | 323k | } |
7075 | 323k | } |
7076 | | |
7077 | | static Bool hevc_parse_short_term_ref_pic_set(GF_BitStream *bs, HEVC_SPS *sps, u32 idx_rps) |
7078 | 76.9k | { |
7079 | 76.9k | u32 i; |
7080 | 76.9k | Bool inter_ref_pic_set_prediction_flag = 0; |
7081 | 76.9k | if (idx_rps != 0) |
7082 | 18.1k | inter_ref_pic_set_prediction_flag = gf_bs_read_int_log_idx(bs, 1, "inter_ref_pic_set_prediction_flag", idx_rps); |
7083 | | |
7084 | 76.9k | if (inter_ref_pic_set_prediction_flag) { |
7085 | 8.80k | HEVC_ReferencePictureSets *ref_ps, *rps; |
7086 | 8.80k | u32 delta_idx_minus1 = 0; |
7087 | 8.80k | u32 ref_idx; |
7088 | 8.80k | u32 delta_rps_sign; |
7089 | 8.80k | u32 abs_delta_rps_minus1, nb_ref_pics; |
7090 | 8.80k | s32 deltaRPS; |
7091 | 8.80k | u32 k = 0, k0 = 0, k1 = 0; |
7092 | 8.80k | if (idx_rps == sps->num_short_term_ref_pic_sets) |
7093 | 1.95k | delta_idx_minus1 = gf_bs_read_ue_log_idx(bs, "delta_idx_minus1", idx_rps); |
7094 | | |
7095 | 8.80k | if (delta_idx_minus1 > idx_rps - 1) |
7096 | 1.20k | return GF_FALSE; |
7097 | | |
7098 | 7.60k | ref_idx = idx_rps - 1 - delta_idx_minus1; |
7099 | 7.60k | delta_rps_sign = gf_bs_read_int_log_idx(bs, 1, "delta_rps_sign", idx_rps); |
7100 | 7.60k | abs_delta_rps_minus1 = gf_bs_read_ue_log_idx(bs, "abs_delta_rps_minus1", idx_rps); |
7101 | 7.60k | deltaRPS = (1 - (delta_rps_sign << 1)) * (abs_delta_rps_minus1 + 1); |
7102 | | |
7103 | 7.60k | rps = &sps->rps[idx_rps]; |
7104 | 7.60k | ref_ps = &sps->rps[ref_idx]; |
7105 | 7.60k | nb_ref_pics = ref_ps->num_negative_pics + ref_ps->num_positive_pics; |
7106 | 28.9k | for (i = 0; i <= nb_ref_pics; i++) { |
7107 | 21.3k | s32 ref_idc; |
7108 | 21.3k | s32 used_by_curr_pic_flag = gf_bs_read_int_log_idx2(bs, 1, "used_by_curr_pic_flag", idx_rps, i); |
7109 | 21.3k | ref_idc = used_by_curr_pic_flag ? 1 : 0; |
7110 | 21.3k | if (!used_by_curr_pic_flag) { |
7111 | 8.54k | used_by_curr_pic_flag = gf_bs_read_int_log_idx2(bs, 1, "use_delta_flag", idx_rps, i); |
7112 | 8.54k | ref_idc = used_by_curr_pic_flag << 1; |
7113 | 8.54k | } |
7114 | 21.3k | if ((ref_idc == 1) || (ref_idc == 2)) { |
7115 | 16.0k | s32 deltaPOC = deltaRPS; |
7116 | 16.0k | if ((i < nb_ref_pics) && (i<16)) { |
7117 | 9.33k | if ((ref_ps->delta_poc[i] > 0 && deltaPOC > GF_INT_MAX - ref_ps->delta_poc[i]) || |
7118 | 9.33k | (ref_ps->delta_poc[i] < 0 && deltaPOC < GF_INT_MIN - ref_ps->delta_poc[i]) ) |
7119 | | |
7120 | 0 | return GF_FALSE; |
7121 | | |
7122 | 9.33k | deltaPOC += ref_ps->delta_poc[i]; |
7123 | 9.33k | } |
7124 | 16.0k | if (k<32) { |
7125 | 16.0k | rps->delta_poc[k] = deltaPOC; |
7126 | 16.0k | rps->used_by_curr_pic[k] = used_by_curr_pic_flag; |
7127 | 16.0k | } |
7128 | | |
7129 | 16.0k | if (deltaPOC < 0) k0++; |
7130 | 6.34k | else k1++; |
7131 | | |
7132 | 16.0k | k++; |
7133 | 16.0k | } |
7134 | 21.3k | } |
7135 | 7.60k | rps->num_negative_pics = k0; |
7136 | 7.60k | rps->num_positive_pics = k1; |
7137 | 7.60k | } |
7138 | 68.1k | else { |
7139 | 68.1k | s32 prev = 0, poc, offset; |
7140 | 68.1k | Bool nb_pics_valid = GF_TRUE; |
7141 | 68.1k | sps->rps[idx_rps].num_negative_pics = gf_bs_read_ue_log_idx(bs, "num_negative_pics", idx_rps); |
7142 | 68.1k | sps->rps[idx_rps].num_positive_pics = gf_bs_read_ue_log_idx(bs, "num_positive_pics", idx_rps); |
7143 | 68.1k | if (sps->rps[idx_rps].num_negative_pics > 16) { |
7144 | 10.2k | sps->rps[idx_rps].num_negative_pics = 0; |
7145 | 10.2k | nb_pics_valid = GF_FALSE; |
7146 | 10.2k | } |
7147 | 68.1k | if (sps->rps[idx_rps].num_positive_pics > 16) { |
7148 | 4.37k | sps->rps[idx_rps].num_positive_pics = 0; |
7149 | 4.37k | nb_pics_valid = GF_FALSE; |
7150 | 4.37k | } |
7151 | 68.1k | if (!nb_pics_valid) |
7152 | 12.3k | return GF_FALSE; |
7153 | | |
7154 | 170k | for (i = 0; i < sps->rps[idx_rps].num_negative_pics; i++) { |
7155 | 114k | u32 delta_poc_s0_minus1 = gf_bs_read_ue_log_idx2(bs, "delta_poc_s0_minus1", idx_rps, i); |
7156 | 114k | poc = prev - delta_poc_s0_minus1 - 1; |
7157 | 114k | prev = poc; |
7158 | 114k | sps->rps[idx_rps].delta_poc[i] = poc; |
7159 | 114k | sps->rps[idx_rps].used_by_curr_pic[i] = gf_bs_read_int_log_idx2(bs, 1, "used_by_curr_pic_s0_flag", idx_rps, i); |
7160 | 114k | } |
7161 | 55.7k | prev = 0; |
7162 | 55.7k | offset = sps->rps[idx_rps].num_negative_pics; |
7163 | 165k | for (i = 0; i < sps->rps[idx_rps].num_positive_pics; i++) { |
7164 | 109k | u32 delta_poc_s1_minus1 = gf_bs_read_ue_log_idx2(bs, "delta_poc_s1_minus1" , idx_rps, i); |
7165 | 109k | poc = prev + delta_poc_s1_minus1 + 1; |
7166 | 109k | prev = poc; |
7167 | 109k | sps->rps[idx_rps].delta_poc[offset + i] = poc; |
7168 | 109k | sps->rps[idx_rps].used_by_curr_pic[offset + i] = gf_bs_read_int_log_idx2(bs, 1, "used_by_curr_pic_s1_flag", idx_rps, i); |
7169 | 109k | } |
7170 | 55.7k | } |
7171 | 63.3k | return GF_TRUE; |
7172 | 76.9k | } |
7173 | | |
7174 | | void hevc_pred_weight_table(GF_BitStream *bs, HEVCState *hevc, HEVCSliceInfo *si, HEVC_PPS *pps, HEVC_SPS *sps, u32 num_ref_idx_l0_active, u32 num_ref_idx_l1_active) |
7175 | 39.0k | { |
7176 | 39.0k | u32 i, num_ref_idx; |
7177 | 39.0k | Bool first_pass = GF_TRUE; |
7178 | 39.0k | u8 luma_weights[20], chroma_weights[20]; |
7179 | 39.0k | u32 ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc; |
7180 | | |
7181 | 39.0k | num_ref_idx = num_ref_idx_l0_active < 20 ? num_ref_idx_l0_active : 19 ; |
7182 | | |
7183 | 39.0k | gf_bs_read_ue_log(bs, "luma_log2_weight_denom"); |
7184 | 39.0k | if (ChromaArrayType != 0) |
7185 | 909 | gf_bs_read_se_log(bs, "delta_chroma_log2_weight_denom"); |
7186 | | |
7187 | 63.5k | parse_weights: |
7188 | 334k | for (i = 0; i < num_ref_idx; i++) { |
7189 | 271k | luma_weights[i] = gf_bs_read_int_log_idx(bs, 1, "luma_weights", i); |
7190 | | //inferred to be 0 if not present |
7191 | 271k | chroma_weights[i] = 0; |
7192 | 271k | } |
7193 | 63.5k | if (ChromaArrayType != 0) { |
7194 | 6.74k | for (i = 0; i < num_ref_idx; i++) { |
7195 | 4.97k | chroma_weights[i] = gf_bs_read_int_log_idx(bs, 1, "chroma_weights", i); |
7196 | 4.97k | } |
7197 | 1.76k | } |
7198 | 334k | for (i = 0; i < num_ref_idx; i++) { |
7199 | 271k | if (luma_weights[i]) { |
7200 | 47.2k | gf_bs_read_se_log_idx(bs, "delta_luma_weight_l0", i); |
7201 | 47.2k | gf_bs_read_se_log_idx(bs, "luma_offset_l0", i); |
7202 | 47.2k | } |
7203 | 271k | if (chroma_weights[i]) { |
7204 | 2.74k | gf_bs_read_se_log_idx(bs, "delta_chroma_weight_l0_0", i); |
7205 | 2.74k | gf_bs_read_se_log_idx(bs, "delta_chroma_offset_l0_0", i); |
7206 | | |
7207 | 2.74k | gf_bs_read_se_log_idx(bs, "delta_chroma_weight_l0_1", i); |
7208 | 2.74k | gf_bs_read_se_log_idx(bs, "delta_chroma_offset_l0_1", i); |
7209 | 2.74k | } |
7210 | 271k | } |
7211 | | |
7212 | 63.5k | if (si->slice_type == GF_HEVC_SLICE_TYPE_B) { |
7213 | 49.0k | if (!first_pass) return; |
7214 | 24.5k | first_pass = GF_FALSE; |
7215 | 24.5k | num_ref_idx = num_ref_idx_l1_active < 20 ? num_ref_idx_l1_active : 19 ; |
7216 | 24.5k | goto parse_weights; |
7217 | 49.0k | } |
7218 | 63.5k | } |
7219 | | |
7220 | | static void hevc_ref_pic_lists_modification(GF_BitStream *bs, HEVC_ReferencePictureSets *rps, u32 slice_type, u32 num_ref_idx_l0_active, u32 num_ref_idx_l1_active, u32 NumPicTotalCurr) |
7221 | 3.15k | { |
7222 | 3.15k | u32 i; |
7223 | 3.15k | rps->modif_flag_l0 = gf_bs_read_int_log(bs, 1, "ref_pic_list_modification_flag_l0"); |
7224 | 3.15k | u32 nb_bits = 1, val=NumPicTotalCurr-1; |
7225 | 6.40k | while ( val >>= 1) nb_bits++; |
7226 | | |
7227 | 3.15k | if (rps->modif_flag_l0) { |
7228 | 3.82k | for (i=0; i<MIN(num_ref_idx_l0_active, GF_ARRAY_LENGTH(rps->modif_idx_l0)); i++) { |
7229 | 3.20k | rps->modif_idx_l0[i] = gf_bs_read_int(bs, nb_bits); |
7230 | 3.20k | } |
7231 | 626 | } |
7232 | 3.15k | if (slice_type == GF_HEVC_SLICE_TYPE_B) { |
7233 | 3.02k | rps->modif_flag_l1 = gf_bs_read_int_log(bs, 1, "ref_pic_list_modification_flag_l1"); |
7234 | 3.02k | if (rps->modif_flag_l1) { |
7235 | 2.00k | for (i=0; i<MIN(num_ref_idx_l1_active, GF_ARRAY_LENGTH(rps->modif_idx_l1)); i++) { |
7236 | 1.49k | rps->modif_idx_l1[i] = gf_bs_read_int(bs, nb_bits); |
7237 | 1.49k | } |
7238 | 503 | } |
7239 | 3.02k | } |
7240 | 3.15k | } |
7241 | | |
7242 | | static |
7243 | | s32 hevc_parse_slice_segment(GF_BitStream *bs, HEVCState *hevc, HEVCSliceInfo *si) |
7244 | 348k | { |
7245 | 348k | u32 i, j; |
7246 | 348k | HEVC_PPS *pps; |
7247 | 348k | HEVC_SPS *sps; |
7248 | 348k | s32 pps_id; |
7249 | 348k | Bool RapPicFlag = GF_FALSE; |
7250 | 348k | Bool IDRPicFlag = GF_FALSE; |
7251 | | |
7252 | 348k | si->first_slice_segment_in_pic_flag = gf_bs_read_int_log(bs, 1, "first_slice_segment_in_pic_flag"); |
7253 | | |
7254 | 348k | switch (si->nal_unit_type) { |
7255 | 6.28k | case GF_HEVC_NALU_SLICE_IDR_W_DLP: |
7256 | 14.0k | case GF_HEVC_NALU_SLICE_IDR_N_LP: |
7257 | 14.0k | IDRPicFlag = GF_TRUE; |
7258 | 14.0k | RapPicFlag = GF_TRUE; |
7259 | 14.0k | break; |
7260 | 4.64k | case GF_HEVC_NALU_SLICE_BLA_W_LP: |
7261 | 8.75k | case GF_HEVC_NALU_SLICE_BLA_W_DLP: |
7262 | 34.5k | case GF_HEVC_NALU_SLICE_BLA_N_LP: |
7263 | 37.1k | case GF_HEVC_NALU_SLICE_CRA: |
7264 | 37.1k | RapPicFlag = GF_TRUE; |
7265 | 37.1k | break; |
7266 | 348k | } |
7267 | | |
7268 | 348k | if (RapPicFlag) { |
7269 | 51.2k | gf_bs_read_int_log(bs, 1, "no_output_of_prior_pics_flag"); |
7270 | 51.2k | } |
7271 | | |
7272 | 348k | pps_id = gf_bs_read_ue_log(bs, "pps_id"); |
7273 | 348k | if ((pps_id<0) || (pps_id >= 64)) |
7274 | 35.0k | return -1; |
7275 | | |
7276 | 312k | pps = &hevc->pps[pps_id]; |
7277 | 312k | sps = &hevc->sps[pps->sps_id]; |
7278 | 312k | si->sps = sps; |
7279 | 312k | si->pps = pps; |
7280 | 312k | si->num_ref_idx_l0_active = si->num_ref_idx_l1_active = 0; |
7281 | | |
7282 | 312k | if (!si->first_slice_segment_in_pic_flag && pps->dependent_slice_segments_enabled_flag) { |
7283 | 11.3k | si->dependent_slice_segment_flag = gf_bs_read_int_log(bs, 1, "dependent_slice_segment_flag"); |
7284 | 11.3k | } |
7285 | 301k | else { |
7286 | 301k | si->dependent_slice_segment_flag = GF_FALSE; |
7287 | 301k | } |
7288 | | |
7289 | 312k | if (!si->first_slice_segment_in_pic_flag) { |
7290 | 206k | si->slice_segment_address = gf_bs_read_int_log(bs, sps->bitsSliceSegmentAddress, "slice_segment_address"); |
7291 | 206k | } |
7292 | 106k | else { |
7293 | 106k | si->slice_segment_address = 0; |
7294 | 106k | } |
7295 | | |
7296 | 312k | if (!si->dependent_slice_segment_flag) { |
7297 | 307k | Bool deblocking_filter_override_flag = 0; |
7298 | 307k | Bool slice_temporal_mvp_enabled_flag = 0; |
7299 | 307k | Bool slice_sao_luma_flag = 0; |
7300 | 307k | Bool slice_sao_chroma_flag = 0; |
7301 | 307k | Bool slice_deblocking_filter_disabled_flag = 0; |
7302 | 307k | si->st_rps = NULL; |
7303 | 307k | si->nb_reference_pocs = 0; |
7304 | 307k | si->nb_lt_ref_pics = 0; |
7305 | | |
7306 | | //"slice_reserved_undetermined_flag[]" |
7307 | 307k | gf_bs_read_int_log(bs, pps->num_extra_slice_header_bits, "slice_reserved_undetermined_flag"); |
7308 | | |
7309 | 307k | si->slice_type = gf_bs_read_ue_log(bs, "slice_type"); |
7310 | | |
7311 | 307k | if (pps->output_flag_present_flag) |
7312 | 152k | gf_bs_read_int_log(bs, 1, "pic_output_flag"); |
7313 | | |
7314 | 307k | if (sps->separate_colour_plane_flag == 1) |
7315 | 1.41k | gf_bs_read_int_log(bs, 2, "colour_plane_id"); |
7316 | | |
7317 | 307k | if (IDRPicFlag) { |
7318 | 12.7k | si->poc_lsb = 0; |
7319 | | |
7320 | | //if not asked to parse full header, abort since we know the poc |
7321 | 12.7k | if (!hevc->full_slice_header_parse) return 0; |
7322 | | |
7323 | 12.7k | } |
7324 | 294k | else { |
7325 | 294k | si->poc_lsb = gf_bs_read_int_log(bs, sps->log2_max_pic_order_cnt_lsb, "poc_lsb"); |
7326 | | |
7327 | | //if not asked to parse full header, abort once we have the poc |
7328 | 294k | if (!hevc->full_slice_header_parse) return 0; |
7329 | | |
7330 | 104k | u32 st_ref_idx = 0; |
7331 | 104k | if (gf_bs_read_int_log(bs, 1, "short_term_ref_pic_set_sps_flag") == 0) { |
7332 | 61.1k | Bool ret = hevc_parse_short_term_ref_pic_set(bs, sps, sps->num_short_term_ref_pic_sets); |
7333 | 61.1k | if (!ret) |
7334 | 13.0k | return -1; |
7335 | 48.0k | st_ref_idx = sps->num_short_term_ref_pic_sets; |
7336 | 48.0k | } |
7337 | 43.3k | else if (sps->num_short_term_ref_pic_sets > 1) { |
7338 | 4.29k | u32 numbits = 0; |
7339 | | |
7340 | 8.58k | while ((u32)(1 << numbits) < sps->num_short_term_ref_pic_sets) |
7341 | 4.29k | numbits++; |
7342 | 4.29k | if (numbits > 0) |
7343 | 4.29k | st_ref_idx = gf_bs_read_int_log(bs, numbits, "short_term_ref_pic_set_idx"); |
7344 | 4.29k | } |
7345 | 91.3k | si->st_rps = &sps->rps[st_ref_idx]; |
7346 | 91.3k | si->st_rps->modif_flag_l0 = si->st_rps->modif_flag_l1 = 0; |
7347 | | |
7348 | 91.3k | if (sps->long_term_ref_pics_present_flag) { |
7349 | 23.4k | u8 DeltaPocMsbCycleLt[32]; |
7350 | 23.4k | u32 num_long_term_sps = 0; |
7351 | 23.4k | u32 num_long_term_pics = 0; |
7352 | | |
7353 | 23.4k | memset(DeltaPocMsbCycleLt, 0, sizeof(u8) * 32); |
7354 | | |
7355 | 23.4k | if (sps->num_long_term_ref_pic_sps > 0) { |
7356 | 6.89k | num_long_term_sps = gf_bs_read_ue_log(bs, "num_long_term_sps"); |
7357 | 6.89k | } |
7358 | 23.4k | num_long_term_pics = gf_bs_read_ue_log(bs, "num_long_term_pics"); |
7359 | 23.4k | if (num_long_term_sps+num_long_term_pics>32) return -1; |
7360 | | |
7361 | 21.5k | si->nb_lt_ref_pics = num_long_term_sps + num_long_term_pics; |
7362 | | |
7363 | 66.4k | for (i = 0; i < si->nb_lt_ref_pics; i++) { |
7364 | 44.9k | if (i < num_long_term_sps) { |
7365 | 8.64k | if (sps->num_long_term_ref_pic_sps > 1) |
7366 | 1.60k | gf_bs_read_int_log_idx(bs, gf_get_bit_size(sps->num_long_term_ref_pic_sps), "lt_idx_sps", i); |
7367 | 8.64k | } |
7368 | 36.2k | else { |
7369 | 36.2k | gf_bs_read_int_log_idx(bs, sps->log2_max_pic_order_cnt_lsb, "PocLsbLt", i); |
7370 | 36.2k | gf_bs_read_int_log_idx(bs, 1, "UsedByCurrPicLt", i); |
7371 | 36.2k | } |
7372 | 44.9k | if (gf_bs_read_int_log_idx(bs, 1, "delta_poc_msb_present_flag", i)) { |
7373 | 9.47k | if (i == 0 || i == num_long_term_sps) |
7374 | 4.41k | DeltaPocMsbCycleLt[i] = gf_bs_read_ue_log_idx(bs, "DeltaPocMsbCycleLt", i); |
7375 | 5.06k | else |
7376 | 5.06k | DeltaPocMsbCycleLt[i] = gf_bs_read_ue_log_idx(bs, "DeltaPocMsbCycleLt", i) + DeltaPocMsbCycleLt[i - 1]; |
7377 | 9.47k | } |
7378 | 44.9k | } |
7379 | 21.5k | } |
7380 | 89.3k | if (sps->temporal_mvp_enable_flag) |
7381 | 15.0k | slice_temporal_mvp_enabled_flag = gf_bs_read_int_log(bs, 1, "slice_temporal_mvp_enabled_flag"); |
7382 | 89.3k | } |
7383 | 93.7k | if (sps->sample_adaptive_offset_enabled_flag) { |
7384 | 9.71k | u32 ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc; |
7385 | 9.71k | slice_sao_luma_flag = gf_bs_read_int_log(bs, 1, "slice_sao_luma_flag"); |
7386 | 9.71k | if (ChromaArrayType != 0) |
7387 | 3.47k | slice_sao_chroma_flag = gf_bs_read_int_log(bs, 1, "slice_sao_chroma_flag"); |
7388 | 9.71k | } |
7389 | | |
7390 | 93.7k | if (si->slice_type == GF_HEVC_SLICE_TYPE_P || si->slice_type == GF_HEVC_SLICE_TYPE_B) { |
7391 | | //u32 NumPocTotalCurr; |
7392 | 65.9k | si->num_ref_idx_l0_active = pps->num_ref_idx_l0_default_active; |
7393 | 65.9k | si->num_ref_idx_l1_active = 0; |
7394 | 65.9k | if (si->slice_type == GF_HEVC_SLICE_TYPE_B) |
7395 | 46.3k | si->num_ref_idx_l1_active = pps->num_ref_idx_l1_default_active; |
7396 | | |
7397 | 65.9k | if (gf_bs_read_int_log(bs, 1, "num_ref_idx_active_override_flag")) { |
7398 | 24.0k | si->num_ref_idx_l0_active = 1 + gf_bs_read_ue_log(bs, "num_ref_idx_l0_active"); |
7399 | 24.0k | if (si->slice_type == GF_HEVC_SLICE_TYPE_B) |
7400 | 22.3k | si->num_ref_idx_l1_active = 1 + gf_bs_read_ue_log(bs, "num_ref_idx_l1_active"); |
7401 | 24.0k | } |
7402 | | |
7403 | 65.9k | if (pps->lists_modification_present_flag && si->st_rps) { |
7404 | 11.6k | u32 NumPicTotalCurr = 0; |
7405 | 11.6k | HEVC_ReferencePictureSets *rps = si->st_rps; |
7406 | 35.7k | for (i=0; i < rps->num_negative_pics; i++) { |
7407 | 24.1k | if (rps->used_by_curr_pic[i]) NumPicTotalCurr++; |
7408 | 24.1k | } |
7409 | 36.7k | for (; i < rps->num_negative_pics+rps->num_positive_pics; i++) { |
7410 | 25.1k | if (rps->used_by_curr_pic[i]) NumPicTotalCurr++; |
7411 | 25.1k | } |
7412 | | //TODO long term pics !! |
7413 | 11.6k | if (pps->curr_pic_ref_enabled_flag) |
7414 | 499 | NumPicTotalCurr++; |
7415 | | |
7416 | 11.6k | if (NumPicTotalCurr>1) { |
7417 | 3.15k | hevc_ref_pic_lists_modification(bs, rps, si->slice_type, si->num_ref_idx_l0_active, si->num_ref_idx_l1_active, NumPicTotalCurr); |
7418 | 3.15k | } |
7419 | 11.6k | } |
7420 | | |
7421 | 65.9k | if (si->slice_type == GF_HEVC_SLICE_TYPE_B) |
7422 | 46.3k | gf_bs_read_int_log(bs, 1, "mvd_l1_zero_flag"); |
7423 | 65.9k | if (pps->cabac_init_present_flag) |
7424 | 14.0k | gf_bs_read_int_log(bs, 1, "cabac_init_flag"); |
7425 | | |
7426 | 65.9k | if (slice_temporal_mvp_enabled_flag) { |
7427 | | // When collocated_from_l0_flag is not present, it is inferred to be equal to 1. |
7428 | 5.16k | Bool collocated_from_l0_flag = 1; |
7429 | 5.16k | if (si->slice_type == GF_HEVC_SLICE_TYPE_B) |
7430 | 4.70k | collocated_from_l0_flag = gf_bs_read_int_log(bs, 1, "collocated_from_l0_flag"); |
7431 | | |
7432 | 5.16k | if ((collocated_from_l0_flag && (si->num_ref_idx_l0_active > 1)) |
7433 | 5.16k | || (!collocated_from_l0_flag && (si->num_ref_idx_l1_active > 1)) |
7434 | 5.16k | ) { |
7435 | 893 | gf_bs_read_ue_log(bs, "collocated_ref_idx"); |
7436 | 893 | } |
7437 | 5.16k | } |
7438 | | |
7439 | 65.9k | if ((pps->weighted_pred_flag && si->slice_type == GF_HEVC_SLICE_TYPE_P) |
7440 | 65.9k | || (pps->weighted_bipred_flag && si->slice_type == GF_HEVC_SLICE_TYPE_B) |
7441 | 65.9k | ) { |
7442 | 39.0k | hevc_pred_weight_table(bs, hevc, si, pps, sps, si->num_ref_idx_l0_active, si->num_ref_idx_l1_active); |
7443 | 39.0k | } |
7444 | 65.9k | gf_bs_read_ue_log(bs, "five_minus_max_num_merge_cand"); |
7445 | 65.9k | } |
7446 | 93.7k | si->slice_qp_delta_start_bits = (s32) (gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs); |
7447 | 93.7k | si->slice_qp_delta = gf_bs_read_se_log(bs, "slice_qp_delta"); |
7448 | | |
7449 | 93.7k | if (pps->slice_chroma_qp_offsets_present_flag) { |
7450 | 48.8k | gf_bs_read_se_log(bs, "slice_cb_qp_offset"); |
7451 | 48.8k | gf_bs_read_se_log(bs, "slice_cr_qp_offset"); |
7452 | 48.8k | } |
7453 | 93.7k | if (pps->deblocking_filter_override_enabled_flag) { |
7454 | 16.7k | deblocking_filter_override_flag = gf_bs_read_int_log(bs, 1, "deblocking_filter_override_flag"); |
7455 | 16.7k | } |
7456 | | |
7457 | 93.7k | if (deblocking_filter_override_flag) { |
7458 | 4.81k | slice_deblocking_filter_disabled_flag = gf_bs_read_int_log(bs, 1, "slice_deblocking_filter_disabled_flag"); |
7459 | 4.81k | if (!slice_deblocking_filter_disabled_flag) { |
7460 | 1.43k | gf_bs_read_se_log(bs, "slice_beta_offset_div2"); |
7461 | 1.43k | gf_bs_read_se_log(bs, "slice_tc_offset_div2"); |
7462 | 1.43k | } |
7463 | 4.81k | } |
7464 | 93.7k | if (pps->loop_filter_across_slices_enabled_flag |
7465 | 93.7k | && (slice_sao_luma_flag || slice_sao_chroma_flag || !slice_deblocking_filter_disabled_flag) |
7466 | 93.7k | ) { |
7467 | 39.7k | gf_bs_read_int_log(bs, 1, "slice_loop_filter_across_slices_enabled_flag"); |
7468 | 39.7k | } |
7469 | 93.7k | } |
7470 | | //dependent slice segment |
7471 | 5.67k | else { |
7472 | | //if not asked to parse full header, abort |
7473 | 5.67k | if (!hevc->full_slice_header_parse) return 0; |
7474 | 5.67k | } |
7475 | | |
7476 | 95.6k | si->entry_point_start_bits = ((u32)gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs); |
7477 | | |
7478 | 95.6k | if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) { |
7479 | 48.6k | u32 num_entry_point_offsets = gf_bs_read_ue_log(bs, "num_entry_point_offsets"); |
7480 | 48.6k | if (num_entry_point_offsets > 0) { |
7481 | 6.92k | u32 offset = gf_bs_read_ue_log(bs, "offset") + 1; |
7482 | 6.92k | u32 segments = offset >> 4; |
7483 | 6.92k | s32 remain = (offset & 15); |
7484 | | |
7485 | 76.2M | for (i = 0; i < num_entry_point_offsets; i++) { |
7486 | | //u32 res = 0; |
7487 | 100M | for (j = 0; j < segments; j++) { |
7488 | | //res <<= 16; |
7489 | 24.6M | /*res +=*/ gf_bs_read_int(bs, 16); |
7490 | 24.6M | } |
7491 | 76.2M | if (remain) { |
7492 | | //res <<= remain; |
7493 | 76.1M | /* res += */ gf_bs_read_int(bs, remain); |
7494 | 76.1M | } |
7495 | | // entry_point_offset = val + 1; // +1; // +1 to get the size |
7496 | 76.2M | } |
7497 | 6.92k | } |
7498 | 48.6k | } |
7499 | | |
7500 | 95.6k | if (pps->slice_segment_header_extension_present_flag) { |
7501 | 33.5k | u32 size_ext = gf_bs_read_ue_log(bs, "size_ext"); |
7502 | 3.08M | while (size_ext) { |
7503 | 3.04M | gf_bs_read_int(bs, 8); |
7504 | 3.04M | size_ext--; |
7505 | 3.04M | } |
7506 | 33.5k | } |
7507 | | |
7508 | 95.6k | si->header_size_bits = (gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs); // av_parser.c modified on 16 jan. 2019 |
7509 | | |
7510 | 95.6k | if (gf_bs_read_int_log(bs, 1, "byte_align") == 0) { |
7511 | 64.2k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("Error parsing slice header: byte_align not found at end of header !\n")); |
7512 | 64.2k | } |
7513 | | |
7514 | 95.6k | gf_bs_align(bs); |
7515 | 95.6k | si->payload_start_offset = (s32)gf_bs_get_position(bs); |
7516 | 95.6k | return 0; |
7517 | 312k | } |
7518 | | |
7519 | | static void gf_hevc_push_ref_poc(HEVCSliceInfo *si, s32 poc) |
7520 | 39.9M | { |
7521 | 39.9M | u32 i; |
7522 | 107M | for (i=0;i<si->nb_reference_pocs; i++) { |
7523 | 107M | if (si->reference_pocs[i]==poc) return; |
7524 | 107M | } |
7525 | 40.9k | if (si->nb_reference_pocs==GF_ARRAY_LENGTH(si->reference_pocs)) return; |
7526 | 40.9k | si->reference_pocs[si->nb_reference_pocs] = poc; |
7527 | 40.9k | si->nb_reference_pocs++; |
7528 | 40.9k | } |
7529 | | |
7530 | | static void gf_hevc_compute_ref_list(HEVCState *hevc, HEVCSliceInfo *si) |
7531 | 95.6k | { |
7532 | 95.6k | u32 i; |
7533 | 95.6k | HEVC_ReferencePictureSets *rps = si->st_rps; |
7534 | | |
7535 | 95.6k | if (si->slice_type == GF_HEVC_SLICE_TYPE_I) |
7536 | 7.01k | return; |
7537 | | |
7538 | 88.6k | if (!rps) return; |
7539 | 84.3k | if (si->nb_lt_ref_pics) { |
7540 | 7.97k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Long-term picture not yet supported for refPicList parsing\n")); |
7541 | 7.97k | si->nb_reference_pocs = 0; |
7542 | 7.97k | return; |
7543 | 7.97k | } |
7544 | | |
7545 | 76.4k | u32 nb_poc_st_curr0=0; |
7546 | 76.4k | s32 poc_st_curr0[16]; |
7547 | 76.4k | u32 nb_poc_st_curr1=0; |
7548 | 76.4k | s32 poc_st_curr1[16]; |
7549 | 76.4k | u32 nb_poc_lt_curr=0; |
7550 | 76.4k | s32 poc_lt_curr[16]; |
7551 | 256k | for (i=0; i < rps->num_negative_pics; i++) { |
7552 | 179k | if (i>=GF_ARRAY_LENGTH(rps->used_by_curr_pic) || i>=GF_ARRAY_LENGTH(rps->delta_poc) || nb_poc_st_curr0>=GF_ARRAY_LENGTH(poc_st_curr0)) |
7553 | 0 | break; |
7554 | 179k | if (!rps->used_by_curr_pic[i]) continue; |
7555 | 32.7k | poc_st_curr0[nb_poc_st_curr0] = si->poc + rps->delta_poc[i]; |
7556 | 32.7k | nb_poc_st_curr0++; |
7557 | 32.7k | } |
7558 | | |
7559 | 199k | for (; i < rps->num_negative_pics+rps->num_positive_pics; i++) { |
7560 | 123k | if (i>=GF_ARRAY_LENGTH(rps->used_by_curr_pic) || i>=GF_ARRAY_LENGTH(rps->delta_poc) || nb_poc_st_curr1>=GF_ARRAY_LENGTH(poc_st_curr1)) |
7561 | 0 | break; |
7562 | 123k | if (!rps->used_by_curr_pic[i]) continue; |
7563 | 41.4k | poc_st_curr1[nb_poc_st_curr1] = si->poc + rps->delta_poc[i]; |
7564 | 41.4k | nb_poc_st_curr1++; |
7565 | 41.4k | } |
7566 | | //todo long term and multi layer |
7567 | 76.4k | u32 num_long_term_pictures = 0; |
7568 | 76.4k | u32 num_interlayer_ref_idx = 0; |
7569 | 76.4k | for (i = rps->num_negative_pics + rps->num_positive_pics + num_long_term_pictures - 1; i >rps->num_negative_pics + rps->num_positive_pics-1 ; i--) { |
7570 | 0 | if (i>=GF_ARRAY_LENGTH(rps->used_by_curr_pic) || nb_poc_lt_curr>=GF_ARRAY_LENGTH(poc_lt_curr)) |
7571 | 0 | break; |
7572 | 0 | if (!rps->used_by_curr_pic[i]) continue; |
7573 | 0 | poc_lt_curr[nb_poc_lt_curr] = 0; //todo, get LT from SH |
7574 | 0 | nb_poc_lt_curr++; |
7575 | 0 | } |
7576 | | //compute deps |
7577 | 76.4k | u32 num_poc_total = nb_poc_st_curr0 + nb_poc_st_curr1 + nb_poc_lt_curr + num_interlayer_ref_idx; |
7578 | | //build L0 |
7579 | 76.4k | s32 ref_pocs_l0[32]; |
7580 | 76.4k | u32 nb_poc_l0 = 0; |
7581 | 109k | for (i=0; i<MIN(nb_poc_st_curr0, GF_ARRAY_LENGTH(ref_pocs_l0)); i++, nb_poc_l0++) { |
7582 | 32.7k | ref_pocs_l0[nb_poc_l0] = poc_st_curr0[i]; |
7583 | 32.7k | } |
7584 | 117k | for ( i=0; i<MIN(nb_poc_st_curr1, GF_ARRAY_LENGTH(ref_pocs_l0)); i++, nb_poc_l0++) { |
7585 | 41.4k | ref_pocs_l0[nb_poc_l0] = poc_st_curr1[i]; |
7586 | 41.4k | } |
7587 | 76.4k | for (i=0; i<MIN(nb_poc_lt_curr, GF_ARRAY_LENGTH(ref_pocs_l0)); i++, nb_poc_l0++) { |
7588 | 0 | ref_pocs_l0[nb_poc_l0] = poc_lt_curr[i]; |
7589 | 0 | } |
7590 | 76.4k | assert(nb_poc_l0 == num_poc_total); |
7591 | | |
7592 | | //no need to compute L1, same IDs as in L0 |
7593 | 76.4k | s32 ref_pocs_l1[32]; |
7594 | 76.4k | u32 nb_poc_l1 = 0; |
7595 | 76.4k | if (si->slice_type == GF_HEVC_SLICE_TYPE_B) { |
7596 | 63.5k | for ( i=0; i<nb_poc_st_curr1; i++, nb_poc_l1++) { |
7597 | 26.3k | if (i>=GF_ARRAY_LENGTH(poc_st_curr1) || nb_poc_l1>=GF_ARRAY_LENGTH(ref_pocs_l1)) |
7598 | 0 | break; |
7599 | 26.3k | ref_pocs_l1[nb_poc_l1] = poc_st_curr1[i]; |
7600 | 26.3k | } |
7601 | 57.2k | for ( i=0; i<nb_poc_st_curr0; i++, nb_poc_l1++) { |
7602 | 19.9k | if (i>=GF_ARRAY_LENGTH(poc_st_curr0) || nb_poc_l1>=GF_ARRAY_LENGTH(ref_pocs_l1)) |
7603 | 0 | break; |
7604 | 19.9k | ref_pocs_l1[nb_poc_l1] = poc_st_curr0[i]; |
7605 | 19.9k | } |
7606 | 37.2k | for ( i=0; i<nb_poc_lt_curr; i++, nb_poc_l1++) { |
7607 | 0 | if (i>=GF_ARRAY_LENGTH(poc_lt_curr) || nb_poc_l1>=GF_ARRAY_LENGTH(ref_pocs_l1)) |
7608 | 0 | break; |
7609 | 0 | ref_pocs_l1[nb_poc_l1] = poc_lt_curr[i]; |
7610 | 0 | } |
7611 | 37.2k | assert(nb_poc_l1 == num_poc_total); |
7612 | 37.2k | } |
7613 | 76.4k | if (rps->modif_flag_l0 || num_poc_total) { |
7614 | 9.86M | for (i=0; i<si->num_ref_idx_l0_active; i++) { |
7615 | 9.83M | u32 idx = (rps->modif_flag_l0 && i<GF_ARRAY_LENGTH(rps->modif_idx_l0)) ? rps->modif_idx_l0[i] : (i%num_poc_total); |
7616 | 9.83M | if (idx < GF_ARRAY_LENGTH(ref_pocs_l0)) |
7617 | 9.83M | gf_hevc_push_ref_poc(si, ref_pocs_l0[idx]); |
7618 | 9.83M | } |
7619 | 29.6k | } |
7620 | 76.4k | if (rps->modif_flag_l1 || num_poc_total) { |
7621 | 30.1M | for (i=0; i<si->num_ref_idx_l1_active; i++) { |
7622 | 30.0M | u32 idx = (rps->modif_flag_l1 && i<GF_ARRAY_LENGTH(rps->modif_idx_l1)) ? rps->modif_idx_l1[i] : (i%num_poc_total); |
7623 | 30.0M | if (idx < GF_ARRAY_LENGTH(ref_pocs_l1)) |
7624 | 30.0M | gf_hevc_push_ref_poc(si, ref_pocs_l1[idx]); |
7625 | 30.0M | } |
7626 | 29.6k | } |
7627 | 76.4k | } |
7628 | | |
7629 | | static void gf_hevc_vvc_parse_sei(char *buffer, u32 nal_size, HEVCState *hevc, VVCState *vvc) |
7630 | 7.53k | { |
7631 | 7.53k | u32 ptype, psize, hdr, i; |
7632 | 7.53k | u8 *dst_ptr; |
7633 | 7.53k | u64 start; |
7634 | 7.53k | GF_BitStream *bs; |
7635 | | |
7636 | 7.53k | hdr = buffer[0]; |
7637 | 7.53k | if (((hdr & 0x7e) >> 1) != GF_HEVC_NALU_SEI_PREFIX) return; |
7638 | | |
7639 | 5.30k | bs = gf_bs_new(buffer, nal_size, GF_BITSTREAM_READ); |
7640 | 5.30k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
7641 | | |
7642 | 5.30k | gf_bs_read_int(bs, 16); |
7643 | | |
7644 | | /*parse SEI*/ |
7645 | 40.8k | while (gf_bs_available(bs)) { |
7646 | 38.2k | u32 consumed, nb_zeros; |
7647 | 38.2k | ptype = 0; |
7648 | 48.0k | while (gf_bs_peek_bits(bs, 8, 0)==0xFF) { |
7649 | 9.83k | gf_bs_read_int(bs, 8); |
7650 | 9.83k | ptype += 255; |
7651 | 9.83k | } |
7652 | 38.2k | ptype += gf_bs_read_int(bs, 8); |
7653 | 38.2k | psize = 0; |
7654 | 38.6k | while (gf_bs_peek_bits(bs, 8, 0)==0xFF) { |
7655 | 444 | gf_bs_read_int(bs, 8); |
7656 | 444 | psize += 255; |
7657 | 444 | } |
7658 | 38.2k | psize += gf_bs_read_int(bs, 8); |
7659 | | |
7660 | 38.2k | start = gf_bs_get_position(bs); |
7661 | 38.2k | if (start+psize >= nal_size) { |
7662 | 2.46k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[%s] SEI user message type %d size error (%d but %d remain), skipping SEI message\n", hevc ? "HEVC" : "VVC", ptype, psize, nal_size-start)); |
7663 | 2.46k | break; |
7664 | 2.46k | } |
7665 | | |
7666 | 35.7k | nb_zeros = gf_bs_get_emulation_byte_removed(bs); |
7667 | 35.7k | if (hevc) { |
7668 | 35.7k | hevc->has_3d_ref_disp_info = 0; |
7669 | 35.7k | } |
7670 | 35.7k | switch (ptype) { |
7671 | 170 | case 4: /*user registered ITU-T T35*/ |
7672 | 170 | if (hevc) { |
7673 | 170 | avc_parse_itu_t_t35_sei(bs, &hevc->sei.dovi); |
7674 | 170 | } |
7675 | 170 | break; |
7676 | | //clli |
7677 | 433 | case 144: |
7678 | 433 | dst_ptr = hevc ? hevc->clli_data : vvc->clli_data; |
7679 | | //do not use read data due to possible EPB |
7680 | 2.16k | for (i=0; i<4; i++) |
7681 | 1.73k | dst_ptr[i] = gf_bs_read_u8(bs); |
7682 | | |
7683 | 433 | if (hevc) { |
7684 | 433 | hevc->clli_valid = 1; |
7685 | 433 | } else { |
7686 | 0 | vvc->clli_valid = 1; |
7687 | 0 | } |
7688 | 433 | break; |
7689 | | //mdcv |
7690 | 575 | case 137: |
7691 | 575 | dst_ptr = hevc ? hevc->mdcv_data : vvc->mdcv_data; |
7692 | | //do not use read data due to possible EPB |
7693 | 14.3k | for (i=0; i<24; i++) |
7694 | 13.8k | dst_ptr[i] = gf_bs_read_u8(bs); |
7695 | | |
7696 | 575 | if (hevc) { |
7697 | 575 | hevc->mdcv_valid = 1; |
7698 | 575 | } else { |
7699 | 0 | vvc->mdcv_valid = 1; |
7700 | 0 | } |
7701 | 575 | break; |
7702 | | // three_dimensional_reference_displays_info |
7703 | 90 | case 176: |
7704 | 90 | if (hevc) { |
7705 | 90 | hevc->has_3d_ref_disp_info = 1; |
7706 | 90 | } |
7707 | 90 | break; |
7708 | 34.4k | default: |
7709 | 34.4k | break; |
7710 | 35.7k | } |
7711 | 35.7k | nb_zeros = gf_bs_get_emulation_byte_removed(bs) - nb_zeros; |
7712 | | |
7713 | 35.7k | gf_bs_align(bs); |
7714 | 35.7k | consumed = (u32) (gf_bs_get_position(bs) - start); |
7715 | 35.7k | consumed -= nb_zeros; |
7716 | 35.7k | psize-=consumed; |
7717 | | //do not use skip bytes due to possible EPB |
7718 | 1.26M | while (psize) { |
7719 | 1.22M | gf_bs_read_u8(bs); |
7720 | 1.22M | psize--; |
7721 | 1.22M | } |
7722 | 35.7k | if (gf_bs_available(bs) <= 2) |
7723 | 202 | break; |
7724 | 35.7k | } |
7725 | 5.30k | gf_bs_del(bs); |
7726 | 5.30k | } |
7727 | | |
7728 | | void gf_hevc_parse_sei(char *buffer, u32 nal_size, HEVCState *hevc) |
7729 | 5.30k | { |
7730 | 5.30k | gf_hevc_vvc_parse_sei(buffer, nal_size, hevc, NULL); |
7731 | 5.30k | } |
7732 | | |
7733 | | static void hevc_compute_poc(HEVCSliceInfo *si) |
7734 | 595k | { |
7735 | 595k | u32 max_poc_lsb = 1 << (si->sps->log2_max_pic_order_cnt_lsb); |
7736 | | |
7737 | | /*POC reset for IDR frames, NOT for CRA*/ |
7738 | 595k | switch (si->nal_unit_type) { |
7739 | 12.3k | case GF_HEVC_NALU_SLICE_IDR_W_DLP: |
7740 | 25.5k | case GF_HEVC_NALU_SLICE_IDR_N_LP: |
7741 | 25.5k | si->poc_lsb_prev = 0; |
7742 | 25.5k | si->poc_msb_prev = 0; |
7743 | 25.5k | break; |
7744 | 595k | } |
7745 | | |
7746 | 595k | if ((si->poc_lsb < si->poc_lsb_prev) && (si->poc_lsb_prev - si->poc_lsb >= max_poc_lsb / 2)) |
7747 | 46.3k | si->poc_msb = si->poc_msb_prev + max_poc_lsb; |
7748 | 549k | else if ((si->poc_lsb > si->poc_lsb_prev) && (si->poc_lsb - si->poc_lsb_prev > max_poc_lsb / 2)) |
7749 | 20.7k | si->poc_msb = si->poc_msb_prev - max_poc_lsb; |
7750 | 528k | else |
7751 | 528k | si->poc_msb = si->poc_msb_prev; |
7752 | | |
7753 | 595k | switch (si->nal_unit_type) { |
7754 | 7.96k | case GF_HEVC_NALU_SLICE_BLA_W_LP: |
7755 | 14.8k | case GF_HEVC_NALU_SLICE_BLA_W_DLP: |
7756 | 56.0k | case GF_HEVC_NALU_SLICE_BLA_N_LP: |
7757 | 56.0k | si->poc_msb = 0; |
7758 | 56.0k | break; |
7759 | 595k | } |
7760 | 595k | si->poc = si->poc_msb + si->poc_lsb; |
7761 | 595k | } |
7762 | | |
7763 | | |
7764 | | static Bool hevc_parse_nal_header(GF_BitStream *bs, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id) |
7765 | 744k | { |
7766 | 744k | u32 val; |
7767 | 744k | val = gf_bs_read_int_log(bs, 1, "forbidden_zero"); |
7768 | 744k | if (val) return GF_FALSE; |
7769 | | |
7770 | 713k | val = gf_bs_read_int_log(bs, 6, "nuh_type"); |
7771 | 713k | if (nal_unit_type) *nal_unit_type = val; |
7772 | | |
7773 | 713k | val = gf_bs_read_int_log(bs, 6, "layerID"); |
7774 | 713k | if (layer_id) *layer_id = val; |
7775 | | |
7776 | 713k | val = gf_bs_read_int_log(bs, 3, "temporalID"); |
7777 | 713k | if (!val) |
7778 | 108k | return GF_FALSE; |
7779 | 604k | val -= 1; |
7780 | 604k | if (temporal_id) *temporal_id = val; |
7781 | 604k | return GF_TRUE; |
7782 | 713k | } |
7783 | | |
7784 | | |
7785 | | void hevc_profile_tier_level(GF_BitStream *bs, Bool ProfilePresentFlag, u8 MaxNumSubLayersMinus1, HEVC_ProfileTierLevel *ptl, u32 idx) |
7786 | 59.0k | { |
7787 | 59.0k | u32 i; |
7788 | 59.0k | if (ProfilePresentFlag) { |
7789 | 56.9k | ptl->profile_space = gf_bs_read_int_log_idx(bs, 2, "profile_space", idx); |
7790 | 56.9k | ptl->tier_flag = gf_bs_read_int_log_idx(bs, 1, "tier_flag", idx); |
7791 | 56.9k | ptl->profile_idc = gf_bs_read_int_log_idx(bs, 5, "profile_idc", idx); |
7792 | | |
7793 | 56.9k | ptl->profile_compatibility_flag = gf_bs_read_int_log_idx(bs, 32, "profile_compatibility_flag", idx); |
7794 | | |
7795 | 56.9k | ptl->general_progressive_source_flag = gf_bs_read_int_log_idx(bs, 1, "general_progressive_source_flag", idx); |
7796 | 56.9k | ptl->general_interlaced_source_flag = gf_bs_read_int_log_idx(bs, 1, "general_interlaced_source_flag", idx); |
7797 | 56.9k | ptl->general_non_packed_constraint_flag = gf_bs_read_int_log_idx(bs, 1, "general_non_packed_constraint_flag", idx); |
7798 | 56.9k | ptl->general_frame_only_constraint_flag = gf_bs_read_int_log_idx(bs, 1, "general_frame_only_constraint_flag", idx); |
7799 | 56.9k | ptl->general_reserved_44bits = gf_bs_read_long_int(bs, 44); |
7800 | 56.9k | } |
7801 | 59.0k | ptl->level_idc = gf_bs_read_int_log(bs, 8, "level_idc"); |
7802 | 182k | for (i = 0; i < MaxNumSubLayersMinus1; i++) { |
7803 | 123k | ptl->sub_ptl[i].profile_present_flag = gf_bs_read_int_log_idx2(bs, 1, "profile_present_flag", idx, i); |
7804 | 123k | ptl->sub_ptl[i].level_present_flag = gf_bs_read_int_log_idx2(bs, 1, "level_present_flag", idx, i); |
7805 | 123k | } |
7806 | 59.0k | if (MaxNumSubLayersMinus1 > 0) { |
7807 | 173k | for (i = MaxNumSubLayersMinus1; i < 8; i++) { |
7808 | 140k | /*reserved_zero_2bits*/gf_bs_read_int(bs, 2); |
7809 | 140k | } |
7810 | 33.0k | } |
7811 | | |
7812 | 182k | for (i = 0; i < MaxNumSubLayersMinus1; i++) { |
7813 | 123k | if (ptl->sub_ptl[i].profile_present_flag) { |
7814 | 25.3k | ptl->sub_ptl[i].profile_space = gf_bs_read_int_log_idx2(bs, 2, "sublayer_profile_space", idx, i); |
7815 | 25.3k | ptl->sub_ptl[i].tier_flag = gf_bs_read_int_log_idx2(bs, 1, "sublayer_tier_flag", idx, i); |
7816 | 25.3k | ptl->sub_ptl[i].profile_idc = gf_bs_read_int_log_idx2(bs, 5, "sublayer_profile_idc", idx, i); |
7817 | 25.3k | ptl->sub_ptl[i].profile_compatibility_flag = gf_bs_read_int_log_idx2(bs, 32, "sublayer_profile_compatibility_flag", idx, i); |
7818 | 25.3k | /*ptl->sub_ptl[i].progressive_source_flag =*/ gf_bs_read_int_log_idx2(bs, 1, "sublayer_progressive_source_flag", idx, i); |
7819 | 25.3k | /*ptl->sub_ptl[i].interlaced_source_flag =*/ gf_bs_read_int_log_idx2(bs, 1, "sublayer_interlaced_source_flag", idx, i); |
7820 | 25.3k | /*ptl->sub_ptl[i].non_packed_constraint_flag =*/ gf_bs_read_int_log_idx2(bs, 1, "sublayer_non_packed_constraint_flag", idx, i); |
7821 | 25.3k | /*ptl->sub_ptl[i].frame_only_constraint_flag =*/ gf_bs_read_int_log_idx2(bs, 1, "sublayer_frame_only_constraint_flag", idx, i); |
7822 | 25.3k | /*ptl->sub_ptl[i].reserved_44bits =*/ gf_bs_read_long_int(bs, 44); |
7823 | 25.3k | } |
7824 | 123k | if (ptl->sub_ptl[i].level_present_flag) |
7825 | 42.5k | ptl->sub_ptl[i].level_idc = gf_bs_read_int_log_idx2(bs, 8, "sublayer_level_idc", idx, i); |
7826 | 123k | } |
7827 | 59.0k | } |
7828 | | |
7829 | | static u32 scalability_type_to_idx(HEVC_VPS *vps, u32 scalability_type) |
7830 | 32.1k | { |
7831 | 32.1k | u32 idx = 0, type; |
7832 | 64.2k | for (type = 0; type < scalability_type; type++) { |
7833 | 32.1k | idx += (vps->scalability_mask[type] ? 1 : 0); |
7834 | 32.1k | } |
7835 | 32.1k | return idx; |
7836 | 32.1k | } |
7837 | | |
7838 | 53.2k | #define LHVC_VIEW_ORDER_INDEX 1 |
7839 | | #define LHVC_SCALABILITY_INDEX 2 |
7840 | | |
7841 | | static u32 lhvc_get_scalability_id(HEVC_VPS *vps, u32 layer_id_in_vps, u32 scalability_type) |
7842 | 53.2k | { |
7843 | 53.2k | u32 idx; |
7844 | 53.2k | if (!vps->scalability_mask[scalability_type]) return 0; |
7845 | 32.1k | idx = scalability_type_to_idx(vps, scalability_type); |
7846 | 32.1k | return vps->dimension_id[layer_id_in_vps][idx]; |
7847 | 53.2k | } |
7848 | | |
7849 | | static u32 lhvc_get_view_index(HEVC_VPS *vps, u32 id) |
7850 | 26.6k | { |
7851 | 26.6k | return lhvc_get_scalability_id(vps, vps->layer_id_in_vps[id], LHVC_VIEW_ORDER_INDEX); |
7852 | 26.6k | } |
7853 | | |
7854 | | static u32 lhvc_get_num_views(HEVC_VPS *vps) |
7855 | 13.6k | { |
7856 | 13.6k | u32 numViews = 1, i; |
7857 | 53.8k | for (i = 0; i < vps->max_layers; i++) { |
7858 | 40.2k | u32 layer_id = vps->layer_id_in_nuh[i]; |
7859 | 40.2k | if (i > 0 && (lhvc_get_view_index(vps, layer_id) != lhvc_get_scalability_id(vps, i - 1, LHVC_VIEW_ORDER_INDEX))) { |
7860 | 12.6k | numViews++; |
7861 | 12.6k | } |
7862 | 40.2k | } |
7863 | 13.6k | return numViews; |
7864 | 13.6k | } |
7865 | | |
7866 | | static void lhvc_parse_rep_format(HEVC_RepFormat *fmt, GF_BitStream *bs, u32 idx) |
7867 | 8.46k | { |
7868 | 8.46k | u8 chroma_bitdepth_present_flag; |
7869 | 8.46k | fmt->pic_width_luma_samples = gf_bs_read_int_log_idx(bs, 16, "pic_width_luma_samples", idx); |
7870 | 8.46k | fmt->pic_height_luma_samples = gf_bs_read_int_log_idx(bs, 16, "pic_height_luma_samples", idx); |
7871 | 8.46k | chroma_bitdepth_present_flag = gf_bs_read_int_log_idx(bs, 1, "chroma_bitdepth_present_flag", idx); |
7872 | 8.46k | if (chroma_bitdepth_present_flag) { |
7873 | 5.23k | fmt->chroma_format_idc = gf_bs_read_int_log_idx(bs, 2, "chroma_format_idc", idx); |
7874 | | |
7875 | 5.23k | if (fmt->chroma_format_idc == 3) |
7876 | 2.85k | fmt->separate_colour_plane_flag = gf_bs_read_int_log_idx(bs, 1, "separate_colour_plane_flag", idx); |
7877 | 5.23k | fmt->bit_depth_luma = 8 + gf_bs_read_int_log_idx(bs, 4, "bit_depth_luma_minus8", idx); |
7878 | 5.23k | fmt->bit_depth_chroma = 8 + gf_bs_read_int_log_idx(bs, 4, "bit_depth_chroma_minus8", idx); |
7879 | 5.23k | } |
7880 | 8.46k | if (gf_bs_read_int_log_idx(bs, 1, "conformance_window_vps_flag", idx)) { |
7881 | 4.24k | gf_bs_read_ue_log_idx(bs, "conf_win_vps_left_offset", idx); |
7882 | 4.24k | gf_bs_read_ue_log_idx(bs, "conf_win_vps_right_offset", idx); |
7883 | 4.24k | gf_bs_read_ue_log_idx(bs, "conf_win_vps_top_offset", idx); |
7884 | 4.24k | gf_bs_read_ue_log_idx(bs, "conf_win_vps_bottom_offset", idx); |
7885 | 4.24k | } |
7886 | 8.46k | } |
7887 | | |
7888 | | |
7889 | | static Bool hevc_parse_vps_extension(HEVC_VPS *vps, GF_BitStream *bs) |
7890 | 9.04k | { |
7891 | 9.04k | u8 splitting_flag, vps_nuh_layer_id_present_flag, view_id_len; |
7892 | 9.04k | u32 i, j, num_scalability_types, num_add_olss, num_add_layer_set, num_indepentdent_layers, nb_bits, default_output_layer_idc = 0; |
7893 | 9.04k | u8 dimension_id_len[16], dim_bit_offset[16]; |
7894 | 9.04k | u8 /*avc_base_layer_flag, */NumLayerSets, /*default_one_target_output_layer_flag, */rep_format_idx_present_flag, ols_ids_to_ls_idx; |
7895 | 9.04k | u8 layer_set_idx_for_ols_minus1[MAX_LHVC_LAYERS]; |
7896 | 9.04k | u8 nb_output_layers_in_output_layer_set[MAX_LHVC_LAYERS + 1]; |
7897 | 9.04k | u8 ols_highest_output_layer_id[MAX_LHVC_LAYERS + 1]; |
7898 | | |
7899 | 9.04k | u32 k, d, r, p, iNuhLId, jNuhLId; |
7900 | 9.04k | u8 num_direct_ref_layers[64], num_pred_layers[64], num_layers_in_tree_partition[MAX_LHVC_LAYERS]; |
7901 | 9.04k | u8 dependency_flag[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS], id_pred_layers[64][MAX_LHVC_LAYERS]; |
7902 | | // u8 num_ref_layers[64]; |
7903 | | // u8 tree_partition_layer_id[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS]; |
7904 | | // u8 id_ref_layers[64][MAX_LHVC_LAYERS]; |
7905 | | // u8 id_direct_ref_layers[64][MAX_LHVC_LAYERS]; |
7906 | 9.04k | u8 layer_id_in_list_flag[64]; |
7907 | 9.04k | Bool OutputLayerFlag[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS]; |
7908 | | |
7909 | 9.04k | vps->vps_extension_found = 1; |
7910 | 9.04k | if ((vps->max_layers > 1) && vps->base_layer_internal_flag) |
7911 | 1.21k | hevc_profile_tier_level(bs, 0, vps->max_sub_layers - 1, &vps->ext_ptl[0], 0); |
7912 | | |
7913 | 9.04k | splitting_flag = gf_bs_read_int_log(bs, 1, "splitting_flag"); |
7914 | 9.04k | num_scalability_types = 0; |
7915 | 153k | for (i = 0; i < 16; i++) { |
7916 | 144k | vps->scalability_mask[i] = gf_bs_read_int_log_idx(bs, 1, "scalability_mask", i); |
7917 | 144k | num_scalability_types += vps->scalability_mask[i]; |
7918 | 144k | } |
7919 | 9.04k | if (num_scalability_types >= 16) { |
7920 | 312 | num_scalability_types = 16; |
7921 | 312 | } |
7922 | 9.04k | dimension_id_len[0] = 0; |
7923 | 9.04k | if (num_scalability_types) { |
7924 | 60.6k | for (i = 0; i < (num_scalability_types - splitting_flag); i++) { |
7925 | 52.4k | dimension_id_len[i] = 1 + gf_bs_read_int_log_idx(bs, 3, "dimension_id_len_minus1", i); |
7926 | 52.4k | } |
7927 | | |
7928 | 8.16k | if (splitting_flag) { |
7929 | 1.97k | u32 num_bits=0; |
7930 | 13.1k | for (i = 0; i < num_scalability_types-1; i++) { |
7931 | 11.2k | dim_bit_offset[i] = 0; |
7932 | 11.2k | num_bits+=dimension_id_len[i]; |
7933 | 73.1k | for (j = 0; j < i; j++) |
7934 | 61.9k | dim_bit_offset[i] += dimension_id_len[j]; |
7935 | 11.2k | } |
7936 | 1.97k | if (num_bits>=6) { |
7937 | 973 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Too many bits defined for dimension IDs (%d vs 5 max)\n", num_bits)); |
7938 | 973 | return GF_FALSE; |
7939 | 973 | } |
7940 | 999 | dimension_id_len[num_scalability_types - 1] = 6 - num_bits; //1 + (5 - dim_bit_offset[num_scalability_types - 1]); |
7941 | 999 | dim_bit_offset[num_scalability_types - 1] = 6; |
7942 | 999 | } |
7943 | 8.16k | } |
7944 | | |
7945 | 8.07k | vps_nuh_layer_id_present_flag = gf_bs_read_int_log(bs, 1, "vps_nuh_layer_id_present_flag"); |
7946 | 8.07k | vps->layer_id_in_nuh[0] = 0; |
7947 | 8.07k | vps->layer_id_in_vps[0] = 0; |
7948 | 22.5k | for (i = 1; i < vps->max_layers; i++) { |
7949 | 15.5k | if (vps_nuh_layer_id_present_flag) { |
7950 | 1.92k | vps->layer_id_in_nuh[i] = gf_bs_read_int_log_idx(bs, 6, "layer_id_in_nuh", i); |
7951 | 1.92k | } |
7952 | 13.6k | else { |
7953 | 13.6k | vps->layer_id_in_nuh[i] = i; |
7954 | 13.6k | } |
7955 | 15.5k | if (vps->layer_id_in_nuh[i] >= MAX_LHVC_LAYERS) { |
7956 | 1.05k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] %d layers in VPS ext but only %d supported in GPAC\n", 1+vps->layer_id_in_nuh[i], MAX_LHVC_LAYERS)); |
7957 | 1.05k | vps->layer_id_in_nuh[i] = 0; |
7958 | 1.05k | return GF_FALSE; |
7959 | 1.05k | } |
7960 | 14.4k | vps->layer_id_in_vps[vps->layer_id_in_nuh[i]] = i; |
7961 | | |
7962 | 14.4k | if (!splitting_flag) { |
7963 | 89.4k | for (j = 0; j < num_scalability_types; j++) { |
7964 | 74.9k | vps->dimension_id[i][j] = gf_bs_read_int_log_idx2(bs, dimension_id_len[j], "dimension_id", i, j); |
7965 | 74.9k | } |
7966 | 14.4k | } |
7967 | 14.4k | } |
7968 | | |
7969 | 7.02k | if (splitting_flag) { |
7970 | 1.01k | if (num_scalability_types==16) |
7971 | 0 | return GF_FALSE; |
7972 | 2.02k | for (i = 0; i < vps->max_layers; i++) |
7973 | 2.02k | for (j = 0; j < num_scalability_types; j++) { |
7974 | 1.00k | if (dim_bit_offset[j + 1] <= 31) |
7975 | 1.00k | vps->dimension_id[i][j] = ((vps->layer_id_in_nuh[i] & ((1 << dim_bit_offset[j + 1]) - 1)) >> dim_bit_offset[j]); |
7976 | 1 | else |
7977 | 1 | return GF_FALSE; |
7978 | 1.00k | } |
7979 | 1.01k | } |
7980 | 6.01k | else { |
7981 | 40.8k | for (j = 0; j < num_scalability_types; j++) |
7982 | 34.8k | vps->dimension_id[0][j] = 0; |
7983 | 6.01k | } |
7984 | | |
7985 | 7.02k | view_id_len = gf_bs_read_int_log(bs, 4, "view_id_len"); |
7986 | 7.02k | if (view_id_len > 0) { |
7987 | 13.6k | for (i = 0; i < lhvc_get_num_views(vps); i++) { |
7988 | 8.32k | gf_bs_read_int_log_idx(bs, view_id_len, "view_id_val", i); |
7989 | 8.32k | } |
7990 | 5.29k | } |
7991 | | |
7992 | 20.6k | for (i = 1; i < vps->max_layers; i++) { |
7993 | 37.4k | for (j = 0; j < i; j++) { |
7994 | 23.8k | vps->direct_dependency_flag[i][j] = gf_bs_read_int_log_idx(bs, 1, "direct_dependency_flag", i); |
7995 | 23.8k | } |
7996 | 13.6k | } |
7997 | | |
7998 | | //we do the test on MAX_LHVC_LAYERS and break in the loop to avoid a wrong GCC 4.8 warning on array bounds |
7999 | 27.6k | for (i = 0; i < MAX_LHVC_LAYERS; i++) { |
8000 | 25.3k | if (i >= vps->max_layers) break; |
8001 | 88.9k | for (j = 0; j < vps->max_layers; j++) { |
8002 | 68.3k | dependency_flag[i][j] = vps->direct_dependency_flag[i][j]; |
8003 | 153k | for (k = 0; k < i; k++) |
8004 | 85.2k | if (vps->direct_dependency_flag[i][k] && vps->direct_dependency_flag[k][j]) |
8005 | 5.20k | dependency_flag[i][j] = 1; |
8006 | 68.3k | } |
8007 | 20.6k | } |
8008 | | |
8009 | 27.6k | for (i = 0; i < vps->max_layers; i++) { |
8010 | 20.6k | iNuhLId = vps->layer_id_in_nuh[i]; |
8011 | 20.6k | d = r = p = 0; |
8012 | 88.9k | for (j = 0; j < vps->max_layers; j++) { |
8013 | 68.3k | jNuhLId = vps->layer_id_in_nuh[j]; |
8014 | 68.3k | if (vps->direct_dependency_flag[i][j]) { |
8015 | | // id_direct_ref_layers[iNuhLId][d] = jNuhLId; |
8016 | 11.9k | d++; |
8017 | 11.9k | } |
8018 | 68.3k | if (dependency_flag[i][j]) { |
8019 | | // id_ref_layers[iNuhLId][r] = jNuhLId; |
8020 | 14.0k | r++; |
8021 | 14.0k | } |
8022 | | |
8023 | 68.3k | if (dependency_flag[j][i]) |
8024 | 14.0k | id_pred_layers[iNuhLId][p++] = jNuhLId; |
8025 | 68.3k | } |
8026 | 20.6k | num_direct_ref_layers[iNuhLId] = d; |
8027 | | // num_ref_layers[iNuhLId] = r; |
8028 | 20.6k | num_pred_layers[iNuhLId] = p; |
8029 | 20.6k | } |
8030 | | |
8031 | 7.02k | memset(layer_id_in_list_flag, 0, 64 * sizeof(u8)); |
8032 | 7.02k | k = 0; //num_indepentdent_layers |
8033 | 27.6k | for (i = 0; i < vps->max_layers; i++) { |
8034 | 20.6k | iNuhLId = vps->layer_id_in_nuh[i]; |
8035 | 20.6k | if (!num_direct_ref_layers[iNuhLId]) { |
8036 | 11.4k | u32 h = 1; |
8037 | | //tree_partition_layer_id[k][0] = iNuhLId; |
8038 | 21.0k | for (j = 0; j < num_pred_layers[iNuhLId]; j++) { |
8039 | 9.64k | u32 predLId = id_pred_layers[iNuhLId][j]; |
8040 | 9.64k | if (!layer_id_in_list_flag[predLId]) { |
8041 | | //tree_partition_layer_id[k][h++] = predLId; |
8042 | 9.19k | layer_id_in_list_flag[predLId] = 1; |
8043 | 9.19k | } |
8044 | 9.64k | } |
8045 | 11.4k | num_layers_in_tree_partition[k++] = h; |
8046 | 11.4k | } |
8047 | 20.6k | } |
8048 | 7.02k | num_indepentdent_layers = k; |
8049 | | |
8050 | 7.02k | num_add_layer_set = 0; |
8051 | 7.02k | if (num_indepentdent_layers > 1) |
8052 | 2.58k | num_add_layer_set = gf_bs_read_ue_log(bs, "num_add_layer_set"); |
8053 | | |
8054 | 340k | for (i = 0; i < num_add_layer_set; i++) |
8055 | 845k | for (j = 1; j < num_indepentdent_layers; j++) { |
8056 | 511k | nb_bits = 1; |
8057 | 511k | while ((1 << nb_bits) < (num_layers_in_tree_partition[j] + 1)) |
8058 | 0 | nb_bits++; |
8059 | 511k | gf_bs_read_int_log_idx2(bs, nb_bits, "highest_layer_idx_plus1", i, j); |
8060 | 511k | } |
8061 | | |
8062 | | |
8063 | 7.02k | if (gf_bs_read_int_log(bs, 1, "vps_sub_layers_max_minus1_present_flag")) { |
8064 | 19.9k | for (i = 0; i < vps->max_layers; i++) { |
8065 | 14.6k | gf_bs_read_int_log_idx(bs, 3, "sub_layers_vps_max_minus1", i); |
8066 | 14.6k | } |
8067 | 5.27k | } |
8068 | | |
8069 | 7.02k | if (gf_bs_read_int_log(bs, 1, "max_tid_ref_present_flag")) { |
8070 | 6.01k | for (i = 0; i < (vps->max_layers - 1); i++) { |
8071 | 10.6k | for (j = i + 1; j < vps->max_layers; j++) { |
8072 | 6.97k | if (vps->direct_dependency_flag[j][i]) |
8073 | 5.58k | gf_bs_read_int_log_idx2(bs, 3, "max_tid_il_ref_pics_plus1", i, j); |
8074 | 6.97k | } |
8075 | 3.66k | } |
8076 | 2.34k | } |
8077 | 7.02k | gf_bs_read_int_log(bs, 1, "default_ref_layers_active_flag"); |
8078 | | |
8079 | 7.02k | vps->num_profile_tier_level = 1 + gf_bs_read_ue_log(bs, "num_profile_tier_level"); |
8080 | 7.02k | if (vps->num_profile_tier_level > MAX_LHVC_LAYERS) { |
8081 | 880 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Wrong number of PTLs in VPS %d\n", vps->num_profile_tier_level)); |
8082 | 880 | vps->num_profile_tier_level = 1; |
8083 | 880 | return GF_FALSE; |
8084 | 880 | } |
8085 | | |
8086 | 8.29k | for (i = vps->base_layer_internal_flag ? 2 : 1; i < vps->num_profile_tier_level; i++) { |
8087 | 2.15k | Bool vps_profile_present_flag = gf_bs_read_int_log_idx(bs, 1, "vps_profile_present_flag", i); |
8088 | 2.15k | hevc_profile_tier_level(bs, vps_profile_present_flag, vps->max_sub_layers - 1, &vps->ext_ptl[i - 1], i-1); |
8089 | 2.15k | } |
8090 | | |
8091 | 6.14k | NumLayerSets = vps->num_layer_sets + num_add_layer_set; |
8092 | 6.14k | num_add_olss = 0; |
8093 | | |
8094 | 6.14k | if (NumLayerSets > 1) { |
8095 | 4.22k | num_add_olss = gf_bs_read_ue_log(bs, "num_add_olss"); |
8096 | 4.22k | default_output_layer_idc = gf_bs_read_int_log(bs, 2, "default_output_layer_idc"); |
8097 | 4.22k | default_output_layer_idc = default_output_layer_idc < 2 ? default_output_layer_idc : 2; |
8098 | 4.22k | } |
8099 | 6.14k | vps->num_output_layer_sets = num_add_olss + NumLayerSets; |
8100 | | |
8101 | 6.14k | if (vps->num_output_layer_sets > MAX_LHVC_LAYERS) { |
8102 | 696 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Wrong number of output layer sets in VPS %d, max %d supported\n", vps->num_output_layer_sets, MAX_LHVC_LAYERS)); |
8103 | 696 | vps->num_output_layer_sets = 1; |
8104 | 696 | return GF_FALSE; |
8105 | 696 | } |
8106 | | |
8107 | 5.44k | layer_set_idx_for_ols_minus1[0] = 1; |
8108 | 5.44k | vps->output_layer_flag[0][0] = 1; |
8109 | | |
8110 | 18.8k | for (i = 0; i < vps->num_output_layer_sets; i++) { |
8111 | 13.4k | if ((NumLayerSets > 2) && (i >= NumLayerSets)) { |
8112 | 183 | nb_bits = 1; |
8113 | 183 | while ((1 << nb_bits) < (NumLayerSets - 1)) |
8114 | 0 | nb_bits++; |
8115 | 183 | layer_set_idx_for_ols_minus1[i] = gf_bs_read_int_log_idx(bs, nb_bits, "layer_set_idx_for_ols_minus1", i); |
8116 | 183 | } |
8117 | 13.2k | else |
8118 | 13.2k | layer_set_idx_for_ols_minus1[i] = 0; |
8119 | 13.4k | ols_ids_to_ls_idx = i < NumLayerSets ? i : layer_set_idx_for_ols_minus1[i] + 1; |
8120 | | |
8121 | 13.4k | if ((i > (vps->num_layer_sets - 1)) || (default_output_layer_idc == 2)) { |
8122 | 11.7k | for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) |
8123 | 7.07k | vps->output_layer_flag[i][j] = gf_bs_read_int_log_idx2(bs, 1, "output_layer_flag", i, j); |
8124 | 4.67k | } |
8125 | | |
8126 | 13.4k | if ((default_output_layer_idc == 0) || (default_output_layer_idc == 1)) { |
8127 | 25.8k | for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) { |
8128 | 16.0k | if ((default_output_layer_idc == 0) || (vps->LayerSetLayerIdList[i][j] == vps->LayerSetLayerIdListMax[i])) |
8129 | 11.4k | OutputLayerFlag[i][j] = GF_TRUE; |
8130 | 4.59k | else |
8131 | 4.59k | OutputLayerFlag[i][j] = GF_FALSE; |
8132 | 16.0k | } |
8133 | 9.81k | } |
8134 | | |
8135 | 34.0k | for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) { |
8136 | 20.6k | if (OutputLayerFlag[i][j]) { |
8137 | 14.6k | u32 curLayerID; |
8138 | 14.6k | vps->necessary_layers_flag[i][j] = GF_TRUE; |
8139 | 14.6k | curLayerID = vps->LayerSetLayerIdList[i][j]; |
8140 | 23.2k | for (k = 0; k < j; k++) { |
8141 | 8.52k | u32 refLayerId = vps->LayerSetLayerIdList[i][k]; |
8142 | 8.52k | if (dependency_flag[vps->layer_id_in_vps[curLayerID]][vps->layer_id_in_vps[refLayerId]]) |
8143 | 5.07k | vps->necessary_layers_flag[i][k] = GF_TRUE; |
8144 | 8.52k | } |
8145 | 14.6k | } |
8146 | 20.6k | } |
8147 | 13.4k | vps->num_necessary_layers[i] = 0; |
8148 | 34.0k | for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) { |
8149 | 20.6k | if (vps->necessary_layers_flag[i][j]) |
8150 | 19.2k | vps->num_necessary_layers[i] += 1; |
8151 | 20.6k | } |
8152 | | |
8153 | 13.4k | if (i == 0) { |
8154 | 5.03k | if (vps->base_layer_internal_flag) { |
8155 | 1.12k | if (vps->max_layers > 1) |
8156 | 592 | vps->profile_tier_level_idx[0][0] = 1; |
8157 | 532 | else |
8158 | 532 | vps->profile_tier_level_idx[0][0] = 0; |
8159 | 1.12k | } |
8160 | 5.03k | continue; |
8161 | 5.03k | } |
8162 | 8.39k | nb_bits = 1; |
8163 | 8.54k | while ((u32)(1 << nb_bits) < vps->num_profile_tier_level) |
8164 | 151 | nb_bits++; |
8165 | 23.9k | for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) |
8166 | 15.5k | if (vps->necessary_layers_flag[i][j] && vps->num_profile_tier_level) |
8167 | 14.1k | vps->profile_tier_level_idx[i][j] = gf_bs_read_int_log_idx2(bs, nb_bits, "profile_tier_level_idx", i, j); |
8168 | 1.41k | else |
8169 | 1.41k | vps->profile_tier_level_idx[i][j] = 0; |
8170 | | |
8171 | | |
8172 | 8.39k | nb_output_layers_in_output_layer_set[i] = 0; |
8173 | 23.9k | for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) { |
8174 | 15.5k | nb_output_layers_in_output_layer_set[i] += OutputLayerFlag[i][j]; |
8175 | 15.5k | if (OutputLayerFlag[i][j]) { |
8176 | 9.99k | ols_highest_output_layer_id[i] = vps->LayerSetLayerIdList[ols_ids_to_ls_idx][j]; |
8177 | 9.99k | } |
8178 | 15.5k | } |
8179 | 8.39k | if (nb_output_layers_in_output_layer_set[i] == 1 && ols_highest_output_layer_id[i] > 0) |
8180 | 3.79k | vps->alt_output_layer_flag[i] = gf_bs_read_int_log_idx(bs, 1, "alt_output_layer_flag", i); |
8181 | 8.39k | } |
8182 | | |
8183 | 5.44k | vps->num_rep_formats = 1 + gf_bs_read_ue_log(bs, "num_rep_formats_minus1"); |
8184 | 5.44k | if (vps->num_rep_formats > 16) { |
8185 | 262 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Wrong number of rep formats in VPS %d\n", vps->num_rep_formats)); |
8186 | 262 | vps->num_rep_formats = 0; |
8187 | 262 | return GF_FALSE; |
8188 | 262 | } |
8189 | | |
8190 | 13.6k | for (i = 0; i < vps->num_rep_formats; i++) { |
8191 | 8.46k | lhvc_parse_rep_format(&vps->rep_formats[i], bs, i); |
8192 | 8.46k | } |
8193 | 5.18k | if (vps->num_rep_formats > 1) |
8194 | 1.51k | rep_format_idx_present_flag = gf_bs_read_int_log(bs, 1, "rep_format_idx_present_flag"); |
8195 | 3.67k | else |
8196 | 3.67k | rep_format_idx_present_flag = 0; |
8197 | | |
8198 | 5.18k | vps->rep_format_idx[0] = 0; |
8199 | 5.18k | nb_bits = 1; |
8200 | 6.51k | while ((u32)(1 << nb_bits) < vps->num_rep_formats) |
8201 | 1.32k | nb_bits++; |
8202 | 19.3k | for (i = vps->base_layer_internal_flag ? 1 : 0; i < vps->max_layers; i++) { |
8203 | 14.1k | if (rep_format_idx_present_flag) { |
8204 | 2.01k | vps->rep_format_idx[i] = gf_bs_read_int_log_idx(bs, nb_bits, "rep_format_idx", i); |
8205 | 2.01k | } |
8206 | 12.1k | else { |
8207 | 12.1k | vps->rep_format_idx[i] = i < vps->num_rep_formats - 1 ? i : vps->num_rep_formats - 1; |
8208 | 12.1k | } |
8209 | 14.1k | } |
8210 | | //TODO - we don't use the rest ... |
8211 | | |
8212 | 5.18k | return GF_TRUE; |
8213 | 5.44k | } |
8214 | | |
8215 | 28.7k | #define HEVC_VPS_BROKEN {\ |
8216 | 28.7k | memset(vps, 0, sizeof(HEVC_VPS)); \ |
8217 | 28.7k | return -1;\ |
8218 | 28.7k | } |
8219 | | |
8220 | | static s32 gf_hevc_read_vps_bs_internal(GF_BitStream *bs, HEVCState *hevc, Bool stop_at_vps_ext) |
8221 | 44.4k | { |
8222 | 44.4k | u8 vps_sub_layer_ordering_info_present_flag, vps_extension_flag; |
8223 | 44.4k | u32 i, j; |
8224 | 44.4k | s32 vps_id; |
8225 | 44.4k | HEVC_VPS *vps; |
8226 | 44.4k | u8 layer_id_included_flag[MAX_LHVC_LAYERS][64]; |
8227 | | |
8228 | | //nalu header already parsed |
8229 | 44.4k | vps_id = gf_bs_read_int_log(bs, 4, "vps_id"); |
8230 | | |
8231 | 44.4k | if ((vps_id<0) || (vps_id >= 16)) return -1; |
8232 | | |
8233 | 44.4k | vps = &hevc->vps[vps_id]; |
8234 | 44.4k | vps->bit_pos_vps_extensions = -1; |
8235 | 44.4k | if (!vps->state) { |
8236 | 29.5k | vps->id = vps_id; |
8237 | 29.5k | vps->state = 1; |
8238 | 29.5k | } |
8239 | | |
8240 | 44.4k | vps->base_layer_internal_flag = gf_bs_read_int_log(bs, 1, "base_layer_internal_flag"); |
8241 | 44.4k | vps->base_layer_available_flag = gf_bs_read_int_log(bs, 1, "base_layer_available_flag"); |
8242 | 44.4k | vps->max_layers = 1 + gf_bs_read_int_log(bs, 6, "max_layers_minus1"); |
8243 | 44.4k | if (vps->max_layers > MAX_LHVC_LAYERS) { |
8244 | 15.9k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] %d layers in VPS but only %d supported in GPAC\n", vps->max_layers, MAX_LHVC_LAYERS)); |
8245 | 15.9k | HEVC_VPS_BROKEN |
8246 | 15.9k | } |
8247 | 28.4k | vps->max_sub_layers = gf_bs_read_int_log(bs, 3, "max_sub_layers_minus1") + 1; |
8248 | 28.4k | vps->temporal_id_nesting = gf_bs_read_int_log(bs, 1, "temporal_id_nesting"); |
8249 | 28.4k | gf_bs_read_int_log(bs, 16, "vps_reserved_ffff_16bits"); |
8250 | 28.4k | hevc_profile_tier_level(bs, 1, vps->max_sub_layers - 1, &vps->ptl, 0); |
8251 | | |
8252 | 28.4k | vps_sub_layer_ordering_info_present_flag = gf_bs_read_int_log(bs, 1, "vps_sub_layer_ordering_info_present_flag"); |
8253 | 59.2k | for (i = (vps_sub_layer_ordering_info_present_flag ? 0 : vps->max_sub_layers - 1); i < vps->max_sub_layers; i++) { |
8254 | 30.8k | gf_bs_read_ue_log_idx(bs, "vps_max_dec_pic_buffering_minus1", i); |
8255 | 30.8k | gf_bs_read_ue_log_idx(bs, "vps_max_num_reorder_pics", i); |
8256 | 30.8k | gf_bs_read_ue_log_idx(bs, "vps_max_latency_increase_plus1", i); |
8257 | 30.8k | } |
8258 | 28.4k | vps->max_layer_id = gf_bs_read_int_log(bs, 6, "max_layer_id"); |
8259 | 28.4k | if (vps->max_layer_id >= MAX_LHVC_LAYERS) { |
8260 | 8.05k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] VPS max layer ID %u but GPAC only supports %u\n", vps->max_layer_id, MAX_LHVC_LAYERS)); |
8261 | 8.05k | HEVC_VPS_BROKEN |
8262 | 8.05k | } |
8263 | 20.3k | vps->num_layer_sets = gf_bs_read_ue_log(bs, "num_layer_sets_minus1") + 1; |
8264 | 20.3k | if (vps->num_layer_sets > MAX_LHVC_LAYERS) { |
8265 | 813 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Wrong number of layer sets in VPS %d\n", vps->num_layer_sets)); |
8266 | 813 | HEVC_VPS_BROKEN |
8267 | 813 | } |
8268 | 28.4k | for (i = 1; i < vps->num_layer_sets; i++) { |
8269 | 39.7k | for (j = 0; j <= vps->max_layer_id; j++) { |
8270 | 30.8k | layer_id_included_flag[i][j] = gf_bs_read_int_log_idx2(bs, 1, "layer_id_included_flag", i, j); |
8271 | 30.8k | } |
8272 | 8.92k | } |
8273 | 19.5k | vps->num_layers_in_id_list[0] = 1; |
8274 | 28.4k | for (i = 1; i < vps->num_layer_sets; i++) { |
8275 | 8.92k | u32 n, m; |
8276 | 8.92k | n = 0; |
8277 | 39.7k | for (m = 0; m <= vps->max_layer_id; m++) { |
8278 | 30.8k | if (layer_id_included_flag[i][m]) { |
8279 | 16.0k | vps->LayerSetLayerIdList[i][n++] = m; |
8280 | 16.0k | if (vps->LayerSetLayerIdListMax[i] < m) |
8281 | 3.69k | vps->LayerSetLayerIdListMax[i] = m; |
8282 | 16.0k | } |
8283 | 30.8k | } |
8284 | 8.92k | vps->num_layers_in_id_list[i] = n; |
8285 | 8.92k | } |
8286 | 19.5k | if (gf_bs_read_int_log(bs, 1, "vps_timing_info_present_flag")) { |
8287 | 1.91k | u32 vps_num_hrd_parameters; |
8288 | 1.91k | gf_bs_read_int_log(bs, 32, "vps_num_units_in_tick"); |
8289 | 1.91k | gf_bs_read_int_log(bs, 32, "vps_time_scale"); |
8290 | 1.91k | if (gf_bs_read_int_log(bs, 1, "vps_poc_proportional_to_timing_flag")) { |
8291 | 1.47k | gf_bs_read_ue_log(bs, "vps_num_ticks_poc_diff_one_minus1"); |
8292 | 1.47k | } |
8293 | 1.91k | vps_num_hrd_parameters = gf_bs_read_ue_log(bs, "vps_num_hrd_parameters"); |
8294 | 36.9k | for (i = 0; i < vps_num_hrd_parameters; i++) { |
8295 | 35.0k | Bool cprms_present_flag = GF_TRUE; |
8296 | 35.0k | gf_bs_read_ue_log_idx(bs, "hrd_layer_set_idx", i); |
8297 | 35.0k | if (i > 0) |
8298 | 34.0k | cprms_present_flag = gf_bs_read_int_log(bs, 1, "cprms_present_flag"); |
8299 | 35.0k | hevc_parse_hrd_parameters(bs, cprms_present_flag, vps->max_sub_layers - 1, i); |
8300 | 35.0k | } |
8301 | 1.91k | } |
8302 | 19.5k | if (stop_at_vps_ext) { |
8303 | 0 | return vps_id; |
8304 | 0 | } |
8305 | | |
8306 | 19.5k | vps_extension_flag = gf_bs_read_int_log(bs, 1, "vps_extension_flag"); |
8307 | 19.5k | if (vps_extension_flag) { |
8308 | 9.04k | Bool res; |
8309 | 9.04k | gf_bs_align(bs); |
8310 | 9.04k | res = hevc_parse_vps_extension(vps, bs); |
8311 | 9.04k | if (res != GF_TRUE) { |
8312 | 3.86k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Failed to parse VPS extensions\n")); |
8313 | 3.86k | HEVC_VPS_BROKEN |
8314 | 3.86k | } |
8315 | 5.18k | if (gf_bs_read_int_log(bs, 1, "vps_extension2_flag")) { |
8316 | | #if 0 |
8317 | | while (gf_bs_available(bs)) { |
8318 | | /*vps_extension_data_flag */ gf_bs_read_int(bs, 1); |
8319 | | } |
8320 | | #endif |
8321 | | |
8322 | 2.22k | } |
8323 | 5.18k | } |
8324 | 15.7k | return vps_id; |
8325 | 19.5k | } |
8326 | | |
8327 | | GF_EXPORT |
8328 | | s32 gf_hevc_read_vps_ex(u8 *data, u32 *size, HEVCState *hevc, Bool remove_extensions) |
8329 | 0 | { |
8330 | 0 | GF_BitStream *bs; |
8331 | 0 | char *data_without_emulation_bytes = NULL; |
8332 | 0 | u32 data_without_emulation_bytes_size = 0; |
8333 | 0 | s32 vps_id = -1; |
8334 | | |
8335 | | /*still contains emulation bytes*/ |
8336 | 0 | data_without_emulation_bytes_size = remove_extensions ? gf_media_nalu_emulation_bytes_remove_count(data, (*size)) : 0; |
8337 | 0 | if (!data_without_emulation_bytes_size) { |
8338 | 0 | bs = gf_bs_new(data, (*size), GF_BITSTREAM_READ); |
8339 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
8340 | 0 | } |
8341 | | //when removing VPS ext, we have to get the full buffer without emulation prevention bytes becuase we do a bit-by-bit copy of the vps |
8342 | 0 | else { |
8343 | 0 | data_without_emulation_bytes = gf_malloc((*size) * sizeof(char)); |
8344 | 0 | data_without_emulation_bytes_size = gf_media_nalu_remove_emulation_bytes(data, data_without_emulation_bytes, (*size)); |
8345 | 0 | bs = gf_bs_new(data_without_emulation_bytes, data_without_emulation_bytes_size, GF_BITSTREAM_READ); |
8346 | 0 | } |
8347 | 0 | if (!bs) goto exit; |
8348 | | |
8349 | | |
8350 | 0 | if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) goto exit; |
8351 | | |
8352 | 0 | vps_id = gf_hevc_read_vps_bs_internal(bs, hevc, remove_extensions); |
8353 | 0 | if (vps_id < 0) goto exit; |
8354 | | |
8355 | 0 | if (remove_extensions) { |
8356 | 0 | u8 *new_vps; |
8357 | 0 | u32 new_vps_size, emulation_bytes; |
8358 | 0 | u32 bit_pos = gf_bs_get_bit_offset(bs); |
8359 | 0 | GF_BitStream *w_bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
8360 | 0 | gf_bs_seek(bs, 0); |
8361 | 0 | gf_bs_write_u8(w_bs, gf_bs_read_u8(bs) ); |
8362 | 0 | gf_bs_write_u8(w_bs, gf_bs_read_u8(bs) ); |
8363 | 0 | gf_bs_write_u8(w_bs, gf_bs_read_u8(bs) ); |
8364 | 0 | gf_bs_write_u8(w_bs, gf_bs_read_u8(bs) ); |
8365 | 0 | gf_bs_write_u16(w_bs, gf_bs_read_u16(bs) ); |
8366 | 0 | bit_pos -= 48; |
8367 | 0 | while (bit_pos) { |
8368 | 0 | u32 v = gf_bs_read_int(bs, 1); |
8369 | 0 | gf_bs_write_int(w_bs, v, 1); |
8370 | 0 | bit_pos--; |
8371 | 0 | } |
8372 | | /*vps extension flag*/ |
8373 | 0 | gf_bs_write_int(w_bs, 0, 1); |
8374 | 0 | new_vps = NULL; |
8375 | 0 | gf_bs_get_content(w_bs, &new_vps, &new_vps_size); |
8376 | 0 | gf_bs_del(w_bs); |
8377 | |
|
8378 | 0 | emulation_bytes = gf_media_nalu_emulation_bytes_add_count(new_vps, new_vps_size); |
8379 | 0 | if (emulation_bytes + new_vps_size > *size) { |
8380 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Buffer too small to rewrite VPS - skipping rewrite\n")); |
8381 | 0 | } |
8382 | 0 | else { |
8383 | 0 | *size = gf_media_nalu_add_emulation_bytes(new_vps, data, new_vps_size); |
8384 | 0 | } |
8385 | 0 | if (new_vps) |
8386 | 0 | gf_free(new_vps); |
8387 | 0 | } |
8388 | |
|
8389 | 0 | exit: |
8390 | 0 | if (bs) |
8391 | 0 | gf_bs_del(bs); |
8392 | 0 | if (data_without_emulation_bytes) gf_free(data_without_emulation_bytes); |
8393 | 0 | return vps_id; |
8394 | 0 | } |
8395 | | |
8396 | | GF_EXPORT |
8397 | | s32 gf_hevc_read_vps(u8 *data, u32 size, HEVCState *hevc) |
8398 | 0 | { |
8399 | 0 | return gf_hevc_read_vps_ex(data, &size, hevc, GF_FALSE); |
8400 | 0 | } |
8401 | | |
8402 | | GF_EXPORT |
8403 | | s32 gf_hevc_read_vps_bs(GF_BitStream *bs, HEVCState *hevc) |
8404 | 0 | { |
8405 | 0 | if (!bs || !hevc) return -1; |
8406 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
8407 | 0 | if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) return -1; |
8408 | 0 | return gf_hevc_read_vps_bs_internal(bs, hevc, GF_FALSE); |
8409 | 0 | } |
8410 | | |
8411 | | static void hevc_scaling_list_data(GF_BitStream *bs) |
8412 | 8.92k | { |
8413 | 8.92k | u32 i, sizeId, matrixId; |
8414 | 44.6k | for (sizeId = 0; sizeId < 4; sizeId++) { |
8415 | 214k | for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3) ? 3 : 1) { |
8416 | 178k | u32 idx = sizeId*100 + 10*matrixId; |
8417 | 178k | u32 scaling_list_pred_mode_flag_sizeId_matrixId = gf_bs_read_int_log_idx(bs, 1, "scaling_list_pred_mode_flag_sizeId_matrixId", idx); |
8418 | 178k | if (!scaling_list_pred_mode_flag_sizeId_matrixId) { |
8419 | 126k | gf_bs_read_ue_log_idx(bs, "scaling_list_pred_matrix_id_delta", idx); |
8420 | 126k | } |
8421 | 52.5k | else { |
8422 | | //u32 nextCoef = 8; |
8423 | 52.5k | u32 coefNum = MIN(64, (1 << (4 + (sizeId << 1)))); |
8424 | 52.5k | if (sizeId > 1) { |
8425 | 16.5k | gf_bs_read_se_log_idx(bs, "scaling_list_dc_coef_minus8", idx); |
8426 | 16.5k | } |
8427 | 2.38M | for (i = 0; i < coefNum; i++) { |
8428 | 2.32M | gf_bs_read_se_log_idx2(bs, "scaling_list_delta_coef", idx, i); |
8429 | 2.32M | } |
8430 | 52.5k | } |
8431 | 178k | } |
8432 | 35.7k | } |
8433 | 8.92k | } |
8434 | | |
8435 | | |
8436 | | static const struct { |
8437 | | u32 w, h; |
8438 | | } hevc_sar[17] = |
8439 | | { |
8440 | | { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, |
8441 | | { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 }, |
8442 | | { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 }, |
8443 | | { 64, 33 }, { 160,99 }, { 4,3}, { 3,2}, { 2,1} |
8444 | | }; |
8445 | | |
8446 | 19.9k | #define HEVC_SPS_BROKEN {\ |
8447 | 19.9k | memset(sps, 0, sizeof(HEVC_SPS)); \ |
8448 | 19.9k | return -1;\ |
8449 | 19.9k | } |
8450 | | |
8451 | | static s32 gf_hevc_read_sps_bs_internal(GF_BitStream *bs, HEVCState *hevc, u8 layer_id, u32 *vui_flag_pos) |
8452 | 37.8k | { |
8453 | 37.8k | s32 vps_id, sps_id = -1; |
8454 | 37.8k | u32 i, nb_CTUs, depth; |
8455 | 37.8k | HEVC_SPS *sps; |
8456 | 37.8k | HEVC_VPS *vps; |
8457 | 37.8k | HEVC_ProfileTierLevel ptl; |
8458 | 37.8k | Bool multiLayerExtSpsFlag; |
8459 | 37.8k | u8 sps_ext_or_max_sub_layers_minus1, max_sub_layers_minus1; |
8460 | | |
8461 | 37.8k | if (vui_flag_pos) *vui_flag_pos = 0; |
8462 | | |
8463 | | //nalu header already parsed |
8464 | 37.8k | vps_id = gf_bs_read_int_log(bs, 4, "vps_id"); |
8465 | 37.8k | if ((vps_id<0) || (vps_id >= 16)) { |
8466 | 0 | return -1; |
8467 | 0 | } |
8468 | 37.8k | memset(&ptl, 0, sizeof(ptl)); |
8469 | 37.8k | max_sub_layers_minus1 = 0; |
8470 | 37.8k | sps_ext_or_max_sub_layers_minus1 = 0; |
8471 | 37.8k | if (layer_id == 0) |
8472 | 3.59k | max_sub_layers_minus1 = gf_bs_read_int_log(bs, 3, "max_sub_layers_minus1"); |
8473 | 34.2k | else { |
8474 | 34.2k | sps_ext_or_max_sub_layers_minus1 = gf_bs_read_int_log(bs, 3, "sps_ext_or_max_sub_layers_minus1"); |
8475 | 34.2k | max_sub_layers_minus1 = sps_ext_or_max_sub_layers_minus1 == 7 ? hevc->vps[vps_id].max_sub_layers - 1 : sps_ext_or_max_sub_layers_minus1; |
8476 | 34.2k | } |
8477 | 37.8k | multiLayerExtSpsFlag = (layer_id != 0) && (sps_ext_or_max_sub_layers_minus1 == 7); |
8478 | 37.8k | if (!multiLayerExtSpsFlag) { |
8479 | 27.2k | gf_bs_read_int_log(bs, 1, "temporal_id_nesting_flag"); |
8480 | 27.2k | hevc_profile_tier_level(bs, 1, max_sub_layers_minus1, &ptl, 0); |
8481 | 27.2k | } |
8482 | | |
8483 | 37.8k | sps_id = gf_bs_read_ue_log(bs, "sps_id"); |
8484 | 37.8k | if ((sps_id < 0) || (sps_id >= 16)) { |
8485 | 3.49k | return -1; |
8486 | 3.49k | } |
8487 | 34.3k | if (!hevc) return sps_id; |
8488 | | |
8489 | 34.3k | sps = &hevc->sps[sps_id]; |
8490 | 34.3k | if (!sps->state) { |
8491 | 20.7k | sps->state = 1; |
8492 | 20.7k | sps->id = sps_id; |
8493 | 20.7k | sps->vps_id = vps_id; |
8494 | 20.7k | } |
8495 | 34.3k | sps->ptl = ptl; |
8496 | 34.3k | vps = &hevc->vps[vps_id]; |
8497 | 34.3k | sps->max_sub_layers_minus1 = max_sub_layers_minus1; |
8498 | 34.3k | sps->sps_ext_or_max_sub_layers_minus1 = sps_ext_or_max_sub_layers_minus1; |
8499 | | |
8500 | | /* default values */ |
8501 | 34.3k | sps->colour_primaries = 2; |
8502 | 34.3k | sps->transfer_characteristic = 2; |
8503 | 34.3k | sps->matrix_coeffs = 2; |
8504 | | |
8505 | | //sps_rep_format_idx = 0; |
8506 | 34.3k | if (multiLayerExtSpsFlag) { |
8507 | 10.5k | sps->update_rep_format_flag = gf_bs_read_int_log(bs, 1, "update_rep_format_flag"); |
8508 | 10.5k | if (sps->update_rep_format_flag) { |
8509 | 1.15k | sps->rep_format_idx = gf_bs_read_int_log(bs, 8, "rep_format_idx"); |
8510 | 9.40k | } else { |
8511 | 9.40k | if (layer_id<MAX_LHVC_LAYERS) { |
8512 | 52 | u32 idx = vps->layer_id_in_vps[layer_id]; |
8513 | 52 | if (idx<=15) |
8514 | 52 | sps->rep_format_idx = vps->rep_format_idx[idx]; |
8515 | 52 | } |
8516 | 9.40k | } |
8517 | 10.5k | if (sps->rep_format_idx>15) { |
8518 | 1.15k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid rep_format_idx index %d\n", sps->rep_format_idx)); |
8519 | 1.15k | HEVC_SPS_BROKEN |
8520 | 1.15k | } |
8521 | 9.40k | sps->width = vps->rep_formats[sps->rep_format_idx].pic_width_luma_samples; |
8522 | 9.40k | sps->height = vps->rep_formats[sps->rep_format_idx].pic_height_luma_samples; |
8523 | 9.40k | sps->chroma_format_idc = vps->rep_formats[sps->rep_format_idx].chroma_format_idc; |
8524 | 9.40k | sps->bit_depth_luma = vps->rep_formats[sps->rep_format_idx].bit_depth_luma; |
8525 | 9.40k | sps->bit_depth_chroma = vps->rep_formats[sps->rep_format_idx].bit_depth_chroma; |
8526 | 9.40k | sps->separate_colour_plane_flag = vps->rep_formats[sps->rep_format_idx].separate_colour_plane_flag; |
8527 | | |
8528 | | //TODO this is crude ... |
8529 | 9.40k | sps->ptl = vps->ext_ptl[0]; |
8530 | 9.40k | } |
8531 | 23.7k | else { |
8532 | 23.7k | sps->chroma_format_idc = gf_bs_read_ue_log(bs, "chroma_format_idc"); |
8533 | 23.7k | if (sps->chroma_format_idc == 3) |
8534 | 54 | sps->separate_colour_plane_flag = gf_bs_read_int_log(bs, 1, "separate_colour_plane_flag"); |
8535 | 23.7k | sps->width = gf_bs_read_ue_log(bs, "width"); |
8536 | 23.7k | sps->height = gf_bs_read_ue_log(bs, "height"); |
8537 | 23.7k | if ((sps->cw_flag = gf_bs_read_int_log(bs, 1, "conformance_window_flag"))) { |
8538 | 4.48k | u32 SubWidthC, SubHeightC; |
8539 | | |
8540 | 4.48k | if (sps->chroma_format_idc == 1) { |
8541 | 208 | SubWidthC = SubHeightC = 2; |
8542 | 208 | } |
8543 | 4.27k | else if (sps->chroma_format_idc == 2) { |
8544 | 290 | SubWidthC = 2; |
8545 | 290 | SubHeightC = 1; |
8546 | 290 | } |
8547 | 3.98k | else { |
8548 | 3.98k | SubWidthC = SubHeightC = 1; |
8549 | 3.98k | } |
8550 | | |
8551 | 4.48k | sps->cw_left = gf_bs_read_ue_log(bs, "conformance_window_left"); |
8552 | 4.48k | sps->cw_right = gf_bs_read_ue_log(bs, "conformance_window_right"); |
8553 | 4.48k | sps->cw_top = gf_bs_read_ue_log(bs, "conformance_window_top"); |
8554 | 4.48k | sps->cw_bottom = gf_bs_read_ue_log(bs, "conformance_window_bottom"); |
8555 | | |
8556 | 4.48k | sps->width -= SubWidthC * (sps->cw_left + sps->cw_right); |
8557 | 4.48k | sps->height -= SubHeightC * (sps->cw_top + sps->cw_bottom); |
8558 | 4.48k | } |
8559 | 23.7k | sps->bit_depth_luma = 8 + gf_bs_read_ue_log(bs, "bit_depth_luma_minus8"); |
8560 | 23.7k | sps->bit_depth_chroma = 8 + gf_bs_read_ue_log(bs, "bit_depth_chroma_minus8"); |
8561 | 23.7k | } |
8562 | | |
8563 | 33.1k | sps->log2_max_pic_order_cnt_lsb = 4 + gf_bs_read_ue_log(bs, "log2_max_pic_order_cnt_lsb_minus4"); |
8564 | 33.1k | if (sps->log2_max_pic_order_cnt_lsb>16) { |
8565 | 866 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid log2_max_pic_order_cnt_lsb_minus4 %d, max shall be 12\n", sps->log2_max_pic_order_cnt_lsb-4)); |
8566 | 866 | HEVC_SPS_BROKEN |
8567 | 866 | } |
8568 | | |
8569 | 32.2k | if (!multiLayerExtSpsFlag) { |
8570 | 22.9k | sps->sub_layer_ordering_info_present_flag = gf_bs_read_int_log(bs, 1, "sub_layer_ordering_info_present_flag"); |
8571 | 51.8k | for (i = sps->sub_layer_ordering_info_present_flag ? 0 : sps->max_sub_layers_minus1; i <= sps->max_sub_layers_minus1; i++) { |
8572 | 28.9k | gf_bs_read_ue_log_idx(bs, "max_dec_pic_buffering", i); |
8573 | 28.9k | gf_bs_read_ue_log_idx(bs, "num_reorder_pics", i); |
8574 | 28.9k | gf_bs_read_ue_log_idx(bs, "max_latency_increase", i); |
8575 | 28.9k | } |
8576 | 22.9k | } |
8577 | | |
8578 | 32.2k | sps->log2_min_luma_coding_block_size = 3 + gf_bs_read_ue_log(bs, "log2_min_luma_coding_block_size_minus3"); |
8579 | 32.2k | sps->log2_diff_max_min_luma_coding_block_size = gf_bs_read_ue_log(bs, "log2_diff_max_min_luma_coding_block_size"); |
8580 | | //we allow more than in max profile, but make sure we don't overflow max CU W/H compute below |
8581 | 32.2k | if (sps->log2_min_luma_coding_block_size + sps->log2_diff_max_min_luma_coding_block_size >= 30) { |
8582 | 915 | HEVC_SPS_BROKEN |
8583 | 915 | } |
8584 | 31.3k | sps->max_CU_width = (1 << (sps->log2_min_luma_coding_block_size + sps->log2_diff_max_min_luma_coding_block_size)); |
8585 | 31.3k | sps->max_CU_height = (1 << (sps->log2_min_luma_coding_block_size + sps->log2_diff_max_min_luma_coding_block_size)); |
8586 | | |
8587 | 31.3k | sps->log2_min_transform_block_size = 2 + gf_bs_read_ue_log(bs, "log2_min_transform_block_size_minus2"); |
8588 | 31.3k | sps->log2_max_transform_block_size = sps->log2_min_transform_block_size + gf_bs_read_ue_log(bs, "log2_max_transform_block_size"); |
8589 | | //The CVS shall not contain data that result in MinTbLog2SizeY greater than or equal to MinCbLog2SizeY |
8590 | 31.3k | if (sps->log2_min_transform_block_size/*MinTbLog2SizeY*/ >= sps->log2_min_luma_coding_block_size/*MinCbLog2SizeY*/) |
8591 | 1.88k | HEVC_SPS_BROKEN |
8592 | | //The CVS shall not contain data that result in MaxTbLog2SizeY greater than Min( CtbLog2SizeY, 5 ). |
8593 | 29.4k | if (sps->log2_max_transform_block_size /*MaxTbLog2SizeY*/ > MIN( sps->log2_min_luma_coding_block_size+ sps->log2_diff_max_min_luma_coding_block_size /*CtbLog2SizeY*/, 5 )) |
8594 | 1.21k | HEVC_SPS_BROKEN |
8595 | | |
8596 | 28.2k | depth = 0; |
8597 | 28.2k | sps->max_transform_hierarchy_depth_inter = gf_bs_read_ue_log(bs, "max_transform_hierarchy_depth_inter"); |
8598 | 28.2k | sps->max_transform_hierarchy_depth_intra = gf_bs_read_ue_log(bs, "max_transform_hierarchy_depth_intra"); |
8599 | 117k | while ((u32)(sps->max_CU_width >> sps->log2_diff_max_min_luma_coding_block_size) > (u32)(1 << (sps->log2_min_transform_block_size + depth))) |
8600 | 88.7k | { |
8601 | 88.7k | depth++; |
8602 | 88.7k | } |
8603 | 28.2k | sps->max_CU_depth = sps->log2_diff_max_min_luma_coding_block_size + depth; |
8604 | | |
8605 | 28.2k | nb_CTUs = ((sps->width + sps->max_CU_width - 1) / sps->max_CU_width) * ((sps->height + sps->max_CU_height - 1) / sps->max_CU_height); |
8606 | 28.2k | sps->bitsSliceSegmentAddress = 0; |
8607 | 30.4k | while (nb_CTUs > (u32)(1 << sps->bitsSliceSegmentAddress)) { |
8608 | 2.13k | sps->bitsSliceSegmentAddress++; |
8609 | 2.13k | if (sps->bitsSliceSegmentAddress==31) |
8610 | 2 | HEVC_SPS_BROKEN |
8611 | 2.13k | } |
8612 | | |
8613 | 28.2k | sps->scaling_list_enable_flag = gf_bs_read_int_log(bs, 1, "scaling_list_enable_flag"); |
8614 | 28.2k | if (sps->scaling_list_enable_flag) { |
8615 | 10.2k | sps->infer_scaling_list_flag = 0; |
8616 | 10.2k | sps->scaling_list_ref_layer_id = 0; |
8617 | 10.2k | if (multiLayerExtSpsFlag) { |
8618 | 6.73k | sps->infer_scaling_list_flag = gf_bs_read_int_log(bs, 1, "infer_scaling_list_flag"); |
8619 | 6.73k | } |
8620 | 10.2k | if (sps->infer_scaling_list_flag) { |
8621 | 6.60k | sps->scaling_list_ref_layer_id = gf_bs_read_int_log(bs, 6, "scaling_list_ref_layer_id"); |
8622 | 6.60k | } |
8623 | 3.65k | else { |
8624 | 3.65k | sps->scaling_list_data_present_flag = gf_bs_read_int_log(bs, 1, "scaling_list_data_present_flag"); |
8625 | 3.65k | if (sps->scaling_list_data_present_flag) { |
8626 | 2.45k | hevc_scaling_list_data(bs); |
8627 | 2.45k | } |
8628 | 3.65k | } |
8629 | 10.2k | } |
8630 | 28.2k | sps->asymmetric_motion_partitions_enabled_flag = gf_bs_read_int_log(bs, 1, "asymmetric_motion_partitions_enabled_flag"); |
8631 | 28.2k | sps->sample_adaptive_offset_enabled_flag = gf_bs_read_int_log(bs, 1, "sample_adaptive_offset_enabled_flag"); |
8632 | 28.2k | if ( (sps->pcm_enabled_flag = gf_bs_read_int_log(bs, 1, "pcm_enabled_flag")) ) { |
8633 | 8.50k | sps->pcm_sample_bit_depth_luma_minus1 = gf_bs_read_int_log(bs, 4, "pcm_sample_bit_depth_luma_minus1"); |
8634 | 8.50k | sps->pcm_sample_bit_depth_chroma_minus1 = gf_bs_read_int_log(bs, 4, "pcm_sample_bit_depth_chroma_minus1"); |
8635 | 8.50k | sps->log2_min_pcm_luma_coding_block_size_minus3 = gf_bs_read_ue_log(bs, "log2_min_pcm_luma_coding_block_size_minus3"); |
8636 | 8.50k | sps->log2_diff_max_min_pcm_luma_coding_block_size = gf_bs_read_ue_log(bs, "log2_diff_max_min_pcm_luma_coding_block_size"); |
8637 | 8.50k | sps->pcm_loop_filter_disable_flag = gf_bs_read_int_log(bs, 1, "pcm_loop_filter_disable_flag"); |
8638 | 8.50k | } |
8639 | 28.2k | sps->num_short_term_ref_pic_sets = gf_bs_read_ue_log(bs, "num_short_term_ref_pic_sets"); |
8640 | 28.2k | if (sps->num_short_term_ref_pic_sets > 64) { |
8641 | 35 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid number of short term reference picture sets %d\n", sps->num_short_term_ref_pic_sets)); |
8642 | 35 | HEVC_SPS_BROKEN |
8643 | 35 | } |
8644 | | |
8645 | 43.5k | for (i = 0; i < sps->num_short_term_ref_pic_sets; i++) { |
8646 | 15.8k | Bool ret = hevc_parse_short_term_ref_pic_set(bs, sps, i); |
8647 | | /*cannot parse short_term_ref_pic_set, skip VUI parsing*/ |
8648 | 15.8k | if (!ret) { |
8649 | 472 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid short_term_ref_pic_set\n")); |
8650 | 472 | HEVC_SPS_BROKEN |
8651 | 472 | } |
8652 | 15.8k | } |
8653 | 27.7k | sps->long_term_ref_pics_present_flag = gf_bs_read_int_log(bs, 1, "long_term_ref_pics_present_flag"); |
8654 | 27.7k | if (sps->long_term_ref_pics_present_flag) { |
8655 | 9.27k | sps->num_long_term_ref_pic_sps = gf_bs_read_ue_log(bs, "num_long_term_ref_pic_sps"); |
8656 | 14.0k | for (i = 0; i < sps->num_long_term_ref_pic_sps; i++) { |
8657 | 4.82k | gf_bs_read_int_log_idx(bs, sps->log2_max_pic_order_cnt_lsb, "lt_ref_pic_poc_lsb_sps", i); |
8658 | 4.82k | gf_bs_read_int_log_idx(bs, 1, "used_by_curr_pic_lt_sps_flag", i); |
8659 | 4.82k | } |
8660 | 9.27k | } |
8661 | 27.7k | sps->temporal_mvp_enable_flag = gf_bs_read_int_log(bs, 1, "temporal_mvp_enable_flag"); |
8662 | 27.7k | sps->strong_intra_smoothing_enable_flag = gf_bs_read_int_log(bs, 1, "strong_intra_smoothing_enable_flag"); |
8663 | | |
8664 | 27.7k | if (vui_flag_pos) |
8665 | 0 | *vui_flag_pos = (u32)gf_bs_get_bit_offset(bs); |
8666 | | |
8667 | 27.7k | if ((sps->vui_parameters_present_flag = gf_bs_read_int_log(bs, 1, "vui_parameters_present_flag")) ) { |
8668 | 9.31k | sps->aspect_ratio_info_present_flag = gf_bs_read_int_log(bs, 1, "aspect_ratio_info_present_flag"); |
8669 | 9.31k | if (sps->aspect_ratio_info_present_flag) { |
8670 | 8.79k | sps->sar_idc = gf_bs_read_int_log(bs, 8, "aspect_ratio_idc"); |
8671 | 8.79k | if (sps->sar_idc == 255) { |
8672 | 4.27k | sps->sar_width = gf_bs_read_int_log(bs, 16, "aspect_ratio_width"); |
8673 | 4.27k | sps->sar_height = gf_bs_read_int_log(bs, 16, "aspect_ratio_height"); |
8674 | 4.27k | } |
8675 | 4.51k | else if (sps->sar_idc < 17) { |
8676 | 1.52k | sps->sar_width = hevc_sar[sps->sar_idc].w; |
8677 | 1.52k | sps->sar_height = hevc_sar[sps->sar_idc].h; |
8678 | 1.52k | } |
8679 | 8.79k | } |
8680 | | |
8681 | 9.31k | if ((sps->overscan_info_present = gf_bs_read_int_log(bs, 1, "overscan_info_present"))) |
8682 | 4.28k | sps->overscan_appropriate = gf_bs_read_int_log(bs, 1, "overscan_appropriate"); |
8683 | | |
8684 | 9.31k | sps->video_signal_type_present_flag = gf_bs_read_int_log(bs, 1, "video_signal_type_present_flag"); |
8685 | 9.31k | if (sps->video_signal_type_present_flag) { |
8686 | 5.99k | sps->video_format = gf_bs_read_int_log(bs, 3, "video_format"); |
8687 | 5.99k | sps->video_full_range_flag = gf_bs_read_int_log(bs, 1, "video_full_range_flag"); |
8688 | 5.99k | if ((sps->colour_description_present_flag = gf_bs_read_int_log(bs, 1, "colour_description_present_flag"))) { |
8689 | 4.43k | sps->colour_primaries = gf_bs_read_int_log(bs, 8, "colour_primaries"); |
8690 | 4.43k | sps->transfer_characteristic = gf_bs_read_int_log(bs, 8, "transfer_characteristic"); |
8691 | 4.43k | sps->matrix_coeffs = gf_bs_read_int_log(bs, 8, "matrix_coefficients"); |
8692 | 4.43k | } |
8693 | 5.99k | } |
8694 | | |
8695 | 9.31k | if ((sps->chroma_loc_info_present_flag = gf_bs_read_int_log(bs, 1, "chroma_loc_info_present_flag"))) { |
8696 | 5.22k | sps->chroma_sample_loc_type_top_field = gf_bs_read_ue_log(bs, "chroma_sample_loc_type_top_field"); |
8697 | 5.22k | sps->chroma_sample_loc_type_bottom_field = gf_bs_read_ue_log(bs, "chroma_sample_loc_type_bottom_field"); |
8698 | 5.22k | } |
8699 | | |
8700 | 9.31k | sps->neutral_chroma_indication_flag = gf_bs_read_int_log(bs, 1, "neutral_chroma_indication_flag"); |
8701 | 9.31k | sps->field_seq_flag = gf_bs_read_int_log(bs, 1, "field_seq_flag"); |
8702 | 9.31k | sps->frame_field_info_present_flag = gf_bs_read_int_log(bs, 1, "frame_field_info_present_flag"); |
8703 | | |
8704 | 9.31k | if ((sps->default_display_window_flag = gf_bs_read_int_log(bs, 1, "default_display_window_flag"))) { |
8705 | 3.30k | sps->left_offset = gf_bs_read_ue_log(bs, "display_window_left_offset"); |
8706 | 3.30k | sps->right_offset = gf_bs_read_ue_log(bs, "display_window_right_offset"); |
8707 | 3.30k | sps->top_offset = gf_bs_read_ue_log(bs, "display_window_top_offset"); |
8708 | 3.30k | sps->bottom_offset = gf_bs_read_ue_log(bs, "display_window_bottom_offset"); |
8709 | 3.30k | } |
8710 | | |
8711 | 9.31k | sps->has_timing_info = gf_bs_read_int_log(bs, 1, "has_timing_info"); |
8712 | 9.31k | if (sps->has_timing_info) { |
8713 | 4.92k | sps->num_units_in_tick = gf_bs_read_int_log(bs, 32, "num_units_in_tick"); |
8714 | 4.92k | sps->time_scale = gf_bs_read_int_log(bs, 32, "time_scale"); |
8715 | 4.92k | sps->poc_proportional_to_timing_flag = gf_bs_read_int_log(bs, 1, "poc_proportional_to_timing_flag"); |
8716 | 4.92k | if (sps->poc_proportional_to_timing_flag) |
8717 | 2.86k | sps->num_ticks_poc_diff_one_minus1 = gf_bs_read_ue_log(bs, "num_ticks_poc_diff_one_minus1"); |
8718 | 4.92k | if ((sps->hrd_parameters_present_flag = gf_bs_read_int_log(bs, 1, "hrd_parameters_present_flag"))) { |
8719 | | // GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[HEVC] HRD param parsing not implemented\n")); |
8720 | 1.82k | return sps_id; |
8721 | 1.82k | } |
8722 | 4.92k | } |
8723 | | |
8724 | 7.48k | if (gf_bs_read_int_log(bs, 1, "bitstream_restriction_flag")) { |
8725 | 1.68k | gf_bs_read_int_log(bs, 1, "tiles_fixed_structure_flag"); |
8726 | 1.68k | gf_bs_read_int_log(bs, 1, "motion_vectors_over_pic_boundaries_flag"); |
8727 | 1.68k | gf_bs_read_int_log(bs, 1, "restricted_ref_pic_lists_flag"); |
8728 | 1.68k | gf_bs_read_ue_log(bs, "min_spatial_segmentation_idc"); |
8729 | 1.68k | gf_bs_read_ue_log(bs, "max_bytes_per_pic_denom"); |
8730 | 1.68k | gf_bs_read_ue_log(bs, "max_bits_per_min_cu_denom"); |
8731 | 1.68k | gf_bs_read_ue_log(bs, "log2_max_mv_length_horizontal"); |
8732 | 1.68k | gf_bs_read_ue_log(bs, "log2_max_mv_length_vertical"); |
8733 | 1.68k | } |
8734 | 7.48k | } |
8735 | | |
8736 | 25.9k | if (gf_bs_read_int_log(bs, 1, "sps_extension_flag")) { |
8737 | | #if 0 |
8738 | | while (gf_bs_available(bs)) { |
8739 | | /*sps_extension_data_flag */ gf_bs_read_int(bs, 1); |
8740 | | } |
8741 | | #endif |
8742 | | |
8743 | 5.67k | } |
8744 | 25.9k | if (gf_bs_is_overflow(bs)) |
8745 | 13.4k | HEVC_SPS_BROKEN |
8746 | 12.4k | return sps_id; |
8747 | 25.9k | } |
8748 | | |
8749 | | GF_EXPORT |
8750 | | s32 gf_hevc_read_sps_ex(char *data, u32 size, HEVCState *hevc, u32 *vui_flag_pos) |
8751 | 0 | { |
8752 | 0 | GF_BitStream *bs; |
8753 | 0 | s32 sps_id = -1; |
8754 | 0 | u8 layer_id; |
8755 | |
|
8756 | 0 | if (vui_flag_pos) *vui_flag_pos = 0; |
8757 | |
|
8758 | 0 | bs = gf_bs_new(data, size, GF_BITSTREAM_READ); |
8759 | 0 | if (!bs) goto exit; |
8760 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
8761 | |
|
8762 | 0 | if (!hevc_parse_nal_header(bs, NULL, NULL, &layer_id)) goto exit; |
8763 | 0 | sps_id = gf_hevc_read_sps_bs_internal(bs, hevc, layer_id, vui_flag_pos); |
8764 | |
|
8765 | 0 | exit: |
8766 | 0 | if (bs) gf_bs_del(bs); |
8767 | 0 | return sps_id; |
8768 | 0 | } |
8769 | | |
8770 | | GF_EXPORT |
8771 | | s32 gf_hevc_read_sps(u8 *data, u32 size, HEVCState *hevc) |
8772 | 0 | { |
8773 | 0 | return gf_hevc_read_sps_ex(data, size, hevc, NULL); |
8774 | 0 | } |
8775 | | |
8776 | | GF_EXPORT |
8777 | | s32 gf_hevc_read_sps_bs(GF_BitStream *bs, HEVCState *hevc) |
8778 | 0 | { |
8779 | 0 | u8 layer_id; |
8780 | 0 | if (!bs || !hevc) return -1; |
8781 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
8782 | 0 | if (!hevc_parse_nal_header(bs, NULL, NULL, &layer_id)) return -1; |
8783 | 0 | return gf_hevc_read_sps_bs_internal(bs, hevc, layer_id, NULL); |
8784 | 0 | } |
8785 | | |
8786 | 22.5k | #define HEVC_PPS_BROKEN {\ |
8787 | 22.5k | memset(pps, 0, sizeof(HEVC_PPS)); \ |
8788 | 22.5k | return -1;\ |
8789 | 22.5k | } |
8790 | | |
8791 | | static void hevc_color_map_octants(GF_BitStream *bs, u32 cm_octant_depth, u32 OctantNumY, u32 PartNumY, u32 CMResLSBits, u32 inpDepth, u32 idxY, u32 idxCb, u32 idxCr, u32 inpLength) |
8792 | 27.2k | { |
8793 | 27.2k | u32 i, j, c, k, n, m; |
8794 | 27.2k | Bool split_octant_flag=GF_FALSE; |
8795 | 27.2k | if ( inpDepth < cm_octant_depth ) |
8796 | 12.7k | split_octant_flag = gf_bs_read_int_log(bs, 1, "split_octant_flag"); |
8797 | 27.2k | if (split_octant_flag) { |
8798 | 8.85k | for (k=0; k<2; k++) { |
8799 | 17.7k | for (m=0; m<2; m++) { |
8800 | 35.4k | for (n=0; n<2; n++) { |
8801 | 23.6k | hevc_color_map_octants(bs, cm_octant_depth, OctantNumY, PartNumY, CMResLSBits, inpDepth + 1, idxY + PartNumY * k * inpLength / 2, idxCb + m * inpLength / 2, idxCr + n * inpLength / 2, inpLength / 2 ); |
8802 | 23.6k | } |
8803 | 11.8k | } |
8804 | 5.90k | } |
8805 | 2.95k | return; |
8806 | 2.95k | } |
8807 | 179k | for (i=0; i<PartNumY; i++) { |
8808 | | // idxShiftY = idxY + (i << (cm_octant_depth-inpDepth)); |
8809 | 774k | for (j=0; j<4; j++) { |
8810 | 619k | if (gf_bs_read_int_log_idx2(bs, 1, "coded_res_flag", PartNumY, j)) { |
8811 | 545k | for (c=0; c<3; c++) { |
8812 | 409k | u32 v = gf_bs_read_ue_log_idx3(bs, "res_coeff_q", PartNumY, j, c); |
8813 | 409k | v += gf_bs_read_int_log_idx3(bs, CMResLSBits, "res_coeff_r", PartNumY, j, c); |
8814 | 409k | if (v) { |
8815 | 388k | gf_bs_read_int_log_idx3(bs, 1, "res_coeff_s", PartNumY, j, c); |
8816 | 388k | } |
8817 | 409k | } |
8818 | 136k | } |
8819 | 619k | } |
8820 | 154k | } |
8821 | 24.3k | } |
8822 | | |
8823 | | static s32 gf_hevc_read_pps_bs_internal(GF_BitStream *bs, HEVCState *hevc) |
8824 | 94.7k | { |
8825 | 94.7k | u32 i; |
8826 | 94.7k | s32 pps_id; |
8827 | 94.7k | HEVC_PPS *pps; |
8828 | | |
8829 | | //NAL header already read |
8830 | 94.7k | pps_id = gf_bs_read_ue_log(bs, "pps_id"); |
8831 | | |
8832 | 94.7k | if ((pps_id < 0) || (pps_id >= 64)) { |
8833 | 2.49k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] wrong PPS ID %d in PPS\n", pps_id)); |
8834 | 2.49k | return -1; |
8835 | 2.49k | } |
8836 | 92.2k | pps = &hevc->pps[pps_id]; |
8837 | | |
8838 | 92.2k | if (!pps->state) { |
8839 | 49.9k | pps->id = pps_id; |
8840 | 49.9k | pps->state = 1; |
8841 | 49.9k | } |
8842 | 92.2k | pps->sps_id = gf_bs_read_ue_log(bs, "sps_id"); |
8843 | 92.2k | if (((s32)pps->sps_id<0) || (pps->sps_id >= 16)) { |
8844 | 924 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] wrong SPS ID %d in PPS\n", pps->sps_id)); |
8845 | 924 | HEVC_PPS_BROKEN |
8846 | 924 | } |
8847 | 91.2k | hevc->sps_active_idx = pps->sps_id; /*set active sps*/ |
8848 | 91.2k | pps->dependent_slice_segments_enabled_flag = gf_bs_read_int_log(bs, 1, "dependent_slice_segments_enabled_flag"); |
8849 | | |
8850 | 91.2k | pps->output_flag_present_flag = gf_bs_read_int_log(bs, 1, "output_flag_present_flag"); |
8851 | 91.2k | pps->num_extra_slice_header_bits = gf_bs_read_int_log(bs, 3, "num_extra_slice_header_bits"); |
8852 | 91.2k | pps->sign_data_hiding_flag = gf_bs_read_int_log(bs, 1, "sign_data_hiding_flag"); |
8853 | 91.2k | pps->cabac_init_present_flag = gf_bs_read_int_log(bs, 1, "cabac_init_present_flag"); |
8854 | 91.2k | pps->num_ref_idx_l0_default_active = 1 + gf_bs_read_ue_log(bs, "num_ref_idx_l0_default_active"); |
8855 | 91.2k | pps->num_ref_idx_l1_default_active = 1 + gf_bs_read_ue_log(bs, "num_ref_idx_l1_default_active"); |
8856 | 91.2k | pps->pic_init_qp_minus26 = gf_bs_read_se_log(bs, "pic_init_qp_minus26"); |
8857 | 91.2k | pps->constrained_intra_pred_flag = gf_bs_read_int_log(bs, 1, "constrained_intra_pred_flag"); |
8858 | 91.2k | pps->transform_skip_enabled_flag = gf_bs_read_int_log(bs, 1, "transform_skip_enabled_flag"); |
8859 | 91.2k | if ((pps->cu_qp_delta_enabled_flag = gf_bs_read_int_log(bs, 1, "cu_qp_delta_enabled_flag"))) |
8860 | 54.1k | pps->diff_cu_qp_delta_depth = gf_bs_read_ue_log(bs, "diff_cu_qp_delta_depth"); |
8861 | | |
8862 | 91.2k | pps->pic_cb_qp_offset = gf_bs_read_se_log(bs, "pic_cb_qp_offset"); |
8863 | 91.2k | pps->pic_cr_qp_offset = gf_bs_read_se_log(bs, "pic_cr_qp_offset"); |
8864 | 91.2k | pps->slice_chroma_qp_offsets_present_flag = gf_bs_read_int_log(bs, 1, "slice_chroma_qp_offsets_present_flag"); |
8865 | 91.2k | pps->weighted_pred_flag = gf_bs_read_int_log(bs, 1, "weighted_pred_flag"); |
8866 | 91.2k | pps->weighted_bipred_flag = gf_bs_read_int_log(bs, 1, "weighted_bipred_flag"); |
8867 | 91.2k | pps->transquant_bypass_enable_flag = gf_bs_read_int_log(bs, 1, "transquant_bypass_enable_flag"); |
8868 | 91.2k | pps->tiles_enabled_flag = gf_bs_read_int_log(bs, 1, "tiles_enabled_flag"); |
8869 | 91.2k | pps->entropy_coding_sync_enabled_flag = gf_bs_read_int_log(bs, 1, "entropy_coding_sync_enabled_flag"); |
8870 | 91.2k | if (pps->tiles_enabled_flag) { |
8871 | 58.0k | pps->num_tile_columns = 1 + gf_bs_read_ue_log(bs, "num_tile_columns_minus1"); |
8872 | 58.0k | if (pps->num_tile_columns > 22) { |
8873 | 439 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid num_tile_columns %u\n", pps->num_tile_columns)); |
8874 | 439 | HEVC_PPS_BROKEN |
8875 | 439 | } |
8876 | 57.6k | pps->num_tile_rows = 1 + gf_bs_read_ue_log(bs, "num_tile_rows_minus1"); |
8877 | 57.6k | if (pps->num_tile_rows > 20) { |
8878 | 56 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid num_tile_rows %u\n", pps->num_tile_rows)); |
8879 | 56 | HEVC_PPS_BROKEN |
8880 | 56 | } |
8881 | 57.5k | pps->uniform_spacing_flag = gf_bs_read_int_log(bs, 1, "uniform_spacing_flag"); |
8882 | 57.5k | if (!pps->uniform_spacing_flag) { |
8883 | 147k | for (i = 0; i < pps->num_tile_columns - 1; i++) { |
8884 | 119k | pps->column_width[i] = 1 + gf_bs_read_ue_log_idx(bs, "column_width_minus1", i); |
8885 | 119k | } |
8886 | 73.4k | for (i = 0; i < pps->num_tile_rows - 1; i++) { |
8887 | 45.1k | pps->row_height[i] = 1 + gf_bs_read_ue_log_idx(bs, "row_height_minus1", i); |
8888 | 45.1k | } |
8889 | 28.2k | } |
8890 | 57.5k | pps->loop_filter_across_tiles_enabled_flag = gf_bs_read_int_log(bs, 1, "loop_filter_across_tiles_enabled_flag"); |
8891 | 57.5k | } |
8892 | 90.7k | pps->loop_filter_across_slices_enabled_flag = gf_bs_read_int_log(bs, 1, "loop_filter_across_slices_enabled_flag"); |
8893 | 90.7k | if ((pps->deblocking_filter_control_present_flag = gf_bs_read_int_log(bs, 1, "deblocking_filter_control_present_flag"))) { |
8894 | 28.2k | pps->deblocking_filter_override_enabled_flag = gf_bs_read_int_log(bs, 1, "deblocking_filter_override_enabled_flag"); |
8895 | 28.2k | if (! (pps->pic_disable_deblocking_filter_flag = gf_bs_read_int_log(bs, 1, "pic_disable_deblocking_filter_flag"))) { |
8896 | 16.3k | pps->beta_offset_div2 = gf_bs_read_se_log(bs, "beta_offset_div2"); |
8897 | 16.3k | pps->tc_offset_div2 = gf_bs_read_se_log(bs, "tc_offset_div2"); |
8898 | 16.3k | } |
8899 | 28.2k | } |
8900 | 90.7k | if ((pps->pic_scaling_list_data_present_flag = gf_bs_read_int_log(bs, 1, "pic_scaling_list_data_present_flag"))) { |
8901 | 6.46k | hevc_scaling_list_data(bs); |
8902 | 6.46k | } |
8903 | 90.7k | pps->lists_modification_present_flag = gf_bs_read_int_log(bs, 1, "lists_modification_present_flag"); |
8904 | 90.7k | pps->log2_parallel_merge_level_minus2 = gf_bs_read_ue_log(bs, "log2_parallel_merge_level_minus2"); |
8905 | 90.7k | pps->slice_segment_header_extension_present_flag = gf_bs_read_int_log(bs, 1, "slice_segment_header_extension_present_flag"); |
8906 | | |
8907 | 90.7k | u8 pps_range_extension_flag=0; |
8908 | 90.7k | u8 pps_multilayer_extension_flag=0; |
8909 | 90.7k | u8 pps_3d_extension_flag=0; |
8910 | 90.7k | u8 pps_scc_extension_flag=0; |
8911 | 90.7k | if (gf_bs_read_int_log(bs, 1, "pps_extension_flag")) { |
8912 | 27.1k | pps_range_extension_flag = gf_bs_read_int_log(bs, 1, "pps_range_extension_flag"); |
8913 | 27.1k | pps_multilayer_extension_flag = gf_bs_read_int_log(bs, 1, "pps_multilayer_extension_flag"); |
8914 | 27.1k | pps_3d_extension_flag = gf_bs_read_int_log(bs, 1, "pps_3d_extension_flag"); |
8915 | 27.1k | pps_scc_extension_flag = gf_bs_read_int_log(bs, 1, "pps_scc_extension_flag"); |
8916 | 27.1k | gf_bs_read_int_log(bs, 4, "pps_extension_4bits"); |
8917 | 27.1k | } |
8918 | 90.7k | if (pps_range_extension_flag) { |
8919 | 15.1k | if (pps->transform_skip_enabled_flag) |
8920 | 9.48k | gf_bs_read_ue_log(bs, "log2_max_transform_skip_block_size_minus2"); |
8921 | 15.1k | gf_bs_read_int_log(bs, 1, "cross_component_prediction_enabled_flag"); |
8922 | 15.1k | u8 flag = gf_bs_read_int_log(bs, 1, "chroma_qp_offset_list_enabled_flag"); |
8923 | 15.1k | if (flag) { |
8924 | 3.50k | gf_bs_read_ue_log(bs, "diff_cu_chroma_qp_offset_depth"); |
8925 | 3.50k | u32 nb_chroma = 1 + gf_bs_read_ue_log(bs, "chroma_qp_offset_list_len_minus1"); |
8926 | 181k | for (i=0; i<nb_chroma; i++) { |
8927 | 177k | gf_bs_read_se_log_idx(bs, "cb_qp_offset_list", i); |
8928 | 177k | gf_bs_read_se_log_idx(bs, "cr_qp_offset_list", i); |
8929 | 177k | } |
8930 | 3.50k | } |
8931 | 15.1k | gf_bs_read_ue_log(bs, "log2_sao_offset_scale_luma"); |
8932 | 15.1k | gf_bs_read_ue_log(bs, "log2_sao_offset_scale_chroma"); |
8933 | 15.1k | } |
8934 | 90.7k | if (pps_multilayer_extension_flag ) { |
8935 | 11.0k | gf_bs_read_int_log(bs, 1, "poc_reset_info_present_flag"); |
8936 | 11.0k | if (gf_bs_read_int_log(bs, 1, "pps_infer_scaling_list_flag")) { |
8937 | 3.16k | gf_bs_read_int_log(bs, 6, "pps_scaling_list_ref_layer_id"); |
8938 | 3.16k | } |
8939 | 11.0k | u32 nb_refs = gf_bs_read_ue_log(bs, "num_ref_loc_offsets"); |
8940 | 54.7M | for (i=0; i<nb_refs; i++) { |
8941 | 54.7M | gf_bs_read_int_log_idx(bs, 6, "ref_loc_offset_layer_id", i); |
8942 | 54.7M | if (gf_bs_read_int_log_idx(bs, 1, "scaled_ref_layer_offset_present_flag", i)) { |
8943 | 85.0k | gf_bs_read_se_log_idx(bs, "scaled_ref_layer_left_offset", i); |
8944 | 85.0k | gf_bs_read_se_log_idx(bs, "scaled_ref_layer_top_offset", i); |
8945 | 85.0k | gf_bs_read_se_log_idx(bs, "scaled_ref_layer_right_offset", i); |
8946 | 85.0k | gf_bs_read_se_log_idx(bs, "scaled_ref_layer_bottom_offset", i); |
8947 | 85.0k | } |
8948 | 54.7M | if (gf_bs_read_int_log_idx(bs, 1, "ref_region_offset_present_flag", i)) { |
8949 | 76.3k | gf_bs_read_se_log_idx(bs, "ref_region_left_offset", i); |
8950 | 76.3k | gf_bs_read_se_log_idx(bs, "ref_region_top_offset", i); |
8951 | 76.3k | gf_bs_read_se_log_idx(bs, "ref_region_right_offset", i); |
8952 | 76.3k | gf_bs_read_se_log_idx(bs, "ref_region_bottom_offset", i); |
8953 | 76.3k | } |
8954 | 54.7M | if (gf_bs_read_int_log_idx(bs, 1, "resample_phase_set_present_flag", i)) { |
8955 | 79.1k | gf_bs_read_ue_log_idx(bs, "phase_hor_luma", i); |
8956 | 79.1k | gf_bs_read_ue_log_idx(bs, "phase_ver_luma", i); |
8957 | 79.1k | gf_bs_read_ue_log_idx(bs, "phase_hor_chroma_plus8", i); |
8958 | 79.1k | gf_bs_read_ue_log_idx(bs, "phase_ver_chroma_plus8", i); |
8959 | 79.1k | } |
8960 | 54.7M | } |
8961 | 11.0k | if (gf_bs_read_int_log(bs, 1, "colour_mapping_enabled_flag")) { |
8962 | 3.64k | u32 nb_vals = gf_bs_read_ue_log(bs, "num_cm_ref_layers_minus1"); |
8963 | 8.43k | for (i=0; i<nb_vals; i++) { |
8964 | 4.79k | gf_bs_read_int_log_idx(bs, 6, "cm_ref_layer_id", i); |
8965 | 4.79k | } |
8966 | 3.64k | u32 cm_octant_depth = gf_bs_read_int_log(bs, 2, "cm_octant_depth"); |
8967 | 3.64k | u32 cm_y_part_num_log2 = gf_bs_read_int_log(bs, 2, "cm_y_part_num_log2"); |
8968 | 3.64k | u32 BitDepthCmInputY = 8 + gf_bs_read_ue_log(bs, "luma_bit_depth_cm_input_minus8"); |
8969 | 3.64k | gf_bs_read_ue_log(bs, "chroma_bit_depth_cm_input_minus8"); |
8970 | 3.64k | u32 BitDepthCmOutputY = 8 + gf_bs_read_ue_log(bs, "luma_bit_depth_cm_output_minus8"); |
8971 | 3.64k | gf_bs_read_ue_log(bs, "chroma_bit_depth_cm_output_minus8"); |
8972 | 3.64k | u32 cm_res_quant_bits = gf_bs_read_int_log(bs, 2, "cm_res_quant_bits"); |
8973 | 3.64k | u32 cm_delta_flc_bits_minus1 = gf_bs_read_int_log(bs, 2, "cm_delta_flc_bits_minus1"); |
8974 | 3.64k | if (cm_octant_depth == 1) { |
8975 | 277 | gf_bs_read_se_log(bs, "cm_adapt_threshold_u_delta"); |
8976 | 277 | gf_bs_read_se_log(bs, "cm_adapt_threshold_v_delta"); |
8977 | 277 | } |
8978 | 3.64k | u32 OctantNumY = 1 << ( cm_octant_depth + cm_y_part_num_log2 ); |
8979 | 3.64k | u32 PartNumY = 1 << cm_y_part_num_log2; |
8980 | 3.64k | u32 CMResLSBits = MAX( 0, (10 + BitDepthCmInputY - BitDepthCmOutputY - cm_res_quant_bits - (cm_delta_flc_bits_minus1 + 1 ) ) ); |
8981 | | |
8982 | 3.64k | hevc_color_map_octants(bs, cm_octant_depth, OctantNumY, PartNumY, CMResLSBits, 0, 0, 0, 0, 1 << cm_octant_depth); |
8983 | | |
8984 | 3.64k | } |
8985 | 11.0k | } |
8986 | 90.7k | if (pps_3d_extension_flag) { |
8987 | 10.0k | if (gf_bs_read_int_log(bs, 1, "dlts_present_flag")) { |
8988 | 2.02k | u32 pps_depth_layers = 1 + gf_bs_read_int_log(bs, 6, "pps_depth_layers_minus1"); |
8989 | 2.02k | u32 pps_bit_depth_for_depth_layers = 8 + gf_bs_read_int_log(bs, 4, "pps_bit_depth_for_depth_layers_minus8"); |
8990 | 43.2k | for (i=0; i<pps_depth_layers; i++) { |
8991 | 41.2k | if (gf_bs_read_int_log_idx(bs, 1, "dlt_flag", i)) { |
8992 | 4.44k | Bool flag_resent = GF_FALSE; |
8993 | 4.44k | if (!gf_bs_read_int_log_idx(bs, 1, "dlt_pred_flag", i)) { |
8994 | 2.45k | flag_resent = gf_bs_read_int_log_idx(bs, 1, "dlt_val_flags_present_flag", i); |
8995 | 2.45k | } |
8996 | 4.44k | if (flag_resent) { |
8997 | 1.53k | u32 j, depthMaxValue = (1 << (pps_bit_depth_for_depth_layers) ) - 1; |
8998 | 17.8M | for (j=0; j<depthMaxValue; j++) { |
8999 | 17.8M | gf_bs_read_int_log_idx2(bs, 1, "dlt_value_flag", i, j); |
9000 | 17.8M | } |
9001 | 2.90k | } else { |
9002 | 2.90k | u32 num_val_delta_dlt = gf_bs_read_int_log_idx(bs, pps_bit_depth_for_depth_layers, "num_val_delta_dlt", i); |
9003 | 2.90k | if (num_val_delta_dlt>0) { |
9004 | 2.68k | u32 max_diff = 0; |
9005 | 2.68k | u32 min_diff=0; |
9006 | 2.68k | if (num_val_delta_dlt>1) |
9007 | 2.67k | max_diff = gf_bs_read_int_log_idx(bs, pps_bit_depth_for_depth_layers, "max_diff", i); |
9008 | | |
9009 | 2.68k | if ((num_val_delta_dlt>2) && (max_diff>0)) { |
9010 | 2.58k | u32 nb_bits = 1, val = max_diff; //+1 - 1 for ceil(log2) |
9011 | 22.0k | while ( val >>= 1) nb_bits++; |
9012 | 2.58k | min_diff = 1+gf_bs_read_int_log_idx(bs, nb_bits, "min_diff_minus1", i); |
9013 | 2.58k | } else { |
9014 | 94 | min_diff = max_diff; |
9015 | 94 | } |
9016 | | //delta_dlt_val0 = |
9017 | 2.68k | gf_bs_read_int_log_idx(bs, pps_bit_depth_for_depth_layers, "delta_dlt_val0", i); |
9018 | 2.68k | if (max_diff > min_diff) { |
9019 | 2.07k | u32 k; |
9020 | 2.07k | u32 nb_bits = 1, val = max_diff - min_diff; |
9021 | 15.2k | while ( val >>= 1) nb_bits++; |
9022 | 107M | for (k=1; k<num_val_delta_dlt; k++) { |
9023 | 107M | gf_bs_read_int_log_idx2(bs, nb_bits, "delta_val_diff_minus_min", i, k); |
9024 | 107M | } |
9025 | 2.07k | } |
9026 | 2.68k | } |
9027 | 2.90k | } |
9028 | 4.44k | } |
9029 | 41.2k | } |
9030 | 2.02k | } |
9031 | 10.0k | } |
9032 | 90.7k | if (pps_scc_extension_flag) { |
9033 | 15.8k | pps->curr_pic_ref_enabled_flag = gf_bs_read_int_log(bs, 1, "pps_curr_pic_ref_enabled_flag"); |
9034 | 15.8k | if (gf_bs_read_int_log(bs, 1, "residual_adaptive_colour_transform_enabled_flag")) { |
9035 | 2.11k | gf_bs_read_int_log(bs, 1, "pps_slice_act_qp_offsets_present_flag"); |
9036 | 2.11k | gf_bs_read_se_log(bs, "pps_act_y_qp_offset_plus5"); |
9037 | 2.11k | gf_bs_read_se_log(bs, "pps_act_cb_qp_offset_plus5"); |
9038 | 2.11k | gf_bs_read_se_log(bs, "pps_act_cr_qp_offset_plus3"); |
9039 | 2.11k | } |
9040 | 15.8k | if (gf_bs_read_int_log(bs, 1, "pps_palette_predictor_initializers_present_flag")) { |
9041 | 1.54k | u32 pps_num_palette_predictor_initializers = gf_bs_read_ue_log(bs, "pps_num_palette_predictor_initializers"); |
9042 | 1.54k | if (pps_num_palette_predictor_initializers>0) { |
9043 | 436 | Bool mono_flag = gf_bs_read_int_log(bs, 1, "monochrome_palette_flag"); |
9044 | 436 | u32 luma_bpp = 8 + gf_bs_read_ue_log(bs, "luma_bit_depth_entry"); |
9045 | 436 | u32 chroma_bpp = 0; |
9046 | 436 | if (!mono_flag) |
9047 | 131 | chroma_bpp = gf_bs_read_ue_log(bs, "chroma_bit_depth_entry_minus8"); |
9048 | 436 | u32 j, numComps = mono_flag ? 1 : 3; |
9049 | 1.13k | for (i=0; i<numComps; i++) { |
9050 | 6.81k | for (j=0; j<pps_num_palette_predictor_initializers; j++) { |
9051 | 6.11k | gf_bs_read_int_log_idx2(bs, i ? chroma_bpp : luma_bpp, "pps_palette_predictor_initializer", i, j); |
9052 | 6.11k | } |
9053 | 698 | } |
9054 | 436 | } |
9055 | 1.54k | } |
9056 | 15.8k | } |
9057 | | |
9058 | 90.7k | if (gf_bs_is_overflow(bs)) |
9059 | 21.0k | HEVC_PPS_BROKEN |
9060 | 69.7k | return pps_id; |
9061 | 90.7k | } |
9062 | | |
9063 | | |
9064 | | GF_EXPORT |
9065 | | s32 gf_hevc_read_pps(u8 *data, u32 size, HEVCState *hevc) |
9066 | 35.2k | { |
9067 | 35.2k | GF_BitStream *bs; |
9068 | 35.2k | s32 pps_id = -1; |
9069 | | |
9070 | 35.2k | bs = gf_bs_new(data, size, GF_BITSTREAM_READ); |
9071 | 35.2k | if (!bs) goto exit; |
9072 | 35.2k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
9073 | | |
9074 | 35.2k | if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) goto exit; |
9075 | | |
9076 | 26.1k | pps_id = gf_hevc_read_pps_bs_internal(bs, hevc); |
9077 | | |
9078 | 35.2k | exit: |
9079 | 35.2k | if (bs) gf_bs_del(bs); |
9080 | 35.2k | return pps_id; |
9081 | 26.1k | } |
9082 | | |
9083 | | GF_EXPORT |
9084 | | s32 gf_hevc_read_pps_bs(GF_BitStream *bs, HEVCState *hevc) |
9085 | 0 | { |
9086 | 0 | if (!bs || !hevc) return -1; |
9087 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
9088 | 0 | if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) return -1; |
9089 | 0 | return gf_hevc_read_pps_bs_internal(bs, hevc); |
9090 | 0 | } |
9091 | | |
9092 | | GF_EXPORT |
9093 | | s32 gf_hevc_parse_nalu_bs(GF_BitStream *bs, HEVCState *hevc, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id) |
9094 | 708k | { |
9095 | 708k | Bool is_slice = GF_FALSE; |
9096 | 708k | s32 ret = -1; |
9097 | 708k | HEVCSliceInfo n_state; |
9098 | | |
9099 | 708k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
9100 | 708k | if (gf_bs_available(bs)<2) return -1; |
9101 | | |
9102 | 708k | gf_bs_mark_overflow(bs, GF_TRUE); |
9103 | | |
9104 | 708k | memcpy(&n_state, &hevc->s_info, sizeof(HEVCSliceInfo)); |
9105 | 708k | if (!hevc_parse_nal_header(bs, nal_unit_type, temporal_id, layer_id)) return -1; |
9106 | | |
9107 | 578k | n_state.nal_unit_type = *nal_unit_type; |
9108 | | |
9109 | 578k | switch (n_state.nal_unit_type) { |
9110 | 3.26k | case GF_HEVC_NALU_ACCESS_UNIT: |
9111 | 4.98k | case GF_HEVC_NALU_END_OF_SEQ: |
9112 | 5.36k | case GF_HEVC_NALU_END_OF_STREAM: |
9113 | 5.36k | ret = 1; |
9114 | 5.36k | break; |
9115 | | |
9116 | | /*slice_segment_layer_rbsp*/ |
9117 | 228k | case GF_HEVC_NALU_SLICE_TRAIL_N: |
9118 | 244k | case GF_HEVC_NALU_SLICE_TRAIL_R: |
9119 | 252k | case GF_HEVC_NALU_SLICE_TSA_N: |
9120 | 266k | case GF_HEVC_NALU_SLICE_TSA_R: |
9121 | 281k | case GF_HEVC_NALU_SLICE_STSA_N: |
9122 | 281k | case GF_HEVC_NALU_SLICE_STSA_R: |
9123 | 286k | case GF_HEVC_NALU_SLICE_BLA_W_LP: |
9124 | 290k | case GF_HEVC_NALU_SLICE_BLA_W_DLP: |
9125 | 316k | case GF_HEVC_NALU_SLICE_BLA_N_LP: |
9126 | 322k | case GF_HEVC_NALU_SLICE_IDR_W_DLP: |
9127 | 330k | case GF_HEVC_NALU_SLICE_IDR_N_LP: |
9128 | 333k | case GF_HEVC_NALU_SLICE_CRA: |
9129 | 334k | case GF_HEVC_NALU_SLICE_RADL_N: |
9130 | 341k | case GF_HEVC_NALU_SLICE_RADL_R: |
9131 | 342k | case GF_HEVC_NALU_SLICE_RASL_N: |
9132 | 348k | case GF_HEVC_NALU_SLICE_RASL_R: |
9133 | 348k | is_slice = GF_TRUE; |
9134 | | /* slice - read the info and compare.*/ |
9135 | 348k | ret = hevc_parse_slice_segment(bs, hevc, &n_state); |
9136 | 348k | if (ret < 0) return ret; |
9137 | | |
9138 | 297k | hevc_compute_poc(&n_state); |
9139 | 297k | if (hevc->full_slice_header_parse) |
9140 | 95.6k | gf_hevc_compute_ref_list(hevc, &n_state); |
9141 | | |
9142 | 297k | ret = 0; |
9143 | | |
9144 | 297k | if (hevc->s_info.poc != n_state.poc) { |
9145 | 58.7k | ret = 1; |
9146 | 58.7k | break; |
9147 | 58.7k | } |
9148 | 239k | if (n_state.first_slice_segment_in_pic_flag) { |
9149 | 76.6k | if (!(*layer_id) || (n_state.prev_layer_id_plus1 && ((*layer_id) <= n_state.prev_layer_id_plus1 - 1))) { |
9150 | 26.1k | ret = 1; |
9151 | 26.1k | break; |
9152 | 26.1k | } |
9153 | 76.6k | } |
9154 | 212k | break; |
9155 | 212k | case GF_HEVC_NALU_SEQ_PARAM: |
9156 | 37.8k | hevc->last_parsed_sps_id = gf_hevc_read_sps_bs_internal(bs, hevc, *layer_id, NULL); |
9157 | 37.8k | ret = (hevc->last_parsed_sps_id>=0) ? 0 : -1; |
9158 | 37.8k | break; |
9159 | 68.5k | case GF_HEVC_NALU_PIC_PARAM: |
9160 | 68.5k | hevc->last_parsed_pps_id = gf_hevc_read_pps_bs_internal(bs, hevc); |
9161 | 68.5k | ret = (hevc->last_parsed_pps_id>=0) ? 0 : -1; |
9162 | 68.5k | break; |
9163 | 44.4k | case GF_HEVC_NALU_VID_PARAM: |
9164 | 44.4k | hevc->last_parsed_vps_id = gf_hevc_read_vps_bs_internal(bs, hevc, GF_FALSE); |
9165 | 44.4k | ret = (hevc->last_parsed_vps_id>=0) ? 0 : -1; |
9166 | 44.4k | break; |
9167 | 74.6k | default: |
9168 | 74.6k | ret = 0; |
9169 | 74.6k | break; |
9170 | 578k | } |
9171 | | |
9172 | 528k | if (gf_bs_is_overflow(bs)) |
9173 | 105k | ret = -1; |
9174 | | |
9175 | | /* save _prev values */ |
9176 | 528k | if ((ret>0) && hevc->s_info.sps) { |
9177 | 72.6k | n_state.frame_num_offset_prev = hevc->s_info.frame_num_offset; |
9178 | 72.6k | n_state.frame_num_prev = hevc->s_info.frame_num; |
9179 | | |
9180 | 72.6k | n_state.poc_lsb_prev = hevc->s_info.poc_lsb; |
9181 | 72.6k | n_state.poc_msb_prev = hevc->s_info.poc_msb; |
9182 | 72.6k | if (is_slice) |
9183 | 67.7k | n_state.prev_layer_id_plus1 = *layer_id + 1; |
9184 | 72.6k | } |
9185 | 528k | if (is_slice) hevc_compute_poc(&n_state); |
9186 | 528k | memcpy(&hevc->s_info, &n_state, sizeof(HEVCSliceInfo)); |
9187 | | |
9188 | 528k | return ret; |
9189 | 578k | } |
9190 | | |
9191 | | GF_EXPORT |
9192 | | s32 gf_hevc_parse_nalu(u8 *data, u32 size, HEVCState *hevc, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id) |
9193 | 0 | { |
9194 | 0 | GF_BitStream *bs = NULL; |
9195 | 0 | s32 ret = -1; |
9196 | |
|
9197 | 0 | if (size<2) return -1; |
9198 | | |
9199 | 0 | if (!hevc) { |
9200 | 0 | if (nal_unit_type) (*nal_unit_type) = (data[0] & 0x7E) >> 1; |
9201 | 0 | if (layer_id) { |
9202 | 0 | u8 id = data[0] & 1; |
9203 | 0 | id <<= 5; |
9204 | 0 | id |= (data[1] >> 3) & 0x1F; |
9205 | 0 | (*layer_id) = id; |
9206 | 0 | } |
9207 | 0 | if (temporal_id) (*temporal_id) = (data[1] & 0x7); |
9208 | 0 | return -1; |
9209 | 0 | } |
9210 | | |
9211 | 0 | bs = gf_bs_new(data, size, GF_BITSTREAM_READ); |
9212 | 0 | if (!bs) return -1; |
9213 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
9214 | |
|
9215 | 0 | ret = gf_hevc_parse_nalu_bs(bs, hevc, nal_unit_type, temporal_id, layer_id); |
9216 | |
|
9217 | 0 | gf_bs_del(bs); |
9218 | 0 | return ret; |
9219 | 0 | } |
9220 | | |
9221 | | GF_EXPORT |
9222 | | GF_Err gf_hevc_change_vui(GF_HEVCConfig *hvcc, GF_VUIInfo *vui_info) |
9223 | 0 | { |
9224 | 0 | GF_BitStream *orig, *mod; |
9225 | 0 | HEVCState hevc; |
9226 | 0 | u32 i, bit_offset, flag; |
9227 | 0 | s32 idx; |
9228 | 0 | GF_NALUFFParamArray *spss; |
9229 | 0 | GF_NALUFFParam *slc; |
9230 | 0 | orig = NULL; |
9231 | |
|
9232 | 0 | memset(&hevc, 0, sizeof(HEVCState)); |
9233 | 0 | hevc.sps_active_idx = -1; |
9234 | |
|
9235 | 0 | i = 0; |
9236 | 0 | spss = NULL; |
9237 | 0 | while ((spss = (GF_NALUFFParamArray *)gf_list_enum(hvcc->param_array, &i))) { |
9238 | 0 | if (spss->type == GF_HEVC_NALU_SEQ_PARAM) |
9239 | 0 | break; |
9240 | 0 | spss = NULL; |
9241 | 0 | } |
9242 | 0 | if (!spss) return GF_NON_COMPLIANT_BITSTREAM; |
9243 | | |
9244 | 0 | i = 0; |
9245 | 0 | while ((slc = (GF_NALUFFParam *)gf_list_enum(spss->nalus, &i))) { |
9246 | 0 | u8 *no_emulation_buf; |
9247 | 0 | u32 no_emulation_buf_size, emulation_bytes; |
9248 | | |
9249 | | /*SPS may still contains emulation bytes*/ |
9250 | 0 | no_emulation_buf = gf_malloc((slc->size) * sizeof(char)); |
9251 | 0 | no_emulation_buf_size = gf_media_nalu_remove_emulation_bytes(slc->data, no_emulation_buf, slc->size); |
9252 | |
|
9253 | 0 | idx = gf_hevc_read_sps_ex(no_emulation_buf, no_emulation_buf_size, &hevc, &bit_offset); |
9254 | 0 | if (idx < 0) { |
9255 | 0 | if (orig) |
9256 | 0 | gf_bs_del(orig); |
9257 | 0 | gf_free(no_emulation_buf); |
9258 | 0 | continue; |
9259 | 0 | } |
9260 | | |
9261 | 0 | orig = gf_bs_new(no_emulation_buf, no_emulation_buf_size, GF_BITSTREAM_READ); |
9262 | 0 | mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
9263 | | |
9264 | | /*copy over till vui flag*/ |
9265 | 0 | while (bit_offset) { |
9266 | 0 | flag = gf_bs_read_int(orig, 1); |
9267 | 0 | gf_bs_write_int(mod, flag, 1); |
9268 | 0 | bit_offset--; |
9269 | 0 | } |
9270 | |
|
9271 | 0 | avc_hevc_vvc_rewrite_vui(vui_info, orig, mod, GF_CODECID_HEVC); |
9272 | | |
9273 | | /*finally copy over remaining*/ |
9274 | 0 | while (gf_bs_bits_available(orig)) { |
9275 | 0 | flag = gf_bs_read_int(orig, 1); |
9276 | 0 | gf_bs_write_int(mod, flag, 1); |
9277 | 0 | } |
9278 | 0 | gf_bs_del(orig); |
9279 | 0 | orig = NULL; |
9280 | 0 | gf_free(no_emulation_buf); |
9281 | | |
9282 | | /*set anti-emulation*/ |
9283 | 0 | gf_bs_get_content(mod, &no_emulation_buf, &no_emulation_buf_size); |
9284 | 0 | emulation_bytes = gf_media_nalu_emulation_bytes_add_count(no_emulation_buf, no_emulation_buf_size); |
9285 | 0 | if (no_emulation_buf_size + emulation_bytes > slc->size) |
9286 | 0 | slc->data = (char*)gf_realloc(slc->data, no_emulation_buf_size + emulation_bytes); |
9287 | |
|
9288 | 0 | slc->size = gf_media_nalu_add_emulation_bytes(no_emulation_buf, slc->data, no_emulation_buf_size); |
9289 | |
|
9290 | 0 | gf_bs_del(mod); |
9291 | 0 | gf_free(no_emulation_buf); |
9292 | 0 | } |
9293 | 0 | return GF_OK; |
9294 | 0 | } |
9295 | | |
9296 | | |
9297 | | GF_EXPORT |
9298 | | GF_Err gf_hevc_change_par(GF_HEVCConfig *hvcc, s32 ar_n, s32 ar_d) |
9299 | 0 | { |
9300 | 0 | GF_VUIInfo vuii; |
9301 | 0 | memset(&vuii, 0, sizeof(GF_VUIInfo)); |
9302 | 0 | vuii.ar_num = ar_n; |
9303 | 0 | vuii.ar_den = ar_d; |
9304 | 0 | vuii.fullrange = -1; |
9305 | 0 | vuii.video_format = -1; |
9306 | 0 | vuii.color_prim = -1; |
9307 | 0 | vuii.color_tfc = -1; |
9308 | 0 | vuii.color_matrix = -1; |
9309 | 0 | return gf_hevc_change_vui(hvcc, &vuii); |
9310 | 0 | } |
9311 | | |
9312 | | GF_EXPORT |
9313 | | GF_Err gf_hevc_change_color(GF_HEVCConfig *hvcc, s32 fullrange, s32 vidformat, s32 colorprim, s32 transfer, s32 colmatrix) |
9314 | 0 | { |
9315 | 0 | GF_VUIInfo vuii; |
9316 | 0 | memset(&vuii, 0, sizeof(GF_VUIInfo)); |
9317 | 0 | vuii.ar_num = -1; |
9318 | 0 | vuii.ar_den = -1; |
9319 | 0 | vuii.fullrange = fullrange; |
9320 | 0 | vuii.video_format = vidformat; |
9321 | 0 | vuii.color_prim = colorprim; |
9322 | 0 | vuii.color_tfc = transfer; |
9323 | 0 | vuii.color_matrix = colmatrix; |
9324 | 0 | return gf_hevc_change_vui(hvcc, &vuii); |
9325 | 0 | } |
9326 | | |
9327 | | |
9328 | | GF_EXPORT |
9329 | | GF_Err gf_hevc_get_sps_info_with_state(HEVCState *hevc, u8 *sps_data, u32 sps_size, u32 *sps_id, u32 *width, u32 *height, s32 *par_n, s32 *par_d) |
9330 | 0 | { |
9331 | 0 | s32 idx; |
9332 | 0 | idx = gf_hevc_read_sps(sps_data, sps_size, hevc); |
9333 | 0 | if (idx < 0) { |
9334 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
9335 | 0 | } |
9336 | 0 | if (sps_id) *sps_id = idx; |
9337 | |
|
9338 | 0 | if (width) *width = hevc->sps[idx].width; |
9339 | 0 | if (height) *height = hevc->sps[idx].height; |
9340 | 0 | if (par_n) *par_n = hevc->sps[idx].aspect_ratio_info_present_flag ? hevc->sps[idx].sar_width : (u32)-1; |
9341 | 0 | if (par_d) *par_d = hevc->sps[idx].aspect_ratio_info_present_flag ? hevc->sps[idx].sar_height : (u32)-1; |
9342 | 0 | return GF_OK; |
9343 | 0 | } |
9344 | | |
9345 | | GF_EXPORT |
9346 | | GF_Err gf_hevc_get_sps_info(u8 *sps_data, u32 sps_size, u32 *sps_id, u32 *width, u32 *height, s32 *par_n, s32 *par_d) |
9347 | 0 | { |
9348 | 0 | HEVCState hevc; |
9349 | 0 | memset(&hevc, 0, sizeof(HEVCState)); |
9350 | 0 | hevc.sps_active_idx = -1; |
9351 | 0 | return gf_hevc_get_sps_info_with_state(&hevc, sps_data, sps_size, sps_id, width, height, par_n, par_d); |
9352 | 0 | } |
9353 | | |
9354 | | static u32 AC3_FindSyncCode(u8 *buf, u32 buflen) |
9355 | 8.75k | { |
9356 | 8.75k | u32 end = buflen - 6; |
9357 | 8.75k | u32 offset = 0; |
9358 | 21.0M | while (offset <= end) { |
9359 | 21.0M | if (buf[offset] == 0x0b && buf[offset + 1] == 0x77) { |
9360 | 5.71k | return offset; |
9361 | 5.71k | } |
9362 | 21.0M | offset++; |
9363 | 21.0M | } |
9364 | 3.03k | return buflen; |
9365 | 8.75k | } |
9366 | | |
9367 | | |
9368 | | static Bool AC3_FindSyncCodeBS(GF_BitStream *bs) |
9369 | 376k | { |
9370 | 376k | u8 b1; |
9371 | 376k | if (gf_bs_available(bs)<6) return GF_FALSE; |
9372 | 339k | u64 pos = gf_bs_get_position(bs); |
9373 | 339k | u64 end = gf_bs_get_size(bs); |
9374 | | |
9375 | 339k | pos += 1; |
9376 | 339k | b1 = gf_bs_read_u8(bs); |
9377 | 87.1M | while (pos + 1 <= end) { |
9378 | 87.1M | u8 b2 = gf_bs_read_u8(bs); |
9379 | 87.1M | if ((b1 == 0x0b) && (b2 == 0x77)) { |
9380 | 335k | gf_bs_seek(bs, pos - 1); |
9381 | 335k | return GF_TRUE; |
9382 | 335k | } |
9383 | 86.8M | pos++; |
9384 | 86.8M | b1 = b2; |
9385 | 86.8M | } |
9386 | 4.63k | return GF_FALSE; |
9387 | 339k | } |
9388 | | |
9389 | | static const u32 ac3_sizecod_to_bitrate[] = { |
9390 | | 32000, 40000, 48000, 56000, 64000, 80000, 96000, |
9391 | | 112000, 128000, 160000, 192000, 224000, 256000, |
9392 | | 320000, 384000, 448000, 512000, 576000, 640000 |
9393 | | }; |
9394 | | |
9395 | | static const u32 ac3_sizecod2_to_framesize[] = { |
9396 | | 96, 120, 144, 168, 192, 240, 288, 336, 384, 480, 576, 672, |
9397 | | 768, 960, 1152, 1344, 1536, 1728, 1920 |
9398 | | }; |
9399 | | |
9400 | | static const u32 ac3_sizecod1_to_framesize[] = { |
9401 | | 69, 87, 104, 121, 139, 174, 208, 243, 278, 348, 417, 487, |
9402 | | 557, 696, 835, 975, 1114, 1253, 1393 |
9403 | | }; |
9404 | | static const u32 ac3_sizecod0_to_framesize[] = { |
9405 | | 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, |
9406 | | 512, 640, 768, 896, 1024, 1152, 1280 |
9407 | | }; |
9408 | | |
9409 | | static const u32 ac3_mod_to_total_chans[] = { |
9410 | | 2, 1, 2, 3, 3, 4, 4, 5 |
9411 | | }; |
9412 | | |
9413 | | static const u32 ac3_mod_to_surround_chans[] = { |
9414 | | 0, 0, 0, 0, 1, 1, 2, 2 |
9415 | | }; |
9416 | | |
9417 | | GF_EXPORT |
9418 | | u32 gf_ac3_get_total_channels(u32 acmod) |
9419 | 0 | { |
9420 | 0 | u32 nb_ch; |
9421 | 0 | nb_ch = ac3_mod_to_total_chans[acmod]; |
9422 | 0 | return nb_ch; |
9423 | 0 | } |
9424 | | |
9425 | | GF_EXPORT |
9426 | | u32 gf_ac3_get_surround_channels(u32 acmod) |
9427 | 0 | { |
9428 | 0 | u32 nb_ch; |
9429 | 0 | nb_ch = ac3_mod_to_surround_chans[acmod]; |
9430 | 0 | return nb_ch; |
9431 | 0 | } |
9432 | | |
9433 | | GF_EXPORT |
9434 | | u32 gf_ac3_get_bitrate(u32 brcode) |
9435 | 6.14k | { |
9436 | 6.14k | return ac3_sizecod_to_bitrate[brcode]; |
9437 | 6.14k | } |
9438 | | |
9439 | | Bool gf_ac3_parser(u8 *buf, u32 buflen, u32 *pos, GF_AC3Config *hdr, Bool full_parse) |
9440 | 8.75k | { |
9441 | 8.75k | GF_BitStream *bs; |
9442 | 8.75k | Bool ret; |
9443 | | |
9444 | 8.75k | if (buflen < 6) return GF_FALSE; |
9445 | 8.75k | (*pos) = AC3_FindSyncCode(buf, buflen); |
9446 | 8.75k | if (*pos >= buflen) return GF_FALSE; |
9447 | | |
9448 | 5.71k | bs = gf_bs_new((const char*)(buf + *pos), buflen, GF_BITSTREAM_READ); |
9449 | 5.71k | ret = gf_ac3_parser_bs(bs, hdr, full_parse); |
9450 | 5.71k | gf_bs_del(bs); |
9451 | | |
9452 | 5.71k | return ret; |
9453 | 8.75k | } |
9454 | | |
9455 | | GF_EXPORT |
9456 | | Bool gf_ac3_parser_bs(GF_BitStream *bs, GF_AC3Config *hdr, Bool full_parse) |
9457 | 68.0k | { |
9458 | 68.0k | u32 fscod, frmsizecod, bsid, ac3_mod, freq, framesize, bsmod, syncword; |
9459 | 68.0k | u64 pos; |
9460 | 68.0k | if (!hdr || !AC3_FindSyncCodeBS(bs)) return GF_FALSE; |
9461 | | |
9462 | 31.6k | memset(hdr, 0, sizeof(GF_AC3Header)); |
9463 | 31.6k | pos = gf_bs_get_position(bs); |
9464 | | |
9465 | 31.6k | syncword = gf_bs_read_u16(bs); |
9466 | 31.6k | if (syncword != 0x0B77) { |
9467 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AC3] Wrong sync word detected (0x%X - expecting 0x0B77).\n", syncword)); |
9468 | 0 | return GF_FALSE; |
9469 | 0 | } |
9470 | 31.6k | gf_bs_read_int_log(bs, 16, "crc1"); |
9471 | 31.6k | fscod = gf_bs_read_int_log(bs, 2, "fscod"); |
9472 | 31.6k | frmsizecod = gf_bs_read_int_log(bs, 6, "frmsizecod"); |
9473 | 31.6k | bsid = gf_bs_read_int_log(bs, 5, "bsid"); |
9474 | 31.6k | bsmod = gf_bs_read_int_log(bs, 3, "bsmod"); |
9475 | 31.6k | ac3_mod = gf_bs_read_int_log(bs, 3, "ac3_mod"); |
9476 | | |
9477 | 31.6k | if (frmsizecod >= 2 * sizeof(ac3_sizecod_to_bitrate) / sizeof(u32)) |
9478 | 2.56k | return GF_FALSE; |
9479 | | |
9480 | 29.0k | switch (fscod) { |
9481 | 22.6k | case 0: |
9482 | 22.6k | freq = 48000; |
9483 | 22.6k | framesize = ac3_sizecod0_to_framesize[frmsizecod / 2] * 2; |
9484 | 22.6k | break; |
9485 | 5.83k | case 1: |
9486 | 5.83k | freq = 44100; |
9487 | 5.83k | framesize = (ac3_sizecod1_to_framesize[frmsizecod / 2] + (frmsizecod & 0x1)) * 2; |
9488 | 5.83k | break; |
9489 | 517 | case 2: |
9490 | 517 | freq = 32000; |
9491 | 517 | framesize = ac3_sizecod2_to_framesize[frmsizecod / 2] * 2; |
9492 | 517 | break; |
9493 | 80 | default: |
9494 | 80 | return GF_FALSE; |
9495 | 29.0k | } |
9496 | 28.9k | hdr->sample_rate = freq; |
9497 | 28.9k | hdr->framesize = framesize; |
9498 | 28.9k | hdr->nb_streams = 1; |
9499 | | |
9500 | 28.9k | if (full_parse) { |
9501 | 23.1k | hdr->streams[0].bsid = bsid; |
9502 | 23.1k | hdr->streams[0].bsmod = bsmod; |
9503 | 23.1k | hdr->streams[0].acmod = ac3_mod; |
9504 | 23.1k | hdr->streams[0].lfon = 0; |
9505 | 23.1k | hdr->streams[0].fscod = fscod; |
9506 | 23.1k | hdr->brcode = frmsizecod / 2; |
9507 | 23.1k | } |
9508 | 28.9k | if (ac3_mod >= 2 * sizeof(ac3_mod_to_total_chans) / sizeof(u32)) |
9509 | 0 | return GF_FALSE; |
9510 | | |
9511 | 28.9k | hdr->streams[0].channels = ac3_mod_to_total_chans[ac3_mod]; |
9512 | 28.9k | hdr->streams[0].surround_channels = ac3_mod_to_surround_chans[ac3_mod]; |
9513 | 28.9k | if ((ac3_mod & 0x1) && (ac3_mod != 1)) gf_bs_read_int_log(bs, 2, "cmixlev"); |
9514 | 28.9k | if (ac3_mod & 0x4) gf_bs_read_int_log(bs, 2, "surmixlev"); |
9515 | 28.9k | if (ac3_mod == 0x2) gf_bs_read_int_log(bs, 2, "dsurmod"); |
9516 | | |
9517 | 28.9k | if (gf_bs_read_int_log(bs, 1, "lfeon")) { |
9518 | 8.64k | hdr->streams[0].channels += 1; |
9519 | 8.64k | hdr->streams[0].lfon = 1; |
9520 | 8.64k | } |
9521 | | |
9522 | 28.9k | gf_bs_seek(bs, pos); |
9523 | | |
9524 | 28.9k | return GF_TRUE; |
9525 | 28.9k | } |
9526 | | |
9527 | | GF_EXPORT |
9528 | | u64 gf_ac3_get_channel_layout(GF_AC3Config *ac3) |
9529 | 25.4k | { |
9530 | 25.4k | u64 layout = 0; |
9531 | 25.4k | switch (ac3->streams[0].acmod) { |
9532 | 4.13k | case 0: |
9533 | 4.13k | layout |= GF_AUDIO_CH_FRONT_LEFT | GF_AUDIO_CH_FRONT_RIGHT; |
9534 | 4.13k | break; |
9535 | 600 | case 1: |
9536 | 600 | layout |= GF_AUDIO_CH_FRONT_CENTER; |
9537 | 600 | break; |
9538 | 2.09k | case 2: |
9539 | 2.09k | layout |= GF_AUDIO_CH_FRONT_LEFT | GF_AUDIO_CH_FRONT_RIGHT; |
9540 | 2.09k | break; |
9541 | 144 | case 3: |
9542 | 144 | layout |= GF_AUDIO_CH_FRONT_LEFT | GF_AUDIO_CH_FRONT_RIGHT | GF_AUDIO_CH_FRONT_CENTER; |
9543 | 144 | break; |
9544 | 0 | case 4: |
9545 | 0 | layout |= GF_AUDIO_CH_FRONT_LEFT | GF_AUDIO_CH_FRONT_RIGHT | GF_AUDIO_CH_REAR_CENTER; |
9546 | 0 | break; |
9547 | 17.8k | case 5: |
9548 | 17.8k | layout |= GF_AUDIO_CH_FRONT_LEFT | GF_AUDIO_CH_FRONT_RIGHT | GF_AUDIO_CH_FRONT_CENTER | GF_AUDIO_CH_REAR_CENTER; |
9549 | 17.8k | break; |
9550 | 20 | case 6: |
9551 | 20 | layout |= GF_AUDIO_CH_FRONT_LEFT | GF_AUDIO_CH_FRONT_RIGHT | GF_AUDIO_CH_SURROUND_LEFT | GF_AUDIO_CH_SURROUND_RIGHT; |
9552 | 20 | break; |
9553 | 667 | case 7: |
9554 | 667 | layout |= GF_AUDIO_CH_FRONT_LEFT | GF_AUDIO_CH_FRONT_RIGHT | GF_AUDIO_CH_SURROUND_LEFT | GF_AUDIO_CH_FRONT_CENTER | GF_AUDIO_CH_SURROUND_RIGHT; |
9555 | 667 | break; |
9556 | 25.4k | } |
9557 | 25.4k | if (ac3->streams[0].lfon) layout |= GF_AUDIO_CH_LFE; |
9558 | 25.4k | if (ac3->streams[0].nb_dep_sub) { |
9559 | 3.69k | u32 chan_loc = ac3->streams[0].chan_loc; |
9560 | 3.69k | if (chan_loc & 1) layout |= GF_AUDIO_CH_FRONT_CENTER_LEFT | GF_AUDIO_CH_FRONT_CENTER_RIGHT; //Lc/Rc pair |
9561 | 3.69k | if (chan_loc & (1<<1)) layout |= GF_AUDIO_CH_REAR_SURROUND_LEFT|GF_AUDIO_CH_REAR_SURROUND_RIGHT; //Lrs/Rrs pair |
9562 | 3.69k | if (chan_loc & (1<<2)) layout |= GF_AUDIO_CH_REAR_CENTER; //Cs |
9563 | 3.69k | if (chan_loc & (1<<3)) layout |= GF_AUDIO_CH_REAR_CENTER_TOP; //Ts |
9564 | 3.69k | if (chan_loc & (1<<4)) layout |= GF_AUDIO_CH_SIDE_SURROUND_LEFT | GF_AUDIO_CH_SIDE_SURROUND_RIGHT; //Lsd/Rsd pair |
9565 | 3.69k | if (chan_loc & (1<<5)) layout |= GF_AUDIO_CH_WIDE_FRONT_LEFT | GF_AUDIO_CH_WIDE_FRONT_RIGHT ; //Lw/Rw pair |
9566 | 3.69k | if (chan_loc & (1<<6)) layout |= GF_AUDIO_CH_FRONT_TOP_LEFT | GF_AUDIO_CH_FRONT_TOP_RIGHT; //Lvh/Rvh pair |
9567 | 3.69k | if (chan_loc & (1<<7)) layout |= GF_AUDIO_CH_FRONT_TOP_CENTER; //Cvh |
9568 | 3.69k | if (chan_loc & (1<<8)) layout |= GF_AUDIO_CH_LFE2; //LFE2 |
9569 | 3.69k | } |
9570 | 25.4k | return layout; |
9571 | 25.4k | } |
9572 | | |
9573 | | GF_EXPORT |
9574 | | u32 gf_eac3_get_chan_loc_count(u32 chan_loc) |
9575 | 6.20k | { |
9576 | 6.20k | u32 nb_ch=0; |
9577 | 6.20k | if (chan_loc & 1) nb_ch+=2; //Lc/Rc pair |
9578 | 6.20k | if (chan_loc & (1<<1)) nb_ch+=2; //Lrs/Rrs pair |
9579 | 6.20k | if (chan_loc & (1<<2)) nb_ch+=1; //Cs |
9580 | 6.20k | if (chan_loc & (1<<3)) nb_ch+=1; //Ts |
9581 | 6.20k | if (chan_loc & (1<<4)) nb_ch+=2; //Lsd/Rsd pair |
9582 | 6.20k | if (chan_loc & (1<<5)) nb_ch+=2; //Lw/Rw pair |
9583 | 6.20k | if (chan_loc & (1<<6)) nb_ch+=2; //Lvh/Rvh pair |
9584 | 6.20k | if (chan_loc & (1<<7)) nb_ch+=1; //Cvh |
9585 | 6.20k | if (chan_loc & (1<<8)) nb_ch+=1; //LFE2 |
9586 | 6.20k | return nb_ch; |
9587 | 6.20k | } |
9588 | | |
9589 | | static u32 eac3_chanmap_to_chan_loc(u32 chan_map) |
9590 | 6.20k | { |
9591 | 6.20k | u32 chan_loc = 0; |
9592 | 6.20k | if (chan_map & (1<<10)) chan_loc |= (1); |
9593 | 6.20k | if (chan_map & (1<<9)) chan_loc |= (1<<1); |
9594 | 6.20k | if (chan_map & (1<<8)) chan_loc |= (1<<2); |
9595 | 6.20k | if (chan_map & (1<<7)) chan_loc |= (1<<3); |
9596 | 6.20k | if (chan_map & (1<<6)) chan_loc |= (1<<4); |
9597 | 6.20k | if (chan_map & (1<<5)) chan_loc |= (1<<5); |
9598 | 6.20k | if (chan_map & (1<<4)) chan_loc |= (1<<6); |
9599 | 6.20k | if (chan_map & (1<<3)) chan_loc |= (1<<7); |
9600 | | //Lts/Rts pair (LSB 2) is not exposed in chan_loc |
9601 | 6.20k | if (chan_map & (1<<1)) chan_loc |= (1<<8); |
9602 | 6.20k | return chan_loc; |
9603 | 6.20k | } |
9604 | | |
9605 | | static void eac3_update_channels(GF_AC3Config *hdr) |
9606 | 117k | { |
9607 | 117k | u32 i; |
9608 | 302k | for (i=0; i<hdr->nb_streams; i++) { |
9609 | 184k | u32 nb_ch = ac3_mod_to_total_chans[hdr->streams[i].acmod]; |
9610 | 184k | if (hdr->streams[i].nb_dep_sub) { |
9611 | 6.20k | hdr->streams[i].chan_loc = eac3_chanmap_to_chan_loc(hdr->streams[i].chan_loc); |
9612 | 6.20k | nb_ch += gf_eac3_get_chan_loc_count(hdr->streams[i].chan_loc); |
9613 | 6.20k | } |
9614 | 184k | if (hdr->streams[i].lfon) nb_ch++; |
9615 | 184k | hdr->streams[i].channels = nb_ch; |
9616 | 184k | hdr->streams[i].surround_channels = ac3_mod_to_surround_chans[hdr->streams[i].acmod]; |
9617 | 184k | } |
9618 | 117k | } |
9619 | | |
9620 | | static Bool gf_eac3_parser_internal(GF_BitStream *bs, GF_AC3Config *hdr, Bool full_parse) |
9621 | 122k | { |
9622 | 122k | u32 fscod, bsid, acmod, freq, framesize, syncword, substreamid, lfon, numblkscod, strmtyp, frmsiz, bsmod; |
9623 | 122k | u64 pos, hdr_pos; |
9624 | 122k | u16 chanmap; |
9625 | 122k | Bool main_indep_found = GF_FALSE; |
9626 | 122k | s32 cur_main_id = -1; |
9627 | 122k | u32 nb_blocks_main; |
9628 | 122k | u16 main_substreams; //bit-mask of independent channels found so far |
9629 | 122k | static u32 numblks[4] = {1, 2, 3, 6}; |
9630 | | |
9631 | 122k | if (!hdr || !AC3_FindSyncCodeBS(bs)) |
9632 | 2.72k | return GF_FALSE; |
9633 | | |
9634 | 130k | retry_frame: |
9635 | 130k | pos = gf_bs_get_position(bs); |
9636 | 130k | framesize = 0; |
9637 | 130k | numblkscod = 0; |
9638 | 130k | bsmod = 0; |
9639 | 130k | nb_blocks_main = 0; |
9640 | 130k | main_substreams = 0; |
9641 | 130k | memset(hdr, 0, sizeof(GF_AC3Config)); |
9642 | | |
9643 | 303k | next_block: |
9644 | 303k | hdr_pos = gf_bs_get_position(bs); |
9645 | | |
9646 | 303k | bsid = gf_bs_peek_bits(bs, 5, 5); |
9647 | | //"If an AC-3 bit stream is present in the Enhanced AC-3 bit stream, then the AC-3 bit stream shall be treated |
9648 | | //as an independent substream assigned substream ID 0." |
9649 | 303k | if (bsid<=8) { |
9650 | 12.6k | GF_AC3Header ac3h; |
9651 | | //we are done |
9652 | 12.6k | if (main_indep_found) { |
9653 | 6.00k | eac3_update_channels(hdr); |
9654 | 6.00k | gf_bs_seek(bs, pos); |
9655 | 6.00k | return GF_TRUE; |
9656 | 6.00k | } |
9657 | 6.67k | if (!gf_ac3_parser_bs(bs, &ac3h, GF_TRUE)) { |
9658 | 524 | gf_bs_seek(bs, pos); |
9659 | 524 | return GF_FALSE; |
9660 | 524 | } |
9661 | 6.14k | hdr->streams[0] = ac3h.streams[0]; |
9662 | 6.14k | hdr->nb_streams = 1; |
9663 | 6.14k | hdr->sample_rate = ac3h.sample_rate; |
9664 | 6.14k | main_substreams |= 1; |
9665 | 6.14k | hdr->framesize = ac3h.framesize; |
9666 | 6.14k | nb_blocks_main = 6; |
9667 | 6.14k | hdr->brcode = gf_ac3_get_bitrate(ac3h.brcode)/1000; |
9668 | | |
9669 | 6.14k | gf_bs_skip_bytes(bs, ac3h.framesize); |
9670 | 6.14k | if (!AC3_FindSyncCodeBS(bs)) { |
9671 | 867 | gf_bs_seek(bs, pos); |
9672 | 867 | return GF_FALSE; |
9673 | 867 | } |
9674 | 5.27k | main_indep_found = GF_TRUE; |
9675 | 5.27k | cur_main_id = 0; |
9676 | 5.27k | goto next_block; |
9677 | 6.14k | } |
9678 | | //corrupted frame, trash |
9679 | 291k | if ((bsid<10) || (bsid>16)) { |
9680 | 29.7k | gf_bs_skip_bytes(bs, 1);//we are still at the startcode |
9681 | 29.7k | if (!AC3_FindSyncCodeBS(bs)) { |
9682 | 364 | gf_bs_seek(bs, pos); |
9683 | 364 | return GF_FALSE; |
9684 | 364 | } |
9685 | 29.3k | goto next_block; |
9686 | 29.7k | } |
9687 | 261k | syncword = gf_bs_read_u16(bs); |
9688 | 261k | if (syncword != 0x0B77) { |
9689 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[E-AC3] Wrong sync word detected (0x%X - expecting 0x0B77).\n", syncword)); |
9690 | 0 | return GF_FALSE; |
9691 | 0 | } |
9692 | | |
9693 | 261k | hdr->is_ec3 = 1; |
9694 | 261k | strmtyp = gf_bs_read_int_log(bs, 2, "strmtyp"); |
9695 | 261k | substreamid = gf_bs_read_int_log(bs, 3, "substreamid"); |
9696 | | |
9697 | | //independent stream |
9698 | 261k | if (strmtyp!=0x1) { |
9699 | | //all blocks gathered and we have seen this substreamid, done with whole frame |
9700 | 235k | if ( (nb_blocks_main>=6) && ( (main_substreams >> substreamid) & 0x1)) { |
9701 | 50.5k | eac3_update_channels(hdr); |
9702 | 50.5k | gf_bs_seek(bs, pos); |
9703 | 50.5k | return GF_TRUE; |
9704 | 50.5k | } |
9705 | | //main independent: "All Enhanced AC-3 bit streams shall contain an independent substream assigned substream ID 0. |
9706 | | // The independent substream assigned substream ID 0 shall be the first substream present in the bit stream." |
9707 | 185k | if (!substreamid) |
9708 | 182k | main_indep_found = 1; |
9709 | 185k | if (cur_main_id != substreamid) |
9710 | 116k | nb_blocks_main=0; |
9711 | 185k | cur_main_id = substreamid; |
9712 | 185k | } |
9713 | | |
9714 | | //trash everything until we find first indep stream |
9715 | 210k | if (!main_indep_found) { |
9716 | 10.7k | gf_bs_align(bs); |
9717 | 10.7k | if (!AC3_FindSyncCodeBS(bs)) { |
9718 | 98 | gf_bs_seek(bs, pos); |
9719 | 98 | return GF_FALSE; |
9720 | 98 | } |
9721 | 10.6k | goto retry_frame; |
9722 | 10.7k | } |
9723 | | |
9724 | 200k | frmsiz = gf_bs_read_int_log(bs, 11, "frmsiz"); |
9725 | 200k | framesize = 2 * (1 + frmsiz); |
9726 | 200k | fscod = gf_bs_read_int_log(bs, 2, "fscod"); |
9727 | 200k | if (fscod == 0x3) { |
9728 | 134 | fscod = gf_bs_read_int_log(bs, 2, "fscod2"); |
9729 | 134 | numblkscod = 3; |
9730 | 199k | } else { |
9731 | 199k | numblkscod = gf_bs_read_int_log(bs, 2, "numblkscod"); |
9732 | 199k | } |
9733 | | |
9734 | | //remember our independent substreams |
9735 | 200k | if (strmtyp!=0x1) { |
9736 | 184k | main_substreams |= (1 << substreamid); |
9737 | 184k | } |
9738 | | |
9739 | 200k | switch (fscod) { |
9740 | 129k | case 0: |
9741 | 129k | freq = 48000; |
9742 | 129k | break; |
9743 | 69.7k | case 1: |
9744 | 69.7k | freq = 44100; |
9745 | 69.7k | break; |
9746 | 566 | case 2: |
9747 | 566 | freq = 32000; |
9748 | 566 | break; |
9749 | 80 | default: |
9750 | | //do not sync |
9751 | 80 | gf_bs_align(bs); |
9752 | 80 | return GF_FALSE; |
9753 | 200k | } |
9754 | | |
9755 | 199k | acmod = gf_bs_read_int_log(bs, 3, "acmod"); |
9756 | 199k | lfon = gf_bs_read_int_log(bs, 1, "lfon"); |
9757 | 199k | bsid = gf_bs_read_int_log(bs, 5, "bsid"); |
9758 | | |
9759 | 199k | gf_bs_read_int_log(bs, 5, "dialnorm"); |
9760 | 199k | if (gf_bs_read_int_log(bs, 1, "compre")) { |
9761 | 65.1k | gf_bs_read_int_log(bs, 8, "compr"); |
9762 | 65.1k | } |
9763 | 199k | if (acmod==0) { |
9764 | 52.5k | gf_bs_read_int_log(bs, 5, "dialnorm2"); |
9765 | 52.5k | if (gf_bs_read_int_log(bs, 1, "compr2e")) { |
9766 | 9.43k | gf_bs_read_int_log(bs, 8, "compr2"); |
9767 | 9.43k | } |
9768 | 52.5k | } |
9769 | 199k | chanmap = 0; |
9770 | 199k | if (strmtyp==0x1) { |
9771 | 15.5k | if (gf_bs_read_int_log(bs, 1, "chanmape")) { |
9772 | 7.39k | chanmap = gf_bs_read_int_log(bs, 16, "chanmap"); |
9773 | 7.39k | } |
9774 | 15.5k | } |
9775 | | |
9776 | 199k | hdr->sample_rate = freq; |
9777 | 199k | hdr->framesize += framesize; |
9778 | 199k | if (strmtyp != 1) { |
9779 | 184k | gf_assert(cur_main_id == substreamid); |
9780 | 184k | hdr->streams[substreamid].lfon = lfon; |
9781 | 184k | hdr->streams[substreamid].bsid = bsid; |
9782 | 184k | hdr->streams[substreamid].bsmod = bsmod; |
9783 | 184k | hdr->streams[substreamid].acmod = acmod; |
9784 | 184k | hdr->streams[substreamid].fscod = fscod; |
9785 | 184k | hdr->brcode = 0; |
9786 | 184k | if (hdr->nb_streams<8) |
9787 | 183k | hdr->nb_streams++; |
9788 | 184k | } |
9789 | | //dependent stream, record max substream ID of dep and store chan map |
9790 | | //"Dependent substreams are assigned substream ID's 0 to 7, which shall be assigned sequentially according to the order the dependent substreams are present in the bit stream. " |
9791 | 15.5k | else { |
9792 | 15.5k | hdr->streams[cur_main_id].nb_dep_sub = 1+substreamid; |
9793 | 15.5k | hdr->streams[cur_main_id].chan_loc |= chanmap; |
9794 | 15.5k | } |
9795 | | |
9796 | | //not clear if this is only for the independent streams - spec says "The value is the sum of the data rates of all the substreams" |
9797 | 199k | hdr->brcode += ((frmsiz+1) * freq) / (numblks[numblkscod]*16) / 1000; |
9798 | | |
9799 | | //start of header only, we are done - chan info might be wrong |
9800 | 199k | if (!full_parse) { |
9801 | 60.9k | eac3_update_channels(hdr); |
9802 | 60.9k | gf_bs_seek(bs, pos); |
9803 | 60.9k | return GF_TRUE; |
9804 | 60.9k | } |
9805 | | |
9806 | | //mix metadata |
9807 | 139k | if (gf_bs_read_int(bs, 1)) { |
9808 | 53.6k | if (acmod > 0x2) gf_bs_read_int(bs, 2); |
9809 | 53.6k | if ((acmod & 0x1) && (acmod > 0x2)) gf_bs_read_int(bs, 6); |
9810 | 53.6k | if (acmod & 0x4) gf_bs_read_int(bs, 6); |
9811 | 53.6k | if (lfon) { |
9812 | 8.56k | if (gf_bs_read_int(bs, 1)) |
9813 | 8.26k | gf_bs_read_int(bs, 5); |
9814 | 8.56k | } |
9815 | 53.6k | if (strmtyp == 0) { |
9816 | | //pgmscle |
9817 | 50.7k | if (gf_bs_read_int(bs, 1)) |
9818 | 46.5k | gf_bs_read_int(bs, 6); |
9819 | 50.7k | if (acmod==0) { |
9820 | | //pgmscl2e |
9821 | 31.9k | if (gf_bs_read_int(bs, 1)) |
9822 | 17.2k | gf_bs_read_int(bs, 6); |
9823 | 31.9k | } |
9824 | | //extpgmscle |
9825 | 50.7k | if (gf_bs_read_int(bs, 1)) |
9826 | 16.9k | gf_bs_read_int(bs, 6); |
9827 | 50.7k | u8 mixdef = gf_bs_read_int(bs, 2); |
9828 | 50.7k | if (mixdef == 0x1) { |
9829 | 5.23k | gf_bs_read_int(bs, 5); |
9830 | 45.5k | } else if (mixdef == 0x2) { |
9831 | 1.36k | gf_bs_read_int(bs, 12); |
9832 | 44.1k | } else if (mixdef == 0x3) { |
9833 | 13.1k | u32 mixdeflen = gf_bs_read_int(bs, 5); |
9834 | 13.1k | mixdeflen = 8 * (mixdeflen + 2); |
9835 | 2.99M | while (mixdeflen) { |
9836 | 2.98M | gf_bs_read_int(bs, 1); |
9837 | 2.98M | mixdeflen--; |
9838 | 2.98M | } |
9839 | 13.1k | } |
9840 | 50.7k | if (acmod < 0x2) { |
9841 | | //paninfoe |
9842 | 35.4k | if (gf_bs_read_int(bs, 1)) |
9843 | 647 | gf_bs_read_int(bs, 14); |
9844 | 35.4k | if (acmod == 0) { |
9845 | | //paninfo2e |
9846 | 31.9k | if (gf_bs_read_int(bs, 1)) |
9847 | 3.17k | gf_bs_read_int(bs, 14); |
9848 | 31.9k | } |
9849 | | |
9850 | 35.4k | } |
9851 | | //frmmixcfginfoe |
9852 | 50.7k | if (gf_bs_read_int(bs, 1)) { |
9853 | 12.9k | if (numblkscod == 0x0) { |
9854 | 10.0k | gf_bs_read_int(bs, 5); |
9855 | 10.0k | } else { |
9856 | 2.85k | u32 i, nb_blocks = numblks[numblkscod]; |
9857 | 9.43k | for (i=0; i<nb_blocks; i++) { |
9858 | 6.58k | if (gf_bs_read_int(bs, 1)) |
9859 | 5.62k | gf_bs_read_int(bs, 5); |
9860 | 6.58k | } |
9861 | 2.85k | } |
9862 | 12.9k | } |
9863 | 50.7k | } |
9864 | 53.6k | } |
9865 | | //info metadata |
9866 | 139k | if (gf_bs_read_int(bs, 1)) { |
9867 | 52.1k | gf_bs_read_int(bs, 5); |
9868 | 52.1k | if (acmod == 0x2) gf_bs_read_int(bs, 4); |
9869 | 52.1k | if (acmod >= 0x6) gf_bs_read_int(bs, 2); |
9870 | | //audprodie |
9871 | 52.1k | if (gf_bs_read_int(bs, 1)) gf_bs_read_int(bs, 8); |
9872 | 52.1k | if (acmod == 0x0) { |
9873 | | //audprodi2e |
9874 | 10.4k | if (gf_bs_read_int(bs, 1)) gf_bs_read_int(bs, 8); |
9875 | 10.4k | } |
9876 | 52.1k | if (fscod < 0x3) gf_bs_read_int(bs, 1); |
9877 | 52.1k | } |
9878 | 139k | if ((strmtyp == 0) && (numblkscod != 0x3)) gf_bs_read_int(bs, 1); |
9879 | 139k | if (strmtyp == 0x2) { |
9880 | 504 | u32 blkid=0; |
9881 | 504 | if (numblkscod == 0x3) blkid=1; |
9882 | 186 | else blkid = gf_bs_read_int(bs, 1); |
9883 | 504 | if (blkid) gf_bs_read_int(bs, 6); |
9884 | 504 | } |
9885 | 139k | u8 addbsie = gf_bs_read_int(bs, 1); |
9886 | 139k | if (addbsie) { |
9887 | 50.3k | u32 addbsil = gf_bs_read_int(bs, 6) + 1; |
9888 | | //we only use the first 2 bytes - cf 8.3 of ETSI 103 420 V1.2.1 |
9889 | 50.3k | if (addbsil>=2) { |
9890 | 49.9k | gf_bs_read_int(bs, 7); |
9891 | 49.9k | if (gf_bs_read_int(bs, 1)) { |
9892 | 41.4k | hdr->atmos_ec3_ext = 1; |
9893 | 41.4k | hdr->complexity_index_type = gf_bs_read_int(bs, 8); |
9894 | 41.4k | } |
9895 | 49.9k | } |
9896 | 50.3k | } |
9897 | | |
9898 | | //remember numbers of block for main |
9899 | 139k | if (strmtyp!=0x1) { |
9900 | 123k | nb_blocks_main += numblks[numblkscod]; |
9901 | 123k | } |
9902 | | |
9903 | 139k | if (gf_bs_seek(bs, hdr_pos + framesize) != GF_OK) { |
9904 | 154 | gf_bs_seek(bs, pos); |
9905 | 154 | return GF_FALSE; |
9906 | 154 | } |
9907 | | |
9908 | 138k | if (!AC3_FindSyncCodeBS(bs)) { |
9909 | 631 | gf_bs_seek(bs, pos); |
9910 | 631 | return GF_FALSE; |
9911 | 631 | } |
9912 | | //we go to next block even if we have 6 of main (to check deps) |
9913 | 138k | goto next_block; |
9914 | 138k | } |
9915 | | |
9916 | | GF_EXPORT |
9917 | | Bool gf_eac3_parser_bs(GF_BitStream *bs, GF_AC3Config *hdr, Bool full_parse) |
9918 | 122k | { |
9919 | 122k | return gf_eac3_parser_internal(bs, hdr, full_parse); |
9920 | 122k | } |
9921 | | |
9922 | | GF_EXPORT |
9923 | | Bool gf_eac3_parser(u8 *buf, u32 buflen, u32 *pos, GF_AC3Config *hdr, Bool full_parse) |
9924 | 0 | { |
9925 | 0 | GF_BitStream *bs; |
9926 | 0 | Bool ret; |
9927 | |
|
9928 | 0 | if (buflen < 6) return GF_FALSE; |
9929 | 0 | (*pos) = AC3_FindSyncCode(buf, buflen); |
9930 | 0 | if (*pos >= buflen) return GF_FALSE; |
9931 | | |
9932 | 0 | bs = gf_bs_new((const char*)(buf + *pos), buflen, GF_BITSTREAM_READ); |
9933 | 0 | ret = gf_eac3_parser_internal(bs, hdr, full_parse); |
9934 | 0 | gf_bs_del(bs); |
9935 | 0 | return ret; |
9936 | 0 | } |
9937 | | |
9938 | | #endif /*GPAC_DISABLE_AV_PARSERS*/ |
9939 | | |
9940 | | u32 gf_id3_read_size(GF_BitStream *bs) |
9941 | 28.5k | { |
9942 | 28.5k | u32 size = 0; |
9943 | 28.5k | gf_bs_read_int(bs, 1); |
9944 | 28.5k | size |= gf_bs_read_int(bs, 7); |
9945 | 28.5k | size<<=7; |
9946 | 28.5k | gf_bs_read_int(bs, 1); |
9947 | 28.5k | size |= gf_bs_read_int(bs, 7); |
9948 | 28.5k | size<<=7; |
9949 | 28.5k | gf_bs_read_int(bs, 1); |
9950 | 28.5k | size |= gf_bs_read_int(bs, 7); |
9951 | 28.5k | size<<=7; |
9952 | 28.5k | gf_bs_read_int(bs, 1); |
9953 | 28.5k | size |= gf_bs_read_int(bs, 7); |
9954 | 28.5k | return size; |
9955 | 28.5k | } |
9956 | | |
9957 | | |
9958 | | #if !defined(GPAC_DISABLE_AV_PARSERS) && !defined (GPAC_DISABLE_OGG) |
9959 | | |
9960 | | /* |
9961 | | Vorbis parser |
9962 | | */ |
9963 | | |
9964 | | static u32 vorbis_book_maptype1_quantvals(u32 entries, u32 dim) |
9965 | 34.7k | { |
9966 | 34.7k | u32 vals = (u32)floor(pow(entries, 1.0 / dim)); |
9967 | 37.5k | while (1) { |
9968 | 37.5k | u32 acc = 1; |
9969 | 37.5k | u32 acc1 = 1; |
9970 | 37.5k | u32 i; |
9971 | 144k | for (i = 0; i < dim; i++) { |
9972 | 106k | acc *= vals; |
9973 | 106k | acc1 *= vals + 1; |
9974 | 106k | } |
9975 | 37.5k | if (acc <= entries && acc1 > entries) return (vals); |
9976 | 2.85k | else { |
9977 | 2.85k | if (acc > entries) vals--; |
9978 | 2.85k | else vals++; |
9979 | 2.85k | } |
9980 | 37.5k | } |
9981 | 34.7k | } |
9982 | | |
9983 | | static u32 ilog(u32 v, Bool dec) |
9984 | 1.13k | { |
9985 | 1.13k | u32 ret = 0; |
9986 | 1.13k | if (dec && v) --v; |
9987 | 4.54k | while (v) { |
9988 | 3.40k | ret++; |
9989 | 3.40k | v >>= 1; |
9990 | 3.40k | } |
9991 | 1.13k | return (ret); |
9992 | 1.13k | } |
9993 | | |
9994 | | static u32 icount(u32 v) |
9995 | 41.8k | { |
9996 | 41.8k | u32 ret = 0; |
9997 | 109k | while (v) { |
9998 | 67.4k | ret += v & 1; |
9999 | 67.4k | v >>= 1; |
10000 | 67.4k | } |
10001 | 41.8k | return(ret); |
10002 | 41.8k | } |
10003 | | |
10004 | | |
10005 | | GF_EXPORT |
10006 | | Bool gf_vorbis_parse_header(GF_VorbisParser *vp, u8 *data, u32 data_len) |
10007 | 10.8k | { |
10008 | 10.8k | u32 pack_type, i, j, k, times, nb_part, nb_books, nb_modes; |
10009 | 10.8k | u32 l; |
10010 | 10.8k | char szNAME[8]; |
10011 | 10.8k | oggpack_buffer opb; |
10012 | | |
10013 | 10.8k | oggpack_readinit(&opb, (u8*)data, data_len); |
10014 | 10.8k | pack_type = oggpack_read(&opb, 8); |
10015 | 10.8k | i = 0; |
10016 | 76.2k | while (i < 6) { |
10017 | 65.3k | szNAME[i] = oggpack_read(&opb, 8); |
10018 | 65.3k | i++; |
10019 | 65.3k | } |
10020 | 10.8k | szNAME[i] = 0; |
10021 | 10.8k | if (strcmp(szNAME, "vorbis")) { |
10022 | 123 | return GF_FALSE; |
10023 | 123 | } |
10024 | | |
10025 | 10.7k | switch (pack_type) { |
10026 | 4.79k | case 0x01: |
10027 | 4.79k | vp->version = oggpack_read(&opb, 32); |
10028 | 4.79k | if (vp->version != 0) { |
10029 | 0 | return GF_FALSE; |
10030 | 0 | } |
10031 | 4.79k | vp->channels = oggpack_read(&opb, 8); |
10032 | 4.79k | vp->sample_rate = oggpack_read(&opb, 32); |
10033 | 4.79k | vp->max_r = oggpack_read(&opb, 32); |
10034 | 4.79k | vp->avg_r = oggpack_read(&opb, 32); |
10035 | 4.79k | vp->low_r = oggpack_read(&opb, 32); |
10036 | | |
10037 | 4.79k | vp->min_block = 1<<oggpack_read(&opb, 4); |
10038 | 4.79k | vp->max_block = 1<<oggpack_read(&opb, 4); |
10039 | 4.79k | if (vp->sample_rate < 1 || vp->channels < 1 || vp->min_block < 8 || vp->max_block < vp->min_block |
10040 | 4.79k | || oggpack_read(&opb, 1) != 1) { |
10041 | 56 | return GF_FALSE; |
10042 | 56 | } |
10043 | 4.74k | vp->nb_init=1; |
10044 | 4.74k | return GF_TRUE; |
10045 | | |
10046 | 3.35k | case 0x03: |
10047 | | /*trash comments*/ |
10048 | 3.35k | vp->nb_init++; |
10049 | 3.35k | return GF_TRUE; |
10050 | 2.61k | case 0x05: |
10051 | | /*need at least bitstream header to make sure we're parsing the right thing*/ |
10052 | 2.61k | if (!vp->nb_init) return GF_FALSE; |
10053 | 2.61k | break; |
10054 | 2.61k | default: |
10055 | 0 | return GF_FALSE; |
10056 | 10.7k | } |
10057 | | /*OK parse codebook*/ |
10058 | 2.61k | nb_books = oggpack_read(&opb, 8) + 1; |
10059 | | /*skip vorbis static books*/ |
10060 | 66.4k | for (i = 0; i < nb_books; i++) { |
10061 | 63.8k | u32 map_type, qb, qq; |
10062 | 63.8k | u32 entries, dim; |
10063 | 63.8k | oggpack_read(&opb, 24); |
10064 | 63.8k | dim = oggpack_read(&opb, 16); |
10065 | 63.8k | entries = oggpack_read(&opb, 24); |
10066 | 63.8k | if ((s32)entries < 0) entries = 0; |
10067 | 63.8k | if (oggpack_read(&opb, 1) == 0) { |
10068 | 62.6k | if (oggpack_read(&opb, 1)) { |
10069 | 9.62M | for (j = 0; j < entries; j++) { |
10070 | 9.60M | if (oggpack_read(&opb, 1)) { |
10071 | 5.36M | oggpack_read(&opb, 5); |
10072 | 5.36M | } |
10073 | 9.60M | } |
10074 | 19.5k | } |
10075 | 43.1k | else { |
10076 | 6.79M | for (j = 0; j < entries; j++) |
10077 | 6.74M | oggpack_read(&opb, 5); |
10078 | 43.1k | } |
10079 | 62.6k | } |
10080 | 1.13k | else { |
10081 | 1.13k | oggpack_read(&opb, 5); |
10082 | 2.27k | for (j = 0; j < entries;) { |
10083 | 1.13k | u32 num = oggpack_read(&opb, ilog(entries - j, GF_FALSE)); |
10084 | 5.68k | for (k = 0; k < num && j < entries; k++, j++) { |
10085 | 4.54k | } |
10086 | 1.13k | } |
10087 | 1.13k | } |
10088 | 63.8k | switch ((map_type = oggpack_read(&opb, 4))) { |
10089 | 29.1k | case 0: |
10090 | 29.1k | break; |
10091 | 34.7k | case 1: |
10092 | 34.7k | case 2: |
10093 | 34.7k | oggpack_read(&opb, 32); |
10094 | 34.7k | oggpack_read(&opb, 32); |
10095 | 34.7k | qq = oggpack_read(&opb, 4) + 1; |
10096 | 34.7k | oggpack_read(&opb, 1); |
10097 | 34.7k | if (map_type == 1) qb = vorbis_book_maptype1_quantvals(entries, dim); |
10098 | 0 | else if (map_type == 2) qb = entries * dim; |
10099 | 0 | else qb = 0; |
10100 | 384k | for (j = 0; j < qb; j++) oggpack_read(&opb, qq); |
10101 | 34.7k | break; |
10102 | 63.8k | } |
10103 | 63.8k | } |
10104 | 2.61k | times = oggpack_read(&opb, 6) + 1; |
10105 | 5.23k | for (i = 0; i < times; i++) oggpack_read(&opb, 16); |
10106 | 2.61k | times = oggpack_read(&opb, 6) + 1; |
10107 | 7.84k | for (i = 0; i < times; i++) { |
10108 | 5.23k | u32 type = oggpack_read(&opb, 16); |
10109 | 5.23k | if (type) { |
10110 | 2.37k | u32 *parts, *class_dims, count, rangebits; |
10111 | 2.37k | u32 max_class = 0; |
10112 | 2.37k | nb_part = oggpack_read(&opb, 5); |
10113 | 2.37k | parts = (u32*)gf_malloc(sizeof(u32) * nb_part); |
10114 | 13.0k | for (j = 0; j < nb_part; j++) { |
10115 | 10.7k | parts[j] = oggpack_read(&opb, 4); |
10116 | 10.7k | if (max_class < parts[j]) max_class = parts[j]; |
10117 | 10.7k | } |
10118 | 2.37k | class_dims = (u32*)gf_malloc(sizeof(u32) * (max_class + 1)); |
10119 | 9.53k | for (j = 0; j < max_class + 1; j++) { |
10120 | 7.16k | u32 class_sub; |
10121 | 7.16k | class_dims[j] = oggpack_read(&opb, 3) + 1; |
10122 | 7.16k | class_sub = oggpack_read(&opb, 2); |
10123 | 7.16k | if (class_sub) oggpack_read(&opb, 8); |
10124 | 27.5k | for (k = 0; k < (u32)(1 << class_sub); k++) oggpack_read(&opb, 8); |
10125 | 7.16k | } |
10126 | 2.37k | oggpack_read(&opb, 2); |
10127 | 2.37k | rangebits = oggpack_read(&opb, 4); |
10128 | 2.37k | count = 0; |
10129 | 13.0k | for (j = 0, k = 0; j < nb_part; j++) { |
10130 | 10.7k | count += class_dims[parts[j]]; |
10131 | 47.6k | for (; k < count; k++) oggpack_read(&opb, rangebits); |
10132 | 10.7k | } |
10133 | 2.37k | gf_free(parts); |
10134 | 2.37k | gf_free(class_dims); |
10135 | 2.37k | } |
10136 | 2.85k | else { |
10137 | 2.85k | oggpack_read(&opb, 8); |
10138 | 2.85k | oggpack_read(&opb, 16); |
10139 | 2.85k | oggpack_read(&opb, 16); |
10140 | 2.85k | oggpack_read(&opb, 6); |
10141 | 2.85k | oggpack_read(&opb, 8); |
10142 | 2.85k | nb_books = oggpack_read(&opb, 4) + 1; |
10143 | 5.71k | for (j = 0; j < nb_books; j++) |
10144 | 2.85k | oggpack_read(&opb, 8); |
10145 | 2.85k | } |
10146 | 5.23k | } |
10147 | 2.61k | times = oggpack_read(&opb, 6) + 1; |
10148 | 7.84k | for (i = 0; i < times; i++) { |
10149 | 5.23k | u32 acc = 0; |
10150 | 5.23k | oggpack_read(&opb, 16);/*type*/ |
10151 | 5.23k | oggpack_read(&opb, 24); |
10152 | 5.23k | oggpack_read(&opb, 24); |
10153 | 5.23k | oggpack_read(&opb, 24); |
10154 | 5.23k | nb_part = oggpack_read(&opb, 6) + 1; |
10155 | 5.23k | oggpack_read(&opb, 8); |
10156 | 47.0k | for (j = 0; j < nb_part; j++) { |
10157 | 41.8k | u32 cascade = oggpack_read(&opb, 3); |
10158 | 41.8k | if (oggpack_read(&opb, 1)) cascade |= (oggpack_read(&opb, 5) << 3); |
10159 | 41.8k | acc += icount(cascade); |
10160 | 41.8k | } |
10161 | 48.9k | for (j = 0; j < acc; j++) oggpack_read(&opb, 8); |
10162 | 5.23k | } |
10163 | 2.61k | times = oggpack_read(&opb, 6) + 1; |
10164 | 7.84k | for (i = 0; i < times; i++) { |
10165 | 5.23k | u32 sub_maps = 1; |
10166 | 5.23k | oggpack_read(&opb, 16); |
10167 | 5.23k | if (oggpack_read(&opb, 1)) sub_maps = oggpack_read(&opb, 4) + 1; |
10168 | 5.23k | if (oggpack_read(&opb, 1)) { |
10169 | 0 | u32 nb_steps = oggpack_read(&opb, 8) + 1; |
10170 | 0 | for (j = 0; j < nb_steps; j++) { |
10171 | 0 | oggpack_read(&opb, ilog(vp->channels, GF_TRUE)); |
10172 | 0 | oggpack_read(&opb, ilog(vp->channels, GF_TRUE)); |
10173 | 0 | } |
10174 | 0 | } |
10175 | 5.23k | oggpack_read(&opb, 2); |
10176 | 5.23k | if (sub_maps>1) { |
10177 | 0 | for(l=0; l<vp->channels; l++) |
10178 | 0 | oggpack_read(&opb, 4); |
10179 | 0 | } |
10180 | 10.4k | for (j = 0; j < sub_maps; j++) { |
10181 | 5.23k | oggpack_read(&opb, 8); |
10182 | 5.23k | oggpack_read(&opb, 8); |
10183 | 5.23k | oggpack_read(&opb, 8); |
10184 | 5.23k | } |
10185 | 5.23k | } |
10186 | 2.61k | nb_modes = oggpack_read(&opb, 6) + 1; |
10187 | 7.84k | for (i = 0; i < nb_modes; i++) { |
10188 | 5.23k | vp->mode_flag[i] = oggpack_read(&opb, 1); |
10189 | 5.23k | oggpack_read(&opb, 16); |
10190 | 5.23k | oggpack_read(&opb, 16); |
10191 | 5.23k | oggpack_read(&opb, 8); |
10192 | 5.23k | } |
10193 | | |
10194 | 2.61k | vp->modebits = 0; |
10195 | 2.61k | j = nb_modes; |
10196 | 5.23k | while (j > 1) { |
10197 | 2.61k | vp->modebits++; |
10198 | 2.61k | j >>= 1; |
10199 | 2.61k | } |
10200 | | |
10201 | 2.61k | return GF_TRUE; |
10202 | 2.61k | } |
10203 | | |
10204 | | GF_EXPORT |
10205 | | u32 gf_vorbis_check_frame(GF_VorbisParser *vp, u8 *data, u32 data_length) |
10206 | 17.0k | { |
10207 | 17.0k | s32 block_size; |
10208 | 17.0k | oggpack_buffer opb; |
10209 | 17.0k | if (!vp) return 0; |
10210 | 17.0k | oggpack_readinit(&opb, (unsigned char*)data, data_length); |
10211 | | /*not audio*/ |
10212 | 17.0k | if (oggpack_read(&opb, 1) != 0) return 0; |
10213 | 3.16k | block_size = oggpack_read(&opb, vp->modebits); |
10214 | 3.16k | if (block_size == -1) return 0; |
10215 | 3.16k | return ((vp->mode_flag[block_size]) ? vp->max_block : vp->min_block) / (2); |
10216 | 3.16k | } |
10217 | | |
10218 | | |
10219 | | #endif /*!defined(GPAC_DISABLE_AV_PARSERS) && !defined (GPAC_DISABLE_OGG)*/ |
10220 | | |
10221 | | |
10222 | | #if !defined(GPAC_DISABLE_AV_PARSERS) |
10223 | | |
10224 | | /*call with vorbis header packets - initializes the parser on success, leave it to NULL otherwise |
10225 | | returns 1 if success, 0 if error.*/ |
10226 | | Bool gf_opus_parse_header(GF_OpusConfig *ocfg, u8 *data, u32 data_len) |
10227 | 1.04k | { |
10228 | 1.04k | char tag[9]; |
10229 | 1.04k | GF_BitStream *bs = gf_bs_new(data, data_len, GF_BITSTREAM_READ); |
10230 | 1.04k | gf_bs_read_data(bs, tag, 8); |
10231 | 1.04k | tag[8]=0; |
10232 | | |
10233 | 1.04k | if (memcmp(data, "OpusHead", sizeof(char)*8)) { |
10234 | 0 | gf_bs_del(bs); |
10235 | 0 | return GF_FALSE; |
10236 | 0 | } |
10237 | | /*Identification Header*/ |
10238 | 1.04k | ocfg->version = gf_bs_read_u8(bs); /*version*/ |
10239 | 1.04k | if (ocfg->version != 1) { |
10240 | 0 | gf_bs_del(bs); |
10241 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[Opus] Unsupported version %d\n", ocfg->version)); |
10242 | 0 | return GF_FALSE; |
10243 | 0 | } |
10244 | 1.04k | ocfg->OutputChannelCount = gf_bs_read_u8(bs); |
10245 | 1.04k | ocfg->PreSkip = gf_bs_read_u16_le(bs); |
10246 | 1.04k | ocfg->InputSampleRate = gf_bs_read_u32_le(bs); |
10247 | 1.04k | ocfg->OutputGain = gf_bs_read_u16_le(bs); |
10248 | 1.04k | ocfg->ChannelMappingFamily = gf_bs_read_u8(bs); |
10249 | 1.04k | if (ocfg->ChannelMappingFamily != 0) { |
10250 | 114 | ocfg->StreamCount = gf_bs_read_u8(bs); |
10251 | 114 | ocfg->CoupledCount = gf_bs_read_u8(bs); |
10252 | 114 | gf_bs_read_data(bs, (char *) ocfg->ChannelMapping, ocfg->OutputChannelCount); |
10253 | 114 | } |
10254 | 1.04k | gf_bs_del(bs); |
10255 | 1.04k | return GF_TRUE; |
10256 | 1.04k | } |
10257 | | |
10258 | | /*returns 0 if init error or not an opus frame, otherwise returns the number of audio samples |
10259 | | in this frame*/ |
10260 | | u32 gf_opus_check_frame(GF_OpusConfig *ocfg, u8 *data, u32 data_length) |
10261 | 12.9k | { |
10262 | 12.9k | u32 block_size; |
10263 | | |
10264 | 12.9k | if (!data || !data_length) return 0; |
10265 | 12.9k | if (!memcmp(data, "OpusHead", sizeof(char)*8)) return 0; |
10266 | 8.01k | if (!memcmp(data, "OpusTags", sizeof(char)*8)) return 0; |
10267 | | |
10268 | | /*consider the whole packet as Ogg packets and ISOBMFF samples for Opus are framed similarly*/ |
10269 | 901 | static const int OpusFrameDurIn48k[] = { 480, 960, 1920, 2880, 480, 960, 1920, 2880, 480, 960, 1920, 2880, |
10270 | 901 | 480, 960, 480, 960, |
10271 | 901 | 120, 240, 480, 960, 120, 240, 480, 960, 120, 240, 480, 960, 120, 240, 480, 960, |
10272 | 901 | }; |
10273 | 901 | int TOC_config = (data[0] & 0xf8) >> 3; |
10274 | | //int s = (data[0] & 0x04) >> 2; |
10275 | 901 | block_size = OpusFrameDurIn48k[TOC_config]; |
10276 | | |
10277 | 901 | int c = data[0] & 0x03; |
10278 | 901 | if (c == 1 || c == 2) { |
10279 | 0 | block_size *= 2; |
10280 | 901 | } else if (c == 3) { |
10281 | | /*unknown number of frames*/ |
10282 | 901 | int num_frames = data[1] & 0x3f; |
10283 | 901 | block_size *= num_frames; |
10284 | 901 | } |
10285 | 901 | return block_size; |
10286 | 8.01k | } |
10287 | | |
10288 | | /* return nb bytes read */ |
10289 | 0 | static u8 gf_opus_read_length(u8 *data, u32 data_length, u32 offset, u16 *read_length) { |
10290 | 0 | if (!data || !data_length || !read_length) { |
10291 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Cannot read Opus length value\n")); |
10292 | 0 | return 0; |
10293 | 0 | } |
10294 | 0 | if (offset >= data_length) { |
10295 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Not enough bytes to read Opus length\n")); |
10296 | 0 | return 0; |
10297 | 0 | } |
10298 | 0 | if (data[offset] < 252) { |
10299 | 0 | *read_length = data[offset]; |
10300 | 0 | return 1; |
10301 | 0 | } else { |
10302 | 0 | if (offset+1 >= data_length) { |
10303 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Not enough bytes to read 2-byte Opus length\n")); |
10304 | 0 | return 0; |
10305 | 0 | } |
10306 | 0 | *read_length = data[offset+1]*4+data[offset]; |
10307 | 0 | return 2; |
10308 | 0 | } |
10309 | 0 | } |
10310 | | |
10311 | | GF_EXPORT |
10312 | | u8 gf_opus_parse_packet_header(u8 *data, u32 data_length, Bool self_delimited, GF_OpusPacketHeader *header) |
10313 | 657 | { |
10314 | 657 | u32 i; |
10315 | 657 | u32 nb_read_bytes; |
10316 | 657 | if (!data || !data_length) |
10317 | 0 | return 0; |
10318 | 657 | if (!header) |
10319 | 0 | return 0; |
10320 | 657 | if (data_length>=8 && !memcmp(data, "OpusHead", sizeof(char)*8)) |
10321 | 0 | return 0; |
10322 | 657 | if (data_length>=8 && !memcmp(data, "OpusTags", sizeof(char)*8)) |
10323 | 0 | return 0; |
10324 | | |
10325 | 657 | GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("Processing Opus packet, self: %d, size %d\n", self_delimited, data_length)); |
10326 | | |
10327 | 657 | if (data_length < 1) { |
10328 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Opus packet size must be at least one to parse TOC byte\n")); |
10329 | 0 | return 0; |
10330 | 0 | } |
10331 | 657 | memset(header, 0, sizeof(GF_OpusPacketHeader)); |
10332 | 657 | header->self_delimited = self_delimited; |
10333 | 657 | header->TOC_config = (data[0] & 0xf8) >> 3; |
10334 | 657 | header->TOC_stereo = (data[0] & 0x4) >> 2; |
10335 | 657 | header->TOC_code = data[0] & 0x03; |
10336 | 657 | header->size = 1; |
10337 | 657 | if (header->TOC_code == 0) { |
10338 | 0 | header->nb_frames = 1; |
10339 | 0 | if (self_delimited) { |
10340 | 0 | nb_read_bytes = gf_opus_read_length(data, data_length, header->size, &header->self_delimited_length); |
10341 | 0 | if (nb_read_bytes) { |
10342 | 0 | header->size += nb_read_bytes; |
10343 | 0 | } else { |
10344 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Could not read self delimited length in Opus packet code 0\n")); |
10345 | 0 | return 0; |
10346 | 0 | } |
10347 | | // 0 1 2 3 |
10348 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10349 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10350 | | // | config |s|0|0| N1 (1-2 bytes): | |
10351 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
10352 | | // | Compressed frame 1 (N1 bytes)... : |
10353 | | // : | |
10354 | | // | | |
10355 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10356 | 0 | header->frame_lengths[0] = header->self_delimited_length; |
10357 | 0 | } else { |
10358 | | // 0 1 2 3 |
10359 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10360 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10361 | | // | config |s|0|0| | |
10362 | | // +-+-+-+-+-+-+-+-+ | |
10363 | | // | Compressed frame 1 (N-1 bytes)... : |
10364 | | // : | |
10365 | | // | | |
10366 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10367 | 0 | header->frame_lengths[0] = data_length - header->size; |
10368 | 0 | } |
10369 | 0 | header->packet_size = header->size + header->frame_lengths[0]; |
10370 | 657 | } else if (header->TOC_code == 1) { |
10371 | 0 | header->nb_frames = 2; |
10372 | 0 | if (self_delimited) { |
10373 | 0 | nb_read_bytes = gf_opus_read_length(data, data_length, header->size, &header->self_delimited_length); |
10374 | 0 | if (nb_read_bytes) { |
10375 | 0 | header->size += nb_read_bytes; |
10376 | 0 | } else { |
10377 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Could not read self delimited length in Opus packet code 1\n")); |
10378 | 0 | return 0; |
10379 | 0 | } |
10380 | | // 0 1 2 3 |
10381 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10382 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10383 | | // | config |s|0|1| N1 (1-2 bytes): | |
10384 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : |
10385 | | // | Compressed frame 1 (N1 bytes)... | |
10386 | | // : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10387 | | // | | | |
10388 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : |
10389 | | // | Compressed frame 2 (N1 bytes)... | |
10390 | | // : +-+-+-+-+-+-+-+-+ |
10391 | | // | | |
10392 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10393 | 0 | header->frame_lengths[0] = header->self_delimited_length; |
10394 | 0 | header->frame_lengths[1] = header->self_delimited_length; |
10395 | 0 | } else { |
10396 | | // 0 1 2 3 |
10397 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10398 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10399 | | // | config |s|0|1| | |
10400 | | // +-+-+-+-+-+-+-+-+ : |
10401 | | // | Compressed frame 1 ((N-1)/2 bytes)... | |
10402 | | // : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10403 | | // | | | |
10404 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : |
10405 | | // | Compressed frame 2 ((N-1)/2 bytes)... | |
10406 | | // : +-+-+-+-+-+-+-+-+ |
10407 | | // | | |
10408 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10409 | 0 | if ((data_length-header->size) % 2) { |
10410 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Size of non-self-delimited Opus packet with code 2 must be even but is %d\n",data_length-header->size)); |
10411 | 0 | return 0; |
10412 | 0 | } |
10413 | 0 | header->frame_lengths[0] = (data_length-header->size)/2; |
10414 | 0 | header->frame_lengths[1] = (data_length-header->size)/2; |
10415 | 0 | } |
10416 | 0 | header->packet_size = header->size + header->frame_lengths[0] + header->frame_lengths[1]; |
10417 | 657 | } else if (header->TOC_code == 2) { |
10418 | 0 | header->nb_frames = 2; |
10419 | 0 | if (self_delimited) { |
10420 | 0 | nb_read_bytes = gf_opus_read_length(data, data_length, header->size, &header->self_delimited_length); |
10421 | 0 | if (nb_read_bytes) { |
10422 | 0 | header->size += nb_read_bytes; |
10423 | 0 | } else { |
10424 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Could not read self delimited length in Opus packet code 2\n")); |
10425 | 0 | return 0; |
10426 | 0 | } |
10427 | 0 | } |
10428 | 0 | nb_read_bytes = gf_opus_read_length(data, data_length, header->size, &header->code2_frame_length); |
10429 | 0 | if (nb_read_bytes) { |
10430 | 0 | header->size += nb_read_bytes; |
10431 | 0 | } else { |
10432 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Could not read frame length in Opus packet code 2\n")); |
10433 | 0 | return 0; |
10434 | 0 | } |
10435 | 0 | if (self_delimited) { |
10436 | | // 0 1 2 3 |
10437 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10438 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10439 | | // | config |s|1|0| N1 (1-2 bytes): N2 (1-2 bytes : | |
10440 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : |
10441 | | // | Compressed frame 1 (N1 bytes)... | |
10442 | | // : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10443 | | // | | | |
10444 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
10445 | | // | Compressed frame 2 (N2 bytes)... : |
10446 | | // : | |
10447 | | // | | |
10448 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10449 | 0 | header->frame_lengths[0] = header->self_delimited_length; |
10450 | 0 | header->frame_lengths[1] = header->code2_frame_length; |
10451 | 0 | } else { |
10452 | | // 0 1 2 3 |
10453 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10454 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10455 | | // | config |s|1|0| N1 (1-2 bytes): | |
10456 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ : |
10457 | | // | Compressed frame 1 (N1 bytes)... | |
10458 | | // : +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10459 | | // | | | |
10460 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
10461 | | // | Compressed frame 2... : |
10462 | | // : | |
10463 | | // | | |
10464 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10465 | 0 | header->frame_lengths[0] = header->code2_frame_length; |
10466 | 0 | header->frame_lengths[1] = data_length - header->size - header->code2_frame_length; |
10467 | 0 | } |
10468 | 0 | header->packet_size = header->size + header->frame_lengths[0] + header->frame_lengths[1]; |
10469 | 657 | } else if (header->TOC_code == 3) { |
10470 | 657 | u32 sum = 0; |
10471 | 657 | if (data_length <= header->size) { |
10472 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Not enough data to parse TOC code 3 data\n")); |
10473 | 0 | return 0; |
10474 | 0 | } |
10475 | 657 | header->code3_vbr = (data[header->size] & 0x80) >> 7; |
10476 | 657 | header->code3_padding = (data[header->size] & 0x40) >> 6; |
10477 | 657 | header->nb_frames = data[header->size] & 0x3f; |
10478 | 657 | header->size++; |
10479 | 657 | if (header->code3_padding) { |
10480 | 0 | if (data_length <= header->size) { |
10481 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Not enough data to parse TOC code 3 padding length\n")); |
10482 | 0 | return 0; |
10483 | 0 | } |
10484 | 0 | if (data[header->size] == 255) { |
10485 | 0 | header->code3_padding_length = 254 + data[header->size+1]; |
10486 | 0 | header->size += 2; |
10487 | 0 | } else { |
10488 | 0 | header->code3_padding_length = data[header->size]; |
10489 | 0 | header->size++; |
10490 | 0 | } |
10491 | 657 | } else { |
10492 | 657 | header->code3_padding_length = 0; |
10493 | 657 | } |
10494 | 657 | if (self_delimited) { |
10495 | 0 | nb_read_bytes = gf_opus_read_length(data, data_length, header->size, &header->self_delimited_length); |
10496 | 0 | if (nb_read_bytes) { |
10497 | 0 | header->size += nb_read_bytes; |
10498 | 0 | } else { |
10499 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Could not read self delimited length in Opus packet code 3\n")); |
10500 | 0 | return 0; |
10501 | 0 | } |
10502 | 0 | } |
10503 | 657 | if (header->code3_vbr) { |
10504 | 0 | u32 max; |
10505 | 0 | u32 min; |
10506 | 0 | if (self_delimited) { |
10507 | | // 0 1 2 3 |
10508 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10509 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10510 | | // | config |s|1|1|1|p| M | Padding length (Optional) : |
10511 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10512 | | // : N1 (1-2 bytes): ... : N[M-1] | N[M] : |
10513 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10514 | | // | | |
10515 | | // : Compressed frame 1 (N1 bytes)... : |
10516 | | // | | |
10517 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10518 | | // | | |
10519 | | // : Compressed frame 2 (N2 bytes)... : |
10520 | | // | | |
10521 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10522 | | // | | |
10523 | | // : ... : |
10524 | | // | | |
10525 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10526 | | // | | |
10527 | | // : Compressed frame M (N[M] bytes)... : |
10528 | | // | | |
10529 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10530 | | // : Opus Padding (Optional)... | |
10531 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10532 | 0 | header->frame_lengths[0] = header->self_delimited_length; |
10533 | 0 | min = 1; |
10534 | 0 | max = header->nb_frames; |
10535 | 0 | sum += header->frame_lengths[0]; |
10536 | 0 | } else { |
10537 | | // 0 1 2 3 |
10538 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10539 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10540 | | // | config |s|1|1|1|p| M | Padding length (Optional) : |
10541 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10542 | | // : N1 (1-2 bytes): N2 (1-2 bytes): ... : N[M-1] | |
10543 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10544 | | // | | |
10545 | | // : Compressed frame 1 (N1 bytes)... : |
10546 | | // | | |
10547 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10548 | | // | | |
10549 | | // : Compressed frame 2 (N2 bytes)... : |
10550 | | // | | |
10551 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10552 | | // | | |
10553 | | // : ... : |
10554 | | // | | |
10555 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10556 | | // | | |
10557 | | // : Compressed frame M... : |
10558 | | // | | |
10559 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10560 | | // : Opus Padding (Optional)... | |
10561 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10562 | 0 | min = 0; |
10563 | 0 | max = header->nb_frames-1; |
10564 | 0 | } |
10565 | 0 | for (i = min; i < max; i++) { |
10566 | 0 | if (data_length <= header->size) { |
10567 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Not enough data to parse TOC code 3 length\n")); |
10568 | 0 | return 0; |
10569 | 0 | } |
10570 | 0 | nb_read_bytes = gf_opus_read_length(data, data_length, header->size, &(header->frame_lengths[i])); |
10571 | 0 | if (nb_read_bytes) { |
10572 | 0 | header->size += nb_read_bytes; |
10573 | 0 | } else { |
10574 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Could not read frame length in Opus packet code 3\n")); |
10575 | 0 | return 0; |
10576 | 0 | } |
10577 | 0 | sum += header->frame_lengths[i]; |
10578 | 0 | } |
10579 | 0 | if (!self_delimited) { |
10580 | 0 | header->frame_lengths[header->nb_frames-1] = data_length - header->size - header->code3_padding_length - sum; |
10581 | 0 | sum += header->frame_lengths[header->nb_frames-1]; |
10582 | 0 | } |
10583 | 657 | } else { |
10584 | 657 | u32 cbr_length; |
10585 | 657 | if (self_delimited) { |
10586 | | // 0 1 2 3 |
10587 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10588 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10589 | | // | config |s|1|1|0|p| M | Pad len (Opt) : N1 (1-2 bytes): |
10590 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10591 | | // | | |
10592 | | // : Compressed frame 1 (N1 bytes)... : |
10593 | | // | | |
10594 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10595 | | // | | |
10596 | | // : Compressed frame 2 (N1 bytes)... : |
10597 | | // | | |
10598 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10599 | | // | | |
10600 | | // : ... : |
10601 | | // | | |
10602 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10603 | | // | | |
10604 | | // : Compressed frame M (N1 bytes)... : |
10605 | | // | | |
10606 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10607 | | // : Opus Padding (Optional)... | |
10608 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10609 | 0 | cbr_length = header->self_delimited_length; |
10610 | 657 | } else { |
10611 | | // 0 1 2 3 |
10612 | | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
10613 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10614 | | // | config |s|1|1|0|p| M | Padding length (Optional) : |
10615 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10616 | | // | | |
10617 | | // : Compressed frame 1 (R/M bytes)... : |
10618 | | // | | |
10619 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10620 | | // | | |
10621 | | // : Compressed frame 2 (R/M bytes)... : |
10622 | | // | | |
10623 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10624 | | // | | |
10625 | | // : ... : |
10626 | | // | | |
10627 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10628 | | // | | |
10629 | | // : Compressed frame M (R/M bytes)... : |
10630 | | // | | |
10631 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10632 | | // : Opus Padding (Optional)... | |
10633 | | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
10634 | 657 | if ((data_length - header->size - header->code3_padding_length) % header->nb_frames) { |
10635 | 657 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Sum of frame lengths is not a multiple of the number of frames\n")); |
10636 | 657 | return 0; |
10637 | 657 | } |
10638 | 0 | cbr_length = (data_length - header->size - header->code3_padding_length)/header->nb_frames; |
10639 | 0 | } |
10640 | 0 | for (i = 0; i < header->nb_frames; i++) { |
10641 | 0 | header->frame_lengths[i] = cbr_length; |
10642 | 0 | sum += header->frame_lengths[i]; |
10643 | 0 | } |
10644 | 0 | } |
10645 | 0 | header->packet_size = header->size + header->code3_padding_length + sum; |
10646 | 0 | } |
10647 | 0 | return 1; |
10648 | 657 | } |
10649 | | |
10650 | | u64 gf_mpegh_escaped_value(GF_BitStream *bs, u32 nBits1, u32 nBits2, u32 nBits3) |
10651 | 303k | { |
10652 | 303k | u64 value = gf_bs_read_int(bs, nBits1); |
10653 | 303k | if (value == (1<<nBits1)-1) { |
10654 | 23.3k | u32 vadd = gf_bs_read_int(bs, nBits2); |
10655 | 23.3k | value += vadd; |
10656 | 23.3k | if (vadd == (1<<nBits2)-1) { |
10657 | 3.59k | vadd = gf_bs_read_int(bs, nBits3); |
10658 | 3.59k | value += vadd; |
10659 | 3.59k | } |
10660 | 23.3k | } |
10661 | 303k | return value; |
10662 | 303k | } |
10663 | | |
10664 | | GF_EXPORT |
10665 | | s32 gf_mpegh_get_mhas_pl(u8 *ptr, u32 size, u64 *ch_layout) |
10666 | 0 | { |
10667 | 0 | s32 PL = -1; |
10668 | 0 | GF_BitStream *bs; |
10669 | 0 | u32 i; |
10670 | 0 | s32 sync_pos=-1; |
10671 | |
|
10672 | 0 | if (!ptr || !size) return 0; |
10673 | | |
10674 | 0 | for (i=0; i<size-3; i++) { |
10675 | 0 | if ((ptr[i]==0xC0) && (ptr[i+1]== 0x01) && (ptr[i+2]==0xA5)) { |
10676 | 0 | sync_pos = i; |
10677 | 0 | break; |
10678 | 0 | } |
10679 | 0 | } |
10680 | 0 | if (sync_pos<0) return 0; |
10681 | 0 | if (ch_layout) *ch_layout = 0; |
10682 | 0 | bs = gf_bs_new(ptr, size, GF_BITSTREAM_READ); |
10683 | 0 | gf_bs_skip_bytes(bs, sync_pos); |
10684 | |
|
10685 | 0 | while (gf_bs_available(bs)) { |
10686 | 0 | u32 type = (u32) gf_mpegh_escaped_value(bs, 3, 8, 8); |
10687 | 0 | /*u64 label = */gf_mpegh_escaped_value(bs, 2, 8, 32); |
10688 | 0 | u64 mh_size = gf_mpegh_escaped_value(bs, 11, 24, 24); |
10689 | 0 | if (mh_size > gf_bs_available(bs)) |
10690 | 0 | break; |
10691 | | //MHAS config |
10692 | 0 | if (type==1) { |
10693 | 0 | PL = gf_bs_read_int(bs, 8); |
10694 | 0 | if (ch_layout) { |
10695 | 0 | u32 idx = gf_bs_read_int(bs, 5); |
10696 | 0 | if (idx==0x1f) |
10697 | 0 | gf_bs_read_int(bs, 24); |
10698 | 0 | /*idx = */gf_bs_read_int(bs, 3); |
10699 | 0 | gf_bs_read_int(bs, 1); |
10700 | 0 | gf_bs_read_int(bs, 1); |
10701 | | |
10702 | | //speaker config |
10703 | 0 | idx = gf_bs_read_int(bs, 2); |
10704 | 0 | if (idx == 0) { |
10705 | 0 | *ch_layout = gf_audio_fmt_get_layout_from_cicp( gf_bs_read_int(bs, 6) ); |
10706 | 0 | } |
10707 | 0 | } |
10708 | 0 | break; |
10709 | 0 | } |
10710 | 0 | gf_bs_skip_bytes(bs, mh_size); |
10711 | 0 | } |
10712 | 0 | gf_bs_del(bs); |
10713 | 0 | return PL; |
10714 | 0 | } |
10715 | | |
10716 | | GF_EXPORT |
10717 | | void gf_vvc_parse_sei(char *buffer, u32 nal_size, VVCState *vvc) |
10718 | 2.23k | { |
10719 | 2.23k | gf_hevc_vvc_parse_sei(buffer, nal_size, NULL, vvc); |
10720 | 2.23k | } |
10721 | | |
10722 | | static Bool vvc_parse_nal_header(GF_BitStream *bs, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id) |
10723 | 488k | { |
10724 | 488k | u32 val; |
10725 | 488k | val = gf_bs_read_int_log(bs, 1, "forbidden_zero"); |
10726 | 488k | if (val) return GF_FALSE; |
10727 | 471k | val = gf_bs_read_int_log(bs, 1, "reserved_zero"); |
10728 | 471k | if (val) return GF_FALSE; |
10729 | | |
10730 | 456k | val = gf_bs_read_int_log(bs, 6, "layerID"); |
10731 | 456k | if (layer_id) *layer_id = val; |
10732 | | |
10733 | 456k | val = gf_bs_read_int_log(bs, 5, "nuh_type"); |
10734 | 456k | if (nal_unit_type) *nal_unit_type = val; |
10735 | | |
10736 | 456k | val = gf_bs_read_int_log(bs, 3, "temporalID"); |
10737 | 456k | if (!val) return GF_FALSE; |
10738 | 389k | val -= 1; |
10739 | 389k | if (temporal_id) *temporal_id = val; |
10740 | 389k | return GF_TRUE; |
10741 | 456k | } |
10742 | | |
10743 | | static void vvc_profile_tier_level(GF_BitStream *bs, VVC_ProfileTierLevel *ptl, u32 idx) |
10744 | 124k | { |
10745 | 124k | u32 i; |
10746 | 124k | if (ptl->pt_present) { |
10747 | 39.8k | ptl->general_profile_idc = gf_bs_read_int_log_idx(bs, 7, "general_profile_idc", idx); |
10748 | 39.8k | ptl->general_tier_flag = gf_bs_read_int_log_idx(bs, 1, "general_tier_flag", idx); |
10749 | 39.8k | } |
10750 | 124k | ptl->general_level_idc = gf_bs_read_int_log_idx(bs, 8, "general_level_idc", idx); |
10751 | 124k | ptl->frame_only_constraint = gf_bs_read_int_log_idx(bs, 1, "frame_only_constraint", idx); |
10752 | 124k | ptl->multilayer_enabled = gf_bs_read_int_log_idx(bs, 1, "multilayer_enabled", idx); |
10753 | | //general constraints info - max size if 1 + 81 + 8 + 255 |
10754 | 124k | if (ptl->pt_present) { |
10755 | | // general_constraints_info |
10756 | 39.8k | ptl->gci_present = gf_bs_read_int_log_idx(bs, 1, "gci_present", idx); |
10757 | 39.8k | if (ptl->gci_present) { |
10758 | 1.22k | u8 res; |
10759 | 1.22k | ptl->gci[0] = 0x80; |
10760 | 1.22k | ptl->gci[0] |= gf_bs_read_int(bs, 7); |
10761 | | //71 buts till reserved, so 71-7 = 64bits = 8 bytes till reserved |
10762 | 1.22k | gf_bs_read_data(bs, ptl->gci+1, 8); |
10763 | 1.22k | ptl->gci[10] = gf_bs_read_int(bs, 2)<<6; |
10764 | | //skip extensions |
10765 | 1.22k | ptl->gci[11] = 0; |
10766 | 1.22k | res = gf_bs_read_int(bs, 8); |
10767 | 1.22k | gf_bs_read_int(bs, res); |
10768 | 1.22k | } |
10769 | 39.8k | gf_bs_align(bs); |
10770 | 39.8k | } |
10771 | 950k | for (i=ptl->ptl_max_tid; i>0; i--) { |
10772 | 825k | ptl->sub_ptl[i-1].level_present_flag = gf_bs_read_int_log_idx2(bs, 1, "level_present_flag", idx, i); |
10773 | 825k | } |
10774 | 124k | gf_bs_align(bs); |
10775 | 950k | for (i=ptl->ptl_max_tid; i>0; i--) { |
10776 | 825k | if (ptl->sub_ptl[i-1].level_present_flag) |
10777 | 17.0k | ptl->sub_ptl[i-1].sublayer_level_idc = gf_bs_read_int_log_idx2(bs, 8, "sublayer_level_idc", idx, i); |
10778 | 825k | } |
10779 | 124k | if (ptl->pt_present) { |
10780 | 39.8k | ptl->num_sub_profiles = gf_bs_read_int_log_idx(bs, 8, "num_sub_profiles", idx); |
10781 | 371k | for (i=0; i<ptl->num_sub_profiles; i++) { |
10782 | 331k | ptl->sub_profile_idc[i] = gf_bs_read_int_log_idx2(bs, 32, "sub_profile_idc", idx, i); |
10783 | 331k | } |
10784 | 39.8k | } |
10785 | 124k | } |
10786 | | |
10787 | 2.36k | #define VVC_VPS_BROKEN {\ |
10788 | 2.36k | memset(vps, 0, sizeof(VVC_VPS)); \ |
10789 | 2.36k | return -1;\ |
10790 | 2.36k | } |
10791 | | |
10792 | | static s32 gf_vvc_read_vps_bs_internal(GF_BitStream *bs, VVCState *vvc, Bool stop_at_vps_ext) |
10793 | 6.05k | { |
10794 | 6.05k | u32 i, j; |
10795 | 6.05k | s32 vps_id; |
10796 | 6.05k | VVC_VPS *vps; |
10797 | 6.05k | Bool vps_default_ptl_dpb_hrd_max_tid_flag=0; |
10798 | | |
10799 | | //nalu header already parsed |
10800 | 6.05k | vps_id = gf_bs_read_int_log(bs, 4, "vps_id"); |
10801 | 6.05k | if ((vps_id<0) || (vps_id >= 16)) return -1; |
10802 | 6.05k | if (!vps_id) { |
10803 | 781 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] VPS ID 0 is forbidden\n")); |
10804 | 781 | return -1; |
10805 | 781 | } |
10806 | 5.26k | vps = &vvc->vps[vps_id]; |
10807 | 5.26k | if (!vps->state) { |
10808 | 2.38k | vps->id = vps_id; |
10809 | 2.38k | vps->state = 1; |
10810 | 2.38k | } |
10811 | 5.26k | vps->max_layers = 1 + gf_bs_read_int_log(bs, 6, "max_layers"); |
10812 | 5.26k | if (vps->max_layers > VVC_MAX_LAYERS) { |
10813 | 1.20k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] %d layers in VPS but only %d supported in GPAC\n", vps->max_layers, VVC_MAX_LAYERS)); |
10814 | 1.20k | VVC_VPS_BROKEN |
10815 | 1.20k | } |
10816 | 4.06k | vps->max_sub_layers = gf_bs_read_int_log(bs, 3, "max_sub_layers_minus1") + 1; |
10817 | | |
10818 | 4.06k | if ((vps->max_layers>1) && (vps->max_sub_layers>1)) |
10819 | 1.00k | vps_default_ptl_dpb_hrd_max_tid_flag = gf_bs_read_int_log(bs, 1, "vps_default_ptl_dpb_hrd_max_tid_flag"); |
10820 | | |
10821 | 4.06k | if (vps->max_layers>1) |
10822 | 1.02k | vps->all_layers_independent = gf_bs_read_int_log(bs, 1, "all_layers_independent"); |
10823 | | |
10824 | 11.0k | for (i=0; i<vps->max_layers; i++) { |
10825 | 6.98k | u32 layer_id = gf_bs_read_int_log_idx(bs, 6, "layer_id", i); |
10826 | 6.98k | if (layer_id>vps->max_layer_id) vps->max_layer_id = layer_id; |
10827 | 6.98k | if (i && !vps->all_layers_independent) { |
10828 | 1.73k | Bool layer_indep = gf_bs_read_int_log_idx(bs, 1, "layer_independent", i); |
10829 | 1.73k | if (!layer_indep) { |
10830 | 1.66k | Bool vps_max_tid_ref_present_flag = gf_bs_read_int_log_idx(bs, 1, "vps_max_tid_ref_present_flag", i); |
10831 | 5.00k | for (j=0; j<i; j++) { |
10832 | 3.33k | Bool vps_direct_ref_layer_flag = gf_bs_read_int_log_idx2(bs, 1, "vps_direct_ref_layer_flag", i, j); |
10833 | 3.33k | if (vps_max_tid_ref_present_flag && vps_direct_ref_layer_flag) { |
10834 | 306 | gf_bs_read_int_log_idx2(bs, 3, "vps_max_tid_il_ref_pics_plus1", i, j); |
10835 | 306 | } |
10836 | 3.33k | } |
10837 | 1.66k | } |
10838 | 1.73k | } |
10839 | 6.98k | } |
10840 | 4.06k | vps->num_ptl = 1; |
10841 | 4.06k | if (vps->max_layers > 1) { |
10842 | 1.02k | if (vps->all_layers_independent) { |
10843 | 436 | vps->each_layer_is_ols = gf_bs_read_int_log(bs, 1, "each_layer_is_ols"); |
10844 | 436 | } |
10845 | 1.02k | if (!vps->each_layer_is_ols) { |
10846 | 595 | u32 vps_ols_mode_idc = 2; |
10847 | 595 | if (!vps->all_layers_independent) { |
10848 | 585 | vps_ols_mode_idc = gf_bs_read_int_log(bs, 2, "vps_ols_mode_idc"); |
10849 | 585 | } |
10850 | 595 | if (vps_ols_mode_idc==2) { |
10851 | 156 | u8 vps_num_output_layer_sets = 2 + gf_bs_read_int_log(bs, 8, "vps_num_output_layer_sets_minus2"); |
10852 | 34.2k | for (i=0; i<vps_num_output_layer_sets; i++) { |
10853 | 167k | for (j=0; j<vps->max_layers; j++) { |
10854 | 133k | gf_bs_read_int_log_idx2(bs, 1, "vps_ols_output_layer_flag", i, j); |
10855 | 133k | } |
10856 | 34.1k | } |
10857 | 156 | } |
10858 | 595 | } |
10859 | 1.02k | vps->num_ptl = 1 + gf_bs_read_int_log(bs, 8, "num_ptl_minus1"); |
10860 | 1.02k | } |
10861 | 4.06k | vps->ptl[0].pt_present = 1; |
10862 | 124k | for (i=0; i<vps->num_ptl; i++) { |
10863 | 120k | if (i) |
10864 | 116k | vps->ptl[i].pt_present = gf_bs_read_int_log_idx(bs, 1, "pt_present", i); |
10865 | 120k | if (!vps_default_ptl_dpb_hrd_max_tid_flag) |
10866 | 13.6k | vps->ptl[i].ptl_max_tid = gf_bs_read_int_log_idx(bs, 3, "ptl_max_tid", i); |
10867 | 106k | else |
10868 | 106k | vps->ptl[i].ptl_max_tid = vps->max_sub_layers - 1; |
10869 | 120k | } |
10870 | | //align |
10871 | 4.06k | gf_bs_align(bs); |
10872 | | |
10873 | 124k | for (i=0; i<vps->num_ptl; i++) { |
10874 | 120k | vvc_profile_tier_level(bs, &vps->ptl[i], i); |
10875 | 120k | } |
10876 | | |
10877 | | //TODO, parse multilayer stuff |
10878 | | |
10879 | | |
10880 | 4.06k | if (gf_bs_is_overflow(bs)) |
10881 | 1.15k | VVC_VPS_BROKEN |
10882 | 2.90k | return vps_id; |
10883 | 4.06k | } |
10884 | | |
10885 | | static s32 vvc_parse_ref_pic_list_struct(GF_BitStream *bs, VVC_SPS *sps, u32 listIdx, u32 rplsIdx, VVC_RefPicList *rpl) |
10886 | 212k | { |
10887 | 212k | u32 i; |
10888 | 212k | Bool is_first = GF_TRUE; |
10889 | 212k | s32 prev_delta = 0; |
10890 | | |
10891 | 212k | memset(rpl, 0, sizeof(VVC_RefPicList)); |
10892 | 212k | rpl->num_ref_entries = gf_bs_read_ue_log_idx2(bs, "num_ref_entries", listIdx, rplsIdx); |
10893 | 212k | if (rpl->num_ref_entries>=VVC_MAX_REF_PICS) { |
10894 | 12.5k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] num_ref_entries %d exceeds maximum allowed value %d\n", rpl->num_ref_entries, VVC_MAX_REF_PICS)); |
10895 | 12.5k | return -1; |
10896 | 12.5k | } |
10897 | | |
10898 | 199k | rpl->ltrp_in_header_flag = 1; |
10899 | 199k | if (sps->long_term_ref_pics_flag |
10900 | 199k | && rplsIdx < sps->num_ref_pic_lists[listIdx] |
10901 | 199k | && (rpl->num_ref_entries > 0) |
10902 | 199k | ) { |
10903 | 6.65k | rpl->ltrp_in_header_flag = gf_bs_read_int_log_idx2(bs, 1, "ltrp_in_header_flag", listIdx, rplsIdx); |
10904 | 6.65k | } |
10905 | 520k | for (i=0; i < rpl->num_ref_entries; i++) { |
10906 | 321k | Bool inter_layer_ref_pic_flag = 0; |
10907 | 321k | if (sps->inter_layer_prediction_enabled_flag) { |
10908 | 63.3k | inter_layer_ref_pic_flag = gf_bs_read_int_log_idx3(bs, 1, "inter_layer_ref_pic_flag", listIdx, rplsIdx, i); |
10909 | 63.3k | } |
10910 | 321k | if (!inter_layer_ref_pic_flag) { |
10911 | 291k | s32 AbsDeltaPocSt; |
10912 | 291k | Bool st_ref_pic_flag = 1; |
10913 | 291k | if (sps->long_term_ref_pics_flag) { |
10914 | 74.6k | st_ref_pic_flag = gf_bs_read_int_log_idx3(bs, 1, "st_ref_pic_flag", listIdx, rplsIdx, i); |
10915 | 74.6k | } |
10916 | 291k | if (st_ref_pic_flag) { |
10917 | 245k | u32 abs_delta_poc_st = gf_bs_read_ue_log_idx3(bs, "abs_delta_poc_st", listIdx, rplsIdx, i); |
10918 | | |
10919 | 245k | if ((sps->weighted_pred_flag || sps->weighted_bipred_flag) && (i!=0)) { |
10920 | 40.6k | AbsDeltaPocSt = abs_delta_poc_st; |
10921 | 204k | } else { |
10922 | 204k | AbsDeltaPocSt = abs_delta_poc_st + 1; |
10923 | 204k | } |
10924 | | |
10925 | 245k | if (AbsDeltaPocSt>0) { |
10926 | 219k | if (gf_bs_read_int_log_idx3(bs, 1, "strp_entry_sign_flag", listIdx, rplsIdx, i)) |
10927 | 91.0k | AbsDeltaPocSt = -AbsDeltaPocSt; |
10928 | 219k | } |
10929 | 245k | if (is_first) { |
10930 | 48.5k | is_first = GF_FALSE; |
10931 | 48.5k | prev_delta = AbsDeltaPocSt; |
10932 | 196k | } else { |
10933 | 196k | AbsDeltaPocSt = prev_delta + AbsDeltaPocSt; |
10934 | 196k | prev_delta = AbsDeltaPocSt; |
10935 | 196k | } |
10936 | 245k | rpl->poc_delta[i] = AbsDeltaPocSt; |
10937 | | |
10938 | 245k | rpl->nb_short_term_pictures++; |
10939 | 245k | rpl->ref_pic_type[i] = VVC_RPL_ST; |
10940 | 245k | } else if( !rpl->ltrp_in_header_flag) { |
10941 | 8.69k | gf_bs_read_int_log_idx3(bs, sps->log2_max_poc_lsb, "rpls_poc_lsb_lt", listIdx, rplsIdx, i); |
10942 | 8.69k | rpl->nb_long_term_pictures++; |
10943 | 8.69k | rpl->ref_pic_type[i] = VVC_RPL_LT; |
10944 | 8.69k | } |
10945 | 291k | } else { |
10946 | 29.8k | gf_bs_read_ue_log_idx3(bs, "ilrp_idx", listIdx, rplsIdx, i); |
10947 | 29.8k | rpl->nb_inter_layer_pictures ++; |
10948 | 29.8k | rpl->ref_pic_type[i] = VVC_RPL_IL; |
10949 | 29.8k | } |
10950 | 321k | } |
10951 | 199k | return 0; |
10952 | 212k | } |
10953 | | |
10954 | | static void vvc_parse_general_timing_hrd_parameters(GF_BitStream *bs, VVC_SPS *sps, VVC_VPS *vps, Bool *general_nal_hrd_params_present_flag, Bool *general_vcl_hrd_params_present_flag, Bool *general_du_hrd_params_present_flag, u32 *hrd_cpb_cnt_minus1) |
10955 | 1.66k | { |
10956 | 1.66k | sps->has_timing_info = 1; |
10957 | 1.66k | sps->num_units_in_tick = gf_bs_read_int_log(bs, 32, "num_units_in_tick"); |
10958 | 1.66k | sps->time_scale = gf_bs_read_int_log(bs, 32, "timescale"); |
10959 | 1.66k | *general_du_hrd_params_present_flag = GF_FALSE; |
10960 | 1.66k | *general_nal_hrd_params_present_flag = gf_bs_read_int_log(bs, 1, "general_nal_hrd_params_present_flag"); |
10961 | 1.66k | *general_vcl_hrd_params_present_flag = gf_bs_read_int_log(bs, 1, "general_vcl_hrd_params_present_flag"); |
10962 | 1.66k | if (*general_nal_hrd_params_present_flag || *general_vcl_hrd_params_present_flag) { |
10963 | 1.00k | gf_bs_read_int_log(bs, 1, "general_same_pic_timing_in_all_ols_flag"); |
10964 | 1.00k | *general_du_hrd_params_present_flag = gf_bs_read_int_log(bs, 1, "general_du_hrd_params_present_flag"); |
10965 | 1.00k | if (*general_du_hrd_params_present_flag) |
10966 | 566 | gf_bs_read_int_log(bs, 8, "tick_divisor_minus2"); |
10967 | 1.00k | gf_bs_read_int_log(bs, 4, "bit_rate_scale"); |
10968 | 1.00k | gf_bs_read_int_log(bs, 4, "cpb_size_scale"); |
10969 | 1.00k | if (*general_du_hrd_params_present_flag) |
10970 | 566 | gf_bs_read_int_log(bs, 4, "cpb_size_du_scale"); |
10971 | 1.00k | *hrd_cpb_cnt_minus1 = gf_bs_read_ue_log(bs, "hrd_cpb_cnt_minus1"); |
10972 | 1.00k | } |
10973 | 1.66k | } |
10974 | | |
10975 | | static void vvc_parse_sublayer_hrd_parameters(GF_BitStream *bs, u32 subLayerId, Bool general_du_hrd_params_present_flag, u32 hrd_cpb_cnt_minus1) |
10976 | 10.1k | { |
10977 | 10.1k | u32 j; |
10978 | 39.8k | for (j=0; j <= hrd_cpb_cnt_minus1; j++) { |
10979 | 29.6k | gf_bs_read_ue_log_idx2(bs, "bit_rate_value_minus1", subLayerId, j); |
10980 | 29.6k | gf_bs_read_ue_log_idx2(bs, "cpb_size_value_minus1", subLayerId, j); |
10981 | 29.6k | if( general_du_hrd_params_present_flag ) { |
10982 | 8.67k | gf_bs_read_ue_log_idx2(bs, "cpb_size_du_value_minus1", subLayerId, j); |
10983 | 8.67k | gf_bs_read_ue_log_idx2(bs, "bit_rate_du_value_minus1", subLayerId, j); |
10984 | 8.67k | } |
10985 | 29.6k | gf_bs_read_int_log_idx2(bs, 1, "cbr_flag", subLayerId, j); |
10986 | 29.6k | } |
10987 | 10.1k | } |
10988 | | |
10989 | | static void vvc_parse_ols_timing_hrd_parameters(GF_BitStream *bs, u32 firstSubLayer, u32 MaxSubLayersVal, Bool general_nal_hrd_params_present_flag, Bool general_vcl_hrd_params_present_flag, Bool general_du_hrd_params_present_flag, u32 hrd_cpb_cnt_minus1) |
10990 | 1.66k | { |
10991 | 1.66k | u32 i; |
10992 | 11.9k | for (i=firstSubLayer; i<=MaxSubLayersVal; i++) { |
10993 | 10.2k | Bool fixed_pic_rate_within_cvs_flag = GF_TRUE; |
10994 | 10.2k | if (! gf_bs_read_int_log_idx(bs, 1, "fixed_pic_rate_general_flag", i)) { |
10995 | 4.09k | fixed_pic_rate_within_cvs_flag = gf_bs_read_int_log_idx(bs, 1, "fixed_pic_rate_within_cvs_flag", i); |
10996 | 4.09k | } |
10997 | 10.2k | if (fixed_pic_rate_within_cvs_flag) { |
10998 | 7.15k | gf_bs_read_ue_log(bs, "elemental_duration_in_tc_minus1"); |
10999 | 7.15k | } else if ( (general_nal_hrd_params_present_flag || general_vcl_hrd_params_present_flag) |
11000 | 3.13k | && (hrd_cpb_cnt_minus1 ==0) |
11001 | 3.13k | ) { |
11002 | 869 | gf_bs_read_int_log_idx(bs, 1, "low_delay_hrd_flag", i); |
11003 | 869 | } |
11004 | 10.2k | if (general_nal_hrd_params_present_flag) { |
11005 | 6.03k | vvc_parse_sublayer_hrd_parameters(bs, i, general_du_hrd_params_present_flag, hrd_cpb_cnt_minus1); |
11006 | 6.03k | } |
11007 | 10.2k | if (general_vcl_hrd_params_present_flag) { |
11008 | 4.10k | vvc_parse_sublayer_hrd_parameters(bs, i, general_du_hrd_params_present_flag, hrd_cpb_cnt_minus1); |
11009 | 4.10k | } |
11010 | 10.2k | } |
11011 | 1.66k | } |
11012 | | |
11013 | 19.2k | #define VVC_SPS_BROKEN {\ |
11014 | 19.2k | memset(sps, 0, sizeof(VVC_SPS)); \ |
11015 | 19.2k | return -1;\ |
11016 | 19.2k | } |
11017 | | |
11018 | | static u32 vvc_ceillog2(u32 val) |
11019 | 30.7k | { |
11020 | 30.7k | u32 maxBits=0; |
11021 | 134k | while (val > (u32)(1 << maxBits)) { |
11022 | 104k | maxBits++; |
11023 | 104k | } |
11024 | 30.7k | return maxBits; |
11025 | 30.7k | } |
11026 | | |
11027 | | static s32 gf_vvc_read_sps_bs_internal(GF_BitStream *bs, VVCState *vvc, u8 layer_id, u32 *vui_flag_pos) |
11028 | 74.4k | { |
11029 | 74.4k | s32 vps_id, sps_id; |
11030 | 74.4k | u32 i, CtbSizeY; |
11031 | 74.4k | VVC_SPS *sps; |
11032 | 74.4k | u8 sps_ptl_dpb_hrd_params_present_flag; |
11033 | | |
11034 | 74.4k | if (vui_flag_pos) *vui_flag_pos = 0; |
11035 | | |
11036 | 74.4k | sps_id = gf_bs_read_int_log(bs, 4, "sps_id"); |
11037 | 74.4k | if ((sps_id<0) || (sps_id >= 16)) { |
11038 | 0 | return -1; |
11039 | 0 | } |
11040 | 74.4k | vps_id = gf_bs_read_int_log(bs, 4, "vps_id"); |
11041 | 74.4k | if ((vps_id<0) || (vps_id >= 16)) { |
11042 | 0 | return -1; |
11043 | 0 | } |
11044 | 74.4k | if (!vps_id && !vvc->vps[0].state) { |
11045 | 1.51k | vvc->vps[0].state = 1; |
11046 | 1.51k | vvc->vps[0].num_ptl = 1; |
11047 | 1.51k | vvc->vps[0].max_layers = 1; |
11048 | 1.51k | vvc->vps[0].all_layers_independent = 1; |
11049 | 1.51k | } |
11050 | | |
11051 | 74.4k | sps = &vvc->sps[sps_id]; |
11052 | 74.4k | if (!sps->state) { |
11053 | 21.0k | sps->state = 1; |
11054 | 21.0k | sps->id = sps_id; |
11055 | 21.0k | sps->vps_id = vps_id; |
11056 | 21.0k | } |
11057 | 74.4k | sps->max_sublayers = 1 + gf_bs_read_int_log(bs, 3, "max_sublayers_minus1"); |
11058 | 74.4k | sps->chroma_format_idc = gf_bs_read_int_log(bs, 2, "chroma_format_idc"); |
11059 | 74.4k | sps->log2_ctu_size = 5 + gf_bs_read_int_log(bs, 2, "log2_ctu_size_minus5"); |
11060 | 74.4k | CtbSizeY = 1<<sps->log2_ctu_size; |
11061 | | |
11062 | 74.4k | sps_ptl_dpb_hrd_params_present_flag = gf_bs_read_int_log(bs, 1, "sps_ptl_dpb_hrd_params_present_flag"); |
11063 | 74.4k | if (sps_ptl_dpb_hrd_params_present_flag) { |
11064 | 4.77k | VVC_ProfileTierLevel ptl, *p_ptl; |
11065 | 4.77k | if (sps->vps_id) { |
11066 | 3.76k | p_ptl = &ptl; |
11067 | 3.76k | } else { |
11068 | 1.01k | p_ptl = &vvc->vps[0].ptl[0]; |
11069 | 1.01k | } |
11070 | 4.77k | memset(p_ptl, 0, sizeof(VVC_ProfileTierLevel)); |
11071 | 4.77k | p_ptl->pt_present = 1; |
11072 | 4.77k | p_ptl->ptl_max_tid = sps->max_sublayers-1; |
11073 | 4.77k | vvc_profile_tier_level(bs, p_ptl, 0); |
11074 | 4.77k | } |
11075 | 74.4k | sps->gdr_enabled = gf_bs_read_int_log(bs, 1, "gdr_enabled"); |
11076 | 74.4k | sps->ref_pic_resampling = gf_bs_read_int_log(bs, 1, "ref_pic_resampling"); |
11077 | 74.4k | if (sps->ref_pic_resampling) |
11078 | 16.4k | sps->res_change_in_clvs = gf_bs_read_int_log(bs, 1, "res_change_in_clvs"); |
11079 | 74.4k | sps->width = gf_bs_read_ue_log(bs, "width"); |
11080 | 74.4k | sps->height = gf_bs_read_ue_log(bs, "height"); |
11081 | 74.4k | sps->conf_window = gf_bs_read_int_log(bs, 1, "conformance_window_present_flag"); |
11082 | 74.4k | if (sps->conf_window) { |
11083 | 19.9k | u32 SubWidthC, SubHeightC; |
11084 | 19.9k | sps->cw_left = gf_bs_read_ue_log(bs, "conformance_window_left"); |
11085 | 19.9k | sps->cw_right = gf_bs_read_ue_log(bs, "conformance_window_right"); |
11086 | 19.9k | sps->cw_top = gf_bs_read_ue_log(bs, "conformance_window_top"); |
11087 | 19.9k | sps->cw_bottom = gf_bs_read_ue_log(bs, "conformance_window_bottom"); |
11088 | | |
11089 | | |
11090 | 19.9k | if (sps->chroma_format_idc == 1) { |
11091 | 61 | SubWidthC = SubHeightC = 2; |
11092 | 19.9k | } else if (sps->chroma_format_idc == 2) { |
11093 | 6.18k | SubWidthC = 2; |
11094 | 6.18k | SubHeightC = 1; |
11095 | 13.7k | } else { |
11096 | 13.7k | SubWidthC = SubHeightC = 1; |
11097 | 13.7k | } |
11098 | 19.9k | sps->width -= SubWidthC * (sps->cw_left + sps->cw_right); |
11099 | 19.9k | sps->height -= SubHeightC * (sps->cw_top + sps->cw_bottom); |
11100 | 19.9k | } |
11101 | | |
11102 | 74.4k | sps->subpic_info_present = gf_bs_read_int_log(bs, 1, "subpic_info_present"); |
11103 | 74.4k | if (sps->subpic_info_present) { |
11104 | 40.7k | sps->nb_subpics = 1 + gf_bs_read_ue_log(bs, "nb_subpics_minus1"); |
11105 | 40.7k | if (sps->nb_subpics>1000) VVC_SPS_BROKEN |
11106 | 40.6k | if (sps->nb_subpics>VVC_MAX_SUBPIC) { |
11107 | 148 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] Maximum subpics supported by GPAC is %u, got %u\n", VVC_MAX_SUBPIC, sps->nb_subpics)); |
11108 | 148 | VVC_SPS_BROKEN |
11109 | 148 | } |
11110 | | |
11111 | 40.4k | if (sps->nb_subpics>1) { |
11112 | 3.28k | u32 tmpWidthBits, tmpHeightBits; |
11113 | 3.28k | sps->independent_subpic_flags = gf_bs_read_int_log(bs, 1, "independent_subpic_flags"); |
11114 | 3.28k | sps->subpic_same_size = gf_bs_read_int_log(bs, 1, "subpic_same_size"); |
11115 | | |
11116 | 3.28k | tmpWidthBits = vvc_ceillog2((sps->width + CtbSizeY-1) / CtbSizeY); |
11117 | 3.28k | tmpHeightBits = vvc_ceillog2((sps->height + CtbSizeY-1) / CtbSizeY); |
11118 | | |
11119 | 26.5k | for (i=0; i<sps->nb_subpics; i++) { |
11120 | 23.2k | VVC_SubpicInfo *sp = &sps->subpics[i]; |
11121 | 23.2k | sp->id = i; |
11122 | 23.2k | if( !sps->subpic_same_size || !i) { |
11123 | 19.2k | if (i && (sps->width > CtbSizeY)) |
11124 | 6.77k | sp->x = gf_bs_read_int_log(bs, tmpWidthBits, "subpic_ctu_top_left_x"); |
11125 | 19.2k | if (i && (sps->height > CtbSizeY)) |
11126 | 4.52k | sp->y = gf_bs_read_int_log(bs, tmpHeightBits, "subpic_ctu_top_left_y"); |
11127 | 19.2k | if ((i+1 < sps->nb_subpics) && (sps->width > CtbSizeY)) |
11128 | 6.99k | sp->w = 1 + gf_bs_read_int_log(bs, tmpWidthBits, "subpic_width_minus1"); |
11129 | 19.2k | if ((i+1 < sps->nb_subpics) && (sps->height > CtbSizeY)) |
11130 | 4.62k | sp->h = 1 + gf_bs_read_int_log(bs, tmpHeightBits, "subpic_height_minus1"); |
11131 | 19.2k | } else { |
11132 | 4.00k | sp->w = sps->subpics[0].w; |
11133 | 4.00k | sp->h = sps->subpics[0].h; |
11134 | 4.00k | } |
11135 | 23.2k | if (!sps->independent_subpic_flags) { |
11136 | 14.3k | gf_bs_read_int_log(bs, 1, "subpic_treated_as_pic_flag"); |
11137 | 14.3k | gf_bs_read_int_log(bs, 1, "loop_filter_across_subpic_enabled_flag"); |
11138 | 14.3k | } |
11139 | 23.2k | } |
11140 | 3.28k | } |
11141 | | //coded even if nb_subpics<=1 |
11142 | 40.4k | sps->subpicid_len = gf_bs_read_ue_log(bs, "subpic_id_len_minus1") + 1; |
11143 | 40.4k | sps->subpicid_mapping_explicit = gf_bs_read_int_log(bs, 1, "subpic_id_mapping_explicitly_signalled_flag"); |
11144 | 40.4k | if (sps->subpicid_mapping_explicit) { |
11145 | 18.2k | sps->subpicid_mapping_present = gf_bs_read_int_log(bs, 1, "subpic_id_mapping_present_flag"); |
11146 | 18.2k | if (sps->subpicid_mapping_present) { |
11147 | 35.0k | for (i=0; i<sps->nb_subpics; i++) { |
11148 | 19.3k | VVC_SubpicInfo *sp = &sps->subpics[i]; |
11149 | 19.3k | sp->id = gf_bs_read_int_log_idx(bs, sps->subpicid_len, "subpic_id", i); |
11150 | 19.3k | } |
11151 | 15.6k | } |
11152 | 18.2k | } |
11153 | 40.4k | } else { |
11154 | 33.7k | sps->nb_subpics = 1; |
11155 | 33.7k | } |
11156 | 74.2k | sps->bitdepth = gf_bs_read_ue_log(bs, "bitdepth_minus8") + 8; |
11157 | 74.2k | sps->entropy_coding_sync_enabled_flag = gf_bs_read_int_log(bs, 1, "entropy_coding_sync_enabled_flag"); |
11158 | 74.2k | sps->entry_point_offsets_present_flag = gf_bs_read_int_log(bs, 1, "entry_point_offsets_present_flag"); |
11159 | 74.2k | sps->log2_max_poc_lsb = 4 + gf_bs_read_int_log(bs, 4, "log2_max_poc_lsb_minus4"); |
11160 | 74.2k | if ((sps->poc_msb_cycle_flag = gf_bs_read_int_log(bs, 1, "poc_msb_cycle_flag"))) |
11161 | 29.8k | sps->poc_msb_cycle_len = 1 + gf_bs_read_ue_log(bs, "poc_msb_cycle_len_minus1"); |
11162 | | |
11163 | 74.2k | u8 sps_num_extra_ph_bits = 8 * gf_bs_read_int_log(bs, 2, "sps_num_extra_ph_bytes"); |
11164 | 876k | for (i=0; i<sps_num_extra_ph_bits; i++) { |
11165 | 802k | if (gf_bs_read_int_log_idx(bs, 1, "extra_ph_bit_present_flag", 1)) |
11166 | 362k | sps->ph_num_extra_bits++; |
11167 | 802k | } |
11168 | 74.2k | u8 sps_num_extra_sh_bits = 8 * gf_bs_read_int_log(bs, 2, "num_extra_sh_bytes"); |
11169 | 430k | for (i=0; i<sps_num_extra_sh_bits; i++) { |
11170 | 356k | if (gf_bs_read_int_log_idx(bs, 1, "extra_sh_bit_present_flag", i)) |
11171 | 193k | sps->sh_num_extra_bits++; |
11172 | 356k | } |
11173 | | |
11174 | 74.2k | if (sps_ptl_dpb_hrd_params_present_flag) { |
11175 | 4.76k | u8 sps_sublayer_dpb_params_flag = 0; |
11176 | 4.76k | if (sps->max_sublayers>1) { |
11177 | 4.57k | sps_sublayer_dpb_params_flag = gf_bs_read_int_log(bs, 1, "sps_sublayer_dpb_params_flag"); |
11178 | 4.57k | } |
11179 | 12.3k | for (i=(sps_sublayer_dpb_params_flag ? 0 : sps->max_sublayers-1); i < sps->max_sublayers; i++ ) { |
11180 | 7.59k | gf_bs_read_ue_log_idx(bs, "dpb_max_dec_pic_buffering_minus1", i); |
11181 | 7.59k | gf_bs_read_ue_log_idx(bs, "dpb_max_num_reorder_pics", i); |
11182 | 7.59k | gf_bs_read_ue_log_idx(bs, "dpb_max_latency_increase_plus1", i); |
11183 | 7.59k | } |
11184 | 4.76k | } |
11185 | 74.2k | gf_bs_read_ue_log(bs, "sps_log2_min_luma_coding_block_size_minus2"); |
11186 | 74.2k | sps->partition_constraints_override_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_partition_constraints_override_enabled_flag"); |
11187 | 74.2k | gf_bs_read_ue_log(bs, "sps_log2_min_luma_coding_block_size_minus2"); |
11188 | 74.2k | u8 sps_max_mtt_hierarchy_depth_intra_slice_luma = gf_bs_read_ue_log(bs, "sps_max_mtt_hierarchy_depth_intra_slice_luma"); |
11189 | 74.2k | if (sps_max_mtt_hierarchy_depth_intra_slice_luma != 0) { |
11190 | 24.8k | gf_bs_read_ue_log(bs, "sps_log2_diff_max_bt_min_qt_intra_slice_luma"); |
11191 | 24.8k | gf_bs_read_ue_log(bs, "sps_log2_diff_max_tt_min_qt_intra_slice_luma"); |
11192 | 24.8k | } |
11193 | 74.2k | u8 sps_qtbtt_dual_tree_intra_flag = 0; |
11194 | 74.2k | if (sps->chroma_format_idc) { |
11195 | 17.5k | sps_qtbtt_dual_tree_intra_flag = gf_bs_read_int_log(bs, 1, "sps_qtbtt_dual_tree_intra_flag"); |
11196 | 17.5k | } |
11197 | 74.2k | if (sps_qtbtt_dual_tree_intra_flag) { |
11198 | 13.3k | gf_bs_read_ue_log(bs, "sps_log2_diff_min_qt_min_cb_intra_slice_chroma"); |
11199 | 13.3k | u8 sps_max_mtt_hierarchy_depth_intra_slice_chroma = gf_bs_read_ue_log(bs, "sps_max_mtt_hierarchy_depth_intra_slice_chroma"); |
11200 | 13.3k | if( sps_max_mtt_hierarchy_depth_intra_slice_chroma != 0) { |
11201 | 3.69k | gf_bs_read_ue_log(bs, "sps_log2_diff_max_bt_min_qt_intra_slice_chroma"); |
11202 | 3.69k | gf_bs_read_ue_log(bs, "sps_log2_diff_max_tt_min_qt_intra_slice_chroma"); |
11203 | 3.69k | } |
11204 | 13.3k | } |
11205 | | |
11206 | 74.2k | gf_bs_read_ue_log(bs, "sps_log2_diff_min_qt_min_cb_inter_slice"); |
11207 | 74.2k | u8 sps_max_mtt_hierarchy_depth_inter_slice = gf_bs_read_ue_log(bs, "sps_max_mtt_hierarchy_depth_inter_slice"); |
11208 | 74.2k | if (sps_max_mtt_hierarchy_depth_inter_slice != 0) { |
11209 | 35.8k | gf_bs_read_ue_log(bs, "sps_log2_diff_max_bt_min_qt_inter_slice"); |
11210 | 35.8k | gf_bs_read_ue_log(bs, "sps_log2_diff_max_tt_min_qt_inter_slice"); |
11211 | 35.8k | } |
11212 | 74.2k | u8 max_luma_transform_size_64_flag = 0; |
11213 | 74.2k | if (CtbSizeY > 32) { |
11214 | 20.7k | max_luma_transform_size_64_flag = gf_bs_read_int_log(bs, 1, "sps_max_luma_transform_size_64_flag"); |
11215 | 20.7k | } |
11216 | 74.2k | sps->transform_skip_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_transform_skip_enabled_flag"); |
11217 | | |
11218 | 74.2k | if (sps->transform_skip_enabled_flag) { |
11219 | 40.2k | gf_bs_read_ue_log(bs, "sps_log2_transform_skip_max_size_minus2"); |
11220 | 40.2k | gf_bs_read_int_log(bs, 1, "sps_bdpcm_enabled_flag"); |
11221 | 40.2k | } |
11222 | 74.2k | if (gf_bs_read_int_log(bs, 1, "sps_mts_enabled_flag")) { |
11223 | 37.0k | gf_bs_read_int_log(bs, 1, "sps_explicit_mts_intra_enabled_flag"); |
11224 | 37.0k | gf_bs_read_int_log(bs, 1, "sps_explicit_mts_inter_enabled_flag"); |
11225 | 37.0k | } |
11226 | 74.2k | Bool lfnst_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_lfnst_enabled_flag"); |
11227 | 74.2k | sps->joint_cbcr_enabled_flag = 0; |
11228 | 74.2k | if (sps->chroma_format_idc) { |
11229 | 17.5k | sps->joint_cbcr_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_joint_cbcr_enabled_flag"); |
11230 | 17.5k | u8 sps_same_qp_table_for_chroma_flag = gf_bs_read_int_log(bs, 1, "sps_same_qp_table_for_chroma_flag"); |
11231 | 17.5k | u32 numQpTables = sps_same_qp_table_for_chroma_flag ? 1 : (sps->joint_cbcr_enabled_flag ? 3 : 2); |
11232 | 45.6k | for (i=0; i<numQpTables; i++) { |
11233 | 28.1k | gf_bs_read_se_log_idx(bs, "sps_qp_table_start_minus26", i); |
11234 | 28.1k | u32 j, sps_num_points_in_qp_table = 1 + gf_bs_read_ue_log_idx(bs, "sps_num_points_in_qp_table_minus1", i); |
11235 | 4.08M | for (j=0; j<sps_num_points_in_qp_table; j++) { |
11236 | 4.05M | gf_bs_read_ue_log_idx2(bs, "sps_delta_qp_in_val_minus1", i, j); |
11237 | 4.05M | gf_bs_read_ue_log_idx2(bs, "sps_delta_qp_diff_val", i, j); |
11238 | 4.05M | } |
11239 | 28.1k | } |
11240 | 17.5k | } |
11241 | 74.2k | sps->sao_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_sao_enabled_flag"); |
11242 | 74.2k | sps->alf_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_alf_enabled_flag"); |
11243 | 74.2k | if (sps->alf_enabled_flag && sps->chroma_format_idc) { |
11244 | 11.4k | sps->ccalf_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_ccalf_enabled_flag"); |
11245 | 11.4k | } |
11246 | 74.2k | sps->lmcs_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_lmcs_enabled_flag"); |
11247 | 74.2k | sps->weighted_pred_flag = gf_bs_read_int_log(bs, 1, "sps_weighted_pred_flag"); |
11248 | 74.2k | sps->weighted_bipred_flag = gf_bs_read_int_log(bs, 1, "sps_weighted_bipred_flag"); |
11249 | 74.2k | sps->long_term_ref_pics_flag = gf_bs_read_int_log(bs, 1, "sps_long_term_ref_pics_flag"); |
11250 | 74.2k | if (sps->vps_id>0) |
11251 | 42.8k | sps->inter_layer_prediction_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_inter_layer_prediction_enabled_flag"); |
11252 | 74.2k | sps->idr_rpl_present_flag = gf_bs_read_int_log(bs, 1, "sps_idr_rpl_present_flag"); |
11253 | 74.2k | u32 sps_rpl1_same_as_rpl0 = gf_bs_read_int_log(bs, 1, "sps_rpl1_same_as_rpl0_flag") ? 1: 2; |
11254 | 175k | for (i=0; i<sps_rpl1_same_as_rpl0; i++) { |
11255 | 110k | u32 j; |
11256 | 110k | sps->num_ref_pic_lists[i] = gf_bs_read_ue_log_idx(bs, "sps_num_ref_pic_lists", i); |
11257 | 110k | if (sps->num_ref_pic_lists[i] > 64) VVC_SPS_BROKEN |
11258 | | |
11259 | 195k | for (j=0; j<sps->num_ref_pic_lists[i]; j++) { |
11260 | 94.9k | s32 res = vvc_parse_ref_pic_list_struct(bs, sps, i, j, &sps->rps[i][j]); |
11261 | 94.9k | if (res<0) VVC_SPS_BROKEN |
11262 | 94.9k | } |
11263 | 106k | } |
11264 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_ref_wraparound_enabled_flag"); |
11265 | 64.7k | sps->temporal_mvp_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_temporal_mvp_enabled_flag"); |
11266 | 64.7k | if (sps->temporal_mvp_enabled_flag) { |
11267 | 46.8k | gf_bs_read_int_log(bs, 1, "sps_sbtmvp_enabled_flag"); |
11268 | 46.8k | } |
11269 | 64.7k | Bool amvr_enabled = gf_bs_read_int_log(bs, 1, "sps_amvr_enabled_flag"); |
11270 | 64.7k | sps->bdof_control_present_in_ph_flag = 0; |
11271 | 64.7k | if (gf_bs_read_int_log(bs, 1, "sps_bdof_enabled_flag")) { |
11272 | 38.3k | sps->bdof_control_present_in_ph_flag = gf_bs_read_int_log(bs, 1, "sps_bdof_control_present_in_ph_flag"); |
11273 | 38.3k | } |
11274 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_smvd_enabled_flag"); |
11275 | 64.7k | sps->dmvr_control_present_in_ph_flag = 0; |
11276 | 64.7k | if (gf_bs_read_int_log(bs, 1, "sps_dmvr_enabled_flag")) { |
11277 | 37.8k | sps->dmvr_control_present_in_ph_flag = gf_bs_read_int_log(bs, 1, "sps_dmvr_control_present_in_ph_flag"); |
11278 | 37.8k | } |
11279 | 64.7k | sps->mmvd_fullpel_only_enabled_flag = 0; |
11280 | 64.7k | if (gf_bs_read_int_log(bs, 1, "sps_mmvd_enabled_flag")) { |
11281 | 38.4k | sps->mmvd_fullpel_only_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_mmvd_fullpel_only_enabled_flag"); |
11282 | 38.4k | } |
11283 | 64.7k | u32 MaxNumMergeCand = 6 - gf_bs_read_ue_log(bs, "sps_six_minus_max_num_merge_cand"); |
11284 | | |
11285 | 64.7k | sps->prof_control_present_in_ph_flag = 0; |
11286 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_sbt_enabled_flag"); |
11287 | 64.7k | if (gf_bs_read_int_log(bs, 1, "sps_affine_enabled_flag")) { |
11288 | 45.7k | gf_bs_read_ue_log(bs, "sps_five_minus_max_num_subblock_merge_cand"); |
11289 | 45.7k | gf_bs_read_int_log(bs, 1, "sps_6param_affine_enabled_flag"); |
11290 | 45.7k | if (amvr_enabled) { |
11291 | 39.3k | gf_bs_read_int_log(bs, 1, "sps_affine_amvr_enabled_flag"); |
11292 | 39.3k | } |
11293 | 45.7k | if (gf_bs_read_int_log(bs, 1, "sps_affine_prof_enabled_flag")) { |
11294 | 31.6k | sps->prof_control_present_in_ph_flag = gf_bs_read_int_log(bs, 1, "sps_prof_control_present_in_ph_flag"); |
11295 | 31.6k | } |
11296 | 45.7k | } |
11297 | | |
11298 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_bcw_enabled_flag"); |
11299 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_ciip_enabled_flag"); |
11300 | 64.7k | if (MaxNumMergeCand >= 2) { |
11301 | 62.9k | Bool gpm_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_gpm_enabled_flag"); |
11302 | 62.9k | if (gpm_enabled_flag && (MaxNumMergeCand >= 3)) { |
11303 | 37.4k | gf_bs_read_ue_log(bs, "sps_max_num_merge_cand_minus_max_num_gpm_cand"); |
11304 | 37.4k | } |
11305 | 62.9k | } |
11306 | 64.7k | gf_bs_read_ue_log(bs, "sps_log2_parallel_merge_level_minus2"); |
11307 | | |
11308 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_isp_enabled_flag"); |
11309 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_mrl_enabled_flag"); |
11310 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_mip_enabled_flag"); |
11311 | 64.7k | if (sps->chroma_format_idc != 0) { |
11312 | 16.8k | gf_bs_read_int_log(bs, 1, "sps_cclm_enabled_flag"); |
11313 | 16.8k | } |
11314 | 64.7k | if (sps->chroma_format_idc == 1) { |
11315 | 1.37k | gf_bs_read_int_log(bs, 1, "sps_chroma_horizontal_collocated_flag"); |
11316 | 1.37k | gf_bs_read_int_log(bs, 1, "sps_chroma_vertical_collocated_flag"); |
11317 | 1.37k | } |
11318 | 64.7k | Bool act_enabled_flag = GF_FALSE; |
11319 | 64.7k | Bool palette_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_palette_enabled_flag"); |
11320 | 64.7k | if ((sps->chroma_format_idc == 3) && !max_luma_transform_size_64_flag) { |
11321 | 6.96k | act_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_act_enabled_flag"); |
11322 | 6.96k | } |
11323 | 64.7k | if (sps->transform_skip_enabled_flag || palette_enabled_flag) { |
11324 | 42.8k | gf_bs_read_ue_log(bs, "sps_min_qp_prime_ts"); |
11325 | 42.8k | } |
11326 | 64.7k | if (gf_bs_read_int_log(bs, 1, "sps_ibc_enabled_flag")) { |
11327 | 30.2k | gf_bs_read_ue_log(bs, "sps_six_minus_max_num_ibc_merge_cand"); |
11328 | 30.2k | } |
11329 | 64.7k | if (gf_bs_read_int_log(bs, 1, "sps_ladf_enabled_flag")) { |
11330 | 35.6k | u32 num_ladf_intervals_minus2 = gf_bs_read_int_log(bs, 2, "sps_num_ladf_intervals_minus2"); |
11331 | 35.6k | gf_bs_read_se_log(bs, "sps_ladf_lowest_interval_qp_offset"); |
11332 | 149k | for (i=0; i<num_ladf_intervals_minus2+1; i++) { |
11333 | 114k | gf_bs_read_se_log_idx(bs, "sps_ladf_qp_offset", i); |
11334 | 114k | gf_bs_read_ue_log_idx(bs, "sps_ladf_delta_threshold_minus1", i); |
11335 | 114k | } |
11336 | 35.6k | } |
11337 | 64.7k | sps->explicit_scaling_list_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_explicit_scaling_list_enabled_flag"); |
11338 | 64.7k | if (lfnst_enabled_flag && sps->explicit_scaling_list_enabled_flag) { |
11339 | 11.3k | gf_bs_read_int_log(bs, 1, "sps_scaling_matrix_for_lfnst_disabled_flag"); |
11340 | 11.3k | } |
11341 | 64.7k | Bool scaling_matrix_for_alternative_colour_space_disabled_flag = 0; |
11342 | 64.7k | if (act_enabled_flag && sps->explicit_scaling_list_enabled_flag) { |
11343 | 634 | scaling_matrix_for_alternative_colour_space_disabled_flag = gf_bs_read_int_log(bs, 1, "sps_scaling_matrix_for_alternative_colour_space_disabled_flag"); |
11344 | 634 | } |
11345 | 64.7k | if (scaling_matrix_for_alternative_colour_space_disabled_flag) { |
11346 | 594 | gf_bs_read_int_log(bs, 1, "sps_scaling_matrix_designated_colour_space_flag"); |
11347 | 594 | } |
11348 | 64.7k | sps->dep_quant_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_dep_quant_enabled_flag"); |
11349 | 64.7k | sps->sign_data_hiding_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_sign_data_hiding_enabled_flag"); |
11350 | 64.7k | sps->virtual_boundaries_enabled_flag = gf_bs_read_int_log(bs, 1, "sps_virtual_boundaries_enabled_flag"); |
11351 | 64.7k | if (sps->virtual_boundaries_enabled_flag) { |
11352 | 27.6k | sps->virtual_boundaries_present_flag = gf_bs_read_int_log(bs, 1, "sps_virtual_boundaries_present_flag"); |
11353 | 27.6k | if (sps->virtual_boundaries_present_flag) { |
11354 | 18.6k | u32 num_virtual_boundaries = gf_bs_read_ue_log(bs, "sps_num_ver_virtual_boundaries"); |
11355 | 28.8k | for (i=0; i<num_virtual_boundaries; i++) { |
11356 | 10.2k | gf_bs_read_ue_log_idx(bs, "sps_virtual_boundary_pos_x_minus1", i); |
11357 | 10.2k | } |
11358 | 18.6k | num_virtual_boundaries = gf_bs_read_ue_log(bs, "sps_num_hor_virtual_boundaries"); |
11359 | 281k | for (i=0; i<num_virtual_boundaries; i++) { |
11360 | 263k | gf_bs_read_ue_log_idx(bs, "sps_virtual_boundary_pos_y_minus1", i); |
11361 | 263k | } |
11362 | 18.6k | } |
11363 | 27.6k | } |
11364 | | |
11365 | 64.7k | if (sps_ptl_dpb_hrd_params_present_flag) { |
11366 | 4.69k | if (gf_bs_read_int_log(bs, 1, "sps_timing_hrd_params_present_flag")) { |
11367 | 1.66k | Bool general_nal_hrd_params_present_flag, general_vcl_hrd_params_present_flag, general_du_hrd_params_present_flag; |
11368 | 1.66k | u32 hrd_cpb_cnt_minus1=0; |
11369 | 1.66k | u32 sublayer_cpb_params_present_flag = 0; |
11370 | 1.66k | vvc_parse_general_timing_hrd_parameters(bs, sps, NULL, &general_nal_hrd_params_present_flag, &general_vcl_hrd_params_present_flag, &general_du_hrd_params_present_flag, &hrd_cpb_cnt_minus1); |
11371 | 1.66k | if (sps->max_sublayers > 1) { |
11372 | 1.64k | sublayer_cpb_params_present_flag = gf_bs_read_int_log(bs, 1, "sps_sublayer_cpb_params_present_flag"); |
11373 | 1.64k | } |
11374 | 1.66k | u32 firstSubLayer = sublayer_cpb_params_present_flag ? 0 : sps->max_sublayers - 1; |
11375 | 1.66k | vvc_parse_ols_timing_hrd_parameters(bs, firstSubLayer, sps->max_sublayers-1, general_nal_hrd_params_present_flag, general_vcl_hrd_params_present_flag, general_du_hrd_params_present_flag, hrd_cpb_cnt_minus1); |
11376 | | |
11377 | 1.66k | } |
11378 | 4.69k | } |
11379 | | |
11380 | 64.7k | gf_bs_read_int_log(bs, 1, "sps_field_seq_flag"); |
11381 | 64.7k | if (vui_flag_pos) { |
11382 | 0 | *vui_flag_pos = (u32)gf_bs_get_bit_offset(bs); |
11383 | 0 | } |
11384 | | //all this to get to VUI !!! |
11385 | 64.7k | if (gf_bs_read_int_log(bs, 1, "sps_vui_parameters_present_flag")) { |
11386 | 22.5k | gf_bs_read_ue_log(bs, "sps_vui_payload_size_minus1"); |
11387 | 88.2k | while (!gf_bs_is_align(bs)) { |
11388 | 65.6k | gf_bs_read_int_log(bs, 1, "sps_vui_alignment_zero_bit"); |
11389 | 65.6k | } |
11390 | | //vui parameters |
11391 | 22.5k | Bool vui_progressive_source_flag = gf_bs_read_int_log(bs, 1, "vui_progressive_source_flag"); |
11392 | 22.5k | Bool vui_interlaced_source_flag = gf_bs_read_int_log(bs, 1, "vui_interlaced_source_flag"); |
11393 | 22.5k | gf_bs_read_int_log(bs, 1, "vui_non_packed_constraint_flag"); |
11394 | 22.5k | gf_bs_read_int_log(bs, 1, "vui_non_projected_constraint_flag"); |
11395 | 22.5k | sps->aspect_ratio_info_present_flag = gf_bs_read_int_log(bs, 1, "vui_aspect_ratio_info_present_flag"); |
11396 | 22.5k | if (sps->aspect_ratio_info_present_flag) { |
11397 | 8.53k | gf_bs_read_int_log(bs, 1, "vui_aspect_ratio_constant_flag"); |
11398 | 8.53k | sps->sar_idc = gf_bs_read_int_log(bs, 8, "vui_aspect_ratio_idc"); |
11399 | 8.53k | if (sps->sar_idc== 0xFF) { |
11400 | 2.10k | sps->sar_width = gf_bs_read_int_log(bs, 16, "vui_sar_width"); |
11401 | 2.10k | sps->sar_height = gf_bs_read_int_log(bs, 16, "vui_sar_height"); |
11402 | 2.10k | } |
11403 | 8.53k | } |
11404 | 22.5k | sps->overscan_info_present_flag = gf_bs_read_int_log(bs, 1, "vui_overscan_info_present_flag"); |
11405 | 22.5k | if (sps->overscan_info_present_flag) { |
11406 | 10.1k | gf_bs_read_int_log(bs, 1, "vui_overscan_appropriate_flag"); |
11407 | 10.1k | } |
11408 | 22.5k | sps->colour_description_present_flag = gf_bs_read_int_log(bs, 1, "vui_colour_description_present_flag"); |
11409 | 22.5k | if (sps->colour_description_present_flag) { |
11410 | 7.79k | sps->colour_primaries = gf_bs_read_int_log(bs, 8, "vui_colour_primaries"); |
11411 | 7.79k | sps->transfer_characteristics = gf_bs_read_int_log(bs, 8, "vui_transfer_characteristics"); |
11412 | 7.79k | sps->matrix_coefficients = gf_bs_read_int_log(bs, 8, "vui_matrix_coeffs"); |
11413 | 7.79k | sps->video_full_range_flag = gf_bs_read_int_log(bs, 1, "vui_full_range_flag"); |
11414 | 7.79k | } |
11415 | 22.5k | if (gf_bs_read_int_log(bs, 1, " vui_chroma_loc_info_present_flag")) { |
11416 | 8.15k | if (vui_progressive_source_flag && !vui_interlaced_source_flag) { |
11417 | 171 | gf_bs_read_ue_log(bs, "vui_chroma_sample_loc_type_frame"); |
11418 | 7.97k | } else { |
11419 | 7.97k | gf_bs_read_ue_log(bs, "vui_chroma_sample_loc_type_top_field"); |
11420 | 7.97k | gf_bs_read_ue_log(bs, "vui_chroma_sample_loc_type_bottom_field"); |
11421 | 7.97k | } |
11422 | 8.15k | } |
11423 | | //WE DON'T PARSE vui_payload_bit_equal_to_one because we dont parse the rest (sps extensions) |
11424 | | //if needed, see rewrite_vui code |
11425 | 22.5k | } |
11426 | | |
11427 | 64.7k | if (gf_bs_is_overflow(bs)) |
11428 | 9.51k | VVC_SPS_BROKEN |
11429 | 55.2k | return sps_id; |
11430 | 64.7k | } |
11431 | | |
11432 | 9.77k | #define VVC_PPS_BROKEN {\ |
11433 | 9.77k | memset(pps, 0, sizeof(VVC_PPS)); \ |
11434 | 9.77k | return -1;\ |
11435 | 9.77k | } |
11436 | | |
11437 | | |
11438 | | static s32 gf_vvc_read_pps_bs_internal(GF_BitStream *bs, VVCState *vvc) |
11439 | 50.2k | { |
11440 | 50.2k | u32 i; |
11441 | 50.2k | s32 pps_id; |
11442 | 50.2k | VVC_PPS *pps; |
11443 | | |
11444 | | //NAL header already read |
11445 | 50.2k | pps_id = gf_bs_read_int_log(bs, 6, "pps_id"); |
11446 | | |
11447 | 50.2k | if ((pps_id < 0) || (pps_id >= 64)) { |
11448 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] wrong PPS ID %d in PPS\n", pps_id)); |
11449 | 0 | return -1; |
11450 | 0 | } |
11451 | 50.2k | pps = &vvc->pps[pps_id]; |
11452 | | |
11453 | 50.2k | if (!pps->state) { |
11454 | 11.2k | pps->id = pps_id; |
11455 | 11.2k | pps->state = 1; |
11456 | 11.2k | } |
11457 | 50.2k | pps->sps_id = gf_bs_read_int_log(bs, 4, "sps_id"); |
11458 | 50.2k | if (((s32)pps->sps_id<0) || (pps->sps_id >= 16)) { |
11459 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] wrong SPS ID %d in PPS\n", pps->sps_id)); |
11460 | 0 | VVC_PPS_BROKEN |
11461 | 0 | } |
11462 | 50.2k | vvc->sps_active_idx = pps->sps_id; /*set active sps*/ |
11463 | 50.2k | pps->mixed_nal_types = gf_bs_read_int_log(bs, 1, "mixed_nal_types"); |
11464 | 50.2k | pps->width = gf_bs_read_ue_log(bs, "width"); |
11465 | 50.2k | pps->height = gf_bs_read_ue_log(bs, "height"); |
11466 | 50.2k | pps->conf_window = gf_bs_read_int_log(bs, 1, "conformance_window_flag"); |
11467 | 50.2k | if (pps->conf_window) { |
11468 | 22.4k | pps->cw_left = gf_bs_read_ue_log(bs, "conf_win_left_offset"); |
11469 | 22.4k | pps->cw_right = gf_bs_read_ue_log(bs, "conf_win_right_offset"); |
11470 | 22.4k | pps->cw_top = gf_bs_read_ue_log(bs, "conf_win_top_offset"); |
11471 | 22.4k | pps->cw_bottom = gf_bs_read_ue_log(bs, "conf_win_bottom_offset"); |
11472 | 22.4k | } |
11473 | | //scaling window |
11474 | 50.2k | if (gf_bs_read_int_log(bs, 1, "scaling_window_explicit_signaling_flag")) { |
11475 | 26.2k | gf_bs_read_se_log(bs, "scaling_win_left_offset"); |
11476 | 26.2k | gf_bs_read_se_log(bs, "scaling_win_right_offset"); |
11477 | 26.2k | gf_bs_read_se_log(bs, "scaling_win_top_offset"); |
11478 | 26.2k | gf_bs_read_se_log(bs, "scaling_win_bottom_offset"); |
11479 | 26.2k | } |
11480 | 50.2k | pps->output_flag_present_flag = gf_bs_read_int_log(bs, 1, "output_flag_present_flag"); |
11481 | 50.2k | pps->no_pic_partition_flag = gf_bs_read_int_log(bs, 1, "no_pic_partition_flag"); |
11482 | 50.2k | pps->subpic_id_mapping_present_flag = gf_bs_read_int_log(bs, 1, "subpic_id_mapping_present_flag"); |
11483 | | |
11484 | 50.2k | VVC_SPS *sps = &vvc->sps[pps->sps_id]; |
11485 | 50.2k | memcpy(pps->subpics, sps->subpics, sizeof(VVC_SubpicInfo)*sps->nb_subpics); |
11486 | | |
11487 | 50.2k | u32 pps_num_subpics = 1; |
11488 | 50.2k | if (pps->subpic_id_mapping_present_flag) { |
11489 | 32.6k | u32 pps_subpic_id_len; |
11490 | 32.6k | if (!pps->no_pic_partition_flag) { |
11491 | 13.9k | pps_num_subpics = 1+gf_bs_read_ue_log(bs, "pps_num_subpics_minus1"); |
11492 | 13.9k | if (pps_num_subpics != sps->nb_subpics) { |
11493 | 5.97k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] wrong number of subpictures %u in PPS vs %u in SPS\n", pps_num_subpics, sps->nb_subpics)); |
11494 | 5.97k | VVC_PPS_BROKEN |
11495 | 5.97k | } |
11496 | 13.9k | } |
11497 | 26.6k | pps_subpic_id_len = 1 + gf_bs_read_ue(bs); |
11498 | 53.3k | for (i=0; i<pps_num_subpics; i++) { |
11499 | 26.6k | VVC_SubpicInfo *sp = &pps->subpics[i]; |
11500 | 26.6k | sp->id = gf_bs_read_int_log_idx(bs, pps_subpic_id_len, "subpic_id", i); |
11501 | 26.6k | } |
11502 | 26.6k | } |
11503 | 44.2k | pps->single_slice_per_subpic_flag = 1; |
11504 | 44.2k | pps->num_slices_in_pic = 1; |
11505 | | |
11506 | 44.2k | pps->num_tiles_in_pic = 0; |
11507 | 44.2k | if (!pps->no_pic_partition_flag) { |
11508 | 22.4k | u32 ctu_size = 5 + gf_bs_read_int_log(bs, 2, "pps_log2_ctu_size_minus5"); |
11509 | 22.4k | u32 num_exp_tile_columns = 1 + gf_bs_read_ue_log(bs, "num_exp_tile_columns_minus1"); |
11510 | 22.4k | u32 num_exp_tile_rows = 1 + gf_bs_read_ue_log(bs, "num_exp_tile_rows_minus1"); |
11511 | | |
11512 | 22.4k | if (num_exp_tile_columns > VVC_MAX_TILE_COLS) { |
11513 | 408 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] wrong num tile columns %d in PPS\n", num_exp_tile_columns)); |
11514 | 408 | VVC_PPS_BROKEN |
11515 | 408 | } |
11516 | 22.0k | if (num_exp_tile_rows > VVC_MAX_TILE_ROWS) { |
11517 | 57 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] wrong num tile rows %d in PPS\n", num_exp_tile_rows)); |
11518 | 57 | VVC_PPS_BROKEN |
11519 | 57 | } |
11520 | | |
11521 | 22.0k | ctu_size = 1<<ctu_size; |
11522 | 22.0k | pps->pic_width_in_ctbsY = pps->width / ctu_size; |
11523 | 22.0k | if (pps->pic_width_in_ctbsY * ctu_size < pps->width) pps->pic_width_in_ctbsY++; |
11524 | 22.0k | pps->pic_height_in_ctbsY = pps->height / ctu_size; |
11525 | 22.0k | if (pps->pic_height_in_ctbsY * ctu_size < pps->height) pps->pic_height_in_ctbsY++; |
11526 | | |
11527 | 22.0k | u32 nb_ctb_left = pps->pic_width_in_ctbsY; |
11528 | 22.0k | pps->num_tile_cols=0; |
11529 | 22.0k | u32 nb_ctb_last=0; |
11530 | 58.9k | for (i=0; i<num_exp_tile_columns; i++) { |
11531 | 38.2k | u32 nb_ctb_width = 1 + gf_bs_read_ue_log_idx(bs, "tile_column_width_minus1", i); |
11532 | 38.2k | if (nb_ctb_left < nb_ctb_width) { |
11533 | 1.33k | VVC_PPS_BROKEN |
11534 | 1.33k | } |
11535 | 36.9k | nb_ctb_left -= nb_ctb_width; |
11536 | 36.9k | pps->tile_cols_width_ctb[i] = nb_ctb_width; |
11537 | 36.9k | nb_ctb_last = nb_ctb_width; |
11538 | 36.9k | pps->num_tile_cols++; |
11539 | 36.9k | if (pps->num_tile_cols > VVC_MAX_TILE_COLS) { |
11540 | 0 | VVC_PPS_BROKEN |
11541 | 0 | } |
11542 | 36.9k | } |
11543 | 20.6k | u32 uni_size_ctb = nb_ctb_last; |
11544 | 216k | while (nb_ctb_left >= uni_size_ctb) { |
11545 | 196k | nb_ctb_left -= uni_size_ctb; |
11546 | 196k | if (pps->num_tile_cols >= VVC_MAX_TILE_COLS) { |
11547 | 609 | VVC_PPS_BROKEN |
11548 | 609 | } |
11549 | 196k | pps->tile_cols_width_ctb[pps->num_tile_cols] = uni_size_ctb; |
11550 | 196k | pps->num_tile_cols++; |
11551 | 196k | } |
11552 | 20.0k | if (nb_ctb_left>0) { |
11553 | 7.38k | if (pps->num_tile_cols >= VVC_MAX_TILE_COLS) { |
11554 | 0 | VVC_PPS_BROKEN |
11555 | 0 | } |
11556 | 7.38k | pps->tile_cols_width_ctb[pps->num_tile_cols] = nb_ctb_left; |
11557 | 7.38k | pps->num_tile_cols++; |
11558 | 7.38k | } |
11559 | | |
11560 | 20.0k | nb_ctb_left = pps->pic_height_in_ctbsY; |
11561 | 20.0k | nb_ctb_last=0; |
11562 | 20.0k | pps->num_tile_rows=0; |
11563 | 50.1k | for (i=0; i<num_exp_tile_rows; i++) { |
11564 | 30.1k | u32 nb_ctb_height = 1 + gf_bs_read_ue_log_idx(bs, "tile_row_height_minus1", i); |
11565 | 30.1k | if (nb_ctb_left < nb_ctb_height) { |
11566 | 63 | VVC_PPS_BROKEN |
11567 | 63 | } |
11568 | 30.0k | nb_ctb_left -= nb_ctb_height; |
11569 | 30.0k | pps->tile_rows_height_ctb[i] = nb_ctb_height; |
11570 | 30.0k | pps->num_tile_rows++; |
11571 | 30.0k | nb_ctb_last = nb_ctb_height; |
11572 | 30.0k | if (pps->num_tile_rows > VVC_MAX_TILE_ROWS) { |
11573 | 0 | VVC_PPS_BROKEN |
11574 | 0 | } |
11575 | 30.0k | } |
11576 | 20.0k | uni_size_ctb = nb_ctb_last; |
11577 | 45.6k | while (nb_ctb_left >= uni_size_ctb) { |
11578 | 25.7k | nb_ctb_left -= uni_size_ctb; |
11579 | 25.7k | if (pps->num_tile_rows >= VVC_MAX_TILE_ROWS) { |
11580 | 44 | VVC_PPS_BROKEN |
11581 | 44 | } |
11582 | 25.6k | pps->tile_rows_height_ctb[pps->num_tile_rows] = uni_size_ctb; |
11583 | 25.6k | pps->num_tile_rows++; |
11584 | 25.6k | } |
11585 | 19.9k | if (nb_ctb_left>0) { |
11586 | 474 | if (pps->num_tile_rows >= VVC_MAX_TILE_ROWS) { |
11587 | 0 | VVC_PPS_BROKEN |
11588 | 0 | } |
11589 | 474 | pps->tile_rows_height_ctb[pps->num_tile_rows] = nb_ctb_left; |
11590 | 474 | pps->num_tile_rows++; |
11591 | 474 | } |
11592 | | |
11593 | 19.9k | pps->num_tiles_in_pic = pps->num_tile_cols * pps->num_tile_rows; |
11594 | 19.9k | pps->slice_address_len = vvc_ceillog2(pps->num_tiles_in_pic); |
11595 | 19.9k | if (pps->num_tiles_in_pic > 1) { |
11596 | 19.2k | gf_bs_read_int_log(bs, 1, "pps_loop_filter_across_tiles_enabled_flag"); |
11597 | 19.2k | pps->rect_slice_flag = gf_bs_read_int_log(bs, 1, "pps_rect_slice_flag"); |
11598 | 19.2k | } |
11599 | | |
11600 | 19.9k | if (pps->rect_slice_flag) { |
11601 | 8.32k | pps->single_slice_per_subpic_flag = gf_bs_read_int_log(bs, 1, "pps_single_slice_per_subpic_flag"); |
11602 | 8.32k | pps->num_slices_in_pic = pps_num_subpics; |
11603 | 8.32k | } |
11604 | | |
11605 | 19.9k | if (pps->rect_slice_flag && !pps->single_slice_per_subpic_flag) { |
11606 | 4.61k | pps->num_slices_in_pic = 1 + gf_bs_read_ue_log(bs, "pps_num_slices_in_pic_minus1"); |
11607 | 4.61k | u8 tile_idx_delta_present_flag = 0; |
11608 | 4.61k | if (pps->num_slices_in_pic > 1) { |
11609 | 3.55k | tile_idx_delta_present_flag = gf_bs_read_int_log(bs, 1, "pps_tile_idx_delta_present_flag"); |
11610 | 3.55k | } |
11611 | 271k | for (i=0; i<pps->num_slices_in_pic-1; i++) { |
11612 | | //TODO FIXME we assume single slice per tile |
11613 | 267k | u32 SliceTopLeftTileIdx=0; |
11614 | 267k | u32 RowHeightVal=0; |
11615 | | |
11616 | | |
11617 | 267k | u32 slice_width_in_tiles_minus1=0; |
11618 | 267k | u32 slice_height_in_tiles_minus1=0; |
11619 | 267k | if (SliceTopLeftTileIdx % pps->num_tile_cols != pps->num_tile_cols - 1) { |
11620 | 1.67k | slice_width_in_tiles_minus1 = gf_bs_read_ue_log_idx(bs, "pps_slice_width_in_tiles_minus1", i); |
11621 | 1.67k | } |
11622 | | |
11623 | 267k | if ( (SliceTopLeftTileIdx / pps->num_tile_cols != pps->num_tile_rows - 1) |
11624 | 267k | && (tile_idx_delta_present_flag || (SliceTopLeftTileIdx % pps->num_tile_cols == 0) ) |
11625 | 267k | ) { |
11626 | 265k | slice_height_in_tiles_minus1 = gf_bs_read_ue_log_idx(bs, "pps_slice_height_in_tiles_minus1", i); |
11627 | 265k | } |
11628 | | |
11629 | 267k | if (!slice_width_in_tiles_minus1 && !slice_height_in_tiles_minus1 && (RowHeightVal > 1 ) |
11630 | 267k | ) { |
11631 | 0 | u32 j, num_exp_slices_in_tile = gf_bs_read_ue_log_idx(bs, "pps_num_exp_slices_in_tile", i); |
11632 | 0 | for (j=0; j<num_exp_slices_in_tile; j++) { |
11633 | 0 | gf_bs_read_ue_log_idx2 (bs, "pps_exp_slice_height_in_ctus_minus1", i, j); |
11634 | 0 | } |
11635 | | //TODO FIXME i += NumSlicesInTile[ i ] − 1 |
11636 | 0 | } |
11637 | | |
11638 | 267k | if (tile_idx_delta_present_flag && (i < pps->num_slices_in_pic)) { |
11639 | 265k | gf_bs_read_se_log_idx(bs, "pps_tile_idx_delta_val", i); |
11640 | 265k | } |
11641 | | |
11642 | 267k | } |
11643 | 4.61k | } |
11644 | 19.9k | if (!pps->rect_slice_flag || pps->single_slice_per_subpic_flag || (pps->num_slices_in_pic > 1)) { |
11645 | 18.8k | gf_bs_read_int_log(bs, 1, "pps_loop_filter_across_slices_enabled_flag"); |
11646 | 18.8k | } |
11647 | 19.9k | } |
11648 | | |
11649 | | //update subpic info - TODO, for now we assume single slice per subpic |
11650 | 41.7k | if (sps->nb_subpics>1) { |
11651 | 2.39k | for (i=0; i<pps_num_subpics; i++) { |
11652 | 1.19k | VVC_SubpicInfo *sp = &pps->subpics[i]; |
11653 | 1.19k | sp->num_slices=1; |
11654 | 1.19k | } |
11655 | 1.19k | } |
11656 | | |
11657 | 41.7k | pps->cabac_init_present_flag = gf_bs_read_int_log(bs, 1, "pps_cabac_init_present_flag"); |
11658 | 125k | for (i=0; i<2; i++) { |
11659 | 83.4k | pps->num_ref_idx_default_active[i] = 1 + gf_bs_read_ue_log_idx(bs, "pps_num_ref_idx_default_active_minus1", i); |
11660 | 83.4k | } |
11661 | 41.7k | pps->rpl1_idx_present_flag = gf_bs_read_int_log(bs, 1, "pps_rpl1_idx_present_flag"); |
11662 | 41.7k | pps->weighted_pred_flag = gf_bs_read_int_log(bs, 1, "pps_weighted_pred_flag"); |
11663 | 41.7k | pps->weighted_bipred_flag = gf_bs_read_int_log(bs, 1, "pps_weighted_bipred_flag"); |
11664 | 41.7k | if (gf_bs_read_int_log(bs, 1, "pps_ref_wraparound_enabled_flag")) { |
11665 | 16.2k | gf_bs_read_ue_log(bs, "pps_pic_width_minus_wraparound_offset"); |
11666 | 16.2k | } |
11667 | 41.7k | gf_bs_read_se_log(bs, "pps_init_qp_minus26"); |
11668 | 41.7k | pps->cu_qp_delta_enabled_flag = gf_bs_read_int_log(bs, 1, "pps_cu_qp_delta_enabled_flag"); |
11669 | 41.7k | pps->slice_chroma_qp_offsets_present_flag = 0; |
11670 | 41.7k | pps->chroma_tool_offsets_present_flag = gf_bs_read_int_log(bs, 1, "pps_chroma_tool_offsets_present_flag"); |
11671 | 41.7k | if (pps->chroma_tool_offsets_present_flag) { |
11672 | 29.0k | gf_bs_read_se_log(bs, "pps_cb_qp_offset"); |
11673 | 29.0k | gf_bs_read_se_log(bs, "pps_cr_qp_offset"); |
11674 | 29.0k | u8 joint_cbcr_qp_offset_present_flag = gf_bs_read_int_log(bs, 1, "pps_joint_cbcr_qp_offset_present_flag"); |
11675 | 29.0k | if (joint_cbcr_qp_offset_present_flag) { |
11676 | 13.4k | gf_bs_read_se_log(bs, "pps_joint_cbcr_qp_offset_value"); |
11677 | 13.4k | } |
11678 | 29.0k | pps->slice_chroma_qp_offsets_present_flag = gf_bs_read_int_log(bs, 1, "pps_slice_chroma_qp_offsets_present_flag"); |
11679 | 29.0k | pps->cu_chroma_qp_offset_list_enabled_flag = gf_bs_read_int_log(bs, 1, "pps_cu_chroma_qp_offset_list_enabled_flag"); |
11680 | 29.0k | if (pps->cu_chroma_qp_offset_list_enabled_flag) { |
11681 | 15.4k | u32 pps_chroma_qp_offset_list_len = 1 + gf_bs_read_ue_log(bs, "pps_chroma_qp_offset_list_len_minus1"); |
11682 | 79.6k | for (i=0; i<pps_chroma_qp_offset_list_len; i++) { |
11683 | 64.1k | gf_bs_read_se_log_idx(bs, "pps_cb_qp_offset_list", i); |
11684 | 64.1k | gf_bs_read_se_log_idx(bs, "pps_cr_qp_offset_list", i); |
11685 | 64.1k | if (joint_cbcr_qp_offset_present_flag) { |
11686 | 33.8k | gf_bs_read_se_log_idx(bs, "pps_joint_cbcr_qp_offset_list", i); |
11687 | 33.8k | } |
11688 | 64.1k | } |
11689 | 15.4k | } |
11690 | 29.0k | } |
11691 | | |
11692 | 41.7k | pps->dbf_info_in_ph_flag = 0; |
11693 | 41.7k | pps->deblocking_filter_disabled_flag = 0; |
11694 | 41.7k | pps->deblocking_filter_override_enabled_flag = 0; |
11695 | 41.7k | if (gf_bs_read_int_log(bs, 1, "pps_deblocking_filter_control_present_flag")) { |
11696 | 26.4k | pps->deblocking_filter_override_enabled_flag = gf_bs_read_int_log(bs, 1, "pps_deblocking_filter_override_enabled_flag"); |
11697 | 26.4k | pps->deblocking_filter_disabled_flag = gf_bs_read_int_log(bs, 1, "pps_deblocking_filter_disabled_flag"); |
11698 | | |
11699 | 26.4k | if (!pps->no_pic_partition_flag && pps->deblocking_filter_override_enabled_flag) { |
11700 | 6.98k | pps->dbf_info_in_ph_flag = gf_bs_read_int_log(bs, 1, "pps_dbf_info_in_ph_flag"); |
11701 | 6.98k | } |
11702 | 26.4k | if (!pps->deblocking_filter_disabled_flag) { |
11703 | 18.6k | gf_bs_read_se_log(bs, "pps_luma_beta_offset_div2"); |
11704 | 18.6k | gf_bs_read_se_log(bs, "pps_luma_tc_offset_div2"); |
11705 | 18.6k | if (pps->chroma_tool_offsets_present_flag) { |
11706 | 11.6k | gf_bs_read_se_log(bs, "pps_cb_beta_offset_div2"); |
11707 | 11.6k | gf_bs_read_se_log(bs, "pps_cb_tc_offset_div2"); |
11708 | 11.6k | gf_bs_read_se_log(bs, "pps_cr_beta_offset_div2"); |
11709 | 11.6k | gf_bs_read_se_log(bs, "pps_cr_tc_offset_div2"); |
11710 | 11.6k | } |
11711 | 18.6k | } |
11712 | 26.4k | } |
11713 | 41.7k | pps->wp_info_in_ph_flag = 0; |
11714 | 41.7k | pps->qp_delta_info_in_ph_flag = 0; |
11715 | 41.7k | pps->sao_info_in_ph_flag = 0; |
11716 | 41.7k | if (!pps->no_pic_partition_flag) { |
11717 | 19.9k | pps->rpl_info_in_ph_flag = gf_bs_read_int_log(bs, 1, "pps_rpl_info_in_ph_flag"); |
11718 | 19.9k | pps->sao_info_in_ph_flag = gf_bs_read_int_log(bs, 1, "pps_sao_info_in_ph_flag"); |
11719 | 19.9k | pps->alf_info_in_ph_flag = gf_bs_read_int_log(bs, 1, "pps_alf_info_in_ph_flag"); |
11720 | 19.9k | if ( (pps->weighted_pred_flag || pps->weighted_bipred_flag) && pps->rpl_info_in_ph_flag) { |
11721 | 8.12k | pps->wp_info_in_ph_flag = gf_bs_read_int_log(bs, 1, "pps_wp_info_in_ph_flag"); |
11722 | 8.12k | } |
11723 | 19.9k | pps->qp_delta_info_in_ph_flag = gf_bs_read_int_log(bs, 1, "pps_qp_delta_info_in_ph_flag"); |
11724 | 19.9k | } |
11725 | 41.7k | pps->picture_header_extension_present_flag = gf_bs_read_int_log(bs, 1, "pps_picture_header_extension_present_flag"); |
11726 | 41.7k | pps->slice_header_extension_present_flag = gf_bs_read_int_log(bs, 1, "pps_slice_header_extension_present_flag"); |
11727 | | |
11728 | | //TODO |
11729 | 41.7k | if (gf_bs_read_int_log(bs, 1, "pps_extension_flag")) { |
11730 | | //while ( more_rbsp_data()) bit(1); |
11731 | 12.8k | } |
11732 | | //rbsp_trailing_bits() |
11733 | | |
11734 | 41.7k | if (gf_bs_is_overflow(bs)) |
11735 | 1.27k | VVC_PPS_BROKEN |
11736 | 40.4k | return pps_id; |
11737 | 41.7k | } |
11738 | | |
11739 | | |
11740 | | static s32 vvc_parse_ref_pic_lists(GF_BitStream *bs, VVCSliceInfo *si, Bool is_pic_header) |
11741 | 67.4k | { |
11742 | 67.4k | u32 i; |
11743 | 67.4k | s32 *p_rpl_idx = is_pic_header ? &si->ph_rpl_idx[0] : &si->rpl_idx[0]; |
11744 | | |
11745 | 67.4k | u8 rpl_sps_flag_prev=0; |
11746 | 190k | for (i=0; i<2; i++) { |
11747 | 129k | VVC_RefPicList *rpl=NULL; |
11748 | 129k | u32 j; |
11749 | 129k | u8 rpl_sps_flag=0; |
11750 | 129k | u32 rpl_idx = 0; |
11751 | 129k | if ((si->sps->num_ref_pic_lists[i]>0) && (!i || si->pps->rpl1_idx_present_flag)) { |
11752 | 18.4k | rpl_sps_flag = gf_bs_read_int_log_idx(bs, 1, "rpl_sps_flag", i); |
11753 | 18.4k | } |
11754 | | /* |
11755 | | When rpl_sps_flag[ i ] is not present, it is inferred as follows: |
11756 | | ⎯ If sps_num_ref_pic_lists[ i ] is equal to 0, the value of rpl_sps_flag[ i ] is inferred to be equal to 0. |
11757 | | ⎯ Otherwise (sps_num_ref_pic_lists[ i ] is greater than 0), when pps_rpl1_idx_present_flag is equal to 0 and i is equal to 1, the value of rpl_sps_flag[ 1 ] is inferred to be equal to rpl_sps_flag[ 0 ]. |
11758 | | */ |
11759 | 111k | else { |
11760 | 111k | if (si->sps->num_ref_pic_lists[i]==0) { |
11761 | 107k | rpl_sps_flag = 0; |
11762 | 107k | } else { |
11763 | 3.23k | rpl_sps_flag = rpl_sps_flag_prev; |
11764 | 3.23k | } |
11765 | 111k | } |
11766 | 129k | rpl_sps_flag_prev = rpl_sps_flag; |
11767 | 129k | if (is_pic_header) { |
11768 | 83.6k | rpl = &si->ph_rpl[i]; |
11769 | 83.6k | } else { |
11770 | 45.9k | rpl = &si->rpl[i]; |
11771 | 45.9k | } |
11772 | | |
11773 | 129k | if (rpl_sps_flag) { |
11774 | 12.0k | if ((si->sps->num_ref_pic_lists[i]>1) && (!i || si->pps->rpl1_idx_present_flag)) { |
11775 | 4.24k | u32 nb_bits = vvc_ceillog2(si->sps->num_ref_pic_lists[i]); |
11776 | 4.24k | rpl_idx = gf_bs_read_int_log_idx(bs, nb_bits, "rpl_idx", i); |
11777 | 4.24k | } |
11778 | 7.82k | else if (si->sps->num_ref_pic_lists[i] == 1) { |
11779 | 6.52k | rpl_idx = 0; |
11780 | 6.52k | } else { |
11781 | 1.30k | gf_assert(p_rpl_idx[0] != -1); |
11782 | 1.30k | rpl_idx = p_rpl_idx[0]; |
11783 | 1.30k | } |
11784 | 12.0k | p_rpl_idx[i] = rpl_idx; |
11785 | 12.0k | if (rpl_idx>=VVC_MAX_REF_PICS) { |
11786 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[VVC] Picture header RplIdx %d greater than max allowed %d\n", rpl_idx, VVC_MAX_REF_PICS)); |
11787 | 0 | return -1; |
11788 | 0 | } |
11789 | | |
11790 | 12.0k | memcpy(rpl, &si->sps->rps[i][rpl_idx], sizeof(VVC_RefPicList)); |
11791 | | |
11792 | 117k | } else { |
11793 | 117k | s32 res = vvc_parse_ref_pic_list_struct(bs, si->sps, i, si->sps->num_ref_pic_lists[i], rpl); |
11794 | 117k | if (res<0) return res; |
11795 | 110k | p_rpl_idx[i] = -1; |
11796 | 110k | } |
11797 | | |
11798 | 122k | if (rpl->nb_long_term_pictures) { |
11799 | 20.9k | for (j=0; j<rpl->num_ref_entries; j++) { |
11800 | 15.7k | if (rpl->ref_pic_type[j] != VVC_RPL_LT) continue; |
11801 | | |
11802 | 10.3k | if (rpl->ltrp_in_header_flag) { |
11803 | 0 | gf_bs_read_int_log_idx2(bs, si->sps->log2_max_poc_lsb, "poc_lsb_lt", i, j); |
11804 | 0 | } |
11805 | 10.3k | if (gf_bs_read_int_log_idx2(bs, 1, "delta_poc_msb_cycle_present_flag", i, j)) { |
11806 | 9.64k | gf_bs_read_ue_log_idx2(bs, "delta_poc_msb_cycle_lt", i, j); |
11807 | 9.64k | } |
11808 | 10.3k | } |
11809 | 5.19k | } |
11810 | 122k | } |
11811 | 60.3k | return 0; |
11812 | 67.4k | } |
11813 | | |
11814 | | static s32 vvc_pred_weight_table(GF_BitStream *bs, VVCState *vvc, VVCSliceInfo *si, VVC_PPS *pps, VVC_SPS *sps, u32 *num_ref_idx_active) |
11815 | 30.4k | { |
11816 | 30.4k | u32 i, num_weights; |
11817 | 30.4k | u8 weights[VVC_MAX_REF_PICS]; |
11818 | 30.4k | gf_bs_read_ue_log(bs, "luma_log2_weight_denom"); |
11819 | 30.4k | if (sps->chroma_format_idc) { |
11820 | 8.47k | gf_bs_read_se_log(bs, "delta_chroma_log2_weight_denom"); |
11821 | 8.47k | } |
11822 | 30.4k | if (pps->wp_info_in_ph_flag) { |
11823 | 21.4k | num_weights = gf_bs_read_ue_log(bs, "num_l0_weights"); |
11824 | 21.4k | } else { |
11825 | 8.93k | num_weights = num_ref_idx_active[0]; |
11826 | 8.93k | } |
11827 | 30.4k | if (num_weights>VVC_MAX_REF_PICS) { |
11828 | 801 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] num weights L0 %d greater than max allowed %d\n", num_weights, VVC_MAX_REF_PICS)); |
11829 | 801 | return -1; |
11830 | 801 | } |
11831 | | |
11832 | 29.6k | memset(weights, 0, sizeof(u8)*VVC_MAX_REF_PICS); |
11833 | 98.6k | for (i=0; i<num_weights; i++) { |
11834 | 69.0k | if (gf_bs_read_int_log_idx(bs, 1, "luma_weight_l0_flag", i)) |
11835 | 25.3k | weights[i] |= 1; |
11836 | 69.0k | } |
11837 | 29.6k | if (sps->chroma_format_idc) { |
11838 | 50.6k | for (i=0; i<num_weights; i++) { |
11839 | 42.2k | if (gf_bs_read_int_log_idx(bs, 1, "chroma_weight_l0_flag", i)) |
11840 | 24.9k | weights[i] |= 2; |
11841 | 42.2k | } |
11842 | 8.35k | } |
11843 | 98.6k | for (i=0; i<num_weights; i++) { |
11844 | 69.0k | if (weights[i] & 1) { |
11845 | 25.3k | gf_bs_read_se_log_idx(bs, "delta_luma_weight_l0", i); |
11846 | 25.3k | gf_bs_read_se_log_idx(bs, "luma_offset_l0", i); |
11847 | 25.3k | } |
11848 | 69.0k | if (weights[i] & 2) { |
11849 | 24.9k | gf_bs_read_se_log_idx2(bs, "delta_chroma_weight_l0", i, 0); |
11850 | 24.9k | gf_bs_read_se_log_idx2(bs, "delta_chroma_offset_l0", i, 0); |
11851 | 24.9k | gf_bs_read_se_log_idx2(bs, "delta_chroma_weight_l0", i, 1); |
11852 | 24.9k | gf_bs_read_se_log_idx2(bs, "delta_chroma_offset_l0", i, 1); |
11853 | 24.9k | } |
11854 | 69.0k | } |
11855 | 29.6k | num_weights = 0; |
11856 | 29.6k | if (pps->weighted_bipred_flag && pps->wp_info_in_ph_flag && (si->ph_rpl[1].num_ref_entries > 0)) { |
11857 | 4.71k | num_weights = gf_bs_read_ue_log(bs, "num_l1_weights"); |
11858 | 4.71k | } |
11859 | 29.6k | if (!num_weights) return 0; |
11860 | | |
11861 | 1.80k | if (num_weights>VVC_MAX_REF_PICS) { |
11862 | 1.08k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] num weights L1 %d greater than max allowed %d\n", num_weights, VVC_MAX_REF_PICS)); |
11863 | 1.08k | return -1; |
11864 | 1.08k | } |
11865 | | |
11866 | 716 | memset(weights, 0, sizeof(u8)*VVC_MAX_REF_PICS); |
11867 | 5.27k | for (i=0; i<num_weights; i++) { |
11868 | 4.55k | if (gf_bs_read_int_log_idx(bs, 1, "luma_weight_l1_flag", i)) |
11869 | 2.04k | weights[i] |= 1; |
11870 | 4.55k | } |
11871 | 716 | if (sps->chroma_format_idc) { |
11872 | 2.56k | for (i=0; i<num_weights; i++) { |
11873 | 2.33k | if (gf_bs_read_int_log_idx(bs, 1, "chroma_weight_l1_flag", i)) |
11874 | 1.45k | weights[i] |= 2; |
11875 | 2.33k | } |
11876 | 239 | } |
11877 | 5.27k | for (i=0; i<num_weights; i++) { |
11878 | 4.55k | if (weights[i] & 1) { |
11879 | 2.04k | gf_bs_read_se_log_idx(bs, "delta_luma_weight_l1", i); |
11880 | 2.04k | gf_bs_read_se_log_idx(bs, "luma_offset_l1", i); |
11881 | 2.04k | } |
11882 | 4.55k | if (weights[i] & 2) { |
11883 | 1.45k | gf_bs_read_se_log_idx2(bs, "delta_chroma_weight_l1", i, 0); |
11884 | 1.45k | gf_bs_read_se_log_idx2(bs, "delta_chroma_offset_l1", i, 0); |
11885 | 1.45k | gf_bs_read_se_log_idx2(bs, "delta_chroma_weight_l1", i, 1); |
11886 | 1.45k | gf_bs_read_se_log_idx2(bs, "delta_chroma_offset_l1", i, 1); |
11887 | 1.45k | } |
11888 | 4.55k | } |
11889 | 716 | return 0; |
11890 | 1.80k | } |
11891 | | |
11892 | | static s32 vvc_parse_picture_header(GF_BitStream *bs, VVCState *vvc, VVCSliceInfo *si) |
11893 | 112k | { |
11894 | 112k | s32 pps_id; |
11895 | | |
11896 | 112k | si->nb_lt_or_il_pics = si->nb_reference_pocs = 0; |
11897 | | |
11898 | 112k | si->irap_or_gdr_pic = gf_bs_read_int_log(bs, 1, "irap_or_gdr_pic"); |
11899 | 112k | si->non_ref_pic = gf_bs_read_int_log(bs, 1, "non_ref_pic"); |
11900 | 112k | if (si->irap_or_gdr_pic) |
11901 | 44.4k | si->gdr_pic = gf_bs_read_int_log(bs, 1, "gdr_pic"); |
11902 | | |
11903 | 112k | si->intra_slice_allowed_flag = 1; |
11904 | 112k | if ((si->inter_slice_allowed_flag = gf_bs_read_int_log(bs, 1, "inter_slice_allowed_flag"))) |
11905 | 87.9k | si->intra_slice_allowed_flag = gf_bs_read_int_log(bs, 1, "intra_slice_allowed_flag"); |
11906 | | |
11907 | 112k | pps_id = gf_bs_read_ue_log(bs, "pps_id"); |
11908 | 112k | if ((pps_id<0) || (pps_id >= 64)) |
11909 | 665 | return -1; |
11910 | 111k | si->pps = &vvc->pps[pps_id]; |
11911 | 111k | si->sps = &vvc->sps[si->pps->sps_id]; |
11912 | 111k | si->poc_lsb = gf_bs_read_int_log(bs, si->sps->log2_max_poc_lsb, "poc_lsb"); |
11913 | | |
11914 | 111k | si->recovery_point_valid = 0; |
11915 | 111k | si->gdr_recovery_count = 0; |
11916 | 111k | if (si->gdr_pic) { |
11917 | 79.8k | si->recovery_point_valid = 1; |
11918 | 79.8k | si->gdr_recovery_count = gf_bs_read_ue_log(bs, "gdr_recovery_count"); |
11919 | 79.8k | } |
11920 | 111k | gf_bs_read_int_log(bs, si->sps->ph_num_extra_bits, "ph_extra_bits"); |
11921 | | |
11922 | 111k | if (si->sps->poc_msb_cycle_flag) { |
11923 | 33.1k | if ( (si->poc_msb_cycle_present_flag = gf_bs_read_int_log(bs, 1, "poc_msb_cycle_present_flag"))) { |
11924 | 20.3k | si->poc_msb_cycle = gf_bs_read_int_log(bs, si->sps->poc_msb_cycle_len, "poc_msb_cycle"); |
11925 | 20.3k | } |
11926 | 33.1k | } |
11927 | | |
11928 | 111k | if (si->sps->alf_enabled_flag && si->pps->alf_info_in_ph_flag ) { |
11929 | 10.4k | if (gf_bs_read_int_log(bs, 1, "ph_alf_enabled_flag")) { |
11930 | 6.70k | u32 i, nb_aps_id = gf_bs_read_int_log(bs, 3, "ph_num_alf_aps_ids_luma"); |
11931 | 42.6k | for (i=0; i<nb_aps_id; i++) { |
11932 | 35.9k | gf_bs_read_int_log_idx(bs, 3, "ph_alf_aps_id_luma", i); |
11933 | 35.9k | } |
11934 | 6.70k | u8 alf_cb_enabled_flag = 0, alf_cr_enabled_flag=0; |
11935 | 6.70k | if (si->sps->chroma_format_idc) { |
11936 | 5.27k | alf_cb_enabled_flag = gf_bs_read_int_log(bs, 1, "ph_alf_cb_enabled_flag"); |
11937 | 5.27k | alf_cr_enabled_flag = gf_bs_read_int_log(bs, 1, "ph_alf_cr_enabled_flag"); |
11938 | 5.27k | } |
11939 | 6.70k | if (alf_cb_enabled_flag || alf_cr_enabled_flag) { |
11940 | 3.31k | gf_bs_read_int_log(bs, 3, "ph_alf_aps_id_chroma"); |
11941 | 3.31k | } |
11942 | 6.70k | if (si->sps->ccalf_enabled_flag ) { |
11943 | 3.68k | if (gf_bs_read_int_log(bs, 1, "ph_alf_cc_cb_enabled_flag")) { |
11944 | 690 | gf_bs_read_int_log(bs, 3, "ph_alf_cc_cb_aps_id"); |
11945 | 690 | } |
11946 | 3.68k | if (gf_bs_read_int_log(bs, 1, "ph_alf_cc_cr_enabled_flag")) { |
11947 | 2.69k | gf_bs_read_int_log(bs, 3, "ph_alf_cc_cr_aps_id"); |
11948 | 2.69k | } |
11949 | 3.68k | } |
11950 | 6.70k | } |
11951 | 10.4k | } |
11952 | 111k | si->lmcs_enabled_flag = 0; |
11953 | 111k | if (si->sps->lmcs_enabled_flag) { |
11954 | 32.0k | si->lmcs_enabled_flag = gf_bs_read_int_log(bs, 1, "ph_lmcs_enabled_flag"); |
11955 | 32.0k | if (si->lmcs_enabled_flag) { |
11956 | 19.8k | gf_bs_read_int_log(bs, 2, "ph_lmcs_aps_id"); |
11957 | 19.8k | if (si->sps->chroma_format_idc) { |
11958 | 2.12k | gf_bs_read_int_log(bs, 1, "ph_chroma_residual_scale_flag"); |
11959 | 2.12k | } |
11960 | 19.8k | } |
11961 | 32.0k | } |
11962 | 111k | si->explicit_scaling_list_enabled_flag = 0; |
11963 | 111k | if (si->sps->explicit_scaling_list_enabled_flag) { |
11964 | 56.4k | si->explicit_scaling_list_enabled_flag = gf_bs_read_int_log(bs, 1, "ph_explicit_scaling_list_enabled_flag"); |
11965 | 56.4k | if (si->explicit_scaling_list_enabled_flag) { |
11966 | 31.9k | gf_bs_read_int_log(bs, 3, "ph_scaling_list_aps_id"); |
11967 | 31.9k | } |
11968 | 56.4k | } |
11969 | 111k | if (si->sps->virtual_boundaries_enabled_flag && !si->sps->virtual_boundaries_present_flag) { |
11970 | 17.3k | if (gf_bs_read_int_log(bs, 1, "ph_virtual_boundaries_present_flag")) { |
11971 | 10.3k | u32 i, nb_virt_boundaries = gf_bs_read_ue_log(bs, "ph_num_ver_virtual_boundaries"); |
11972 | 42.5k | for (i=0; i<nb_virt_boundaries; i++) { |
11973 | 32.2k | gf_bs_read_ue_log_idx(bs, "ph_virtual_boundary_pos_x_minus1", i); |
11974 | 32.2k | } |
11975 | 10.3k | nb_virt_boundaries = gf_bs_read_ue_log(bs, "ph_num_hor_virtual_boundaries"); |
11976 | 37.9k | for (i=0; i<nb_virt_boundaries; i++) { |
11977 | 27.6k | gf_bs_read_ue_log_idx(bs, "ph_virtual_boundary_pos_x_minus1", i); |
11978 | 27.6k | } |
11979 | 10.3k | } |
11980 | 17.3k | } |
11981 | 111k | if (si->pps->output_flag_present_flag && !si->non_ref_pic) { |
11982 | 2.93k | gf_bs_read_int_log(bs, 1, "ph_pic_output_flag"); |
11983 | 2.93k | } |
11984 | 111k | if (si->pps->rpl_info_in_ph_flag) { |
11985 | 42.5k | s32 res = vvc_parse_ref_pic_lists(bs, si, GF_TRUE); |
11986 | 42.5k | if (res<0) return res; |
11987 | 42.5k | } |
11988 | 109k | u8 partition_constraints_override_flag = 0; |
11989 | 109k | if (si->sps->partition_constraints_override_enabled_flag) { |
11990 | 30.2k | partition_constraints_override_flag = gf_bs_read_int_log(bs, 1, "ph_partition_constraints_override_flag"); |
11991 | 30.2k | } |
11992 | 109k | if (si->intra_slice_allowed_flag) { |
11993 | 97.5k | if (partition_constraints_override_flag) { |
11994 | 12.3k | gf_bs_read_ue_log(bs, "ph_log2_diff_min_qt_min_cb_inter_slice"); |
11995 | 12.3k | u32 max_mtt_hierarchy_depth_inter_slice = gf_bs_read_ue_log(bs, "ph_max_mtt_hierarchy_depth_inter_slice"); |
11996 | 12.3k | if (max_mtt_hierarchy_depth_inter_slice) { |
11997 | 2.71k | gf_bs_read_ue_log(bs, "ph_log2_diff_max_bt_min_qt_inter_slice"); |
11998 | 2.71k | gf_bs_read_ue_log(bs, "ph_log2_diff_max_tt_min_qt_inter_slice"); |
11999 | 2.71k | } |
12000 | 12.3k | } |
12001 | 97.5k | if (si->pps->cu_qp_delta_enabled_flag) { |
12002 | 44.4k | gf_bs_read_ue_log(bs, "ph_cu_qp_delta_subdiv_inter_slice"); |
12003 | 44.4k | } |
12004 | 97.5k | if (si->pps->cu_chroma_qp_offset_list_enabled_flag) { |
12005 | 37.7k | gf_bs_read_ue_log(bs, "ph_cu_chroma_qp_offset_subdiv_intra_slice"); |
12006 | 37.7k | } |
12007 | 97.5k | } |
12008 | 109k | si->temporal_mvp_enabled_flag = 0; |
12009 | 109k | if (si->inter_slice_allowed_flag) { |
12010 | 85.2k | if (partition_constraints_override_flag) { |
12011 | 11.9k | gf_bs_read_ue_log(bs, "ph_log2_diff_min_qt_min_cb_inter_slice"); |
12012 | 11.9k | u32 max_mtt_hierarchy_depth_inter_slice = gf_bs_read_ue_log(bs, "ph_max_mtt_hierarchy_depth_inter_slice"); |
12013 | 11.9k | if (max_mtt_hierarchy_depth_inter_slice) { |
12014 | 4.72k | gf_bs_read_ue_log(bs, "ph_log2_diff_max_bt_min_qt_inter_slice"); |
12015 | 4.72k | gf_bs_read_ue_log(bs, "ph_log2_diff_max_tt_min_qt_inter_slice"); |
12016 | 4.72k | } |
12017 | 11.9k | } |
12018 | 85.2k | if (si->pps->cu_qp_delta_enabled_flag) { |
12019 | 41.2k | gf_bs_read_ue_log(bs, "ph_cu_qp_delta_subdiv_inter_slice"); |
12020 | 41.2k | } |
12021 | 85.2k | if (si->pps->cu_chroma_qp_offset_list_enabled_flag) { |
12022 | 36.5k | gf_bs_read_ue_log(bs, "ph_cu_chroma_qp_offset_subdiv_inter_slice"); |
12023 | 36.5k | } |
12024 | 85.2k | if (si->sps->temporal_mvp_enabled_flag) { |
12025 | 29.5k | si->temporal_mvp_enabled_flag = gf_bs_read_int_log(bs, 1, "ph_temporal_mvp_enabled_flag"); |
12026 | 29.5k | if (si->temporal_mvp_enabled_flag && si->pps->rpl_info_in_ph_flag) { |
12027 | 8.91k | u8 collocated_from_l0_flag = 1; |
12028 | 8.91k | if (si->ph_rpl[1].num_ref_entries>0) |
12029 | 1.46k | collocated_from_l0_flag = gf_bs_read_int_log(bs, 1, "ph_collocated_from_l0_flag"); |
12030 | | |
12031 | 8.91k | if ( (collocated_from_l0_flag && si->ph_rpl[0].num_ref_entries > 1) |
12032 | 8.91k | || (!collocated_from_l0_flag && si->ph_rpl[1].num_ref_entries > 1) |
12033 | 8.91k | ) { |
12034 | 5.53k | gf_bs_read_ue_log(bs, "ph_collocated_ref_idx"); |
12035 | 5.53k | } |
12036 | 8.91k | } |
12037 | 29.5k | } |
12038 | 85.2k | if (si->sps->mmvd_fullpel_only_enabled_flag) { |
12039 | 11.5k | gf_bs_read_int_log(bs, 1, "ph_mmvd_fullpel_only_flag"); |
12040 | 11.5k | } |
12041 | 85.2k | u8 presenceFlag = 0; |
12042 | 85.2k | if (!si->pps->rpl_info_in_ph_flag) { |
12043 | 45.9k | presenceFlag = 1; |
12044 | 45.9k | } |
12045 | 39.2k | else if (si->ph_rpl[1].num_ref_entries > 0) { |
12046 | 10.3k | presenceFlag = 1; |
12047 | 10.3k | } |
12048 | 85.2k | if (presenceFlag) { |
12049 | 56.3k | gf_bs_read_int_log(bs, 1, "ph_mvd_l1_zero_flag"); |
12050 | 56.3k | if (si->sps->bdof_control_present_in_ph_flag) |
12051 | 8.52k | gf_bs_read_int_log(bs, 1, "ph_bdof_disabled_flag"); |
12052 | 56.3k | if (si->sps->dmvr_control_present_in_ph_flag) |
12053 | 21.5k | gf_bs_read_int_log(bs, 1, "ph_dmvr_disabled_flag"); |
12054 | 56.3k | } |
12055 | 85.2k | if (si->sps->prof_control_present_in_ph_flag) |
12056 | 29.0k | gf_bs_read_int_log(bs, 1, "ph_prof_disabled_flag"); |
12057 | | |
12058 | 85.2k | if ( (si->pps->weighted_pred_flag || si->pps->weighted_bipred_flag) && si->pps->wp_info_in_ph_flag) { |
12059 | 21.4k | s32 res = vvc_pred_weight_table(bs, vvc, si, si->pps, si->sps, NULL); |
12060 | 21.4k | if (res<0) return res; |
12061 | 21.4k | } |
12062 | 85.2k | } |
12063 | 108k | if (si->pps->qp_delta_info_in_ph_flag) { |
12064 | 19.9k | gf_bs_read_se_log(bs, "ph_qp_delta"); |
12065 | 19.9k | } |
12066 | 108k | if (si->sps->joint_cbcr_enabled_flag) { |
12067 | 9.10k | gf_bs_read_int_log(bs, 1, "ph_joint_cbcr_sign_flag"); |
12068 | 9.10k | } |
12069 | 108k | if (si->sps->sao_enabled_flag && si->pps->sao_info_in_ph_flag) { |
12070 | 3.64k | gf_bs_read_int_log(bs, 1, "ph_sao_luma_enabled_flag"); |
12071 | 3.64k | if (si->sps->chroma_format_idc) |
12072 | 2.71k | gf_bs_read_int_log(bs, 1, "ph_sao_chroma_enabled_flag"); |
12073 | 3.64k | } |
12074 | 108k | if (si->pps->dbf_info_in_ph_flag) { |
12075 | 17.2k | if (gf_bs_read_int_log(bs, 1, "ph_deblocking_params_present_flag")) { |
12076 | | //defaults to 0 |
12077 | 9.47k | u8 deblocking_filter_disabled_flag = 0; |
12078 | | |
12079 | 9.47k | if (!si->pps->deblocking_filter_disabled_flag) { |
12080 | 766 | deblocking_filter_disabled_flag = gf_bs_read_int_log(bs, 1, "deblocking_filter_disabled_flag"); |
12081 | 766 | } |
12082 | 9.47k | if (!deblocking_filter_disabled_flag) { |
12083 | 8.89k | gf_bs_read_se_log(bs, "ph_luma_beta_offset_div2"); |
12084 | 8.89k | gf_bs_read_se_log(bs, "ph_luma_tc_offset_div2"); |
12085 | 8.89k | if (si->pps->chroma_tool_offsets_present_flag) { |
12086 | 5.34k | gf_bs_read_se_log(bs, "ph_cb_beta_offset_div2"); |
12087 | 5.34k | gf_bs_read_se_log(bs, "ph_cb_tc_offset_div2"); |
12088 | 5.34k | gf_bs_read_se_log(bs, "ph_cr_beta_offset_div2"); |
12089 | 5.34k | gf_bs_read_se_log(bs, "ph_cr_tc_offset_div2"); |
12090 | 5.34k | } |
12091 | 8.89k | } |
12092 | 9.47k | } |
12093 | 17.2k | } |
12094 | 108k | if (si->pps->picture_header_extension_present_flag) { |
12095 | 14.9k | u32 i=0, num_ext = gf_bs_read_ue_log(bs, "ph_extension_length"); |
12096 | 66.7M | while (i<num_ext) { |
12097 | 66.7M | gf_bs_read_int_log_idx(bs, 8, "ph_extension_data_byte", i); |
12098 | 66.7M | i++; |
12099 | 66.7M | } |
12100 | 14.9k | } |
12101 | 108k | return 0; |
12102 | 109k | } |
12103 | | |
12104 | | static s32 vvc_get_ctb_info_in_slice(VVCSliceInfo *si, u32 sh_slice_address, u32 sh_num_tiles_in_slice, s32 ctu_index) |
12105 | 215k | { |
12106 | 215k | u32 ctb_y, ctb_x; |
12107 | 215k | if (si->pps->rect_slice_flag) { |
12108 | 21.0k | if (!si->sps->subpic_info_present) { |
12109 | 11.4k | u32 i, j, NumCtusInCurrSlice=0; |
12110 | 11.4k | u32 tcolw_bd[VVC_MAX_TILE_COLS+1]; |
12111 | 11.4k | tcolw_bd[0] = 0; |
12112 | 22.9k | for (i=0; i<si->pps->num_tile_cols; i++) { |
12113 | 11.4k | tcolw_bd[i+1] = tcolw_bd[i] + si->pps->tile_cols_width_ctb[i]; |
12114 | 11.4k | } |
12115 | 11.4k | u32 trowh_bd[VVC_MAX_TILE_ROWS+1]; |
12116 | 11.4k | trowh_bd[0] = 0; |
12117 | 170k | for (i=0; i<si->pps->num_tile_rows; i++) { |
12118 | 159k | trowh_bd[i+1] = trowh_bd[i] + si->pps->tile_rows_height_ctb[i]; |
12119 | 159k | } |
12120 | | |
12121 | 88.5k | for (j=0; j<si->pps->num_tile_rows; j++) { |
12122 | 164k | for (i=0; i<si->pps->num_tile_cols; i++) { |
12123 | 87.8k | u32 min_ctbx = tcolw_bd[i]; |
12124 | 87.8k | u32 max_ctbx = tcolw_bd[i+1]; |
12125 | 87.8k | u32 min_ctby = trowh_bd[j]; |
12126 | 87.8k | u32 max_ctby = trowh_bd[j+1]; |
12127 | 547k | for (ctb_y=min_ctby; ctb_y < max_ctby; ctb_y++) { |
12128 | 929k | for (ctb_x=min_ctbx; ctb_x < max_ctbx; ctb_x++) { |
12129 | 470k | if (ctu_index>=0) { |
12130 | 459k | if (ctu_index == NumCtusInCurrSlice) |
12131 | 10.7k | return ctb_y * si->pps->pic_width_in_ctbsY + ctb_x; |
12132 | 459k | } |
12133 | 459k | NumCtusInCurrSlice++; |
12134 | 459k | } |
12135 | 470k | } |
12136 | 87.8k | } |
12137 | 87.8k | } |
12138 | 699 | return NumCtusInCurrSlice; |
12139 | 11.4k | } |
12140 | | /* |
12141 | | TODO for tiles in subpic ! |
12142 | | */ |
12143 | 9.56k | return -1; |
12144 | | |
12145 | 194k | } else { |
12146 | 194k | u32 i, tidx, NumCtusInCurrSlice = 0; |
12147 | | |
12148 | 531k | for (tidx=sh_slice_address; tidx < sh_slice_address + sh_num_tiles_in_slice; tidx++) { |
12149 | 521k | u32 tileX = tidx % si->pps->num_tile_cols; |
12150 | 521k | u32 tileY = tidx / si->pps->num_tile_cols; |
12151 | 521k | u32 min_ctbx=0; |
12152 | 521k | u32 max_ctbx=0; |
12153 | 521k | u32 min_ctby=0; |
12154 | 521k | u32 max_ctby=0; |
12155 | | |
12156 | 521k | if (tileY>=VVC_MAX_TILE_ROWS) return -1; |
12157 | 521k | if (tileX>=VVC_MAX_TILE_COLS) return -1; |
12158 | | |
12159 | 738k | for (i=0; i<tileY; i++) min_ctby += si->pps->tile_rows_height_ctb[i]; |
12160 | 521k | max_ctby = min_ctby + si->pps->tile_rows_height_ctb[i]; |
12161 | | |
12162 | 3.09M | for (i=0; i<tileX; i++) min_ctbx += si->pps->tile_cols_width_ctb[i]; |
12163 | 521k | max_ctbx = min_ctbx + si->pps->tile_cols_width_ctb[i]; |
12164 | | |
12165 | | |
12166 | 894k | for (ctb_y=min_ctby; ctb_y < max_ctby; ctb_y++) { |
12167 | 15.9M | for (ctb_x=min_ctbx; ctb_x < max_ctbx; ctb_x++) { |
12168 | 15.6M | if (ctu_index>=0) { |
12169 | 15.4M | if (ctu_index == NumCtusInCurrSlice) |
12170 | 184k | return ctb_y * si->pps->pic_width_in_ctbsY + ctb_x; |
12171 | 15.4M | } |
12172 | 15.4M | NumCtusInCurrSlice++; |
12173 | 15.4M | } |
12174 | 557k | } |
12175 | 521k | } |
12176 | 10.0k | if (ctu_index>=0) return -1; |
12177 | 10.0k | return NumCtusInCurrSlice; |
12178 | 10.0k | } |
12179 | 0 | return -1; |
12180 | 215k | } |
12181 | | |
12182 | | static u32 vvc_ctb_to_tile_row_bd(VVCSliceInfo *si, u32 ctb_addr_y) |
12183 | 195k | { |
12184 | 195k | u32 i, tile_y = 0; |
12185 | 195k | u32 tile_row_bd_val = 0; |
12186 | | |
12187 | 755k | for (i=0; i <= si->pps->pic_height_in_ctbsY; i++) { |
12188 | 754k | if (i == tile_row_bd_val + si->pps->tile_rows_height_ctb[tile_y]) { |
12189 | 134k | tile_row_bd_val += si->pps->tile_rows_height_ctb[tile_y]; |
12190 | 134k | tile_y++; |
12191 | 134k | } |
12192 | 754k | if (ctb_addr_y == i) return tile_row_bd_val; |
12193 | 754k | } |
12194 | 130 | return 0; |
12195 | 195k | } |
12196 | | |
12197 | | static u32 vvc_ctb_to_tile_col_bd(VVCSliceInfo *si, u32 ctb_addr_x) |
12198 | 195k | { |
12199 | 195k | u32 i, tile_x = 0; |
12200 | 195k | u32 tile_col_bd_val = 0; |
12201 | 17.7M | for (i=0; i <= si->pps->pic_width_in_ctbsY; i++) { |
12202 | 17.7M | if (i == tile_col_bd_val + si->pps->tile_cols_width_ctb[tile_x] ) { |
12203 | 371k | tile_col_bd_val += si->pps->tile_cols_width_ctb[tile_x]; |
12204 | 371k | tile_x++; |
12205 | 371k | } |
12206 | 17.7M | if (ctb_addr_x == i) return tile_col_bd_val; |
12207 | 17.7M | } |
12208 | 0 | return 0; |
12209 | 195k | } |
12210 | | |
12211 | | static s32 vvc_get_num_entry_points(VVCSliceInfo *si, u32 sh_slice_address, u32 sh_num_tiles_in_slice) |
12212 | 24.1k | { |
12213 | 24.1k | if (!si->sps->entry_point_offsets_present_flag) return 0; |
12214 | | |
12215 | 24.1k | s32 nb_entry_points = 0; |
12216 | 24.1k | u32 prev_ctb_addr_y=0; |
12217 | 24.1k | u32 prev_ctb_to_tile_row_bd, prev_ctb_to_tile_col_bd; |
12218 | 24.1k | s32 i; |
12219 | 24.1k | if (!si->pps->num_tile_rows || !si->pps->num_tile_cols) return 0; |
12220 | | |
12221 | 20.2k | s32 nb_ctus_in_slice = vvc_get_ctb_info_in_slice(si, sh_slice_address, sh_num_tiles_in_slice, -1); |
12222 | 20.2k | if (nb_ctus_in_slice<0) return -1; |
12223 | | |
12224 | 206k | for (i=0; i < nb_ctus_in_slice; i++ ) { |
12225 | 195k | s32 addr; |
12226 | 195k | u32 ctb_addr_x, ctb_addr_y; |
12227 | 195k | u32 ctb_to_tile_row_bd, ctb_to_tile_col_bd; |
12228 | | |
12229 | 195k | addr = vvc_get_ctb_info_in_slice(si, sh_slice_address, sh_num_tiles_in_slice, i); |
12230 | 195k | if (addr<0) return -1; |
12231 | 195k | ctb_addr_x = (u32) ( addr % si->pps->pic_width_in_ctbsY ); |
12232 | 195k | ctb_addr_y = (u32) ( addr / si->pps->pic_width_in_ctbsY ); |
12233 | | |
12234 | 195k | ctb_to_tile_row_bd = vvc_ctb_to_tile_row_bd(si, ctb_addr_y); |
12235 | 195k | ctb_to_tile_col_bd = vvc_ctb_to_tile_col_bd(si, ctb_addr_x); |
12236 | | |
12237 | | //ignore first CTU, not an entry point |
12238 | 195k | if (i) { |
12239 | 184k | if ( ctb_to_tile_row_bd != prev_ctb_to_tile_row_bd |
12240 | 184k | || ctb_to_tile_col_bd != prev_ctb_to_tile_col_bd |
12241 | 184k | || ((ctb_addr_y != prev_ctb_addr_y) && si->sps->entropy_coding_sync_enabled_flag) |
12242 | 184k | ) { |
12243 | 19.7k | nb_entry_points++; |
12244 | 19.7k | } |
12245 | 184k | } |
12246 | 195k | prev_ctb_addr_y = ctb_addr_y; |
12247 | 195k | prev_ctb_to_tile_row_bd = ctb_to_tile_row_bd; |
12248 | 195k | prev_ctb_to_tile_col_bd = ctb_to_tile_col_bd; |
12249 | 195k | } |
12250 | 10.7k | return nb_entry_points; |
12251 | 10.7k | } |
12252 | | |
12253 | | static s32 vvc_parse_slice(GF_BitStream *bs, VVCState *vvc, VVCSliceInfo *si) |
12254 | 194k | { |
12255 | 194k | u32 i, subpic_id=0, slice_address=0, num_tiles_in_slice=1; |
12256 | | |
12257 | 194k | si->picture_header_in_slice_header_flag = gf_bs_read_int_log(bs, 1, "picture_header_in_slice_header_flag"); |
12258 | 194k | if (si->picture_header_in_slice_header_flag) { |
12259 | 100k | s32 res = vvc_parse_picture_header(bs, vvc, si); |
12260 | 100k | if (res<0) return res; |
12261 | 100k | } |
12262 | 190k | if (!si->sps) return -1; |
12263 | 187k | if (!si->pps) return -1; |
12264 | 187k | si->slice_type = GF_VVC_SLICE_TYPE_I; |
12265 | 187k | if (si->sps->subpic_info_present) { |
12266 | 78.2k | subpic_id = gf_bs_read_int_log(bs, si->sps->subpicid_len, "subpic_id"); |
12267 | 78.2k | } |
12268 | | |
12269 | 187k | if (si->pps->rect_slice_flag) { |
12270 | 70.0k | if ((si->sps->nb_subpics==1) && (si->pps->num_slices_in_pic<=1)) { |
12271 | | |
12272 | 46.6k | } else { |
12273 | 46.6k | if (!vvc->parse_mode) { |
12274 | 31.8k | return 0; |
12275 | 31.8k | } |
12276 | 14.7k | VVC_SubpicInfo *sp = NULL; |
12277 | 23.7k | for (i=0;i<si->sps->nb_subpics; i++) { |
12278 | 17.3k | if (si->pps->subpics[i].id==subpic_id) { |
12279 | 8.34k | sp = &si->pps->subpics[i]; |
12280 | 8.34k | break; |
12281 | 8.34k | } |
12282 | 17.3k | } |
12283 | 14.7k | if (!sp) { |
12284 | 6.41k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] sub-picture with id %u not found\n", subpic_id)); |
12285 | 6.41k | return -1; |
12286 | 6.41k | } |
12287 | 8.34k | if (sp->num_slices > 1 ) { |
12288 | 0 | u32 shbits = vvc_ceillog2(sp->num_slices); |
12289 | 0 | slice_address = gf_bs_read_int_log(bs, shbits, "sh_slice_address"); |
12290 | 0 | } |
12291 | 8.34k | } |
12292 | 117k | } else { |
12293 | 117k | if (si->pps->num_tiles_in_pic > 1) { |
12294 | 31.6k | slice_address = gf_bs_read_int_log(bs, si->pps->slice_address_len, "sh_slice_address"); |
12295 | 31.6k | } |
12296 | 117k | } |
12297 | | |
12298 | 148k | if (si->sps->sh_num_extra_bits) |
12299 | 58.7k | gf_bs_read_int_log(bs, si->sps->sh_num_extra_bits, "num_extra_bits"); |
12300 | | |
12301 | 148k | if (!si->pps->rect_slice_flag && (si->pps->num_tiles_in_pic - slice_address > 1)) { |
12302 | 29.2k | num_tiles_in_slice = 1 + gf_bs_read_ue_log(bs, "sh_num_tiles_in_slice_minus1"); |
12303 | 29.2k | if (num_tiles_in_slice > si->pps->num_tiles_in_pic) { |
12304 | 7.94k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] Invalid number of tiles in slice %u, should be less than %u\n", num_tiles_in_slice, si->pps->num_tiles_in_pic)); |
12305 | 7.94k | num_tiles_in_slice = si->pps->num_tiles_in_pic; |
12306 | 7.94k | } |
12307 | 29.2k | } |
12308 | | |
12309 | 148k | if (si->inter_slice_allowed_flag ) |
12310 | 107k | si->slice_type = gf_bs_read_ue_log(bs, "slice_type"); |
12311 | | |
12312 | 148k | if (!vvc->parse_mode) return 0; |
12313 | | |
12314 | 49.8k | switch (si->nal_unit_type) { |
12315 | 13.9k | case GF_VVC_NALU_SLICE_IDR_W_RADL: |
12316 | 21.4k | case GF_VVC_NALU_SLICE_IDR_N_LP: |
12317 | 22.1k | case GF_VVC_NALU_SLICE_CRA: |
12318 | 24.1k | case GF_VVC_NALU_SLICE_GDR: |
12319 | 24.1k | gf_bs_read_int_log(bs, 1, "sh_no_output_of_prior_pics_flag"); |
12320 | 24.1k | break; |
12321 | 49.8k | } |
12322 | 49.8k | if (si->sps->alf_enabled_flag && !si->pps->alf_info_in_ph_flag) { |
12323 | 9.42k | if (gf_bs_read_int_log(bs, 1, "sh_alf_enabled_flag")) { |
12324 | 3.75k | u32 nb_vals = gf_bs_read_int_log(bs, 3, "sh_num_alf_aps_ids_luma"); |
12325 | 15.3k | for (i=0; i<nb_vals; i++) { |
12326 | 11.5k | gf_bs_read_int_log(bs, 3, "sh_alf_aps_id_luma"); |
12327 | 11.5k | } |
12328 | 3.75k | u8 sh_alf_cb_enabled_flag = 0; |
12329 | 3.75k | u8 sh_alf_cr_enabled_flag = 0; |
12330 | 3.75k | if (si->sps->chroma_format_idc) { |
12331 | 1.47k | sh_alf_cb_enabled_flag = gf_bs_read_int_log(bs, 1, "sh_alf_cb_enabled_flag"); |
12332 | 1.47k | sh_alf_cr_enabled_flag = gf_bs_read_int_log(bs, 1, "sh_alf_cr_enabled_flag"); |
12333 | 1.47k | } |
12334 | 3.75k | if (sh_alf_cb_enabled_flag || sh_alf_cr_enabled_flag) { |
12335 | 1.02k | gf_bs_read_int_log(bs, 3, "sh_alf_aps_id_chroma"); |
12336 | 1.02k | } |
12337 | 3.75k | if (si->sps->ccalf_enabled_flag) { |
12338 | 435 | if (gf_bs_read_int_log(bs, 1, "sh_alf_cc_cb_enabled_flag")) { |
12339 | 107 | gf_bs_read_int_log(bs, 3, "sh_alf_cc_cb_aps_id"); |
12340 | 107 | } |
12341 | 435 | if (gf_bs_read_int_log(bs, 1, "sh_alf_cc_cr_enabled_flag")) { |
12342 | 180 | gf_bs_read_int_log(bs, 3, "sh_alf_cc_cr_aps_id"); |
12343 | 180 | } |
12344 | 435 | } |
12345 | 3.75k | } |
12346 | 9.42k | } |
12347 | 49.8k | if (si->lmcs_enabled_flag && !si->picture_header_in_slice_header_flag) { |
12348 | 3.99k | gf_bs_read_int_log(bs, 1, "sh_lmcs_used_flag"); |
12349 | 3.99k | } |
12350 | 49.8k | if (si->explicit_scaling_list_enabled_flag && !si->picture_header_in_slice_header_flag) { |
12351 | 7.30k | gf_bs_read_int_log(bs, 3, "sh_explicit_scaling_list_used_flag"); |
12352 | 7.30k | } |
12353 | | |
12354 | 49.8k | if (si->pps->rpl_info_in_ph_flag) { |
12355 | 18.8k | si->rpl[0] = si->ph_rpl[0]; |
12356 | 18.8k | si->rpl[1] = si->ph_rpl[1]; |
12357 | 30.9k | } else { |
12358 | 30.9k | memset(&si->rpl[0], 0, sizeof(VVC_RefPicList)); |
12359 | 30.9k | memset(&si->rpl[1], 0, sizeof(VVC_RefPicList)); |
12360 | 30.9k | } |
12361 | | |
12362 | 49.8k | if (!si->pps->rpl_info_in_ph_flag |
12363 | 49.8k | && ( ( (si->nal_unit_type != GF_VVC_NALU_SLICE_IDR_N_LP) && (si->nal_unit_type != GF_VVC_NALU_SLICE_IDR_W_RADL) ) || si->sps->idr_rpl_present_flag) |
12364 | 49.8k | ) { |
12365 | 24.9k | s32 res = vvc_parse_ref_pic_lists(bs, si, GF_FALSE); |
12366 | 24.9k | if (res<0) return (vvc->parse_mode==1) ? res : 0; |
12367 | 24.9k | } |
12368 | | |
12369 | 44.7k | si->num_ref_idx_active[0] = si->num_ref_idx_active[1] = 0; |
12370 | | |
12371 | 44.7k | if ( |
12372 | 44.7k | ((si->slice_type != GF_VVC_SLICE_TYPE_I) && (si->rpl[0].num_ref_entries > 1) ) |
12373 | 44.7k | || ((si->slice_type == GF_VVC_SLICE_TYPE_B) && (si->rpl[1].num_ref_entries > 1)) |
12374 | 44.7k | ) { |
12375 | 10.0k | if (gf_bs_read_int_log(bs, 1, "sh_num_ref_idx_active_override_flag")) { |
12376 | | //L0 |
12377 | 3.62k | u32 nb_active = 0; |
12378 | 3.62k | if (si->rpl[0].num_ref_entries>1) { |
12379 | 2.63k | nb_active = 1 + gf_bs_read_ue_log_idx(bs, "sh_num_ref_idx_active_minus1", 0); |
12380 | 2.63k | } |
12381 | 3.62k | si->num_ref_idx_active[0] = nb_active; |
12382 | | //L1 |
12383 | 3.62k | if (si->slice_type == GF_VVC_SLICE_TYPE_B) { |
12384 | 2.50k | nb_active = 0; |
12385 | 2.50k | if (si->rpl[1].num_ref_entries>1) { |
12386 | 1.21k | nb_active = 1 + gf_bs_read_ue_log_idx(bs, "sh_num_ref_idx_active_minus1", 1); |
12387 | 1.21k | } |
12388 | 2.50k | si->num_ref_idx_active[1] = nb_active; |
12389 | 2.50k | } else { |
12390 | 1.12k | si->num_ref_idx_active[1] = 0; |
12391 | 1.12k | } |
12392 | 6.42k | } else { |
12393 | 6.42k | if (si->rpl[0].num_ref_entries >= si->pps->num_ref_idx_default_active[0]) { |
12394 | 4.54k | si->num_ref_idx_active[0] = si->pps->num_ref_idx_default_active[0]; |
12395 | 4.54k | } else { |
12396 | 1.87k | si->num_ref_idx_active[0] = si->rpl[0].num_ref_entries; |
12397 | 1.87k | } |
12398 | 6.42k | if (si->slice_type == GF_VVC_SLICE_TYPE_B) { |
12399 | 4.92k | if (si->rpl[1].num_ref_entries >= si->pps->num_ref_idx_default_active[1]) { |
12400 | 2.25k | si->num_ref_idx_active[1] = si->pps->num_ref_idx_default_active[1]; |
12401 | 2.66k | } else { |
12402 | 2.66k | si->num_ref_idx_active[1] = si->rpl[1].num_ref_entries; |
12403 | 2.66k | } |
12404 | 4.92k | } else { |
12405 | 1.50k | si->num_ref_idx_active[1] = 0; |
12406 | 1.50k | } |
12407 | 6.42k | } |
12408 | 34.6k | } else { |
12409 | 34.6k | si->num_ref_idx_active[0] = (si->slice_type == GF_VVC_SLICE_TYPE_I) ? 0 : 1; |
12410 | 34.6k | si->num_ref_idx_active[1] = (si->slice_type == GF_VVC_SLICE_TYPE_B) ? 1 : 0; |
12411 | 34.6k | } |
12412 | | |
12413 | 44.7k | if (si->slice_type != GF_VVC_SLICE_TYPE_I) { |
12414 | 34.8k | if (si->pps->cabac_init_present_flag) |
12415 | 25.2k | gf_bs_read_int_log(bs, 1, "sh_cabac_init_flag"); |
12416 | | |
12417 | 34.8k | if (si->temporal_mvp_enabled_flag && !si->pps->rpl_info_in_ph_flag) { |
12418 | 3.43k | u8 collocated_from_l0_flag = 0; |
12419 | 3.43k | if (si->slice_type == GF_VVC_SLICE_TYPE_B) { |
12420 | 2.87k | collocated_from_l0_flag = gf_bs_read_int_log(bs, 1, "sh_collocated_from_l0_flag"); |
12421 | 2.87k | } else { |
12422 | | //(sh_slice_type is equal to P), the value of sh_collocated_from_l0_flag is inferred to be equal to 1 |
12423 | 559 | collocated_from_l0_flag = 1; |
12424 | 559 | } |
12425 | 3.43k | if ( (collocated_from_l0_flag && (si->num_ref_idx_active[0] > 1)) |
12426 | 3.43k | || (!collocated_from_l0_flag && (si->num_ref_idx_active[1] > 1)) |
12427 | 3.43k | ) { |
12428 | 152 | gf_bs_read_ue_log(bs, "sh_collocated_ref_idx"); |
12429 | 152 | } |
12430 | 3.43k | } |
12431 | 34.8k | if (!si->pps->wp_info_in_ph_flag |
12432 | 34.8k | && ( |
12433 | 28.6k | (si->pps->weighted_pred_flag && (si->slice_type == GF_VVC_SLICE_TYPE_P) ) |
12434 | 28.6k | || (si->pps->weighted_bipred_flag && (si->slice_type == GF_VVC_SLICE_TYPE_B)) |
12435 | 28.6k | ) |
12436 | 34.8k | ) { |
12437 | 8.93k | s32 res = vvc_pred_weight_table(bs, vvc, si, si->pps, si->sps, si->num_ref_idx_active); |
12438 | 8.93k | if (res<0) return (vvc->parse_mode==1) ? res : 0; |
12439 | 8.93k | } |
12440 | 34.8k | } |
12441 | 44.6k | if (!si->pps->qp_delta_info_in_ph_flag) { |
12442 | 35.7k | gf_bs_read_se_log(bs, "sh_qp_delta"); |
12443 | 35.7k | } |
12444 | 44.6k | if (si->pps->slice_chroma_qp_offsets_present_flag) { |
12445 | 18.5k | gf_bs_read_se_log(bs, "sh_cb_qp_offset"); |
12446 | 18.5k | gf_bs_read_se_log(bs, "sh_cr_qp_offset"); |
12447 | 18.5k | if (si->sps->joint_cbcr_enabled_flag) |
12448 | 4.50k | gf_bs_read_se_log(bs, "sh_joint_cbcr_qp_offset"); |
12449 | 18.5k | } |
12450 | 44.6k | if (si->pps->cu_chroma_qp_offset_list_enabled_flag) |
12451 | 22.1k | gf_bs_read_int_log(bs, 1, "sh_cu_chroma_qp_offset_enabled_flag"); |
12452 | 44.6k | if (si->sps->sao_enabled_flag && !si->pps->sao_info_in_ph_flag) { |
12453 | 5.75k | gf_bs_read_int_log(bs, 1, "sh_sao_luma_used_flag"); |
12454 | 5.75k | if (si->sps->chroma_format_idc) |
12455 | 2.51k | gf_bs_read_int_log(bs, 1, "sh_sao_chroma_used_flag"); |
12456 | 5.75k | } |
12457 | | |
12458 | 44.6k | if (si->pps->deblocking_filter_override_enabled_flag && !si->pps->dbf_info_in_ph_flag) { |
12459 | 7.93k | if (gf_bs_read_int_log(bs, 1, "sh_deblocking_params_present_flag")) { |
12460 | 3.23k | u8 deblocking_params_disabled_flag=0; |
12461 | 3.23k | if (!si->pps->deblocking_filter_disabled_flag) { |
12462 | 1.55k | deblocking_params_disabled_flag = gf_bs_read_int_log(bs, 1, "sh_deblocking_filter_disabled_flag"); |
12463 | 1.55k | } |
12464 | | |
12465 | 3.23k | if (!deblocking_params_disabled_flag) { |
12466 | 2.49k | gf_bs_read_se_log(bs, "sh_luma_beta_offset_div2"); |
12467 | 2.49k | gf_bs_read_se_log(bs, "sh_luma_tc_offset_div2"); |
12468 | 2.49k | if (si->pps->chroma_tool_offsets_present_flag) { |
12469 | 2.49k | gf_bs_read_se_log(bs, "sh_cb_beta_offset_div2"); |
12470 | 2.49k | gf_bs_read_se_log(bs, "sh_cb_tc_offset_div2"); |
12471 | 2.49k | gf_bs_read_se_log(bs, "sh_cr_beta_offset_div2"); |
12472 | 2.49k | gf_bs_read_se_log(bs, "sh_cr_tc_offset_div2"); |
12473 | 2.49k | } |
12474 | 2.49k | } |
12475 | 3.23k | } |
12476 | 7.93k | } |
12477 | | |
12478 | 44.6k | u8 dep_quant_used_flag = 0; |
12479 | 44.6k | if (si->sps->dep_quant_enabled_flag) { |
12480 | 10.1k | dep_quant_used_flag = gf_bs_read_int_log(bs, 1, "sh_dep_quant_used_flag"); |
12481 | 10.1k | } |
12482 | 44.6k | u8 sign_data_hiding_used_flag = 0; |
12483 | 44.6k | if (si->sps->sign_data_hiding_enabled_flag && !dep_quant_used_flag) { |
12484 | 12.1k | sign_data_hiding_used_flag = gf_bs_read_int_log(bs, 1, "sh_sign_data_hiding_used_flag"); |
12485 | 12.1k | } |
12486 | 44.6k | u8 ts_residual_coding_disabled_flag = 0; |
12487 | 44.6k | if (si->sps->transform_skip_enabled_flag && !dep_quant_used_flag && !sign_data_hiding_used_flag) { |
12488 | 24.7k | ts_residual_coding_disabled_flag = gf_bs_read_int_log(bs, 1, "sh_ts_residual_coding_disabled_flag"); |
12489 | 24.7k | } |
12490 | 44.6k | if (!ts_residual_coding_disabled_flag && si->sps->ts_residual_coding_rice_present_in_sh_flag) { |
12491 | 0 | gf_bs_read_int_log(bs, 3, "sh_ts_residual_coding_rice_idx_minus1"); |
12492 | 0 | } |
12493 | 44.6k | if (si->sps->reverse_last_sig_coeff_enabled_flag) { |
12494 | 0 | gf_bs_read_int_log(bs, 1, "sh_reverse_last_sig_coeff_flag"); |
12495 | 0 | } |
12496 | 44.6k | if (si->pps->slice_header_extension_present_flag) { |
12497 | 12.4k | u32 j=0, slice_header_extension_length = gf_bs_read_ue_log(bs, "sh_slice_header_extension_length"); |
12498 | 8.16M | while (j<slice_header_extension_length) { |
12499 | 8.14M | gf_bs_read_int_log_idx(bs, 8, "sh_slice_header_extension_data_byte", j); |
12500 | 8.14M | j++; |
12501 | 8.14M | } |
12502 | 12.4k | } |
12503 | | |
12504 | 44.6k | if (si->sps->entry_point_offsets_present_flag) { |
12505 | 24.1k | s32 nb_entry_points = vvc_get_num_entry_points(si, slice_address, num_tiles_in_slice); |
12506 | 24.1k | if (nb_entry_points<0) { |
12507 | 9.56k | if (vvc->parse_mode==1) { |
12508 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[VVC] Entry point offsets parsing for tiled sub-picture not yet implemented, wrong slice header size estimation (result might be non-compliant) - patch welcome\n")); |
12509 | |
|
12510 | 0 | gf_bs_align(bs); |
12511 | 0 | si->payload_start_offset = (u32) gf_bs_get_position(bs); |
12512 | 0 | return -2; |
12513 | 0 | } |
12514 | 9.56k | GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[VVC] Entry point offsets parsing for tiled sub-picture not yet implemented, aborting slice header parsing - patch welcome\n")); |
12515 | 9.56k | return 0; |
12516 | 9.56k | } |
12517 | 14.6k | if (nb_entry_points) { |
12518 | 5.56k | u32 nb_bits = 1 + gf_bs_read_ue_log(bs, "sh_entry_offset_len_minus1"); |
12519 | 25.3k | for (i=0; i<(u32) nb_entry_points; i++) { |
12520 | 19.7k | gf_bs_read_int_log_idx(bs, nb_bits, "sh_entry_point_offset_minus1", i); |
12521 | 19.7k | } |
12522 | 5.56k | } |
12523 | 14.6k | } |
12524 | | |
12525 | 35.0k | u8 align_bit = gf_bs_read_int(bs, 1); |
12526 | 35.0k | if (align_bit != 1) { |
12527 | 26.0k | GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[VVC] Align bit at end of slice header not set to 1 !\n")); |
12528 | | //return error only for strict mdoe |
12529 | 26.0k | if (vvc->parse_mode==1) { |
12530 | 0 | return -1; |
12531 | 0 | } |
12532 | 26.0k | } |
12533 | 35.0k | gf_bs_align(bs); |
12534 | | //end of slice header |
12535 | 35.0k | si->payload_start_offset = (u32) gf_bs_get_position(bs); |
12536 | 35.0k | return 0; |
12537 | 35.0k | } |
12538 | | |
12539 | | static void vvc_compute_poc(VVCSliceInfo *si, Bool poc_reset) |
12540 | 99.3k | { |
12541 | 99.3k | u32 max_poc_lsb = 1 << (si->sps->log2_max_poc_lsb); |
12542 | | |
12543 | 99.3k | if (si->poc_msb_cycle_present_flag) { |
12544 | 39.4k | si->poc_msb = si->poc_msb_cycle * max_poc_lsb; |
12545 | 59.9k | } else if (poc_reset) { |
12546 | 26.3k | si->poc_msb = 0; |
12547 | 33.5k | } else { |
12548 | 33.5k | if ((si->poc_lsb < si->poc_lsb_prev) && (si->poc_lsb_prev - si->poc_lsb >= max_poc_lsb / 2)) |
12549 | 4.82k | si->poc_msb = si->poc_msb_prev + max_poc_lsb; |
12550 | 28.7k | else if ((si->poc_lsb > si->poc_lsb_prev) && (si->poc_lsb - si->poc_lsb_prev > max_poc_lsb / 2)) |
12551 | 4.41k | si->poc_msb = si->poc_msb_prev - max_poc_lsb; |
12552 | 24.3k | else |
12553 | 24.3k | si->poc_msb = si->poc_msb_prev; |
12554 | 33.5k | } |
12555 | | |
12556 | 99.3k | si->poc = si->poc_msb + si->poc_lsb; |
12557 | 99.3k | } |
12558 | | |
12559 | | static void vvc_push_ref_poc(VVCSliceInfo *si, s32 poc) |
12560 | 14.0k | { |
12561 | 14.0k | u32 i; |
12562 | 37.3k | for (i=0;i<si->nb_reference_pocs; i++) { |
12563 | 25.2k | if (si->reference_pocs[i]==poc) return; |
12564 | 25.2k | } |
12565 | 12.1k | if (si->nb_reference_pocs==GF_ARRAY_LENGTH(si->reference_pocs)) return; |
12566 | 11.6k | si->reference_pocs[si->nb_reference_pocs] = poc; |
12567 | 11.6k | si->nb_reference_pocs++; |
12568 | 11.6k | } |
12569 | | |
12570 | | static void vvc_compute_refs(VVCState *vvc, VVCSliceInfo *si) |
12571 | 26.0k | { |
12572 | 26.0k | u32 lidx, ridx; |
12573 | | |
12574 | 26.0k | if (si->slice_type==GF_VVC_SLICE_TYPE_I) { |
12575 | 7.03k | si->nb_reference_pocs = 0; |
12576 | 7.03k | return; |
12577 | 7.03k | } |
12578 | | |
12579 | 57.0k | for (lidx=0; lidx<2; lidx++) { |
12580 | 38.0k | VVC_RefPicList *rpl = &si->rpl[lidx]; |
12581 | 38.0k | u32 num_active_refs = si->num_ref_idx_active[lidx]; |
12582 | | |
12583 | 164k | for (ridx=0; ridx < MIN(rpl->num_ref_entries, VVC_MAX_REF_PICS); ridx++) { |
12584 | 126k | Bool is_active_ref = ridx < num_active_refs ? GF_TRUE : GF_FALSE; |
12585 | 126k | s32 refPOC=0; |
12586 | | //bool isLongTerm = false; |
12587 | 126k | if (rpl->ref_pic_type[ridx] == VVC_RPL_IL) { |
12588 | | //TODO interlayer |
12589 | 2.50k | if (!si->nb_lt_or_il_pics) { |
12590 | 816 | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] Inter-Layer pictures in RefPicList not supported, patch welcome\n")); |
12591 | 816 | } |
12592 | 2.50k | si->nb_lt_or_il_pics ++; |
12593 | 123k | } else if (rpl->ref_pic_type[ridx] == VVC_RPL_ST) { |
12594 | 121k | refPOC = si->poc + rpl->poc_delta[ridx]; |
12595 | 121k | } else { |
12596 | | //TODO LongTerm |
12597 | 2.33k | if (!si->nb_lt_or_il_pics) { |
12598 | 1.16k | GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VVC] Long-Term pictures in RefPicList not supported, patch welcome\n")); |
12599 | 1.16k | } |
12600 | 2.33k | si->nb_lt_or_il_pics ++; |
12601 | 2.33k | } |
12602 | | |
12603 | 126k | if (!is_active_ref) continue; |
12604 | 14.0k | vvc_push_ref_poc(si, refPOC); |
12605 | 14.0k | } |
12606 | 38.0k | } |
12607 | 19.0k | } |
12608 | | |
12609 | | GF_EXPORT |
12610 | | s32 gf_vvc_parse_nalu_bs(GF_BitStream *bs, VVCState *vvc, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id) |
12611 | 488k | { |
12612 | 488k | Bool is_slice = GF_FALSE; |
12613 | 488k | s32 ret = -1; |
12614 | 488k | Bool poc_reset = GF_FALSE; |
12615 | 488k | VVCSliceInfo n_state; |
12616 | | |
12617 | 488k | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
12618 | 488k | if (gf_bs_available(bs)<2) return -1; |
12619 | 488k | gf_bs_mark_overflow(bs, GF_TRUE); |
12620 | | |
12621 | 488k | memcpy(&n_state, &vvc->s_info, sizeof(VVCSliceInfo)); |
12622 | 488k | if (!vvc_parse_nal_header(bs, nal_unit_type, temporal_id, layer_id)) return -1; |
12623 | | |
12624 | 389k | n_state.nal_unit_type = *nal_unit_type; |
12625 | | |
12626 | 389k | switch (n_state.nal_unit_type) { |
12627 | 262 | case GF_VVC_NALU_ACCESS_UNIT: |
12628 | 7.53k | case GF_VVC_NALU_END_OF_SEQ: |
12629 | 8.55k | case GF_VVC_NALU_END_OF_STREAM: |
12630 | | //don't restore slice info, we don't have any change and n_state.poc_lsb / n_state.poc_msb is not valid |
12631 | 8.55k | vvc->s_info.nal_unit_type = n_state.nal_unit_type; |
12632 | 8.55k | return 1; |
12633 | | |
12634 | 55.4k | case GF_VVC_NALU_SLICE_IDR_W_RADL: |
12635 | 80.5k | case GF_VVC_NALU_SLICE_IDR_N_LP: |
12636 | 80.5k | poc_reset = GF_TRUE; |
12637 | 99.4k | case GF_VVC_NALU_SLICE_TRAIL: |
12638 | 155k | case GF_VVC_NALU_SLICE_STSA: |
12639 | 185k | case GF_VVC_NALU_SLICE_RADL: |
12640 | 186k | case GF_VVC_NALU_SLICE_RASL: |
12641 | 188k | case GF_VVC_NALU_SLICE_CRA: |
12642 | 194k | case GF_VVC_NALU_SLICE_GDR: |
12643 | | /* slice - read the info and compare.*/ |
12644 | 194k | ret = vvc_parse_slice(bs, vvc, &n_state); |
12645 | 194k | if (ret < 0) { |
12646 | 13.4k | memcpy(&vvc->s_info, &n_state, sizeof(VVCSliceInfo)); |
12647 | 13.4k | return ret; |
12648 | 13.4k | } |
12649 | | |
12650 | 180k | ret = 0; |
12651 | 180k | if (n_state.compute_poc_defer || n_state.picture_header_in_slice_header_flag) { |
12652 | 99.3k | is_slice = GF_TRUE; |
12653 | 99.3k | n_state.compute_poc_defer = 0; |
12654 | | |
12655 | 99.3k | vvc_compute_poc(&n_state, poc_reset); |
12656 | 99.3k | if (vvc->parse_mode) |
12657 | 26.0k | vvc_compute_refs(vvc, &n_state); |
12658 | | |
12659 | 99.3k | if (vvc->s_info.poc != n_state.poc) { |
12660 | 40.8k | ret = 1; |
12661 | 40.8k | break; |
12662 | 40.8k | } |
12663 | 58.4k | if (!(*layer_id) || (n_state.prev_layer_id_plus1 && ((*layer_id) <= n_state.prev_layer_id_plus1 - 1))) { |
12664 | 41.4k | ret = 1; |
12665 | 41.4k | break; |
12666 | 41.4k | } |
12667 | 58.4k | } |
12668 | 98.3k | break; |
12669 | | |
12670 | 98.3k | case GF_VVC_NALU_PIC_HEADER: |
12671 | 12.1k | if (vvc_parse_picture_header(bs, vvc, &n_state)<0) { |
12672 | 602 | ret = -1; |
12673 | 602 | break; |
12674 | 602 | } |
12675 | 11.5k | is_slice = GF_TRUE; |
12676 | | |
12677 | | //we cannot compute poc until we know the first picture unit type, since IDR will reset poc count |
12678 | | //and irap_or_gdr_pic=0 does not prevent IDR from following |
12679 | 11.5k | n_state.compute_poc_defer = 1; |
12680 | | |
12681 | 11.5k | if (!(*layer_id) || (n_state.prev_layer_id_plus1 && ((*layer_id) <= n_state.prev_layer_id_plus1 - 1))) { |
12682 | 7.36k | ret = 1; |
12683 | 7.36k | } |
12684 | 11.5k | break; |
12685 | 74.4k | case GF_VVC_NALU_SEQ_PARAM: |
12686 | 74.4k | vvc->last_parsed_sps_id = gf_vvc_read_sps_bs_internal(bs, vvc, *layer_id, NULL); |
12687 | 74.4k | ret = (vvc->last_parsed_sps_id>=0) ? 0 : -1; |
12688 | 74.4k | break; |
12689 | 50.2k | case GF_VVC_NALU_PIC_PARAM: |
12690 | 50.2k | vvc->last_parsed_pps_id = gf_vvc_read_pps_bs_internal(bs, vvc); |
12691 | 50.2k | ret = (vvc->last_parsed_pps_id>=0) ? 0 : -1; |
12692 | 50.2k | break; |
12693 | 6.05k | case GF_VVC_NALU_VID_PARAM: |
12694 | 6.05k | vvc->last_parsed_vps_id = gf_vvc_read_vps_bs_internal(bs, vvc, GF_FALSE); |
12695 | 6.05k | ret = (vvc->last_parsed_vps_id>=0) ? 0 : -1; |
12696 | 6.05k | break; |
12697 | 764 | case GF_VVC_NALU_DEC_PARAM: |
12698 | 764 | ret = 0; |
12699 | 764 | break; |
12700 | 2.23k | case GF_VVC_NALU_APS_PREFIX: |
12701 | | //we use the mix aps type + aps id (first 8 bits) as unique identifier |
12702 | 2.23k | vvc->last_parsed_aps_id = gf_bs_read_int_log(bs, 8, "aps_id"); |
12703 | 2.23k | ret = 0; |
12704 | 2.23k | break; |
12705 | 41.1k | default: |
12706 | 41.1k | ret = 0; |
12707 | 41.1k | break; |
12708 | 389k | } |
12709 | 367k | if (gf_bs_is_overflow(bs)) ret = -1; |
12710 | | |
12711 | | /* save current POC lsb/msb to prev values */ |
12712 | 367k | if ((ret>0) && vvc->s_info.sps) { |
12713 | 72.3k | if (!n_state.compute_poc_defer) { |
12714 | 65.1k | n_state.poc_lsb_prev = n_state.poc_lsb; |
12715 | 65.1k | n_state.poc_msb_prev = n_state.poc_msb; |
12716 | 65.1k | } |
12717 | 72.3k | if (is_slice) |
12718 | 72.3k | n_state.prev_layer_id_plus1 = *layer_id + 1; |
12719 | 72.3k | } |
12720 | | |
12721 | 367k | memcpy(&vvc->s_info, &n_state, sizeof(VVCSliceInfo)); |
12722 | | |
12723 | 367k | return ret; |
12724 | 389k | } |
12725 | | |
12726 | | GF_EXPORT |
12727 | | s32 gf_vvc_parse_nalu(u8 *data, u32 size, VVCState *vvc, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id) |
12728 | 0 | { |
12729 | 0 | GF_BitStream *bs = NULL; |
12730 | 0 | s32 ret; |
12731 | |
|
12732 | 0 | if (size<2) return -1; |
12733 | | |
12734 | 0 | if (!vvc) { |
12735 | 0 | if (nal_unit_type) (*nal_unit_type) = data[1] >> 3; |
12736 | 0 | if (layer_id) (*layer_id) = data[0] & 0x3f; |
12737 | 0 | if (temporal_id) (*temporal_id) = (data[1] & 0x7); |
12738 | 0 | return -1; |
12739 | 0 | } |
12740 | 0 | bs = gf_bs_new(data, size, GF_BITSTREAM_READ); |
12741 | 0 | if (!bs) return -1; |
12742 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
12743 | |
|
12744 | 0 | ret = gf_vvc_parse_nalu_bs(bs, vvc, nal_unit_type, temporal_id, layer_id); |
12745 | 0 | gf_bs_del(bs); |
12746 | 0 | return ret; |
12747 | 0 | } |
12748 | | |
12749 | | Bool gf_vvc_slice_is_ref(VVCState *vvc) |
12750 | 126k | { |
12751 | 126k | if (!vvc->s_info.irap_or_gdr_pic) { |
12752 | 64.4k | return GF_FALSE; |
12753 | 64.4k | } |
12754 | 62.1k | if (vvc->s_info.gdr_pic) { |
12755 | 49.9k | if (vvc->s_info.recovery_point_valid) { |
12756 | 13.8k | vvc->s_info.recovery_point_valid = 0; |
12757 | 13.8k | return GF_TRUE; |
12758 | 13.8k | } |
12759 | 36.0k | return GF_FALSE; |
12760 | 49.9k | } |
12761 | 12.2k | return GF_TRUE; |
12762 | 62.1k | } |
12763 | | |
12764 | | |
12765 | | GF_EXPORT |
12766 | | GF_Err gf_vvc_change_vui(GF_VVCConfig *vvcc, GF_VUIInfo *vui_info) |
12767 | 0 | { |
12768 | 0 | GF_BitStream *orig, *mod; |
12769 | 0 | VVCState *vvc; |
12770 | 0 | u32 i, bit_offset, flag; |
12771 | 0 | s32 idx; |
12772 | 0 | GF_NALUFFParamArray *spss; |
12773 | 0 | GF_NALUFFParam *slc; |
12774 | 0 | orig = NULL; |
12775 | |
|
12776 | 0 | GF_SAFEALLOC(vvc, VVCState); |
12777 | 0 | if (!vvc) return GF_OUT_OF_MEM; |
12778 | 0 | vvc->sps_active_idx = -1; |
12779 | |
|
12780 | 0 | i = 0; |
12781 | 0 | spss = NULL; |
12782 | 0 | while ((spss = (GF_NALUFFParamArray *)gf_list_enum(vvcc->param_array, &i))) { |
12783 | 0 | if (spss->type == GF_VVC_NALU_SEQ_PARAM) |
12784 | 0 | break; |
12785 | 0 | spss = NULL; |
12786 | 0 | } |
12787 | 0 | if (!spss) { |
12788 | 0 | gf_free(vvc); |
12789 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
12790 | 0 | } |
12791 | | |
12792 | 0 | i = 0; |
12793 | 0 | while ((slc = (GF_NALUFFParam *)gf_list_enum(spss->nalus, &i))) { |
12794 | 0 | u8 *no_emulation_buf; |
12795 | 0 | u32 no_emulation_buf_size, emulation_bytes; |
12796 | 0 | u8 nal_unit_type, temporal_id, layer_id; |
12797 | | |
12798 | | /*SPS may still contains emulation bytes*/ |
12799 | 0 | no_emulation_buf = gf_malloc((slc->size) * sizeof(char)); |
12800 | 0 | no_emulation_buf_size = gf_media_nalu_remove_emulation_bytes(slc->data, no_emulation_buf, slc->size); |
12801 | |
|
12802 | 0 | orig = gf_bs_new(no_emulation_buf, no_emulation_buf_size, GF_BITSTREAM_READ); |
12803 | 0 | vvc_parse_nal_header(orig, &nal_unit_type, &temporal_id, &layer_id); |
12804 | 0 | idx = gf_vvc_read_sps_bs_internal(orig, vvc, layer_id, &bit_offset); |
12805 | |
|
12806 | 0 | if (idx < 0) { |
12807 | 0 | if (orig) |
12808 | 0 | gf_bs_del(orig); |
12809 | 0 | gf_free(no_emulation_buf); |
12810 | 0 | gf_bs_del(orig); |
12811 | 0 | continue; |
12812 | 0 | } |
12813 | | |
12814 | 0 | gf_bs_seek(orig, 0); |
12815 | 0 | mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE); |
12816 | | |
12817 | | /*copy over till vui flag*/ |
12818 | 0 | while (bit_offset) { |
12819 | 0 | flag = gf_bs_read_int(orig, 1); |
12820 | 0 | gf_bs_write_int(mod, flag, 1); |
12821 | 0 | bit_offset--; |
12822 | 0 | } |
12823 | |
|
12824 | 0 | avc_hevc_vvc_rewrite_vui(vui_info, orig, mod, GF_CODECID_VVC); |
12825 | | |
12826 | | /*finally copy over remaining*/ |
12827 | 0 | while (gf_bs_bits_available(orig)) { |
12828 | 0 | flag = gf_bs_read_int(orig, 1); |
12829 | 0 | gf_bs_write_int(mod, flag, 1); |
12830 | 0 | } |
12831 | 0 | gf_bs_del(orig); |
12832 | 0 | orig = NULL; |
12833 | 0 | gf_free(no_emulation_buf); |
12834 | | |
12835 | | /*set anti-emulation*/ |
12836 | 0 | gf_bs_get_content(mod, &no_emulation_buf, &no_emulation_buf_size); |
12837 | 0 | emulation_bytes = gf_media_nalu_emulation_bytes_add_count(no_emulation_buf, no_emulation_buf_size); |
12838 | 0 | if (no_emulation_buf_size + emulation_bytes > slc->size) |
12839 | 0 | slc->data = (char*)gf_realloc(slc->data, no_emulation_buf_size + emulation_bytes); |
12840 | |
|
12841 | 0 | slc->size = gf_media_nalu_add_emulation_bytes(no_emulation_buf, slc->data, no_emulation_buf_size); |
12842 | |
|
12843 | 0 | gf_bs_del(mod); |
12844 | 0 | gf_free(no_emulation_buf); |
12845 | 0 | } |
12846 | 0 | gf_free(vvc); |
12847 | 0 | return GF_OK; |
12848 | 0 | } |
12849 | | |
12850 | | |
12851 | | GF_EXPORT |
12852 | | GF_Err gf_vvc_change_par(GF_VVCConfig *vvcc, s32 ar_n, s32 ar_d) |
12853 | 0 | { |
12854 | 0 | GF_VUIInfo vuii; |
12855 | 0 | memset(&vuii, 0, sizeof(GF_VUIInfo)); |
12856 | 0 | vuii.ar_num = ar_n; |
12857 | 0 | vuii.ar_den = ar_d; |
12858 | 0 | vuii.fullrange = -1; |
12859 | 0 | vuii.video_format = -1; |
12860 | 0 | vuii.color_prim = -1; |
12861 | 0 | vuii.color_tfc = -1; |
12862 | 0 | vuii.color_matrix = -1; |
12863 | 0 | return gf_vvc_change_vui(vvcc, &vuii); |
12864 | 0 | } |
12865 | | |
12866 | | GF_EXPORT |
12867 | | GF_Err gf_vvc_change_color(GF_VVCConfig *vvcc, s32 fullrange, s32 vidformat, s32 colorprim, s32 transfer, s32 colmatrix) |
12868 | 0 | { |
12869 | 0 | GF_VUIInfo vuii; |
12870 | 0 | memset(&vuii, 0, sizeof(GF_VUIInfo)); |
12871 | 0 | vuii.ar_num = -1; |
12872 | 0 | vuii.ar_den = -1; |
12873 | 0 | vuii.fullrange = fullrange; |
12874 | 0 | vuii.video_format = vidformat; |
12875 | 0 | vuii.color_prim = colorprim; |
12876 | 0 | vuii.color_tfc = transfer; |
12877 | 0 | vuii.color_matrix = colmatrix; |
12878 | 0 | return gf_vvc_change_vui(vvcc, &vuii); |
12879 | 0 | } |
12880 | | |
12881 | | |
12882 | | GF_EXPORT |
12883 | | GF_Err gf_vvc_get_sps_info(u8 *sps_data, u32 sps_size, u32 *sps_id, u32 *width, u32 *height, s32 *par_n, s32 *par_d) |
12884 | 0 | { |
12885 | 0 | VVCState *vvc; |
12886 | 0 | s32 idx; |
12887 | 0 | GF_SAFEALLOC(vvc, VVCState); |
12888 | 0 | if (!vvc) return GF_OUT_OF_MEM; |
12889 | 0 | vvc->sps_active_idx = -1; |
12890 | |
|
12891 | 0 | GF_BitStream *bs = gf_bs_new(sps_data, sps_size, GF_BITSTREAM_READ); |
12892 | 0 | gf_bs_enable_emulation_byte_removal(bs, GF_TRUE); |
12893 | |
|
12894 | 0 | u8 nal_unit_type, temporal_id, layer_id; |
12895 | |
|
12896 | 0 | vvc_parse_nal_header(bs, &nal_unit_type, &temporal_id, &layer_id); |
12897 | 0 | idx = gf_vvc_read_sps_bs_internal(bs, vvc, layer_id, NULL); |
12898 | 0 | gf_bs_del(bs); |
12899 | 0 | if (idx < 0) { |
12900 | 0 | gf_free(vvc); |
12901 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
12902 | 0 | } |
12903 | | |
12904 | 0 | if (sps_id) *sps_id = idx; |
12905 | |
|
12906 | 0 | if (width) *width = vvc->sps[idx].width; |
12907 | 0 | if (height) *height = vvc->sps[idx].height; |
12908 | 0 | if (par_n) *par_n = vvc->sps[idx].aspect_ratio_info_present_flag ? vvc->sps[idx].sar_width : (u32)-1; |
12909 | 0 | if (par_d) *par_d = vvc->sps[idx].aspect_ratio_info_present_flag ? vvc->sps[idx].sar_height : (u32)-1; |
12910 | 0 | gf_free(vvc); |
12911 | 0 | return GF_OK; |
12912 | 0 | } |
12913 | | |
12914 | | |
12915 | | GF_EXPORT |
12916 | | const char *gf_vvc_get_profile_name(u8 video_prof) |
12917 | 0 | { |
12918 | 0 | switch (video_prof) { |
12919 | 0 | case 1: |
12920 | 0 | return "Main 10"; |
12921 | 0 | case 65: |
12922 | 0 | return "Main 10 Still Picture"; |
12923 | 0 | case 17: |
12924 | 0 | return "Multilayer Main 10"; |
12925 | 0 | case 2: |
12926 | 0 | return "Main 12"; |
12927 | 0 | case 10: |
12928 | 0 | return "Main 12 Intra"; |
12929 | 0 | case 66: |
12930 | 0 | return "Main 12 Still Picture"; |
12931 | 0 | case 34: |
12932 | 0 | return "Main 12 4:4:4"; |
12933 | 0 | case 42: |
12934 | 0 | return "Main 12 4:4:4 Intra"; |
12935 | 0 | case 98: |
12936 | 0 | return "Main 12 4:4:4 Still Picture"; |
12937 | 0 | case 36: |
12938 | 0 | return "Main 16 4:4:4"; |
12939 | 0 | case 44: |
12940 | 0 | return "Main 16 4:4:4 Intra"; |
12941 | 0 | case 100: |
12942 | 0 | return "Main 16 4:4:4 Still Picture"; |
12943 | 0 | default: |
12944 | 0 | return "Unknown"; |
12945 | 0 | } |
12946 | 0 | } |
12947 | | |
12948 | | |
12949 | | GF_Err gf_media_vc1_seq_header_to_dsi(const u8 *seq_hdr, u32 seq_hdr_len, u8 **dsi, u32 *dsi_size) |
12950 | 0 | { |
12951 | 0 | GF_BitStream *bs; |
12952 | 0 | u8 level=0, interlace=0; |
12953 | 0 | u8 profile=12; |
12954 | 0 | u8 *sqhdr = memchr(seq_hdr+1, 0x0F, seq_hdr_len); |
12955 | 0 | if (sqhdr) { |
12956 | 0 | u32 skip = (u32) (sqhdr - seq_hdr - 3); |
12957 | 0 | seq_hdr+=skip; |
12958 | 0 | seq_hdr_len-=skip; |
12959 | 0 | bs = gf_bs_new(seq_hdr+4, seq_hdr_len-4, GF_BITSTREAM_READ); |
12960 | 0 | profile = gf_bs_read_int(bs, 2); |
12961 | 0 | if (profile==3) { |
12962 | 0 | level = gf_bs_read_int(bs, 3); |
12963 | 0 | /*cfmt*/gf_bs_read_int(bs, 2); |
12964 | 0 | /*fps*/gf_bs_read_int(bs, 3); |
12965 | 0 | /*btrt*/gf_bs_read_int(bs, 5); |
12966 | 0 | gf_bs_read_int(bs, 1); |
12967 | 0 | /*mw*/gf_bs_read_int(bs, 12); |
12968 | 0 | /*mh*/gf_bs_read_int(bs, 12); |
12969 | 0 | /*bcast*/gf_bs_read_int(bs, 1); |
12970 | 0 | interlace = gf_bs_read_int(bs, 1); |
12971 | 0 | } |
12972 | 0 | gf_bs_del(bs); |
12973 | 0 | } |
12974 | 0 | *dsi_size = seq_hdr_len+7; |
12975 | 0 | *dsi = gf_malloc(seq_hdr_len+7); |
12976 | 0 | if (! (*dsi) ) return GF_OUT_OF_MEM; |
12977 | | |
12978 | 0 | bs = gf_bs_new(*dsi, *dsi_size, GF_BITSTREAM_WRITE); |
12979 | 0 | gf_bs_write_int(bs, 12, 4); //profile |
12980 | 0 | gf_bs_write_int(bs, level, 3); //level |
12981 | 0 | gf_bs_write_int(bs, 0, 1); //reserved |
12982 | 0 | gf_bs_write_int(bs, level, 3); //level |
12983 | 0 | gf_bs_write_int(bs, 0, 1); //cbr |
12984 | 0 | gf_bs_write_int(bs, 0, 6); //reserved |
12985 | 0 | gf_bs_write_int(bs, !interlace, 1); //no interlace |
12986 | 0 | gf_bs_write_int(bs, 1, 1); //no multiple seq |
12987 | 0 | gf_bs_write_int(bs, 1, 1); //no multiple entry |
12988 | 0 | gf_bs_write_int(bs, 1, 1); //no slice present |
12989 | 0 | gf_bs_write_int(bs, 0, 1); //no b-frames |
12990 | 0 | gf_bs_write_int(bs, 0, 1); //reserved |
12991 | 0 | gf_bs_write_u32(bs, 0xFFFFFFFF); //framerate |
12992 | 0 | gf_bs_write_data(bs, seq_hdr, seq_hdr_len); //VOS |
12993 | 0 | gf_bs_del(bs); |
12994 | 0 | return GF_OK; |
12995 | 0 | } |
12996 | | |
12997 | | void gf_hevc_parse_ps(GF_HEVCConfig* hevccfg, HEVCState* hevc, u32 nal_type) |
12998 | 0 | { |
12999 | 0 | u32 i, j; |
13000 | 0 | if (!hevccfg) return; |
13001 | | |
13002 | 0 | for (i = 0; i < gf_list_count(hevccfg->param_array); i++) { |
13003 | 0 | GF_NALUFFParamArray* ar = gf_list_get(hevccfg->param_array, i); |
13004 | 0 | if (ar->type != nal_type) continue; |
13005 | 0 | for (j = 0; j < gf_list_count(ar->nalus); j++) { |
13006 | 0 | u8 ntype, tid, lid; |
13007 | 0 | GF_NALUFFParam* sl = gf_list_get(ar->nalus, j); |
13008 | 0 | gf_hevc_parse_nalu(sl->data, sl->size, hevc, &ntype, &tid, &lid); |
13009 | 0 | } |
13010 | 0 | } |
13011 | 0 | } |
13012 | | |
13013 | | void gf_vvc_parse_ps(GF_VVCConfig* vvccfg, VVCState* vvc, u32 nal_type) |
13014 | 0 | { |
13015 | 0 | u32 i, j; |
13016 | 0 | if (!vvccfg) return; |
13017 | | |
13018 | 0 | for (i = 0; i < gf_list_count(vvccfg->param_array); i++) { |
13019 | 0 | GF_NALUFFParamArray* ar = gf_list_get(vvccfg->param_array, i); |
13020 | 0 | if (ar->type != nal_type) continue; |
13021 | 0 | for (j = 0; j < gf_list_count(ar->nalus); j++) { |
13022 | 0 | u8 ntype, tid, lid; |
13023 | 0 | GF_NALUFFParam* sl = gf_list_get(ar->nalus, j); |
13024 | 0 | gf_vvc_parse_nalu(sl->data, sl->size, vvc, &ntype, &tid, &lid); |
13025 | 0 | } |
13026 | 0 | } |
13027 | 0 | } |
13028 | | |
13029 | | #endif /*GPAC_DISABLE_AV_PARSERS*/ |