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