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