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.

649 lines
24KB

  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/intmath.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/opt.h"
  23. #include "avcodec.h"
  24. #include "snow_dwt.h"
  25. #include "internal.h"
  26. #include "snow.h"
  27. #include "rangecoder.h"
  28. #include "mathops.h"
  29. #include "mpegvideo.h"
  30. #include "h263.h"
  31. static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
  32. Plane *p= &s->plane[plane_index];
  33. const int mb_w= s->b_width << s->block_max_depth;
  34. const int mb_h= s->b_height << s->block_max_depth;
  35. int x, y, mb_x;
  36. int block_size = MB_SIZE >> s->block_max_depth;
  37. int block_w = plane_index ? block_size>>s->chroma_h_shift : block_size;
  38. int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  39. const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
  40. int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
  41. int ref_stride= s->current_picture->linesize[plane_index];
  42. uint8_t *dst8= s->current_picture->data[plane_index];
  43. int w= p->width;
  44. int h= p->height;
  45. if(s->keyframe || (s->avctx->debug&512)){
  46. if(mb_y==mb_h)
  47. return;
  48. if(add){
  49. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  50. // DWTELEM * line = slice_buffer_get_line(sb, y);
  51. IDWTELEM * line = sb->line[y];
  52. for(x=0; x<w; x++){
  53. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  54. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  55. v >>= FRAC_BITS;
  56. if(v&(~255)) v= ~(v>>31);
  57. dst8[x + y*ref_stride]= v;
  58. }
  59. }
  60. }else{
  61. for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
  62. // DWTELEM * line = slice_buffer_get_line(sb, y);
  63. IDWTELEM * line = sb->line[y];
  64. for(x=0; x<w; x++){
  65. line[x] -= 128 << FRAC_BITS;
  66. // buf[x + y*w]-= 128<<FRAC_BITS;
  67. }
  68. }
  69. }
  70. return;
  71. }
  72. for(mb_x=0; mb_x<=mb_w; mb_x++){
  73. add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  74. block_w*mb_x - block_w/2,
  75. block_h*mb_y - block_h/2,
  76. block_w, block_h,
  77. w, h,
  78. w, ref_stride, obmc_stride,
  79. mb_x - 1, mb_y - 1,
  80. add, 0, plane_index);
  81. }
  82. if(s->avmv && mb_y < mb_h && plane_index == 0)
  83. for(mb_x=0; mb_x<mb_w; mb_x++){
  84. AVMotionVector *avmv = s->avmv + s->avmv_index;
  85. const int b_width = s->b_width << s->block_max_depth;
  86. const int b_stride= b_width;
  87. BlockNode *bn= &s->block[mb_x + mb_y*b_stride];
  88. if (bn->type)
  89. continue;
  90. s->avmv_index++;
  91. avmv->w = block_w;
  92. avmv->h = block_h;
  93. avmv->dst_x = block_w*mb_x - block_w/2;
  94. avmv->dst_y = block_h*mb_y - block_h/2;
  95. avmv->src_x = avmv->dst_x + (bn->mx * s->mv_scale)/8;
  96. avmv->src_y = avmv->dst_y + (bn->my * s->mv_scale)/8;
  97. avmv->source= -1 - bn->ref;
  98. avmv->flags = 0;
  99. }
  100. }
  101. static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
  102. const int w= b->width;
  103. int y;
  104. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  105. int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  106. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  107. int new_index = 0;
  108. if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
  109. qadd= 0;
  110. qmul= 1<<QEXPSHIFT;
  111. }
  112. /* If we are on the second or later slice, restore our index. */
  113. if (start_y != 0)
  114. new_index = save_state[0];
  115. for(y=start_y; y<h; y++){
  116. int x = 0;
  117. int v;
  118. IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
  119. memset(line, 0, b->width*sizeof(IDWTELEM));
  120. v = b->x_coeff[new_index].coeff;
  121. x = b->x_coeff[new_index++].x;
  122. while(x < w){
  123. register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
  124. register int u= -(v&1);
  125. line[x] = (t^u) - u;
  126. v = b->x_coeff[new_index].coeff;
  127. x = b->x_coeff[new_index++].x;
  128. }
  129. }
  130. /* Save our variables for the next slice. */
  131. save_state[0] = new_index;
  132. return;
  133. }
  134. static int decode_q_branch(SnowContext *s, int level, int x, int y){
  135. const int w= s->b_width << s->block_max_depth;
  136. const int rem_depth= s->block_max_depth - level;
  137. const int index= (x + y*w) << rem_depth;
  138. int trx= (x+1)<<rem_depth;
  139. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  140. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  141. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  142. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  143. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  144. int res;
  145. if(s->keyframe){
  146. 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);
  147. return 0;
  148. }
  149. if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
  150. int type, mx, my;
  151. int l = left->color[0];
  152. int cb= left->color[1];
  153. int cr= left->color[2];
  154. unsigned ref = 0;
  155. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  156. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
  157. int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
  158. type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  159. if(type){
  160. pred_mv(s, &mx, &my, 0, left, top, tr);
  161. l += get_symbol(&s->c, &s->block_state[32], 1);
  162. if (s->nb_planes > 2) {
  163. cb+= get_symbol(&s->c, &s->block_state[64], 1);
  164. cr+= get_symbol(&s->c, &s->block_state[96], 1);
  165. }
  166. }else{
  167. if(s->ref_frames > 1)
  168. ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
  169. if (ref >= s->ref_frames) {
  170. av_log(s->avctx, AV_LOG_ERROR, "Invalid ref\n");
  171. return AVERROR_INVALIDDATA;
  172. }
  173. pred_mv(s, &mx, &my, ref, left, top, tr);
  174. mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
  175. my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
  176. }
  177. set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
  178. }else{
  179. if ((res = decode_q_branch(s, level+1, 2*x+0, 2*y+0)) < 0 ||
  180. (res = decode_q_branch(s, level+1, 2*x+1, 2*y+0)) < 0 ||
  181. (res = decode_q_branch(s, level+1, 2*x+0, 2*y+1)) < 0 ||
  182. (res = decode_q_branch(s, level+1, 2*x+1, 2*y+1)) < 0)
  183. return res;
  184. }
  185. return 0;
  186. }
  187. static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
  188. const int w= b->width;
  189. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  190. const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  191. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  192. int x,y;
  193. if(s->qlog == LOSSLESS_QLOG) return;
  194. for(y=start_y; y<end_y; y++){
  195. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  196. IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  197. for(x=0; x<w; x++){
  198. int i= line[x];
  199. if(i<0){
  200. line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  201. }else if(i>0){
  202. line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
  203. }
  204. }
  205. }
  206. }
  207. 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){
  208. const int w= b->width;
  209. int x,y;
  210. IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning
  211. IDWTELEM * prev;
  212. if (start_y != 0)
  213. line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  214. for(y=start_y; y<end_y; y++){
  215. prev = line;
  216. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  217. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  218. for(x=0; x<w; x++){
  219. if(x){
  220. if(use_median){
  221. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  222. else line[x] += line[x - 1];
  223. }else{
  224. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  225. else line[x] += line[x - 1];
  226. }
  227. }else{
  228. if(y) line[x] += prev[x];
  229. }
  230. }
  231. }
  232. }
  233. static void decode_qlogs(SnowContext *s){
  234. int plane_index, level, orientation;
  235. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  236. for(level=0; level<s->spatial_decomposition_count; level++){
  237. for(orientation=level ? 1:0; orientation<4; orientation++){
  238. int q;
  239. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  240. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  241. else q= get_symbol(&s->c, s->header_state, 1);
  242. s->plane[plane_index].band[level][orientation].qlog= q;
  243. }
  244. }
  245. }
  246. }
  247. #define GET_S(dst, check) \
  248. tmp= get_symbol(&s->c, s->header_state, 0);\
  249. if(!(check)){\
  250. av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
  251. return AVERROR_INVALIDDATA;\
  252. }\
  253. dst= tmp;
  254. static int decode_header(SnowContext *s){
  255. int plane_index, tmp;
  256. uint8_t kstate[32];
  257. memset(kstate, MID_STATE, sizeof(kstate));
  258. s->keyframe= get_rac(&s->c, kstate);
  259. if(s->keyframe || s->always_reset){
  260. ff_snow_reset_contexts(s);
  261. s->spatial_decomposition_type=
  262. s->qlog=
  263. s->qbias=
  264. s->mv_scale=
  265. s->block_max_depth= 0;
  266. }
  267. if(s->keyframe){
  268. GET_S(s->version, tmp <= 0U)
  269. s->always_reset= get_rac(&s->c, s->header_state);
  270. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  271. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  272. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  273. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  274. if (s->colorspace_type == 1) {
  275. s->avctx->pix_fmt= AV_PIX_FMT_GRAY8;
  276. s->nb_planes = 1;
  277. } else if(s->colorspace_type == 0) {
  278. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  279. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  280. if(s->chroma_h_shift == 1 && s->chroma_v_shift==1){
  281. s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
  282. }else if(s->chroma_h_shift == 0 && s->chroma_v_shift==0){
  283. s->avctx->pix_fmt= AV_PIX_FMT_YUV444P;
  284. }else if(s->chroma_h_shift == 2 && s->chroma_v_shift==2){
  285. s->avctx->pix_fmt= AV_PIX_FMT_YUV410P;
  286. } else {
  287. av_log(s, AV_LOG_ERROR, "unsupported color subsample mode %d %d\n", s->chroma_h_shift, s->chroma_v_shift);
  288. s->chroma_h_shift = s->chroma_v_shift = 1;
  289. s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
  290. return AVERROR_INVALIDDATA;
  291. }
  292. s->nb_planes = 3;
  293. } else {
  294. av_log(s, AV_LOG_ERROR, "unsupported color space\n");
  295. s->chroma_h_shift = s->chroma_v_shift = 1;
  296. s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
  297. return AVERROR_INVALIDDATA;
  298. }
  299. s->spatial_scalability= get_rac(&s->c, s->header_state);
  300. // s->rate_scalability= get_rac(&s->c, s->header_state);
  301. GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
  302. s->max_ref_frames++;
  303. decode_qlogs(s);
  304. }
  305. if(!s->keyframe){
  306. if(get_rac(&s->c, s->header_state)){
  307. for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
  308. int htaps, i, sum=0;
  309. Plane *p= &s->plane[plane_index];
  310. p->diag_mc= get_rac(&s->c, s->header_state);
  311. htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
  312. if((unsigned)htaps > HTAPS_MAX || htaps==0)
  313. return AVERROR_INVALIDDATA;
  314. p->htaps= htaps;
  315. for(i= htaps/2; i; i--){
  316. p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1));
  317. sum += p->hcoeff[i];
  318. }
  319. p->hcoeff[0]= 32-sum;
  320. }
  321. s->plane[2].diag_mc= s->plane[1].diag_mc;
  322. s->plane[2].htaps = s->plane[1].htaps;
  323. memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
  324. }
  325. if(get_rac(&s->c, s->header_state)){
  326. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  327. decode_qlogs(s);
  328. }
  329. }
  330. s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
  331. if(s->spatial_decomposition_type > 1U){
  332. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported\n", s->spatial_decomposition_type);
  333. return AVERROR_INVALIDDATA;
  334. }
  335. if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
  336. s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 1){
  337. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size\n", s->spatial_decomposition_count);
  338. return AVERROR_INVALIDDATA;
  339. }
  340. s->qlog += get_symbol(&s->c, s->header_state, 1);
  341. s->mv_scale += get_symbol(&s->c, s->header_state, 1);
  342. s->qbias += get_symbol(&s->c, s->header_state, 1);
  343. s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
  344. if(s->block_max_depth > 1 || s->block_max_depth < 0){
  345. av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large\n", s->block_max_depth);
  346. s->block_max_depth= 0;
  347. return AVERROR_INVALIDDATA;
  348. }
  349. return 0;
  350. }
  351. static av_cold int decode_init(AVCodecContext *avctx)
  352. {
  353. int ret;
  354. if ((ret = ff_snow_common_init(avctx)) < 0) {
  355. return ret;
  356. }
  357. return 0;
  358. }
  359. static int decode_blocks(SnowContext *s){
  360. int x, y;
  361. int w= s->b_width;
  362. int h= s->b_height;
  363. int res;
  364. for(y=0; y<h; y++){
  365. for(x=0; x<w; x++){
  366. if ((res = decode_q_branch(s, 0, x, y)) < 0)
  367. return res;
  368. }
  369. }
  370. return 0;
  371. }
  372. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  373. AVPacket *avpkt)
  374. {
  375. const uint8_t *buf = avpkt->data;
  376. int buf_size = avpkt->size;
  377. SnowContext *s = avctx->priv_data;
  378. RangeCoder * const c= &s->c;
  379. int bytes_read;
  380. AVFrame *picture = data;
  381. int level, orientation, plane_index;
  382. int res;
  383. ff_init_range_decoder(c, buf, buf_size);
  384. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  385. s->current_picture->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  386. if ((res = decode_header(s)) < 0)
  387. return res;
  388. if ((res=ff_snow_common_init_after_header(avctx)) < 0)
  389. return res;
  390. // realloc slice buffer for the case that spatial_decomposition_count changed
  391. ff_slice_buffer_destroy(&s->sb);
  392. if ((res = ff_slice_buffer_init(&s->sb, s->plane[0].height,
  393. (MB_SIZE >> s->block_max_depth) +
  394. s->spatial_decomposition_count * 11 + 1,
  395. s->plane[0].width,
  396. s->spatial_idwt_buffer)) < 0)
  397. return res;
  398. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  399. Plane *p= &s->plane[plane_index];
  400. p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
  401. && p->hcoeff[1]==-10
  402. && p->hcoeff[2]==2;
  403. }
  404. ff_snow_alloc_blocks(s);
  405. if((res = ff_snow_frame_start(s)) < 0)
  406. return res;
  407. s->current_picture->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  408. //keyframe flag duplication mess FIXME
  409. if(avctx->debug&FF_DEBUG_PICT_INFO)
  410. av_log(avctx, AV_LOG_ERROR,
  411. "keyframe:%d qlog:%d qbias: %d mvscale: %d "
  412. "decomposition_type:%d decomposition_count:%d\n",
  413. s->keyframe, s->qlog, s->qbias, s->mv_scale,
  414. s->spatial_decomposition_type,
  415. s->spatial_decomposition_count
  416. );
  417. av_assert0(!s->avmv);
  418. if (s->avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
  419. s->avmv = av_malloc_array(s->b_width * s->b_height, sizeof(AVMotionVector) << (s->block_max_depth*2));
  420. }
  421. s->avmv_index = 0;
  422. if ((res = decode_blocks(s)) < 0)
  423. return res;
  424. for(plane_index=0; plane_index < s->nb_planes; plane_index++){
  425. Plane *p= &s->plane[plane_index];
  426. int w= p->width;
  427. int h= p->height;
  428. int x, y;
  429. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  430. if(s->avctx->debug&2048){
  431. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  432. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  433. for(y=0; y<h; y++){
  434. for(x=0; x<w; x++){
  435. int v= s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x];
  436. s->mconly_picture->data[plane_index][y*s->mconly_picture->linesize[plane_index] + x]= v;
  437. }
  438. }
  439. }
  440. {
  441. for(level=0; level<s->spatial_decomposition_count; level++){
  442. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  443. SubBand *b= &p->band[level][orientation];
  444. unpack_coeffs(s, b, b->parent, orientation);
  445. }
  446. }
  447. }
  448. {
  449. const int mb_h= s->b_height << s->block_max_depth;
  450. const int block_size = MB_SIZE >> s->block_max_depth;
  451. const int block_h = plane_index ? block_size>>s->chroma_v_shift : block_size;
  452. int mb_y;
  453. DWTCompose cs[MAX_DECOMPOSITIONS];
  454. int yd=0, yq=0;
  455. int y;
  456. int end_y;
  457. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  458. for(mb_y=0; mb_y<=mb_h; mb_y++){
  459. int slice_starty = block_h*mb_y;
  460. int slice_h = block_h*(mb_y+1);
  461. if (!(s->keyframe || s->avctx->debug&512)){
  462. slice_starty = FFMAX(0, slice_starty - (block_h >> 1));
  463. slice_h -= (block_h >> 1);
  464. }
  465. for(level=0; level<s->spatial_decomposition_count; level++){
  466. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  467. SubBand *b= &p->band[level][orientation];
  468. int start_y;
  469. int end_y;
  470. int our_mb_start = mb_y;
  471. int our_mb_end = (mb_y + 1);
  472. const int extra= 3;
  473. start_y = (mb_y ? ((block_h * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  474. end_y = (((block_h * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  475. if (!(s->keyframe || s->avctx->debug&512)){
  476. start_y = FFMAX(0, start_y - (block_h >> (1+s->spatial_decomposition_count - level)));
  477. end_y = FFMAX(0, end_y - (block_h >> (1+s->spatial_decomposition_count - level)));
  478. }
  479. start_y = FFMIN(b->height, start_y);
  480. end_y = FFMIN(b->height, end_y);
  481. if (start_y != end_y){
  482. if (orientation == 0){
  483. SubBand * correlate_band = &p->band[0][0];
  484. int correlate_end_y = FFMIN(b->height, end_y + 1);
  485. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  486. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  487. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  488. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
  489. }
  490. else
  491. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  492. }
  493. }
  494. }
  495. for(; yd<slice_h; yd+=4){
  496. 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);
  497. }
  498. if(s->qlog == LOSSLESS_QLOG){
  499. for(; yq<slice_h && yq<h; yq++){
  500. IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  501. for(x=0; x<w; x++){
  502. line[x] <<= FRAC_BITS;
  503. }
  504. }
  505. }
  506. predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
  507. y = FFMIN(p->height, slice_starty);
  508. end_y = FFMIN(p->height, slice_h);
  509. while(y < end_y)
  510. ff_slice_buffer_release(&s->sb, y++);
  511. }
  512. ff_slice_buffer_flush(&s->sb);
  513. }
  514. }
  515. emms_c();
  516. ff_snow_release_buffer(avctx);
  517. if(!(s->avctx->debug&2048))
  518. res = av_frame_ref(picture, s->current_picture);
  519. else
  520. res = av_frame_ref(picture, s->mconly_picture);
  521. if (res >= 0 && s->avmv_index) {
  522. AVFrameSideData *sd;
  523. sd = av_frame_new_side_data(picture, AV_FRAME_DATA_MOTION_VECTORS, s->avmv_index * sizeof(AVMotionVector));
  524. if (!sd)
  525. return AVERROR(ENOMEM);
  526. memcpy(sd->data, s->avmv, s->avmv_index * sizeof(AVMotionVector));
  527. }
  528. av_freep(&s->avmv);
  529. if (res < 0)
  530. return res;
  531. *got_frame = 1;
  532. bytes_read= c->bytestream - c->bytestream_start;
  533. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  534. return bytes_read;
  535. }
  536. static av_cold int decode_end(AVCodecContext *avctx)
  537. {
  538. SnowContext *s = avctx->priv_data;
  539. ff_slice_buffer_destroy(&s->sb);
  540. ff_snow_common_end(s);
  541. return 0;
  542. }
  543. AVCodec ff_snow_decoder = {
  544. .name = "snow",
  545. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  546. .type = AVMEDIA_TYPE_VIDEO,
  547. .id = AV_CODEC_ID_SNOW,
  548. .priv_data_size = sizeof(SnowContext),
  549. .init = decode_init,
  550. .close = decode_end,
  551. .decode = decode_frame,
  552. .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
  553. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  554. FF_CODEC_CAP_INIT_CLEANUP,
  555. };