state.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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: state.c 17576 2010-10-29 01:07:51Z tterribe $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "state.h"
  18. #if defined(OC_DUMP_IMAGES)
  19. # include <stdio.h>
  20. # include "png.h"
  21. #endif
  22. /*The function used to fill in the chroma plane motion vectors for a macro
  23. block when 4 different motion vectors are specified in the luma plane.
  24. This version is for use with chroma decimated in the X and Y directions
  25. (4:2:0).
  26. _cbmvs: The chroma block-level motion vectors to fill in.
  27. _lbmvs: The luma block-level motion vectors.*/
  28. static void oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  29. int dx;
  30. int dy;
  31. dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[1])
  32. +OC_MV_X(_lbmvs[2])+OC_MV_X(_lbmvs[3]);
  33. dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[1])
  34. +OC_MV_Y(_lbmvs[2])+OC_MV_Y(_lbmvs[3]);
  35. _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,2,2),OC_DIV_ROUND_POW2(dy,2,2));
  36. }
  37. /*The function used to fill in the chroma plane motion vectors for a macro
  38. block when 4 different motion vectors are specified in the luma plane.
  39. This version is for use with chroma decimated in the Y direction.
  40. _cbmvs: The chroma block-level motion vectors to fill in.
  41. _lbmvs: The luma block-level motion vectors.*/
  42. static void oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  43. int dx;
  44. int dy;
  45. dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[2]);
  46. dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[2]);
  47. _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  48. dx=OC_MV_X(_lbmvs[1])+OC_MV_X(_lbmvs[3]);
  49. dy=OC_MV_Y(_lbmvs[1])+OC_MV_Y(_lbmvs[3]);
  50. _cbmvs[1]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  51. }
  52. /*The function used to fill in the chroma plane motion vectors for a macro
  53. block when 4 different motion vectors are specified in the luma plane.
  54. This version is for use with chroma decimated in the X direction (4:2:2).
  55. _cbmvs: The chroma block-level motion vectors to fill in.
  56. _lbmvs: The luma block-level motion vectors.*/
  57. static void oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  58. int dx;
  59. int dy;
  60. dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[1]);
  61. dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[1]);
  62. _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  63. dx=OC_MV_X(_lbmvs[2])+OC_MV_X(_lbmvs[3]);
  64. dy=OC_MV_Y(_lbmvs[2])+OC_MV_Y(_lbmvs[3]);
  65. _cbmvs[2]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  66. }
  67. /*The function used to fill in the chroma plane motion vectors for a macro
  68. block when 4 different motion vectors are specified in the luma plane.
  69. This version is for use with no chroma decimation (4:4:4).
  70. _cbmvs: The chroma block-level motion vectors to fill in.
  71. _lmbmv: The luma macro-block level motion vector to fill in for use in
  72. prediction.
  73. _lbmvs: The luma block-level motion vectors.*/
  74. static void oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  75. _cbmvs[0]=_lbmvs[0];
  76. _cbmvs[1]=_lbmvs[1];
  77. _cbmvs[2]=_lbmvs[2];
  78. _cbmvs[3]=_lbmvs[3];
  79. }
  80. /*A table of functions used to fill in the chroma plane motion vectors for a
  81. macro block when 4 different motion vectors are specified in the luma
  82. plane.*/
  83. const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS]={
  84. (oc_set_chroma_mvs_func)oc_set_chroma_mvs00,
  85. (oc_set_chroma_mvs_func)oc_set_chroma_mvs01,
  86. (oc_set_chroma_mvs_func)oc_set_chroma_mvs10,
  87. (oc_set_chroma_mvs_func)oc_set_chroma_mvs11
  88. };
  89. /*Returns the fragment index of the top-left block in a macro block.
  90. This can be used to test whether or not the whole macro block is valid.
  91. _sb_map: The super block map.
  92. _quadi: The quadrant number.
  93. Return: The index of the fragment of the upper left block in the macro
  94. block, or -1 if the block lies outside the coded frame.*/
  95. static ptrdiff_t oc_sb_quad_top_left_frag(oc_sb_map_quad _sb_map[4],int _quadi){
  96. /*It so happens that under the Hilbert curve ordering described below, the
  97. upper-left block in each macro block is at index 0, except in macro block
  98. 3, where it is at index 2.*/
  99. return _sb_map[_quadi][_quadi&_quadi<<1];
  100. }
  101. /*Fills in the mapping from block positions to fragment numbers for a single
  102. color plane.
  103. This function also fills in the "valid" flag of each quadrant in the super
  104. block flags.
  105. _sb_maps: The array of super block maps for the color plane.
  106. _sb_flags: The array of super block flags for the color plane.
  107. _frag0: The index of the first fragment in the plane.
  108. _hfrags: The number of horizontal fragments in a coded frame.
  109. _vfrags: The number of vertical fragments in a coded frame.*/
  110. static void oc_sb_create_plane_mapping(oc_sb_map _sb_maps[],
  111. oc_sb_flags _sb_flags[],ptrdiff_t _frag0,int _hfrags,int _vfrags){
  112. /*Contains the (macro_block,block) indices for a 4x4 grid of
  113. fragments.
  114. The pattern is a 4x4 Hilbert space-filling curve.
  115. A Hilbert curve has the nice property that as the curve grows larger, its
  116. fractal dimension approaches 2.
  117. The intuition is that nearby blocks in the curve are also close spatially,
  118. with the previous element always an immediate neighbor, so that runs of
  119. blocks should be well correlated.*/
  120. static const int SB_MAP[4][4][2]={
  121. {{0,0},{0,1},{3,2},{3,3}},
  122. {{0,3},{0,2},{3,1},{3,0}},
  123. {{1,0},{1,3},{2,0},{2,3}},
  124. {{1,1},{1,2},{2,1},{2,2}}
  125. };
  126. ptrdiff_t yfrag;
  127. unsigned sbi;
  128. int y;
  129. sbi=0;
  130. yfrag=_frag0;
  131. for(y=0;;y+=4){
  132. int imax;
  133. int x;
  134. /*Figure out how many columns of blocks in this super block lie within the
  135. image.*/
  136. imax=_vfrags-y;
  137. if(imax>4)imax=4;
  138. else if(imax<=0)break;
  139. for(x=0;;x+=4,sbi++){
  140. ptrdiff_t xfrag;
  141. int jmax;
  142. int quadi;
  143. int i;
  144. /*Figure out how many rows of blocks in this super block lie within the
  145. image.*/
  146. jmax=_hfrags-x;
  147. if(jmax>4)jmax=4;
  148. else if(jmax<=0)break;
  149. /*By default, set all fragment indices to -1.*/
  150. memset(_sb_maps[sbi],0xFF,sizeof(_sb_maps[sbi]));
  151. /*Fill in the fragment map for this super block.*/
  152. xfrag=yfrag+x;
  153. for(i=0;i<imax;i++){
  154. int j;
  155. for(j=0;j<jmax;j++){
  156. _sb_maps[sbi][SB_MAP[i][j][0]][SB_MAP[i][j][1]]=xfrag+j;
  157. }
  158. xfrag+=_hfrags;
  159. }
  160. /*Mark which quadrants of this super block lie within the image.*/
  161. for(quadi=0;quadi<4;quadi++){
  162. _sb_flags[sbi].quad_valid|=
  163. (oc_sb_quad_top_left_frag(_sb_maps[sbi],quadi)>=0)<<quadi;
  164. }
  165. }
  166. yfrag+=_hfrags<<2;
  167. }
  168. }
  169. /*Fills in the Y plane fragment map for a macro block given the fragment
  170. coordinates of its upper-left hand corner.
  171. _mb_map: The macro block map to fill.
  172. _fplane: The description of the Y plane.
  173. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  174. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  175. static void oc_mb_fill_ymapping(oc_mb_map_plane _mb_map[3],
  176. const oc_fragment_plane *_fplane,int _xfrag0,int _yfrag0){
  177. int i;
  178. int j;
  179. for(i=0;i<2;i++)for(j=0;j<2;j++){
  180. _mb_map[0][i<<1|j]=(_yfrag0+i)*(ptrdiff_t)_fplane->nhfrags+_xfrag0+j;
  181. }
  182. }
  183. /*Fills in the chroma plane fragment maps for a macro block.
  184. This version is for use with chroma decimated in the X and Y directions
  185. (4:2:0).
  186. _mb_map: The macro block map to fill.
  187. _fplanes: The descriptions of the fragment planes.
  188. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  189. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  190. static void oc_mb_fill_cmapping00(oc_mb_map_plane _mb_map[3],
  191. const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){
  192. ptrdiff_t fragi;
  193. _xfrag0>>=1;
  194. _yfrag0>>=1;
  195. fragi=_yfrag0*(ptrdiff_t)_fplanes[1].nhfrags+_xfrag0;
  196. _mb_map[1][0]=fragi+_fplanes[1].froffset;
  197. _mb_map[2][0]=fragi+_fplanes[2].froffset;
  198. }
  199. /*Fills in the chroma plane fragment maps for a macro block.
  200. This version is for use with chroma decimated in the Y direction.
  201. _mb_map: The macro block map to fill.
  202. _fplanes: The descriptions of the fragment planes.
  203. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  204. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  205. static void oc_mb_fill_cmapping01(oc_mb_map_plane _mb_map[3],
  206. const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){
  207. ptrdiff_t fragi;
  208. int j;
  209. _yfrag0>>=1;
  210. fragi=_yfrag0*(ptrdiff_t)_fplanes[1].nhfrags+_xfrag0;
  211. for(j=0;j<2;j++){
  212. _mb_map[1][j]=fragi+_fplanes[1].froffset;
  213. _mb_map[2][j]=fragi+_fplanes[2].froffset;
  214. fragi++;
  215. }
  216. }
  217. /*Fills in the chroma plane fragment maps for a macro block.
  218. This version is for use with chroma decimated in the X direction (4:2:2).
  219. _mb_map: The macro block map to fill.
  220. _fplanes: The descriptions of the fragment planes.
  221. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  222. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  223. static void oc_mb_fill_cmapping10(oc_mb_map_plane _mb_map[3],
  224. const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){
  225. ptrdiff_t fragi;
  226. int i;
  227. _xfrag0>>=1;
  228. fragi=_yfrag0*(ptrdiff_t)_fplanes[1].nhfrags+_xfrag0;
  229. for(i=0;i<2;i++){
  230. _mb_map[1][i<<1]=fragi+_fplanes[1].froffset;
  231. _mb_map[2][i<<1]=fragi+_fplanes[2].froffset;
  232. fragi+=_fplanes[1].nhfrags;
  233. }
  234. }
  235. /*Fills in the chroma plane fragment maps for a macro block.
  236. This version is for use with no chroma decimation (4:4:4).
  237. This uses the already filled-in luma plane values.
  238. _mb_map: The macro block map to fill.
  239. _fplanes: The descriptions of the fragment planes.*/
  240. static void oc_mb_fill_cmapping11(oc_mb_map_plane _mb_map[3],
  241. const oc_fragment_plane _fplanes[3]){
  242. int k;
  243. for(k=0;k<4;k++){
  244. _mb_map[1][k]=_mb_map[0][k]+_fplanes[1].froffset;
  245. _mb_map[2][k]=_mb_map[0][k]+_fplanes[2].froffset;
  246. }
  247. }
  248. /*The function type used to fill in the chroma plane fragment maps for a
  249. macro block.
  250. _mb_map: The macro block map to fill.
  251. _fplanes: The descriptions of the fragment planes.
  252. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  253. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  254. typedef void (*oc_mb_fill_cmapping_func)(oc_mb_map_plane _mb_map[3],
  255. const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0);
  256. /*A table of functions used to fill in the chroma plane fragment maps for a
  257. macro block for each type of chrominance decimation.*/
  258. static const oc_mb_fill_cmapping_func OC_MB_FILL_CMAPPING_TABLE[4]={
  259. oc_mb_fill_cmapping00,
  260. oc_mb_fill_cmapping01,
  261. oc_mb_fill_cmapping10,
  262. (oc_mb_fill_cmapping_func)oc_mb_fill_cmapping11
  263. };
  264. /*Fills in the mapping from macro blocks to their corresponding fragment
  265. numbers in each plane.
  266. _mb_maps: The list of macro block maps.
  267. _mb_modes: The list of macro block modes; macro blocks completely outside
  268. the coded region are marked invalid.
  269. _fplanes: The descriptions of the fragment planes.
  270. _pixel_fmt: The chroma decimation type.*/
  271. static void oc_mb_create_mapping(oc_mb_map _mb_maps[],
  272. signed char _mb_modes[],const oc_fragment_plane _fplanes[3],int _pixel_fmt){
  273. oc_mb_fill_cmapping_func mb_fill_cmapping;
  274. unsigned sbi;
  275. int y;
  276. mb_fill_cmapping=OC_MB_FILL_CMAPPING_TABLE[_pixel_fmt];
  277. /*Loop through the luma plane super blocks.*/
  278. for(sbi=y=0;y<_fplanes[0].nvfrags;y+=4){
  279. int x;
  280. for(x=0;x<_fplanes[0].nhfrags;x+=4,sbi++){
  281. int ymb;
  282. /*Loop through the macro blocks in each super block in display order.*/
  283. for(ymb=0;ymb<2;ymb++){
  284. int xmb;
  285. for(xmb=0;xmb<2;xmb++){
  286. unsigned mbi;
  287. int mbx;
  288. int mby;
  289. mbi=sbi<<2|OC_MB_MAP[ymb][xmb];
  290. mbx=x|xmb<<1;
  291. mby=y|ymb<<1;
  292. /*Initialize fragment indices to -1.*/
  293. memset(_mb_maps[mbi],0xFF,sizeof(_mb_maps[mbi]));
  294. /*Make sure this macro block is within the encoded region.*/
  295. if(mbx>=_fplanes[0].nhfrags||mby>=_fplanes[0].nvfrags){
  296. _mb_modes[mbi]=OC_MODE_INVALID;
  297. continue;
  298. }
  299. /*Fill in the fragment indices for the luma plane.*/
  300. oc_mb_fill_ymapping(_mb_maps[mbi],_fplanes,mbx,mby);
  301. /*Fill in the fragment indices for the chroma planes.*/
  302. (*mb_fill_cmapping)(_mb_maps[mbi],_fplanes,mbx,mby);
  303. }
  304. }
  305. }
  306. }
  307. }
  308. /*Marks the fragments which fall all or partially outside the displayable
  309. region of the frame.
  310. _state: The Theora state containing the fragments to be marked.*/
  311. static void oc_state_border_init(oc_theora_state *_state){
  312. oc_fragment *frag;
  313. oc_fragment *yfrag_end;
  314. oc_fragment *xfrag_end;
  315. oc_fragment_plane *fplane;
  316. int crop_x0;
  317. int crop_y0;
  318. int crop_xf;
  319. int crop_yf;
  320. int pli;
  321. int y;
  322. int x;
  323. /*The method we use here is slow, but the code is dead simple and handles
  324. all the special cases easily.
  325. We only ever need to do it once.*/
  326. /*Loop through the fragments, marking those completely outside the
  327. displayable region and constructing a border mask for those that straddle
  328. the border.*/
  329. _state->nborders=0;
  330. yfrag_end=frag=_state->frags;
  331. for(pli=0;pli<3;pli++){
  332. fplane=_state->fplanes+pli;
  333. /*Set up the cropping rectangle for this plane.*/
  334. crop_x0=_state->info.pic_x;
  335. crop_xf=_state->info.pic_x+_state->info.pic_width;
  336. crop_y0=_state->info.pic_y;
  337. crop_yf=_state->info.pic_y+_state->info.pic_height;
  338. if(pli>0){
  339. if(!(_state->info.pixel_fmt&1)){
  340. crop_x0=crop_x0>>1;
  341. crop_xf=crop_xf+1>>1;
  342. }
  343. if(!(_state->info.pixel_fmt&2)){
  344. crop_y0=crop_y0>>1;
  345. crop_yf=crop_yf+1>>1;
  346. }
  347. }
  348. y=0;
  349. for(yfrag_end+=fplane->nfrags;frag<yfrag_end;y+=8){
  350. x=0;
  351. for(xfrag_end=frag+fplane->nhfrags;frag<xfrag_end;frag++,x+=8){
  352. /*First check to see if this fragment is completely outside the
  353. displayable region.*/
  354. /*Note the special checks for an empty cropping rectangle.
  355. This guarantees that if we count a fragment as straddling the
  356. border below, at least one pixel in the fragment will be inside
  357. the displayable region.*/
  358. if(x+8<=crop_x0||crop_xf<=x||y+8<=crop_y0||crop_yf<=y||
  359. crop_x0>=crop_xf||crop_y0>=crop_yf){
  360. frag->invalid=1;
  361. }
  362. /*Otherwise, check to see if it straddles the border.*/
  363. else if(x<crop_x0&&crop_x0<x+8||x<crop_xf&&crop_xf<x+8||
  364. y<crop_y0&&crop_y0<y+8||y<crop_yf&&crop_yf<y+8){
  365. ogg_int64_t mask;
  366. int npixels;
  367. int i;
  368. mask=npixels=0;
  369. for(i=0;i<8;i++){
  370. int j;
  371. for(j=0;j<8;j++){
  372. if(x+j>=crop_x0&&x+j<crop_xf&&y+i>=crop_y0&&y+i<crop_yf){
  373. mask|=(ogg_int64_t)1<<(i<<3|j);
  374. npixels++;
  375. }
  376. }
  377. }
  378. /*Search the fragment array for border info with the same pattern.
  379. In general, there will be at most 8 different patterns (per
  380. plane).*/
  381. for(i=0;;i++){
  382. if(i>=_state->nborders){
  383. _state->nborders++;
  384. _state->borders[i].mask=mask;
  385. _state->borders[i].npixels=npixels;
  386. }
  387. else if(_state->borders[i].mask!=mask)continue;
  388. frag->borderi=i;
  389. break;
  390. }
  391. }
  392. else frag->borderi=-1;
  393. }
  394. }
  395. }
  396. }
  397. static int oc_state_frarray_init(oc_theora_state *_state){
  398. int yhfrags;
  399. int yvfrags;
  400. int chfrags;
  401. int cvfrags;
  402. ptrdiff_t yfrags;
  403. ptrdiff_t cfrags;
  404. ptrdiff_t nfrags;
  405. unsigned yhsbs;
  406. unsigned yvsbs;
  407. unsigned chsbs;
  408. unsigned cvsbs;
  409. unsigned ysbs;
  410. unsigned csbs;
  411. unsigned nsbs;
  412. size_t nmbs;
  413. int hdec;
  414. int vdec;
  415. int pli;
  416. /*Figure out the number of fragments in each plane.*/
  417. /*These parameters have already been validated to be multiples of 16.*/
  418. yhfrags=_state->info.frame_width>>3;
  419. yvfrags=_state->info.frame_height>>3;
  420. hdec=!(_state->info.pixel_fmt&1);
  421. vdec=!(_state->info.pixel_fmt&2);
  422. chfrags=yhfrags+hdec>>hdec;
  423. cvfrags=yvfrags+vdec>>vdec;
  424. yfrags=yhfrags*(ptrdiff_t)yvfrags;
  425. cfrags=chfrags*(ptrdiff_t)cvfrags;
  426. nfrags=yfrags+2*cfrags;
  427. /*Figure out the number of super blocks in each plane.*/
  428. yhsbs=yhfrags+3>>2;
  429. yvsbs=yvfrags+3>>2;
  430. chsbs=chfrags+3>>2;
  431. cvsbs=cvfrags+3>>2;
  432. ysbs=yhsbs*yvsbs;
  433. csbs=chsbs*cvsbs;
  434. nsbs=ysbs+2*csbs;
  435. nmbs=(size_t)ysbs<<2;
  436. /*Check for overflow.
  437. We support the ridiculous upper limits of the specification (1048560 by
  438. 1048560, or 3 TB frames) if the target architecture has 64-bit pointers,
  439. but for those with 32-bit pointers (or smaller!) we have to check.
  440. If the caller wants to prevent denial-of-service by imposing a more
  441. reasonable upper limit on the size of attempted allocations, they must do
  442. so themselves; we have no platform independent way to determine how much
  443. system memory there is nor an application-independent way to decide what a
  444. "reasonable" allocation is.*/
  445. if(yfrags/yhfrags!=yvfrags||2*cfrags<cfrags||nfrags<yfrags||
  446. ysbs/yhsbs!=yvsbs||2*csbs<csbs||nsbs<ysbs||nmbs>>2!=ysbs){
  447. return TH_EIMPL;
  448. }
  449. /*Initialize the fragment array.*/
  450. _state->fplanes[0].nhfrags=yhfrags;
  451. _state->fplanes[0].nvfrags=yvfrags;
  452. _state->fplanes[0].froffset=0;
  453. _state->fplanes[0].nfrags=yfrags;
  454. _state->fplanes[0].nhsbs=yhsbs;
  455. _state->fplanes[0].nvsbs=yvsbs;
  456. _state->fplanes[0].sboffset=0;
  457. _state->fplanes[0].nsbs=ysbs;
  458. _state->fplanes[1].nhfrags=_state->fplanes[2].nhfrags=chfrags;
  459. _state->fplanes[1].nvfrags=_state->fplanes[2].nvfrags=cvfrags;
  460. _state->fplanes[1].froffset=yfrags;
  461. _state->fplanes[2].froffset=yfrags+cfrags;
  462. _state->fplanes[1].nfrags=_state->fplanes[2].nfrags=cfrags;
  463. _state->fplanes[1].nhsbs=_state->fplanes[2].nhsbs=chsbs;
  464. _state->fplanes[1].nvsbs=_state->fplanes[2].nvsbs=cvsbs;
  465. _state->fplanes[1].sboffset=ysbs;
  466. _state->fplanes[2].sboffset=ysbs+csbs;
  467. _state->fplanes[1].nsbs=_state->fplanes[2].nsbs=csbs;
  468. _state->nfrags=nfrags;
  469. _state->frags=_ogg_calloc(nfrags,sizeof(*_state->frags));
  470. _state->frag_mvs=_ogg_malloc(nfrags*sizeof(*_state->frag_mvs));
  471. _state->nsbs=nsbs;
  472. _state->sb_maps=_ogg_malloc(nsbs*sizeof(*_state->sb_maps));
  473. _state->sb_flags=_ogg_calloc(nsbs,sizeof(*_state->sb_flags));
  474. _state->nhmbs=yhsbs<<1;
  475. _state->nvmbs=yvsbs<<1;
  476. _state->nmbs=nmbs;
  477. _state->mb_maps=_ogg_calloc(nmbs,sizeof(*_state->mb_maps));
  478. _state->mb_modes=_ogg_calloc(nmbs,sizeof(*_state->mb_modes));
  479. _state->coded_fragis=_ogg_malloc(nfrags*sizeof(*_state->coded_fragis));
  480. if(_state->frags==NULL||_state->frag_mvs==NULL||_state->sb_maps==NULL||
  481. _state->sb_flags==NULL||_state->mb_maps==NULL||_state->mb_modes==NULL||
  482. _state->coded_fragis==NULL){
  483. return TH_EFAULT;
  484. }
  485. /*Create the mapping from super blocks to fragments.*/
  486. for(pli=0;pli<3;pli++){
  487. oc_fragment_plane *fplane;
  488. fplane=_state->fplanes+pli;
  489. oc_sb_create_plane_mapping(_state->sb_maps+fplane->sboffset,
  490. _state->sb_flags+fplane->sboffset,fplane->froffset,
  491. fplane->nhfrags,fplane->nvfrags);
  492. }
  493. /*Create the mapping from macro blocks to fragments.*/
  494. oc_mb_create_mapping(_state->mb_maps,_state->mb_modes,
  495. _state->fplanes,_state->info.pixel_fmt);
  496. /*Initialize the invalid and borderi fields of each fragment.*/
  497. oc_state_border_init(_state);
  498. return 0;
  499. }
  500. static void oc_state_frarray_clear(oc_theora_state *_state){
  501. _ogg_free(_state->coded_fragis);
  502. _ogg_free(_state->mb_modes);
  503. _ogg_free(_state->mb_maps);
  504. _ogg_free(_state->sb_flags);
  505. _ogg_free(_state->sb_maps);
  506. _ogg_free(_state->frag_mvs);
  507. _ogg_free(_state->frags);
  508. }
  509. /*Initializes the buffers used for reconstructed frames.
  510. These buffers are padded with 16 extra pixels on each side, to allow
  511. unrestricted motion vectors without special casing the boundary.
  512. If chroma is decimated in either direction, the padding is reduced by a
  513. factor of 2 on the appropriate sides.
  514. _nrefs: The number of reference buffers to init; must be in the range 3...6.*/
  515. static int oc_state_ref_bufs_init(oc_theora_state *_state,int _nrefs){
  516. th_info *info;
  517. unsigned char *ref_frame_data;
  518. size_t ref_frame_data_sz;
  519. size_t ref_frame_sz;
  520. size_t yplane_sz;
  521. size_t cplane_sz;
  522. int yhstride;
  523. int yheight;
  524. int chstride;
  525. int cheight;
  526. ptrdiff_t align;
  527. ptrdiff_t yoffset;
  528. ptrdiff_t coffset;
  529. ptrdiff_t *frag_buf_offs;
  530. ptrdiff_t fragi;
  531. int hdec;
  532. int vdec;
  533. int rfi;
  534. int pli;
  535. if(_nrefs<3||_nrefs>6)return TH_EINVAL;
  536. info=&_state->info;
  537. /*Compute the image buffer parameters for each plane.*/
  538. hdec=!(info->pixel_fmt&1);
  539. vdec=!(info->pixel_fmt&2);
  540. yhstride=info->frame_width+2*OC_UMV_PADDING;
  541. yheight=info->frame_height+2*OC_UMV_PADDING;
  542. /*Require 16-byte aligned rows in the chroma planes.*/
  543. chstride=(yhstride>>hdec)+15&~15;
  544. cheight=yheight>>vdec;
  545. yplane_sz=yhstride*(size_t)yheight;
  546. cplane_sz=chstride*(size_t)cheight;
  547. yoffset=OC_UMV_PADDING+OC_UMV_PADDING*(ptrdiff_t)yhstride;
  548. coffset=(OC_UMV_PADDING>>hdec)+(OC_UMV_PADDING>>vdec)*(ptrdiff_t)chstride;
  549. /*Although we guarantee the rows of the chroma planes are a multiple of 16
  550. bytes, the initial padding on the first row may only be 8 bytes.
  551. Compute the offset needed to the actual image data to a multiple of 16.*/
  552. align=-coffset&15;
  553. ref_frame_sz=yplane_sz+2*cplane_sz+16;
  554. ref_frame_data_sz=_nrefs*ref_frame_sz;
  555. /*Check for overflow.
  556. The same caveats apply as for oc_state_frarray_init().*/
  557. if(yplane_sz/yhstride!=(size_t)yheight||2*cplane_sz+16<cplane_sz||
  558. ref_frame_sz<yplane_sz||ref_frame_data_sz/_nrefs!=ref_frame_sz){
  559. return TH_EIMPL;
  560. }
  561. ref_frame_data=oc_aligned_malloc(ref_frame_data_sz,16);
  562. frag_buf_offs=_state->frag_buf_offs=
  563. _ogg_malloc(_state->nfrags*sizeof(*frag_buf_offs));
  564. if(ref_frame_data==NULL||frag_buf_offs==NULL){
  565. _ogg_free(frag_buf_offs);
  566. oc_aligned_free(ref_frame_data);
  567. return TH_EFAULT;
  568. }
  569. /*Set up the width, height and stride for the image buffers.*/
  570. _state->ref_frame_bufs[0][0].width=info->frame_width;
  571. _state->ref_frame_bufs[0][0].height=info->frame_height;
  572. _state->ref_frame_bufs[0][0].stride=yhstride;
  573. _state->ref_frame_bufs[0][1].width=_state->ref_frame_bufs[0][2].width=
  574. info->frame_width>>hdec;
  575. _state->ref_frame_bufs[0][1].height=_state->ref_frame_bufs[0][2].height=
  576. info->frame_height>>vdec;
  577. _state->ref_frame_bufs[0][1].stride=_state->ref_frame_bufs[0][2].stride=
  578. chstride;
  579. for(rfi=1;rfi<_nrefs;rfi++){
  580. memcpy(_state->ref_frame_bufs[rfi],_state->ref_frame_bufs[0],
  581. sizeof(_state->ref_frame_bufs[0]));
  582. }
  583. _state->ref_frame_handle=ref_frame_data;
  584. /*Set up the data pointers for the image buffers.*/
  585. for(rfi=0;rfi<_nrefs;rfi++){
  586. _state->ref_frame_bufs[rfi][0].data=ref_frame_data+yoffset;
  587. ref_frame_data+=yplane_sz+align;
  588. _state->ref_frame_bufs[rfi][1].data=ref_frame_data+coffset;
  589. ref_frame_data+=cplane_sz;
  590. _state->ref_frame_bufs[rfi][2].data=ref_frame_data+coffset;
  591. ref_frame_data+=cplane_sz+(16-align);
  592. /*Flip the buffer upside down.
  593. This allows us to decode Theora's bottom-up frames in their natural
  594. order, yet return a top-down buffer with a positive stride to the user.*/
  595. oc_ycbcr_buffer_flip(_state->ref_frame_bufs[rfi],
  596. _state->ref_frame_bufs[rfi]);
  597. }
  598. _state->ref_ystride[0]=-yhstride;
  599. _state->ref_ystride[1]=_state->ref_ystride[2]=-chstride;
  600. /*Initialize the fragment buffer offsets.*/
  601. ref_frame_data=_state->ref_frame_bufs[0][0].data;
  602. fragi=0;
  603. for(pli=0;pli<3;pli++){
  604. th_img_plane *iplane;
  605. oc_fragment_plane *fplane;
  606. unsigned char *vpix;
  607. ptrdiff_t stride;
  608. ptrdiff_t vfragi_end;
  609. int nhfrags;
  610. iplane=_state->ref_frame_bufs[0]+pli;
  611. fplane=_state->fplanes+pli;
  612. vpix=iplane->data;
  613. vfragi_end=fplane->froffset+fplane->nfrags;
  614. nhfrags=fplane->nhfrags;
  615. stride=iplane->stride;
  616. while(fragi<vfragi_end){
  617. ptrdiff_t hfragi_end;
  618. unsigned char *hpix;
  619. hpix=vpix;
  620. for(hfragi_end=fragi+nhfrags;fragi<hfragi_end;fragi++){
  621. frag_buf_offs[fragi]=hpix-ref_frame_data;
  622. hpix+=8;
  623. }
  624. vpix+=stride<<3;
  625. }
  626. }
  627. /*Initialize the reference frame pointers and indices.*/
  628. _state->ref_frame_idx[OC_FRAME_GOLD]=
  629. _state->ref_frame_idx[OC_FRAME_PREV]=
  630. _state->ref_frame_idx[OC_FRAME_GOLD_ORIG]=
  631. _state->ref_frame_idx[OC_FRAME_PREV_ORIG]=
  632. _state->ref_frame_idx[OC_FRAME_SELF]=
  633. _state->ref_frame_idx[OC_FRAME_IO]=-1;
  634. _state->ref_frame_data[OC_FRAME_GOLD]=
  635. _state->ref_frame_data[OC_FRAME_PREV]=
  636. _state->ref_frame_data[OC_FRAME_GOLD_ORIG]=
  637. _state->ref_frame_data[OC_FRAME_PREV_ORIG]=
  638. _state->ref_frame_data[OC_FRAME_SELF]=
  639. _state->ref_frame_data[OC_FRAME_IO]=NULL;
  640. return 0;
  641. }
  642. static void oc_state_ref_bufs_clear(oc_theora_state *_state){
  643. _ogg_free(_state->frag_buf_offs);
  644. oc_aligned_free(_state->ref_frame_handle);
  645. }
  646. void oc_state_accel_init_c(oc_theora_state *_state){
  647. _state->cpu_flags=0;
  648. #if defined(OC_STATE_USE_VTABLE)
  649. _state->opt_vtable.frag_copy=oc_frag_copy_c;
  650. _state->opt_vtable.frag_copy_list=oc_frag_copy_list_c;
  651. _state->opt_vtable.frag_recon_intra=oc_frag_recon_intra_c;
  652. _state->opt_vtable.frag_recon_inter=oc_frag_recon_inter_c;
  653. _state->opt_vtable.frag_recon_inter2=oc_frag_recon_inter2_c;
  654. _state->opt_vtable.idct8x8=oc_idct8x8_c;
  655. _state->opt_vtable.state_frag_recon=oc_state_frag_recon_c;
  656. _state->opt_vtable.loop_filter_init=oc_loop_filter_init_c;
  657. _state->opt_vtable.state_loop_filter_frag_rows=
  658. oc_state_loop_filter_frag_rows_c;
  659. _state->opt_vtable.restore_fpu=oc_restore_fpu_c;
  660. #endif
  661. _state->opt_data.dct_fzig_zag=OC_FZIG_ZAG;
  662. }
  663. int oc_state_init(oc_theora_state *_state,const th_info *_info,int _nrefs){
  664. int ret;
  665. /*First validate the parameters.*/
  666. if(_info==NULL)return TH_EFAULT;
  667. /*The width and height of the encoded frame must be multiples of 16.
  668. They must also, when divided by 16, fit into a 16-bit unsigned integer.
  669. The displayable frame offset coordinates must fit into an 8-bit unsigned
  670. integer.
  671. Note that the offset Y in the API is specified on the opposite side from
  672. how it is specified in the bitstream, because the Y axis is flipped in
  673. the bitstream.
  674. The displayable frame must fit inside the encoded frame.
  675. The color space must be one known by the encoder.*/
  676. if((_info->frame_width&0xF)||(_info->frame_height&0xF)||
  677. _info->frame_width<=0||_info->frame_width>=0x100000||
  678. _info->frame_height<=0||_info->frame_height>=0x100000||
  679. _info->pic_x+_info->pic_width>_info->frame_width||
  680. _info->pic_y+_info->pic_height>_info->frame_height||
  681. _info->pic_x>255||_info->frame_height-_info->pic_height-_info->pic_y>255||
  682. /*Note: the following <0 comparisons may generate spurious warnings on
  683. platforms where enums are unsigned.
  684. We could cast them to unsigned and just use the following >= comparison,
  685. but there are a number of compilers which will mis-optimize this.
  686. It's better to live with the spurious warnings.*/
  687. _info->colorspace<0||_info->colorspace>=TH_CS_NSPACES||
  688. _info->pixel_fmt<0||_info->pixel_fmt>=TH_PF_NFORMATS){
  689. return TH_EINVAL;
  690. }
  691. memset(_state,0,sizeof(*_state));
  692. memcpy(&_state->info,_info,sizeof(*_info));
  693. /*Invert the sense of pic_y to match Theora's right-handed coordinate
  694. system.*/
  695. _state->info.pic_y=_info->frame_height-_info->pic_height-_info->pic_y;
  696. _state->frame_type=OC_UNKWN_FRAME;
  697. oc_state_accel_init(_state);
  698. ret=oc_state_frarray_init(_state);
  699. if(ret>=0)ret=oc_state_ref_bufs_init(_state,_nrefs);
  700. if(ret<0){
  701. oc_state_frarray_clear(_state);
  702. return ret;
  703. }
  704. /*If the keyframe_granule_shift is out of range, use the maximum allowable
  705. value.*/
  706. if(_info->keyframe_granule_shift<0||_info->keyframe_granule_shift>31){
  707. _state->info.keyframe_granule_shift=31;
  708. }
  709. _state->keyframe_num=0;
  710. _state->curframe_num=-1;
  711. /*3.2.0 streams mark the frame index instead of the frame count.
  712. This was changed with stream version 3.2.1 to conform to other Ogg
  713. codecs.
  714. We add an extra bias when computing granule positions for new streams.*/
  715. _state->granpos_bias=TH_VERSION_CHECK(_info,3,2,1);
  716. return 0;
  717. }
  718. void oc_state_clear(oc_theora_state *_state){
  719. oc_state_ref_bufs_clear(_state);
  720. oc_state_frarray_clear(_state);
  721. }
  722. /*Duplicates the pixels on the border of the image plane out into the
  723. surrounding padding for use by unrestricted motion vectors.
  724. This function only adds the left and right borders, and only for the fragment
  725. rows specified.
  726. _refi: The index of the reference buffer to pad.
  727. _pli: The color plane.
  728. _y0: The Y coordinate of the first row to pad.
  729. _yend: The Y coordinate of the row to stop padding at.*/
  730. void oc_state_borders_fill_rows(oc_theora_state *_state,int _refi,int _pli,
  731. int _y0,int _yend){
  732. th_img_plane *iplane;
  733. unsigned char *apix;
  734. unsigned char *bpix;
  735. unsigned char *epix;
  736. int stride;
  737. int hpadding;
  738. hpadding=OC_UMV_PADDING>>(_pli!=0&&!(_state->info.pixel_fmt&1));
  739. iplane=_state->ref_frame_bufs[_refi]+_pli;
  740. stride=iplane->stride;
  741. apix=iplane->data+_y0*(ptrdiff_t)stride;
  742. bpix=apix+iplane->width-1;
  743. epix=iplane->data+_yend*(ptrdiff_t)stride;
  744. /*Note the use of != instead of <, which allows the stride to be negative.*/
  745. while(apix!=epix){
  746. memset(apix-hpadding,apix[0],hpadding);
  747. memset(bpix+1,bpix[0],hpadding);
  748. apix+=stride;
  749. bpix+=stride;
  750. }
  751. }
  752. /*Duplicates the pixels on the border of the image plane out into the
  753. surrounding padding for use by unrestricted motion vectors.
  754. This function only adds the top and bottom borders, and must be called after
  755. the left and right borders are added.
  756. _refi: The index of the reference buffer to pad.
  757. _pli: The color plane.*/
  758. void oc_state_borders_fill_caps(oc_theora_state *_state,int _refi,int _pli){
  759. th_img_plane *iplane;
  760. unsigned char *apix;
  761. unsigned char *bpix;
  762. unsigned char *epix;
  763. int stride;
  764. int hpadding;
  765. int vpadding;
  766. int fullw;
  767. hpadding=OC_UMV_PADDING>>(_pli!=0&&!(_state->info.pixel_fmt&1));
  768. vpadding=OC_UMV_PADDING>>(_pli!=0&&!(_state->info.pixel_fmt&2));
  769. iplane=_state->ref_frame_bufs[_refi]+_pli;
  770. stride=iplane->stride;
  771. fullw=iplane->width+(hpadding<<1);
  772. apix=iplane->data-hpadding;
  773. bpix=iplane->data+(iplane->height-1)*(ptrdiff_t)stride-hpadding;
  774. epix=apix-stride*(ptrdiff_t)vpadding;
  775. while(apix!=epix){
  776. memcpy(apix-stride,apix,fullw);
  777. memcpy(bpix+stride,bpix,fullw);
  778. apix-=stride;
  779. bpix+=stride;
  780. }
  781. }
  782. /*Duplicates the pixels on the border of the given reference image out into
  783. the surrounding padding for use by unrestricted motion vectors.
  784. _state: The context containing the reference buffers.
  785. _refi: The index of the reference buffer to pad.*/
  786. void oc_state_borders_fill(oc_theora_state *_state,int _refi){
  787. int pli;
  788. for(pli=0;pli<3;pli++){
  789. oc_state_borders_fill_rows(_state,_refi,pli,0,
  790. _state->ref_frame_bufs[_refi][pli].height);
  791. oc_state_borders_fill_caps(_state,_refi,pli);
  792. }
  793. }
  794. /*Determines the offsets in an image buffer to use for motion compensation.
  795. _state: The Theora state the offsets are to be computed with.
  796. _offsets: Returns the offset for the buffer(s).
  797. _offsets[0] is always set.
  798. _offsets[1] is set if the motion vector has non-zero fractional
  799. components.
  800. _pli: The color plane index.
  801. _mv: The motion vector.
  802. Return: The number of offsets returned: 1 or 2.*/
  803. int oc_state_get_mv_offsets(const oc_theora_state *_state,int _offsets[2],
  804. int _pli,oc_mv _mv){
  805. /*Here is a brief description of how Theora handles motion vectors:
  806. Motion vector components are specified to half-pixel accuracy in
  807. undecimated directions of each plane, and quarter-pixel accuracy in
  808. decimated directions.
  809. Integer parts are extracted by dividing (not shifting) by the
  810. appropriate amount, with truncation towards zero.
  811. These integer values are used to calculate the first offset.
  812. If either of the fractional parts are non-zero, then a second offset is
  813. computed.
  814. No third or fourth offsets are computed, even if both components have
  815. non-zero fractional parts.
  816. The second offset is computed by dividing (not shifting) by the
  817. appropriate amount, always truncating _away_ from zero.*/
  818. #if 0
  819. /*This version of the code doesn't use any tables, but is slower.*/
  820. int ystride;
  821. int xprec;
  822. int yprec;
  823. int xfrac;
  824. int yfrac;
  825. int offs;
  826. int dx;
  827. int dy;
  828. ystride=_state->ref_ystride[_pli];
  829. /*These two variables decide whether we are in half- or quarter-pixel
  830. precision in each component.*/
  831. xprec=1+(_pli!=0&&!(_state->info.pixel_fmt&1));
  832. yprec=1+(_pli!=0&&!(_state->info.pixel_fmt&2));
  833. dx=OC_MV_X(_mv);
  834. dy=OC_MV_Y(_mv);
  835. /*These two variables are either 0 if all the fractional bits are zero or -1
  836. if any of them are non-zero.*/
  837. xfrac=OC_SIGNMASK(-(dx&(xprec|1)));
  838. yfrac=OC_SIGNMASK(-(dy&(yprec|1)));
  839. offs=(dx>>xprec)+(dy>>yprec)*ystride;
  840. if(xfrac||yfrac){
  841. int xmask;
  842. int ymask;
  843. xmask=OC_SIGNMASK(dx);
  844. ymask=OC_SIGNMASK(dy);
  845. yfrac&=ystride;
  846. _offsets[0]=offs-(xfrac&xmask)+(yfrac&ymask);
  847. _offsets[1]=offs-(xfrac&~xmask)+(yfrac&~ymask);
  848. return 2;
  849. }
  850. else{
  851. _offsets[0]=offs;
  852. return 1;
  853. }
  854. #else
  855. /*Using tables simplifies the code, and there's enough arithmetic to hide the
  856. latencies of the memory references.*/
  857. static const signed char OC_MVMAP[2][64]={
  858. {
  859. -15,-15,-14,-14,-13,-13,-12,-12,-11,-11,-10,-10, -9, -9, -8,
  860. -8, -7, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0,
  861. 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
  862. 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15
  863. },
  864. {
  865. -7, -7, -7, -7, -6, -6, -6, -6, -5, -5, -5, -5, -4, -4, -4,
  866. -4, -3, -3, -3, -3, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 0,
  867. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
  868. 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7
  869. }
  870. };
  871. static const signed char OC_MVMAP2[2][64]={
  872. {
  873. -1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1,
  874. 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1,
  875. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  876. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
  877. },
  878. {
  879. -1,-1,-1, 0,-1,-1,-1, 0,-1,-1,-1, 0,-1,-1,-1,
  880. 0,-1,-1,-1, 0,-1,-1,-1, 0,-1,-1,-1, 0,-1,-1,-1,
  881. 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
  882. 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1
  883. }
  884. };
  885. int ystride;
  886. int qpx;
  887. int qpy;
  888. int mx;
  889. int my;
  890. int mx2;
  891. int my2;
  892. int offs;
  893. int dx;
  894. int dy;
  895. ystride=_state->ref_ystride[_pli];
  896. qpy=_pli!=0&&!(_state->info.pixel_fmt&2);
  897. dx=OC_MV_X(_mv);
  898. dy=OC_MV_Y(_mv);
  899. my=OC_MVMAP[qpy][dy+31];
  900. my2=OC_MVMAP2[qpy][dy+31];
  901. qpx=_pli!=0&&!(_state->info.pixel_fmt&1);
  902. mx=OC_MVMAP[qpx][dx+31];
  903. mx2=OC_MVMAP2[qpx][dx+31];
  904. offs=my*ystride+mx;
  905. if(mx2||my2){
  906. _offsets[1]=offs+my2*ystride+mx2;
  907. _offsets[0]=offs;
  908. return 2;
  909. }
  910. _offsets[0]=offs;
  911. return 1;
  912. #endif
  913. }
  914. void oc_state_frag_recon_c(const oc_theora_state *_state,ptrdiff_t _fragi,
  915. int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,ogg_uint16_t _dc_quant){
  916. unsigned char *dst;
  917. ptrdiff_t frag_buf_off;
  918. int ystride;
  919. int refi;
  920. /*Apply the inverse transform.*/
  921. /*Special case only having a DC component.*/
  922. if(_last_zzi<2){
  923. ogg_int16_t p;
  924. int ci;
  925. /*We round this dequant product (and not any of the others) because there's
  926. no iDCT rounding.*/
  927. p=(ogg_int16_t)(_dct_coeffs[0]*(ogg_int32_t)_dc_quant+15>>5);
  928. /*LOOP VECTORIZES.*/
  929. for(ci=0;ci<64;ci++)_dct_coeffs[64+ci]=p;
  930. }
  931. else{
  932. /*First, dequantize the DC coefficient.*/
  933. _dct_coeffs[0]=(ogg_int16_t)(_dct_coeffs[0]*(int)_dc_quant);
  934. oc_idct8x8(_state,_dct_coeffs+64,_dct_coeffs,_last_zzi);
  935. }
  936. /*Fill in the target buffer.*/
  937. frag_buf_off=_state->frag_buf_offs[_fragi];
  938. refi=_state->frags[_fragi].refi;
  939. ystride=_state->ref_ystride[_pli];
  940. dst=_state->ref_frame_data[OC_FRAME_SELF]+frag_buf_off;
  941. if(refi==OC_FRAME_SELF)oc_frag_recon_intra(_state,dst,ystride,_dct_coeffs+64);
  942. else{
  943. const unsigned char *ref;
  944. int mvoffsets[2];
  945. ref=_state->ref_frame_data[refi]+frag_buf_off;
  946. if(oc_state_get_mv_offsets(_state,mvoffsets,_pli,
  947. _state->frag_mvs[_fragi])>1){
  948. oc_frag_recon_inter2(_state,
  949. dst,ref+mvoffsets[0],ref+mvoffsets[1],ystride,_dct_coeffs+64);
  950. }
  951. else{
  952. oc_frag_recon_inter(_state,dst,ref+mvoffsets[0],ystride,_dct_coeffs+64);
  953. }
  954. }
  955. }
  956. static void loop_filter_h(unsigned char *_pix,int _ystride,signed char *_bv){
  957. int y;
  958. _pix-=2;
  959. for(y=0;y<8;y++){
  960. int f;
  961. f=_pix[0]-_pix[3]+3*(_pix[2]-_pix[1]);
  962. /*The _bv array is used to compute the function
  963. f=OC_CLAMPI(OC_MINI(-_2flimit-f,0),f,OC_MAXI(_2flimit-f,0));
  964. where _2flimit=_state->loop_filter_limits[_state->qis[0]]<<1;*/
  965. f=*(_bv+(f+4>>3));
  966. _pix[1]=OC_CLAMP255(_pix[1]+f);
  967. _pix[2]=OC_CLAMP255(_pix[2]-f);
  968. _pix+=_ystride;
  969. }
  970. }
  971. static void loop_filter_v(unsigned char *_pix,int _ystride,signed char *_bv){
  972. int x;
  973. _pix-=_ystride*2;
  974. for(x=0;x<8;x++){
  975. int f;
  976. f=_pix[x]-_pix[_ystride*3+x]+3*(_pix[_ystride*2+x]-_pix[_ystride+x]);
  977. /*The _bv array is used to compute the function
  978. f=OC_CLAMPI(OC_MINI(-_2flimit-f,0),f,OC_MAXI(_2flimit-f,0));
  979. where _2flimit=_state->loop_filter_limits[_state->qis[0]]<<1;*/
  980. f=*(_bv+(f+4>>3));
  981. _pix[_ystride+x]=OC_CLAMP255(_pix[_ystride+x]+f);
  982. _pix[_ystride*2+x]=OC_CLAMP255(_pix[_ystride*2+x]-f);
  983. }
  984. }
  985. /*Initialize the bounding values array used by the loop filter.
  986. _bv: Storage for the array.
  987. _flimit: The filter limit as defined in Section 7.10 of the spec.*/
  988. void oc_loop_filter_init_c(signed char _bv[256],int _flimit){
  989. int i;
  990. memset(_bv,0,sizeof(_bv[0])*256);
  991. for(i=0;i<_flimit;i++){
  992. if(127-i-_flimit>=0)_bv[127-i-_flimit]=(signed char)(i-_flimit);
  993. _bv[127-i]=(signed char)(-i);
  994. _bv[127+i]=(signed char)(i);
  995. if(127+i+_flimit<256)_bv[127+i+_flimit]=(signed char)(_flimit-i);
  996. }
  997. }
  998. /*Apply the loop filter to a given set of fragment rows in the given plane.
  999. The filter may be run on the bottom edge, affecting pixels in the next row of
  1000. fragments, so this row also needs to be available.
  1001. _bv: The bounding values array.
  1002. _refi: The index of the frame buffer to filter.
  1003. _pli: The color plane to filter.
  1004. _fragy0: The Y coordinate of the first fragment row to filter.
  1005. _fragy_end: The Y coordinate of the fragment row to stop filtering at.*/
  1006. void oc_state_loop_filter_frag_rows_c(const oc_theora_state *_state,
  1007. signed char *_bv,int _refi,int _pli,int _fragy0,int _fragy_end){
  1008. const oc_fragment_plane *fplane;
  1009. const oc_fragment *frags;
  1010. const ptrdiff_t *frag_buf_offs;
  1011. unsigned char *ref_frame_data;
  1012. ptrdiff_t fragi_top;
  1013. ptrdiff_t fragi_bot;
  1014. ptrdiff_t fragi0;
  1015. ptrdiff_t fragi0_end;
  1016. int ystride;
  1017. int nhfrags;
  1018. _bv+=127;
  1019. fplane=_state->fplanes+_pli;
  1020. nhfrags=fplane->nhfrags;
  1021. fragi_top=fplane->froffset;
  1022. fragi_bot=fragi_top+fplane->nfrags;
  1023. fragi0=fragi_top+_fragy0*(ptrdiff_t)nhfrags;
  1024. fragi0_end=fragi_top+_fragy_end*(ptrdiff_t)nhfrags;
  1025. ystride=_state->ref_ystride[_pli];
  1026. frags=_state->frags;
  1027. frag_buf_offs=_state->frag_buf_offs;
  1028. ref_frame_data=_state->ref_frame_data[_refi];
  1029. /*The following loops are constructed somewhat non-intuitively on purpose.
  1030. The main idea is: if a block boundary has at least one coded fragment on
  1031. it, the filter is applied to it.
  1032. However, the order that the filters are applied in matters, and VP3 chose
  1033. the somewhat strange ordering used below.*/
  1034. while(fragi0<fragi0_end){
  1035. ptrdiff_t fragi;
  1036. ptrdiff_t fragi_end;
  1037. fragi=fragi0;
  1038. fragi_end=fragi+nhfrags;
  1039. while(fragi<fragi_end){
  1040. if(frags[fragi].coded){
  1041. unsigned char *ref;
  1042. ref=ref_frame_data+frag_buf_offs[fragi];
  1043. if(fragi>fragi0)loop_filter_h(ref,ystride,_bv);
  1044. if(fragi0>fragi_top)loop_filter_v(ref,ystride,_bv);
  1045. if(fragi+1<fragi_end&&!frags[fragi+1].coded){
  1046. loop_filter_h(ref+8,ystride,_bv);
  1047. }
  1048. if(fragi+nhfrags<fragi_bot&&!frags[fragi+nhfrags].coded){
  1049. loop_filter_v(ref+(ystride<<3),ystride,_bv);
  1050. }
  1051. }
  1052. fragi++;
  1053. }
  1054. fragi0+=nhfrags;
  1055. }
  1056. }
  1057. #if defined(OC_DUMP_IMAGES)
  1058. int oc_state_dump_frame(const oc_theora_state *_state,int _frame,
  1059. const char *_suf){
  1060. /*Dump a PNG of the reconstructed image.*/
  1061. png_structp png;
  1062. png_infop info;
  1063. png_bytep *image;
  1064. FILE *fp;
  1065. char fname[16];
  1066. unsigned char *y_row;
  1067. unsigned char *u_row;
  1068. unsigned char *v_row;
  1069. unsigned char *y;
  1070. unsigned char *u;
  1071. unsigned char *v;
  1072. ogg_int64_t iframe;
  1073. ogg_int64_t pframe;
  1074. int y_stride;
  1075. int u_stride;
  1076. int v_stride;
  1077. int framei;
  1078. int width;
  1079. int height;
  1080. int imgi;
  1081. int imgj;
  1082. width=_state->info.frame_width;
  1083. height=_state->info.frame_height;
  1084. iframe=_state->granpos>>_state->info.keyframe_granule_shift;
  1085. pframe=_state->granpos-(iframe<<_state->info.keyframe_granule_shift);
  1086. sprintf(fname,"%08i%s.png",(int)(iframe+pframe),_suf);
  1087. fp=fopen(fname,"wb");
  1088. if(fp==NULL)return TH_EFAULT;
  1089. image=(png_bytep *)oc_malloc_2d(height,6*width,sizeof(**image));
  1090. if(image==NULL){
  1091. fclose(fp);
  1092. return TH_EFAULT;
  1093. }
  1094. png=png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
  1095. if(png==NULL){
  1096. oc_free_2d(image);
  1097. fclose(fp);
  1098. return TH_EFAULT;
  1099. }
  1100. info=png_create_info_struct(png);
  1101. if(info==NULL){
  1102. png_destroy_write_struct(&png,NULL);
  1103. oc_free_2d(image);
  1104. fclose(fp);
  1105. return TH_EFAULT;
  1106. }
  1107. if(setjmp(png_jmpbuf(png))){
  1108. png_destroy_write_struct(&png,&info);
  1109. oc_free_2d(image);
  1110. fclose(fp);
  1111. return TH_EFAULT;
  1112. }
  1113. framei=_state->ref_frame_idx[_frame];
  1114. y_row=_state->ref_frame_bufs[framei][0].data;
  1115. u_row=_state->ref_frame_bufs[framei][1].data;
  1116. v_row=_state->ref_frame_bufs[framei][2].data;
  1117. y_stride=_state->ref_frame_bufs[framei][0].stride;
  1118. u_stride=_state->ref_frame_bufs[framei][1].stride;
  1119. v_stride=_state->ref_frame_bufs[framei][2].stride;
  1120. /*Chroma up-sampling is just done with a box filter.
  1121. This is very likely what will actually be used in practice on a real
  1122. display, and also removes one more layer to search in for the source of
  1123. artifacts.
  1124. As an added bonus, it's dead simple.*/
  1125. for(imgi=height;imgi-->0;){
  1126. int dc;
  1127. y=y_row;
  1128. u=u_row;
  1129. v=v_row;
  1130. for(imgj=0;imgj<6*width;){
  1131. float yval;
  1132. float uval;
  1133. float vval;
  1134. unsigned rval;
  1135. unsigned gval;
  1136. unsigned bval;
  1137. /*This is intentionally slow and very accurate.*/
  1138. yval=(*y-16)*(1.0F/219);
  1139. uval=(*u-128)*(2*(1-0.114F)/224);
  1140. vval=(*v-128)*(2*(1-0.299F)/224);
  1141. rval=OC_CLAMPI(0,(int)(65535*(yval+vval)+0.5F),65535);
  1142. gval=OC_CLAMPI(0,(int)(65535*(
  1143. yval-uval*(0.114F/0.587F)-vval*(0.299F/0.587F))+0.5F),65535);
  1144. bval=OC_CLAMPI(0,(int)(65535*(yval+uval)+0.5F),65535);
  1145. image[imgi][imgj++]=(unsigned char)(rval>>8);
  1146. image[imgi][imgj++]=(unsigned char)(rval&0xFF);
  1147. image[imgi][imgj++]=(unsigned char)(gval>>8);
  1148. image[imgi][imgj++]=(unsigned char)(gval&0xFF);
  1149. image[imgi][imgj++]=(unsigned char)(bval>>8);
  1150. image[imgi][imgj++]=(unsigned char)(bval&0xFF);
  1151. dc=(y-y_row&1)|(_state->info.pixel_fmt&1);
  1152. y++;
  1153. u+=dc;
  1154. v+=dc;
  1155. }
  1156. dc=-((height-1-imgi&1)|_state->info.pixel_fmt>>1);
  1157. y_row+=y_stride;
  1158. u_row+=dc&u_stride;
  1159. v_row+=dc&v_stride;
  1160. }
  1161. png_init_io(png,fp);
  1162. png_set_compression_level(png,Z_BEST_COMPRESSION);
  1163. png_set_IHDR(png,info,width,height,16,PNG_COLOR_TYPE_RGB,
  1164. PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
  1165. switch(_state->info.colorspace){
  1166. case TH_CS_ITU_REC_470M:{
  1167. png_set_gAMA(png,info,2.2);
  1168. png_set_cHRM_fixed(png,info,31006,31616,
  1169. 67000,32000,21000,71000,14000,8000);
  1170. }break;
  1171. case TH_CS_ITU_REC_470BG:{
  1172. png_set_gAMA(png,info,2.67);
  1173. png_set_cHRM_fixed(png,info,31271,32902,
  1174. 64000,33000,29000,60000,15000,6000);
  1175. }break;
  1176. default:break;
  1177. }
  1178. png_set_pHYs(png,info,_state->info.aspect_numerator,
  1179. _state->info.aspect_denominator,0);
  1180. png_set_rows(png,info,image);
  1181. png_write_png(png,info,PNG_TRANSFORM_IDENTITY,NULL);
  1182. png_write_end(png,info);
  1183. png_destroy_write_struct(&png,&info);
  1184. oc_free_2d(image);
  1185. fclose(fp);
  1186. return 0;
  1187. }
  1188. #endif
  1189. ogg_int64_t th_granule_frame(void *_encdec,ogg_int64_t _granpos){
  1190. oc_theora_state *state;
  1191. state=(oc_theora_state *)_encdec;
  1192. if(_granpos>=0){
  1193. ogg_int64_t iframe;
  1194. ogg_int64_t pframe;
  1195. iframe=_granpos>>state->info.keyframe_granule_shift;
  1196. pframe=_granpos-(iframe<<state->info.keyframe_granule_shift);
  1197. /*3.2.0 streams store the frame index in the granule position.
  1198. 3.2.1 and later store the frame count.
  1199. We return the index, so adjust the value if we have a 3.2.1 or later
  1200. stream.*/
  1201. return iframe+pframe-TH_VERSION_CHECK(&state->info,3,2,1);
  1202. }
  1203. return -1;
  1204. }
  1205. double th_granule_time(void *_encdec,ogg_int64_t _granpos){
  1206. oc_theora_state *state;
  1207. state=(oc_theora_state *)_encdec;
  1208. if(_granpos>=0){
  1209. return (th_granule_frame(_encdec, _granpos)+1)*(
  1210. (double)state->info.fps_denominator/state->info.fps_numerator);
  1211. }
  1212. return -1;
  1213. }