1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147 |
- // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
- /********************************************************************
- Copyright (C) 2002-2009 Xiph.org Foundation
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- - Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- - Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- - Neither the name of the Xiph.org Foundation nor the names of its
- contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ********************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "bitpack.h"
- #include "internal.h"
- #include "decint.h"
- /*The maximum output of the DCT with +/- 255 inputs is +/- 8157.
- These minimum quantizers ensure the result after quantization (and after
- prediction for DC) will be no more than +/- 510.
- The tokenization system can handle values up to +/- 580, so there is no need
- to do any coefficient clamping.
- I would rather have allowed smaller quantizers and had to clamp, but these
- minimums were required when constructing the original VP3 matrices and have
- been formalized in the spec.*/
- static const uint32_t OC_DC_QUANT_MIN[2]={4<<2,8<<2};
- static const uint32_t OC_AC_QUANT_MIN[2]={2<<2,4<<2};
- /*The map from external spec-defined tokens to internal tokens.
- This is constructed so that any extra bits read with the original token value
- can be masked off the least significant bits of its internal token index.
- In addition, all of the tokens which require additional extra bits are placed
- at the start of the list, and grouped by type.
- OC_DCT_REPEAT_RUN3_TOKEN is placed first, as it is an extra-special case, so
- giving it index 0 may simplify comparisons on some architectures.
- These requirements require some substantial reordering.*/
- static const unsigned char OC_DCT_TOKEN_MAP[TH_NDCT_TOKENS]=
- {
- /*OC_DCT_EOB1_TOKEN (0 extra bits)*/
- 15,
- /*OC_DCT_EOB2_TOKEN (0 extra bits)*/
- 16,
- /*OC_DCT_EOB3_TOKEN (0 extra bits)*/
- 17,
- /*OC_DCT_REPEAT_RUN0_TOKEN (2 extra bits)*/
- 88,
- /*OC_DCT_REPEAT_RUN1_TOKEN (3 extra bits)*/
- 80,
- /*OC_DCT_REPEAT_RUN2_TOKEN (4 extra bits)*/
- 1,
- /*OC_DCT_REPEAT_RUN3_TOKEN (12 extra bits)*/
- 0,
- /*OC_DCT_SHORT_ZRL_TOKEN (3 extra bits)*/
- 48,
- /*OC_DCT_ZRL_TOKEN (6 extra bits)*/
- 14,
- /*OC_ONE_TOKEN (0 extra bits)*/
- 56,
- /*OC_MINUS_ONE_TOKEN (0 extra bits)*/
- 57,
- /*OC_TWO_TOKEN (0 extra bits)*/
- 58,
- /*OC_MINUS_TWO_TOKEN (0 extra bits)*/
- 59,
- /*OC_DCT_VAL_CAT2 (1 extra bit)*/
- 60,
- 62,
- 64,
- 66,
- /*OC_DCT_VAL_CAT3 (2 extra bits)*/
- 68,
- /*OC_DCT_VAL_CAT4 (3 extra bits)*/
- 72,
- /*OC_DCT_VAL_CAT5 (4 extra bits)*/
- 2,
- /*OC_DCT_VAL_CAT6 (5 extra bits)*/
- 4,
- /*OC_DCT_VAL_CAT7 (6 extra bits)*/
- 6,
- /*OC_DCT_VAL_CAT8 (10 extra bits)*/
- 8,
- /*OC_DCT_RUN_CAT1A (1 extra bit)*/
- 18,
- 20,
- 22,
- 24,
- 26,
- /*OC_DCT_RUN_CAT1B (3 extra bits)*/
- 32,
- /*OC_DCT_RUN_CAT1C (4 extra bits)*/
- 12,
- /*OC_DCT_RUN_CAT2A (2 extra bits)*/
- 28,
- /*OC_DCT_RUN_CAT2B (3 extra bits)*/
- 40
- };
- /*The log base 2 of number of internal tokens associated with each of the spec
- tokens (i.e., how many of the extra bits are folded into the token value).
- Increasing the maximum value beyond 3 will enlarge the amount of stack
- required for tree construction.*/
- static const unsigned char OC_DCT_TOKEN_MAP_LOG_NENTRIES[TH_NDCT_TOKENS]={
- 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
- };
- #define OC_HUFF_SLUSH (2)
- #define OC_ROOT_HUFF_SLUSH (7)
- /*Unpacks a Huffman codebook.
- _opb: The buffer to unpack from.
- _tokens: Stores a list of internal tokens, in the order they were found in
- the codebook, and the lengths of their corresponding codewords.
- This is enough to completely define the codebook, while minimizing
- stack usage and avoiding temporary allocations (for platforms
- where free() is a no-op).
- Return: The number of internal tokens in the codebook, or a negative value
- on error.*/
- int32_t oc_huff_tree_unpack(oc_pack_buf *_opb,unsigned char _tokens[256][2])
- {
- uint32_t code;
- int32_t len;
- int32_t ntokens;
- int32_t nleaves;
- code=0;
- len=ntokens=nleaves=0;
- for(;;){
- long bits;
- bits=oc_pack_read1(_opb);
- /*Only process nodes so long as there's more bits in the buffer.*/
- if(oc_pack_bytes_left(_opb)<0) return TH_EBADHEADER;
- /*Read an internal node:*/
- if(!bits){
- len++;
- /*Don't allow codewords longer than 32 bits.*/
- if(len>32)return TH_EBADHEADER;
- }
- /*Read a leaf node:*/
- else{
- uint32_t code_bit;
- int32_t neb;
- int32_t nentries;
- int32_t token;
- /*Don't allow more than 32 spec-tokens per codebook.*/
- if(++nleaves>32)return TH_EBADHEADER;
- bits=oc_pack_read(_opb,OC_NDCT_TOKEN_BITS);
- neb=OC_DCT_TOKEN_MAP_LOG_NENTRIES[bits];
- token=OC_DCT_TOKEN_MAP[bits];
- nentries=1<<neb;
- while(nentries-->0){
- _tokens[ntokens][0]=(unsigned char)token++;
- _tokens[ntokens][1]=(unsigned char)(len+neb);
- ntokens++;
- }
- code_bit=0x80000000U>>len-1;
- while(len>0&&(code&code_bit)){
- code^=code_bit;
- code_bit<<=1;
- len--;
- }
- if(len<=0)break;
- code|=code_bit;
- }
- }
- return ntokens;
- }
- /*Count how many tokens would be required to fill a subtree at depth _depth.
- _tokens: A list of internal tokens, in the order they are found in the
- codebook, and the lengths of their corresponding codewords.
- _depth: The depth of the desired node in the corresponding tree structure.
- Return: The number of tokens that belong to that subtree.*/
- static int32_t oc_huff_subtree_tokens(unsigned char _tokens[][2],int32_t _depth)
- {
- uint32_t code;
- int32_t ti;
- code=0;
- ti=0;
- do{
- if(_tokens[ti][1]-_depth<32)code+=0x80000000U>>_tokens[ti++][1]-_depth;
- else{
- /*Because of the expanded internal tokens, we can have codewords as long
- as 35 bits.
- A single recursion here is enough to advance past them.*/
- code++;
- ti+=oc_huff_subtree_tokens(_tokens+ti,_depth+31);
- }
- }
- while(code<0x80000000U);
- return ti;
- }
- /*Compute the number of bits to use for a collapsed tree node at the given
- depth.
- _tokens: A list of internal tokens, in the order they are found in the
- codebook, and the lengths of their corresponding codewords.
- _ntokens: The number of tokens corresponding to this tree node.
- _depth: The depth of this tree node.
- Return: The number of bits to use for a collapsed tree node rooted here.
- This is always at least one, even if this was a leaf node.*/
- static int32_t oc_huff_tree_collapse_depth(unsigned char _tokens[][2],
- int32_t _ntokens,int32_t _depth)
- {
- int32_t got_leaves;
- int32_t loccupancy;
- int32_t occupancy;
- int32_t slush;
- int32_t nbits;
- int32_t best_nbits;
- slush=_depth>0?OC_HUFF_SLUSH:OC_ROOT_HUFF_SLUSH;
- /*It's legal to have a tree with just a single node, which requires no bits
- to decode and always returns the same token.
- However, no encoder actually does this (yet).
- To avoid a special case in oc_huff_token_decode(), we force the number of
- lookahead bits to be at least one.
- This will produce a tree that looks ahead one bit and then advances the
- stream zero bits.*/
- nbits=1;
- occupancy=2;
- got_leaves=1;
- do{
- int32_t ti;
- if(got_leaves)best_nbits=nbits;
- nbits++;
- got_leaves=0;
- loccupancy=occupancy;
- for(occupancy=ti=0;ti<_ntokens;occupancy++){
- if(_tokens[ti][1]<_depth+nbits)ti++;
- else if(_tokens[ti][1]==_depth+nbits){
- got_leaves=1;
- ti++;
- }
- else ti+=oc_huff_subtree_tokens(_tokens+ti,_depth+nbits);
- }
- }
- while(occupancy>loccupancy&&occupancy*slush>=1<<nbits);
- return best_nbits;
- }
- /*Determines the size in words of a Huffman tree node that represents a
- subtree of depth _nbits.
- _nbits: The depth of the subtree.
- This must be greater than zero.
- Return: The number of words required to store the node.*/
- static size_t oc_huff_node_size(int32_t _nbits){
- return 1+(1<<_nbits);
- }
- /*Produces a collapsed-tree representation of the given token list.
- _tree: The storage for the collapsed Huffman tree.
- This may be NULL to compute the required storage size instead of
- constructing the tree.
- _tokens: A list of internal tokens, in the order they are found in the
- codebook, and the lengths of their corresponding codewords.
- _ntokens: The number of tokens corresponding to this tree node.
- Return: The number of words required to store the tree.*/
- static size_t oc_huff_tree_collapse(int16_t *_tree,
- unsigned char _tokens[][2],int32_t _ntokens)
- {
- int16_t node[34];
- unsigned char depth[34];
- unsigned char last[34];
- size_t ntree;
- int32_t ti;
- int32_t l;
- depth[0]=0;
- last[0]=(unsigned char)(_ntokens-1);
- ntree=0;
- ti=0;
- l=0;
- do{
- int32_t nbits;
- nbits=oc_huff_tree_collapse_depth(_tokens+ti,last[l]+1-ti,depth[l]);
- node[l]=(int16_t)ntree;
- ntree+=oc_huff_node_size(nbits);
- if(_tree!=NULL)_tree[node[l]++]=(int16_t)nbits;
- do{
- while(ti<=last[l]&&_tokens[ti][1]<=depth[l]+nbits){
- if(_tree!=NULL){
- int16_t leaf;
- int32_t nentries;
- nentries=1<<depth[l]+nbits-_tokens[ti][1];
- leaf=(int16_t)-(_tokens[ti][1]-depth[l]<<8|_tokens[ti][0]);
- while(nentries-->0)_tree[node[l]++]=leaf;
- }
- ti++;
- }
- if(ti<=last[l]){
- /*We need to recurse*/
- depth[l+1]=(unsigned char)(depth[l]+nbits);
- if(_tree!=NULL)_tree[node[l]++]=(int16_t)ntree;
- l++;
- last[l]=
- (unsigned char)(ti+oc_huff_subtree_tokens(_tokens+ti,depth[l])-1);
- break;
- }
- /*Pop back up a level of recursion.*/
- else if(l-->0)nbits=depth[l+1]-depth[l];
- }
- while(l>=0);
- }
- while(l>=0);
- return ntree;
- }
- /*Unpacks a set of Huffman trees, and reduces them to a collapsed
- representation.
- _opb: The buffer to unpack the trees from.
- _nodes: The table to fill with the Huffman trees.
- Return: 0 on success, or a negative value on error.*/
- int32_t oc_huff_trees_unpack(oc_pack_buf *_opb,
- int16_t *_nodes[TH_NHUFFMAN_TABLES])
- {
- int32_t ret;
- int32_t i;
- ret=0;
- for(i=0;i<TH_NHUFFMAN_TABLES;i++){
- unsigned char tokens[256][2];
- int32_t ntokens;
- int16_t *tree;
- size_t size;
- /*Unpack the full tree into a temporary buffer.*/
- ntokens=oc_huff_tree_unpack(_opb,tokens);
- if(ntokens<0){
- ret=ntokens;
- break;
- }
- /*Figure out how big the collapsed tree will be and allocate space for it.*/
- size=oc_huff_tree_collapse(NULL,tokens,ntokens);
- if(size>32767){
- /*This should never happen; if it does it means you set OC_HUFF_SLUSH or
- OC_ROOT_HUFF_SLUSH too large.*/
- ret=TH_EIMPL;
- break;
- }
- tree=(int16_t *)malloc(size*sizeof(*tree));
- if(tree==NULL){
- ret=TH_EFAULT;
- break;
- }
- /*Construct the collapsed the tree.*/
- oc_huff_tree_collapse(tree,tokens,ntokens);
- _nodes[i]=tree;
- }
- if(ret<0)while(i-->0)free(_nodes[i]);
- return ret;
- }
- /*Determines the size in words of a Huffman subtree.
- _tree: The complete Huffman tree.
- _node: The index of the root of the desired subtree.
- Return: The number of words required to store the tree.*/
- static size_t oc_huff_tree_size(const int16_t *_tree,int32_t _node)
- {
- size_t size;
- int32_t nchildren;
- int32_t n;
- int32_t i;
- n=_tree[_node];
- size=oc_huff_node_size(n);
- nchildren=1<<n;
- i=0;
- do{
- int32_t child;
- child=_tree[_node+i+1];
- if(child<=0)i+=1<<n-(-child>>8);
- else{
- size+=oc_huff_tree_size(_tree,child);
- i++;
- }
- }
- while(i<nchildren);
- return size;
- }
- /*Makes a copy of the given set of Huffman trees.
- _dst: The array to store the copy in.
- _src: The array of trees to copy.*/
- int32_t oc_huff_trees_copy(int16_t *_dst[TH_NHUFFMAN_TABLES],
- const int16_t *const _src[TH_NHUFFMAN_TABLES])
- {
- int32_t total;
- int32_t i;
- total=0;
- for(i=0;i<TH_NHUFFMAN_TABLES;i++){
- size_t size;
- size=oc_huff_tree_size(_src[i],0);
- total+=size;
- _dst[i]=(int16_t *)malloc(size*sizeof(*_dst[i]));
- if(_dst[i]==NULL){
- while(i-->0)free(_dst[i]);
- return TH_EFAULT;
- }
- memcpy(_dst[i],_src[i],size*sizeof(*_dst[i]));
- }
- return 0;
- }
- /*Frees the memory used by a set of Huffman trees.
- _nodes: The array of trees to free.*/
- void oc_huff_trees_clear(int16_t *_nodes[TH_NHUFFMAN_TABLES]){
- int32_t i;
- for(i=0;i<TH_NHUFFMAN_TABLES;i++)free(_nodes[i]);
- }
- /*Unpacks a single token using the given Huffman tree.
- _opb: The buffer to unpack the token from.
- _node: The tree to unpack the token with.
- Return: The token value.*/
- int32_t oc_huff_token_decode_c(oc_pack_buf *_opb,const int16_t *_tree)
- {
- const unsigned char *ptr;
- const unsigned char *stop;
- oc_pb_window window;
- int32_t available;
- long bits;
- int32_t node;
- int32_t n;
- ptr=_opb->ptr;
- window=_opb->window;
- stop=_opb->stop;
- available=_opb->bits;
- node=0;
- for(;;){
- n=_tree[node];
- if(n>available){
- uint32_t shift;
- shift=OC_PB_WINDOW_SIZE-available;
- do{
- /*We don't bother setting eof because we won't check for it after we've
- started decoding DCT tokens.*/
- if(ptr>=stop){
- shift=-OC_LOTS_OF_BITS;
- break;
- }
- shift-=8;
- window|=(oc_pb_window)*ptr++<<shift;
- }
- while(shift>=8);
- /*Note: We never request more than 24 bits, so there's no need to fill in
- the last partial byte here.*/
- available=OC_PB_WINDOW_SIZE-shift;
- }
- bits=window>>OC_PB_WINDOW_SIZE-n;
- node=_tree[node+1+bits];
- if(node<=0)break;
- window<<=n;
- available-=n;
- }
- node=-node;
- n=node>>8;
- window<<=n;
- available-=n;
- _opb->ptr=ptr;
- _opb->window=window;
- _opb->bits=available;
- return node&255;
- }
- void oc_pack_readinit(oc_pack_buf *_b,unsigned char *_buf,long _bytes)
- {
- memset(_b,0,sizeof(*_b));
- _b->ptr=_buf;
- _b->stop=_buf+_bytes;
- }
- static oc_pb_window oc_pack_refill(oc_pack_buf *_b,int32_t _bits)
- {
- const unsigned char *ptr;
- const unsigned char *stop;
- oc_pb_window window;
- int32_t available;
- uint32_t shift;
- stop=_b->stop;
- ptr=_b->ptr;
- window=_b->window;
- available=_b->bits;
- shift=OC_PB_WINDOW_SIZE-available;
- while(7<shift&&ptr<stop){
- shift-=8;
- window|=(oc_pb_window)*ptr++<<shift;
- }
- _b->ptr=ptr;
- available=OC_PB_WINDOW_SIZE-shift;
- if(_bits>available){
- if(ptr>=stop){
- _b->eof=1;
- available=OC_LOTS_OF_BITS;
- }
- else window|=*ptr>>(available&7);
- }
- _b->bits=available;
- return window;
- }
- int32_t oc_pack_look1(oc_pack_buf *_b)
- {
- oc_pb_window window;
- int32_t available;
- window=_b->window;
- available=_b->bits;
- if(available<1)_b->window=window=oc_pack_refill(_b,1);
- return window>>OC_PB_WINDOW_SIZE-1;
- }
- void oc_pack_adv1(oc_pack_buf *_b)
- {
- _b->window<<=1;
- _b->bits--;
- }
- /*Here we assume that 0<=_bits&&_bits<=32.*/
- long oc_pack_read_c(oc_pack_buf *_b,int32_t _bits)
- {
- oc_pb_window window;
- int32_t available;
- long result;
- window=_b->window;
- available=_b->bits;
- if(_bits==0)return 0;
- if(available<_bits){
- window=oc_pack_refill(_b,_bits);
- available=_b->bits;
- }
- result=window>>OC_PB_WINDOW_SIZE-_bits;
- available-=_bits;
- window<<=1;
- window<<=_bits-1;
- _b->window=window;
- _b->bits=available;
- return result;
- }
- int32_t oc_pack_read1_c(oc_pack_buf *_b)
- {
- oc_pb_window window;
- int32_t available;
- int32_t result;
- window=_b->window;
- available=_b->bits;
- if(available<1){
- window=oc_pack_refill(_b,1);
- available=_b->bits;
- }
- result=window>>OC_PB_WINDOW_SIZE-1;
- available--;
- window<<=1;
- _b->window=window;
- _b->bits=available;
- return result;
- }
- long oc_pack_bytes_left(oc_pack_buf *_b)
- {
- if(_b->eof)return -1;
- return _b->stop-_b->ptr+(_b->bits>>3);
- }
- /*Unpacks a series of octets from a given byte array into the pack buffer.
- No checking is done to ensure the buffer contains enough data.
- _opb: The pack buffer to read the octets from.
- _buf: The byte array to store the unpacked bytes in.
- _len: The number of octets to unpack.*/
- static void oc_unpack_octets(oc_pack_buf *_opb,char *_buf,size_t _len)
- {
- while(_len-->0){
- long val;
- val=oc_pack_read(_opb,8);
- *_buf++=(char)val;
- }
- }
- /*Unpacks a 32-bit integer encoded by octets in little-endian form.*/
- static long oc_unpack_length(oc_pack_buf *_opb)
- {
- long ret[4];
- int32_t i;
- for(i=0;i<4;i++)ret[i]=oc_pack_read(_opb,8);
- return ret[0]|ret[1]<<8|ret[2]<<16|ret[3]<<24;
- }
- static int32_t oc_info_unpack(oc_pack_buf *_opb,th_info *_info)
- {
- long val;
- /*Check the codec bitstream version.*/
- val=oc_pack_read(_opb,8);
- _info->version_major=(unsigned char)val;
- val=oc_pack_read(_opb,8);
- _info->version_minor=(unsigned char)val;
- val=oc_pack_read(_opb,8);
- _info->version_subminor=(unsigned char)val;
- /*verify we can parse this bitstream version.
- We accept earlier minors and all subminors, by spec*/
- if(_info->version_major>TH_VERSION_MAJOR||
- _info->version_major==TH_VERSION_MAJOR&&
- _info->version_minor>TH_VERSION_MINOR){
- return TH_EVERSION;
- }
- /*Read the encoded frame description.*/
- val=oc_pack_read(_opb,16);
- _info->frame_width=(uint32_t)val<<4;
- val=oc_pack_read(_opb,16);
- _info->frame_height=(uint32_t)val<<4;
- val=oc_pack_read(_opb,24);
- _info->pic_width=(uint32_t)val;
- val=oc_pack_read(_opb,24);
- _info->pic_height=(uint32_t)val;
- val=oc_pack_read(_opb,8);
- _info->pic_x=(uint32_t)val;
- val=oc_pack_read(_opb,8);
- _info->pic_y=(uint32_t)val;
- val=oc_pack_read(_opb,32);
- _info->fps_numerator=(uint32_t)val;
- val=oc_pack_read(_opb,32);
- _info->fps_denominator=(uint32_t)val;
- if(_info->frame_width==0||_info->frame_height==0||
- _info->pic_width+_info->pic_x>_info->frame_width||
- _info->pic_height+_info->pic_y>_info->frame_height||
- _info->fps_numerator==0||_info->fps_denominator==0){
- return TH_EBADHEADER;
- }
- /*Note: The sense of pic_y is inverted in what we pass back to the
- application compared to how it is stored in the bitstream.
- This is because the bitstream uses a right-handed coordinate system, while
- applications expect a left-handed one.*/
- _info->pic_y=_info->frame_height-_info->pic_height-_info->pic_y;
- val=oc_pack_read(_opb,24);
- _info->aspect_numerator=(uint32_t)val;
- val=oc_pack_read(_opb,24);
- _info->aspect_denominator=(uint32_t)val;
- val=oc_pack_read(_opb,8);
- _info->colorspace=(th_colorspace)val;
- val=oc_pack_read(_opb,24);
- _info->target_bitrate=(int32_t)val;
- val=oc_pack_read(_opb,6);
- _info->quality=(int32_t)val;
- val=oc_pack_read(_opb,5);
- _info->keyframe_granule_shift=(int32_t)val;
- val=oc_pack_read(_opb,2);
- _info->pixel_fmt=(th_pixel_fmt)val;
- if(_info->pixel_fmt==TH_PF_RSVD)return TH_EBADHEADER;
- val=oc_pack_read(_opb,3);
- if(val!=0||oc_pack_bytes_left(_opb)<0)return TH_EBADHEADER;
- return 0;
- }
- static int32_t oc_comment_unpack(oc_pack_buf *_opb,th_comment *_tc)
- {
- long len;
- int32_t i;
- /*Read the vendor string.*/
- len=oc_unpack_length(_opb);
- if(len<0||len>oc_pack_bytes_left(_opb))return TH_EBADHEADER;
- _tc->vendor=malloc((size_t)len+1);
- if(_tc->vendor==NULL)return TH_EFAULT;
- oc_unpack_octets(_opb,_tc->vendor,len);
- _tc->vendor[len]='\0';
- /*Read the user comments.*/
- _tc->comments=(int32_t)oc_unpack_length(_opb);
- len=_tc->comments;
- if(len<0||len>(LONG_MAX>>2)||len<<2>oc_pack_bytes_left(_opb)){
- _tc->comments=0;
- return TH_EBADHEADER;
- }
- _tc->comment_lengths=(int32_t *)malloc(
- _tc->comments*sizeof(_tc->comment_lengths[0]));
- _tc->user_comments=(char **)malloc(
- _tc->comments*sizeof(_tc->user_comments[0]));
- if(_tc->comment_lengths==NULL||_tc->user_comments==NULL){
- _tc->comments=0;
- return TH_EFAULT;
- }
- for(i=0;i<_tc->comments;i++){
- len=oc_unpack_length(_opb);
- if(len<0||len>oc_pack_bytes_left(_opb)){
- _tc->comments=i;
- return TH_EBADHEADER;
- }
- _tc->comment_lengths[i]=len;
- _tc->user_comments[i]=malloc((size_t)len+1);
- if(_tc->user_comments[i]==NULL){
- _tc->comments=i;
- return TH_EFAULT;
- }
- oc_unpack_octets(_opb,_tc->user_comments[i],len);
- _tc->user_comments[i][len]='\0';
- }
- return oc_pack_bytes_left(_opb)<0?TH_EBADHEADER:0;
- }
- static int32_t oc_setup_unpack(oc_pack_buf *_opb,th_setup_info *_setup)
- {
- int32_t ret;
- /*Read the quantizer tables.*/
- ret=oc_quant_params_unpack(_opb,&_setup->qinfo);
- if(ret<0)return ret;
- /*Read the Huffman trees.*/
- return oc_huff_trees_unpack(_opb,_setup->huff_tables);
- }
- static void oc_setup_clear(th_setup_info *_setup){
- oc_quant_params_clear(&_setup->qinfo);
- oc_huff_trees_clear(_setup->huff_tables);
- }
- static int32_t split_headers(uint8_t *packet, int32_t packet_size,
- int32_t first_header_size, uint8_t *header_start[3],
- int32_t header_len[3])
- {
- int32_t i;
- if (packet_size >= 6 && (((((const uint8_t*)(packet))[0] << 8) | ((const uint8_t*)(packet))[1]) == first_header_size))
- {
- int32_t overall_len = 6;
- for (i=0; i<3; i++)
- {
- header_len[i] = ((((const uint8_t*)(packet))[0] << 8) | ((const uint8_t*)(packet))[1]);
- packet += 2;
- header_start[i] = packet;
- packet += header_len[i];
- if (overall_len > packet_size - header_len[i])
- return -1;
- overall_len += header_len[i];
- }
- }
- else if (packet_size >= 3 && packet_size < INT_MAX - 0x1ff && packet[0] == 2)
- {
- int32_t overall_len = 3;
- packet++;
- for (i=0; i<2; i++, packet++)
- {
- header_len[i] = 0;
- for (; overall_len < packet_size && *packet==0xff; packet++)
- {
- header_len[i] += 0xff;
- overall_len += 0xff + 1;
- }
- header_len[i] += *packet;
- overall_len += *packet;
- if (overall_len > packet_size)
- return -1;
- }
- header_len[2] = packet_size - overall_len;
- header_start[0] = packet;
- header_start[1] = header_start[0] + header_len[0];
- header_start[2] = header_start[1] + header_len[1];
- }
- else
- {
- return -1;
- }
- return 0;
- }
- static int32_t oc_dec_headerin(oc_pack_buf *_opb,th_info *_info,
- th_comment *_tc,th_setup_info **_setup)
- {
- char buffer[6];
- long val;
- int32_t packtype;
- int32_t ret;
- val=oc_pack_read(_opb,8);
- packtype=(int32_t)val;
- /*If we're at a data packet and we have received all three headers, we're
- done.*/
- if(!(packtype&0x80)&&_info->frame_width>0&&_tc->vendor!=NULL&&*_setup!=NULL){
- return 0;
- }
- /*Check the codec string.*/
- oc_unpack_octets(_opb,buffer,6);
- if(memcmp(buffer,"theora",6)!=0)return TH_ENOTFORMAT;
- switch(packtype)
- {
- /*Codec info header.*/
- case 0x80:{
- /*This should be the first packet, and we should not already be
- initialized.*/
- ret=oc_info_unpack(_opb,_info);
- if(ret<0)th_info_clear(_info);
- else ret=3;
- }break;
- /*Comment header.*/
- case 0x81:{
- if(_tc==NULL)return TH_EFAULT;
- /*We shoud have already decoded the info header, and should not yet have
- decoded the comment header.*/
- ret=oc_comment_unpack(_opb,_tc);
- if(ret<0)th_comment_clear(_tc);
- else ret=2;
- }break;
- /*Codec setup header.*/
- case 0x82:{
- oc_setup_info *setup;
- setup=(oc_setup_info *)calloc(1,sizeof(*setup));
- if(setup==NULL)return TH_EFAULT;
- ret=oc_setup_unpack(_opb,setup);
- if(ret<0){
- oc_setup_clear(setup);
- free(setup);
- }
- else{
- *_setup=setup;
- ret=1;
- }
- }break;
- default:{
- /*We don't know what this header is.*/
- return TH_EBADHEADER;
- }break;
- }
- return ret;
- }
- /*Decodes one header packet.
- This should be called repeatedly with the packets at the beginning of the
- stream until it returns 0.*/
- int32_t th_decode_headerin(th_info *_info,th_comment *_tc, th_setup_info **_setup,
- unsigned char *header, int32_t header_size)
- {
- int32_t i;
- int32_t header_len[3];
- uint8_t *header_start[3];
- oc_pack_buf opb;
- if(header==NULL)return TH_EBADHEADER;
- if(_info==NULL)return TH_EFAULT;
- if (split_headers(header, header_size, 42,
- header_start, header_len) < 0)
- {
- fprintf(stderr, "[Error] Corrupt extradata\n");
- return -1;
- }
- for (i = 0; i < 3; i++)
- {
- oc_pack_readinit(&opb, header_start[i], header_len[i]);
- oc_dec_headerin(&opb,_info,_tc,_setup);
- // conform!!
- //if (s->theora < 0x030200)
- // break;
- }
- return 0;
- }
- void th_setupsys_mem_free(th_setup_info *_setup)
- {
- if(_setup!=NULL){
- oc_setup_clear(_setup);
- free(_setup);
- }
- }
- /*Initializes the dequantization tables from a set of quantizer info.
- Currently the dequantizer (and elsewhere enquantizer) tables are expected to
- be initialized as pointing to the storage reserved for them in the
- oc_theora_state (resp. oc_enc_ctx) structure.
- If some tables are duplicates of others, the pointers will be adjusted to
- point to a single copy of the tables, but the storage for them will not be
- freed.
- If you're concerned about the memory footprint, the obvious thing to do is
- to move the storage out of its fixed place in the structures and allocate
- it on demand.
- However, a much, much better option is to only store the quantization
- matrices being used for the current frame, and to recalculate these as the
- qi values change between frames (this is what VP3 did).*/
- void oc_dequant_tables_init(uint16_t *_dequant[64][3][2],
- int32_t _pp_dc_scale[64],const th_quant_info *_qinfo)
- {
- /*Coding mode: intra or inter.*/
- int32_t qti;
- /*Y', C_b, C_r*/
- int32_t pli;
- for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++)
- {
- /*Quality index.*/
- int32_t qi;
- /*Range iterator.*/
- int32_t qri;
- for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){
- th_quant_base base;
- uint32_t q;
- int32_t qi_start;
- int32_t qi_end;
- memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri],
- sizeof(base));
- qi_start=qi;
- if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1;
- else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri];
- /*Iterate over quality indicies in this range.*/
- for(;;){
- uint32_t qfac;
- int32_t zzi;
- int32_t ci;
- /*In the original VP3.2 code, the rounding offset and the size of the
- dead zone around 0 were controlled by a "sharpness" parameter.
- The size of our dead zone is now controlled by the per-coefficient
- quality thresholds returned by our HVS module.
- We round down from a more accurate value when the quality of the
- reconstruction does not fall below our threshold and it saves bits.
- Hence, all of that VP3.2 code is gone from here, and the remaining
- floating point code has been implemented as equivalent integer code
- with exact precision.*/
- qfac=(uint32_t)_qinfo->dc_scale[qi]*base[0];
- /*For postprocessing, not dequantization.*/
- if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int32_t)(qfac/160);
- /*Scale DC the coefficient from the proper table.*/
- q=(qfac/100)<<2;
- q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX);
- _dequant[qi][pli][qti][0]=(uint16_t)q;
- /*Now scale AC coefficients from the proper table.*/
- for(zzi=1;zzi<64;zzi++){
- q=((uint32_t)_qinfo->ac_scale[qi]*base[zzi]/100)<<2;
- q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX);
- _dequant[qi][pli][qti][zzi]=(uint16_t)q;
- }
- /*If this is a duplicate of a previous matrix, use that instead.
- This simple check helps us improve cache coherency later.*/
- {
- int32_t dupe;
- int32_t qtj;
- int32_t plj;
- dupe=0;
- for(qtj=0;qtj<=qti;qtj++){
- for(plj=0;plj<(qtj<qti?3:pli);plj++){
- if(!memcmp(_dequant[qi][pli][qti],_dequant[qi][plj][qtj],
- sizeof(oc_quant_table))){
- dupe=1;
- break;
- }
- }
- if(dupe)break;
- }
- if(dupe)_dequant[qi][pli][qti]=_dequant[qi][plj][qtj];
- }
- if(++qi>=qi_end)break;
- /*Interpolate the next base matrix.*/
- for(ci=0;ci<64;ci++){
- base[ci]=(unsigned char)(
- (2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+
- (qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci])
- +_qinfo->qi_ranges[qti][pli].sizes[qri])/
- (2*_qinfo->qi_ranges[qti][pli].sizes[qri]));
- }
- }
- }
- }
- }
- int32_t oc_quant_params_unpack(oc_pack_buf *_opb,th_quant_info *_qinfo)
- {
- th_quant_base *base_mats;
- long val;
- int32_t nbase_mats;
- int32_t sizes[64];
- int32_t indices[64];
- int32_t nbits;
- int32_t bmi;
- int32_t ci;
- int32_t qti;
- int32_t pli;
- int32_t qri;
- int32_t qi;
- int32_t i;
- val=oc_pack_read(_opb,3);
- nbits=(int32_t)val;
- for(qi=0;qi<64;qi++){
- val=oc_pack_read(_opb,nbits);
- _qinfo->loop_filter_limits[qi]=(unsigned char)val;
- }
- val=oc_pack_read(_opb,4);
- nbits=(int32_t)val+1;
- for(qi=0;qi<64;qi++){
- val=oc_pack_read(_opb,nbits);
- _qinfo->ac_scale[qi]=(uint16_t)val;
- }
- val=oc_pack_read(_opb,4);
- nbits=(int32_t)val+1;
- for(qi=0;qi<64;qi++){
- val=oc_pack_read(_opb,nbits);
- _qinfo->dc_scale[qi]=(uint16_t)val;
- }
- val=oc_pack_read(_opb,9);
- nbase_mats=(int32_t)val+1;
- base_mats=malloc(nbase_mats*sizeof(base_mats[0]));
- if(base_mats==NULL)return TH_EFAULT;
- for(bmi=0;bmi<nbase_mats;bmi++){
- for(ci=0;ci<64;ci++){
- val=oc_pack_read(_opb,8);
- base_mats[bmi][ci]=(unsigned char)val;
- }
- }
- nbits=oc_ilog(nbase_mats-1);
- for(i=0;i<6;i++){
- th_quant_ranges *qranges;
- th_quant_base *qrbms;
- int32_t *qrsizes;
- qti=i/3;
- pli=i%3;
- qranges=_qinfo->qi_ranges[qti]+pli;
- if(i>0){
- val=oc_pack_read1(_opb);
- if(!val){
- int32_t qtj;
- int32_t plj;
- if(qti>0){
- val=oc_pack_read1(_opb);
- if(val){
- qtj=qti-1;
- plj=pli;
- }
- else{
- qtj=(i-1)/3;
- plj=(i-1)%3;
- }
- }
- else{
- qtj=(i-1)/3;
- plj=(i-1)%3;
- }
- *qranges=*(_qinfo->qi_ranges[qtj]+plj);
- continue;
- }
- }
- val=oc_pack_read(_opb,nbits);
- indices[0]=(int32_t)val;
- for(qi=qri=0;qi<63;){
- val=oc_pack_read(_opb,oc_ilog(62-qi));
- sizes[qri]=(int32_t)val+1;
- qi+=(int32_t)val+1;
- val=oc_pack_read(_opb,nbits);
- indices[++qri]=(int32_t)val;
- }
- /*Note: The caller is responsible for cleaning up any partially
- constructed qinfo.*/
- if(qi>63){
- free(base_mats);
- return TH_EBADHEADER;
- }
- qranges->nranges=qri;
- qranges->sizes=qrsizes=(int32_t *)malloc(qri*sizeof(qrsizes[0]));
- if(qranges->sizes==NULL){
- /*Note: The caller is responsible for cleaning up any partially
- constructed qinfo.*/
- free(base_mats);
- return TH_EFAULT;
- }
- memcpy(qrsizes,sizes,qri*sizeof(qrsizes[0]));
- qrbms=(th_quant_base *)malloc((qri+1)*sizeof(qrbms[0]));
- if(qrbms==NULL){
- /*Note: The caller is responsible for cleaning up any partially
- constructed qinfo.*/
- free(base_mats);
- return TH_EFAULT;
- }
- qranges->base_matrices=(const th_quant_base *)qrbms;
- do{
- bmi=indices[qri];
- /*Note: The caller is responsible for cleaning up any partially
- constructed qinfo.*/
- if(bmi>=nbase_mats){
- free(base_mats);
- return TH_EBADHEADER;
- }
- memcpy(qrbms[qri],base_mats[bmi],sizeof(qrbms[qri]));
- }
- while(qri-->0);
- }
- free(base_mats);
- return 0;
- }
- void oc_quant_params_clear(th_quant_info *_qinfo)
- {
- int32_t i;
- for(i=6;i-->0;){
- int32_t qti;
- int32_t pli;
- qti=i/3;
- pli=i%3;
- /*Clear any duplicate pointer references.*/
- if(i>0){
- int32_t qtj;
- int32_t plj;
- qtj=(i-1)/3;
- plj=(i-1)%3;
- if(_qinfo->qi_ranges[qti][pli].sizes==
- _qinfo->qi_ranges[qtj][plj].sizes){
- _qinfo->qi_ranges[qti][pli].sizes=NULL;
- }
- if(_qinfo->qi_ranges[qti][pli].base_matrices==
- _qinfo->qi_ranges[qtj][plj].base_matrices){
- _qinfo->qi_ranges[qti][pli].base_matrices=NULL;
- }
- }
- if(qti>0){
- if(_qinfo->qi_ranges[1][pli].sizes==
- _qinfo->qi_ranges[0][pli].sizes){
- _qinfo->qi_ranges[1][pli].sizes=NULL;
- }
- if(_qinfo->qi_ranges[1][pli].base_matrices==
- _qinfo->qi_ranges[0][pli].base_matrices){
- _qinfo->qi_ranges[1][pli].base_matrices=NULL;
- }
- }
- /*Now free all the non-duplicate storage.*/
- free((void *)_qinfo->qi_ranges[qti][pli].sizes);
- free((void *)_qinfo->qi_ranges[qti][pli].base_matrices);
- }
- }
|