dequant.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "dequant.h"
  19. #include "decint.h"
  20. int oc_quant_params_unpack(oc_pack_buf *_opb,th_quant_info *_qinfo){
  21. th_quant_base *base_mats;
  22. long val;
  23. int nbase_mats;
  24. int sizes[64];
  25. int indices[64];
  26. int nbits;
  27. int bmi;
  28. int ci;
  29. int qti;
  30. int pli;
  31. int qri;
  32. int qi;
  33. int i;
  34. val=oc_pack_read(_opb,3);
  35. nbits=(int)val;
  36. for(qi=0;qi<64;qi++){
  37. val=oc_pack_read(_opb,nbits);
  38. _qinfo->loop_filter_limits[qi]=(unsigned char)val;
  39. }
  40. val=oc_pack_read(_opb,4);
  41. nbits=(int)val+1;
  42. for(qi=0;qi<64;qi++){
  43. val=oc_pack_read(_opb,nbits);
  44. _qinfo->ac_scale[qi]=(ogg_uint16_t)val;
  45. }
  46. val=oc_pack_read(_opb,4);
  47. nbits=(int)val+1;
  48. for(qi=0;qi<64;qi++){
  49. val=oc_pack_read(_opb,nbits);
  50. _qinfo->dc_scale[qi]=(ogg_uint16_t)val;
  51. }
  52. val=oc_pack_read(_opb,9);
  53. nbase_mats=(int)val+1;
  54. base_mats=_ogg_malloc(nbase_mats*sizeof(base_mats[0]));
  55. if(base_mats==NULL)return TH_EFAULT;
  56. for(bmi=0;bmi<nbase_mats;bmi++){
  57. for(ci=0;ci<64;ci++){
  58. val=oc_pack_read(_opb,8);
  59. base_mats[bmi][ci]=(unsigned char)val;
  60. }
  61. }
  62. nbits=oc_ilog(nbase_mats-1);
  63. for(i=0;i<6;i++){
  64. th_quant_ranges *qranges;
  65. th_quant_base *qrbms;
  66. int *qrsizes;
  67. qti=i/3;
  68. pli=i%3;
  69. qranges=_qinfo->qi_ranges[qti]+pli;
  70. if(i>0){
  71. val=oc_pack_read1(_opb);
  72. if(!val){
  73. int qtj;
  74. int plj;
  75. if(qti>0){
  76. val=oc_pack_read1(_opb);
  77. if(val){
  78. qtj=qti-1;
  79. plj=pli;
  80. }
  81. else{
  82. qtj=(i-1)/3;
  83. plj=(i-1)%3;
  84. }
  85. }
  86. else{
  87. qtj=(i-1)/3;
  88. plj=(i-1)%3;
  89. }
  90. *qranges=*(_qinfo->qi_ranges[qtj]+plj);
  91. continue;
  92. }
  93. }
  94. val=oc_pack_read(_opb,nbits);
  95. indices[0]=(int)val;
  96. for(qi=qri=0;qi<63;){
  97. val=oc_pack_read(_opb,oc_ilog(62-qi));
  98. sizes[qri]=(int)val+1;
  99. qi+=(int)val+1;
  100. val=oc_pack_read(_opb,nbits);
  101. indices[++qri]=(int)val;
  102. }
  103. /*Note: The caller is responsible for cleaning up any partially
  104. constructed qinfo.*/
  105. if(qi>63){
  106. _ogg_free(base_mats);
  107. return TH_EBADHEADER;
  108. }
  109. qranges->nranges=qri;
  110. qranges->sizes=qrsizes=(int *)_ogg_malloc(qri*sizeof(qrsizes[0]));
  111. if(qranges->sizes==NULL){
  112. /*Note: The caller is responsible for cleaning up any partially
  113. constructed qinfo.*/
  114. _ogg_free(base_mats);
  115. return TH_EFAULT;
  116. }
  117. memcpy(qrsizes,sizes,qri*sizeof(qrsizes[0]));
  118. qrbms=(th_quant_base *)_ogg_malloc((qri+1)*sizeof(qrbms[0]));
  119. if(qrbms==NULL){
  120. /*Note: The caller is responsible for cleaning up any partially
  121. constructed qinfo.*/
  122. _ogg_free(base_mats);
  123. return TH_EFAULT;
  124. }
  125. qranges->base_matrices=(const th_quant_base *)qrbms;
  126. do{
  127. bmi=indices[qri];
  128. /*Note: The caller is responsible for cleaning up any partially
  129. constructed qinfo.*/
  130. if(bmi>=nbase_mats){
  131. _ogg_free(base_mats);
  132. return TH_EBADHEADER;
  133. }
  134. memcpy(qrbms[qri],base_mats[bmi],sizeof(qrbms[qri]));
  135. }
  136. while(qri-->0);
  137. }
  138. _ogg_free(base_mats);
  139. return 0;
  140. }
  141. void oc_quant_params_clear(th_quant_info *_qinfo){
  142. int i;
  143. for(i=6;i-->0;){
  144. int qti;
  145. int pli;
  146. qti=i/3;
  147. pli=i%3;
  148. /*Clear any duplicate pointer references.*/
  149. if(i>0){
  150. int qtj;
  151. int plj;
  152. qtj=(i-1)/3;
  153. plj=(i-1)%3;
  154. if(_qinfo->qi_ranges[qti][pli].sizes==
  155. _qinfo->qi_ranges[qtj][plj].sizes){
  156. _qinfo->qi_ranges[qti][pli].sizes=NULL;
  157. }
  158. if(_qinfo->qi_ranges[qti][pli].base_matrices==
  159. _qinfo->qi_ranges[qtj][plj].base_matrices){
  160. _qinfo->qi_ranges[qti][pli].base_matrices=NULL;
  161. }
  162. }
  163. if(qti>0){
  164. if(_qinfo->qi_ranges[1][pli].sizes==
  165. _qinfo->qi_ranges[0][pli].sizes){
  166. _qinfo->qi_ranges[1][pli].sizes=NULL;
  167. }
  168. if(_qinfo->qi_ranges[1][pli].base_matrices==
  169. _qinfo->qi_ranges[0][pli].base_matrices){
  170. _qinfo->qi_ranges[1][pli].base_matrices=NULL;
  171. }
  172. }
  173. /*Now free all the non-duplicate storage.*/
  174. _ogg_free((void *)_qinfo->qi_ranges[qti][pli].sizes);
  175. _ogg_free((void *)_qinfo->qi_ranges[qti][pli].base_matrices);
  176. }
  177. }