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.

721 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 "hpeldsp.h"
  24. #include "me_cmp.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. MECmpContext mecc;
  100. HpelDSPContext hdsp;
  101. QpelDSPContext qdsp;
  102. VideoDSPContext vdsp;
  103. H264QpelContext h264qpel;
  104. MpegvideoEncDSPContext mpvencdsp;
  105. SnowDWTContext dwt;
  106. const 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. // When src_stride is large enough, it is possible to interleave the blocks.
  267. // Otherwise the blocks are written sequentially in the tmp buffer.
  268. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  269. uint8_t *tmp = s->scratchbuf;
  270. uint8_t *ptmp;
  271. int x,y;
  272. if(b_x<0){
  273. lt= rt;
  274. lb= rb;
  275. }else if(b_x + 1 >= b_width){
  276. rt= lt;
  277. rb= lb;
  278. }
  279. if(b_y<0){
  280. lt= lb;
  281. rt= rb;
  282. }else if(b_y + 1 >= b_height){
  283. lb= lt;
  284. rb= rt;
  285. }
  286. if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
  287. obmc -= src_x;
  288. b_w += src_x;
  289. if(!sliced && !offset_dst)
  290. dst -= src_x;
  291. src_x=0;
  292. }
  293. if(src_x + b_w > w){
  294. b_w = w - src_x;
  295. }
  296. if(src_y<0){
  297. obmc -= src_y*obmc_stride;
  298. b_h += src_y;
  299. if(!sliced && !offset_dst)
  300. dst -= src_y*dst_stride;
  301. src_y=0;
  302. }
  303. if(src_y + b_h> h){
  304. b_h = h - src_y;
  305. }
  306. if(b_w<=0 || b_h<=0) return;
  307. if(!sliced && offset_dst)
  308. dst += src_x + src_y*dst_stride;
  309. dst8+= src_x + src_y*src_stride;
  310. // src += src_x + src_y*src_stride;
  311. ptmp= tmp + 3*tmp_step;
  312. block[0]= ptmp;
  313. ptmp+=tmp_step;
  314. ff_snow_pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  315. if(same_block(lt, rt)){
  316. block[1]= block[0];
  317. }else{
  318. block[1]= ptmp;
  319. ptmp+=tmp_step;
  320. ff_snow_pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  321. }
  322. if(same_block(lt, lb)){
  323. block[2]= block[0];
  324. }else if(same_block(rt, lb)){
  325. block[2]= block[1];
  326. }else{
  327. block[2]= ptmp;
  328. ptmp+=tmp_step;
  329. ff_snow_pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  330. }
  331. if(same_block(lt, rb) ){
  332. block[3]= block[0];
  333. }else if(same_block(rt, rb)){
  334. block[3]= block[1];
  335. }else if(same_block(lb, rb)){
  336. block[3]= block[2];
  337. }else{
  338. block[3]= ptmp;
  339. ff_snow_pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  340. }
  341. if(sliced){
  342. s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  343. }else{
  344. for(y=0; y<b_h; y++){
  345. //FIXME ugly misuse of obmc_stride
  346. const uint8_t *obmc1= obmc + y*obmc_stride;
  347. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  348. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  349. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  350. for(x=0; x<b_w; x++){
  351. int v= obmc1[x] * block[3][x + y*src_stride]
  352. +obmc2[x] * block[2][x + y*src_stride]
  353. +obmc3[x] * block[1][x + y*src_stride]
  354. +obmc4[x] * block[0][x + y*src_stride];
  355. v <<= 8 - LOG2_OBMC_MAX;
  356. if(FRAC_BITS != 8){
  357. v >>= 8 - FRAC_BITS;
  358. }
  359. if(add){
  360. v += dst[x + y*dst_stride];
  361. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  362. if(v&(~255)) v= ~(v>>31);
  363. dst8[x + y*src_stride] = v;
  364. }else{
  365. dst[x + y*dst_stride] -= v;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
  372. Plane *p= &s->plane[plane_index];
  373. const int mb_w= s->b_width << s->block_max_depth;
  374. const int mb_h= s->b_height << s->block_max_depth;
  375. int x, y, mb_x;
  376. int block_size = MB_SIZE >> s->block_max_depth;
  377. int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  378. int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  379. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  380. const int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  381. int ref_stride= s->current_picture->linesize[plane_index];
  382. uint8_t *dst8= s->current_picture->data[plane_index];
  383. int w= p->width;
  384. int h= p->height;
  385. av_assert2(s->chroma_h_shift == s->chroma_v_shift); // obmc params assume squares
  386. if(s->keyframe || (s->avctx->debug&512)){
  387. if(mb_y==mb_h)
  388. return;
  389. if(add){
  390. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  391. for(x=0; x<w; x++){
  392. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  393. v >>= FRAC_BITS;
  394. if(v&(~255)) v= ~(v>>31);
  395. dst8[x + y*ref_stride]= v;
  396. }
  397. }
  398. }else{
  399. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  400. for(x=0; x<w; x++){
  401. buf[x + y*w]-= 128<<FRAC_BITS;
  402. }
  403. }
  404. }
  405. return;
  406. }
  407. for(mb_x=0; mb_x<=mb_w; mb_x++){
  408. add_yblock(s, 0, NULL, buf, dst8, obmc,
  409. block_w*mb_x - block_w/2,
  410. block_h*mb_y - block_h/2,
  411. block_w, block_h,
  412. w, h,
  413. w, ref_stride, obmc_stride,
  414. mb_x - 1, mb_y - 1,
  415. add, 1, plane_index);
  416. }
  417. }
  418. static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
  419. const int mb_h= s->b_height << s->block_max_depth;
  420. int mb_y;
  421. for(mb_y=0; mb_y<=mb_h; mb_y++)
  422. predict_slice(s, buf, plane_index, add, mb_y);
  423. }
  424. 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){
  425. const int w= s->b_width << s->block_max_depth;
  426. const int rem_depth= s->block_max_depth - level;
  427. const int index= (x + y*w) << rem_depth;
  428. const int block_w= 1<<rem_depth;
  429. const int block_h= 1<<rem_depth; //FIXME "w!=h"
  430. BlockNode block;
  431. int i,j;
  432. block.color[0]= l;
  433. block.color[1]= cb;
  434. block.color[2]= cr;
  435. block.mx= mx;
  436. block.my= my;
  437. block.ref= ref;
  438. block.type= type;
  439. block.level= level;
  440. for(j=0; j<block_h; j++){
  441. for(i=0; i<block_w; i++){
  442. s->block[index + i + j*w]= block;
  443. }
  444. }
  445. }
  446. 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){
  447. SnowContext *s = c->avctx->priv_data;
  448. const int offset[3]= {
  449. y*c-> stride + x,
  450. ((y*c->uvstride + x)>>s->chroma_h_shift),
  451. ((y*c->uvstride + x)>>s->chroma_h_shift),
  452. };
  453. int i;
  454. for(i=0; i<3; i++){
  455. c->src[0][i]= src [i];
  456. c->ref[0][i]= ref [i] + offset[i];
  457. }
  458. av_assert2(!ref_index);
  459. }
  460. /* bitstream functions */
  461. extern const int8_t ff_quant3bA[256];
  462. #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
  463. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  464. int i;
  465. if(v){
  466. const int a= FFABS(v);
  467. const int e= av_log2(a);
  468. const int el= FFMIN(e, 10);
  469. put_rac(c, state+0, 0);
  470. for(i=0; i<el; i++){
  471. put_rac(c, state+1+i, 1); //1..10
  472. }
  473. for(; i<e; i++){
  474. put_rac(c, state+1+9, 1); //1..10
  475. }
  476. put_rac(c, state+1+FFMIN(i,9), 0);
  477. for(i=e-1; i>=el; i--){
  478. put_rac(c, state+22+9, (a>>i)&1); //22..31
  479. }
  480. for(; i>=0; i--){
  481. put_rac(c, state+22+i, (a>>i)&1); //22..31
  482. }
  483. if(is_signed)
  484. put_rac(c, state+11 + el, v < 0); //11..21
  485. }else{
  486. put_rac(c, state+0, 1);
  487. }
  488. }
  489. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  490. if(get_rac(c, state+0))
  491. return 0;
  492. else{
  493. int i, e;
  494. unsigned a;
  495. e= 0;
  496. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  497. e++;
  498. if (e > 31)
  499. return AVERROR_INVALIDDATA;
  500. }
  501. a= 1;
  502. for(i=e-1; i>=0; i--){
  503. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  504. }
  505. e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21
  506. return (a^e)-e;
  507. }
  508. }
  509. static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
  510. int i;
  511. int r= log2>=0 ? 1<<log2 : 1;
  512. av_assert2(v>=0);
  513. av_assert2(log2>=-4);
  514. while(v >= r){
  515. put_rac(c, state+4+log2, 1);
  516. v -= r;
  517. log2++;
  518. if(log2>0) r+=r;
  519. }
  520. put_rac(c, state+4+log2, 0);
  521. for(i=log2-1; i>=0; i--){
  522. put_rac(c, state+31-i, (v>>i)&1);
  523. }
  524. }
  525. static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
  526. int i;
  527. int r= log2>=0 ? 1<<log2 : 1;
  528. int v=0;
  529. av_assert2(log2>=-4);
  530. while(log2<28 && get_rac(c, state+4+log2)){
  531. v+= r;
  532. log2++;
  533. if(log2>0) r+=r;
  534. }
  535. for(i=log2-1; i>=0; i--){
  536. v+= get_rac(c, state+31-i)<<i;
  537. }
  538. return v;
  539. }
  540. static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
  541. const int w= b->width;
  542. const int h= b->height;
  543. int x,y;
  544. int run, runs;
  545. x_and_coeff *xc= b->x_coeff;
  546. x_and_coeff *prev_xc= NULL;
  547. x_and_coeff *prev2_xc= xc;
  548. x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
  549. x_and_coeff *prev_parent_xc= parent_xc;
  550. runs= get_symbol2(&s->c, b->state[30], 0);
  551. if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
  552. else run= INT_MAX;
  553. for(y=0; y<h; y++){
  554. int v=0;
  555. int lt=0, t=0, rt=0;
  556. if(y && prev_xc->x == 0){
  557. rt= prev_xc->coeff;
  558. }
  559. for(x=0; x<w; x++){
  560. int p=0;
  561. const int l= v;
  562. lt= t; t= rt;
  563. if(y){
  564. if(prev_xc->x <= x)
  565. prev_xc++;
  566. if(prev_xc->x == x + 1)
  567. rt= prev_xc->coeff;
  568. else
  569. rt=0;
  570. }
  571. if(parent_xc){
  572. if(x>>1 > parent_xc->x){
  573. parent_xc++;
  574. }
  575. if(x>>1 == parent_xc->x){
  576. p= parent_xc->coeff;
  577. }
  578. }
  579. if(/*ll|*/l|lt|t|rt|p){
  580. int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
  581. v=get_rac(&s->c, &b->state[0][context]);
  582. if(v){
  583. v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
  584. v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3*ff_quant3bA[t&0xFF]]);
  585. if ((uint16_t)v != v) {
  586. av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
  587. v = 1;
  588. }
  589. xc->x=x;
  590. (xc++)->coeff= v;
  591. }
  592. }else{
  593. if(!run){
  594. if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
  595. else run= INT_MAX;
  596. v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
  597. v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
  598. if ((uint16_t)v != v) {
  599. av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
  600. v = 1;
  601. }
  602. xc->x=x;
  603. (xc++)->coeff= v;
  604. }else{
  605. int max_run;
  606. run--;
  607. v=0;
  608. av_assert2(run >= 0);
  609. if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
  610. else max_run= FFMIN(run, w-x-1);
  611. if(parent_xc)
  612. max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
  613. av_assert2(max_run >= 0 && max_run <= run);
  614. x+= max_run;
  615. run-= max_run;
  616. }
  617. }
  618. }
  619. (xc++)->x= w+1; //end marker
  620. prev_xc= prev2_xc;
  621. prev2_xc= xc;
  622. if(parent_xc){
  623. if(y&1){
  624. while(parent_xc->x != parent->width+1)
  625. parent_xc++;
  626. parent_xc++;
  627. prev_parent_xc= parent_xc;
  628. }else{
  629. parent_xc= prev_parent_xc;
  630. }
  631. }
  632. }
  633. (xc++)->x= w+1; //end marker
  634. }
  635. #endif /* AVCODEC_SNOW_H */