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.

704 lines
21KB

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