1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
19 * ------------------------------------------------------------------- *
20 * MPEG-4 Simple Profile Video Decoder *
21 * ------------------------------------------------------------------- *
23 * This software module was originally developed by
25 * Paulo Nunes (IST / ACTS-MoMuSyS)
26 * Robert Danielsen (Telenor / ACTS-MoMuSyS)
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
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
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
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
54 *****************************************************************************/
56 /***********************************************************HeaderBegin*******
60 * Author: Paulo Nunes (IST) - Paulo.Nunes@lx.it.pt
63 * Description: This file contains the VLC functions needed to decode a
67 * The functions contained in this file were adapted from
69 * Written by Karl Olav Lillevold <kol@nta.no>,
71 * Donated to the Momusys-project as background code by
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>
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
83 * 09.03.98 Paulo Nunes: Cleaning.
85 ***********************************************************HeaderEnd*********/
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"
94 /* ====================================================================== /
95 Function : DecodeUserData()
98 Modified : 04/16/2001 : removed status checking of PV_BitstreamFlushBits
100 This is simply a realization of the user_data() function
101 in the ISO/IEC 14496-2 manual.
102 / ====================================================================== */
103 PV_STATUS DecodeUserData(BitstreamDecVideo *stream)
108 BitstreamReadBits32HC(stream);
109 BitstreamShowBits32(stream, 24, &code);
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 */
124 /***********************************************************CommentBegin******
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
131 ***********************************************************CommentEnd********/
132 PV_STATUS PV_GetMBvectors(VideoDecData *video, uint mode)
135 BitstreamDecVideo *stream = video->bitstream;
136 int f_code_f = video->currVop->fcodeForward;
140 MOT *mot_x = video->motX;
141 MOT *mot_y = video->motY;
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;
153 #ifdef PV_ANNEX_IJKT_SUPPORT
154 if (mode == MODE_INTER4V || mode == MODE_INTER4V_Q)
156 if (mode == MODE_INTER4V)
159 for (k = 0; k < 4; k++)
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)
170 mvx += (MOT)vlc_code_mag;
171 mvx = (MOT)(((mvx + 32) & 0x3F) - 32);
174 status = PV_VlcDecMV(stream, &vlc_code_mag);
175 if (status != PV_SUCCESS)
180 mvy += (MOT)vlc_code_mag;
181 mvy = (MOT)(((mvy + 32) & 0x3F) - 32);
183 mot_x[pos+offset] = (MOT) mvx;
184 mot_y[pos+offset] = (MOT) mvy;
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)
197 mvx += (MOT)vlc_code_mag;
198 mvx = (MOT)(((mvx + 32) & 0x3F) - 32);
201 status = PV_VlcDecMV(stream, &vlc_code_mag);
202 if (status != PV_SUCCESS)
208 mvy += (MOT)vlc_code_mag;
209 mvy = (MOT)(((mvy + 32) & 0x3F) - 32);
212 mot_x[pos] = mot_x[pos+1] = (MOT) mvx;
213 mot_y[pos] = mot_y[pos+1] = (MOT) mvy;
215 mot_x[pos] = mot_x[pos+1] = (MOT) mvx;
216 mot_y[pos] = mot_y[pos+1] = (MOT) mvy;
221 #ifdef PV_ANNEX_IJKT_SUPPORT
222 if (mode == MODE_INTER4V || mode == MODE_INTER4V_Q)
224 if (mode == MODE_INTER4V)
227 for (k = 0; k < 4; k++)
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)
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;
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)
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********/
267 PV_STATUS PV_DecodeMBVec(BitstreamDecVideo *stream, MOT *mv_x, MOT *mv_y, int f_code_f)
270 int vlc_code_magx, vlc_code_magy;
271 int residualx = 0, residualy = 0;
273 /* decode component x */
274 status = PV_VlcDecMV(stream, &vlc_code_magx);
275 if (status != PV_SUCCESS)
282 residualx = (int) BitstreamReadBits16_INLINE(stream, (int)(f_code_f - 1));
286 /* decode component y */
287 status = PV_VlcDecMV(stream, &vlc_code_magy);
288 if (status != PV_SUCCESS)
295 residualy = (int) BitstreamReadBits16_INLINE(stream, (int)(f_code_f - 1));
299 if (PV_DeScaleMVD(f_code_f, residualx, vlc_code_magx, mv_x) != PV_SUCCESS)
304 if (PV_DeScaleMVD(f_code_f, residualy, vlc_code_magy, mv_y) != PV_SUCCESS)
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.
318 ***********************************************************CommentEnd********/
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 */
326 int half_range = (1 << (f_code + 4));
327 int mask = (half_range << 1) - 1;
331 if (vlc_code_mag == 0)
333 diff_vector = vlc_code_mag;
337 diff_vector = ((PV_ABS(vlc_code_mag) - 1) << (f_code - 1)) + residual + 1;
338 if (vlc_code_mag < 0)
340 diff_vector = -diff_vector;
344 *vector += (MOT)(diff_vector);
346 *vector = (MOT)((*vector + half_range) & mask) - half_range;
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;
376 indx = ((mbnum_col << 1) + (block & 1)) + ((mbnum_row << 1) + (block >> 1)) * nMVPerRow - 1; /* left block */
378 if (block & 1) /* block 1, 3 */
380 p1x = motxdata[indx];
381 p1y = motydata[indx];
384 else /* block 0, 2 */
386 if (mbnum_col > 0 && slice_nb[mbnum] == slice_nb[mbnum-1])
388 p1x = motxdata[indx];
389 p1y = motydata[indx];
394 indx = indx + 1 - nMVPerRow; /* upper_block */
398 p2x = motxdata[indx];
399 p2y = motydata[indx];
400 p3x = motxdata[indx + 1];
401 p3y = motydata[indx + 1];
408 if (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow])
410 p2x = motxdata[indx];
411 p2y = motydata[indx];
414 if (mbnum_col < nMBPerRow - 1 && slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow+1])
416 indx = indx + 2 - (block & 1);
417 p3x = motxdata[indx];
418 p3y = motydata[indx];
424 if (rule1 + rule2 + rule3 > 1)
426 *mvx = (MOT)PV_MEDIAN(p1x, p2x, p3x);
427 *mvy = (MOT)PV_MEDIAN(p1y, p2y, p3y);
429 else if (rule1 + rule2 + rule3 == 1)
431 /* two of three are zero */
432 *mvx = (MOT)(p1x + p2x + p3x);
433 *mvy = (MOT)(p1y + p2y + p3y);
437 /* all MBs are outside the VOP */
440 /*----------------------------------------------------------------------------
441 ; Return nothing or data or data pointer
442 ----------------------------------------------------------------------------*/
446 /***********************************************************CommentBegin******
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********/
452 PV_STATUS PV_VlcDecMV(BitstreamDecVideo *stream, int *mv)
454 PV_STATUS status = PV_SUCCESS;
457 BitstreamShow13Bits(stream, &code);
461 *mv = 0; /* Vector difference = 0 */
462 PV_BitstreamFlushBits(stream, 1);
468 code = (code >> 8) - 2;
469 PV_BitstreamFlushBits(stream, PV_TMNMVtab0[code].len + 1);
470 *mv = PV_TMNMVtab0[code].val;
476 code = (code >> 2) - 32;
477 PV_BitstreamFlushBits(stream, PV_TMNMVtab1[code].len + 1);
478 *mv = PV_TMNMVtab1[code].val;
490 PV_BitstreamFlushBits(stream, PV_TMNMVtab2[code].len + 1);
492 *mv = PV_TMNMVtab2[code].val;
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********/
503 int PV_VlcDecMCBPC_com_intra(BitstreamDecVideo *stream)
507 BitstreamShowBits16(stream, 9, &code);
512 return VLC_CODE_ERROR;
519 PV_BitstreamFlushBits(stream, 1);
523 PV_BitstreamFlushBits(stream, PV_MCBPCtabintra[code].len);
525 return PV_MCBPCtabintra[code].val;
529 /***********************************************************CommentBegin******
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********/
536 int PV_VlcDecMCBPC_com_inter(BitstreamDecVideo *stream)
540 BitstreamShowBits16(stream, 9, &code);
544 return VLC_CODE_ERROR;
546 else if (code >= 256)
548 PV_BitstreamFlushBits(stream, 1);
552 PV_BitstreamFlushBits(stream, PV_MCBPCtab[code].len);
553 return PV_MCBPCtab[code].val;
556 #ifdef PV_ANNEX_IJKT_SUPPORT
557 int PV_VlcDecMCBPC_com_inter_H263(BitstreamDecVideo *stream)
561 BitstreamShow13Bits(stream, &code);
565 return VLC_CODE_ERROR;
567 else if (code >= 4096)
569 PV_BitstreamFlushBits(stream, 1);
574 PV_BitstreamFlushBits(stream, PV_MCBPCtab[code >> 4].len);
575 return PV_MCBPCtab[code >> 4].val;
579 PV_BitstreamFlushBits(stream, PV_MCBPCtab1[code - 8].len);
580 return PV_MCBPCtab1[code - 8].val;
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********/
590 int PV_VlcDecCBPY(BitstreamDecVideo *stream, int intra)
595 BitstreamShowBits16(stream, 6, &code);
604 PV_BitstreamFlushBits(stream, 2);
609 PV_BitstreamFlushBits(stream, PV_CBPYtab[code].len);
610 CBPY = PV_CBPYtab[code].val;
613 if (intra == 0) CBPY = 15 - CBPY;
619 /***********************************************************CommentBegin******
620 * 3/31/2000 : initial modification to the new PV-Decoder Lib format.
622 * 8/23/2000 : optimize the function by removing unnecessary BitstreamShowBits()
625 * 9/6/2000 : change the API to check for end-of-buffer for proper
626 * termination of decoding process.
627 ***********************************************************CommentEnd********/
628 PV_STATUS PV_VlcDecIntraDCPredSize(BitstreamDecVideo *stream, int compnum, uint *DC_size)
630 PV_STATUS status = PV_FAIL; /* 07/09/01 */
634 if (compnum < 4) /* luminance block */
637 BitstreamShowBits16(stream, 11, &code);
642 PV_BitstreamFlushBits(stream, 11);
649 PV_BitstreamFlushBits(stream, 10);
656 PV_BitstreamFlushBits(stream, 9);
664 PV_BitstreamFlushBits(stream, 8);
672 PV_BitstreamFlushBits(stream, 7);
680 PV_BitstreamFlushBits(stream, 6);
688 PV_BitstreamFlushBits(stream, 5);
696 PV_BitstreamFlushBits(stream, 4);
704 PV_BitstreamFlushBits(stream, 3);
710 PV_BitstreamFlushBits(stream, 3);
716 PV_BitstreamFlushBits(stream, 3);
724 PV_BitstreamFlushBits(stream, 2);
730 PV_BitstreamFlushBits(stream, 2);
734 else /* chrominance block */
737 BitstreamShow13Bits(stream, &code);
742 PV_BitstreamFlushBits(stream, 12);
750 PV_BitstreamFlushBits(stream, 11);
758 PV_BitstreamFlushBits(stream, 10);
766 PV_BitstreamFlushBits(stream, 9);
774 PV_BitstreamFlushBits(stream, 8);
782 PV_BitstreamFlushBits(stream, 7);
790 PV_BitstreamFlushBits(stream, 6);
798 PV_BitstreamFlushBits(stream, 5);
806 PV_BitstreamFlushBits(stream, 4);
814 PV_BitstreamFlushBits(stream, 3);
820 *DC_size = (int)(3 - code);
821 PV_BitstreamFlushBits(stream, 2);
829 /***********************************************************CommentBegin******
832 * 3/30/2000 : initial modification to the new PV-Decoder Lib
833 * format and the change of error-handling method.
835 ***********************************************************CommentEnd********/
839 PV_STATUS VlcDecTCOEFIntra(BitstreamDecVideo *stream, Tcoef *pTcoef)
844 BitstreamShow13Bits(stream, &code);
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;*/
850 tab = &PV_DCT3Dtab3[(code >> 6) - 16];
856 tab = &PV_DCT3Dtab4[(code >> 3) - 32];
862 tab = &PV_DCT3Dtab5[(code>>1) - 8];
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;
878 /* the following is modified for 3-mode escape -- boon */
879 if (tab->level != 0xFF)
884 //if (((tab->run<<8)|(tab->level)|(tab->last<<16)) == VLC_ESCAPE_CODE)
888 /* first escape mode. level is offset */
889 BitstreamShow13Bits(stream, &code);
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;*/
895 tab = &PV_DCT3Dtab3[(code >> 6) - 16];
901 tab = &PV_DCT3Dtab4[(code >> 3) - 32];
907 tab = &PV_DCT3Dtab5[(code>>1) - 8];
916 PV_BitstreamFlushBits(stream, tab->len + 1);
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;
925 /* need to add back the max level */
926 if ((pTcoef->last == 0 && pTcoef->run > 14) || (pTcoef->last == 1 && pTcoef->run > 20))
930 pTcoef->level = pTcoef->level + intra_max_level[pTcoef->last][pTcoef->run];
937 run_offset = BitstreamRead1Bits_INLINE(stream);
941 /* second escape mode. run is offset */
942 BitstreamShow13Bits(stream, &code);
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;*/
948 tab = &PV_DCT3Dtab3[(code >> 6) - 16];
954 tab = &PV_DCT3Dtab4[(code >> 3) - 32];
960 tab = &PV_DCT3Dtab5[(code>>1) - 8];
969 PV_BitstreamFlushBits(stream, tab->len + 1);
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;
978 /* need to add back the max run */
981 if (pTcoef->level > 8)
985 pTcoef->run = pTcoef->run + intra_max_run1[pTcoef->level] + 1;
989 if (pTcoef->level > 27)
993 pTcoef->run = pTcoef->run + intra_max_run0[pTcoef->level] + 1;
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);
1006 if (pTcoef->level >= 2048)
1009 pTcoef->level = 4096 - pTcoef->level;
1020 } /* VlcDecTCOEFIntra */
1022 PV_STATUS VlcDecTCOEFInter(BitstreamDecVideo *stream, Tcoef *pTcoef)
1027 BitstreamShow13Bits(stream, &code);
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;*/
1033 tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1039 tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1045 tab = &PV_DCT3Dtab2[(code>>1) - 8];
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;
1059 /* the following is modified for 3-mode escape -- boon */
1060 if (tab->run != 0xBF)
1064 //if (((tab->run<<4)|(tab->level)|(tab->last<<12)) == VLC_ESCAPE_CODE)
1069 /* first escape mode. level is offset */
1070 BitstreamShow13Bits(stream, &code);
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;*/
1076 tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1082 tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1088 tab = &PV_DCT3Dtab2[(code>>1) - 8];
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;
1102 /* need to add back the max level */
1103 if ((pTcoef->last == 0 && pTcoef->run > 26) || (pTcoef->last == 1 && pTcoef->run > 40))
1107 pTcoef->level = pTcoef->level + inter_max_level[pTcoef->last][pTcoef->run];
1112 run_offset = BitstreamRead1Bits_INLINE(stream);
1116 /* second escape mode. run is offset */
1117 BitstreamShow13Bits(stream, &code);
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;*/
1123 tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1129 tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1135 tab = &PV_DCT3Dtab2[(code>>1) - 8];
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;
1149 /* need to add back the max run */
1152 if (pTcoef->level > 3)
1156 pTcoef->run = pTcoef->run + inter_max_run1[pTcoef->level] + 1;
1160 if (pTcoef->level > 12)
1164 pTcoef->run = pTcoef->run + inter_max_run0[pTcoef->level] + 1;
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);
1177 if (pTcoef->level >= 2048)
1180 pTcoef->level = 4096 - pTcoef->level;
1191 } /* VlcDecTCOEFInter */
1193 /*=======================================================
1194 Function: VlcDecTCOEFShortHeader()
1196 Purpose : New function used in decoding of video planes
1198 Modified: 05/23/2000
1199 for new decoder structure.
1200 =========================================================*/
1201 PV_STATUS VlcDecTCOEFShortHeader(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1206 BitstreamShow13Bits(stream, &code);
1210 if (code >= 1024) tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1213 if (code >= 256) tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1216 if (code >= 16) tab = &PV_DCT3Dtab2[(code>>1) - 8];
1217 else return PV_FAIL;
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;
1227 /* the following is modified for 3-mode escape -- boon */
1228 if (((tab->run << 4) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE) /* ESCAPE */
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 */
1239 if (pTcoef->level == 0 || pTcoef->level == 128)
1244 if (pTcoef->level > 128)
1247 pTcoef->level = 256 - pTcoef->level;
1258 } /* VlcDecTCOEFShortHeader */
1260 #ifdef PV_ANNEX_IJKT_SUPPORT
1261 PV_STATUS VlcDecTCOEFShortHeader_AnnexI(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1266 BitstreamShow13Bits(stream, &code);
1270 if (code >= 1024) tab = &PV_DCT3Dtab6[(code >> 6) - 16];
1273 if (code >= 256) tab = &PV_DCT3Dtab7[(code >> 3) - 32];
1276 if (code >= 16) tab = &PV_DCT3Dtab8[(code>>1) - 8];
1277 else return PV_FAIL;
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;
1287 /* the following is modified for 3-mode escape -- boon */
1288 if (((tab->run << 6) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE) /* ESCAPE */
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 */
1297 if (pTcoef->level == 0 || pTcoef->level == 128)
1303 if (pTcoef->level > 128)
1306 pTcoef->level = 256 - pTcoef->level;
1308 else pTcoef->sign = 0;
1314 } /* VlcDecTCOEFShortHeader_AnnexI */
1316 PV_STATUS VlcDecTCOEFShortHeader_AnnexT(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1321 BitstreamShow13Bits(stream, &code);
1325 if (code >= 1024) tab = &PV_DCT3Dtab0[(code >> 6) - 16];
1328 if (code >= 256) tab = &PV_DCT3Dtab1[(code >> 3) - 32];
1331 if (code >= 16) tab = &PV_DCT3Dtab2[(code>>1) - 8];
1332 else return PV_FAIL;
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;
1342 /* the following is modified for 3-mode escape -- */
1343 if (((tab->run << 4) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE) /* ESCAPE */
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 */
1352 if (pTcoef->level == 0)
1357 if (pTcoef->level >= 128)
1360 pTcoef->level = 256 - pTcoef->level;
1367 if (pTcoef->level == 128)
1369 code = BitstreamReadBits16(stream, 11); /* ANNEX_T */
1371 code = (code >> 6 & 0x1F) | (code << 5 & 0x7ff);
1375 pTcoef->level = (2048 - code);
1380 pTcoef->level = code;
1386 } /* VlcDecTCOEFShortHeader */
1389 PV_STATUS VlcDecTCOEFShortHeader_AnnexIT(BitstreamDecVideo *stream, Tcoef *pTcoef/*, int intra*/)
1394 BitstreamShow13Bits(stream, &code);
1398 if (code >= 1024) tab = &PV_DCT3Dtab6[(code >> 6) - 16];
1401 if (code >= 256) tab = &PV_DCT3Dtab7[(code >> 3) - 32];
1404 if (code >= 16) tab = &PV_DCT3Dtab8[(code>>1) - 8];
1405 else return PV_FAIL;
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;
1415 /* the following is modified for 3-mode escape -- */
1416 if (((tab->run << 6) | (tab->level) | (tab->last << 12)) != VLC_ESCAPE_CODE) /* ESCAPE */
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 */
1425 if (pTcoef->level == 0)
1430 if (pTcoef->level >= 128)
1433 pTcoef->level = 256 - pTcoef->level;
1440 if (pTcoef->level == 128)
1442 code = BitstreamReadBits16(stream, 11); /* ANNEX_T */
1444 code = (code >> 6 & 0x1F) | (code << 5 & 0x7ff);
1448 pTcoef->level = (2048 - code);
1453 pTcoef->level = code;
1460 } /* VlcDecTCOEFShortHeader_AnnexI */
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.
1468 ***********************************************************CommentEnd********/
1471 PV_STATUS RvlcDecTCOEFInter(BitstreamDecVideo *stream, Tcoef *pTcoef)
1474 const VLCtab2 *tab2;
1475 int count, len, num[2] = {0, 0} /* 01/30/01 */;
1477 mask = 0x4000; /* mask 100000000000000 */
1478 BitstreamShow15Bits(stream, &code); /* 03/07/01 */
1482 // 09/20/99 Escape mode
1483 /// Bitstream Exchange
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 */
1502 while (mask && count > 0) /* fix 3/28/01 */
1508 num[0]++; /* number of zeros in the middle */
1515 while (mask && count > 0) /* fix 3/28/01 */
1521 num[count-1]++; /* number of ones in the middle */
1526 code = code & 0x7fff;
1527 code = code >> (15 - (len + 1));
1529 /* 1/30/01, add fast decoding algorithm here */
1530 /* code is in two forms : 0xxxx0xxx00 or 0xxx0xxx01
1532 or : 1xxxxx10 or 1xxxxx11
1535 /* len+1 is the length of the above */
1537 if (num[1] > 10 || num[0] > 11) /* invalid RVLC code */
1540 if (code&(1 << len))
1541 tab2 = RvlcDCTtabInter + 146 + (num[0] << 1) + (code & 1);
1543 tab2 = RvlcDCTtabInter + ptrRvlcTab[num[1]] + (num[0] << 1) + (code & 1);
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;
1550 pTcoef->sign = BitstreamRead1Bits_INLINE(stream);
1552 } /* RvlcDecTCOEFInter */
1554 PV_STATUS RvlcDecTCOEFIntra(BitstreamDecVideo *stream, Tcoef *pTcoef)
1557 const VLCtab2 *tab2;
1558 int count, len, num[2] = {0, 0} /* 01/30/01 */;
1560 mask = 0x4000; /* mask 100000000000000 */
1561 BitstreamShow15Bits(stream, &code);
1565 // 09/20/99 Escape mode
1566 /// Bitstream Exchange
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 */
1585 while (mask && count > 0) /* fix 03/28/01 */
1591 num[0]++; /* number of zeros in the middle */
1598 while (mask && count > 0) /* fix 03/28/01 */
1604 num[count-1]++; /* number of ones in the middle */
1609 code = code & 0x7fff;
1610 code = code >> (15 - (len + 1));
1612 /* 1/30/01, add fast decoding algorithm here */
1613 /* code is in two forms : 0xxxx0xxx00 or 0xxx0xxx01
1615 or : 1xxxxx10 or 1xxxxx11
1618 /* len+1 is the length of the above */
1620 if (num[1] > 10 || num[0] > 11) /* invalid RVLC code */
1623 if (code & (1 << len))
1624 tab2 = RvlcDCTtabIntra + 146 + (num[0] << 1) + (code & 1);
1626 tab2 = RvlcDCTtabIntra + ptrRvlcTab[num[1]] + (num[0] << 1) + (code & 1);
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;
1633 pTcoef->sign = BitstreamRead1Bits_INLINE(stream);
1635 } /* RvlcDecTCOEFIntra */