MPEG4的量化方式

MPEG4主要支持两种量化方式,一种是H.263, 另一种是MPEG4。

由于H.263的量化方式较为简单,因此在嵌入式上进行mpeg4视频编码时多使用H.263方式。

 

两种量化方式的主要区别:

MPEG4量化方式引入了量化加权矩阵,因此量化起来更为精细。

 

QP: 量化参数, 实际为量化步长q_step的索引值, 它们之间满足一定的对应关系。如在H.263量化方式中, 实际值被除以2*QP,所以这里的量化步长与QP的对应关系为2倍QP。

 

在逆量化的过程中,一般取两个相邻电平的中值作为判决电平,所以在H.263的逆量化中有一个加1的系数。

 

以下是xvid中采用H.263方式量化的C代码:

 

 

  1 /*****************************************************************************
  2  *
  3  *  XVID MPEG-4 VIDEO CODEC
  4  *  - MPEG4 Quantization H263 implementation -
  5  *
  6  *  Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
  7  *
  8  *  This program is free software ; you can redistribute it and/or modify
  9  *  it under the terms of the GNU General Public License as published by
 10  *  the Free Software Foundation ; either version 2 of the License, or
 11  *  (at your option) any later version.
 12  *
 13  *  This program is distributed in the hope that it will be useful,
 14  *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
 15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  *  GNU General Public License for more details.
 17  *
 18  *  You should have received a copy of the GNU General Public License
 19  *  along with this program ; if not, write to the Free Software
 20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 21  *
 22  * $Id: quant_h263.c,v 1.8 2004/03/22 22:36:24 edgomez Exp $
 23  *
 24  ****************************************************************************/
 25
 26 #include "../global.h"
 27 #include "quant.h"
 28
 29 /*****************************************************************************
 30  * Global function pointers
 31  ****************************************************************************/
 32
 33 /* Quant */
 34 quant_intraFuncPtr quant_h263_intra;
 35 quant_interFuncPtr quant_h263_inter;
 36
 37 /* DeQuant */
 38 quant_intraFuncPtr dequant_h263_intra;
 39 quant_interFuncPtr dequant_h263_inter;
 40
 41 /*****************************************************************************
 42  * Local data
 43  ****************************************************************************/
 44
 45 /* divide-by-multiply table
 46  * a 16 bit shiting is enough in this case */
 47
 48 #define SCALEBITS   16
 49 #define FIX(X)      ((1L << SCALEBITS) / (X) + 1)
 50
 51 static const uint32_t multipliers[32] =
 52 {
 53     0,       FIX(2),  FIX(4),  FIX(6),
 54     FIX(8),  FIX(10), FIX(12), FIX(14),
 55     FIX(16), FIX(18), FIX(20), FIX(22),
 56     FIX(24), FIX(26), FIX(28), FIX(30),
 57     FIX(32), FIX(34), FIX(36), FIX(38),
 58     FIX(40), FIX(42), FIX(44), FIX(46),
 59     FIX(48), FIX(50), FIX(52), FIX(54),
 60     FIX(56), FIX(58), FIX(60), FIX(62)
 61 };
 62
 63 /*****************************************************************************
 64  * Function definitions
 65  ****************************************************************************/
 66
 67 /*  quantize intra-block
 68  */
 69
 70 uint32_t
 71 quant_h263_intra_c(int16_t * coeff,
 72                    const int16_t * data,
 73                    const uint32_t quant,
 74                    const uint32_t dcscalar,
 75                    const uint16_t * mpeg_quant_matrices)
 76 {
 77     const uint32_t mult = multipliers[quant];
 78     const uint16_t quant_m_2 = quant << 1;
 79     int i;
 80
 81     coeff[0] = DIV_DIV(data[0], (int32_t) dcscalar);
 82
 83     for (i = 1; i < 64; i++) {
 84         int16_t acLevel = data[i];
 85
 86         if (acLevel < 0) {
 87             acLevel = -acLevel;
 88             if (acLevel < quant_m_2) {
 89                 coeff[i] = 0;
 90                 continue;
 91             }
 92             acLevel = (acLevel * mult) >> SCALEBITS;
 93             coeff[i] = -acLevel;
 94         } else {
 95             if (acLevel < quant_m_2) {
 96                 coeff[i] = 0;
 97                 continue;
 98             }
 99             acLevel = (acLevel * mult) >> SCALEBITS;
100             coeff[i] = acLevel;
101         }
102     }
103
104     return(0);
105 }
106
107
108 /*  quantize inter-block
109  */
110
111 uint32_t
112 quant_h263_inter_c(int16_t * coeff,
113                    const int16_t * data,
114                    const uint32_t quant,
115                    const uint16_t * mpeg_quant_matrices)
116 {
117     const uint32_t mult = multipliers[quant];
118     const uint16_t quant_m_2 = quant << 1;
119     const uint16_t quant_d_2 = quant >> 1;
120     uint32_t sum = 0;
121     uint32_t i;
122
123     for (i = 0; i < 64; i++) {
124         int16_t acLevel = data[i];
125
126         if (acLevel < 0) {
127             acLevel = (-acLevel) - quant_d_2;
128             if (acLevel < quant_m_2) {
129                 coeff[i] = 0;
130                 continue;
131             }
132
133             acLevel = (acLevel * mult) >> SCALEBITS;
134             sum += acLevel;     /* sum += |acLevel| */
135             coeff[i] = -acLevel;
136         } else {
137             acLevel -= quant_d_2;
138             if (acLevel < quant_m_2) {
139                 coeff[i] = 0;
140                 continue;
141             }
142             acLevel = (acLevel * mult) >> SCALEBITS;
143             sum += acLevel;
144             coeff[i] = acLevel;
145         }
146     }
147
148     return(sum);
149 }
150
151
152 /*  dequantize intra-block & clamp to [-2048,2047]
153  */
154
155 uint32_t
156 dequant_h263_intra_c(int16_t * data,
157                      const int16_t * coeff,
158                      const uint32_t quant,
159                      const uint32_t dcscalar,
160                      const uint16_t * mpeg_quant_matrices)
161 {
162     const int32_t quant_m_2 = quant << 1;
163     const int32_t quant_add = (quant & 1 ? quant : quant - 1);
164     int i;
165
166     data[0] = coeff[0] * dcscalar;
167     if (data[0] < -2048) {
168         data[0] = -2048;
169     } else if (data[0] > 2047) {
170         data[0] = 2047;
171     }
172
173     for (i = 1; i < 64; i++) {
174         int32_t acLevel = coeff[i];
175
176         if (acLevel == 0) {
177             data[i] = 0;
178         } else if (acLevel < 0) {
179             acLevel = quant_m_2 * -acLevel + quant_add;
180             data[i] = (acLevel <= 2048 ? -acLevel : -2048);
181         } else {
182             acLevel = quant_m_2 * acLevel + quant_add;
183             data[i] = (acLevel <= 2047 ? acLevel : 2047);
184         }
185     }
186
187     return(0);
188 }
189
190
191
192 /* dequantize inter-block & clamp to [-2048,2047]
193  */
194
195 uint32_t
196 dequant_h263_inter_c(int16_t * data,
197                      const int16_t * coeff,
198                      const uint32_t quant,
199                      const uint16_t * mpeg_quant_matrices)
200 {
201     const uint16_t quant_m_2 = quant << 1;
202     const uint16_t quant_add = (quant & 1 ? quant : quant - 1);
203     int i;
204
205     for (i = 0; i < 64; i++) {
206         int16_t acLevel = coeff[i];
207
208         if (acLevel == 0) {
209             data[i] = 0;
210         } else if (acLevel < 0) {
211             acLevel = acLevel * quant_m_2 - quant_add;
212             data[i] = (acLevel >= -2048 ? acLevel : -2048);
213         } else {
214             acLevel = acLevel * quant_m_2 + quant_add;
215             data[i] = (acLevel <= 2047 ? acLevel : 2047);
216         }
217     }
218
219     return(0);
220 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值