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