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