1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 #include "log/log.h"
19
20 #include "mp4dec_lib.h"
21 #include "bitstream.h"
22 #include "vlc_decode.h"
23 #include "zigzag.h"
24
25 #define OSCL_DISABLE_WARNING_CONV_POSSIBLE_LOSS_OF_DATA
26
27 #ifdef PV_SUPPORT_MAIN_PROFILE
28 /* INTRA */
29 const static int mpeg_iqmat_def[NCOEFF_BLOCK] =
30 {
31 8, 17, 18, 19, 21, 23, 25, 27,
32 17, 18, 19, 21, 23, 25, 27, 28,
33 20, 21, 22, 23, 24, 26, 28, 30,
34 21, 22, 23, 24, 26, 28, 30, 32,
35 22, 23, 24, 26, 28, 30, 32, 35,
36 23, 24, 26, 28, 30, 32, 35, 38,
37 25, 26, 28, 30, 32, 35, 38, 41,
38 27, 28, 30, 32, 35, 38, 41, 45
39 };
40
41 /* INTER */
42 const static int mpeg_nqmat_def[64] =
43 {
44 16, 17, 18, 19, 20, 21, 22, 23,
45 17, 18, 19, 20, 21, 22, 23, 24,
46 18, 19, 20, 21, 22, 23, 24, 25,
47 19, 20, 21, 22, 23, 24, 26, 27,
48 20, 21, 22, 23, 25, 26, 27, 28,
49 21, 22, 23, 24, 26, 27, 28, 30,
50 22, 23, 24, 26, 27, 28, 30, 31,
51 23, 24, 25, 27, 28, 30, 31, 33
52 };
53 #endif
54
55 /* ======================================================================== */
56 /* Function : CalcNumBits() */
57 /* Purpose : */
58 /* In/out : */
59 /* Return : Calculate the minimum number of bits required to */
60 /* represent x. */
61 /* Note : This is an equivalent implementation of */
62 /* (long)ceil(log((double)x)/log(2.0)) */
63 /* Modified : */
64 /* ======================================================================== */
CalcNumBits(uint x)65 int CalcNumBits(uint x)
66 {
67 int i = 1;
68 while (x >>= 1) i++;
69 return i;
70 }
71
72
73
74 /***********************************************************CommentBegin******
75 *
76 * -- DecodeVolHeader -- Decode the header of a VOL
77 *
78 * 04/10/2000 : initial modification to the new PV-Decoder Lib format.
79 * 10/12/2001 : reject non compliant bitstreams
80 *
81 ***********************************************************CommentEnd********/
DecodeVOLHeader(VideoDecData * video,int layer)82 PV_STATUS DecodeVOLHeader(VideoDecData *video, int layer)
83 {
84 PV_STATUS status;
85 Vol *currVol;
86 BitstreamDecVideo *stream;
87 uint32 tmpvar, vol_shape;
88 uint32 startCode;
89 #ifdef PV_SUPPORT_MAIN_PROFILE
90 int *qmat, i, j;
91 #endif
92 int version_id = 1;
93 #ifdef PV_TOLERATE_VOL_ERRORS
94 uint32 profile = 0x01;
95 #endif
96 /* There's a "currLayer" variable inside videoDecData. */
97 /* However, we don't maintain it until we decode frame data. 04/05/2000 */
98 currVol = video->vol[layer];
99 stream = currVol->bitstream;
100 currVol->moduloTimeBase = 0;
101
102 /* Determine which start code for the decoder to begin with */
103 status = BitstreamShowBits32HC(stream, &startCode);
104
105 if (startCode == VISUAL_OBJECT_SEQUENCE_START_CODE)
106 { /* Bitstream Exhchange Fix 9/99 */
107 /* Bitstream Exchange requires we allow start with Video Object Sequence */
108 /* visual_object_sequence_start_code */
109 (void) BitstreamReadBits32HC(stream);
110 tmpvar = (uint32) BitstreamReadBits16(stream, 8); /* profile */
111 #ifndef PV_TOLERATE_VOL_ERRORS
112 if (layer) /* */
113 {
114 /* support SSPL0-2 */
115 if (tmpvar != 0x10 && tmpvar != 0x11 && tmpvar != 0x12 &&
116 tmpvar != 0xA1 && tmpvar != 0xA2 && tmpvar != 0xA3/* Core SP@L1-L3 */)
117 return PV_FAIL;
118 }
119 else
120 {
121 /* support SPL0-3 & SSPL0-2 */
122 if (tmpvar != 0x01 && tmpvar != 0x02 && tmpvar != 0x03 && tmpvar != 0x08 &&
123 /* While not technically supported, try to decode SPL4&SPL5 files as well. */
124 /* We'll fail later if the size is too large. This is to allow playback of */
125 /* some <=CIF files generated by other encoders. */
126 tmpvar != 0x04 && tmpvar != 0x05 &&
127 tmpvar != 0x10 && tmpvar != 0x11 && tmpvar != 0x12 &&
128 tmpvar != 0x21 && tmpvar != 0x22 && /* Core Profile Levels */
129 tmpvar != 0xA1 && tmpvar != 0xA2 && tmpvar != 0xA3 &&
130 tmpvar != 0xF0 && tmpvar != 0xF1 && /* Advanced Simple Profile Levels*/
131 tmpvar != 0xF2 && tmpvar != 0xF3 &&
132 tmpvar != 0xF4 && tmpvar != 0xF5)
133 return PV_FAIL;
134 }
135 #else
136 profile = tmpvar;
137 #endif
138
139 // save the profile and level for the query
140 currVol->profile_level_id = (uint)tmpvar; // 6/10/04
141
142
143
144 status = BitstreamShowBits32HC(stream, &tmpvar);
145 if (tmpvar == USER_DATA_START_CODE)
146 {
147 /* Something has to be done with user data 11/11/99 */
148 status = DecodeUserData(stream);
149 if (status != PV_SUCCESS) return PV_FAIL;
150 }
151 /* visual_object_start_code */
152 BitstreamShowBits32HC(stream, &tmpvar);
153 if (tmpvar != VISUAL_OBJECT_START_CODE)
154 {
155 do
156 {
157 /* Search for VOL_HEADER */
158 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */
159 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
160 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
161 PV_BitstreamFlushBits(stream, 8);
162 }
163 while (tmpvar != VOL_START_CODE);
164 goto decode_vol;
165 }
166 else
167 {
168 BitstreamReadBits32HC(stream);
169 }
170
171 /* is_visual_object_identifier */
172 tmpvar = (uint32) BitstreamRead1Bits(stream);
173 if (tmpvar)
174 {
175 /* visual_object_verid */
176 tmpvar = (uint32) BitstreamReadBits16(stream, 4);
177 /* visual_object_priority */
178 tmpvar = (uint32) BitstreamReadBits16(stream, 3);
179 }
180 /* visual_object_type */
181 BitstreamShowBits32(stream, 4, &tmpvar);
182 if (tmpvar == 1)
183 { /* video_signal_type */
184 PV_BitstreamFlushBits(stream, 4);
185 tmpvar = (uint32) BitstreamRead1Bits(stream);
186 if (tmpvar == 1)
187 {
188 /* video_format */
189 tmpvar = (uint32) BitstreamReadBits16(stream, 3);
190 /* video_range */
191 tmpvar = (uint32) BitstreamRead1Bits(stream);
192 /* color_description */
193 tmpvar = (uint32) BitstreamRead1Bits(stream);
194 if (tmpvar == 1)
195 {
196 /* color_primaries */
197 tmpvar = (uint32) BitstreamReadBits16(stream, 8);
198 /* transfer_characteristics */
199 tmpvar = (uint32) BitstreamReadBits16(stream, 8);
200 /* matrix_coefficients */
201 tmpvar = (uint32) BitstreamReadBits16(stream, 8);
202 }
203 }
204 }
205 else
206 {
207 do
208 {
209 /* Search for VOL_HEADER */
210 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */
211 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
212 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
213 PV_BitstreamFlushBits(stream, 8);
214 }
215 while (tmpvar != VOL_START_CODE);
216 goto decode_vol;
217 }
218
219 /* next_start_code() */
220 status = PV_BitstreamByteAlign(stream); /* 10/12/01 */
221 status = BitstreamShowBits32HC(stream, &tmpvar);
222
223 if (tmpvar == USER_DATA_START_CODE)
224 {
225 /* Something has to be done to deal with user data (parse it) 11/11/99 */
226 status = DecodeUserData(stream);
227 if (status != PV_SUCCESS) return PV_FAIL;
228 }
229 status = BitstreamShowBits32(stream, 27, &tmpvar); /* 10/12/01 */
230 }
231 else
232 {
233 /* tmpvar = 0; */ /* 10/12/01 */
234 status = BitstreamShowBits32(stream, 27, &tmpvar); /* uncomment this line if you want
235 to start decoding with a
236 video_object_start_code */
237 }
238
239 if (tmpvar == VO_START_CODE)
240 {
241 /*****
242 *
243 * Read the VOL header entries from the bitstream
244 *
245 *****/
246 /* video_object_start_code */
247 tmpvar = BitstreamReadBits32(stream, 27);
248 tmpvar = (uint32) BitstreamReadBits16(stream, 5);
249
250
251 /* video_object_layer_start_code */
252 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
253 if (tmpvar != VOL_START_CODE)
254 {
255 status = BitstreamCheckEndBuffer(stream);
256 if (status == PV_END_OF_VOP)
257 {
258 video->shortVideoHeader = TRUE;
259 return PV_SUCCESS;
260 }
261 else
262 {
263 do
264 {
265 /* Search for VOL_HEADER */
266 status = PVSearchNextM4VFrame(stream);/* search 0x00 0x00 0x01 */
267 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
268 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
269 PV_BitstreamFlushBits(stream, 8); /* advance the byte ptr */
270 }
271 while (tmpvar != VOL_START_CODE);
272 }
273 }
274 else
275 {
276 PV_BitstreamFlushBits(stream, 8);
277 }
278
279 decode_vol:
280 PV_BitstreamFlushBits(stream, VOL_START_CODE_LENGTH - 8);
281 video->shortVideoHeader = 0;
282
283 /* vol_id (4 bits) */
284 currVol->volID = (int) BitstreamReadBits16(stream, 4);
285
286 /* RandomAccessible flag */
287 tmpvar = (uint32) BitstreamRead1Bits(stream);
288
289 /* object type */
290 tmpvar = (uint32) BitstreamReadBits16(stream, 8); /* */
291
292 #ifdef PV_TOLERATE_VOL_ERRORS
293 if (tmpvar == 0)
294 {
295 if (layer) /* */
296 {
297 /* support SSPL0-2 */
298 if (profile != 0x10 && profile != 0x11 && profile != 0x12)
299 return PV_FAIL;
300 tmpvar = 0x02;
301 }
302 else
303 {
304 /* support SPL0-3 & SSPL0-2 */
305 if (profile != 0x01 && profile != 0x02 && profile != 0x03 && profile != 0x08 &&
306 profile != 0x10 && profile != 0x11 && profile != 0x12)
307 return PV_FAIL;
308 tmpvar = 0x01;
309 }
310 profile |= 0x0100;
311 }
312 #endif
313
314 if (layer)
315 {
316 if (tmpvar != 0x02) return PV_FAIL;
317 }
318 else
319 {
320 if (tmpvar != 0x01) return PV_FAIL;
321 }
322
323 /* version id specified? */
324 tmpvar = (uint32) BitstreamRead1Bits(stream);
325 if (tmpvar == 1)
326 {
327 /* version ID */
328 version_id = (uint32) BitstreamReadBits16(stream, 4);
329 /* priority */
330 tmpvar = (uint32) BitstreamReadBits16(stream, 3);
331
332 }
333
334 /* aspect ratio info */
335 tmpvar = (uint32) BitstreamReadBits16(stream, 4);
336 if (tmpvar == 0) return PV_FAIL;
337 if (tmpvar == 0xf /* extended_par */)
338 {
339 /* width */
340 tmpvar = (uint32) BitstreamReadBits16(stream, 8);
341 /* height */
342 tmpvar = (uint32) BitstreamReadBits16(stream, 8);
343 }
344
345
346 /* control parameters present? */
347 tmpvar = (uint32) BitstreamRead1Bits(stream);
348
349 /* Get the parameters (skipped) */
350 /* 03/10/99 */
351 if (tmpvar)
352 {
353 /* chroma_format */
354 tmpvar = BitstreamReadBits16(stream, 2);
355 if (tmpvar != 1) return PV_FAIL;
356 /* low_delay */
357 tmpvar = BitstreamRead1Bits(stream);
358
359 /* vbv_parameters present? */
360 tmpvar = (uint32) BitstreamRead1Bits(stream);
361 if (tmpvar)
362 {
363 /* first_half_bit_rate */
364 BitstreamReadBits16(stream, 15);
365 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
366 /* latter_half_bit_rate */
367 BitstreamReadBits16(stream, 15);
368 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
369 /* first_half_vbv_buffer_size */
370 BitstreamReadBits16(stream, 15);
371 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
372 /* latter_half_vbv_buffer_size */
373 BitstreamReadBits16(stream, 3);
374 /* first_half_vbv_occupancy */
375 BitstreamReadBits16(stream, 11);
376 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
377 /* latter_half_vbv_occupancy */
378 BitstreamReadBits16(stream, 15);
379 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
380 }
381 }
382
383 /* video_object_layer_shape (2 bits), only 00 (rect) is supported for now */
384 vol_shape = (uint32) BitstreamReadBits16(stream, 2);
385 if (vol_shape) return PV_FAIL;
386
387 /* marker bit, 03/10/99 */
388 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
389
390 /* vop_time_increment_resolution */
391 currVol->timeIncrementResolution = BitstreamReadBits16(stream, 16);
392 if (currVol->timeIncrementResolution == 0) return PV_FAIL;
393
394 /* . since nbitsTimeIncRes will be used over and over again, */
395 /* we should put it in Vol structure. 04/12/2000. */
396 currVol->nbitsTimeIncRes = CalcNumBits((uint)currVol->timeIncrementResolution - 1);
397
398 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
399
400 /* fixed_vop_rate */
401 currVol->fixedVopRate = (int) BitstreamRead1Bits(stream);
402 if (currVol->fixedVopRate)
403 {
404 /* fixed_vop_time_increment */
405 tmpvar = BitstreamReadBits16(stream, currVol->nbitsTimeIncRes);
406 }
407
408 /* marker bit */
409 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
410
411 /* video_object_layer_width (13 bits) */
412 tmpvar = BitstreamReadBits16(stream, 13);
413 if (!tmpvar) return PV_FAIL;
414 video->displayWidth = video->width = tmpvar;
415
416 /* round up to a multiple of MB_SIZE. 08/09/2000 */
417 video->width = (video->width + 15) & -16;
418 // video->displayWidth += (video->displayWidth & 0x1); /* displayed image should be even size */
419
420 /* marker bit */
421 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
422
423 /* video_object_layer_height (13 bits) */
424 tmpvar = BitstreamReadBits16(stream, 13);
425 if (!tmpvar) return PV_FAIL;
426 video->displayHeight = video->height = tmpvar;
427
428 /* round up to a multiple of MB_SIZE. 08/09/2000 */
429 video->height = (video->height + 15) & -16;
430 // video->displayHeight += (video->displayHeight & 0x1); /* displayed image should be even size */
431 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
432
433 /* 03/10/99 */
434 /* interlaced */
435 tmpvar = (uint32) BitstreamRead1Bits(stream);
436 if (tmpvar != 0)
437 {
438 mp4dec_log("DecodeVOLHeader(): Interlaced video is not supported.\n");
439 return PV_FAIL;
440 }
441
442 /* obmc_disable */
443 tmpvar = (uint32) BitstreamRead1Bits(stream);
444 if (tmpvar == 0) return PV_FAIL;
445
446 if (version_id == 1)
447 {
448 /* sprite_enable (1 bits) */
449 tmpvar = (uint32) BitstreamRead1Bits(stream);
450 if (tmpvar)
451 {
452 mp4dec_log("DecodeVOLHeader(): Sprite is not supported.\n");
453 return PV_FAIL;
454 }
455 }
456 else
457 {
458 /* For version 2, vol_sprite_usage has two bits. */
459 /* sprite_enable */
460 tmpvar = (uint32) BitstreamReadBits16(stream, 2);
461 if (tmpvar)
462 {
463 mp4dec_log("DecodeVOLHeader(): Sprite is not supported.\n");
464 return PV_FAIL;
465 }
466 }
467
468 /* not_8_bit */
469 if (BitstreamRead1Bits(stream))
470 {
471 /* quant_precision */
472 currVol->quantPrecision = BitstreamReadBits16(stream, 4);
473 /* bits_per_pixel */
474 currVol->bitsPerPixel = BitstreamReadBits16(stream, 4);
475 mp4dec_log("DecodeVOLHeader(): not an 8-bit stream.\n"); // For the time being we do not support != 8 bits
476
477 return PV_FAIL;
478 }
479 else
480 {
481 currVol->quantPrecision = 5;
482 currVol->bitsPerPixel = 8;
483 }
484
485 /* quant_type (1 bit) */
486 currVol->quantType = BitstreamRead1Bits(stream);
487 if (currVol->quantType)
488 {
489 #ifdef PV_SUPPORT_MAIN_PROFILE
490 /* load quantization matrices. 5/22/2000 */
491 /* load_intra_quant_mat (1 bit) */
492 qmat = currVol->iqmat;
493 currVol->loadIntraQuantMat = BitstreamRead1Bits(stream);
494 if (currVol->loadIntraQuantMat)
495 {
496 /* intra_quant_mat (8*64 bits) */
497 i = 0;
498 do
499 {
500 qmat[*(zigzag_inv+i)] = (int) BitstreamReadBits16(stream, 8);
501 }
502 while ((qmat[*(zigzag_inv+i)] != 0) && (++i < 64));
503
504 for (j = i; j < 64; j++)
505 qmat[*(zigzag_inv+j)] = qmat[*(zigzag_inv+i-1)];
506 }
507 else
508 {
509 oscl_memcpy(qmat, mpeg_iqmat_def, 64*sizeof(int));
510 }
511
512 qmat[0] = 0; /* necessary for switched && MPEG quant 07/09/01 */
513
514 /* load_nonintra_quant_mat (1 bit) */
515 qmat = currVol->niqmat;
516 currVol->loadNonIntraQuantMat = BitstreamRead1Bits(stream);
517 if (currVol->loadNonIntraQuantMat)
518 {
519 /* nonintra_quant_mat (8*64 bits) */
520 i = 0;
521 do
522 {
523 qmat[*(zigzag_inv+i)] = (int) BitstreamReadBits16(stream, 8);
524 }
525 while ((qmat[*(zigzag_inv+i)] != 0) && (++i < 64));
526
527 for (j = i; j < 64; j++)
528 qmat[*(zigzag_inv+j)] = qmat[*(zigzag_inv+i-1)];
529 }
530 else
531 {
532 oscl_memcpy(qmat, mpeg_nqmat_def, 64*sizeof(int));
533 }
534 #else
535 return PV_FAIL;
536 #endif
537 }
538
539 if (version_id != 1)
540 {
541 /* quarter_sample enabled */
542 tmpvar = BitstreamRead1Bits(stream);
543 if (tmpvar) return PV_FAIL;
544 }
545
546 /* complexity_estimation_disable */
547 currVol->complexity_estDisable = BitstreamRead1Bits(stream);
548 if (currVol->complexity_estDisable == 0)
549 {
550 currVol->complexity_estMethod = BitstreamReadBits16(stream, 2);
551
552 if (currVol->complexity_estMethod < 2)
553 {
554 /* shape_complexity_estimation_disable */
555 tmpvar = BitstreamRead1Bits(stream);
556 if (tmpvar == 0)
557 {
558 mp4dec_log("DecodeVOLHeader(): Shape Complexity estimation is not supported.\n");
559 return PV_FAIL;
560 }
561 /* texture_complexity_estimation_set_1_disable */
562 tmpvar = BitstreamRead1Bits(stream);
563 if (tmpvar == 0)
564 {
565 currVol->complexity.text_1 = BitstreamReadBits16(stream, 4);
566 }
567 /* marker bit */
568 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
569 /* texture_complexity_estimation_set_2_disable */
570 tmpvar = BitstreamRead1Bits(stream);
571 if (tmpvar == 0)
572 {
573 currVol->complexity.text_2 = BitstreamReadBits16(stream, 4);
574 }
575 /* motion_compensation_complexity_disable */
576 tmpvar = BitstreamRead1Bits(stream);
577 if (tmpvar == 0)
578 {
579 currVol->complexity.mc = BitstreamReadBits16(stream, 6);
580 }
581 /* marker bit */
582 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
583
584 if (currVol->complexity_estMethod == 1)
585 { /* version2_complexity_estimation_disable */
586 tmpvar = BitstreamRead1Bits(stream);
587 if (tmpvar == 0)
588 {
589 mp4dec_log("DecodeVOLHeader(): sadct, quarter pel not supported.\n");
590 return PV_FAIL;
591 }
592 }
593 }
594 }
595
596 /* 03/10/99 */
597 /* resync_marker_disable */
598 currVol->errorResDisable = (int) BitstreamRead1Bits(stream);
599 /* data_partititioned */
600 currVol->dataPartitioning = (int) BitstreamRead1Bits(stream);
601
602 video->vlcDecCoeffIntra = &VlcDecTCOEFIntra;
603 video->vlcDecCoeffInter = &VlcDecTCOEFInter;
604
605 if (currVol->dataPartitioning)
606 {
607 if (layer) return PV_FAIL; /* */
608 /* reversible_vlc */
609 currVol->useReverseVLC = (int)BitstreamRead1Bits(stream);
610 if (currVol->useReverseVLC)
611 {
612 video->vlcDecCoeffIntra = &RvlcDecTCOEFIntra;
613 video->vlcDecCoeffInter = &RvlcDecTCOEFInter;
614 }
615 currVol->errorResDisable = 0;
616 }
617 else
618 {
619 currVol->useReverseVLC = 0;
620 }
621
622 if (version_id != 1)
623 {
624 /* newpred_enable */
625 tmpvar = BitstreamRead1Bits(stream);
626 if (tmpvar) return PV_FAIL;
627
628 /* reduced_resolution_vop */
629 tmpvar = BitstreamRead1Bits(stream);
630 if (tmpvar) return PV_FAIL;
631
632 }
633
634 /* Intra AC/DC prediction is always true */
635 video->intra_acdcPredDisable = 0;
636 /* scalability */
637 currVol->scalability = (int) BitstreamRead1Bits(stream);
638
639 if (currVol->scalability)
640 {
641 if (layer == 0) return PV_FAIL; /* */
642 /* hierarchy_type: 1 : temporal, 0 : spatial */
643 /* 03/10/99 */
644 currVol->scalType = (int) BitstreamRead1Bits(stream); /* */
645 if (!currVol->scalType) return PV_FAIL;
646
647 /* ref_layer_id (4 bits) */
648 currVol->refVolID = (int) BitstreamReadBits16(stream, 4);
649 if (layer) /* */
650 {
651 if (currVol->refVolID != video->vol[0]->volID) return PV_FAIL;
652 }
653 /* ref_layer_sampling_direc (1 bits) */
654 /* 1 : ref. layer has higher resolution */
655 /* 0 : ref. layer has equal or lower resolution */
656 currVol->refSampDir = (int) BitstreamRead1Bits(stream);
657 if (currVol->refSampDir) return PV_FAIL;
658
659 /* hor_sampling_factor_n (5 bits) */
660 currVol->horSamp_n = (int) BitstreamReadBits16(stream, 5);
661
662 /* hor_sampling_factor_m (5 bits) */
663 currVol->horSamp_m = (int) BitstreamReadBits16(stream, 5);
664
665 if (currVol->horSamp_m == 0) return PV_FAIL;
666 if (currVol->horSamp_n != currVol->horSamp_m) return PV_FAIL;
667
668 /* ver_sampling_factor_n (5 bits) */
669 currVol->verSamp_n = (int) BitstreamReadBits16(stream, 5);
670
671 /* ver_sampling_factor_m (5 bits) */
672 currVol->verSamp_m = (int) BitstreamReadBits16(stream, 5);
673
674 if (currVol->verSamp_m == 0) return PV_FAIL;
675 if (currVol->verSamp_n != currVol->verSamp_m) return PV_FAIL;
676
677
678 /* enhancement_type: 1 : partial region, 0 : full region */
679 /* 04/10/2000: we only support full region enhancement layer. */
680 if (BitstreamRead1Bits(stream)) return PV_FAIL;
681 }
682
683 PV_BitstreamByteAlign(stream);
684
685 status = BitstreamShowBits32HC(stream, &tmpvar);
686
687 /* if we hit the end of buffer, tmpvar == 0. 08/30/2000 */
688 if (tmpvar == USER_DATA_START_CODE)
689 {
690 status = DecodeUserData(stream);
691 /* you should not check for status here 03/19/2002 */
692 status = PV_SUCCESS;
693 }
694
695 /* Compute some convenience variables: 04/13/2000 */
696 video->nMBPerRow = video->width / MB_SIZE;
697 video->nMBPerCol = video->height / MB_SIZE;
698 video->nTotalMB = video->nMBPerRow * video->nMBPerCol;
699 video->nBitsForMBID = CalcNumBits((uint)video->nTotalMB - 1);
700 #ifdef PV_ANNEX_IJKT_SUPPORT
701 video->modified_quant = 0;
702 video->advanced_INTRA = 0;
703 video->deblocking = 0;
704 video->slice_structure = 0;
705 #endif
706 }
707 else
708 {
709 /* SHORT_HEADER */
710 status = BitstreamShowBits32(stream, SHORT_VIDEO_START_MARKER_LENGTH, &tmpvar);
711
712 if (tmpvar == SHORT_VIDEO_START_MARKER)
713 {
714 video->shortVideoHeader = TRUE;
715 }
716 else
717 {
718 do
719 {
720 /* Search for VOL_HEADER */
721 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */
722 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
723 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
724 PV_BitstreamFlushBits(stream, 8);
725 }
726 while (tmpvar != VOL_START_CODE);
727 goto decode_vol;
728 }
729 }
730 #ifdef PV_TOLERATE_VOL_ERRORS
731 if (profile > 0xFF || profile == 0)
732 {
733 return PV_BAD_VOLHEADER;
734 }
735 #endif
736
737 return status;
738 }
739
740
741 /***********************************************************CommentBegin******
742 *
743 * -- DecodeGOV -- Decodes the Group of VOPs from bitstream
744 *
745 * 04/20/2000 initial modification to the new PV-Decoder Lib format.
746 *
747 ***********************************************************CommentEnd********/
DecodeGOVHeader(BitstreamDecVideo * stream,uint32 * time_base)748 PV_STATUS DecodeGOVHeader(BitstreamDecVideo *stream, uint32 *time_base)
749 {
750 uint32 tmpvar, time_s;
751 int closed_gov, broken_link;
752
753 /* group_start_code (32 bits) */
754 // tmpvar = BitstreamReadBits32(stream, 32);
755
756 /* hours */
757 tmpvar = (uint32) BitstreamReadBits16(stream, 5);
758 time_s = tmpvar * 3600;
759
760 /* minutes */
761 tmpvar = (uint32) BitstreamReadBits16(stream, 6);
762 time_s += tmpvar * 60;
763
764 /* marker bit */
765 tmpvar = (uint32) BitstreamRead1Bits(stream);
766
767 /* seconds */
768 tmpvar = (uint32) BitstreamReadBits16(stream, 6);
769 time_s += tmpvar;
770
771 /* We have to check the timestamp here. If the sync timestamp is */
772 /* earlier than the previous timestamp or longer than 60 sec. */
773 /* after the previous timestamp, assume the GOV header is */
774 /* corrupted. 05/12/2000 */
775 *time_base = time_s; /* 02/27/2002 */
776 // *time_base = *time_base/1000;
777 // tmpvar = time_s - *time_base;
778 // if (tmpvar <= 60) *time_base = time_s;
779 // else return PV_FAIL;
780
781 tmpvar = (uint32) BitstreamRead1Bits(stream);
782 closed_gov = tmpvar;
783 tmpvar = (uint32) BitstreamRead1Bits(stream);
784 broken_link = tmpvar;
785
786 if ((closed_gov == 0) && (broken_link == 1))
787 {
788 return PV_SUCCESS; /* 03/15/2002 you can also return PV_FAIL */
789 }
790
791 PV_BitstreamByteAlign(stream);
792
793 BitstreamShowBits32HC(stream, &tmpvar);
794
795 while (tmpvar == USER_DATA_START_CODE) /* 03/15/2002 */
796 {
797 DecodeUserData(stream);
798 BitstreamShowBits32HC(stream, &tmpvar);
799 }
800
801 return PV_SUCCESS;
802 }
803
804 /***********************************************************CommentBegin******
805 *
806 * -- DecodeVopHeader -- Decodes the VOPheader information from the bitstream
807 *
808 * 04/12/2000 Initial port to the new PV decoder library format.
809 * 05/10/2000 Error resilient decoding of vop header.
810 *
811 ***********************************************************CommentEnd********/
DecodeVOPHeader(VideoDecData * video,Vop * currVop,Bool use_ext_timestamp)812 PV_STATUS DecodeVOPHeader(VideoDecData *video, Vop *currVop, Bool use_ext_timestamp)
813 {
814 PV_STATUS status = PV_SUCCESS;
815 Vol *currVol = video->vol[video->currLayer];
816 BitstreamDecVideo *stream = currVol->bitstream;
817 uint32 tmpvar;
818 int time_base;
819
820 /*****
821 * Read the VOP header from the bitstream (No shortVideoHeader Mode here!)
822 *****/
823 BitstreamShowBits32HC(stream, &tmpvar);
824
825 /* check if we have a GOV header here. 08/30/2000 */
826 if (tmpvar == GROUP_START_CODE)
827 {
828 tmpvar = BitstreamReadBits32HC(stream);
829 // rewindBitstream(stream, START_CODE_LENGTH); /* for backward compatibility */
830 status = DecodeGOVHeader(stream, &tmpvar);
831 if (status != PV_SUCCESS)
832 {
833 return status;
834 }
835 // use_ext_timestamp = TRUE; /* 02/08/2002 */
836 /* We should have a VOP header following the GOV header. 03/15/2001 */
837 BitstreamShowBits32HC(stream, &tmpvar);
838 }
839 #ifdef PV_SUPPORT_TEMPORAL_SCALABILITY
840 currVop->timeStamp = -1;
841 #endif
842 if (tmpvar == VOP_START_CODE)
843 {
844 tmpvar = BitstreamReadBits32HC(stream);
845 }
846 else
847 {
848 PV_BitstreamFlushBits(stream, 8); // advance by a byte
849 status = PV_FAIL;
850 goto return_point;
851 }
852
853
854
855 /* vop_prediction_type (2 bits) */
856 currVop->predictionType = (int) BitstreamReadBits16(stream, 2);
857
858 /* modulo_time_base (? bits) */
859 time_base = -1;
860 do
861 {
862 time_base++;
863 tmpvar = (uint32) BitstreamRead1Bits(stream);
864 }
865 while (tmpvar == 1);
866
867
868
869 if (!use_ext_timestamp)
870 {
871 currVol->moduloTimeBase += 1000 * time_base; /* milliseconds based MTB 11/12/01 */
872 }
873
874 /* marker_bit (1 bit) */
875 if (!BitstreamRead1Bits(stream))
876 {
877 status = PV_FAIL;
878 goto return_point;
879 }
880
881 /* vop_time_increment (1-15 bits) in Nov_Compliant (1-16 bits) */
882 /* we always assumes fixed vop rate here */
883 currVop->timeInc = BitstreamReadBits16(stream, currVol->nbitsTimeIncRes);
884
885
886 /* marker_bit (1 bit) */
887 if (!BitstreamRead1Bits(stream))
888 {
889 status = PV_FAIL;
890 goto return_point;
891 }
892
893 /* vop_coded */
894 currVop->vopCoded = (int) BitstreamRead1Bits(stream);
895
896
897 if (currVop->vopCoded == 0)
898 {
899 status = PV_SUCCESS;
900 goto return_point;
901 }
902
903
904 /* read vop_rounding_type */
905 if (currVop->predictionType == P_VOP)
906 {
907 currVop->roundingType = (int) BitstreamRead1Bits(stream);
908 }
909 else
910 {
911 currVop->roundingType = 0;
912 }
913
914 if (currVol->complexity_estDisable == 0)
915 {
916 if (currVol->complexity_estMethod < 2) /* OCT 2002 */
917 {
918 if ((currVol->complexity.text_1 >> 3) & 0x1) /* intra */
919 BitstreamReadBits16(stream, 8);
920 if (currVol->complexity.text_1 & 0x1) /* not_coded */
921 BitstreamReadBits16(stream, 8);
922 if ((currVol->complexity.text_2 >> 3) & 0x1) /* dct_coefs */
923 BitstreamReadBits16(stream, 8);
924 if ((currVol->complexity.text_2 >> 2) & 0x1) /* dct_lines */
925 BitstreamReadBits16(stream, 8);
926 if ((currVol->complexity.text_2 >> 1) & 0x1) /* vlc_symbols */
927 BitstreamReadBits16(stream, 8);
928 if (currVol->complexity.text_2 & 0x1) /* vlc_bits */
929 BitstreamReadBits16(stream, 4);
930
931 if (currVop->predictionType != I_VOP)
932 {
933 if ((currVol->complexity.text_1 >> 2) & 0x1) /* inter */
934 BitstreamReadBits16(stream, 8);
935 if ((currVol->complexity.text_1 >> 1) & 0x1) /* inter_4v */
936 BitstreamReadBits16(stream, 8);
937 if ((currVol->complexity.mc >> 5) & 0x1) /* apm */
938 BitstreamReadBits16(stream, 8);
939 if ((currVol->complexity.mc >> 4) & 0x1) /* npm */
940 BitstreamReadBits16(stream, 8);
941 /* interpolate_mc_q */
942 if ((currVol->complexity.mc >> 2) & 0x1) /* forw_back_mc_q */
943 BitstreamReadBits16(stream, 8);
944 if ((currVol->complexity.mc >> 1) & 0x1) /* halfpel2 */
945 BitstreamReadBits16(stream, 8);
946 if (currVol->complexity.mc & 0x1) /* halfpel4 */
947 BitstreamReadBits16(stream, 8);
948 }
949 if (currVop->predictionType == B_VOP)
950 {
951 if ((currVol->complexity.mc >> 3) & 0x1) /* interpolate_mc_q */
952 BitstreamReadBits16(stream, 8);
953 }
954 }
955 }
956
957 /* read intra_dc_vlc_thr */
958 currVop->intraDCVlcThr = (int) BitstreamReadBits16(stream, 3);
959
960 /* read vop_quant (currVol->quantPrecision bits) */
961 currVop->quantizer = (int16) BitstreamReadBits16(stream, currVol->quantPrecision);
962 if (currVop->quantizer == 0)
963 {
964 currVop->quantizer = video->prevVop->quantizer;
965 status = PV_FAIL;
966 goto return_point;
967 }
968
969
970 /* read vop_fcode_forward */
971 if (currVop->predictionType != I_VOP)
972 {
973 tmpvar = (uint32) BitstreamReadBits16(stream, 3);
974 if (tmpvar < 1)
975 {
976 currVop->fcodeForward = 1;
977 status = PV_FAIL;
978 goto return_point;
979 }
980 currVop->fcodeForward = tmpvar;
981 }
982 else
983 {
984 currVop->fcodeForward = 0;
985 }
986
987 /* read vop_fcode_backward */
988 if (currVop->predictionType == B_VOP)
989 {
990 tmpvar = (uint32) BitstreamReadBits16(stream, 3);
991 if (tmpvar < 1)
992 {
993 currVop->fcodeBackward = 1;
994 status = PV_FAIL;
995 goto return_point;
996 }
997 currVop->fcodeBackward = tmpvar;
998 }
999 else
1000 {
1001 currVop->fcodeBackward = 0;
1002 }
1003
1004 if (currVol->scalability)
1005 {
1006 currVop->refSelectCode = (int) BitstreamReadBits16(stream, 2);
1007 }
1008
1009 return_point:
1010 return status;
1011 }
1012
1013
1014 /***********************************************************CommentBegin******
1015 *
1016 * -- VideoPlaneWithShortHeader -- Decodes the short_video_header information from the bitstream
1017 * Modified :
1018 04/23/2001. Remove the codes related to the
1019 "first pass" decoding. We use a different function
1020 to set up the decoder now.
1021 ***********************************************************CommentEnd********/
DecodeShortHeader(VideoDecData * video,Vop * currVop)1022 PV_STATUS DecodeShortHeader(VideoDecData *video, Vop *currVop)
1023 {
1024 PV_STATUS status = PV_SUCCESS;
1025 Vol *currVol = video->vol[0];
1026 BitstreamDecVideo *stream = currVol->bitstream;
1027 uint32 tmpvar;
1028 int32 size;
1029
1030 int extended_PTYPE = FALSE;
1031 int UFEP = 0, custom_PFMT = 0, custom_PCF = 0;
1032
1033 status = BitstreamShowBits32(stream, SHORT_VIDEO_START_MARKER_LENGTH, &tmpvar);
1034
1035 if (tmpvar != SHORT_VIDEO_START_MARKER)
1036 {
1037 status = PV_FAIL;
1038 goto return_point;
1039 }
1040
1041
1042 PV_BitstreamFlushBits(stream, SHORT_VIDEO_START_MARKER_LENGTH);
1043
1044 /* Temporal reference. Using vop_time_increment_resolution = 30000 */
1045 tmpvar = (uint32) BitstreamReadBits16(stream, 8);
1046 currVop->temporalRef = (int) tmpvar;
1047
1048
1049 currVop->timeInc = 0xff & (256 + currVop->temporalRef - video->prevVop->temporalRef);
1050 currVol->moduloTimeBase += currVop->timeInc; /* mseconds 11/12/01 */
1051 /* Marker Bit */
1052 if (!BitstreamRead1Bits(stream))
1053 {
1054 mp4dec_log("DecodeShortHeader(): Marker bit wrong.\n");
1055 status = PV_FAIL;
1056 goto return_point;
1057 }
1058
1059 /* Zero Bit */
1060 if (BitstreamRead1Bits(stream))
1061 {
1062 mp4dec_log("DecodeShortHeader(): Zero bit wrong.\n");
1063 status = PV_FAIL;
1064 goto return_point;
1065 }
1066
1067 /*split_screen_indicator*/
1068 if (BitstreamRead1Bits(stream))
1069 {
1070 mp4dec_log("DecodeShortHeader(): Split Screen not supported.\n");
1071 VideoDecoderErrorDetected(video);
1072 }
1073
1074 /*document_freeze_camera*/
1075 if (BitstreamRead1Bits(stream))
1076 {
1077 mp4dec_log("DecodeShortHeader(): Freeze Camera not supported.\n");
1078 VideoDecoderErrorDetected(video);
1079 }
1080
1081 /*freeze_picture_release*/
1082 if (BitstreamRead1Bits(stream))
1083 {
1084 mp4dec_log("DecodeShortHeader(): Freeze Release not supported.\n");
1085 VideoDecoderErrorDetected(video);
1086 }
1087 /* source format */
1088 switch (BitstreamReadBits16(stream, 3))
1089 {
1090 case 1:
1091 if (video->size < 128*96)
1092 {
1093 status = PV_FAIL;
1094 goto return_point;
1095 }
1096 video->displayWidth = video->width = 128;
1097 video->displayHeight = video->height = 96;
1098 break;
1099
1100 case 2:
1101 if (video->size < 176*144)
1102 {
1103 status = PV_FAIL;
1104 goto return_point;
1105 }
1106 video->displayWidth = video->width = 176;
1107 video->displayHeight = video->height = 144;
1108 break;
1109
1110 case 3:
1111 if (video->size < 352*288)
1112 {
1113 status = PV_FAIL;
1114 goto return_point;
1115 }
1116 video->displayWidth = video->width = 352;
1117 video->displayHeight = video->height = 288;
1118 break;
1119
1120 case 4:
1121 if (video->size < 704*576)
1122 {
1123 status = PV_FAIL;
1124 goto return_point;
1125 }
1126 video->displayWidth = video->width = 704;
1127 video->displayHeight = video->height = 576;
1128 break;
1129
1130 case 5:
1131 if (video->size < 1408*1152)
1132 {
1133 status = PV_FAIL;
1134 goto return_point;
1135 }
1136 video->displayWidth = video->width = 1408;
1137 video->displayHeight = video->height = 1152;
1138 break;
1139
1140 case 7:
1141 extended_PTYPE = TRUE;
1142 break;
1143
1144 default:
1145 /* Msg("H.263 source format not legal\n"); */
1146 status = PV_FAIL;
1147 goto return_point;
1148 }
1149
1150
1151 currVop->roundingType = 0;
1152
1153 if (extended_PTYPE == FALSE)
1154 {
1155 currVop->predictionType = (int) BitstreamRead1Bits(stream);
1156
1157 /* four_reserved_zero_bits */
1158 if (BitstreamReadBits16(stream, 4))
1159 {
1160 mp4dec_log("DecodeShortHeader(): Reserved bits wrong.\n");
1161 status = PV_FAIL;
1162 goto return_point;
1163 }
1164 }
1165 else
1166 {
1167 UFEP = BitstreamReadBits16(stream, 3);
1168 if (UFEP == 1)
1169 {
1170 /* source format */
1171 switch (BitstreamReadBits16(stream, 3))
1172 {
1173 case 1:
1174 if (video->size < 128*96)
1175 {
1176 status = PV_FAIL;
1177 goto return_point;
1178 }
1179 video->displayWidth = video->width = 128;
1180 video->displayHeight = video->height = 96;
1181 break;
1182
1183 case 2:
1184 if (video->size < 176*144)
1185 {
1186 status = PV_FAIL;
1187 goto return_point;
1188 }
1189 video->displayWidth = video->width = 176;
1190 video->displayHeight = video->height = 144;
1191 break;
1192
1193 case 3:
1194 if (video->size < 352*288)
1195 {
1196 status = PV_FAIL;
1197 goto return_point;
1198 }
1199 video->displayWidth = video->width = 352;
1200 video->displayHeight = video->height = 288;
1201 break;
1202
1203 case 4:
1204 if (video->size < 704*576)
1205 {
1206 status = PV_FAIL;
1207 goto return_point;
1208 }
1209 video->displayWidth = video->width = 704;
1210 video->displayHeight = video->height = 576;
1211 break;
1212
1213 case 5:
1214 if (video->size < 1408*1152)
1215 {
1216 status = PV_FAIL;
1217 goto return_point;
1218 }
1219 video->displayWidth = video->width = 1408;
1220 video->displayHeight = video->height = 1152;
1221 break;
1222
1223 case 6:
1224 custom_PFMT = TRUE;
1225 break;
1226
1227 default:
1228 /* Msg("H.263 source format not legal\n"); */
1229 status = PV_FAIL;
1230 goto return_point;
1231 }
1232
1233 custom_PCF = BitstreamRead1Bits(stream);
1234 /* unrestricted MV */
1235 if (BitstreamRead1Bits(stream))
1236 {
1237 status = PV_FAIL;
1238 goto return_point;
1239 }
1240 /* SAC */
1241 if (BitstreamRead1Bits(stream))
1242 {
1243 status = PV_FAIL;
1244 goto return_point;
1245 }
1246
1247 /* AP */
1248 if (BitstreamRead1Bits(stream))
1249 {
1250 status = PV_FAIL;
1251 goto return_point;
1252 }
1253
1254 video->advanced_INTRA = BitstreamRead1Bits(stream);
1255
1256 video->deblocking = BitstreamRead1Bits(stream);
1257
1258 video->slice_structure = BitstreamRead1Bits(stream);
1259
1260 /* RPS, ISD, AIV */
1261 if (BitstreamReadBits16(stream, 3))
1262 {
1263 status = PV_FAIL;
1264 goto return_point;
1265 }
1266 video->modified_quant = BitstreamRead1Bits(stream);
1267
1268 /* Marker Bit and reserved*/
1269 if (BitstreamReadBits16(stream, 4) != 8)
1270 {
1271 status = PV_FAIL;
1272 goto return_point;
1273 }
1274 }
1275 #ifndef PV_ANNEX_IJKT_SUPPORT
1276 if (video->advanced_INTRA | video->deblocking | video->modified_quant | video->modified_quant)
1277 {
1278 status = PV_FAIL;
1279 goto return_point;
1280 }
1281 #endif
1282
1283 if (UFEP == 0 || UFEP == 1)
1284 {
1285 tmpvar = BitstreamReadBits16(stream, 3);
1286 if (tmpvar > 1)
1287 {
1288 status = PV_FAIL;
1289 goto return_point;
1290 }
1291 currVop->predictionType = tmpvar;
1292 /* RPR */
1293 if (BitstreamRead1Bits(stream))
1294 {
1295 status = PV_FAIL;
1296 goto return_point;
1297 }
1298
1299 /* RRU */
1300 if (BitstreamRead1Bits(stream))
1301 {
1302 status = PV_FAIL;
1303 goto return_point;
1304 }
1305 currVop->roundingType = (int) BitstreamRead1Bits(stream);
1306 if (BitstreamReadBits16(stream, 3) != 1)
1307 {
1308 status = PV_FAIL;
1309 goto return_point;
1310 }
1311 }
1312 else
1313 {
1314 status = PV_FAIL;
1315 goto return_point;
1316 }
1317 /* CPM */
1318 if (BitstreamRead1Bits(stream))
1319 {
1320 status = PV_FAIL;
1321 goto return_point;
1322 }
1323 /* CPFMT */
1324 if (custom_PFMT == 1 && UFEP == 1)
1325 {
1326 /* aspect ratio */
1327 tmpvar = BitstreamReadBits16(stream, 4);
1328 if (tmpvar == 0)
1329 {
1330 status = PV_FAIL;
1331 goto return_point;
1332 }
1333 /* Extended PAR */
1334 if (tmpvar == 0xF)
1335 {
1336 /* Read par_width and par_height but do nothing */
1337 /* par_width */
1338 tmpvar = BitstreamReadBits16(stream, 8);
1339
1340 /* par_height */
1341 tmpvar = BitstreamReadBits16(stream, 8);
1342 }
1343 tmpvar = BitstreamReadBits16(stream, 9);
1344
1345 int tmpDisplayWidth = (tmpvar + 1) << 2;
1346 /* marker bit */
1347 if (!BitstreamRead1Bits(stream))
1348 {
1349 status = PV_FAIL;
1350 goto return_point;
1351 }
1352 tmpvar = BitstreamReadBits16(stream, 9);
1353 if (tmpvar == 0)
1354 {
1355 status = PV_FAIL;
1356 goto return_point;
1357 }
1358 int tmpDisplayHeight = tmpvar << 2;
1359 int tmpHeight = (tmpDisplayHeight + 15) & -16;
1360 int tmpWidth = (tmpDisplayWidth + 15) & -16;
1361
1362 if (tmpWidth > video->width)
1363 {
1364 // while allowed by the spec, this decoder does not actually
1365 // support an increase in size.
1366 ALOGE("width increase not supported");
1367 status = PV_FAIL;
1368 goto return_point;
1369 }
1370 if (tmpHeight * tmpWidth > video->size)
1371 {
1372 // This is just possibly "b/37079296".
1373 ALOGE("b/37079296");
1374 status = PV_FAIL;
1375 goto return_point;
1376 }
1377 video->displayWidth = tmpDisplayWidth;
1378 video->width = tmpWidth;
1379 video->displayHeight = tmpDisplayHeight;
1380 video->height = tmpHeight;
1381
1382 video->nTotalMB = video->width / MB_SIZE * video->height / MB_SIZE;
1383
1384 if (video->nTotalMB <= 48)
1385 {
1386 video->nBitsForMBID = 6;
1387 }
1388 else if (video->nTotalMB <= 99)
1389 {
1390 video->nBitsForMBID = 7;
1391 }
1392 else if (video->nTotalMB <= 396)
1393 {
1394 video->nBitsForMBID = 9;
1395 }
1396 else if (video->nTotalMB <= 1584)
1397 {
1398 video->nBitsForMBID = 11;
1399 }
1400 else if (video->nTotalMB <= 6336)
1401 {
1402 video->nBitsForMBID = 13 ;
1403 }
1404 else if (video->nTotalMB <= 9216)
1405 {
1406 video->nBitsForMBID = 14 ;
1407 }
1408 else
1409 {
1410 status = PV_FAIL;
1411 goto return_point;
1412 }
1413 }
1414 if (UFEP == 1 && custom_PCF == 1)
1415 {
1416 BitstreamRead1Bits(stream);
1417
1418 tmpvar = BitstreamReadBits16(stream, 7);
1419 if (tmpvar == 0)
1420 {
1421 status = PV_FAIL;
1422 goto return_point;
1423 }
1424 }
1425
1426 if (custom_PCF == 1)
1427 {
1428 currVop->ETR = BitstreamReadBits16(stream, 2);
1429 }
1430
1431 if (UFEP == 1 && video->slice_structure == 1)
1432 {
1433 /* SSS */
1434 tmpvar = BitstreamReadBits16(stream, 2);
1435 if (tmpvar != 0)
1436 {
1437 status = PV_FAIL;
1438 goto return_point;
1439 }
1440 }
1441 }
1442
1443 /* Recalculate number of macroblocks per row & col since */
1444 /* the frame size can change. 04/23/2001. */
1445 video->nMBinGOB = video->nMBPerRow = video->width / MB_SIZE;
1446 video->nGOBinVop = video->nMBPerCol = video->height / MB_SIZE;
1447 video->nTotalMB = video->nMBPerRow * video->nMBPerCol;
1448 if (custom_PFMT == 0 || UFEP == 0)
1449 {
1450 video->nBitsForMBID = CalcNumBits((uint)video->nTotalMB - 1); /* otherwise calculate above */
1451 }
1452 size = (int32)video->width * video->height;
1453 if (currVop->predictionType == P_VOP && size > video->videoDecControls->size)
1454 {
1455 status = PV_FAIL;
1456 goto return_point;
1457 }
1458 video->videoDecControls->size = size;
1459 video->currVop->uChan = video->currVop->yChan + size;
1460 video->currVop->vChan = video->currVop->uChan + (size >> 2);
1461 video->prevVop->uChan = video->prevVop->yChan + size;
1462 video->prevVop->vChan = video->prevVop->uChan + (size >> 2);
1463
1464
1465 currVop->quantizer = (int16) BitstreamReadBits16(stream, 5);
1466
1467 if (currVop->quantizer == 0) /* 04/03/01 */
1468 {
1469 currVop->quantizer = video->prevVop->quantizer;
1470 status = PV_FAIL;
1471 goto return_point;
1472 }
1473
1474
1475 /* Zero bit */
1476 if (extended_PTYPE == FALSE)
1477 {
1478 if (BitstreamRead1Bits(stream))
1479 {
1480 mp4dec_log("DecodeShortHeader(): Zero bit wrong.\n");
1481 status = PV_FAIL;
1482 goto return_point;
1483 }
1484 }
1485 /* pei */
1486 tmpvar = (uint32) BitstreamRead1Bits(stream);
1487
1488 while (tmpvar)
1489 {
1490 tmpvar = (uint32) BitstreamReadBits16(stream, 8); /* "PSPARE" */
1491 tmpvar = (uint32) BitstreamRead1Bits(stream); /* "PEI" */
1492 }
1493
1494 if (video->slice_structure) /* ANNEX_K */
1495 {
1496 if (!BitstreamRead1Bits(stream)) /* SEPB1 */
1497 {
1498 status = PV_FAIL;
1499 goto return_point;
1500 }
1501
1502 // if (currVol->nBitsForMBID //
1503 if (BitstreamReadBits16(stream, video->nBitsForMBID))
1504 {
1505 status = PV_FAIL; /* no ASO, RS support for Annex K */
1506 goto return_point;
1507 }
1508
1509 if (!BitstreamRead1Bits(stream)) /*SEPB3 */
1510 {
1511 status = PV_FAIL;
1512 goto return_point;
1513 }
1514
1515 }
1516 /* Setting of other VOP-header parameters */
1517 currVop->gobNumber = 0;
1518 currVop->vopCoded = 1;
1519
1520 currVop->intraDCVlcThr = 0;
1521 currVop->gobFrameID = 0; /* initial value, 05/22/00 */
1522 currVol->errorResDisable = 0;
1523 /*PutVopInterlaced(0,curr_vop); no implemented yet */
1524 if (currVop->predictionType != I_VOP)
1525 currVop->fcodeForward = 1;
1526 else
1527 currVop->fcodeForward = 0;
1528
1529 return_point:
1530
1531 return status;
1532 }
1533 /***********************************************************CommentBegin******
1534 *
1535 * -- PV_DecodeVop -- Decodes the VOP information from the bitstream
1536 *
1537 * 04/12/2000
1538 * Initial port to the new PV decoder library format.
1539 * This function is different from the one in MoMuSys MPEG-4
1540 * visual decoder. We handle combined mode with or withput
1541 * error resilience and H.263 mode through the sam path now.
1542 *
1543 * 05/04/2000
1544 * Added temporal scalability to the decoder.
1545 *
1546 ***********************************************************CommentEnd********/
PV_DecodeVop(VideoDecData * video)1547 PV_STATUS PV_DecodeVop(VideoDecData *video)
1548 {
1549 Vol *currVol = video->vol[video->currLayer];
1550 PV_STATUS status;
1551 uint32 tmpvar;
1552
1553 /*****
1554 * Do scalable or non-scalable decoding of the current VOP
1555 *****/
1556
1557 if (!currVol->scalability)
1558 {
1559 if (currVol->dataPartitioning)
1560 {
1561 /* Data partitioning mode comes here */
1562 status = DecodeFrameDataPartMode(video);
1563 }
1564 else
1565 {
1566 /* Combined mode with or without error resilience */
1567 /* and short video header comes here. */
1568 status = DecodeFrameCombinedMode(video);
1569 }
1570 }
1571 else
1572 {
1573 #ifdef DO_NOT_FOLLOW_STANDARD
1574 /* according to the standard, only combined mode is allowed */
1575 /* in the enhancement layer. 06/01/2000. */
1576 if (currVol->dataPartitioning)
1577 {
1578 /* Data partitioning mode comes here */
1579 status = DecodeFrameDataPartMode(video);
1580 }
1581 else
1582 {
1583 /* Combined mode with or without error resilience */
1584 /* and short video header comes here. */
1585 status = DecodeFrameCombinedMode(video);
1586 }
1587 #else
1588 status = DecodeFrameCombinedMode(video);
1589 #endif
1590 }
1591
1592 /* This part is for consuming Visual_object_sequence_end_code and EOS Code */ /* 10/15/01 */
1593 if (!video->shortVideoHeader)
1594 {
1595 /* at this point bitstream is expected to be byte aligned */
1596 BitstreamByteAlignNoForceStuffing(currVol->bitstream);
1597
1598 status = BitstreamShowBits32HC(currVol->bitstream, &tmpvar); /* 07/07/01 */
1599 if (tmpvar == VISUAL_OBJECT_SEQUENCE_END_CODE)/* VOS_END_CODE */
1600 {
1601 PV_BitstreamFlushBits(currVol->bitstream, 16);
1602 PV_BitstreamFlushBits(currVol->bitstream, 16);
1603 }
1604
1605 }
1606 else
1607 {
1608 #ifdef PV_ANNEX_IJKT_SUPPORT
1609 if (video->deblocking)
1610 {
1611 H263_Deblock(video->currVop->yChan, video->width, video->height, video->QPMB, video->headerInfo.Mode, 0, 0);
1612 H263_Deblock(video->currVop->uChan, video->width >> 1, video->height >> 1, video->QPMB, video->headerInfo.Mode, 1, video->modified_quant);
1613 H263_Deblock(video->currVop->vChan, video->width >> 1, video->height >> 1, video->QPMB, video->headerInfo.Mode, 1, video->modified_quant);
1614 }
1615 #endif
1616 /* Read EOS code for shortheader bitstreams */
1617 status = BitstreamShowBits32(currVol->bitstream, 22, &tmpvar);
1618 if (tmpvar == SHORT_VIDEO_END_MARKER)
1619 {
1620 PV_BitstreamFlushBits(currVol->bitstream, 22);
1621 }
1622 else
1623 {
1624 status = PV_BitstreamShowBitsByteAlign(currVol->bitstream, 22, &tmpvar);
1625 if (tmpvar == SHORT_VIDEO_END_MARKER)
1626 {
1627 PV_BitstreamByteAlign(currVol->bitstream);
1628 PV_BitstreamFlushBits(currVol->bitstream, 22);
1629 }
1630 }
1631 }
1632 return status;
1633 }
1634
1635
1636 /***********************************************************CommentBegin******
1637 *
1638 * -- CalcVopDisplayTime -- calculate absolute time when VOP is to be displayed
1639 *
1640 * 04/12/2000 Initial port to the new PV decoder library format.
1641 *
1642 ***********************************************************CommentEnd********/
CalcVopDisplayTime(Vol * currVol,Vop * currVop,int shortVideoHeader)1643 uint32 CalcVopDisplayTime(Vol *currVol, Vop *currVop, int shortVideoHeader)
1644 {
1645 uint32 display_time;
1646
1647
1648 /*****
1649 * Calculate the time when the VOP is to be displayed next
1650 *****/
1651
1652 if (!shortVideoHeader)
1653 {
1654 display_time = (uint32)(currVol->moduloTimeBase + (((int32)currVop->timeInc - (int32)currVol->timeInc_offset) * 1000) / ((int32)currVol->timeIncrementResolution)); /* 11/12/2001 */
1655 if (currVop->timeStamp >= display_time)
1656 {
1657 display_time += 1000; /* this case is valid if GOVHeader timestamp is ignored */
1658 }
1659 }
1660 else
1661 {
1662 display_time = (uint32)(currVol->moduloTimeBase * 33 + (currVol->moduloTimeBase * 11) / 30); /* 11/12/2001 */
1663 }
1664
1665 return(display_time);
1666 }
1667
1668