1 /* ///////////////////////////////////////////////////////////////////////
2 //
3 //               INTEL CORPORATION PROPRIETARY INFORMATION
4 //  This software is supplied under the terms of a license agreement or
5 //  nondisclosure agreement with Intel Corporation and may not be copied
6 //  or disclosed except in accordance with the terms of that agreement.
7 //        Copyright (c) 2008 Intel Corporation. All Rights Reserved.
8 //
9 //  Description: Parses VLC syntax elements within VC-1 bitstream.
10 //
11 */
12 
13 #include "vc1parse.h"
14 
15 /*----------------------------------------------------------------------------*/
16 
vc1_DecodeHuffmanOne(void * ctxt,int32_t * pDst,const int32_t * pDecodeTable)17 vc1_Status vc1_DecodeHuffmanOne(void* ctxt, int32_t *pDst, const int32_t *pDecodeTable)
18 {
19     uint32_t tempValue;
20     const int32_t *pTable = pDecodeTable;
21     vc1_Status status = VC1_STATUS_OK;
22     int32_t i, j, maxBits, loopCount, totalBits, value;
23 
24     maxBits = *pTable++;
25     loopCount = *pTable++;
26     totalBits = 0;
27     for (i = 0; i < loopCount; i++)
28         totalBits += *pTable++;
29 
30     if (totalBits != maxBits)
31         return VC1_STATUS_PARSE_ERROR;
32 
33     value = 0;
34     for (i = 0; i < maxBits; i++)
35     {
36         VC1_GET_BITS9(1, tempValue);
37         value = (value << 1) | tempValue;
38         loopCount = *pTable++;
39         if (loopCount == -1)
40             break;
41         for (j = 0; j < loopCount; j++)
42         {
43             if (value == *pTable++)
44             {
45                 *pDst = *pTable;
46                 return status;
47             }
48             else
49                 pTable++;
50         }
51     }
52 
53     return status;
54 }
55 
56 /*----------------------------------------------------------------------------*/
57 
vc1_DecodeHuffmanPair(void * ctxt,const int32_t * pDecodeTable,int8_t * pFirst,int16_t * pSecond)58 vc1_Status vc1_DecodeHuffmanPair(void* ctxt, const int32_t *pDecodeTable,
59                                  int8_t *pFirst, int16_t *pSecond)
60 {
61     uint32_t tempValue;
62     const int32_t *pTable = pDecodeTable;
63     vc1_Status status = VC1_STATUS_OK;
64     int32_t i, j, maxBits, loopCount, totalBits, value;
65 
66     maxBits = *pTable++;
67     loopCount = *pTable++;
68     totalBits = 0;
69     for (i = 0; i < loopCount; i++)
70         totalBits += *pTable++;
71 
72     if (totalBits != maxBits)
73         return VC1_STATUS_PARSE_ERROR;
74 
75     value = 0;
76     for (i = 0; i < maxBits; i++)
77     {
78         VC1_GET_BITS9(1, tempValue);
79         value = (value << 1) | tempValue;
80         loopCount = *pTable++;
81         if (loopCount == -1)
82             break;
83         for (j = 0; j < loopCount; j++)
84         {
85             if (value == *pTable++)
86             {
87                 *pFirst = *pTable++;
88                 *pSecond = *pTable;
89                 return status;
90             }
91             else
92                 pTable += 2;
93         }
94     }
95 
96     return status;
97 }
98