1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libtheora/lib/internal.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,212 @@ 1.4 +/******************************************************************** 1.5 + * * 1.6 + * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * 1.7 + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * 1.8 + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * 1.9 + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * 1.10 + * * 1.11 + * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * 1.12 + * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * 1.13 + * * 1.14 + ******************************************************************** 1.15 + 1.16 + function: 1.17 + last mod: $Id: internal.c 17506 2010-10-13 02:52:41Z tterribe $ 1.18 + 1.19 + ********************************************************************/ 1.20 + 1.21 +#include <stdlib.h> 1.22 +#include <limits.h> 1.23 +#include <string.h> 1.24 +#include "internal.h" 1.25 + 1.26 + 1.27 + 1.28 +/*A map from the index in the zig zag scan to the coefficient number in a 1.29 + block. 1.30 + All zig zag indices beyond 63 are sent to coefficient 64, so that zero runs 1.31 + past the end of a block in bogus streams get mapped to a known location.*/ 1.32 +const unsigned char OC_FZIG_ZAG[128]={ 1.33 + 0, 1, 8,16, 9, 2, 3,10, 1.34 + 17,24,32,25,18,11, 4, 5, 1.35 + 12,19,26,33,40,48,41,34, 1.36 + 27,20,13, 6, 7,14,21,28, 1.37 + 35,42,49,56,57,50,43,36, 1.38 + 29,22,15,23,30,37,44,51, 1.39 + 58,59,52,45,38,31,39,46, 1.40 + 53,60,61,54,47,55,62,63, 1.41 + 64,64,64,64,64,64,64,64, 1.42 + 64,64,64,64,64,64,64,64, 1.43 + 64,64,64,64,64,64,64,64, 1.44 + 64,64,64,64,64,64,64,64, 1.45 + 64,64,64,64,64,64,64,64, 1.46 + 64,64,64,64,64,64,64,64, 1.47 + 64,64,64,64,64,64,64,64, 1.48 + 64,64,64,64,64,64,64,64 1.49 +}; 1.50 + 1.51 +/*A map from the coefficient number in a block to its index in the zig zag 1.52 + scan.*/ 1.53 +const unsigned char OC_IZIG_ZAG[64]={ 1.54 + 0, 1, 5, 6,14,15,27,28, 1.55 + 2, 4, 7,13,16,26,29,42, 1.56 + 3, 8,12,17,25,30,41,43, 1.57 + 9,11,18,24,31,40,44,53, 1.58 + 10,19,23,32,39,45,52,54, 1.59 + 20,22,33,38,46,51,55,60, 1.60 + 21,34,37,47,50,56,59,61, 1.61 + 35,36,48,49,57,58,62,63 1.62 +}; 1.63 + 1.64 +/*A map from physical macro block ordering to bitstream macro block 1.65 + ordering within a super block.*/ 1.66 +const unsigned char OC_MB_MAP[2][2]={{0,3},{1,2}}; 1.67 + 1.68 +/*A list of the indices in the oc_mb.map array that can be valid for each of 1.69 + the various chroma decimation types.*/ 1.70 +const unsigned char OC_MB_MAP_IDXS[TH_PF_NFORMATS][12]={ 1.71 + {0,1,2,3,4,8}, 1.72 + {0,1,2,3,4,5,8,9}, 1.73 + {0,1,2,3,4,6,8,10}, 1.74 + {0,1,2,3,4,5,6,7,8,9,10,11} 1.75 +}; 1.76 + 1.77 +/*The number of indices in the oc_mb.map array that can be valid for each of 1.78 + the various chroma decimation types.*/ 1.79 +const unsigned char OC_MB_MAP_NIDXS[TH_PF_NFORMATS]={6,8,8,12}; 1.80 + 1.81 +/*The number of extra bits that are coded with each of the DCT tokens. 1.82 + Each DCT token has some fixed number of additional bits (possibly 0) stored 1.83 + after the token itself, containing, for example, coefficient magnitude, 1.84 + sign bits, etc.*/ 1.85 +const unsigned char OC_DCT_TOKEN_EXTRA_BITS[TH_NDCT_TOKENS]={ 1.86 + 0,0,0,2,3,4,12,3,6, 1.87 + 0,0,0,0, 1.88 + 1,1,1,1,2,3,4,5,6,10, 1.89 + 1,1,1,1,1,3,4, 1.90 + 2,3 1.91 +}; 1.92 + 1.93 + 1.94 + 1.95 +int oc_ilog(unsigned _v){ 1.96 + int ret; 1.97 + for(ret=0;_v;ret++)_v>>=1; 1.98 + return ret; 1.99 +} 1.100 + 1.101 + 1.102 + 1.103 +void *oc_aligned_malloc(size_t _sz,size_t _align){ 1.104 + unsigned char *p; 1.105 + if(_align-1>UCHAR_MAX||(_align&_align-1)||_sz>~(size_t)0-_align)return NULL; 1.106 + p=(unsigned char *)_ogg_malloc(_sz+_align); 1.107 + if(p!=NULL){ 1.108 + int offs; 1.109 + offs=((p-(unsigned char *)0)-1&_align-1); 1.110 + p[offs]=offs; 1.111 + p+=offs+1; 1.112 + } 1.113 + return p; 1.114 +} 1.115 + 1.116 +void oc_aligned_free(void *_ptr){ 1.117 + unsigned char *p; 1.118 + p=(unsigned char *)_ptr; 1.119 + if(p!=NULL){ 1.120 + int offs; 1.121 + offs=*--p; 1.122 + _ogg_free(p-offs); 1.123 + } 1.124 +} 1.125 + 1.126 + 1.127 +void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz){ 1.128 + size_t rowsz; 1.129 + size_t colsz; 1.130 + size_t datsz; 1.131 + char *ret; 1.132 + colsz=_height*sizeof(void *); 1.133 + rowsz=_sz*_width; 1.134 + datsz=rowsz*_height; 1.135 + /*Alloc array and row pointers.*/ 1.136 + ret=(char *)_ogg_malloc(datsz+colsz); 1.137 + if(ret==NULL)return NULL; 1.138 + /*Initialize the array.*/ 1.139 + if(ret!=NULL){ 1.140 + size_t i; 1.141 + void **p; 1.142 + char *datptr; 1.143 + p=(void **)ret; 1.144 + i=_height; 1.145 + for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr; 1.146 + } 1.147 + return (void **)ret; 1.148 +} 1.149 + 1.150 +void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz){ 1.151 + size_t colsz; 1.152 + size_t rowsz; 1.153 + size_t datsz; 1.154 + char *ret; 1.155 + colsz=_height*sizeof(void *); 1.156 + rowsz=_sz*_width; 1.157 + datsz=rowsz*_height; 1.158 + /*Alloc array and row pointers.*/ 1.159 + ret=(char *)_ogg_calloc(datsz+colsz,1); 1.160 + if(ret==NULL)return NULL; 1.161 + /*Initialize the array.*/ 1.162 + if(ret!=NULL){ 1.163 + size_t i; 1.164 + void **p; 1.165 + char *datptr; 1.166 + p=(void **)ret; 1.167 + i=_height; 1.168 + for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr; 1.169 + } 1.170 + return (void **)ret; 1.171 +} 1.172 + 1.173 +void oc_free_2d(void *_ptr){ 1.174 + _ogg_free(_ptr); 1.175 +} 1.176 + 1.177 +/*Fills in a Y'CbCr buffer with a pointer to the image data in the first 1.178 + buffer, but with the opposite vertical orientation. 1.179 + _dst: The destination buffer. 1.180 + This can be the same as _src. 1.181 + _src: The source buffer.*/ 1.182 +void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst, 1.183 + const th_ycbcr_buffer _src){ 1.184 + int pli; 1.185 + for(pli=0;pli<3;pli++){ 1.186 + _dst[pli].width=_src[pli].width; 1.187 + _dst[pli].height=_src[pli].height; 1.188 + _dst[pli].stride=-_src[pli].stride; 1.189 + _dst[pli].data=_src[pli].data 1.190 + +(1-_dst[pli].height)*(ptrdiff_t)_dst[pli].stride; 1.191 + } 1.192 +} 1.193 + 1.194 +const char *th_version_string(void){ 1.195 + return OC_VENDOR_STRING; 1.196 +} 1.197 + 1.198 +ogg_uint32_t th_version_number(void){ 1.199 + return (TH_VERSION_MAJOR<<16)+(TH_VERSION_MINOR<<8)+TH_VERSION_SUB; 1.200 +} 1.201 + 1.202 +/*Determines the packet type. 1.203 + Note that this correctly interprets a 0-byte packet as a video data packet. 1.204 + Return: 1 for a header packet, 0 for a data packet.*/ 1.205 +int th_packet_isheader(ogg_packet *_op){ 1.206 + return _op->bytes>0?_op->packet[0]>>7:0; 1.207 +} 1.208 + 1.209 +/*Determines the frame type of a video data packet. 1.210 + Note that this correctly interprets a 0-byte packet as a delta frame. 1.211 + Return: 1 for a key frame, 0 for a delta frame, and -1 for a header 1.212 + packet.*/ 1.213 +int th_packet_iskeyframe(ogg_packet *_op){ 1.214 + return _op->bytes<=0?0:_op->packet[0]&0x80?-1:!(_op->packet[0]&0x40); 1.215 +}