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