FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
snow.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (C) 2006 Robert Edele <yartrebo@earthlink.net>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef AVCODEC_SNOW_H
23 #define AVCODEC_SNOW_H
24 
25 #include "dsputil.h"
26 #include "dwt.h"
27 
28 #include "rangecoder.h"
29 #include "mathops.h"
30 #include "mpegvideo.h"
31 
32 #define MID_STATE 128
33 
34 #define MAX_PLANES 4
35 #define QSHIFT 5
36 #define QROOT (1<<QSHIFT)
37 #define LOSSLESS_QLOG -128
38 #define FRAC_BITS 4
39 #define MAX_REF_FRAMES 8
40 
41 #define LOG2_OBMC_MAX 8
42 #define OBMC_MAX (1<<(LOG2_OBMC_MAX))
43 typedef struct BlockNode{
44  int16_t mx;
45  int16_t my;
49 //#define TYPE_SPLIT 1
50 #define BLOCK_INTRA 1
51 #define BLOCK_OPT 2
52 //#define TYPE_NOCOLOR 4
53  uint8_t level; //FIXME merge into type?
54 }BlockNode;
55 
56 static const BlockNode null_block= { //FIXME add border maybe
57  .color= {128,128,128},
58  .mx= 0,
59  .my= 0,
60  .ref= 0,
61  .type= 0,
62  .level= 0,
63 };
64 
65 #define LOG2_MB_SIZE 4
66 #define MB_SIZE (1<<LOG2_MB_SIZE)
67 #define ENCODER_EXTRA_BITS 4
68 #define HTAPS_MAX 8
69 
70 typedef struct x_and_coeff{
71  int16_t x;
72  uint16_t coeff;
73 } x_and_coeff;
74 
75 typedef struct SubBand{
76  int level;
77  int stride;
78  int width;
79  int height;
80  int qlog; ///< log(qscale)/log[2^(1/6)]
82  IDWTELEM *ibuf;
85  int stride_line; ///< Stride measured in lines, not pixels.
87  struct SubBand *parent;
88  uint8_t state[/*7*2*/ 7 + 512][32];
89 }SubBand;
90 
91 typedef struct Plane{
92  int width;
93  int height;
95 
96  int htaps;
97  int8_t hcoeff[HTAPS_MAX/2];
98  int diag_mc;
99  int fast_mc;
100 
104 }Plane;
105 
106 typedef struct SnowContext{
107  AVClass *class;
114  AVFrame input_picture; ///< new_picture with the internal linesizes
119 // uint8_t q_context[16];
121  uint8_t block_state[128 + 32*128];
122  int keyframe;
124  int version;
133  int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
144  int qlog;
146  int lambda;
147  int lambda2;
148  int pass1_rc;
149  int mv_scale;
151  int qbias;
153 #define QBIAS_SHIFT 3
154  int b_width;
155  int b_height;
160 #define ME_CACHE_SIZE 1024
166 
167  MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
168 
171 }SnowContext;
172 
173 /* Tables */
174 extern const uint8_t * const ff_obmc_tab[4];
175 extern uint8_t ff_qexp[QROOT];
177 
178 /* C bits used by mmx/sse2/altivec */
179 
180 static av_always_inline void snow_interleave_line_header(int * i, int width, IDWTELEM * low, IDWTELEM * high){
181  (*i) = (width) - 2;
182 
183  if (width & 1){
184  low[(*i)+1] = low[((*i)+1)>>1];
185  (*i)--;
186  }
187 }
188 
189 static av_always_inline void snow_interleave_line_footer(int * i, IDWTELEM * low, IDWTELEM * high){
190  for (; (*i)>=0; (*i)-=2){
191  low[(*i)+1] = high[(*i)>>1];
192  low[*i] = low[(*i)>>1];
193  }
194 }
195 
196 static av_always_inline void snow_horizontal_compose_lift_lead_out(int i, IDWTELEM * dst, IDWTELEM * src, IDWTELEM * ref, int width, int w, int lift_high, int mul, int add, int shift){
197  for(; i<w; i++){
198  dst[i] = src[i] - ((mul * (ref[i] + ref[i + 1]) + add) >> shift);
199  }
200 
201  if((width^lift_high)&1){
202  dst[w] = src[w] - ((mul * 2 * ref[w] + add) >> shift);
203  }
204 }
205 
207  for(; i<w; i++){
208  dst[i] = src[i] + ((ref[i] + ref[(i+1)]+W_BO + 4 * src[i]) >> W_BS);
209  }
210 
211  if(width&1){
212  dst[w] = src[w] + ((2 * ref[w] + W_BO + 4 * src[w]) >> W_BS);
213  }
214 }
215 
216 /* common code */
217 
226  int sx, int sy, int b_w, int b_h, BlockNode *block,
227  int plane_index, int w, int h);
228 /* common inline functions */
229 //XXX doublecheck all of them should stay inlined
230 
231 static inline void snow_set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
232  const int w= s->b_width << s->block_max_depth;
233  const int rem_depth= s->block_max_depth - level;
234  const int index= (x + y*w) << rem_depth;
235  const int block_w= 1<<rem_depth;
237  int i,j;
238 
239  block.color[0]= l;
240  block.color[1]= cb;
241  block.color[2]= cr;
242  block.mx= mx;
243  block.my= my;
244  block.ref= ref;
245  block.type= type;
246  block.level= level;
247 
248  for(j=0; j<block_w; j++){
249  for(i=0; i<block_w; i++){
250  s->block[index + i + j*w]= block;
251  }
252  }
253 }
254 
255 static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
256  const BlockNode *left, const BlockNode *top, const BlockNode *tr){
257  if(s->ref_frames == 1){
258  *mx = mid_pred(left->mx, top->mx, tr->mx);
259  *my = mid_pred(left->my, top->my, tr->my);
260  }else{
261  const int *scale = ff_scale_mv_ref[ref];
262  *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
263  (top ->mx * scale[top ->ref] + 128) >>8,
264  (tr ->mx * scale[tr ->ref] + 128) >>8);
265  *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
266  (top ->my * scale[top ->ref] + 128) >>8,
267  (tr ->my * scale[tr ->ref] + 128) >>8);
268  }
269 }
270 
272  if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
273  return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
274  }else{
275  return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
276  }
277 }
278 
279 //FIXME name cleanup (b_w, block_w, b_width stuff)
280 //XXX should we really inline it?
281 static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index){
282  const int b_width = s->b_width << s->block_max_depth;
283  const int b_height= s->b_height << s->block_max_depth;
284  const int b_stride= b_width;
285  BlockNode *lt= &s->block[b_x + b_y*b_stride];
286  BlockNode *rt= lt+1;
287  BlockNode *lb= lt+b_stride;
288  BlockNode *rb= lb+1;
289  uint8_t *block[4];
290  int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
291  uint8_t *tmp = s->scratchbuf;
292  uint8_t *ptmp;
293  int x,y;
294 
295  if(b_x<0){
296  lt= rt;
297  lb= rb;
298  }else if(b_x + 1 >= b_width){
299  rt= lt;
300  rb= lb;
301  }
302  if(b_y<0){
303  lt= lb;
304  rt= rb;
305  }else if(b_y + 1 >= b_height){
306  lb= lt;
307  rb= rt;
308  }
309 
310  if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
311  obmc -= src_x;
312  b_w += src_x;
313  if(!sliced && !offset_dst)
314  dst -= src_x;
315  src_x=0;
316  }else if(src_x + b_w > w){
317  b_w = w - src_x;
318  }
319  if(src_y<0){
320  obmc -= src_y*obmc_stride;
321  b_h += src_y;
322  if(!sliced && !offset_dst)
323  dst -= src_y*dst_stride;
324  src_y=0;
325  }else if(src_y + b_h> h){
326  b_h = h - src_y;
327  }
328 
329  if(b_w<=0 || b_h<=0) return;
330 
331  av_assert2(src_stride > 2*MB_SIZE + 5);
332 
333  if(!sliced && offset_dst)
334  dst += src_x + src_y*dst_stride;
335  dst8+= src_x + src_y*src_stride;
336 // src += src_x + src_y*src_stride;
337 
338  ptmp= tmp + 3*tmp_step;
339  block[0]= ptmp;
340  ptmp+=tmp_step;
341  ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
342 
343  if(same_block(lt, rt)){
344  block[1]= block[0];
345  }else{
346  block[1]= ptmp;
347  ptmp+=tmp_step;
348  ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
349  }
350 
351  if(same_block(lt, lb)){
352  block[2]= block[0];
353  }else if(same_block(rt, lb)){
354  block[2]= block[1];
355  }else{
356  block[2]= ptmp;
357  ptmp+=tmp_step;
358  ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
359  }
360 
361  if(same_block(lt, rb) ){
362  block[3]= block[0];
363  }else if(same_block(rt, rb)){
364  block[3]= block[1];
365  }else if(same_block(lb, rb)){
366  block[3]= block[2];
367  }else{
368  block[3]= ptmp;
369  ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
370  }
371  if(sliced){
372  s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
373  }else{
374  for(y=0; y<b_h; y++){
375  //FIXME ugly misuse of obmc_stride
376  const uint8_t *obmc1= obmc + y*obmc_stride;
377  const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
378  const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
379  const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
380  for(x=0; x<b_w; x++){
381  int v= obmc1[x] * block[3][x + y*src_stride]
382  +obmc2[x] * block[2][x + y*src_stride]
383  +obmc3[x] * block[1][x + y*src_stride]
384  +obmc4[x] * block[0][x + y*src_stride];
385 
386  v <<= 8 - LOG2_OBMC_MAX;
387  if(FRAC_BITS != 8){
388  v >>= 8 - FRAC_BITS;
389  }
390  if(add){
391  v += dst[x + y*dst_stride];
392  v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
393  if(v&(~255)) v= ~(v>>31);
394  dst8[x + y*src_stride] = v;
395  }else{
396  dst[x + y*dst_stride] -= v;
397  }
398  }
399  }
400  }
401 }
402 
403 static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
404  Plane *p= &s->plane[plane_index];
405  const int mb_w= s->b_width << s->block_max_depth;
406  const int mb_h= s->b_height << s->block_max_depth;
407  int x, y, mb_x;
408  int block_size = MB_SIZE >> s->block_max_depth;
409  int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
410  int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
411  const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
412  const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
413  int ref_stride= s->current_picture.linesize[plane_index];
414  uint8_t *dst8= s->current_picture.data[plane_index];
415  int w= p->width;
416  int h= p->height;
417  av_assert2(s->chroma_h_shift == s->chroma_v_shift); // obmc params assume squares
418  if(s->keyframe || (s->avctx->debug&512)){
419  if(mb_y==mb_h)
420  return;
421 
422  if(add){
423  for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
424  for(x=0; x<w; x++){
425  int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
426  v >>= FRAC_BITS;
427  if(v&(~255)) v= ~(v>>31);
428  dst8[x + y*ref_stride]= v;
429  }
430  }
431  }else{
432  for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
433  for(x=0; x<w; x++){
434  buf[x + y*w]-= 128<<FRAC_BITS;
435  }
436  }
437  }
438 
439  return;
440  }
441 
442  for(mb_x=0; mb_x<=mb_w; mb_x++){
443  add_yblock(s, 0, NULL, buf, dst8, obmc,
444  block_w*mb_x - block_w/2,
445  block_h*mb_y - block_h/2,
446  block_w, block_h,
447  w, h,
448  w, ref_stride, obmc_stride,
449  mb_x - 1, mb_y - 1,
450  add, 1, plane_index);
451  }
452 }
453 
454 static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
455  const int mb_h= s->b_height << s->block_max_depth;
456  int mb_y;
457  for(mb_y=0; mb_y<=mb_h; mb_y++)
458  predict_slice(s, buf, plane_index, add, mb_y);
459 }
460 
461 static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
462  const int w= s->b_width << s->block_max_depth;
463  const int rem_depth= s->block_max_depth - level;
464  const int index= (x + y*w) << rem_depth;
465  const int block_w= 1<<rem_depth;
466  const int block_h= 1<<rem_depth; //FIXME "w!=h"
468  int i,j;
469 
470  block.color[0]= l;
471  block.color[1]= cb;
472  block.color[2]= cr;
473  block.mx= mx;
474  block.my= my;
475  block.ref= ref;
476  block.type= type;
477  block.level= level;
478 
479  for(j=0; j<block_h; j++){
480  for(i=0; i<block_w; i++){
481  s->block[index + i + j*w]= block;
482  }
483  }
484 }
485 
486 static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
487  SnowContext *s = c->avctx->priv_data;
488  const int offset[3]= {
489  y*c-> stride + x,
490  ((y*c->uvstride + x)>>s->chroma_h_shift),
491  ((y*c->uvstride + x)>>s->chroma_h_shift),
492  };
493  int i;
494  for(i=0; i<3; i++){
495  c->src[0][i]= src [i];
496  c->ref[0][i]= ref [i] + offset[i];
497  }
498  av_assert2(!ref_index);
499 }
500 
501 
502 /* bitstream functions */
503 
504 extern const int8_t ff_quant3bA[256];
505 
506 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
507 
508 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
509  int i;
510 
511  if(v){
512  const int a= FFABS(v);
513  const int e= av_log2(a);
514  const int el= FFMIN(e, 10);
515  put_rac(c, state+0, 0);
516 
517  for(i=0; i<el; i++){
518  put_rac(c, state+1+i, 1); //1..10
519  }
520  for(; i<e; i++){
521  put_rac(c, state+1+9, 1); //1..10
522  }
523  put_rac(c, state+1+FFMIN(i,9), 0);
524 
525  for(i=e-1; i>=el; i--){
526  put_rac(c, state+22+9, (a>>i)&1); //22..31
527  }
528  for(; i>=0; i--){
529  put_rac(c, state+22+i, (a>>i)&1); //22..31
530  }
531 
532  if(is_signed)
533  put_rac(c, state+11 + el, v < 0); //11..21
534  }else{
535  put_rac(c, state+0, 1);
536  }
537 }
538 
539 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
540  if(get_rac(c, state+0))
541  return 0;
542  else{
543  int i, e, a;
544  e= 0;
545  while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
546  e++;
547  }
548 
549  a= 1;
550  for(i=e-1; i>=0; i--){
551  a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
552  }
553 
554  e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21
555  return (a^e)-e;
556  }
557 }
558 
559 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
560  int i;
561  int r= log2>=0 ? 1<<log2 : 1;
562 
563  av_assert2(v>=0);
564  av_assert2(log2>=-4);
565 
566  while(v >= r){
567  put_rac(c, state+4+log2, 1);
568  v -= r;
569  log2++;
570  if(log2>0) r+=r;
571  }
572  put_rac(c, state+4+log2, 0);
573 
574  for(i=log2-1; i>=0; i--){
575  put_rac(c, state+31-i, (v>>i)&1);
576  }
577 }
578 
579 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
580  int i;
581  int r= log2>=0 ? 1<<log2 : 1;
582  int v=0;
583 
584  av_assert2(log2>=-4);
585 
586  while(log2<28 && get_rac(c, state+4+log2)){
587  v+= r;
588  log2++;
589  if(log2>0) r+=r;
590  }
591 
592  for(i=log2-1; i>=0; i--){
593  v+= get_rac(c, state+31-i)<<i;
594  }
595 
596  return v;
597 }
598 
599 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
600  const int w= b->width;
601  const int h= b->height;
602  int x,y;
603 
604  int run, runs;
605  x_and_coeff *xc= b->x_coeff;
606  x_and_coeff *prev_xc= NULL;
607  x_and_coeff *prev2_xc= xc;
608  x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
609  x_and_coeff *prev_parent_xc= parent_xc;
610 
611  runs= get_symbol2(&s->c, b->state[30], 0);
612  if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
613  else run= INT_MAX;
614 
615  for(y=0; y<h; y++){
616  int v=0;
617  int lt=0, t=0, rt=0;
618 
619  if(y && prev_xc->x == 0){
620  rt= prev_xc->coeff;
621  }
622  for(x=0; x<w; x++){
623  int p=0;
624  const int l= v;
625 
626  lt= t; t= rt;
627 
628  if(y){
629  if(prev_xc->x <= x)
630  prev_xc++;
631  if(prev_xc->x == x + 1)
632  rt= prev_xc->coeff;
633  else
634  rt=0;
635  }
636  if(parent_xc){
637  if(x>>1 > parent_xc->x){
638  parent_xc++;
639  }
640  if(x>>1 == parent_xc->x){
641  p= parent_xc->coeff;
642  }
643  }
644  if(/*ll|*/l|lt|t|rt|p){
645  int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
646 
647  v=get_rac(&s->c, &b->state[0][context]);
648  if(v){
649  v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
650  v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3*ff_quant3bA[t&0xFF]]);
651 
652  xc->x=x;
653  (xc++)->coeff= v;
654  }
655  }else{
656  if(!run){
657  if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
658  else run= INT_MAX;
659  v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
660  v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
661 
662  xc->x=x;
663  (xc++)->coeff= v;
664  }else{
665  int max_run;
666  run--;
667  v=0;
668  av_assert2(run >= 0);
669  if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
670  else max_run= FFMIN(run, w-x-1);
671  if(parent_xc)
672  max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
673  av_assert2(max_run >= 0 && max_run <= run);
674 
675  x+= max_run;
676  run-= max_run;
677  }
678  }
679  }
680  (xc++)->x= w+1; //end marker
681  prev_xc= prev2_xc;
682  prev2_xc= xc;
683 
684  if(parent_xc){
685  if(y&1){
686  while(parent_xc->x != parent->width+1)
687  parent_xc++;
688  parent_xc++;
689  prev_parent_xc= parent_xc;
690  }else{
691  parent_xc= prev_parent_xc;
692  }
693  }
694  }
695 
696  (xc++)->x= w+1; //end marker
697 }
698 
699 #endif /* AVCODEC_SNOW_H */