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.

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