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.

740 lines
25KB

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