snow.h 21 KB

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