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.

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