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