Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /******************************************************************** |
michael@0 | 2 | * * |
michael@0 | 3 | * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * |
michael@0 | 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
michael@0 | 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
michael@0 | 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
michael@0 | 7 | * * |
michael@0 | 8 | * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * |
michael@0 | 9 | * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * |
michael@0 | 10 | * * |
michael@0 | 11 | ******************************************************************** |
michael@0 | 12 | |
michael@0 | 13 | function: |
michael@0 | 14 | last mod: $Id: internal.c 17506 2010-10-13 02:52:41Z tterribe $ |
michael@0 | 15 | |
michael@0 | 16 | ********************************************************************/ |
michael@0 | 17 | |
michael@0 | 18 | #include <stdlib.h> |
michael@0 | 19 | #include <limits.h> |
michael@0 | 20 | #include <string.h> |
michael@0 | 21 | #include "internal.h" |
michael@0 | 22 | |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | /*A map from the index in the zig zag scan to the coefficient number in a |
michael@0 | 26 | block. |
michael@0 | 27 | All zig zag indices beyond 63 are sent to coefficient 64, so that zero runs |
michael@0 | 28 | past the end of a block in bogus streams get mapped to a known location.*/ |
michael@0 | 29 | const unsigned char OC_FZIG_ZAG[128]={ |
michael@0 | 30 | 0, 1, 8,16, 9, 2, 3,10, |
michael@0 | 31 | 17,24,32,25,18,11, 4, 5, |
michael@0 | 32 | 12,19,26,33,40,48,41,34, |
michael@0 | 33 | 27,20,13, 6, 7,14,21,28, |
michael@0 | 34 | 35,42,49,56,57,50,43,36, |
michael@0 | 35 | 29,22,15,23,30,37,44,51, |
michael@0 | 36 | 58,59,52,45,38,31,39,46, |
michael@0 | 37 | 53,60,61,54,47,55,62,63, |
michael@0 | 38 | 64,64,64,64,64,64,64,64, |
michael@0 | 39 | 64,64,64,64,64,64,64,64, |
michael@0 | 40 | 64,64,64,64,64,64,64,64, |
michael@0 | 41 | 64,64,64,64,64,64,64,64, |
michael@0 | 42 | 64,64,64,64,64,64,64,64, |
michael@0 | 43 | 64,64,64,64,64,64,64,64, |
michael@0 | 44 | 64,64,64,64,64,64,64,64, |
michael@0 | 45 | 64,64,64,64,64,64,64,64 |
michael@0 | 46 | }; |
michael@0 | 47 | |
michael@0 | 48 | /*A map from the coefficient number in a block to its index in the zig zag |
michael@0 | 49 | scan.*/ |
michael@0 | 50 | const unsigned char OC_IZIG_ZAG[64]={ |
michael@0 | 51 | 0, 1, 5, 6,14,15,27,28, |
michael@0 | 52 | 2, 4, 7,13,16,26,29,42, |
michael@0 | 53 | 3, 8,12,17,25,30,41,43, |
michael@0 | 54 | 9,11,18,24,31,40,44,53, |
michael@0 | 55 | 10,19,23,32,39,45,52,54, |
michael@0 | 56 | 20,22,33,38,46,51,55,60, |
michael@0 | 57 | 21,34,37,47,50,56,59,61, |
michael@0 | 58 | 35,36,48,49,57,58,62,63 |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | /*A map from physical macro block ordering to bitstream macro block |
michael@0 | 62 | ordering within a super block.*/ |
michael@0 | 63 | const unsigned char OC_MB_MAP[2][2]={{0,3},{1,2}}; |
michael@0 | 64 | |
michael@0 | 65 | /*A list of the indices in the oc_mb.map array that can be valid for each of |
michael@0 | 66 | the various chroma decimation types.*/ |
michael@0 | 67 | const unsigned char OC_MB_MAP_IDXS[TH_PF_NFORMATS][12]={ |
michael@0 | 68 | {0,1,2,3,4,8}, |
michael@0 | 69 | {0,1,2,3,4,5,8,9}, |
michael@0 | 70 | {0,1,2,3,4,6,8,10}, |
michael@0 | 71 | {0,1,2,3,4,5,6,7,8,9,10,11} |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | /*The number of indices in the oc_mb.map array that can be valid for each of |
michael@0 | 75 | the various chroma decimation types.*/ |
michael@0 | 76 | const unsigned char OC_MB_MAP_NIDXS[TH_PF_NFORMATS]={6,8,8,12}; |
michael@0 | 77 | |
michael@0 | 78 | /*The number of extra bits that are coded with each of the DCT tokens. |
michael@0 | 79 | Each DCT token has some fixed number of additional bits (possibly 0) stored |
michael@0 | 80 | after the token itself, containing, for example, coefficient magnitude, |
michael@0 | 81 | sign bits, etc.*/ |
michael@0 | 82 | const unsigned char OC_DCT_TOKEN_EXTRA_BITS[TH_NDCT_TOKENS]={ |
michael@0 | 83 | 0,0,0,2,3,4,12,3,6, |
michael@0 | 84 | 0,0,0,0, |
michael@0 | 85 | 1,1,1,1,2,3,4,5,6,10, |
michael@0 | 86 | 1,1,1,1,1,3,4, |
michael@0 | 87 | 2,3 |
michael@0 | 88 | }; |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | |
michael@0 | 92 | int oc_ilog(unsigned _v){ |
michael@0 | 93 | int ret; |
michael@0 | 94 | for(ret=0;_v;ret++)_v>>=1; |
michael@0 | 95 | return ret; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | |
michael@0 | 99 | |
michael@0 | 100 | void *oc_aligned_malloc(size_t _sz,size_t _align){ |
michael@0 | 101 | unsigned char *p; |
michael@0 | 102 | if(_align-1>UCHAR_MAX||(_align&_align-1)||_sz>~(size_t)0-_align)return NULL; |
michael@0 | 103 | p=(unsigned char *)_ogg_malloc(_sz+_align); |
michael@0 | 104 | if(p!=NULL){ |
michael@0 | 105 | int offs; |
michael@0 | 106 | offs=((p-(unsigned char *)0)-1&_align-1); |
michael@0 | 107 | p[offs]=offs; |
michael@0 | 108 | p+=offs+1; |
michael@0 | 109 | } |
michael@0 | 110 | return p; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | void oc_aligned_free(void *_ptr){ |
michael@0 | 114 | unsigned char *p; |
michael@0 | 115 | p=(unsigned char *)_ptr; |
michael@0 | 116 | if(p!=NULL){ |
michael@0 | 117 | int offs; |
michael@0 | 118 | offs=*--p; |
michael@0 | 119 | _ogg_free(p-offs); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | |
michael@0 | 124 | void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz){ |
michael@0 | 125 | size_t rowsz; |
michael@0 | 126 | size_t colsz; |
michael@0 | 127 | size_t datsz; |
michael@0 | 128 | char *ret; |
michael@0 | 129 | colsz=_height*sizeof(void *); |
michael@0 | 130 | rowsz=_sz*_width; |
michael@0 | 131 | datsz=rowsz*_height; |
michael@0 | 132 | /*Alloc array and row pointers.*/ |
michael@0 | 133 | ret=(char *)_ogg_malloc(datsz+colsz); |
michael@0 | 134 | if(ret==NULL)return NULL; |
michael@0 | 135 | /*Initialize the array.*/ |
michael@0 | 136 | if(ret!=NULL){ |
michael@0 | 137 | size_t i; |
michael@0 | 138 | void **p; |
michael@0 | 139 | char *datptr; |
michael@0 | 140 | p=(void **)ret; |
michael@0 | 141 | i=_height; |
michael@0 | 142 | for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr; |
michael@0 | 143 | } |
michael@0 | 144 | return (void **)ret; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz){ |
michael@0 | 148 | size_t colsz; |
michael@0 | 149 | size_t rowsz; |
michael@0 | 150 | size_t datsz; |
michael@0 | 151 | char *ret; |
michael@0 | 152 | colsz=_height*sizeof(void *); |
michael@0 | 153 | rowsz=_sz*_width; |
michael@0 | 154 | datsz=rowsz*_height; |
michael@0 | 155 | /*Alloc array and row pointers.*/ |
michael@0 | 156 | ret=(char *)_ogg_calloc(datsz+colsz,1); |
michael@0 | 157 | if(ret==NULL)return NULL; |
michael@0 | 158 | /*Initialize the array.*/ |
michael@0 | 159 | if(ret!=NULL){ |
michael@0 | 160 | size_t i; |
michael@0 | 161 | void **p; |
michael@0 | 162 | char *datptr; |
michael@0 | 163 | p=(void **)ret; |
michael@0 | 164 | i=_height; |
michael@0 | 165 | for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr; |
michael@0 | 166 | } |
michael@0 | 167 | return (void **)ret; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | void oc_free_2d(void *_ptr){ |
michael@0 | 171 | _ogg_free(_ptr); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | /*Fills in a Y'CbCr buffer with a pointer to the image data in the first |
michael@0 | 175 | buffer, but with the opposite vertical orientation. |
michael@0 | 176 | _dst: The destination buffer. |
michael@0 | 177 | This can be the same as _src. |
michael@0 | 178 | _src: The source buffer.*/ |
michael@0 | 179 | void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst, |
michael@0 | 180 | const th_ycbcr_buffer _src){ |
michael@0 | 181 | int pli; |
michael@0 | 182 | for(pli=0;pli<3;pli++){ |
michael@0 | 183 | _dst[pli].width=_src[pli].width; |
michael@0 | 184 | _dst[pli].height=_src[pli].height; |
michael@0 | 185 | _dst[pli].stride=-_src[pli].stride; |
michael@0 | 186 | _dst[pli].data=_src[pli].data |
michael@0 | 187 | +(1-_dst[pli].height)*(ptrdiff_t)_dst[pli].stride; |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | const char *th_version_string(void){ |
michael@0 | 192 | return OC_VENDOR_STRING; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | ogg_uint32_t th_version_number(void){ |
michael@0 | 196 | return (TH_VERSION_MAJOR<<16)+(TH_VERSION_MINOR<<8)+TH_VERSION_SUB; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | /*Determines the packet type. |
michael@0 | 200 | Note that this correctly interprets a 0-byte packet as a video data packet. |
michael@0 | 201 | Return: 1 for a header packet, 0 for a data packet.*/ |
michael@0 | 202 | int th_packet_isheader(ogg_packet *_op){ |
michael@0 | 203 | return _op->bytes>0?_op->packet[0]>>7:0; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /*Determines the frame type of a video data packet. |
michael@0 | 207 | Note that this correctly interprets a 0-byte packet as a delta frame. |
michael@0 | 208 | Return: 1 for a key frame, 0 for a delta frame, and -1 for a header |
michael@0 | 209 | packet.*/ |
michael@0 | 210 | int th_packet_iskeyframe(ogg_packet *_op){ |
michael@0 | 211 | return _op->bytes<=0?0:_op->packet[0]&0x80?-1:!(_op->packet[0]&0x40); |
michael@0 | 212 | } |