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 <assert.h>
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/thread.h"
27 #include "avcodec.h"
28 #include "snow_dwt.h"
29 #include "snow.h"
30 #include "snowdata.h"
31 
32 #define pixeltmp int16_t
33 #define BIT_DEPTH 8
34 #define SNOW
35 #include "h264qpel_template.c"
36 
37 static void put_snow_qpel2_h_lowpass_8(uint8_t *dst, const uint8_t *restrict src, int dstStride, int srcStride)
38 {
39  const int h = 2;
40  for (int i = 0; i < h; ++i) {
41  dst[0] = av_clip_uint8(((src[0]+src[1])*20 - (src[-1]+src[2])*5 + (src[-2]+src[3]) + 16) >> 5);
42  dst[1] = av_clip_uint8(((src[1]+src[2])*20 - (src[0 ]+src[3])*5 + (src[-1]+src[4]) + 16) >> 5);
43  dst += dstStride;
44  src += srcStride;
45  }
46 }
47 
48 static void put_snow_qpel2_v_lowpass_8(uint8_t *dst, const uint8_t *restrict src, int dstStride, int srcStride)
49 {
50  const int w = 2;
51  for (int i = 0; i < w; ++i) {
52  const int srcB = src[-2*srcStride];
53  const int srcA = src[-1*srcStride];
54  const int src0 = src[0 *srcStride];
55  const int src1 = src[1 *srcStride];
56  const int src2 = src[2 *srcStride];
57  const int src3 = src[3 *srcStride];
58  const int src4 = src[4 *srcStride];
59  dst[0*dstStride] = av_clip_uint8(((src0+src1)*20 - (srcA+src2)*5 + (srcB+src3) + 16) >> 5);
60  dst[1*dstStride] = av_clip_uint8(((src1+src2)*20 - (src0+src3)*5 + (srcA+src4) + 16) >> 5);
61  dst++;
62  src++;
63  }
64 }
65 
66 static void put_snow_qpel2_hv_lowpass_8(uint8_t *dst, pixeltmp *tmp, const uint8_t *restrict src, int dstStride, int tmpStride, int srcStride)
67 {
68  const int h = 2;
69  const int w = 2;
70  src -= 2*srcStride;
71  for (int i = 0; i < h + 5; ++i) {
72  tmp[0] = (src[0]+src[1])*20 - (src[-1]+src[2])*5 + (src[-2]+src[3]);
73  tmp[1] = (src[1]+src[2])*20 - (src[0 ]+src[3])*5 + (src[-1]+src[4]);
74  tmp += tmpStride;
75  src += srcStride;
76  }
77  tmp -= tmpStride*(h+5-2);
78  for (int i = 0; i < w; ++i) {
79  const int tmpB = tmp[-2*tmpStride];
80  const int tmpA = tmp[-1*tmpStride];
81  const int tmp0 = tmp[0 *tmpStride];
82  const int tmp1 = tmp[1 *tmpStride];
83  const int tmp2 = tmp[2 *tmpStride];
84  const int tmp3 = tmp[3 *tmpStride];
85  const int tmp4 = tmp[4 *tmpStride];
86  dst[0*dstStride] = av_clip_uint8(((tmp0+tmp1)*20 - (tmpA+tmp2)*5 + (tmpB+tmp3) + 512) >> 10);
87  dst[1*dstStride] = av_clip_uint8(((tmp1+tmp2)*20 - (tmp0+tmp3)*5 + (tmpA+tmp4) + 512) >> 10);
88  dst++;
89  tmp++;
90  }
91 }
92 
93 H264_MC(put_, snow, 2)
94 
95 static av_cold void init_qpel(SnowContext *const s)
96 {
97  static_assert(offsetof(H264QpelContext, put_h264_qpel_pixels_tab) == 0,
98  "put_h264_qpel_pixels_tab not at start of H264QpelContext");
99  ff_h264qpel_init(&s->h264qpel, 8);
100  s->put_snow_qpel_pixels_tab[3][0] = put_snow_qpel2_mc00_8_c;
101  s->put_snow_qpel_pixels_tab[3][1] = put_snow_qpel2_mc10_8_c;
102  s->put_snow_qpel_pixels_tab[3][2] = put_snow_qpel2_mc20_8_c;
103  s->put_snow_qpel_pixels_tab[3][3] = put_snow_qpel2_mc30_8_c;
104  s->put_snow_qpel_pixels_tab[3][4] = put_snow_qpel2_mc01_8_c;
105  s->put_snow_qpel_pixels_tab[3][5] = put_snow_qpel2_mc11_8_c;
106  s->put_snow_qpel_pixels_tab[3][6] = put_snow_qpel2_mc21_8_c;
107  s->put_snow_qpel_pixels_tab[3][7] = put_snow_qpel2_mc31_8_c;
108  s->put_snow_qpel_pixels_tab[3][8] = put_snow_qpel2_mc02_8_c;
109  s->put_snow_qpel_pixels_tab[3][9] = put_snow_qpel2_mc12_8_c;
110  s->put_snow_qpel_pixels_tab[3][10] = put_snow_qpel2_mc22_8_c;
111  s->put_snow_qpel_pixels_tab[3][11] = put_snow_qpel2_mc32_8_c;
112  s->put_snow_qpel_pixels_tab[3][12] = put_snow_qpel2_mc03_8_c;
113  s->put_snow_qpel_pixels_tab[3][13] = put_snow_qpel2_mc13_8_c;
114  s->put_snow_qpel_pixels_tab[3][14] = put_snow_qpel2_mc23_8_c;
115  s->put_snow_qpel_pixels_tab[3][15] = put_snow_qpel2_mc33_8_c;
116 }
117 
118 void ff_snow_inner_add_yblock_c(const uint8_t *obmc, const int obmc_stride, uint8_t **block, int b_w, int b_h,
119  int src_x, int src_stride, IDWTELEM *const *lines, int add, uint8_t *dst8)
120 {
121  int y, x;
122 
123  av_assume(add); // add == 0 is currently unused
124 
125  for(y=0; y<b_h; y++){
126  //FIXME ugly misuse of obmc_stride
127  const uint8_t *obmc1= obmc + y*obmc_stride;
128  const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
129  const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
130  const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
131  IDWTELEM *dst = lines[y];
132  av_assert2(dst);
133 
134  for(x=0; x<b_w; x++){
135  int v= obmc1[x] * block[3][x + y*src_stride]
136  +obmc2[x] * block[2][x + y*src_stride]
137  +obmc3[x] * block[1][x + y*src_stride]
138  +obmc4[x] * block[0][x + y*src_stride];
139 
140 #if FRAC_BITS > LOG2_OBMC_MAX
141  v <<= FRAC_BITS - LOG2_OBMC_MAX;
142 #elif FRAC_BITS < LOG2_OBMC_MAX
143  v >>= LOG2_OBMC_MAX - FRAC_BITS;
144 #endif
145  if(add){
146  v += dst[x + src_x];
147  v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
148  if(v&(~255)) v= ~(v>>31);
149  dst8[x + y*src_stride] = v;
150  }else{
151  dst[x + src_x] -= v;
152  }
153  }
154  }
155 }
156 
157 void ff_snow_reset_contexts(SnowContext *s){ //FIXME better initial contexts
158  int plane_index, level, orientation;
159 
160  for(plane_index=0; plane_index<3; plane_index++){
161  for(level=0; level<MAX_DECOMPOSITIONS; level++){
162  for(orientation=level ? 1:0; orientation<4; orientation++){
163  memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
164  }
165  }
166  }
167  memset(s->header_state, MID_STATE, sizeof(s->header_state));
168  memset(s->block_state, MID_STATE, sizeof(s->block_state));
169 }
170 
172  int w= AV_CEIL_RSHIFT(s->avctx->width, LOG2_MB_SIZE);
173  int h= AV_CEIL_RSHIFT(s->avctx->height, LOG2_MB_SIZE);
174 
175  s->b_width = w;
176  s->b_height= h;
177 
178  av_free(s->block);
179  s->block = av_calloc(w * h, sizeof(*s->block) << (s->block_max_depth*2));
180  if (!s->block)
181  return AVERROR(ENOMEM);
182 
183  return 0;
184 }
185 
186 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){
187  static const uint8_t weight[64]={
188  8,7,6,5,4,3,2,1,
189  7,7,0,0,0,0,0,1,
190  6,0,6,0,0,0,2,0,
191  5,0,0,5,0,3,0,0,
192  4,0,0,0,4,0,0,0,
193  3,0,0,5,0,3,0,0,
194  2,0,6,0,0,0,2,0,
195  1,7,0,0,0,0,0,1,
196  };
197 
198  static const uint8_t brane[256]={
199  0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
200  0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
201  0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
202  0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
203  0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
204  0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
205  0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
206  0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
207  0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
208  0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
209  0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
210  0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
211  0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
212  0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
213  0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
214  0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
215  };
216 
217  static const uint8_t needs[16]={
218  0,1,0,0,
219  2,4,2,0,
220  0,1,0,0,
221  15
222  };
223 
224  int x, y, b, r, l;
225  int16_t tmpIt [64*(32+HTAPS_MAX)];
226  uint8_t tmp2t[3][64*(32+HTAPS_MAX)];
227  int16_t *tmpI= tmpIt;
228  uint8_t *tmp2= tmp2t[0];
229  const uint8_t *hpel[11];
230  av_assert2(dx<16 && dy<16);
231  r= brane[dx + 16*dy]&15;
232  l= brane[dx + 16*dy]>>4;
233 
234  b= needs[l] | needs[r];
235  if(p && !p->diag_mc)
236  b= 15;
237 
238  if(b&5){
239  for(y=0; y < b_h+HTAPS_MAX-1; y++){
240  for(x=0; x < b_w; x++){
241  int a_1=src[x + HTAPS_MAX/2-4];
242  int a0= src[x + HTAPS_MAX/2-3];
243  int a1= src[x + HTAPS_MAX/2-2];
244  int a2= src[x + HTAPS_MAX/2-1];
245  int a3= src[x + HTAPS_MAX/2+0];
246  int a4= src[x + HTAPS_MAX/2+1];
247  int a5= src[x + HTAPS_MAX/2+2];
248  int a6= src[x + HTAPS_MAX/2+3];
249  int am=0;
250  if(!p || p->fast_mc){
251  am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
252  tmpI[x]= am;
253  am= (am+16)>>5;
254  }else{
255  am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
256  tmpI[x]= am;
257  am= (am+32)>>6;
258  }
259 
260  if(am&(~255)) am= ~(am>>31);
261  tmp2[x]= am;
262  }
263  tmpI+= 64;
264  tmp2+= 64;
265  src += stride;
266  }
267  src -= stride*y;
268  }
269  src += HTAPS_MAX/2 - 1;
270  tmp2= tmp2t[1];
271 
272  if(b&2){
273  for(y=0; y < b_h; y++){
274  for(x=0; x < b_w+1; x++){
275  int a_1=src[x + (HTAPS_MAX/2-4)*stride];
276  int a0= src[x + (HTAPS_MAX/2-3)*stride];
277  int a1= src[x + (HTAPS_MAX/2-2)*stride];
278  int a2= src[x + (HTAPS_MAX/2-1)*stride];
279  int a3= src[x + (HTAPS_MAX/2+0)*stride];
280  int a4= src[x + (HTAPS_MAX/2+1)*stride];
281  int a5= src[x + (HTAPS_MAX/2+2)*stride];
282  int a6= src[x + (HTAPS_MAX/2+3)*stride];
283  int am=0;
284  if(!p || p->fast_mc)
285  am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
286  else
287  am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
288 
289  if(am&(~255)) am= ~(am>>31);
290  tmp2[x]= am;
291  }
292  src += stride;
293  tmp2+= 64;
294  }
295  src -= stride*y;
296  }
297  src += stride*(HTAPS_MAX/2 - 1);
298  tmp2= tmp2t[2];
299  tmpI= tmpIt;
300  if(b&4){
301  for(y=0; y < b_h; y++){
302  for(x=0; x < b_w; x++){
303  int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
304  int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
305  int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
306  int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
307  int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
308  int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
309  int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
310  int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
311  int am=0;
312  if(!p || p->fast_mc)
313  am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
314  else
315  am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
316  if(am&(~255)) am= ~(am>>31);
317  tmp2[x]= am;
318  }
319  tmpI+= 64;
320  tmp2+= 64;
321  }
322  }
323 
324  hpel[ 0]= src;
325  hpel[ 1]= tmp2t[0] + 64*(HTAPS_MAX/2-1);
326  hpel[ 2]= src + 1;
327 
328  hpel[ 4]= tmp2t[1];
329  hpel[ 5]= tmp2t[2];
330  hpel[ 6]= tmp2t[1] + 1;
331 
332  hpel[ 8]= src + stride;
333  hpel[ 9]= hpel[1] + 64;
334  hpel[10]= hpel[8] + 1;
335 
336 #define MC_STRIDE(x) (needs[x] ? 64 : stride)
337 
338  if(b==15){
339  int dxy = dx / 8 + dy / 8 * 4;
340  const uint8_t *src1 = hpel[dxy ];
341  const uint8_t *src2 = hpel[dxy + 1];
342  const uint8_t *src3 = hpel[dxy + 4];
343  const uint8_t *src4 = hpel[dxy + 5];
344  int stride1 = MC_STRIDE(dxy);
345  int stride2 = MC_STRIDE(dxy + 1);
346  int stride3 = MC_STRIDE(dxy + 4);
347  int stride4 = MC_STRIDE(dxy + 5);
348  dx&=7;
349  dy&=7;
350  for(y=0; y < b_h; y++){
351  for(x=0; x < b_w; x++){
352  dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
353  (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
354  }
355  src1+=stride1;
356  src2+=stride2;
357  src3+=stride3;
358  src4+=stride4;
359  dst +=stride;
360  }
361  }else{
362  const uint8_t *src1= hpel[l];
363  const uint8_t *src2= hpel[r];
364  int stride1 = MC_STRIDE(l);
365  int stride2 = MC_STRIDE(r);
366  int a= weight[((dx&7) + (8*(dy&7)))];
367  int b= 8-a;
368  for(y=0; y < b_h; y++){
369  for(x=0; x < b_w; x++){
370  dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
371  }
372  src1+=stride1;
373  src2+=stride2;
374  dst +=stride;
375  }
376  }
377 }
378 
379 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){
380  if(block->type & BLOCK_INTRA){
381  int x, y;
382  const unsigned color = block->color[plane_index];
383  const unsigned color4 = color*0x01010101;
384  if(b_w==32){
385  for(y=0; y < b_h; y++){
386  *(uint32_t*)&dst[0 + y*stride]= color4;
387  *(uint32_t*)&dst[4 + y*stride]= color4;
388  *(uint32_t*)&dst[8 + y*stride]= color4;
389  *(uint32_t*)&dst[12+ y*stride]= color4;
390  *(uint32_t*)&dst[16+ y*stride]= color4;
391  *(uint32_t*)&dst[20+ y*stride]= color4;
392  *(uint32_t*)&dst[24+ y*stride]= color4;
393  *(uint32_t*)&dst[28+ y*stride]= color4;
394  }
395  }else if(b_w==16){
396  for(y=0; y < b_h; y++){
397  *(uint32_t*)&dst[0 + y*stride]= color4;
398  *(uint32_t*)&dst[4 + y*stride]= color4;
399  *(uint32_t*)&dst[8 + y*stride]= color4;
400  *(uint32_t*)&dst[12+ y*stride]= color4;
401  }
402  }else if(b_w==8){
403  for(y=0; y < b_h; y++){
404  *(uint32_t*)&dst[0 + y*stride]= color4;
405  *(uint32_t*)&dst[4 + y*stride]= color4;
406  }
407  }else if(b_w==4){
408  for(y=0; y < b_h; y++){
409  *(uint32_t*)&dst[0 + y*stride]= color4;
410  }
411  }else{
412  for(y=0; y < b_h; y++){
413  for(x=0; x < b_w; x++){
414  dst[x + y*stride]= color;
415  }
416  }
417  }
418  }else{
419  const uint8_t *src = s->last_picture[block->ref]->data[plane_index];
420  const int scale= plane_index ? (2*s->mv_scale)>>s->chroma_h_shift : 2*s->mv_scale;
421  int mx= block->mx*scale;
422  int my= block->my*scale;
423  const int dx= mx&15;
424  const int dy= my&15;
425  const int tab_index= 3 - (b_w>>2) + (b_w>>4);
426  sx += (mx>>4) - (HTAPS_MAX/2-1);
427  sy += (my>>4) - (HTAPS_MAX/2-1);
428  src += sx + sy*stride;
429  if( (unsigned)sx >= FFMAX(w - b_w - (HTAPS_MAX-2), 0)
430  || (unsigned)sy >= FFMAX(h - b_h - (HTAPS_MAX-2), 0)){
431  s->vdsp.emulated_edge_mc(tmp + MB_SIZE, src,
432  stride, stride,
433  b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1,
434  sx, sy, w, h);
435  src= tmp + MB_SIZE;
436  }
437 
438  av_assert2(s->chroma_h_shift == s->chroma_v_shift); // only one mv_scale
439 
440  av_assert2((tab_index>=0 && tab_index<4) || b_w==32);
441  if( (dx&3) || (dy&3)
442  || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h)
443  || (b_w&(b_w-1))
444  || b_w == 1
445  || b_h == 1
446  || !s->plane[plane_index].fast_mc )
447  mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
448  else if(b_w==32){
449  int y;
450  for(y=0; y<b_h; y+=16){
451  s->put_snow_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
452  s->put_snow_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
453  }
454  }else if(b_w==b_h)
455  s->put_snow_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
456  else if(b_w==2*b_h){
457  s->put_snow_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
458  s->put_snow_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
459  }else{
460  av_assert2(2*b_w==b_h);
461  s->put_snow_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
462  s->put_snow_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
463  }
464  }
465 }
466 
467 #define mca(dx,dy,b_w)\
468 static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, ptrdiff_t stride, int h){\
469  av_assert2(h==b_w);\
470  mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, stride, b_w, b_w, dx, dy);\
471 }
472 
473 mca( 8, 0,16)
474 mca( 0, 8,16)
475 mca( 8, 8,16)
476 mca( 8, 0,8)
477 mca( 0, 8,8)
478 mca( 8, 8,8)
479 
480 static av_cold void snow_static_init(void)
481 {
482  for (int i = 0; i < MAX_REF_FRAMES; i++)
483  for (int j = 0; j < MAX_REF_FRAMES; j++)
484  ff_scale_mv_ref[i][j] = 256 * (i + 1) / (j + 1);
485 }
486 
488  static AVOnce init_static_once = AV_ONCE_INIT;
489  SnowContext *s = avctx->priv_data;
490  int width, height;
491  int i;
492 
493  s->avctx= avctx;
494  s->max_ref_frames=1; //just make sure it's not an invalid value in case of no initial keyframe
495  s->spatial_decomposition_count = 1;
496 
497  ff_videodsp_init(&s->vdsp, 8);
498  ff_dwt_init(&s->dwt);
499 
500  init_qpel(s);
501 
502 #define mcfh(dx,dy)\
503  s->hdsp.put_pixels_tab [0][dy/4+dx/8]=\
504  s->hdsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
505  mc_block_hpel ## dx ## dy ## 16;\
506  s->hdsp.put_pixels_tab [1][dy/4+dx/8]=\
507  s->hdsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
508  mc_block_hpel ## dx ## dy ## 8;
509 
510  mcfh(8, 0)
511  mcfh(0, 8)
512  mcfh(8, 8)
513 
514 // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
515 
516  width= s->avctx->width;
517  height= s->avctx->height;
518 
519  if (!FF_ALLOCZ_TYPED_ARRAY(s->spatial_idwt_buffer, width * height) ||
520  !FF_ALLOCZ_TYPED_ARRAY(s->spatial_dwt_buffer, width * height) || //FIXME this does not belong here
521  !FF_ALLOCZ_TYPED_ARRAY(s->temp_dwt_buffer, width) ||
522  !FF_ALLOCZ_TYPED_ARRAY(s->temp_idwt_buffer, width) ||
523  !FF_ALLOCZ_TYPED_ARRAY(s->run_buffer, ((width + 1) >> 1) * ((height + 1) >> 1) + 1))
524  return AVERROR(ENOMEM);
525 
526  for(i=0; i<MAX_REF_FRAMES; i++) {
527  s->last_picture[i] = av_frame_alloc();
528  if (!s->last_picture[i])
529  return AVERROR(ENOMEM);
530  }
531 
532  s->mconly_picture = av_frame_alloc();
533  s->current_picture = av_frame_alloc();
534  if (!s->mconly_picture || !s->current_picture)
535  return AVERROR(ENOMEM);
536 
537  ff_thread_once(&init_static_once, snow_static_init);
538 
539  return 0;
540 }
541 
543  SnowContext *s = avctx->priv_data;
544  int plane_index, level, orientation;
545 
546  if(!s->scratchbuf) {
547  if (!FF_ALLOCZ_TYPED_ARRAY(s->scratchbuf, FFMAX(s->current_picture->linesize[0], 2*avctx->width+256) * 7 * MB_SIZE))
548  return AVERROR(ENOMEM);
549  }
550 
551  for(plane_index=0; plane_index < s->nb_planes; plane_index++){
552  int w= s->avctx->width;
553  int h= s->avctx->height;
554 
555  if(plane_index){
556  w = AV_CEIL_RSHIFT(w, s->chroma_h_shift);
557  h = AV_CEIL_RSHIFT(h, s->chroma_v_shift);
558  }
559  s->plane[plane_index].width = w;
560  s->plane[plane_index].height= h;
561 
562  for(level=s->spatial_decomposition_count-1; level>=0; level--){
563  for(orientation=level ? 1 : 0; orientation<4; orientation++){
564  SubBand *b= &s->plane[plane_index].band[level][orientation];
565 
566  b->buf= s->spatial_dwt_buffer;
567  b->level= level;
568  b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
569  b->width = (w + !(orientation&1))>>1;
570  b->height= (h + !(orientation>1))>>1;
571 
572  b->stride_line = 1 << (s->spatial_decomposition_count - level);
573  b->buf_x_offset = 0;
574  b->buf_y_offset = 0;
575 
576  if(orientation&1){
577  b->buf += (w+1)>>1;
578  b->buf_x_offset = (w+1)>>1;
579  }
580  if(orientation>1){
581  b->buf += b->stride>>1;
582  b->buf_y_offset = b->stride_line >> 1;
583  }
584  b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
585 
586  if(level)
587  b->parent= &s->plane[plane_index].band[level-1][orientation];
588  //FIXME avoid this realloc
589  av_freep(&b->x_coeff);
590  b->x_coeff = av_calloc((b->width + 1) * b->height + 1,
591  sizeof(*b->x_coeff));
592  if (!b->x_coeff)
593  return AVERROR(ENOMEM);
594  }
595  w= (w+1)>>1;
596  h= (h+1)>>1;
597  }
598  }
599 
600  return 0;
601 }
602 
604 {
605  AVFrame *tmp;
606 
607  tmp= s->last_picture[s->max_ref_frames-1];
608  for (int i = s->max_ref_frames - 1; i > 0; i--)
609  s->last_picture[i] = s->last_picture[i-1];
610  s->last_picture[0] = s->current_picture;
611  s->current_picture = tmp;
612 
613  av_frame_unref(s->current_picture);
614 
615  if(s->keyframe){
616  s->ref_frames= 0;
617  s->current_picture->flags |= AV_FRAME_FLAG_KEY;
618  }else{
619  int i;
620  for(i=0; i<s->max_ref_frames && s->last_picture[i]->data[0]; i++)
621  if(i && (s->last_picture[i-1]->flags & AV_FRAME_FLAG_KEY))
622  break;
623  s->ref_frames= i;
624  if(s->ref_frames==0){
625  av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
626  return AVERROR_INVALIDDATA;
627  }
628  s->current_picture->flags &= ~AV_FRAME_FLAG_KEY;
629  }
630 
631  return 0;
632 }
633 
635 {
636  int plane_index, level, orientation, i;
637 
638  av_freep(&s->spatial_dwt_buffer);
639  av_freep(&s->temp_dwt_buffer);
640  av_freep(&s->spatial_idwt_buffer);
641  av_freep(&s->temp_idwt_buffer);
642  av_freep(&s->run_buffer);
643 
644  av_freep(&s->block);
645  av_freep(&s->scratchbuf);
646 
647  for(i=0; i<MAX_REF_FRAMES; i++){
648  av_frame_free(&s->last_picture[i]);
649  }
650 
651  for(plane_index=0; plane_index < MAX_PLANES; plane_index++){
652  for(level=MAX_DECOMPOSITIONS-1; level>=0; level--){
653  for(orientation=level ? 1 : 0; orientation<4; orientation++){
654  SubBand *b= &s->plane[plane_index].band[level][orientation];
655 
656  av_freep(&b->x_coeff);
657  }
658  }
659  }
660  av_frame_free(&s->mconly_picture);
661  av_frame_free(&s->current_picture);
662 }
FF_ALLOCZ_TYPED_ARRAY
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition: internal.h:78
put_snow_qpel2_v_lowpass_8
static void put_snow_qpel2_v_lowpass_8(uint8_t *dst, const uint8_t *restrict src, int dstStride, int srcStride)
Definition: snow.c:48
MC_STRIDE
#define MC_STRIDE(x)
level
uint8_t level
Definition: svq3.c:208
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
h264qpel_template.c
color
Definition: vf_paletteuse.c:513
MID_STATE
#define MID_STATE
Definition: snow.h:39
thread.h
src1
const pixel * src1
Definition: h264pred_template.c:420
LOG2_MB_SIZE
#define LOG2_MB_SIZE
Definition: snow.h:72
av_cold
#define av_cold
Definition: attributes.h:119
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:466
b
#define b
Definition: input.c:43
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
put_snow_qpel2_hv_lowpass_8
static void put_snow_qpel2_hv_lowpass_8(uint8_t *dst, pixeltmp *tmp, const uint8_t *restrict src, int dstStride, int tmpStride, int srcStride)
Definition: snow.c:66
ff_snow_common_end
av_cold void ff_snow_common_end(SnowContext *s)
Definition: snow.c:634
mx
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition: dsp.h:57
LOG2_OBMC_MAX
#define LOG2_OBMC_MAX
Definition: snow.h:48
BlockNode
Definition: snow.h:50
weight
const h264_weight_func weight
Definition: h264dsp_init.c:33
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:379
ff_h264qpel_init
av_cold void ff_h264qpel_init(H264QpelContext *c, int bit_depth)
Definition: h264qpel.c:50
a2
static double a2(void *priv, double x, double y)
Definition: vf_xfade.c:2030
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:52
ff_snow_common_init_after_header
int ff_snow_common_init_after_header(AVCodecContext *avctx)
Definition: snow.c:542
avassert.h
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:210
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:681
s
#define s(width, name)
Definition: cbs_vp9.c:198
MAX_PLANES
#define MAX_PLANES
Definition: ffv1.h:44
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
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:186
ff_snow_common_init
av_cold int ff_snow_common_init(AVCodecContext *avctx)
Definition: snow.c:487
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
my
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition: dsp.h:57
av_assume
#define av_assume(cond)
Definition: avassert.h:119
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
a3
static double a3(void *priv, double x, double y)
Definition: vf_xfade.c:2031
snow.h
snowdata.h
AVOnce
#define AVOnce
Definition: thread.h:202
init_qpel
static av_cold void init_qpel(SnowContext *const s)
Definition: snow.c:95
MB_SIZE
#define MB_SIZE
Definition: cinepakenc.c:54
height
#define height
Definition: dsp.h:89
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
obmc4
static const uint8_t obmc4[16]
Definition: snowdata.h:97
color
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:98
SubBand
Definition: cfhd.h:116
pixeltmp
#define pixeltmp
Definition: snow.c:32
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
a0
static double a0(void *priv, double x, double y)
Definition: vf_xfade.c:2028
snow_dwt.h
put_snow_qpel2_h_lowpass_8
static void put_snow_qpel2_h_lowpass_8(uint8_t *dst, const uint8_t *restrict src, int dstStride, int srcStride)
Definition: snow.c:37
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:68
log.h
src2
const pixel * src2
Definition: h264pred_template.c:421
BLOCK_INTRA
#define BLOCK_INTRA
Intra block, inter otherwise.
Definition: snow.h:57
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
H264QpelContext
Definition: h264qpel.h:27
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
avcodec.h
ff_snow_inner_add_yblock_c
void ff_snow_inner_add_yblock_c(const uint8_t *obmc, const int obmc_stride, uint8_t **block, int b_w, int b_h, int src_x, int src_stride, IDWTELEM *const *lines, int add, uint8_t *dst8)
Definition: snow.c:118
H264_MC
#define H264_MC(OPNAME, NAME, SIZE)
Definition: h264qpel_template.c:307
ff_snow_reset_contexts
void ff_snow_reset_contexts(SnowContext *s)
Definition: snow.c:157
AVCodecContext
main external API structure.
Definition: avcodec.h:443
FRAC_BITS
#define FRAC_BITS
Definition: g729postfilter.c:36
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
Plane
Definition: cfhd.h:125
av_clip_uint8
#define av_clip_uint8
Definition: common.h:106
src0
const pixel *const src0
Definition: h264pred_template.c:419
mem.h
ff_scale_mv_ref
int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES]
Definition: snowdata.h:136
w
uint8_t w
Definition: llvidencdsp.c:39
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:278
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:470
ff_snow_frames_prepare
int ff_snow_frames_prepare(SnowContext *s)
Definition: snow.c:603
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:604
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
a1
static double a1(void *priv, double x, double y)
Definition: vf_xfade.c:2029
h
h
Definition: vp9dsp_template.c:2070
stride
#define stride
Definition: h264pred_template.c:536
width
#define width
Definition: dsp.h:89
ff_snow_alloc_blocks
int ff_snow_alloc_blocks(SnowContext *s)
Definition: snow.c:171
mca
#define mca(dx, dy, b_w)
Definition: snow.c:467
src
#define src
Definition: vp8dsp.c:248