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 VC-1 syntax elements VOPDQUANT and DQUANT.
10//
11*/
12
13#include "vc1parse.h"
14
15#define VC1_UNDEF_PQUANT 0
16
17static const uint8_t MapPQIndToQuant_Impl[] =
18{
19    VC1_UNDEF_PQUANT,
20    1,  2,  3,  4,  5, 6,   7,  8,
21    6,  7,  8,  9, 10, 11, 12, 13,
22    14, 15, 16, 17, 18, 19, 20, 21,
23    22, 23, 24, 25, 27, 29, 31
24};
25
26/*------------------------------------------------------------------------------
27 * Parse syntax element VOPDQuant as defined in Table 24 of SMPTE 421M.
28 *------------------------------------------------------------------------------
29 */
30
31vc1_Status vc1_VOPDQuant(void* ctxt, vc1_Info *pInfo)
32{
33    vc1_Status status = VC1_STATUS_OK;
34    vc1_metadata_t *md = &pInfo->metadata;
35    vc1_PictureLayerHeader *picLayerHeader = &pInfo->picLayerHeader;
36
37    if (md->DQUANT == 0)
38        return status;
39
40    if (md->DQUANT == 2)
41    {
42        VC1_GET_BITS9(3, picLayerHeader->PQDIFF);
43        if (picLayerHeader->PQDIFF == 7)
44        {
45            VC1_GET_BITS9(5, picLayerHeader->ABSPQ);
46        }
47    }
48    else
49    {
50        VC1_GET_BITS9(1, picLayerHeader->DQUANTFRM);
51        if (picLayerHeader->DQUANTFRM == 1)
52        {
53            VC1_GET_BITS9(2, picLayerHeader->DQPROFILE);
54            if (picLayerHeader->DQPROFILE == VC1_DQPROFILE_SNGLEDGES)
55            {
56                VC1_GET_BITS9(2, picLayerHeader->DQSBEDGE);
57            }
58            else if (picLayerHeader->DQPROFILE == VC1_DQPROFILE_DBLEDGES)
59            {
60#ifdef VBP
61                VC1_GET_BITS9(2, picLayerHeader->DQDBEDGE);
62#else
63      			VC1_GET_BITS9(2, picLayerHeader->DQSBEDGE); /* DQDBEDGE. */
64#endif
65            }
66            else if (picLayerHeader->DQPROFILE == VC1_DQPROFILE_ALLMBLKS)
67            {
68                VC1_GET_BITS9(1, picLayerHeader->DQBILEVEL);
69            }
70            if (! (picLayerHeader->DQPROFILE == VC1_DQPROFILE_ALLMBLKS &&
71                   picLayerHeader->DQBILEVEL == 0))
72            {
73                VC1_GET_BITS9(3, picLayerHeader->PQDIFF);
74                if (picLayerHeader->PQDIFF == 7)
75                {
76                    VC1_GET_BITS9(5, picLayerHeader->ABSPQ);
77                }
78            }
79        }
80    }
81#ifdef VBP
82	if ((picLayerHeader->DQUANTFRM == 1 && md->DQUANT == 1) || (md->DQUANT == 2))
83	{
84		if (picLayerHeader->PQDIFF == 7)
85		{
86			picLayerHeader->ALTPQUANT = picLayerHeader->ABSPQ;
87		}
88		else
89		{
90			picLayerHeader->ALTPQUANT = picLayerHeader->PQUANT + picLayerHeader->PQDIFF + 1;
91		}
92	}
93#endif
94    return status;
95}
96
97/*------------------------------------------------------------------------------
98 * Compute value for PQUANT syntax element that does not exist in bitstreams for
99 * progressive I and BI pictures.
100 *------------------------------------------------------------------------------
101 */
102
103vc1_Status vc1_CalculatePQuant(vc1_Info *pInfo)
104{
105    vc1_Status status = VC1_STATUS_OK;
106    vc1_metadata_t *md = &pInfo->metadata;
107    vc1_PictureLayerHeader *picLayerHeader = &pInfo->picLayerHeader;
108
109    picLayerHeader->PQUANT = picLayerHeader->PQINDEX;
110    picLayerHeader->UniformQuant = VC1_QUANTIZER_UNIFORM;
111
112    if (md->QUANTIZER == 0)
113    {
114        if (picLayerHeader->PQINDEX < 9)
115            picLayerHeader->UniformQuant = VC1_QUANTIZER_UNIFORM;
116        else
117        {
118            picLayerHeader->UniformQuant = VC1_QUANTIZER_NONUNIFORM;
119            picLayerHeader->PQUANT =
120                MapPQIndToQuant_Impl[picLayerHeader->PQINDEX];
121        }
122    }
123    else
124    {
125        if (md->QUANTIZER == 2)
126            picLayerHeader->UniformQuant = VC1_QUANTIZER_NONUNIFORM;
127    }
128
129    return status;
130}
131