FFmpeg
snow.c
Go to the documentation of this file.
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 
21 #include "libavutil/log.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/thread.h"
24 #include "avcodec.h"
25 #include "snow_dwt.h"
26 #include "snow.h"
27 #include "snowdata.h"
28 
29 
30 void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
31  int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
32  int y, x;
33  IDWTELEM * dst;
34  for(y=0; y<b_h; y++){
35  //FIXME ugly misuse of obmc_stride
36  const uint8_t *obmc1= obmc + y*obmc_stride;
37  const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
38  const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
39  const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
40  dst = slice_buffer_get_line(sb, src_y + y);
41  for(x=0; x<b_w; x++){
42  int v= obmc1[x] * block[3][x + y*src_stride]
43  +obmc2[x] * block[2][x + y*src_stride]
44  +obmc3[x] * block[1][x + y*src_stride]
45  +obmc4[x] * block[0][x + y*src_stride];
46 
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 
63 void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
64  int plane_index, level, orientation;
65 
66  for(plane_index=0; plane_index<3; plane_index++){
68  for(orientation=level ? 1:0; orientation<4; orientation++){
69  memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
70  }
71  }
72  }
73  memset(s->header_state, MID_STATE, sizeof(s->header_state));
74  memset(s->block_state, MID_STATE, sizeof(s->block_state));
75 }
76 
78  int w= AV_CEIL_RSHIFT(s->avctx->width, LOG2_MB_SIZE);
79  int h= AV_CEIL_RSHIFT(s->avctx->height, LOG2_MB_SIZE);
80 
81  s->b_width = w;
82  s->b_height= h;
83 
84  av_free(s->block);
85  s->block = av_calloc(w * h, sizeof(*s->block) << (s->block_max_depth*2));
86  if (!s->block)
87  return AVERROR(ENOMEM);
88 
89  return 0;
90 }
91 
92 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){
93  static const uint8_t weight[64]={
94  8,7,6,5,4,3,2,1,
95  7,7,0,0,0,0,0,1,
96  6,0,6,0,0,0,2,0,
97  5,0,0,5,0,3,0,0,
98  4,0,0,0,4,0,0,0,
99  3,0,0,5,0,3,0,0,
100  2,0,6,0,0,0,2,0,
101  1,7,0,0,0,0,0,1,
102  };
103 
104  static const uint8_t brane[256]={
105  0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
106  0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
107  0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
108  0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
109  0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
110  0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
111  0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
112  0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
113  0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
114  0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
115  0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
116  0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
117  0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
118  0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
119  0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
120  0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
121  };
122 
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 
130  int x, y, b, r, l;
131  int16_t tmpIt [64*(32+HTAPS_MAX)];
132  uint8_t tmp2t[3][64*(32+HTAPS_MAX)];
133  int16_t *tmpI= tmpIt;
134  uint8_t *tmp2= tmp2t[0];
135  const uint8_t *hpel[11];
136  av_assert2(dx<16 && dy<16);
137  r= brane[dx + 16*dy]&15;
138  l= brane[dx + 16*dy]>>4;
139 
140  b= needs[l] | needs[r];
141  if(p && !p->diag_mc)
142  b= 15;
143 
144  if(b&5){
145  for(y=0; y < b_h+HTAPS_MAX-1; y++){
146  for(x=0; x < b_w; x++){
147  int a_1=src[x + HTAPS_MAX/2-4];
148  int a0= src[x + HTAPS_MAX/2-3];
149  int a1= src[x + HTAPS_MAX/2-2];
150  int a2= src[x + HTAPS_MAX/2-1];
151  int a3= src[x + HTAPS_MAX/2+0];
152  int a4= src[x + HTAPS_MAX/2+1];
153  int a5= src[x + HTAPS_MAX/2+2];
154  int a6= src[x + HTAPS_MAX/2+3];
155  int am=0;
156  if(!p || p->fast_mc){
157  am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
158  tmpI[x]= am;
159  am= (am+16)>>5;
160  }else{
161  am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
162  tmpI[x]= am;
163  am= (am+32)>>6;
164  }
165 
166  if(am&(~255)) am= ~(am>>31);
167  tmp2[x]= am;
168  }
169  tmpI+= 64;
170  tmp2+= 64;
171  src += stride;
172  }
173  src -= stride*y;
174  }
175  src += HTAPS_MAX/2 - 1;
176  tmp2= tmp2t[1];
177 
178  if(b&2){
179  for(y=0; y < b_h; y++){
180  for(x=0; x < b_w+1; x++){
181  int a_1=src[x + (HTAPS_MAX/2-4)*stride];
182  int a0= src[x + (HTAPS_MAX/2-3)*stride];
183  int a1= src[x + (HTAPS_MAX/2-2)*stride];
184  int a2= src[x + (HTAPS_MAX/2-1)*stride];
185  int a3= src[x + (HTAPS_MAX/2+0)*stride];
186  int a4= src[x + (HTAPS_MAX/2+1)*stride];
187  int a5= src[x + (HTAPS_MAX/2+2)*stride];
188  int a6= src[x + (HTAPS_MAX/2+3)*stride];
189  int am=0;
190  if(!p || p->fast_mc)
191  am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
192  else
193  am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
194 
195  if(am&(~255)) am= ~(am>>31);
196  tmp2[x]= am;
197  }
198  src += stride;
199  tmp2+= 64;
200  }
201  src -= stride*y;
202  }
203  src += stride*(HTAPS_MAX/2 - 1);
204  tmp2= tmp2t[2];
205  tmpI= tmpIt;
206  if(b&4){
207  for(y=0; y < b_h; y++){
208  for(x=0; x < b_w; x++){
209  int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
210  int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
211  int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
212  int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
213  int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
214  int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
215  int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
216  int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
217  int am=0;
218  if(!p || p->fast_mc)
219  am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
220  else
221  am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
222  if(am&(~255)) am= ~(am>>31);
223  tmp2[x]= am;
224  }
225  tmpI+= 64;
226  tmp2+= 64;
227  }
228  }
229 
230  hpel[ 0]= src;
231  hpel[ 1]= tmp2t[0] + 64*(HTAPS_MAX/2-1);
232  hpel[ 2]= src + 1;
233 
234  hpel[ 4]= tmp2t[1];
235  hpel[ 5]= tmp2t[2];
236  hpel[ 6]= tmp2t[1] + 1;
237 
238  hpel[ 8]= src + stride;
239  hpel[ 9]= hpel[1] + 64;
240  hpel[10]= hpel[8] + 1;
241 
242 #define MC_STRIDE(x) (needs[x] ? 64 : stride)
243 
244  if(b==15){
245  int dxy = dx / 8 + dy / 8 * 4;
246  const uint8_t *src1 = hpel[dxy ];
247  const uint8_t *src2 = hpel[dxy + 1];
248  const uint8_t *src3 = hpel[dxy + 4];
249  const uint8_t *src4 = hpel[dxy + 5];
250  int stride1 = MC_STRIDE(dxy);
251  int stride2 = MC_STRIDE(dxy + 1);
252  int stride3 = MC_STRIDE(dxy + 4);
253  int stride4 = MC_STRIDE(dxy + 5);
254  dx&=7;
255  dy&=7;
256  for(y=0; y < b_h; y++){
257  for(x=0; x < b_w; x++){
258  dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
259  (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
260  }
261  src1+=stride1;
262  src2+=stride2;
263  src3+=stride3;
264  src4+=stride4;
265  dst +=stride;
266  }
267  }else{
268  const uint8_t *src1= hpel[l];
269  const uint8_t *src2= hpel[r];
270  int stride1 = MC_STRIDE(l);
271  int stride2 = MC_STRIDE(r);
272  int a= weight[((dx&7) + (8*(dy&7)))];
273  int b= 8-a;
274  for(y=0; y < b_h; y++){
275  for(x=0; x < b_w; x++){
276  dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
277  }
278  src1+=stride1;
279  src2+=stride2;
280  dst +=stride;
281  }
282  }
283 }
284 
285 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){
286  if(block->type & BLOCK_INTRA){
287  int x, y;
288  const unsigned color = block->color[plane_index];
289  const unsigned color4 = color*0x01010101;
290  if(b_w==32){
291  for(y=0; y < b_h; y++){
292  *(uint32_t*)&dst[0 + y*stride]= color4;
293  *(uint32_t*)&dst[4 + y*stride]= color4;
294  *(uint32_t*)&dst[8 + y*stride]= color4;
295  *(uint32_t*)&dst[12+ y*stride]= color4;
296  *(uint32_t*)&dst[16+ y*stride]= color4;
297  *(uint32_t*)&dst[20+ y*stride]= color4;
298  *(uint32_t*)&dst[24+ y*stride]= color4;
299  *(uint32_t*)&dst[28+ y*stride]= color4;
300  }
301  }else if(b_w==16){
302  for(y=0; y < b_h; y++){
303  *(uint32_t*)&dst[0 + y*stride]= color4;
304  *(uint32_t*)&dst[4 + y*stride]= color4;
305  *(uint32_t*)&dst[8 + y*stride]= color4;
306  *(uint32_t*)&dst[12+ y*stride]= color4;
307  }
308  }else if(b_w==8){
309  for(y=0; y < b_h; y++){
310  *(uint32_t*)&dst[0 + y*stride]= color4;
311  *(uint32_t*)&dst[4 + y*stride]= color4;
312  }
313  }else if(b_w==4){
314  for(y=0; y < b_h; y++){
315  *(uint32_t*)&dst[0 + y*stride]= color4;
316  }
317  }else{
318  for(y=0; y < b_h; y++){
319  for(x=0; x < b_w; x++){
320  dst[x + y*stride]= color;
321  }
322  }
323  }
324  }else{
325  const uint8_t *src = s->last_picture[block->ref]->data[plane_index];
326  const int scale= plane_index ? (2*s->mv_scale)>>s->chroma_h_shift : 2*s->mv_scale;
327  int mx= block->mx*scale;
328  int my= block->my*scale;
329  const int dx= mx&15;
330  const int dy= my&15;
331  const int tab_index= 3 - (b_w>>2) + (b_w>>4);
332  sx += (mx>>4) - (HTAPS_MAX/2-1);
333  sy += (my>>4) - (HTAPS_MAX/2-1);
334  src += sx + sy*stride;
335  if( (unsigned)sx >= FFMAX(w - b_w - (HTAPS_MAX-2), 0)
336  || (unsigned)sy >= FFMAX(h - b_h - (HTAPS_MAX-2), 0)){
337  s->vdsp.emulated_edge_mc(tmp + MB_SIZE, src,
338  stride, stride,
339  b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1,
340  sx, sy, w, h);
341  src= tmp + MB_SIZE;
342  }
343 
344  av_assert2(s->chroma_h_shift == s->chroma_v_shift); // only one mv_scale
345 
346  av_assert2((tab_index>=0 && tab_index<4) || b_w==32);
347  if( (dx&3) || (dy&3)
348  || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h)
349  || (b_w&(b_w-1))
350  || b_w == 1
351  || b_h == 1
352  || !s->plane[plane_index].fast_mc )
353  mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
354  else if(b_w==32){
355  int y;
356  for(y=0; y<b_h; y+=16){
357  s->h264qpel.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
358  s->h264qpel.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
359  }
360  }else if(b_w==b_h)
361  s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
362  else if(b_w==2*b_h){
363  s->h264qpel.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
364  s->h264qpel.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
365  }else{
366  av_assert2(2*b_w==b_h);
367  s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
368  s->h264qpel.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
369  }
370  }
371 }
372 
373 #define mca(dx,dy,b_w)\
374 static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, ptrdiff_t stride, int h){\
375  av_assert2(h==b_w);\
376  mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, stride, b_w, b_w, dx, dy);\
377 }
378 
379 mca( 0, 0,16)
380 mca( 8, 0,16)
381 mca( 0, 8,16)
382 mca( 8, 8,16)
383 mca( 0, 0,8)
384 mca( 8, 0,8)
385 mca( 0, 8,8)
386 mca( 8, 8,8)
387 
388 static av_cold void snow_static_init(void)
389 {
390  for (int i = 0; i < MAX_REF_FRAMES; i++)
391  for (int j = 0; j < MAX_REF_FRAMES; j++)
392  ff_scale_mv_ref[i][j] = 256 * (i + 1) / (j + 1);
393 }
394 
396  static AVOnce init_static_once = AV_ONCE_INIT;
397  SnowContext *s = avctx->priv_data;
398  int width, height;
399  int i;
400 
401  s->avctx= avctx;
402  s->max_ref_frames=1; //just make sure it's not an invalid value in case of no initial keyframe
403  s->spatial_decomposition_count = 1;
404 
405  ff_videodsp_init(&s->vdsp, 8);
406  ff_dwt_init(&s->dwt);
407  ff_h264qpel_init(&s->h264qpel, 8);
408 
409 #define mcfh(dx,dy)\
410  s->hdsp.put_pixels_tab [0][dy/4+dx/8]=\
411  s->hdsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
412  mc_block_hpel ## dx ## dy ## 16;\
413  s->hdsp.put_pixels_tab [1][dy/4+dx/8]=\
414  s->hdsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
415  mc_block_hpel ## dx ## dy ## 8;
416 
417  mcfh(0, 0)
418  mcfh(8, 0)
419  mcfh(0, 8)
420  mcfh(8, 8)
421 
422 // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
423 
424  width= s->avctx->width;
425  height= s->avctx->height;
426 
427  if (!FF_ALLOCZ_TYPED_ARRAY(s->spatial_idwt_buffer, width * height) ||
428  !FF_ALLOCZ_TYPED_ARRAY(s->spatial_dwt_buffer, width * height) || //FIXME this does not belong here
429  !FF_ALLOCZ_TYPED_ARRAY(s->temp_dwt_buffer, width) ||
430  !FF_ALLOCZ_TYPED_ARRAY(s->temp_idwt_buffer, width) ||
431  !FF_ALLOCZ_TYPED_ARRAY(s->run_buffer, ((width + 1) >> 1) * ((height + 1) >> 1)))
432  return AVERROR(ENOMEM);
433 
434  for(i=0; i<MAX_REF_FRAMES; i++) {
435  s->last_picture[i] = av_frame_alloc();
436  if (!s->last_picture[i])
437  return AVERROR(ENOMEM);
438  }
439 
440  s->mconly_picture = av_frame_alloc();
441  s->current_picture = av_frame_alloc();
442  if (!s->mconly_picture || !s->current_picture)
443  return AVERROR(ENOMEM);
444 
445  ff_thread_once(&init_static_once, snow_static_init);
446 
447  return 0;
448 }
449 
451  SnowContext *s = avctx->priv_data;
452  int plane_index, level, orientation;
453 
454  if(!s->scratchbuf) {
455  int emu_buf_size;
456  emu_buf_size = FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
457  if (!FF_ALLOCZ_TYPED_ARRAY(s->scratchbuf, FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * 7 * MB_SIZE) ||
458  !FF_ALLOCZ_TYPED_ARRAY(s->emu_edge_buffer, emu_buf_size))
459  return AVERROR(ENOMEM);
460  }
461 
462  for(plane_index=0; plane_index < s->nb_planes; plane_index++){
463  int w= s->avctx->width;
464  int h= s->avctx->height;
465 
466  if(plane_index){
467  w = AV_CEIL_RSHIFT(w, s->chroma_h_shift);
468  h = AV_CEIL_RSHIFT(h, s->chroma_v_shift);
469  }
470  s->plane[plane_index].width = w;
471  s->plane[plane_index].height= h;
472 
473  for(level=s->spatial_decomposition_count-1; level>=0; level--){
474  for(orientation=level ? 1 : 0; orientation<4; orientation++){
475  SubBand *b= &s->plane[plane_index].band[level][orientation];
476 
477  b->buf= s->spatial_dwt_buffer;
478  b->level= level;
479  b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
480  b->width = (w + !(orientation&1))>>1;
481  b->height= (h + !(orientation>1))>>1;
482 
483  b->stride_line = 1 << (s->spatial_decomposition_count - level);
484  b->buf_x_offset = 0;
485  b->buf_y_offset = 0;
486 
487  if(orientation&1){
488  b->buf += (w+1)>>1;
489  b->buf_x_offset = (w+1)>>1;
490  }
491  if(orientation>1){
492  b->buf += b->stride>>1;
493  b->buf_y_offset = b->stride_line >> 1;
494  }
495  b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
496 
497  if(level)
498  b->parent= &s->plane[plane_index].band[level-1][orientation];
499  //FIXME avoid this realloc
500  av_freep(&b->x_coeff);
501  b->x_coeff = av_calloc((b->width + 1) * b->height + 1,
502  sizeof(*b->x_coeff));
503  if (!b->x_coeff)
504  return AVERROR(ENOMEM);
505  }
506  w= (w+1)>>1;
507  h= (h+1)>>1;
508  }
509  }
510 
511  return 0;
512 }
513 
515 {
516  SnowContext *s = avctx->priv_data;
517 
518  if(s->last_picture[s->max_ref_frames-1]->data[0]){
519  av_frame_unref(s->last_picture[s->max_ref_frames-1]);
520  }
521 }
522 
524 {
525  AVFrame *tmp;
526 
527  ff_snow_release_buffer(s->avctx);
528 
529  tmp= s->last_picture[s->max_ref_frames-1];
530  for (int i = s->max_ref_frames - 1; i > 0; i--)
531  s->last_picture[i] = s->last_picture[i-1];
532  s->last_picture[0] = s->current_picture;
533  s->current_picture = tmp;
534 
535  if(s->keyframe){
536  s->ref_frames= 0;
537  s->current_picture->flags |= AV_FRAME_FLAG_KEY;
538  }else{
539  int i;
540  for(i=0; i<s->max_ref_frames && s->last_picture[i]->data[0]; i++)
541  if(i && (s->last_picture[i-1]->flags & AV_FRAME_FLAG_KEY))
542  break;
543  s->ref_frames= i;
544  if(s->ref_frames==0){
545  av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
546  return AVERROR_INVALIDDATA;
547  }
548  s->current_picture->flags &= ~AV_FRAME_FLAG_KEY;
549  }
550 
551  return 0;
552 }
553 
555 {
556  int plane_index, level, orientation, i;
557 
558  av_freep(&s->spatial_dwt_buffer);
559  av_freep(&s->temp_dwt_buffer);
560  av_freep(&s->spatial_idwt_buffer);
561  av_freep(&s->temp_idwt_buffer);
562  av_freep(&s->run_buffer);
563 
564  av_freep(&s->block);
565  av_freep(&s->scratchbuf);
566  av_freep(&s->emu_edge_buffer);
567 
568  for(i=0; i<MAX_REF_FRAMES; i++){
569  if(s->last_picture[i] && s->last_picture[i]->data[0]) {
570  av_assert0(s->last_picture[i]->data[0] != s->current_picture->data[0]);
571  }
572  av_frame_free(&s->last_picture[i]);
573  }
574 
575  for(plane_index=0; plane_index < MAX_PLANES; plane_index++){
576  for(level=MAX_DECOMPOSITIONS-1; level>=0; level--){
577  for(orientation=level ? 1 : 0; orientation<4; orientation++){
578  SubBand *b= &s->plane[plane_index].band[level][orientation];
579 
580  av_freep(&b->x_coeff);
581  }
582  }
583  }
584  av_frame_free(&s->mconly_picture);
585  av_frame_free(&s->current_picture);
586 }
FF_ALLOCZ_TYPED_ARRAY
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition: internal.h:78
MC_STRIDE
#define MC_STRIDE(x)
level
uint8_t level
Definition: svq3.c:205
MAX_DECOMPOSITIONS
#define MAX_DECOMPOSITIONS
Definition: dirac_dwt.h:30
r
const char * r
Definition: vf_curves.c:127
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
color
Definition: vf_paletteuse.c:512
Plane::fast_mc
int fast_mc
Definition: snow.h:106
MID_STATE
#define MID_STATE
Definition: snow.h:39
thread.h
src1
const pixel * src1
Definition: h264pred_template.c:421
LOG2_MB_SIZE
#define LOG2_MB_SIZE
Definition: snow.h:72
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
w
uint8_t w
Definition: llviddspenc.c:38
b
#define b
Definition: input.c:41
mcfh
#define mcfh(dx, dy)
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
SnowContext
Definition: snow.h:113
MAX_REF_FRAMES
#define MAX_REF_FRAMES
Definition: snow.h:46
ff_snow_common_end
av_cold void ff_snow_common_end(SnowContext *s)
Definition: snow.c:554
Plane::diag_mc
int diag_mc
Definition: snow.h:105
LOG2_OBMC_MAX
#define LOG2_OBMC_MAX
Definition: snow.h:48
BlockNode
Definition: snow.h:50
ff_videodsp_init
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:39
ff_snow_pred_block
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)
Definition: snow.c:285
ff_h264qpel_init
av_cold void ff_h264qpel_init(H264QpelContext *c, int bit_depth)
Definition: h264qpel.c:49
ff_dwt_init
av_cold void ff_dwt_init(SnowDWTContext *c)
Definition: snow_dwt.c:851
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:148
a1
#define a1
Definition: regdef.h:47
ff_snow_common_init_after_header
int ff_snow_common_init_after_header(AVCodecContext *avctx)
Definition: snow.c:450
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:198
MAX_PLANES
#define MAX_PLANES
Definition: ffv1.h:43
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:59
mc_block
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)
Definition: snow.c:92
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
a4
#define a4
Definition: regdef.h:50
ff_snow_common_init
av_cold int ff_snow_common_init(AVCodecContext *avctx)
Definition: snow.c:395
ff_snow_inner_add_yblock
void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t **block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer *sb, int add, uint8_t *dst8)
Definition: snow.c:30
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
snow.h
ff_snow_release_buffer
void ff_snow_release_buffer(AVCodecContext *avctx)
Definition: snow.c:514
snowdata.h
AVOnce
#define AVOnce
Definition: thread.h:202
weight
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1563
MB_SIZE
#define MB_SIZE
Definition: cinepakenc.c:54
obmc4
static const uint8_t obmc4[16]
Definition: snowdata.h:96
color
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
SubBand
Definition: cfhd.h:108
height
#define height
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
snow_dwt.h
a0
#define a0
Definition: regdef.h:46
Plane::hcoeff
int8_t hcoeff[HTAPS_MAX/2]
Definition: snow.h:104
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
src2
const pixel * src2
Definition: h264pred_template.c:422
BLOCK_INTRA
#define BLOCK_INTRA
Intra block, inter otherwise.
Definition: snow.h:57
a2
#define a2
Definition: regdef.h:48
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:606
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
ff_snow_reset_contexts
void ff_snow_reset_contexts(SnowContext *s)
Definition: snow.c:63
AVCodecContext
main external API structure.
Definition: avcodec.h:445
a5
#define a5
Definition: regdef.h:51
FRAC_BITS
#define FRAC_BITS
Definition: g729postfilter.c:36
Plane
Definition: cfhd.h:117
slice_buffer_get_line
#define slice_buffer_get_line(slice_buf, line_num)
Definition: snow_dwt.h:91
mem.h
ff_scale_mv_ref
int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES]
Definition: snowdata.h:135
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
ff_snow_frames_prepare
int ff_snow_frames_prepare(SnowContext *s)
Definition: snow.c:523
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
HTAPS_MAX
#define HTAPS_MAX
Definition: snow.h:75
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
IDWTELEM
short IDWTELEM
Definition: dirac_dwt.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
ff_snow_alloc_blocks
int ff_snow_alloc_blocks(SnowContext *s)
Definition: snow.c:77
a3
#define a3
Definition: regdef.h:49
mca
#define mca(dx, dy, b_w)
Definition: snow.c:373