state.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /********************************************************************
  3. Copyright (C) 2002-2009 Xiph.org Foundation
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
  19. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ********************************************************************/
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "state.h"
  30. #include "ocintrin.h"
  31. #if defined(OC_DUMP_IMAGES)
  32. # include <stdio.h>
  33. # include "png.h"
  34. #endif
  35. /*The function used to fill in the chroma plane motion vectors for a macro
  36. block when 4 different motion vectors are specified in the luma plane.
  37. This version is for use with chroma decimated in the X and Y directions
  38. (4:2:0).
  39. _cbmvs: The chroma block-level motion vectors to fill in.
  40. _lbmvs: The luma block-level motion vectors.*/
  41. static void oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4])
  42. {
  43. int32_t dx;
  44. int32_t dy;
  45. dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[1])
  46. +OC_MV_X(_lbmvs[2])+OC_MV_X(_lbmvs[3]);
  47. dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[1])
  48. +OC_MV_Y(_lbmvs[2])+OC_MV_Y(_lbmvs[3]);
  49. _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,2,2),OC_DIV_ROUND_POW2(dy,2,2));
  50. }
  51. /*The function used to fill in the chroma plane motion vectors for a macro
  52. block when 4 different motion vectors are specified in the luma plane.
  53. This version is for use with chroma decimated in the Y direction.
  54. _cbmvs: The chroma block-level motion vectors to fill in.
  55. _lbmvs: The luma block-level motion vectors.*/
  56. static void oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
  57. int32_t dx;
  58. int32_t dy;
  59. dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[2]);
  60. dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[2]);
  61. _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  62. dx=OC_MV_X(_lbmvs[1])+OC_MV_X(_lbmvs[3]);
  63. dy=OC_MV_Y(_lbmvs[1])+OC_MV_Y(_lbmvs[3]);
  64. _cbmvs[1]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  65. }
  66. /*The function used to fill in the chroma plane motion vectors for a macro
  67. block when 4 different motion vectors are specified in the luma plane.
  68. This version is for use with chroma decimated in the X direction (4:2:2).
  69. _cbmvs: The chroma block-level motion vectors to fill in.
  70. _lbmvs: The luma block-level motion vectors.*/
  71. static void oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4])
  72. {
  73. int32_t dx;
  74. int32_t dy;
  75. dx=OC_MV_X(_lbmvs[0])+OC_MV_X(_lbmvs[1]);
  76. dy=OC_MV_Y(_lbmvs[0])+OC_MV_Y(_lbmvs[1]);
  77. _cbmvs[0]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  78. dx=OC_MV_X(_lbmvs[2])+OC_MV_X(_lbmvs[3]);
  79. dy=OC_MV_Y(_lbmvs[2])+OC_MV_Y(_lbmvs[3]);
  80. _cbmvs[2]=OC_MV(OC_DIV_ROUND_POW2(dx,1,1),OC_DIV_ROUND_POW2(dy,1,1));
  81. }
  82. /*The function used to fill in the chroma plane motion vectors for a macro
  83. block when 4 different motion vectors are specified in the luma plane.
  84. This version is for use with no chroma decimation (4:4:4).
  85. _cbmvs: The chroma block-level motion vectors to fill in.
  86. _lmbmv: The luma macro-block level motion vector to fill in for use in
  87. prediction.
  88. _lbmvs: The luma block-level motion vectors.*/
  89. static void oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4])
  90. {
  91. _cbmvs[0]=_lbmvs[0];
  92. _cbmvs[1]=_lbmvs[1];
  93. _cbmvs[2]=_lbmvs[2];
  94. _cbmvs[3]=_lbmvs[3];
  95. }
  96. /*A table of functions used to fill in the chroma plane motion vectors for a
  97. macro block when 4 different motion vectors are specified in the luma
  98. plane.*/
  99. const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS]={
  100. (oc_set_chroma_mvs_func)oc_set_chroma_mvs00,
  101. (oc_set_chroma_mvs_func)oc_set_chroma_mvs01,
  102. (oc_set_chroma_mvs_func)oc_set_chroma_mvs10,
  103. (oc_set_chroma_mvs_func)oc_set_chroma_mvs11
  104. };
  105. /*Returns the fragment index of the top-left block in a macro block.
  106. This can be used to test whether or not the whole macro block is valid.
  107. _sb_map: The super block map.
  108. _quadi: The quadrant number.
  109. Return: The index of the fragment of the upper left block in the macro
  110. block, or -1 if the block lies outside the coded frame.*/
  111. static int32_t oc_sb_quad_top_left_frag(oc_sb_map_quad _sb_map[4],int32_t _quadi)
  112. {
  113. /*It so happens that under the Hilbert curve ordering described below, the
  114. upper-left block in each macro block is at index 0, except in macro block
  115. 3, where it is at index 2.*/
  116. return _sb_map[_quadi][_quadi&_quadi<<1];
  117. }
  118. /*Fills in the mapping from block positions to fragment numbers for a single
  119. color plane.
  120. This function also fills in the "valid" flag of each quadrant in the super
  121. block flags.
  122. _sb_maps: The array of super block maps for the color plane.
  123. _sb_flags: The array of super block flags for the color plane.
  124. _frag0: The index of the first fragment in the plane.
  125. _hfrags: The number of horizontal fragments in a coded frame.
  126. _vfrags: The number of vertical fragments in a coded frame.*/
  127. static void oc_sb_create_plane_mapping(oc_sb_map _sb_maps[],
  128. oc_sb_flags _sb_flags[],int32_t _frag0,int32_t _hfrags,int32_t _vfrags)
  129. {
  130. /*Contains the (macro_block,block) indices for a 4x4 grid of
  131. fragments.
  132. The pattern is a 4x4 Hilbert space-filling curve.
  133. A Hilbert curve has the nice property that as the curve grows larger, its
  134. fractal dimension approaches 2.
  135. The intuition is that nearby blocks in the curve are also close spatially,
  136. with the previous element always an immediate neighbor, so that runs of
  137. blocks should be well correlated.*/
  138. static const int32_t SB_MAP[4][4][2]={
  139. {{0,0},{0,1},{3,2},{3,3}},
  140. {{0,3},{0,2},{3,1},{3,0}},
  141. {{1,0},{1,3},{2,0},{2,3}},
  142. {{1,1},{1,2},{2,1},{2,2}}
  143. };
  144. uint32_t sbi;
  145. int32_t yfrag;
  146. int32_t y;
  147. sbi=0;
  148. yfrag=_frag0;
  149. for(y=0;;y+=4)
  150. {
  151. int32_t imax;
  152. int32_t x;
  153. /*Figure out how many columns of blocks in this super block lie within the
  154. image.*/
  155. imax=_vfrags-y;
  156. if(imax>4)imax=4;
  157. else if(imax<=0)break;
  158. for(x=0;;x+=4,sbi++)
  159. {
  160. int32_t xfrag;
  161. int32_t jmax;
  162. int32_t quadi;
  163. int32_t i;
  164. /*Figure out how many rows of blocks in this super block lie within the
  165. image.*/
  166. jmax=_hfrags-x;
  167. if(jmax>4)jmax=4;
  168. else if(jmax<=0)break;
  169. /*By default, set all fragment indices to -1.*/
  170. memset(_sb_maps[sbi][0],0xFF,sizeof(_sb_maps[sbi]));
  171. /*Fill in the fragment map for this super block.*/
  172. xfrag=yfrag+x;
  173. for(i=0;i<imax;i++){
  174. int32_t j;
  175. for(j=0;j<jmax;j++)
  176. {
  177. _sb_maps[sbi][SB_MAP[i][j][0]][SB_MAP[i][j][1]]=xfrag+j;
  178. }
  179. xfrag+=_hfrags;
  180. }
  181. /*Mark which quadrants of this super block lie within the image.*/
  182. for(quadi=0;quadi<4;quadi++) {
  183. _sb_flags[sbi].quad_valid|=
  184. (oc_sb_quad_top_left_frag(_sb_maps[sbi],quadi)>=0)<<quadi;
  185. }
  186. }
  187. yfrag+=_hfrags<<2;
  188. }
  189. }
  190. /*Fills in the Y plane fragment map for a macro block given the fragment
  191. coordinates of its upper-left hand corner.
  192. _mb_map: The macro block map to fill.
  193. _fplane: The description of the Y plane.
  194. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  195. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  196. static void oc_mb_fill_ymapping(oc_mb_map_plane _mb_map[3],
  197. const oc_fragment_plane *_fplane,int32_t _xfrag0,int32_t _yfrag0)
  198. {
  199. int32_t i;
  200. int32_t j;
  201. for(i=0;i<2;i++)for(j=0;j<2;j++){
  202. _mb_map[0][i<<1|j]=(_yfrag0+i)*(int32_t)_fplane->nhfrags+_xfrag0+j;
  203. }
  204. }
  205. static void oc_mb_fill_ymapping_raster(oc_mb_map_plane _mb_map[3],
  206. const oc_fragment_plane *_fplane,int32_t _xfrag0,int32_t _yfrag0)
  207. {
  208. _mb_map[0][0]=(_yfrag0+1)*(int32_t)_fplane->nhfrags+_xfrag0;
  209. _mb_map[0][1]=(_yfrag0+1)*(int32_t)_fplane->nhfrags+_xfrag0+1;
  210. _mb_map[0][2]=(_yfrag0)*(int32_t)_fplane->nhfrags+_xfrag0;
  211. _mb_map[0][3]=(_yfrag0)*(int32_t)_fplane->nhfrags+_xfrag0+1;
  212. }
  213. /*Fills in the chroma plane fragment maps for a macro block.
  214. This version is for use with chroma decimated in the X and Y directions
  215. (4:2:0).
  216. _mb_map: The macro block map to fill.
  217. _fplanes: The descriptions of the fragment planes.
  218. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  219. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  220. static void oc_mb_fill_cmapping00(oc_mb_map_plane _mb_map[3],
  221. const oc_fragment_plane _fplanes[3],int32_t _xfrag0,int32_t _yfrag0)
  222. {
  223. int32_t fragi;
  224. _xfrag0>>=1;
  225. _yfrag0>>=1;
  226. fragi=_yfrag0*(int32_t)_fplanes[1].nhfrags+_xfrag0;
  227. _mb_map[1][0]=fragi+_fplanes[1].froffset;
  228. _mb_map[2][0]=fragi+_fplanes[2].froffset;
  229. }
  230. /*Fills in the chroma plane fragment maps for a macro block.
  231. This version is for use with chroma decimated in the Y direction.
  232. _mb_map: The macro block map to fill.
  233. _fplanes: The descriptions of the fragment planes.
  234. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  235. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  236. static void oc_mb_fill_cmapping01(oc_mb_map_plane _mb_map[3],
  237. const oc_fragment_plane _fplanes[3],int32_t _xfrag0,int32_t _yfrag0)
  238. {
  239. int32_t fragi;
  240. int32_t j;
  241. _yfrag0>>=1;
  242. fragi=_yfrag0*(int32_t)_fplanes[1].nhfrags+_xfrag0;
  243. for(j=0;j<2;j++){
  244. _mb_map[1][j]=fragi+_fplanes[1].froffset;
  245. _mb_map[2][j]=fragi+_fplanes[2].froffset;
  246. fragi++;
  247. }
  248. }
  249. /*Fills in the chroma plane fragment maps for a macro block.
  250. This version is for use with chroma decimated in the X direction (4:2:2).
  251. _mb_map: The macro block map to fill.
  252. _fplanes: The descriptions of the fragment planes.
  253. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  254. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  255. static void oc_mb_fill_cmapping10(oc_mb_map_plane _mb_map[3],
  256. const oc_fragment_plane _fplanes[3],int32_t _xfrag0,int32_t _yfrag0)
  257. {
  258. int32_t fragi;
  259. int32_t i;
  260. _xfrag0>>=1;
  261. fragi=_yfrag0*(int32_t)_fplanes[1].nhfrags+_xfrag0;
  262. for(i=0;i<2;i++){
  263. _mb_map[1][i<<1]=fragi+_fplanes[1].froffset;
  264. _mb_map[2][i<<1]=fragi+_fplanes[2].froffset;
  265. fragi+=_fplanes[1].nhfrags;
  266. }
  267. }
  268. /*Fills in the chroma plane fragment maps for a macro block.
  269. This version is for use with no chroma decimation (4:4:4).
  270. This uses the already filled-in luma plane values.
  271. _mb_map: The macro block map to fill.
  272. _fplanes: The descriptions of the fragment planes.*/
  273. static void oc_mb_fill_cmapping11(oc_mb_map_plane _mb_map[3],
  274. const oc_fragment_plane _fplanes[3])
  275. {
  276. int32_t k;
  277. for(k=0;k<4;k++){
  278. _mb_map[1][k]=_mb_map[0][k]+_fplanes[1].froffset;
  279. _mb_map[2][k]=_mb_map[0][k]+_fplanes[2].froffset;
  280. }
  281. }
  282. /*The function type used to fill in the chroma plane fragment maps for a
  283. macro block.
  284. _mb_map: The macro block map to fill.
  285. _fplanes: The descriptions of the fragment planes.
  286. _xfrag0: The X location of the upper-left hand fragment in the luma plane.
  287. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/
  288. typedef void (*oc_mb_fill_cmapping_func)(oc_mb_map_plane _mb_map[3],
  289. const oc_fragment_plane _fplanes[3],int32_t _xfrag0,int32_t _yfrag0);
  290. /*A table of functions used to fill in the chroma plane fragment maps for a
  291. macro block for each type of chrominance decimation.*/
  292. static const oc_mb_fill_cmapping_func OC_MB_FILL_CMAPPING_TABLE[4]={
  293. oc_mb_fill_cmapping00,
  294. oc_mb_fill_cmapping01,
  295. oc_mb_fill_cmapping10,
  296. (oc_mb_fill_cmapping_func)oc_mb_fill_cmapping11
  297. };
  298. /*Fills in the mapping from macro blocks to their corresponding fragment
  299. numbers in each plane.
  300. _mb_maps: The list of macro block maps.
  301. _mb_modes: The list of macro block modes; macro blocks completely outside
  302. the coded region are marked invalid.
  303. _fplanes: The descriptions of the fragment planes.
  304. _pixel_fmt: The chroma decimation type.*/
  305. static void oc_mb_create_mapping(oc_mb_map _mb_maps[], oc_mb_map _mb_maps_rater_order[],
  306. signed char _mb_modes[],const oc_fragment_plane _fplanes[3],int32_t _pixel_fmt)
  307. {
  308. oc_mb_fill_cmapping_func mb_fill_cmapping;
  309. int32_t sbi, mbi, mbx, mby, ymb, xmb;
  310. int32_t y;
  311. mb_fill_cmapping=OC_MB_FILL_CMAPPING_TABLE[_pixel_fmt];
  312. /*Loop through the luma plane super blocks.*/
  313. for(sbi=y=0;y<_fplanes[0].nvfrags;y+=4){
  314. int32_t x;
  315. for(x=0;x<_fplanes[0].nhfrags;x+=4,sbi++){
  316. int32_t ymb;
  317. /*Loop through the macro blocks in each super block in display order.*/
  318. for(ymb=0;ymb<2;ymb++){
  319. int32_t xmb;
  320. for(xmb=0;xmb<2;xmb++){
  321. uint32_t mbi;
  322. int32_t mbx;
  323. int32_t mby;
  324. mbi=sbi<<2|OC_MB_MAP[ymb][xmb];
  325. mbx=x|xmb<<1;
  326. mby=y|ymb<<1;
  327. /*Initialize fragment indices to -1.*/
  328. memset(_mb_maps[mbi],0xFF,sizeof(_mb_maps[mbi]));
  329. /*Make sure this macro block is within the encoded region.*/
  330. if(mbx>=_fplanes[0].nhfrags||mby>=_fplanes[0].nvfrags){
  331. _mb_modes[mbi]=OC_MODE_INVALID;
  332. continue;
  333. }
  334. /*Fill in the fragment indices for the luma plane.*/
  335. oc_mb_fill_ymapping(_mb_maps[mbi],_fplanes,mbx,mby);
  336. /*Fill in the fragment indices for the chroma planes.*/
  337. (*mb_fill_cmapping)(_mb_maps[mbi],_fplanes,mbx,mby);
  338. }
  339. }
  340. }
  341. }
  342. /*Loop through the luma plane super blocks.*/
  343. for(mbi=ymb=0; ymb<_fplanes[0].nvfrags;ymb+=2)
  344. {
  345. for(xmb=0; xmb<_fplanes[0].nhfrags; xmb+=2 ,mbi++)
  346. {
  347. /*Loop through the macro blocks in each super block in display order.*/
  348. mbx=xmb;
  349. mby=ymb;
  350. /*Initialize fragment indices to -1.*/
  351. memset(_mb_maps_rater_order[mbi],0xFF,sizeof(_mb_maps_rater_order[mbi]));
  352. /*Make sure this macro block is within the encoded region.*/
  353. if(mbx>=_fplanes[0].nhfrags||mby>=_fplanes[0].nvfrags)
  354. {
  355. _mb_modes[mbi]=OC_MODE_INVALID;
  356. continue;
  357. }
  358. /*Fill in the fragment indices for the luma plane.*/
  359. oc_mb_fill_ymapping_raster(_mb_maps_rater_order[mbi],_fplanes,mbx,mby);
  360. /*Fill in the fragment indices for the chroma planes.*/
  361. (*mb_fill_cmapping)(_mb_maps_rater_order[mbi],_fplanes,mbx,mby);
  362. }
  363. }
  364. }
  365. static int32_t oc_state_frarray_init(oc_theora_state *_state)
  366. {
  367. int32_t yhfrags;
  368. int32_t yvfrags;
  369. int32_t chfrags;
  370. int32_t cvfrags;
  371. int32_t yfrags;
  372. int32_t cfrags;
  373. int32_t nfrags;
  374. uint32_t yhsbs;
  375. uint32_t yvsbs;
  376. uint32_t chsbs;
  377. uint32_t cvsbs;
  378. uint32_t ysbs;
  379. uint32_t csbs;
  380. uint32_t nsbs;
  381. size_t nmbs;
  382. int32_t hdec;
  383. int32_t vdec;
  384. int32_t pli;
  385. /*Figure out the number of fragments in each plane.*/
  386. /*These parameters have already been validated to be multiples of 16.*/
  387. yhfrags=_state->info.frame_width>>3;
  388. yvfrags=_state->info.frame_height>>3;
  389. hdec=!(_state->info.pixel_fmt&1);
  390. vdec=!(_state->info.pixel_fmt&2);
  391. chfrags=yhfrags+hdec>>hdec;
  392. cvfrags=yvfrags+vdec>>vdec;
  393. yfrags=yhfrags*(int32_t)yvfrags;
  394. cfrags=chfrags*(int32_t)cvfrags;
  395. nfrags=yfrags+2*cfrags;
  396. /*Figure out the number of super blocks in each plane.*/
  397. yhsbs=yhfrags+3>>2;
  398. yvsbs=yvfrags+3>>2;
  399. chsbs=chfrags+3>>2;
  400. cvsbs=cvfrags+3>>2;
  401. ysbs=yhsbs*yvsbs;
  402. csbs=chsbs*cvsbs;
  403. nsbs=ysbs+2*csbs;
  404. nmbs=(size_t)ysbs<<2;
  405. /*Check for overflow.
  406. We support the ridiculous upper limits of the specification (1048560 by
  407. 1048560, or 3 TB frames) if the target architecture has 64-bit pointers,
  408. but for those with 32-bit pointers (or smaller!) we have to check.
  409. If the caller wants to prevent denial-of-service by imposing a more
  410. reasonable upper limit on the size of attempted allocations, they must do
  411. so themselves; we have no platform independent way to determine how much
  412. system memory there is nor an application-independent way to decide what a
  413. "reasonable" allocation is.*/
  414. if(yfrags/yhfrags!=yvfrags||2*cfrags<cfrags||nfrags<yfrags||
  415. ysbs/yhsbs!=yvsbs||2*csbs<csbs||nsbs<ysbs||nmbs>>2!=ysbs){
  416. return TH_EIMPL;
  417. }
  418. /*Initialize the fragment array.*/
  419. _state->fplanes[0].nhfrags=yhfrags;
  420. _state->fplanes[0].nvfrags=yvfrags;
  421. _state->fplanes[0].froffset=0;
  422. _state->fplanes[0].nfrags=yfrags;
  423. _state->fplanes[0].nhsbs=yhsbs;
  424. _state->fplanes[0].nvsbs=yvsbs;
  425. _state->fplanes[0].sboffset=0;
  426. _state->fplanes[0].nsbs=ysbs;
  427. _state->fplanes[1].nhfrags=_state->fplanes[2].nhfrags=chfrags;
  428. _state->fplanes[1].nvfrags=_state->fplanes[2].nvfrags=cvfrags;
  429. _state->fplanes[1].froffset=yfrags;
  430. _state->fplanes[2].froffset=yfrags+cfrags;
  431. _state->fplanes[1].nfrags=_state->fplanes[2].nfrags=cfrags;
  432. _state->fplanes[1].nhsbs=_state->fplanes[2].nhsbs=chsbs;
  433. _state->fplanes[1].nvsbs=_state->fplanes[2].nvsbs=cvsbs;
  434. _state->fplanes[1].sboffset=ysbs;
  435. _state->fplanes[2].sboffset=ysbs+csbs;
  436. _state->fplanes[1].nsbs=_state->fplanes[2].nsbs=csbs;
  437. _state->nfrags=nfrags;
  438. _state->frags=calloc(nfrags,sizeof(*_state->frags));
  439. _state->frag_eob=calloc(nfrags,sizeof(*_state->frag_eob));
  440. _state->frag_qcm=calloc(nfrags,sizeof(*_state->frag_qcm));
  441. _state->frag_mvs=malloc(nfrags*sizeof(*_state->frag_mvs));
  442. _state->nsbs=nsbs;
  443. _state->sb_maps=malloc(nsbs*sizeof(*_state->sb_maps));
  444. _state->sb_flags=calloc(nsbs,sizeof(*_state->sb_flags));
  445. _state->nhmbs=yhsbs<<1;
  446. _state->nvmbs=yvsbs<<1;
  447. _state->nmbs=nmbs;
  448. _state->mb_maps=calloc(nmbs,sizeof(*_state->mb_maps));
  449. _state->mb_maps_rater_order= calloc(nmbs,sizeof(*_state->mb_maps_rater_order));
  450. _state->mb_modes=calloc(nmbs,sizeof(*_state->mb_modes));
  451. _state->coded_fragis=malloc(nfrags*sizeof(*_state->coded_fragis));
  452. if(_state->frags==NULL||_state->frag_mvs==NULL||_state->sb_maps==NULL||
  453. _state->sb_flags==NULL||_state->mb_maps==NULL||_state->mb_modes==NULL||
  454. _state->coded_fragis==NULL){
  455. return TH_EFAULT;
  456. }
  457. /*Create the mapping from super blocks to fragments.*/
  458. for(pli=0;pli<3;pli++){
  459. oc_fragment_plane *fplane;
  460. fplane=_state->fplanes+pli;
  461. oc_sb_create_plane_mapping(_state->sb_maps+fplane->sboffset,
  462. _state->sb_flags+fplane->sboffset,fplane->froffset,
  463. fplane->nhfrags,fplane->nvfrags);
  464. }
  465. /*Create the mapping from macro blocks to fragments.*/
  466. oc_mb_create_mapping(_state->mb_maps,_state->mb_maps_rater_order, _state->mb_modes,
  467. _state->fplanes,_state->info.pixel_fmt);
  468. return 0;
  469. }
  470. static void oc_state_frarray_clear(oc_theora_state *_state)
  471. {
  472. free(_state->coded_fragis);
  473. free(_state->mb_modes);
  474. free(_state->mb_maps);
  475. free(_state->mb_maps_rater_order);
  476. free(_state->sb_flags);
  477. free(_state->sb_maps);
  478. free(_state->frag_mvs);
  479. free(_state->frag_qcm);
  480. free(_state->frag_eob);
  481. free(_state->frags);
  482. }
  483. static void oc_state_ref_bufs_clear(oc_theora_state *_state)
  484. {
  485. free(_state->frag_buf_offs);
  486. }
  487. int32_t oc_state_init(oc_theora_state *_state,const th_info *_info,int32_t _nrefs)
  488. {
  489. int32_t ret;
  490. /*First validate the parameters.*/
  491. if(_info==NULL)return TH_EFAULT;
  492. /*The width and height of the encoded frame must be multiples of 16.
  493. They must also, when divided by 16, fit into a 16-bit unsigned integer.
  494. The displayable frame offset coordinates must fit into an 8-bit unsigned
  495. integer.
  496. Note that the offset Y in the API is specified on the opposite side from
  497. how it is specified in the bitstream, because the Y axis is flipped in
  498. the bitstream.
  499. The displayable frame must fit inside the encoded frame.
  500. The color space must be one known by the encoder.*/
  501. if((_info->frame_width&0xF)||(_info->frame_height&0xF)||
  502. _info->frame_width<=0||_info->frame_width>=0x100000||
  503. _info->frame_height<=0||_info->frame_height>=0x100000||
  504. _info->pic_x+_info->pic_width>_info->frame_width||
  505. _info->pic_y+_info->pic_height>_info->frame_height||
  506. _info->pic_x>255||_info->frame_height-_info->pic_height-_info->pic_y>255||
  507. /*Note: the following <0 comparisons may generate spurious warnings on
  508. platforms where enums are unsigned.
  509. We could cast them to unsigned and just use the following >= comparison,
  510. but there are a number of compilers which will mis-optimize this.
  511. It's better to live with the spurious warnings.*/
  512. _info->colorspace<0||_info->colorspace>=TH_CS_NSPACES||
  513. _info->pixel_fmt<0||_info->pixel_fmt>=TH_PF_NFORMATS){
  514. return TH_EINVAL;
  515. }
  516. memset(_state,0,sizeof(*_state));
  517. memcpy(&_state->info,_info,sizeof(*_info));
  518. /*Invert the sense of pic_y to match Theora's right-handed coordinate
  519. system.*/
  520. _state->info.pic_y=_info->frame_height-_info->pic_height-_info->pic_y;
  521. _state->frame_type=OC_UNKWN_FRAME;
  522. _state->dct_fzig_zag=OC_FZIG_ZAG;
  523. ret=oc_state_frarray_init(_state);
  524. if(ret<0){
  525. oc_state_frarray_clear(_state);
  526. return ret;
  527. }
  528. /*If the keyframe_granule_shift is out of range, use the maximum allowable
  529. value.*/
  530. if(_info->keyframe_granule_shift<0||_info->keyframe_granule_shift>31){
  531. _state->info.keyframe_granule_shift=31;
  532. }
  533. _state->keyframe_num=0;
  534. _state->curframe_num=-1;
  535. /*3.2.0 streams mark the frame index instead of the frame count.
  536. This was changed with stream version 3.2.1 to conform to other Ogg
  537. codecs.
  538. We add an extra bias when computing granule positions for new streams.*/
  539. _state->granpos_bias=TH_VERSION_CHECK(_info,3,2,1);
  540. return 0;
  541. }
  542. void oc_state_clear(oc_theora_state *_state)
  543. {
  544. oc_state_ref_bufs_clear(_state);
  545. oc_state_frarray_clear(_state);
  546. }
  547. int64_t th_granule_frame(void *_encdec,int64_t _granpos)
  548. {
  549. oc_theora_state *state;
  550. state=(oc_theora_state *)_encdec;
  551. if(_granpos>=0){
  552. int64_t iframe;
  553. int64_t pframe;
  554. iframe=_granpos>>state->info.keyframe_granule_shift;
  555. pframe=_granpos-(iframe<<state->info.keyframe_granule_shift);
  556. /*3.2.0 streams store the frame index in the granule position.
  557. 3.2.1 and later store the frame count.
  558. We return the index, so adjust the value if we have a 3.2.1 or later
  559. stream.*/
  560. return iframe+pframe-TH_VERSION_CHECK(&state->info,3,2,1);
  561. }
  562. return -1;
  563. }
  564. #if 0
  565. double th_granule_time(void *_encdec,int64_t _granpos)
  566. {
  567. oc_theora_state *state;
  568. state=(oc_theora_state *)_encdec;
  569. if(_granpos>=0){
  570. return (th_granule_frame(_encdec, _granpos)+1)*(
  571. (double)state->info.fps_denominator/state->info.fps_numerator);
  572. }
  573. return -1;
  574. }
  575. #endif