internal.c 5.8 KB

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