bitpack.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /********************************************************************
  3. Copyright (C) 2002-2009 Xiph.org Foundation
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
  19. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ********************************************************************/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "bitpack.h"
  31. #include "internal.h"
  32. #include "decint.h"
  33. /*The maximum output of the DCT with +/- 255 inputs is +/- 8157.
  34. These minimum quantizers ensure the result after quantization (and after
  35. prediction for DC) will be no more than +/- 510.
  36. The tokenization system can handle values up to +/- 580, so there is no need
  37. to do any coefficient clamping.
  38. I would rather have allowed smaller quantizers and had to clamp, but these
  39. minimums were required when constructing the original VP3 matrices and have
  40. been formalized in the spec.*/
  41. static const uint32_t OC_DC_QUANT_MIN[2]={4<<2,8<<2};
  42. static const uint32_t OC_AC_QUANT_MIN[2]={2<<2,4<<2};
  43. /*The map from external spec-defined tokens to internal tokens.
  44. This is constructed so that any extra bits read with the original token value
  45. can be masked off the least significant bits of its internal token index.
  46. In addition, all of the tokens which require additional extra bits are placed
  47. at the start of the list, and grouped by type.
  48. OC_DCT_REPEAT_RUN3_TOKEN is placed first, as it is an extra-special case, so
  49. giving it index 0 may simplify comparisons on some architectures.
  50. These requirements require some substantial reordering.*/
  51. static const unsigned char OC_DCT_TOKEN_MAP[TH_NDCT_TOKENS]=
  52. {
  53. /*OC_DCT_EOB1_TOKEN (0 extra bits)*/
  54. 15,
  55. /*OC_DCT_EOB2_TOKEN (0 extra bits)*/
  56. 16,
  57. /*OC_DCT_EOB3_TOKEN (0 extra bits)*/
  58. 17,
  59. /*OC_DCT_REPEAT_RUN0_TOKEN (2 extra bits)*/
  60. 88,
  61. /*OC_DCT_REPEAT_RUN1_TOKEN (3 extra bits)*/
  62. 80,
  63. /*OC_DCT_REPEAT_RUN2_TOKEN (4 extra bits)*/
  64. 1,
  65. /*OC_DCT_REPEAT_RUN3_TOKEN (12 extra bits)*/
  66. 0,
  67. /*OC_DCT_SHORT_ZRL_TOKEN (3 extra bits)*/
  68. 48,
  69. /*OC_DCT_ZRL_TOKEN (6 extra bits)*/
  70. 14,
  71. /*OC_ONE_TOKEN (0 extra bits)*/
  72. 56,
  73. /*OC_MINUS_ONE_TOKEN (0 extra bits)*/
  74. 57,
  75. /*OC_TWO_TOKEN (0 extra bits)*/
  76. 58,
  77. /*OC_MINUS_TWO_TOKEN (0 extra bits)*/
  78. 59,
  79. /*OC_DCT_VAL_CAT2 (1 extra bit)*/
  80. 60,
  81. 62,
  82. 64,
  83. 66,
  84. /*OC_DCT_VAL_CAT3 (2 extra bits)*/
  85. 68,
  86. /*OC_DCT_VAL_CAT4 (3 extra bits)*/
  87. 72,
  88. /*OC_DCT_VAL_CAT5 (4 extra bits)*/
  89. 2,
  90. /*OC_DCT_VAL_CAT6 (5 extra bits)*/
  91. 4,
  92. /*OC_DCT_VAL_CAT7 (6 extra bits)*/
  93. 6,
  94. /*OC_DCT_VAL_CAT8 (10 extra bits)*/
  95. 8,
  96. /*OC_DCT_RUN_CAT1A (1 extra bit)*/
  97. 18,
  98. 20,
  99. 22,
  100. 24,
  101. 26,
  102. /*OC_DCT_RUN_CAT1B (3 extra bits)*/
  103. 32,
  104. /*OC_DCT_RUN_CAT1C (4 extra bits)*/
  105. 12,
  106. /*OC_DCT_RUN_CAT2A (2 extra bits)*/
  107. 28,
  108. /*OC_DCT_RUN_CAT2B (3 extra bits)*/
  109. 40
  110. };
  111. /*The log base 2 of number of internal tokens associated with each of the spec
  112. tokens (i.e., how many of the extra bits are folded into the token value).
  113. Increasing the maximum value beyond 3 will enlarge the amount of stack
  114. required for tree construction.*/
  115. static const unsigned char OC_DCT_TOKEN_MAP_LOG_NENTRIES[TH_NDCT_TOKENS]={
  116. 0,0,0,2,3,0,0,3,0,0,0,0,0,1,1,1,1,2,3,1,1,1,2,1,1,1,1,1,3,1,2,3
  117. };
  118. #define OC_HUFF_SLUSH (2)
  119. #define OC_ROOT_HUFF_SLUSH (7)
  120. /*Unpacks a Huffman codebook.
  121. _opb: The buffer to unpack from.
  122. _tokens: Stores a list of internal tokens, in the order they were found in
  123. the codebook, and the lengths of their corresponding codewords.
  124. This is enough to completely define the codebook, while minimizing
  125. stack usage and avoiding temporary allocations (for platforms
  126. where free() is a no-op).
  127. Return: The number of internal tokens in the codebook, or a negative value
  128. on error.*/
  129. int32_t oc_huff_tree_unpack(oc_pack_buf *_opb,unsigned char _tokens[256][2])
  130. {
  131. uint32_t code;
  132. int32_t len;
  133. int32_t ntokens;
  134. int32_t nleaves;
  135. code=0;
  136. len=ntokens=nleaves=0;
  137. for(;;){
  138. long bits;
  139. bits=oc_pack_read1(_opb);
  140. /*Only process nodes so long as there's more bits in the buffer.*/
  141. if(oc_pack_bytes_left(_opb)<0) return TH_EBADHEADER;
  142. /*Read an internal node:*/
  143. if(!bits){
  144. len++;
  145. /*Don't allow codewords longer than 32 bits.*/
  146. if(len>32)return TH_EBADHEADER;
  147. }
  148. /*Read a leaf node:*/
  149. else{
  150. uint32_t code_bit;
  151. int32_t neb;
  152. int32_t nentries;
  153. int32_t token;
  154. /*Don't allow more than 32 spec-tokens per codebook.*/
  155. if(++nleaves>32)return TH_EBADHEADER;
  156. bits=oc_pack_read(_opb,OC_NDCT_TOKEN_BITS);
  157. neb=OC_DCT_TOKEN_MAP_LOG_NENTRIES[bits];
  158. token=OC_DCT_TOKEN_MAP[bits];
  159. nentries=1<<neb;
  160. while(nentries-->0){
  161. _tokens[ntokens][0]=(unsigned char)token++;
  162. _tokens[ntokens][1]=(unsigned char)(len+neb);
  163. ntokens++;
  164. }
  165. code_bit=0x80000000U>>len-1;
  166. while(len>0&&(code&code_bit)){
  167. code^=code_bit;
  168. code_bit<<=1;
  169. len--;
  170. }
  171. if(len<=0)break;
  172. code|=code_bit;
  173. }
  174. }
  175. return ntokens;
  176. }
  177. /*Count how many tokens would be required to fill a subtree at depth _depth.
  178. _tokens: A list of internal tokens, in the order they are found in the
  179. codebook, and the lengths of their corresponding codewords.
  180. _depth: The depth of the desired node in the corresponding tree structure.
  181. Return: The number of tokens that belong to that subtree.*/
  182. static int32_t oc_huff_subtree_tokens(unsigned char _tokens[][2],int32_t _depth)
  183. {
  184. uint32_t code;
  185. int32_t ti;
  186. code=0;
  187. ti=0;
  188. do{
  189. if(_tokens[ti][1]-_depth<32)code+=0x80000000U>>_tokens[ti++][1]-_depth;
  190. else{
  191. /*Because of the expanded internal tokens, we can have codewords as long
  192. as 35 bits.
  193. A single recursion here is enough to advance past them.*/
  194. code++;
  195. ti+=oc_huff_subtree_tokens(_tokens+ti,_depth+31);
  196. }
  197. }
  198. while(code<0x80000000U);
  199. return ti;
  200. }
  201. /*Compute the number of bits to use for a collapsed tree node at the given
  202. depth.
  203. _tokens: A list of internal tokens, in the order they are found in the
  204. codebook, and the lengths of their corresponding codewords.
  205. _ntokens: The number of tokens corresponding to this tree node.
  206. _depth: The depth of this tree node.
  207. Return: The number of bits to use for a collapsed tree node rooted here.
  208. This is always at least one, even if this was a leaf node.*/
  209. static int32_t oc_huff_tree_collapse_depth(unsigned char _tokens[][2],
  210. int32_t _ntokens,int32_t _depth)
  211. {
  212. int32_t got_leaves;
  213. int32_t loccupancy;
  214. int32_t occupancy;
  215. int32_t slush;
  216. int32_t nbits;
  217. int32_t best_nbits;
  218. slush=_depth>0?OC_HUFF_SLUSH:OC_ROOT_HUFF_SLUSH;
  219. /*It's legal to have a tree with just a single node, which requires no bits
  220. to decode and always returns the same token.
  221. However, no encoder actually does this (yet).
  222. To avoid a special case in oc_huff_token_decode(), we force the number of
  223. lookahead bits to be at least one.
  224. This will produce a tree that looks ahead one bit and then advances the
  225. stream zero bits.*/
  226. nbits=1;
  227. occupancy=2;
  228. got_leaves=1;
  229. do{
  230. int32_t ti;
  231. if(got_leaves)best_nbits=nbits;
  232. nbits++;
  233. got_leaves=0;
  234. loccupancy=occupancy;
  235. for(occupancy=ti=0;ti<_ntokens;occupancy++){
  236. if(_tokens[ti][1]<_depth+nbits)ti++;
  237. else if(_tokens[ti][1]==_depth+nbits){
  238. got_leaves=1;
  239. ti++;
  240. }
  241. else ti+=oc_huff_subtree_tokens(_tokens+ti,_depth+nbits);
  242. }
  243. }
  244. while(occupancy>loccupancy&&occupancy*slush>=1<<nbits);
  245. return best_nbits;
  246. }
  247. /*Determines the size in words of a Huffman tree node that represents a
  248. subtree of depth _nbits.
  249. _nbits: The depth of the subtree.
  250. This must be greater than zero.
  251. Return: The number of words required to store the node.*/
  252. static size_t oc_huff_node_size(int32_t _nbits){
  253. return 1+(1<<_nbits);
  254. }
  255. /*Produces a collapsed-tree representation of the given token list.
  256. _tree: The storage for the collapsed Huffman tree.
  257. This may be NULL to compute the required storage size instead of
  258. constructing the tree.
  259. _tokens: A list of internal tokens, in the order they are found in the
  260. codebook, and the lengths of their corresponding codewords.
  261. _ntokens: The number of tokens corresponding to this tree node.
  262. Return: The number of words required to store the tree.*/
  263. static size_t oc_huff_tree_collapse(int16_t *_tree,
  264. unsigned char _tokens[][2],int32_t _ntokens)
  265. {
  266. int16_t node[34];
  267. unsigned char depth[34];
  268. unsigned char last[34];
  269. size_t ntree;
  270. int32_t ti;
  271. int32_t l;
  272. depth[0]=0;
  273. last[0]=(unsigned char)(_ntokens-1);
  274. ntree=0;
  275. ti=0;
  276. l=0;
  277. do{
  278. int32_t nbits;
  279. nbits=oc_huff_tree_collapse_depth(_tokens+ti,last[l]+1-ti,depth[l]);
  280. node[l]=(int16_t)ntree;
  281. ntree+=oc_huff_node_size(nbits);
  282. if(_tree!=NULL)_tree[node[l]++]=(int16_t)nbits;
  283. do{
  284. while(ti<=last[l]&&_tokens[ti][1]<=depth[l]+nbits){
  285. if(_tree!=NULL){
  286. int16_t leaf;
  287. int32_t nentries;
  288. nentries=1<<depth[l]+nbits-_tokens[ti][1];
  289. leaf=(int16_t)-(_tokens[ti][1]-depth[l]<<8|_tokens[ti][0]);
  290. while(nentries-->0)_tree[node[l]++]=leaf;
  291. }
  292. ti++;
  293. }
  294. if(ti<=last[l]){
  295. /*We need to recurse*/
  296. depth[l+1]=(unsigned char)(depth[l]+nbits);
  297. if(_tree!=NULL)_tree[node[l]++]=(int16_t)ntree;
  298. l++;
  299. last[l]=
  300. (unsigned char)(ti+oc_huff_subtree_tokens(_tokens+ti,depth[l])-1);
  301. break;
  302. }
  303. /*Pop back up a level of recursion.*/
  304. else if(l-->0)nbits=depth[l+1]-depth[l];
  305. }
  306. while(l>=0);
  307. }
  308. while(l>=0);
  309. return ntree;
  310. }
  311. /*Unpacks a set of Huffman trees, and reduces them to a collapsed
  312. representation.
  313. _opb: The buffer to unpack the trees from.
  314. _nodes: The table to fill with the Huffman trees.
  315. Return: 0 on success, or a negative value on error.*/
  316. int32_t oc_huff_trees_unpack(oc_pack_buf *_opb,
  317. int16_t *_nodes[TH_NHUFFMAN_TABLES])
  318. {
  319. int32_t ret;
  320. int32_t i;
  321. ret=0;
  322. for(i=0;i<TH_NHUFFMAN_TABLES;i++){
  323. unsigned char tokens[256][2];
  324. int32_t ntokens;
  325. int16_t *tree;
  326. size_t size;
  327. /*Unpack the full tree into a temporary buffer.*/
  328. ntokens=oc_huff_tree_unpack(_opb,tokens);
  329. if(ntokens<0){
  330. ret=ntokens;
  331. break;
  332. }
  333. /*Figure out how big the collapsed tree will be and allocate space for it.*/
  334. size=oc_huff_tree_collapse(NULL,tokens,ntokens);
  335. if(size>32767){
  336. /*This should never happen; if it does it means you set OC_HUFF_SLUSH or
  337. OC_ROOT_HUFF_SLUSH too large.*/
  338. ret=TH_EIMPL;
  339. break;
  340. }
  341. tree=(int16_t *)malloc(size*sizeof(*tree));
  342. if(tree==NULL){
  343. ret=TH_EFAULT;
  344. break;
  345. }
  346. /*Construct the collapsed the tree.*/
  347. oc_huff_tree_collapse(tree,tokens,ntokens);
  348. _nodes[i]=tree;
  349. }
  350. if(ret<0)while(i-->0)free(_nodes[i]);
  351. return ret;
  352. }
  353. /*Determines the size in words of a Huffman subtree.
  354. _tree: The complete Huffman tree.
  355. _node: The index of the root of the desired subtree.
  356. Return: The number of words required to store the tree.*/
  357. static size_t oc_huff_tree_size(const int16_t *_tree,int32_t _node)
  358. {
  359. size_t size;
  360. int32_t nchildren;
  361. int32_t n;
  362. int32_t i;
  363. n=_tree[_node];
  364. size=oc_huff_node_size(n);
  365. nchildren=1<<n;
  366. i=0;
  367. do{
  368. int32_t child;
  369. child=_tree[_node+i+1];
  370. if(child<=0)i+=1<<n-(-child>>8);
  371. else{
  372. size+=oc_huff_tree_size(_tree,child);
  373. i++;
  374. }
  375. }
  376. while(i<nchildren);
  377. return size;
  378. }
  379. /*Makes a copy of the given set of Huffman trees.
  380. _dst: The array to store the copy in.
  381. _src: The array of trees to copy.*/
  382. int32_t oc_huff_trees_copy(int16_t *_dst[TH_NHUFFMAN_TABLES],
  383. const int16_t *const _src[TH_NHUFFMAN_TABLES])
  384. {
  385. int32_t total;
  386. int32_t i;
  387. total=0;
  388. for(i=0;i<TH_NHUFFMAN_TABLES;i++){
  389. size_t size;
  390. size=oc_huff_tree_size(_src[i],0);
  391. total+=size;
  392. _dst[i]=(int16_t *)malloc(size*sizeof(*_dst[i]));
  393. if(_dst[i]==NULL){
  394. while(i-->0)free(_dst[i]);
  395. return TH_EFAULT;
  396. }
  397. memcpy(_dst[i],_src[i],size*sizeof(*_dst[i]));
  398. }
  399. return 0;
  400. }
  401. /*Frees the memory used by a set of Huffman trees.
  402. _nodes: The array of trees to free.*/
  403. void oc_huff_trees_clear(int16_t *_nodes[TH_NHUFFMAN_TABLES]){
  404. int32_t i;
  405. for(i=0;i<TH_NHUFFMAN_TABLES;i++)free(_nodes[i]);
  406. }
  407. /*Unpacks a single token using the given Huffman tree.
  408. _opb: The buffer to unpack the token from.
  409. _node: The tree to unpack the token with.
  410. Return: The token value.*/
  411. int32_t oc_huff_token_decode_c(oc_pack_buf *_opb,const int16_t *_tree)
  412. {
  413. const unsigned char *ptr;
  414. const unsigned char *stop;
  415. oc_pb_window window;
  416. int32_t available;
  417. long bits;
  418. int32_t node;
  419. int32_t n;
  420. ptr=_opb->ptr;
  421. window=_opb->window;
  422. stop=_opb->stop;
  423. available=_opb->bits;
  424. node=0;
  425. for(;;){
  426. n=_tree[node];
  427. if(n>available){
  428. uint32_t shift;
  429. shift=OC_PB_WINDOW_SIZE-available;
  430. do{
  431. /*We don't bother setting eof because we won't check for it after we've
  432. started decoding DCT tokens.*/
  433. if(ptr>=stop){
  434. shift=-OC_LOTS_OF_BITS;
  435. break;
  436. }
  437. shift-=8;
  438. window|=(oc_pb_window)*ptr++<<shift;
  439. }
  440. while(shift>=8);
  441. /*Note: We never request more than 24 bits, so there's no need to fill in
  442. the last partial byte here.*/
  443. available=OC_PB_WINDOW_SIZE-shift;
  444. }
  445. bits=window>>OC_PB_WINDOW_SIZE-n;
  446. node=_tree[node+1+bits];
  447. if(node<=0)break;
  448. window<<=n;
  449. available-=n;
  450. }
  451. node=-node;
  452. n=node>>8;
  453. window<<=n;
  454. available-=n;
  455. _opb->ptr=ptr;
  456. _opb->window=window;
  457. _opb->bits=available;
  458. return node&255;
  459. }
  460. void oc_pack_readinit(oc_pack_buf *_b,unsigned char *_buf,long _bytes)
  461. {
  462. memset(_b,0,sizeof(*_b));
  463. _b->ptr=_buf;
  464. _b->stop=_buf+_bytes;
  465. }
  466. static oc_pb_window oc_pack_refill(oc_pack_buf *_b,int32_t _bits)
  467. {
  468. const unsigned char *ptr;
  469. const unsigned char *stop;
  470. oc_pb_window window;
  471. int32_t available;
  472. uint32_t shift;
  473. stop=_b->stop;
  474. ptr=_b->ptr;
  475. window=_b->window;
  476. available=_b->bits;
  477. shift=OC_PB_WINDOW_SIZE-available;
  478. while(7<shift&&ptr<stop){
  479. shift-=8;
  480. window|=(oc_pb_window)*ptr++<<shift;
  481. }
  482. _b->ptr=ptr;
  483. available=OC_PB_WINDOW_SIZE-shift;
  484. if(_bits>available){
  485. if(ptr>=stop){
  486. _b->eof=1;
  487. available=OC_LOTS_OF_BITS;
  488. }
  489. else window|=*ptr>>(available&7);
  490. }
  491. _b->bits=available;
  492. return window;
  493. }
  494. int32_t oc_pack_look1(oc_pack_buf *_b)
  495. {
  496. oc_pb_window window;
  497. int32_t available;
  498. window=_b->window;
  499. available=_b->bits;
  500. if(available<1)_b->window=window=oc_pack_refill(_b,1);
  501. return window>>OC_PB_WINDOW_SIZE-1;
  502. }
  503. void oc_pack_adv1(oc_pack_buf *_b)
  504. {
  505. _b->window<<=1;
  506. _b->bits--;
  507. }
  508. /*Here we assume that 0<=_bits&&_bits<=32.*/
  509. long oc_pack_read_c(oc_pack_buf *_b,int32_t _bits)
  510. {
  511. oc_pb_window window;
  512. int32_t available;
  513. long result;
  514. window=_b->window;
  515. available=_b->bits;
  516. if(_bits==0)return 0;
  517. if(available<_bits){
  518. window=oc_pack_refill(_b,_bits);
  519. available=_b->bits;
  520. }
  521. result=window>>OC_PB_WINDOW_SIZE-_bits;
  522. available-=_bits;
  523. window<<=1;
  524. window<<=_bits-1;
  525. _b->window=window;
  526. _b->bits=available;
  527. return result;
  528. }
  529. int32_t oc_pack_read1_c(oc_pack_buf *_b)
  530. {
  531. oc_pb_window window;
  532. int32_t available;
  533. int32_t result;
  534. window=_b->window;
  535. available=_b->bits;
  536. if(available<1){
  537. window=oc_pack_refill(_b,1);
  538. available=_b->bits;
  539. }
  540. result=window>>OC_PB_WINDOW_SIZE-1;
  541. available--;
  542. window<<=1;
  543. _b->window=window;
  544. _b->bits=available;
  545. return result;
  546. }
  547. long oc_pack_bytes_left(oc_pack_buf *_b)
  548. {
  549. if(_b->eof)return -1;
  550. return _b->stop-_b->ptr+(_b->bits>>3);
  551. }
  552. /*Unpacks a series of octets from a given byte array into the pack buffer.
  553. No checking is done to ensure the buffer contains enough data.
  554. _opb: The pack buffer to read the octets from.
  555. _buf: The byte array to store the unpacked bytes in.
  556. _len: The number of octets to unpack.*/
  557. static void oc_unpack_octets(oc_pack_buf *_opb,char *_buf,size_t _len)
  558. {
  559. while(_len-->0){
  560. long val;
  561. val=oc_pack_read(_opb,8);
  562. *_buf++=(char)val;
  563. }
  564. }
  565. /*Unpacks a 32-bit integer encoded by octets in little-endian form.*/
  566. static long oc_unpack_length(oc_pack_buf *_opb)
  567. {
  568. long ret[4];
  569. int32_t i;
  570. for(i=0;i<4;i++)ret[i]=oc_pack_read(_opb,8);
  571. return ret[0]|ret[1]<<8|ret[2]<<16|ret[3]<<24;
  572. }
  573. static int32_t oc_info_unpack(oc_pack_buf *_opb,th_info *_info)
  574. {
  575. long val;
  576. /*Check the codec bitstream version.*/
  577. val=oc_pack_read(_opb,8);
  578. _info->version_major=(unsigned char)val;
  579. val=oc_pack_read(_opb,8);
  580. _info->version_minor=(unsigned char)val;
  581. val=oc_pack_read(_opb,8);
  582. _info->version_subminor=(unsigned char)val;
  583. /*verify we can parse this bitstream version.
  584. We accept earlier minors and all subminors, by spec*/
  585. if(_info->version_major>TH_VERSION_MAJOR||
  586. _info->version_major==TH_VERSION_MAJOR&&
  587. _info->version_minor>TH_VERSION_MINOR){
  588. return TH_EVERSION;
  589. }
  590. /*Read the encoded frame description.*/
  591. val=oc_pack_read(_opb,16);
  592. _info->frame_width=(uint32_t)val<<4;
  593. val=oc_pack_read(_opb,16);
  594. _info->frame_height=(uint32_t)val<<4;
  595. val=oc_pack_read(_opb,24);
  596. _info->pic_width=(uint32_t)val;
  597. val=oc_pack_read(_opb,24);
  598. _info->pic_height=(uint32_t)val;
  599. val=oc_pack_read(_opb,8);
  600. _info->pic_x=(uint32_t)val;
  601. val=oc_pack_read(_opb,8);
  602. _info->pic_y=(uint32_t)val;
  603. val=oc_pack_read(_opb,32);
  604. _info->fps_numerator=(uint32_t)val;
  605. val=oc_pack_read(_opb,32);
  606. _info->fps_denominator=(uint32_t)val;
  607. if(_info->frame_width==0||_info->frame_height==0||
  608. _info->pic_width+_info->pic_x>_info->frame_width||
  609. _info->pic_height+_info->pic_y>_info->frame_height||
  610. _info->fps_numerator==0||_info->fps_denominator==0){
  611. return TH_EBADHEADER;
  612. }
  613. /*Note: The sense of pic_y is inverted in what we pass back to the
  614. application compared to how it is stored in the bitstream.
  615. This is because the bitstream uses a right-handed coordinate system, while
  616. applications expect a left-handed one.*/
  617. _info->pic_y=_info->frame_height-_info->pic_height-_info->pic_y;
  618. val=oc_pack_read(_opb,24);
  619. _info->aspect_numerator=(uint32_t)val;
  620. val=oc_pack_read(_opb,24);
  621. _info->aspect_denominator=(uint32_t)val;
  622. val=oc_pack_read(_opb,8);
  623. _info->colorspace=(th_colorspace)val;
  624. val=oc_pack_read(_opb,24);
  625. _info->target_bitrate=(int32_t)val;
  626. val=oc_pack_read(_opb,6);
  627. _info->quality=(int32_t)val;
  628. val=oc_pack_read(_opb,5);
  629. _info->keyframe_granule_shift=(int32_t)val;
  630. val=oc_pack_read(_opb,2);
  631. _info->pixel_fmt=(th_pixel_fmt)val;
  632. if(_info->pixel_fmt==TH_PF_RSVD)return TH_EBADHEADER;
  633. val=oc_pack_read(_opb,3);
  634. if(val!=0||oc_pack_bytes_left(_opb)<0)return TH_EBADHEADER;
  635. return 0;
  636. }
  637. static int32_t oc_comment_unpack(oc_pack_buf *_opb,th_comment *_tc)
  638. {
  639. long len;
  640. int32_t i;
  641. /*Read the vendor string.*/
  642. len=oc_unpack_length(_opb);
  643. if(len<0||len>oc_pack_bytes_left(_opb))return TH_EBADHEADER;
  644. _tc->vendor=malloc((size_t)len+1);
  645. if(_tc->vendor==NULL)return TH_EFAULT;
  646. oc_unpack_octets(_opb,_tc->vendor,len);
  647. _tc->vendor[len]='\0';
  648. /*Read the user comments.*/
  649. _tc->comments=(int32_t)oc_unpack_length(_opb);
  650. len=_tc->comments;
  651. if(len<0||len>(LONG_MAX>>2)||len<<2>oc_pack_bytes_left(_opb)){
  652. _tc->comments=0;
  653. return TH_EBADHEADER;
  654. }
  655. _tc->comment_lengths=(int32_t *)malloc(
  656. _tc->comments*sizeof(_tc->comment_lengths[0]));
  657. _tc->user_comments=(char **)malloc(
  658. _tc->comments*sizeof(_tc->user_comments[0]));
  659. if(_tc->comment_lengths==NULL||_tc->user_comments==NULL){
  660. _tc->comments=0;
  661. return TH_EFAULT;
  662. }
  663. for(i=0;i<_tc->comments;i++){
  664. len=oc_unpack_length(_opb);
  665. if(len<0||len>oc_pack_bytes_left(_opb)){
  666. _tc->comments=i;
  667. return TH_EBADHEADER;
  668. }
  669. _tc->comment_lengths[i]=len;
  670. _tc->user_comments[i]=malloc((size_t)len+1);
  671. if(_tc->user_comments[i]==NULL){
  672. _tc->comments=i;
  673. return TH_EFAULT;
  674. }
  675. oc_unpack_octets(_opb,_tc->user_comments[i],len);
  676. _tc->user_comments[i][len]='\0';
  677. }
  678. return oc_pack_bytes_left(_opb)<0?TH_EBADHEADER:0;
  679. }
  680. static int32_t oc_setup_unpack(oc_pack_buf *_opb,th_setup_info *_setup)
  681. {
  682. int32_t ret;
  683. /*Read the quantizer tables.*/
  684. ret=oc_quant_params_unpack(_opb,&_setup->qinfo);
  685. if(ret<0)return ret;
  686. /*Read the Huffman trees.*/
  687. return oc_huff_trees_unpack(_opb,_setup->huff_tables);
  688. }
  689. static void oc_setup_clear(th_setup_info *_setup){
  690. oc_quant_params_clear(&_setup->qinfo);
  691. oc_huff_trees_clear(_setup->huff_tables);
  692. }
  693. static int32_t split_headers(uint8_t *packet, int32_t packet_size,
  694. int32_t first_header_size, uint8_t *header_start[3],
  695. int32_t header_len[3])
  696. {
  697. int32_t i;
  698. if (packet_size >= 6 && (((((const uint8_t*)(packet))[0] << 8) | ((const uint8_t*)(packet))[1]) == first_header_size))
  699. {
  700. int32_t overall_len = 6;
  701. for (i=0; i<3; i++)
  702. {
  703. header_len[i] = ((((const uint8_t*)(packet))[0] << 8) | ((const uint8_t*)(packet))[1]);
  704. packet += 2;
  705. header_start[i] = packet;
  706. packet += header_len[i];
  707. if (overall_len > packet_size - header_len[i])
  708. return -1;
  709. overall_len += header_len[i];
  710. }
  711. }
  712. else if (packet_size >= 3 && packet_size < INT_MAX - 0x1ff && packet[0] == 2)
  713. {
  714. int32_t overall_len = 3;
  715. packet++;
  716. for (i=0; i<2; i++, packet++)
  717. {
  718. header_len[i] = 0;
  719. for (; overall_len < packet_size && *packet==0xff; packet++)
  720. {
  721. header_len[i] += 0xff;
  722. overall_len += 0xff + 1;
  723. }
  724. header_len[i] += *packet;
  725. overall_len += *packet;
  726. if (overall_len > packet_size)
  727. return -1;
  728. }
  729. header_len[2] = packet_size - overall_len;
  730. header_start[0] = packet;
  731. header_start[1] = header_start[0] + header_len[0];
  732. header_start[2] = header_start[1] + header_len[1];
  733. }
  734. else
  735. {
  736. return -1;
  737. }
  738. return 0;
  739. }
  740. static int32_t oc_dec_headerin(oc_pack_buf *_opb,th_info *_info,
  741. th_comment *_tc,th_setup_info **_setup)
  742. {
  743. char buffer[6];
  744. long val;
  745. int32_t packtype;
  746. int32_t ret;
  747. val=oc_pack_read(_opb,8);
  748. packtype=(int32_t)val;
  749. /*If we're at a data packet and we have received all three headers, we're
  750. done.*/
  751. if(!(packtype&0x80)&&_info->frame_width>0&&_tc->vendor!=NULL&&*_setup!=NULL){
  752. return 0;
  753. }
  754. /*Check the codec string.*/
  755. oc_unpack_octets(_opb,buffer,6);
  756. if(memcmp(buffer,"theora",6)!=0)return TH_ENOTFORMAT;
  757. switch(packtype)
  758. {
  759. /*Codec info header.*/
  760. case 0x80:{
  761. /*This should be the first packet, and we should not already be
  762. initialized.*/
  763. ret=oc_info_unpack(_opb,_info);
  764. if(ret<0)th_info_clear(_info);
  765. else ret=3;
  766. }break;
  767. /*Comment header.*/
  768. case 0x81:{
  769. if(_tc==NULL)return TH_EFAULT;
  770. /*We shoud have already decoded the info header, and should not yet have
  771. decoded the comment header.*/
  772. ret=oc_comment_unpack(_opb,_tc);
  773. if(ret<0)th_comment_clear(_tc);
  774. else ret=2;
  775. }break;
  776. /*Codec setup header.*/
  777. case 0x82:{
  778. oc_setup_info *setup;
  779. setup=(oc_setup_info *)calloc(1,sizeof(*setup));
  780. if(setup==NULL)return TH_EFAULT;
  781. ret=oc_setup_unpack(_opb,setup);
  782. if(ret<0){
  783. oc_setup_clear(setup);
  784. free(setup);
  785. }
  786. else{
  787. *_setup=setup;
  788. ret=1;
  789. }
  790. }break;
  791. default:{
  792. /*We don't know what this header is.*/
  793. return TH_EBADHEADER;
  794. }break;
  795. }
  796. return ret;
  797. }
  798. /*Decodes one header packet.
  799. This should be called repeatedly with the packets at the beginning of the
  800. stream until it returns 0.*/
  801. int32_t th_decode_headerin(th_info *_info,th_comment *_tc, th_setup_info **_setup,
  802. unsigned char *header, int32_t header_size)
  803. {
  804. int32_t i;
  805. int32_t header_len[3];
  806. uint8_t *header_start[3];
  807. oc_pack_buf opb;
  808. if(header==NULL)return TH_EBADHEADER;
  809. if(_info==NULL)return TH_EFAULT;
  810. if (split_headers(header, header_size, 42,
  811. header_start, header_len) < 0)
  812. {
  813. fprintf(stderr, "[Error] Corrupt extradata\n");
  814. return -1;
  815. }
  816. for (i = 0; i < 3; i++)
  817. {
  818. oc_pack_readinit(&opb, header_start[i], header_len[i]);
  819. oc_dec_headerin(&opb,_info,_tc,_setup);
  820. // conform!!
  821. //if (s->theora < 0x030200)
  822. // break;
  823. }
  824. return 0;
  825. }
  826. void th_setupsys_mem_free(th_setup_info *_setup)
  827. {
  828. if(_setup!=NULL){
  829. oc_setup_clear(_setup);
  830. free(_setup);
  831. }
  832. }
  833. /*Initializes the dequantization tables from a set of quantizer info.
  834. Currently the dequantizer (and elsewhere enquantizer) tables are expected to
  835. be initialized as pointing to the storage reserved for them in the
  836. oc_theora_state (resp. oc_enc_ctx) structure.
  837. If some tables are duplicates of others, the pointers will be adjusted to
  838. point to a single copy of the tables, but the storage for them will not be
  839. freed.
  840. If you're concerned about the memory footprint, the obvious thing to do is
  841. to move the storage out of its fixed place in the structures and allocate
  842. it on demand.
  843. However, a much, much better option is to only store the quantization
  844. matrices being used for the current frame, and to recalculate these as the
  845. qi values change between frames (this is what VP3 did).*/
  846. void oc_dequant_tables_init(uint16_t *_dequant[64][3][2],
  847. int32_t _pp_dc_scale[64],const th_quant_info *_qinfo)
  848. {
  849. /*Coding mode: intra or inter.*/
  850. int32_t qti;
  851. /*Y', C_b, C_r*/
  852. int32_t pli;
  853. for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++)
  854. {
  855. /*Quality index.*/
  856. int32_t qi;
  857. /*Range iterator.*/
  858. int32_t qri;
  859. for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){
  860. th_quant_base base;
  861. uint32_t q;
  862. int32_t qi_start;
  863. int32_t qi_end;
  864. memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri],
  865. sizeof(base));
  866. qi_start=qi;
  867. if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1;
  868. else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri];
  869. /*Iterate over quality indicies in this range.*/
  870. for(;;){
  871. uint32_t qfac;
  872. int32_t zzi;
  873. int32_t ci;
  874. /*In the original VP3.2 code, the rounding offset and the size of the
  875. dead zone around 0 were controlled by a "sharpness" parameter.
  876. The size of our dead zone is now controlled by the per-coefficient
  877. quality thresholds returned by our HVS module.
  878. We round down from a more accurate value when the quality of the
  879. reconstruction does not fall below our threshold and it saves bits.
  880. Hence, all of that VP3.2 code is gone from here, and the remaining
  881. floating point code has been implemented as equivalent integer code
  882. with exact precision.*/
  883. qfac=(uint32_t)_qinfo->dc_scale[qi]*base[0];
  884. /*For postprocessing, not dequantization.*/
  885. if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int32_t)(qfac/160);
  886. /*Scale DC the coefficient from the proper table.*/
  887. q=(qfac/100)<<2;
  888. q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX);
  889. _dequant[qi][pli][qti][0]=(uint16_t)q;
  890. /*Now scale AC coefficients from the proper table.*/
  891. for(zzi=1;zzi<64;zzi++){
  892. q=((uint32_t)_qinfo->ac_scale[qi]*base[zzi]/100)<<2;
  893. q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX);
  894. _dequant[qi][pli][qti][zzi]=(uint16_t)q;
  895. }
  896. /*If this is a duplicate of a previous matrix, use that instead.
  897. This simple check helps us improve cache coherency later.*/
  898. {
  899. int32_t dupe;
  900. int32_t qtj;
  901. int32_t plj;
  902. dupe=0;
  903. for(qtj=0;qtj<=qti;qtj++){
  904. for(plj=0;plj<(qtj<qti?3:pli);plj++){
  905. if(!memcmp(_dequant[qi][pli][qti],_dequant[qi][plj][qtj],
  906. sizeof(oc_quant_table))){
  907. dupe=1;
  908. break;
  909. }
  910. }
  911. if(dupe)break;
  912. }
  913. if(dupe)_dequant[qi][pli][qti]=_dequant[qi][plj][qtj];
  914. }
  915. if(++qi>=qi_end)break;
  916. /*Interpolate the next base matrix.*/
  917. for(ci=0;ci<64;ci++){
  918. base[ci]=(unsigned char)(
  919. (2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+
  920. (qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci])
  921. +_qinfo->qi_ranges[qti][pli].sizes[qri])/
  922. (2*_qinfo->qi_ranges[qti][pli].sizes[qri]));
  923. }
  924. }
  925. }
  926. }
  927. }
  928. int32_t oc_quant_params_unpack(oc_pack_buf *_opb,th_quant_info *_qinfo)
  929. {
  930. th_quant_base *base_mats;
  931. long val;
  932. int32_t nbase_mats;
  933. int32_t sizes[64];
  934. int32_t indices[64];
  935. int32_t nbits;
  936. int32_t bmi;
  937. int32_t ci;
  938. int32_t qti;
  939. int32_t pli;
  940. int32_t qri;
  941. int32_t qi;
  942. int32_t i;
  943. val=oc_pack_read(_opb,3);
  944. nbits=(int32_t)val;
  945. for(qi=0;qi<64;qi++){
  946. val=oc_pack_read(_opb,nbits);
  947. _qinfo->loop_filter_limits[qi]=(unsigned char)val;
  948. }
  949. val=oc_pack_read(_opb,4);
  950. nbits=(int32_t)val+1;
  951. for(qi=0;qi<64;qi++){
  952. val=oc_pack_read(_opb,nbits);
  953. _qinfo->ac_scale[qi]=(uint16_t)val;
  954. }
  955. val=oc_pack_read(_opb,4);
  956. nbits=(int32_t)val+1;
  957. for(qi=0;qi<64;qi++){
  958. val=oc_pack_read(_opb,nbits);
  959. _qinfo->dc_scale[qi]=(uint16_t)val;
  960. }
  961. val=oc_pack_read(_opb,9);
  962. nbase_mats=(int32_t)val+1;
  963. base_mats=malloc(nbase_mats*sizeof(base_mats[0]));
  964. if(base_mats==NULL)return TH_EFAULT;
  965. for(bmi=0;bmi<nbase_mats;bmi++){
  966. for(ci=0;ci<64;ci++){
  967. val=oc_pack_read(_opb,8);
  968. base_mats[bmi][ci]=(unsigned char)val;
  969. }
  970. }
  971. nbits=oc_ilog(nbase_mats-1);
  972. for(i=0;i<6;i++){
  973. th_quant_ranges *qranges;
  974. th_quant_base *qrbms;
  975. int32_t *qrsizes;
  976. qti=i/3;
  977. pli=i%3;
  978. qranges=_qinfo->qi_ranges[qti]+pli;
  979. if(i>0){
  980. val=oc_pack_read1(_opb);
  981. if(!val){
  982. int32_t qtj;
  983. int32_t plj;
  984. if(qti>0){
  985. val=oc_pack_read1(_opb);
  986. if(val){
  987. qtj=qti-1;
  988. plj=pli;
  989. }
  990. else{
  991. qtj=(i-1)/3;
  992. plj=(i-1)%3;
  993. }
  994. }
  995. else{
  996. qtj=(i-1)/3;
  997. plj=(i-1)%3;
  998. }
  999. *qranges=*(_qinfo->qi_ranges[qtj]+plj);
  1000. continue;
  1001. }
  1002. }
  1003. val=oc_pack_read(_opb,nbits);
  1004. indices[0]=(int32_t)val;
  1005. for(qi=qri=0;qi<63;){
  1006. val=oc_pack_read(_opb,oc_ilog(62-qi));
  1007. sizes[qri]=(int32_t)val+1;
  1008. qi+=(int32_t)val+1;
  1009. val=oc_pack_read(_opb,nbits);
  1010. indices[++qri]=(int32_t)val;
  1011. }
  1012. /*Note: The caller is responsible for cleaning up any partially
  1013. constructed qinfo.*/
  1014. if(qi>63){
  1015. free(base_mats);
  1016. return TH_EBADHEADER;
  1017. }
  1018. qranges->nranges=qri;
  1019. qranges->sizes=qrsizes=(int32_t *)malloc(qri*sizeof(qrsizes[0]));
  1020. if(qranges->sizes==NULL){
  1021. /*Note: The caller is responsible for cleaning up any partially
  1022. constructed qinfo.*/
  1023. free(base_mats);
  1024. return TH_EFAULT;
  1025. }
  1026. memcpy(qrsizes,sizes,qri*sizeof(qrsizes[0]));
  1027. qrbms=(th_quant_base *)malloc((qri+1)*sizeof(qrbms[0]));
  1028. if(qrbms==NULL){
  1029. /*Note: The caller is responsible for cleaning up any partially
  1030. constructed qinfo.*/
  1031. free(base_mats);
  1032. return TH_EFAULT;
  1033. }
  1034. qranges->base_matrices=(const th_quant_base *)qrbms;
  1035. do{
  1036. bmi=indices[qri];
  1037. /*Note: The caller is responsible for cleaning up any partially
  1038. constructed qinfo.*/
  1039. if(bmi>=nbase_mats){
  1040. free(base_mats);
  1041. return TH_EBADHEADER;
  1042. }
  1043. memcpy(qrbms[qri],base_mats[bmi],sizeof(qrbms[qri]));
  1044. }
  1045. while(qri-->0);
  1046. }
  1047. free(base_mats);
  1048. return 0;
  1049. }
  1050. void oc_quant_params_clear(th_quant_info *_qinfo)
  1051. {
  1052. int32_t i;
  1053. for(i=6;i-->0;){
  1054. int32_t qti;
  1055. int32_t pli;
  1056. qti=i/3;
  1057. pli=i%3;
  1058. /*Clear any duplicate pointer references.*/
  1059. if(i>0){
  1060. int32_t qtj;
  1061. int32_t plj;
  1062. qtj=(i-1)/3;
  1063. plj=(i-1)%3;
  1064. if(_qinfo->qi_ranges[qti][pli].sizes==
  1065. _qinfo->qi_ranges[qtj][plj].sizes){
  1066. _qinfo->qi_ranges[qti][pli].sizes=NULL;
  1067. }
  1068. if(_qinfo->qi_ranges[qti][pli].base_matrices==
  1069. _qinfo->qi_ranges[qtj][plj].base_matrices){
  1070. _qinfo->qi_ranges[qti][pli].base_matrices=NULL;
  1071. }
  1072. }
  1073. if(qti>0){
  1074. if(_qinfo->qi_ranges[1][pli].sizes==
  1075. _qinfo->qi_ranges[0][pli].sizes){
  1076. _qinfo->qi_ranges[1][pli].sizes=NULL;
  1077. }
  1078. if(_qinfo->qi_ranges[1][pli].base_matrices==
  1079. _qinfo->qi_ranges[0][pli].base_matrices){
  1080. _qinfo->qi_ranges[1][pli].base_matrices=NULL;
  1081. }
  1082. }
  1083. /*Now free all the non-duplicate storage.*/
  1084. free((void *)_qinfo->qi_ranges[qti][pli].sizes);
  1085. free((void *)_qinfo->qi_ranges[qti][pli].base_matrices);
  1086. }
  1087. }