state.c 46 KB

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