00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/intmath.h"
00022 #include "libavutil/log.h"
00023 #include "libavutil/opt.h"
00024 #include "avcodec.h"
00025 #include "dsputil.h"
00026 #include "dwt.h"
00027 #include "snow.h"
00028
00029 #include "rangecoder.h"
00030 #include "mathops.h"
00031
00032 #include "mpegvideo.h"
00033 #include "h263.h"
00034
00035 #undef NDEBUG
00036 #include <assert.h>
00037
00038 static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
00039 Plane *p= &s->plane[plane_index];
00040 const int mb_w= s->b_width << s->block_max_depth;
00041 const int mb_h= s->b_height << s->block_max_depth;
00042 int x, y, mb_x;
00043 int block_size = MB_SIZE >> s->block_max_depth;
00044 int block_w = plane_index ? block_size/2 : block_size;
00045 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+1] : ff_obmc_tab[s->block_max_depth];
00046 int obmc_stride= plane_index ? block_size : 2*block_size;
00047 int ref_stride= s->current_picture.linesize[plane_index];
00048 uint8_t *dst8= s->current_picture.data[plane_index];
00049 int w= p->width;
00050 int h= p->height;
00051
00052 if(s->keyframe || (s->avctx->debug&512)){
00053 if(mb_y==mb_h)
00054 return;
00055
00056 if(add){
00057 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
00058
00059 IDWTELEM * line = sb->line[y];
00060 for(x=0; x<w; x++){
00061
00062 int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
00063 v >>= FRAC_BITS;
00064 if(v&(~255)) v= ~(v>>31);
00065 dst8[x + y*ref_stride]= v;
00066 }
00067 }
00068 }else{
00069 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
00070
00071 IDWTELEM * line = sb->line[y];
00072 for(x=0; x<w; x++){
00073 line[x] -= 128 << FRAC_BITS;
00074
00075 }
00076 }
00077 }
00078
00079 return;
00080 }
00081
00082 for(mb_x=0; mb_x<=mb_w; mb_x++){
00083 add_yblock(s, 1, sb, old_buffer, dst8, obmc,
00084 block_w*mb_x - block_w/2,
00085 block_w*mb_y - block_w/2,
00086 block_w, block_w,
00087 w, h,
00088 w, ref_stride, obmc_stride,
00089 mb_x - 1, mb_y - 1,
00090 add, 0, plane_index);
00091 }
00092 }
00093
00094 static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
00095 const int w= b->width;
00096 int y;
00097 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
00098 int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
00099 int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
00100 int new_index = 0;
00101
00102 if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
00103 qadd= 0;
00104 qmul= 1<<QEXPSHIFT;
00105 }
00106
00107
00108 if (start_y != 0)
00109 new_index = save_state[0];
00110
00111
00112 for(y=start_y; y<h; y++){
00113 int x = 0;
00114 int v;
00115 IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
00116 memset(line, 0, b->width*sizeof(IDWTELEM));
00117 v = b->x_coeff[new_index].coeff;
00118 x = b->x_coeff[new_index++].x;
00119 while(x < w){
00120 register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
00121 register int u= -(v&1);
00122 line[x] = (t^u) - u;
00123
00124 v = b->x_coeff[new_index].coeff;
00125 x = b->x_coeff[new_index++].x;
00126 }
00127 }
00128
00129
00130 save_state[0] = new_index;
00131
00132 return;
00133 }
00134
00135 static int decode_q_branch(SnowContext *s, int level, int x, int y){
00136 const int w= s->b_width << s->block_max_depth;
00137 const int rem_depth= s->block_max_depth - level;
00138 const int index= (x + y*w) << rem_depth;
00139 int trx= (x+1)<<rem_depth;
00140 const BlockNode *left = x ? &s->block[index-1] : &null_block;
00141 const BlockNode *top = y ? &s->block[index-w] : &null_block;
00142 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
00143 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl;
00144 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
00145 int res;
00146
00147 if(s->keyframe){
00148 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);
00149 return 0;
00150 }
00151
00152 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
00153 int type, mx, my;
00154 int l = left->color[0];
00155 int cb= left->color[1];
00156 int cr= left->color[2];
00157 int ref = 0;
00158 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
00159 int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
00160 int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
00161
00162 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
00163
00164 if(type){
00165 pred_mv(s, &mx, &my, 0, left, top, tr);
00166 l += get_symbol(&s->c, &s->block_state[32], 1);
00167 cb+= get_symbol(&s->c, &s->block_state[64], 1);
00168 cr+= get_symbol(&s->c, &s->block_state[96], 1);
00169 }else{
00170 if(s->ref_frames > 1)
00171 ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
00172 if (ref >= s->ref_frames) {
00173 av_log(s->avctx, AV_LOG_ERROR, "Invalid ref\n");
00174 return AVERROR_INVALIDDATA;
00175 }
00176 pred_mv(s, &mx, &my, ref, left, top, tr);
00177 mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
00178 my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
00179 }
00180 set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
00181 }else{
00182 if ((res = decode_q_branch(s, level+1, 2*x+0, 2*y+0)) < 0 ||
00183 (res = decode_q_branch(s, level+1, 2*x+1, 2*y+0)) < 0 ||
00184 (res = decode_q_branch(s, level+1, 2*x+0, 2*y+1)) < 0 ||
00185 (res = decode_q_branch(s, level+1, 2*x+1, 2*y+1)) < 0)
00186 return res;
00187 }
00188 return 0;
00189 }
00190
00191 static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
00192 const int w= b->width;
00193 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
00194 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
00195 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
00196 int x,y;
00197
00198 if(s->qlog == LOSSLESS_QLOG) return;
00199
00200 for(y=start_y; y<end_y; y++){
00201
00202 IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
00203 for(x=0; x<w; x++){
00204 int i= line[x];
00205 if(i<0){
00206 line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT));
00207 }else if(i>0){
00208 line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
00209 }
00210 }
00211 }
00212 }
00213
00214 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){
00215 const int w= b->width;
00216 int x,y;
00217
00218 IDWTELEM * line=0;
00219 IDWTELEM * prev;
00220
00221 if (start_y != 0)
00222 line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
00223
00224 for(y=start_y; y<end_y; y++){
00225 prev = line;
00226
00227 line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
00228 for(x=0; x<w; x++){
00229 if(x){
00230 if(use_median){
00231 if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
00232 else line[x] += line[x - 1];
00233 }else{
00234 if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
00235 else line[x] += line[x - 1];
00236 }
00237 }else{
00238 if(y) line[x] += prev[x];
00239 }
00240 }
00241 }
00242 }
00243
00244 static void decode_qlogs(SnowContext *s){
00245 int plane_index, level, orientation;
00246
00247 for(plane_index=0; plane_index<3; plane_index++){
00248 for(level=0; level<s->spatial_decomposition_count; level++){
00249 for(orientation=level ? 1:0; orientation<4; orientation++){
00250 int q;
00251 if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
00252 else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
00253 else q= get_symbol(&s->c, s->header_state, 1);
00254 s->plane[plane_index].band[level][orientation].qlog= q;
00255 }
00256 }
00257 }
00258 }
00259
00260 #define GET_S(dst, check) \
00261 tmp= get_symbol(&s->c, s->header_state, 0);\
00262 if(!(check)){\
00263 av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
00264 return -1;\
00265 }\
00266 dst= tmp;
00267
00268 static int decode_header(SnowContext *s){
00269 int plane_index, tmp;
00270 uint8_t kstate[32];
00271
00272 memset(kstate, MID_STATE, sizeof(kstate));
00273
00274 s->keyframe= get_rac(&s->c, kstate);
00275 if(s->keyframe || s->always_reset){
00276 ff_snow_reset_contexts(s);
00277 s->spatial_decomposition_type=
00278 s->qlog=
00279 s->qbias=
00280 s->mv_scale=
00281 s->block_max_depth= 0;
00282 }
00283 if(s->keyframe){
00284 GET_S(s->version, tmp <= 0U)
00285 s->always_reset= get_rac(&s->c, s->header_state);
00286 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
00287 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
00288 GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
00289 s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
00290 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
00291 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
00292 s->spatial_scalability= get_rac(&s->c, s->header_state);
00293
00294 GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
00295 s->max_ref_frames++;
00296
00297 decode_qlogs(s);
00298 }
00299
00300 if(!s->keyframe){
00301 if(get_rac(&s->c, s->header_state)){
00302 for(plane_index=0; plane_index<2; plane_index++){
00303 int htaps, i, sum=0;
00304 Plane *p= &s->plane[plane_index];
00305 p->diag_mc= get_rac(&s->c, s->header_state);
00306 htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
00307 if((unsigned)htaps > HTAPS_MAX || htaps==0)
00308 return -1;
00309 p->htaps= htaps;
00310 for(i= htaps/2; i; i--){
00311 p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1));
00312 sum += p->hcoeff[i];
00313 }
00314 p->hcoeff[0]= 32-sum;
00315 }
00316 s->plane[2].diag_mc= s->plane[1].diag_mc;
00317 s->plane[2].htaps = s->plane[1].htaps;
00318 memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
00319 }
00320 if(get_rac(&s->c, s->header_state)){
00321 GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
00322 decode_qlogs(s);
00323 }
00324 }
00325
00326 s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
00327 if(s->spatial_decomposition_type > 1U){
00328 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
00329 return -1;
00330 }
00331 if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
00332 s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 0){
00333 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size", s->spatial_decomposition_count);
00334 return -1;
00335 }
00336
00337 if (s->chroma_h_shift != 1 || s->chroma_v_shift != 1) {
00338 av_log(s->avctx, AV_LOG_ERROR, "Invalid chroma shift\n");
00339 return AVERROR_PATCHWELCOME;
00340 }
00341
00342 s->qlog += get_symbol(&s->c, s->header_state, 1);
00343 s->mv_scale += get_symbol(&s->c, s->header_state, 1);
00344 s->qbias += get_symbol(&s->c, s->header_state, 1);
00345 s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
00346 if(s->block_max_depth > 1 || s->block_max_depth < 0){
00347 av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
00348 s->block_max_depth= 0;
00349 return -1;
00350 }
00351
00352 return 0;
00353 }
00354
00355 static av_cold int decode_init(AVCodecContext *avctx)
00356 {
00357 avctx->pix_fmt= PIX_FMT_YUV420P;
00358
00359 ff_snow_common_init(avctx);
00360
00361 return 0;
00362 }
00363
00364 static int decode_blocks(SnowContext *s){
00365 int x, y;
00366 int w= s->b_width;
00367 int h= s->b_height;
00368 int res;
00369
00370 for(y=0; y<h; y++){
00371 for(x=0; x<w; x++){
00372 if ((res = decode_q_branch(s, 0, x, y)) < 0)
00373 return res;
00374 }
00375 }
00376 return 0;
00377 }
00378
00379 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
00380 const uint8_t *buf = avpkt->data;
00381 int buf_size = avpkt->size;
00382 SnowContext *s = avctx->priv_data;
00383 RangeCoder * const c= &s->c;
00384 int bytes_read;
00385 AVFrame *picture = data;
00386 int level, orientation, plane_index;
00387 int res;
00388
00389 ff_init_range_decoder(c, buf, buf_size);
00390 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00391
00392 s->current_picture.pict_type= AV_PICTURE_TYPE_I;
00393 if(decode_header(s)<0)
00394 return -1;
00395 ff_snow_common_init_after_header(avctx);
00396
00397
00398 ff_slice_buffer_destroy(&s->sb);
00399 if ((res = ff_slice_buffer_init(&s->sb, s->plane[0].height,
00400 (MB_SIZE >> s->block_max_depth) +
00401 s->spatial_decomposition_count * 8 + 1,
00402 s->plane[0].width,
00403 s->spatial_idwt_buffer)) < 0)
00404 return res;
00405
00406 for(plane_index=0; plane_index<3; plane_index++){
00407 Plane *p= &s->plane[plane_index];
00408 p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
00409 && p->hcoeff[1]==-10
00410 && p->hcoeff[2]==2;
00411 }
00412
00413 ff_snow_alloc_blocks(s);
00414
00415 if(ff_snow_frame_start(s) < 0)
00416 return -1;
00417
00418 if(avctx->debug&FF_DEBUG_PICT_INFO)
00419 av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
00420
00421 if ((res = decode_blocks(s)) < 0)
00422 return res;
00423
00424 for(plane_index=0; plane_index<3; plane_index++){
00425 Plane *p= &s->plane[plane_index];
00426 int w= p->width;
00427 int h= p->height;
00428 int x, y;
00429 int decode_state[MAX_DECOMPOSITIONS][4][1];
00430
00431 if(s->avctx->debug&2048){
00432 memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
00433 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
00434
00435 for(y=0; y<h; y++){
00436 for(x=0; x<w; x++){
00437 int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
00438 s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
00439 }
00440 }
00441 }
00442
00443 {
00444 for(level=0; level<s->spatial_decomposition_count; level++){
00445 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00446 SubBand *b= &p->band[level][orientation];
00447 unpack_coeffs(s, b, b->parent, orientation);
00448 }
00449 }
00450 }
00451
00452 {
00453 const int mb_h= s->b_height << s->block_max_depth;
00454 const int block_size = MB_SIZE >> s->block_max_depth;
00455 const int block_w = plane_index ? block_size/2 : block_size;
00456 int mb_y;
00457 DWTCompose cs[MAX_DECOMPOSITIONS];
00458 int yd=0, yq=0;
00459 int y;
00460 int end_y;
00461
00462 ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
00463 for(mb_y=0; mb_y<=mb_h; mb_y++){
00464
00465 int slice_starty = block_w*mb_y;
00466 int slice_h = block_w*(mb_y+1);
00467 if (!(s->keyframe || s->avctx->debug&512)){
00468 slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
00469 slice_h -= (block_w >> 1);
00470 }
00471
00472 for(level=0; level<s->spatial_decomposition_count; level++){
00473 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00474 SubBand *b= &p->band[level][orientation];
00475 int start_y;
00476 int end_y;
00477 int our_mb_start = mb_y;
00478 int our_mb_end = (mb_y + 1);
00479 const int extra= 3;
00480 start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
00481 end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
00482 if (!(s->keyframe || s->avctx->debug&512)){
00483 start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
00484 end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
00485 }
00486 start_y = FFMIN(b->height, start_y);
00487 end_y = FFMIN(b->height, end_y);
00488
00489 if (start_y != end_y){
00490 if (orientation == 0){
00491 SubBand * correlate_band = &p->band[0][0];
00492 int correlate_end_y = FFMIN(b->height, end_y + 1);
00493 int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
00494 decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
00495 correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
00496 dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
00497 }
00498 else
00499 decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
00500 }
00501 }
00502 }
00503
00504 for(; yd<slice_h; yd+=4){
00505 ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
00506 }
00507
00508 if(s->qlog == LOSSLESS_QLOG){
00509 for(; yq<slice_h && yq<h; yq++){
00510 IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
00511 for(x=0; x<w; x++){
00512 line[x] <<= FRAC_BITS;
00513 }
00514 }
00515 }
00516
00517 predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
00518
00519 y = FFMIN(p->height, slice_starty);
00520 end_y = FFMIN(p->height, slice_h);
00521 while(y < end_y)
00522 ff_slice_buffer_release(&s->sb, y++);
00523 }
00524
00525 ff_slice_buffer_flush(&s->sb);
00526 }
00527
00528 }
00529
00530 emms_c();
00531
00532 ff_snow_release_buffer(avctx);
00533
00534 if(!(s->avctx->debug&2048))
00535 *picture= s->current_picture;
00536 else
00537 *picture= s->mconly_picture;
00538
00539 *data_size = sizeof(AVFrame);
00540
00541 bytes_read= c->bytestream - c->bytestream_start;
00542 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n");
00543
00544 return bytes_read;
00545 }
00546
00547 static av_cold int decode_end(AVCodecContext *avctx)
00548 {
00549 SnowContext *s = avctx->priv_data;
00550
00551 ff_slice_buffer_destroy(&s->sb);
00552
00553 ff_snow_common_end(s);
00554
00555 return 0;
00556 }
00557
00558 AVCodec ff_snow_decoder = {
00559 .name = "snow",
00560 .type = AVMEDIA_TYPE_VIDEO,
00561 .id = CODEC_ID_SNOW,
00562 .priv_data_size = sizeof(SnowContext),
00563 .init = decode_init,
00564 .close = decode_end,
00565 .decode = decode_frame,
00566 .capabilities = CODEC_CAP_DR1 ,
00567 .long_name = NULL_IF_CONFIG_SMALL("Snow"),
00568 };