blob: b16c2d112f79f27ad29aa1fa928b01ceca6b098b
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 | |
14 | const 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 */ |
19 | const 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 | |
30 | const 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 | |
41 | const 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 */ |
61 | static 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 | |
70 | static 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 | |
119 | static 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 | |
136 | static 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 | pr_err("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 | pr_err("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 | |
214 | static 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 | pr_err("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 | pr_err("only rectangular vol supported\n"); |
290 | if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) { |
291 | pr_err("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 | pr_err("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 | pr_info("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 | pr_err("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 | pr_err("%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 | pr_err("N-bit not supported\n"); |
377 | if (s->quant_precision != 5) |
378 | pr_err("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 | pr_err("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 | pr_err("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 | pr_err("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 | pr_err("Invalid Complexity estimation method %d\n", |
506 | estimation_method); |
507 | } else { |
508 | |
509 | no_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 | pr_err("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 | pr_err("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 | pr_err("scalability not supported\n"); |
560 | |
561 | // bin shape stuff FIXME |
562 | } |
563 | } |
564 | |
565 | if (1) { |
566 | pr_info("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 | |
577 | return 0; |
578 | } |
579 | |
580 | |
581 | /** |
582 | * Decode the user data stuff in the header. |
583 | * Also initializes divx/xvid/lavc_version/build. |
584 | */ |
585 | static int decode_user_data(struct mpeg4_dec_param *ctx, struct get_bits_context *gb) |
586 | { |
587 | struct MpegEncContext *s = &ctx->m; |
588 | char buf[256]; |
589 | int i; |
590 | int e; |
591 | int ver = 0, build = 0, ver2 = 0, ver3 = 0; |
592 | char last; |
593 | |
594 | for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) { |
595 | if (show_bits(gb, 23) == 0) |
596 | break; |
597 | buf[i] = get_bits(gb, 8); |
598 | } |
599 | buf[i] = 0; |
600 | |
601 | /* divx detection */ |
602 | e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last); |
603 | if (e < 2) |
604 | e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last); |
605 | if (e >= 2) { |
606 | ctx->divx_version = ver; |
607 | ctx->divx_build = build; |
608 | s->divx_packed = e == 3 && last == 'p'; |
609 | } |
610 | |
611 | /* libavcodec detection */ |
612 | e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3; |
613 | if (e != 4) |
614 | e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build); |
615 | if (e != 4) { |
616 | e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1; |
617 | if (e > 1) { |
618 | if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) { |
619 | pr_info("Unknown Lavc version string encountered, %d.%d.%d; " |
620 | "clamping sub-version values to 8-bits.\n", |
621 | ver, ver2, ver3); |
622 | } |
623 | build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF); |
624 | } |
625 | } |
626 | if (e != 4) { |
627 | if (strcmp(buf, "ffmpeg") == 0) |
628 | ctx->lavc_build = 4600; |
629 | } |
630 | if (e == 4) |
631 | ctx->lavc_build = build; |
632 | |
633 | /* Xvid detection */ |
634 | e = sscanf(buf, "XviD%d", &build); |
635 | if (e == 1) |
636 | ctx->xvid_build = build; |
637 | |
638 | return 0; |
639 | } |
640 | |
641 | |
642 | static int mpeg4_decode_gop_header(struct MpegEncContext *s, struct get_bits_context *gb) |
643 | { |
644 | int hours, minutes, seconds; |
645 | |
646 | if (!show_bits(gb, 23)) { |
647 | pr_err("GOP header invalid\n"); |
648 | return -1; |
649 | } |
650 | |
651 | hours = get_bits(gb, 5); |
652 | minutes = get_bits(gb, 6); |
653 | check_marker(gb, "in gop_header"); |
654 | seconds = get_bits(gb, 6); |
655 | |
656 | s->time_base = seconds + 60*(minutes + 60*hours); |
657 | |
658 | skip_bits1(gb); |
659 | skip_bits1(gb); |
660 | |
661 | return 0; |
662 | } |
663 | |
664 | |
665 | static int mpeg4_decode_profile_level(struct MpegEncContext *s, struct get_bits_context *gb, int *profile, int *level) |
666 | { |
667 | |
668 | *profile = get_bits(gb, 4); |
669 | *level = get_bits(gb, 4); |
670 | |
671 | // for Simple profile, level 0 |
672 | if (*profile == 0 && *level == 8) { |
673 | *level = 0; |
674 | } |
675 | |
676 | return 0; |
677 | } |
678 | |
679 | |
680 | static int decode_studiovisualobject(struct mpeg4_dec_param *ctx, struct get_bits_context *gb) |
681 | { |
682 | struct MpegEncContext *s = &ctx->m; |
683 | int visual_object_type; |
684 | |
685 | skip_bits(gb, 4); /* visual_object_verid */ |
686 | visual_object_type = get_bits(gb, 4); |
687 | if (visual_object_type != VOT_VIDEO_ID) { |
688 | pr_err("VO type %u", visual_object_type); |
689 | return -1; |
690 | } |
691 | |
692 | next_start_code_studio(gb); |
693 | extension_and_user_data(s, gb, 1); |
694 | |
695 | return 0; |
696 | } |
697 | |
698 | |
699 | static int mpeg4_decode_visual_object(struct MpegEncContext *s, struct get_bits_context *gb) |
700 | { |
701 | int visual_object_type; |
702 | int is_visual_object_identifier = get_bits1(gb); |
703 | |
704 | if (is_visual_object_identifier) { |
705 | skip_bits(gb, 4+3); |
706 | } |
707 | visual_object_type = get_bits(gb, 4); |
708 | |
709 | if (visual_object_type == VOT_VIDEO_ID || |
710 | visual_object_type == VOT_STILL_TEXTURE_ID) { |
711 | int video_signal_type = get_bits1(gb); |
712 | if (video_signal_type) { |
713 | int video_range, color_description; |
714 | skip_bits(gb, 3); // video_format |
715 | video_range = get_bits1(gb); |
716 | color_description = get_bits1(gb); |
717 | |
718 | s->ctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; |
719 | |
720 | if (color_description) { |
721 | s->ctx->color_primaries = get_bits(gb, 8); |
722 | s->ctx->color_trc = get_bits(gb, 8); |
723 | s->ctx->colorspace = get_bits(gb, 8); |
724 | } |
725 | } |
726 | } |
727 | |
728 | return 0; |
729 | } |
730 | |
731 | static void decode_smpte_tc(struct mpeg4_dec_param *ctx, struct get_bits_context *gb) |
732 | { |
733 | skip_bits(gb, 16); /* Time_code[63..48] */ |
734 | check_marker(gb, "after Time_code[63..48]"); |
735 | skip_bits(gb, 16); /* Time_code[47..32] */ |
736 | check_marker(gb, "after Time_code[47..32]"); |
737 | skip_bits(gb, 16); /* Time_code[31..16] */ |
738 | check_marker(gb, "after Time_code[31..16]"); |
739 | skip_bits(gb, 16); /* Time_code[15..0] */ |
740 | check_marker(gb, "after Time_code[15..0]"); |
741 | skip_bits(gb, 4); /* reserved_bits */ |
742 | } |
743 | |
744 | static void reset_studio_dc_predictors(struct MpegEncContext *s) |
745 | { |
746 | /* Reset DC Predictors */ |
747 | s->last_dc[0] = |
748 | s->last_dc[1] = |
749 | s->last_dc[2] = 1 << (s->ctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1); |
750 | } |
751 | |
752 | /** |
753 | * Decode the next studio vop header. |
754 | * @return <0 if something went wrong |
755 | */ |
756 | static int decode_studio_vop_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb) |
757 | { |
758 | struct MpegEncContext *s = &ctx->m; |
759 | |
760 | if (get_bits_left(gb) <= 32) |
761 | return 0; |
762 | |
763 | //s->decode_mb = mpeg4_decode_studio_mb; |
764 | |
765 | decode_smpte_tc(ctx, gb); |
766 | |
767 | skip_bits(gb, 10); /* temporal_reference */ |
768 | skip_bits(gb, 2); /* vop_structure */ |
769 | s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */ |
770 | if (get_bits1(gb)) { /* vop_coded */ |
771 | skip_bits1(gb); /* top_field_first */ |
772 | skip_bits1(gb); /* repeat_first_field */ |
773 | s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */ |
774 | } |
775 | |
776 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
777 | if (get_bits1(gb)) |
778 | reset_studio_dc_predictors(s); |
779 | } |
780 | |
781 | if (ctx->shape != BIN_ONLY_SHAPE) { |
782 | s->alternate_scan = get_bits1(gb); |
783 | s->frame_pred_frame_dct = get_bits1(gb); |
784 | s->dct_precision = get_bits(gb, 2); |
785 | s->intra_dc_precision = get_bits(gb, 2); |
786 | s->q_scale_type = get_bits1(gb); |
787 | } |
788 | |
789 | //if (s->alternate_scan) { } |
790 | |
791 | //mpeg4_load_default_matrices(s); |
792 | |
793 | next_start_code_studio(gb); |
794 | extension_and_user_data(s, gb, 4); |
795 | |
796 | return 0; |
797 | } |
798 | |
799 | static int decode_new_pred(struct mpeg4_dec_param *ctx, struct get_bits_context *gb) |
800 | { |
801 | int len = FFMIN(ctx->time_increment_bits + 3, 15); |
802 | |
803 | get_bits(gb, len); |
804 | if (get_bits1(gb)) |
805 | get_bits(gb, len); |
806 | check_marker(gb, "after new_pred"); |
807 | |
808 | return 0; |
809 | } |
810 | |
811 | static int decode_vop_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb) |
812 | { |
813 | struct MpegEncContext *s = &ctx->m; |
814 | int time_incr, time_increment; |
815 | int64_t pts; |
816 | |
817 | s->mcsel = 0; |
818 | s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */ |
819 | if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay && |
820 | ctx->vol_control_parameters == 0) { |
821 | pr_err("low_delay flag set incorrectly, clearing it\n"); |
822 | s->low_delay = 0; |
823 | } |
824 | |
825 | s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B; |
826 | /*if (s->partitioned_frame) |
827 | s->decode_mb = mpeg4_decode_partitioned_mb; |
828 | else |
829 | s->decode_mb = mpeg4_decode_mb;*/ |
830 | |
831 | time_incr = 0; |
832 | while (get_bits1(gb) != 0) |
833 | time_incr++; |
834 | |
835 | check_marker(gb, "before time_increment"); |
836 | |
837 | if (ctx->time_increment_bits == 0 || |
838 | !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) { |
839 | pr_info("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); |
840 | |
841 | for (ctx->time_increment_bits = 1; |
842 | ctx->time_increment_bits < 16; |
843 | ctx->time_increment_bits++) { |
844 | if (s->pict_type == AV_PICTURE_TYPE_P || |
845 | (s->pict_type == AV_PICTURE_TYPE_S && |
846 | ctx->vol_sprite_usage == GMC_SPRITE)) { |
847 | if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30) |
848 | break; |
849 | } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18) |
850 | break; |
851 | } |
852 | |
853 | pr_info("time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits); |
854 | if (ctx->framerate.num && 4*ctx->framerate.num < 1<<ctx->time_increment_bits) { |
855 | ctx->framerate.num = 1<<ctx->time_increment_bits; |
856 | //ctx->time_base = av_inv_q(av_mul_q(ctx->framerate, (AVRational){ctx->ticks_per_frame, 1})); |
857 | } |
858 | } |
859 | |
860 | if (IS_3IV1) |
861 | time_increment = get_bits1(gb); // FIXME investigate further |
862 | else |
863 | time_increment = get_bits(gb, ctx->time_increment_bits); |
864 | |
865 | if (s->pict_type != AV_PICTURE_TYPE_B) { |
866 | s->last_time_base = s->time_base; |
867 | s->time_base += time_incr; |
868 | s->time = s->time_base * (int64_t)ctx->framerate.num + time_increment; |
869 | //if (s->workaround_bugs & FF_BUG_UMP4) { } |
870 | s->pp_time = s->time - s->last_non_b_time; |
871 | s->last_non_b_time = s->time; |
872 | } else { |
873 | s->time = (s->last_time_base + time_incr) * (int64_t)ctx->framerate.num + time_increment; |
874 | s->pb_time = s->pp_time - (s->last_non_b_time - s->time); |
875 | if (s->pp_time <= s->pb_time || |
876 | s->pp_time <= s->pp_time - s->pb_time || |
877 | s->pp_time <= 0) { |
878 | /* messed up order, maybe after seeking? skipping current B-frame */ |
879 | return FRAME_SKIPPED; |
880 | } |
881 | //ff_mpeg4_init_direct_mv(s); |
882 | |
883 | if (ctx->t_frame == 0) |
884 | ctx->t_frame = s->pb_time; |
885 | if (ctx->t_frame == 0) |
886 | ctx->t_frame = 1; // 1/0 protection |
887 | s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) - |
888 | ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2; |
889 | s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) - |
890 | ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2; |
891 | if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) { |
892 | s->pb_field_time = 2; |
893 | s->pp_field_time = 4; |
894 | if (!s->progressive_sequence) |
895 | return FRAME_SKIPPED; |
896 | } |
897 | } |
898 | |
899 | if (ctx->framerate.den) |
900 | pts = ROUNDED_DIV(s->time, ctx->framerate.den); |
901 | else |
902 | pts = AV_NOPTS_VALUE; |
903 | pr_info("MPEG4 PTS: %lld\n", pts); |
904 | |
905 | check_marker(gb, "before vop_coded"); |
906 | |
907 | /* vop coded */ |
908 | if (get_bits1(gb) != 1) { |
909 | if (1) |
910 | pr_err("vop not coded\n"); |
911 | return FRAME_SKIPPED; |
912 | } |
913 | if (ctx->new_pred) |
914 | decode_new_pred(ctx, gb); |
915 | |
916 | if (ctx->shape != BIN_ONLY_SHAPE && |
917 | (s->pict_type == AV_PICTURE_TYPE_P || |
918 | (s->pict_type == AV_PICTURE_TYPE_S && |
919 | ctx->vol_sprite_usage == GMC_SPRITE))) { |
920 | /* rounding type for motion estimation */ |
921 | s->no_rounding = get_bits1(gb); |
922 | } else { |
923 | s->no_rounding = 0; |
924 | } |
925 | // FIXME reduced res stuff |
926 | |
927 | if (ctx->shape != RECT_SHAPE) { |
928 | if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) { |
929 | skip_bits(gb, 13); /* width */ |
930 | check_marker(gb, "after width"); |
931 | skip_bits(gb, 13); /* height */ |
932 | check_marker(gb, "after height"); |
933 | skip_bits(gb, 13); /* hor_spat_ref */ |
934 | check_marker(gb, "after hor_spat_ref"); |
935 | skip_bits(gb, 13); /* ver_spat_ref */ |
936 | } |
937 | skip_bits1(gb); /* change_CR_disable */ |
938 | |
939 | if (get_bits1(gb) != 0) |
940 | skip_bits(gb, 8); /* constant_alpha_value */ |
941 | } |
942 | |
943 | // FIXME complexity estimation stuff |
944 | |
945 | if (ctx->shape != BIN_ONLY_SHAPE) { |
946 | skip_bits_long(gb, ctx->cplx_estimation_trash_i); |
947 | if (s->pict_type != AV_PICTURE_TYPE_I) |
948 | skip_bits_long(gb, ctx->cplx_estimation_trash_p); |
949 | if (s->pict_type == AV_PICTURE_TYPE_B) |
950 | skip_bits_long(gb, ctx->cplx_estimation_trash_b); |
951 | |
952 | if (get_bits_left(gb) < 3) { |
953 | pr_err("Header truncated\n"); |
954 | return -1; |
955 | } |
956 | ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)]; |
957 | if (!s->progressive_sequence) { |
958 | s->top_field_first = get_bits1(gb); |
959 | s->alternate_scan = get_bits1(gb); |
960 | } else |
961 | s->alternate_scan = 0; |
962 | } |
963 | |
964 | /*if (s->alternate_scan) { } */ |
965 | |
966 | if (s->pict_type == AV_PICTURE_TYPE_S) { |
967 | if((ctx->vol_sprite_usage == STATIC_SPRITE || |
968 | ctx->vol_sprite_usage == GMC_SPRITE)) { |
969 | //if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0) |
970 | //return -1; |
971 | if (ctx->sprite_brightness_change) |
972 | pr_err("sprite_brightness_change not supported\n"); |
973 | if (ctx->vol_sprite_usage == STATIC_SPRITE) |
974 | pr_err("static sprite not supported\n"); |
975 | } else { |
976 | memset(s->sprite_offset, 0, sizeof(s->sprite_offset)); |
977 | memset(s->sprite_delta, 0, sizeof(s->sprite_delta)); |
978 | } |
979 | } |
980 | |
981 | if (ctx->shape != BIN_ONLY_SHAPE) { |
982 | s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision); |
983 | if (s->qscale == 0) { |
984 | pr_err("Error, header damaged or not MPEG-4 header (qscale=0)\n"); |
985 | return -1; // makes no sense to continue, as there is nothing left from the image then |
986 | } |
987 | |
988 | if (s->pict_type != AV_PICTURE_TYPE_I) { |
989 | s->f_code = get_bits(gb, 3); /* fcode_for */ |
990 | if (s->f_code == 0) { |
991 | pr_err("Error, header damaged or not MPEG-4 header (f_code=0)\n"); |
992 | s->f_code = 1; |
993 | return -1; // makes no sense to continue, as there is nothing left from the image then |
994 | } |
995 | } else |
996 | s->f_code = 1; |
997 | |
998 | if (s->pict_type == AV_PICTURE_TYPE_B) { |
999 | s->b_code = get_bits(gb, 3); |
1000 | if (s->b_code == 0) { |
1001 | pr_err("Error, header damaged or not MPEG4 header (b_code=0)\n"); |
1002 | s->b_code=1; |
1003 | return -1; // makes no sense to continue, as the MV decoding will break very quickly |
1004 | } |
1005 | } else |
1006 | s->b_code = 1; |
1007 | |
1008 | if (1) { |
1009 | pr_info("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", |
1010 | s->qscale, s->f_code, s->b_code, |
1011 | 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")), |
1012 | gb->size_in_bits,s->progressive_sequence, s->alternate_scan, |
1013 | s->top_field_first, s->quarter_sample ? "q" : "h", |
1014 | s->data_partitioning, ctx->resync_marker, |
1015 | ctx->num_sprite_warping_points, s->sprite_warping_accuracy, |
1016 | 1 - s->no_rounding, s->vo_type, |
1017 | ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold, |
1018 | ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p, |
1019 | ctx->cplx_estimation_trash_b, |
1020 | s->time, |
1021 | time_increment |
1022 | ); |
1023 | } |
1024 | |
1025 | if (!ctx->scalability) { |
1026 | if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I) |
1027 | skip_bits1(gb); // vop shape coding type |
1028 | } else { |
1029 | if (ctx->enhancement_type) { |
1030 | int load_backward_shape = get_bits1(gb); |
1031 | if (load_backward_shape) |
1032 | pr_err("load backward shape isn't supported\n"); |
1033 | } |
1034 | skip_bits(gb, 2); // ref_select_code |
1035 | } |
1036 | } |
1037 | /* detect buggy encoders which don't set the low_delay flag |
1038 | * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames |
1039 | * easily (although it's buggy too) */ |
1040 | if (s->vo_type == 0 && ctx->vol_control_parameters == 0 && |
1041 | ctx->divx_version == -1 && s->picture_number == 0) { |
1042 | pr_info("looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n"); |
1043 | s->low_delay = 1; |
1044 | } |
1045 | |
1046 | s->picture_number++; // better than pic number==0 always ;) |
1047 | |
1048 | // FIXME add short header support |
1049 | //s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table; |
1050 | //s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table; |
1051 | |
1052 | return 0; |
1053 | } |
1054 | |
1055 | /** |
1056 | * Decode MPEG-4 headers. |
1057 | * @return <0 if no VOP found (or a damaged one) |
1058 | * FRAME_SKIPPED if a not coded VOP is found |
1059 | * 0 if a VOP is found |
1060 | */ |
1061 | int ff_mpeg4_decode_picture_header(struct mpeg4_dec_param *ctx, struct get_bits_context *gb) |
1062 | { |
1063 | struct MpegEncContext *s = &ctx->m; |
1064 | |
1065 | unsigned startcode, v; |
1066 | int ret; |
1067 | int vol = 0; |
1068 | int bits_per_raw_sample = 0; |
1069 | |
1070 | s->ctx = ctx; |
1071 | |
1072 | /* search next start code */ |
1073 | align_get_bits(gb); |
1074 | |
1075 | // If we have not switched to studio profile than we also did not switch bps |
1076 | // that means something else (like a previous instance) outside set bps which |
1077 | // would be inconsistant with the currect state, thus reset it |
1078 | if (!s->studio_profile && bits_per_raw_sample != 8) |
1079 | bits_per_raw_sample = 0; |
1080 | |
1081 | if (show_bits(gb, 24) == 0x575630) { |
1082 | skip_bits(gb, 24); |
1083 | if (get_bits(gb, 8) == 0xF0) |
1084 | goto end; |
1085 | } |
1086 | |
1087 | startcode = 0xff; |
1088 | for (;;) { |
1089 | if (get_bits_count(gb) >= gb->size_in_bits) { |
1090 | if (gb->size_in_bits == 8) { |
1091 | pr_info("frame skip %d\n", gb->size_in_bits); |
1092 | return FRAME_SKIPPED; // divx bug |
1093 | } else |
1094 | return -1; // end of stream |
1095 | } |
1096 | |
1097 | /* use the bits after the test */ |
1098 | v = get_bits(gb, 8); |
1099 | startcode = ((startcode << 8) | v) & 0xffffffff; |
1100 | |
1101 | if ((startcode & 0xFFFFFF00) != 0x100) |
1102 | continue; // no startcode |
1103 | |
1104 | if (1) { //debug |
1105 | pr_info("startcode: %3X \n", startcode); |
1106 | if (startcode <= 0x11F) |
1107 | pr_info("Video Object Start\n"); |
1108 | else if (startcode <= 0x12F) |
1109 | pr_info("Video Object Layer Start\n"); |
1110 | else if (startcode <= 0x13F) |
1111 | pr_info("Reserved\n"); |
1112 | else if (startcode <= 0x15F) |
1113 | pr_info("FGS bp start\n"); |
1114 | else if (startcode <= 0x1AF) |
1115 | pr_info("Reserved\n"); |
1116 | else if (startcode == 0x1B0) |
1117 | pr_info("Visual Object Seq Start\n"); |
1118 | else if (startcode == 0x1B1) |
1119 | pr_info("Visual Object Seq End\n"); |
1120 | else if (startcode == 0x1B2) |
1121 | pr_info("User Data\n"); |
1122 | else if (startcode == 0x1B3) |
1123 | pr_info("Group of VOP start\n"); |
1124 | else if (startcode == 0x1B4) |
1125 | pr_info("Video Session Error\n"); |
1126 | else if (startcode == 0x1B5) |
1127 | pr_info("Visual Object Start\n"); |
1128 | else if (startcode == 0x1B6) |
1129 | pr_info("Video Object Plane start\n"); |
1130 | else if (startcode == 0x1B7) |
1131 | pr_info("slice start\n"); |
1132 | else if (startcode == 0x1B8) |
1133 | pr_info("extension start\n"); |
1134 | else if (startcode == 0x1B9) |
1135 | pr_info("fgs start\n"); |
1136 | else if (startcode == 0x1BA) |
1137 | pr_info("FBA Object start\n"); |
1138 | else if (startcode == 0x1BB) |
1139 | pr_info("FBA Object Plane start\n"); |
1140 | else if (startcode == 0x1BC) |
1141 | pr_info("Mesh Object start\n"); |
1142 | else if (startcode == 0x1BD) |
1143 | pr_info("Mesh Object Plane start\n"); |
1144 | else if (startcode == 0x1BE) |
1145 | pr_info("Still Texture Object start\n"); |
1146 | else if (startcode == 0x1BF) |
1147 | pr_info("Texture Spatial Layer start\n"); |
1148 | else if (startcode == 0x1C0) |
1149 | pr_info("Texture SNR Layer start\n"); |
1150 | else if (startcode == 0x1C1) |
1151 | pr_info("Texture Tile start\n"); |
1152 | else if (startcode == 0x1C2) |
1153 | pr_info("Texture Shape Layer start\n"); |
1154 | else if (startcode == 0x1C3) |
1155 | pr_info("stuffing start\n"); |
1156 | else if (startcode <= 0x1C5) |
1157 | pr_info("reserved\n"); |
1158 | else if (startcode <= 0x1FF) |
1159 | pr_info("System start\n"); |
1160 | } |
1161 | |
1162 | if (startcode >= 0x120 && startcode <= 0x12F) { |
1163 | if (vol) { |
1164 | pr_err("Ignoring multiple VOL headers\n"); |
1165 | continue; |
1166 | } |
1167 | vol++; |
1168 | if ((ret = decode_vol_header(ctx, gb)) < 0) |
1169 | return ret; |
1170 | } else if (startcode == USER_DATA_STARTCODE) { |
1171 | decode_user_data(ctx, gb); |
1172 | } else if (startcode == GOP_STARTCODE) { |
1173 | mpeg4_decode_gop_header(s, gb); |
1174 | } else if (startcode == VOS_STARTCODE) { |
1175 | int profile, level; |
1176 | mpeg4_decode_profile_level(s, gb, &profile, &level); |
1177 | if (profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO && |
1178 | (level > 0 && level < 9)) { |
1179 | s->studio_profile = 1; |
1180 | next_start_code_studio(gb); |
1181 | extension_and_user_data(s, gb, 0); |
1182 | } else if (s->studio_profile) { |
1183 | pr_err("Mixes studio and non studio profile\n"); |
1184 | return -1; |
1185 | } |
1186 | ctx->profile = profile; |
1187 | ctx->level = level; |
1188 | } else if (startcode == VISUAL_OBJ_STARTCODE) { |
1189 | if (s->studio_profile) { |
1190 | if ((ret = decode_studiovisualobject(ctx, gb)) < 0) |
1191 | return ret; |
1192 | } else |
1193 | mpeg4_decode_visual_object(s, gb); |
1194 | } else if (startcode == VOP_STARTCODE) { |
1195 | break; |
1196 | } |
1197 | |
1198 | align_get_bits(gb); |
1199 | startcode = 0xff; |
1200 | } |
1201 | |
1202 | end: |
1203 | if (s->studio_profile) { |
1204 | if (!bits_per_raw_sample) { |
1205 | pr_err("Missing VOL header\n"); |
1206 | return -1; |
1207 | } |
1208 | return decode_studio_vop_header(ctx, gb); |
1209 | } else |
1210 | return decode_vop_header(ctx, gb); |
1211 | } |
1212 | |
1213 | int mpeg4_decode_extradata_ps(u8 *buf, int size, struct mpeg4_param_sets *ps) |
1214 | { |
1215 | int ret = 0; |
1216 | struct get_bits_context gb; |
1217 | |
1218 | ps->head_parsed = false; |
1219 | |
1220 | init_get_bits8(&gb, buf, size); |
1221 | |
1222 | ret = ff_mpeg4_decode_picture_header(&ps->dec_ps, &gb); |
1223 | if (ret < -1) { |
1224 | pr_err("Failed to parse extradata\n"); |
1225 | return ret; |
1226 | } |
1227 | |
1228 | if (ps->dec_ps.m.width && ps->dec_ps.m.height) |
1229 | ps->head_parsed = true; |
1230 | |
1231 | return 0; |
1232 | } |
1233 | |
1234 |