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.

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