You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

689 lines
20KB

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