annotate ffmpeg/libavcodec/snowdec.c @ 13:844d341cf643 tip

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