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