quant.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ogg/ogg.h>
  18. #include "quant.h"
  19. #include "decint.h"
  20. /*The maximum output of the DCT with +/- 255 inputs is +/- 8157.
  21. These minimum quantizers ensure the result after quantization (and after
  22. prediction for DC) will be no more than +/- 510.
  23. The tokenization system can handle values up to +/- 580, so there is no need
  24. to do any coefficient clamping.
  25. I would rather have allowed smaller quantizers and had to clamp, but these
  26. minimums were required when constructing the original VP3 matrices and have
  27. been formalized in the spec.*/
  28. static const unsigned OC_DC_QUANT_MIN[2]={4<<2,8<<2};
  29. static const unsigned OC_AC_QUANT_MIN[2]={2<<2,4<<2};
  30. /*Initializes the dequantization tables from a set of quantizer info.
  31. Currently the dequantizer (and elsewhere enquantizer) tables are expected to
  32. be initialized as pointing to the storage reserved for them in the
  33. oc_theora_state (resp. oc_enc_ctx) structure.
  34. If some tables are duplicates of others, the pointers will be adjusted to
  35. point to a single copy of the tables, but the storage for them will not be
  36. freed.
  37. If you're concerned about the memory footprint, the obvious thing to do is
  38. to move the storage out of its fixed place in the structures and allocate
  39. it on demand.
  40. However, a much, much better option is to only store the quantization
  41. matrices being used for the current frame, and to recalculate these as the
  42. qi values change between frames (this is what VP3 did).*/
  43. void oc_dequant_tables_init(ogg_uint16_t *_dequant[64][3][2],
  44. int _pp_dc_scale[64],const th_quant_info *_qinfo){
  45. /*Coding mode: intra or inter.*/
  46. int qti;
  47. /*Y', C_b, C_r*/
  48. int pli;
  49. for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
  50. /*Quality index.*/
  51. int qi;
  52. /*Range iterator.*/
  53. int qri;
  54. for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){
  55. th_quant_base base;
  56. ogg_uint32_t q;
  57. int qi_start;
  58. int qi_end;
  59. memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri],
  60. sizeof(base));
  61. qi_start=qi;
  62. if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1;
  63. else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri];
  64. /*Iterate over quality indicies in this range.*/
  65. for(;;){
  66. ogg_uint32_t qfac;
  67. int zzi;
  68. int ci;
  69. /*In the original VP3.2 code, the rounding offset and the size of the
  70. dead zone around 0 were controlled by a "sharpness" parameter.
  71. The size of our dead zone is now controlled by the per-coefficient
  72. quality thresholds returned by our HVS module.
  73. We round down from a more accurate value when the quality of the
  74. reconstruction does not fall below our threshold and it saves bits.
  75. Hence, all of that VP3.2 code is gone from here, and the remaining
  76. floating point code has been implemented as equivalent integer code
  77. with exact precision.*/
  78. qfac=(ogg_uint32_t)_qinfo->dc_scale[qi]*base[0];
  79. /*For postprocessing, not dequantization.*/
  80. if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int)(qfac/160);
  81. /*Scale DC the coefficient from the proper table.*/
  82. q=(qfac/100)<<2;
  83. q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX);
  84. _dequant[qi][pli][qti][0]=(ogg_uint16_t)q;
  85. /*Now scale AC coefficients from the proper table.*/
  86. for(zzi=1;zzi<64;zzi++){
  87. q=((ogg_uint32_t)_qinfo->ac_scale[qi]*base[OC_FZIG_ZAG[zzi]]/100)<<2;
  88. q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX);
  89. _dequant[qi][pli][qti][zzi]=(ogg_uint16_t)q;
  90. }
  91. /*If this is a duplicate of a previous matrix, use that instead.
  92. This simple check helps us improve cache coherency later.*/
  93. {
  94. int dupe;
  95. int qtj;
  96. int plj;
  97. dupe=0;
  98. for(qtj=0;qtj<=qti;qtj++){
  99. for(plj=0;plj<(qtj<qti?3:pli);plj++){
  100. if(!memcmp(_dequant[qi][pli][qti],_dequant[qi][plj][qtj],
  101. sizeof(oc_quant_table))){
  102. dupe=1;
  103. break;
  104. }
  105. }
  106. if(dupe)break;
  107. }
  108. if(dupe)_dequant[qi][pli][qti]=_dequant[qi][plj][qtj];
  109. }
  110. if(++qi>=qi_end)break;
  111. /*Interpolate the next base matrix.*/
  112. for(ci=0;ci<64;ci++){
  113. base[ci]=(unsigned char)(
  114. (2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+
  115. (qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci])
  116. +_qinfo->qi_ranges[qti][pli].sizes[qri])/
  117. (2*_qinfo->qi_ranges[qti][pli].sizes[qri]));
  118. }
  119. }
  120. }
  121. }
  122. }