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