snow.c 20 KB

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