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.

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