state.c 44 KB

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