• 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 /*
19 *     -------------------------------------------------------------------       *
20 *                    MPEG-4 Simple Profile Video Decoder                        *
21 *     -------------------------------------------------------------------       *
22 *
23 * This software module was originally developed by
24 *
25 *   Paulo Nunes (IST / ACTS-MoMuSyS)
26 *   Robert Danielsen (Telenor / ACTS-MoMuSyS)
27 *
28 * in the course of development of the MPEG-4 Video (ISO/IEC 14496-2) standard.
29 * This software module is an implementation of a part of one or more MPEG-4
30 * Video (ISO/IEC 14496-2) tools as specified by the MPEG-4 Video (ISO/IEC
31 * 14496-2) standard.
32 *
33 * ISO/IEC gives users of the MPEG-4 Video (ISO/IEC 14496-2) standard free
34 * license to this software module or modifications thereof for use in hardware
35 * or software products claiming conformance to the MPEG-4 Video (ISO/IEC
36 * 14496-2) standard.
37 *
38 * Those intending to use this software module in hardware or software products
39 * are advised that its use may infringe existing patents. The original
40 * developer of this software module and his/her company, the subsequent
41 * editors and their companies, and ISO/IEC have no liability for use of this
42 * software module or modifications thereof in an implementation. Copyright is
43 * not released for non MPEG-4 Video (ISO/IEC 14496-2) Standard conforming
44 * products.
45 *
46 * ACTS-MoMuSys partners retain full right to use the code for his/her own
47 * purpose, assign or donate the code to a third party and to inhibit third
48 * parties from using the code for non MPEG-4 Video (ISO/IEC 14496-2) Standard
49 * conforming products. This copyright notice must be included in all copies or
50 * derivative works.
51 *
52 * Copyright (c) 1996
53 *
54 *****************************************************************************/
55 
56 /***********************************************************HeaderBegin*******
57 *
58 * File: vlc_dec.c
59 *
60 * Author:   Paulo Nunes (IST) - Paulo.Nunes@lx.it.pt
61 * Created:  1-Mar-96
62 *
63 * Description: This file contains the VLC functions needed to decode a
64 *       bitstream.
65 *
66 * Notes:
67 *       The functions contained in this file were adapted from
68 *       tmndecode
69 *       Written by Karl Olav Lillevold <kol@nta.no>,
70 *       1995 Telenor R&D.
71 *       Donated to the Momusys-project as background code by
72 *       Telenor.
73 *
74 *       based on mpeg2decode, (C) 1994, MPEG Software Simulation Group
75 *       and mpeg2play, (C) 1994 Stefan Eckart
76 *                   <stefan@lis.e-technik.tu-muenchen.de>
77 *
78 *
79 * Modified: 9-May-96 Paulo Nunes: Reformatted. New headers.
80 *              17-Jan-97 Jan De Lameillieure (HHI) : corrected in
81 *              01.05.97 Luis Ducla-Soares: added RvlcDecTCOEF() to allow decoding
82 *                                          of Reversible VLCs.
83 *       09.03.98 Paulo Nunes: Cleaning.
84 *
85 ***********************************************************HeaderEnd*********/
86 
87 #include "mp4dec_lib.h"
88 #include "vlc_dec_tab.h"
89 #include "vlc_decode.h"
90 #include "bitstream.h"
91 #include "max_level.h"
92 
93 
94 /* ====================================================================== /
95     Function : DecodeUserData()
96     Date     : 04/10/2000
97     History  :
98     Modified : 04/16/2001 : removed status checking of PV_BitstreamFlushBits
99 
100         This is simply a realization of the user_data() function
101         in the ISO/IEC 14496-2 manual.
102 / ====================================================================== */
DecodeUserData(BitstreamDecVideo * stream)103 PV_STATUS DecodeUserData(BitstreamDecVideo *stream)
104 {
105     PV_STATUS status;
106     uint32 code;
107 
108     BitstreamReadBits32HC(stream);
109     BitstreamShowBits32(stream, 24, &code);
110 
111     while (code != 1)
112     {
113         /* Discard user data for now.   04/05/2000 */
114         BitstreamReadBits16(stream, 8);
115         BitstreamShowBits32(stream, 24, &code);
116         status = BitstreamCheckEndBuffer(stream);
117         if (status == PV_END_OF_VOP) return status;    /*  03/19/2002 */
118     }
119     return PV_SUCCESS;
120 }
121 
122 
123 
124 /***********************************************************CommentBegin******
125 *
126 *       3/10/00  : initial modification to the
127 *                new PV-Decoder Lib format.
128 *       3/29/00  : added return code check to some functions and
129 *                optimize the code.
130 *
131 ***********************************************************CommentEnd********/
PV_GetMBvectors(VideoDecData * video,uint mode)132 PV_STATUS PV_GetMBvectors(VideoDecData *video, uint mode)
133 {
134     PV_STATUS status;
135     BitstreamDecVideo *stream = video->bitstream;
136     int  f_code_f = video->currVop->fcodeForward;
137     int  vlc_code_mag;
138 
139 
140     MOT *mot_x = video->motX;
141     MOT *mot_y = video->motY;
142 
143     int k, offset;
144     int x_pos = video->mbnum_col;
145     int y_pos = video->mbnum_row;
146     int doubleWidth = video->nMBPerRow << 1;
147     int pos = (x_pos + y_pos * doubleWidth) << 1;
148     MOT mvx = 0, mvy = 0;
149 
150 
151     if (f_code_f == 1)
152     {
153 #ifdef PV_ANNEX_IJKT_SUPPORT
154         if (mode == MODE_INTER4V || mode == MODE_INTER4V_Q)
155 #else
156         if (mode == MODE_INTER4V)
157 #endif
158         {
159             for (k = 0; k < 4; k++)
160             {
161                 offset = (k & 1) + (k >> 1) * doubleWidth;
162                 mv_prediction(video, k, &mvx, &mvy);
163                 /* decode component x */
164                 status = PV_VlcDecMV(stream, &vlc_code_mag);
165                 if (status != PV_SUCCESS)
166                 {
167                     return status;
168                 }
169 
170                 mvx += (MOT)vlc_code_mag;
171                 mvx = (MOT)(((mvx + 32) & 0x3F) - 32);
172 
173 
174                 status = PV_VlcDecMV(stream, &vlc_code_mag);
175                 if (status != PV_SUCCESS)
176                 {
177                     return status;
178                 }
179 
180                 mvy += (MOT)vlc_code_mag;
181                 mvy = (MOT)(((mvy + 32) & 0x3F) - 32);
182 
183                 mot_x[pos+offset] = (MOT) mvx;
184                 mot_y[pos+offset] = (MOT) mvy;
185             }
186         }
187         else
188         {
189             mv_prediction(video, 0, &mvx, &mvy);
190             /* For PVOPs, field  appears only in MODE_INTER & MODE_INTER_Q */
191             status = PV_VlcDecMV(stream, &vlc_code_mag);
192             if (status != PV_SUCCESS)
193             {
194                 return status;
195             }
196 
197             mvx += (MOT)vlc_code_mag;
198             mvx = (MOT)(((mvx + 32) & 0x3F) - 32);
199 
200 
201             status = PV_VlcDecMV(stream, &vlc_code_mag);
202             if (status != PV_SUCCESS)
203             {
204                 return status;
205             }
206 
207 
208             mvy += (MOT)vlc_code_mag;
209             mvy = (MOT)(((mvy + 32) & 0x3F) - 32);
210 
211 
212             mot_x[pos] = mot_x[pos+1] = (MOT) mvx;
213             mot_y[pos] = mot_y[pos+1] = (MOT) mvy;
214             pos += doubleWidth;
215             mot_x[pos] = mot_x[pos+1] = (MOT) mvx;
216             mot_y[pos] = mot_y[pos+1] = (MOT) mvy;
217         }
218     }
219     else
220     {
221 #ifdef PV_ANNEX_IJKT_SUPPORT
222         if (mode == MODE_INTER4V || mode == MODE_INTER4V_Q)
223 #else
224         if (mode == MODE_INTER4V)
225 #endif
226         {
227             for (k = 0; k < 4; k++)
228             {
229                 offset = (k & 1) + (k >> 1) * doubleWidth;
230                 mv_prediction(video, k, &mvx, &mvy);
231                 status = PV_DecodeMBVec(stream, &mvx, &mvy, f_code_f);
232                 mot_x[pos+offset] = (MOT) mvx;
233                 mot_y[pos+offset] = (MOT) mvy;
234                 if (status != PV_SUCCESS)
235                 {
236                     return status;
237                 }
238             }
239         }
240         else
241         {
242             mv_prediction(video, 0, &mvx, &mvy);
243             /* For PVOPs, field  appears only in MODE_INTER & MODE_INTER_Q */
244             status = PV_DecodeMBVec(stream, &mvx, &mvy, f_code_f);
245             mot_x[pos] = mot_x[pos+1] = (MOT) mvx;
246             mot_y[pos] = mot_y[pos+1] = (MOT) mvy;
247             pos += doubleWidth;
248             mot_x[pos] = mot_x[pos+1] = (MOT) mvx;
249             mot_y[pos] = mot_y[pos+1] = (MOT) mvy;
250             if (status != PV_SUCCESS)
251             {
252                 return status;
253             }
254         }
255     }
256     return PV_SUCCESS;
257 }
258 
259 
260 /***********************************************************CommentBegin******
261 *       3/10/00  : initial modification to the
262 *                new PV-Decoder Lib format.
263 *       3/29/00  : added return code check to some functions
264 *       5/10/00  : check whether the decoded vector is legal.
265 *       4/17/01  : use MOT type
266 ***********************************************************CommentEnd********/
PV_DecodeMBVec(BitstreamDecVideo * stream,MOT * mv_x,MOT * mv_y,int f_code_f)267 PV_STATUS PV_DecodeMBVec(BitstreamDecVideo *stream, MOT *mv_x, MOT *mv_y, int f_code_f)
268 {
269     PV_STATUS status;
270     int  vlc_code_magx, vlc_code_magy;
271     int  residualx = 0, residualy = 0;
272 
273     /* decode component x */
274     status = PV_VlcDecMV(stream, &vlc_code_magx);
275     if (status != PV_SUCCESS)
276     {
277         return status;
278     }
279 
280     if (vlc_code_magx)
281     {
282         residualx = (int) BitstreamReadBits16_INLINE(stream, (int)(f_code_f - 1));
283     }
284 
285 
286     /* decode component y */
287     status = PV_VlcDecMV(stream, &vlc_code_magy);
288     if (status != PV_SUCCESS)
289     {
290         return status;
291     }
292 
293     if (vlc_code_magy)
294     {
295         residualy = (int) BitstreamReadBits16_INLINE(stream, (int)(f_code_f - 1));
296     }
297 
298 
299     if (PV_DeScaleMVD(f_code_f, residualx, vlc_code_magx, mv_x) != PV_SUCCESS)
300     {
301         return PV_FAIL;
302     }
303 
304     if (PV_DeScaleMVD(f_code_f, residualy, vlc_code_magy, mv_y) != PV_SUCCESS)
305     {
306         return PV_FAIL;
307     }
308 
309     return PV_SUCCESS;
310 }
311 
312 
313 /***********************************************************CommentBegin******
314 *       3/31/2000 : initial modification to the new PV-Decoder Lib format.
315 *       5/10/2000 : check to see if the decoded vector falls within
316 *                           the legal fcode range.
317 *
318 ***********************************************************CommentEnd********/
PV_DeScaleMVD(int f_code,int residual,int vlc_code_mag,MOT * vector)319 PV_STATUS PV_DeScaleMVD(
320     int  f_code,       /* <-- MV range in 1/2 units: 1=32,2=64,...,7=2048     */
321     int  residual,     /* <-- part of the MV Diff. FLC coded                  */
322     int  vlc_code_mag, /* <-- part of the MV Diff. VLC coded                  */
323     MOT  *vector       /* --> Obtained MV component in 1/2 units              */
324 )
325 {
326     int   half_range = (1 << (f_code + 4));
327     int   mask = (half_range << 1) - 1;
328     int   diff_vector;
329 
330 
331     if (vlc_code_mag == 0)
332     {
333         diff_vector = vlc_code_mag;
334     }
335     else
336     {
337         diff_vector = ((PV_ABS(vlc_code_mag) - 1) << (f_code - 1)) + residual + 1;
338         if (vlc_code_mag < 0)
339         {
340             diff_vector = -diff_vector;
341         }
342     }
343 
344     *vector += (MOT)(diff_vector);
345 
346     *vector = (MOT)((*vector + half_range) & mask) - half_range;
347 
348     return PV_SUCCESS;
349 }
350 
351 
352 
mv_prediction(VideoDecData * video,int block,MOT * mvx,MOT * mvy)353 void mv_prediction(
354     VideoDecData *video,
355     int block,
356     MOT *mvx,
357     MOT *mvy
358 )
359 {
360     /*----------------------------------------------------------------------------
361     ; Define all local variables
362     ----------------------------------------------------------------------------*/
363     MOT *motxdata = video->motX;
364     MOT *motydata = video->motY;
365     int mbnum_col = video->mbnum_col;
366     int mbnum_row = video->mbnum_row;
367     uint8 *slice_nb = video->sliceNo;
368     int nMBPerRow = video->nMBPerRow;
369     int nMVPerRow = nMBPerRow << 1;
370     int mbnum = video->mbnum;
371     int p1x = 0, p2x = 0, p3x = 0;
372     int p1y = 0, p2y = 0, p3y = 0;
373     int rule1 = 0, rule2 = 0, rule3 = 0;
374     int     indx;
375 
376     indx = ((mbnum_col << 1) + (block & 1)) + ((mbnum_row << 1)  + (block >> 1)) * nMVPerRow - 1; /* left block */
377 
378     if (block & 1)           /* block 1, 3 */
379     {
380         p1x = motxdata[indx];
381         p1y = motydata[indx];
382         rule1 = 1;
383     }
384     else                    /* block 0, 2 */
385     {
386         if (mbnum_col > 0 && slice_nb[mbnum] == slice_nb[mbnum-1])
387         {
388             p1x = motxdata[indx];
389             p1y = motydata[indx];
390             rule1 = 1;
391         }
392     }
393 
394     indx = indx + 1 - nMVPerRow; /* upper_block */
395     if (block >> 1)
396     {
397         indx -= (block & 1);
398         p2x = motxdata[indx];
399         p2y = motydata[indx];
400         p3x = motxdata[indx + 1];
401         p3y = motydata[indx + 1];
402         rule2 = rule3 = 1;
403     }
404     else
405     {                           /* block 0,1 */
406         if (mbnum_row)
407         {
408             if (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow])
409             {
410                 p2x = motxdata[indx];
411                 p2y = motydata[indx];
412                 rule2 = 1;
413             }
414             if (mbnum_col < nMBPerRow - 1 && slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow+1])
415             {
416                 indx = indx + 2 - (block & 1);
417                 p3x = motxdata[indx];
418                 p3y = motydata[indx];
419                 rule3 = 1;
420             }
421         }
422     }
423 
424     if (rule1 + rule2 + rule3 > 1)
425     {
426         *mvx = (MOT)PV_MEDIAN(p1x, p2x, p3x);
427         *mvy = (MOT)PV_MEDIAN(p1y, p2y, p3y);
428     }
429     else if (rule1 + rule2 + rule3 == 1)
430     {
431         /* two of three are zero */
432         *mvx = (MOT)(p1x + p2x + p3x);
433         *mvy = (MOT)(p1y + p2y + p3y);
434     }
435     else
436     {
437         /* all MBs are outside the VOP */
438         *mvx = *mvy = 0;
439     }
440     /*----------------------------------------------------------------------------
441     ; Return nothing or data or data pointer
442     ----------------------------------------------------------------------------*/
443     return;
444 }
445 
446 /***********************************************************CommentBegin******
447 *
448 *       3/30/2000 : initial modification to the new PV-Decoder Lib format.
449 *       4/16/2001 : removed checking of status for PV_BitstreamFlushBits
450 ***********************************************************CommentEnd********/
451 
PV_VlcDecMV(BitstreamDecVideo * stream,int * mv)452 PV_STATUS PV_VlcDecMV(BitstreamDecVideo *stream, int *mv)
453 {
454     PV_STATUS status = PV_SUCCESS;
455     uint code;
456 
457     BitstreamShow13Bits(stream, &code);
458 
459     if (code >> 12)
460     {
461         *mv = 0; /* Vector difference = 0 */
462         PV_BitstreamFlushBits(stream, 1);
463         return PV_SUCCESS;
464     }
465 
466     if (code >= 512)
467     {
468         code = (code >> 8) - 2;
469         PV_BitstreamFlushBits(stream, PV_TMNMVtab0[code].len + 1);
470         *mv = PV_TMNMVtab0[code].val;
471         return status;
472     }
473 
474     if (code >= 128)
475     {
476         code = (code >> 2) - 32;
477         PV_BitstreamFlushBits(stream, PV_TMNMVtab1[code].len + 1);
478         *mv = PV_TMNMVtab1[code].val;
479         return status;
480     }
481 
482     if (code < 4)
483     {
484         *mv = -1;
485         return PV_FAIL;
486     }
487 
488     code -= 4;
489 
490     PV_BitstreamFlushBits(stream, PV_TMNMVtab2[code].len + 1);
491 
492     *mv = PV_TMNMVtab2[code].val;
493     return status;
494 }
495 
496 
497 /***********************************************************CommentBegin******
498 *       3/30/2000 : initial modification to the new PV-Decoder Lib
499 *                           format and the change of error-handling method.
500 *       4/16/01   : removed status checking of PV_BitstreamFlushBits
501 ***********************************************************CommentEnd********/
502 
PV_VlcDecMCBPC_com_intra(BitstreamDecVideo * stream)503 int PV_VlcDecMCBPC_com_intra(BitstreamDecVideo *stream)
504 {
505     uint code;
506 
507     BitstreamShowBits16(stream, 9, &code);
508 
509 
510     if (code < 8)
511     {
512         return VLC_CODE_ERROR;
513     }
514 
515     code >>= 3;
516 
517     if (code >= 32)
518     {
519         PV_BitstreamFlushBits(stream, 1);
520         return 3;
521     }
522 
523     PV_BitstreamFlushBits(stream, PV_MCBPCtabintra[code].len);
524 
525     return PV_MCBPCtabintra[code].val;
526 }
527 
528 
529 /***********************************************************CommentBegin******
530 *
531 *       3/30/2000 : initial modification to the new PV-Decoder Lib
532 *                           format and the change of error-handling method.
533 *       4/16/2001 : removed checking of return status of PV_BitstreamFlushBits
534 ***********************************************************CommentEnd********/
535 
PV_VlcDecMCBPC_com_inter(BitstreamDecVideo * stream)536 int PV_VlcDecMCBPC_com_inter(BitstreamDecVideo *stream)
537 {
538     uint code;
539 
540     BitstreamShowBits16(stream, 9, &code);
541 
542     if (code == 0)
543     {
544         return VLC_CODE_ERROR;
545     }
546     else if (code >= 256)
547     {
548         PV_BitstreamFlushBits(stream, 1);
549         return 0;
550     }
551 
552     PV_BitstreamFlushBits(stream, PV_MCBPCtab[code].len);
553     return PV_MCBPCtab[code].val;
554 }
555 
556 #ifdef PV_ANNEX_IJKT_SUPPORT
PV_VlcDecMCBPC_com_inter_H263(BitstreamDecVideo * stream)557 int PV_VlcDecMCBPC_com_inter_H263(BitstreamDecVideo *stream)
558 {
559     uint code;
560 
561     BitstreamShow13Bits(stream, &code);
562 
563     if (code < 8)
564     {
565         return VLC_CODE_ERROR;
566     }
567     else if (code >= 4096)
568     {
569         PV_BitstreamFlushBits(stream, 1);
570         return 0;
571     }
572     if (code >= 16)
573     {
574         PV_BitstreamFlushBits(stream, PV_MCBPCtab[code >> 4].len);
575         return PV_MCBPCtab[code >> 4].val;
576     }
577     else
578     {
579         PV_BitstreamFlushBits(stream, PV_MCBPCtab1[code - 8].len);
580         return PV_MCBPCtab1[code - 8].val;
581     }
582 }
583 #endif
584 /***********************************************************CommentBegin******
585 *       3/30/2000 : initial modification to the new PV-Decoder Lib
586 *                           format and the change of error-handling method.
587 *       4/16/2001 : removed status checking for PV_BitstreamFlushBits
588 ***********************************************************CommentEnd********/
589 
PV_VlcDecCBPY(BitstreamDecVideo * stream,int intra)590 int PV_VlcDecCBPY(BitstreamDecVideo *stream, int intra)
591 {
592     int CBPY = 0;
593     uint code;
594 
595     BitstreamShowBits16(stream, 6, &code);
596 
597 
598     if (code < 2)
599     {
600         return -1;
601     }
602     else if (code >= 48)
603     {
604         PV_BitstreamFlushBits(stream, 2);
605         CBPY = 15;
606     }
607     else
608     {
609         PV_BitstreamFlushBits(stream, PV_CBPYtab[code].len);
610         CBPY = PV_CBPYtab[code].val;
611     }
612 
613     if (intra == 0) CBPY = 15 - CBPY;
614     CBPY = CBPY & 15;
615     return CBPY;
616 }
617 
618 
619 /***********************************************************CommentBegin******
620 *       3/31/2000 : initial modification to the new PV-Decoder Lib format.
621 *
622 *       8/23/2000 : optimize the function by removing unnecessary BitstreamShowBits()
623 *                       function calls.
624 *
625 *       9/6/2000 : change the API to check for end-of-buffer for proper
626 *                           termination of decoding process.
627 ***********************************************************CommentEnd********/
PV_VlcDecIntraDCPredSize(BitstreamDecVideo * stream,int compnum,uint * DC_size)628 PV_STATUS PV_VlcDecIntraDCPredSize(BitstreamDecVideo *stream, int compnum, uint *DC_size)
629 {
630     PV_STATUS status = PV_FAIL;      /*  07/09/01 */
631     uint  code;
632 
633     *DC_size = 0;
634     if (compnum < 4)  /* luminance block */
635     {
636 
637         BitstreamShowBits16(stream, 11, &code);
638 
639         if (code == 1)
640         {
641             *DC_size = 12;
642             PV_BitstreamFlushBits(stream, 11);
643             return PV_SUCCESS;
644         }
645         code >>= 1;
646         if (code == 1)
647         {
648             *DC_size = 11;
649             PV_BitstreamFlushBits(stream, 10);
650             return PV_SUCCESS;
651         }
652         code >>= 1;
653         if (code == 1)
654         {
655             *DC_size = 10;
656             PV_BitstreamFlushBits(stream, 9);
657             return PV_SUCCESS;
658         }
659 
660         code >>= 1;
661         if (code == 1)
662         {
663             *DC_size = 9;
664             PV_BitstreamFlushBits(stream, 8);
665             return PV_SUCCESS;
666         }
667 
668         code >>= 1;
669         if (code == 1)
670         {
671             *DC_size = 8;
672             PV_BitstreamFlushBits(stream, 7);
673             return PV_SUCCESS;
674         }
675 
676         code >>= 1;
677         if (code == 1)
678         {
679             *DC_size = 7;
680             PV_BitstreamFlushBits(stream, 6);
681             return PV_SUCCESS;
682         }
683 
684         code >>= 1;
685         if (code == 1)
686         {
687             *DC_size = 6;
688             PV_BitstreamFlushBits(stream, 5);
689             return PV_SUCCESS;
690         }
691 
692         code >>= 1;
693         if (code == 1)
694         {
695             *DC_size = 5;
696             PV_BitstreamFlushBits(stream, 4);
697             return PV_SUCCESS;
698         }
699 
700         code >>= 1;
701         if (code == 1)
702         {
703             *DC_size = 4;
704             PV_BitstreamFlushBits(stream, 3);
705             return PV_SUCCESS;
706         }
707         else if (code == 2)
708         {
709             *DC_size = 3;
710             PV_BitstreamFlushBits(stream, 3);
711             return PV_SUCCESS;
712         }
713         else if (code == 3)
714         {
715             *DC_size = 0;
716             PV_BitstreamFlushBits(stream, 3);
717             return PV_SUCCESS;
718         }
719 
720         code >>= 1;
721         if (code == 2)
722         {
723             *DC_size = 2;
724             PV_BitstreamFlushBits(stream, 2);
725             return PV_SUCCESS;
726         }
727         else if (code == 3)
728         {
729             *DC_size = 1;
730             PV_BitstreamFlushBits(stream, 2);
731             return PV_SUCCESS;
732         }
733     }
734     else /* chrominance block */
735     {
736 
737         BitstreamShow13Bits(stream, &code);
738         code >>= 1;
739         if (code == 1)
740         {
741             *DC_size = 12;
742             PV_BitstreamFlushBits(stream, 12);
743             return PV_SUCCESS;
744         }
745 
746         code >>= 1;
747         if (code == 1)
748         {
749             *DC_size = 11;
750             PV_BitstreamFlushBits(stream, 11);
751             return PV_SUCCESS;
752         }
753 
754         code >>= 1;
755         if (code == 1)
756         {
757             *DC_size = 10;
758             PV_BitstreamFlushBits(stream, 10);
759             return PV_SUCCESS;
760         }
761 
762         code >>= 1;
763         if (code == 1)
764         {
765             *DC_size = 9;
766             PV_BitstreamFlushBits(stream, 9);
767             return PV_SUCCESS;
768         }
769 
770         code >>= 1;
771         if (code == 1)
772         {
773             *DC_size = 8;
774             PV_BitstreamFlushBits(stream, 8);
775             return PV_SUCCESS;
776         }
777 
778         code >>= 1;
779         if (code == 1)
780         {
781             *DC_size = 7;
782             PV_BitstreamFlushBits(stream, 7);
783             return PV_SUCCESS;
784         }
785 
786         code >>= 1;
787         if (code == 1)
788         {
789             *DC_size = 6;
790             PV_BitstreamFlushBits(stream, 6);
791             return PV_SUCCESS;
792         }
793 
794         code >>= 1;
795         if (code == 1)
796         {
797             *DC_size = 5;
798             PV_BitstreamFlushBits(stream, 5);
799             return PV_SUCCESS;
800         }
801 
802         code >>= 1;
803         if (code == 1)
804         {
805             *DC_size = 4;
806             PV_BitstreamFlushBits(stream, 4);
807             return PV_SUCCESS;
808         }
809 
810         code >>= 1;
811         if (code == 1)
812         {
813             *DC_size = 3;
814             PV_BitstreamFlushBits(stream, 3);
815             return PV_SUCCESS;
816         }
817 
818         code >>= 1;
819         {
820             *DC_size = (int)(3 - code);
821             PV_BitstreamFlushBits(stream, 2);
822             return PV_SUCCESS;
823         }
824     }
825 
826     return status;
827 }
828 
829 /***********************************************************CommentBegin******
830 *
831 *
832 *       3/30/2000 : initial modification to the new PV-Decoder Lib
833 *                           format and the change of error-handling method.
834 *
835 ***********************************************************CommentEnd********/
836 
837 
838 
VlcDecTCOEFIntra(BitstreamDecVideo * stream,Tcoef * pTcoef)839 PV_STATUS VlcDecTCOEFIntra(BitstreamDecVideo *stream, Tcoef *pTcoef)
840 {
841     uint code;
842     const VLCtab2 *tab;
843 
844     BitstreamShow13Bits(stream, &code);
845 
846     /* 10/17/2000, perform a little bit better on ARM by putting the whole function in VlcDecTCOEFFIntra */
847     /*  if(GetTcoeffIntra(code,pTcoef,&tab,stream)!=PV_SUCCESS) return status;*/
848     if (code >= 1024)
849     {
850         tab = &PV_DCT3Dtab3[(code >> 6) - 16];
851     }
852     else
853     {
854         if (code >= 256)
855         {
856             tab = &PV_DCT3Dtab4[(code >> 3) - 32];
857         }
858         else
859         {
860             if (code >= 16)
861             {
862                 tab = &PV_DCT3Dtab5[(code>>1) - 8];
863             }
864             else
865             {
866                 return PV_FAIL;
867             }
868         }
869     }
870 
871     PV_BitstreamFlushBits(stream, tab->len + 1);
872     pTcoef->sign = (code >> (12 - tab->len)) & 1;
873     pTcoef->run = (uint) tab->run; //(tab->val >> 8) & 255;
874     pTcoef->level = (int) tab->level; //tab->val & 255;
875     pTcoef->last = (uint) tab->last; //(tab->val >> 16) & 1;
876 
877 
878     /* the following is modified for 3-mode escape -- boon */
879     if (tab->level != 0xFF)
880     {
881         return PV_SUCCESS;
882     }
883 
884     //if (((tab->run<<8)|(tab->level)|(tab->last<<16)) == VLC_ESCAPE_CODE)
885 
886     if (!pTcoef->sign)
887     {
888         /* first escape mode. level is offset */
889         BitstreamShow13Bits(stream, &code);
890 
891         /* 10/17/2000, perform a little bit better on ARM by putting the whole function in VlcDecTCOEFFIntra */
892         /*          if(GetTcoeffIntra(code,pTcoef,&tab,stream)!=PV_SUCCESS) return status;*/
893         if (code >= 1024)
894         {
895             tab = &PV_DCT3Dtab3[(code >> 6) - 16];
896         }
897         else
898         {
899             if (code >= 256)
900             {
901                 tab = &PV_DCT3Dtab4[(code >> 3) - 32];
902             }
903             else
904             {
905                 if (code >= 16)
906                 {
907                     tab = &PV_DCT3Dtab5[(code>>1) - 8];
908                 }
909                 else
910                 {
911                     return PV_FAIL;
912                 }
913             }
914         }
915 
916         PV_BitstreamFlushBits(stream, tab->len + 1);
917 
918         /* sign bit */
919         pTcoef->sign = (code >> (12 - tab->len)) & 1;
920         pTcoef->run = (uint)tab->run; //(tab->val >> 8) & 255;
921         pTcoef->level = (int)tab->level; //tab->val & 255;
922         pTcoef->last = (uint)tab->last; //(tab->val >> 16) & 1;
923 
924 
925         /* need to add back the max level */
926         if ((pTcoef->last == 0 && pTcoef->run > 14) || (pTcoef->last == 1 && pTcoef->run > 20))
927         {
928             return PV_FAIL;
929         }
930         pTcoef->level = pTcoef->level + intra_max_level[pTcoef->last][pTcoef->run];
931 
932 
933     }
934     else
935     {
936         uint run_offset;
937         run_offset = BitstreamRead1Bits_INLINE(stream);
938 
939         if (!run_offset)
940         {
941             /* second escape mode. run is offset */
942             BitstreamShow13Bits(stream, &code);
943 
944             /* 10/17/2000, perform a little bit better on ARM by putting the whole function in VlcDecTCOEFFIntra */
945             /*              if(GetTcoeffIntra(code,pTcoef,&tab,stream)!=PV_SUCCESS) return status;*/
946             if (code >= 1024)
947             {
948                 tab = &PV_DCT3Dtab3[(code >> 6) - 16];
949             }
950             else
951             {
952                 if (code >= 256)
953                 {
954                     tab = &PV_DCT3Dtab4[(code >> 3) - 32];
955                 }
956                 else
957                 {
958                     if (code >= 16)
959                     {
960                         tab = &PV_DCT3Dtab5[(code>>1) - 8];
961                     }
962                     else
963                     {
964                         return PV_FAIL;
965                     }
966                 }
967             }
968 
969             PV_BitstreamFlushBits(stream, tab->len + 1);
970             /* sign bit */
971             pTcoef->sign = (code >> (12 - tab->len)) & 1;
972             pTcoef->run = (uint)tab->run; //(tab->val >> 8) & 255;
973             pTcoef->level = (int)tab->level; //tab->val & 255;
974             pTcoef->last = (uint)tab->last; //(tab->val >> 16) & 1;
975 
976 
977 
978             /* need to add back the max run */
979             if (pTcoef->last)
980             {
981                 if (pTcoef->level > 8)
982                 {
983                     return PV_FAIL;
984                 }
985                 pTcoef->run = pTcoef->run + intra_max_run1[pTcoef->level] + 1;
986             }
987             else
988             {
989                 if (pTcoef->level > 27)
990                 {
991                     return PV_FAIL;
992                 }
993                 pTcoef->run = pTcoef->run + intra_max_run0[pTcoef->level] + 1;
994             }
995 
996 
997         }
998         else
999         {
1000 
1001             code = BitstreamReadBits16_INLINE(stream, 8);
1002             pTcoef->last = code >> 7;
1003             pTcoef->run = (code >> 1) & 0x3F;
1004             pTcoef->level = (int)(BitstreamReadBits16_INLINE(stream, 13) >> 1);
1005 
1006             if (pTcoef->level >= 2048)
1007             {
1008                 pTcoef->sign = 1;
1009                 pTcoef->level = 4096 - pTcoef->level;
1010             }
1011             else
1012             {
1013                 pTcoef->sign = 0;
1014             }
1015         } /* flc */
1016     }
1017 
1018     return PV_SUCCESS;
1019 
1020 } /* VlcDecTCOEFIntra */
1021 
VlcDecTCOEFInter(BitstreamDecVideo * stream,Tcoef * pTcoef)1022 PV_STATUS VlcDecTCOEFInter(BitstreamDecVideo *stream, Tcoef *pTcoef)
1023 {
1024     uint code;
1025     const VLCtab2 *tab;
1026 
1027     BitstreamShow13Bits(stream, &code);
1028 
1029     /* 10/17/2000, perform a little bit better on ARM by putting the whole function in VlcDecTCOEFFInter */
1030     /*  if(GetTcoeffInter(code,pTcoef,&tab,stream)!=PV_SUCCESS) return status;*/
1031     if (code >= 1024)
1032     {
1033         tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1034     }
1035     else
1036     {
1037         if (code >= 256)
1038         {
1039             tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1040         }
1041         else
1042         {
1043             if (code >= 16)
1044             {
1045                 tab = &PV_DCT3Dtab2[(code>>1) - 8];
1046             }
1047             else
1048             {
1049                 return PV_FAIL;
1050             }
1051         }
1052     }
1053     PV_BitstreamFlushBits(stream, tab->len + 1);
1054     pTcoef->sign = (code >> (12 - tab->len)) & 1;
1055     pTcoef->run = (uint)tab->run;     //(tab->val >> 4) & 255;
1056     pTcoef->level = (int)tab->level; //tab->val & 15;
1057     pTcoef->last = (uint)tab->last;   //(tab->val >> 12) & 1;
1058 
1059     /* the following is modified for 3-mode escape -- boon */
1060     if (tab->run != 0xBF)
1061     {
1062         return PV_SUCCESS;
1063     }
1064     //if (((tab->run<<4)|(tab->level)|(tab->last<<12)) == VLC_ESCAPE_CODE)
1065 
1066 
1067     if (!pTcoef->sign)
1068     {
1069         /* first escape mode. level is offset */
1070         BitstreamShow13Bits(stream, &code);
1071 
1072         /* 10/17/2000, perform a little bit better on ARM by putting the whole function in VlcDecTCOEFFInter */
1073         /*          if(GetTcoeffInter(code,pTcoef,&tab,stream)!=PV_SUCCESS) return status;*/
1074         if (code >= 1024)
1075         {
1076             tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1077         }
1078         else
1079         {
1080             if (code >= 256)
1081             {
1082                 tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1083             }
1084             else
1085             {
1086                 if (code >= 16)
1087                 {
1088                     tab = &PV_DCT3Dtab2[(code>>1) - 8];
1089                 }
1090                 else
1091                 {
1092                     return PV_FAIL;
1093                 }
1094             }
1095         }
1096         PV_BitstreamFlushBits(stream, tab->len + 1);
1097         pTcoef->sign = (code >> (12 - tab->len)) & 1;
1098         pTcoef->run = (uint)tab->run;     //(tab->val >> 4) & 255;
1099         pTcoef->level = (int)tab->level; //tab->val & 15;
1100         pTcoef->last = (uint)tab->last;   //(tab->val >> 12) & 1;
1101 
1102         /* need to add back the max level */
1103         if ((pTcoef->last == 0 && pTcoef->run > 26) || (pTcoef->last == 1 && pTcoef->run > 40))
1104         {
1105             return PV_FAIL;
1106         }
1107         pTcoef->level = pTcoef->level + inter_max_level[pTcoef->last][pTcoef->run];
1108     }
1109     else
1110     {
1111         uint run_offset;
1112         run_offset = BitstreamRead1Bits_INLINE(stream);
1113 
1114         if (!run_offset)
1115         {
1116             /* second escape mode. run is offset */
1117             BitstreamShow13Bits(stream, &code);
1118 
1119             /* 10/17/2000, perform a little bit better on ARM by putting the whole function in VlcDecTCOEFFInter */
1120             /*if(GetTcoeffInter(code,pTcoef,&tab,stream)!=PV_SUCCESS) return status;*/
1121             if (code >= 1024)
1122             {
1123                 tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1124             }
1125             else
1126             {
1127                 if (code >= 256)
1128                 {
1129                     tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1130                 }
1131                 else
1132                 {
1133                     if (code >= 16)
1134                     {
1135                         tab = &PV_DCT3Dtab2[(code>>1) - 8];
1136                     }
1137                     else
1138                     {
1139                         return PV_FAIL;
1140                     }
1141                 }
1142             }
1143             PV_BitstreamFlushBits(stream, tab->len + 1);
1144             pTcoef->sign = (code >> (12 - tab->len)) & 1;
1145             pTcoef->run = (uint)tab->run;     //(tab->val >> 4) & 255;
1146             pTcoef->level = (int)tab->level; //tab->val & 15;
1147             pTcoef->last = (uint)tab->last;   //(tab->val >> 12) & 1;
1148 
1149             /* need to add back the max run */
1150             if (pTcoef->last)
1151             {
1152                 if (pTcoef->level > 3)
1153                 {
1154                     return PV_FAIL;
1155                 }
1156                 pTcoef->run = pTcoef->run + inter_max_run1[pTcoef->level] + 1;
1157             }
1158             else
1159             {
1160                 if (pTcoef->level > 12)
1161                 {
1162                     return PV_FAIL;
1163                 }
1164                 pTcoef->run = pTcoef->run + inter_max_run0[pTcoef->level] + 1;
1165             }
1166         }
1167         else
1168         {
1169 
1170             code = BitstreamReadBits16_INLINE(stream, 8);
1171             pTcoef->last = code >> 7;
1172             pTcoef->run = (code >> 1) & 0x3F;
1173             pTcoef->level = (int)(BitstreamReadBits16_INLINE(stream, 13) >> 1);
1174 
1175 
1176 
1177             if (pTcoef->level >= 2048)
1178             {
1179                 pTcoef->sign = 1;
1180                 pTcoef->level = 4096 - pTcoef->level;
1181             }
1182             else
1183             {
1184                 pTcoef->sign = 0;
1185             }
1186         } /* flc */
1187     }
1188 
1189     return PV_SUCCESS;
1190 
1191 } /* VlcDecTCOEFInter */
1192 
1193 /*=======================================================
1194     Function:   VlcDecTCOEFShortHeader()
1195     Date    :   04/27/99
1196     Purpose :   New function used in decoding of video planes
1197                 with short header
1198     Modified:   05/23/2000
1199                 for new decoder structure.
1200 =========================================================*/
VlcDecTCOEFShortHeader(BitstreamDecVideo * stream,Tcoef * pTcoef)1201 PV_STATUS VlcDecTCOEFShortHeader(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1202 {
1203     uint code;
1204     const VLCtab2 *tab;
1205 
1206     BitstreamShow13Bits(stream, &code);
1207 
1208     /*intra = 0;*/
1209 
1210     if (code >= 1024) tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1211     else
1212     {
1213         if (code >= 256) tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1214         else
1215         {
1216             if (code >= 16) tab = &PV_DCT3Dtab2[(code>>1) - 8];
1217             else return PV_FAIL;
1218         }
1219     }
1220 
1221     PV_BitstreamFlushBits(stream, tab->len + 1);
1222     pTcoef->sign = (code >> (12 - tab->len)) & 1;
1223     pTcoef->run = (uint)tab->run;//(tab->val >> 4) & 255;
1224     pTcoef->level = (int)tab->level;//tab->val & 15;
1225     pTcoef->last = (uint)tab->last;//(tab->val >> 12) & 1;
1226 
1227     /* the following is modified for 3-mode escape -- boon */
1228     if (((tab->run << 4) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE)    /* ESCAPE */
1229     {
1230         return PV_SUCCESS;
1231     }
1232 
1233 
1234     /* escape mode 4 - H.263 type */
1235     pTcoef->last = pTcoef->sign; /* Last */
1236     pTcoef->run = BitstreamReadBits16_INLINE(stream, 6); /* Run */
1237     pTcoef->level = (int) BitstreamReadBits16_INLINE(stream, 8); /* Level */
1238 
1239     if (pTcoef->level == 0 || pTcoef->level == 128)
1240     {
1241         return PV_FAIL;
1242     }
1243 
1244     if (pTcoef->level > 128)
1245     {
1246         pTcoef->sign = 1;
1247         pTcoef->level = 256 - pTcoef->level;
1248     }
1249     else
1250     {
1251         pTcoef->sign = 0;
1252     }
1253 
1254 
1255 
1256     return PV_SUCCESS;
1257 
1258 }   /* VlcDecTCOEFShortHeader */
1259 
1260 #ifdef PV_ANNEX_IJKT_SUPPORT
VlcDecTCOEFShortHeader_AnnexI(BitstreamDecVideo * stream,Tcoef * pTcoef)1261 PV_STATUS VlcDecTCOEFShortHeader_AnnexI(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1262 {
1263     uint code;
1264     const VLCtab2 *tab;
1265 
1266     BitstreamShow13Bits(stream, &code);
1267 
1268     /*intra = 0;*/
1269 
1270     if (code >= 1024) tab = &PV_DCT3Dtab6[(code >> 6) - 16];
1271     else
1272     {
1273         if (code >= 256) tab = &PV_DCT3Dtab7[(code >> 3) - 32];
1274         else
1275         {
1276             if (code >= 16) tab = &PV_DCT3Dtab8[(code>>1) - 8];
1277             else return PV_FAIL;
1278         }
1279     }
1280 
1281     PV_BitstreamFlushBits(stream, tab->len + 1);
1282     pTcoef->sign = (code >> (12 - tab->len)) & 1;
1283     pTcoef->run = (uint)tab->run;//(tab->val >> 4) & 255;
1284     pTcoef->level = (int)tab->level;//tab->val & 15;
1285     pTcoef->last = (uint)tab->last;//(tab->val >> 12) & 1;
1286 
1287     /* the following is modified for 3-mode escape -- boon */
1288     if (((tab->run << 6) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE)    /* ESCAPE */
1289     {
1290         return PV_SUCCESS;
1291     }
1292     /* escape mode 4 - H.263 type */
1293     pTcoef->last = pTcoef->sign; /* Last */
1294     pTcoef->run = BitstreamReadBits16(stream, 6); /* Run */
1295     pTcoef->level = (int) BitstreamReadBits16(stream, 8); /* Level */
1296 
1297     if (pTcoef->level == 0 || pTcoef->level == 128)
1298     {
1299         return PV_FAIL;
1300     }
1301 
1302 
1303     if (pTcoef->level > 128)
1304     {
1305         pTcoef->sign = 1;
1306         pTcoef->level = 256 - pTcoef->level;
1307     }
1308     else pTcoef->sign = 0;
1309 
1310 
1311 
1312     return PV_SUCCESS;
1313 
1314 }   /* VlcDecTCOEFShortHeader_AnnexI */
1315 
VlcDecTCOEFShortHeader_AnnexT(BitstreamDecVideo * stream,Tcoef * pTcoef)1316 PV_STATUS VlcDecTCOEFShortHeader_AnnexT(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1317 {
1318     uint code;
1319     const VLCtab2 *tab;
1320 
1321     BitstreamShow13Bits(stream, &code);
1322 
1323     /*intra = 0;*/
1324 
1325     if (code >= 1024) tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1326     else
1327     {
1328         if (code >= 256) tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1329         else
1330         {
1331             if (code >= 16) tab = &PV_DCT3Dtab2[(code>>1) - 8];
1332             else return PV_FAIL;
1333         }
1334     }
1335 
1336     PV_BitstreamFlushBits(stream, tab->len + 1);
1337     pTcoef->sign = (code >> (12 - tab->len)) & 1;
1338     pTcoef->run = (uint)tab->run;//(tab->val >> 4) & 255;
1339     pTcoef->level = (int)tab->level;//tab->val & 15;
1340     pTcoef->last = (uint)tab->last;//(tab->val >> 12) & 1;
1341 
1342     /* the following is modified for 3-mode escape --  */
1343     if (((tab->run << 4) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE)    /* ESCAPE */
1344     {
1345         return PV_SUCCESS;
1346     }
1347     /* escape mode 4 - H.263 type */
1348     pTcoef->last = pTcoef->sign; /* Last */
1349     pTcoef->run = BitstreamReadBits16(stream, 6); /* Run */
1350     pTcoef->level = (int) BitstreamReadBits16(stream, 8); /* Level */
1351 
1352     if (pTcoef->level == 0)
1353     {
1354         return PV_FAIL;
1355     }
1356 
1357     if (pTcoef->level >= 128)
1358     {
1359         pTcoef->sign = 1;
1360         pTcoef->level = 256 - pTcoef->level;
1361     }
1362     else
1363     {
1364         pTcoef->sign = 0;
1365     }
1366 
1367     if (pTcoef->level == 128)
1368     {
1369         code = BitstreamReadBits16(stream, 11);        /* ANNEX_T */
1370 
1371         code = (code >> 6 & 0x1F) | (code << 5 & 0x7ff);
1372         if (code > 1024)
1373         {
1374             pTcoef->sign = 1;
1375             pTcoef->level = (2048 - code);
1376         }
1377         else
1378         {
1379             pTcoef->sign = 0;
1380             pTcoef->level = code;
1381         }
1382     }
1383 
1384     return PV_SUCCESS;
1385 
1386 }   /* VlcDecTCOEFShortHeader */
1387 
1388 
VlcDecTCOEFShortHeader_AnnexIT(BitstreamDecVideo * stream,Tcoef * pTcoef)1389 PV_STATUS VlcDecTCOEFShortHeader_AnnexIT(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1390 {
1391     uint code;
1392     const VLCtab2 *tab;
1393 
1394     BitstreamShow13Bits(stream, &code);
1395 
1396     /*intra = 0;*/
1397 
1398     if (code >= 1024) tab = &PV_DCT3Dtab6[(code >> 6) - 16];
1399     else
1400     {
1401         if (code >= 256) tab = &PV_DCT3Dtab7[(code >> 3) - 32];
1402         else
1403         {
1404             if (code >= 16) tab = &PV_DCT3Dtab8[(code>>1) - 8];
1405             else return PV_FAIL;
1406         }
1407     }
1408 
1409     PV_BitstreamFlushBits(stream, tab->len + 1);
1410     pTcoef->sign = (code >> (12 - tab->len)) & 1;
1411     pTcoef->run = (uint)tab->run;//(tab->val >> 4) & 255;
1412     pTcoef->level = (int)tab->level;//tab->val & 15;
1413     pTcoef->last = (uint)tab->last;//(tab->val >> 12) & 1;
1414 
1415     /* the following is modified for 3-mode escape --  */
1416     if (((tab->run << 6) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE)    /* ESCAPE */
1417     {
1418         return PV_SUCCESS;
1419     }
1420     /* escape mode 4 - H.263 type */
1421     pTcoef->last = pTcoef->sign; /* Last */
1422     pTcoef->run = BitstreamReadBits16(stream, 6); /* Run */
1423     pTcoef->level = (int) BitstreamReadBits16(stream, 8); /* Level */
1424 
1425     if (pTcoef->level == 0)
1426     {
1427         return PV_FAIL;
1428     }
1429 
1430     if (pTcoef->level >= 128)
1431     {
1432         pTcoef->sign = 1;
1433         pTcoef->level = 256 - pTcoef->level;
1434     }
1435     else
1436     {
1437         pTcoef->sign = 0;
1438     }
1439 
1440     if (pTcoef->level == 128)
1441     {
1442         code = BitstreamReadBits16(stream, 11);        /* ANNEX_T */
1443 
1444         code = (code >> 6 & 0x1F) | (code << 5 & 0x7ff);
1445         if (code > 1024)
1446         {
1447             pTcoef->sign = 1;
1448             pTcoef->level = (2048 - code);
1449         }
1450         else
1451         {
1452             pTcoef->sign = 0;
1453             pTcoef->level = code;
1454         }
1455     }
1456 
1457 
1458     return PV_SUCCESS;
1459 
1460 }   /* VlcDecTCOEFShortHeader_AnnexI */
1461 #endif
1462 /***********************************************************CommentBegin******
1463 *       3/30/2000 : initial modification to the new PV-Decoder Lib
1464 *                           format and the change of error-handling method.
1465 *                           The coefficient is now returned thru a pre-
1466 *                           initialized parameters for speedup.
1467 *
1468 ***********************************************************CommentEnd********/
1469 
1470 
RvlcDecTCOEFInter(BitstreamDecVideo * stream,Tcoef * pTcoef)1471 PV_STATUS RvlcDecTCOEFInter(BitstreamDecVideo *stream, Tcoef *pTcoef)
1472 {
1473     uint code, mask;
1474     const VLCtab2 *tab2;
1475     int count, len, num[2] = {0, 0} /*  01/30/01 */;
1476 
1477     mask = 0x4000;      /* mask  100000000000000   */
1478     BitstreamShow15Bits(stream, &code);   /*  03/07/01 */
1479 
1480     len = 1;
1481 
1482     //  09/20/99 Escape mode
1483     /// Bitstream Exchange
1484     if (code < 2048)
1485     {
1486         PV_BitstreamFlushBits(stream, 5);
1487         pTcoef->last = BitstreamRead1Bits_INLINE(stream);
1488         pTcoef->run = BitstreamReadBits16_INLINE(stream, 6);
1489         //  09/20/99 New marker bit
1490         PV_BitstreamFlushBits(stream, 1);
1491         //  09/20/99 The length for LEVEL used to be 7 in the old version
1492         pTcoef->level = (int)(BitstreamReadBits16_INLINE(stream, 12) >> 1);
1493         //  09/20/99 Another new marker bit
1494 //      PV_BitstreamFlushBitsCheck(stream, 1);
1495         pTcoef->sign = BitstreamReadBits16_INLINE(stream, 5) & 0x1;  /* fix   3/13/01  */
1496         return PV_SUCCESS;
1497     }
1498 
1499     if (code & mask)
1500     {
1501         count = 1;
1502         while (mask && count > 0)       /* fix  3/28/01  */
1503         {
1504             mask = mask >> 1;
1505             if (code & mask)
1506                 count--;
1507             else
1508                 num[0]++; /* number of zeros in the middle */
1509             len++;
1510         }
1511     }
1512     else
1513     {
1514         count = 2;
1515         while (mask && count > 0)           /* fix  3/28/01  */
1516         {
1517             mask = mask >> 1;
1518             if (!(code & mask))
1519                 count--;
1520             else
1521                 num[count-1]++; /* number of ones in the middle */
1522             len++;
1523         }
1524     }
1525 
1526     code = code & 0x7fff;
1527     code = code >> (15 - (len + 1));
1528 
1529     /*  1/30/01, add fast decoding algorithm here */
1530     /* code is in two forms : 0xxxx0xxx00 or 0xxx0xxx01
1531                          num[1] and num[0] x
1532                         or  : 1xxxxx10 or 1xxxxx11
1533                                 num[0]  x      */
1534 
1535     /* len+1 is the length of the above */
1536 
1537     if (num[1] > 10 || num[0] > 11) /* invalid RVLC code */
1538         return PV_FAIL;
1539 
1540     if (code&(1 << len))
1541         tab2 = RvlcDCTtabInter + 146 + (num[0] << 1) + (code & 1);
1542     else
1543         tab2 = RvlcDCTtabInter + ptrRvlcTab[num[1]] + (num[0] << 1) + (code & 1);
1544 
1545     PV_BitstreamFlushBits(stream, (int) tab2->len);
1546     pTcoef->run = (uint)tab2->run;//(tab->val >> 8) & 255;
1547     pTcoef->level = (int)tab2->level;//tab->val & 255;
1548     pTcoef->last = (uint)tab2->last;//(tab->val >> 16) & 1;
1549 
1550     pTcoef->sign = BitstreamRead1Bits_INLINE(stream);
1551     return PV_SUCCESS;
1552 }               /* RvlcDecTCOEFInter */
1553 
RvlcDecTCOEFIntra(BitstreamDecVideo * stream,Tcoef * pTcoef)1554 PV_STATUS RvlcDecTCOEFIntra(BitstreamDecVideo *stream, Tcoef *pTcoef)
1555 {
1556     uint code, mask;
1557     const VLCtab2 *tab2;
1558     int count, len, num[2] = {0, 0} /*  01/30/01 */;
1559 
1560     mask = 0x4000;      /* mask  100000000000000   */
1561     BitstreamShow15Bits(stream, &code);
1562 
1563     len = 1;
1564 
1565     //  09/20/99 Escape mode
1566     /// Bitstream Exchange
1567     if (code < 2048)
1568     {
1569         PV_BitstreamFlushBits(stream, 5);
1570         pTcoef->last = BitstreamRead1Bits_INLINE(stream);
1571         pTcoef->run = BitstreamReadBits16_INLINE(stream, 6);
1572         //  09/20/99 New marker bit
1573         PV_BitstreamFlushBits(stream, 1);
1574         //  09/20/99 The length for LEVEL used to be 7 in the old version
1575         pTcoef->level = (int)(BitstreamReadBits16_INLINE(stream, 12) >> 1);
1576         //  09/20/99 Another new marker bit
1577 //      PV_BitstreamFlushBitsCheck(stream, 1);
1578         pTcoef->sign = BitstreamReadBits16_INLINE(stream, 5) & 0x1; /* fix   03/13/01 */
1579         return PV_SUCCESS;
1580     }
1581 
1582     if (code & mask)
1583     {
1584         count = 1;
1585         while (mask && count > 0)                          /* fix  03/28/01 */
1586         {
1587             mask = mask >> 1;
1588             if (code & mask)
1589                 count--;
1590             else
1591                 num[0]++; /* number of zeros in the middle */
1592             len++;
1593         }
1594     }
1595     else
1596     {
1597         count = 2;
1598         while (mask && count > 0)              /* fix  03/28/01 */
1599         {
1600             mask = mask >> 1;
1601             if (!(code & mask))
1602                 count--;
1603             else
1604                 num[count-1]++; /* number of ones in the middle */
1605             len++;
1606         }
1607     }
1608 
1609     code = code & 0x7fff;
1610     code = code >> (15 - (len + 1));
1611 
1612     /*  1/30/01, add fast decoding algorithm here */
1613     /* code is in two forms : 0xxxx0xxx00 or 0xxx0xxx01
1614                          num[1] and num[0] x
1615                         or  : 1xxxxx10 or 1xxxxx11
1616                                 num[0]  x      */
1617 
1618     /* len+1 is the length of the above */
1619 
1620     if (num[1] > 10 || num[0] > 11) /* invalid RVLC code */
1621         return PV_FAIL;
1622 
1623     if (code & (1 << len))
1624         tab2 = RvlcDCTtabIntra + 146 + (num[0] << 1) + (code & 1);
1625     else
1626         tab2 = RvlcDCTtabIntra + ptrRvlcTab[num[1]] + (num[0] << 1) + (code & 1);
1627 
1628     PV_BitstreamFlushBits(stream, (int) tab2->len);
1629     pTcoef->run = (uint)tab2->run;//(tab->val >> 8) & 255;
1630     pTcoef->level = (int)tab2->level;//tab->val & 255;
1631     pTcoef->last = (uint)tab2->last;//(tab->val >> 16) & 1;
1632 
1633     pTcoef->sign = BitstreamRead1Bits_INLINE(stream);
1634     return PV_SUCCESS;
1635 }               /* RvlcDecTCOEFIntra */
1636 
1637