]> git.sesse.net Git - ffmpeg/blob - libavcodec/snow.h
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / snow.h
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;
46     uint8_t ref;
47     uint8_t color[3];
48     uint8_t type;
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)]
81     DWTELEM *buf;
82     IDWTELEM *ibuf;
83     int buf_x_offset;
84     int buf_y_offset;
85     int stride_line; ///< Stride measured in lines, not pixels.
86     x_and_coeff * x_coeff;
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;
94     SubBand band[MAX_DECOMPOSITIONS][4];
95
96     int htaps;
97     int8_t hcoeff[HTAPS_MAX/2];
98     int diag_mc;
99     int fast_mc;
100
101     int last_htaps;
102     int8_t last_hcoeff[HTAPS_MAX/2];
103     int last_diag_mc;
104 }Plane;
105
106 typedef struct SnowContext{
107     AVClass *class;
108     AVCodecContext *avctx;
109     RangeCoder c;
110     DSPContext dsp;
111     DWTContext dwt;
112     AVFrame new_picture;
113     AVFrame input_picture;              ///< new_picture with the internal linesizes
114     AVFrame current_picture;
115     AVFrame last_picture[MAX_REF_FRAMES];
116     uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4];
117     AVFrame mconly_picture;
118 //     uint8_t q_context[16];
119     uint8_t header_state[32];
120     uint8_t block_state[128 + 32*128];
121     int keyframe;
122     int always_reset;
123     int version;
124     int spatial_decomposition_type;
125     int last_spatial_decomposition_type;
126     int temporal_decomposition_type;
127     int spatial_decomposition_count;
128     int last_spatial_decomposition_count;
129     int temporal_decomposition_count;
130     int max_ref_frames;
131     int ref_frames;
132     int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
133     uint32_t *ref_scores[MAX_REF_FRAMES];
134     DWTELEM *spatial_dwt_buffer;
135     IDWTELEM *spatial_idwt_buffer;
136     int colorspace_type;
137     int chroma_h_shift;
138     int chroma_v_shift;
139     int spatial_scalability;
140     int qlog;
141     int last_qlog;
142     int lambda;
143     int lambda2;
144     int pass1_rc;
145     int mv_scale;
146     int last_mv_scale;
147     int qbias;
148     int last_qbias;
149 #define QBIAS_SHIFT 3
150     int b_width;
151     int b_height;
152     int block_max_depth;
153     int last_block_max_depth;
154     Plane plane[MAX_PLANES];
155     BlockNode *block;
156 #define ME_CACHE_SIZE 1024
157     unsigned me_cache[ME_CACHE_SIZE];
158     unsigned me_cache_generation;
159     slice_buffer sb;
160     int memc_only;
161     int no_bitstream;
162
163     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)
164
165     uint8_t *scratchbuf;
166     int *runs;
167 }SnowContext;
168
169 /* Tables */
170 extern const uint8_t * const ff_obmc_tab[4];
171 extern uint8_t ff_qexp[QROOT];
172 extern int ff_scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
173
174 /* C bits used by mmx/sse2/altivec */
175
176 static av_always_inline void snow_interleave_line_header(int * i, int width, IDWTELEM * low, IDWTELEM * high){
177     (*i) = (width) - 2;
178
179     if (width & 1){
180         low[(*i)+1] = low[((*i)+1)>>1];
181         (*i)--;
182     }
183 }
184
185 static av_always_inline void snow_interleave_line_footer(int * i, IDWTELEM * low, IDWTELEM * high){
186     for (; (*i)>=0; (*i)-=2){
187         low[(*i)+1] = high[(*i)>>1];
188         low[*i] = low[(*i)>>1];
189     }
190 }
191
192 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){
193     for(; i<w; i++){
194         dst[i] = src[i] - ((mul * (ref[i] + ref[i + 1]) + add) >> shift);
195     }
196
197     if((width^lift_high)&1){
198         dst[w] = src[w] - ((mul * 2 * ref[w] + add) >> shift);
199     }
200 }
201
202 static av_always_inline void snow_horizontal_compose_liftS_lead_out(int i, IDWTELEM * dst, IDWTELEM * src, IDWTELEM * ref, int width, int w){
203         for(; i<w; i++){
204             dst[i] = src[i] + ((ref[i] + ref[(i+1)]+W_BO + 4 * src[i]) >> W_BS);
205         }
206
207         if(width&1){
208             dst[w] = src[w] + ((2 * ref[w] + W_BO + 4 * src[w]) >> W_BS);
209         }
210 }
211
212 /* common code */
213
214 int ff_snow_common_init(AVCodecContext *avctx);
215 int ff_snow_common_init_after_header(AVCodecContext *avctx);
216 void ff_snow_common_end(SnowContext *s);
217 void ff_snow_release_buffer(AVCodecContext *avctx);
218 void ff_snow_reset_contexts(SnowContext *s);
219 int ff_snow_alloc_blocks(SnowContext *s);
220 int ff_snow_frame_start(SnowContext *s);
221 void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride,
222                      int sx, int sy, int b_w, int b_h, BlockNode *block,
223                      int plane_index, int w, int h);
224 /* common inline functions */
225 //XXX doublecheck all of them should stay inlined
226
227 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){
228     const int w= s->b_width << s->block_max_depth;
229     const int rem_depth= s->block_max_depth - level;
230     const int index= (x + y*w) << rem_depth;
231     const int block_w= 1<<rem_depth;
232     BlockNode block;
233     int i,j;
234
235     block.color[0]= l;
236     block.color[1]= cb;
237     block.color[2]= cr;
238     block.mx= mx;
239     block.my= my;
240     block.ref= ref;
241     block.type= type;
242     block.level= level;
243
244     for(j=0; j<block_w; j++){
245         for(i=0; i<block_w; i++){
246             s->block[index + i + j*w]= block;
247         }
248     }
249 }
250
251 static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
252                            const BlockNode *left, const BlockNode *top, const BlockNode *tr){
253     if(s->ref_frames == 1){
254         *mx = mid_pred(left->mx, top->mx, tr->mx);
255         *my = mid_pred(left->my, top->my, tr->my);
256     }else{
257         const int *scale = ff_scale_mv_ref[ref];
258         *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
259                        (top ->mx * scale[top ->ref] + 128) >>8,
260                        (tr  ->mx * scale[tr  ->ref] + 128) >>8);
261         *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
262                        (top ->my * scale[top ->ref] + 128) >>8,
263                        (tr  ->my * scale[tr  ->ref] + 128) >>8);
264     }
265 }
266
267 static av_always_inline int same_block(BlockNode *a, BlockNode *b){
268     if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
269         return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
270     }else{
271         return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
272     }
273 }
274
275 //FIXME name cleanup (b_w, block_w, b_width stuff)
276 //XXX should we really inline it?
277 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){
278     const int b_width = s->b_width  << s->block_max_depth;
279     const int b_height= s->b_height << s->block_max_depth;
280     const int b_stride= b_width;
281     BlockNode *lt= &s->block[b_x + b_y*b_stride];
282     BlockNode *rt= lt+1;
283     BlockNode *lb= lt+b_stride;
284     BlockNode *rb= lb+1;
285     uint8_t *block[4];
286     int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
287     uint8_t *tmp = s->scratchbuf;
288     uint8_t *ptmp;
289     int x,y;
290
291     if(b_x<0){
292         lt= rt;
293         lb= rb;
294     }else if(b_x + 1 >= b_width){
295         rt= lt;
296         rb= lb;
297     }
298     if(b_y<0){
299         lt= lb;
300         rt= rb;
301     }else if(b_y + 1 >= b_height){
302         lb= lt;
303         rb= rt;
304     }
305
306     if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
307         obmc -= src_x;
308         b_w += src_x;
309         if(!sliced && !offset_dst)
310             dst -= src_x;
311         src_x=0;
312     }else if(src_x + b_w > w){
313         b_w = w - src_x;
314     }
315     if(src_y<0){
316         obmc -= src_y*obmc_stride;
317         b_h += src_y;
318         if(!sliced && !offset_dst)
319             dst -= src_y*dst_stride;
320         src_y=0;
321     }else if(src_y + b_h> h){
322         b_h = h - src_y;
323     }
324
325     if(b_w<=0 || b_h<=0) return;
326
327     assert(src_stride > 2*MB_SIZE + 5);
328
329     if(!sliced && offset_dst)
330         dst += src_x + src_y*dst_stride;
331     dst8+= src_x + src_y*src_stride;
332 //    src += src_x + src_y*src_stride;
333
334     ptmp= tmp + 3*tmp_step;
335     block[0]= ptmp;
336     ptmp+=tmp_step;
337     ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
338
339     if(same_block(lt, rt)){
340         block[1]= block[0];
341     }else{
342         block[1]= ptmp;
343         ptmp+=tmp_step;
344         ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
345     }
346
347     if(same_block(lt, lb)){
348         block[2]= block[0];
349     }else if(same_block(rt, lb)){
350         block[2]= block[1];
351     }else{
352         block[2]= ptmp;
353         ptmp+=tmp_step;
354         ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
355     }
356
357     if(same_block(lt, rb) ){
358         block[3]= block[0];
359     }else if(same_block(rt, rb)){
360         block[3]= block[1];
361     }else if(same_block(lb, rb)){
362         block[3]= block[2];
363     }else{
364         block[3]= ptmp;
365         ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
366     }
367     if(sliced){
368         s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
369     }else{
370         for(y=0; y<b_h; y++){
371             //FIXME ugly misuse of obmc_stride
372             const uint8_t *obmc1= obmc + y*obmc_stride;
373             const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
374             const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
375             const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
376             for(x=0; x<b_w; x++){
377                 int v=   obmc1[x] * block[3][x + y*src_stride]
378                         +obmc2[x] * block[2][x + y*src_stride]
379                         +obmc3[x] * block[1][x + y*src_stride]
380                         +obmc4[x] * block[0][x + y*src_stride];
381
382                 v <<= 8 - LOG2_OBMC_MAX;
383                 if(FRAC_BITS != 8){
384                     v >>= 8 - FRAC_BITS;
385                 }
386                 if(add){
387                     v += dst[x + y*dst_stride];
388                     v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
389                     if(v&(~255)) v= ~(v>>31);
390                     dst8[x + y*src_stride] = v;
391                 }else{
392                     dst[x + y*dst_stride] -= v;
393                 }
394             }
395         }
396     }
397 }
398
399 static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
400     Plane *p= &s->plane[plane_index];
401     const int mb_w= s->b_width  << s->block_max_depth;
402     const int mb_h= s->b_height << s->block_max_depth;
403     int x, y, mb_x;
404     int block_size = MB_SIZE >> s->block_max_depth;
405     int block_w    = plane_index ? block_size/2 : block_size;
406     const uint8_t *obmc  = plane_index ? ff_obmc_tab[s->block_max_depth+1] : ff_obmc_tab[s->block_max_depth];
407     const int obmc_stride= plane_index ? block_size : 2*block_size;
408     int ref_stride= s->current_picture.linesize[plane_index];
409     uint8_t *dst8= s->current_picture.data[plane_index];
410     int w= p->width;
411     int h= p->height;
412
413     if(s->keyframe || (s->avctx->debug&512)){
414         if(mb_y==mb_h)
415             return;
416
417         if(add){
418             for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
419                 for(x=0; x<w; x++){
420                     int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
421                     v >>= FRAC_BITS;
422                     if(v&(~255)) v= ~(v>>31);
423                     dst8[x + y*ref_stride]= v;
424                 }
425             }
426         }else{
427             for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
428                 for(x=0; x<w; x++){
429                     buf[x + y*w]-= 128<<FRAC_BITS;
430                 }
431             }
432         }
433
434         return;
435     }
436
437     for(mb_x=0; mb_x<=mb_w; mb_x++){
438         add_yblock(s, 0, NULL, buf, dst8, obmc,
439                    block_w*mb_x - block_w/2,
440                    block_w*mb_y - block_w/2,
441                    block_w, block_w,
442                    w, h,
443                    w, ref_stride, obmc_stride,
444                    mb_x - 1, mb_y - 1,
445                    add, 1, plane_index);
446     }
447 }
448
449 static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
450     const int mb_h= s->b_height << s->block_max_depth;
451     int mb_y;
452     for(mb_y=0; mb_y<=mb_h; mb_y++)
453         predict_slice(s, buf, plane_index, add, mb_y);
454 }
455
456 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){
457     const int w= s->b_width << s->block_max_depth;
458     const int rem_depth= s->block_max_depth - level;
459     const int index= (x + y*w) << rem_depth;
460     const int block_w= 1<<rem_depth;
461     BlockNode block;
462     int i,j;
463
464     block.color[0]= l;
465     block.color[1]= cb;
466     block.color[2]= cr;
467     block.mx= mx;
468     block.my= my;
469     block.ref= ref;
470     block.type= type;
471     block.level= level;
472
473     for(j=0; j<block_w; j++){
474         for(i=0; i<block_w; i++){
475             s->block[index + i + j*w]= block;
476         }
477     }
478 }
479
480 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){
481     const int offset[3]= {
482           y*c->  stride + x,
483         ((y*c->uvstride + x)>>1),
484         ((y*c->uvstride + x)>>1),
485     };
486     int i;
487     for(i=0; i<3; i++){
488         c->src[0][i]= src [i];
489         c->ref[0][i]= ref [i] + offset[i];
490     }
491     assert(!ref_index);
492 }
493
494
495 /* bitstream functions */
496
497 extern const int8_t ff_quant3bA[256];
498
499 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
500
501 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
502     int i;
503
504     if(v){
505         const int a= FFABS(v);
506         const int e= av_log2(a);
507         const int el= FFMIN(e, 10);
508         put_rac(c, state+0, 0);
509
510         for(i=0; i<el; i++){
511             put_rac(c, state+1+i, 1);  //1..10
512         }
513         for(; i<e; i++){
514             put_rac(c, state+1+9, 1);  //1..10
515         }
516         put_rac(c, state+1+FFMIN(i,9), 0);
517
518         for(i=e-1; i>=el; i--){
519             put_rac(c, state+22+9, (a>>i)&1); //22..31
520         }
521         for(; i>=0; i--){
522             put_rac(c, state+22+i, (a>>i)&1); //22..31
523         }
524
525         if(is_signed)
526             put_rac(c, state+11 + el, v < 0); //11..21
527     }else{
528         put_rac(c, state+0, 1);
529     }
530 }
531
532 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
533     if(get_rac(c, state+0))
534         return 0;
535     else{
536         int i, e, a;
537         e= 0;
538         while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
539             e++;
540         }
541
542         a= 1;
543         for(i=e-1; i>=0; i--){
544             a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
545         }
546
547         e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21
548         return (a^e)-e;
549     }
550 }
551
552 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
553     int i;
554     int r= log2>=0 ? 1<<log2 : 1;
555
556     assert(v>=0);
557     assert(log2>=-4);
558
559     while(v >= r){
560         put_rac(c, state+4+log2, 1);
561         v -= r;
562         log2++;
563         if(log2>0) r+=r;
564     }
565     put_rac(c, state+4+log2, 0);
566
567     for(i=log2-1; i>=0; i--){
568         put_rac(c, state+31-i, (v>>i)&1);
569     }
570 }
571
572 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
573     int i;
574     int r= log2>=0 ? 1<<log2 : 1;
575     int v=0;
576
577     assert(log2>=-4);
578
579     while(get_rac(c, state+4+log2)){
580         v+= r;
581         log2++;
582         if(log2>0) r+=r;
583     }
584
585     for(i=log2-1; i>=0; i--){
586         v+= get_rac(c, state+31-i)<<i;
587     }
588
589     return v;
590 }
591
592 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
593     const int w= b->width;
594     const int h= b->height;
595     int x,y;
596
597     int run, runs;
598     x_and_coeff *xc= b->x_coeff;
599     x_and_coeff *prev_xc= NULL;
600     x_and_coeff *prev2_xc= xc;
601     x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
602     x_and_coeff *prev_parent_xc= parent_xc;
603
604     runs= get_symbol2(&s->c, b->state[30], 0);
605     if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
606     else           run= INT_MAX;
607
608     for(y=0; y<h; y++){
609         int v=0;
610         int lt=0, t=0, rt=0;
611
612         if(y && prev_xc->x == 0){
613             rt= prev_xc->coeff;
614         }
615         for(x=0; x<w; x++){
616             int p=0;
617             const int l= v;
618
619             lt= t; t= rt;
620
621             if(y){
622                 if(prev_xc->x <= x)
623                     prev_xc++;
624                 if(prev_xc->x == x + 1)
625                     rt= prev_xc->coeff;
626                 else
627                     rt=0;
628             }
629             if(parent_xc){
630                 if(x>>1 > parent_xc->x){
631                     parent_xc++;
632                 }
633                 if(x>>1 == parent_xc->x){
634                     p= parent_xc->coeff;
635                 }
636             }
637             if(/*ll|*/l|lt|t|rt|p){
638                 int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
639
640                 v=get_rac(&s->c, &b->state[0][context]);
641                 if(v){
642                     v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
643                     v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3*ff_quant3bA[t&0xFF]]);
644
645                     xc->x=x;
646                     (xc++)->coeff= v;
647                 }
648             }else{
649                 if(!run){
650                     if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
651                     else           run= INT_MAX;
652                     v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
653                     v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
654
655                     xc->x=x;
656                     (xc++)->coeff= v;
657                 }else{
658                     int max_run;
659                     run--;
660                     v=0;
661
662                     if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
663                     else  max_run= FFMIN(run, w-x-1);
664                     if(parent_xc)
665                         max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
666                     x+= max_run;
667                     run-= max_run;
668                 }
669             }
670         }
671         (xc++)->x= w+1; //end marker
672         prev_xc= prev2_xc;
673         prev2_xc= xc;
674
675         if(parent_xc){
676             if(y&1){
677                 while(parent_xc->x != parent->width+1)
678                     parent_xc++;
679                 parent_xc++;
680                 prev_parent_xc= parent_xc;
681             }else{
682                 parent_xc= prev_parent_xc;
683             }
684         }
685     }
686
687     (xc++)->x= w+1; //end marker
688 }
689
690 #endif /* AVCODEC_SNOW_H */