You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

657 lines
23KB

  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/intmath.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/opt.h"
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "dwt.h"
  26. #include "snow.h"
  27. #include "snowdata.h"
  28. #include "rangecoder.h"
  29. #include "mathops.h"
  30. #include "h263.h"
  31. #undef NDEBUG
  32. #include <assert.h>
  33. void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
  34. int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
  35. int y, x;
  36. IDWTELEM * dst;
  37. for(y=0; y<b_h; y++){
  38. //FIXME ugly misuse of obmc_stride
  39. const uint8_t *obmc1= obmc + y*obmc_stride;
  40. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  41. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  42. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  43. dst = slice_buffer_get_line(sb, src_y + y);
  44. for(x=0; x<b_w; x++){
  45. int v= obmc1[x] * block[3][x + y*src_stride]
  46. +obmc2[x] * block[2][x + y*src_stride]
  47. +obmc3[x] * block[1][x + y*src_stride]
  48. +obmc4[x] * block[0][x + y*src_stride];
  49. v <<= 8 - LOG2_OBMC_MAX;
  50. if(FRAC_BITS != 8){
  51. v >>= 8 - FRAC_BITS;
  52. }
  53. if(add){
  54. v += dst[x + src_x];
  55. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  56. if(v&(~255)) v= ~(v>>31);
  57. dst8[x + y*src_stride] = v;
  58. }else{
  59. dst[x + src_x] -= v;
  60. }
  61. }
  62. }
  63. }
  64. void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
  65. int plane_index, level, orientation;
  66. for(plane_index=0; plane_index<3; plane_index++){
  67. for(level=0; level<MAX_DECOMPOSITIONS; level++){
  68. for(orientation=level ? 1:0; orientation<4; orientation++){
  69. memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
  70. }
  71. }
  72. }
  73. memset(s->header_state, MID_STATE, sizeof(s->header_state));
  74. memset(s->block_state, MID_STATE, sizeof(s->block_state));
  75. }
  76. int ff_snow_alloc_blocks(SnowContext *s){
  77. int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
  78. int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
  79. s->b_width = w;
  80. s->b_height= h;
  81. av_free(s->block);
  82. s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
  83. return 0;
  84. }
  85. static void init_qexp(void){
  86. int i;
  87. double v=128;
  88. for(i=0; i<QROOT; i++){
  89. ff_qexp[i]= lrintf(v);
  90. v *= pow(2, 1.0 / QROOT);
  91. }
  92. }
  93. static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, int stride, int b_w, int b_h, int dx, int dy){
  94. static const uint8_t weight[64]={
  95. 8,7,6,5,4,3,2,1,
  96. 7,7,0,0,0,0,0,1,
  97. 6,0,6,0,0,0,2,0,
  98. 5,0,0,5,0,3,0,0,
  99. 4,0,0,0,4,0,0,0,
  100. 3,0,0,5,0,3,0,0,
  101. 2,0,6,0,0,0,2,0,
  102. 1,7,0,0,0,0,0,1,
  103. };
  104. static const uint8_t brane[256]={
  105. 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
  106. 0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
  107. 0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
  108. 0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
  109. 0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
  110. 0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
  111. 0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
  112. 0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
  113. 0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
  114. 0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
  115. 0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
  116. 0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
  117. 0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
  118. 0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
  119. 0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
  120. 0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
  121. };
  122. static const uint8_t needs[16]={
  123. 0,1,0,0,
  124. 2,4,2,0,
  125. 0,1,0,0,
  126. 15
  127. };
  128. int x, y, b, r, l;
  129. int16_t tmpIt [64*(32+HTAPS_MAX)];
  130. uint8_t tmp2t[3][stride*(32+HTAPS_MAX)];
  131. int16_t *tmpI= tmpIt;
  132. uint8_t *tmp2= tmp2t[0];
  133. const uint8_t *hpel[11];
  134. assert(dx<16 && dy<16);
  135. r= brane[dx + 16*dy]&15;
  136. l= brane[dx + 16*dy]>>4;
  137. b= needs[l] | needs[r];
  138. if(p && !p->diag_mc)
  139. b= 15;
  140. if(b&5){
  141. for(y=0; y < b_h+HTAPS_MAX-1; y++){
  142. for(x=0; x < b_w; x++){
  143. int a_1=src[x + HTAPS_MAX/2-4];
  144. int a0= src[x + HTAPS_MAX/2-3];
  145. int a1= src[x + HTAPS_MAX/2-2];
  146. int a2= src[x + HTAPS_MAX/2-1];
  147. int a3= src[x + HTAPS_MAX/2+0];
  148. int a4= src[x + HTAPS_MAX/2+1];
  149. int a5= src[x + HTAPS_MAX/2+2];
  150. int a6= src[x + HTAPS_MAX/2+3];
  151. int am=0;
  152. if(!p || p->fast_mc){
  153. am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  154. tmpI[x]= am;
  155. am= (am+16)>>5;
  156. }else{
  157. am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
  158. tmpI[x]= am;
  159. am= (am+32)>>6;
  160. }
  161. if(am&(~255)) am= ~(am>>31);
  162. tmp2[x]= am;
  163. }
  164. tmpI+= 64;
  165. tmp2+= stride;
  166. src += stride;
  167. }
  168. src -= stride*y;
  169. }
  170. src += HTAPS_MAX/2 - 1;
  171. tmp2= tmp2t[1];
  172. if(b&2){
  173. for(y=0; y < b_h; y++){
  174. for(x=0; x < b_w+1; x++){
  175. int a_1=src[x + (HTAPS_MAX/2-4)*stride];
  176. int a0= src[x + (HTAPS_MAX/2-3)*stride];
  177. int a1= src[x + (HTAPS_MAX/2-2)*stride];
  178. int a2= src[x + (HTAPS_MAX/2-1)*stride];
  179. int a3= src[x + (HTAPS_MAX/2+0)*stride];
  180. int a4= src[x + (HTAPS_MAX/2+1)*stride];
  181. int a5= src[x + (HTAPS_MAX/2+2)*stride];
  182. int a6= src[x + (HTAPS_MAX/2+3)*stride];
  183. int am=0;
  184. if(!p || p->fast_mc)
  185. am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
  186. else
  187. am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
  188. if(am&(~255)) am= ~(am>>31);
  189. tmp2[x]= am;
  190. }
  191. src += stride;
  192. tmp2+= stride;
  193. }
  194. src -= stride*y;
  195. }
  196. src += stride*(HTAPS_MAX/2 - 1);
  197. tmp2= tmp2t[2];
  198. tmpI= tmpIt;
  199. if(b&4){
  200. for(y=0; y < b_h; y++){
  201. for(x=0; x < b_w; x++){
  202. int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
  203. int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
  204. int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
  205. int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
  206. int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
  207. int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
  208. int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
  209. int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
  210. int am=0;
  211. if(!p || p->fast_mc)
  212. am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
  213. else
  214. am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
  215. if(am&(~255)) am= ~(am>>31);
  216. tmp2[x]= am;
  217. }
  218. tmpI+= 64;
  219. tmp2+= stride;
  220. }
  221. }
  222. hpel[ 0]= src;
  223. hpel[ 1]= tmp2t[0] + stride*(HTAPS_MAX/2-1);
  224. hpel[ 2]= src + 1;
  225. hpel[ 4]= tmp2t[1];
  226. hpel[ 5]= tmp2t[2];
  227. hpel[ 6]= tmp2t[1] + 1;
  228. hpel[ 8]= src + stride;
  229. hpel[ 9]= hpel[1] + stride;
  230. hpel[10]= hpel[8] + 1;
  231. if(b==15){
  232. const uint8_t *src1= hpel[dx/8 + dy/8*4 ];
  233. const uint8_t *src2= hpel[dx/8 + dy/8*4+1];
  234. const uint8_t *src3= hpel[dx/8 + dy/8*4+4];
  235. const uint8_t *src4= hpel[dx/8 + dy/8*4+5];
  236. dx&=7;
  237. dy&=7;
  238. for(y=0; y < b_h; y++){
  239. for(x=0; x < b_w; x++){
  240. dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
  241. (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
  242. }
  243. src1+=stride;
  244. src2+=stride;
  245. src3+=stride;
  246. src4+=stride;
  247. dst +=stride;
  248. }
  249. }else{
  250. const uint8_t *src1= hpel[l];
  251. const uint8_t *src2= hpel[r];
  252. int a= weight[((dx&7) + (8*(dy&7)))];
  253. int b= 8-a;
  254. for(y=0; y < b_h; y++){
  255. for(x=0; x < b_w; x++){
  256. dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
  257. }
  258. src1+=stride;
  259. src2+=stride;
  260. dst +=stride;
  261. }
  262. }
  263. }
  264. void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
  265. if(block->type & BLOCK_INTRA){
  266. int x, y;
  267. const unsigned color = block->color[plane_index];
  268. const unsigned color4 = color*0x01010101;
  269. if(b_w==32){
  270. for(y=0; y < b_h; y++){
  271. *(uint32_t*)&dst[0 + y*stride]= color4;
  272. *(uint32_t*)&dst[4 + y*stride]= color4;
  273. *(uint32_t*)&dst[8 + y*stride]= color4;
  274. *(uint32_t*)&dst[12+ y*stride]= color4;
  275. *(uint32_t*)&dst[16+ y*stride]= color4;
  276. *(uint32_t*)&dst[20+ y*stride]= color4;
  277. *(uint32_t*)&dst[24+ y*stride]= color4;
  278. *(uint32_t*)&dst[28+ y*stride]= color4;
  279. }
  280. }else if(b_w==16){
  281. for(y=0; y < b_h; y++){
  282. *(uint32_t*)&dst[0 + y*stride]= color4;
  283. *(uint32_t*)&dst[4 + y*stride]= color4;
  284. *(uint32_t*)&dst[8 + y*stride]= color4;
  285. *(uint32_t*)&dst[12+ y*stride]= color4;
  286. }
  287. }else if(b_w==8){
  288. for(y=0; y < b_h; y++){
  289. *(uint32_t*)&dst[0 + y*stride]= color4;
  290. *(uint32_t*)&dst[4 + y*stride]= color4;
  291. }
  292. }else if(b_w==4){
  293. for(y=0; y < b_h; y++){
  294. *(uint32_t*)&dst[0 + y*stride]= color4;
  295. }
  296. }else{
  297. for(y=0; y < b_h; y++){
  298. for(x=0; x < b_w; x++){
  299. dst[x + y*stride]= color;
  300. }
  301. }
  302. }
  303. }else{
  304. uint8_t *src= s->last_picture[block->ref].data[plane_index];
  305. const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
  306. int mx= block->mx*scale;
  307. int my= block->my*scale;
  308. const int dx= mx&15;
  309. const int dy= my&15;
  310. const int tab_index= 3 - (b_w>>2) + (b_w>>4);
  311. sx += (mx>>4) - (HTAPS_MAX/2-1);
  312. sy += (my>>4) - (HTAPS_MAX/2-1);
  313. src += sx + sy*stride;
  314. if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)
  315. || (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){
  316. s->dsp.emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);
  317. src= tmp + MB_SIZE;
  318. }
  319. // assert(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h);
  320. // assert(!(b_w&(b_w-1)));
  321. assert(b_w>1 && b_h>1);
  322. assert((tab_index>=0 && tab_index<4) || b_w==32);
  323. if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )
  324. mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
  325. else if(b_w==32){
  326. int y;
  327. for(y=0; y<b_h; y+=16){
  328. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
  329. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
  330. }
  331. }else if(b_w==b_h)
  332. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
  333. else if(b_w==2*b_h){
  334. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
  335. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
  336. }else{
  337. assert(2*b_w==b_h);
  338. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
  339. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
  340. }
  341. }
  342. }
  343. #define mca(dx,dy,b_w)\
  344. static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, int stride, int h){\
  345. assert(h==b_w);\
  346. mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, stride, b_w, b_w, dx, dy);\
  347. }
  348. mca( 0, 0,16)
  349. mca( 8, 0,16)
  350. mca( 0, 8,16)
  351. mca( 8, 8,16)
  352. mca( 0, 0,8)
  353. mca( 8, 0,8)
  354. mca( 0, 8,8)
  355. mca( 8, 8,8)
  356. av_cold int ff_snow_common_init(AVCodecContext *avctx){
  357. SnowContext *s = avctx->priv_data;
  358. int width, height;
  359. int i, j;
  360. s->avctx= avctx;
  361. s->max_ref_frames=1; //just make sure its not an invalid value in case of no initial keyframe
  362. ff_dsputil_init(&s->dsp, avctx);
  363. ff_dwt_init(&s->dwt);
  364. #define mcf(dx,dy)\
  365. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  366. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  367. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  368. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  369. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  370. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  371. mcf( 0, 0)
  372. mcf( 4, 0)
  373. mcf( 8, 0)
  374. mcf(12, 0)
  375. mcf( 0, 4)
  376. mcf( 4, 4)
  377. mcf( 8, 4)
  378. mcf(12, 4)
  379. mcf( 0, 8)
  380. mcf( 4, 8)
  381. mcf( 8, 8)
  382. mcf(12, 8)
  383. mcf( 0,12)
  384. mcf( 4,12)
  385. mcf( 8,12)
  386. mcf(12,12)
  387. #define mcfh(dx,dy)\
  388. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  389. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  390. mc_block_hpel ## dx ## dy ## 16;\
  391. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  392. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  393. mc_block_hpel ## dx ## dy ## 8;
  394. mcfh(0, 0)
  395. mcfh(8, 0)
  396. mcfh(0, 8)
  397. mcfh(8, 8)
  398. init_qexp();
  399. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  400. width= s->avctx->width;
  401. height= s->avctx->height;
  402. s->spatial_idwt_buffer= av_mallocz(width*height*sizeof(IDWTELEM));
  403. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM)); //FIXME this does not belong here
  404. s->temp_dwt_buffer = av_mallocz(width * sizeof(DWTELEM));
  405. s->temp_idwt_buffer = av_mallocz(width * sizeof(IDWTELEM));
  406. for(i=0; i<MAX_REF_FRAMES; i++)
  407. for(j=0; j<MAX_REF_FRAMES; j++)
  408. ff_scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  409. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  410. s->scratchbuf = av_malloc(s->mconly_picture.linesize[0]*7*MB_SIZE);
  411. return 0;
  412. }
  413. int ff_snow_common_init_after_header(AVCodecContext *avctx) {
  414. SnowContext *s = avctx->priv_data;
  415. int plane_index, level, orientation;
  416. for(plane_index=0; plane_index<3; plane_index++){
  417. int w= s->avctx->width;
  418. int h= s->avctx->height;
  419. if(plane_index){
  420. w>>= s->chroma_h_shift;
  421. h>>= s->chroma_v_shift;
  422. }
  423. s->plane[plane_index].width = w;
  424. s->plane[plane_index].height= h;
  425. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  426. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  427. SubBand *b= &s->plane[plane_index].band[level][orientation];
  428. b->buf= s->spatial_dwt_buffer;
  429. b->level= level;
  430. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  431. b->width = (w + !(orientation&1))>>1;
  432. b->height= (h + !(orientation>1))>>1;
  433. b->stride_line = 1 << (s->spatial_decomposition_count - level);
  434. b->buf_x_offset = 0;
  435. b->buf_y_offset = 0;
  436. if(orientation&1){
  437. b->buf += (w+1)>>1;
  438. b->buf_x_offset = (w+1)>>1;
  439. }
  440. if(orientation>1){
  441. b->buf += b->stride>>1;
  442. b->buf_y_offset = b->stride_line >> 1;
  443. }
  444. b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
  445. if(level)
  446. b->parent= &s->plane[plane_index].band[level-1][orientation];
  447. //FIXME avoid this realloc
  448. av_freep(&b->x_coeff);
  449. b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
  450. }
  451. w= (w+1)>>1;
  452. h= (h+1)>>1;
  453. }
  454. }
  455. return 0;
  456. }
  457. #define USE_HALFPEL_PLANE 0
  458. static void halfpel_interpol(SnowContext *s, uint8_t *halfpel[4][4], AVFrame *frame){
  459. int p,x,y;
  460. for(p=0; p<3; p++){
  461. int is_chroma= !!p;
  462. int w= s->avctx->width >>is_chroma;
  463. int h= s->avctx->height >>is_chroma;
  464. int ls= frame->linesize[p];
  465. uint8_t *src= frame->data[p];
  466. halfpel[1][p] = (uint8_t*) av_malloc(ls * (h + 2 * EDGE_WIDTH)) + EDGE_WIDTH * (1 + ls);
  467. halfpel[2][p] = (uint8_t*) av_malloc(ls * (h + 2 * EDGE_WIDTH)) + EDGE_WIDTH * (1 + ls);
  468. halfpel[3][p] = (uint8_t*) av_malloc(ls * (h + 2 * EDGE_WIDTH)) + EDGE_WIDTH * (1 + ls);
  469. halfpel[0][p]= src;
  470. for(y=0; y<h; y++){
  471. for(x=0; x<w; x++){
  472. int i= y*ls + x;
  473. halfpel[1][p][i]= (20*(src[i] + src[i+1]) - 5*(src[i-1] + src[i+2]) + (src[i-2] + src[i+3]) + 16 )>>5;
  474. }
  475. }
  476. for(y=0; y<h; y++){
  477. for(x=0; x<w; x++){
  478. int i= y*ls + x;
  479. halfpel[2][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  480. }
  481. }
  482. src= halfpel[1][p];
  483. for(y=0; y<h; y++){
  484. for(x=0; x<w; x++){
  485. int i= y*ls + x;
  486. halfpel[3][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
  487. }
  488. }
  489. //FIXME border!
  490. }
  491. }
  492. void ff_snow_release_buffer(AVCodecContext *avctx)
  493. {
  494. SnowContext *s = avctx->priv_data;
  495. int i;
  496. if(s->last_picture[s->max_ref_frames-1].data[0]){
  497. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  498. for(i=0; i<9; i++)
  499. if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3])
  500. av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture.linesize[i%3]));
  501. }
  502. }
  503. int ff_snow_frame_start(SnowContext *s){
  504. AVFrame tmp;
  505. int w= s->avctx->width; //FIXME round up to x16 ?
  506. int h= s->avctx->height;
  507. if (s->current_picture.data[0] && !(s->avctx->flags&CODEC_FLAG_EMU_EDGE)) {
  508. s->dsp.draw_edges(s->current_picture.data[0],
  509. s->current_picture.linesize[0], w , h ,
  510. EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  511. s->dsp.draw_edges(s->current_picture.data[1],
  512. s->current_picture.linesize[1], w>>1, h>>1,
  513. EDGE_WIDTH/2, EDGE_WIDTH/2, EDGE_TOP | EDGE_BOTTOM);
  514. s->dsp.draw_edges(s->current_picture.data[2],
  515. s->current_picture.linesize[2], w>>1, h>>1,
  516. EDGE_WIDTH/2, EDGE_WIDTH/2, EDGE_TOP | EDGE_BOTTOM);
  517. }
  518. ff_snow_release_buffer(s->avctx);
  519. tmp= s->last_picture[s->max_ref_frames-1];
  520. memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
  521. memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
  522. if(USE_HALFPEL_PLANE && s->current_picture.data[0])
  523. halfpel_interpol(s, s->halfpel_plane[0], &s->current_picture);
  524. s->last_picture[0]= s->current_picture;
  525. s->current_picture= tmp;
  526. if(s->keyframe){
  527. s->ref_frames= 0;
  528. }else{
  529. int i;
  530. for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
  531. if(i && s->last_picture[i-1].key_frame)
  532. break;
  533. s->ref_frames= i;
  534. if(s->ref_frames==0){
  535. av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
  536. return -1;
  537. }
  538. }
  539. s->current_picture.reference= 1;
  540. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  541. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  542. return -1;
  543. }
  544. s->current_picture.key_frame= s->keyframe;
  545. return 0;
  546. }
  547. av_cold void ff_snow_common_end(SnowContext *s)
  548. {
  549. int plane_index, level, orientation, i;
  550. av_freep(&s->spatial_dwt_buffer);
  551. av_freep(&s->temp_dwt_buffer);
  552. av_freep(&s->spatial_idwt_buffer);
  553. av_freep(&s->temp_idwt_buffer);
  554. s->m.me.temp= NULL;
  555. av_freep(&s->m.me.scratchpad);
  556. av_freep(&s->m.me.map);
  557. av_freep(&s->m.me.score_map);
  558. av_freep(&s->m.obmc_scratchpad);
  559. av_freep(&s->block);
  560. av_freep(&s->scratchbuf);
  561. for(i=0; i<MAX_REF_FRAMES; i++){
  562. av_freep(&s->ref_mvs[i]);
  563. av_freep(&s->ref_scores[i]);
  564. if(s->last_picture[i].data[0])
  565. s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
  566. }
  567. for(plane_index=0; plane_index<3; plane_index++){
  568. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  569. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  570. SubBand *b= &s->plane[plane_index].band[level][orientation];
  571. av_freep(&b->x_coeff);
  572. }
  573. }
  574. }
  575. if (s->mconly_picture.data[0])
  576. s->avctx->release_buffer(s->avctx, &s->mconly_picture);
  577. if (s->current_picture.data[0])
  578. s->avctx->release_buffer(s->avctx, &s->current_picture);
  579. }