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