internal.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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: internal.c 16503 2009-08-22 18:14:02Z giles $
  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. /*The function used to fill in the chroma plane motion vectors for a macro
  84. block when 4 different motion vectors are specified in the luma plane.
  85. This version is for use with chroma decimated in the X and Y directions
  86. (4:2:0).
  87. _cbmvs: The chroma block-level motion vectors to fill in.
  88. _lbmvs: The luma block-level motion vectors.*/
  89. static void oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  90. int dx;
  91. int dy;
  92. dx=_lbmvs[0][0]+_lbmvs[1][0]+_lbmvs[2][0]+_lbmvs[3][0];
  93. dy=_lbmvs[0][1]+_lbmvs[1][1]+_lbmvs[2][1]+_lbmvs[3][1];
  94. _cbmvs[0][0]=(signed char)OC_DIV_ROUND_POW2(dx,2,2);
  95. _cbmvs[0][1]=(signed char)OC_DIV_ROUND_POW2(dy,2,2);
  96. }
  97. /*The function used to fill in the chroma plane motion vectors for a macro
  98. block when 4 different motion vectors are specified in the luma plane.
  99. This version is for use with chroma decimated in the Y direction.
  100. _cbmvs: The chroma block-level motion vectors to fill in.
  101. _lbmvs: The luma block-level motion vectors.*/
  102. static void oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  103. int dx;
  104. int dy;
  105. dx=_lbmvs[0][0]+_lbmvs[2][0];
  106. dy=_lbmvs[0][1]+_lbmvs[2][1];
  107. _cbmvs[0][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
  108. _cbmvs[0][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
  109. dx=_lbmvs[1][0]+_lbmvs[3][0];
  110. dy=_lbmvs[1][1]+_lbmvs[3][1];
  111. _cbmvs[1][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
  112. _cbmvs[1][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
  113. }
  114. /*The function used to fill in the chroma plane motion vectors for a macro
  115. block when 4 different motion vectors are specified in the luma plane.
  116. This version is for use with chroma decimated in the X direction (4:2:2).
  117. _cbmvs: The chroma block-level motion vectors to fill in.
  118. _lbmvs: The luma block-level motion vectors.*/
  119. static void oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  120. int dx;
  121. int dy;
  122. dx=_lbmvs[0][0]+_lbmvs[1][0];
  123. dy=_lbmvs[0][1]+_lbmvs[1][1];
  124. _cbmvs[0][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
  125. _cbmvs[0][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
  126. dx=_lbmvs[2][0]+_lbmvs[3][0];
  127. dy=_lbmvs[2][1]+_lbmvs[3][1];
  128. _cbmvs[2][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
  129. _cbmvs[2][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
  130. }
  131. /*The function used to fill in the chroma plane motion vectors for a macro
  132. block when 4 different motion vectors are specified in the luma plane.
  133. This version is for use with no chroma decimation (4:4:4).
  134. _cbmvs: The chroma block-level motion vectors to fill in.
  135. _lmbmv: The luma macro-block level motion vector to fill in for use in
  136. prediction.
  137. _lbmvs: The luma block-level motion vectors.*/
  138. static void oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  139. memcpy(_cbmvs,_lbmvs,4*sizeof(_lbmvs[0]));
  140. }
  141. /*A table of functions used to fill in the chroma plane motion vectors for a
  142. macro block when 4 different motion vectors are specified in the luma
  143. plane.*/
  144. const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS]={
  145. (oc_set_chroma_mvs_func)oc_set_chroma_mvs00,
  146. (oc_set_chroma_mvs_func)oc_set_chroma_mvs01,
  147. (oc_set_chroma_mvs_func)oc_set_chroma_mvs10,
  148. (oc_set_chroma_mvs_func)oc_set_chroma_mvs11
  149. };
  150. void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz){
  151. size_t rowsz;
  152. size_t colsz;
  153. size_t datsz;
  154. char *ret;
  155. colsz=_height*sizeof(void *);
  156. rowsz=_sz*_width;
  157. datsz=rowsz*_height;
  158. /*Alloc array and row pointers.*/
  159. ret=(char *)_ogg_malloc(datsz+colsz);
  160. if(ret==NULL)return NULL;
  161. /*Initialize the array.*/
  162. if(ret!=NULL){
  163. size_t i;
  164. void **p;
  165. char *datptr;
  166. p=(void **)ret;
  167. i=_height;
  168. for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr;
  169. }
  170. return (void **)ret;
  171. }
  172. void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz){
  173. size_t colsz;
  174. size_t rowsz;
  175. size_t datsz;
  176. char *ret;
  177. colsz=_height*sizeof(void *);
  178. rowsz=_sz*_width;
  179. datsz=rowsz*_height;
  180. /*Alloc array and row pointers.*/
  181. ret=(char *)_ogg_calloc(datsz+colsz,1);
  182. if(ret==NULL)return NULL;
  183. /*Initialize the array.*/
  184. if(ret!=NULL){
  185. size_t i;
  186. void **p;
  187. char *datptr;
  188. p=(void **)ret;
  189. i=_height;
  190. for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr;
  191. }
  192. return (void **)ret;
  193. }
  194. void oc_free_2d(void *_ptr){
  195. _ogg_free(_ptr);
  196. }
  197. /*Fills in a Y'CbCr buffer with a pointer to the image data in the first
  198. buffer, but with the opposite vertical orientation.
  199. _dst: The destination buffer.
  200. This can be the same as _src.
  201. _src: The source buffer.*/
  202. void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst,
  203. const th_ycbcr_buffer _src){
  204. int pli;
  205. for(pli=0;pli<3;pli++){
  206. _dst[pli].width=_src[pli].width;
  207. _dst[pli].height=_src[pli].height;
  208. _dst[pli].stride=-_src[pli].stride;
  209. _dst[pli].data=_src[pli].data
  210. +(1-_dst[pli].height)*(ptrdiff_t)_dst[pli].stride;
  211. }
  212. }
  213. const char *th_version_string(void){
  214. return OC_VENDOR_STRING;
  215. }
  216. ogg_uint32_t th_version_number(void){
  217. return (TH_VERSION_MAJOR<<16)+(TH_VERSION_MINOR<<8)+TH_VERSION_SUB;
  218. }
  219. /*Determines the packet type.
  220. Note that this correctly interprets a 0-byte packet as a video data packet.
  221. Return: 1 for a header packet, 0 for a data packet.*/
  222. int th_packet_isheader(ogg_packet *_op){
  223. return _op->bytes>0?_op->packet[0]>>7:0;
  224. }
  225. /*Determines the frame type of a video data packet.
  226. Note that this correctly interprets a 0-byte packet as a delta frame.
  227. Return: 1 for a key frame, 0 for a delta frame, and -1 for a header
  228. packet.*/
  229. int th_packet_iskeyframe(ogg_packet *_op){
  230. return _op->bytes<=0?0:_op->packet[0]&0x80?-1:!(_op->packet[0]&0x40);
  231. }