• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2017 Amlogic, Inc. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Description:
19 */
20 
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/vmalloc.h>
24 #include <linux/mm.h>
25 #include <linux/string.h>
26 
27 #include "aml_mpeg4_parser.h"
28 #include "../utils/get_bits.h"
29 #include "../utils/put_bits.h"
30 #include "../utils/golomb.h"
31 #include "../utils/common.h"
32 #include "utils.h"
33 
34 const u8 ff_mpeg4_dc_threshold[8]={
35     99, 13, 15, 17, 19, 21, 23, 0
36 };
37 
38 /* these matrixes will be permuted for the idct */
39 const int16_t ff_mpeg4_default_intra_matrix[64] = {
40 	 8, 17, 18, 19, 21, 23, 25, 27,
41 	17, 18, 19, 21, 23, 25, 27, 28,
42 	20, 21, 22, 23, 24, 26, 28, 30,
43 	21, 22, 23, 24, 26, 28, 30, 32,
44 	22, 23, 24, 26, 28, 30, 32, 35,
45 	23, 24, 26, 28, 30, 32, 35, 38,
46 	25, 26, 28, 30, 32, 35, 38, 41,
47 	27, 28, 30, 32, 35, 38, 41, 45,
48 };
49 
50 const int16_t ff_mpeg4_default_non_intra_matrix[64] = {
51 	16, 17, 18, 19, 20, 21, 22, 23,
52 	17, 18, 19, 20, 21, 22, 23, 24,
53 	18, 19, 20, 21, 22, 23, 24, 25,
54 	19, 20, 21, 22, 23, 24, 26, 27,
55 	20, 21, 22, 23, 25, 26, 27, 28,
56 	21, 22, 23, 24, 26, 27, 28, 30,
57 	22, 23, 24, 26, 27, 28, 30, 31,
58 	23, 24, 25, 27, 28, 30, 31, 33,
59 };
60 
61 const struct AVRational ff_h263_pixel_aspect[16] = {
62 	{  0,  1 },
63 	{  1,  1 },
64 	{ 12, 11 },
65 	{ 10, 11 },
66 	{ 16, 11 },
67 	{ 40, 33 },
68 	{  0,  1 },
69 	{  0,  1 },
70 	{  0,  1 },
71 	{  0,  1 },
72 	{  0,  1 },
73 	{  0,  1 },
74 	{  0,  1 },
75 	{  0,  1 },
76 	{  0,  1 },
77 	{  0,  1 },
78 };
79 
80 /* As per spec, studio start code search isn't the same as the old type of start code */
next_start_code_studio(struct get_bits_context * gb)81 static void next_start_code_studio(struct get_bits_context *gb)
82 {
83 	align_get_bits(gb);
84 
85 	while (get_bits_left(gb) >= 24 && show_bits_long(gb, 24) != 0x1) {
86 		get_bits(gb, 8);
87 	}
88 }
89 
read_quant_matrix_ext(struct MpegEncContext * s,struct get_bits_context * gb)90 static int read_quant_matrix_ext(struct MpegEncContext *s, struct get_bits_context *gb)
91 {
92 	int i, /*j,*/ v;
93 
94 	if (get_bits1(gb)) {
95 		if (get_bits_left(gb) < 64*8)
96 			return -1;
97 		/* intra_quantiser_matrix */
98 		for (i = 0; i < 64; i++) {
99 			v = get_bits(gb, 8);
100 			//j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
101 			//s->intra_matrix[j]        = v;
102 			//s->chroma_intra_matrix[j] = v;
103 		}
104 	}
105 
106 	if (get_bits1(gb)) {
107 		if (get_bits_left(gb) < 64*8)
108 			return -1;
109 		/* non_intra_quantiser_matrix */
110 		for (i = 0; i < 64; i++) {
111 			get_bits(gb, 8);
112 		}
113 	}
114 
115 	if (get_bits1(gb)) {
116 		if (get_bits_left(gb) < 64*8)
117 			return -1;
118 		/* chroma_intra_quantiser_matrix */
119 		for (i = 0; i < 64; i++) {
120 			v = get_bits(gb, 8);
121 			//j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
122 			//s->chroma_intra_matrix[j] = v;
123 		}
124 	}
125 
126 	if (get_bits1(gb)) {
127 		if (get_bits_left(gb) < 64*8)
128 			return -1;
129 		/* chroma_non_intra_quantiser_matrix */
130 		for (i = 0; i < 64; i++) {
131 			get_bits(gb, 8);
132 		}
133 	}
134 
135 	next_start_code_studio(gb);
136 	return 0;
137 }
138 
extension_and_user_data(struct MpegEncContext * s,struct get_bits_context * gb,int id)139 static void extension_and_user_data(struct MpegEncContext *s, struct get_bits_context *gb, int id)
140 {
141 	u32 startcode;
142 	u8 extension_type;
143 
144 	startcode = show_bits_long(gb, 32);
145 	if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
146 		if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
147 			skip_bits_long(gb, 32);
148 			extension_type = get_bits(gb, 4);
149 			if (extension_type == QUANT_MATRIX_EXT_ID)
150 				read_quant_matrix_ext(s, gb);
151 		}
152 	}
153 }
154 
155 
decode_studio_vol_header(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)156 static int decode_studio_vol_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
157 {
158 	struct MpegEncContext *s = &ctx->m;
159 	int width, height;
160 	int bits_per_raw_sample;
161 
162 	// random_accessible_vol and video_object_type_indication have already
163 	// been read by the caller decode_vol_header()
164 	skip_bits(gb, 4); /* video_object_layer_verid */
165 	ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
166 	skip_bits(gb, 4); /* video_object_layer_shape_extension */
167 	skip_bits1(gb); /* progressive_sequence */
168 	if (ctx->shape != BIN_ONLY_SHAPE) {
169 		ctx->rgb = get_bits1(gb); /* rgb_components */
170 		s->chroma_format = get_bits(gb, 2); /* chroma_format */
171 		if (!s->chroma_format) {
172 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "illegal chroma format\n");
173 			return -1;
174 		}
175 
176 		bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
177 		if (bits_per_raw_sample == 10) {
178 			if (ctx->rgb) {
179 				ctx->pix_fmt = AV_PIX_FMT_GBRP10;
180 			} else {
181 				ctx->pix_fmt = s->chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
182 			}
183 		}
184 		else {
185 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
186 			return -1;
187 		}
188 		ctx->bits_per_raw_sample = bits_per_raw_sample;
189 	}
190 	if (ctx->shape == RECT_SHAPE) {
191 		check_marker(gb, "before video_object_layer_width");
192 		width = get_bits(gb, 14); /* video_object_layer_width */
193 		check_marker(gb, "before video_object_layer_height");
194 		height = get_bits(gb, 14); /* video_object_layer_height */
195 		check_marker(gb, "after video_object_layer_height");
196 
197 		/* Do the same check as non-studio profile */
198 		if (width && height) {
199 			if (s->width && s->height &&
200 				(s->width != width || s->height != height))
201 				s->context_reinit = 1;
202 			s->width  = width;
203 			s->height = height;
204 		}
205 	}
206 	s->aspect_ratio_info = get_bits(gb, 4);
207 	if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
208 		ctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
209 		ctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
210 	} else {
211 		ctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
212 	}
213 	skip_bits(gb, 4); /* frame_rate_code */
214 	skip_bits(gb, 15); /* first_half_bit_rate */
215 	check_marker(gb, "after first_half_bit_rate");
216 	skip_bits(gb, 15); /* latter_half_bit_rate */
217 	check_marker(gb, "after latter_half_bit_rate");
218 	skip_bits(gb, 15); /* first_half_vbv_buffer_size */
219 	check_marker(gb, "after first_half_vbv_buffer_size");
220 	skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
221 	skip_bits(gb, 11); /* first_half_vbv_buffer_size */
222 	check_marker(gb, "after first_half_vbv_buffer_size");
223 	skip_bits(gb, 15); /* latter_half_vbv_occupancy */
224 	check_marker(gb, "after latter_half_vbv_occupancy");
225 	s->low_delay = get_bits1(gb);
226 	s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
227 
228 	next_start_code_studio(gb);
229 	extension_and_user_data(s, gb, 2);
230 
231 	return 0;
232 }
233 
decode_vol_header(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)234 static int decode_vol_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
235 {
236 	struct MpegEncContext *s = &ctx->m;
237 	int width, height, vo_ver_id;
238 
239 	/* vol header */
240 	skip_bits(gb, 1);                   /* random access */
241 	s->vo_type = get_bits(gb, 8);
242 
243 	/* If we are in studio profile (per vo_type), check if its all consistent
244 	* and if so continue pass control to decode_studio_vol_header().
245 	* elIf something is inconsistent, error out
246 	* else continue with (non studio) vol header decpoding.
247 	*/
248 	if (s->vo_type == CORE_STUDIO_VO_TYPE ||
249 		s->vo_type == SIMPLE_STUDIO_VO_TYPE) {
250 		if (ctx->profile != FF_PROFILE_UNKNOWN && ctx->profile != FF_PROFILE_MPEG4_SIMPLE_STUDIO)
251 			return -1;
252 		s->studio_profile = 1;
253 		ctx->profile = FF_PROFILE_MPEG4_SIMPLE_STUDIO;
254 		return decode_studio_vol_header(ctx, gb);
255 	} else if (s->studio_profile) {
256 		return -1;
257 	}
258 
259 	if (get_bits1(gb) != 0) {           /* is_ol_id */
260 		vo_ver_id = get_bits(gb, 4);    /* vo_ver_id */
261 		skip_bits(gb, 3);               /* vo_priority */
262 	} else {
263 		vo_ver_id = 1;
264 	}
265 	s->aspect_ratio_info = get_bits(gb, 4);
266 	if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
267 		ctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
268 		ctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
269 	} else {
270 		ctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
271 	}
272 
273 	if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
274 		int chroma_format = get_bits(gb, 2);
275 		if (chroma_format != CHROMA_420)
276 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "illegal chroma format\n");
277 
278 		s->low_delay = get_bits1(gb);
279 		if (get_bits1(gb)) {    /* vbv parameters */
280 			get_bits(gb, 15);   /* first_half_bitrate */
281 			check_marker(gb, "after first_half_bitrate");
282 			get_bits(gb, 15);   /* latter_half_bitrate */
283 			check_marker(gb, "after latter_half_bitrate");
284 			get_bits(gb, 15);   /* first_half_vbv_buffer_size */
285 			check_marker(gb, "after first_half_vbv_buffer_size");
286 			get_bits(gb, 3);    /* latter_half_vbv_buffer_size */
287 			get_bits(gb, 11);   /* first_half_vbv_occupancy */
288 			check_marker(gb, "after first_half_vbv_occupancy");
289 			get_bits(gb, 15);   /* latter_half_vbv_occupancy */
290 			check_marker(gb, "after latter_half_vbv_occupancy");
291 		}
292 	} else {
293 		/* is setting low delay flag only once the smartest thing to do?
294 		* low delay detection will not be overridden. */
295 		if (s->picture_number == 0) {
296 			switch (s->vo_type) {
297 			case SIMPLE_VO_TYPE:
298 			case ADV_SIMPLE_VO_TYPE:
299 				s->low_delay = 1;
300 				break;
301 			default:
302 				s->low_delay = 0;
303 			}
304 		}
305 	}
306 
307 	ctx->shape = get_bits(gb, 2); /* vol shape */
308 	if (ctx->shape != RECT_SHAPE)
309 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "only rectangular vol supported\n");
310 	if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
311 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Gray shape not supported\n");
312 		skip_bits(gb, 4);  /* video_object_layer_shape_extension */
313 	}
314 
315 	check_marker(gb, "before time_increment_resolution");
316 
317 	ctx->framerate.num = get_bits(gb, 16);
318 	if (!ctx->framerate.num) {
319 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "framerate==0\n");
320 		return -1;
321 	}
322 
323 	ctx->time_increment_bits = av_log2(ctx->framerate.num - 1) + 1;
324 	if (ctx->time_increment_bits < 1)
325 		ctx->time_increment_bits = 1;
326 
327 	check_marker(gb, "before fixed_vop_rate");
328 
329 	if (get_bits1(gb) != 0)     /* fixed_vop_rate  */
330 		ctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
331 	else
332 		ctx->framerate.den = 1;
333 
334 	//ctx->time_base = av_inv_q(av_mul_q(ctx->framerate, (AVRational){ctx->ticks_per_frame, 1}));
335 
336 	ctx->t_frame = 0;
337 
338 	if (ctx->shape != BIN_ONLY_SHAPE) {
339 		if (ctx->shape == RECT_SHAPE) {
340 			check_marker(gb, "before width");
341 			width = get_bits(gb, 13);
342 			check_marker(gb, "before height");
343 			height = get_bits(gb, 13);
344 			check_marker(gb, "after height");
345 			if (width && height &&  /* they should be non zero but who knows */
346 			!(s->width && s->codec_tag == AV_RL32("MP4S"))) {
347 				if (s->width && s->height &&
348 				(s->width != width || s->height != height))
349 				s->context_reinit = 1;
350 				s->width  = width;
351 				s->height = height;
352 			}
353 		}
354 
355 		s->progressive_sequence  =
356 		s->progressive_frame     = get_bits1(gb) ^ 1;
357 		s->interlaced_dct        = 0;
358 		if (!get_bits1(gb)) /* OBMC Disable */
359 			v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
360 		if (vo_ver_id == 1)
361 			ctx->vol_sprite_usage = get_bits1(gb);    /* vol_sprite_usage */
362 		else
363 			ctx->vol_sprite_usage = get_bits(gb, 2);  /* vol_sprite_usage */
364 
365 		if (ctx->vol_sprite_usage == STATIC_SPRITE)
366 			v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Static Sprites not supported\n");
367 		if (ctx->vol_sprite_usage == STATIC_SPRITE ||
368 			ctx->vol_sprite_usage == GMC_SPRITE) {
369 		if (ctx->vol_sprite_usage == STATIC_SPRITE) {
370 			skip_bits(gb, 13); // sprite_width
371 			check_marker(gb, "after sprite_width");
372 			skip_bits(gb, 13); // sprite_height
373 			check_marker(gb, "after sprite_height");
374 			skip_bits(gb, 13); // sprite_left
375 			check_marker(gb, "after sprite_left");
376 			skip_bits(gb, 13); // sprite_top
377 			check_marker(gb, "after sprite_top");
378 		}
379 		ctx->num_sprite_warping_points = get_bits(gb, 6);
380 		if (ctx->num_sprite_warping_points > 3) {
381 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "%d sprite_warping_points\n",
382 				ctx->num_sprite_warping_points);
383 			ctx->num_sprite_warping_points = 0;
384 			return -1;
385 		}
386 		s->sprite_warping_accuracy  = get_bits(gb, 2);
387 		ctx->sprite_brightness_change = get_bits1(gb);
388 		if (ctx->vol_sprite_usage == STATIC_SPRITE)
389 			skip_bits1(gb); // low_latency_sprite
390 		}
391 		// FIXME sadct disable bit if verid!=1 && shape not rect
392 
393 		if (get_bits1(gb) == 1) {                   /* not_8_bit */
394 				s->quant_precision = get_bits(gb, 4);   /* quant_precision */
395 			if (get_bits(gb, 4) != 8)               /* bits_per_pixel */
396 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "N-bit not supported\n");
397 			if (s->quant_precision != 5)
398 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "quant precision %d\n", s->quant_precision);
399 			if (s->quant_precision<3 || s->quant_precision>9) {
400 				s->quant_precision = 5;
401 			}
402 		} else {
403 			s->quant_precision = 5;
404 		}
405 
406 		// FIXME a bunch of grayscale shape things
407 
408 		if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
409 			int i, v;
410 
411 			//mpeg4_load_default_matrices(s);
412 
413 			/* load custom intra matrix */
414 			if (get_bits1(gb)) {
415 				int last = 0;
416 			for (i = 0; i < 64; i++) {
417 				//int j;
418 				if (get_bits_left(gb) < 8) {
419 					v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "insufficient data for custom matrix\n");
420 					return -1;
421 				}
422 				v = get_bits(gb, 8);
423 				if (v == 0)
424 					break;
425 
426 				last = v;
427 				//j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
428 				//s->intra_matrix[j]        = last;
429 				//s->chroma_intra_matrix[j] = last;
430 			}
431 
432 			/* replicate last value */
433 			//for (; i < 64; i++) {
434 				//int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
435 				//s->intra_matrix[j]        = last;
436 				//s->chroma_intra_matrix[j] = last;
437 			//}
438 			}
439 
440 			/* load custom non intra matrix */
441 			if (get_bits1(gb)) {
442 				int last = 0;
443 				for (i = 0; i < 64; i++) {
444 					//int j;
445 					if (get_bits_left(gb) < 8) {
446 						v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "insufficient data for custom matrix\n");
447 						return -1;
448 					}
449 					v = get_bits(gb, 8);
450 					if (v == 0)
451 						break;
452 
453 					last = v;
454 					//j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
455 					//s->inter_matrix[j]        = v;
456 					//s->chroma_inter_matrix[j] = v;
457 				}
458 
459 				/* replicate last value */
460 				//for (; i < 64; i++) {
461 					//int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
462 					//s->inter_matrix[j]        = last;
463 					//s->chroma_inter_matrix[j] = last;
464 				//}
465 			}
466 
467 			// FIXME a bunch of grayscale shape things
468 		}
469 
470 		if (vo_ver_id != 1)
471 			s->quarter_sample = get_bits1(gb);
472 		else
473 			s->quarter_sample = 0;
474 
475 		if (get_bits_left(gb) < 4) {
476 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "VOL Header truncated\n");
477 			return -1;
478 		}
479 
480 		if (!get_bits1(gb)) {
481 			int pos               = get_bits_count(gb);
482 			int estimation_method = get_bits(gb, 2);
483 			if (estimation_method < 2) {
484 				if (!get_bits1(gb)) {
485 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* opaque */
486 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* transparent */
487 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_cae */
488 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* inter_cae */
489 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* no_update */
490 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* upsampling */
491 				}
492 				if (!get_bits1(gb)) {
493 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_blocks */
494 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter_blocks */
495 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter4v_blocks */
496 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* not coded blocks */
497 				}
498 				if (!check_marker(gb, "in complexity estimation part 1")) {
499 					skip_bits_long(gb, pos - get_bits_count(gb));
500 					goto no_cplx_est;
501 				}
502 				if (!get_bits1(gb)) {
503 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_coeffs */
504 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_lines */
505 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* vlc_syms */
506 					ctx->cplx_estimation_trash_i += 4 * get_bits1(gb);  /* vlc_bits */
507 				}
508 				if (!get_bits1(gb)) {
509 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* apm */
510 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* npm */
511 					ctx->cplx_estimation_trash_b += 8 * get_bits1(gb);  /* interpolate_mc_q */
512 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* forwback_mc_q */
513 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel2 */
514 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel4 */
515 				}
516 				if (!check_marker(gb, "in complexity estimation part 2")) {
517 					skip_bits_long(gb, pos - get_bits_count(gb));
518 					goto no_cplx_est;
519 				}
520 				if (estimation_method == 1) {
521 					ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* sadct */
522 					ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* qpel */
523 				}
524 			} else
525 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Invalid Complexity estimation method %d\n",
526 				estimation_method);
527 		} else {
528 
529 no_cplx_est:
530 			ctx->cplx_estimation_trash_i =
531 			ctx->cplx_estimation_trash_p =
532 			ctx->cplx_estimation_trash_b = 0;
533 		}
534 
535 		ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
536 
537 		s->data_partitioning = get_bits1(gb);
538 		if (s->data_partitioning)
539 			ctx->rvlc = get_bits1(gb);
540 
541 		if (vo_ver_id != 1) {
542 			ctx->new_pred = get_bits1(gb);
543 		if (ctx->new_pred) {
544 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "new pred not supported\n");
545 			skip_bits(gb, 2); /* requested upstream message type */
546 			skip_bits1(gb);   /* newpred segment type */
547 		}
548 		if (get_bits1(gb)) // reduced_res_vop
549 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "reduced resolution VOP not supported\n");
550 		} else {
551 			ctx->new_pred = 0;
552 		}
553 
554 		ctx->scalability = get_bits1(gb);
555 
556 		if (ctx->scalability) {
557 			struct get_bits_context bak = *gb;
558 			int h_sampling_factor_n;
559 			int h_sampling_factor_m;
560 			int v_sampling_factor_n;
561 			int v_sampling_factor_m;
562 
563 			skip_bits1(gb);    // hierarchy_type
564 			skip_bits(gb, 4);  /* ref_layer_id */
565 			skip_bits1(gb);    /* ref_layer_sampling_dir */
566 			h_sampling_factor_n = get_bits(gb, 5);
567 			h_sampling_factor_m = get_bits(gb, 5);
568 			v_sampling_factor_n = get_bits(gb, 5);
569 			v_sampling_factor_m = get_bits(gb, 5);
570 			ctx->enhancement_type = get_bits1(gb);
571 
572 			if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
573 				v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
574 				/* illegal scalability header (VERY broken encoder),
575 				* trying to workaround */
576 				ctx->scalability = 0;
577 				*gb            = bak;
578 			} else
579 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "scalability not supported\n");
580 
581 			// bin shape stuff FIXME
582 		}
583 	}
584 
585 	if (1) {
586 		v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d  %s%s%s%s\n",
587 			ctx->framerate.den, ctx->framerate.num,
588 			ctx->time_increment_bits,
589 			s->quant_precision,
590 			s->progressive_sequence,
591 			s->low_delay,
592 			ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
593 			s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : "");
594 	}
595 
596 	return 0;
597 }
598 
599 
600 /**
601  * Decode the user data stuff in the header.
602  * Also initializes divx/xvid/lavc_version/build.
603  */
decode_user_data(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)604 static int decode_user_data(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
605 {
606 	struct MpegEncContext *s = &ctx->m;
607 	char buf[256];
608 	int i;
609 	int e;
610 	int ver = 0, build = 0, ver2 = 0, ver3 = 0;
611 	char last;
612 
613 	for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
614 		if (show_bits(gb, 23) == 0)
615 		break;
616 		buf[i] = get_bits(gb, 8);
617 	}
618 	buf[i] = 0;
619 
620 	/* divx detection */
621 	e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
622 	if (e < 2)
623 		e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
624 	if (e >= 2) {
625 		ctx->divx_version = ver;
626 		ctx->divx_build   = build;
627 		s->divx_packed  = e == 3 && last == 'p';
628 	}
629 
630 	/* libavcodec detection */
631 	e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
632 	if (e != 4)
633 		e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
634 	if (e != 4) {
635 		e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
636 		if (e > 1) {
637 			if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
638 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Unknown Lavc version string encountered, %d.%d.%d; "
639 					"clamping sub-version values to 8-bits.\n",
640 					ver, ver2, ver3);
641 			}
642 			build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
643 		}
644 	}
645 	if (e != 4) {
646 		if (strcmp(buf, "ffmpeg") == 0)
647 			ctx->lavc_build = 4600;
648 	}
649 	if (e == 4)
650 		ctx->lavc_build = build;
651 
652 	/* Xvid detection */
653 	e = sscanf(buf, "XviD%d", &build);
654 	if (e == 1)
655 		ctx->xvid_build = build;
656 
657 	return 0;
658 }
659 
660 
mpeg4_decode_gop_header(struct MpegEncContext * s,struct get_bits_context * gb)661 static int mpeg4_decode_gop_header(struct MpegEncContext *s, struct get_bits_context *gb)
662 {
663 	int hours, minutes, seconds;
664 
665 	if (!show_bits(gb, 23)) {
666 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "GOP header invalid\n");
667 		return -1;
668 	}
669 
670 	hours   = get_bits(gb, 5);
671 	minutes = get_bits(gb, 6);
672 	check_marker(gb, "in gop_header");
673 	seconds = get_bits(gb, 6);
674 
675 	s->time_base = seconds + 60*(minutes + 60*hours);
676 
677 	skip_bits1(gb);
678 	skip_bits1(gb);
679 
680 	return 0;
681 }
682 
683 
mpeg4_decode_profile_level(struct MpegEncContext * s,struct get_bits_context * gb,int * profile,int * level)684 static int mpeg4_decode_profile_level(struct MpegEncContext *s, struct get_bits_context *gb, int *profile, int *level)
685 {
686 
687 	*profile = get_bits(gb, 4);
688 	*level   = get_bits(gb, 4);
689 
690 	// for Simple profile, level 0
691 	if (*profile == 0 && *level == 8) {
692 		*level = 0;
693 	}
694 
695 	return 0;
696 }
697 
698 
decode_studiovisualobject(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)699 static int decode_studiovisualobject(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
700 {
701 	struct MpegEncContext *s = &ctx->m;
702 	int visual_object_type;
703 
704 	skip_bits(gb, 4); /* visual_object_verid */
705 	visual_object_type = get_bits(gb, 4);
706 	if (visual_object_type != VOT_VIDEO_ID) {
707 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "VO type %u", visual_object_type);
708 		return -1;
709 	}
710 
711 	next_start_code_studio(gb);
712 	extension_and_user_data(s, gb, 1);
713 
714 	return 0;
715 }
716 
717 
mpeg4_decode_visual_object(struct MpegEncContext * s,struct get_bits_context * gb)718 static int mpeg4_decode_visual_object(struct MpegEncContext *s, struct get_bits_context *gb)
719 {
720 	int visual_object_type;
721 	int is_visual_object_identifier = get_bits1(gb);
722 
723 	if (is_visual_object_identifier) {
724 		skip_bits(gb, 4+3);
725 	}
726 	visual_object_type = get_bits(gb, 4);
727 
728 	if (visual_object_type == VOT_VIDEO_ID ||
729 	visual_object_type == VOT_STILL_TEXTURE_ID) {
730 		int video_signal_type = get_bits1(gb);
731 		if (video_signal_type) {
732 			int video_range, color_description;
733 			skip_bits(gb, 3); // video_format
734 			video_range = get_bits1(gb);
735 			color_description = get_bits1(gb);
736 
737 			s->ctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
738 
739 			if (color_description) {
740 				s->ctx->color_primaries = get_bits(gb, 8);
741 				s->ctx->color_trc       = get_bits(gb, 8);
742 				s->ctx->colorspace      = get_bits(gb, 8);
743 			}
744 		}
745 	}
746 
747 	return 0;
748 }
749 
decode_smpte_tc(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)750 static void decode_smpte_tc(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
751 {
752 	skip_bits(gb, 16); /* Time_code[63..48] */
753 	check_marker(gb, "after Time_code[63..48]");
754 	skip_bits(gb, 16); /* Time_code[47..32] */
755 	check_marker(gb, "after Time_code[47..32]");
756 	skip_bits(gb, 16); /* Time_code[31..16] */
757 	check_marker(gb, "after Time_code[31..16]");
758 	skip_bits(gb, 16); /* Time_code[15..0] */
759 	check_marker(gb, "after Time_code[15..0]");
760 	skip_bits(gb, 4); /* reserved_bits */
761 }
762 
reset_studio_dc_predictors(struct MpegEncContext * s)763 static void reset_studio_dc_predictors(struct MpegEncContext *s)
764 {
765 	/* Reset DC Predictors */
766 	s->last_dc[0] =
767 	s->last_dc[1] =
768 	s->last_dc[2] = 1 << (s->ctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
769 }
770 
771 /**
772  * Decode the next studio vop header.
773  * @return <0 if something went wrong
774  */
decode_studio_vop_header(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)775 static int decode_studio_vop_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
776 {
777 	struct MpegEncContext *s = &ctx->m;
778 
779 	if (get_bits_left(gb) <= 32)
780 		return 0;
781 
782 	//s->decode_mb = mpeg4_decode_studio_mb;
783 
784 	decode_smpte_tc(ctx, gb);
785 
786 	skip_bits(gb, 10); /* temporal_reference */
787 	skip_bits(gb, 2); /* vop_structure */
788 	s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
789 	if (get_bits1(gb)) { /* vop_coded */
790 		skip_bits1(gb); /* top_field_first */
791 		skip_bits1(gb); /* repeat_first_field */
792 		s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
793 	}
794 
795 	if (s->pict_type == AV_PICTURE_TYPE_I) {
796 		if (get_bits1(gb))
797 			reset_studio_dc_predictors(s);
798 	}
799 
800 	if (ctx->shape != BIN_ONLY_SHAPE) {
801 		s->alternate_scan = get_bits1(gb);
802 		s->frame_pred_frame_dct = get_bits1(gb);
803 		s->dct_precision = get_bits(gb, 2);
804 		s->intra_dc_precision = get_bits(gb, 2);
805 		s->q_scale_type = get_bits1(gb);
806 	}
807 
808 	//if (s->alternate_scan) {    }
809 
810 	//mpeg4_load_default_matrices(s);
811 
812 	next_start_code_studio(gb);
813 	extension_and_user_data(s, gb, 4);
814 
815 	return 0;
816 }
817 
decode_new_pred(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)818 static int decode_new_pred(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
819 {
820 	int len = FFMIN(ctx->time_increment_bits + 3, 15);
821 
822 	get_bits(gb, len);
823 	if (get_bits1(gb))
824 		get_bits(gb, len);
825 	check_marker(gb, "after new_pred");
826 
827 	return 0;
828 }
829 
decode_vop_header(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)830 static int decode_vop_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
831 {
832 	struct MpegEncContext *s = &ctx->m;
833 	int time_incr, time_increment;
834 	int64_t pts;
835 
836 	s->mcsel       = 0;
837 	s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I;        /* pict type: I = 0 , P = 1 */
838 	if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
839 		ctx->vol_control_parameters == 0) {
840 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "low_delay flag set incorrectly, clearing it\n");
841 		s->low_delay = 0;
842 	}
843 
844 	s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
845 	/*if (s->partitioned_frame)
846 		s->decode_mb = mpeg4_decode_partitioned_mb;
847 	else
848 		s->decode_mb = mpeg4_decode_mb;*/
849 
850 	time_incr = 0;
851 	while (get_bits1(gb) != 0)
852 		time_incr++;
853 
854 	check_marker(gb, "before time_increment");
855 
856 	if (ctx->time_increment_bits == 0 ||
857 		!(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
858 		v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits);
859 
860 		for (ctx->time_increment_bits = 1;
861 			ctx->time_increment_bits < 16;
862 			ctx->time_increment_bits++) {
863 			if (s->pict_type == AV_PICTURE_TYPE_P ||
864 				(s->pict_type == AV_PICTURE_TYPE_S &&
865 				ctx->vol_sprite_usage == GMC_SPRITE)) {
866 				if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
867 					break;
868 			} else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
869 				break;
870 		}
871 
872 		v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
873 		if (ctx->framerate.num && 4*ctx->framerate.num < 1<<ctx->time_increment_bits) {
874 			ctx->framerate.num = 1<<ctx->time_increment_bits;
875 			//ctx->time_base = av_inv_q(av_mul_q(ctx->framerate, (AVRational){ctx->ticks_per_frame, 1}));
876 		}
877 	}
878 
879 	if (IS_3IV1)
880 		time_increment = get_bits1(gb);        // FIXME investigate further
881 	else
882 		time_increment = get_bits(gb, ctx->time_increment_bits);
883 
884 	if (s->pict_type != AV_PICTURE_TYPE_B) {
885 		s->last_time_base = s->time_base;
886 		s->time_base     += time_incr;
887 		s->time = s->time_base * (int64_t)ctx->framerate.num + time_increment;
888 		//if (s->workaround_bugs & FF_BUG_UMP4) { }
889 		s->pp_time         = s->time - s->last_non_b_time;
890 		s->last_non_b_time = s->time;
891 	} else {
892 		s->time    = (s->last_time_base + time_incr) * (int64_t)ctx->framerate.num + time_increment;
893 		s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
894 		if (s->pp_time <= s->pb_time ||
895 			s->pp_time <= s->pp_time - s->pb_time ||
896 			s->pp_time <= 0) {
897 			/* messed up order, maybe after seeking? skipping current B-frame */
898 			return FRAME_SKIPPED;
899 		}
900 		//ff_mpeg4_init_direct_mv(s);
901 
902 			if (ctx->t_frame == 0)
903 		ctx->t_frame = s->pb_time;
904 		if (ctx->t_frame == 0)
905 			ctx->t_frame = 1;  // 1/0 protection
906 		s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
907 		ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
908 		s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
909 		ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
910 		if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
911 			s->pb_field_time = 2;
912 			s->pp_field_time = 4;
913 			if (!s->progressive_sequence)
914 				return FRAME_SKIPPED;
915 		}
916 	}
917 
918 	if (ctx->framerate.den)
919 		pts = ROUNDED_DIV(s->time, ctx->framerate.den);
920 	else
921 		pts = AV_NOPTS_VALUE;
922 	v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "MPEG4 PTS: %lld\n", pts);
923 
924 	check_marker(gb, "before vop_coded");
925 
926 	/* vop coded */
927 	if (get_bits1(gb) != 1) {
928 		if (1)
929 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "vop not coded\n");
930 		return FRAME_SKIPPED;
931 	}
932 	if (ctx->new_pred)
933 		decode_new_pred(ctx, gb);
934 
935 	if (ctx->shape != BIN_ONLY_SHAPE &&
936 		(s->pict_type == AV_PICTURE_TYPE_P ||
937 		(s->pict_type == AV_PICTURE_TYPE_S &&
938 		ctx->vol_sprite_usage == GMC_SPRITE))) {
939 		/* rounding type for motion estimation */
940 		s->no_rounding = get_bits1(gb);
941 	} else {
942 		s->no_rounding = 0;
943 	}
944 	// FIXME reduced res stuff
945 
946 	if (ctx->shape != RECT_SHAPE) {
947 		if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
948 			skip_bits(gb, 13);  /* width */
949 			check_marker(gb, "after width");
950 			skip_bits(gb, 13);  /* height */
951 			check_marker(gb, "after height");
952 			skip_bits(gb, 13);  /* hor_spat_ref */
953 			check_marker(gb, "after hor_spat_ref");
954 			skip_bits(gb, 13);  /* ver_spat_ref */
955 		}
956 		skip_bits1(gb);         /* change_CR_disable */
957 
958 		if (get_bits1(gb) != 0)
959 			skip_bits(gb, 8);   /* constant_alpha_value */
960 	}
961 
962 	// FIXME complexity estimation stuff
963 
964 	if (ctx->shape != BIN_ONLY_SHAPE) {
965 		skip_bits_long(gb, ctx->cplx_estimation_trash_i);
966 		if (s->pict_type != AV_PICTURE_TYPE_I)
967 			skip_bits_long(gb, ctx->cplx_estimation_trash_p);
968 		if (s->pict_type == AV_PICTURE_TYPE_B)
969 			skip_bits_long(gb, ctx->cplx_estimation_trash_b);
970 
971 		if (get_bits_left(gb) < 3) {
972 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Header truncated\n");
973 			return -1;
974 		}
975 		ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
976 		if (!s->progressive_sequence) {
977 			s->top_field_first = get_bits1(gb);
978 			s->alternate_scan  = get_bits1(gb);
979 		} else
980 			s->alternate_scan = 0;
981 	}
982 
983 	/*if (s->alternate_scan) { } */
984 
985 	if (s->pict_type == AV_PICTURE_TYPE_S) {
986 		if((ctx->vol_sprite_usage == STATIC_SPRITE ||
987 			ctx->vol_sprite_usage == GMC_SPRITE)) {
988 			//if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
989 				//return -1;
990 			if (ctx->sprite_brightness_change)
991 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "sprite_brightness_change not supported\n");
992 			if (ctx->vol_sprite_usage == STATIC_SPRITE)
993 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "static sprite not supported\n");
994 		} else {
995 			memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
996 			memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
997 		}
998 	}
999 
1000 	if (ctx->shape != BIN_ONLY_SHAPE) {
1001 		s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
1002 		if (s->qscale == 0) {
1003 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Error, header damaged or not MPEG-4 header (qscale=0)\n");
1004 			return -1;  // makes no sense to continue, as there is nothing left from the image then
1005 		}
1006 
1007 		if (s->pict_type != AV_PICTURE_TYPE_I) {
1008 			s->f_code = get_bits(gb, 3);        /* fcode_for */
1009 			if (s->f_code == 0) {
1010 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Error, header damaged or not MPEG-4 header (f_code=0)\n");
1011 					s->f_code = 1;
1012 				return -1;  // makes no sense to continue, as there is nothing left from the image then
1013 			}
1014 		} else
1015 			s->f_code = 1;
1016 
1017 		if (s->pict_type == AV_PICTURE_TYPE_B) {
1018 			s->b_code = get_bits(gb, 3);
1019 			if (s->b_code == 0) {
1020 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Error, header damaged or not MPEG4 header (b_code=0)\n");
1021 					s->b_code=1;
1022 				return -1; // makes no sense to continue, as the MV decoding will break very quickly
1023 			}
1024 		} else
1025 			s->b_code = 1;
1026 
1027 		if (1) {
1028 			v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%ld tincr:%d\n",
1029 				s->qscale, s->f_code, s->b_code,
1030 				s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1031 				gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
1032 				s->top_field_first, s->quarter_sample ? "q" : "h",
1033 				s->data_partitioning, ctx->resync_marker,
1034 				ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
1035 				1 - s->no_rounding, s->vo_type,
1036 				ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
1037 				ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
1038 				ctx->cplx_estimation_trash_b,
1039 				s->time,
1040 				time_increment);
1041 		}
1042 
1043 		if (!ctx->scalability) {
1044 			if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
1045 				skip_bits1(gb);  // vop shape coding type
1046 		} else {
1047 			if (ctx->enhancement_type) {
1048 				int load_backward_shape = get_bits1(gb);
1049 				if (load_backward_shape)
1050 					v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "load backward shape isn't supported\n");
1051 			}
1052 			skip_bits(gb, 2);  // ref_select_code
1053 		}
1054 	}
1055 	/* detect buggy encoders which don't set the low_delay flag
1056 	* (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
1057 	* easily (although it's buggy too) */
1058 	if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
1059 		ctx->divx_version == -1 && s->picture_number == 0) {
1060 		v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
1061 			s->low_delay = 1;
1062 	}
1063 
1064 	s->picture_number++;  // better than pic number==0 always ;)
1065 
1066 	// FIXME add short header support
1067 	//s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
1068 	//s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
1069 
1070 	return 0;
1071 }
1072 
1073 /**
1074  * Decode MPEG-4 headers.
1075  * @return <0 if no VOP found (or a damaged one)
1076  *         FRAME_SKIPPED if a not coded VOP is found
1077  *         0 if a VOP is found
1078  */
ff_mpeg4_decode_picture_header(struct mpeg4_dec_param * ctx,struct get_bits_context * gb)1079 int ff_mpeg4_decode_picture_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb)
1080 {
1081 	struct MpegEncContext *s = &ctx->m;
1082 
1083 	unsigned startcode, v;
1084 	int ret;
1085 	int vol = 0;
1086 	int bits_per_raw_sample = 0;
1087 
1088 	s->ctx = ctx;
1089 
1090 	/* search next start code */
1091 	align_get_bits(gb);
1092 
1093 	// If we have not switched to studio profile than we also did not switch bps
1094 	// that means something else (like a previous instance) outside set bps which
1095 	// would be inconsistant with the currect state, thus reset it
1096 	if (!s->studio_profile && bits_per_raw_sample != 8)
1097 		bits_per_raw_sample = 0;
1098 
1099 	if (show_bits(gb, 24) == 0x575630) {
1100 		skip_bits(gb, 24);
1101 		if (get_bits(gb, 8) == 0xF0)
1102 			goto end;
1103 	}
1104 
1105 	startcode = 0xff;
1106 	for (;;) {
1107 		if (get_bits_count(gb) >= gb->size_in_bits) {
1108 			if (gb->size_in_bits == 8) {
1109 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "frame skip %d\n", gb->size_in_bits);
1110 				return FRAME_SKIPPED;  // divx bug
1111 			} else
1112 				return -1;  // end of stream
1113 		}
1114 
1115 		/* use the bits after the test */
1116 		v = get_bits(gb, 8);
1117 		startcode = ((startcode << 8) | v) & 0xffffffff;
1118 
1119 		if ((startcode & 0xFFFFFF00) != 0x100)
1120 			continue;  // no startcode
1121 
1122 		if (1) { //debug
1123 			v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "startcode: %3X \n", startcode);
1124 			if (startcode <= 0x11F)
1125 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Video Object Start\n");
1126 			else if (startcode <= 0x12F)
1127 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Video Object Layer Start\n");
1128 			else if (startcode <= 0x13F)
1129 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Reserved\n");
1130 			else if (startcode <= 0x15F)
1131 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "FGS bp start\n");
1132 			else if (startcode <= 0x1AF)
1133 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Reserved\n");
1134 			else if (startcode == 0x1B0)
1135 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Visual Object Seq Start\n");
1136 			else if (startcode == 0x1B1)
1137 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Visual Object Seq End\n");
1138 			else if (startcode == 0x1B2)
1139 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "User Data\n");
1140 			else if (startcode == 0x1B3)
1141 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Group of VOP start\n");
1142 			else if (startcode == 0x1B4)
1143 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Video Session Error\n");
1144 			else if (startcode == 0x1B5)
1145 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Visual Object Start\n");
1146 			else if (startcode == 0x1B6)
1147 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Video Object Plane start\n");
1148 			else if (startcode == 0x1B7)
1149 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "slice start\n");
1150 			else if (startcode == 0x1B8)
1151 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "extension start\n");
1152 			else if (startcode == 0x1B9)
1153 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "fgs start\n");
1154 			else if (startcode == 0x1BA)
1155 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "FBA Object start\n");
1156 			else if (startcode == 0x1BB)
1157 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "FBA Object Plane start\n");
1158 			else if (startcode == 0x1BC)
1159 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Mesh Object start\n");
1160 			else if (startcode == 0x1BD)
1161 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Mesh Object Plane start\n");
1162 			else if (startcode == 0x1BE)
1163 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Still Texture Object start\n");
1164 			else if (startcode == 0x1BF)
1165 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Texture Spatial Layer start\n");
1166 			else if (startcode == 0x1C0)
1167 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Texture SNR Layer start\n");
1168 			else if (startcode == 0x1C1)
1169 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Texture Tile start\n");
1170 			else if (startcode == 0x1C2)
1171 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "Texture Shape Layer start\n");
1172 			else if (startcode == 0x1C3)
1173 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "stuffing start\n");
1174 			else if (startcode <= 0x1C5)
1175 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "reserved\n");
1176 			else if (startcode <= 0x1FF)
1177 				v4l_dbg(0, V4L_DEBUG_CODEC_PARSER, "System start\n");
1178 		}
1179 
1180 		if (startcode >= 0x120 && startcode <= 0x12F) {
1181 			if (vol) {
1182 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Ignoring multiple VOL headers\n");
1183 				continue;
1184 			}
1185 			vol++;
1186 			if ((ret = decode_vol_header(ctx, gb)) < 0)
1187 				return ret;
1188 		} else if (startcode == USER_DATA_STARTCODE) {
1189 			decode_user_data(ctx, gb);
1190 		} else if (startcode == GOP_STARTCODE) {
1191 			mpeg4_decode_gop_header(s, gb);
1192 		} else if (startcode == VOS_STARTCODE) {
1193 		int profile, level;
1194 		mpeg4_decode_profile_level(s, gb, &profile, &level);
1195 		if (profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO &&
1196 			(level > 0 && level < 9)) {
1197 				s->studio_profile = 1;
1198 				next_start_code_studio(gb);
1199 				extension_and_user_data(s, gb, 0);
1200 			} else if (s->studio_profile) {
1201 				v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Mixes studio and non studio profile\n");
1202 				return -1;
1203 			}
1204 			ctx->profile = profile;
1205 			ctx->level   = level;
1206 		} else if (startcode == VISUAL_OBJ_STARTCODE) {
1207 			if (s->studio_profile) {
1208 				if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
1209 					return ret;
1210 			} else
1211 			mpeg4_decode_visual_object(s, gb);
1212 		} else if (startcode == VOP_STARTCODE) {
1213 			break;
1214 		}
1215 
1216 		align_get_bits(gb);
1217 		startcode = 0xff;
1218 	}
1219 
1220 end:
1221 	if (s->studio_profile) {
1222 		if (!bits_per_raw_sample) {
1223 			v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Missing VOL header\n");
1224 			return -1;
1225 		}
1226 		return decode_studio_vop_header(ctx, gb);
1227 	} else
1228 		return decode_vop_header(ctx, gb);
1229 }
1230 
mpeg4_decode_extradata_ps(u8 * buf,int size,struct mpeg4_param_sets * ps)1231 int mpeg4_decode_extradata_ps(u8 *buf, int size, struct mpeg4_param_sets *ps)
1232 {
1233 	int ret = 0;
1234 	struct get_bits_context gb;
1235 
1236 	ps->head_parsed = false;
1237 
1238 	init_get_bits8(&gb, buf, size);
1239 
1240 	ret = ff_mpeg4_decode_picture_header(&ps->dec_ps, &gb);
1241 	if (ret < -1) {
1242 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR, "Failed to parse extradata\n");
1243 		return ret;
1244 	}
1245 
1246 	if (ps->dec_ps.m.width && ps->dec_ps.m.height)
1247 		ps->head_parsed = true;
1248 
1249 	return 0;
1250 }
1251 
1252