inv_quantize.c
上传用户:aoeyumen
上传日期:2007-01-06
资源大小:3329k
文件大小:5k
源码类别:

DVD

开发平台:

Unix_Linux

  1. /*
  2.  *  inv_quantize.c
  3.  *
  4.  *  Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999
  5.  *
  6.  *  This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
  7.  *
  8.  *  mpeg2dec 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, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  mpeg2dec 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 GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 
  21.  *
  22.  */
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include "mpeg2.h"
  27. #include "mpeg2_internal.h"
  28. #include "mb_buffer.h"
  29. #include "inv_quantize.h"
  30. static uint_8 default_intra_quantization_matrix[64] = 
  31. {
  32.    8, 16, 19, 22, 26, 27, 29, 34,
  33.   16, 16, 22, 24, 27, 29, 34, 37,
  34.   19, 22, 26, 27, 29, 34, 34, 38,
  35.   22, 22, 26, 27, 29, 34, 37, 40,
  36.   22, 26, 27, 29, 32, 35, 40, 48,
  37.   26, 27, 29, 32, 35, 40, 48, 58,
  38.   26, 27, 29, 34, 38, 46, 56, 69,
  39.   27, 29, 35, 38, 46, 56, 69, 83
  40. };
  41. static uint_8 default_non_intra_quantization_matrix[64] = 
  42. {
  43. 16, 16, 16, 16, 16, 16, 16, 16, 
  44. 16, 16, 16, 16, 16, 16, 16, 16, 
  45. 16, 16, 16, 16, 16, 16, 16, 16, 
  46. 16, 16, 16, 16, 16, 16, 16, 16, 
  47. 16, 16, 16, 16, 16, 16, 16, 16, 
  48. 16, 16, 16, 16, 16, 16, 16, 16, 
  49. 16, 16, 16, 16, 16, 16, 16, 16, 
  50. 16, 16, 16, 16, 16, 16, 16, 16 
  51. };
  52. //FIXME this should go
  53. //it stays until I get this working
  54. static inline sint_16 clip(sint_16 x) 
  55. if(x > 2047) 
  56. x = 2047; 
  57. else if(x < -2048) 
  58. x = -2048;
  59. // if (x != 0)
  60. // x = (x - 1) | 1; 
  61. return x;
  62. }
  63. static void
  64. inv_quantize_intra_mb(const picture_t *picture,macroblock_t *mb)
  65. {
  66. uint_8 *quant_matrix;
  67. uint_32 i;
  68. sint_16 *block;
  69. //choose the appropriate quantization matrix
  70. if(picture->use_custom_intra_quantizer_matrix)
  71. quant_matrix = picture->custom_intra_quantization_matrix;
  72. else 
  73. quant_matrix = default_intra_quantization_matrix;
  74. block = mb->y_blocks;
  75. for(i=1;i<64;i++)
  76. {
  77. block[i] = (block[i] * (sint_16) quant_matrix[i]) / 16;
  78. block[i] = clip(block[i]);
  79. }
  80. block = mb->y_blocks + 64;
  81. for(i=1;i<64;i++)
  82. {
  83. block[i] = (block[i] * quant_matrix[i]) / 16;
  84. block[i] = clip(block[i]);
  85. }
  86. block = mb->y_blocks + 64 * 2;
  87. for(i=1;i<64;i++)
  88. {
  89. block[i] = (block[i] * quant_matrix[i]) / 16;
  90. block[i] = clip(block[i]);
  91. }
  92. block = mb->y_blocks + 64 * 3;
  93. for(i=1;i<64;i++)
  94. {
  95. block[i] = (block[i] * quant_matrix[i]) / 16;
  96. block[i] = clip(block[i]);
  97. }
  98. block = mb->cr_blocks;
  99. for(i=1;i<64;i++)
  100. {
  101. block[i] = (block[i] * quant_matrix[i]) / 16;
  102. block[i] = clip(block[i]);
  103. }
  104. block = mb->cb_blocks;
  105. for(i=1;i<64;i++)
  106. {
  107. block[i] = (block[i] * quant_matrix[i]) / 16;
  108. block[i] = clip(block[i]);
  109. }
  110. }
  111. static void
  112. inv_quantize_non_intra_mb(picture_t *picture,macroblock_t *mb)
  113. {
  114. uint_8 *quant_matrix;
  115. uint_32 i;
  116. sint_16 *block;
  117. //choose the appropriate quantization matrix
  118. if(picture->use_custom_non_intra_quantizer_matrix)
  119. quant_matrix = picture->custom_non_intra_quantization_matrix;
  120. else 
  121. quant_matrix = default_non_intra_quantization_matrix;
  122. //FIXME we can optimize this so that we load the quantization
  123. //coeff once and use it on all 6 blocks at once
  124. block = mb->y_blocks;
  125. for(i=0;i<64;i++)
  126. block[i] = (block[i] * quant_matrix[i]) / 32;
  127. block = mb->y_blocks + 64;
  128. for(i=0;i<64;i++)
  129. block[i] = (block[i] * quant_matrix[i]) / 32;
  130. block = mb->y_blocks + 64 * 2;
  131. for(i=0;i<64;i++)
  132. block[i] = (block[i] * quant_matrix[i]) / 32;
  133. block = mb->y_blocks + 64 * 3;
  134. for(i=0;i<64;i++)
  135. block[i] = (block[i] * quant_matrix[i]) / 32;
  136. block = mb->cr_blocks;
  137. for(i=0;i<64;i++)
  138. block[i] = (block[i] * quant_matrix[i]) / 32;
  139. block = mb->cb_blocks;
  140. for(i=0;i<64;i++)
  141. block[i] = (block[i] * quant_matrix[i]) / 32;
  142. }
  143. void
  144. inv_quantize(picture_t *picture,mb_buffer_t *mb_buffer)
  145. {
  146. uint_32 i;
  147. macroblock_t *mb = mb_buffer->macroblocks;
  148. uint_32 num_blocks = mb_buffer->num_blocks;
  149. for(i=0;i<num_blocks;i++)
  150. {
  151. #if 1
  152. sint_16 *block;
  153. uint_32 j;
  154. block = mb[i].y_blocks;
  155. if(i == 0)
  156. {
  157. fprintf(stderr,"block_beforen");
  158. for(j=0;j<64;j++)
  159. {
  160. fprintf(stderr,"%06d, ",block[j]);
  161. if(!((j+1)%8))
  162. fprintf(stderr,"n");
  163. }
  164. fprintf(stderr,"nn");
  165. }
  166. #endif
  167. if(mb[i].macroblock_type & MACROBLOCK_INTRA)
  168. inv_quantize_intra_mb(picture,&mb[i]);
  169. else
  170. inv_quantize_non_intra_mb(picture,&mb[i]);
  171. #if 1
  172. if(i == 0)
  173. {
  174. fprintf(stderr,"block_aftern");
  175. for(j=0;j<64;j++)
  176. {
  177. fprintf(stderr,"%06d, ",block[j]);
  178. if(!((j+1)%8))
  179. fprintf(stderr,"n");
  180. }
  181. fprintf(stderr,"nn");
  182. }
  183. #endif
  184. }
  185. }