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.

688 lines
24KB

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