1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Jean Le Feuvre, Romain Bouqueau, Cyril Concolato
5  *			Copyright (c) Telecom ParisTech 2000-2020
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 static const struct {
37 	u32 w, h;
38 } std_par[] =
39 {
40 	{ 4, 3}, {3, 2}, {16, 9}, {5, 3}, {5, 4}, {8, 5}, {2, 1}, {1, 1},
41 	{0, 0},
42 };
43 
44 GF_EXPORT
gf_media_reduce_aspect_ratio(u32 * width,u32 * height)45 void gf_media_reduce_aspect_ratio(u32 *width, u32 *height)
46 {
47 	u32 i = 0;
48 	u32 w = *width;
49 	u32 h = *height;
50 	while (std_par[i].w) {
51 		if (std_par[i].w * h == std_par[i].h * w) {
52 			*width = std_par[i].w;
53 			*height = std_par[i].h;
54 			return;
55 		}
56 		i++;
57 	}
58 }
59 
60 GF_EXPORT
gf_media_get_reduced_frame_rate(u32 * timescale,u32 * sample_dur)61 void gf_media_get_reduced_frame_rate(u32 *timescale, u32 *sample_dur)
62 {
63 	u32 res;
64 	if (!*sample_dur) return;
65 	res = *timescale / *sample_dur;
66 	if (res * (*sample_dur) == *timescale) {
67 		*timescale = res;
68 		*sample_dur = 1;
69 	}
70 	else if ((double)(*timescale * 1001 - (res + 1) * *sample_dur * 1000) / ((res + 1) * *sample_dur * 1000) < 0.001) {
71 		*timescale = (res + 1) * 1000;
72 		*sample_dur = 1001;
73 	}
74 }
75 
76 GF_EXPORT
gf_m4v_get_profile_name(u8 video_pl)77 const char *gf_m4v_get_profile_name(u8 video_pl)
78 {
79 	switch (video_pl) {
80 	case 0x00:
81 		return "Reserved (0x00) Profile";
82 	case 0x01:
83 		return "Simple Profile @ Level 1";
84 	case 0x02:
85 		return "Simple Profile @ Level 2";
86 	case 0x03:
87 		return "Simple Profile @ Level 3";
88 	case 0x08:
89 		return "Simple Profile @ Level 0";
90 	case 0x10:
91 		return "Simple Scalable Profile @ Level 0";
92 	case 0x11:
93 		return "Simple Scalable Profile @ Level 1";
94 	case 0x12:
95 		return "Simple Scalable Profile @ Level 2";
96 	case 0x21:
97 		return "Core Profile @ Level 1";
98 	case 0x22:
99 		return "Core Profile @ Level 2";
100 	case 0x32:
101 		return "Main Profile @ Level 2";
102 	case 0x33:
103 		return "Main Profile @ Level 3";
104 	case 0x34:
105 		return "Main Profile @ Level 4";
106 	case 0x42:
107 		return "N-bit Profile @ Level 2";
108 	case 0x51:
109 		return "Scalable Texture Profile @ Level 1";
110 	case 0x61:
111 		return "Simple Face Animation Profile @ Level 1";
112 	case 0x62:
113 		return "Simple Face Animation Profile @ Level 2";
114 	case 0x63:
115 		return "Simple FBA Profile @ Level 1";
116 	case 0x64:
117 		return "Simple FBA Profile @ Level 2";
118 	case 0x71:
119 		return "Basic Animated Texture Profile @ Level 1";
120 	case 0x72:
121 		return "Basic Animated Texture Profile @ Level 2";
122 	case 0x7F:
123 		return "AVC/H264 Profile";
124 	case 0x81:
125 		return "Hybrid Profile @ Level 1";
126 	case 0x82:
127 		return "Hybrid Profile @ Level 2";
128 	case 0x91:
129 		return "Advanced Real Time Simple Profile @ Level 1";
130 	case 0x92:
131 		return "Advanced Real Time Simple Profile @ Level 2";
132 	case 0x93:
133 		return "Advanced Real Time Simple Profile @ Level 3";
134 	case 0x94:
135 		return "Advanced Real Time Simple Profile @ Level 4";
136 	case 0xA1:
137 		return "Core Scalable Profile @ Level1";
138 	case 0xA2:
139 		return "Core Scalable Profile @ Level2";
140 	case 0xA3:
141 		return "Core Scalable Profile @ Level3";
142 	case 0xB1:
143 		return "Advanced Coding Efficiency Profile @ Level 1";
144 	case 0xB2:
145 		return "Advanced Coding Efficiency Profile @ Level 2";
146 	case 0xB3:
147 		return "Advanced Coding Efficiency Profile @ Level 3";
148 	case 0xB4:
149 		return "Advanced Coding Efficiency Profile @ Level 4";
150 	case 0xC1:
151 		return "Advanced Core Profile @ Level 1";
152 	case 0xC2:
153 		return "Advanced Core Profile @ Level 2";
154 	case 0xD1:
155 		return "Advanced Scalable Texture @ Level1";
156 	case 0xD2:
157 		return "Advanced Scalable Texture @ Level2";
158 	case 0xE1:
159 		return "Simple Studio Profile @ Level 1";
160 	case 0xE2:
161 		return "Simple Studio Profile @ Level 2";
162 	case 0xE3:
163 		return "Simple Studio Profile @ Level 3";
164 	case 0xE4:
165 		return "Simple Studio Profile @ Level 4";
166 	case 0xE5:
167 		return "Core Studio Profile @ Level 1";
168 	case 0xE6:
169 		return "Core Studio Profile @ Level 2";
170 	case 0xE7:
171 		return "Core Studio Profile @ Level 3";
172 	case 0xE8:
173 		return "Core Studio Profile @ Level 4";
174 	case 0xF0:
175 		return "Advanced Simple Profile @ Level 0";
176 	case 0xF1:
177 		return "Advanced Simple Profile @ Level 1";
178 	case 0xF2:
179 		return "Advanced Simple Profile @ Level 2";
180 	case 0xF3:
181 		return "Advanced Simple Profile @ Level 3";
182 	case 0xF4:
183 		return "Advanced Simple Profile @ Level 4";
184 	case 0xF5:
185 		return "Advanced Simple Profile @ Level 5";
186 	case 0xF7:
187 		return "Advanced Simple Profile @ Level 3b";
188 	case 0xF8:
189 		return "Fine Granularity Scalable Profile @ Level 0";
190 	case 0xF9:
191 		return "Fine Granularity Scalable Profile @ Level 1";
192 	case 0xFA:
193 		return "Fine Granularity Scalable Profile @ Level 2";
194 	case 0xFB:
195 		return "Fine Granularity Scalable Profile @ Level 3";
196 	case 0xFC:
197 		return "Fine Granularity Scalable Profile @ Level 4";
198 	case 0xFD:
199 		return "Fine Granularity Scalable Profile @ Level 5";
200 	case 0xFE:
201 		return "Not part of MPEG-4 Visual profiles";
202 	case 0xFF:
203 		return "No visual capability required";
204 	default:
205 		return "ISO Reserved Profile";
206 	}
207 }
208 
209 
210 #ifndef GPAC_DISABLE_AV_PARSERS
211 
212 #define MPEG12_START_CODE_PREFIX		0x000001
213 #define MPEG12_PICTURE_START_CODE		0x00000100
214 #define MPEG12_SLICE_MIN_START			0x00000101
215 #define MPEG12_SLICE_MAX_START			0x000001af
216 #define MPEG12_USER_DATA_START_CODE		0x000001b2
217 #define MPEG12_SEQUENCE_START_CODE		0x000001b3
218 #define MPEG12_SEQUENCE_ERR_START_CODE	0x000001b4
219 #define MPEG12_EXT_START_CODE			0x000001b5
220 #define MPEG12_SEQUENCE_END_START_CODE	0x000001b7
221 #define MPEG12_GOP_START_CODE			0x000001b8
222 
gf_mv12_next_start_code(unsigned char * pbuffer,u32 buflen,u32 * optr,u32 * scode)223 s32 gf_mv12_next_start_code(unsigned char *pbuffer, u32 buflen, u32 *optr, u32 *scode)
224 {
225 	u32 value;
226 	u32 offset;
227 
228 	if (buflen < 4) return -1;
229 	for (offset = 0; offset < buflen - 3; offset++, pbuffer++) {
230 #ifdef GPAC_BIG_ENDIAN
231 		value = *(u32 *)pbuffer >> 8;
232 #else
233 		value = (pbuffer[0] << 16) | (pbuffer[1] << 8) | (pbuffer[2] << 0);
234 #endif
235 
236 		if (value == MPEG12_START_CODE_PREFIX) {
237 			*optr = offset;
238 			*scode = (value << 8) | pbuffer[3];
239 			return 0;
240 		}
241 	}
242 	return -1;
243 }
244 
gf_mv12_next_slice_start(unsigned char * pbuffer,u32 startoffset,u32 buflen,u32 * slice_offset)245 s32 gf_mv12_next_slice_start(unsigned char *pbuffer, u32 startoffset, u32 buflen, u32 *slice_offset)
246 {
247 	u32 slicestart, code;
248 	while (gf_mv12_next_start_code(pbuffer + startoffset, buflen - startoffset, &slicestart, &code) >= 0) {
249 		if ((code >= MPEG12_SLICE_MIN_START) && (code <= MPEG12_SLICE_MAX_START)) {
250 			*slice_offset = slicestart + startoffset;
251 			return 0;
252 		}
253 		startoffset += slicestart + 4;
254 	}
255 	return -1;
256 }
257 
258 
259 /*
260 	MPEG-4 video (14496-2)
261 */
262 
263 struct __tag_m4v_parser
264 {
265 	GF_BitStream *bs;
266 	Bool mpeg12, step_mode;
267 	u32 current_object_type;
268 	u32 force_next_obj_type;
269 	u64 current_object_start;
270 	u32 tc_dec, prev_tc_dec, tc_disp, prev_tc_disp;
271 };
272 
273 GF_EXPORT
gf_m4v_parser_new(u8 * data,u64 data_size,Bool mpeg12video)274 GF_M4VParser *gf_m4v_parser_new(u8 *data, u64 data_size, Bool mpeg12video)
275 {
276 	GF_M4VParser *tmp;
277 	if (!data || !data_size) return NULL;
278 	GF_SAFEALLOC(tmp, GF_M4VParser);
279 	if (!tmp) return NULL;
280 	tmp->bs = gf_bs_new(data, data_size, GF_BITSTREAM_READ);
281 	tmp->mpeg12 = mpeg12video;
282 	return tmp;
283 }
284 
gf_m4v_parser_bs_new(GF_BitStream * bs,Bool mpeg12video)285 GF_M4VParser *gf_m4v_parser_bs_new(GF_BitStream *bs, Bool mpeg12video)
286 {
287 	GF_M4VParser *tmp;
288 	GF_SAFEALLOC(tmp, GF_M4VParser);
289 	if (!tmp) return NULL;
290 	tmp->bs = bs;
291 	tmp->mpeg12 = mpeg12video;
292 	return tmp;
293 }
294 
295 GF_EXPORT
gf_m4v_parser_del(GF_M4VParser * m4v)296 void gf_m4v_parser_del(GF_M4VParser *m4v)
297 {
298 	gf_bs_del(m4v->bs);
299 	gf_free(m4v);
300 }
301 
302 GF_EXPORT
gf_m4v_parser_del_no_bs(GF_M4VParser * m4v)303 void gf_m4v_parser_del_no_bs(GF_M4VParser *m4v)
304 {
305 	gf_free(m4v);
306 }
307 
308 GF_EXPORT
gf_m4v_parser_set_inspect(GF_M4VParser * m4v)309 void gf_m4v_parser_set_inspect(GF_M4VParser *m4v)
310 {
311 	if (m4v) m4v->step_mode = 1;
312 }
313 GF_EXPORT
gf_m4v_parser_get_obj_type(GF_M4VParser * m4v)314 u32 gf_m4v_parser_get_obj_type(GF_M4VParser *m4v)
315 {
316 	if (m4v) return m4v->current_object_type;
317 	return 0;
318 }
319 
320 #define M4V_CACHE_SIZE		4096
M4V_LoadObject(GF_M4VParser * m4v)321 s32 M4V_LoadObject(GF_M4VParser *m4v)
322 {
323 	u32 v, bpos, found;
324 	char m4v_cache[M4V_CACHE_SIZE];
325 	u64 end, cache_start, load_size;
326 	if (!m4v) return 0;
327 	if (m4v->force_next_obj_type) {
328 		m4v->current_object_type = m4v->force_next_obj_type - 1;
329 		m4v->force_next_obj_type = 0;
330 		return (s32)m4v->current_object_type;
331 	}
332 
333 	bpos = 0;
334 	found = 0;
335 	load_size = 0;
336 	end = 0;
337 	cache_start = 0;
338 	v = 0xffffffff;
339 	while (!end) {
340 		/*refill cache*/
341 		if (bpos == (u32)load_size) {
342 			if (!gf_bs_available(m4v->bs)) break;
343 			load_size = gf_bs_available(m4v->bs);
344 			if (load_size > M4V_CACHE_SIZE) load_size = M4V_CACHE_SIZE;
345 			bpos = 0;
346 			cache_start = gf_bs_get_position(m4v->bs);
347 			gf_bs_read_data(m4v->bs, m4v_cache, (u32)load_size);
348 		}
349 		v = ((v << 8) & 0xFFFFFF00) | ((u8)m4v_cache[bpos]);
350 		bpos++;
351 		if ((v & 0xFFFFFF00) == 0x00000100) {
352 			end = cache_start + bpos - 4;
353 			found = 1;
354 			break;
355 		}
356 	}
357 	if (!found) return -1;
358 	m4v->current_object_start = end;
359 	gf_bs_seek(m4v->bs, end + 3);
360 	m4v->current_object_type = gf_bs_read_u8(m4v->bs);
361 	return (s32)m4v->current_object_type;
362 }
363 
364 
365 GF_EXPORT
gf_m4v_rewrite_pl(u8 ** o_data,u32 * o_dataLen,u8 PL)366 void gf_m4v_rewrite_pl(u8 **o_data, u32 *o_dataLen, u8 PL)
367 {
368 	u32 pos = 0;
369 	unsigned char *data = (unsigned char *)*o_data;
370 	u32 dataLen = *o_dataLen;
371 
372 	while (pos + 4 < dataLen) {
373 		if (!data[pos] && !data[pos + 1] && (data[pos + 2] == 0x01) && (data[pos + 3] == M4V_VOS_START_CODE)) {
374 			data[pos + 4] = PL;
375 			return;
376 		}
377 		pos++;
378 	}
379 	/*emulate VOS at beggining*/
380 	(*o_data) = (char *)gf_malloc(sizeof(char)*(dataLen + 5));
381 	(*o_data)[0] = 0;
382 	(*o_data)[1] = 0;
383 	(*o_data)[2] = 1;
384 	(*o_data)[3] = (char)M4V_VOS_START_CODE;
385 	(*o_data)[4] = PL;
386 	memcpy((*o_data + 5), data, sizeof(char)*dataLen);
387 	gf_free(data);
388 	(*o_dataLen) = dataLen + 5;
389 }
390 
M4V_Reset(GF_M4VParser * m4v,u64 start)391 static GF_Err M4V_Reset(GF_M4VParser *m4v, u64 start)
392 {
393 	gf_bs_seek(m4v->bs, start);
394 
395 	assert(start < (u64)1<<31);
396 	m4v->current_object_start = (u32)start;
397 	m4v->current_object_type = 0;
398 	return GF_OK;
399 }
400 
gf_m4v_parser_reset(GF_M4VParser * m4v,u8 sc_type)401 void gf_m4v_parser_reset(GF_M4VParser *m4v, u8 sc_type)
402 {
403 	m4v->current_object_start = 0;
404 	m4v->current_object_type = 0;
405 	m4v->force_next_obj_type = sc_type;
406 }
gf_m4v_parse_config_mpeg12(GF_M4VParser * m4v,GF_M4VDecSpecInfo * dsi)407 static GF_Err gf_m4v_parse_config_mpeg12(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi)
408 {
409 	unsigned char p[4];
410 	u32 ext_type;
411 	s32 o_type;
412 	u8 go, par;
413 
414 	if (!m4v || !dsi) return GF_BAD_PARAM;
415 
416 	memset(dsi, 0, sizeof(GF_M4VDecSpecInfo));
417 	dsi->VideoPL = 0;
418 
419 	go = 1;
420 	while (go) {
421 		o_type = M4V_LoadObject(m4v);
422 		switch (o_type) {
423 		case M2V_SEQ_START_CODE:
424 			dsi->RAP_stream = 1;
425 			gf_bs_read_data(m4v->bs, (char *)p, 4);
426 			dsi->width = (p[0] << 4) | ((p[1] >> 4) & 0xf);
427 			dsi->height = ((p[1] & 0xf) << 8) | p[2];
428 
429 			dsi->VideoPL = GF_CODECID_MPEG1;
430 			par = (p[3] >> 4) & 0xf;
431 			switch (par) {
432 			case 2:
433 				dsi->par_num = dsi->height / 3;
434 				dsi->par_den = dsi->width / 4;
435 				break;
436 			case 3:
437 				dsi->par_num = dsi->height / 9;
438 				dsi->par_den = dsi->width / 16;
439 				break;
440 			case 4:
441 				dsi->par_num = dsi->height / 2;
442 				dsi->par_den = dsi->width / 21;
443 				break;
444 			default:
445 				dsi->par_den = dsi->par_num = 0;
446 				break;
447 			}
448 			switch (p[3] & 0xf) {
449 			case 0:
450 				break;
451 			case 1:
452 				dsi->fps = 24000.0 / 1001.0;
453 				break;
454 			case 2:
455 				dsi->fps = 24.0;
456 				break;
457 			case 3:
458 				dsi->fps = 25.0;
459 				break;
460 			case 4:
461 				dsi->fps = 30000.0 / 1001.0;
462 				break;
463 			case 5:
464 				dsi->fps = 30.0;
465 				break;
466 			case 6:
467 				dsi->fps = 50.0;
468 				break;
469 			case 7:
470 				dsi->fps = ((60.0*1000.0) / 1001.0);
471 				break;
472 			case 8:
473 				dsi->fps = 60.0;
474 				break;
475 			case 9:
476 				dsi->fps = 1;
477 				break;
478 			case 10:
479 				dsi->fps = 5;
480 				break;
481 			case 11:
482 				dsi->fps = 10;
483 				break;
484 			case 12:
485 				dsi->fps = 12;
486 				break;
487 			case 13:
488 				dsi->fps = 15;
489 				break;
490 			}
491 			break;
492 		case M2V_EXT_START_CODE:
493 			gf_bs_read_data(m4v->bs, (char *)p, 4);
494 			ext_type = ((p[0] >> 4) & 0xf);
495 			if (ext_type == 1) {
496 				dsi->VideoPL = 0x65;
497 				dsi->height = ((p[1] & 0x1) << 13) | ((p[2] & 0x80) << 5) | (dsi->height & 0x0fff);
498 				dsi->width = (((p[2] >> 5) & 0x3) << 12) | (dsi->width & 0x0fff);
499 			}
500 			break;
501 		case M2V_PIC_START_CODE:
502 			if (dsi->width) go = 0;
503 			break;
504 		default:
505 			break;
506 			/*EOS*/
507 		case -1:
508 			go = 0;
509 			m4v->current_object_start = gf_bs_get_position(m4v->bs);
510 			break;
511 		}
512 	}
513 	M4V_Reset(m4v, 0);
514 	return GF_OK;
515 }
516 
517 
518 static const struct {
519 	u32 w, h;
520 } m4v_sar[6] = { { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, { 40, 33 } };
521 
m4v_get_sar_idx(u32 w,u32 h)522 static u8 m4v_get_sar_idx(u32 w, u32 h)
523 {
524 	u32 i;
525 	for (i = 0; i < 6; i++) {
526 		if ((m4v_sar[i].w == w) && (m4v_sar[i].h == h)) return i;
527 	}
528 	return 0xF;
529 }
530 
gf_m4v_parse_vol(GF_M4VParser * m4v,GF_M4VDecSpecInfo * dsi)531 static void gf_m4v_parse_vol(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi)
532 {
533 	u8 verid, par;
534 	s32 clock_rate;
535 	u8 vpl = dsi->VideoPL;
536 
537 	memset(dsi, 0, sizeof(GF_M4VDecSpecInfo));
538 	dsi->VideoPL = vpl;
539 
540 	verid = 0;
541 	dsi->RAP_stream = gf_bs_read_int(m4v->bs, 1);
542 	dsi->objectType = gf_bs_read_int(m4v->bs, 8);
543 	if (gf_bs_read_int(m4v->bs, 1)) {
544 		verid = gf_bs_read_int(m4v->bs, 4);
545 		gf_bs_read_int(m4v->bs, 3);
546 	}
547 	par = gf_bs_read_int(m4v->bs, 4);
548 	if (par == 0xF) {
549 		dsi->par_num = gf_bs_read_int(m4v->bs, 8);
550 		dsi->par_den = gf_bs_read_int(m4v->bs, 8);
551 	} else if (par<6) {
552 		dsi->par_num = m4v_sar[par].w;
553 		dsi->par_den = m4v_sar[par].h;
554 	}
555 	if (gf_bs_read_int(m4v->bs, 1)) {
556 		gf_bs_read_int(m4v->bs, 3);
557 		if (gf_bs_read_int(m4v->bs, 1)) gf_bs_read_int(m4v->bs, 79);
558 	}
559 	dsi->has_shape = gf_bs_read_int(m4v->bs, 2);
560 	if (dsi->has_shape && (verid!=1) ) gf_bs_read_int(m4v->bs, 4);
561 	gf_bs_read_int(m4v->bs, 1);
562 	/*clock rate*/
563 	dsi->clock_rate = gf_bs_read_int(m4v->bs, 16);
564 	/*marker*/
565 	gf_bs_read_int(m4v->bs, 1);
566 
567 	clock_rate = dsi->clock_rate-1;
568 	if (clock_rate >= 65536) clock_rate = 65535;
569 	if (clock_rate > 0) {
570 		for (dsi->NumBitsTimeIncrement = 1; dsi->NumBitsTimeIncrement < 16; dsi->NumBitsTimeIncrement++)	{
571 			if (clock_rate == 1) break;
572 			clock_rate = (clock_rate >> 1);
573 		}
574 	} else {
575 		/*fix from vivien for divX*/
576 		dsi->NumBitsTimeIncrement = 1;
577 	}
578 	/*fixed FPS stream*/
579 	dsi->time_increment = 0;
580 	if (gf_bs_read_int(m4v->bs, 1)) {
581 		dsi->time_increment = gf_bs_read_int(m4v->bs, dsi->NumBitsTimeIncrement);
582 	}
583 	if (!dsi->has_shape) {
584 		gf_bs_read_int(m4v->bs, 1);
585 		dsi->width = gf_bs_read_int(m4v->bs, 13);
586 		gf_bs_read_int(m4v->bs, 1);
587 		dsi->height = gf_bs_read_int(m4v->bs, 13);
588 	} else {
589 		dsi->width = dsi->height = 0;
590 	}
591 
592 }
593 
gf_m4v_parse_config_mpeg4(GF_M4VParser * m4v,GF_M4VDecSpecInfo * dsi)594 static GF_Err gf_m4v_parse_config_mpeg4(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi)
595 {
596 	s32 o_type;
597 	u8 go;
598 
599 	if (!m4v || !dsi) return GF_BAD_PARAM;
600 
601 	memset(dsi, 0, sizeof(GF_M4VDecSpecInfo));
602 
603 	go = 1;
604 	while (go) {
605 		o_type = M4V_LoadObject(m4v);
606 		switch (o_type) {
607 			/*vosh*/
608 		case M4V_VOS_START_CODE:
609 			dsi->VideoPL = (u8)gf_bs_read_u8(m4v->bs);
610 			break;
611 
612 		case M4V_VOL_START_CODE:
613 			gf_m4v_parse_vol(m4v, dsi);
614 			/*shape will be done later*/
615 			gf_bs_align(m4v->bs);
616 			break;
617 
618 		case M4V_VOP_START_CODE:
619 		case M4V_GOV_START_CODE:
620 			go = 0;
621 			break;
622 			/*EOS*/
623 		case -1:
624 			m4v->current_object_start = gf_bs_get_position(m4v->bs);
625 			return GF_EOS;
626 			/*don't interest us*/
627 		case M4V_UDTA_START_CODE:
628 		default:
629 			break;
630 		}
631 	}
632 	return GF_OK;
633 }
634 
635 GF_EXPORT
gf_m4v_parse_config(GF_M4VParser * m4v,GF_M4VDecSpecInfo * dsi)636 GF_Err gf_m4v_parse_config(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi)
637 {
638 	if (m4v->mpeg12) {
639 		return gf_m4v_parse_config_mpeg12(m4v, dsi);
640 	}
641 	else {
642 		return gf_m4v_parse_config_mpeg4(m4v, dsi);
643 	}
644 }
645 
gf_m4v_parse_frame_mpeg12(GF_M4VParser * m4v,GF_M4VDecSpecInfo * dsi,u8 * frame_type,u32 * time_inc,u64 * size,u64 * start,Bool * is_coded)646 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)
647 {
648 	u8 go, hasVOP, firstObj, val;
649 	s32 o_type;
650 
651 	if (!m4v || !size || !start || !frame_type) return GF_BAD_PARAM;
652 
653 	*size = 0;
654 	firstObj = 1;
655 	hasVOP = 0;
656 	*is_coded = GF_FALSE;
657 	m4v->current_object_type = (u32)-1;
658 	*frame_type = 0;
659 
660 	if (!m4v->step_mode)
661 		M4V_Reset(m4v, m4v->current_object_start);
662 
663 	go = 1;
664 	while (go) {
665 		o_type = M4V_LoadObject(m4v);
666 		switch (o_type) {
667 		case M2V_PIC_START_CODE:
668 			/*done*/
669 			if (hasVOP) {
670 				go = 0;
671 				break;
672 			}
673 			if (firstObj) {
674 				*start = m4v->current_object_start;
675 				firstObj = 0;
676 			}
677 			hasVOP = 1;
678 			*is_coded = 1;
679 
680 			/*val = */gf_bs_read_u8(m4v->bs);
681 			val = gf_bs_read_u8(m4v->bs);
682 			*frame_type = ((val >> 3) & 0x7) - 1;
683 			break;
684 		case M2V_GOP_START_CODE:
685 			if (firstObj) {
686 				*start = m4v->current_object_start;
687 				firstObj = 0;
688 			}
689 			if (hasVOP) go = 0;
690 			break;
691 
692 		case M2V_SEQ_START_CODE:
693 			if (firstObj) {
694 				*start = m4v->current_object_start;
695 				firstObj = 0;
696 			}
697 			if (hasVOP) {
698 				go = 0;
699 				break;
700 			}
701 
702 			/**/
703 			break;
704 
705 		default:
706 			break;
707 
708 		case -1:
709 			*size = gf_bs_get_position(m4v->bs) - *start;
710 			return GF_EOS;
711 		}
712 		if (m4v->step_mode)
713 			return GF_OK;
714 	}
715 	*size = m4v->current_object_start - *start;
716 	return GF_OK;
717 }
718 
gf_m4v_parse_frame_mpeg4(GF_M4VParser * m4v,GF_M4VDecSpecInfo * dsi,u8 * frame_type,u32 * time_inc,u64 * size,u64 * start,Bool * is_coded)719 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)
720 {
721 	u8 go, hasVOP, firstObj, secs;
722 	s32 o_type;
723 	u32 vop_inc = 0;
724 
725 	if (!m4v || !size || !start || !frame_type) return GF_BAD_PARAM;
726 
727 	*size = 0;
728 	firstObj = 1;
729 	hasVOP = 0;
730 	*is_coded = 0;
731 	m4v->current_object_type = (u32)-1;
732 	*frame_type = 0;
733 	*start = 0;
734 
735 	if (!m4v->step_mode)
736 		M4V_Reset(m4v, m4v->current_object_start);
737 
738 	go = 1;
739 	while (go) {
740 		o_type = M4V_LoadObject(m4v);
741 		switch (o_type) {
742 		case M4V_VOP_START_CODE:
743 			/*done*/
744 			if (hasVOP) {
745 				go = 0;
746 				break;
747 			}
748 			if (firstObj) {
749 				*start = m4v->current_object_start;
750 				firstObj = 0;
751 			}
752 			hasVOP = 1;
753 
754 			/*coding type*/
755 			*frame_type = gf_bs_read_int(m4v->bs, 2);
756 			/*modulo time base*/
757 			secs = 0;
758 			while (gf_bs_read_int(m4v->bs, 1) != 0)
759 				secs++;
760 			/*no support for B frames in parsing*/
761 			secs += (dsi->enh_layer || *frame_type!=2) ? m4v->tc_dec : m4v->tc_disp;
762 			/*marker*/
763 			gf_bs_read_int(m4v->bs, 1);
764 			/*vop_time_inc*/
765 			if (dsi->NumBitsTimeIncrement)
766 				vop_inc = gf_bs_read_int(m4v->bs, dsi->NumBitsTimeIncrement);
767 
768 			m4v->prev_tc_dec = m4v->tc_dec;
769 			m4v->prev_tc_disp = m4v->tc_disp;
770 			if (dsi->enh_layer || *frame_type!=2) {
771 				m4v->tc_disp = m4v->tc_dec;
772 				m4v->tc_dec = secs;
773 			}
774 			*time_inc = secs * dsi->clock_rate + vop_inc;
775 			/*marker*/
776 			gf_bs_read_int(m4v->bs, 1);
777 			/*coded*/
778 			*is_coded = gf_bs_read_int(m4v->bs, 1);
779 			gf_bs_align(m4v->bs);
780 			break;
781 		case M4V_GOV_START_CODE:
782 			if (firstObj) {
783 				*start = m4v->current_object_start;
784 				firstObj = 0;
785 			}
786 			if (hasVOP) go = 0;
787 			break;
788 
789 		case M4V_VOL_START_CODE:
790 			if (m4v->step_mode)
791 				gf_m4v_parse_vol(m4v, dsi);
792 		case M4V_VOS_START_CODE:
793 			if (hasVOP) {
794 				go = 0;
795 			}
796 			else if (firstObj) {
797 				*start = m4v->current_object_start;
798 				firstObj = 0;
799 			}
800 			break;
801 
802 		case M4V_VO_START_CODE:
803 		default:
804 			break;
805 
806 		case -1:
807 			*size = gf_bs_get_position(m4v->bs) - *start;
808 			return GF_EOS;
809 		}
810 		if (m4v->step_mode)
811 			return GF_OK;
812 	}
813 	assert(m4v->current_object_start >= *start);
814 	*size = m4v->current_object_start - *start;
815 	return GF_OK;
816 }
817 
818 GF_EXPORT
gf_m4v_parse_frame(GF_M4VParser * m4v,GF_M4VDecSpecInfo * dsi,u8 * frame_type,u32 * time_inc,u64 * size,u64 * start,Bool * is_coded)819 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)
820 {
821 	if (m4v->mpeg12) {
822 		return gf_m4v_parse_frame_mpeg12(m4v, dsi, frame_type, time_inc, size, start, is_coded);
823 	}
824 	else {
825 		return gf_m4v_parse_frame_mpeg4(m4v, dsi, frame_type, time_inc, size, start, is_coded);
826 	}
827 }
828 
gf_m4v_rewrite_par(u8 ** o_data,u32 * o_dataLen,s32 par_n,s32 par_d)829 GF_Err gf_m4v_rewrite_par(u8 **o_data, u32 *o_dataLen, s32 par_n, s32 par_d)
830 {
831 	u64 start, end, size;
832 	GF_BitStream *mod;
833 	GF_M4VParser *m4v;
834 	Bool go = 1;
835 
836 	m4v = gf_m4v_parser_new(*o_data, *o_dataLen, 0);
837 	mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
838 
839 	start = 0;
840 	while (go) {
841 		u32 type = M4V_LoadObject(m4v);
842 
843 		end = gf_bs_get_position(m4v->bs) - 4;
844 		size = end - start;
845 		/*store previous object*/
846 		if (size) {
847 			assert (size < (u64)1<<31);
848 			gf_bs_write_data(mod, *o_data + start, (u32)size);
849 			start = end;
850 		}
851 
852 		switch (type) {
853 		case M4V_VOL_START_CODE:
854 			gf_bs_write_int(mod, 0, 8);
855 			gf_bs_write_int(mod, 0, 8);
856 			gf_bs_write_int(mod, 1, 8);
857 			gf_bs_write_int(mod, M4V_VOL_START_CODE, 8);
858 			gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 1), 1);
859 			gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 8), 8);
860 			start = gf_bs_read_int(m4v->bs, 1);
861 			gf_bs_write_int(mod, (u32)start, 1);
862 			if (start) {
863 				gf_bs_write_int(mod, gf_bs_read_int(m4v->bs, 7), 7);
864 			}
865 			start = gf_bs_read_int(m4v->bs, 4);
866 			if (start == 0xF) {
867 				gf_bs_read_int(m4v->bs, 8);
868 				gf_bs_read_int(m4v->bs, 8);
869 			}
870 			if ((par_n >= 0) && (par_d >= 0)) {
871 				u8 par = m4v_get_sar_idx(par_n, par_d);
872 				gf_bs_write_int(mod, par, 4);
873 				if (par == 0xF) {
874 					gf_bs_write_int(mod, par_n, 8);
875 					gf_bs_write_int(mod, par_d, 8);
876 				}
877 			}
878 			else {
879 				gf_bs_write_int(mod, 0x0, 4);
880 			}
881 		case -1:
882 			go = 0;
883 			break;
884 		default:
885 			break;
886 		}
887 	}
888 	while (gf_bs_bits_available(m4v->bs)) {
889 		u32 b = gf_bs_read_int(m4v->bs, 1);
890 		gf_bs_write_int(mod, b, 1);
891 	}
892 
893 	gf_m4v_parser_del(m4v);
894 	gf_free(*o_data);
895 	gf_bs_get_content(mod, o_data, o_dataLen);
896 	gf_bs_del(mod);
897 	return GF_OK;
898 }
899 
900 GF_EXPORT
gf_m4v_get_object_start(GF_M4VParser * m4v)901 u64 gf_m4v_get_object_start(GF_M4VParser *m4v)
902 {
903 	return m4v->current_object_start;
904 }
905 
906 #if 0 //unused
907 Bool gf_m4v_is_valid_object_type(GF_M4VParser *m4v)
908 {
909 	return ((s32)m4v->current_object_type == -1) ? 0 : 1;
910 }
911 #endif
912 
913 
914 GF_EXPORT
gf_m4v_get_config(u8 * rawdsi,u32 rawdsi_size,GF_M4VDecSpecInfo * dsi)915 GF_Err gf_m4v_get_config(u8 *rawdsi, u32 rawdsi_size, GF_M4VDecSpecInfo *dsi)
916 {
917 	GF_Err e;
918 	GF_M4VParser *vparse;
919 	if (!rawdsi || !rawdsi_size) return GF_NON_COMPLIANT_BITSTREAM;
920 	vparse = gf_m4v_parser_new(rawdsi, rawdsi_size, 0);
921 	e = gf_m4v_parse_config(vparse, dsi);
922 	dsi->next_object_start = (u32)vparse->current_object_start;
923 	gf_m4v_parser_del(vparse);
924 	return e < 0 ? e : GF_OK;
925 }
926 
927 GF_EXPORT
gf_mpegv12_get_config(u8 * rawdsi,u32 rawdsi_size,GF_M4VDecSpecInfo * dsi)928 GF_Err gf_mpegv12_get_config(u8 *rawdsi, u32 rawdsi_size, GF_M4VDecSpecInfo *dsi)
929 {
930 	GF_Err e;
931 	GF_M4VParser *vparse;
932 	if (!rawdsi || !rawdsi_size) return GF_NON_COMPLIANT_BITSTREAM;
933 	vparse = gf_m4v_parser_new(rawdsi, rawdsi_size, GF_TRUE);
934 	e = gf_m4v_parse_config(vparse, dsi);
935 	dsi->next_object_start = (u32)vparse->current_object_start;
936 	gf_m4v_parser_del(vparse);
937 	return e;
938 }
939 
940 #endif
941 
942 
943 /*
944 	AAC parser
945 */
946 
947 GF_EXPORT
gf_m4a_object_type_name(u32 objectType)948 const char *gf_m4a_object_type_name(u32 objectType)
949 {
950 	switch (objectType) {
951 	case 0:
952 		return "MPEG-4 Audio Reserved";
953 	case 1:
954 		return "MPEG-4 Audio AAC Main";
955 	case 2:
956 		return "MPEG-4 Audio AAC LC";
957 	case 3:
958 		return "MPEG-4 Audio AAC SSR";
959 	case 4:
960 		return "MPEG-4 Audio AAC LTP";
961 	case 5:
962 		return "MPEG-4 Audio SBR";
963 	case 6:
964 		return "MPEG-4 Audio AAC Scalable";
965 	case 7:
966 		return "MPEG-4 Audio TwinVQ";
967 	case 8:
968 		return "MPEG-4 Audio CELP";
969 	case 9:
970 		return "MPEG-4 Audio HVXC";
971 	case 10:
972 		return "MPEG-4 Audio Reserved";
973 	case 11:
974 		return "MPEG-4 Audio Reserved";
975 	case 12:
976 		return "MPEG-4 Audio TTSI";
977 	case 13:
978 		return "MPEG-4 Audio Main synthetic";
979 	case 14:
980 		return "MPEG-4 Audio Wavetable synthesis";
981 	case 15:
982 		return "MPEG-4 Audio General MIDI";
983 	case 16:
984 		return "MPEG-4 Audio Algorithmic Synthesis and Audio FX";
985 	case 17:
986 		return "MPEG-4 Audio ER AAC LC";
987 	case 18:
988 		return "MPEG-4 Audio Reserved";
989 	case 19:
990 		return "MPEG-4 Audio ER AAC LTP";
991 	case 20:
992 		return "MPEG-4 Audio ER AAC scalable";
993 	case 21:
994 		return "MPEG-4 Audio ER TwinVQ";
995 	case 22:
996 		return "MPEG-4 Audio ER BSAC";
997 	case 23:
998 		return "MPEG-4 Audio ER AAC LD";
999 	case 24:
1000 		return "MPEG-4 Audio ER CELP";
1001 	case 25:
1002 		return "MPEG-4 Audio ER HVXC";
1003 	case 26:
1004 		return "MPEG-4 Audio ER HILN";
1005 	case 27:
1006 		return "MPEG-4 Audio ER Parametric";
1007 	case 28:
1008 		return "MPEG-4 Audio SSC";
1009 	case 29:
1010 		return "MPEG-4 Audio ParametricStereo";
1011 	case 30:
1012 		return "MPEG-4 Audio Reserved";
1013 	case 31:
1014 		return "MPEG-4 Audio Reserved";
1015 	case 32:
1016 		return "MPEG-1 Audio Layer-1";
1017 	case 33:
1018 		return "MPEG-1 Audio Layer-2";
1019 	case 34:
1020 		return "MPEG-1 Audio Layer-3";
1021 	case 35:
1022 		return "MPEG-4 Audio DST";
1023 	case 36:
1024 		return "MPEG-4 Audio ALS";
1025 	default:
1026 		return "MPEG-4 Audio Unknown";
1027 	}
1028 }
1029 
1030 GF_EXPORT
gf_m4a_get_profile_name(u8 audio_pl)1031 const char *gf_m4a_get_profile_name(u8 audio_pl)
1032 {
1033 	switch (audio_pl) {
1034 	case 0x00:
1035 		return "ISO Reserved (0x00)";
1036 	case 0x01:
1037 		return "Main Audio Profile @ Level 1";
1038 	case 0x02:
1039 		return "Main Audio Profile @ Level 2";
1040 	case 0x03:
1041 		return "Main Audio Profile @ Level 3";
1042 	case 0x04:
1043 		return "Main Audio Profile @ Level 4";
1044 	case 0x05:
1045 		return "Scalable Audio Profile @ Level 1";
1046 	case 0x06:
1047 		return "Scalable Audio Profile @ Level 2";
1048 	case 0x07:
1049 		return "Scalable Audio Profile @ Level 3";
1050 	case 0x08:
1051 		return "Scalable Audio Profile @ Level 4";
1052 	case 0x09:
1053 		return "Speech Audio Profile @ Level 1";
1054 	case 0x0A:
1055 		return "Speech Audio Profile @ Level 2";
1056 	case 0x0B:
1057 		return "Synthetic Audio Profile @ Level 1";
1058 	case 0x0C:
1059 		return "Synthetic Audio Profile @ Level 2";
1060 	case 0x0D:
1061 		return "Synthetic Audio Profile @ Level 3";
1062 	case 0x0E:
1063 		return "High Quality Audio Profile @ Level 1";
1064 	case 0x0F:
1065 		return "High Quality Audio Profile @ Level 2";
1066 	case 0x10:
1067 		return "High Quality Audio Profile @ Level 3";
1068 	case 0x11:
1069 		return "High Quality Audio Profile @ Level 4";
1070 	case 0x12:
1071 		return "High Quality Audio Profile @ Level 5";
1072 	case 0x13:
1073 		return "High Quality Audio Profile @ Level 6";
1074 	case 0x14:
1075 		return "High Quality Audio Profile @ Level 7";
1076 	case 0x15:
1077 		return "High Quality Audio Profile @ Level 8";
1078 	case 0x16:
1079 		return "Low Delay Audio Profile @ Level 1";
1080 	case 0x17:
1081 		return "Low Delay Audio Profile @ Level 2";
1082 	case 0x18:
1083 		return "Low Delay Audio Profile @ Level 3";
1084 	case 0x19:
1085 		return "Low Delay Audio Profile @ Level 4";
1086 	case 0x1A:
1087 		return "Low Delay Audio Profile @ Level 5";
1088 	case 0x1B:
1089 		return "Low Delay Audio Profile @ Level 6";
1090 	case 0x1C:
1091 		return "Low Delay Audio Profile @ Level 7";
1092 	case 0x1D:
1093 		return "Low Delay Audio Profile @ Level 8";
1094 	case 0x1E:
1095 		return "Natural Audio Profile @ Level 1";
1096 	case 0x1F:
1097 		return "Natural Audio Profile @ Level 2";
1098 	case 0x20:
1099 		return "Natural Audio Profile @ Level 3";
1100 	case 0x21:
1101 		return "Natural Audio Profile @ Level 4";
1102 	case 0x22:
1103 		return "Mobile Audio Internetworking Profile @ Level 1";
1104 	case 0x23:
1105 		return "Mobile Audio Internetworking Profile @ Level 2";
1106 	case 0x24:
1107 		return "Mobile Audio Internetworking Profile @ Level 3";
1108 	case 0x25:
1109 		return "Mobile Audio Internetworking Profile @ Level 4";
1110 	case 0x26:
1111 		return "Mobile Audio Internetworking Profile @ Level 5";
1112 	case 0x27:
1113 		return "Mobile Audio Internetworking Profile @ Level 6";
1114 	case 0x28:
1115 		return "AAC Profile @ Level 1";
1116 	case 0x29:
1117 		return "AAC Profile @ Level 2";
1118 	case 0x2A:
1119 		return "AAC Profile @ Level 4";
1120 	case 0x2B:
1121 		return "AAC Profile @ Level 5";
1122 	case 0x2C:
1123 		return "High Efficiency AAC Profile @ Level 2";
1124 	case 0x2D:
1125 		return "High Efficiency AAC Profile @ Level 3";
1126 	case 0x2E:
1127 		return "High Efficiency AAC Profile @ Level 4";
1128 	case 0x2F:
1129 		return "High Efficiency AAC Profile @ Level 5";
1130 	case 0x30:
1131 		return "High Efficiency AAC v2 Profile @ Level 2";
1132 	case 0x31:
1133 		return "High Efficiency AAC v2 Profile @ Level 3";
1134 	case 0x32:
1135 		return "High Efficiency AAC v2 Profile @ Level 4";
1136 	case 0x33:
1137 		return "High Efficiency AAC v2 Profile @ Level 5";
1138 	case 0x34:
1139 		return "Low Delay AAC Profile";
1140 	case 0x35:
1141 		return "Baseline MPEG Surround Profile @ Level 1";
1142 	case 0x36:
1143 		return "Baseline MPEG Surround Profile @ Level 2";
1144 	case 0x37:
1145 		return "Baseline MPEG Surround Profile @ Level 3";
1146 	case 0x38:
1147 		return "Baseline MPEG Surround Profile @ Level 4";
1148 	case 0x39:
1149 		return "Baseline MPEG Surround Profile @ Level 5";
1150 	case 0x3A:
1151 		return "Baseline MPEG Surround Profile @ Level 6";
1152 
1153 	case 0x50:
1154 		return "AAC Profile @ Level 6";
1155 	case 0x51:
1156 		return "AAC Profile @ Level 7";
1157 	case 0x52:
1158 		return "High Efficiency AAC Profile @ Level 6";
1159 	case 0x53:
1160 		return "High Efficiency AAC Profile @ Level 7";
1161 	case 0x54:
1162 		return "High Efficiency AAC v2 Profile @ Level 6";
1163 	case 0x55:
1164 		return "High Efficiency AAC v2 Profile @ Level 7";
1165 	case 0x56:
1166 		return "Extended High Efficiency AAC Profile @ Level 6";
1167 	case 0x57:
1168 		return "Extended High Efficiency AAC Profile @ Level 7";
1169 
1170 	case 0xFE:
1171 		return "Not part of MPEG-4 audio profiles";
1172 	case 0xFF:
1173 		return "No audio capability required";
1174 	default:
1175 		return "ISO Reserved / User Private";
1176 	}
1177 }
1178 
1179 #ifndef GPAC_DISABLE_AV_PARSERS
1180 
1181 GF_EXPORT
gf_m4a_get_profile(GF_M4ADecSpecInfo * cfg)1182 u32 gf_m4a_get_profile(GF_M4ADecSpecInfo *cfg)
1183 {
1184 	switch (cfg->base_object_type) {
1185 	case 2: /*AAC LC*/
1186 		if (cfg->nb_chan <= 2)
1187 			return (cfg->base_sr <= 24000) ? 0x28 : 0x29; /*LC@L1 or LC@L2*/
1188 		if (cfg->nb_chan <= 5)
1189 			return (cfg->base_sr <= 48000) ? 0x2A : 0x2B; /*LC@L4 or LC@L5*/
1190 		return (cfg->base_sr <= 48000) ? 0x50 : 0x51; /*LC@L4 or LC@L5*/
1191 	case 5: /*HE-AAC - SBR*/
1192 		if (cfg->nb_chan <= 2)
1193 			return (cfg->base_sr <= 24000) ? 0x2C : 0x2D; /*HE@L2 or HE@L3*/
1194 		if (cfg->nb_chan <= 5)
1195 			return (cfg->base_sr <= 48000) ? 0x2E : 0x2F; /*HE@L4 or HE@L5*/
1196 		return (cfg->base_sr <= 48000) ? 0x52 : 0x53; /*HE@L6 or HE@L7*/
1197 	case 29: /*HE-AACv2 - SBR+PS*/
1198 		if (cfg->nb_chan <= 2)
1199 			return (cfg->base_sr <= 24000) ? 0x30 : 0x31; /*HE-AACv2@L2 or HE-AACv2@L3*/
1200 		if (cfg->nb_chan <= 5)
1201 			return (cfg->base_sr <= 48000) ? 0x32 : 0x33; /*HE-AACv2@L4 or HE-AACv2@L5*/
1202 		return (cfg->base_sr <= 48000) ? 0x54 : 0x55; /*HE-AACv2@L6 or HE-AACv2@L7*/
1203 	/*default to HQ*/
1204 	default:
1205 		if (cfg->nb_chan <= 2) return (cfg->base_sr < 24000) ? 0x0E : 0x0F; /*HQ@L1 or HQ@L2*/
1206 		return 0x10; /*HQ@L3*/
1207 	}
1208 }
1209 
1210 
1211 
1212 GF_EXPORT
gf_m4a_parse_config(GF_BitStream * bs,GF_M4ADecSpecInfo * cfg,Bool size_known)1213 GF_Err gf_m4a_parse_config(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg, Bool size_known)
1214 {
1215 	u32 channel_configuration = 0;
1216 	memset(cfg, 0, sizeof(GF_M4ADecSpecInfo));
1217 	cfg->base_object_type = gf_bs_read_int(bs, 5);
1218 	/*extended object type*/
1219 	if (cfg->base_object_type == 31) {
1220 		cfg->base_object_type = 32 + gf_bs_read_int(bs, 6);
1221 	}
1222 	cfg->base_sr_index = gf_bs_read_int(bs, 4);
1223 	if (cfg->base_sr_index == 0x0F) {
1224 		cfg->base_sr = gf_bs_read_int(bs, 24);
1225 	}
1226 	else {
1227 		cfg->base_sr = GF_M4ASampleRates[cfg->base_sr_index];
1228 	}
1229 
1230 	channel_configuration = gf_bs_read_int(bs, 4);
1231 
1232 	if (channel_configuration) {
1233 		cfg->nb_chan = GF_M4ANumChannels[channel_configuration - 1];
1234 	}
1235 
1236 	if (cfg->base_object_type == 5 || cfg->base_object_type == 29) {
1237 		if (cfg->base_object_type == 29) {
1238 			cfg->has_ps = 1;
1239 			cfg->nb_chan = 1;
1240 		}
1241 		cfg->has_sbr = GF_TRUE;
1242 		cfg->sbr_sr_index = gf_bs_read_int(bs, 4);
1243 		if (cfg->sbr_sr_index == 0x0F) {
1244 			cfg->sbr_sr = gf_bs_read_int(bs, 24);
1245 		}
1246 		else {
1247 			cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index];
1248 		}
1249 		cfg->sbr_object_type = gf_bs_read_int(bs, 5);
1250 	}
1251 
1252 	/*object cfg*/
1253 	switch (cfg->base_object_type) {
1254 	case 1:
1255 	case 2:
1256 	case 3:
1257 	case 4:
1258 	case 6:
1259 	case 7:
1260 	case 17:
1261 	case 19:
1262 	case 20:
1263 	case 21:
1264 	case 22:
1265 	case 23:
1266 	{
1267 		Bool ext_flag;
1268 		/*frame length flag*/
1269 		/*fl_flag = */gf_bs_read_int(bs, 1);
1270 		/*depends on core coder*/
1271 		if (gf_bs_read_int(bs, 1))
1272 			/*delay = */gf_bs_read_int(bs, 14);
1273 		ext_flag = gf_bs_read_int(bs, 1);
1274 
1275 		if (!channel_configuration) {
1276 			u32 i, cpe_channels=0;
1277 			cfg->program_config_element_present = 1;
1278 			cfg->element_instance_tag = gf_bs_read_int(bs, 4);
1279 			cfg->object_type = gf_bs_read_int(bs, 2);
1280 			cfg->sampling_frequency_index = gf_bs_read_int(bs, 4);
1281 			cfg->num_front_channel_elements = gf_bs_read_int(bs, 4);
1282 			cfg->num_side_channel_elements = gf_bs_read_int(bs, 4);
1283 			cfg->num_back_channel_elements = gf_bs_read_int(bs, 4);
1284 			cfg->num_lfe_channel_elements = gf_bs_read_int(bs, 2);
1285 			cfg->num_assoc_data_elements = gf_bs_read_int(bs, 3);
1286 			cfg->num_valid_cc_elements = gf_bs_read_int(bs, 4);
1287 			cfg->mono_mixdown_present = (Bool)gf_bs_read_int(bs, 1);
1288 			if (cfg->mono_mixdown_present) {
1289 				cfg->mono_mixdown_element_number = gf_bs_read_int(bs, 4);
1290 			}
1291 			cfg->stereo_mixdown_present = gf_bs_read_int(bs, 1);
1292 			if (cfg->stereo_mixdown_present) {
1293 				cfg->stereo_mixdown_element_number = gf_bs_read_int(bs, 4);
1294 			}
1295 			cfg->matrix_mixdown_idx_present = gf_bs_read_int(bs, 1);
1296 			if (cfg->matrix_mixdown_idx_present) {
1297 				cfg->matrix_mixdown_idx = gf_bs_read_int(bs, 2);
1298 				cfg->pseudo_surround_enable = gf_bs_read_int(bs, 1);
1299 			}
1300 			for (i = 0; i < cfg->num_front_channel_elements; i++) {
1301 				cfg->front_element_is_cpe[i] = gf_bs_read_int(bs, 1);
1302 				cfg->front_element_tag_select[i] = gf_bs_read_int(bs, 4);
1303 				if (cfg->front_element_is_cpe[i]) cpe_channels++;
1304 			}
1305 			for (i = 0; i < cfg->num_side_channel_elements; i++) {
1306 				cfg->side_element_is_cpe[i] = gf_bs_read_int(bs, 1);
1307 				cfg->side_element_tag_select[i] = gf_bs_read_int(bs, 4);
1308 				if (cfg->side_element_is_cpe[i]) cpe_channels++;
1309 			}
1310 			for (i = 0; i < cfg->num_back_channel_elements; i++) {
1311 				cfg->back_element_is_cpe[i] = gf_bs_read_int(bs, 1);
1312 				cfg->back_element_tag_select[i] = gf_bs_read_int(bs, 4);
1313 				if (cfg->back_element_is_cpe[i]) cpe_channels++;
1314 			}
1315 			for (i = 0; i < cfg->num_lfe_channel_elements; i++) {
1316 				cfg->lfe_element_tag_select[i] = gf_bs_read_int(bs, 4);
1317 			}
1318 			for (i = 0; i < cfg->num_assoc_data_elements; i++) {
1319 				cfg->assoc_data_element_tag_select[i] = gf_bs_read_int(bs, 4);
1320 			}
1321 
1322 			for (i = 0; i < cfg->num_valid_cc_elements; i++) {
1323 				cfg->cc_element_is_ind_sw[i] = gf_bs_read_int(bs, 1);
1324 				cfg->valid_cc_element_tag_select[i] = gf_bs_read_int(bs, 4);
1325 			}
1326 			gf_bs_align(bs);
1327 			cfg->comment_field_bytes = gf_bs_read_int(bs, 8);
1328 			gf_bs_read_data(bs, (char *)cfg->comments, cfg->comment_field_bytes);
1329 
1330 			cfg->nb_chan = cfg->num_front_channel_elements + cfg->num_back_channel_elements + cfg->num_side_channel_elements + cfg->num_lfe_channel_elements;
1331 			cfg->nb_chan += cpe_channels;
1332 		}
1333 
1334 		if ((cfg->base_object_type == 6) || (cfg->base_object_type == 20)) {
1335 			gf_bs_read_int(bs, 3);
1336 		}
1337 		if (ext_flag) {
1338 			if (cfg->base_object_type == 22) {
1339 				gf_bs_read_int(bs, 5);
1340 				gf_bs_read_int(bs, 11);
1341 			}
1342 			if ((cfg->base_object_type == 17)
1343 				|| (cfg->base_object_type == 19)
1344 				|| (cfg->base_object_type == 20)
1345 				|| (cfg->base_object_type == 23)
1346 				) {
1347 				gf_bs_read_int(bs, 1);
1348 				gf_bs_read_int(bs, 1);
1349 				gf_bs_read_int(bs, 1);
1350 			}
1351 			/*ext_flag = */gf_bs_read_int(bs, 1);
1352 		}
1353 	}
1354 	break;
1355 	}
1356 	/*ER cfg*/
1357 	switch (cfg->base_object_type) {
1358 	case 17:
1359 	case 19:
1360 	case 20:
1361 	case 21:
1362 	case 22:
1363 	case 23:
1364 	case 24:
1365 	case 25:
1366 	case 26:
1367 	case 27:
1368 	{
1369 		u32 epConfig = gf_bs_read_int(bs, 2);
1370 		if ((epConfig == 2) || (epConfig == 3)) {
1371 		}
1372 		if (epConfig == 3) {
1373 			gf_bs_read_int(bs, 1);
1374 		}
1375 	}
1376 	break;
1377 	}
1378 
1379 	if (size_known && (cfg->base_object_type != 5) && (cfg->base_object_type != 29)) {
1380 		while (gf_bs_available(bs) >= 2) {
1381 			u32 sync = gf_bs_peek_bits(bs, 11, 0);
1382 			if (sync == 0x2b7) {
1383 				gf_bs_read_int(bs, 11);
1384 				cfg->sbr_object_type = gf_bs_read_int(bs, 5);
1385 				cfg->has_sbr = gf_bs_read_int(bs, 1);
1386 				if (cfg->has_sbr) {
1387 					cfg->sbr_sr_index = gf_bs_read_int(bs, 4);
1388 					if (cfg->sbr_sr_index == 0x0F) {
1389 						cfg->sbr_sr = gf_bs_read_int(bs, 24);
1390 					}
1391 					else {
1392 						cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index];
1393 					}
1394 				}
1395 			}
1396 			else if (sync == 0x548) {
1397 				gf_bs_read_int(bs, 11);
1398 				cfg->has_ps = gf_bs_read_int(bs, 1);
1399 				if (cfg->has_ps)
1400 					cfg->nb_chan = 1;
1401 			}
1402 			else {
1403 				break;
1404 			}
1405 		}
1406 	}
1407 	cfg->audioPL = gf_m4a_get_profile(cfg);
1408 	return GF_OK;
1409 }
1410 
1411 GF_EXPORT
gf_m4a_get_config(u8 * dsi,u32 dsi_size,GF_M4ADecSpecInfo * cfg)1412 GF_Err gf_m4a_get_config(u8 *dsi, u32 dsi_size, GF_M4ADecSpecInfo *cfg)
1413 {
1414 	GF_BitStream *bs;
1415 	if (!dsi || !dsi_size || (dsi_size < 2)) return GF_NON_COMPLIANT_BITSTREAM;
1416 	bs = gf_bs_new(dsi, dsi_size, GF_BITSTREAM_READ);
1417 	gf_m4a_parse_config(bs, cfg, GF_TRUE);
1418 	gf_bs_del(bs);
1419 	return GF_OK;
1420 }
1421 
gf_latm_get_value(GF_BitStream * bs)1422 u32 gf_latm_get_value(GF_BitStream *bs)
1423 {
1424 	u32 i, tmp, value = 0;
1425 	u32 bytesForValue = gf_bs_read_int(bs, 2);
1426 	for (i = 0; i <= bytesForValue; i++) {
1427 		value <<= 8;
1428 		tmp = gf_bs_read_int(bs, 8);
1429 		value += tmp;
1430 	}
1431 	return value;
1432 }
1433 
1434 GF_EXPORT
gf_m4a_get_channel_cfg(u32 nb_chan)1435 u32 gf_m4a_get_channel_cfg(u32 nb_chan)
1436 {
1437 	u32 i, count = sizeof(GF_M4ANumChannels) / sizeof(u32);
1438 	for (i = 0; i < count; i++) {
1439 		if (GF_M4ANumChannels[i] == nb_chan) return i + 1;
1440 	}
1441 	return 0;
1442 }
1443 
1444 GF_EXPORT
gf_m4a_write_config_bs(GF_BitStream * bs,GF_M4ADecSpecInfo * cfg)1445 GF_Err gf_m4a_write_config_bs(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg)
1446 {
1447 	if (!cfg->base_sr_index) {
1448 		if (!cfg->base_sr) return GF_BAD_PARAM;
1449 		while (GF_M4ASampleRates[cfg->base_sr_index]) {
1450 			if (GF_M4ASampleRates[cfg->base_sr_index] == cfg->base_sr)
1451 				break;
1452 			cfg->base_sr_index++;
1453 		}
1454 	}
1455 	if (cfg->sbr_sr && !cfg->sbr_sr_index) {
1456 		while (GF_M4ASampleRates[cfg->sbr_sr_index]) {
1457 			if (GF_M4ASampleRates[cfg->sbr_sr_index] == cfg->sbr_sr)
1458 				break;
1459 			cfg->sbr_sr_index++;
1460 		}
1461 	}
1462 	/*extended object type*/
1463 	if (cfg->base_object_type >= 32) {
1464 		gf_bs_write_int(bs, 31, 5);
1465 		gf_bs_write_int(bs, cfg->base_object_type - 32, 6);
1466 	}
1467 	else {
1468 		gf_bs_write_int(bs, cfg->base_object_type, 5);
1469 	}
1470 	gf_bs_write_int(bs, cfg->base_sr_index, 4);
1471 	if (cfg->base_sr_index == 0x0F) {
1472 		gf_bs_write_int(bs, cfg->base_sr, 24);
1473 	}
1474 
1475 	if (cfg->program_config_element_present) {
1476 		gf_bs_write_int(bs, 0, 4);
1477 	}
1478 	else {
1479 		gf_bs_write_int(bs, gf_m4a_get_channel_cfg(cfg->nb_chan), 4);
1480 	}
1481 
1482 	if (cfg->base_object_type == 5 || cfg->base_object_type == 29) {
1483 		if (cfg->base_object_type == 29) {
1484 			cfg->has_ps = 1;
1485 			cfg->nb_chan = 1;
1486 		}
1487 		cfg->has_sbr = 1;
1488 		gf_bs_write_int(bs, cfg->sbr_sr_index, 4);
1489 		if (cfg->sbr_sr_index == 0x0F) {
1490 			gf_bs_write_int(bs, cfg->sbr_sr, 24);
1491 		}
1492 		gf_bs_write_int(bs, cfg->sbr_object_type, 5);
1493 	}
1494 
1495 	/*object cfg*/
1496 	switch (cfg->base_object_type) {
1497 	case 1:
1498 	case 2:
1499 	case 3:
1500 	case 4:
1501 	case 6:
1502 	case 7:
1503 	case 17:
1504 	case 19:
1505 	case 20:
1506 	case 21:
1507 	case 22:
1508 	case 23:
1509 	{
1510 		/*frame length flag*/
1511 		gf_bs_write_int(bs, 0, 1);
1512 		/*depends on core coder*/
1513 		gf_bs_write_int(bs, 0, 1);
1514 		/*ext flag*/
1515 		gf_bs_write_int(bs, 0, 1);
1516 
1517 		if (cfg->program_config_element_present) {
1518 			u32 i;
1519 			gf_bs_write_int(bs, cfg->element_instance_tag, 4);
1520 			gf_bs_write_int(bs, cfg->object_type, 2);
1521 			gf_bs_write_int(bs, cfg->sampling_frequency_index, 4);
1522 			gf_bs_write_int(bs, cfg->num_front_channel_elements, 4);
1523 			gf_bs_write_int(bs, cfg->num_side_channel_elements, 4);
1524 			gf_bs_write_int(bs, cfg->num_back_channel_elements, 4);
1525 			gf_bs_write_int(bs, cfg->num_lfe_channel_elements, 2);
1526 			gf_bs_write_int(bs, cfg->num_assoc_data_elements, 3);
1527 			gf_bs_write_int(bs, cfg->num_valid_cc_elements, 4);
1528 			gf_bs_write_int(bs, cfg->mono_mixdown_present, 1);
1529 			if (cfg->mono_mixdown_present) {
1530 				gf_bs_write_int(bs, cfg->mono_mixdown_element_number, 4);
1531 			}
1532 			gf_bs_write_int(bs, cfg->stereo_mixdown_present, 1);
1533 			if (cfg->stereo_mixdown_present) {
1534 				gf_bs_write_int(bs, cfg->stereo_mixdown_element_number, 4);
1535 			}
1536 			gf_bs_write_int(bs, cfg->matrix_mixdown_idx_present, 1);
1537 			if (cfg->matrix_mixdown_idx_present) {
1538 				gf_bs_write_int(bs, cfg->matrix_mixdown_idx, 2);
1539 				gf_bs_write_int(bs, cfg->pseudo_surround_enable, 1);
1540 			}
1541 			for (i = 0; i < cfg->num_front_channel_elements; i++) {
1542 				gf_bs_write_int(bs, cfg->front_element_is_cpe[i], 1);
1543 				gf_bs_write_int(bs, cfg->front_element_tag_select[i], 4);
1544 			}
1545 			for (i = 0; i < cfg->num_side_channel_elements; i++) {
1546 				gf_bs_write_int(bs, cfg->side_element_is_cpe[i], 1);
1547 				gf_bs_write_int(bs, cfg->side_element_tag_select[i], 4);
1548 			}
1549 			for (i = 0; i < cfg->num_back_channel_elements; i++) {
1550 				gf_bs_write_int(bs, cfg->back_element_is_cpe[i], 1);
1551 				gf_bs_write_int(bs, cfg->back_element_tag_select[i], 4);
1552 			}
1553 			for (i = 0; i < cfg->num_lfe_channel_elements; i++) {
1554 				gf_bs_write_int(bs, cfg->lfe_element_tag_select[i], 4);
1555 			}
1556 			for (i = 0; i < cfg->num_assoc_data_elements; i++) {
1557 				gf_bs_write_int(bs, cfg->assoc_data_element_tag_select[i], 4);
1558 			}
1559 
1560 			for (i = 0; i < cfg->num_valid_cc_elements; i++) {
1561 				gf_bs_write_int(bs, cfg->cc_element_is_ind_sw[i], 1);
1562 				gf_bs_write_int(bs, cfg->valid_cc_element_tag_select[i], 4);
1563 			}
1564 			gf_bs_align(bs);
1565 			gf_bs_write_int(bs, cfg->comment_field_bytes, 8);
1566 			gf_bs_write_data(bs, (char *)cfg->comments, cfg->comment_field_bytes);
1567 		}
1568 
1569 		if ((cfg->base_object_type == 6) || (cfg->base_object_type == 20)) {
1570 			gf_bs_write_int(bs, 0, 3);
1571 		}
1572 	}
1573 	break;
1574 	}
1575 	/*ER cfg - not supported*/
1576 
1577 	/*implicit sbr - not used yet*/
1578 #if 0
1579 	if ((cfg->base_object_type != 5) && (cfg->base_object_type != 29)) {
1580 		gf_bs_write_int(bs, 0x2b7, 11);
1581 		cfg->sbr_object_type = gf_bs_read_int(bs, 5);
1582 		cfg->has_sbr = gf_bs_read_int(bs, 1);
1583 		if (cfg->has_sbr) {
1584 			cfg->sbr_sr_index = gf_bs_read_int(bs, 4);
1585 			if (cfg->sbr_sr_index == 0x0F) {
1586 				cfg->sbr_sr = gf_bs_read_int(bs, 24);
1587 			}
1588 			else {
1589 				cfg->sbr_sr = GF_M4ASampleRates[cfg->sbr_sr_index];
1590 			}
1591 		}
1592 	}
1593 #endif
1594 
1595 	return GF_OK;
1596 }
1597 
1598 GF_EXPORT
gf_m4a_write_config(GF_M4ADecSpecInfo * cfg,u8 ** dsi,u32 * dsi_size)1599 GF_Err gf_m4a_write_config(GF_M4ADecSpecInfo *cfg, u8 **dsi, u32 *dsi_size)
1600 {
1601 	GF_BitStream *bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
1602 	gf_m4a_write_config_bs(bs, cfg);
1603 	gf_bs_get_content(bs, dsi, dsi_size);
1604 	gf_bs_del(bs);
1605 	return GF_OK;
1606 }
1607 
1608 
1609 /*AV1 parsing*/
1610 
av1_read_ns(GF_BitStream * bs,u32 n)1611 static u32 av1_read_ns(GF_BitStream *bs, u32 n)
1612 {
1613 	u32 v;
1614 	Bool extra_bit;
1615 	int w = (u32)(log(n) / log(2)) + 1;
1616 	u32 m = (1 << w) - n;
1617 	assert(w < 32);
1618 	v = gf_bs_read_int(bs, w - 1);
1619 	if (v < m)
1620 		return v;
1621 	extra_bit = gf_bs_read_int(bs, 1);
1622 	return (v << 1) - m + extra_bit;
1623 }
1624 
av1_color_config(GF_BitStream * bs,AV1State * state)1625 static void av1_color_config(GF_BitStream *bs, AV1State *state)
1626 {
1627 	state->config->high_bitdepth = gf_bs_read_int(bs, 1);
1628 	state->bit_depth = 8;
1629 	if (state->config->seq_profile == 2 && state->config->high_bitdepth) {
1630 		state->config->twelve_bit = gf_bs_read_int(bs, 1);
1631 		state->bit_depth = state->config->twelve_bit ? 12 : 10;
1632 	}
1633 	else if (state->config->seq_profile <= 2) {
1634 		state->bit_depth = state->config->high_bitdepth ? 10 : 8;
1635 	}
1636 
1637 	state->config->monochrome = GF_FALSE;
1638 	if (state->config->seq_profile == 1) {
1639 		state->config->monochrome = GF_FALSE;
1640 	}
1641 	else {
1642 		state->config->monochrome = gf_bs_read_int(bs, 1);
1643 	}
1644 	/*NumPlanes = mono_chrome ? 1 : 3;*/
1645 	state->color_description_present_flag = gf_bs_read_int(bs, 1);
1646 	if (state->color_description_present_flag) {
1647 		state->color_primaries = gf_bs_read_int(bs, 8);
1648 		state->transfer_characteristics = gf_bs_read_int(bs, 8);
1649 		state->matrix_coefficients = gf_bs_read_int(bs, 8);
1650 	}
1651 	else {
1652 		state->color_primaries = 2/*CP_UNSPECIFIED*/;
1653 		state->transfer_characteristics = 2/*TC_UNSPECIFIED*/;
1654 		state->matrix_coefficients = 2/*MC_UNSPECIFIED*/;
1655 	}
1656 	if (state->config->monochrome) {
1657 		state->color_range = gf_bs_read_int(bs, 1);
1658 		state->config->chroma_subsampling_x = GF_TRUE;
1659 		state->config->chroma_subsampling_y = GF_TRUE;
1660 		state->config->chroma_sample_position = 0/*CSP_UNKNOWN*/;
1661 		state->separate_uv_delta_q = 0;
1662 		return;
1663 	}
1664 	else if (state->color_primaries == 0/*CP_BT_709*/ &&
1665 		state->transfer_characteristics == 13/*TC_SRGB*/ &&
1666 		state->matrix_coefficients == 0/*MC_IDENTITY*/) {
1667 		state->color_range = GF_TRUE;
1668 		state->config->chroma_subsampling_x = GF_FALSE;
1669 		state->config->chroma_subsampling_y = GF_FALSE;
1670 	}
1671 	else {
1672 		state->config->chroma_subsampling_x = GF_FALSE;
1673 		state->config->chroma_subsampling_y = GF_FALSE;
1674 
1675 		state->color_range = gf_bs_read_int(bs, 1);
1676 		if (state->config->seq_profile == 0) {
1677 			state->config->chroma_subsampling_x = GF_TRUE;
1678 			state->config->chroma_subsampling_y = GF_TRUE;
1679 		}
1680 		else if (state->config->seq_profile == 1) {
1681 			state->config->chroma_subsampling_x = GF_FALSE;
1682 			state->config->chroma_subsampling_y = GF_FALSE;
1683 		}
1684 		else {
1685 			if (state->bit_depth == 12) {
1686 				state->config->chroma_subsampling_x = gf_bs_read_int(bs, 1);
1687 				if (state->config->chroma_subsampling_x)
1688 					state->config->chroma_subsampling_y = gf_bs_read_int(bs, 1);
1689 				else
1690 					state->config->chroma_subsampling_y = GF_FALSE;
1691 			}
1692 			else {
1693 				state->config->chroma_subsampling_x = GF_TRUE;
1694 				state->config->chroma_subsampling_y = GF_FALSE;
1695 			}
1696 		}
1697 		if (state->config->chroma_subsampling_x && state->config->chroma_subsampling_y) {
1698 			state->config->chroma_sample_position = gf_bs_read_int(bs, 2);
1699 		}
1700 	}
1701 	state->separate_uv_delta_q = gf_bs_read_int(bs, 1);
1702 }
1703 
1704 
uvlc(GF_BitStream * bs)1705 static u32 uvlc(GF_BitStream *bs) {
1706 	u8 leadingZeros = 0;
1707 	while (1) {
1708 		Bool done = gf_bs_read_int(bs, 1);
1709 		if (done)
1710 			break;
1711 		leadingZeros++;
1712 	}
1713 	if (leadingZeros >= 32) {
1714 		return 0xFFFFFFFF;
1715 	}
1716 	return gf_bs_read_int(bs, leadingZeros) + (1 << leadingZeros) - 1;
1717 }
1718 
timing_info(GF_BitStream * bs,AV1State * state)1719 static void timing_info(GF_BitStream *bs, AV1State *state) {
1720 	u32 time_scale = 0;
1721 	/*num_units_in_display_tick*/ gf_bs_read_int(bs, 32);
1722 	time_scale = gf_bs_read_int(bs, 32);
1723 	state->equal_picture_interval = gf_bs_read_int(bs, 1);
1724 	if (state->equal_picture_interval) {
1725 		u32 num_ticks_per_picture_minus_1 = uvlc(bs);
1726 		state->tb_num = (num_ticks_per_picture_minus_1 + 1);
1727 		state->tb_den = time_scale;
1728 	}
1729 	else {
1730 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] VFR not supported.\n"));
1731 		//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()
1732 	}
1733 }
1734 
decoder_model_info(AV1State * state,GF_BitStream * bs)1735 static void decoder_model_info(AV1State *state, GF_BitStream *bs) {
1736 	state->buffer_delay_length = 1 + gf_bs_read_int(bs, 5);
1737 	/*num_units_in_decoding_tick =*/ gf_bs_read_int(bs, 32);
1738 	state->buffer_removal_time_length = gf_bs_read_int(bs, 5);
1739 	state->frame_presentation_time_length = 1 + gf_bs_read_int(bs, 5);
1740 }
1741 
operating_parameters_info(GF_BitStream * bs,const u8 idx,const u8 buffer_delay_length_minus_1)1742 static void operating_parameters_info(GF_BitStream *bs, const u8 idx, const u8 buffer_delay_length_minus_1) {
1743 	const u8 n = buffer_delay_length_minus_1 + 1;
1744 	/*decoder_buffer_delay[op] =*/ gf_bs_read_int(bs, n);
1745 	/*encoder_buffer_delay[op] =*/ gf_bs_read_int(bs, n);
1746 	/*low_delay_mode_flag[op] =*/ gf_bs_read_int(bs, 1);
1747 }
1748 
av1_parse_sequence_header_obu(GF_BitStream * bs,AV1State * state)1749 static void av1_parse_sequence_header_obu(GF_BitStream *bs, AV1State *state)
1750 {
1751 	u8 buffer_delay_length_minus_1 = 0;
1752 	state->frame_state.seen_seq_header = GF_TRUE;
1753 	state->config->seq_profile = gf_bs_read_int(bs, 3);
1754 	state->still_picture = gf_bs_read_int(bs, 1);
1755 	state->reduced_still_picture_header = gf_bs_read_int(bs, 1);
1756 	if (state->reduced_still_picture_header) {
1757 		//timing_info_present_flag = GF_FALSE;
1758 		//initial_display_delay_present_flag = GF_FALSE;
1759 		state->operating_points_count = 1;
1760 		state->config->seq_level_idx_0 = gf_bs_read_int(bs, 5);
1761 	}
1762 	else {
1763 		u8 i = 0;
1764 		Bool initial_display_delay_present_flag;
1765 		Bool timing_info_present_flag = gf_bs_read_int(bs, 1);
1766 		if (timing_info_present_flag) {
1767 			timing_info(bs, state);
1768 			state->decoder_model_info_present_flag = gf_bs_read_int(bs, 1);
1769 			if (state->decoder_model_info_present_flag) {
1770 				decoder_model_info(state, bs);
1771 			}
1772 		}
1773 		else {
1774 			state->decoder_model_info_present_flag = GF_FALSE;
1775 		}
1776 		initial_display_delay_present_flag = gf_bs_read_int(bs, 1);
1777 		state->operating_points_count = 1 + gf_bs_read_int(bs, 5);
1778 		for (i = 0; i < state->operating_points_count; i++) {
1779 			u8 seq_level_idx_i, seq_tier = 0;
1780 
1781 			state->operating_point_idc[i] = gf_bs_read_int(bs, 12);
1782 
1783 			seq_level_idx_i = gf_bs_read_int(bs, 5);
1784 			if (i == 0) state->config->seq_level_idx_0 = seq_level_idx_i;
1785 
1786 			if (seq_level_idx_i > 7) {
1787 				seq_tier = gf_bs_read_int(bs, 1);
1788 			}
1789 			if (i == 0) state->config->seq_tier_0 = seq_tier;
1790 
1791 			if (state->decoder_model_info_present_flag) {
1792 				state->decoder_model_present_for_this_op[i] = gf_bs_read_int(bs, 1);
1793 				if (state->decoder_model_present_for_this_op[i]) {
1794 					operating_parameters_info(bs, i, buffer_delay_length_minus_1);
1795 				}
1796 			}
1797 			else {
1798 				state->decoder_model_present_for_this_op[i] = 0;
1799 			}
1800 			if (initial_display_delay_present_flag) {
1801 				if (gf_bs_read_int(bs, 1) /*initial_display_delay_present_for_this_op[i]*/) {
1802 					/*initial_display_delay_minus_1[i] =*/ gf_bs_read_int(bs, 4);
1803 				}
1804 			}
1805 		}
1806 	}
1807 
1808 	//operatingPoint = av1_choose_operating_point(bs);
1809 	state->OperatingPointIdc = 0;//TODO: operating_point_idc[operatingPoint];
1810 
1811 	state->frame_width_bits_minus_1 = gf_bs_read_int(bs, 4);
1812 	state->frame_height_bits_minus_1 = gf_bs_read_int(bs, 4);
1813 	state->width = gf_bs_read_int(bs, state->frame_width_bits_minus_1 + 1) + 1;
1814 	state->height = gf_bs_read_int(bs, state->frame_height_bits_minus_1 + 1) + 1;
1815 	state->frame_id_numbers_present_flag = GF_FALSE;
1816 	if (!state->reduced_still_picture_header) {
1817 		state->frame_id_numbers_present_flag = gf_bs_read_int(bs, 1);
1818 	}
1819 	if (state->frame_id_numbers_present_flag) {
1820 		state->delta_frame_id_length_minus_2 = gf_bs_read_int(bs, 4);
1821 		state->additional_frame_id_length_minus_1 = gf_bs_read_int(bs, 3);
1822 	}
1823 	state->use_128x128_superblock = gf_bs_read_int(bs, 1);
1824 	/*enable_filter_intra =*/ gf_bs_read_int(bs, 1);
1825 	/*enable_intra_edge_filter =*/ gf_bs_read_int(bs, 1);
1826 	if (state->reduced_still_picture_header) {
1827 		/*enable_interintra_compound = 0;
1828 		enable_masked_compound = 0;
1829 		enable_dual_filter = 0;
1830 		enable_jnt_comp = 0;
1831 		enable_ref_frame_mvs = 0;*/
1832 		state->enable_warped_motion = 0;
1833 		state->enable_order_hint = GF_FALSE;
1834 		state->OrderHintBits = 0;
1835 		state->seq_force_integer_mv = 2/*SELECT_INTEGER_MV*/;
1836 		state->seq_force_screen_content_tools = 2/*SELECT_SCREEN_CONTENT_TOOLS*/;
1837 	}
1838 	else {
1839 		Bool seq_choose_screen_content_tools;
1840 		/*enable_interintra_compound =*/ gf_bs_read_int(bs, 1);
1841 		/*enable_masked_compound =*/ gf_bs_read_int(bs, 1);
1842 		state->enable_warped_motion = gf_bs_read_int(bs, 1);
1843 		/*enable_dual_filter =*/ gf_bs_read_int(bs, 1);
1844 		state->enable_order_hint = gf_bs_read_int(bs, 1);
1845 		if (state->enable_order_hint) {
1846 			/*enable_jnt_comp =*/ gf_bs_read_int(bs, 1);
1847 			state->enable_ref_frame_mvs = gf_bs_read_int(bs, 1);
1848 		}
1849 		else {
1850 			/*enable_jnt_comp =  0*/;
1851 			/*enable_ref_frame_mvs = 0*/;
1852 		}
1853 		seq_choose_screen_content_tools = gf_bs_read_int(bs, 1);
1854 		state->seq_force_screen_content_tools = 0;
1855 		if (seq_choose_screen_content_tools) {
1856 			state->seq_force_screen_content_tools = 2/*SELECT_SCREEN_CONTENT_TOOLS*/;
1857 		}
1858 		else {
1859 			state->seq_force_screen_content_tools = gf_bs_read_int(bs, 1);
1860 		}
1861 
1862 		state->seq_force_integer_mv = 0;
1863 		if (state->seq_force_screen_content_tools > 0) {
1864 			const Bool seq_choose_integer_mv = gf_bs_read_int(bs, 1);
1865 			if (seq_choose_integer_mv) {
1866 				state->seq_force_integer_mv = 2/*SELECT_INTEGER_MV*/;
1867 			}
1868 			else {
1869 				state->seq_force_integer_mv = gf_bs_read_int(bs, 1);
1870 			}
1871 		}
1872 		else {
1873 			state->seq_force_integer_mv = 2/*SELECT_INTEGER_MV*/;
1874 		}
1875 		if (state->enable_order_hint) {
1876 			u8 order_hint_bits_minus_1 = gf_bs_read_int(bs, 3);
1877 			state->OrderHintBits = order_hint_bits_minus_1 + 1;
1878 		}
1879 		else {
1880 			state->OrderHintBits = 0;
1881 		}
1882 	}
1883 
1884 	state->enable_superres = gf_bs_read_int(bs, 1);
1885 	state->enable_cdef = gf_bs_read_int(bs, 1);
1886 	state->enable_restoration = gf_bs_read_int(bs, 1);
1887 	av1_color_config(bs, state);
1888 	state->film_grain_params_present = gf_bs_read_int(bs, 1);
1889 }
1890 
1891 
1892 
1893 #define IVF_FILE_HEADER_SIZE 32
1894 
gf_media_probe_ivf(GF_BitStream * bs)1895 Bool gf_media_probe_ivf(GF_BitStream *bs)
1896 {
1897 	u32 dw = 0;
1898 	if (gf_bs_available(bs) < IVF_FILE_HEADER_SIZE) return GF_FALSE;
1899 
1900 	dw = gf_bs_peek_bits(bs, 32, 0);
1901 	if (dw != GF_4CC('D', 'K', 'I', 'F')) {
1902 		return GF_FALSE;
1903 	}
1904 	return GF_TRUE;
1905 }
1906 
gf_media_parse_ivf_file_header(GF_BitStream * bs,u32 * width,u32 * height,u32 * codec_fourcc,u32 * frame_rate,u32 * time_scale,u32 * num_frames)1907 GF_Err gf_media_parse_ivf_file_header(GF_BitStream *bs, u32 *width, u32 *height, u32 *codec_fourcc, u32 *frame_rate, u32 *time_scale, u32 *num_frames)
1908 {
1909 	u32 dw = 0;
1910 
1911 	if (!width || !height || !codec_fourcc || !frame_rate || !time_scale || !num_frames) {
1912 		assert(0);
1913 		return GF_BAD_PARAM;
1914 	}
1915 
1916 	if (gf_bs_available(bs) < IVF_FILE_HEADER_SIZE) {
1917 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Not enough bytes available ("LLU").\n", gf_bs_available(bs)));
1918 		return GF_NON_COMPLIANT_BITSTREAM;
1919 	}
1920 
1921 	dw = gf_bs_read_u32(bs);
1922 	if (dw != GF_4CC('D', 'K', 'I', 'F')) {
1923 		GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[IVF] Invalid signature\n"));
1924 		return GF_NON_COMPLIANT_BITSTREAM;
1925 	}
1926 
1927 	dw = gf_bs_read_u16_le(bs);
1928 	if (dw != 0) {
1929 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Wrong IVF version. 0 expected, got %u\n", dw));
1930 		return GF_NON_COMPLIANT_BITSTREAM;
1931 	}
1932 
1933 	dw = gf_bs_read_u16_le(bs); //length of header in bytes
1934 	if (dw != IVF_FILE_HEADER_SIZE) {
1935 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Wrong IVF header length. Expected 32 bytes, got %u\n", dw));
1936 		return GF_NON_COMPLIANT_BITSTREAM;
1937 	}
1938 
1939 	*codec_fourcc = gf_bs_read_u32(bs);
1940 
1941 	*width = gf_bs_read_u16_le(bs);
1942 	*height = gf_bs_read_u16_le(bs);
1943 
1944 	*frame_rate = gf_bs_read_u32_le(bs);
1945 	*time_scale = gf_bs_read_u32_le(bs);
1946 
1947 	*num_frames = gf_bs_read_u32_le(bs);
1948 	gf_bs_read_u32_le(bs); //skip unused
1949 
1950 	return GF_OK;
1951 }
1952 
gf_media_parse_ivf_frame_header(GF_BitStream * bs,u64 * frame_size,u64 * pts)1953 GF_Err gf_media_parse_ivf_frame_header(GF_BitStream *bs, u64 *frame_size, u64 *pts)
1954 {
1955 	if (!frame_size) return GF_BAD_PARAM;
1956 
1957 	*frame_size = gf_bs_read_u32_le(bs);
1958 	if (*frame_size > 256 * 1024 * 1024) {
1959 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[IVF] Wrong frame size %u\n", *frame_size));
1960 		*frame_size = 0;
1961 		return GF_NON_COMPLIANT_BITSTREAM;
1962 	}
1963 
1964 	*pts = gf_bs_read_u64_le(bs);
1965 
1966 	return GF_OK;
1967 }
1968 
gf_media_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)1969 GF_Err gf_media_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)
1970 {
1971 	u32 byte, bytes_per_framesize;
1972 	u64 pos = gf_bs_get_position(bs), i = 0;
1973 	GF_Err e;
1974 
1975 	assert(bs && num_frames_in_superframe);
1976 
1977 	/*initialize like there is no superframe*/
1978 	memset(frame_sizes, 0, VP9_MAX_FRAMES_IN_SUPERFRAME * sizeof(frame_sizes[0]));
1979 	*num_frames_in_superframe = 1;
1980 	frame_sizes[0] = (u32)ivf_frame_size;
1981 	*superframe_index_size = 0;
1982 
1983 	e = gf_bs_seek(bs, pos + ivf_frame_size - 1);
1984 	if (e) return e;
1985 
1986 	byte = gf_bs_read_u8(bs);
1987 	if ((byte & 0xe0) != 0xc0)
1988 		goto exit; /*no superframe*/
1989 
1990 	bytes_per_framesize = 1 + ((byte & 0x18) >> 3);
1991 	*num_frames_in_superframe = (u32)(1 + (byte & 0x7));
1992 
1993 	/*superframe_index()*/
1994 	*superframe_index_size = 2 + bytes_per_framesize * *num_frames_in_superframe;
1995 	gf_bs_seek(bs, pos + ivf_frame_size - *superframe_index_size);
1996 	byte = gf_bs_read_u8(bs);
1997 	if ((byte & 0xe0) != 0xc0)
1998 		goto exit; /*no superframe*/
1999 
2000 	frame_sizes[0] = 0;
2001 	for (i = 0; i < *num_frames_in_superframe; ++i) {
2002 		gf_bs_read_data(bs, (char*)(frame_sizes + i), bytes_per_framesize);
2003 	}
2004 
2005 exit:
2006 	gf_bs_seek(bs, pos);
2007 	return e;
2008 }
2009 
vp9_frame_sync_code(GF_BitStream * bs)2010 static Bool vp9_frame_sync_code(GF_BitStream *bs)
2011 {
2012 	u8 val = gf_bs_read_int(bs, 8);
2013 	if (val != 0x49)
2014 		return GF_FALSE;
2015 
2016 	val = gf_bs_read_int(bs, 8);
2017 	if (val != 0x83)
2018 		return GF_FALSE;
2019 
2020 	val = gf_bs_read_int(bs, 8);
2021 	if (val != 0x42)
2022 		return GF_FALSE;
2023 
2024 	return GF_TRUE;
2025 }
2026 
2027 typedef enum {
2028 	CS_UNKNOWN = 0,
2029 	CS_BT_601 = 1,
2030 	CS_BT_709 = 2,
2031 	CS_SMPTE_170 = 3,
2032 	CS_SMPTE_240 = 4,
2033 	CS_BT_2020 = 5,
2034 	CS_RESERVED = 6,
2035 	CS_RGB = 7,
2036 } VP9_color_space;
2037 
2038 static const int VP9_CS_to_23001_8_colour_primaries[] = { -1/*undefined*/, 5, 1, 6, 7, 9, -1/*reserved*/, 1 };
2039 static const int VP9_CS_to_23001_8_transfer_characteristics[] = { -1/*undefined*/, 5, 1, 6, 7, 9, -1/*reserved*/, 13 };
2040 
vp9_color_config(GF_BitStream * bs,GF_VPConfig * vp9_cfg)2041 static GF_Err vp9_color_config(GF_BitStream *bs, GF_VPConfig *vp9_cfg)
2042 {
2043 	VP9_color_space color_space;
2044 
2045 	if (vp9_cfg->profile >= 2) {
2046 		Bool ten_or_twelve_bit = gf_bs_read_int(bs, 1);
2047 		vp9_cfg->bit_depth = ten_or_twelve_bit ? 12 : 10;
2048 	}
2049 	else {
2050 		vp9_cfg->bit_depth = 8;
2051 	}
2052 
2053 	color_space = gf_bs_read_int(bs, 3);
2054 	vp9_cfg->colour_primaries = VP9_CS_to_23001_8_colour_primaries[color_space];
2055 	vp9_cfg->transfer_characteristics = VP9_CS_to_23001_8_transfer_characteristics[color_space];
2056 	if (color_space != CS_RGB) {
2057 		vp9_cfg->video_fullRange_flag = gf_bs_read_int(bs, 1);
2058 		if (vp9_cfg->profile == 1 || vp9_cfg->profile == 3) {
2059 			u8 subsampling_x, subsampling_y, subsampling_xy_to_chroma_subsampling[2][2] = { {3, 0}, {2, 0} };
2060 			subsampling_x = gf_bs_read_int(bs, 1);
2061 			subsampling_y = gf_bs_read_int(bs, 1);
2062 			vp9_cfg->chroma_subsampling = subsampling_xy_to_chroma_subsampling[subsampling_x][subsampling_y];
2063 			Bool reserved_zero = gf_bs_read_int(bs, 1);
2064 			if (reserved_zero) {
2065 				GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VP9] color config reserved zero (1) is not zero.\n"));
2066 				return GF_NON_COMPLIANT_BITSTREAM;
2067 			}
2068 		}
2069 		else {
2070 			vp9_cfg->chroma_subsampling = 0;
2071 		}
2072 	}
2073 	else {
2074 		vp9_cfg->video_fullRange_flag = GF_TRUE;
2075 		if (vp9_cfg->profile == 1 || vp9_cfg->profile == 3) {
2076 			vp9_cfg->chroma_subsampling = 3;
2077 			Bool reserved_zero = gf_bs_read_int(bs, 1);
2078 			if (reserved_zero) {
2079 				GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VP9] color config reserved zero (2) is not zero.\n"));
2080 				return GF_NON_COMPLIANT_BITSTREAM;
2081 			}
2082 		}
2083 	}
2084 
2085 	return GF_OK;
2086 }
2087 
vp9_compute_image_size(int FrameWidth,int FrameHeight,int * Sb64Cols,int * Sb64Rows)2088 static void vp9_compute_image_size(int FrameWidth, int FrameHeight, int *Sb64Cols, int *Sb64Rows)
2089 {
2090 	int MiCols = (FrameWidth + 7) >> 3;
2091 	int MiRows = (FrameHeight + 7) >> 3;
2092 	*Sb64Cols = (MiCols + 7) >> 3;
2093 	*Sb64Rows = (MiRows + 7) >> 3;
2094 }
2095 
vp9_frame_size(GF_BitStream * bs,int * FrameWidth,int * FrameHeight,int * Sb64Cols,int * Sb64Rows)2096 static void vp9_frame_size(GF_BitStream *bs, int *FrameWidth, int *FrameHeight, int *Sb64Cols, int *Sb64Rows)
2097 {
2098 	int frame_width_minus_1 = gf_bs_read_int(bs, 16);
2099 	int frame_height_minus_1 = gf_bs_read_int(bs, 16);
2100 	if (frame_width_minus_1 + 1 != *FrameWidth || frame_height_minus_1 + 1 != *FrameHeight) {
2101 		if (*FrameWidth || *FrameHeight)
2102 			GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[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));
2103 	}
2104 	*FrameWidth = frame_width_minus_1 + 1;
2105 	*FrameHeight = frame_height_minus_1 + 1;
2106 	vp9_compute_image_size(*FrameWidth, *FrameHeight, Sb64Cols, Sb64Rows);
2107 }
2108 
vp9_render_size(GF_BitStream * bs,int FrameWidth,int FrameHeight,int * renderWidth,int * renderHeight)2109 static void vp9_render_size(GF_BitStream *bs, int FrameWidth, int FrameHeight, int *renderWidth, int *renderHeight)
2110 {
2111 	Bool render_and_frame_size_different = gf_bs_read_int(bs, 1);
2112 	if (render_and_frame_size_different == 1) {
2113 		int render_width_minus_1 = gf_bs_read_int(bs, 16);
2114 		int render_height_minus_1 = gf_bs_read_int(bs, 16);
2115 		*renderWidth = render_width_minus_1 + 1;
2116 		*renderHeight = render_height_minus_1 + 1;
2117 	}
2118 	else {
2119 		*renderWidth = FrameWidth;
2120 		*renderHeight = FrameHeight;
2121 	}
2122 }
2123 
vp9_s(GF_BitStream * bs,int n)2124 static s64 vp9_s(GF_BitStream *bs, int n) {
2125 	s64 value = gf_bs_read_int(bs, n);
2126 	Bool sign = gf_bs_read_int(bs, 1);
2127 	return sign ? -value : value;
2128 }
2129 
vp9_loop_filter_params(GF_BitStream * bs)2130 static void vp9_loop_filter_params(GF_BitStream *bs)
2131 {
2132 	/*loop_filter_level = */gf_bs_read_int(bs, 6);
2133 	/*loop_filter_sharpness = */gf_bs_read_int(bs, 3);
2134 	Bool loop_filter_delta_enabled = gf_bs_read_int(bs, 1);
2135 	if (loop_filter_delta_enabled == 1) {
2136 		Bool loop_filter_delta_update = gf_bs_read_int(bs, 1);
2137 		if (loop_filter_delta_update == GF_TRUE) {
2138 			int i;
2139 			for (i = 0; i < 4; i++) {
2140 				Bool update_ref_delta = gf_bs_read_int(bs, 1);
2141 				if (update_ref_delta == GF_TRUE)
2142 					/*loop_filter_ref_deltas[i] =*/ vp9_s(bs, 6);
2143 			}
2144 			for (i = 0; i < 2; i++) {
2145 				Bool update_mode_delta = gf_bs_read_int(bs, 1);
2146 				if (update_mode_delta == GF_TRUE)
2147 					/*loop_filter_mode_deltas[i] =*/ vp9_s(bs, 6);
2148 			}
2149 		}
2150 	}
2151 }
2152 
vp9_quantization_params(GF_BitStream * bs)2153 static void vp9_quantization_params(GF_BitStream *bs)
2154 {
2155 	/*base_q_idx = */gf_bs_read_int(bs, 8);
2156 }
2157 
2158 #define VP9_MAX_SEGMENTS 8
2159 #define VP9_SEG_LVL_MAX 4
2160 static const int segmentation_feature_bits[VP9_SEG_LVL_MAX] = { 8, 6, 2, 0 };
2161 static const int segmentation_feature_signed[VP9_SEG_LVL_MAX] = { 1, 1, 0, 0 };
2162 
2163 #define VP9_MIN_TILE_WIDTH_B64 4
2164 #define VP9_MAX_TILE_WIDTH_B64 64
2165 
vp9_segmentation_params(GF_BitStream * bs)2166 static void vp9_segmentation_params(GF_BitStream *bs)
2167 {
2168 	Bool segmentation_enabled = gf_bs_read_int(bs, 1);
2169 	if (segmentation_enabled == 1) {
2170 		int i;
2171 		Bool segmentation_update_map = gf_bs_read_int(bs, 1);
2172 		if (segmentation_update_map) {
2173 			for (i = 0; i < 7; i++)
2174 				/*segmentation_tree_probs[i] = read_prob()*/
2175 				/*segmentation_temporal_update = */gf_bs_read_int(bs, 1);
2176 			/*for (i = 0; i < 3; i++)
2177 				segmentation_pred_prob[i] = segmentation_temporal_update ? read_prob() : 255*/
2178 		}
2179 		Bool segmentation_update_data = gf_bs_read_int(bs, 1);
2180 		if (segmentation_update_data == 1) {
2181 			/*segmentation_abs_or_delta_update =*/ gf_bs_read_int(bs, 1);
2182 			for (i = 0; i < VP9_MAX_SEGMENTS; i++) {
2183 				int j;
2184 				for (j = 0; j < VP9_SEG_LVL_MAX; j++) {
2185 					/*feature_value = 0*/
2186 					Bool feature_enabled = gf_bs_read_int(bs, 1);
2187 					/*FeatureEnabled[i][j] = feature_enabled*/
2188 					if (feature_enabled) {
2189 						int bits_to_read = segmentation_feature_bits[j];
2190 						/*feature_value =*/ gf_bs_read_int(bs, bits_to_read);
2191 						if (segmentation_feature_signed[j] == 1) {
2192 							/*Bool feature_sign = */gf_bs_read_int(bs, 1);
2193 							/*if (feature_sign == 1)
2194 								feature_value *= -1*/
2195 						}
2196 					}
2197 					/*FeatureData[i][j] = feature_value*/
2198 				}
2199 			}
2200 		}
2201 	}
2202 }
2203 
calc_min_log2_tile_cols(int Sb64Cols)2204 static int calc_min_log2_tile_cols(int Sb64Cols) {
2205 	int minLog2 = 0;
2206 	while ((VP9_MAX_TILE_WIDTH_B64 << minLog2) < Sb64Cols)
2207 		minLog2++;
2208 
2209 	return minLog2;
2210 }
2211 
calc_max_log2_tile_cols(int Sb64Cols)2212 static int calc_max_log2_tile_cols(int Sb64Cols) {
2213 	int maxLog2 = 1;
2214 	while ((Sb64Cols >> maxLog2) >= VP9_MIN_TILE_WIDTH_B64)
2215 		maxLog2++;
2216 
2217 	return maxLog2 - 1;
2218 }
2219 
vp9_tile_info(GF_BitStream * bs,int Sb64Cols)2220 static void vp9_tile_info(GF_BitStream *bs, int Sb64Cols)
2221 {
2222 	Bool tile_rows_log2;
2223 	int minLog2TileCols = calc_min_log2_tile_cols(Sb64Cols);
2224 	int maxLog2TileCols = calc_max_log2_tile_cols(Sb64Cols);
2225 	int tile_cols_log2 = minLog2TileCols;
2226 	while (tile_cols_log2 < maxLog2TileCols) {
2227 		Bool increment_tile_cols_log2 = gf_bs_read_int(bs, 1);
2228 		if (increment_tile_cols_log2)
2229 			tile_cols_log2++;
2230 		else
2231 			break;
2232 	}
2233 	tile_rows_log2 = gf_bs_read_int(bs, 1);
2234 	if (tile_rows_log2) {
2235 		/*Bool increment_tile_rows_log2 = */gf_bs_read_int(bs, 1);
2236 		//tile_rows_log2 += increment_tile_rows_log2;
2237 	}
2238 }
2239 
vp9_frame_size_with_refs(GF_BitStream * bs,u8 refresh_frame_flags,u8 * ref_frame_idx,int * RefFrameWidth,int * RefFrameHeight,int * FrameWidth,int * FrameHeight,int * RenderWidth,int * RenderHeight,int * Sb64Cols,int * Sb64Rows)2240 static void vp9_frame_size_with_refs(GF_BitStream *bs, u8 refresh_frame_flags, u8 * ref_frame_idx, int * RefFrameWidth, int *RefFrameHeight,
2241 	int *FrameWidth, int *FrameHeight, int *RenderWidth, int *RenderHeight, int *Sb64Cols, int *Sb64Rows)
2242 {
2243 	Bool found_ref;
2244 	int i;
2245 	for (i = 0; i < 3; i++) {
2246 		found_ref = gf_bs_read_int(bs, 1);
2247 		if (found_ref) {
2248 			*FrameWidth  = RefFrameWidth [ref_frame_idx[i]];
2249 			*FrameHeight = RefFrameHeight[ref_frame_idx[i]];
2250 			break;
2251 		}
2252 	}
2253 	if (found_ref == 0) {
2254 		vp9_frame_size(bs, FrameWidth, FrameHeight, Sb64Cols, Sb64Rows);
2255 	}
2256 	else {
2257 		vp9_compute_image_size(*FrameWidth, *FrameHeight, Sb64Cols, Sb64Rows);
2258 	}
2259 
2260 	vp9_render_size(bs, *FrameWidth, *FrameHeight, RenderWidth, RenderHeight);
2261 }
2262 
vp9_read_interpolation_filter(GF_BitStream * bs)2263 static void vp9_read_interpolation_filter(GF_BitStream *bs)
2264 {
2265 	Bool is_filter_switchable = gf_bs_read_int(bs, 1);
2266 	if (!is_filter_switchable) {
2267 		/*raw_interpolation_filter = */gf_bs_read_int(bs, 2);
2268 	}
2269 }
2270 
2271 
2272 #define VP9_KEY_FRAME 0
2273 
gf_media_vp9_parse_sample(GF_BitStream * bs,GF_VPConfig * vp9_cfg,Bool * key_frame,u32 * FrameWidth,u32 * FrameHeight,u32 * renderWidth,u32 * renderHeight)2274 GF_Err gf_media_vp9_parse_sample(GF_BitStream *bs, GF_VPConfig *vp9_cfg, Bool *key_frame, u32 *FrameWidth, u32 *FrameHeight, u32 *renderWidth, u32 *renderHeight)
2275 {
2276 	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;
2277 	/*u8 frame_context_idx = 0, reset_frame_context = 0, frame_marker = 0*/;
2278 	int Sb64Cols = 0, Sb64Rows = 0, i;
2279 	u8 refresh_frame_flags = 0;
2280 
2281 	assert(bs && key_frame);
2282 
2283 	/*uncompressed header*/
2284 	/*frame_marker = */gf_bs_read_int(bs, 2);
2285 	profile_low_bit = gf_bs_read_int(bs, 1);
2286 	profile_high_bit = gf_bs_read_int(bs, 1);
2287 	vp9_cfg->profile = (profile_high_bit << 1) + profile_low_bit;
2288 	if (vp9_cfg->profile == 3) {
2289 		Bool reserved_zero = gf_bs_read_int(bs, 1);
2290 		if (reserved_zero) {
2291 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[VP9] uncompressed header reserved zero is not zero.\n"));
2292 			return GF_NON_COMPLIANT_BITSTREAM;
2293 		}
2294 	}
2295 
2296 	show_existing_frame = gf_bs_read_int(bs, 1);
2297 	if (show_existing_frame == GF_TRUE) {
2298 		/*frame_to_show_map_idx = */gf_bs_read_int(bs, 3);
2299 		return GF_OK;
2300 	}
2301 
2302 	frame_type = gf_bs_read_int(bs, 1);
2303 	show_frame = gf_bs_read_int(bs, 1);
2304 	error_resilient_mode = gf_bs_read_int(bs, 1);
2305 	if (frame_type == VP9_KEY_FRAME) {
2306 		if (!vp9_frame_sync_code(bs))
2307 			return GF_NON_COMPLIANT_BITSTREAM;
2308 		if (vp9_color_config(bs, vp9_cfg) != GF_OK)
2309 			return GF_NON_COMPLIANT_BITSTREAM;
2310 		vp9_frame_size(bs, FrameWidth, FrameHeight, &Sb64Cols, &Sb64Rows);
2311 		vp9_render_size(bs, *FrameWidth, *FrameHeight, renderWidth, renderHeight);
2312 		refresh_frame_flags = 0xFF;
2313 		*key_frame = GF_TRUE;
2314 		FrameIsIntra = GF_TRUE;
2315 	}
2316 	else {
2317 		Bool intra_only = GF_FALSE;
2318 		*key_frame = GF_FALSE;
2319 
2320 		if (show_frame == GF_FALSE) {
2321 			intra_only = gf_bs_read_int(bs, 1);
2322 		}
2323 		FrameIsIntra = intra_only;
2324 
2325 		if (error_resilient_mode == GF_FALSE) {
2326 			/*reset_frame_context = */gf_bs_read_int(bs, 2);
2327 		}
2328 
2329 		if (intra_only == GF_TRUE) {
2330 			if (!vp9_frame_sync_code(bs))
2331 				return GF_NON_COMPLIANT_BITSTREAM;
2332 
2333 			if (vp9_cfg->profile > 0) {
2334 				if (vp9_color_config(bs, vp9_cfg) != GF_OK)
2335 					return GF_NON_COMPLIANT_BITSTREAM;
2336 			}
2337 			else {
2338 				u8 color_space = CS_BT_601;
2339 				vp9_cfg->colour_primaries = VP9_CS_to_23001_8_colour_primaries[color_space];
2340 				vp9_cfg->transfer_characteristics = VP9_CS_to_23001_8_transfer_characteristics[color_space];
2341 				vp9_cfg->chroma_subsampling = 0;
2342 				vp9_cfg->bit_depth = 8;
2343 			}
2344 			refresh_frame_flags = gf_bs_read_int(bs, 8);
2345 			vp9_frame_size(bs, FrameWidth, FrameHeight, &Sb64Cols, &Sb64Rows);
2346 			vp9_render_size(bs, *FrameWidth, *FrameHeight, renderWidth, renderHeight);
2347 		}
2348 		else {
2349 			refresh_frame_flags = gf_bs_read_int(bs, 8);
2350 			u8 ref_frame_idx[3];
2351 			for (i = 0; i < 3; i++) {
2352 				ref_frame_idx[i] = gf_bs_read_int(bs, 3);
2353 				/*ref_frame_sign_bias[LAST_FRAME + i] = */gf_bs_read_int(bs, 1);
2354 			}
2355 			vp9_frame_size_with_refs(bs, refresh_frame_flags, ref_frame_idx, vp9_cfg->RefFrameWidth, vp9_cfg->RefFrameHeight, FrameWidth, FrameHeight, renderWidth, renderHeight, &Sb64Cols, &Sb64Rows);
2356 			/*allow_high_precision_mv = */gf_bs_read_int(bs, 1);
2357 			vp9_read_interpolation_filter(bs);
2358 		}
2359 	}
2360 
2361 	if (error_resilient_mode == 0) {
2362 		/*refresh_frame_context = */gf_bs_read_int(bs, 1);
2363 		/*frame_parallel_decoding_mode = */gf_bs_read_int(bs, 1);
2364 	}
2365 
2366 	/*frame_context_idx = */gf_bs_read_int(bs, 2);
2367 	if (FrameIsIntra || error_resilient_mode) {
2368 		/*setup_past_independence + save_probs ...*/
2369 		//frame_context_idx = 0;
2370 	}
2371 
2372 	vp9_loop_filter_params(bs);
2373 	vp9_quantization_params(bs);
2374 	vp9_segmentation_params(bs);
2375 	vp9_tile_info(bs, Sb64Cols);
2376 
2377 	/*header_size_in_bytes = */gf_bs_read_int(bs, 16);
2378 
2379 	/*Reference frame update process (8.10 - partial)*/
2380 	for (i = 0; i < VP9_NUM_REF_FRAMES; i++) {
2381 		if ((refresh_frame_flags >> i) & 1) {
2382 			vp9_cfg->RefFrameWidth[i] = *FrameWidth;
2383 			vp9_cfg->RefFrameHeight[i] = *FrameHeight;
2384 		}
2385 	}
2386 
2387 	return GF_OK;
2388 }
2389 
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)2390 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)
2391 {
2392 	Bool forbidden = gf_bs_read_int(bs, 1);
2393 	if (forbidden) {
2394 		return GF_NON_COMPLIANT_BITSTREAM;
2395 	}
2396 
2397 	*obu_type = gf_bs_read_int(bs, 4);
2398 	*obu_extension_flag = gf_bs_read_int(bs, 1);
2399 	*obu_has_size_field = gf_bs_read_int(bs, 1);
2400 	if (gf_bs_read_int(bs, 1) /*obu_reserved_1bit*/) {
2401 		return GF_NON_COMPLIANT_BITSTREAM;
2402 	}
2403 	if (*obu_extension_flag) {
2404 		*temporal_id = gf_bs_read_int(bs, 3);
2405 		*spatial_id = gf_bs_read_int(bs, 2);
2406 		/*extension_header_reserved_3bits = */gf_bs_read_int(bs, 3);
2407 	}
2408 
2409 	return GF_OK;
2410 }
2411 
2412 #endif // GPAC_DISABLE_AV_PARSERS
2413 
2414 GF_EXPORT
gf_av1_get_obu_name(ObuType obu_type)2415 const char *gf_av1_get_obu_name(ObuType obu_type)
2416 {
2417 	switch (obu_type) {
2418 	case OBU_SEQUENCE_HEADER: return "seq_header";
2419 	case OBU_TEMPORAL_DELIMITER: return "delimiter";
2420 	case OBU_FRAME_HEADER: return "frame_header";
2421 	case OBU_TILE_GROUP: return "tile_group";
2422 	case OBU_METADATA: return "metadata";
2423 	case OBU_FRAME: return "frame";
2424 	case OBU_REDUNDANT_FRAME_HEADER: return "redundant_frame_header";
2425 	case OBU_TILE_LIST: return "tile_list";
2426 	case OBU_PADDING: return "padding";
2427 	case OBU_RESERVED_0:
2428 	case OBU_RESERVED_9:
2429 	case OBU_RESERVED_10:
2430 	case OBU_RESERVED_11:
2431 	case OBU_RESERVED_12:
2432 	case OBU_RESERVED_13:
2433 	case OBU_RESERVED_14:
2434 		return "reserved";
2435 	default: return "unknown";
2436 	}
2437 }
2438 
av1_is_obu_header(ObuType obu_type)2439 Bool av1_is_obu_header(ObuType obu_type) {
2440 	switch (obu_type) {
2441 	case OBU_SEQUENCE_HEADER:
2442 	case OBU_METADATA:
2443 		// TODO add check based on the metadata type
2444 		return GF_TRUE;
2445 	default:
2446 		return GF_FALSE;
2447 	}
2448 }
2449 
2450 #ifndef GPAC_DISABLE_AV_PARSERS
2451 
av1_is_obu_frame(AV1State * state,ObuType obu_type)2452 static Bool av1_is_obu_frame(AV1State *state, ObuType obu_type)
2453 {
2454 	switch (obu_type) {
2455 	case OBU_PADDING:
2456 	case OBU_REDUNDANT_FRAME_HEADER:
2457 		return GF_FALSE;
2458 	case OBU_TEMPORAL_DELIMITER:
2459 		return state->keep_temporal_delim ? GF_TRUE : GF_FALSE;
2460 	default:
2461 		return GF_TRUE;
2462 	}
2463 }
2464 
gf_av1_leb128_read(GF_BitStream * bs,u8 * opt_Leb128Bytes)2465 u64 gf_av1_leb128_read(GF_BitStream *bs, u8 *opt_Leb128Bytes) {
2466 	u64 value = 0;
2467 	u8 Leb128Bytes = 0, i = 0;
2468 	for (i = 0; i < 8; i++) {
2469 		u8 leb128_byte = gf_bs_read_u8(bs);
2470 		value |= ( ((u64) (leb128_byte & 0x7f)) << (i * 7));
2471 		Leb128Bytes += 1;
2472 		if (!(leb128_byte & 0x80)) {
2473 			break;
2474 		}
2475 	}
2476 
2477 	if (opt_Leb128Bytes) {
2478 		*opt_Leb128Bytes = Leb128Bytes;
2479 	}
2480 	return value;
2481 }
2482 
gf_av1_leb128_size(u64 value)2483 u32 gf_av1_leb128_size(u64 value)
2484 {
2485 	u32 gf_av1_leb128_size = 0;
2486 	do {
2487 		++gf_av1_leb128_size;
2488 	} while ((value >>= 7) != 0);
2489 
2490 	return gf_av1_leb128_size;
2491 }
2492 
gf_av1_leb128_write(GF_BitStream * bs,u64 value)2493 u64 gf_av1_leb128_write(GF_BitStream *bs, u64 value)
2494 {
2495 	u32 i, leb_size = gf_av1_leb128_size(value);
2496 	for (i = 0; i < leb_size; ++i) {
2497 		u8 byte = value & 0x7f;
2498 		value >>= 7;
2499 		if (value != 0) byte |= 0x80; //more bytes follow
2500 		gf_bs_write_u8(bs, byte);
2501 	}
2502 
2503 	return leb_size;
2504 }
2505 
2506 #define OBU_BLOCK_SIZE 4096
av1_add_obu_internal(GF_BitStream * bs,u64 pos,u64 obu_length,ObuType obu_type,GF_List ** obu_list,AV1State * state)2507 static void av1_add_obu_internal(GF_BitStream *bs, u64 pos, u64 obu_length, ObuType obu_type, GF_List **obu_list, AV1State *state)
2508 {
2509 	char block[OBU_BLOCK_SIZE];
2510 	Bool has_size_field = 0, obu_extension_flag = 0;
2511 	u8 temporal_id, spatial_id;
2512 	GF_AV1_OBUArrayEntry *a = NULL;
2513 
2514 	if (state && state->mem_mode) {
2515 		if (!state->bs) state->bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
2516 		else gf_bs_reassign_buffer(state->bs, state->frame_obus, state->frame_obus_alloc);
2517 	}
2518 	else {
2519 		GF_SAFEALLOC(a, GF_AV1_OBUArrayEntry);
2520 		if (!a) {
2521 			GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[AV1] Failed to allocate OBU\n"));
2522 			return;
2523 		}
2524 	}
2525 
2526 	gf_bs_seek(bs, pos);
2527 	gf_av1_parse_obu_header(bs, &obu_type, &obu_extension_flag, &has_size_field, &temporal_id, &spatial_id);
2528 	gf_bs_seek(bs, pos);
2529 
2530 	if (has_size_field) {
2531 		if (a) {
2532 			a->obu = gf_malloc((size_t)obu_length);
2533 			gf_bs_read_data(bs, a->obu, (u32)obu_length);
2534 			a->obu_length = obu_length;
2535 		}
2536 		else {
2537 			u32 remain = (u32)obu_length;
2538 			while (remain) {
2539 				u32 block_size = OBU_BLOCK_SIZE;
2540 				if (block_size > remain) block_size = remain;
2541 				gf_bs_read_data(bs, block, block_size);
2542 				gf_bs_write_data(state->bs, block, block_size);
2543 				remain -= block_size;
2544 			}
2545 			return;
2546 		}
2547 	}
2548 	else {
2549 		u8 i, hdr_size = obu_extension_flag ? 2 : 1;
2550 		const u32 leb_size = (u32)gf_av1_leb128_size(obu_length);
2551 		const u64 obu_size = obu_length - hdr_size;
2552 
2553 		if (a) {
2554 			a->obu = gf_malloc((size_t)obu_length + leb_size);
2555 			a->obu_length = obu_length + leb_size;
2556 			for (i = 0; i < hdr_size; ++i) {
2557 				a->obu[i] = gf_bs_read_u8(bs);
2558 				/*add size field flag*/
2559 				if (i == 0) a->obu[0] |= 0x02;
2560 			}
2561 			{
2562 				u32 out_size = 0;
2563 				u8 *output = NULL;
2564 				GF_BitStream *bsLeb128 = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
2565 				/*write size field*/
2566 				gf_av1_leb128_write(bsLeb128, obu_size);
2567 				assert(gf_bs_get_position(bsLeb128) == leb_size);
2568 				gf_bs_get_content(bsLeb128, &output, &out_size);
2569 				gf_bs_del(bsLeb128);
2570 				memcpy(a->obu + hdr_size, output, out_size);
2571 				gf_free(output);
2572 			}
2573 			gf_bs_read_data(bs, a->obu + hdr_size + leb_size, (u32)(obu_size));
2574 			assert(gf_bs_get_position(bs) == pos + obu_length);
2575 		}
2576 		else {
2577 			u32 remain;
2578 			for (i = 0; i < hdr_size; ++i) {
2579 				u8 hdr_b = gf_bs_read_u8(bs);
2580 				if (i == 0) hdr_b |= 0x02; /*add size field flag*/
2581 				gf_bs_write_u8(state->bs, hdr_b);
2582 			}
2583 			/*add size field */
2584 			gf_av1_leb128_write(state->bs, obu_size);
2585 			remain = (u32)obu_length - hdr_size;
2586 			while (remain) {
2587 				u32 block_size = OBU_BLOCK_SIZE;
2588 				if (block_size > remain) block_size = remain;
2589 				gf_bs_read_data(bs, block, block_size);
2590 				gf_bs_write_data(state->bs, block, block_size);
2591 				remain -= block_size;
2592 			}
2593 			assert(gf_bs_get_position(bs) == pos + obu_length);
2594 			return;
2595 		}
2596 	}
2597 	if (!obu_list) {
2598 		GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[AV1] internal error, no OBU list cannot add\n"));
2599 		gf_free(a->obu);
2600 		gf_free(a);
2601 		return;
2602 	}
2603 	a->obu_type = obu_type;
2604 	if (! *obu_list)
2605 		*obu_list = gf_list_new();
2606 	gf_list_add(*obu_list, a);
2607 }
2608 
av1_populate_state_from_obu(GF_BitStream * bs,u64 pos,u64 obu_length,ObuType obu_type,AV1State * state)2609 static void av1_populate_state_from_obu(GF_BitStream *bs, u64 pos, u64 obu_length, ObuType obu_type, AV1State *state)
2610 {
2611 	if (av1_is_obu_header(obu_type)) {
2612 		av1_add_obu_internal(bs, pos, obu_length, obu_type, &state->frame_state.header_obus, NULL);
2613 	}
2614 	if (!state->skip_frames && av1_is_obu_frame(state, obu_type)) {
2615 		if (!state->mem_mode) {
2616 			av1_add_obu_internal(bs, pos, obu_length, obu_type, &state->frame_state.frame_obus, NULL);
2617 		}
2618 		else {
2619 			av1_add_obu_internal(bs, pos, obu_length, obu_type, NULL, state);
2620 		}
2621 	}
2622 }
2623 
aom_av1_parse_temporal_unit_from_section5(GF_BitStream * bs,AV1State * state)2624 GF_Err aom_av1_parse_temporal_unit_from_section5(GF_BitStream *bs, AV1State *state)
2625 {
2626 	if (!state) return GF_BAD_PARAM;
2627 	state->obu_type = -1;
2628 
2629 	while (state->obu_type != OBU_TEMPORAL_DELIMITER) {
2630 		GF_Err e;
2631 		if (!gf_bs_available(bs))
2632 			return state->unframed ? GF_BUFFER_TOO_SMALL : GF_OK;
2633 
2634 		u64 pos = gf_bs_get_position(bs), obu_length = 0;
2635 
2636 		e = gf_media_aom_av1_parse_obu(bs, &state->obu_type, &obu_length, NULL, state);
2637 		if (e)
2638 			return e;
2639 
2640 		if (obu_length != gf_bs_get_position(bs) - pos) {
2641 			GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[AV1] OBU (Section 5) frame size "LLU" different from consumed bytes "LLU".\n", obu_length, gf_bs_get_position(bs) - pos));
2642 			return GF_NON_COMPLIANT_BITSTREAM;
2643 		}
2644 
2645 		GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("[AV1] Section5 OBU detected (size "LLU")\n", obu_length));
2646 		av1_populate_state_from_obu(bs, pos, obu_length, state->obu_type, state);
2647 	}
2648 
2649 	return GF_OK;
2650 }
2651 
gf_media_aom_probe_annexb(GF_BitStream * bs)2652 Bool gf_media_aom_probe_annexb(GF_BitStream *bs)
2653 {
2654 	Bool res = GF_TRUE;
2655 	u64 init_pos = gf_bs_get_position(bs);
2656 	u64 sz = gf_av1_leb128_read(bs, NULL);
2657 	if (!sz) res = GF_FALSE;
2658 	while (sz > 0) {
2659 		u8 Leb128Bytes = 0;
2660 		u64 frame_unit_size = gf_av1_leb128_read(bs, &Leb128Bytes);
2661 
2662 		if (!frame_unit_size) {
2663 			res = GF_FALSE;
2664 			break;
2665 		}
2666 
2667 		if (sz < Leb128Bytes + frame_unit_size) {
2668 			res = GF_FALSE;
2669 			break;
2670 		}
2671 		sz -= Leb128Bytes + frame_unit_size;
2672 
2673 		while (frame_unit_size > 0) {
2674 			ObuType obu_type;
2675 			u64 pos, obu_length = gf_av1_leb128_read(bs, &Leb128Bytes);
2676 			if (frame_unit_size < Leb128Bytes + obu_length) {
2677 				res = GF_FALSE;
2678 				break;
2679 			}
2680 			pos = gf_bs_get_position(bs);
2681 			frame_unit_size -= Leb128Bytes;
2682 
2683 			u8 tid, sid;
2684 			Bool extflag, has_size;
2685 			GF_Err e = gf_av1_parse_obu_header(bs, &obu_type, &extflag, &has_size, &tid, &sid);
2686 			if (e) {
2687 				res = GF_FALSE;
2688 				break;
2689 			}
2690 
2691 			if (has_size) {
2692 				obu_length = (u32)gf_av1_leb128_read(bs, NULL);
2693 			}
2694 			else {
2695 				if (obu_length >= 1 + extflag) {
2696 					obu_length = obu_length - 1 - extflag;
2697 				}
2698 				else {
2699 					res = GF_FALSE;
2700 					break;
2701 				}
2702 			}
2703 			u32 hdr_size = (u32)(gf_bs_get_position(bs) - pos);
2704 			obu_length += hdr_size;
2705 
2706 			if (frame_unit_size < obu_length) {
2707 				res = GF_FALSE;
2708 				break;
2709 			}
2710 			frame_unit_size -= obu_length;
2711 			gf_bs_skip_bytes(bs, obu_length - hdr_size);
2712 		}
2713 		if (!res) break;
2714 	}
2715 	gf_bs_seek(bs, init_pos);
2716 	return res;
2717 }
2718 
aom_av1_parse_temporal_unit_from_annexb(GF_BitStream * bs,AV1State * state)2719 GF_Err aom_av1_parse_temporal_unit_from_annexb(GF_BitStream *bs, AV1State *state)
2720 {
2721 	GF_Err e;
2722 	u64 tupos;
2723 	u64 tusize, sz;
2724 	if (!bs || !state) return GF_BAD_PARAM;
2725 
2726 	state->bs_overread = GF_FALSE;
2727 	tusize = sz = gf_av1_leb128_read(bs, NULL);
2728 	tupos = gf_bs_get_position(bs);
2729 	if (!sz) {
2730 		GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[AV1] temporal unit size is 0, likely not annex B\n"));
2731 		return GF_NON_COMPLIANT_BITSTREAM;
2732 	}
2733 
2734 	GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("[AV1] Annex B temporal unit detected (size "LLU") ***** \n", sz));
2735 	while (sz > 0) {
2736 		u8 Leb128Bytes = 0;
2737 		u64 frame_unit_size = gf_av1_leb128_read(bs, &Leb128Bytes);
2738 
2739 		if (state->bs_overread) {
2740 			return GF_BUFFER_TOO_SMALL;
2741 		}
2742 		if (sz < Leb128Bytes + frame_unit_size) {
2743 			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));
2744 			return GF_NON_COMPLIANT_BITSTREAM;
2745 		}
2746 		GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("[AV1] Annex B frame unit detected (size "LLU")\n", frame_unit_size));
2747 		sz -= Leb128Bytes + frame_unit_size;
2748 
2749 		while (frame_unit_size > 0) {
2750 			u64 pos, obu_length = gf_av1_leb128_read(bs, &Leb128Bytes);
2751 
2752 			if (state->bs_overread) {
2753 				return GF_BUFFER_TOO_SMALL;
2754 			}
2755 			if (frame_unit_size < Leb128Bytes + obu_length) {
2756 				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_length));
2757 				return GF_NON_COMPLIANT_BITSTREAM;
2758 			}
2759 			GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("[AV1] Annex B OBU detected (size "LLU")\n", obu_length));
2760 			pos = gf_bs_get_position(bs);
2761 			frame_unit_size -= Leb128Bytes;
2762 
2763 			e = gf_media_aom_av1_parse_obu(bs, &state->obu_type, &obu_length, NULL, state);
2764 			if (e) return e;
2765 
2766 			if (obu_length != gf_bs_get_position(bs) - pos) {
2767 				GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[AV1] Annex B frame size "LLU" different from consumed bytes "LLU".\n", obu_length, gf_bs_get_position(bs) - pos));
2768 				return GF_NON_COMPLIANT_BITSTREAM;
2769 			}
2770 
2771 			av1_populate_state_from_obu(bs, pos, obu_length, state->obu_type, state);
2772 			if (frame_unit_size < obu_length) {
2773 				GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] Annex B frame_unit_size("LLU") < OBU size ("LLU")\n", frame_unit_size, obu_length));
2774 				return GF_NON_COMPLIANT_BITSTREAM;
2775 			}
2776 			frame_unit_size -= obu_length;
2777 		}
2778 	}
2779 	assert(sz == 0);
2780 	if (tusize != gf_bs_get_position(bs) - tupos) {
2781 		GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[AV1] Annex B TU size "LLU" different from consumed bytes "LLU".\n", tusize, gf_bs_get_position(bs) - tupos));
2782 		return GF_NON_COMPLIANT_BITSTREAM;
2783 	}
2784 	return GF_OK;
2785 }
2786 
aom_av1_parse_temporal_unit_from_ivf(GF_BitStream * bs,AV1State * state)2787 GF_Err aom_av1_parse_temporal_unit_from_ivf(GF_BitStream *bs, AV1State *state)
2788 {
2789 	u64 frame_size, pts_ignored;
2790 	GF_Err e;
2791 	if (gf_bs_available(bs)<12) return GF_EOS;
2792 	e = gf_media_parse_ivf_frame_header(bs, &frame_size, &pts_ignored);
2793 	if (e) return e;
2794 	GF_LOG(GF_LOG_DEBUG, GF_LOG_CONTAINER, ("[AV1] IVF frame detected (size "LLU")\n", frame_size));
2795 
2796 	if (gf_bs_available(bs) < frame_size) return GF_EOS;
2797 
2798 	while (frame_size > 0) {
2799 		u64 obu_size = 0, pos = gf_bs_get_position(bs);
2800 
2801 		e = gf_media_aom_av1_parse_obu(bs, &state->obu_type, &obu_size, NULL, state);
2802 		if (e != GF_OK)
2803 			return e;
2804 
2805 		if (obu_size != gf_bs_get_position(bs) - pos) {
2806 			GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[AV1] IVF frame size "LLU" different from consumed bytes "LLU".\n", obu_size, gf_bs_get_position(bs) - pos));
2807 			return GF_NON_COMPLIANT_BITSTREAM;
2808 		}
2809 
2810 		av1_populate_state_from_obu(bs, pos, obu_size, state->obu_type, state);
2811 
2812 		frame_size -= obu_size;
2813 	}
2814 	return GF_OK;
2815 }
2816 
2817 #define AV1_NUM_REF_FRAMES 8
2818 #define AV1_ALL_FRAMES ((1 << AV1_NUM_REF_FRAMES) - 1)
2819 
2820 #define AV1_SUPERRES_DENOM_MIN 9
2821 #define AV1_SUPERRES_DENOM_BITS 3
2822 #define AV1_SUPERRES_NUM 8
2823 
2824 #define AV1_REFS_PER_FRAME 7
2825 #define AV1_PRIMARY_REF_NONE 7
2826 
2827 #define MAX_TILE_WIDTH 4096
2828 #define MAX_TILE_AREA (4096 * 2304)
2829 
aom_av1_tile_log2(u32 blkSize,u32 target)2830 static u32 aom_av1_tile_log2(u32 blkSize, u32 target)
2831 {
2832 	u32 k;
2833 	for (k = 0; (blkSize << k) < target; k++) {
2834 	}
2835 	return k;
2836 }
2837 
aom_av1_le(GF_BitStream * bs,u32 n)2838 static u64 aom_av1_le(GF_BitStream *bs, u32 n) {
2839 	u32 i = 0;
2840 	u64 t = 0;
2841 	for (i = 0; i < n; i++) {
2842 		u8 byte = gf_bs_read_int(bs, 8);
2843 		t += (byte << (i * 8));
2844 	}
2845 	return t;
2846 }
2847 
2848 
av1_parse_tile_info(GF_BitStream * bs,AV1State * state)2849 static void av1_parse_tile_info(GF_BitStream *bs, AV1State *state)
2850 {
2851 	u32 i;
2852 	u32 MiCols = 2 * ((state->width + 7) >> 3);
2853 	u32 MiRows = 2 * ((state->height + 7) >> 3);
2854 	u32 sbCols = state->use_128x128_superblock ? ((MiCols + 31) >> 5) : ((MiCols + 15) >> 4);
2855 	u32 sbRows = state->use_128x128_superblock ? ((MiRows + 31) >> 5) : ((MiRows + 15) >> 4);
2856 	u32 sbShift = state->use_128x128_superblock ? 5 : 4;
2857 	u32 sbSize = sbShift + 2;
2858 	u32 maxTileWidthSb = MAX_TILE_WIDTH >> sbSize;
2859 	u32 maxTileAreaSb = MAX_TILE_AREA >> (2 * sbSize);
2860 	u32 minLog2tileCols = aom_av1_tile_log2(maxTileWidthSb, sbCols);
2861 	u32 maxLog2tileCols = aom_av1_tile_log2(1, MIN(sbCols, AV1_MAX_TILE_COLS));
2862 	u32 maxLog2tileRows = aom_av1_tile_log2(1, MIN(sbRows, AV1_MAX_TILE_ROWS));
2863 	u32 minLog2Tiles = MAX(minLog2tileCols, aom_av1_tile_log2(maxTileAreaSb, sbRows * sbCols));
2864 	Bool uniform_tile_spacing_flag = gf_bs_read_int(bs, 1);
2865 	if (uniform_tile_spacing_flag) {
2866 		u32 startSb, tileWidthSb, tileHeightSb, minLog2tileRows;
2867 		state->tileColsLog2 = minLog2tileCols;
2868 		while (state->tileColsLog2 < maxLog2tileCols) {
2869 			Bool increment_tile_cols_log2 = gf_bs_read_int(bs, 1);
2870 			if (increment_tile_cols_log2 == 1)
2871 				state->tileColsLog2++;
2872 			else
2873 				break;
2874 		}
2875 
2876 		tileWidthSb = (sbCols + (1 << state->tileColsLog2) - 1) >> state->tileColsLog2;
2877 		i = 0;
2878 		for (startSb = 0; startSb < sbCols; startSb += tileWidthSb) {
2879 			i += 1;
2880 		}
2881 		state->tileCols = i;
2882 		minLog2tileRows = MAX((int)(minLog2Tiles - state->tileColsLog2), 0);
2883 		state->tileRowsLog2 = minLog2tileRows;
2884 		while (state->tileRowsLog2 < maxLog2tileRows) {
2885 			Bool increment_tile_rows_log2 = gf_bs_read_int(bs, 1);
2886 			if (increment_tile_rows_log2 == 1)
2887 				state->tileRowsLog2++;
2888 			else
2889 				break;
2890 		}
2891 
2892 		tileHeightSb = (sbRows + (1 << state->tileRowsLog2) - 1) >> state->tileRowsLog2;
2893 		i = 0;
2894 		for (startSb = 0; startSb < sbRows; startSb += tileHeightSb) {
2895 			i += 1;
2896 		}
2897 		state->tileRows = i;
2898 	}
2899 	else {
2900 		u32 startSb, maxTileHeightSb, widestTileSb;
2901 		widestTileSb = 0;
2902 		startSb = 0;
2903 		for (i = 0; startSb < sbCols; i++) {
2904 			u32 maxWidth = MIN((int)(sbCols - startSb), maxTileWidthSb);
2905 			u32 width_in_sbs_minus_1 = av1_read_ns(bs, maxWidth);
2906 			u32 sizeSb = width_in_sbs_minus_1 + 1;
2907 			widestTileSb = MAX(sizeSb, widestTileSb);
2908 			startSb += sizeSb;
2909 		}
2910 		if (!widestTileSb) {
2911 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] widest tile is 0, broken bitstream\n"));
2912 			return;
2913 		}
2914 		state->tileCols = i;
2915 		state->tileColsLog2 = aom_av1_tile_log2(1, state->tileCols);
2916 
2917 		if (minLog2Tiles > 0)
2918 			maxTileAreaSb = (sbRows * sbCols) >> (minLog2Tiles + 1);
2919 		else
2920 			maxTileAreaSb = sbRows * sbCols;
2921 		maxTileHeightSb = MAX(maxTileAreaSb / widestTileSb, 1);
2922 
2923 		startSb = 0;
2924 		for (i = 0; startSb < sbRows; i++) {
2925 			u32 maxHeight = MIN((int)(sbRows - startSb), maxTileHeightSb);
2926 			u32 height_in_sbs_minus_1 = av1_read_ns(bs, maxHeight);
2927 			u32 sizeSb = height_in_sbs_minus_1 + 1;
2928 			startSb += sizeSb;
2929 		}
2930 
2931 		state->tileRows = i;
2932 		state->tileRowsLog2 = aom_av1_tile_log2(1, state->tileRows);
2933 	}
2934 	if (state->tileColsLog2 > 0 || state->tileRowsLog2 > 0) {
2935 		/*context_update_tile_id = */gf_bs_read_int(bs, state->tileRowsLog2 + state->tileColsLog2);
2936 		state->tile_size_bytes = gf_bs_read_int(bs, 2) + 1;
2937 	}
2938 }
2939 
superres_params(GF_BitStream * bs,AV1State * state)2940 static void superres_params(GF_BitStream *bs, AV1State *state)
2941 {
2942 	u32 SuperresDenom;
2943 	Bool use_superres;
2944 
2945 	if (state->enable_superres) {
2946 		use_superres = gf_bs_read_int(bs, 1);
2947 	}
2948 	else {
2949 		use_superres = GF_FALSE;
2950 	}
2951 	if (use_superres) {
2952 		u8 coded_denom = gf_bs_read_int(bs, AV1_SUPERRES_DENOM_BITS);
2953 		SuperresDenom = coded_denom + AV1_SUPERRES_DENOM_MIN;
2954 	}
2955 	else {
2956 		SuperresDenom = AV1_SUPERRES_NUM;
2957 	}
2958 	state->UpscaledWidth = state->width;
2959 	state->width = (state->UpscaledWidth * AV1_SUPERRES_NUM + (SuperresDenom / 2)) / SuperresDenom;
2960 }
2961 
av1_frame_size(GF_BitStream * bs,AV1State * state,Bool frame_size_override_flag)2962 static void av1_frame_size(GF_BitStream *bs, AV1State *state, Bool frame_size_override_flag)
2963 {
2964 	if (frame_size_override_flag) {
2965 		u32 frame_width_minus_1, frame_height_minus_1;
2966 		u8 n = state->frame_width_bits_minus_1 + 1;
2967 		frame_width_minus_1 = gf_bs_read_int(bs, n);
2968 		n = state->frame_height_bits_minus_1 + 1;
2969 		frame_height_minus_1 = gf_bs_read_int(bs, n);
2970 		state->width = frame_width_minus_1 + 1;
2971 		state->height = frame_height_minus_1 + 1;
2972 	}
2973 	superres_params(bs, state);
2974 	//compute_image_size(); //no bits
2975 }
2976 
av1_render_size(GF_BitStream * bs)2977 static void av1_render_size(GF_BitStream *bs)
2978 {
2979 	Bool render_and_frame_size_different = gf_bs_read_int(bs, 1);
2980 	if (render_and_frame_size_different == GF_TRUE) {
2981 		/*render_width_minus_1 =*/ gf_bs_read_int(bs, 16);
2982 		/*render_height_minus_1 =*/ gf_bs_read_int(bs, 16);
2983 		//RenderWidth = render_width_minus_1 + 1;
2984 		//RenderHeight = render_height_minus_1 + 1;
2985 	}
2986 	else {
2987 		//RenderWidth = UpscaledWidth;
2988 		//RenderHeight = FrameHeight;
2989 	}
2990 }
2991 
read_interpolation_filter(GF_BitStream * bs)2992 static void read_interpolation_filter(GF_BitStream *bs)
2993 {
2994 	Bool is_filter_switchable = gf_bs_read_int(bs, 1);
2995 	if (!is_filter_switchable) {
2996 		/*interpolation_filter =*/ gf_bs_read_int(bs, 2);
2997 	}
2998 }
2999 
frame_size_with_refs(GF_BitStream * bs,AV1State * state,Bool frame_size_override_flag)3000 static void frame_size_with_refs(GF_BitStream *bs, AV1State *state, Bool frame_size_override_flag)
3001 {
3002 	Bool found_ref = GF_FALSE;
3003 	u32 i = 0;
3004 	for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
3005 		found_ref = gf_bs_read_int(bs, 1);
3006 		if (found_ref == 1) {
3007 #if 0
3008 			UpscaledWidth = RefUpscaledWidth[ref_frame_idx[i]];
3009 			FrameWidth = UpscaledWidth;
3010 			FrameHeight = RefFrameHeight[ref_frame_idx[i]];
3011 			RenderWidth = RefRenderWidth[ref_frame_idx[i]];
3012 			RenderHeight = RefRenderHeight[ref_frame_idx[i]];
3013 #endif
3014 			break;
3015 		}
3016 	}
3017 	if (found_ref == 0) {
3018 		av1_frame_size(bs, state, frame_size_override_flag);
3019 		av1_render_size(bs);
3020 	}
3021 	else {
3022 		superres_params(bs, state);
3023 		//compute_image_size();
3024 	}
3025 }
3026 
av1_delta_q(GF_BitStream * bs)3027 static s32 av1_delta_q(GF_BitStream *bs)
3028 {
3029 	Bool delta_coded = gf_bs_read_int(bs, 1);
3030 	s32 delta_q = 0;
3031 	if (delta_coded) {
3032 		u32 signMask = 1 << (7 - 1);
3033 		delta_q = gf_bs_read_int(bs, 7);
3034 		if (delta_q & signMask)
3035 			delta_q = delta_q - 2 * signMask;
3036 	}
3037 	return delta_q;
3038 }
3039 
3040 static u8 Segmentation_Feature_Bits[] = { 8,6,6,6,6,3,0,0 };
3041 static u8 Segmentation_Feature_Signed[] = { 1, 1, 1, 1, 1, 0, 0, 0 };
3042 
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)3043 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)
3044 {
3045 	//If seg_feature_active_idx( segmentId, SEG_LVL_ALT_Q ) is equal to 1 the following ordered steps apply:
3046 	if (segmentation_enabled && features_SEG_LVL_ALT_Q_enabled[segmentId]) {
3047 		//Set the variable data equal to FeatureData[ segmentId ][ SEG_LVL_ALT_Q ].
3048 		s32 data = features_SEG_LVL_ALT_Q[segmentId];
3049 		s32 qindex = base_q_idx + data;
3050 		//If ignoreDeltaQ is equal to 0 and delta_q_present is equal to 1, set qindex equal to CurrentQIndex + data.
3051 		if ((ignoreDeltaQ == 0) && (delta_q_present == 1)) qindex = CurrentQIndex + data;
3052 		//Return Clip3( 0, 255, qindex ).
3053 		if (qindex < 0) return 0;
3054 		else if (qindex > 255) return 255;
3055 		else return (u8)qindex;
3056 	}
3057 	//Otherwise, if ignoreDeltaQ is equal to 0 and delta_q_present is equal to 1, return CurrentQIndex.
3058 	if ((ignoreDeltaQ == 0) && (delta_q_present == 1)) return CurrentQIndex;
3059 	//otherwise
3060 	return base_q_idx;
3061 }
3062 
3063 enum {
3064 	AV1_RESTORE_NONE = 0,
3065 	AV1_RESTORE_SWITCHABLE,
3066 	AV1_RESTORE_WIENER,
3067 	AV1_RESTORE_SGRPROJ
3068 };
3069 
3070 #define AV1_GMC_IDENTITY  0
3071 #define AV1_GMC_TRANSLATION 1
3072 #define AV1_GMC_ROTZOOM 2
3073 #define AV1_GMC_AFFINE 3
3074 
3075 #define AV1_LAST_FRAME 1
3076 #define AV1_LAST2_FRAME 2
3077 #define AV1_LAST3_FRAME 3
3078 #define AV1_GOLDEN_FRAME 4
3079 #define AV1_BWDREF_FRAME 5
3080 #define AV1_ALTREF2_FRAME 6
3081 #define AV1_ALTREF_FRAME 7
3082 
3083 #define GM_ABS_ALPHA_BITS 12
3084 #define GM_ALPHA_PREC_BITS 15
3085 #define GM_ABS_TRANS_ONLY_BITS 9
3086 #define GM_TRANS_ONLY_PREC_BITS 3
3087 #define GM_ABS_TRANS_BITS 12
3088 #define GM_TRANS_PREC_BITS 6
3089 #define WARPEDMODEL_PREC_BITS 16
3090 
3091 
av1_decode_subexp(GF_BitStream * bs,s32 numSyms)3092 static u32 av1_decode_subexp(GF_BitStream *bs, s32 numSyms)
3093 {
3094 	s32 i = 0;
3095 	s32 mk = 0;
3096 	s32 k = 3;
3097 	while (1) {
3098 		s32 b2 = i ? k + i - 1 : k;
3099 		s32 a = 1 << b2;
3100 		if (numSyms <= mk + 3 * a) {
3101 			s32 subexp_final_bits = av1_read_ns(bs, numSyms - mk);
3102 			return subexp_final_bits + mk;
3103 		}
3104 		else {
3105 			s32 subexp_more_bits = gf_bs_read_int(bs, 1);
3106 			if (subexp_more_bits) {
3107 				i++;
3108 				mk += a;
3109 			}
3110 			else {
3111 				s32 subexp_bits = gf_bs_read_int(bs, b2);
3112 				return subexp_bits + mk;
3113 			}
3114 		}
3115 	}
3116 }
3117 
inverse_recenter(s32 r,u32 v)3118 static GFINLINE s32 inverse_recenter(s32 r, u32 v)
3119 {
3120 	if ((s64)v > (s64)(2 * r))
3121 		return v;
3122 	else if (v & 1)
3123 		return r - ((v + 1) >> 1);
3124 	else
3125 		return r + (v >> 1);
3126 }
3127 
av1_decode_unsigned_subexp_with_ref(GF_BitStream * bs,s32 mx,s32 r)3128 static s32 av1_decode_unsigned_subexp_with_ref(GF_BitStream *bs, s32 mx, s32 r)
3129 {
3130 	u32 v = av1_decode_subexp(bs, mx);
3131 	if ((r < 0) && (-(-r << 1) <= mx)) {
3132 		return inverse_recenter(r, v);
3133 	}
3134 	else if ((r << 1) <= mx) {
3135 		return inverse_recenter(r, v);
3136 	}
3137 	else {
3138 		return mx - 1 - inverse_recenter(mx - 1 - r, v);
3139 	}
3140 }
av1_decode_signed_subexp_with_ref(GF_BitStream * bs,s32 low,s32 high,s32 r)3141 static s16 av1_decode_signed_subexp_with_ref(GF_BitStream *bs, s32 low, s32 high, s32 r)
3142 {
3143 	s16 x = av1_decode_unsigned_subexp_with_ref(bs, high - low, r - low);
3144 	return x + low;
3145 }
3146 
av1_read_global_param(AV1State * state,GF_BitStream * bs,u8 type,u8 ref,u8 idx)3147 static void av1_read_global_param(AV1State *state, GF_BitStream *bs, u8 type, u8 ref, u8 idx)
3148 {
3149 	u8 absBits = GM_ABS_ALPHA_BITS;
3150 	u8 precBits = GM_ALPHA_PREC_BITS;
3151 	if (idx < 2) {
3152 		if (type == AV1_GMC_TRANSLATION) {
3153 			absBits = GM_ABS_TRANS_ONLY_BITS - (!state->frame_state.allow_high_precision_mv ? 1 : 0);
3154 			precBits = GM_TRANS_ONLY_PREC_BITS - (!state->frame_state.allow_high_precision_mv ? 1 : 0);
3155 		}
3156 		else {
3157 			absBits = GM_ABS_TRANS_BITS;
3158 			precBits = GM_TRANS_PREC_BITS;
3159 		}
3160 	}
3161 	s32 precDiff = WARPEDMODEL_PREC_BITS - precBits;
3162 	s32 round = (idx % 3) == 2 ? (1 << WARPEDMODEL_PREC_BITS) : 0;
3163 	s32 sub = (idx % 3) == 2 ? (1 << precBits) : 0;
3164 	s32 mx = (1 << absBits);
3165 	s32 r = (state->PrevGmParams.coefs[ref][idx] >> precDiff) - sub;
3166 	s32 val = av1_decode_signed_subexp_with_ref(bs, -mx, mx + 1, r);
3167 
3168 	if (val < 0) {
3169 		val = -val;
3170 		state->GmParams.coefs[ref][idx] = (-(val << precDiff) + round);
3171 	}
3172 	else {
3173 		state->GmParams.coefs[ref][idx] = (val << precDiff) + round;
3174 	}
3175 }
3176 
av1_get_relative_dist(s32 a,s32 b,AV1State * state)3177 static s32 av1_get_relative_dist(s32 a, s32 b, AV1State *state)
3178 {
3179 	if (!state->enable_order_hint)
3180 		return 0;
3181 	s32 diff = a - b;
3182 	s32 m = 1 << (state->OrderHintBits - 1);
3183 	diff = (diff & (m - 1)) - (diff & m);
3184 	return diff;
3185 }
3186 
av1_setup_past_independence(AV1State * state)3187 static void av1_setup_past_independence(AV1State *state)
3188 {
3189 	u32 ref, i;
3190 	for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ref++) {
3191 		for (i = 0; i <= 5; i++) {
3192 			state->PrevGmParams.coefs[ref][i] = ((i % 3 == 2) ? 1 << WARPEDMODEL_PREC_BITS : 0);
3193 		}
3194 	}
3195 }
3196 
av1_load_previous(AV1State * state,u8 primary_ref_frame,s8 * ref_frame_idx)3197 static void av1_load_previous(AV1State *state, u8 primary_ref_frame, s8 *ref_frame_idx)
3198 {
3199 	s8 prevFrame = ref_frame_idx[primary_ref_frame];
3200 	if (prevFrame < 0) {
3201 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AV1] load_previous: prevFrame reference index %d is invalid\n", prevFrame));
3202 	}
3203 	else {
3204 		state->PrevGmParams = state->SavedGmParams[prevFrame];
3205 		// load_loop_filter_params( prevFrame )
3206 		// load_segmentation_params( prevFrame )
3207 	}
3208 }
3209 
av1_decode_frame_wrapup(AV1State * state)3210 static void av1_decode_frame_wrapup(AV1State *state)
3211 {
3212 	u32 i;
3213 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3214 		if ((state->frame_state.refresh_frame_flags >> i) & 1) {
3215 			state->RefOrderHint[i] = state->frame_state.order_hint;
3216 			state->SavedGmParams[i] = state->GmParams;
3217 			state->RefFrameType[i] = state->frame_state.frame_type;
3218 		}
3219 	}
3220 	state->frame_state.seen_frame_header = GF_FALSE;
3221 	//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
3222 	if ((state->frame_state.show_existing_frame) && (state->frame_state.frame_type == AV1_KEY_FRAME)) {
3223 		state->frame_state.order_hint = state->RefOrderHint[state->frame_state.frame_to_show_map_idx];
3224 		//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.
3225 
3226 		//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.
3227 		state->GmParams = state->SavedGmParams[state->frame_state.frame_to_show_map_idx];
3228 
3229 	}
3230 }
3231 
find_latest_forward(u32 curFrameHint,u8 * shiftedOrderHints,u8 * usedFrame)3232 static s32 find_latest_forward(u32 curFrameHint, u8 *shiftedOrderHints, u8 *usedFrame)
3233 {
3234 	u32 i;
3235 	s32 ref = -1;
3236 	s32 latestOrderHint = 0;
3237 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3238 		s32 hint = shiftedOrderHints[i];
3239 		if (!usedFrame[i] && ((u32)hint < curFrameHint) && (ref < 0 || hint >= latestOrderHint)) {
3240 			ref = i;
3241 			latestOrderHint = hint;
3242 		}
3243 	}
3244 	return ref;
3245 }
3246 
3247 //see 7.8 of AV1 spec
av1_set_frame_refs(AV1State * state,u8 last_frame_idx,u8 gold_frame_idx,s8 * ref_frame_idx)3248 static void av1_set_frame_refs(AV1State *state, u8 last_frame_idx, u8 gold_frame_idx, s8 *ref_frame_idx)
3249 {
3250 	u32 i;
3251 	u8 usedFrame[AV1_NUM_REF_FRAMES];
3252 	u8 shiftedOrderHints[AV1_NUM_REF_FRAMES];
3253 
3254 	for (i = 0; i < AV1_REFS_PER_FRAME; i++)
3255 		ref_frame_idx[i] = -1;
3256 
3257 	ref_frame_idx[AV1_LAST_FRAME - AV1_LAST_FRAME] = last_frame_idx;
3258 	ref_frame_idx[AV1_GOLDEN_FRAME - AV1_LAST_FRAME] = gold_frame_idx;
3259 
3260 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3261 		usedFrame[i] = 0;
3262 	}
3263 
3264 	usedFrame[last_frame_idx] = 1;
3265 	usedFrame[gold_frame_idx] = 1;
3266 	u32 curFrameHint = 1 << (state->OrderHintBits - 1);
3267 
3268 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3269 		shiftedOrderHints[i] = curFrameHint + av1_get_relative_dist(state->RefOrderHint[i], state->frame_state.order_hint, state);
3270 	}
3271 
3272 	u8 lastOrderHint = shiftedOrderHints[last_frame_idx];
3273 	u8 goldOrderHint = shiftedOrderHints[gold_frame_idx];
3274 
3275 	//It is a requirement of bitstream conformance that lastOrderHint is strictly less than curFrameHint.
3276 	if (lastOrderHint >= curFrameHint) {
3277 		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));
3278 	}
3279 	//It is a requirement of bitstream conformance that goldOrderHint is strictly less than curFrameHint.
3280 	if (goldOrderHint >= curFrameHint) {
3281 		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));
3282 	}
3283 
3284 	//find_latest_backward() {
3285 	s32 ref = -1;
3286 	s32 latestOrderHint = 0;
3287 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3288 		s32 hint = shiftedOrderHints[i];
3289 		if (!usedFrame[i] && ((u32)hint >= curFrameHint) && (ref < 0 || hint >= latestOrderHint)) {
3290 			ref = i;
3291 			latestOrderHint = hint;
3292 		}
3293 	}
3294 	if (ref >= 0) {
3295 		ref_frame_idx[AV1_ALTREF_FRAME - AV1_LAST_FRAME] = ref;
3296 		usedFrame[ref] = 1;
3297 	}
3298 	//find_earliest_backward() for BWDREF_FRAME
3299 	ref = -1;
3300 	s32 earliestOrderHint = 0;
3301 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3302 		s32 hint = shiftedOrderHints[i];
3303 		if (!usedFrame[i] && ((u32)hint >= curFrameHint) && (ref < 0 || hint < earliestOrderHint)) {
3304 			ref = i;
3305 			earliestOrderHint = hint;
3306 		}
3307 	}
3308 	if (ref >= 0) {
3309 		ref_frame_idx[AV1_BWDREF_FRAME - AV1_LAST_FRAME] = ref;
3310 		usedFrame[ref] = 1;
3311 	}
3312 
3313 	//find_earliest_backward() for ALTREF2_FRAME
3314 	ref = -1;
3315 	earliestOrderHint = 0;
3316 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3317 		s32 hint = shiftedOrderHints[i];
3318 		if (!usedFrame[i] && ((u32)hint >= curFrameHint) && (ref < 0 || hint < earliestOrderHint)) {
3319 			ref = i;
3320 			earliestOrderHint = hint;
3321 		}
3322 	}
3323 	if (ref >= 0) {
3324 		ref_frame_idx[AV1_ALTREF2_FRAME - AV1_LAST_FRAME] = ref;
3325 		usedFrame[ref] = 1;
3326 	}
3327 
3328 	//The remaining references are set to be forward references in anti-chronological order as follows:
3329 
3330 	const u8 Ref_Frame_List[AV1_REFS_PER_FRAME - 2] = {
3331 		AV1_LAST2_FRAME, AV1_LAST3_FRAME, AV1_BWDREF_FRAME, AV1_ALTREF2_FRAME, AV1_ALTREF_FRAME
3332 	};
3333 
3334 	for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) {
3335 		u8 refFrame = Ref_Frame_List[i];
3336 		if (ref_frame_idx[refFrame - AV1_LAST_FRAME] < 0) {
3337 			s32 last_ref = find_latest_forward(curFrameHint, shiftedOrderHints, usedFrame);
3338 			if (last_ref >= 0) {
3339 				ref_frame_idx[refFrame - AV1_LAST_FRAME] = last_ref;
3340 				usedFrame[last_ref] = 1;
3341 			}
3342 		}
3343 	}
3344 	//Finally, any remaining references are set to the reference frame with smallest output order as follows:
3345 	ref = -1;
3346 	for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3347 		s32 hint = shiftedOrderHints[i];
3348 		if (ref < 0 || hint < earliestOrderHint) {
3349 			ref = i;
3350 			earliestOrderHint = hint;
3351 		}
3352 	}
3353 	for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
3354 		if (ref_frame_idx[i] < 0) {
3355 			ref_frame_idx[i] = ref;
3356 		}
3357 	}
3358 }
3359 
3360 
av1_parse_uncompressed_header(GF_BitStream * bs,AV1State * state)3361 static void av1_parse_uncompressed_header(GF_BitStream *bs, AV1State *state)
3362 {
3363 	Bool error_resilient_mode = GF_FALSE, allow_screen_content_tools = GF_FALSE, force_integer_mv = GF_FALSE;
3364 	Bool /*use_ref_frame_mvs = GF_FALSE,*/ FrameIsIntra = GF_FALSE, frame_size_override_flag = GF_FALSE;
3365 	Bool disable_cdf_update = GF_FALSE;
3366 	u8 showable_frame;
3367 	u8 primary_ref_frame;
3368 	u16 idLen = 0;
3369 	u32 idx;
3370 	s8 ref_frame_idx[AV1_REFS_PER_FRAME];
3371 	AV1StateFrame *frame_state = &state->frame_state;
3372 
3373 	if (state->frame_id_numbers_present_flag) {
3374 		idLen = (state->additional_frame_id_length_minus_1 + state->delta_frame_id_length_minus_2 + 3);
3375 	}
3376 	frame_state->refresh_frame_flags = 0;
3377 
3378 	showable_frame = 0;
3379 	if (state->reduced_still_picture_header) {
3380 		frame_state->key_frame = GF_TRUE;
3381 		FrameIsIntra = GF_TRUE;
3382 		frame_state->frame_type = AV1_KEY_FRAME;
3383 		frame_state->show_frame = GF_TRUE;
3384 		frame_state->show_existing_frame = 0;
3385 	}
3386 	else {
3387 		frame_state->show_existing_frame = gf_bs_read_int(bs, 1);
3388 		if (frame_state->show_existing_frame == GF_TRUE) {
3389 			frame_state->frame_to_show_map_idx = gf_bs_read_int(bs, 3);
3390 			frame_state->frame_type = state->RefFrameType[frame_state->frame_to_show_map_idx];
3391 
3392 			if (state->decoder_model_info_present_flag && !state->equal_picture_interval) {
3393 				/*frame_presentation_time = */gf_bs_read_int(bs, state->frame_presentation_time_length);
3394 			}
3395 
3396 			frame_state->refresh_frame_flags = 0;
3397 			if (state->frame_id_numbers_present_flag) {
3398 				/*display_frame_id = */gf_bs_read_int(bs, idLen);
3399 			}
3400 			if (frame_state->frame_type == AV1_KEY_FRAME) {
3401 				frame_state->refresh_frame_flags = AV1_ALL_FRAMES;
3402 			}
3403 			/*
3404 			if (film_grain_params_present) {
3405 				load_grain_params(frame_to_show_map_idx)
3406 			}*/
3407 			return;
3408 		}
3409 		frame_state->frame_type = gf_bs_read_int(bs, 2);
3410 		FrameIsIntra = (frame_state->frame_type == AV1_INTRA_ONLY_FRAME || frame_state->frame_type == AV1_KEY_FRAME);
3411 		frame_state->show_frame = gf_bs_read_int(bs, 1);
3412 		if (frame_state->is_first_frame) {
3413 			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;
3414 		}
3415 		if (frame_state->show_frame && state->decoder_model_info_present_flag && !state->equal_picture_interval) {
3416 			/*frame_presentation_time = */gf_bs_read_int(bs, state->frame_presentation_time_length);
3417 		}
3418 		if (frame_state->show_frame) {
3419 			showable_frame = frame_state->frame_type != AV1_KEY_FRAME;
3420 
3421 		}
3422 		else {
3423 			showable_frame = gf_bs_read_int(bs, 1);
3424 		}
3425 		if (frame_state->frame_type == AV1_SWITCH_FRAME || (frame_state->frame_type == AV1_KEY_FRAME && frame_state->show_frame))
3426 			error_resilient_mode = GF_TRUE;
3427 		else
3428 			error_resilient_mode = gf_bs_read_int(bs, 1);
3429 	}
3430 
3431 	if ((frame_state->frame_type == AV1_KEY_FRAME) && frame_state->show_frame) {
3432 		u32 i;
3433 		for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3434 			state->RefValid[i] = 0;
3435 			state->RefOrderHint[i] = 0;
3436 		}
3437 		for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
3438 			state->OrderHints[AV1_LAST_FRAME + i] = 0;
3439 		}
3440 	}
3441 
3442 	disable_cdf_update = gf_bs_read_int(bs, 1);
3443 	if (state->seq_force_screen_content_tools == 2/*SELECT_SCREEN_CONTENT_TOOLS*/) {
3444 		allow_screen_content_tools = gf_bs_read_int(bs, 1);
3445 	}
3446 	else {
3447 		allow_screen_content_tools = state->seq_force_screen_content_tools;
3448 	}
3449 	if (allow_screen_content_tools) {
3450 		if (state->seq_force_integer_mv == 2/*SELECT_INTEGER_MV*/) {
3451 			force_integer_mv = gf_bs_read_int(bs, 1);
3452 		}
3453 		else {
3454 			force_integer_mv = state->seq_force_integer_mv;
3455 		}
3456 	}
3457 	else {
3458 		force_integer_mv = 0;
3459 	}
3460 	if (FrameIsIntra) {
3461 		force_integer_mv = 1;
3462 	}
3463 	if (state->frame_id_numbers_present_flag) {
3464 		/*current_frame_id = */gf_bs_read_int(bs, idLen);
3465 	}
3466 	if (frame_state->frame_type == AV1_SWITCH_FRAME)
3467 		frame_size_override_flag = GF_TRUE;
3468 	else if (state->reduced_still_picture_header)
3469 		frame_size_override_flag = GF_FALSE;
3470 	else
3471 		frame_size_override_flag = gf_bs_read_int(bs, 1);
3472 
3473 	frame_state->order_hint = gf_bs_read_int(bs, state->OrderHintBits);
3474 	if (FrameIsIntra || error_resilient_mode) {
3475 		primary_ref_frame = AV1_PRIMARY_REF_NONE;
3476 	}
3477 	else {
3478 		primary_ref_frame = gf_bs_read_int(bs, 3);
3479 	}
3480 
3481 	if (state->decoder_model_info_present_flag) {
3482 		u8 buffer_removal_time_present_flag = gf_bs_read_int(bs, 1);
3483 		if (buffer_removal_time_present_flag) {
3484 			u32 opNum;
3485 			for (opNum = 0; opNum < state->operating_points_count; opNum++) {
3486 				if (state->decoder_model_present_for_this_op[opNum]) {
3487 					u8 opPtIdc = state->operating_point_idc[opNum];
3488 					u8 inTemporalLayer = (opPtIdc >> state->temporal_id) & 1;
3489 					u8 inSpatialLayer = (opPtIdc >> (state->spatial_id + 8)) & 1;
3490 					if (opPtIdc == 0 || (inTemporalLayer && inSpatialLayer)) {
3491 						/*buffer_removal_time[opNum] = */gf_bs_read_int(bs, state->buffer_removal_time_length);
3492 					}
3493 				}
3494 			}
3495 		}
3496 	}
3497 
3498 	if (frame_state->frame_type == AV1_SWITCH_FRAME || (frame_state->frame_type == AV1_KEY_FRAME && frame_state->show_frame)) {
3499 		frame_state->refresh_frame_flags = AV1_ALL_FRAMES;
3500 	}
3501 	else {
3502 		frame_state->refresh_frame_flags = gf_bs_read_int(bs, 8);
3503 	}
3504 	if (!FrameIsIntra || frame_state->refresh_frame_flags != AV1_ALL_FRAMES) {
3505 		if (error_resilient_mode && state->enable_order_hint) {
3506 			u32 i = 0;
3507 			for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
3508 				u8 ref_order_hint = gf_bs_read_int(bs, state->OrderHintBits);
3509 				if (ref_order_hint != state->RefOrderHint[i]) {
3510 					state->RefValid[i] = 0;
3511 				}
3512 				state->RefOrderHint[i] = ref_order_hint;
3513 			}
3514 		}
3515 	}
3516 
3517 	u8 allow_intrabc = 0;
3518 	if (frame_state->frame_type == AV1_KEY_FRAME) {
3519 		av1_frame_size(bs, state, frame_size_override_flag);
3520 		av1_render_size(bs);
3521 		if (allow_screen_content_tools && state->UpscaledWidth == state->width) {
3522 			allow_intrabc = gf_bs_read_int(bs, 1);
3523 		}
3524 	}
3525 	else {
3526 		if (frame_state->frame_type == AV1_INTRA_ONLY_FRAME) {
3527 			av1_frame_size(bs, state, frame_size_override_flag);
3528 			av1_render_size(bs);
3529 			if (allow_screen_content_tools && state->UpscaledWidth == state->width) {
3530 				allow_intrabc = gf_bs_read_int(bs, 1);
3531 			}
3532 		}
3533 		else {
3534 			u32 i = 0;
3535 			Bool frame_refs_short_signaling = GF_FALSE;
3536 			if (state->enable_order_hint) {
3537 				frame_refs_short_signaling = gf_bs_read_int(bs, 1);
3538 				if (frame_refs_short_signaling) {
3539 					u8 last_frame_idx = gf_bs_read_int(bs, 3);
3540 					u8 gold_frame_idx = gf_bs_read_int(bs, 3);
3541 					av1_set_frame_refs(state, last_frame_idx, gold_frame_idx, ref_frame_idx);
3542 				}
3543 			}
3544 			for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
3545 				if (!frame_refs_short_signaling)
3546 					ref_frame_idx[i] = gf_bs_read_int(bs, 3);
3547 
3548 				if (state->frame_id_numbers_present_flag) {
3549 					u32 n = state->delta_frame_id_length_minus_2 + 2;
3550 					/*delta_frame_id_minus_1 =*/ gf_bs_read_int(bs, n);
3551 					//DeltaFrameId = delta_frame_id_minus_1 + 1;
3552 					//expectedFrameId[i] = ((current_frame_id + (1 << idLen) - DeltaFrameId) % (1 << idLen));
3553 				}
3554 			}
3555 			if (frame_size_override_flag && !error_resilient_mode) {
3556 				frame_size_with_refs(bs, state, frame_size_override_flag);
3557 			}
3558 			else {
3559 				av1_frame_size(bs, state, frame_size_override_flag);
3560 				av1_render_size(bs);
3561 			}
3562 			frame_state->allow_high_precision_mv = 0;
3563 			if (!force_integer_mv) {
3564 				frame_state->allow_high_precision_mv = gf_bs_read_int(bs, 1);
3565 			}
3566 
3567 			read_interpolation_filter(bs);
3568 
3569 			/*is_motion_mode_switchable =*/ gf_bs_read_int(bs, 1);
3570 			if (!(error_resilient_mode || !state->enable_ref_frame_mvs)) {
3571 				/*use_ref_frame_mvs = */gf_bs_read_int(bs, 1);
3572 			}
3573 		}
3574 	}
3575 
3576 	if (!FrameIsIntra) {
3577 		u32 i;
3578 		for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
3579 			u8 refFrame = AV1_LAST_FRAME + i;
3580 			u8 ridx = ref_frame_idx[i];
3581 			if (ridx >= 0) {
3582 				u8 hint = state->RefOrderHint[ridx];
3583 				state->OrderHints[refFrame] = hint;
3584 				/*			if ( !enable_order_hint ) {
3585 								RefFrameSignBias[ refFrame ] = 0;
3586 							} else {
3587 								RefFrameSignBias[ refFrame ] = get_relative_dist( hint, OrderHint) > 0;
3588 							}
3589 				*/
3590 			}
3591 
3592 		}
3593 	}
3594 
3595 	if (!(state->reduced_still_picture_header || disable_cdf_update))
3596 		/*disable_frame_end_update_cdf = */gf_bs_read_int(bs, 1);
3597 
3598 	if (primary_ref_frame == AV1_PRIMARY_REF_NONE) {
3599 		//init_non_coeff_cdfs();
3600 		av1_setup_past_independence(state);
3601 	}
3602 	else {
3603 		//load_cdfs(ref_frame_idx[primary_ref_frame]);
3604 		av1_load_previous(state, primary_ref_frame, ref_frame_idx);
3605 	}
3606 
3607 	av1_parse_tile_info(bs, state);
3608 	//quantization_params( ):
3609 	u8 base_q_idx = gf_bs_read_int(bs, 8);
3610 	s32 DeltaQUDc = 0;
3611 	s32 DeltaQUAc = 0;
3612 	s32 DeltaQVDc = 0;
3613 	s32 DeltaQVAc = 0;
3614 	s32 DeltaQYDc = av1_delta_q(bs);
3615 	if (!state->config->monochrome) {
3616 		u8 diff_uv_delta = 0;
3617 		if (state->separate_uv_delta_q)
3618 			diff_uv_delta = gf_bs_read_int(bs, 1);
3619 
3620 		DeltaQUDc = av1_delta_q(bs);
3621 		DeltaQUAc = av1_delta_q(bs);
3622 		if (diff_uv_delta) {
3623 			DeltaQVDc = av1_delta_q(bs);
3624 			DeltaQVAc = av1_delta_q(bs);
3625 		}
3626 	}
3627 	if (/*using_qmatrix*/gf_bs_read_int(bs, 1)) {
3628 		/*qm_y = */gf_bs_read_int(bs, 4);
3629 		/*qm_y = */gf_bs_read_int(bs, 4);
3630 		if (!state->separate_uv_delta_q) {
3631 			/*qm_v = */gf_bs_read_int(bs, 4);
3632 		}
3633 	}
3634 
3635 	u8 seg_features_SEG_LVL_ALT_Q_enabled[8] = { 0,0,0,0,0,0,0,0 };
3636 	s32 seg_features_SEG_LVL_ALT_Q[8] = { 0,0,0,0,0,0,0,0 };
3637 
3638 	//segmentation_params( ):
3639 	u8 segmentation_enabled = gf_bs_read_int(bs, 1);
3640 	if (segmentation_enabled) {
3641 		/*u8 segmentation_temporal_update = 0;*/
3642 		u8 segmentation_update_data = 1;
3643 		if (primary_ref_frame != AV1_PRIMARY_REF_NONE) {
3644 			u8 segmentation_update_map = gf_bs_read_int(bs, 1);
3645 			if (segmentation_update_map == 1)
3646 				/*segmentation_temporal_update = */gf_bs_read_int(bs, 1);
3647 			segmentation_update_data = gf_bs_read_int(bs, 1);
3648 		}
3649 		if (segmentation_update_data == 1) {
3650 			u32 i, j;
3651 			for (i = 0; i < 8/*=MAX_SEGMENTS*/; i++) {
3652 				for (j = 0; j < 8 /*=SEG_LVL_MAX*/; j++) {
3653 					if (/*feature_enabled = */gf_bs_read_int(bs, 1) == 1) {
3654 						s32 val;
3655 						u32 bitsToRead = Segmentation_Feature_Bits[j];
3656 						//this is SEG_LVL_ALT_Q
3657 						if (!j) seg_features_SEG_LVL_ALT_Q_enabled[i] = 1;
3658 
3659 						if (Segmentation_Feature_Signed[j] == 1) {
3660 							val = gf_bs_read_int(bs, 1 + bitsToRead);
3661 						}
3662 						else {
3663 							val = gf_bs_read_int(bs, bitsToRead);
3664 						}
3665 						if (!j) seg_features_SEG_LVL_ALT_Q[i] = val;
3666 					}
3667 				}
3668 			}
3669 			//ignore all init steps
3670 		}
3671 
3672 	}
3673 
3674 	//delta_q_params():
3675 	/*u8 delta_q_res = 0;*/
3676 	u8 delta_q_present = 0;
3677 	if (base_q_idx > 0) {
3678 		delta_q_present = gf_bs_read_int(bs, 1);
3679 	}
3680 	if (delta_q_present) {
3681 		/*delta_q_res = */gf_bs_read_int(bs, 2);
3682 	}
3683 
3684 	//delta_lf_params():
3685 	u8 delta_lf_present = 0;
3686 	/*u8 delta_lf_res = 0;
3687 	u8 delta_lf_multi = 0;*/
3688 	if (delta_q_present) {
3689 		if (!allow_intrabc) {
3690 			delta_lf_present = gf_bs_read_int(bs, 1);
3691 		}
3692 		if (delta_lf_present) {
3693 			/*delta_lf_res = */gf_bs_read_int(bs, 2);
3694 			/*delta_lf_multi = */gf_bs_read_int(bs, 1);
3695 		}
3696 	}
3697 
3698 	//init lossless stuff!
3699 	u8 CodedLossless = 1;
3700 	for (idx = 0; idx < 8; idx++) {
3701 		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);
3702 		Bool LosslessArray = (qindex == 0) && (DeltaQYDc == 0) && (DeltaQUAc == 0) && (DeltaQUDc == 0) && (DeltaQVAc == 0) && (DeltaQVDc == 0);
3703 		if (!LosslessArray)
3704 			CodedLossless = 0;
3705 	}
3706 	Bool AllLossless = CodedLossless && (state->width == state->UpscaledWidth);
3707 
3708 	//loop_filter_params():
3709 	if (!CodedLossless && !allow_intrabc) {
3710 		u8 loop_filter_level_0 = gf_bs_read_int(bs, 6);
3711 		u8 loop_filter_level_1 = gf_bs_read_int(bs, 6);
3712 		if (!state->config->monochrome) {
3713 			if (loop_filter_level_0 || loop_filter_level_1) {
3714 				/*u8 loop_filter_level_2 = */gf_bs_read_int(bs, 6);
3715 				/*u8 loop_filter_level_3 = */gf_bs_read_int(bs, 6);
3716 			}
3717 		}
3718 		/*u8 loop_filter_sharpness = */gf_bs_read_int(bs, 3);
3719 		u8 loop_filter_delta_enabled = gf_bs_read_int(bs, 1);
3720 		if (loop_filter_delta_enabled == 1) {
3721 			u8 loop_filter_delta_update = gf_bs_read_int(bs, 1);
3722 			if (loop_filter_delta_update) {
3723 				u32 i;
3724 				for (i = 0; i < 8/*TOTAL_REFS_PER_FRAME*/; i++) {
3725 					u8 update_ref_delta = gf_bs_read_int(bs, 1);
3726 					if (update_ref_delta == 1) {
3727 						/*u8 loop_filter_ref_deltas_i = */gf_bs_read_int(bs, 1 + 6);
3728 					}
3729 				}
3730 				for (i = 0; i < 2; i++) {
3731 					u8 update_mode_delta = gf_bs_read_int(bs, 1);
3732 					if (update_mode_delta) {
3733 						/*u8 loop_filter_mode_deltas_i = */gf_bs_read_int(bs, 1 + 6);
3734 					}
3735 				}
3736 			}
3737 		}
3738 	}
3739 	//cdef_params( ):
3740 	if (!CodedLossless && !allow_intrabc && state->enable_cdef) {
3741 		/*u8 cdef_damping_minus_3 = */gf_bs_read_int(bs, 2);
3742 		u8 cdef_bits = gf_bs_read_int(bs, 2);
3743 		u32 i, num_cd = 1 << cdef_bits;
3744 		for (i = 0; i < num_cd; i++) {
3745 			/*u8 cdef_y_pri_strength_i = */gf_bs_read_int(bs, 4);
3746 			/*u8 cdef_y_sec_strength_i = */gf_bs_read_int(bs, 2);
3747 			if (!state->config->monochrome) {
3748 				/*u8 cdef_uv_pri_strength_i = */gf_bs_read_int(bs, 4);
3749 				/*u8 cdef_uv_sec_strength_i = */gf_bs_read_int(bs, 2);
3750 			}
3751 		}
3752 	}
3753 
3754 	//lr_params( ) :
3755 	if (!AllLossless && !allow_intrabc && state->enable_restoration) {
3756 		u32 i, nb_planes = state->config->monochrome ? 1 : 3;
3757 		u8 UsesLr = 0;
3758 		u8 usesChromaLr = 0;
3759 		for (i = 0; i < nb_planes; i++) {
3760 			u8 lr_type = gf_bs_read_int(bs, 2);
3761 			//FrameRestorationType[i] = Remap_Lr_Type[lr_type]
3762 			if (lr_type != AV1_RESTORE_NONE) {
3763 				UsesLr = 1;
3764 				if (i > 0) {
3765 					usesChromaLr = 1;
3766 				}
3767 			}
3768 		}
3769 		if (UsesLr) {
3770 			if (state->use_128x128_superblock) {
3771 				/*u8 lr_unit_shift_minus_1 = */gf_bs_read_int(bs, 1);
3772 			}
3773 			else {
3774 				u8 lr_unit_shift = gf_bs_read_int(bs, 1);
3775 				if (lr_unit_shift) {
3776 					/*u8 lr_unit_extra_shift = */gf_bs_read_int(bs, 1);
3777 					//lr_unit_shift += lr_unit_extra_shift;
3778 				}
3779 			}
3780 			if (state->config->chroma_subsampling_x && state->config->chroma_subsampling_y && usesChromaLr) {
3781 				/*u8 lr_uv_shift = */gf_bs_read_int(bs, 1);
3782 			}
3783 		}
3784 	}
3785 	//read_tx_mode():
3786 	if (CodedLossless == 1) {
3787 	}
3788 	else {
3789 		/*tx_mode_select = */gf_bs_read_int(bs, 1);
3790 	}
3791 
3792 	//frame_reference_mode( ):
3793 	u8 reference_select = 0;
3794 	if (FrameIsIntra) {
3795 	}
3796 	else {
3797 		reference_select = gf_bs_read_int(bs, 1);
3798 	}
3799 
3800 	//skip_mode_params( ):
3801 	u8 skipModeAllowed = 0;
3802 	if (FrameIsIntra || !reference_select || !state->enable_order_hint) {
3803 	}
3804 	else {
3805 		u32 i;
3806 		s32 forwardIdx = -1;
3807 		s32 backwardIdx = -1;
3808 		s32 forwardHint = 0;
3809 		s32 backwardHint = 0;
3810 		for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
3811 			u8 refHint = state->RefOrderHint[ref_frame_idx[i]];
3812 			if (av1_get_relative_dist(refHint, frame_state->order_hint, state) < 0) {
3813 				if (forwardIdx < 0 || av1_get_relative_dist(refHint, forwardHint, state) > 0) {
3814 					forwardIdx = i;
3815 					forwardHint = refHint;
3816 				}
3817 			}
3818 			else if (av1_get_relative_dist(refHint, frame_state->order_hint, state) > 0) {
3819 				if (backwardIdx < 0 || av1_get_relative_dist(refHint, backwardHint, state) < 0) {
3820 					backwardIdx = i;
3821 					backwardHint = refHint;
3822 				}
3823 			}
3824 		}
3825 		if (forwardIdx < 0) {
3826 			skipModeAllowed = 0;
3827 		}
3828 		else if (backwardIdx >= 0) {
3829 			skipModeAllowed = 1;
3830 			//SkipModeFrame[0] = AV1_LAST_FRAME + MIN(forwardIdx, backwardIdx);
3831 			//SkipModeFrame[1] = AV1_LAST_FRAME + MAX(forwardIdx, backwardIdx);
3832 		}
3833 		else {
3834 			s32 secondForwardIdx = -1;
3835 			s32 secondForwardHint = 0;
3836 			for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
3837 				u8 refHint = state->RefOrderHint[ref_frame_idx[i]];
3838 				if (av1_get_relative_dist(refHint, forwardHint, state) < 0) {
3839 					if (secondForwardIdx < 0 || av1_get_relative_dist(refHint, secondForwardHint, state) > 0) {
3840 						secondForwardIdx = i;
3841 						secondForwardHint = refHint;
3842 					}
3843 				}
3844 			}
3845 			if (secondForwardIdx < 0) {
3846 				skipModeAllowed = 0;
3847 			}
3848 			else {
3849 				skipModeAllowed = 1;
3850 				//SkipModeFrame[ 0 ] = LAST_FRAME + Min(forwardIdx, secondForwardIdx)
3851 				//SkipModeFrame[ 1 ] = LAST_FRAME + Max(forwardIdx, secondForwardIdx)
3852 			}
3853 		}
3854 	}
3855 	if (skipModeAllowed) {
3856 		/*skip_mode_present = */gf_bs_read_int(bs, 1);
3857 	}
3858 
3859 
3860 	if (FrameIsIntra || error_resilient_mode || !state->enable_warped_motion) {
3861 
3862 	}
3863 	else {
3864 		/*allow_warped_motion = */gf_bs_read_int(bs, 1);
3865 	}
3866 
3867 	/*reduced_tx = */gf_bs_read_int(bs, 1);
3868 
3869 	//global_motion_params( )
3870 	u32 ref;
3871 	for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ref++) {
3872 		u32 i;
3873 		for (i = 0; i < 6; i++) {
3874 			state->GmParams.coefs[ref][i] = ((i % 3 == 2) ? 1 << WARPEDMODEL_PREC_BITS : 0);
3875 		}
3876 	}
3877 	if (!FrameIsIntra) {
3878 		u32 refs;
3879 		for (refs = AV1_LAST_FRAME; refs <= AV1_ALTREF_FRAME; refs++) {
3880 			u8 type = AV1_GMC_IDENTITY;
3881 			Bool is_global = gf_bs_read_int(bs, 1);
3882 			if (is_global) {
3883 				Bool is_rot_zoom = gf_bs_read_int(bs, 1);
3884 				if (is_rot_zoom) {
3885 					type = AV1_GMC_ROTZOOM;
3886 				}
3887 				else {
3888 					Bool is_trans = gf_bs_read_int(bs, 1);
3889 					type = is_trans ? AV1_GMC_TRANSLATION : AV1_GMC_AFFINE;
3890 
3891 				}
3892 			}
3893 
3894 			if (type >= AV1_GMC_ROTZOOM) {
3895 				av1_read_global_param(state, bs, type, refs, 2);
3896 				av1_read_global_param(state, bs, type, refs, 3);
3897 				if (type == AV1_GMC_AFFINE) {
3898 					av1_read_global_param(state, bs, type, refs, 4);
3899 					av1_read_global_param(state, bs, type, refs, 5);
3900 				}
3901 				else {
3902 					state->GmParams.coefs[refs][4] = -state->GmParams.coefs[refs][3];
3903 					state->GmParams.coefs[refs][5] = state->GmParams.coefs[refs][2];
3904 
3905 				}
3906 			}
3907 			if (type >= AV1_GMC_TRANSLATION) {
3908 				av1_read_global_param(state, bs, type, refs, 0);
3909 				av1_read_global_param(state, bs, type, refs, 1);
3910 			}
3911 		}
3912 	}
3913 
3914 	//film_grain_params()
3915 	if (!state->film_grain_params_present || (!state->frame_state.show_frame && !showable_frame)) {
3916 	}
3917 	else {
3918 		u8 apply_grain = gf_bs_read_int(bs, 1);
3919 		if (apply_grain) {
3920 			/*u8 grain_seed = */gf_bs_read_int(bs, 16);
3921 			u8 update_grain = 1;
3922 			if (state->frame_state.frame_type == AV1_INTER_FRAME) {
3923 				update_grain = gf_bs_read_int(bs, 1);
3924 			}
3925 			if (!update_grain) {
3926 				/*u8 film_grain_params_ref_idx = */gf_bs_read_int(bs, 3);
3927 			}
3928 			else {
3929 				u32 i, num_y_points = gf_bs_read_int(bs, 4);
3930 				for (i = 0; i < num_y_points; i++) {
3931 					/*u8 point_y_value = */gf_bs_read_int(bs, 8);
3932 					/*u8 point_y_scaling = */gf_bs_read_int(bs, 8);
3933 				}
3934 				u8 chroma_scaling_from_luma = 0;
3935 				if (!state->config->monochrome) chroma_scaling_from_luma = gf_bs_read_int(bs, 1);
3936 
3937 				u8 num_cb_points = 0;
3938 				u8 num_cr_points = 0;
3939 				if (state->config->monochrome || chroma_scaling_from_luma ||
3940 					((state->config->chroma_subsampling_x == 1) && (state->config->chroma_subsampling_y == 1) && (num_y_points == 0))
3941 					) {
3942 				}
3943 				else {
3944 					num_cb_points = gf_bs_read_int(bs, 4);
3945 					for (i = 0; i < num_cb_points; i++) {
3946 						/*point_cb_value[ i ] = */gf_bs_read_int(bs, 8);
3947 						/*point_cb_scaling[ i ] = */gf_bs_read_int(bs, 8);
3948 					}
3949 					num_cr_points = gf_bs_read_int(bs, 4);
3950 					for (i = 0; i < num_cr_points; i++) {
3951 						/*point_cb_value[ i ] = */gf_bs_read_int(bs, 8);
3952 						/*point_cb_scaling[ i ] = */gf_bs_read_int(bs, 8);
3953 					}
3954 				}
3955 				/*u8 grain_scaling_minus_8 = */gf_bs_read_int(bs, 2);
3956 				u8 ar_coeff_lag = gf_bs_read_int(bs, 2);
3957 				u16 numPosLuma = 2 * ar_coeff_lag * (ar_coeff_lag + 1);
3958 				u16 numPosChroma = numPosLuma;
3959 				if (num_y_points) {
3960 					numPosChroma = numPosLuma + 1;
3961 					for (i = 0; i < numPosLuma; i++) {
3962 						/*ar_coeffs_y_plus_128[ i ] = */gf_bs_read_int(bs, 8);
3963 					}
3964 				}
3965 				if (chroma_scaling_from_luma || num_cb_points) {
3966 					for (i = 0; i < numPosChroma; i++) {
3967 						/*ar_coeffs_cb_plus_128[ i ] =*/gf_bs_read_int(bs, 8);
3968 					}
3969 				}
3970 				if (chroma_scaling_from_luma || num_cr_points) {
3971 					for (i = 0; i < numPosChroma; i++) {
3972 						/*ar_coeffs_cr_plus_128[ i ] =*/gf_bs_read_int(bs, 8);
3973 					}
3974 				}
3975 				/*u8 ar_coeff_shift_minus_6 = */gf_bs_read_int(bs, 2);
3976 				/*u8 grain_scale_shift = */gf_bs_read_int(bs, 2);
3977 				if (num_cb_points) {
3978 					/*u8 cb_mult = */gf_bs_read_int(bs, 8);
3979 					/*u8 cb_luma_mult = */gf_bs_read_int(bs, 8);
3980 					/*u8 cb_offset = */gf_bs_read_int(bs, 9);
3981 				}
3982 				if (num_cr_points) {
3983 					/*u8 cr_mult = */gf_bs_read_int(bs, 8);
3984 					/*u8 cr_luma_mult = */gf_bs_read_int(bs, 8);
3985 					/*u8 cr_offset = */gf_bs_read_int(bs, 9);
3986 				}
3987 				/*u8 overlap_flag = */gf_bs_read_int(bs, 1);
3988 				/*u8 clip_to_restricted_range = */gf_bs_read_int(bs, 1);
3989 			}
3990 		}
3991 	}
3992 
3993 	//end of uncompressed header !!
3994 }
3995 
3996 GF_EXPORT
gf_av1_init_state(AV1State * state)3997 void gf_av1_init_state(AV1State *state)
3998 {
3999 	if (!state) return;
4000 	memset(state, 0, sizeof(AV1State));
4001 	state->color_primaries = 2;
4002 	state->transfer_characteristics = 2;
4003 	state->matrix_coefficients = 2;
4004 }
4005 
4006 GF_EXPORT
gf_av1_reset_state(AV1State * state,Bool is_destroy)4007 void gf_av1_reset_state(AV1State *state, Bool is_destroy)
4008 {
4009 	GF_List *l1, *l2;
4010 
4011 	if (state->frame_state.header_obus) {
4012 		while (gf_list_count(state->frame_state.header_obus)) {
4013 			GF_AV1_OBUArrayEntry *a = (GF_AV1_OBUArrayEntry*)gf_list_pop_back(state->frame_state.header_obus);
4014 			if (a->obu) gf_free(a->obu);
4015 			gf_free(a);
4016 		}
4017 	}
4018 
4019 	if (state->frame_state.frame_obus) {
4020 		while (gf_list_count(state->frame_state.frame_obus)) {
4021 			GF_AV1_OBUArrayEntry *a = (GF_AV1_OBUArrayEntry*)gf_list_pop_back(state->frame_state.frame_obus);
4022 			if (a->obu) gf_free(a->obu);
4023 			gf_free(a);
4024 		}
4025 	}
4026 	l1 = state->frame_state.frame_obus;
4027 	l2 = state->frame_state.header_obus;
4028 	memset(&state->frame_state, 0, sizeof(AV1StateFrame));
4029 	state->frame_state.is_first_frame = GF_TRUE;
4030 
4031 	if (is_destroy) {
4032 		gf_list_del(l1);
4033 		gf_list_del(l2);
4034 		if (state->bs) {
4035 			if (gf_bs_get_position(state->bs)) {
4036 				u32 size;
4037 				gf_bs_get_content_no_truncate(state->bs, &state->frame_obus, &size, &state->frame_obus_alloc);
4038 			}
4039 			gf_bs_del(state->bs);
4040 		}
4041 		state->bs = NULL;
4042 	}
4043 	else {
4044 		state->frame_state.frame_obus = l1;
4045 		state->frame_state.header_obus = l2;
4046 		if (state->bs)
4047 			gf_bs_seek(state->bs, 0);
4048 	}
4049 }
4050 
av1_parse_tile_group(GF_BitStream * bs,AV1State * state,u64 obu_start,u64 obu_size)4051 static GF_Err av1_parse_tile_group(GF_BitStream *bs, AV1State *state, u64 obu_start, u64 obu_size)
4052 {
4053 	u32 TileNum, tg_start = 0, tg_end = 0;
4054 	Bool numTiles = state->tileCols * state->tileRows;
4055 	Bool tile_start_and_end_present_flag = GF_FALSE;
4056 	GF_Err e = GF_OK;
4057 	if (numTiles > 1)
4058 		tile_start_and_end_present_flag = gf_bs_read_int(bs, 1);
4059 
4060 	if (numTiles == 1 || !tile_start_and_end_present_flag) {
4061 		tg_start = 0;
4062 		tg_end = numTiles - 1;
4063 		/*state->frame_state.tg[0].start_idx = 0;
4064 		state->frame_state.tg[0].end_idx = numTiles - 1;*/
4065 	}
4066 	else {
4067 		u32 tileBits = state->tileColsLog2 + state->tileRowsLog2;
4068 		/*state->frame_state.tg[state->frame_state.tg_idx].start_idx*/ tg_start = gf_bs_read_int(bs, tileBits);
4069 		/*state->frame_state.tg[state->frame_state.tg_idx].end_idx*/ tg_end = gf_bs_read_int(bs, tileBits);
4070 	}
4071 	/*state->frame_state.tg_idx++;*/
4072 
4073 	gf_bs_align(bs);
4074 
4075 	if (tg_end >= GF_ARRAY_LENGTH(state->frame_state.tiles))
4076 		return GF_NON_COMPLIANT_BITSTREAM;
4077 
4078 	state->frame_state.nb_tiles_in_obu = 0;
4079 	for (TileNum = tg_start; TileNum <= tg_end; TileNum++) {
4080 		u32 tile_start_offset, tile_size;
4081 		/*u32 tileRow = TileNum / state->tileCols;
4082 		u32 tileCol = TileNum % state->tileCols;*/
4083 		Bool lastTile = TileNum == tg_end;
4084 		u64 pos = gf_bs_get_position(bs);
4085 		if (lastTile) {
4086 			tile_start_offset = (u32)(pos - obu_start);
4087 			tile_size = (u32)(obu_size - (pos - obu_start));
4088 		}
4089 		else {
4090 			u64 tile_size_minus_1 = aom_av1_le(bs, state->tile_size_bytes);
4091 			pos = gf_bs_get_position(bs);
4092 			tile_start_offset = (u32)(pos - obu_start);
4093 			tile_size = (u32)(tile_size_minus_1 + 1/* + state->tile_size_bytes*/);
4094 		}
4095 
4096 
4097 		if (tile_start_offset + tile_size > obu_size) {
4098 			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));
4099 			e = GF_NON_COMPLIANT_BITSTREAM;
4100 			break;
4101 		}
4102 
4103 		state->frame_state.tiles[state->frame_state.nb_tiles_in_obu].obu_start_offset = tile_start_offset;
4104 		state->frame_state.tiles[state->frame_state.nb_tiles_in_obu].size = tile_size;
4105 		gf_bs_skip_bytes(bs, tile_size);
4106 		state->frame_state.nb_tiles_in_obu++;
4107 	}
4108 	if (tg_end == numTiles - 1) {
4109 		av1_decode_frame_wrapup(state);
4110 	}
4111 	return e;
4112 }
4113 
av1_parse_frame_header(GF_BitStream * bs,AV1State * state)4114 static void av1_parse_frame_header(GF_BitStream *bs, AV1State *state)
4115 {
4116 	AV1StateFrame *frame_state = &state->frame_state;
4117 	if (frame_state->seen_frame_header == GF_FALSE) {
4118 		u64 pos = gf_bs_get_position(bs);
4119 		state->frame_state.show_existing_frame = GF_FALSE;
4120 		frame_state->seen_frame_header = GF_TRUE;
4121 		av1_parse_uncompressed_header(bs, state);
4122 		state->frame_state.is_first_frame = GF_FALSE;
4123 		state->frame_state.uncompressed_header_bytes = (u32) (gf_bs_get_position(bs) - pos);
4124 
4125 		if (state->frame_state.show_existing_frame) {
4126 			av1_decode_frame_wrapup(state);
4127 			frame_state->seen_frame_header = GF_FALSE;
4128 		}
4129 		else {
4130 			//TileNum = 0;
4131 			frame_state->seen_frame_header = GF_TRUE;
4132 		}
4133 	}
4134 }
4135 
av1_parse_frame(GF_BitStream * bs,AV1State * state,u64 obu_start,u64 obu_size)4136 static GF_Err av1_parse_frame(GF_BitStream *bs, AV1State *state, u64 obu_start, u64 obu_size)
4137 {
4138 	av1_parse_frame_header(bs, state);
4139 	//byte alignment
4140 	gf_bs_align(bs);
4141 	return av1_parse_tile_group(bs, state, obu_start, obu_size);
4142 }
4143 
on_aom_av1_eos(void * _state)4144 static void on_aom_av1_eos(void *_state)
4145 {
4146 	AV1State *state = (AV1State *)_state;
4147 	state->bs_overread = GF_TRUE;
4148 }
4149 
4150 GF_EXPORT
gf_media_aom_av1_parse_obu(GF_BitStream * bs,ObuType * obu_type,u64 * obu_size,u32 * obu_hdr_size,AV1State * state)4151 GF_Err gf_media_aom_av1_parse_obu(GF_BitStream *bs, ObuType *obu_type, u64 *obu_size, u32 *obu_hdr_size, AV1State *state)
4152 {
4153 	GF_Err e = GF_OK;
4154 	u32 hdr_size;
4155 	u64 pos = gf_bs_get_position(bs);
4156 
4157 	if (!bs || !obu_type || !state)
4158 		return GF_BAD_PARAM;
4159 
4160 	state->bs_overread = GF_FALSE;
4161 	gf_bs_set_eos_callback(bs, on_aom_av1_eos, state);
4162 
4163 	state->obu_extension_flag = state->obu_has_size_field = 0;
4164 	state->temporal_id = state->spatial_id = 0;
4165 	state->frame_state.uncompressed_header_bytes = 0;
4166 	e = gf_av1_parse_obu_header(bs, obu_type, &state->obu_extension_flag, &state->obu_has_size_field, &state->temporal_id, &state->spatial_id);
4167 	if (e)
4168 		return e;
4169 
4170 	if (state->obu_has_size_field) {
4171 		*obu_size = (u32)gf_av1_leb128_read(bs, NULL);
4172 	}
4173 	else {
4174 		if (*obu_size >= 1 + state->obu_extension_flag) {
4175 			*obu_size = *obu_size - 1 - state->obu_extension_flag;
4176 		}
4177 		else {
4178 			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));
4179 			return GF_NON_COMPLIANT_BITSTREAM;
4180 		}
4181 	}
4182 	hdr_size = (u32)(gf_bs_get_position(bs) - pos);
4183 	if ((gf_bs_available(bs) < *obu_size) || state->bs_overread) {
4184 		gf_bs_seek(bs, pos);
4185 		return GF_BUFFER_TOO_SMALL;
4186 	}
4187 	*obu_size += hdr_size;
4188 	if (obu_hdr_size) *obu_hdr_size = hdr_size;
4189 
4190 
4191 	if (*obu_type != OBU_SEQUENCE_HEADER && *obu_type != OBU_TEMPORAL_DELIMITER &&
4192 		state->OperatingPointIdc != 0 && state->obu_extension_flag == 1)
4193 	{
4194 		u32 inTemporalLayer = (state->OperatingPointIdc >> state->temporal_id) & 1;
4195 		u32 inSpatialLayer = (state->OperatingPointIdc >> (state->spatial_id + 8)) & 1;
4196 		if (!inTemporalLayer || !inSpatialLayer) {
4197 			*obu_type = -1;
4198 			gf_bs_seek(bs, pos + *obu_size);
4199 			return GF_OK;
4200 		}
4201 	}
4202 
4203 	e = GF_OK;
4204 	switch (*obu_type) {
4205 	case OBU_SEQUENCE_HEADER:
4206 		av1_parse_sequence_header_obu(bs, state);
4207 		if (gf_bs_get_position(bs) > pos + *obu_size) {
4208 			GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Sequence header parsing consumed too many bytes !\n"));
4209 			e = GF_NON_COMPLIANT_BITSTREAM;
4210 		}
4211 		gf_bs_seek(bs, pos + *obu_size);
4212 		break;
4213 
4214 	case OBU_METADATA:
4215 #if 0
4216 		//TODO + sample groups
4217 		const ObuMetadataType metadata_type = (u32)read_leb128(bs, NULL); we should check for 16 bits limit(AV1MetadataSampleGroupEntry) for ISOBMFF bindings, see https ://github.com/AOMediaCodec/av1-isobmff/pull/86#issuecomment-416659538
4218 		if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) {
4219 		}
4220 		else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) {
4221 		}
4222 		else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) {
4223 		}
4224 		else if (metadata_type == OBU_METADATA_TYPE_SCALABILITY) {
4225 		}
4226 		else if (metadata_type == METADATA_TYPE_TIMECODE) {
4227 		}
4228 #endif
4229 		GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[AV1] parsing for metadata is not implemented. Forwarding.\n"));
4230 
4231 		if (gf_bs_get_position(bs) > pos + *obu_size) {
4232 			GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Metadata parsing consumed too many bytes !\n"));
4233 			e = GF_NON_COMPLIANT_BITSTREAM;
4234 		}
4235 		gf_bs_seek(bs, pos + *obu_size);
4236 		break;
4237 
4238 	case OBU_FRAME_HEADER:
4239 	case OBU_REDUNDANT_FRAME_HEADER:
4240 		if (state->config) {
4241 			av1_parse_frame_header(bs, state);
4242 			if (gf_bs_get_position(bs) > pos + *obu_size) {
4243 				GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Frame header parsing consumed too many bytes !\n"));
4244 				e = GF_NON_COMPLIANT_BITSTREAM;
4245 			}
4246 		}
4247 		gf_bs_seek(bs, pos + *obu_size);
4248 		break;
4249 	case OBU_FRAME:
4250 		e = av1_parse_frame(bs, state, pos, *obu_size);
4251 		if (gf_bs_get_position(bs) != pos + *obu_size) {
4252 			GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Frame parsing did not consume the right number of bytes !\n"));
4253 			e = GF_NON_COMPLIANT_BITSTREAM;
4254 		}
4255 		gf_bs_seek(bs, pos + *obu_size);
4256 		break;
4257 	case OBU_TILE_GROUP:
4258 		if (state->config) {
4259 			e = av1_parse_tile_group(bs, state, pos, *obu_size);
4260 			if (gf_bs_get_position(bs) != pos + *obu_size) {
4261 				GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] Tile group parsing did not consume the right number of bytes !\n"));
4262 				e = GF_NON_COMPLIANT_BITSTREAM;
4263 			}
4264 		}
4265 		gf_bs_seek(bs, pos + *obu_size);
4266 		break;
4267 	case OBU_TEMPORAL_DELIMITER:
4268 		state->frame_state.seen_frame_header = GF_FALSE;
4269 	case OBU_PADDING:
4270 		gf_bs_seek(bs, pos + *obu_size);
4271 		break;
4272 	default:
4273 		GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[AV1] unknown OBU type %u (size "LLU"). Skipping.\n", *obu_type, *obu_size));
4274 		gf_bs_seek(bs, pos + *obu_size);
4275 		break;
4276 	}
4277 	return e;
4278 }
4279 
4280 
4281 GF_EXPORT
gf_media_prores_parse_bs(GF_BitStream * bs,GF_ProResFrameInfo * prores_frame)4282 GF_Err gf_media_prores_parse_bs(GF_BitStream *bs, GF_ProResFrameInfo *prores_frame)
4283 {
4284 	u32 i, j;
4285 	u64 start, pos;
4286 	memset(prores_frame, 0, sizeof(GF_ProResFrameInfo));
4287 
4288 	start = gf_bs_get_position(bs);
4289 	if (gf_bs_available(bs) < 10)
4290 		return GF_BUFFER_TOO_SMALL;
4291 
4292 	prores_frame->frame_size = gf_bs_read_u32(bs);
4293 	prores_frame->frame_identifier = gf_bs_read_u32(bs);
4294 	if (prores_frame->frame_identifier != GF_4CC('i','c','p','f')) {
4295 		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) ));
4296 		gf_bs_seek(bs, start);
4297 		return GF_NON_COMPLIANT_BITSTREAM;
4298 	}
4299 	/*parse frame header*/
4300 	pos = gf_bs_get_position(bs);
4301 	prores_frame->frame_hdr_size = gf_bs_read_u16(bs);
4302 	if (gf_bs_available(bs) + 2 < prores_frame->frame_hdr_size) {
4303 		gf_bs_seek(bs, start);
4304 		return GF_BUFFER_TOO_SMALL;
4305 	}
4306 	gf_bs_read_u8(bs);
4307 	prores_frame->version = gf_bs_read_u8(bs);
4308 	prores_frame->encoder_id = gf_bs_read_u32(bs);
4309 	prores_frame->width = gf_bs_read_u16(bs);
4310 	prores_frame->height = gf_bs_read_u16(bs);
4311 	prores_frame->chroma_format = gf_bs_read_int(bs, 2);
4312 	gf_bs_read_int(bs, 2);
4313 	prores_frame->interlaced_mode = gf_bs_read_int(bs, 2);
4314 	gf_bs_read_int(bs, 2);
4315 	prores_frame->aspect_ratio_information = gf_bs_read_int(bs, 4);
4316 	prores_frame->framerate_code = gf_bs_read_int(bs, 4);
4317 	prores_frame->color_primaries = gf_bs_read_u8(bs);
4318 	prores_frame->transfer_characteristics = gf_bs_read_u8(bs);
4319 	prores_frame->matrix_coefficients = gf_bs_read_u8(bs);
4320 	gf_bs_read_int(bs, 4);
4321 	prores_frame->alpha_channel_type = gf_bs_read_int(bs, 4);
4322 	gf_bs_read_int(bs, 14);
4323 	prores_frame->load_luma_quant_matrix = gf_bs_read_int(bs, 1);
4324 	prores_frame->load_chroma_quant_matrix = gf_bs_read_int(bs, 1);
4325 	if (prores_frame->load_luma_quant_matrix) {
4326 		for (i=0; i<8; i++) {
4327 			for (j=0; j<8; j++) {
4328 				prores_frame->luma_quant_matrix[i][j] = gf_bs_read_u8(bs);
4329 			}
4330 		}
4331 	}
4332 	if (prores_frame->load_chroma_quant_matrix) {
4333 		for (i=0; i<8; i++) {
4334 			for (j=0; j<8; j++) {
4335 				prores_frame->chroma_quant_matrix[i][j] = gf_bs_read_u8(bs);
4336 			}
4337 		}
4338 	}
4339 	pos = gf_bs_get_position(bs) - pos;
4340 	if (pos != prores_frame->frame_hdr_size) {
4341 		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));
4342 		gf_bs_seek(bs, start);
4343 		return GF_NON_COMPLIANT_BITSTREAM;
4344 	}
4345 	prores_frame->nb_pic = ((prores_frame->interlaced_mode==1) || (prores_frame->interlaced_mode==2)) ? 2 : 1;
4346 	gf_bs_seek(bs, start);
4347 
4348 	return GF_OK;
4349 }
4350 
4351 #endif /*GPAC_DISABLE_AV_PARSERS*/
4352 
4353 GF_EXPORT
gf_mp3_version(u32 hdr)4354 u8 gf_mp3_version(u32 hdr)
4355 {
4356 	return ((hdr >> 19) & 0x3);
4357 }
4358 
4359 GF_EXPORT
gf_mp3_version_name(u32 hdr)4360 const char *gf_mp3_version_name(u32 hdr)
4361 {
4362 	u32 v = gf_mp3_version(hdr);
4363 	switch (v) {
4364 	case 0:
4365 		return "MPEG-2.5";
4366 	case 1:
4367 		return "Reserved";
4368 	case 2:
4369 		return "MPEG-2";
4370 	case 3:
4371 		return "MPEG-1";
4372 	default:
4373 		return "Unknown";
4374 	}
4375 }
4376 
4377 #ifndef GPAC_DISABLE_AV_PARSERS
4378 
4379 GF_EXPORT
gf_mp3_layer(u32 hdr)4380 u8 gf_mp3_layer(u32 hdr)
4381 {
4382 	return 4 - (((hdr >> 17) & 0x3));
4383 }
4384 
4385 GF_EXPORT
gf_mp3_num_channels(u32 hdr)4386 u8 gf_mp3_num_channels(u32 hdr)
4387 {
4388 	if (((hdr >> 6) & 0x3) == 3) return 1;
4389 	return 2;
4390 }
4391 
4392 GF_EXPORT
gf_mp3_sampling_rate(u32 hdr)4393 u16 gf_mp3_sampling_rate(u32 hdr)
4394 {
4395 	u16 res;
4396 	/* extract the necessary fields from the MP3 header */
4397 	u8 version = gf_mp3_version(hdr);
4398 	u8 sampleRateIndex = (hdr >> 10) & 0x3;
4399 
4400 	switch (sampleRateIndex) {
4401 	case 0:
4402 		res = 44100;
4403 		break;
4404 	case 1:
4405 		res = 48000;
4406 		break;
4407 	case 2:
4408 		res = 32000;
4409 		break;
4410 	default:
4411 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] Samplerate index not valid\n"));
4412 		return 0;
4413 	}
4414 	/*reserved or MPEG-1*/
4415 	if (version & 1) return res;
4416 
4417 	/*MPEG-2*/
4418 	res /= 2;
4419 	/*MPEG-2.5*/
4420 	if (version == 0) res /= 2;
4421 	return res;
4422 }
4423 
4424 GF_EXPORT
gf_mp3_window_size(u32 hdr)4425 u16 gf_mp3_window_size(u32 hdr)
4426 {
4427 	u8 version = gf_mp3_version(hdr);
4428 	u8 layer = gf_mp3_layer(hdr);
4429 
4430 	if (layer == 3) {
4431 		if (version == 3) return 1152;
4432 		return 576;
4433 	}
4434 	if (layer == 2) return 1152;
4435 	return 384;
4436 }
4437 
4438 GF_EXPORT
gf_mp3_object_type_indication(u32 hdr)4439 u8 gf_mp3_object_type_indication(u32 hdr)
4440 {
4441 	switch (gf_mp3_version(hdr)) {
4442 	case 3:
4443 		return GF_CODECID_MPEG_AUDIO;
4444 	case 2:
4445 	case 0:
4446 		return GF_CODECID_MPEG2_PART3;
4447 	default:
4448 		return 0x00;
4449 	}
4450 }
4451 
4452 /*aligned bitrate parsing with libMAD*/
4453 
4454 static
4455 u32 const bitrate_table[5][15] = {
4456 	/* MPEG-1 */
4457 	{	0,  32000,  64000,  96000, 128000, 160000, 192000, 224000,  /* Layer I   */
4458 		256000, 288000, 320000, 352000, 384000, 416000, 448000
4459 	},
4460 	{	0,  32000,  48000,  56000,  64000,  80000,  96000, 112000,  /* Layer II  */
4461 		128000, 160000, 192000, 224000, 256000, 320000, 384000
4462 	},
4463 	{	0,  32000,  40000,  48000,  56000,  64000,  80000,  96000,  /* Layer III */
4464 		112000, 128000, 160000, 192000, 224000, 256000, 320000
4465 	},
4466 
4467 	/* MPEG-2 LSF */
4468 	{	0,  32000,  48000,  56000,  64000,  80000,  96000, 112000,  /* Layer I   */
4469 		128000, 144000, 160000, 176000, 192000, 224000, 256000
4470 	},
4471 	{	0,   8000,  16000,  24000,  32000,  40000,  48000,  56000,  /* Layers    */
4472 		64000,  80000,  96000, 112000, 128000, 144000, 160000
4473 	} /* II & III  */
4474 };
4475 
4476 
gf_mp3_bit_rate(u32 hdr)4477 u32 gf_mp3_bit_rate(u32 hdr)
4478 {
4479 	u8 version = gf_mp3_version(hdr);
4480 	u8 layer = gf_mp3_layer(hdr);
4481 	u8 bitRateIndex = (hdr >> 12) & 0xF;
4482 	u32 lidx;
4483 	/*MPEG-1*/
4484 	if (version & 1) {
4485 		if (!layer) {
4486 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] layer index not valid\n"));
4487 			return 0;
4488 		}
4489 		lidx = layer - 1;
4490 	}
4491 	/*MPEG-2/2.5*/
4492 	else {
4493 		lidx = 3 + (layer >> 1);
4494 	}
4495 	if (lidx>4) {
4496 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[MPEG-1/2 Audio] layer index not valid\n"));
4497 		return 0;
4498 	}
4499 	return bitrate_table[lidx][bitRateIndex];
4500 }
4501 
4502 
4503 
4504 GF_EXPORT
gf_mp3_frame_size(u32 hdr)4505 u16 gf_mp3_frame_size(u32 hdr)
4506 {
4507 	u8 version = gf_mp3_version(hdr);
4508 	u8 layer = gf_mp3_layer(hdr);
4509 	u32 pad = ((hdr >> 9) & 0x1) ? 1 : 0;
4510 	u32 bitrate = gf_mp3_bit_rate(hdr);
4511 	u32 samplerate = gf_mp3_sampling_rate(hdr);
4512 
4513 	u32 frameSize = 0;
4514 	if (!samplerate || !bitrate) return 0;
4515 
4516 	if (layer == 1) {
4517 		frameSize = ((12 * bitrate / samplerate) + pad) * 4;
4518 	}
4519 	else {
4520 		u32 slots_per_frame = 144;
4521 		if ((layer == 3) && !(version & 1)) slots_per_frame = 72;
4522 		frameSize = (slots_per_frame * bitrate / samplerate) + pad;
4523 	}
4524 	return (u16)frameSize;
4525 }
4526 
4527 
4528 GF_EXPORT
gf_mp3_get_next_header(FILE * in)4529 u32 gf_mp3_get_next_header(FILE* in)
4530 {
4531 	u8 b, state = 0;
4532 	u32 dropped = 0;
4533 	unsigned char bytes[4];
4534 	bytes[0] = bytes[1] = bytes[2] = bytes[3] = 0;
4535 
4536 	while (1) {
4537 		if (gf_fread(&b, 1, in) == 0) return 0;
4538 
4539 		if (state == 3) {
4540 			bytes[state] = b;
4541 			return GF_4CC((u32)bytes[0], bytes[1], bytes[2], bytes[3]);
4542 		}
4543 		if (state == 2) {
4544 			if (((b & 0xF0) == 0) || ((b & 0xF0) == 0xF0) || ((b & 0x0C) == 0x0C)) {
4545 				if (bytes[1] == 0xFF) state = 1;
4546 				else state = 0;
4547 			}
4548 			else {
4549 				bytes[state] = b;
4550 				state = 3;
4551 			}
4552 		}
4553 		if (state == 1) {
4554 			if (((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) {
4555 				bytes[state] = b;
4556 				state = 2;
4557 			}
4558 			else {
4559 				state = 0;
4560 			}
4561 		}
4562 
4563 		if (state == 0) {
4564 			if (b == 0xFF) {
4565 				bytes[state] = b;
4566 				state = 1;
4567 			}
4568 			else {
4569 				if ((dropped == 0) && ((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) {
4570 					bytes[0] = (u8)0xFF;
4571 					bytes[1] = b;
4572 					state = 2;
4573 				}
4574 				else {
4575 					dropped++;
4576 				}
4577 			}
4578 		}
4579 	}
4580 	return 0;
4581 }
4582 
4583 GF_EXPORT
gf_mp3_get_next_header_mem(const u8 * buffer,u32 size,u32 * pos)4584 u32 gf_mp3_get_next_header_mem(const u8 *buffer, u32 size, u32 *pos)
4585 {
4586 	u32 cur;
4587 	u8 b, state = 0;
4588 	u32 dropped = 0;
4589 	unsigned char bytes[4];
4590 	bytes[0] = bytes[1] = bytes[2] = bytes[3] = 0;
4591 
4592 	cur = 0;
4593 	*pos = 0;
4594 	while (cur < size) {
4595 		b = (u8)buffer[cur];
4596 		cur++;
4597 
4598 		if (state == 3) {
4599 			u32 val;
4600 			bytes[state] = b;
4601 			val = GF_4CC((u32)bytes[0], bytes[1], bytes[2], bytes[3]);
4602 			if (gf_mp3_frame_size(val)) {
4603 				*pos = dropped;
4604 				return val;
4605 			}
4606 			state = 0;
4607 			dropped = cur;
4608 		}
4609 		if (state == 2) {
4610 			if (((b & 0xF0) == 0) || ((b & 0xF0) == 0xF0) || ((b & 0x0C) == 0x0C)) {
4611 				if (bytes[1] == 0xFF) {
4612 					state = 1;
4613 					dropped += 1;
4614 				}
4615 				else {
4616 					state = 0;
4617 					dropped = cur;
4618 				}
4619 			}
4620 			else {
4621 				bytes[state] = b;
4622 				state = 3;
4623 			}
4624 		}
4625 		if (state == 1) {
4626 			if (((b & 0xE0) == 0xE0) && ((b & 0x18) != 0x08) && ((b & 0x06) != 0)) {
4627 				bytes[state] = b;
4628 				state = 2;
4629 			}
4630 			else {
4631 				state = 0;
4632 				dropped = cur;
4633 			}
4634 		}
4635 
4636 		if (state == 0) {
4637 			if (b == 0xFF) {
4638 				bytes[state] = b;
4639 				state = 1;
4640 			}
4641 			else {
4642 				dropped++;
4643 			}
4644 		}
4645 	}
4646 	return 0;
4647 }
4648 
4649 #endif /*GPAC_DISABLE_AV_PARSERS*/
4650 
4651 
4652 GF_EXPORT
gf_avc_is_rext_profile(u8 profile_idc)4653 Bool gf_avc_is_rext_profile(u8 profile_idc)
4654 {
4655 	switch (profile_idc) {
4656 	case 100:
4657 	case 110:
4658 	case 122:
4659 	case 244:
4660 	case 44:
4661 	case 83:
4662 	case 86:
4663 	case 118:
4664 	case 128:
4665 	case 138:
4666 	case 139:
4667 	case 134:
4668 	case 135:
4669 		return GF_TRUE;
4670 	default:
4671 		return GF_FALSE;
4672 	}
4673 }
4674 
4675 GF_EXPORT
gf_avc_get_profile_name(u8 video_prof)4676 const char *gf_avc_get_profile_name(u8 video_prof)
4677 {
4678 	switch (video_prof) {
4679 	case 0x42:
4680 		return "Baseline";
4681 	case 0x4D:
4682 		return "Main";
4683 	case 0x53:
4684 		return "Scalable Baseline";
4685 	case 0x56:
4686 		return "Scalable High";
4687 	case 0x58:
4688 		return "Extended";
4689 	case 0x64:
4690 		return "High";
4691 	case 0x6E:
4692 		return "High 10";
4693 	case 0x7A:
4694 		return "High 4:2:2";
4695 	case 0x90:
4696 	case 0xF4:
4697 		return "High 4:4:4";
4698 	default:
4699 		return "Unknown";
4700 	}
4701 }
4702 
4703 GF_EXPORT
gf_hevc_get_profile_name(u8 video_prof)4704 const char *gf_hevc_get_profile_name(u8 video_prof)
4705 {
4706 	switch (video_prof) {
4707 	case 0x01:
4708 		return "Main";
4709 	case 0x02:
4710 		return "Main 10";
4711 	case 0x03:
4712 		return "Main Still Picture";
4713 	default:
4714 		return "Unknown";
4715 	}
4716 }
4717 GF_EXPORT
gf_avc_hevc_get_chroma_format_name(u8 chroma_format)4718 const char *gf_avc_hevc_get_chroma_format_name(u8 chroma_format)
4719 {
4720 	switch (chroma_format) {
4721 	case 1:
4722 		return "YUV 4:2:0";
4723 	case 2:
4724 		return "YUV 4:2:2";
4725 	case 3:
4726 		return "YUV 4:4:4";
4727 	default:
4728 		return "Unknown";
4729 	}
4730 }
4731 
4732 #ifndef GPAC_DISABLE_AV_PARSERS
4733 
4734 
4735 static u8 avc_golomb_bits[256] = {
4736 	8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3,
4737 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2,
4738 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
4739 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1,
4740 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
4741 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
4742 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
4743 	1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
4744 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4745 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4746 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4747 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4748 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4749 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4750 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4751 	0
4752 };
4753 
gf_bs_get_ue(GF_BitStream * bs)4754 u32 gf_bs_get_ue(GF_BitStream *bs)
4755 {
4756 	u8 coded;
4757 	u32 bits = 0, read = 0;
4758 	while (1) {
4759 		read = gf_bs_peek_bits(bs, 8, 0);
4760 		if (read) break;
4761 		//check whether we still have bits once the peek is done since we may have less than 8 bits available
4762 		if (!gf_bs_available(bs)) {
4763 			//log moved as debug, erro log thrown by gf_bs_read_*
4764 			GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[AVC/HEVC] Not enough bits in bitstream !!\n"));
4765 			return 0;
4766 		}
4767 		gf_bs_read_int(bs, 8);
4768 		bits += 8;
4769 	}
4770 	coded = avc_golomb_bits[read];
4771 	gf_bs_read_int(bs, coded);
4772 	bits += coded;
4773 	return gf_bs_read_int(bs, bits + 1) - 1;
4774 }
4775 
gf_bs_get_se(GF_BitStream * bs)4776 s32 gf_bs_get_se(GF_BitStream *bs)
4777 {
4778 	u32 v = gf_bs_get_ue(bs);
4779 	if ((v & 0x1) == 0) return (s32)(0 - (v >> 1));
4780 	return (v + 1) >> 1;
4781 }
4782 
gf_bs_set_ue(GF_BitStream * bs,u32 num)4783 void gf_bs_set_ue(GF_BitStream *bs, u32 num) {
4784 	s32 length = 1;
4785 	s32 temp = ++num;
4786 
4787 	while (temp != 1) {
4788 		temp >>= 1;
4789 		length += 2;
4790 	}
4791 
4792 	gf_bs_write_int(bs, 0, length >> 1);
4793 	gf_bs_write_int(bs, num, (length + 1) >> 1);
4794 }
4795 
gf_bs_set_se(GF_BitStream * bs,s32 num)4796 void gf_bs_set_se(GF_BitStream *bs, s32 num)
4797 {
4798 	u32 v;
4799 	if (num <= 0)
4800 		v = (-1 * num) << 1;
4801 	else
4802 		v = (num << 1) - 1;
4803 
4804 	gf_bs_set_ue(bs, v);
4805 }
4806 
gf_media_nalu_is_start_code(GF_BitStream * bs)4807 u32 gf_media_nalu_is_start_code(GF_BitStream *bs)
4808 {
4809 	u8 s1, s2, s3, s4;
4810 	Bool is_sc = 0;
4811 	u64 pos = gf_bs_get_position(bs);
4812 	s1 = gf_bs_read_int(bs, 8);
4813 	s2 = gf_bs_read_int(bs, 8);
4814 	if (!s1 && !s2) {
4815 		s3 = gf_bs_read_int(bs, 8);
4816 		if (s3 == 0x01) is_sc = 3;
4817 		else if (!s3) {
4818 			s4 = gf_bs_read_int(bs, 8);
4819 			if (s4 == 0x01) is_sc = 4;
4820 		}
4821 	}
4822 	gf_bs_seek(bs, pos + is_sc);
4823 	return is_sc;
4824 }
4825 
4826 /*read that amount of data at each IO access rather than fetching byte by byte...*/
4827 #define AVC_CACHE_SIZE	4096
4828 
gf_media_nalu_locate_start_code_bs(GF_BitStream * bs,Bool locate_trailing)4829 static u32 gf_media_nalu_locate_start_code_bs(GF_BitStream *bs, Bool locate_trailing)
4830 {
4831 	u32 v, bpos, nb_cons_zeros = 0;
4832 	char avc_cache[AVC_CACHE_SIZE];
4833 	u64 end, cache_start, load_size;
4834 	u64 start = gf_bs_get_position(bs);
4835 	if (start < 3) return 0;
4836 
4837 	load_size = 0;
4838 	bpos = 0;
4839 	cache_start = 0;
4840 	end = 0;
4841 	v = 0xffffffff;
4842 	while (!end) {
4843 		/*refill cache*/
4844 		if (bpos == (u32)load_size) {
4845 			if (!gf_bs_available(bs)) break;
4846 			load_size = gf_bs_available(bs);
4847 			if (load_size > AVC_CACHE_SIZE) load_size = AVC_CACHE_SIZE;
4848 			bpos = 0;
4849 			cache_start = gf_bs_get_position(bs);
4850 			gf_bs_read_data(bs, avc_cache, (u32)load_size);
4851 		}
4852 		v = ( (v<<8) & 0xFFFFFF00) | ((u32) avc_cache[bpos]);
4853 		bpos++;
4854 
4855 		if (locate_trailing) {
4856 			if ((v & 0x000000FF) == 0) nb_cons_zeros++;
4857 			else nb_cons_zeros = 0;
4858 		}
4859 
4860 		if (v == 0x00000001) end = cache_start + bpos - 4;
4861 		else if ((v & 0x00FFFFFF) == 0x00000001) end = cache_start + bpos - 3;
4862 	}
4863 
4864 	gf_bs_seek(bs, start);
4865 	if (!end) end = gf_bs_get_size(bs);
4866 	if (locate_trailing) {
4867 		if (nb_cons_zeros >= 3)
4868 			return (u32)(end - start - nb_cons_zeros);
4869 	}
4870 	return (u32)(end - start);
4871 }
4872 
4873 GF_EXPORT
gf_media_nalu_next_start_code_bs(GF_BitStream * bs)4874 u32 gf_media_nalu_next_start_code_bs(GF_BitStream *bs)
4875 {
4876 	return gf_media_nalu_locate_start_code_bs(bs, 0);
4877 }
4878 
4879 GF_EXPORT
gf_media_nalu_next_start_code(const u8 * data,u32 data_len,u32 * sc_size)4880 u32 gf_media_nalu_next_start_code(const u8 *data, u32 data_len, u32 *sc_size)
4881 {
4882 	u32 avail = data_len;
4883 	const u8 *cur = data;
4884 
4885 	while (cur) {
4886 		u32 v, bpos;
4887 		u8 *next_zero = memchr(cur, 0, avail);
4888 		if (!next_zero) return data_len;
4889 
4890 		v = 0xffffff00;
4891 		bpos = (u32)(next_zero - data) + 1;
4892 		while (1) {
4893 			u8 cval;
4894 			if (bpos == (u32)data_len)
4895 				return data_len;
4896 
4897 			cval = data[bpos];
4898 			v = ((v << 8) & 0xFFFFFF00) | ((u32)cval);
4899 			bpos++;
4900 			if (v == 0x00000001) {
4901 				*sc_size = 4;
4902 				return bpos - 4;
4903 			}
4904 			else if ((v & 0x00FFFFFF) == 0x00000001) {
4905 				*sc_size = 3;
4906 				return bpos - 3;
4907 			}
4908 			if (cval)
4909 				break;
4910 		}
4911 		if (bpos >= data_len)
4912 			break;
4913 		cur = data + bpos;
4914 		avail = data_len - bpos;
4915 	}
4916 	return data_len;
4917 }
4918 
gf_media_avc_slice_is_intra(AVCState * avc)4919 Bool gf_media_avc_slice_is_intra(AVCState *avc)
4920 {
4921 	switch (avc->s_info.slice_type) {
4922 	case GF_AVC_TYPE_I:
4923 	case GF_AVC_TYPE2_I:
4924 	case GF_AVC_TYPE_SI:
4925 	case GF_AVC_TYPE2_SI:
4926 		return 1;
4927 	default:
4928 		return 0;
4929 	}
4930 }
4931 
4932 #if 0 //unused
4933 Bool gf_media_avc_slice_is_IDR(AVCState *avc)
4934 {
4935 	if (avc->sei.recovery_point.valid)
4936 	{
4937 		avc->sei.recovery_point.valid = 0;
4938 		return 1;
4939 	}
4940 	if (avc->s_info.nal_unit_type != GF_AVC_NALU_IDR_SLICE)
4941 		return 0;
4942 	return gf_media_avc_slice_is_intra(avc);
4943 }
4944 #endif
4945 
4946 struct sar_ {
4947 	u32 w, h;
4948 };
4949 
4950 static const struct sar_ avc_sar[] = {
4951 	{ 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
4952 	{ 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
4953 	{ 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
4954 	{ 64, 33 }, { 160,99 }, {  4,  3 }, {  3,  2 },
4955 	{  2,  1 }
4956 };
4957 
4958 
4959 /*ISO 14496-10 (N11084) E.1.2*/
avc_parse_hrd_parameters(GF_BitStream * bs,AVC_HRD * hrd)4960 static void avc_parse_hrd_parameters(GF_BitStream *bs, AVC_HRD *hrd)
4961 {
4962 	int i, cpb_cnt_minus1;
4963 
4964 	cpb_cnt_minus1 = gf_bs_get_ue(bs);		/*cpb_cnt_minus1*/
4965 	if (cpb_cnt_minus1 > 31)
4966 		GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] invalid cpb_cnt_minus1 value: %d (expected in [0;31])\n", cpb_cnt_minus1));
4967 	gf_bs_read_int(bs, 4);				/*bit_rate_scale*/
4968 	gf_bs_read_int(bs, 4);				/*cpb_size_scale*/
4969 
4970 	/*for( SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ ) {*/
4971 	for (i = 0; i <= cpb_cnt_minus1; i++) {
4972 		gf_bs_get_ue(bs);					/*bit_rate_value_minus1[ SchedSelIdx ]*/
4973 		gf_bs_get_ue(bs);					/*cpb_size_value_minus1[ SchedSelIdx ]*/
4974 		gf_bs_read_int(bs, 1);			/*cbr_flag[ SchedSelIdx ]*/
4975 	}
4976 	gf_bs_read_int(bs, 5);											/*initial_cpb_removal_delay_length_minus1*/
4977 	hrd->cpb_removal_delay_length_minus1 = gf_bs_read_int(bs, 5);	/*cpb_removal_delay_length_minus1*/
4978 	hrd->dpb_output_delay_length_minus1 = gf_bs_read_int(bs, 5);	/*dpb_output_delay_length_minus1*/
4979 	hrd->time_offset_length = gf_bs_read_int(bs, 5);				/*time_offset_length*/
4980 
4981 	return;
4982 }
4983 
4984 /*returns the nal_size without emulation prevention bytes*/
gf_media_nalu_emulation_bytes_add_count(u8 * buffer,u32 nal_size)4985 u32 gf_media_nalu_emulation_bytes_add_count(u8 *buffer, u32 nal_size)
4986 {
4987 	u32 i = 0, emulation_bytes_count = 0;
4988 	u8 num_zero = 0;
4989 
4990 	while (i < nal_size) {
4991 		/*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
4992 		other than the following sequences shall not occur at any byte-aligned position:
4993 		\96 0x00000300
4994 		\96 0x00000301
4995 		\96 0x00000302
4996 		\96 0x00000303"
4997 		*/
4998 		if (num_zero == 2 && (u8)buffer[i] < 0x04) {
4999 			/*emulation code found*/
5000 			num_zero = 0;
5001 			emulation_bytes_count++;
5002 			if (!buffer[i])
5003 				num_zero = 1;
5004 		}
5005 		else {
5006 			if (!buffer[i])
5007 				num_zero++;
5008 			else
5009 				num_zero = 0;
5010 		}
5011 		i++;
5012 	}
5013 	return emulation_bytes_count;
5014 }
5015 
gf_media_nalu_add_emulation_bytes(const u8 * buffer_src,u8 * buffer_dst,u32 nal_size)5016 u32 gf_media_nalu_add_emulation_bytes(const u8 *buffer_src, u8 *buffer_dst, u32 nal_size)
5017 {
5018 	u32 i = 0, emulation_bytes_count = 0;
5019 	u8 num_zero = 0;
5020 
5021 	while (i < nal_size) {
5022 		/*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
5023 		other than the following sequences shall not occur at any byte-aligned position:
5024 		0x00000300
5025 		0x00000301
5026 		0x00000302
5027 		0x00000303"
5028 		*/
5029 		if (num_zero == 2 && (u8)buffer_src[i] < 0x04) {
5030 			/*add emulation code*/
5031 			num_zero = 0;
5032 			buffer_dst[i + emulation_bytes_count] = 0x03;
5033 			emulation_bytes_count++;
5034 			if (!buffer_src[i])
5035 				num_zero = 1;
5036 		}
5037 		else {
5038 			if (!buffer_src[i])
5039 				num_zero++;
5040 			else
5041 				num_zero = 0;
5042 		}
5043 		buffer_dst[i + emulation_bytes_count] = buffer_src[i];
5044 		i++;
5045 	}
5046 	return nal_size + emulation_bytes_count;
5047 }
5048 
5049 /*returns the nal_size without emulation prevention bytes*/
gf_media_nalu_emulation_bytes_remove_count(const u8 * buffer,u32 nal_size)5050 u32 gf_media_nalu_emulation_bytes_remove_count(const u8 *buffer, u32 nal_size)
5051 {
5052 	u32 i = 0, emulation_bytes_count = 0;
5053 	u8 num_zero = 0;
5054 
5055 	while (i < nal_size)
5056 	{
5057 		/*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
5058 		  other than the following sequences shall not occur at any byte-aligned position:
5059 		  \96 0x00000300
5060 		  \96 0x00000301
5061 		  \96 0x00000302
5062 		  \96 0x00000303"
5063 		*/
5064 		if (num_zero == 2
5065 			&& buffer[i] == 0x03
5066 			&& i + 1 < nal_size /*next byte is readable*/
5067 			&& (u8)buffer[i + 1] < 0x04)
5068 		{
5069 			/*emulation code found*/
5070 			num_zero = 0;
5071 			emulation_bytes_count++;
5072 			i++;
5073 		}
5074 
5075 		if (!buffer[i])
5076 			num_zero++;
5077 		else
5078 			num_zero = 0;
5079 
5080 		i++;
5081 	}
5082 
5083 	return emulation_bytes_count;
5084 }
5085 
5086 /*nal_size is updated to allow better error detection*/
5087 GF_EXPORT
gf_media_nalu_remove_emulation_bytes(const u8 * buffer_src,u8 * buffer_dst,u32 nal_size)5088 u32 gf_media_nalu_remove_emulation_bytes(const u8 *buffer_src, u8 *buffer_dst, u32 nal_size)
5089 {
5090 	u32 i = 0, emulation_bytes_count = 0;
5091 	u8 num_zero = 0;
5092 
5093 	while (i < nal_size)
5094 	{
5095 		/*ISO 14496-10: "Within the NAL unit, any four-byte sequence that starts with 0x000003
5096 		  other than the following sequences shall not occur at any byte-aligned position:
5097 		  0x00000300
5098 		  0x00000301
5099 		  0x00000302
5100 		  0x00000303"
5101 		*/
5102 		if (num_zero == 2
5103 			&& buffer_src[i] == 0x03
5104 			&& i + 1 < nal_size /*next byte is readable*/
5105 			&& (u8)buffer_src[i + 1] < 0x04)
5106 		{
5107 			/*emulation code found*/
5108 			num_zero = 0;
5109 			emulation_bytes_count++;
5110 			i++;
5111 		}
5112 
5113 		buffer_dst[i - emulation_bytes_count] = buffer_src[i];
5114 
5115 		if (!buffer_src[i])
5116 			num_zero++;
5117 		else
5118 			num_zero = 0;
5119 
5120 		i++;
5121 	}
5122 
5123 	return nal_size - emulation_bytes_count;
5124 }
5125 
gf_media_avc_read_sps_bs_internal(GF_BitStream * bs,AVCState * avc,u32 subseq_sps,u32 * vui_flag_pos,u32 nal_hdr)5126 static s32 gf_media_avc_read_sps_bs_internal(GF_BitStream *bs, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos, u32 nal_hdr)
5127 {
5128 	AVC_SPS *sps;
5129 	s32 mb_width, mb_height, sps_id = -1;
5130 	u32 profile_idc, level_idc, pcomp, i, chroma_format_idc, cl = 0, cr = 0, ct = 0, cb = 0, luma_bd, chroma_bd;
5131 	u8 separate_colour_plane_flag = 0;
5132 
5133 	if (!vui_flag_pos) {
5134 		gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
5135 	}
5136 
5137 	if (!bs) {
5138 		return -1;
5139 	}
5140 
5141 	if (!nal_hdr) {
5142 		/*nal_hdr =*/ gf_bs_read_int(bs, 8);
5143 	}
5144 	profile_idc = gf_bs_read_int(bs, 8);
5145 
5146 	pcomp = gf_bs_read_int(bs, 8);
5147 	/*sanity checks*/
5148 	if (pcomp & 0x3)
5149 		return -1;
5150 
5151 	level_idc = gf_bs_read_int(bs, 8);
5152 
5153 	/*SubsetSps is used to be sure that AVC SPS are not going to be scratched
5154 	by subset SPS. According to the SVC standard, subset SPS can have the same sps_id
5155 	than its base layer, but it does not refer to the same SPS. */
5156 	sps_id = gf_bs_get_ue(bs) + GF_SVC_SSPS_ID_SHIFT * subseq_sps;
5157 	if (sps_id >= 32) {
5158 		return -1;
5159 	}
5160 	if (sps_id < 0) {
5161 		return -1;
5162 	}
5163 
5164 	luma_bd = chroma_bd = 0;
5165 	sps = &avc->sps[sps_id];
5166 	chroma_format_idc = sps->ChromaArrayType = 1;
5167 	sps->state |= subseq_sps ? AVC_SUBSPS_PARSED : AVC_SPS_PARSED;
5168 
5169 	/*High Profile and SVC*/
5170 	switch (profile_idc) {
5171 	case 100:
5172 	case 110:
5173 	case 122:
5174 	case 244:
5175 	case 44:
5176 		/*sanity checks: note1 from 7.4.2.1.1 of iso/iec 14496-10-N11084*/
5177 		if (pcomp & 0xE0)
5178 			return -1;
5179 	case 83:
5180 	case 86:
5181 	case 118:
5182 	case 128:
5183 		chroma_format_idc = gf_bs_get_ue(bs);
5184 		sps->ChromaArrayType = chroma_format_idc;
5185 		if (chroma_format_idc == 3) {
5186 			separate_colour_plane_flag = gf_bs_read_int(bs, 1);
5187 			/*
5188 			Depending on the value of separate_colour_plane_flag, the value of the variable ChromaArrayType is assigned as follows.
5189 			\96	If separate_colour_plane_flag is equal to 0, ChromaArrayType is set equal to chroma_format_idc.
5190 			\96	Otherwise (separate_colour_plane_flag is equal to 1), ChromaArrayType is set equal to 0.
5191 			*/
5192 			if (separate_colour_plane_flag) sps->ChromaArrayType = 0;
5193 		}
5194 		luma_bd = gf_bs_get_ue(bs);
5195 		chroma_bd = gf_bs_get_ue(bs);
5196 		/*qpprime_y_zero_transform_bypass_flag = */ gf_bs_read_int(bs, 1);
5197 		/*seq_scaling_matrix_present_flag*/
5198 		if (gf_bs_read_int(bs, 1)) {
5199 			u32 k;
5200 			for (k = 0; k < 8; k++) {
5201 				if (gf_bs_read_int(bs, 1)) {
5202 					u32 z, last = 8, next = 8;
5203 					u32 sl = k < 6 ? 16 : 64;
5204 					for (z = 0; z < sl; z++) {
5205 						if (next) {
5206 							s32 delta = gf_bs_get_se(bs);
5207 							next = (last + delta + 256) % 256;
5208 						}
5209 						last = next ? next : last;
5210 					}
5211 				}
5212 			}
5213 		}
5214 		break;
5215 	}
5216 
5217 	sps->profile_idc = profile_idc;
5218 	sps->level_idc = level_idc;
5219 	sps->prof_compat = pcomp;
5220 	sps->log2_max_frame_num = gf_bs_get_ue(bs) + 4;
5221 	sps->poc_type = gf_bs_get_ue(bs);
5222 	sps->chroma_format = chroma_format_idc;
5223 	sps->luma_bit_depth_m8 = luma_bd;
5224 	sps->chroma_bit_depth_m8 = chroma_bd;
5225 
5226 	if (sps->poc_type == 0) {
5227 		sps->log2_max_poc_lsb = gf_bs_get_ue(bs) + 4;
5228 	}
5229 	else if (sps->poc_type == 1) {
5230 		sps->delta_pic_order_always_zero_flag = gf_bs_read_int(bs, 1);
5231 		sps->offset_for_non_ref_pic = gf_bs_get_se(bs);
5232 		sps->offset_for_top_to_bottom_field = gf_bs_get_se(bs);
5233 		sps->poc_cycle_length = gf_bs_get_ue(bs);
5234 		if (sps->poc_cycle_length > GF_ARRAY_LENGTH(sps->offset_for_ref_frame)) {
5235 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[avc-h264] offset_for_ref_frame overflow from poc_cycle_length\n"));
5236 			return -1;
5237 		}
5238 		for (i = 0; i < sps->poc_cycle_length; i++) sps->offset_for_ref_frame[i] = gf_bs_get_se(bs);
5239 	}
5240 	if (sps->poc_type > 2) {
5241 		return -1;
5242 	}
5243 	sps->max_num_ref_frames = gf_bs_get_ue(bs);
5244 	sps->gaps_in_frame_num_value_allowed_flag = gf_bs_read_int(bs, 1);
5245 	mb_width = gf_bs_get_ue(bs) + 1;
5246 	mb_height = gf_bs_get_ue(bs) + 1;
5247 
5248 	sps->frame_mbs_only_flag = gf_bs_read_int(bs, 1);
5249 
5250 	sps->width = mb_width * 16;
5251 	sps->height = (2 - sps->frame_mbs_only_flag) * mb_height * 16;
5252 
5253 	if (!sps->frame_mbs_only_flag) sps->mb_adaptive_frame_field_flag = gf_bs_read_int(bs, 1);
5254 	gf_bs_read_int(bs, 1); /*direct_8x8_inference_flag*/
5255 
5256 	if (gf_bs_read_int(bs, 1)) { /*crop*/
5257 		int CropUnitX, CropUnitY, SubWidthC = -1, SubHeightC = -1;
5258 
5259 		if (chroma_format_idc == 1) {
5260 			SubWidthC = 2; SubHeightC = 2;
5261 		}
5262 		else if (chroma_format_idc == 2) {
5263 			SubWidthC = 2; SubHeightC = 1;
5264 		}
5265 		else if ((chroma_format_idc == 3) && (separate_colour_plane_flag == 0)) {
5266 			SubWidthC = 1; SubHeightC = 1;
5267 		}
5268 
5269 		if (sps->ChromaArrayType == 0) {
5270 			assert(SubWidthC == -1);
5271 			CropUnitX = 1;
5272 			CropUnitY = 2 - sps->frame_mbs_only_flag;
5273 		}
5274 		else {
5275 			CropUnitX = SubWidthC;
5276 			CropUnitY = SubHeightC * (2 - sps->frame_mbs_only_flag);
5277 		}
5278 
5279 		cl = gf_bs_get_ue(bs); /*crop_left*/
5280 		cr = gf_bs_get_ue(bs); /*crop_right*/
5281 		ct = gf_bs_get_ue(bs); /*crop_top*/
5282 		cb = gf_bs_get_ue(bs); /*crop_bottom*/
5283 
5284 		sps->width -= CropUnitX * (cl + cr);
5285 		sps->height -= CropUnitY * (ct + cb);
5286 		cl *= CropUnitX;
5287 		cr *= CropUnitX;
5288 		ct *= CropUnitY;
5289 		cb *= CropUnitY;
5290 	}
5291 	sps->crop.left = cl;
5292 	sps->crop.right = cr;
5293 	sps->crop.top = ct;
5294 	sps->crop.bottom = cb;
5295 
5296 	if (vui_flag_pos) {
5297 		*vui_flag_pos = (u32)gf_bs_get_bit_offset(bs);
5298 	}
5299 	/*vui_parameters_present_flag*/
5300 	sps->vui_parameters_present_flag = gf_bs_read_int(bs, 1);
5301 	if (sps->vui_parameters_present_flag) {
5302 		sps->vui.aspect_ratio_info_present_flag = gf_bs_read_int(bs, 1);
5303 		if (sps->vui.aspect_ratio_info_present_flag) {
5304 			s32 aspect_ratio_idc = gf_bs_read_int(bs, 8);
5305 			if (aspect_ratio_idc == 255) {
5306 				sps->vui.par_num = gf_bs_read_int(bs, 16); /*AR num*/
5307 				sps->vui.par_den = gf_bs_read_int(bs, 16); /*AR den*/
5308 			}
5309 			else if (aspect_ratio_idc < sizeof(avc_sar) / sizeof(struct sar_)) {
5310 				sps->vui.par_num = avc_sar[aspect_ratio_idc].w;
5311 				sps->vui.par_den = avc_sar[aspect_ratio_idc].h;
5312 			}
5313 			else {
5314 				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"));
5315 			}
5316 		}
5317 		sps->vui.overscan_info_present_flag = gf_bs_read_int(bs, 1);
5318 		if (sps->vui.overscan_info_present_flag)
5319 			gf_bs_read_int(bs, 1);		/* overscan_appropriate_flag */
5320 
5321 		/* default values */
5322 		sps->vui.video_format = 5;
5323 		sps->vui.colour_primaries = 2;
5324 		sps->vui.transfer_characteristics = 2;
5325 		sps->vui.matrix_coefficients = 2;
5326 		/* now read values if possible */
5327 		sps->vui.video_signal_type_present_flag = gf_bs_read_int(bs, 1);
5328 		if (sps->vui.video_signal_type_present_flag) {
5329 			sps->vui.video_format = gf_bs_read_int(bs, 3);
5330 			sps->vui.video_full_range_flag = gf_bs_read_int(bs, 1);
5331 			sps->vui.colour_description_present_flag = gf_bs_read_int(bs, 1);
5332 			if (sps->vui.colour_description_present_flag) {
5333 				sps->vui.colour_primaries = gf_bs_read_int(bs, 8);
5334 				sps->vui.transfer_characteristics = gf_bs_read_int(bs, 8);
5335 				sps->vui.matrix_coefficients = gf_bs_read_int(bs, 8);
5336 			}
5337 		}
5338 
5339 		if (gf_bs_read_int(bs, 1)) {	/* chroma_location_info_present_flag */
5340 			gf_bs_get_ue(bs);				/* chroma_sample_location_type_top_field */
5341 			gf_bs_get_ue(bs);				/* chroma_sample_location_type_bottom_field */
5342 		}
5343 
5344 		sps->vui.timing_info_present_flag = gf_bs_read_int(bs, 1);
5345 		if (sps->vui.timing_info_present_flag) {
5346 			sps->vui.num_units_in_tick = gf_bs_read_int(bs, 32);
5347 			sps->vui.time_scale = gf_bs_read_int(bs, 32);
5348 			sps->vui.fixed_frame_rate_flag = gf_bs_read_int(bs, 1);
5349 		}
5350 
5351 		sps->vui.nal_hrd_parameters_present_flag = gf_bs_read_int(bs, 1);
5352 		if (sps->vui.nal_hrd_parameters_present_flag)
5353 			avc_parse_hrd_parameters(bs, &sps->vui.hrd);
5354 
5355 		sps->vui.vcl_hrd_parameters_present_flag = gf_bs_read_int(bs, 1);
5356 		if (sps->vui.vcl_hrd_parameters_present_flag)
5357 			avc_parse_hrd_parameters(bs, &sps->vui.hrd);
5358 
5359 		if (sps->vui.nal_hrd_parameters_present_flag || sps->vui.vcl_hrd_parameters_present_flag)
5360 			sps->vui.low_delay_hrd_flag = gf_bs_read_int(bs, 1);
5361 
5362 		sps->vui.pic_struct_present_flag = gf_bs_read_int(bs, 1);
5363 	}
5364 	/*end of seq_parameter_set_data*/
5365 
5366 	if (subseq_sps) {
5367 		if ((profile_idc == 83) || (profile_idc == 86)) {
5368 			u8 extended_spatial_scalability_idc;
5369 			/*parsing seq_parameter_set_svc_extension*/
5370 
5371 			/*inter_layer_deblocking_filter_control_present_flag=*/	gf_bs_read_int(bs, 1);
5372 			extended_spatial_scalability_idc = gf_bs_read_int(bs, 2);
5373 			if (sps->ChromaArrayType == 1 || sps->ChromaArrayType == 2) {
5374 				/*chroma_phase_x_plus1_flag*/ gf_bs_read_int(bs, 1);
5375 			}
5376 			if (sps->ChromaArrayType == 1) {
5377 				/*chroma_phase_y_plus1*/ gf_bs_read_int(bs, 2);
5378 			}
5379 			if (extended_spatial_scalability_idc == 1) {
5380 				if (sps->ChromaArrayType > 0) {
5381 					/*seq_ref_layer_chroma_phase_x_plus1_flag*/gf_bs_read_int(bs, 1);
5382 					/*seq_ref_layer_chroma_phase_y_plus1*/gf_bs_read_int(bs, 2);
5383 				}
5384 				/*seq_scaled_ref_layer_left_offset*/ gf_bs_get_se(bs);
5385 				/*seq_scaled_ref_layer_top_offset*/gf_bs_get_se(bs);
5386 				/*seq_scaled_ref_layer_right_offset*/gf_bs_get_se(bs);
5387 				/*seq_scaled_ref_layer_bottom_offset*/gf_bs_get_se(bs);
5388 			}
5389 			if (/*seq_tcoeff_level_prediction_flag*/gf_bs_read_int(bs, 1)) {
5390 				/*adaptive_tcoeff_level_prediction_flag*/ gf_bs_read_int(bs, 1);
5391 			}
5392 			/*slice_header_restriction_flag*/gf_bs_read_int(bs, 1);
5393 
5394 			/*svc_vui_parameters_present*/
5395 			if (gf_bs_read_int(bs, 1)) {
5396 				u32 vui_ext_num_entries_minus1 = gf_bs_get_ue(bs);
5397 
5398 				for (i = 0; i <= vui_ext_num_entries_minus1; i++) {
5399 					u8 vui_ext_nal_hrd_parameters_present_flag, vui_ext_vcl_hrd_parameters_present_flag, vui_ext_timing_info_present_flag;
5400 					/*u8 vui_ext_dependency_id =*/ gf_bs_read_int(bs, 3);
5401 					/*u8 vui_ext_quality_id =*/ gf_bs_read_int(bs, 4);
5402 					/*u8 vui_ext_temporal_id =*/ gf_bs_read_int(bs, 3);
5403 					vui_ext_timing_info_present_flag = gf_bs_read_int(bs, 1);
5404 					if (vui_ext_timing_info_present_flag) {
5405 						/*u32 vui_ext_num_units_in_tick = */gf_bs_read_int(bs, 32);
5406 						/*u32 vui_ext_time_scale = */gf_bs_read_int(bs, 32);
5407 						/*u8 vui_ext_fixed_frame_rate_flag = */gf_bs_read_int(bs, 1);
5408 					}
5409 					vui_ext_nal_hrd_parameters_present_flag = gf_bs_read_int(bs, 1);
5410 					if (vui_ext_nal_hrd_parameters_present_flag) {
5411 						//hrd_parameters( )
5412 					}
5413 					vui_ext_vcl_hrd_parameters_present_flag = gf_bs_read_int(bs, 1);
5414 					if (vui_ext_vcl_hrd_parameters_present_flag) {
5415 						//hrd_parameters( )
5416 					}
5417 					if (vui_ext_nal_hrd_parameters_present_flag || vui_ext_vcl_hrd_parameters_present_flag) {
5418 						/*vui_ext_low_delay_hrd_flag*/gf_bs_read_int(bs, 1);
5419 					}
5420 					/*vui_ext_pic_struct_present_flag*/gf_bs_read_int(bs, 1);
5421 				}
5422 			}
5423 		}
5424 		else if ((profile_idc == 118) || (profile_idc == 128)) {
5425 			GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] MVC not supported - skipping parsing end of Subset SPS\n"));
5426 			return sps_id;
5427 		}
5428 
5429 		if (gf_bs_read_int(bs, 1)) {
5430 			GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[avc-h264] skipping parsing end of Subset SPS (additional_extension2)\n"));
5431 			return sps_id;
5432 		}
5433 	}
5434 	return sps_id;
5435 }
5436 
5437 GF_EXPORT
gf_media_avc_read_sps_bs(GF_BitStream * bs,AVCState * avc,u32 subseq_sps,u32 * vui_flag_pos)5438 s32 gf_media_avc_read_sps_bs(GF_BitStream *bs, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos)
5439 {
5440 	return gf_media_avc_read_sps_bs_internal(bs, avc, subseq_sps, vui_flag_pos, 0);
5441 }
5442 
5443 GF_EXPORT
gf_media_avc_read_sps(const u8 * sps_data,u32 sps_size,AVCState * avc,u32 subseq_sps,u32 * vui_flag_pos)5444 s32 gf_media_avc_read_sps(const u8 *sps_data, u32 sps_size, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos)
5445 {
5446 	s32 sps_id = -1;
5447 	GF_BitStream *bs;
5448 	char *sps_data_without_emulation_bytes = NULL;
5449 	u32 sps_data_without_emulation_bytes_size = 0;
5450 
5451 	if (vui_flag_pos) {
5452 		/*SPS still contains emulation bytes*/
5453 		sps_data_without_emulation_bytes = gf_malloc(sps_size * sizeof(char));
5454 		sps_data_without_emulation_bytes_size = gf_media_nalu_remove_emulation_bytes(sps_data, sps_data_without_emulation_bytes, sps_size);
5455 		bs = gf_bs_new(sps_data_without_emulation_bytes, sps_data_without_emulation_bytes_size, GF_BITSTREAM_READ);
5456 
5457 		*vui_flag_pos = 0;
5458 	}
5459 	else {
5460 		bs = gf_bs_new(sps_data, sps_size, GF_BITSTREAM_READ);
5461 	}
5462 
5463 	if (!bs) {
5464 		sps_id = -1;
5465 		goto exit;
5466 	}
5467 
5468 	sps_id = gf_media_avc_read_sps_bs(bs, avc, subseq_sps, vui_flag_pos);
5469 
5470 exit:
5471 	gf_bs_del(bs);
5472 	if (sps_data_without_emulation_bytes) gf_free(sps_data_without_emulation_bytes);
5473 	return sps_id;
5474 }
5475 
gf_media_avc_read_pps_bs_internal(GF_BitStream * bs,AVCState * avc,u32 nal_hdr)5476 static s32 gf_media_avc_read_pps_bs_internal(GF_BitStream *bs, AVCState *avc, u32 nal_hdr)
5477 {
5478 	s32 pps_id;
5479 	AVC_PPS *pps;
5480 
5481 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
5482 
5483 	if (!nal_hdr) {
5484 		/*nal_hdr = */gf_bs_read_u8(bs);
5485 	}
5486 	pps_id = gf_bs_get_ue(bs);
5487 	if (pps_id >= 255) {
5488 		return -1;
5489 	}
5490 	pps = &avc->pps[pps_id];
5491 	pps->id = pps_id;
5492 
5493 	if (!pps->status) pps->status = 1;
5494 	pps->sps_id = gf_bs_get_ue(bs);
5495 	if (pps->sps_id >= 32) {
5496 		pps->sps_id = 0;
5497 		return -1;
5498 	}
5499 	/*sps_id may be refer to regular SPS or subseq sps, depending on the coded slice refering to the pps*/
5500 	if (!avc->sps[pps->sps_id].state && !avc->sps[pps->sps_id + GF_SVC_SSPS_ID_SHIFT].state) {
5501 		return -1;
5502 	}
5503 	avc->pps_active_idx = pps->id; /*set active sps*/
5504 	avc->sps_active_idx = pps->sps_id; /*set active sps*/
5505 	pps->entropy_coding_mode_flag = gf_bs_read_int(bs, 1);
5506 	pps->pic_order_present = gf_bs_read_int(bs, 1);
5507 	pps->slice_group_count = gf_bs_get_ue(bs) + 1;
5508 	if (pps->slice_group_count > 1) {
5509 		u32 iGroup;
5510 		pps->mb_slice_group_map_type = gf_bs_get_ue(bs);
5511 		if (pps->mb_slice_group_map_type == 0) {
5512 			for (iGroup = 0; iGroup <= pps->slice_group_count - 1; iGroup++)
5513 				/*run_length_minus1[iGroup] = */ gf_bs_get_ue(bs);
5514 		}
5515 		else if (pps->mb_slice_group_map_type == 2) {
5516 			for (iGroup = 0; iGroup < pps->slice_group_count - 1; iGroup++) {
5517 				/*top_left[iGroup] = */ gf_bs_get_ue(bs);
5518 				/*bottom_right[iGroup] = */ gf_bs_get_ue(bs);
5519 			}
5520 		}
5521 		else if (pps->mb_slice_group_map_type == 3 || pps->mb_slice_group_map_type == 4 || pps->mb_slice_group_map_type == 5) {
5522 			/*slice_group_change_direction_flag =*/ gf_bs_read_int(bs, 1);
5523 			/*slice_group_change_rate_minus1 = */ gf_bs_get_ue(bs);
5524 		}
5525 		else if (pps->mb_slice_group_map_type == 6) {
5526 			u32 i;
5527 			pps->pic_size_in_map_units_minus1 = gf_bs_get_ue(bs);
5528 			for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++) {
5529 				/*slice_group_id[i] = */ gf_bs_read_int(bs, (u32)ceil(log(pps->slice_group_count) / log(2)));
5530 			}
5531 		}
5532 	}
5533 	pps->num_ref_idx_l0_default_active_minus1 = gf_bs_get_ue(bs);
5534 	pps->num_ref_idx_l1_default_active_minus1 = gf_bs_get_ue(bs);
5535 
5536 	/*
5537 	if ((pps->ref_count[0] > 32) || (pps->ref_count[1] > 32)) goto exit;
5538 	*/
5539 
5540 	pps->weighted_pred_flag = gf_bs_read_int(bs, 1);
5541 	/*pps->weighted_bipred_idc = */gf_bs_read_int(bs, 2);
5542 	/*pps->init_qp = */gf_bs_get_se(bs) /*+ 26*/;
5543 	/*pps->init_qs= */gf_bs_get_se(bs) /*+ 26*/;
5544 	/*pps->chroma_qp_index_offset = */gf_bs_get_se(bs);
5545 	pps->deblocking_filter_control_present_flag = gf_bs_read_int(bs, 1);
5546 	/*pps->constrained_intra_pred = */gf_bs_read_int(bs, 1);
5547 	pps->redundant_pic_cnt_present = gf_bs_read_int(bs, 1);
5548 
5549 	return pps_id;
5550 }
5551 
5552 GF_EXPORT
gf_media_avc_read_pps_bs(GF_BitStream * bs,AVCState * avc)5553 s32 gf_media_avc_read_pps_bs(GF_BitStream *bs, AVCState *avc)
5554 {
5555 	return gf_media_avc_read_pps_bs_internal(bs, avc, 0);
5556 }
5557 
5558 GF_EXPORT
gf_media_avc_read_pps(const u8 * pps_data,u32 pps_size,AVCState * avc)5559 s32 gf_media_avc_read_pps(const u8 *pps_data, u32 pps_size, AVCState *avc)
5560 {
5561 	GF_BitStream *bs;
5562 	s32 pps_id;
5563 
5564 	/*PPS still contains emulation bytes*/
5565 	bs = gf_bs_new(pps_data, pps_size, GF_BITSTREAM_READ);
5566 	if (!bs) {
5567 		return -1;
5568 	}
5569 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
5570 	pps_id = gf_media_avc_read_pps_bs(bs, avc);
5571 	gf_bs_del(bs);
5572 	return pps_id;
5573 }
5574 
gf_media_avc_read_sps_ext_bs_internal(GF_BitStream * bs,u32 nal_hdr)5575 static s32 gf_media_avc_read_sps_ext_bs_internal(GF_BitStream *bs, u32 nal_hdr)
5576 {
5577 	s32 sps_id;
5578 
5579 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
5580 	if (!nal_hdr) {
5581 		/*nal_hdr = */gf_bs_read_u8(bs);
5582 	}
5583 	sps_id = gf_bs_get_ue(bs);
5584 	return sps_id;
5585 }
5586 
5587 #if 0 //unused
5588 s32 gf_media_avc_read_sps_ext_bs(GF_BitStream *bs)
5589 {
5590 	return gf_media_avc_read_sps_ext_bs_internal(bs, 0);
5591 }
5592 
5593 s32 gf_media_avc_read_sps_ext(const char *spse_data, u32 spse_size)
5594 {
5595 	GF_BitStream *bs;
5596 	s32 sps_id;
5597 
5598 	bs = gf_bs_new(spse_data, spse_size, GF_BITSTREAM_READ);
5599 	sps_id = gf_media_avc_read_sps_ext_bs(bs);
5600 
5601 	gf_bs_del(bs);
5602 	return sps_id;
5603 }
5604 #endif
5605 
SVC_ReadNal_header_extension(GF_BitStream * bs,SVC_NALUHeader * NalHeader)5606 static s32 SVC_ReadNal_header_extension(GF_BitStream *bs, SVC_NALUHeader *NalHeader)
5607 {
5608 	gf_bs_read_int(bs, 1); //reserved_one_bits
5609 	NalHeader->idr_pic_flag = gf_bs_read_int(bs, 1); //idr_flag
5610 	NalHeader->priority_id = gf_bs_read_int(bs, 6); //priority_id
5611 	gf_bs_read_int(bs, 1); //no_inter_layer_pred_flag
5612 	NalHeader->dependency_id = gf_bs_read_int(bs, 3); //DependencyId
5613 	NalHeader->quality_id = gf_bs_read_int(bs, 4); //quality_id
5614 	NalHeader->temporal_id = gf_bs_read_int(bs, 3); //temporal_id
5615 	gf_bs_read_int(bs, 1); //use_ref_base_pic_flag
5616 	gf_bs_read_int(bs, 1); //discardable_flag
5617 	gf_bs_read_int(bs, 1); //output_flag
5618 	gf_bs_read_int(bs, 2); //reserved_three_2bits
5619 	return 1;
5620 }
5621 
ref_pic_list_modification(GF_BitStream * bs,u32 slice_type)5622 static void ref_pic_list_modification(GF_BitStream *bs, u32 slice_type) {
5623 	if (slice_type % 5 != 2 && slice_type % 5 != 4) {
5624 		if (/*ref_pic_list_modification_flag_l0*/ gf_bs_read_int(bs, 1)) {
5625 			u32 modification_of_pic_nums_idc;
5626 			do {
5627 				modification_of_pic_nums_idc = gf_bs_get_ue(bs);
5628 				if (modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1) {
5629 					/*abs_diff_pic_num_minus1 =*/ gf_bs_get_ue(bs);
5630 				}
5631 				else if (modification_of_pic_nums_idc == 2) {
5632 					/*long_term_pic_num =*/ gf_bs_get_ue(bs);
5633 				}
5634 			} while ((modification_of_pic_nums_idc != 3) && gf_bs_available(bs));
5635 		}
5636 	}
5637 	if (slice_type % 5 == 1) {
5638 		if (/*ref_pic_list_modification_flag_l1*/ gf_bs_read_int(bs, 1)) {
5639 			u32 modification_of_pic_nums_idc;
5640 			do {
5641 				modification_of_pic_nums_idc = gf_bs_get_ue(bs);
5642 				if (modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1) {
5643 					/*abs_diff_pic_num_minus1 =*/ gf_bs_get_ue(bs);
5644 				}
5645 				else if (modification_of_pic_nums_idc == 2) {
5646 					/*long_term_pic_num =*/ gf_bs_get_ue(bs);
5647 				}
5648 			} while ((modification_of_pic_nums_idc != 3) && gf_bs_available(bs));
5649 		}
5650 	}
5651 }
5652 
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)5653 static void 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) {
5654 	u32 i, j;
5655 	/*luma_log2_weight_denom =*/ gf_bs_get_ue(bs);
5656 	if (ChromaArrayType != 0) {
5657 		/*chroma_log2_weight_denom =*/ gf_bs_get_ue(bs);
5658 	}
5659 	for (i = 0; i <= num_ref_idx_l0_active_minus1; i++) {
5660 		if (/*luma_weight_l0_flag*/ gf_bs_read_int(bs, 1)) {
5661 			/*luma_weight_l0[i] =*/ gf_bs_get_se(bs);
5662 			/*luma_offset_l0[i] =*/ gf_bs_get_se(bs);
5663 		}
5664 		if (ChromaArrayType != 0) {
5665 			if (/*chroma_weight_l0_flag*/ gf_bs_read_int(bs, 1))
5666 				for (j = 0; j < 2; j++) {
5667 					/*chroma_weight_l0[i][j] =*/ gf_bs_get_se(bs);
5668 					/*chroma_offset_l0[i][j] =*/ gf_bs_get_se(bs);
5669 				}
5670 		}
5671 	}
5672 	if (slice_type % 5 == 1) {
5673 		for (i = 0; i <= num_ref_idx_l1_active_minus1; i++) {
5674 			if (/*luma_weight_l1_flag*/ gf_bs_read_int(bs, 1)) {
5675 				/*luma_weight_l1[i] =*/ gf_bs_get_se(bs);
5676 				/*luma_offset_l1[i] =*/ gf_bs_get_se(bs);
5677 			}
5678 			if (ChromaArrayType != 0) {
5679 				if (/*chroma_weight_l1_flag*/ gf_bs_read_int(bs, 1)) {
5680 					for (j = 0; j < 2; j++) {
5681 						/*chroma_weight_l1[i][j] =*/ gf_bs_get_se(bs);
5682 						/*chroma_offset_l1[i][j] =*/ gf_bs_get_se(bs);
5683 					}
5684 				}
5685 			}
5686 		}
5687 	}
5688 }
5689 
dec_ref_pic_marking(GF_BitStream * bs,Bool IdrPicFlag)5690 static void dec_ref_pic_marking(GF_BitStream *bs, Bool IdrPicFlag) {
5691 	if (IdrPicFlag) {
5692 		/*no_output_of_prior_pics_flag =*/ gf_bs_read_int(bs, 1);
5693 		/*long_term_reference_flag =*/ gf_bs_read_int(bs, 1);
5694 	}
5695 	else {
5696 		if (/*adaptive_ref_pic_marking_mode_flag*/ gf_bs_read_int(bs, 1)) {
5697 			u32 memory_management_control_operation;
5698 			do {
5699 				memory_management_control_operation = gf_bs_get_ue(bs);
5700 				if (memory_management_control_operation == 1 || memory_management_control_operation == 3)
5701 					/*difference_of_pic_nums_minus1 =*/ gf_bs_get_ue(bs);
5702 				if (memory_management_control_operation == 2)
5703 					/*long_term_pic_num =*/ gf_bs_get_ue(bs);
5704 				if (memory_management_control_operation == 3 || memory_management_control_operation == 6)
5705 					/*long_term_frame_idx =*/ gf_bs_get_ue(bs);
5706 				if (memory_management_control_operation == 4)
5707 					/*max_long_term_frame_idx_plus1 =*/ gf_bs_get_ue(bs);
5708 			} while (memory_management_control_operation != 0);
5709 		}
5710 	}
5711 }
5712 
avc_parse_slice(GF_BitStream * bs,AVCState * avc,Bool svc_idr_flag,AVCSliceInfo * si)5713 static s32 avc_parse_slice(GF_BitStream *bs, AVCState *avc, Bool svc_idr_flag, AVCSliceInfo *si)
5714 {
5715 	s32 pps_id, num_ref_idx_l0_active_minus1 = 0, num_ref_idx_l1_active_minus1 = 0;
5716 
5717 	/*s->current_picture.reference= h->nal_ref_idc != 0;*/
5718 	/*first_mb_in_slice = */gf_bs_get_ue(bs);
5719 	si->slice_type = gf_bs_get_ue(bs);
5720 	if (si->slice_type > 9) return -1;
5721 
5722 	pps_id = gf_bs_get_ue(bs);
5723 	if (pps_id > 255) return -1;
5724 	si->pps = &avc->pps[pps_id];
5725 	if (!si->pps->slice_group_count) return -2;
5726 	si->sps = &avc->sps[si->pps->sps_id];
5727 	if (!si->sps->log2_max_frame_num) return -2;
5728 	avc->sps_active_idx = si->pps->sps_id;
5729 	avc->pps_active_idx = pps_id;
5730 
5731 	si->frame_num = gf_bs_read_int(bs, si->sps->log2_max_frame_num);
5732 
5733 	si->field_pic_flag = 0;
5734 	si->bottom_field_flag = 0;
5735 	if (!si->sps->frame_mbs_only_flag) {
5736 		si->field_pic_flag = gf_bs_read_int(bs, 1);
5737 		if (si->field_pic_flag)
5738 			si->bottom_field_flag = gf_bs_read_int(bs, 1);
5739 	}
5740 
5741 	if ((si->nal_unit_type == GF_AVC_NALU_IDR_SLICE) || svc_idr_flag)
5742 		si->idr_pic_id = gf_bs_get_ue(bs);
5743 
5744 	if (si->sps->poc_type == 0) {
5745 		si->poc_lsb = gf_bs_read_int(bs, si->sps->log2_max_poc_lsb);
5746 		if (si->pps->pic_order_present && !si->field_pic_flag) {
5747 			si->delta_poc_bottom = gf_bs_get_se(bs);
5748 		}
5749 	}
5750 	else if ((si->sps->poc_type == 1) && !si->sps->delta_pic_order_always_zero_flag) {
5751 		si->delta_poc[0] = gf_bs_get_se(bs);
5752 		if ((si->pps->pic_order_present == 1) && !si->field_pic_flag)
5753 			si->delta_poc[1] = gf_bs_get_se(bs);
5754 	}
5755 
5756 	if (si->pps->redundant_pic_cnt_present) {
5757 		si->redundant_pic_cnt = gf_bs_get_ue(bs);
5758 	}
5759 
5760 	if (si->slice_type % 5 == GF_AVC_TYPE_B) {
5761 		/*direct_spatial_mv_pred_flag = */gf_bs_read_int(bs, 1);
5762 	}
5763 
5764 	num_ref_idx_l0_active_minus1 = si->pps->num_ref_idx_l0_default_active_minus1;
5765 	num_ref_idx_l1_active_minus1 = si->pps->num_ref_idx_l1_default_active_minus1;
5766 
5767 	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) {
5768 		Bool num_ref_idx_active_override_flag = gf_bs_read_int(bs, 1);
5769 		if (num_ref_idx_active_override_flag) {
5770 			num_ref_idx_l0_active_minus1 = gf_bs_get_ue(bs);
5771 			if (si->slice_type % 5 == GF_AVC_TYPE_B) {
5772 				num_ref_idx_l1_active_minus1 = gf_bs_get_ue(bs);
5773 			}
5774 		}
5775 	}
5776 
5777 	if (si->nal_unit_type == 20 || si->nal_unit_type == 21) {
5778 		//ref_pic_list_mvc_modification(); /* specified in Annex H */
5779 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[avc-h264] unimplemented ref_pic_list_mvc_modification() in slide header\n"));
5780 		assert(0);
5781 		return -1;
5782 	}
5783 	else {
5784 		ref_pic_list_modification(bs, si->slice_type);
5785 	}
5786 
5787 	if ((si->pps->weighted_pred_flag && (si->slice_type % 5 == GF_AVC_TYPE_P || si->slice_type % 5 == GF_AVC_TYPE_SP))
5788 		|| (si->pps->weighted_bipred_idc == 1 && si->slice_type % 5 == GF_AVC_TYPE_B)) {
5789 		pred_weight_table(bs, si->slice_type, si->sps->ChromaArrayType, num_ref_idx_l0_active_minus1, num_ref_idx_l1_active_minus1);
5790 	}
5791 
5792 	if (si->nal_ref_idc != 0) {
5793 		dec_ref_pic_marking(bs, (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE));
5794 	}
5795 
5796 	if (si->pps->entropy_coding_mode_flag && si->slice_type % 5 != GF_AVC_TYPE_I && si->slice_type % 5 != GF_AVC_TYPE_SI) {
5797 		/*cabac_init_idc = */gf_bs_get_ue(bs);
5798 	}
5799 
5800 	/*slice_qp_delta = */gf_bs_get_se(bs);
5801 	if (si->slice_type % 5 == GF_AVC_TYPE_SP || si->slice_type % 5 == GF_AVC_TYPE_SI) {
5802 		if (si->slice_type % 5 == GF_AVC_TYPE_SP) {
5803 			/*sp_for_switch_flag = */gf_bs_read_int(bs, 1);
5804 		}
5805 		/*slice_qs_delta = */gf_bs_get_se(bs);
5806 	}
5807 
5808 	if (si->pps->deblocking_filter_control_present_flag) {
5809 		if (/*disable_deblocking_filter_idc*/ gf_bs_get_ue(bs) != 1) {
5810 			/*slice_alpha_c0_offset_div2 =*/ gf_bs_get_se(bs);
5811 			/*slice_beta_offset_div2 =*/ gf_bs_get_se(bs);
5812 		}
5813 	}
5814 
5815 	if (si->pps->slice_group_count > 1 && si->pps->mb_slice_group_map_type >= 3 && si->pps->mb_slice_group_map_type <= 5) {
5816 		/*slice_group_change_cycle = */gf_bs_read_int(bs, (u32)ceil(log1p((si->pps->pic_size_in_map_units_minus1 + 1) / (si->pps->slice_group_change_rate_minus1 + 1) ) / log(2)));
5817 	}
5818 
5819 	return 0;
5820 }
5821 
5822 
svc_parse_slice(GF_BitStream * bs,AVCState * avc,AVCSliceInfo * si)5823 static s32 svc_parse_slice(GF_BitStream *bs, AVCState *avc, AVCSliceInfo *si)
5824 {
5825 	s32 pps_id;
5826 
5827 	/*s->current_picture.reference= h->nal_ref_idc != 0;*/
5828 	/*first_mb_in_slice = */gf_bs_get_ue(bs);
5829 	si->slice_type = gf_bs_get_ue(bs);
5830 	if (si->slice_type > 9) return -1;
5831 
5832 	pps_id = gf_bs_get_ue(bs);
5833 	if (pps_id > 255)
5834 		return -1;
5835 	si->pps = &avc->pps[pps_id];
5836 	si->pps->id = pps_id;
5837 	if (!si->pps->slice_group_count)
5838 		return -2;
5839 	si->sps = &avc->sps[si->pps->sps_id + GF_SVC_SSPS_ID_SHIFT];
5840 	if (!si->sps->log2_max_frame_num)
5841 		return -2;
5842 
5843 	si->frame_num = gf_bs_read_int(bs, si->sps->log2_max_frame_num);
5844 
5845 	si->field_pic_flag = 0;
5846 	if (si->sps->frame_mbs_only_flag) {
5847 		/*s->picture_structure= PICT_FRAME;*/
5848 	}
5849 	else {
5850 		si->field_pic_flag = gf_bs_read_int(bs, 1);
5851 		if (si->field_pic_flag) si->bottom_field_flag = gf_bs_read_int(bs, 1);
5852 	}
5853 	if (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE || si->NalHeader.idr_pic_flag)
5854 		si->idr_pic_id = gf_bs_get_ue(bs);
5855 
5856 	if (si->sps->poc_type == 0) {
5857 		si->poc_lsb = gf_bs_read_int(bs, si->sps->log2_max_poc_lsb);
5858 		if (si->pps->pic_order_present && !si->field_pic_flag) {
5859 			si->delta_poc_bottom = gf_bs_get_se(bs);
5860 		}
5861 	}
5862 	else if ((si->sps->poc_type == 1) && !si->sps->delta_pic_order_always_zero_flag) {
5863 		si->delta_poc[0] = gf_bs_get_se(bs);
5864 		if ((si->pps->pic_order_present == 1) && !si->field_pic_flag)
5865 			si->delta_poc[1] = gf_bs_get_se(bs);
5866 	}
5867 	if (si->pps->redundant_pic_cnt_present) {
5868 		si->redundant_pic_cnt = gf_bs_get_ue(bs);
5869 	}
5870 	return 0;
5871 }
5872 
5873 
avc_parse_recovery_point_sei(GF_BitStream * bs,AVCState * avc)5874 static s32 avc_parse_recovery_point_sei(GF_BitStream *bs, AVCState *avc)
5875 {
5876 	AVCSeiRecoveryPoint *rp = &avc->sei.recovery_point;
5877 
5878 	rp->frame_cnt = gf_bs_get_ue(bs);
5879 	rp->exact_match_flag = gf_bs_read_int(bs, 1);
5880 	rp->broken_link_flag = gf_bs_read_int(bs, 1);
5881 	rp->changing_slice_group_idc = gf_bs_read_int(bs, 2);
5882 	rp->valid = 1;
5883 
5884 	return 0;
5885 }
5886 
5887 /*for interpretation see ISO 14496-10 N.11084, table D-1*/
avc_parse_pic_timing_sei(GF_BitStream * bs,AVCState * avc)5888 static s32 avc_parse_pic_timing_sei(GF_BitStream *bs, AVCState *avc)
5889 {
5890 	int sps_id = avc->sps_active_idx;
5891 	const char NumClockTS[] = { 1, 1, 1, 2, 2, 3, 3, 2, 3 };
5892 	AVCSeiPicTiming *pt = &avc->sei.pic_timing;
5893 
5894 	if (sps_id < 0) {
5895 		/*sps_active_idx equals -1 when no sps has been detected. In this case SEI should not be decoded.*/
5896 		assert(0);
5897 		return 1;
5898 	}
5899 	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*/
5900 		gf_bs_read_int(bs, 1 + avc->sps[sps_id].vui.hrd.cpb_removal_delay_length_minus1); /*cpb_removal_delay*/
5901 		gf_bs_read_int(bs, 1 + avc->sps[sps_id].vui.hrd.dpb_output_delay_length_minus1);  /*dpb_output_delay*/
5902 	}
5903 
5904 	/*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*/
5905 	if (avc->sps[sps_id].vui.pic_struct_present_flag) {
5906 		int i;
5907 		pt->pic_struct = gf_bs_read_int(bs, 4);
5908 		if (pt->pic_struct > 8) {
5909 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[avc-h264] invalid pic_struct value %d\n", pt->pic_struct));
5910 			return 1;
5911 		}
5912 
5913 		for (i = 0; i < NumClockTS[pt->pic_struct]; i++) {
5914 			if (gf_bs_read_int(bs, 1)) {/*clock_timestamp_flag[i]*/
5915 				Bool full_timestamp_flag;
5916 				gf_bs_read_int(bs, 2);						/*ct_type*/
5917 				gf_bs_read_int(bs, 1);						/*nuit_field_based_flag*/
5918 				gf_bs_read_int(bs, 5);						/*counting_type*/
5919 				full_timestamp_flag = gf_bs_read_int(bs, 1);/*full_timestamp_flag*/
5920 				gf_bs_read_int(bs, 1);						/*discontinuity_flag*/
5921 				gf_bs_read_int(bs, 1);						/*cnt_dropped_flag*/
5922 				gf_bs_read_int(bs, 8);						/*n_frames*/
5923 				if (full_timestamp_flag) {
5924 					gf_bs_read_int(bs, 6);					/*seconds_value*/
5925 					gf_bs_read_int(bs, 6);					/*minutes_value*/
5926 					gf_bs_read_int(bs, 5);					/*hours_value*/
5927 				}
5928 				else {
5929 					if (gf_bs_read_int(bs, 1)) {			/*seconds_flag*/
5930 						gf_bs_read_int(bs, 6);				/*seconds_value*/
5931 						if (gf_bs_read_int(bs, 1)) {		/*minutes_flag*/
5932 							gf_bs_read_int(bs, 6);			/*minutes_value*/
5933 							if (gf_bs_read_int(bs, 1)) {	/*hours_flag*/
5934 								gf_bs_read_int(bs, 5);		/*hours_value*/
5935 							}
5936 						}
5937 					}
5938 					if (avc->sps[sps_id].vui.hrd.time_offset_length > 0)
5939 						gf_bs_read_int(bs, avc->sps[sps_id].vui.hrd.time_offset_length);	/*time_offset*/
5940 				}
5941 			}
5942 		}
5943 	}
5944 
5945 	return 0;
5946 }
5947 
5948 
avc_parse_itu_t_t35_sei(GF_BitStream * bs,AVCSeiItuTT35DolbyVision * dovi)5949 static void avc_parse_itu_t_t35_sei(GF_BitStream* bs, AVCSeiItuTT35DolbyVision *dovi)
5950 {
5951 	u8 itu_t_t35_country_code = gf_bs_read_u8(bs);
5952 	u16 terminal_provider_code = gf_bs_read_u16(bs);
5953 	u32 user_id = gf_bs_read_u32(bs);
5954 	u8 data_type_code = gf_bs_read_u8(bs);
5955 	if (itu_t_t35_country_code == 0xB5 && terminal_provider_code == 0x31 && user_id == 0x47413934 && (data_type_code == 0x8 || data_type_code == 0x9)) {
5956 		dovi->rpu_flag = GF_TRUE;
5957 	}
5958 }
5959 
5960 
avc_compute_poc(AVCSliceInfo * si)5961 static void avc_compute_poc(AVCSliceInfo *si)
5962 {
5963 	enum {
5964 		AVC_PIC_FRAME,
5965 		AVC_PIC_FIELD_TOP,
5966 		AVC_PIC_FIELD_BOTTOM,
5967 	} pic_type;
5968 	s32 field_poc[2] = { 0,0 };
5969 	s32 max_frame_num;
5970 
5971 	if (!si->sps) return;
5972 
5973 	max_frame_num = 1 << (si->sps->log2_max_frame_num);
5974 
5975 	/* picture type */
5976 	if (si->sps->frame_mbs_only_flag || !si->field_pic_flag) pic_type = AVC_PIC_FRAME;
5977 	else if (si->bottom_field_flag) pic_type = AVC_PIC_FIELD_BOTTOM;
5978 	else pic_type = AVC_PIC_FIELD_TOP;
5979 
5980 	/* frame_num_offset */
5981 	if (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE) {
5982 		si->poc_lsb_prev = 0;
5983 		si->poc_msb_prev = 0;
5984 		si->frame_num_offset = 0;
5985 	}
5986 	else {
5987 		if (si->frame_num < si->frame_num_prev)
5988 			si->frame_num_offset = si->frame_num_offset_prev + max_frame_num;
5989 		else
5990 			si->frame_num_offset = si->frame_num_offset_prev;
5991 	}
5992 
5993 	/*ISO 14496-10 N.11084 8.2.1.1*/
5994 	if (si->sps->poc_type == 0)
5995 	{
5996 		const u32 max_poc_lsb = 1 << (si->sps->log2_max_poc_lsb);
5997 
5998 		/*ISO 14496-10 N.11084 eq (8-3)*/
5999 		if ((si->poc_lsb < si->poc_lsb_prev) &&
6000 			(si->poc_lsb_prev - si->poc_lsb >= max_poc_lsb / 2))
6001 			si->poc_msb = si->poc_msb_prev + max_poc_lsb;
6002 		else if ((si->poc_lsb > si->poc_lsb_prev) &&
6003 			(si->poc_lsb - si->poc_lsb_prev > max_poc_lsb / 2))
6004 			si->poc_msb = si->poc_msb_prev - max_poc_lsb;
6005 		else
6006 			si->poc_msb = si->poc_msb_prev;
6007 
6008 		/*ISO 14496-10 N.11084 eq (8-4)*/
6009 		if (pic_type != AVC_PIC_FIELD_BOTTOM)
6010 			field_poc[0] = si->poc_msb + si->poc_lsb;
6011 
6012 		/*ISO 14496-10 N.11084 eq (8-5)*/
6013 		if (pic_type != AVC_PIC_FIELD_TOP) {
6014 			if (!si->field_pic_flag)
6015 				field_poc[1] = field_poc[0] + si->delta_poc_bottom;
6016 			else
6017 				field_poc[1] = si->poc_msb + si->poc_lsb;
6018 		}
6019 	}
6020 	/*ISO 14496-10 N.11084 8.2.1.2*/
6021 	else if (si->sps->poc_type == 1)
6022 	{
6023 		u32 i;
6024 		s32 abs_frame_num, expected_delta_per_poc_cycle, expected_poc;
6025 
6026 		if (si->sps->poc_cycle_length)
6027 			abs_frame_num = si->frame_num_offset + si->frame_num;
6028 		else
6029 			abs_frame_num = 0;
6030 
6031 		if (!si->nal_ref_idc && (abs_frame_num > 0)) abs_frame_num--;
6032 
6033 		expected_delta_per_poc_cycle = 0;
6034 		for (i = 0; i < si->sps->poc_cycle_length; i++)
6035 			expected_delta_per_poc_cycle += si->sps->offset_for_ref_frame[i];
6036 
6037 		if (abs_frame_num > 0) {
6038 			const u32 poc_cycle_cnt = (abs_frame_num - 1) / si->sps->poc_cycle_length;
6039 			const u32 frame_num_in_poc_cycle = (abs_frame_num - 1) % si->sps->poc_cycle_length;
6040 
6041 			expected_poc = poc_cycle_cnt * expected_delta_per_poc_cycle;
6042 			for (i = 0; i <= frame_num_in_poc_cycle; i++)
6043 				expected_poc += si->sps->offset_for_ref_frame[i];
6044 		}
6045 		else {
6046 			expected_poc = 0;
6047 		}
6048 
6049 		if (!si->nal_ref_idc) expected_poc += si->sps->offset_for_non_ref_pic;
6050 
6051 		field_poc[0] = expected_poc + si->delta_poc[0];
6052 		field_poc[1] = field_poc[0] + si->sps->offset_for_top_to_bottom_field;
6053 		if (pic_type == AVC_PIC_FRAME) field_poc[1] += si->delta_poc[1];
6054 	}
6055 	/*ISO 14496-10 N.11084 8.2.1.3*/
6056 	else if (si->sps->poc_type == 2)
6057 	{
6058 		int poc;
6059 		if (si->nal_unit_type == GF_AVC_NALU_IDR_SLICE) {
6060 			poc = 0;
6061 		}
6062 		else {
6063 			const int abs_frame_num = si->frame_num_offset + si->frame_num;
6064 			poc = 2 * abs_frame_num;
6065 			if (!si->nal_ref_idc) poc -= 1;
6066 		}
6067 		field_poc[0] = poc;
6068 		field_poc[1] = poc;
6069 	}
6070 
6071 	/*ISO 14496-10 N.11084 eq (8-1)*/
6072 	if (pic_type == AVC_PIC_FRAME)
6073 		si->poc = MIN(field_poc[0], field_poc[1]);
6074 	else if (pic_type == AVC_PIC_FIELD_TOP)
6075 		si->poc = field_poc[0];
6076 	else
6077 		si->poc = field_poc[1];
6078 }
6079 
6080 GF_EXPORT
gf_media_avc_parse_nalu(GF_BitStream * bs,AVCState * avc)6081 s32 gf_media_avc_parse_nalu(GF_BitStream *bs, AVCState *avc)
6082 {
6083 	u8 idr_flag;
6084 	s32 slice, ret;
6085 	u32 nal_hdr;
6086 	AVCSliceInfo n_state;
6087 
6088 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
6089 
6090 	nal_hdr = gf_bs_read_u8(bs);
6091 
6092 	slice = 0;
6093 	memcpy(&n_state, &avc->s_info, sizeof(AVCSliceInfo));
6094 	avc->last_nal_type_parsed = n_state.nal_unit_type = nal_hdr & 0x1F;
6095 	n_state.nal_ref_idc = (nal_hdr >> 5) & 0x3;
6096 
6097 	idr_flag = 0;
6098 
6099 	switch (n_state.nal_unit_type) {
6100 	case GF_AVC_NALU_ACCESS_UNIT:
6101 	case GF_AVC_NALU_END_OF_SEQ:
6102 	case GF_AVC_NALU_END_OF_STREAM:
6103 		ret = 1;
6104 		break;
6105 
6106 	case GF_AVC_NALU_SVC_SLICE:
6107 		SVC_ReadNal_header_extension(bs, &n_state.NalHeader);
6108 		// slice buffer - read the info and compare.
6109 		/*ret = */svc_parse_slice(bs, avc, &n_state);
6110 		if (avc->s_info.nal_ref_idc) {
6111 			n_state.poc_lsb_prev = avc->s_info.poc_lsb;
6112 			n_state.poc_msb_prev = avc->s_info.poc_msb;
6113 		}
6114 		avc_compute_poc(&n_state);
6115 
6116 		if (avc->s_info.poc != n_state.poc) {
6117 			memcpy(&avc->s_info, &n_state, sizeof(AVCSliceInfo));
6118 			return 1;
6119 		}
6120 		memcpy(&avc->s_info, &n_state, sizeof(AVCSliceInfo));
6121 		return 0;
6122 
6123 	case GF_AVC_NALU_SVC_PREFIX_NALU:
6124 		SVC_ReadNal_header_extension(bs, &n_state.NalHeader);
6125 		return 0;
6126 
6127 	case GF_AVC_NALU_IDR_SLICE:
6128 	case GF_AVC_NALU_NON_IDR_SLICE:
6129 	case GF_AVC_NALU_DP_A_SLICE:
6130 	case GF_AVC_NALU_DP_B_SLICE:
6131 	case GF_AVC_NALU_DP_C_SLICE:
6132 		slice = 1;
6133 		/* slice buffer - read the info and compare.*/
6134 		ret = avc_parse_slice(bs, avc, idr_flag, &n_state);
6135 		if (ret < 0) return ret;
6136 		ret = 0;
6137 		if (
6138 			((avc->s_info.nal_unit_type > GF_AVC_NALU_IDR_SLICE) || (avc->s_info.nal_unit_type < GF_AVC_NALU_NON_IDR_SLICE))
6139 			&& (avc->s_info.nal_unit_type != GF_AVC_NALU_SVC_SLICE)
6140 			) {
6141 			break;
6142 		}
6143 		if (avc->s_info.frame_num != n_state.frame_num) {
6144 			ret = 1;
6145 			break;
6146 		}
6147 
6148 		if (avc->s_info.field_pic_flag != n_state.field_pic_flag) {
6149 			ret = 1;
6150 			break;
6151 		}
6152 		if ((avc->s_info.nal_ref_idc != n_state.nal_ref_idc) &&
6153 			(!avc->s_info.nal_ref_idc || !n_state.nal_ref_idc)) {
6154 			ret = 1;
6155 			break;
6156 		}
6157 		assert(avc->s_info.sps);
6158 
6159 		if (avc->s_info.sps->poc_type == n_state.sps->poc_type) {
6160 			if (!avc->s_info.sps->poc_type) {
6161 				if (!n_state.bottom_field_flag && (avc->s_info.poc_lsb != n_state.poc_lsb)) {
6162 					ret = 1;
6163 					break;
6164 				}
6165 				if (avc->s_info.delta_poc_bottom != n_state.delta_poc_bottom) {
6166 					ret = 1;
6167 					break;
6168 				}
6169 			}
6170 			else if (avc->s_info.sps->poc_type == 1) {
6171 				if (avc->s_info.delta_poc[0] != n_state.delta_poc[0]) {
6172 					ret = 1;
6173 					break;
6174 				}
6175 				if (avc->s_info.delta_poc[1] != n_state.delta_poc[1]) {
6176 					ret = 1;
6177 					break;
6178 				}
6179 			}
6180 		}
6181 
6182 		if (n_state.nal_unit_type == GF_AVC_NALU_IDR_SLICE) {
6183 			if (avc->s_info.nal_unit_type != GF_AVC_NALU_IDR_SLICE) { /*IdrPicFlag differs in value*/
6184 				ret = 1;
6185 				break;
6186 			}
6187 			else if (avc->s_info.idr_pic_id != n_state.idr_pic_id) { /*both IDR and idr_pic_id differs*/
6188 				ret = 1;
6189 				break;
6190 			}
6191 		}
6192 		break;
6193 	case GF_AVC_NALU_SEQ_PARAM:
6194 		avc->last_ps_idx = gf_media_avc_read_sps_bs_internal(bs, avc, 0, NULL, nal_hdr);
6195 		if (avc->last_ps_idx < 0) return -1;
6196 		return 0;
6197 
6198 	case GF_AVC_NALU_PIC_PARAM:
6199 		avc->last_ps_idx = gf_media_avc_read_pps_bs_internal(bs, avc, nal_hdr);
6200 		if (avc->last_ps_idx < 0) return -1;
6201 		return 0;
6202 	case GF_AVC_NALU_SVC_SUBSEQ_PARAM:
6203 		avc->last_ps_idx = gf_media_avc_read_sps_bs_internal(bs, avc, 1, NULL, nal_hdr);
6204 		if (avc->last_ps_idx < 0) return -1;
6205 		return 0;
6206 	case GF_AVC_NALU_SEQ_PARAM_EXT:
6207 		avc->last_ps_idx = gf_media_avc_read_sps_ext_bs_internal(bs, nal_hdr);
6208 		if (avc->last_ps_idx < 0) return -1;
6209 		return 0;
6210 
6211 	case GF_AVC_NALU_SEI:
6212 	case GF_AVC_NALU_FILLER_DATA:
6213 		return 0;
6214 
6215 	default:
6216 		if (avc->s_info.nal_unit_type <= GF_AVC_NALU_IDR_SLICE) ret = 1;
6217 		//To detect change of AU when multiple sps and pps in stream
6218 		else if ((nal_hdr & 0x1F) == GF_AVC_NALU_SEI && avc->s_info.nal_unit_type == GF_AVC_NALU_SVC_SLICE)
6219 			ret = 1;
6220 		else if ((nal_hdr & 0x1F) == GF_AVC_NALU_SEQ_PARAM && avc->s_info.nal_unit_type == GF_AVC_NALU_SVC_SLICE)
6221 			ret = 1;
6222 		else
6223 			ret = 0;
6224 		break;
6225 	}
6226 
6227 	/* save _prev values */
6228 	if (ret && avc->s_info.sps) {
6229 		n_state.frame_num_offset_prev = avc->s_info.frame_num_offset;
6230 		if ((avc->s_info.sps->poc_type != 2) || (avc->s_info.nal_ref_idc != 0))
6231 			n_state.frame_num_prev = avc->s_info.frame_num;
6232 		if (avc->s_info.nal_ref_idc) {
6233 			n_state.poc_lsb_prev = avc->s_info.poc_lsb;
6234 			n_state.poc_msb_prev = avc->s_info.poc_msb;
6235 		}
6236 	}
6237 	if (slice)
6238 		avc_compute_poc(&n_state);
6239 	memcpy(&avc->s_info, &n_state, sizeof(AVCSliceInfo));
6240 	return ret;
6241 }
6242 
6243 
gf_media_avc_reformat_sei(u8 * buffer,u32 nal_size,Bool isobmf_rewrite,AVCState * avc)6244 u32 gf_media_avc_reformat_sei(u8 *buffer, u32 nal_size, Bool isobmf_rewrite, AVCState *avc)
6245 {
6246 	u32 ptype, psize, hdr, var;
6247 	u32 start;
6248 	GF_BitStream *bs;
6249 	GF_BitStream *bs_dest = NULL;
6250 	u8 nhdr;
6251 	Bool sei_removed = GF_FALSE;
6252 	char store;
6253 
6254 	hdr = buffer[0];
6255 	if ((hdr & 0x1F) != GF_AVC_NALU_SEI) return 0;
6256 
6257 	if (isobmf_rewrite) bs_dest = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
6258 
6259 	bs = gf_bs_new(buffer, nal_size, GF_BITSTREAM_READ);
6260 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
6261 
6262 	nhdr = gf_bs_read_int(bs, 8);
6263 	if (bs_dest) gf_bs_write_int(bs_dest, nhdr, 8);
6264 
6265 	/*parse SEI*/
6266 	while (gf_bs_available(bs)) {
6267 		Bool do_copy;
6268 		ptype = 0;
6269 		while (1) {
6270 			u8 v = gf_bs_read_int(bs, 8);
6271 			ptype += v;
6272 			if (v != 0xFF) break;
6273 		}
6274 
6275 		psize = 0;
6276 		while (1) {
6277 			u8 v = gf_bs_read_int(bs, 8);
6278 			psize += v;
6279 			if (v != 0xFF) break;
6280 		}
6281 
6282 		start = (u32)gf_bs_get_position(bs);
6283 
6284 		do_copy = 1;
6285 
6286 		if (start + psize >= nal_size) {
6287 			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));
6288 			if (bs_dest) gf_bs_del(bs_dest);
6289 			return nal_size;
6290 		}
6291 		switch (ptype) {
6292 			/*remove SEI messages forbidden in MP4*/
6293 		case 3: /*filler data*/
6294 		case 10: /*sub_seq info*/
6295 		case 11: /*sub_seq_layer char*/
6296 		case 12: /*sub_seq char*/
6297 			do_copy = 0;
6298 			sei_removed = GF_TRUE;
6299 			break;
6300 		case 5: /*user unregistered */
6301 			store = buffer[start + psize];
6302 			buffer[start + psize] = 0;
6303 			GF_LOG(GF_LOG_DEBUG, GF_LOG_CODING, ("[avc-h264] SEI user message %s\n", buffer + start + 16));
6304 			buffer[start + psize] = store;
6305 			break;
6306 
6307 		case 6: /*recovery point*/
6308 			avc_parse_recovery_point_sei(bs, avc);
6309 			break;
6310 
6311 		case 1: /*pic_timing*/
6312 			avc_parse_pic_timing_sei(bs, avc);
6313 			break;
6314 
6315 		case 0: /*buffering period*/
6316 		case 2: /*pan scan rect*/
6317 		case 4: /*user registered ITU t35*/
6318 		case 7: /*def_rec_pic_marking_repetition*/
6319 		case 8: /*spare_pic*/
6320 		case 9: /*scene info*/
6321 		case 13: /*full frame freeze*/
6322 		case 14: /*full frame freeze release*/
6323 		case 15: /*full frame snapshot*/
6324 		case 16: /*progressive refinement segment start*/
6325 		case 17: /*progressive refinement segment end*/
6326 		case 18: /*motion constrained slice group*/
6327 		default: /*add all unknown SEIs*/
6328 			break;
6329 		}
6330 
6331 		if (do_copy && bs_dest) {
6332 			var = ptype;
6333 			while (var >= 255) {
6334 				gf_bs_write_int(bs_dest, 0xFF, 8);
6335 				var -= 255;
6336 			}
6337 			gf_bs_write_int(bs_dest, var, 8);
6338 
6339 			var = psize;
6340 			while (var >= 255) {
6341 				gf_bs_write_int(bs_dest, 0xFF, 8);
6342 				var -= 255;
6343 			}
6344 			gf_bs_write_int(bs_dest, var, 8);
6345 			gf_bs_seek(bs, start);
6346 
6347 			//bs_read_data does not skip EPB, read byte per byte
6348 			var = psize;
6349 			while (var) {
6350 				gf_bs_write_u8(bs_dest, gf_bs_read_u8(bs));
6351 				var--;
6352 			}
6353 		}
6354 		else {
6355 			gf_bs_seek(bs, start);
6356 
6357 			//bs_skip_bytes does not skip EPB, skip byte per byte
6358 			while (psize) {
6359 				gf_bs_read_u8(bs);
6360 				psize--;
6361 			}
6362 		}
6363 
6364 		if (gf_bs_available(bs) <= 2) {
6365 			var = gf_bs_read_int(bs, 8);
6366 			if (var != 0x80) {
6367 				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\n"));
6368 			}
6369 			if (bs_dest) gf_bs_write_int(bs_dest, 0x80, 8);
6370 			break;
6371 		}
6372 	}
6373 	gf_bs_del(bs);
6374 	//we cannot compare final size and original size since original may have EPB and final does not yet have them
6375 	if (bs_dest && sei_removed) {
6376 		u8 *dst_no_epb = NULL;
6377 		u32 dst_no_epb_size = 0;
6378 		gf_bs_get_content(bs_dest, &dst_no_epb, &dst_no_epb_size);
6379 		nal_size = gf_media_nalu_add_emulation_bytes(buffer, dst_no_epb, dst_no_epb_size);
6380 	}
6381 	if (bs_dest) gf_bs_del(bs_dest);
6382 	return nal_size;
6383 }
6384 
6385 
avc_get_sar_idx(u32 w,u32 h)6386 static u8 avc_get_sar_idx(u32 w, u32 h)
6387 {
6388 	u32 i;
6389 	for (i = 0; i < 14; i++) {
6390 		if ((avc_sar[i].w == w) && (avc_sar[i].h == h)) return i;
6391 	}
6392 	return 0xFF;
6393 }
6394 
gf_media_avc_change_par(GF_AVCConfig * avcc,s32 ar_n,s32 ar_d)6395 GF_Err gf_media_avc_change_par(GF_AVCConfig *avcc, s32 ar_n, s32 ar_d)
6396 {
6397 	GF_BitStream *orig, *mod;
6398 	AVCState avc;
6399 	u32 i, bit_offset, flag;
6400 	s32 idx;
6401 	GF_AVCConfigSlot *slc;
6402 	orig = NULL;
6403 
6404 	memset(&avc, 0, sizeof(AVCState));
6405 	avc.sps_active_idx = -1;
6406 
6407 	i = 0;
6408 	while ((slc = (GF_AVCConfigSlot *)gf_list_enum(avcc->sequenceParameterSets, &i))) {
6409 		u8 *no_emulation_buf = NULL;
6410 		u32 no_emulation_buf_size = 0, emulation_bytes = 0;
6411 		idx = gf_media_avc_read_sps(slc->data, slc->size, &avc, 0, &bit_offset);
6412 		if (idx < 0) {
6413 			if (orig)
6414 				gf_bs_del(orig);
6415 			continue;
6416 		}
6417 
6418 		/*SPS still contains emulation bytes*/
6419 		no_emulation_buf = gf_malloc((slc->size - 1) * sizeof(char));
6420 		no_emulation_buf_size = gf_media_nalu_remove_emulation_bytes(slc->data + 1, no_emulation_buf, slc->size - 1);
6421 
6422 		orig = gf_bs_new(no_emulation_buf, no_emulation_buf_size, GF_BITSTREAM_READ);
6423 		gf_bs_read_data(orig, no_emulation_buf, no_emulation_buf_size);
6424 		gf_bs_seek(orig, 0);
6425 		mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
6426 
6427 		/*copy over till vui flag*/
6428 		assert(bit_offset >= 8);
6429 		while (bit_offset - 8/*bit_offset doesn't take care of the first byte (NALU type)*/) {
6430 			flag = gf_bs_read_int(orig, 1);
6431 			gf_bs_write_int(mod, flag, 1);
6432 			bit_offset--;
6433 		}
6434 		/*check VUI*/
6435 		flag = gf_bs_read_int(orig, 1);
6436 		gf_bs_write_int(mod, 1, 1); /*vui_parameters_present_flag*/
6437 		if (flag) {
6438 			/*aspect_ratio_info_present_flag*/
6439 			if (gf_bs_read_int(orig, 1)) {
6440 				s32 aspect_ratio_idc = gf_bs_read_int(orig, 8);
6441 				if (aspect_ratio_idc == 255) {
6442 					gf_bs_read_int(orig, 16); /*AR num*/
6443 					gf_bs_read_int(orig, 16); /*AR den*/
6444 				}
6445 			}
6446 		}
6447 		if ((ar_d < 0) || (ar_n < 0)) {
6448 			/*no AR signaled*/
6449 			gf_bs_write_int(mod, 0, 1);
6450 		}
6451 		else {
6452 			u32 sarx;
6453 			gf_bs_write_int(mod, 1, 1);
6454 			sarx = avc_get_sar_idx((u32)ar_n, (u32)ar_d);
6455 			gf_bs_write_int(mod, sarx, 8);
6456 			if (sarx == 0xFF) {
6457 				gf_bs_write_int(mod, ar_n, 16);
6458 				gf_bs_write_int(mod, ar_d, 16);
6459 			}
6460 		}
6461 		/*no VUI in input bitstream, set all vui flags to 0*/
6462 		if (!flag) {
6463 			gf_bs_write_int(mod, 0, 1);		/*overscan_info_present_flag */
6464 			gf_bs_write_int(mod, 0, 1);		/*video_signal_type_present_flag */
6465 			gf_bs_write_int(mod, 0, 1);		/*chroma_location_info_present_flag */
6466 			gf_bs_write_int(mod, 0, 1);		/*timing_info_present_flag*/
6467 			gf_bs_write_int(mod, 0, 1);		/*nal_hrd_parameters_present*/
6468 			gf_bs_write_int(mod, 0, 1);		/*vcl_hrd_parameters_present*/
6469 			gf_bs_write_int(mod, 0, 1);		/*pic_struct_present*/
6470 			gf_bs_write_int(mod, 0, 1);		/*bitstream_restriction*/
6471 		}
6472 
6473 		/*finally copy over remaining*/
6474 		while (gf_bs_bits_available(orig)) {
6475 			flag = gf_bs_read_int(orig, 1);
6476 			gf_bs_write_int(mod, flag, 1);
6477 		}
6478 		gf_bs_del(orig);
6479 		orig = NULL;
6480 		gf_free(no_emulation_buf);
6481 
6482 		/*set anti-emulation*/
6483 		gf_bs_get_content(mod, &no_emulation_buf, &flag);
6484 		emulation_bytes = gf_media_nalu_emulation_bytes_add_count(no_emulation_buf, flag);
6485 		if (flag + emulation_bytes + 1 > slc->size)
6486 			slc->data = (char*)gf_realloc(slc->data, flag + emulation_bytes + 1);
6487 		slc->size = gf_media_nalu_add_emulation_bytes(no_emulation_buf, slc->data + 1, flag) + 1;
6488 
6489 		gf_bs_del(mod);
6490 		gf_free(no_emulation_buf);
6491 	}
6492 	return GF_OK;
6493 }
6494 
6495 GF_EXPORT
gf_avc_get_sps_info(u8 * sps_data,u32 sps_size,u32 * sps_id,u32 * width,u32 * height,s32 * par_n,s32 * par_d)6496 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)
6497 {
6498 	AVCState avc;
6499 	s32 idx;
6500 	memset(&avc, 0, sizeof(AVCState));
6501 	avc.sps_active_idx = -1;
6502 
6503 	idx = gf_media_avc_read_sps(sps_data, sps_size, &avc, 0, NULL);
6504 	if (idx < 0) {
6505 		return GF_NON_COMPLIANT_BITSTREAM;
6506 	}
6507 	if (sps_id) *sps_id = idx;
6508 
6509 	if (width) *width = avc.sps[idx].width;
6510 	if (height) *height = avc.sps[idx].height;
6511 	if (par_n) *par_n = avc.sps[idx].vui.par_num ? avc.sps[idx].vui.par_num : (u32)-1;
6512 	if (par_d) *par_d = avc.sps[idx].vui.par_den ? avc.sps[idx].vui.par_den : (u32)-1;
6513 
6514 	return GF_OK;
6515 }
6516 
6517 GF_EXPORT
gf_avc_get_pps_info(u8 * pps_data,u32 pps_size,u32 * pps_id,u32 * sps_id)6518 GF_Err gf_avc_get_pps_info(u8 *pps_data, u32 pps_size, u32 *pps_id, u32 *sps_id)
6519 {
6520 	GF_BitStream *bs;
6521 	GF_Err e = GF_OK;
6522 
6523 	bs = gf_bs_new(pps_data, pps_size, GF_BITSTREAM_READ);
6524 	if (!bs) {
6525 		e = GF_NON_COMPLIANT_BITSTREAM;
6526 		goto exit;
6527 	}
6528 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
6529 	/*nal hdr*/ gf_bs_read_int(bs, 8);
6530 
6531 	*pps_id = gf_bs_get_ue(bs);
6532 	*sps_id = gf_bs_get_ue(bs);
6533 
6534 exit:
6535 	gf_bs_del(bs);
6536 	return e;
6537 }
6538 
6539 #ifndef GPAC_DISABLE_HEVC
6540 
6541 /**********
6542 HEVC parsing
6543 **********/
6544 
gf_media_hevc_slice_is_intra(HEVCState * hevc)6545 Bool gf_media_hevc_slice_is_intra(HEVCState *hevc)
6546 {
6547 	switch (hevc->s_info.nal_unit_type) {
6548 	case GF_HEVC_NALU_SLICE_BLA_W_LP:
6549 	case GF_HEVC_NALU_SLICE_BLA_W_DLP:
6550 	case GF_HEVC_NALU_SLICE_BLA_N_LP:
6551 	case GF_HEVC_NALU_SLICE_IDR_W_DLP:
6552 	case GF_HEVC_NALU_SLICE_IDR_N_LP:
6553 	case GF_HEVC_NALU_SLICE_CRA:
6554 		return GF_TRUE;
6555 	default:
6556 		return GF_FALSE;
6557 	}
6558 }
6559 
gf_media_hevc_slice_is_IDR(HEVCState * hevc)6560 Bool gf_media_hevc_slice_is_IDR(HEVCState *hevc)
6561 {
6562 	if (hevc->sei.recovery_point.valid)
6563 	{
6564 		hevc->sei.recovery_point.valid = 0;
6565 		return GF_TRUE;
6566 	}
6567 	switch (hevc->s_info.nal_unit_type) {
6568 	case GF_HEVC_NALU_SLICE_IDR_W_DLP:
6569 	case GF_HEVC_NALU_SLICE_IDR_N_LP:
6570 		return GF_TRUE;
6571 	default:
6572 		return GF_FALSE;
6573 	}
6574 }
6575 
parse_short_term_ref_pic_set(GF_BitStream * bs,HEVC_SPS * sps,u32 idx_rps)6576 static Bool parse_short_term_ref_pic_set(GF_BitStream *bs, HEVC_SPS *sps, u32 idx_rps)
6577 {
6578 	u32 i;
6579 	Bool inter_ref_pic_set_prediction_flag = 0;
6580 	if (idx_rps != 0)
6581 		inter_ref_pic_set_prediction_flag = gf_bs_read_int(bs, 1);
6582 
6583 	if (inter_ref_pic_set_prediction_flag) {
6584 		HEVC_ReferencePictureSets *ref_ps, *rps;
6585 		u32 delta_idx_minus1 = 0;
6586 		u32 ref_idx;
6587 		u32 delta_rps_sign;
6588 		u32 abs_delta_rps_minus1, nb_ref_pics;
6589 		s32 deltaRPS;
6590 		u32 k = 0, k0 = 0, k1 = 0;
6591 		if (idx_rps == sps->num_short_term_ref_pic_sets)
6592 			delta_idx_minus1 = gf_bs_get_ue(bs);
6593 
6594 		assert(delta_idx_minus1 <= idx_rps - 1);
6595 		ref_idx = idx_rps - 1 - delta_idx_minus1;
6596 		delta_rps_sign = gf_bs_read_int(bs, 1);
6597 		abs_delta_rps_minus1 = gf_bs_get_ue(bs);
6598 		deltaRPS = (1 - (delta_rps_sign << 1)) * (abs_delta_rps_minus1 + 1);
6599 
6600 		rps = &sps->rps[idx_rps];
6601 		ref_ps = &sps->rps[ref_idx];
6602 		nb_ref_pics = ref_ps->num_negative_pics + ref_ps->num_positive_pics;
6603 		for (i = 0; i <= nb_ref_pics; i++) {
6604 			s32 ref_idc;
6605 			s32 used_by_curr_pic_flag = gf_bs_read_int(bs, 1);
6606 			ref_idc = used_by_curr_pic_flag ? 1 : 0;
6607 			if (!used_by_curr_pic_flag) {
6608 				used_by_curr_pic_flag = gf_bs_read_int(bs, 1);
6609 				ref_idc = used_by_curr_pic_flag << 1;
6610 			}
6611 			if ((ref_idc == 1) || (ref_idc == 2)) {
6612 				s32 deltaPOC = deltaRPS;
6613 				if (i < nb_ref_pics)
6614 					deltaPOC += ref_ps->delta_poc[i];
6615 
6616 				rps->delta_poc[k] = deltaPOC;
6617 
6618 				if (deltaPOC < 0)  k0++;
6619 				else k1++;
6620 
6621 				k++;
6622 			}
6623 		}
6624 		rps->num_negative_pics = k0;
6625 		rps->num_positive_pics = k1;
6626 	}
6627 	else {
6628 		s32 prev = 0, poc;
6629 		sps->rps[idx_rps].num_negative_pics = gf_bs_get_ue(bs);
6630 		sps->rps[idx_rps].num_positive_pics = gf_bs_get_ue(bs);
6631 		if (sps->rps[idx_rps].num_negative_pics > 16)
6632 			return GF_FALSE;
6633 		if (sps->rps[idx_rps].num_positive_pics > 16)
6634 			return GF_FALSE;
6635 		for (i = 0; i < sps->rps[idx_rps].num_negative_pics; i++) {
6636 			u32 delta_poc_s0_minus1 = gf_bs_get_ue(bs);
6637 			poc = prev - delta_poc_s0_minus1 - 1;
6638 			prev = poc;
6639 			sps->rps[idx_rps].delta_poc[i] = poc;
6640 			/*used_by_curr_pic_s1_flag[ i ] = */gf_bs_read_int(bs, 1);
6641 		}
6642 		for (i = 0; i < sps->rps[idx_rps].num_positive_pics; i++) {
6643 			u32 delta_poc_s1_minus1 = gf_bs_get_ue(bs);
6644 			poc = prev + delta_poc_s1_minus1 + 1;
6645 			prev = poc;
6646 			sps->rps[idx_rps].delta_poc[i] = poc;
6647 			/*used_by_curr_pic_s1_flag[ i ] = */gf_bs_read_int(bs, 1);
6648 		}
6649 	}
6650 	return GF_TRUE;
6651 }
6652 
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)6653 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)
6654 {
6655 	u32 i, num_ref_idx;
6656 	Bool first_pass = GF_TRUE;
6657 	u8 luma_weights[20], chroma_weights[20];
6658 	u32 ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
6659 
6660 	num_ref_idx = num_ref_idx_l0_active;
6661 
6662 	/*luma_log2_weight_denom=*/gf_bs_get_ue(bs);
6663 	if (ChromaArrayType != 0)
6664 		/*delta_chroma_log2_weight_denom=*/gf_bs_get_se(bs);
6665 
6666 parse_weights:
6667 	for (i = 0; i < num_ref_idx; i++) {
6668 		luma_weights[i] = gf_bs_read_int(bs, 1);
6669 		//infered to be 0 if not present
6670 		chroma_weights[i] = 0;
6671 	}
6672 	if (ChromaArrayType != 0) {
6673 		for (i = 0; i < num_ref_idx; i++) {
6674 			chroma_weights[i] = gf_bs_read_int(bs, 1);
6675 		}
6676 	}
6677 	for (i = 0; i < num_ref_idx; i++) {
6678 		if (luma_weights[i]) {
6679 			/*delta_luma_weight_l0[ i ]=*/gf_bs_get_se(bs);
6680 			/*luma_offset_l0[ i ]=*/gf_bs_get_se(bs);
6681 		}
6682 		if (chroma_weights[i]) {
6683 			/*delta_chroma_weight_l0[ i ][ 0 ]=*/gf_bs_get_se(bs);
6684 			/*delta_chroma_offset_l0[ i ][ 0 ]=*/gf_bs_get_se(bs);
6685 
6686 			/*delta_chroma_weight_l0[ i ][ 1 ]=*/gf_bs_get_se(bs);
6687 			/*delta_chroma_offset_l0[ i ][ 1 ]=*/gf_bs_get_se(bs);
6688 		}
6689 	}
6690 
6691 	if (si->slice_type == GF_HEVC_SLICE_TYPE_B) {
6692 		if (!first_pass) return;
6693 		first_pass = GF_FALSE;
6694 		num_ref_idx = num_ref_idx_l1_active;
6695 		goto parse_weights;
6696 	}
6697 }
6698 
6699 static
ref_pic_lists_modification(GF_BitStream * bs,u32 slice_type,u32 num_ref_idx_l0_active,u32 num_ref_idx_l1_active)6700 Bool ref_pic_lists_modification(GF_BitStream *bs, u32 slice_type, u32 num_ref_idx_l0_active, u32 num_ref_idx_l1_active)
6701 {
6702 	//u32 i;
6703 	Bool ref_pic_list_modification_flag_l0 = gf_bs_read_int(bs, 1);
6704 	if (ref_pic_list_modification_flag_l0) {
6705 		/*for (i=0; i<num_ref_idx_l0_active; i++) {
6706 			list_entry_l0[i] = *//*gf_bs_read_int(bs, (u32)ceil(log(getNumPicTotalCurr())/log(2)));
6707 		}*/
6708 		return GF_FALSE;
6709 	}
6710 	if (slice_type == GF_HEVC_SLICE_TYPE_B) {
6711 		Bool ref_pic_list_modification_flag_l1 = gf_bs_read_int(bs, 1);
6712 		if (ref_pic_list_modification_flag_l1) {
6713 			/*for (i=0; i<num_ref_idx_l1_active; i++) {
6714 				list_entry_l1[i] = *//*gf_bs_read_int(bs, (u32)ceil(log(getNumPicTotalCurr()) / log(2)));
6715 			}*/
6716 			return GF_FALSE;
6717 		}
6718 	}
6719 
6720 	return GF_TRUE;
6721 }
6722 
6723 static
hevc_parse_slice_segment(GF_BitStream * bs,HEVCState * hevc,HEVCSliceInfo * si)6724 s32 hevc_parse_slice_segment(GF_BitStream *bs, HEVCState *hevc, HEVCSliceInfo *si)
6725 {
6726 	u32 i, j;
6727 	u32 num_ref_idx_l0_active = 0, num_ref_idx_l1_active = 0;
6728 	HEVC_PPS *pps;
6729 	HEVC_SPS *sps;
6730 	s32 pps_id;
6731 	Bool RapPicFlag = GF_FALSE;
6732 	Bool IDRPicFlag = GF_FALSE;
6733 
6734 	si->first_slice_segment_in_pic_flag = gf_bs_read_int(bs, 1);
6735 
6736 	switch (si->nal_unit_type) {
6737 	case GF_HEVC_NALU_SLICE_IDR_W_DLP:
6738 	case GF_HEVC_NALU_SLICE_IDR_N_LP:
6739 		IDRPicFlag = GF_TRUE;
6740 		RapPicFlag = GF_TRUE;
6741 		break;
6742 	case GF_HEVC_NALU_SLICE_BLA_W_LP:
6743 	case GF_HEVC_NALU_SLICE_BLA_W_DLP:
6744 	case GF_HEVC_NALU_SLICE_BLA_N_LP:
6745 	case GF_HEVC_NALU_SLICE_CRA:
6746 		RapPicFlag = GF_TRUE;
6747 		break;
6748 	}
6749 
6750 	if (RapPicFlag) {
6751 		/*Bool no_output_of_prior_pics_flag = */gf_bs_read_int(bs, 1);
6752 	}
6753 
6754 	pps_id = gf_bs_get_ue(bs);
6755 	if (pps_id >= 64)
6756 		return -1;
6757 
6758 	pps = &hevc->pps[pps_id];
6759 	sps = &hevc->sps[pps->sps_id];
6760 	si->sps = sps;
6761 	si->pps = pps;
6762 
6763 	if (!si->first_slice_segment_in_pic_flag && pps->dependent_slice_segments_enabled_flag) {
6764 		si->dependent_slice_segment_flag = gf_bs_read_int(bs, 1);
6765 	}
6766 	else {
6767 		si->dependent_slice_segment_flag = GF_FALSE;
6768 	}
6769 
6770 	if (!si->first_slice_segment_in_pic_flag) {
6771 		si->slice_segment_address = gf_bs_read_int(bs, sps->bitsSliceSegmentAddress);
6772 	}
6773 	else {
6774 		si->slice_segment_address = 0;
6775 	}
6776 
6777 	if (!si->dependent_slice_segment_flag) {
6778 		Bool deblocking_filter_override_flag = 0;
6779 		Bool slice_temporal_mvp_enabled_flag = 0;
6780 		Bool slice_sao_luma_flag = 0;
6781 		Bool slice_sao_chroma_flag = 0;
6782 		Bool slice_deblocking_filter_disabled_flag = 0;
6783 
6784 		//"slice_reserved_undetermined_flag[]"
6785 		gf_bs_read_int(bs, pps->num_extra_slice_header_bits);
6786 
6787 		si->slice_type = gf_bs_get_ue(bs);
6788 
6789 		if (pps->output_flag_present_flag)
6790 			/*pic_output_flag = */gf_bs_read_int(bs, 1);
6791 
6792 		if (sps->separate_colour_plane_flag == 1)
6793 			/*colour_plane_id = */gf_bs_read_int(bs, 2);
6794 
6795 		if (IDRPicFlag) {
6796 			si->poc_lsb = 0;
6797 
6798 			//if not asked to parse full header, abort since we know the poc
6799 			if (!hevc->full_slice_header_parse) return 0;
6800 
6801 		}
6802 		else {
6803 			si->poc_lsb = gf_bs_read_int(bs, sps->log2_max_pic_order_cnt_lsb);
6804 
6805 			//if not asked to parse full header, abort once we have the poc
6806 			if (!hevc->full_slice_header_parse) return 0;
6807 
6808 			if (/*short_term_ref_pic_set_sps_flag =*/gf_bs_read_int(bs, 1) == 0) {
6809 				Bool ret = parse_short_term_ref_pic_set(bs, sps, sps->num_short_term_ref_pic_sets);
6810 				if (!ret)
6811 					return -1;
6812 			}
6813 			else if (sps->num_short_term_ref_pic_sets > 1) {
6814 				u32 numbits = 0;
6815 
6816 				while ((u32)(1 << numbits) < sps->num_short_term_ref_pic_sets)
6817 					numbits++;
6818 				if (numbits > 0)
6819 					/*s32 short_term_ref_pic_set_idx = */gf_bs_read_int(bs, numbits);
6820 				/*else
6821 					short_term_ref_pic_set_idx = 0;*/
6822 			}
6823 			if (sps->long_term_ref_pics_present_flag) {
6824 				u8 DeltaPocMsbCycleLt[32];
6825 				u32 num_long_term_sps = 0;
6826 				u32 num_long_term_pics = 0;
6827 
6828 				memset(DeltaPocMsbCycleLt, 0, sizeof(u8) * 32);
6829 
6830 				if (sps->num_long_term_ref_pic_sps > 0) {
6831 					num_long_term_sps = gf_bs_get_ue(bs);
6832 				}
6833 				num_long_term_pics = gf_bs_get_ue(bs);
6834 
6835 				for (i = 0; i < num_long_term_sps + num_long_term_pics; i++) {
6836 					if (i < num_long_term_sps) {
6837 						if (sps->num_long_term_ref_pic_sps > 1)
6838 							/*u8 lt_idx_sps = */gf_bs_read_int(bs, gf_get_bit_size(sps->num_long_term_ref_pic_sps));
6839 					}
6840 					else {
6841 						/*PocLsbLt[ i ] = */ gf_bs_read_int(bs, sps->log2_max_pic_order_cnt_lsb);
6842 						/*UsedByCurrPicLt[ i ] = */ gf_bs_read_int(bs, 1);
6843 					}
6844 					if (/*delta_poc_msb_present_flag[ i ] = */ gf_bs_read_int(bs, 1)) {
6845 						if (i == 0 || i == num_long_term_sps)
6846 							DeltaPocMsbCycleLt[i] = gf_bs_get_ue(bs);
6847 						else
6848 							DeltaPocMsbCycleLt[i] = gf_bs_get_ue(bs) + DeltaPocMsbCycleLt[i - 1];
6849 					}
6850 				}
6851 			}
6852 			if (sps->temporal_mvp_enable_flag)
6853 				slice_temporal_mvp_enabled_flag = gf_bs_read_int(bs, 1);
6854 		}
6855 		if (sps->sample_adaptive_offset_enabled_flag) {
6856 			u32 ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
6857 			slice_sao_luma_flag = gf_bs_read_int(bs, 1);
6858 			if (ChromaArrayType != 0)
6859 				slice_sao_chroma_flag = gf_bs_read_int(bs, 1);
6860 		}
6861 
6862 		if (si->slice_type == GF_HEVC_SLICE_TYPE_P || si->slice_type == GF_HEVC_SLICE_TYPE_B) {
6863 			//u32 NumPocTotalCurr;
6864 			num_ref_idx_l0_active = pps->num_ref_idx_l0_default_active;
6865 			num_ref_idx_l1_active = 0;
6866 			if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
6867 				num_ref_idx_l1_active = pps->num_ref_idx_l1_default_active;
6868 
6869 			if ( /*num_ref_idx_active_override_flag =*/gf_bs_read_int(bs, 1)) {
6870 				num_ref_idx_l0_active = 1 + gf_bs_get_ue(bs);
6871 				if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
6872 					num_ref_idx_l1_active = 1 + gf_bs_get_ue(bs);
6873 			}
6874 
6875 			if (pps->lists_modification_present_flag /*TODO: && NumPicTotalCurr > 1*/) {
6876 				if (!ref_pic_lists_modification(bs, si->slice_type, num_ref_idx_l0_active, num_ref_idx_l1_active)) {
6877 					GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[hevc] ref_pic_lists_modification( ) not implemented\n"));
6878 					return -1;
6879 				}
6880 			}
6881 
6882 			if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
6883 				/*mvd_l1_zero_flag=*/gf_bs_read_int(bs, 1);
6884 			if (pps->cabac_init_present_flag)
6885 				/*cabac_init_flag=*/gf_bs_read_int(bs, 1);
6886 
6887 			if (slice_temporal_mvp_enabled_flag) {
6888 				// When collocated_from_l0_flag is not present, it is inferred to be equal to 1.
6889 				Bool collocated_from_l0_flag = 1;
6890 				if (si->slice_type == GF_HEVC_SLICE_TYPE_B)
6891 					collocated_from_l0_flag = gf_bs_read_int(bs, 1);
6892 
6893 				if ((collocated_from_l0_flag && (num_ref_idx_l0_active > 1))
6894 					|| (!collocated_from_l0_flag && (num_ref_idx_l1_active > 1))
6895 					) {
6896 					/*collocated_ref_idx=*/gf_bs_get_ue(bs);
6897 				}
6898 			}
6899 
6900 			if ((pps->weighted_pred_flag && si->slice_type == GF_HEVC_SLICE_TYPE_P)
6901 				|| (pps->weighted_bipred_flag && si->slice_type == GF_HEVC_SLICE_TYPE_B)
6902 				) {
6903 				hevc_pred_weight_table(bs, hevc, si, pps, sps, num_ref_idx_l0_active, num_ref_idx_l1_active);
6904 			}
6905 			/*five_minus_max_num_merge_cand=*/gf_bs_get_ue(bs);
6906 		}
6907 		si->slice_qp_delta_start_bits = (s32) (gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs);
6908 		/*slice_qp_delta = */si->slice_qp_delta = gf_bs_get_se(bs);
6909 
6910 		if (pps->slice_chroma_qp_offsets_present_flag) {
6911 			/*slice_cb_qp_offset=*/gf_bs_get_se(bs);
6912 			/*slice_cr_qp_offset=*/gf_bs_get_se(bs);
6913 		}
6914 		if (pps->deblocking_filter_override_enabled_flag) {
6915 			deblocking_filter_override_flag = gf_bs_read_int(bs, 1);
6916 		}
6917 
6918 		if (deblocking_filter_override_flag) {
6919 			slice_deblocking_filter_disabled_flag = gf_bs_read_int(bs, 1);
6920 			if (!slice_deblocking_filter_disabled_flag) {
6921 				/*slice_beta_offset_div2=*/ gf_bs_get_se(bs);
6922 				/*slice_tc_offset_div2=*/gf_bs_get_se(bs);
6923 			}
6924 		}
6925 		if (pps->loop_filter_across_slices_enabled_flag
6926 			&& (slice_sao_luma_flag || slice_sao_chroma_flag || !slice_deblocking_filter_disabled_flag)
6927 			) {
6928 			/*slice_loop_filter_across_slices_enabled_flag = */gf_bs_read_int(bs, 1);
6929 		}
6930 	}
6931 	//dependent slice segment
6932 	else {
6933 		//if not asked to parse full header, abort
6934 		if (!hevc->full_slice_header_parse) return 0;
6935 	}
6936 
6937 	si->entry_point_start_bits = ((u32)gf_bs_get_position(bs) - 1) * 8 + gf_bs_get_bit_position(bs);
6938 
6939 	if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
6940 		u32 num_entry_point_offsets = gf_bs_get_ue(bs);
6941 		if (num_entry_point_offsets > 0) {
6942 			u32 offset = gf_bs_get_ue(bs) + 1;
6943 			u32 segments = offset >> 4;
6944 			s32 remain = (offset & 15);
6945 
6946 			for (i = 0; i < num_entry_point_offsets; i++) {
6947 				//u32 res = 0;
6948 				for (j = 0; j < segments; j++) {
6949 					//res <<= 16;
6950 					/*res +=*/ gf_bs_read_int(bs, 16);
6951 				}
6952 				if (remain) {
6953 					//res <<= remain;
6954 					/* res += */ gf_bs_read_int(bs, remain);
6955 				}
6956 				// entry_point_offset = val + 1; // +1; // +1 to get the size
6957 			}
6958 		}
6959 	}
6960 
6961 	if (pps->slice_segment_header_extension_present_flag) {
6962 		u32 size_ext = gf_bs_get_ue(bs);
6963 		while (size_ext) {
6964 			gf_bs_read_int(bs, 8);
6965 			size_ext--;
6966 		}
6967 	}
6968 
6969 	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
6970 
6971 	if (gf_bs_read_int(bs, 1) == 0) {
6972 		GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("Error parsing slice header: byte_align not found at end of header !\n"));
6973 	}
6974 
6975 	gf_bs_align(bs);
6976 	si->payload_start_offset = (s32)gf_bs_get_position(bs);
6977 	return 0;
6978 }
6979 
gf_media_hevc_parse_sei(char * buffer,u32 nal_size,HEVCState * hevc)6980 void gf_media_hevc_parse_sei(char *buffer, u32 nal_size, HEVCState *hevc)
6981 {
6982 	u32 ptype, psize, hdr;
6983 	u64 start;
6984 	GF_BitStream *bs;
6985 
6986 	hdr = buffer[0];
6987 	if (((hdr & 0x7e) >> 1) != GF_HEVC_NALU_SEI_PREFIX) return;
6988 
6989 	bs = gf_bs_new(buffer, nal_size, GF_BITSTREAM_READ);
6990 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
6991 
6992 	gf_bs_read_int(bs, 16);
6993 
6994 	/*parse SEI*/
6995 	while (gf_bs_available(bs)) {
6996 		ptype = 0;
6997 		while (gf_bs_peek_bits(bs, 8, 0)==0xFF) {
6998 			gf_bs_read_int(bs, 8);
6999 			ptype += 255;
7000 		}
7001 		ptype += gf_bs_read_int(bs, 8);
7002 		psize = 0;
7003 		while (gf_bs_peek_bits(bs, 8, 0)==0xFF) {
7004 			gf_bs_read_int(bs, 8);
7005 			psize += 255;
7006 		}
7007 		psize += gf_bs_read_int(bs, 8);
7008 
7009 		start = gf_bs_get_position(bs);
7010 		if (start+psize >= nal_size) {
7011 			GF_LOG(GF_LOG_WARNING, GF_LOG_CODING, ("[hevc-h265] SEI user message type %d size error (%d but %d remain), skipping SEI message\n", ptype, psize, nal_size-start));
7012 			break;
7013 		}
7014 
7015 		switch (ptype) {
7016 		case 4: /*user registered ITU-T T35*/
7017 		{
7018 			GF_BitStream * itu_t_t35_bs = gf_bs_new(buffer + start, psize, GF_BITSTREAM_READ);
7019 			avc_parse_itu_t_t35_sei(itu_t_t35_bs, &hevc->sei.dovi);
7020 			gf_bs_del(itu_t_t35_bs);
7021 		}
7022 		default: break;
7023 		}
7024 
7025 		gf_bs_skip_bytes(bs, (u64) psize);
7026 		gf_bs_align(bs);
7027 		if (gf_bs_available(bs) <= 2)
7028 			break;
7029 	}
7030 	gf_bs_del(bs);
7031 }
7032 
hevc_compute_poc(HEVCSliceInfo * si)7033 static void hevc_compute_poc(HEVCSliceInfo *si)
7034 {
7035 	u32 max_poc_lsb = 1 << (si->sps->log2_max_pic_order_cnt_lsb);
7036 
7037 	/*POC reset for IDR frames, NOT for CRA*/
7038 	switch (si->nal_unit_type) {
7039 	case GF_HEVC_NALU_SLICE_IDR_W_DLP:
7040 	case GF_HEVC_NALU_SLICE_IDR_N_LP:
7041 		si->poc_lsb_prev = 0;
7042 		si->poc_msb_prev = 0;
7043 		break;
7044 	}
7045 
7046 	if ((si->poc_lsb < si->poc_lsb_prev) && (si->poc_lsb_prev - si->poc_lsb >= max_poc_lsb / 2))
7047 		si->poc_msb = si->poc_msb_prev + max_poc_lsb;
7048 	else if ((si->poc_lsb > si->poc_lsb_prev) && (si->poc_lsb - si->poc_lsb_prev > max_poc_lsb / 2))
7049 		si->poc_msb = si->poc_msb_prev - max_poc_lsb;
7050 	else
7051 		si->poc_msb = si->poc_msb_prev;
7052 
7053 	switch (si->nal_unit_type) {
7054 	case GF_HEVC_NALU_SLICE_BLA_W_LP:
7055 	case GF_HEVC_NALU_SLICE_BLA_W_DLP:
7056 	case GF_HEVC_NALU_SLICE_BLA_N_LP:
7057 		si->poc_msb = 0;
7058 		break;
7059 	}
7060 	si->poc = si->poc_msb + si->poc_lsb;
7061 }
7062 
7063 
hevc_parse_nal_header(GF_BitStream * bs,u8 * nal_unit_type,u8 * temporal_id,u8 * layer_id)7064 static Bool hevc_parse_nal_header(GF_BitStream *bs, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id)
7065 {
7066 	u32 val;
7067 	val = gf_bs_read_int(bs, 1);
7068 	if (val) return GF_FALSE;
7069 
7070 	val = gf_bs_read_int(bs, 6);
7071 	if (nal_unit_type) *nal_unit_type = val;
7072 
7073 	val = gf_bs_read_int(bs, 6);
7074 	if (layer_id) *layer_id = val;
7075 
7076 	val = gf_bs_read_int(bs, 3);
7077 	if (!val)
7078 		return GF_FALSE;
7079 	val -= 1;
7080 	if (temporal_id) *temporal_id = val;
7081 	return GF_TRUE;
7082 }
7083 
7084 
profile_tier_level(GF_BitStream * bs,Bool ProfilePresentFlag,u8 MaxNumSubLayersMinus1,HEVC_ProfileTierLevel * ptl)7085 void profile_tier_level(GF_BitStream *bs, Bool ProfilePresentFlag, u8 MaxNumSubLayersMinus1, HEVC_ProfileTierLevel *ptl)
7086 {
7087 	u32 i;
7088 	if (ProfilePresentFlag) {
7089 		ptl->profile_space = gf_bs_read_int(bs, 2);
7090 		ptl->tier_flag = gf_bs_read_int(bs, 1);
7091 		ptl->profile_idc = gf_bs_read_int(bs, 5);
7092 
7093 		ptl->profile_compatibility_flag = gf_bs_read_int(bs, 32);
7094 
7095 		ptl->general_progressive_source_flag = gf_bs_read_int(bs, 1);
7096 		ptl->general_interlaced_source_flag = gf_bs_read_int(bs, 1);
7097 		ptl->general_non_packed_constraint_flag = gf_bs_read_int(bs, 1);
7098 		ptl->general_frame_only_constraint_flag = gf_bs_read_int(bs, 1);
7099 		ptl->general_reserved_44bits = gf_bs_read_long_int(bs, 44);
7100 	}
7101 	ptl->level_idc = gf_bs_read_int(bs, 8);
7102 	for (i = 0; i < MaxNumSubLayersMinus1; i++) {
7103 		ptl->sub_ptl[i].profile_present_flag = gf_bs_read_int(bs, 1);
7104 		ptl->sub_ptl[i].level_present_flag = gf_bs_read_int(bs, 1);
7105 	}
7106 	if (MaxNumSubLayersMinus1 > 0) {
7107 		for (i = MaxNumSubLayersMinus1; i < 8; i++) {
7108 			/*reserved_zero_2bits*/gf_bs_read_int(bs, 2);
7109 		}
7110 	}
7111 
7112 	for (i = 0; i < MaxNumSubLayersMinus1; i++) {
7113 		if (ptl->sub_ptl[i].profile_present_flag) {
7114 			ptl->sub_ptl[i].profile_space = gf_bs_read_int(bs, 2);
7115 			ptl->sub_ptl[i].tier_flag = gf_bs_read_int(bs, 1);
7116 			ptl->sub_ptl[i].profile_idc = gf_bs_read_int(bs, 5);
7117 			ptl->sub_ptl[i].profile_compatibility_flag = gf_bs_read_int(bs, 32);
7118 			/*ptl->sub_ptl[i].progressive_source_flag =*/ gf_bs_read_int(bs, 1);
7119 			/*ptl->sub_ptl[i].interlaced_source_flag =*/ gf_bs_read_int(bs, 1);
7120 			/*ptl->sub_ptl[i].non_packed_constraint_flag =*/ gf_bs_read_int(bs, 1);
7121 			/*ptl->sub_ptl[i].frame_only_constraint_flag =*/ gf_bs_read_int(bs, 1);
7122 			/*ptl->sub_ptl[i].reserved_44bits =*/ gf_bs_read_long_int(bs, 44);
7123 		}
7124 		if (ptl->sub_ptl[i].level_present_flag)
7125 			ptl->sub_ptl[i].level_idc = gf_bs_read_int(bs, 8);
7126 	}
7127 }
7128 
scalability_type_to_idx(HEVC_VPS * vps,u32 scalability_type)7129 static u32 scalability_type_to_idx(HEVC_VPS *vps, u32 scalability_type)
7130 {
7131 	u32 idx = 0, type;
7132 	for (type = 0; type < scalability_type; type++) {
7133 		idx += (vps->scalability_mask[type] ? 1 : 0);
7134 	}
7135 	return idx;
7136 }
7137 
7138 #define LHVC_VIEW_ORDER_INDEX  1
7139 #define LHVC_SCALABILITY_INDEX	2
7140 
lhvc_get_scalability_id(HEVC_VPS * vps,u32 layer_id_in_vps,u32 scalability_type)7141 static u32 lhvc_get_scalability_id(HEVC_VPS *vps, u32 layer_id_in_vps, u32 scalability_type)
7142 {
7143 	u32 idx;
7144 	if (!vps->scalability_mask[scalability_type]) return 0;
7145 	idx = scalability_type_to_idx(vps, scalability_type);
7146 	return vps->dimension_id[layer_id_in_vps][idx];
7147 }
7148 
lhvc_get_view_index(HEVC_VPS * vps,u32 id)7149 static u32 lhvc_get_view_index(HEVC_VPS *vps, u32 id)
7150 {
7151 	return lhvc_get_scalability_id(vps, vps->layer_id_in_vps[id], LHVC_VIEW_ORDER_INDEX);
7152 }
7153 
lhvc_get_num_views(HEVC_VPS * vps)7154 static u32 lhvc_get_num_views(HEVC_VPS *vps)
7155 {
7156 	u32 numViews = 1, i;
7157 	for (i = 0; i < vps->max_layers; i++) {
7158 		u32 layer_id = vps->layer_id_in_nuh[i];
7159 		if (i > 0 && (lhvc_get_view_index(vps, layer_id) != lhvc_get_scalability_id(vps, i - 1, LHVC_VIEW_ORDER_INDEX))) {
7160 			numViews++;
7161 		}
7162 	}
7163 	return numViews;
7164 }
7165 
lhvc_parse_rep_format(HEVC_RepFormat * fmt,GF_BitStream * bs)7166 static void lhvc_parse_rep_format(HEVC_RepFormat *fmt, GF_BitStream *bs)
7167 {
7168 	u8 chroma_bitdepth_present_flag;
7169 	fmt->pic_width_luma_samples = gf_bs_read_int(bs, 16);
7170 	fmt->pic_height_luma_samples = gf_bs_read_int(bs, 16);
7171 	chroma_bitdepth_present_flag = gf_bs_read_int(bs, 1);
7172 	if (chroma_bitdepth_present_flag) {
7173 		fmt->chroma_format_idc = gf_bs_read_int(bs, 2);
7174 
7175 		if (fmt->chroma_format_idc == 3)
7176 			fmt->separate_colour_plane_flag = gf_bs_read_int(bs, 1);
7177 		fmt->bit_depth_luma = 8 + gf_bs_read_int(bs, 4);
7178 		fmt->bit_depth_chroma = 8 + gf_bs_read_int(bs, 4);
7179 	}
7180 	if (/*conformance_window_vps_flag*/ gf_bs_read_int(bs, 1)) {
7181 		/*conf_win_vps_left_offset*/gf_bs_get_ue(bs);
7182 		/*conf_win_vps_right_offset*/gf_bs_get_ue(bs);
7183 		/*conf_win_vps_top_offset*/gf_bs_get_ue(bs);
7184 		/*conf_win_vps_bottom_offset*/gf_bs_get_ue(bs);
7185 	}
7186 }
7187 
7188 
hevc_parse_vps_extension(HEVC_VPS * vps,GF_BitStream * bs)7189 static Bool hevc_parse_vps_extension(HEVC_VPS *vps, GF_BitStream *bs)
7190 {
7191 	u8 splitting_flag, vps_nuh_layer_id_present_flag, view_id_len;
7192 	u32 i, j, num_scalability_types, num_add_olss, num_add_layer_set, num_indepentdent_layers, nb_bits, default_output_layer_idc = 0;
7193 	u8 dimension_id_len[16], dim_bit_offset[16];
7194 	u8 /*avc_base_layer_flag, */NumLayerSets, /*default_one_target_output_layer_flag, */rep_format_idx_present_flag, ols_ids_to_ls_idx;
7195 	u8 layer_set_idx_for_ols_minus1[MAX_LHVC_LAYERS];
7196 	u8 nb_output_layers_in_output_layer_set[MAX_LHVC_LAYERS + 1];
7197 	u8 ols_highest_output_layer_id[MAX_LHVC_LAYERS + 1];
7198 
7199 	u32 k, d, r, p, iNuhLId, jNuhLId;
7200 	u8 num_direct_ref_layers[64], num_pred_layers[64], num_layers_in_tree_partition[MAX_LHVC_LAYERS];
7201 	u8 dependency_flag[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS], id_pred_layers[64][MAX_LHVC_LAYERS];
7202 	//	u8 num_ref_layers[64];
7203 	//	u8 tree_partition_layer_id[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS];
7204 	//	u8 id_ref_layers[64][MAX_LHVC_LAYERS];
7205 	//	u8 id_direct_ref_layers[64][MAX_LHVC_LAYERS];
7206 	u8 layer_id_in_list_flag[64];
7207 	Bool OutputLayerFlag[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS];
7208 
7209 	vps->vps_extension_found = 1;
7210 	if ((vps->max_layers > 1) && vps->base_layer_internal_flag)
7211 		profile_tier_level(bs, 0, vps->max_sub_layers - 1, &vps->ext_ptl[0]);
7212 
7213 	splitting_flag = gf_bs_read_int(bs, 1);
7214 	num_scalability_types = 0;
7215 	for (i = 0; i < 16; i++) {
7216 		vps->scalability_mask[i] = gf_bs_read_int(bs, 1);
7217 		num_scalability_types += vps->scalability_mask[i];
7218 	}
7219 	if (num_scalability_types >= 16) {
7220 		num_scalability_types = 16;
7221 	}
7222 	dimension_id_len[0] = 0;
7223 	for (i = 0; i < (num_scalability_types - splitting_flag); i++) {
7224 		dimension_id_len[i] = 1 + gf_bs_read_int(bs, 3);
7225 	}
7226 
7227 	if (splitting_flag) {
7228 		for (i = 0; i < num_scalability_types; i++) {
7229 			dim_bit_offset[i] = 0;
7230 			for (j = 0; j < i; j++)
7231 				dim_bit_offset[i] += dimension_id_len[j];
7232 		}
7233 		dimension_id_len[num_scalability_types - 1] = 1 + (5 - dim_bit_offset[num_scalability_types - 1]);
7234 		dim_bit_offset[num_scalability_types] = 6;
7235 	}
7236 
7237 	vps_nuh_layer_id_present_flag = gf_bs_read_int(bs, 1);
7238 	vps->layer_id_in_nuh[0] = 0;
7239 	vps->layer_id_in_vps[0] = 0;
7240 	for (i = 1; i < vps->max_layers; i++) {
7241 		if (vps_nuh_layer_id_present_flag) {
7242 			vps->layer_id_in_nuh[i] = gf_bs_read_int(bs, 6);
7243 		}
7244 		else {
7245 			vps->layer_id_in_nuh[i] = i;
7246 		}
7247 		vps->layer_id_in_vps[vps->layer_id_in_nuh[i]] = i;
7248 
7249 		if (!splitting_flag) {
7250 			for (j = 0; j < num_scalability_types; j++) {
7251 				vps->dimension_id[i][j] = gf_bs_read_int(bs, dimension_id_len[j]);
7252 			}
7253 		}
7254 	}
7255 
7256 	if (splitting_flag) {
7257 		for (i = 0; i < vps->max_layers; i++)
7258 			for (j = 0; j < num_scalability_types; j++)
7259 				vps->dimension_id[i][j] = ((vps->layer_id_in_nuh[i] & ((1 << dim_bit_offset[j + 1]) - 1)) >> dim_bit_offset[j]);
7260 	}
7261 	else {
7262 		for (j = 0; j < num_scalability_types; j++)
7263 			vps->dimension_id[0][j] = 0;
7264 	}
7265 
7266 	view_id_len = gf_bs_read_int(bs, 4);
7267 	if (view_id_len > 0) {
7268 		for (i = 0; i < lhvc_get_num_views(vps); i++) {
7269 			/*m_viewIdVal[i] = */ gf_bs_read_int(bs, view_id_len);
7270 		}
7271 	}
7272 
7273 	for (i = 1; i < vps->max_layers; i++) {
7274 		for (j = 0; j < i; j++) {
7275 			vps->direct_dependency_flag[i][j] = gf_bs_read_int(bs, 1);
7276 		}
7277 	}
7278 
7279 	//we do the test on MAX_LHVC_LAYERS and break in the loop to avoid a wrong GCC 4.8 warning on array bounds
7280 	for (i = 0; i < MAX_LHVC_LAYERS; i++) {
7281 		if (i >= vps->max_layers) break;
7282 		for (j = 0; j < vps->max_layers; j++) {
7283 			dependency_flag[i][j] = vps->direct_dependency_flag[i][j];
7284 			for (k = 0; k < i; k++)
7285 				if (vps->direct_dependency_flag[i][k] && vps->direct_dependency_flag[k][j])
7286 					dependency_flag[i][j] = 1;
7287 		}
7288 	}
7289 
7290 	for (i = 0; i < vps->max_layers; i++) {
7291 		iNuhLId = vps->layer_id_in_nuh[i];
7292 		d = r = p = 0;
7293 		for (j = 0; j < vps->max_layers; j++) {
7294 			jNuhLId = vps->layer_id_in_nuh[j];
7295 			if (vps->direct_dependency_flag[i][j]) {
7296 				//				id_direct_ref_layers[iNuhLId][d] = jNuhLId;
7297 				d++;
7298 			}
7299 			if (dependency_flag[i][j]) {
7300 				//				id_ref_layers[iNuhLId][r] = jNuhLId;
7301 				r++;
7302 			}
7303 
7304 			if (dependency_flag[j][i])
7305 				id_pred_layers[iNuhLId][p++] = jNuhLId;
7306 		}
7307 		num_direct_ref_layers[iNuhLId] = d;
7308 		//		num_ref_layers[iNuhLId] = r;
7309 		num_pred_layers[iNuhLId] = p;
7310 	}
7311 
7312 	memset(layer_id_in_list_flag, 0, 64 * sizeof(u8));
7313 	k = 0; //num_indepentdent_layers
7314 	for (i = 0; i < vps->max_layers; i++) {
7315 		iNuhLId = vps->layer_id_in_nuh[i];
7316 		if (!num_direct_ref_layers[iNuhLId]) {
7317 			u32 h = 1;
7318 			//tree_partition_layer_id[k][0] = iNuhLId;
7319 			for (j = 0; j < num_pred_layers[iNuhLId]; j++) {
7320 				u32 predLId = id_pred_layers[iNuhLId][j];
7321 				if (!layer_id_in_list_flag[predLId]) {
7322 					//tree_partition_layer_id[k][h++] = predLId;
7323 					layer_id_in_list_flag[predLId] = 1;
7324 				}
7325 			}
7326 			num_layers_in_tree_partition[k++] = h;
7327 		}
7328 	}
7329 	num_indepentdent_layers = k;
7330 
7331 	num_add_layer_set = 0;
7332 	if (num_indepentdent_layers > 1)
7333 		num_add_layer_set = gf_bs_get_ue(bs);
7334 
7335 	for (i = 0; i < num_add_layer_set; i++)
7336 		for (j = 1; j < num_indepentdent_layers; j++) {
7337 			nb_bits = 1;
7338 			while ((1 << nb_bits) < (num_layers_in_tree_partition[j] + 1))
7339 				nb_bits++;
7340 			/*highest_layer_idx_plus1[i][j]*/gf_bs_read_int(bs, nb_bits);
7341 		}
7342 
7343 
7344 	if (/*vps_sub_layers_max_minus1_present_flag*/gf_bs_read_int(bs, 1)) {
7345 		for (i = 0; i < vps->max_layers; i++) {
7346 			/*sub_layers_vps_max_minus1[ i ]*/gf_bs_read_int(bs, 3);
7347 		}
7348 	}
7349 
7350 	if (/*max_tid_ref_present_flag = */gf_bs_read_int(bs, 1)) {
7351 		for (i = 0; i < (vps->max_layers - 1); i++) {
7352 			for (j = i + 1; j < vps->max_layers; j++) {
7353 				if (vps->direct_dependency_flag[j][i])
7354 					/*max_tid_il_ref_pics_plus1[ i ][ j ]*/gf_bs_read_int(bs, 3);
7355 			}
7356 		}
7357 	}
7358 	/*default_ref_layers_active_flag*/gf_bs_read_int(bs, 1);
7359 
7360 	vps->num_profile_tier_level = 1 + gf_bs_get_ue(bs);
7361 	if (vps->num_profile_tier_level > MAX_LHVC_LAYERS) {
7362 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Wrong number of PTLs in VPS %d\n", vps->num_profile_tier_level));
7363 		vps->num_profile_tier_level = 1;
7364 		return GF_FALSE;
7365 	}
7366 
7367 	for (i = vps->base_layer_internal_flag ? 2 : 1; i < vps->num_profile_tier_level; i++) {
7368 		Bool vps_profile_present_flag = gf_bs_read_int(bs, 1);
7369 		profile_tier_level(bs, vps_profile_present_flag, vps->max_sub_layers - 1, &vps->ext_ptl[i - 1]);
7370 	}
7371 
7372 	NumLayerSets = vps->num_layer_sets + num_add_layer_set;
7373 	num_add_olss = 0;
7374 
7375 	if (NumLayerSets > 1) {
7376 		num_add_olss = gf_bs_get_ue(bs);
7377 		default_output_layer_idc = gf_bs_read_int(bs, 2);
7378 		default_output_layer_idc = default_output_layer_idc < 2 ? default_output_layer_idc : 2;
7379 	}
7380 	vps->num_output_layer_sets = num_add_olss + NumLayerSets;
7381 
7382 
7383 	layer_set_idx_for_ols_minus1[0] = 1;
7384 	vps->output_layer_flag[0][0] = 1;
7385 
7386 	for (i = 0; i < vps->num_output_layer_sets; i++) {
7387 		if ((NumLayerSets > 2) && (i >= NumLayerSets)) {
7388 			nb_bits = 1;
7389 			while ((1 << nb_bits) < (NumLayerSets - 1))
7390 				nb_bits++;
7391 			layer_set_idx_for_ols_minus1[i] = gf_bs_read_int(bs, nb_bits);
7392 		}
7393 		else
7394 			layer_set_idx_for_ols_minus1[i] = 0;
7395 		ols_ids_to_ls_idx = i < NumLayerSets ? i : layer_set_idx_for_ols_minus1[i] + 1;
7396 
7397 		if ((i > (vps->num_layer_sets - 1)) || (default_output_layer_idc == 2)) {
7398 			for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++)
7399 				vps->output_layer_flag[i][j] = gf_bs_read_int(bs, 1);
7400 		}
7401 
7402 		if ((default_output_layer_idc == 0) || (default_output_layer_idc == 1)) {
7403 			for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) {
7404 				if ((default_output_layer_idc == 0) || (vps->LayerSetLayerIdList[i][j] == vps->LayerSetLayerIdListMax[i]))
7405 					OutputLayerFlag[i][j] = GF_TRUE;
7406 				else
7407 					OutputLayerFlag[i][j] = GF_FALSE;
7408 			}
7409 		}
7410 
7411 		for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) {
7412 			if (OutputLayerFlag[i][j]) {
7413 				u32 curLayerID;
7414 				vps->necessary_layers_flag[i][j] = GF_TRUE;
7415 				curLayerID = vps->LayerSetLayerIdList[i][j];
7416 				for (k = 0; k < j; k++) {
7417 					u32 refLayerId = vps->LayerSetLayerIdList[i][k];
7418 					if (dependency_flag[vps->layer_id_in_vps[curLayerID]][vps->layer_id_in_vps[refLayerId]])
7419 						vps->necessary_layers_flag[i][k] = GF_TRUE;
7420 				}
7421 			}
7422 		}
7423 		vps->num_necessary_layers[i] = 0;
7424 		for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) {
7425 			if (vps->necessary_layers_flag[i][j])
7426 				vps->num_necessary_layers[i] += 1;
7427 		}
7428 
7429 		if (i == 0) {
7430 			if (vps->base_layer_internal_flag) {
7431 				if (vps->max_layers > 1)
7432 					vps->profile_tier_level_idx[0][0] = 1;
7433 				else
7434 					vps->profile_tier_level_idx[0][0] = 0;
7435 			}
7436 			continue;
7437 		}
7438 		nb_bits = 1;
7439 		while ((u32)(1 << nb_bits) < vps->num_profile_tier_level)
7440 			nb_bits++;
7441 		for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++)
7442 			if (vps->necessary_layers_flag[i][j] && vps->num_profile_tier_level)
7443 				vps->profile_tier_level_idx[i][j] = gf_bs_read_int(bs, nb_bits);
7444 			else
7445 				vps->profile_tier_level_idx[i][j] = 0;
7446 
7447 
7448 		nb_output_layers_in_output_layer_set[i] = 0;
7449 		for (j = 0; j < vps->num_layers_in_id_list[ols_ids_to_ls_idx]; j++) {
7450 			nb_output_layers_in_output_layer_set[i] += OutputLayerFlag[i][j];
7451 			if (OutputLayerFlag[i][j]) {
7452 				ols_highest_output_layer_id[i] = vps->LayerSetLayerIdList[ols_ids_to_ls_idx][j];
7453 			}
7454 		}
7455 		if (nb_output_layers_in_output_layer_set[i] == 1 && ols_highest_output_layer_id[i] > 0)
7456 			vps->alt_output_layer_flag[i] = gf_bs_read_int(bs, 1);
7457 	}
7458 
7459 	vps->num_rep_formats = 1 + gf_bs_get_ue(bs);
7460 	if (vps->num_rep_formats > 16) {
7461 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Wrong number of rep formats in VPS %d\n", vps->num_rep_formats));
7462 		vps->num_rep_formats = 0;
7463 		return GF_FALSE;
7464 	}
7465 
7466 	for (i = 0; i < vps->num_rep_formats; i++) {
7467 		lhvc_parse_rep_format(&vps->rep_formats[i], bs);
7468 	}
7469 	if (vps->num_rep_formats > 1)
7470 		rep_format_idx_present_flag = gf_bs_read_int(bs, 1);
7471 	else
7472 		rep_format_idx_present_flag = 0;
7473 
7474 	vps->rep_format_idx[0] = 0;
7475 	nb_bits = 1;
7476 	while ((u32)(1 << nb_bits) < vps->num_rep_formats)
7477 		nb_bits++;
7478 	for (i = vps->base_layer_internal_flag ? 1 : 0; i < vps->max_layers; i++) {
7479 		if (rep_format_idx_present_flag) {
7480 			vps->rep_format_idx[i] = gf_bs_read_int(bs, nb_bits);
7481 		}
7482 		else {
7483 			vps->rep_format_idx[i] = i < vps->num_rep_formats - 1 ? i : vps->num_rep_formats - 1;
7484 		}
7485 	}
7486 	//TODO - we don't use the rest ...
7487 
7488 	return GF_TRUE;
7489 }
7490 
sub_layer_hrd_parameters(GF_BitStream * bs,int subLayerId,u32 cpb_cnt,Bool sub_pic_hrd_params_present_flag)7491 static void sub_layer_hrd_parameters(GF_BitStream *bs, int subLayerId, u32 cpb_cnt, Bool sub_pic_hrd_params_present_flag)
7492 {
7493 	u32 i;
7494 	if (!gf_bs_available(bs)) return;
7495 
7496 	for (i = 0; i <= cpb_cnt; i++) {
7497 		/*bit_rate_value_minus1[i] = */gf_bs_get_ue(bs);
7498 		/*cpb_size_value_minus1[i] = */gf_bs_get_ue(bs);
7499 		if (sub_pic_hrd_params_present_flag) {
7500 			/*cpb_size_du_value_minus1[i] = */gf_bs_get_ue(bs);
7501 			/*bit_rate_du_value_minus1[i] = */gf_bs_get_ue(bs);
7502 		}
7503 		/*cbr_flag[i] = */gf_bs_read_int(bs, 1);
7504 	}
7505 }
7506 
hevc_parse_hrd_parameters(GF_BitStream * bs,Bool commonInfPresentFlag,int maxNumSubLayersMinus1)7507 static void hevc_parse_hrd_parameters(GF_BitStream *bs, Bool commonInfPresentFlag, int maxNumSubLayersMinus1)
7508 {
7509 	int i;
7510 	Bool nal_hrd_parameters_present_flag = GF_FALSE;
7511 	Bool vcl_hrd_parameters_present_flag = GF_FALSE;
7512 	Bool sub_pic_hrd_params_present_flag = GF_FALSE;
7513 	if (commonInfPresentFlag) {
7514 		nal_hrd_parameters_present_flag = gf_bs_read_int(bs, 1);
7515 		vcl_hrd_parameters_present_flag = gf_bs_read_int(bs, 1);
7516 		if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) {
7517 			sub_pic_hrd_params_present_flag = gf_bs_read_int(bs, 1);
7518 			if (sub_pic_hrd_params_present_flag) {
7519 				/*tick_divisor_minus2 = */gf_bs_read_int(bs, 8);
7520 				/*du_cpb_removal_delay_increment_length_minus1 = */gf_bs_read_int(bs, 5);
7521 				/*sub_pic_cpb_params_in_pic_timing_sei_flag = */gf_bs_read_int(bs, 1);
7522 				/*dpb_output_delay_du_length_minus1 = */gf_bs_read_int(bs, 5);
7523 			}
7524 			/*bit_rate_scale = */gf_bs_read_int(bs, 4);
7525 			/*cpb_size_scale = */gf_bs_read_int(bs, 4);
7526 			if (sub_pic_hrd_params_present_flag) {
7527 				/*cpb_size_du_scale = */gf_bs_read_int(bs, 4);
7528 			}
7529 			/*initial_cpb_removal_delay_length_minus1 = */gf_bs_read_int(bs, 5);
7530 			/*au_cpb_removal_delay_length_minus1 = */gf_bs_read_int(bs, 5);
7531 			/*dpb_output_delay_length_minus1 = */gf_bs_read_int(bs, 5);
7532 		}
7533 	}
7534 	for (i = 0; i <= maxNumSubLayersMinus1; i++) {
7535 		Bool fixed_pic_rate_general_flag_i = gf_bs_read_int(bs, 1);
7536 		Bool fixed_pic_rate_within_cvs_flag_i = GF_TRUE;
7537 		Bool low_delay_hrd_flag_i = GF_FALSE;
7538 		u32 cpb_cnt_minus1_i = 0;
7539 		if (!fixed_pic_rate_general_flag_i) {
7540 			fixed_pic_rate_within_cvs_flag_i = gf_bs_read_int(bs, 1);
7541 		}
7542 		if (fixed_pic_rate_within_cvs_flag_i)
7543 			/*elemental_duration_in_tc_minus1[i] = */gf_bs_get_ue(bs);
7544 		else
7545 			low_delay_hrd_flag_i = gf_bs_read_int(bs, 1);
7546 		if (!low_delay_hrd_flag_i) {
7547 			cpb_cnt_minus1_i = gf_bs_get_ue(bs);
7548 		}
7549 		if (nal_hrd_parameters_present_flag) {
7550 			sub_layer_hrd_parameters(bs, i, cpb_cnt_minus1_i, sub_pic_hrd_params_present_flag);
7551 		}
7552 		if (vcl_hrd_parameters_present_flag) {
7553 			sub_layer_hrd_parameters(bs, i, cpb_cnt_minus1_i, sub_pic_hrd_params_present_flag);
7554 		}
7555 	}
7556 }
7557 
gf_media_hevc_read_vps_bs_internal(GF_BitStream * bs,HEVCState * hevc,Bool stop_at_vps_ext)7558 static s32 gf_media_hevc_read_vps_bs_internal(GF_BitStream *bs, HEVCState *hevc, Bool stop_at_vps_ext)
7559 {
7560 	u8 vps_sub_layer_ordering_info_present_flag, vps_extension_flag;
7561 	u32 i, j;
7562 	s32 vps_id;
7563 	HEVC_VPS *vps;
7564 	u8 layer_id_included_flag[MAX_LHVC_LAYERS][64];
7565 
7566 	//nalu header already parsed
7567 	vps_id = gf_bs_read_int(bs, 4);
7568 
7569 	if (vps_id >= 16) return -1;
7570 
7571 	vps = &hevc->vps[vps_id];
7572 	vps->bit_pos_vps_extensions = -1;
7573 	if (!vps->state) {
7574 		vps->id = vps_id;
7575 		vps->state = 1;
7576 	}
7577 
7578 	vps->base_layer_internal_flag = gf_bs_read_int(bs, 1);
7579 	vps->base_layer_available_flag = gf_bs_read_int(bs, 1);
7580 	vps->max_layers = 1 + gf_bs_read_int(bs, 6);
7581 	if (vps->max_layers > MAX_LHVC_LAYERS) {
7582 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] sorry, %d layers in VPS but only %d supported\n", vps->max_layers, MAX_LHVC_LAYERS));
7583 		return -1;
7584 	}
7585 	vps->max_sub_layers = gf_bs_read_int(bs, 3) + 1;
7586 	vps->temporal_id_nesting = gf_bs_read_int(bs, 1);
7587 	/* vps_reserved_ffff_16bits = */ gf_bs_read_int(bs, 16);
7588 	profile_tier_level(bs, 1, vps->max_sub_layers - 1, &vps->ptl);
7589 
7590 	vps_sub_layer_ordering_info_present_flag = gf_bs_read_int(bs, 1);
7591 	for (i = (vps_sub_layer_ordering_info_present_flag ? 0 : vps->max_sub_layers - 1); i < vps->max_sub_layers; i++) {
7592 		/*vps_max_dec_pic_buffering_minus1[i] = */gf_bs_get_ue(bs);
7593 		/*vps_max_num_reorder_pics[i] = */gf_bs_get_ue(bs);
7594 		/*vps_max_latency_increase_plus1[i] = */gf_bs_get_ue(bs);
7595 	}
7596 	vps->max_layer_id = gf_bs_read_int(bs, 6);
7597 	if (vps->max_layer_id > MAX_LHVC_LAYERS) {
7598 		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));
7599 		return -1;
7600 	}
7601 	vps->num_layer_sets = gf_bs_get_ue(bs) + 1;
7602 	if (vps->num_layer_sets > MAX_LHVC_LAYERS) {
7603 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Wrong number of layer sets in VPS %d\n", vps->num_layer_sets));
7604 		return -1;
7605 	}
7606 	for (i = 1; i < vps->num_layer_sets; i++) {
7607 		for (j = 0; j <= vps->max_layer_id; j++) {
7608 			layer_id_included_flag[i][j] = gf_bs_read_int(bs, 1);
7609 		}
7610 	}
7611 	vps->num_layers_in_id_list[0] = 1;
7612 	for (i = 1; i < vps->num_layer_sets; i++) {
7613 		u32 n, m;
7614 		n = 0;
7615 		for (m = 0; m <= vps->max_layer_id; m++) {
7616 			if (layer_id_included_flag[i][m]) {
7617 				vps->LayerSetLayerIdList[i][n++] = m;
7618 				if (vps->LayerSetLayerIdListMax[i] < m)
7619 					vps->LayerSetLayerIdListMax[i] = m;
7620 			}
7621 		}
7622 		vps->num_layers_in_id_list[i] = n;
7623 	}
7624 	if (/*vps_timing_info_present_flag*/gf_bs_read_int(bs, 1)) {
7625 		u32 vps_num_hrd_parameters;
7626 		/*u32 vps_num_units_in_tick = */gf_bs_read_int(bs, 32);
7627 		/*u32 vps_time_scale = */gf_bs_read_int(bs, 32);
7628 		if (/*vps_poc_proportional_to_timing_flag*/gf_bs_read_int(bs, 1)) {
7629 			/*vps_num_ticks_poc_diff_one_minus1*/gf_bs_get_ue(bs);
7630 		}
7631 		vps_num_hrd_parameters = gf_bs_get_ue(bs);
7632 		for (i = 0; i < vps_num_hrd_parameters; i++) {
7633 			Bool cprms_present_flag = GF_TRUE;
7634 			/*hrd_layer_set_idx[i] = */gf_bs_get_ue(bs);
7635 			if (i > 0)
7636 				cprms_present_flag = gf_bs_read_int(bs, 1);
7637 			hevc_parse_hrd_parameters(bs, cprms_present_flag, vps->max_sub_layers - 1);
7638 		}
7639 	}
7640 	if (stop_at_vps_ext) {
7641 		return vps_id;
7642 	}
7643 
7644 	vps_extension_flag = gf_bs_read_int(bs, 1);
7645 	if (vps_extension_flag) {
7646 		Bool res;
7647 		gf_bs_align(bs);
7648 		res = hevc_parse_vps_extension(vps, bs);
7649 		if (res != GF_TRUE) {
7650 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Failed to parse VPS extensions\n"));
7651 			return -1;
7652 		}
7653 		if (/*vps_extension2_flag*/gf_bs_read_int(bs, 1)) {
7654 #if 0
7655 			while (gf_bs_available(bs)) {
7656 				/*vps_extension_data_flag */ gf_bs_read_int(bs, 1);
7657 			}
7658 #endif
7659 
7660 		}
7661 	}
7662 	return vps_id;
7663 }
7664 
7665 GF_EXPORT
gf_media_hevc_read_vps_ex(u8 * data,u32 * size,HEVCState * hevc,Bool remove_extensions)7666 s32 gf_media_hevc_read_vps_ex(u8 *data, u32 *size, HEVCState *hevc, Bool remove_extensions)
7667 {
7668 	GF_BitStream *bs;
7669 	char *data_without_emulation_bytes = NULL;
7670 	u32 data_without_emulation_bytes_size = 0;
7671 	s32 vps_id = -1;
7672 
7673 	/*still contains emulation bytes*/
7674 	data_without_emulation_bytes_size = remove_extensions ? gf_media_nalu_emulation_bytes_remove_count(data, (*size)) : 0;
7675 	if (!data_without_emulation_bytes_size) {
7676 		bs = gf_bs_new(data, (*size), GF_BITSTREAM_READ);
7677 		gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
7678 	}
7679 	//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
7680 	else {
7681 		data_without_emulation_bytes = gf_malloc((*size) * sizeof(char));
7682 		data_without_emulation_bytes_size = gf_media_nalu_remove_emulation_bytes(data, data_without_emulation_bytes, (*size));
7683 		bs = gf_bs_new(data_without_emulation_bytes, data_without_emulation_bytes_size, GF_BITSTREAM_READ);
7684 	}
7685 	if (!bs) goto exit;
7686 
7687 
7688 	if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) goto exit;
7689 
7690 	vps_id = gf_media_hevc_read_vps_bs_internal(bs, hevc, remove_extensions);
7691 	if (vps_id < 0) goto exit;
7692 
7693 	if (remove_extensions) {
7694 		u8 *new_vps;
7695 		u32 new_vps_size, emulation_bytes;
7696 		u32 bit_pos = gf_bs_get_bit_offset(bs);
7697 		GF_BitStream *w_bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
7698 		gf_bs_write_u8(w_bs, data[0]);
7699 		gf_bs_write_u8(w_bs, data[1]);
7700 		gf_bs_write_u8(w_bs, data[2]);
7701 		gf_bs_write_u8(w_bs, data[3]);
7702 		gf_bs_write_u16(w_bs, 0xFFFF);
7703 		gf_bs_seek(bs, 6);
7704 		bit_pos -= 48;
7705 		while (bit_pos) {
7706 			u32 v = gf_bs_read_int(bs, 1);
7707 			gf_bs_write_int(w_bs, v, 1);
7708 			bit_pos--;
7709 		}
7710 		/*vps extension flag*/
7711 		gf_bs_write_int(w_bs, 0, 1);
7712 		new_vps = NULL;
7713 		gf_bs_get_content(w_bs, &new_vps, &new_vps_size);
7714 		gf_bs_del(w_bs);
7715 
7716 		emulation_bytes = gf_media_nalu_emulation_bytes_add_count(new_vps, new_vps_size);
7717 		if (emulation_bytes + new_vps_size > *size) {
7718 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("Buffer too small to rewrite VPS - skipping rewrite\n"));
7719 		}
7720 		else {
7721 			*size = gf_media_nalu_add_emulation_bytes(new_vps, data, new_vps_size);
7722 		}
7723 	}
7724 
7725 exit:
7726 	if (bs) gf_bs_del(bs);
7727 	if (data_without_emulation_bytes) gf_free(data_without_emulation_bytes);
7728 	return vps_id;
7729 }
7730 
7731 GF_EXPORT
gf_media_hevc_read_vps(u8 * data,u32 size,HEVCState * hevc)7732 s32 gf_media_hevc_read_vps(u8 *data, u32 size, HEVCState *hevc)
7733 {
7734 	return gf_media_hevc_read_vps_ex(data, &size, hevc, GF_FALSE);
7735 }
7736 
7737 GF_EXPORT
gf_media_hevc_read_vps_bs(GF_BitStream * bs,HEVCState * hevc)7738 s32 gf_media_hevc_read_vps_bs(GF_BitStream *bs, HEVCState *hevc)
7739 {
7740 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
7741 	if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) return -1;
7742 	return gf_media_hevc_read_vps_bs_internal(bs, hevc, GF_FALSE);
7743 }
7744 
hevc_scaling_list_data(GF_BitStream * bs)7745 static void hevc_scaling_list_data(GF_BitStream *bs)
7746 {
7747 	u32 i, sizeId, matrixId;
7748 	for (sizeId = 0; sizeId < 4; sizeId++) {
7749 		for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3) ? 3 : 1) {
7750 			u32 scaling_list_pred_mode_flag_sizeId_matrixId = gf_bs_read_int(bs, 1);
7751 			if (!scaling_list_pred_mode_flag_sizeId_matrixId) {
7752 				/*scaling_list_pred_matrix_id_delta[ sizeId ][ matrixId ] =*/ gf_bs_get_ue(bs);
7753 			}
7754 			else {
7755 				//u32 nextCoef = 8;
7756 				u32 coefNum = MIN(64, (1 << (4 + (sizeId << 1))));
7757 				if (sizeId > 1) {
7758 					/*scaling_list_dc_coef_minus8[ sizeId - 2 ][ matrixId ] = */gf_bs_get_se(bs);
7759 				}
7760 				for (i = 0; i < coefNum; i++) {
7761 					/*scaling_list_delta_coef = */gf_bs_get_se(bs);
7762 				}
7763 			}
7764 		}
7765 	}
7766 }
7767 
7768 
7769 static const struct {
7770 	u32 w, h;
7771 } hevc_sar[17] =
7772 {
7773 	{ 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
7774 	{ 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
7775 	{ 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
7776 	{ 64, 33 }, { 160,99 }, { 4,3}, { 3,2}, { 2,1}
7777 };
7778 
gf_media_hevc_read_sps_bs_internal(GF_BitStream * bs,HEVCState * hevc,u8 layer_id,u32 * vui_flag_pos)7779 static s32 gf_media_hevc_read_sps_bs_internal(GF_BitStream *bs, HEVCState *hevc, u8 layer_id, u32 *vui_flag_pos)
7780 {
7781 	s32 vps_id, sps_id = -1;
7782 	u32 i, nb_CTUs, depth;
7783 	HEVC_SPS *sps;
7784 	HEVC_VPS *vps;
7785 	HEVC_ProfileTierLevel ptl;
7786 	Bool multiLayerExtSpsFlag;
7787 	u8 sps_ext_or_max_sub_layers_minus1, max_sub_layers_minus1;
7788 
7789 	if (vui_flag_pos) *vui_flag_pos = 0;
7790 
7791 	//nalu header already parsed
7792 	vps_id = gf_bs_read_int(bs, 4);
7793 	if (vps_id >= 16) {
7794 		return -1;
7795 	}
7796 	memset(&ptl, 0, sizeof(ptl));
7797 	max_sub_layers_minus1 = 0;
7798 	sps_ext_or_max_sub_layers_minus1 = 0;
7799 	if (layer_id == 0)
7800 		max_sub_layers_minus1 = gf_bs_read_int(bs, 3);
7801 	else
7802 		sps_ext_or_max_sub_layers_minus1 = gf_bs_read_int(bs, 3);
7803 	multiLayerExtSpsFlag = (layer_id != 0) && (sps_ext_or_max_sub_layers_minus1 == 7);
7804 	if (!multiLayerExtSpsFlag) {
7805 		/*temporal_id_nesting_flag = */gf_bs_read_int(bs, 1);
7806 		profile_tier_level(bs, 1, max_sub_layers_minus1, &ptl);
7807 	}
7808 
7809 	sps_id = gf_bs_get_ue(bs);
7810 	if ((sps_id < 0) || (sps_id >= 16)) {
7811 		return -1;
7812 	}
7813 
7814 	sps = &hevc->sps[sps_id];
7815 	if (!sps->state) {
7816 		sps->state = 1;
7817 		sps->id = sps_id;
7818 		sps->vps_id = vps_id;
7819 	}
7820 	sps->ptl = ptl;
7821 	vps = &hevc->vps[vps_id];
7822 	sps->max_sub_layers_minus1 = 0;
7823 	sps->sps_ext_or_max_sub_layers_minus1 = 0;
7824 
7825 	/* default values */
7826 	sps->colour_primaries = 2;
7827 	sps->transfer_characteristic = 2;
7828 	sps->matrix_coeffs = 2;
7829 
7830 	//sps_rep_format_idx = 0;
7831 	if (multiLayerExtSpsFlag) {
7832 		sps->update_rep_format_flag = gf_bs_read_int(bs, 1);
7833 		if (sps->update_rep_format_flag) {
7834 			sps->rep_format_idx = gf_bs_read_int(bs, 8);
7835 		}
7836 		else {
7837 			sps->rep_format_idx = vps->rep_format_idx[layer_id];
7838 		}
7839 		sps->width = vps->rep_formats[sps->rep_format_idx].pic_width_luma_samples;
7840 		sps->height = vps->rep_formats[sps->rep_format_idx].pic_height_luma_samples;
7841 		sps->chroma_format_idc = vps->rep_formats[sps->rep_format_idx].chroma_format_idc;
7842 		sps->bit_depth_luma = vps->rep_formats[sps->rep_format_idx].bit_depth_luma;
7843 		sps->bit_depth_chroma = vps->rep_formats[sps->rep_format_idx].bit_depth_chroma;
7844 		sps->separate_colour_plane_flag = vps->rep_formats[sps->rep_format_idx].separate_colour_plane_flag;
7845 
7846 		//TODO this is crude ...
7847 		sps->ptl = vps->ext_ptl[0];
7848 	}
7849 	else {
7850 		sps->chroma_format_idc = gf_bs_get_ue(bs);
7851 		if (sps->chroma_format_idc == 3)
7852 			sps->separate_colour_plane_flag = gf_bs_read_int(bs, 1);
7853 		sps->width = gf_bs_get_ue(bs);
7854 		sps->height = gf_bs_get_ue(bs);
7855 		if ((sps->cw_flag = gf_bs_read_int(bs, 1))) {
7856 			u32 SubWidthC, SubHeightC;
7857 
7858 			if (sps->chroma_format_idc == 1) {
7859 				SubWidthC = SubHeightC = 2;
7860 			}
7861 			else if (sps->chroma_format_idc == 2) {
7862 				SubWidthC = 2;
7863 				SubHeightC = 1;
7864 			}
7865 			else {
7866 				SubWidthC = SubHeightC = 1;
7867 			}
7868 
7869 			sps->cw_left = gf_bs_get_ue(bs);
7870 			sps->cw_right = gf_bs_get_ue(bs);
7871 			sps->cw_top = gf_bs_get_ue(bs);
7872 			sps->cw_bottom = gf_bs_get_ue(bs);
7873 
7874 			sps->width -= SubWidthC * (sps->cw_left + sps->cw_right);
7875 			sps->height -= SubHeightC * (sps->cw_top + sps->cw_bottom);
7876 		}
7877 		sps->bit_depth_luma = 8 + gf_bs_get_ue(bs);
7878 		sps->bit_depth_chroma = 8 + gf_bs_get_ue(bs);
7879 	}
7880 
7881 	sps->log2_max_pic_order_cnt_lsb = 4 + gf_bs_get_ue(bs);
7882 
7883 	if (!multiLayerExtSpsFlag) {
7884 		sps->sub_layer_ordering_info_present_flag = gf_bs_read_int(bs, 1);
7885 		for (i = sps->sub_layer_ordering_info_present_flag ? 0 : sps->max_sub_layers_minus1; i <= sps->max_sub_layers_minus1; i++) {
7886 			/*max_dec_pic_buffering = */ gf_bs_get_ue(bs);
7887 			/*num_reorder_pics = */ gf_bs_get_ue(bs);
7888 			/*max_latency_increase = */ gf_bs_get_ue(bs);
7889 		}
7890 	}
7891 
7892 	sps->log2_min_luma_coding_block_size = 3 + gf_bs_get_ue(bs);
7893 	sps->log2_diff_max_min_luma_coding_block_size = gf_bs_get_ue(bs);
7894 	sps->max_CU_width = (1 << (sps->log2_min_luma_coding_block_size + sps->log2_diff_max_min_luma_coding_block_size));
7895 	sps->max_CU_height = (1 << (sps->log2_min_luma_coding_block_size + sps->log2_diff_max_min_luma_coding_block_size));
7896 
7897 	sps->log2_min_transform_block_size = 2 + gf_bs_get_ue(bs);
7898 	sps->log2_max_transform_block_size = sps->log2_min_transform_block_size  + gf_bs_get_ue(bs);
7899 
7900 	depth = 0;
7901 	sps->max_transform_hierarchy_depth_inter = gf_bs_get_ue(bs);
7902 	sps->max_transform_hierarchy_depth_intra = gf_bs_get_ue(bs);
7903 	while ((u32)(sps->max_CU_width >> sps->log2_diff_max_min_luma_coding_block_size) > (u32)(1 << (sps->log2_min_transform_block_size + depth)))
7904 	{
7905 		depth++;
7906 	}
7907 	sps->max_CU_depth = sps->log2_diff_max_min_luma_coding_block_size + depth;
7908 
7909 	nb_CTUs = ((sps->width + sps->max_CU_width - 1) / sps->max_CU_width) * ((sps->height + sps->max_CU_height - 1) / sps->max_CU_height);
7910 	sps->bitsSliceSegmentAddress = 0;
7911 	while (nb_CTUs > (u32)(1 << sps->bitsSliceSegmentAddress)) {
7912 		sps->bitsSliceSegmentAddress++;
7913 	}
7914 
7915 	sps->scaling_list_enable_flag = gf_bs_read_int(bs, 1);
7916 	if (sps->scaling_list_enable_flag) {
7917 		sps->infer_scaling_list_flag = 0;
7918 		sps->scaling_list_ref_layer_id = 0;
7919 		if (multiLayerExtSpsFlag) {
7920 			sps->infer_scaling_list_flag = gf_bs_read_int(bs, 1);
7921 		}
7922 		if (sps->infer_scaling_list_flag) {
7923 			sps->scaling_list_ref_layer_id = gf_bs_read_int(bs, 6);
7924 		}
7925 		else {
7926 			sps->scaling_list_data_present_flag = gf_bs_read_int(bs, 1);
7927 			if (sps->scaling_list_data_present_flag) {
7928 				hevc_scaling_list_data(bs);
7929 			}
7930 		}
7931 	}
7932 	sps->asymmetric_motion_partitions_enabled_flag = gf_bs_read_int(bs, 1);
7933 	sps->sample_adaptive_offset_enabled_flag = gf_bs_read_int(bs, 1);
7934 	if ( (sps->pcm_enabled_flag = gf_bs_read_int(bs, 1)) ) {
7935 		sps->pcm_sample_bit_depth_luma_minus1 = gf_bs_read_int(bs, 4);
7936 		sps->pcm_sample_bit_depth_chroma_minus1 = gf_bs_read_int(bs, 4);
7937 		sps->log2_min_pcm_luma_coding_block_size_minus3 = gf_bs_get_ue(bs);
7938 		sps->log2_diff_max_min_pcm_luma_coding_block_size = gf_bs_get_ue(bs);
7939 		sps->pcm_loop_filter_disable_flag = gf_bs_read_int(bs, 1);
7940 	}
7941 	sps->num_short_term_ref_pic_sets = gf_bs_get_ue(bs);
7942 	if (sps->num_short_term_ref_pic_sets > 64) {
7943 		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));
7944 		return -1;
7945 	}
7946 
7947 	for (i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
7948 		Bool ret = parse_short_term_ref_pic_set(bs, sps, i);
7949 		/*cannot parse short_term_ref_pic_set, skip VUI parsing*/
7950 		if (!ret) {
7951 			GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] Invalid short_term_ref_pic_set\n"));
7952 			return -1;
7953 		}
7954 	}
7955 	sps->long_term_ref_pics_present_flag = gf_bs_read_int(bs, 1);
7956 	if (sps->long_term_ref_pics_present_flag) {
7957 		sps->num_long_term_ref_pic_sps = gf_bs_get_ue(bs);
7958 		for (i = 0; i < sps->num_long_term_ref_pic_sps; i++) {
7959 			/*lt_ref_pic_poc_lsb_sps=*/gf_bs_read_int(bs, sps->log2_max_pic_order_cnt_lsb);
7960 			/*used_by_curr_pic_lt_sps_flag*/gf_bs_read_int(bs, 1);
7961 		}
7962 	}
7963 	sps->temporal_mvp_enable_flag = gf_bs_read_int(bs, 1);
7964 	sps->strong_intra_smoothing_enable_flag = gf_bs_read_int(bs, 1);
7965 
7966 	if (vui_flag_pos)
7967 		*vui_flag_pos = (u32)gf_bs_get_bit_offset(bs);
7968 
7969 	if ((sps->vui_parameters_present_flag = gf_bs_read_int(bs, 1)) ) {
7970 		sps->aspect_ratio_info_present_flag = gf_bs_read_int(bs, 1);
7971 		if (sps->aspect_ratio_info_present_flag) {
7972 			sps->sar_idc = gf_bs_read_int(bs, 8);
7973 			if (sps->sar_idc == 255) {
7974 				sps->sar_width = gf_bs_read_int(bs, 16);
7975 				sps->sar_height = gf_bs_read_int(bs, 16);
7976 			}
7977 			else if (sps->sar_idc < 17) {
7978 				sps->sar_width = hevc_sar[sps->sar_idc].w;
7979 				sps->sar_height = hevc_sar[sps->sar_idc].h;
7980 			}
7981 		}
7982 
7983 		if ((sps->overscan_info_present = gf_bs_read_int(bs, 1)))
7984 			sps->overscan_appropriate = gf_bs_read_int(bs, 1);
7985 
7986 		sps->video_signal_type_present_flag = gf_bs_read_int(bs, 1);
7987 		if (sps->video_signal_type_present_flag) {
7988 			sps->video_format = gf_bs_read_int(bs, 3);
7989 			sps->video_full_range_flag = gf_bs_read_int(bs, 1);
7990 			if ((sps->colour_description_present_flag = gf_bs_read_int(bs, 1))) {
7991 				sps->colour_primaries = gf_bs_read_int(bs, 8);
7992 				sps->transfer_characteristic = gf_bs_read_int(bs, 8);
7993 				sps->matrix_coeffs = gf_bs_read_int(bs, 8);
7994 			}
7995 		}
7996 
7997 		if ((sps->chroma_loc_info_present_flag = gf_bs_read_int(bs, 1))) {
7998 			sps->chroma_sample_loc_type_top_field = gf_bs_get_ue(bs);
7999 			sps->chroma_sample_loc_type_bottom_field = gf_bs_get_ue(bs);
8000 		}
8001 
8002 		sps->neutra_chroma_indication_flag = gf_bs_read_int(bs, 1);
8003 		sps->field_seq_flag = gf_bs_read_int(bs, 1);
8004 		sps->frame_field_info_present_flag = gf_bs_read_int(bs, 1);
8005 
8006 		if ((sps->default_display_window_flag = gf_bs_read_int(bs, 1))) {
8007 			sps->left_offset = gf_bs_get_ue(bs);
8008 			sps->right_offset = gf_bs_get_ue(bs);
8009 			sps->top_offset = gf_bs_get_ue(bs);
8010 			sps->bottom_offset = gf_bs_get_ue(bs);
8011 		}
8012 
8013 		sps->has_timing_info = gf_bs_read_int(bs, 1);
8014 		if (sps->has_timing_info) {
8015 			sps->num_units_in_tick = gf_bs_read_int(bs, 32);
8016 			sps->time_scale = gf_bs_read_int(bs, 32);
8017 			sps->poc_proportional_to_timing_flag = gf_bs_read_int(bs, 1);
8018 			if (sps->poc_proportional_to_timing_flag)
8019 				sps->num_ticks_poc_diff_one_minus1 = gf_bs_get_ue(bs);
8020 			if ((sps->hrd_parameters_present_flag = gf_bs_read_int(bs, 1))) {
8021 				//				GF_LOG(GF_LOG_INFO, GF_LOG_CODING, ("[HEVC] HRD param parsing not implemented\n"));
8022 				return sps_id;
8023 			}
8024 		}
8025 
8026 		if (/*bitstream_restriction_flag=*/gf_bs_read_int(bs, 1)) {
8027 			/*tiles_fixed_structure_flag = */gf_bs_read_int(bs, 1);
8028 			/*motion_vectors_over_pic_boundaries_flag = */gf_bs_read_int(bs, 1);
8029 			/*restricted_ref_pic_lists_flag = */gf_bs_read_int(bs, 1);
8030 			/*min_spatial_segmentation_idc = */gf_bs_get_ue(bs);
8031 			/*max_bytes_per_pic_denom = */gf_bs_get_ue(bs);
8032 			/*max_bits_per_min_cu_denom = */gf_bs_get_ue(bs);
8033 			/*log2_max_mv_length_horizontal = */gf_bs_get_ue(bs);
8034 			/*log2_max_mv_length_vertical = */gf_bs_get_ue(bs);
8035 		}
8036 	}
8037 
8038 	if (/*sps_extension_flag*/gf_bs_read_int(bs, 1)) {
8039 #if 0
8040 		while (gf_bs_available(bs)) {
8041 			/*sps_extension_data_flag */ gf_bs_read_int(bs, 1);
8042 		}
8043 #endif
8044 
8045 	}
8046 
8047 	return sps_id;
8048 }
8049 
8050 GF_EXPORT
gf_media_hevc_read_sps_ex(char * data,u32 size,HEVCState * hevc,u32 * vui_flag_pos)8051 s32 gf_media_hevc_read_sps_ex(char *data, u32 size, HEVCState *hevc, u32 *vui_flag_pos)
8052 {
8053 	GF_BitStream *bs;
8054 	s32 sps_id = -1;
8055 	u8 layer_id;
8056 
8057 	if (vui_flag_pos) *vui_flag_pos = 0;
8058 
8059 	bs = gf_bs_new(data, size, GF_BITSTREAM_READ);
8060 	if (!bs) goto exit;
8061 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
8062 
8063 	if (!hevc_parse_nal_header(bs, NULL, NULL, &layer_id)) goto exit;
8064 	sps_id = gf_media_hevc_read_sps_bs_internal(bs, hevc, layer_id, vui_flag_pos);
8065 
8066 exit:
8067 	if (bs) gf_bs_del(bs);
8068 	return sps_id;
8069 }
8070 
8071 GF_EXPORT
gf_media_hevc_read_sps(u8 * data,u32 size,HEVCState * hevc)8072 s32 gf_media_hevc_read_sps(u8 *data, u32 size, HEVCState *hevc)
8073 {
8074 	return gf_media_hevc_read_sps_ex(data, size, hevc, NULL);
8075 }
8076 
8077 GF_EXPORT
gf_media_hevc_read_sps_bs(GF_BitStream * bs,HEVCState * hevc)8078 s32 gf_media_hevc_read_sps_bs(GF_BitStream *bs, HEVCState *hevc)
8079 {
8080 	u8 layer_id;
8081 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
8082 	if (!hevc_parse_nal_header(bs, NULL, NULL, &layer_id)) return -1;
8083 	return gf_media_hevc_read_sps_bs_internal(bs, hevc, layer_id, NULL);
8084 }
8085 
8086 
gf_media_hevc_read_pps_bs_internal(GF_BitStream * bs,HEVCState * hevc)8087 static s32 gf_media_hevc_read_pps_bs_internal(GF_BitStream *bs, HEVCState *hevc)
8088 {
8089 	u32 i;
8090 	s32 pps_id;
8091 	HEVC_PPS *pps;
8092 
8093 	//NAL header already read
8094 	pps_id = gf_bs_get_ue(bs);
8095 
8096 	if ((pps_id < 0) || (pps_id >= 64)) {
8097 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] wrong PPS ID %d in PPS\n", pps_id));
8098 		return -1;
8099 	}
8100 	pps = &hevc->pps[pps_id];
8101 
8102 	if (!pps->state) {
8103 		pps->id = pps_id;
8104 		pps->state = 1;
8105 	}
8106 	pps->sps_id = gf_bs_get_ue(bs);
8107 	if (pps->sps_id > 16) {
8108 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[HEVC] wrong SPS ID %d in PPS\n", pps->sps_id));
8109 		return -1;
8110 	}
8111 	hevc->sps_active_idx = pps->sps_id; /*set active sps*/
8112 	pps->dependent_slice_segments_enabled_flag = gf_bs_read_int(bs, 1);
8113 
8114 	pps->output_flag_present_flag = gf_bs_read_int(bs, 1);
8115 	pps->num_extra_slice_header_bits = gf_bs_read_int(bs, 3);
8116 	pps->sign_data_hiding_flag = gf_bs_read_int(bs, 1);
8117 	pps->cabac_init_present_flag = gf_bs_read_int(bs, 1);
8118 	pps->num_ref_idx_l0_default_active = 1 + gf_bs_get_ue(bs);
8119 	pps->num_ref_idx_l1_default_active = 1 + gf_bs_get_ue(bs);
8120 	pps->pic_init_qp_minus26 = gf_bs_get_se(bs);
8121 	pps->constrained_intra_pred_flag = gf_bs_read_int(bs, 1);
8122 	pps->transform_skip_enabled_flag = gf_bs_read_int(bs, 1);
8123 	if ((pps->cu_qp_delta_enabled_flag = gf_bs_read_int(bs, 1)))
8124 		pps->diff_cu_qp_delta_depth = gf_bs_get_ue(bs);
8125 
8126 	pps->pic_cb_qp_offset = gf_bs_get_se(bs);
8127 	pps->pic_cr_qp_offset = gf_bs_get_se(bs);
8128 	pps->slice_chroma_qp_offsets_present_flag = gf_bs_read_int(bs, 1);
8129 	pps->weighted_pred_flag = gf_bs_read_int(bs, 1);
8130 	pps->weighted_bipred_flag = gf_bs_read_int(bs, 1);
8131 	pps->transquant_bypass_enable_flag = gf_bs_read_int(bs, 1);
8132 	pps->tiles_enabled_flag = gf_bs_read_int(bs, 1);
8133 	pps->entropy_coding_sync_enabled_flag = gf_bs_read_int(bs, 1);
8134 	if (pps->tiles_enabled_flag) {
8135 		pps->num_tile_columns = 1 + gf_bs_get_ue(bs);
8136 		pps->num_tile_rows = 1 + gf_bs_get_ue(bs);
8137 		pps->uniform_spacing_flag = gf_bs_read_int(bs, 1);
8138 		if (!pps->uniform_spacing_flag) {
8139 			for (i = 0; i < pps->num_tile_columns - 1; i++) {
8140 				pps->column_width[i] = 1 + gf_bs_get_ue(bs);
8141 			}
8142 			for (i = 0; i < pps->num_tile_rows - 1; i++) {
8143 				pps->row_height[i] = 1 + gf_bs_get_ue(bs);
8144 			}
8145 		}
8146 		pps->loop_filter_across_tiles_enabled_flag = gf_bs_read_int(bs, 1);
8147 	}
8148 	pps->loop_filter_across_slices_enabled_flag = gf_bs_read_int(bs, 1);
8149 	if ((pps->deblocking_filter_control_present_flag = gf_bs_read_int(bs, 1))) {
8150 		pps->deblocking_filter_override_enabled_flag = gf_bs_read_int(bs, 1);
8151 		if (! (pps->pic_disable_deblocking_filter_flag = gf_bs_read_int(bs, 1))) {
8152 			pps->beta_offset_div2 = gf_bs_get_se(bs);
8153 			pps->tc_offset_div2 = gf_bs_get_se(bs);
8154 		}
8155 	}
8156 	if ((pps->pic_scaling_list_data_present_flag = gf_bs_read_int(bs, 1))) {
8157 		hevc_scaling_list_data(bs);
8158 	}
8159 	pps->lists_modification_present_flag = gf_bs_read_int(bs, 1);
8160 	pps->log2_parallel_merge_level_minus2 = gf_bs_get_ue(bs);
8161 	pps->slice_segment_header_extension_present_flag = gf_bs_read_int(bs, 1);
8162 	if ( /*pps_extension_flag= */gf_bs_read_int(bs, 1)) {
8163 #if 0
8164 		while (gf_bs_available(bs)) {
8165 			/*pps_extension_data_flag */ gf_bs_read_int(bs, 1);
8166 		}
8167 #endif
8168 
8169 	}
8170 	return pps_id;
8171 }
8172 
8173 
8174 GF_EXPORT
gf_media_hevc_read_pps(u8 * data,u32 size,HEVCState * hevc)8175 s32 gf_media_hevc_read_pps(u8 *data, u32 size, HEVCState *hevc)
8176 {
8177 	GF_BitStream *bs;
8178 	s32 pps_id = -1;
8179 
8180 	bs = gf_bs_new(data, size, GF_BITSTREAM_READ);
8181 	if (!bs) goto exit;
8182 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
8183 
8184 	if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) goto exit;
8185 
8186 	pps_id = gf_media_hevc_read_pps_bs_internal(bs, hevc);
8187 
8188 exit:
8189 	if (bs) gf_bs_del(bs);
8190 	return pps_id;
8191 }
8192 
8193 GF_EXPORT
gf_media_hevc_read_pps_bs(GF_BitStream * bs,HEVCState * hevc)8194 s32 gf_media_hevc_read_pps_bs(GF_BitStream *bs, HEVCState *hevc)
8195 {
8196 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
8197 	if (!hevc_parse_nal_header(bs, NULL, NULL, NULL)) return -1;
8198 	return gf_media_hevc_read_pps_bs_internal(bs, hevc);
8199 }
8200 
8201 GF_EXPORT
gf_media_hevc_parse_nalu_bs(GF_BitStream * bs,HEVCState * hevc,u8 * nal_unit_type,u8 * temporal_id,u8 * layer_id)8202 s32 gf_media_hevc_parse_nalu_bs(GF_BitStream *bs, HEVCState *hevc, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id)
8203 {
8204 	Bool is_slice = GF_FALSE;
8205 	s32 ret = -1;
8206 	HEVCSliceInfo n_state;
8207 
8208 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
8209 
8210 	memcpy(&n_state, &hevc->s_info, sizeof(HEVCSliceInfo));
8211 	if (!hevc_parse_nal_header(bs, nal_unit_type, temporal_id, layer_id)) return -1;
8212 
8213 	n_state.nal_unit_type = *nal_unit_type;
8214 
8215 	switch (n_state.nal_unit_type) {
8216 	case GF_HEVC_NALU_ACCESS_UNIT:
8217 	case GF_HEVC_NALU_END_OF_SEQ:
8218 	case GF_HEVC_NALU_END_OF_STREAM:
8219 		ret = 1;
8220 		break;
8221 
8222 		/*slice_segment_layer_rbsp*/
8223 	case GF_HEVC_NALU_SLICE_TRAIL_N:
8224 	case GF_HEVC_NALU_SLICE_TRAIL_R:
8225 	case GF_HEVC_NALU_SLICE_TSA_N:
8226 	case GF_HEVC_NALU_SLICE_TSA_R:
8227 	case GF_HEVC_NALU_SLICE_STSA_N:
8228 	case GF_HEVC_NALU_SLICE_STSA_R:
8229 	case GF_HEVC_NALU_SLICE_BLA_W_LP:
8230 	case GF_HEVC_NALU_SLICE_BLA_W_DLP:
8231 	case GF_HEVC_NALU_SLICE_BLA_N_LP:
8232 	case GF_HEVC_NALU_SLICE_IDR_W_DLP:
8233 	case GF_HEVC_NALU_SLICE_IDR_N_LP:
8234 	case GF_HEVC_NALU_SLICE_CRA:
8235 	case GF_HEVC_NALU_SLICE_RADL_N:
8236 	case GF_HEVC_NALU_SLICE_RADL_R:
8237 	case GF_HEVC_NALU_SLICE_RASL_N:
8238 	case GF_HEVC_NALU_SLICE_RASL_R:
8239 		is_slice = GF_TRUE;
8240 		/* slice - read the info and compare.*/
8241 		ret = hevc_parse_slice_segment(bs, hevc, &n_state);
8242 		if (ret < 0) return ret;
8243 
8244 		hevc_compute_poc(&n_state);
8245 
8246 		ret = 0;
8247 
8248 		if (hevc->s_info.poc != n_state.poc) {
8249 			ret = 1;
8250 			break;
8251 		}
8252 		if (n_state.first_slice_segment_in_pic_flag) {
8253 			if (!(*layer_id) || (n_state.prev_layer_id_plus1 && ((*layer_id) <= n_state.prev_layer_id_plus1 - 1))) {
8254 				ret = 1;
8255 				break;
8256 			}
8257 		}
8258 		break;
8259 	case GF_HEVC_NALU_SEQ_PARAM:
8260 		hevc->last_parsed_sps_id = gf_media_hevc_read_sps_bs_internal(bs, hevc, *layer_id, NULL);
8261 		ret = (hevc->last_parsed_sps_id>=0) ? 0 : -1;
8262 		break;
8263 	case GF_HEVC_NALU_PIC_PARAM:
8264 		hevc->last_parsed_pps_id = gf_media_hevc_read_pps_bs_internal(bs, hevc);
8265 		ret = (hevc->last_parsed_pps_id>=0) ? 0 : -1;
8266 		break;
8267 	case GF_HEVC_NALU_VID_PARAM:
8268 		hevc->last_parsed_vps_id = gf_media_hevc_read_vps_bs_internal(bs, hevc, GF_FALSE);
8269 		ret = (hevc->last_parsed_vps_id>=0) ? 0 : -1;
8270 		break;
8271 	default:
8272 		ret = 0;
8273 		break;
8274 	}
8275 
8276 	/* save _prev values */
8277 	if ((ret>0) && hevc->s_info.sps) {
8278 		n_state.frame_num_offset_prev = hevc->s_info.frame_num_offset;
8279 		n_state.frame_num_prev = hevc->s_info.frame_num;
8280 
8281 		n_state.poc_lsb_prev = hevc->s_info.poc_lsb;
8282 		n_state.poc_msb_prev = hevc->s_info.poc_msb;
8283 		if (is_slice)
8284 			n_state.prev_layer_id_plus1 = *layer_id + 1;
8285 	}
8286 	if (is_slice) hevc_compute_poc(&n_state);
8287 	memcpy(&hevc->s_info, &n_state, sizeof(HEVCSliceInfo));
8288 
8289 	return ret;
8290 }
8291 
8292 GF_EXPORT
gf_media_hevc_parse_nalu(u8 * data,u32 size,HEVCState * hevc,u8 * nal_unit_type,u8 * temporal_id,u8 * layer_id)8293 s32 gf_media_hevc_parse_nalu(u8 *data, u32 size, HEVCState *hevc, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id)
8294 {
8295 	GF_BitStream *bs = NULL;
8296 	s32 ret = -1;
8297 
8298 	if (!hevc) {
8299 		if (nal_unit_type) (*nal_unit_type) = (data[0] & 0x7E) >> 1;
8300 		if (layer_id) {
8301 			u8 id = data[0] & 1;
8302 			id <<= 5;
8303 			id |= (data[1] >> 3) & 0x1F;
8304 			(*layer_id) = id;
8305 		}
8306 		if (temporal_id) (*temporal_id) = (data[1] & 0x7);
8307 		return -1;
8308 	}
8309 
8310 	bs = gf_bs_new(data, size, GF_BITSTREAM_READ);
8311 	if (!bs) return -1;
8312 	gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
8313 
8314 	ret = gf_media_hevc_parse_nalu_bs(bs, hevc, nal_unit_type, temporal_id, layer_id);
8315 
8316 	gf_bs_del(bs);
8317 	return ret;
8318 }
8319 
hevc_get_sar_idx(u32 w,u32 h)8320 static u8 hevc_get_sar_idx(u32 w, u32 h)
8321 {
8322 	u32 i;
8323 	for (i = 0; i < 14; i++) {
8324 		if ((avc_sar[i].w == w) && (avc_sar[i].h == h)) return i;
8325 	}
8326 	return 0xFF;
8327 }
8328 
gf_media_hevc_change_par(GF_HEVCConfig * hvcc,s32 ar_n,s32 ar_d)8329 GF_Err gf_media_hevc_change_par(GF_HEVCConfig *hvcc, s32 ar_n, s32 ar_d)
8330 {
8331 	GF_BitStream *orig, *mod;
8332 	HEVCState hevc;
8333 	u32 i, bit_offset, flag;
8334 	s32 idx;
8335 	GF_HEVCParamArray *spss;
8336 	GF_AVCConfigSlot *slc;
8337 	orig = NULL;
8338 
8339 	memset(&hevc, 0, sizeof(HEVCState));
8340 	hevc.sps_active_idx = -1;
8341 
8342 	i = 0;
8343 	spss = NULL;
8344 	while ((spss = (GF_HEVCParamArray *)gf_list_enum(hvcc->param_array, &i))) {
8345 		if (spss->type == GF_HEVC_NALU_SEQ_PARAM)
8346 			break;
8347 		spss = NULL;
8348 	}
8349 	if (!spss) return GF_NON_COMPLIANT_BITSTREAM;
8350 
8351 	i = 0;
8352 	while ((slc = (GF_AVCConfigSlot *)gf_list_enum(spss->nalus, &i))) {
8353 		u8 *no_emulation_buf;
8354 		u32 no_emulation_buf_size, emulation_bytes;
8355 
8356 		/*SPS may still contains emulation bytes*/
8357 		no_emulation_buf = gf_malloc((slc->size) * sizeof(char));
8358 		no_emulation_buf_size = gf_media_nalu_remove_emulation_bytes(slc->data, no_emulation_buf, slc->size);
8359 
8360 		idx = gf_media_hevc_read_sps_ex(no_emulation_buf, no_emulation_buf_size, &hevc, &bit_offset);
8361 		if (idx < 0) {
8362 			if (orig)
8363 				gf_bs_del(orig);
8364 			gf_free(no_emulation_buf);
8365 			continue;
8366 		}
8367 
8368 		orig = gf_bs_new(no_emulation_buf, no_emulation_buf_size, GF_BITSTREAM_READ);
8369 		mod = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
8370 
8371 		/*copy over till vui flag*/
8372 		assert(bit_offset >= 0);
8373 		while (bit_offset) {
8374 			flag = gf_bs_read_int(orig, 1);
8375 			gf_bs_write_int(mod, flag, 1);
8376 			bit_offset--;
8377 		}
8378 
8379 		/*check VUI*/
8380 		flag = gf_bs_read_int(orig, 1);
8381 		gf_bs_write_int(mod, 1, 1); /*vui_parameters_present_flag*/
8382 		if (flag) {
8383 			/*aspect_ratio_info_present_flag*/
8384 			if (gf_bs_read_int(orig, 1)) {
8385 				s32 aspect_ratio_idc = gf_bs_read_int(orig, 8);
8386 				if (aspect_ratio_idc == 255) {
8387 					gf_bs_read_int(orig, 16); /*AR num*/
8388 					gf_bs_read_int(orig, 16); /*AR den*/
8389 				}
8390 			}
8391 		}
8392 		if ((ar_d < 0) || (ar_n < 0)) {
8393 			/*no AR signaled*/
8394 			gf_bs_write_int(mod, 0, 1);
8395 		}
8396 		else {
8397 			u32 sarx;
8398 			gf_bs_write_int(mod, 1, 1);
8399 			sarx = hevc_get_sar_idx((u32)ar_n, (u32)ar_d);
8400 			gf_bs_write_int(mod, sarx, 8);
8401 			if (sarx == 0xFF) {
8402 				gf_bs_write_int(mod, ar_n, 16);
8403 				gf_bs_write_int(mod, ar_d, 16);
8404 			}
8405 		}
8406 		/*no VUI in input bitstream, set all vui flags to 0*/
8407 		if (!flag) {
8408 			gf_bs_write_int(mod, 0, 1);		/*overscan_info_present_flag */
8409 			gf_bs_write_int(mod, 0, 1);		/*video_signal_type_present_flag */
8410 			gf_bs_write_int(mod, 0, 1);		/*chroma_location_info_present_flag */
8411 
8412 			gf_bs_write_int(mod, 0, 1); /*neutra_chroma_indication_flag */;
8413 			gf_bs_write_int(mod, 0, 1); /*field_seq_flag */;
8414 			gf_bs_write_int(mod, 0, 1); /*frame_field_info_present_flag*/;
8415 			gf_bs_write_int(mod, 0, 1); /*default_display_window_flag*/;
8416 
8417 			gf_bs_write_int(mod, 0, 1);		/*timing_info_present_flag*/
8418 			gf_bs_write_int(mod, 0, 1);		/*bitstream_restriction*/
8419 		}
8420 
8421 		/*finally copy over remaining*/
8422 		while (gf_bs_bits_available(orig)) {
8423 			flag = gf_bs_read_int(orig, 1);
8424 			gf_bs_write_int(mod, flag, 1);
8425 		}
8426 		gf_bs_del(orig);
8427 		orig = NULL;
8428 		gf_free(no_emulation_buf);
8429 
8430 		/*set anti-emulation*/
8431 		gf_bs_get_content(mod, &no_emulation_buf, &no_emulation_buf_size);
8432 		emulation_bytes = gf_media_nalu_emulation_bytes_add_count(no_emulation_buf, no_emulation_buf_size);
8433 		if (no_emulation_buf_size + emulation_bytes > slc->size)
8434 			slc->data = (char*)gf_realloc(slc->data, no_emulation_buf_size + emulation_bytes);
8435 
8436 		slc->size = gf_media_nalu_add_emulation_bytes(no_emulation_buf, slc->data, no_emulation_buf_size);
8437 
8438 		gf_bs_del(mod);
8439 		gf_free(no_emulation_buf);
8440 	}
8441 	return GF_OK;
8442 }
8443 
8444 GF_EXPORT
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)8445 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)
8446 {
8447 	s32 idx;
8448 	idx = gf_media_hevc_read_sps(sps_data, sps_size, hevc);
8449 	if (idx < 0) {
8450 		return GF_NON_COMPLIANT_BITSTREAM;
8451 	}
8452 	if (sps_id) *sps_id = idx;
8453 
8454 	if (width) *width = hevc->sps[idx].width;
8455 	if (height) *height = hevc->sps[idx].height;
8456 	if (par_n) *par_n = hevc->sps[idx].aspect_ratio_info_present_flag ? hevc->sps[idx].sar_width : (u32)-1;
8457 	if (par_d) *par_d = hevc->sps[idx].aspect_ratio_info_present_flag ? hevc->sps[idx].sar_height : (u32)-1;
8458 	return GF_OK;
8459 }
8460 
8461 GF_EXPORT
gf_hevc_get_sps_info(u8 * sps_data,u32 sps_size,u32 * sps_id,u32 * width,u32 * height,s32 * par_n,s32 * par_d)8462 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)
8463 {
8464 	HEVCState hevc;
8465 	memset(&hevc, 0, sizeof(HEVCState));
8466 	hevc.sps_active_idx = -1;
8467 	return gf_hevc_get_sps_info_with_state(&hevc, sps_data, sps_size, sps_id, width, height, par_n, par_d);
8468 }
8469 
8470 
8471 #endif //GPAC_DISABLE_HEVC
8472 
AC3_FindSyncCode(u8 * buf,u32 buflen)8473 static u32 AC3_FindSyncCode(u8 *buf, u32 buflen)
8474 {
8475 	u32 end = buflen - 6;
8476 	u32 offset = 0;
8477 	while (offset <= end) {
8478 		if (buf[offset] == 0x0b && buf[offset + 1] == 0x77) {
8479 			return offset;
8480 		}
8481 		offset++;
8482 	}
8483 	return buflen;
8484 }
8485 
8486 
AC3_FindSyncCodeBS(GF_BitStream * bs)8487 static Bool AC3_FindSyncCodeBS(GF_BitStream *bs)
8488 {
8489 	u8 b1;
8490 	u64 pos = gf_bs_get_position(bs);
8491 	u64 end = gf_bs_get_size(bs);
8492 
8493 	pos += 1;
8494 	b1 = gf_bs_read_u8(bs);
8495 	while (pos + 1 <= end) {
8496 		u8 b2 = gf_bs_read_u8(bs);
8497 		if ((b1 == 0x0b) && (b2 == 0x77)) {
8498 			gf_bs_seek(bs, pos - 1);
8499 			return GF_TRUE;
8500 		}
8501 		pos++;
8502 		b1 = b2;
8503 	}
8504 	return GF_FALSE;
8505 }
8506 
8507 static const u32 ac3_sizecod_to_bitrate[] = {
8508 	32000, 40000, 48000, 56000, 64000, 80000, 96000,
8509 	112000, 128000, 160000, 192000, 224000, 256000,
8510 	320000, 384000, 448000, 512000, 576000, 640000
8511 };
8512 
8513 static const u32 ac3_sizecod2_to_framesize[] = {
8514 	96, 120, 144, 168, 192, 240, 288, 336, 384, 480, 576, 672,
8515 	768, 960, 1152, 1344, 1536, 1728, 1920
8516 };
8517 
8518 static const u32 ac3_sizecod1_to_framesize[] = {
8519 	69, 87, 104, 121, 139, 174, 208, 243, 278, 348, 417, 487,
8520 	557, 696, 835, 975, 1114, 1253, 1393
8521 };
8522 static const u32 ac3_sizecod0_to_framesize[] = {
8523 	64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448,
8524 	512, 640, 768, 896, 1024, 1152, 1280
8525 };
8526 
8527 static const u32 ac3_mod_to_chans[] = {
8528 	2, 1, 2, 3, 3, 4, 4, 5
8529 };
8530 
8531 GF_EXPORT
gf_ac3_get_channels(u32 acmod)8532 u32 gf_ac3_get_channels(u32 acmod)
8533 {
8534 	u32 nb_ch;
8535 	nb_ch = ac3_mod_to_chans[acmod];
8536 	return nb_ch;
8537 }
8538 
8539 GF_EXPORT
gf_ac3_get_bitrate(u32 brcode)8540 u32 gf_ac3_get_bitrate(u32 brcode)
8541 {
8542 	return ac3_sizecod_to_bitrate[brcode];
8543 }
8544 
gf_ac3_parser(u8 * buf,u32 buflen,u32 * pos,GF_AC3Header * hdr,Bool full_parse)8545 Bool gf_ac3_parser(u8 *buf, u32 buflen, u32 *pos, GF_AC3Header *hdr, Bool full_parse)
8546 {
8547 	GF_BitStream *bs;
8548 	Bool ret;
8549 
8550 	if (buflen < 6) return GF_FALSE;
8551 	(*pos) = AC3_FindSyncCode(buf, buflen);
8552 	if (*pos >= buflen) return GF_FALSE;
8553 
8554 	bs = gf_bs_new((const char*)(buf + *pos), buflen, GF_BITSTREAM_READ);
8555 	ret = gf_ac3_parser_bs(bs, hdr, full_parse);
8556 	gf_bs_del(bs);
8557 
8558 	return ret;
8559 }
8560 
8561 GF_EXPORT
gf_ac3_parser_bs(GF_BitStream * bs,GF_AC3Header * hdr,Bool full_parse)8562 Bool gf_ac3_parser_bs(GF_BitStream *bs, GF_AC3Header *hdr, Bool full_parse)
8563 {
8564 	u32 fscod, frmsizecod, bsid, ac3_mod, freq, framesize, bsmod, syncword;
8565 	u64 pos;
8566 	if (!hdr || (gf_bs_available(bs) < 6)) return GF_FALSE;
8567 	if (!AC3_FindSyncCodeBS(bs)) return GF_FALSE;
8568 
8569 	pos = gf_bs_get_position(bs);
8570 
8571 	syncword = gf_bs_read_u16(bs);
8572 	if (syncword != 0x0B77) {
8573 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[AC3] Wrong sync word detected (0x%X - expecting 0x0B77).\n", syncword));
8574 		return GF_FALSE;
8575 	}
8576 	gf_bs_read_u16(bs); //crc1
8577 	fscod = gf_bs_read_int(bs, 2);
8578 	frmsizecod = gf_bs_read_int(bs, 6);
8579 	bsid = gf_bs_read_int(bs, 5);
8580 	bsmod = gf_bs_read_int(bs, 3);
8581 	ac3_mod = gf_bs_read_int(bs, 3);
8582 	if (frmsizecod >= 2 * sizeof(ac3_sizecod_to_bitrate) / sizeof(u32))
8583 		return GF_FALSE;
8584 
8585 	hdr->bitrate = ac3_sizecod_to_bitrate[frmsizecod / 2];
8586 	if (bsid > 8) hdr->bitrate = hdr->bitrate >> (bsid - 8);
8587 
8588 	switch (fscod) {
8589 	case 0:
8590 		if (frmsizecod >=  2 * sizeof(ac3_sizecod0_to_framesize) / sizeof(u32))
8591 			return GF_FALSE;
8592 		freq = 48000;
8593 		framesize = ac3_sizecod0_to_framesize[frmsizecod / 2] * 2;
8594 		break;
8595 	case 1:
8596 		if (frmsizecod >= 2 * sizeof(ac3_sizecod1_to_framesize) / sizeof(u32))
8597 			return GF_FALSE;
8598 		freq = 44100;
8599 		framesize = (ac3_sizecod1_to_framesize[frmsizecod / 2] + (frmsizecod & 0x1)) * 2;
8600 		break;
8601 	case 2:
8602 		if (frmsizecod >= 2 * sizeof(ac3_sizecod2_to_framesize) / sizeof(u32))
8603 			return GF_FALSE;
8604 		freq = 32000;
8605 		framesize = ac3_sizecod2_to_framesize[frmsizecod / 2] * 2;
8606 		break;
8607 	default:
8608 		return GF_FALSE;
8609 	}
8610 	hdr->sample_rate = freq;
8611 	hdr->framesize = framesize;
8612 
8613 	if (full_parse) {
8614 		hdr->streams[0].bsid = bsid;
8615 		hdr->streams[0].bsmod = bsmod;
8616 		hdr->streams[0].acmod = ac3_mod;
8617 		hdr->streams[0].lfon = 0;
8618 		hdr->streams[0].fscod = fscod;
8619 		hdr->streams[0].brcode = frmsizecod / 2;
8620 	}
8621 	if (ac3_mod >= 2 * sizeof(ac3_mod_to_chans) / sizeof(u32))
8622 		return GF_FALSE;
8623 
8624 	hdr->channels = ac3_mod_to_chans[ac3_mod];
8625 	if ((ac3_mod & 0x1) && (ac3_mod != 1)) gf_bs_read_int(bs, 2);
8626 	if (ac3_mod & 0x4) gf_bs_read_int(bs, 2);
8627 	if (ac3_mod == 0x2) gf_bs_read_int(bs, 2);
8628 	/*LFEon*/
8629 	if (gf_bs_read_int(bs, 1)) {
8630 		hdr->channels += 1;
8631 		hdr->streams[0].lfon = 1;
8632 	}
8633 
8634 	gf_bs_seek(bs, pos);
8635 
8636 	return GF_TRUE;
8637 }
8638 
8639 GF_EXPORT
gf_eac3_parser_bs(GF_BitStream * bs,GF_AC3Header * hdr,Bool full_parse)8640 Bool gf_eac3_parser_bs(GF_BitStream *bs, GF_AC3Header *hdr, Bool full_parse)
8641 {
8642 	u32 fscod, bsid, ac3_mod, freq, framesize, syncword, substreamid, lfon, channels, numblkscod, strmtyp, frmsiz;
8643 	u64 pos;
8644 	u16 chanmap;
8645 	static u32 numblks[4] = {1, 2, 3, 6};
8646 
8647 	if (!hdr || (gf_bs_available(bs) < 6))
8648 		return GF_FALSE;
8649 	if (!AC3_FindSyncCodeBS(bs))
8650 		return GF_FALSE;
8651 
8652 	pos = gf_bs_get_position(bs);
8653 	framesize = 0;
8654 	numblkscod = 0;
8655 	memset(hdr, 0, sizeof(GF_AC3Header));
8656 
8657 block:
8658 	syncword = gf_bs_read_u16(bs);
8659 	if (syncword != 0x0B77) {
8660 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[E-AC3] Wrong sync word detected (0x%X - expecting 0x0B77).\n", syncword));
8661 		return GF_FALSE;
8662 	}
8663 
8664 	strmtyp = gf_bs_read_int(bs, 2);
8665 	substreamid = gf_bs_read_int(bs, 3);
8666 	//next main (independent) AU, done with this frame
8667 	if ((strmtyp!=0x1) && ((hdr->substreams >> substreamid) & 0x1)) {
8668 		hdr->framesize = framesize;
8669 		gf_bs_seek(bs, pos);
8670 		return GF_TRUE;
8671 	}
8672 
8673 	frmsiz = gf_bs_read_int(bs, 11);
8674 	framesize += 2 * (1 + frmsiz);
8675 	fscod = gf_bs_read_int(bs, 2);
8676 	if (fscod == 0x3) {
8677 		fscod = gf_bs_read_int(bs, 2);
8678 		numblkscod += 6;
8679 	}
8680 	else {
8681 		numblkscod += gf_bs_read_int(bs, 2);
8682 	}
8683 	assert(numblkscod <= 9);
8684 
8685 
8686 	if ((hdr->substreams >> substreamid) & 0x1) {
8687 		//we still have sync frames following
8688 		if (substreamid) {
8689 			if (gf_bs_seek(bs, pos + framesize) != GF_OK) {
8690 				gf_bs_seek(bs, pos);
8691 				return GF_FALSE;
8692 			}
8693 			if ((gf_bs_available(bs) < 6) || !AC3_FindSyncCodeBS(bs)) {
8694 				gf_bs_seek(bs, pos);
8695 				return GF_FALSE;
8696 			}
8697 			goto block;
8698 		}
8699 	}
8700 
8701 	hdr->substreams |= (1 << substreamid);
8702 
8703 	switch (fscod) {
8704 	case 0:
8705 		freq = 48000;
8706 		break;
8707 	case 1:
8708 		freq = 44100;
8709 		break;
8710 	case 2:
8711 		freq = 32000;
8712 		break;
8713 	default:
8714 		return GF_FALSE;
8715 	}
8716 
8717 	ac3_mod = gf_bs_read_int(bs, 3);
8718 	lfon = gf_bs_read_int(bs, 1);
8719 	bsid = gf_bs_read_int(bs, 5);
8720 	if (!substreamid && (bsid != 16/*E-AC3*/))
8721 		return GF_FALSE;
8722 	/*dialnorm=*/gf_bs_read_int(bs, 5);
8723 	if (/*compre=*/gf_bs_read_int(bs, 1)) {
8724 		/*compr=*/gf_bs_read_int(bs, 8);
8725 	}
8726 	if (ac3_mod==0) {
8727 		/*dialnorm2=*/gf_bs_read_int(bs, 5);
8728 		if (/*compr2e=*/gf_bs_read_int(bs, 1)) {
8729 			/*compr2=*/gf_bs_read_int(bs, 8);
8730 		}
8731 	}
8732 	chanmap = 0;
8733 	if (strmtyp==0x1) {
8734 		if (/*chanmape=*/gf_bs_read_int(bs, 1)) {
8735 			chanmap = gf_bs_read_int(bs, 16);
8736 		}
8737 	}
8738 
8739 	channels = ac3_mod_to_chans[ac3_mod];
8740 	if (lfon)
8741 		channels += 1;
8742 
8743 	hdr->bitrate = 0;
8744 	hdr->sample_rate = freq;
8745 	hdr->framesize = framesize;
8746 	if (strmtyp != 1) {
8747 		hdr->channels = channels;
8748 		hdr->streams[substreamid].lfon = lfon;
8749 		if (full_parse) {
8750 			hdr->streams[substreamid].bsid = bsid;
8751 			hdr->streams[substreamid].bsmod = 0;
8752 			hdr->streams[substreamid].acmod = ac3_mod;
8753 			hdr->streams[substreamid].fscod = fscod;
8754 			hdr->streams[substreamid].brcode = 0;
8755 		}
8756 		hdr->nb_streams++;
8757 		//not clear if this is only for the independent streams
8758 		hdr->data_rate += ((frmsiz+1) * freq) / (numblks[numblkscod]*16) / 1000;
8759 
8760 		if (lfon)
8761 			hdr->channels += 1;
8762 
8763 	} else {
8764 		hdr->streams[substreamid].num_dep_sub = substreamid;
8765 		hdr->streams[substreamid].chan_loc |= chanmap;
8766 	}
8767 
8768 	if (numblkscod < 6) { //we need 6 blocks to make a sample
8769 		if (gf_bs_seek(bs, pos + framesize) != GF_OK) {
8770 			gf_bs_seek(bs, pos);
8771 			return GF_FALSE;
8772 		}
8773 
8774 		if ((gf_bs_available(bs) < 6) || !AC3_FindSyncCodeBS(bs))
8775 			return GF_FALSE;
8776 		goto block;
8777 	}
8778 
8779 	gf_bs_seek(bs, pos);
8780 
8781 	return GF_TRUE;
8782 }
8783 
8784 #endif /*GPAC_DISABLE_AV_PARSERS*/
8785 
gf_id3_read_size(GF_BitStream * bs)8786 u32 gf_id3_read_size(GF_BitStream *bs)
8787 {
8788 	u32 size = 0;
8789 	gf_bs_read_int(bs, 1);
8790 	size |= gf_bs_read_int(bs, 7);
8791 	size<<=7;
8792 	gf_bs_read_int(bs, 1);
8793 	size |= gf_bs_read_int(bs, 7);
8794 	size<<=7;
8795 	gf_bs_read_int(bs, 1);
8796 	size |= gf_bs_read_int(bs, 7);
8797 	size<<=7;
8798 	gf_bs_read_int(bs, 1);
8799 	size |= gf_bs_read_int(bs, 7);
8800 	return size;
8801 }
8802 
8803 
8804 #if !defined(GPAC_DISABLE_AV_PARSERS) && !defined (GPAC_DISABLE_OGG)
8805 
8806 /*
8807 	Vorbis parser
8808 */
8809 
vorbis_book_maptype1_quantvals(u32 entries,u32 dim)8810 static u32 vorbis_book_maptype1_quantvals(u32 entries, u32 dim)
8811 {
8812 	u32 vals = (u32)floor(pow(entries, 1.0 / dim));
8813 	while (1) {
8814 		u32 acc = 1;
8815 		u32 acc1 = 1;
8816 		u32 i;
8817 		for (i = 0; i < dim; i++) {
8818 			acc *= vals;
8819 			acc1 *= vals + 1;
8820 		}
8821 		if (acc <= entries && acc1 > entries) return (vals);
8822 		else {
8823 			if (acc > entries) vals--;
8824 			else vals++;
8825 		}
8826 	}
8827 }
8828 
ilog(u32 v,Bool dec)8829 static u32 ilog(u32 v, Bool dec)
8830 {
8831 	u32 ret = 0;
8832 	if (dec && v) --v;
8833 	while (v) {
8834 		ret++;
8835 		v >>= 1;
8836 	}
8837 	return (ret);
8838 }
8839 
icount(u32 v)8840 static u32 icount(u32 v)
8841 {
8842 	u32 ret = 0;
8843 	while (v) {
8844 		ret += v & 1;
8845 		v >>= 1;
8846 	}
8847 	return(ret);
8848 }
8849 
8850 
8851 GF_EXPORT
gf_vorbis_parse_header(GF_VorbisParser * vp,u8 * data,u32 data_len)8852 Bool gf_vorbis_parse_header(GF_VorbisParser *vp, u8 *data, u32 data_len)
8853 {
8854 	u32 pack_type, i, j, k, times, nb_part, nb_books, nb_modes;
8855 	u32 l;
8856 	char szNAME[8];
8857 	oggpack_buffer opb;
8858 
8859 	oggpack_readinit(&opb, (u8*)data, data_len);
8860 	pack_type = oggpack_read(&opb, 8);
8861 	i = 0;
8862 	while (i < 6) {
8863 		szNAME[i] = oggpack_read(&opb, 8);
8864 		i++;
8865 	}
8866 	szNAME[i] = 0;
8867 	if (strcmp(szNAME, "vorbis")) {
8868 		return GF_FALSE;
8869 	}
8870 
8871 	switch (pack_type) {
8872 	case 0x01:
8873 		vp->version = oggpack_read(&opb, 32);
8874 		if (vp->version != 0) {
8875 			return GF_FALSE;
8876 		}
8877 		vp->channels = oggpack_read(&opb, 8);
8878 		vp->sample_rate = oggpack_read(&opb, 32);
8879 		vp->max_r = oggpack_read(&opb, 32);
8880 		vp->avg_r = oggpack_read(&opb, 32);
8881 		vp->low_r = oggpack_read(&opb, 32);
8882 
8883 		vp->min_block = 1<<oggpack_read(&opb, 4);
8884 		vp->max_block = 1<<oggpack_read(&opb, 4);
8885 		if (vp->sample_rate < 1 || vp->channels < 1 || vp->min_block < 8 || vp->max_block < vp->min_block
8886 		    || oggpack_read(&opb, 1) != 1) {
8887 			return GF_FALSE;
8888 		}
8889 		vp->nb_init=1;
8890 		return GF_TRUE;
8891 
8892 	case 0x03:
8893 		/*trash comments*/
8894 		vp->nb_init++;
8895 		return GF_TRUE;
8896 	case 0x05:
8897 		/*need at least bitstream header to make sure we're parsing the right thing*/
8898 		if (!vp->nb_init) return GF_FALSE;
8899 		break;
8900 	default:
8901 		return GF_FALSE;
8902 	}
8903 	/*OK parse codebook*/
8904 	nb_books = oggpack_read(&opb, 8) + 1;
8905 	/*skip vorbis static books*/
8906 	for (i = 0; i < nb_books; i++) {
8907 		u32 map_type, qb, qq;
8908 		u32 entries, dim;
8909 		oggpack_read(&opb, 24);
8910 		dim = oggpack_read(&opb, 16);
8911 		entries = oggpack_read(&opb, 24);
8912 		if ((s32)entries < 0) entries = 0;
8913 		if (oggpack_read(&opb, 1) == 0) {
8914 			if (oggpack_read(&opb, 1)) {
8915 				for (j = 0; j < entries; j++) {
8916 					if (oggpack_read(&opb, 1)) {
8917 						oggpack_read(&opb, 5);
8918 					}
8919 				}
8920 			}
8921 			else {
8922 				for (j = 0; j < entries; j++)
8923 					oggpack_read(&opb, 5);
8924 			}
8925 		}
8926 		else {
8927 			oggpack_read(&opb, 5);
8928 			for (j = 0; j < entries;) {
8929 				u32 num = oggpack_read(&opb, ilog(entries - j, GF_FALSE));
8930 				for (k = 0; k < num && j < entries; k++, j++) {
8931 				}
8932 			}
8933 		}
8934 		switch ((map_type = oggpack_read(&opb, 4))) {
8935 		case 0:
8936 			break;
8937 		case 1:
8938 		case 2:
8939 			oggpack_read(&opb, 32);
8940 			oggpack_read(&opb, 32);
8941 			qq = oggpack_read(&opb, 4) + 1;
8942 			oggpack_read(&opb, 1);
8943 			if (map_type == 1) qb = vorbis_book_maptype1_quantvals(entries, dim);
8944 			else if (map_type == 2) qb = entries * dim;
8945 			else qb = 0;
8946 			for (j = 0; j < qb; j++) oggpack_read(&opb, qq);
8947 			break;
8948 		}
8949 	}
8950 	times = oggpack_read(&opb, 6) + 1;
8951 	for (i = 0; i < times; i++) oggpack_read(&opb, 16);
8952 	times = oggpack_read(&opb, 6) + 1;
8953 	for (i = 0; i < times; i++) {
8954 		u32 type = oggpack_read(&opb, 16);
8955 		if (type) {
8956 			u32 *parts, *class_dims, count, rangebits;
8957 			u32 max_class = 0;
8958 			nb_part = oggpack_read(&opb, 5);
8959 			parts = (u32*)gf_malloc(sizeof(u32) * nb_part);
8960 			for (j = 0; j < nb_part; j++) {
8961 				parts[j] = oggpack_read(&opb, 4);
8962 				if (max_class < parts[j]) max_class = parts[j];
8963 			}
8964 			class_dims = (u32*)gf_malloc(sizeof(u32) * (max_class + 1));
8965 			for (j = 0; j < max_class + 1; j++) {
8966 				u32 class_sub;
8967 				class_dims[j] = oggpack_read(&opb, 3) + 1;
8968 				class_sub = oggpack_read(&opb, 2);
8969 				if (class_sub) oggpack_read(&opb, 8);
8970 				for (k = 0; k < (u32)(1 << class_sub); k++) oggpack_read(&opb, 8);
8971 			}
8972 			oggpack_read(&opb, 2);
8973 			rangebits = oggpack_read(&opb, 4);
8974 			count = 0;
8975 			for (j = 0, k = 0; j < nb_part; j++) {
8976 				count += class_dims[parts[j]];
8977 				for (; k < count; k++) oggpack_read(&opb, rangebits);
8978 			}
8979 			gf_free(parts);
8980 			gf_free(class_dims);
8981 		}
8982 		else {
8983 			oggpack_read(&opb, 8 + 16 + 16 + 6 + 8);
8984 			nb_books = oggpack_read(&opb, 4) + 1;
8985 			for (j = 0; j < nb_books; j++)
8986 				oggpack_read(&opb, 8);
8987 		}
8988 	}
8989 	times = oggpack_read(&opb, 6) + 1;
8990 	for (i = 0; i < times; i++) {
8991 		u32 acc = 0;
8992 		oggpack_read(&opb, 16);/*type*/
8993 		oggpack_read(&opb, 24);
8994 		oggpack_read(&opb, 24);
8995 		oggpack_read(&opb, 24);
8996 		nb_part = oggpack_read(&opb, 6) + 1;
8997 		oggpack_read(&opb, 8);
8998 		for (j = 0; j < nb_part; j++) {
8999 			u32 cascade = oggpack_read(&opb, 3);
9000 			if (oggpack_read(&opb, 1)) cascade |= (oggpack_read(&opb, 5) << 3);
9001 			acc += icount(cascade);
9002 		}
9003 		for (j = 0; j < acc; j++) oggpack_read(&opb, 8);
9004 	}
9005 	times = oggpack_read(&opb, 6) + 1;
9006 	for (i = 0; i < times; i++) {
9007 		u32 sub_maps = 1;
9008 		oggpack_read(&opb, 16);
9009 		if (oggpack_read(&opb, 1)) sub_maps = oggpack_read(&opb, 4) + 1;
9010 		if (oggpack_read(&opb, 1)) {
9011 			u32 nb_steps = oggpack_read(&opb, 8) + 1;
9012 			for (j = 0; j < nb_steps; j++) {
9013 				oggpack_read(&opb, ilog(vp->channels, GF_TRUE));
9014 				oggpack_read(&opb, ilog(vp->channels, GF_TRUE));
9015 			}
9016 		}
9017 		oggpack_read(&opb, 2);
9018 		if (sub_maps>1) {
9019 			for(l=0; l<vp->channels; l++)
9020 				oggpack_read(&opb, 4);
9021 		}
9022 		for (j = 0; j < sub_maps; j++) {
9023 			oggpack_read(&opb, 8);
9024 			oggpack_read(&opb, 8);
9025 			oggpack_read(&opb, 8);
9026 		}
9027 	}
9028 	nb_modes = oggpack_read(&opb, 6) + 1;
9029 	for (i = 0; i < nb_modes; i++) {
9030 		vp->mode_flag[i] = oggpack_read(&opb, 1);
9031 		oggpack_read(&opb, 16);
9032 		oggpack_read(&opb, 16);
9033 		oggpack_read(&opb, 8);
9034 	}
9035 
9036 	vp->modebits = 0;
9037 	j = nb_modes;
9038 	while (j > 1) {
9039 		vp->modebits++;
9040 		j >>= 1;
9041 	}
9042 
9043 	return GF_TRUE;
9044 }
9045 
9046 GF_EXPORT
gf_vorbis_check_frame(GF_VorbisParser * vp,u8 * data,u32 data_length)9047 u32 gf_vorbis_check_frame(GF_VorbisParser *vp, u8 *data, u32 data_length)
9048 {
9049 	s32 block_size;
9050 	oggpack_buffer opb;
9051 	if (!vp) return 0;
9052 	oggpack_readinit(&opb, (unsigned char*)data, data_length);
9053 	/*not audio*/
9054 	if (oggpack_read(&opb, 1) != 0) return 0;
9055 	block_size = oggpack_read(&opb, vp->modebits);
9056 	if (block_size == -1) return 0;
9057 	return ((vp->mode_flag[block_size]) ? vp->max_block : vp->min_block) / (2);
9058 }
9059 
9060 /*call with vorbis header packets - initializes the parser on success, leave it to NULL otherwise
9061 returns 1 if success, 0 if error.*/
gf_opus_parse_header(GF_OpusParser * opus,u8 * data,u32 data_len)9062 Bool gf_opus_parse_header(GF_OpusParser *opus, u8 *data, u32 data_len)
9063 {
9064 	char tag[9];
9065 	GF_BitStream *bs = gf_bs_new(data, data_len, GF_BITSTREAM_READ);
9066 	gf_bs_read_data(bs, tag, 8);
9067 	tag[8]=0;
9068 
9069 	if (memcmp(data, "OpusHead", sizeof(char)*8)) {
9070 		gf_bs_del(bs);
9071 		return GF_FALSE;
9072 	}
9073 	/*Identification Header*/
9074 	opus->version = gf_bs_read_u8(bs); /*version*/
9075 	if (opus->version != 1) {
9076 		gf_bs_del(bs);
9077 		GF_LOG(GF_LOG_ERROR, GF_LOG_CODING, ("[Opus] Unsupported version %d\n", opus->version));
9078 		return GF_FALSE;
9079 	}
9080 	opus->OutputChannelCount = gf_bs_read_u8(bs);
9081 	opus->PreSkip = gf_bs_read_u16_le(bs);
9082 	opus->InputSampleRate = gf_bs_read_u32_le(bs);
9083 	opus->OutputGain = gf_bs_read_u16_le(bs);
9084 	opus->ChannelMappingFamily = gf_bs_read_u8(bs);
9085 	if (opus->ChannelMappingFamily != 0) {
9086 		opus->StreamCount = gf_bs_read_u8(bs);
9087 		opus->CoupledCount = gf_bs_read_u8(bs);
9088 		gf_bs_read_data(bs, (char *) opus->ChannelMapping, opus->OutputChannelCount);
9089 	}
9090 	gf_bs_del(bs);
9091 	return GF_TRUE;
9092 }
9093 
9094 /*returns 0 if init error or not a vorbis frame, otherwise returns the number of audio samples
9095 in this frame*/
gf_opus_check_frame(GF_OpusParser * op,u8 * data,u32 data_length)9096 u32 gf_opus_check_frame(GF_OpusParser *op, u8 *data, u32 data_length)
9097 {
9098 	u32 block_size;
9099 
9100 	if (!memcmp(data, "OpusHead", sizeof(char)*8))
9101 		return 0;
9102 	if (!memcmp(data, "OpusTags", sizeof(char)*8))
9103 		return 0;
9104 
9105 	/*consider the whole packet as Ogg packets and ISOBMFF samples for Opus are framed similarly*/
9106 	static const int OpusFrameDurIn48k[] = { 480, 960, 1920, 2880, 480, 960, 1920, 2880, 480, 960, 1920, 2880,
9107 		480, 960, 480, 960,
9108 		120, 240, 480, 960, 120, 240, 480, 960, 120, 240, 480, 960, 120, 240, 480, 960,
9109 	};
9110 	int TOC_config = (data[0] & 0xf8) >> 3;
9111 	//int s = (data[0] & 0x04) >> 2;
9112 	block_size = OpusFrameDurIn48k[TOC_config];
9113 
9114 	int c = data[0] & 0x03;
9115 	if (c == 1 || c == 2) {
9116 		block_size *= 2;
9117 	} else if (c == 3) {
9118 		/*unknown number of frames*/
9119 		int num_frames = data[1] & 0x3f;
9120 		block_size *= num_frames;
9121 	}
9122 	return block_size;
9123 }
9124 
9125 #endif /*!defined(GPAC_DISABLE_AV_PARSERS) && !defined (GPAC_DISABLE_OGG)*/
9126