FFmpeg
 All Data Structures Namespaces 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 
26 
27 #include "hpeldsp.h"
28 #include "me_cmp.h"
29 #include "qpeldsp.h"
30 #include "snow_dwt.h"
31 
32 #include "rangecoder.h"
33 #include "mathops.h"
34 
35 #define FF_MPV_OFFSET(x) (offsetof(MpegEncContext, x) + offsetof(SnowContext, m))
36 #include "mpegvideo.h"
37 #include "h264qpel.h"
38 
39 #define MID_STATE 128
40 
41 #define MAX_PLANES 4
42 #define QSHIFT 5
43 #define QROOT (1<<QSHIFT)
44 #define LOSSLESS_QLOG -128
45 #define FRAC_BITS 4
46 #define MAX_REF_FRAMES 8
47 
48 #define LOG2_OBMC_MAX 8
49 #define OBMC_MAX (1<<(LOG2_OBMC_MAX))
50 typedef struct BlockNode{
51  int16_t mx;
52  int16_t my;
56 //#define TYPE_SPLIT 1
57 #define BLOCK_INTRA 1
58 #define BLOCK_OPT 2
59 //#define TYPE_NOCOLOR 4
60  uint8_t level; //FIXME merge into type?
61 }BlockNode;
62 
63 static const BlockNode null_block= { //FIXME add border maybe
64  .color= {128,128,128},
65  .mx= 0,
66  .my= 0,
67  .ref= 0,
68  .type= 0,
69  .level= 0,
70 };
71 
72 #define LOG2_MB_SIZE 4
73 #define MB_SIZE (1<<LOG2_MB_SIZE)
74 #define ENCODER_EXTRA_BITS 4
75 #define HTAPS_MAX 8
76 
77 typedef struct x_and_coeff{
78  int16_t x;
79  uint16_t coeff;
80 } x_and_coeff;
81 
82 typedef struct SubBand{
83  int level;
84  int stride;
85  int width;
86  int height;
87  int qlog; ///< log(qscale)/log[2^(1/6)]
89  IDWTELEM *ibuf;
92  int stride_line; ///< Stride measured in lines, not pixels.
94  struct SubBand *parent;
95  uint8_t state[/*7*2*/ 7 + 512][32];
96 }SubBand;
97 
98 typedef struct Plane{
99  int width;
100  int height;
102 
103  int htaps;
104  int8_t hcoeff[HTAPS_MAX/2];
105  int diag_mc;
106  int fast_mc;
107 
111 }Plane;
112 
113 typedef struct SnowContext{
114  AVClass *class;
125  AVFrame *input_picture; ///< new_picture with the internal linesizes
130 // uint8_t q_context[16];
132  uint8_t block_state[128 + 32*128];
133  int keyframe;
135  int version;
144  int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
155  int qlog;
157  int lambda;
158  int lambda2;
159  int pass1_rc;
160  int mv_scale;
162  int qbias;
164 #define QBIAS_SHIFT 3
165  int b_width;
166  int b_height;
172 #define ME_CACHE_SIZE 1024
178 
179  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)
180 
183 
186 }SnowContext;
187 
188 /* Tables */
189 extern const uint8_t * const ff_obmc_tab[4];
190 extern uint8_t ff_qexp[QROOT];
192 
193 /* C bits used by mmx/sse2/altivec */
194 
195 static av_always_inline void snow_interleave_line_header(int * i, int width, IDWTELEM * low, IDWTELEM * high){
196  (*i) = (width) - 2;
197 
198  if (width & 1){
199  low[(*i)+1] = low[((*i)+1)>>1];
200  (*i)--;
201  }
202 }
203 
204 static av_always_inline void snow_interleave_line_footer(int * i, IDWTELEM * low, IDWTELEM * high){
205  for (; (*i)>=0; (*i)-=2){
206  low[(*i)+1] = high[(*i)>>1];
207  low[*i] = low[(*i)>>1];
208  }
209 }
210 
211 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){
212  for(; i<w; i++){
213  dst[i] = src[i] - ((mul * (ref[i] + ref[i + 1]) + add) >> shift);
214  }
215 
216  if((width^lift_high)&1){
217  dst[w] = src[w] - ((mul * 2 * ref[w] + add) >> shift);
218  }
219 }
220 
222  for(; i<w; i++){
223  dst[i] = src[i] + ((ref[i] + ref[(i+1)]+W_BO + 4 * src[i]) >> W_BS);
224  }
225 
226  if(width&1){
227  dst[w] = src[w] + ((2 * ref[w] + W_BO + 4 * src[w]) >> W_BS);
228  }
229 }
230 
231 /* common code */
232 
240 void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, ptrdiff_t stride,
241  int sx, int sy, int b_w, int b_h, const BlockNode *block,
242  int plane_index, int w, int h);
244 /* common inline functions */
245 //XXX doublecheck all of them should stay inlined
246 
247 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){
248  const int w= s->b_width << s->block_max_depth;
249  const int rem_depth= s->block_max_depth - level;
250  const int index= (x + y*w) << rem_depth;
251  const int block_w= 1<<rem_depth;
253  int i,j;
254 
255  block.color[0]= l;
256  block.color[1]= cb;
257  block.color[2]= cr;
258  block.mx= mx;
259  block.my= my;
260  block.ref= ref;
261  block.type= type;
262  block.level= level;
263 
264  for(j=0; j<block_w; j++){
265  for(i=0; i<block_w; i++){
266  s->block[index + i + j*w]= block;
267  }
268  }
269 }
270 
271 static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
272  const BlockNode *left, const BlockNode *top, const BlockNode *tr){
273  if(s->ref_frames == 1){
274  *mx = mid_pred(left->mx, top->mx, tr->mx);
275  *my = mid_pred(left->my, top->my, tr->my);
276  }else{
277  const int *scale = ff_scale_mv_ref[ref];
278  *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
279  (top ->mx * scale[top ->ref] + 128) >>8,
280  (tr ->mx * scale[tr ->ref] + 128) >>8);
281  *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
282  (top ->my * scale[top ->ref] + 128) >>8,
283  (tr ->my * scale[tr ->ref] + 128) >>8);
284  }
285 }
286 
288  if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
289  return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
290  }else{
291  return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
292  }
293 }
294 
295 //FIXME name cleanup (b_w, block_w, b_width stuff)
296 //XXX should we really inline it?
297 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){
298  const int b_width = s->b_width << s->block_max_depth;
299  const int b_height= s->b_height << s->block_max_depth;
300  const int b_stride= b_width;
301  BlockNode *lt= &s->block[b_x + b_y*b_stride];
302  BlockNode *rt= lt+1;
303  BlockNode *lb= lt+b_stride;
304  BlockNode *rb= lb+1;
305  uint8_t *block[4];
306  int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
307  uint8_t *tmp = s->scratchbuf;
308  uint8_t *ptmp;
309  int x,y;
310 
311  if(b_x<0){
312  lt= rt;
313  lb= rb;
314  }else if(b_x + 1 >= b_width){
315  rt= lt;
316  rb= lb;
317  }
318  if(b_y<0){
319  lt= lb;
320  rt= rb;
321  }else if(b_y + 1 >= b_height){
322  lb= lt;
323  rb= rt;
324  }
325 
326  if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
327  obmc -= src_x;
328  b_w += src_x;
329  if(!sliced && !offset_dst)
330  dst -= src_x;
331  src_x=0;
332  }
333  if(src_x + b_w > w){
334  b_w = w - src_x;
335  }
336  if(src_y<0){
337  obmc -= src_y*obmc_stride;
338  b_h += src_y;
339  if(!sliced && !offset_dst)
340  dst -= src_y*dst_stride;
341  src_y=0;
342  }
343  if(src_y + b_h> h){
344  b_h = h - src_y;
345  }
346 
347  if(b_w<=0 || b_h<=0) return;
348 
349  av_assert2(src_stride > 2*MB_SIZE + 5);
350 
351  if(!sliced && offset_dst)
352  dst += src_x + src_y*dst_stride;
353  dst8+= src_x + src_y*src_stride;
354 // src += src_x + src_y*src_stride;
355 
356  ptmp= tmp + 3*tmp_step;
357  block[0]= ptmp;
358  ptmp+=tmp_step;
359  ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
360 
361  if(same_block(lt, rt)){
362  block[1]= block[0];
363  }else{
364  block[1]= ptmp;
365  ptmp+=tmp_step;
366  ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
367  }
368 
369  if(same_block(lt, lb)){
370  block[2]= block[0];
371  }else if(same_block(rt, lb)){
372  block[2]= block[1];
373  }else{
374  block[2]= ptmp;
375  ptmp+=tmp_step;
376  ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
377  }
378 
379  if(same_block(lt, rb) ){
380  block[3]= block[0];
381  }else if(same_block(rt, rb)){
382  block[3]= block[1];
383  }else if(same_block(lb, rb)){
384  block[3]= block[2];
385  }else{
386  block[3]= ptmp;
387  ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
388  }
389  if(sliced){
390  s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
391  }else{
392  for(y=0; y<b_h; y++){
393  //FIXME ugly misuse of obmc_stride
394  const uint8_t *obmc1= obmc + y*obmc_stride;
395  const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
396  const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
397  const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
398  for(x=0; x<b_w; x++){
399  int v= obmc1[x] * block[3][x + y*src_stride]
400  +obmc2[x] * block[2][x + y*src_stride]
401  +obmc3[x] * block[1][x + y*src_stride]
402  +obmc4[x] * block[0][x + y*src_stride];
403 
404  v <<= 8 - LOG2_OBMC_MAX;
405  if(FRAC_BITS != 8){
406  v >>= 8 - FRAC_BITS;
407  }
408  if(add){
409  v += dst[x + y*dst_stride];
410  v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
411  if(v&(~255)) v= ~(v>>31);
412  dst8[x + y*src_stride] = v;
413  }else{
414  dst[x + y*dst_stride] -= v;
415  }
416  }
417  }
418  }
419 }
420 
421 static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
422  Plane *p= &s->plane[plane_index];
423  const int mb_w= s->b_width << s->block_max_depth;
424  const int mb_h= s->b_height << s->block_max_depth;
425  int x, y, mb_x;
426  int block_size = MB_SIZE >> s->block_max_depth;
427  int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
428  int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
429  const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
430  const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
431  int ref_stride= s->current_picture->linesize[plane_index];
432  uint8_t *dst8= s->current_picture->data[plane_index];
433  int w= p->width;
434  int h= p->height;
435  av_assert2(s->chroma_h_shift == s->chroma_v_shift); // obmc params assume squares
436  if(s->keyframe || (s->avctx->debug&512)){
437  if(mb_y==mb_h)
438  return;
439 
440  if(add){
441  for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
442  for(x=0; x<w; x++){
443  int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
444  v >>= FRAC_BITS;
445  if(v&(~255)) v= ~(v>>31);
446  dst8[x + y*ref_stride]= v;
447  }
448  }
449  }else{
450  for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
451  for(x=0; x<w; x++){
452  buf[x + y*w]-= 128<<FRAC_BITS;
453  }
454  }
455  }
456 
457  return;
458  }
459 
460  for(mb_x=0; mb_x<=mb_w; mb_x++){
461  add_yblock(s, 0, NULL, buf, dst8, obmc,
462  block_w*mb_x - block_w/2,
463  block_h*mb_y - block_h/2,
464  block_w, block_h,
465  w, h,
466  w, ref_stride, obmc_stride,
467  mb_x - 1, mb_y - 1,
468  add, 1, plane_index);
469  }
470 }
471 
472 static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
473  const int mb_h= s->b_height << s->block_max_depth;
474  int mb_y;
475  for(mb_y=0; mb_y<=mb_h; mb_y++)
476  predict_slice(s, buf, plane_index, add, mb_y);
477 }
478 
479 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){
480  const int w= s->b_width << s->block_max_depth;
481  const int rem_depth= s->block_max_depth - level;
482  const int index= (x + y*w) << rem_depth;
483  const int block_w= 1<<rem_depth;
484  const int block_h= 1<<rem_depth; //FIXME "w!=h"
486  int i,j;
487 
488  block.color[0]= l;
489  block.color[1]= cb;
490  block.color[2]= cr;
491  block.mx= mx;
492  block.my= my;
493  block.ref= ref;
494  block.type= type;
495  block.level= level;
496 
497  for(j=0; j<block_h; j++){
498  for(i=0; i<block_w; i++){
499  s->block[index + i + j*w]= block;
500  }
501  }
502 }
503 
504 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){
505  SnowContext *s = c->avctx->priv_data;
506  const int offset[3]= {
507  y*c-> stride + x,
508  ((y*c->uvstride + x)>>s->chroma_h_shift),
509  ((y*c->uvstride + x)>>s->chroma_h_shift),
510  };
511  int i;
512  for(i=0; i<3; i++){
513  c->src[0][i]= src [i];
514  c->ref[0][i]= ref [i] + offset[i];
515  }
516  av_assert2(!ref_index);
517 }
518 
519 
520 /* bitstream functions */
521 
522 extern const int8_t ff_quant3bA[256];
523 
524 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
525 
526 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
527  int i;
528 
529  if(v){
530  const int a= FFABS(v);
531  const int e= av_log2(a);
532  const int el= FFMIN(e, 10);
533  put_rac(c, state+0, 0);
534 
535  for(i=0; i<el; i++){
536  put_rac(c, state+1+i, 1); //1..10
537  }
538  for(; i<e; i++){
539  put_rac(c, state+1+9, 1); //1..10
540  }
541  put_rac(c, state+1+FFMIN(i,9), 0);
542 
543  for(i=e-1; i>=el; i--){
544  put_rac(c, state+22+9, (a>>i)&1); //22..31
545  }
546  for(; i>=0; i--){
547  put_rac(c, state+22+i, (a>>i)&1); //22..31
548  }
549 
550  if(is_signed)
551  put_rac(c, state+11 + el, v < 0); //11..21
552  }else{
553  put_rac(c, state+0, 1);
554  }
555 }
556 
557 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
558  if(get_rac(c, state+0))
559  return 0;
560  else{
561  int i, e, a;
562  e= 0;
563  while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
564  e++;
565  }
566 
567  a= 1;
568  for(i=e-1; i>=0; i--){
569  a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
570  }
571 
572  e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21
573  return (a^e)-e;
574  }
575 }
576 
577 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
578  int i;
579  int r= log2>=0 ? 1<<log2 : 1;
580 
581  av_assert2(v>=0);
582  av_assert2(log2>=-4);
583 
584  while(v >= r){
585  put_rac(c, state+4+log2, 1);
586  v -= r;
587  log2++;
588  if(log2>0) r+=r;
589  }
590  put_rac(c, state+4+log2, 0);
591 
592  for(i=log2-1; i>=0; i--){
593  put_rac(c, state+31-i, (v>>i)&1);
594  }
595 }
596 
597 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
598  int i;
599  int r= log2>=0 ? 1<<log2 : 1;
600  int v=0;
601 
602  av_assert2(log2>=-4);
603 
604  while(log2<28 && get_rac(c, state+4+log2)){
605  v+= r;
606  log2++;
607  if(log2>0) r+=r;
608  }
609 
610  for(i=log2-1; i>=0; i--){
611  v+= get_rac(c, state+31-i)<<i;
612  }
613 
614  return v;
615 }
616 
617 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
618  const int w= b->width;
619  const int h= b->height;
620  int x,y;
621 
622  int run, runs;
623  x_and_coeff *xc= b->x_coeff;
624  x_and_coeff *prev_xc= NULL;
625  x_and_coeff *prev2_xc= xc;
626  x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
627  x_and_coeff *prev_parent_xc= parent_xc;
628 
629  runs= get_symbol2(&s->c, b->state[30], 0);
630  if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
631  else run= INT_MAX;
632 
633  for(y=0; y<h; y++){
634  int v=0;
635  int lt=0, t=0, rt=0;
636 
637  if(y && prev_xc->x == 0){
638  rt= prev_xc->coeff;
639  }
640  for(x=0; x<w; x++){
641  int p=0;
642  const int l= v;
643 
644  lt= t; t= rt;
645 
646  if(y){
647  if(prev_xc->x <= x)
648  prev_xc++;
649  if(prev_xc->x == x + 1)
650  rt= prev_xc->coeff;
651  else
652  rt=0;
653  }
654  if(parent_xc){
655  if(x>>1 > parent_xc->x){
656  parent_xc++;
657  }
658  if(x>>1 == parent_xc->x){
659  p= parent_xc->coeff;
660  }
661  }
662  if(/*ll|*/l|lt|t|rt|p){
663  int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
664 
665  v=get_rac(&s->c, &b->state[0][context]);
666  if(v){
667  v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
668  v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3*ff_quant3bA[t&0xFF]]);
669  if ((uint16_t)v != v) {
670  av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
671  v = 1;
672  }
673  xc->x=x;
674  (xc++)->coeff= v;
675  }
676  }else{
677  if(!run){
678  if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
679  else run= INT_MAX;
680  v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
681  v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
682  if ((uint16_t)v != v) {
683  av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
684  v = 1;
685  }
686 
687  xc->x=x;
688  (xc++)->coeff= v;
689  }else{
690  int max_run;
691  run--;
692  v=0;
693  av_assert2(run >= 0);
694  if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
695  else max_run= FFMIN(run, w-x-1);
696  if(parent_xc)
697  max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
698  av_assert2(max_run >= 0 && max_run <= run);
699 
700  x+= max_run;
701  run-= max_run;
702  }
703  }
704  }
705  (xc++)->x= w+1; //end marker
706  prev_xc= prev2_xc;
707  prev2_xc= xc;
708 
709  if(parent_xc){
710  if(y&1){
711  while(parent_xc->x != parent->width+1)
712  parent_xc++;
713  parent_xc++;
714  prev_parent_xc= parent_xc;
715  }else{
716  parent_xc= prev_parent_xc;
717  }
718  }
719  }
720 
721  (xc++)->x= w+1; //end marker
722 }
723 
724 #endif /* AVCODEC_SNOW_H */