snowdec.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/emms.h"
  21. #include "libavutil/intmath.h"
  22. #include "libavutil/log.h"
  23. #include "libavutil/mem.h"
  24. #include "avcodec.h"
  25. #include "codec_internal.h"
  26. #include "decode.h"
  27. #include "snow_dwt.h"
  28. #include "snow.h"
  29. #include "rangecoder.h"
  30. #include "mathops.h"
  31. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
  32. {
  33. if (get_rac(c, state + 0))
  34. return 0;
  35. else {
  36. int e;
  37. unsigned a;
  38. e = 0;
  39. while (get_rac(c, state + 1 + FFMIN(e, 9))) { //1..10
  40. e++;
  41. if (e > 31)
  42. return AVERROR_INVALIDDATA;
  43. }
  44. a = 1;
  45. for (int i = e - 1; i >= 0; i--)
  46. a += a + get_rac(c, state + 22 + FFMIN(i, 9)); //22..31
  47. e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); //11..21
  48. return (a ^ e) - e;
  49. }
  50. }
  51. static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2)
  52. {
  53. int r = log2 >= 0 ? 1 << log2 : 1;
  54. int v = 0;
  55. av_assert2(log2 >= -4);
  56. while (log2 < 28 && get_rac(c, state + 4 + log2)) {
  57. v += r;
  58. log2++;
  59. if (log2 > 0) r += r;
  60. }
  61. for (int i = log2 - 1; i >= 0; i--)
  62. v += get_rac(c, state + 31 - i) << i;
  63. return v;
  64. }
  65. static void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation)
  66. {
  67. const int w = b->width;
  68. const int h = b->height;
  69. int run, runs;
  70. x_and_coeff *xc = b->x_coeff;
  71. x_and_coeff *prev_xc = NULL;
  72. x_and_coeff *prev2_xc = xc;
  73. x_and_coeff *parent_xc = parent ? parent->x_coeff : NULL;
  74. x_and_coeff *prev_parent_xc = parent_xc;
  75. runs = get_symbol2(&s->c, b->state[30], 0);
  76. if (runs-- > 0) run = get_symbol2(&s->c, b->state[1], 3);
  77. else run = INT_MAX;
  78. for (int y = 0; y < h; y++) {
  79. int v = 0;
  80. int lt = 0, t = 0, rt = 0;
  81. if (y && prev_xc->x == 0)
  82. rt = prev_xc->coeff;
  83. for (int x = 0; x < w; x++) {
  84. int p = 0;
  85. const int l = v;
  86. lt= t; t= rt;
  87. if (y) {
  88. if (prev_xc->x <= x)
  89. prev_xc++;
  90. if (prev_xc->x == x + 1)
  91. rt = prev_xc->coeff;
  92. else
  93. rt = 0;
  94. }
  95. if (parent_xc) {
  96. if (x>>1 > parent_xc->x)
  97. parent_xc++;
  98. if (x>>1 == parent_xc->x)
  99. p = parent_xc->coeff;
  100. }
  101. if (/*ll|*/l|lt|t|rt|p) {
  102. int context = av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
  103. v = get_rac(&s->c, &b->state[0][context]);
  104. if (v) {
  105. v = 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
  106. v += get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3 * ff_quant3bA[t&0xFF]]);
  107. if ((uint16_t)v != v) {
  108. av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
  109. v = 1;
  110. }
  111. xc->x = x;
  112. (xc++)->coeff = v;
  113. }
  114. } else {
  115. if (!run) {
  116. if (runs-- > 0) run = get_symbol2(&s->c, b->state[1], 3);
  117. else run = INT_MAX;
  118. v = 2 * (get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
  119. v += get_rac(&s->c, &b->state[0][16 + 1 + 3]);
  120. if ((uint16_t)v != v) {
  121. av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
  122. v = 1;
  123. }
  124. xc->x = x;
  125. (xc++)->coeff = v;
  126. } else {
  127. int max_run;
  128. run--;
  129. v = 0;
  130. av_assert2(run >= 0);
  131. if (y) max_run = FFMIN(run, prev_xc->x - x - 2);
  132. else max_run = FFMIN(run, w-x-1);
  133. if (parent_xc)
  134. max_run = FFMIN(max_run, 2*parent_xc->x - x - 1);
  135. av_assert2(max_run >= 0 && max_run <= run);
  136. x += max_run;
  137. run -= max_run;
  138. }
  139. }
  140. }
  141. (xc++)->x = w+1; //end marker
  142. prev_xc = prev2_xc;
  143. prev2_xc = xc;
  144. if (parent_xc) {
  145. if (y & 1) {
  146. while (parent_xc->x != parent->width+1)
  147. parent_xc++;
  148. parent_xc++;
  149. prev_parent_xc= parent_xc;
  150. } else {
  151. parent_xc= prev_parent_xc;
  152. }
  153. }
  154. }
  155. (xc++)->x = w + 1; //end marker
  156. }
  157. static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
  158. Plane *p= &s->plane[plane_index];
  159. const int mb_w= s->b_width << s->block_max_depth;
  160. const int mb_h= s->b_height << s->block_max_depth;
  161. int x, y, mb_x;
  162. int block_size = MB_SIZE >> s->block_max_depth;
  163. int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  164. int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  165. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  166. int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  167. int ref_stride= s->current_picture->linesize[plane_index];
  168. uint8_t *dst8= s->current_picture->data[plane_index];
  169. int w= p->width;
  170. int h= p->height;
  171. if(s->keyframe || (s->avctx->debug&512)){
  172. if(mb_y==mb_h)
  173. return;
  174. if(add){
  175. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  176. // DWTELEM * line = slice_buffer_get_line(sb, y);
  177. IDWTELEM * line = sb->line[y];
  178. for(x=0; x<w; x++){
  179. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  180. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  181. v >>= FRAC_BITS;
  182. if(v&(~255)) v= ~(v>>31);
  183. dst8[x + y*ref_stride]= v;
  184. }
  185. }
  186. }else{
  187. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  188. // DWTELEM * line = slice_buffer_get_line(sb, y);
  189. IDWTELEM * line = sb->line[y];
  190. for(x=0; x<w; x++){
  191. line[x] -= 128 << FRAC_BITS;
  192. // buf[x + y*w]-= 128<<FRAC_BITS;
  193. }
  194. }
  195. }
  196. return;
  197. }
  198. for(mb_x=0; mb_x<=mb_w; mb_x++){
  199. add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  200. block_w*mb_x - block_w/2,
  201. block_h*mb_y - block_h/2,
  202. block_w, block_h,
  203. w, h,
  204. w, ref_stride, obmc_stride,
  205. mb_x - 1, mb_y - 1,
  206. add, 0, plane_index);
  207. }
  208. if(s->avmv && mb_y < mb_h && plane_index == 0)
  209. for(mb_x=0; mb_x<mb_w; mb_x++){
  210. AVMotionVector *avmv = s->avmv + s->avmv_index;
  211. const int b_width = s->b_width << s->block_max_depth;
  212. const int b_stride= b_width;
  213. BlockNode *bn= &s->block[mb_x + mb_y*b_stride];
  214. if (bn->type)
  215. continue;
  216. s->avmv_index++;
  217. avmv->w = block_w;
  218. avmv->h = block_h;
  219. avmv->dst_x = block_w*mb_x - block_w/2;
  220. avmv->dst_y = block_h*mb_y - block_h/2;
  221. avmv->motion_scale = 8;
  222. avmv->motion_x = bn->mx * s->mv_scale;
  223. avmv->motion_y = bn->my * s->mv_scale;
  224. avmv->src_x = avmv->dst_x + avmv->motion_x / 8;
  225. avmv->src_y = avmv->dst_y + avmv->motion_y / 8;
  226. avmv->source= -1 - bn->ref;
  227. avmv->flags = 0;
  228. }
  229. }
  230. static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
  231. const int w= b->width;
  232. int y;
  233. const int qlog= av_clip(s->qlog + (int64_t)b->qlog, 0, QROOT*16);
  234. int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  235. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  236. int new_index = 0;
  237. if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
  238. qadd= 0;
  239. qmul= 1<<QEXPSHIFT;
  240. }
  241. /* If we are on the second or later slice, restore our index. */
  242. if (start_y != 0)
  243. new_index = save_state[0];
  244. for(y=start_y; y<h; y++){
  245. int x = 0;
  246. int v;
  247. IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
  248. memset(line, 0, b->width*sizeof(IDWTELEM));
  249. v = b->x_coeff[new_index].coeff;
  250. x = b->x_coeff[new_index++].x;
  251. while(x < w){
  252. register int t= (int)( (v>>1)*(unsigned)qmul + qadd)>>QEXPSHIFT;
  253. register int u= -(v&1);
  254. line[x] = (t^u) - u;
  255. v = b->x_coeff[new_index].coeff;
  256. x = b->x_coeff[new_index++].x;
  257. }
  258. }
  259. /* Save our variables for the next slice. */
  260. save_state[0] = new_index;
  261. return;
  262. }
  263. static int decode_q_branch(SnowContext *s, int level, int x, int y){
  264. const int w= s->b_width << s->block_max_depth;
  265. const int rem_depth= s->block_max_depth - level;
  266. const int index= (x + y*w) << rem_depth;
  267. int trx= (x+1)<<rem_depth;
  268. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  269. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  270. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  271. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  272. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  273. int res;
  274. if(s->keyframe){
  275. set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
  276. return 0;
  277. }
  278. if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
  279. int type, mx, my;
  280. int l = left->color[0];
  281. int cb= left->color[1];
  282. int cr= left->color[2];
  283. unsigned ref = 0;
  284. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  285. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
  286. int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
  287. type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  288. if(type){
  289. int ld, cbd, crd;
  290. pred_mv(s, &mx, &my, 0, left, top, tr);
  291. ld = get_symbol(&s->c, &s->block_state[32], 1);
  292. if (ld < -255 || ld > 255) {
  293. return AVERROR_INVALIDDATA;
  294. }
  295. l += ld;
  296. if (s->nb_planes > 2) {
  297. cbd = get_symbol(&s->c, &s->block_state[64], 1);
  298. crd = get_symbol(&s->c, &s->block_state[96], 1);
  299. if (cbd < -255 || cbd > 255 || crd < -255 || crd > 255) {
  300. return AVERROR_INVALIDDATA;
  301. }
  302. cb += cbd;
  303. cr += crd;
  304. }
  305. }else{
  306. if(s->ref_frames > 1)
  307. ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
  308. if (ref >= s->ref_frames) {
  309. av_log(s->avctx, AV_LOG_ERROR, "Invalid ref\n");
  310. return AVERROR_INVALIDDATA;
  311. }
  312. pred_mv(s, &mx, &my, ref, left, top, tr);
  313. mx+= (unsigned)get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
  314. my+= (unsigned)get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
  315. }
  316. set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
  317. }else{
  318. if ((res = decode_q_branch(s, level+1, 2*x+0, 2*y+0)) < 0 ||
  319. (res = decode_q_branch(s, level+1, 2*x+1, 2*y+0)) < 0 ||
  320. (res = decode_q_branch(s, level+1, 2*x+0, 2*y+1)) < 0 ||
  321. (res = decode_q_branch(s, level+1, 2*x+1, 2*y+1)) < 0)
  322. return res;
  323. }
  324. return 0;
  325. }
  326. static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
  327. const int w= b->width;
  328. const int qlog= av_clip(s->qlog + (int64_t)b->qlog, 0, QROOT*16);
  329. const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  330. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  331. int x,y;
  332. if(s->qlog == LOSSLESS_QLOG) return;
  333. for(y=start_y; y<end_y; y++){
  334. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  335. IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  336. for(x=0; x<w; x++){
  337. int i= line[x];
  338. if(i<0){
  339. line[x]= -((-i*(unsigned)qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  340. }else if(i>0){
  341. line[x]= (( i*(unsigned)qmul + qadd)>>(QEXPSHIFT));
  342. }
  343. }
  344. }
  345. }
  346. static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){
  347. const int w= b->width;
  348. int x,y;
  349. IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning
  350. IDWTELEM * prev;
  351. if (start_y != 0)
  352. line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  353. for(y=start_y; y<end_y; y++){
  354. prev = line;
  355. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  356. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  357. for(x=0; x<w; x++){
  358. if(x){
  359. if(use_median){
  360. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  361. else line[x] += line[x - 1];
  362. }else{
  363. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  364. else line[x] += line[x - 1];
  365. }
  366. }else{
  367. if(y) line[x] += prev[x];
  368. }
  369. }
  370. }
  371. }
  372. static void decode_qlogs(SnowContext *s){
  373. int plane_index, level, orientation;
  374. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  375. for(level=0; level<s->spatial_decomposition_count; level++){
  376. for(orientation=level ? 1:0; orientation<4; orientation++){
  377. int q;
  378. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  379. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  380. else q= get_symbol(&s->c, s->header_state, 1);
  381. s->plane[plane_index].band[level][orientation].qlog= q;
  382. }
  383. }
  384. }
  385. }
  386. #define GET_S(dst, check) \
  387. tmp= get_symbol(&s->c, s->header_state, 0);\
  388. if(!(check)){\
  389. av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
  390. return AVERROR_INVALIDDATA;\
  391. }\
  392. dst= tmp;
  393. static int decode_header(SnowContext *s){
  394. int plane_index, tmp;
  395. uint8_t kstate[32];
  396. memset(kstate, MID_STATE, sizeof(kstate));
  397. s->keyframe= get_rac(&s->c, kstate);
  398. if(s->keyframe || s->always_reset){
  399. ff_snow_reset_contexts(s);
  400. s->spatial_decomposition_type=
  401. s->qlog=
  402. s->qbias=
  403. s->mv_scale=
  404. s->block_max_depth= 0;
  405. }
  406. if(s->keyframe){
  407. GET_S(s->version, tmp <= 0U)
  408. s->always_reset= get_rac(&s->c, s->header_state);
  409. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  410. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  411. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  412. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  413. if (s->colorspace_type == 1) {
  414. s->avctx->pix_fmt= AV_PIX_FMT_GRAY8;
  415. s->nb_planes = 1;
  416. } else if(s->colorspace_type == 0) {
  417. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  418. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  419. if(s->chroma_h_shift == 1 && s->chroma_v_shift==1){
  420. s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
  421. }else if(s->chroma_h_shift == 0 && s->chroma_v_shift==0){
  422. s->avctx->pix_fmt= AV_PIX_FMT_YUV444P;
  423. }else if(s->chroma_h_shift == 2 && s->chroma_v_shift==2){
  424. s->avctx->pix_fmt= AV_PIX_FMT_YUV410P;
  425. } else {
  426. av_log(s->avctx, AV_LOG_ERROR,
  427. "unsupported color subsample mode %d %d\n",
  428. s->chroma_h_shift, s->chroma_v_shift);
  429. s->chroma_h_shift = s->chroma_v_shift = 1;
  430. s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
  431. return AVERROR_INVALIDDATA;
  432. }
  433. s->nb_planes = 3;
  434. } else {
  435. av_log(s->avctx, AV_LOG_ERROR, "unsupported color space\n");
  436. s->chroma_h_shift = s->chroma_v_shift = 1;
  437. s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
  438. return AVERROR_INVALIDDATA;
  439. }
  440. s->spatial_scalability= get_rac(&s->c, s->header_state);
  441. // s->rate_scalability= get_rac(&s->c, s->header_state);
  442. GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
  443. s->max_ref_frames++;
  444. decode_qlogs(s);
  445. }
  446. if(!s->keyframe){
  447. if(get_rac(&s->c, s->header_state)){
  448. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  449. int htaps, i, sum=0;
  450. Plane *p= &s->plane[plane_index];
  451. p->diag_mc= get_rac(&s->c, s->header_state);
  452. htaps= get_symbol(&s->c, s->header_state, 0);
  453. if((unsigned)htaps >= HTAPS_MAX/2 - 1)
  454. return AVERROR_INVALIDDATA;
  455. htaps = htaps*2 + 2;
  456. p->htaps= htaps;
  457. for(i= htaps/2; i; i--){
  458. unsigned hcoeff = get_symbol(&s->c, s->header_state, 0);
  459. if (hcoeff > 127)
  460. return AVERROR_INVALIDDATA;
  461. p->hcoeff[i]= hcoeff * (1-2*(i&1));
  462. sum += p->hcoeff[i];
  463. }
  464. p->hcoeff[0]= 32-sum;
  465. }
  466. s->plane[2].diag_mc= s->plane[1].diag_mc;
  467. s->plane[2].htaps = s->plane[1].htaps;
  468. memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
  469. }
  470. if(get_rac(&s->c, s->header_state)){
  471. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  472. decode_qlogs(s);
  473. }
  474. }
  475. s->spatial_decomposition_type+= (unsigned)get_symbol(&s->c, s->header_state, 1);
  476. if(s->spatial_decomposition_type > 1U){
  477. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported\n", s->spatial_decomposition_type);
  478. return AVERROR_INVALIDDATA;
  479. }
  480. if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
  481. s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 1){
  482. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size\n", s->spatial_decomposition_count);
  483. return AVERROR_INVALIDDATA;
  484. }
  485. if (s->avctx->width > 65536-4) {
  486. av_log(s->avctx, AV_LOG_ERROR, "Width %d is too large\n", s->avctx->width);
  487. return AVERROR_INVALIDDATA;
  488. }
  489. s->qlog += (unsigned)get_symbol(&s->c, s->header_state, 1);
  490. s->mv_scale += (unsigned)get_symbol(&s->c, s->header_state, 1);
  491. s->qbias += (unsigned)get_symbol(&s->c, s->header_state, 1);
  492. s->block_max_depth+= (unsigned)get_symbol(&s->c, s->header_state, 1);
  493. if(s->block_max_depth > 1 || s->block_max_depth < 0 || s->mv_scale > 256U){
  494. av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large\n", s->block_max_depth);
  495. s->block_max_depth= 0;
  496. s->mv_scale = 0;
  497. return AVERROR_INVALIDDATA;
  498. }
  499. if (FFABS(s->qbias) > 127) {
  500. av_log(s->avctx, AV_LOG_ERROR, "qbias %d is too large\n", s->qbias);
  501. s->qbias = 0;
  502. return AVERROR_INVALIDDATA;
  503. }
  504. return 0;
  505. }
  506. static int decode_blocks(SnowContext *s){
  507. int x, y;
  508. int w= s->b_width;
  509. int h= s->b_height;
  510. int res;
  511. for(y=0; y<h; y++){
  512. for(x=0; x<w; x++){
  513. if (s->c.bytestream >= s->c.bytestream_end)
  514. return AVERROR_INVALIDDATA;
  515. if ((res = decode_q_branch(s, 0, x, y)) < 0)
  516. return res;
  517. }
  518. }
  519. return 0;
  520. }
  521. static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
  522. int *got_frame, AVPacket *avpkt)
  523. {
  524. const uint8_t *buf = avpkt->data;
  525. int buf_size = avpkt->size;
  526. SnowContext *s = avctx->priv_data;
  527. RangeCoder * const c= &s->c;
  528. int bytes_read;
  529. int level, orientation, plane_index;
  530. int res;
  531. ff_init_range_decoder(c, buf, buf_size);
  532. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  533. s->current_picture->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  534. if ((res = decode_header(s)) < 0)
  535. return res;
  536. if (!s->mconly_picture->data[0]) {
  537. res = ff_get_buffer(avctx, s->mconly_picture, AV_GET_BUFFER_FLAG_REF);
  538. if (res < 0)
  539. return res;
  540. }
  541. if (s->mconly_picture->format != avctx->pix_fmt) {
  542. av_log(avctx, AV_LOG_ERROR, "pixel format changed\n");
  543. return AVERROR_INVALIDDATA;
  544. }
  545. if ((res=ff_snow_common_init_after_header(avctx)) < 0)
  546. return res;
  547. // realloc slice buffer for the case that spatial_decomposition_count changed
  548. ff_slice_buffer_destroy(&s->sb);
  549. if ((res = ff_slice_buffer_init(&s->sb, s->plane[0].height,
  550. (MB_SIZE >> s->block_max_depth) +
  551. s->spatial_decomposition_count * 11 + 1,
  552. s->plane[0].width,
  553. s->spatial_idwt_buffer)) < 0)
  554. return res;
  555. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  556. Plane *p= &s->plane[plane_index];
  557. p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
  558. && p->hcoeff[1]==-10
  559. && p->hcoeff[2]==2;
  560. }
  561. ff_snow_alloc_blocks(s);
  562. if ((res = ff_snow_frames_prepare(s)) < 0)
  563. return res;
  564. s->current_picture->width = s->avctx->width;
  565. s->current_picture->height = s->avctx->height;
  566. res = ff_get_buffer(s->avctx, s->current_picture, AV_GET_BUFFER_FLAG_REF);
  567. if (res < 0)
  568. return res;
  569. s->current_picture->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  570. //keyframe flag duplication mess FIXME
  571. if(avctx->debug&FF_DEBUG_PICT_INFO)
  572. av_log(avctx, AV_LOG_ERROR,
  573. "keyframe:%d qlog:%d qbias: %d mvscale: %d "
  574. "decomposition_type:%d decomposition_count:%d\n",
  575. s->keyframe, s->qlog, s->qbias, s->mv_scale,
  576. s->spatial_decomposition_type,
  577. s->spatial_decomposition_count
  578. );
  579. if (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) {
  580. size_t size;
  581. res = av_size_mult(s->b_width * s->b_height, sizeof(AVMotionVector) << (s->block_max_depth*2), &size);
  582. if (res)
  583. return res;
  584. av_fast_malloc(&s->avmv, &s->avmv_size, size);
  585. if (!s->avmv)
  586. return AVERROR(ENOMEM);
  587. } else {
  588. s->avmv_size = 0;
  589. av_freep(&s->avmv);
  590. }
  591. s->avmv_index = 0;
  592. if ((res = decode_blocks(s)) < 0)
  593. return res;
  594. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  595. Plane *p= &s->plane[plane_index];
  596. int w= p->width;
  597. int h= p->height;
  598. int x, y;
  599. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  600. if(s->avctx->debug&2048){
  601. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  602. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  603. for(y=0; y<h; y++){
  604. for(x=0; x<w; x++){
  605. int v= s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x];
  606. s->mconly_picture->data[plane_index][y*s->mconly_picture->linesize[plane_index] + x]= v;
  607. }
  608. }
  609. }
  610. for(level=0; level<s->spatial_decomposition_count; level++){
  611. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  612. SubBand *b= &p->band[level][orientation];
  613. unpack_coeffs(s, b, b->parent, orientation);
  614. }
  615. }
  616. {
  617. const int mb_h= s->b_height << s->block_max_depth;
  618. const int block_size = MB_SIZE >> s->block_max_depth;
  619. const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  620. int mb_y;
  621. DWTCompose cs[MAX_DECOMPOSITIONS];
  622. int yd=0, yq=0;
  623. int y;
  624. int end_y;
  625. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  626. for(mb_y=0; mb_y<=mb_h; mb_y++){
  627. int slice_starty = block_h*mb_y;
  628. int slice_h = block_h*(mb_y+1);
  629. if (!(s->keyframe || s->avctx->debug&512)){
  630. slice_starty = FFMAX(0, slice_starty - (block_h >> 1));
  631. slice_h -= (block_h >> 1);
  632. }
  633. for(level=0; level<s->spatial_decomposition_count; level++){
  634. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  635. SubBand *b= &p->band[level][orientation];
  636. int start_y;
  637. int end_y;
  638. int our_mb_start = mb_y;
  639. int our_mb_end = (mb_y + 1);
  640. const int extra= 3;
  641. start_y = (mb_y ? ((block_h * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  642. end_y = (((block_h * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  643. if (!(s->keyframe || s->avctx->debug&512)){
  644. start_y = FFMAX(0, start_y - (block_h >> (1+s->spatial_decomposition_count - level)));
  645. end_y = FFMAX(0, end_y - (block_h >> (1+s->spatial_decomposition_count - level)));
  646. }
  647. start_y = FFMIN(b->height, start_y);
  648. end_y = FFMIN(b->height, end_y);
  649. if (start_y != end_y){
  650. if (orientation == 0){
  651. SubBand * correlate_band = &p->band[0][0];
  652. int correlate_end_y = FFMIN(b->height, end_y + 1);
  653. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  654. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  655. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  656. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
  657. }
  658. else
  659. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  660. }
  661. }
  662. }
  663. for(; yd<slice_h; yd+=4){
  664. ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, s->temp_idwt_buffer, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  665. }
  666. if(s->qlog == LOSSLESS_QLOG){
  667. for(; yq<slice_h && yq<h; yq++){
  668. IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  669. for(x=0; x<w; x++){
  670. line[x] *= 1<<FRAC_BITS;
  671. }
  672. }
  673. }
  674. predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
  675. y = FFMIN(p->height, slice_starty);
  676. end_y = FFMIN(p->height, slice_h);
  677. while(y < end_y)
  678. ff_slice_buffer_release(&s->sb, y++);
  679. }
  680. ff_slice_buffer_flush(&s->sb);
  681. }
  682. }
  683. emms_c();
  684. av_frame_unref(s->last_picture[s->max_ref_frames - 1]);
  685. if(!(s->avctx->debug&2048))
  686. res = av_frame_ref(picture, s->current_picture);
  687. else
  688. res = av_frame_ref(picture, s->mconly_picture);
  689. if (res >= 0 && s->avmv_index) {
  690. AVFrameSideData *sd;
  691. sd = av_frame_new_side_data(picture, AV_FRAME_DATA_MOTION_VECTORS, s->avmv_index * sizeof(AVMotionVector));
  692. if (!sd)
  693. return AVERROR(ENOMEM);
  694. memcpy(sd->data, s->avmv, s->avmv_index * sizeof(AVMotionVector));
  695. }
  696. if (res < 0)
  697. return res;
  698. *got_frame = 1;
  699. bytes_read= c->bytestream - c->bytestream_start;
  700. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  701. return bytes_read;
  702. }
  703. static av_cold int decode_end(AVCodecContext *avctx)
  704. {
  705. SnowContext *s = avctx->priv_data;
  706. ff_slice_buffer_destroy(&s->sb);
  707. ff_snow_common_end(s);
  708. s->avmv_size = 0;
  709. av_freep(&s->avmv);
  710. return 0;
  711. }
  712. const FFCodec ff_snow_decoder = {
  713. .p.name = "snow",
  714. CODEC_LONG_NAME("Snow"),
  715. .p.type = AVMEDIA_TYPE_VIDEO,
  716. .p.id = AV_CODEC_ID_SNOW,
  717. .priv_data_size = sizeof(SnowContext),
  718. .init = ff_snow_common_init,
  719. .close = decode_end,
  720. FF_CODEC_DECODE_CB(decode_frame),
  721. .p.capabilities = AV_CODEC_CAP_DR1,
  722. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  723. };