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 ? obmc_tab[s->block_max_depth+1] : 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= 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 void 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
00146 if(s->keyframe){
00147 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);
00148 return;
00149 }
00150
00151 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
00152 int type, mx, my;
00153 int l = left->color[0];
00154 int cb= left->color[1];
00155 int cr= left->color[2];
00156 int ref = 0;
00157 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
00158 int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
00159 int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
00160
00161 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
00162
00163 if(type){
00164 pred_mv(s, &mx, &my, 0, left, top, tr);
00165 l += get_symbol(&s->c, &s->block_state[32], 1);
00166 cb+= get_symbol(&s->c, &s->block_state[64], 1);
00167 cr+= get_symbol(&s->c, &s->block_state[96], 1);
00168 }else{
00169 if(s->ref_frames > 1)
00170 ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
00171 pred_mv(s, &mx, &my, ref, left, top, tr);
00172 mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
00173 my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
00174 }
00175 set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
00176 }else{
00177 decode_q_branch(s, level+1, 2*x+0, 2*y+0);
00178 decode_q_branch(s, level+1, 2*x+1, 2*y+0);
00179 decode_q_branch(s, level+1, 2*x+0, 2*y+1);
00180 decode_q_branch(s, level+1, 2*x+1, 2*y+1);
00181 }
00182 }
00183
00184 static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
00185 const int w= b->width;
00186 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
00187 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
00188 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
00189 int x,y;
00190
00191 if(s->qlog == LOSSLESS_QLOG) return;
00192
00193 for(y=start_y; y<end_y; y++){
00194
00195 IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
00196 for(x=0; x<w; x++){
00197 int i= line[x];
00198 if(i<0){
00199 line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT));
00200 }else if(i>0){
00201 line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
00202 }
00203 }
00204 }
00205 }
00206
00207 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){
00208 const int w= b->width;
00209 int x,y;
00210
00211 IDWTELEM * line=0;
00212 IDWTELEM * prev;
00213
00214 if (start_y != 0)
00215 line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
00216
00217 for(y=start_y; y<end_y; y++){
00218 prev = line;
00219
00220 line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
00221 for(x=0; x<w; x++){
00222 if(x){
00223 if(use_median){
00224 if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
00225 else line[x] += line[x - 1];
00226 }else{
00227 if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
00228 else line[x] += line[x - 1];
00229 }
00230 }else{
00231 if(y) line[x] += prev[x];
00232 }
00233 }
00234 }
00235 }
00236
00237 static void decode_qlogs(SnowContext *s){
00238 int plane_index, level, orientation;
00239
00240 for(plane_index=0; plane_index<3; plane_index++){
00241 for(level=0; level<s->spatial_decomposition_count; level++){
00242 for(orientation=level ? 1:0; orientation<4; orientation++){
00243 int q;
00244 if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
00245 else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
00246 else q= get_symbol(&s->c, s->header_state, 1);
00247 s->plane[plane_index].band[level][orientation].qlog= q;
00248 }
00249 }
00250 }
00251 }
00252
00253 #define GET_S(dst, check) \
00254 tmp= get_symbol(&s->c, s->header_state, 0);\
00255 if(!(check)){\
00256 av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
00257 return -1;\
00258 }\
00259 dst= tmp;
00260
00261 static int decode_header(SnowContext *s){
00262 int plane_index, tmp;
00263 uint8_t kstate[32];
00264
00265 memset(kstate, MID_STATE, sizeof(kstate));
00266
00267 s->keyframe= get_rac(&s->c, kstate);
00268 if(s->keyframe || s->always_reset){
00269 ff_snow_reset_contexts(s);
00270 s->spatial_decomposition_type=
00271 s->qlog=
00272 s->qbias=
00273 s->mv_scale=
00274 s->block_max_depth= 0;
00275 }
00276 if(s->keyframe){
00277 GET_S(s->version, tmp <= 0U)
00278 s->always_reset= get_rac(&s->c, s->header_state);
00279 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
00280 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
00281 GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
00282 s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
00283 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
00284 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
00285 s->spatial_scalability= get_rac(&s->c, s->header_state);
00286
00287 GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
00288 s->max_ref_frames++;
00289
00290 decode_qlogs(s);
00291 }
00292
00293 if(!s->keyframe){
00294 if(get_rac(&s->c, s->header_state)){
00295 for(plane_index=0; plane_index<2; plane_index++){
00296 int htaps, i, sum=0;
00297 Plane *p= &s->plane[plane_index];
00298 p->diag_mc= get_rac(&s->c, s->header_state);
00299 htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
00300 if((unsigned)htaps > HTAPS_MAX || htaps==0)
00301 return -1;
00302 p->htaps= htaps;
00303 for(i= htaps/2; i; i--){
00304 p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1));
00305 sum += p->hcoeff[i];
00306 }
00307 p->hcoeff[0]= 32-sum;
00308 }
00309 s->plane[2].diag_mc= s->plane[1].diag_mc;
00310 s->plane[2].htaps = s->plane[1].htaps;
00311 memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
00312 }
00313 if(get_rac(&s->c, s->header_state)){
00314 GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
00315 decode_qlogs(s);
00316 }
00317 }
00318
00319 s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
00320 if(s->spatial_decomposition_type > 1U){
00321 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
00322 return -1;
00323 }
00324 if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
00325 s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 0){
00326 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size", s->spatial_decomposition_count);
00327 return -1;
00328 }
00329
00330 s->qlog += get_symbol(&s->c, s->header_state, 1);
00331 s->mv_scale += get_symbol(&s->c, s->header_state, 1);
00332 s->qbias += get_symbol(&s->c, s->header_state, 1);
00333 s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
00334 if(s->block_max_depth > 1 || s->block_max_depth < 0){
00335 av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
00336 s->block_max_depth= 0;
00337 return -1;
00338 }
00339
00340 return 0;
00341 }
00342
00343 static av_cold int decode_init(AVCodecContext *avctx)
00344 {
00345 avctx->pix_fmt= PIX_FMT_YUV420P;
00346
00347 ff_snow_common_init(avctx);
00348
00349 return 0;
00350 }
00351
00352 static void decode_blocks(SnowContext *s){
00353 int x, y;
00354 int w= s->b_width;
00355 int h= s->b_height;
00356
00357 for(y=0; y<h; y++){
00358 for(x=0; x<w; x++){
00359 decode_q_branch(s, 0, x, y);
00360 }
00361 }
00362 }
00363
00364 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
00365 const uint8_t *buf = avpkt->data;
00366 int buf_size = avpkt->size;
00367 SnowContext *s = avctx->priv_data;
00368 RangeCoder * const c= &s->c;
00369 int bytes_read;
00370 AVFrame *picture = data;
00371 int level, orientation, plane_index;
00372
00373 ff_init_range_decoder(c, buf, buf_size);
00374 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
00375
00376 s->current_picture.pict_type= AV_PICTURE_TYPE_I;
00377 if(decode_header(s)<0)
00378 return -1;
00379 ff_snow_common_init_after_header(avctx);
00380
00381
00382 ff_slice_buffer_destroy(&s->sb);
00383 ff_slice_buffer_init(&s->sb, s->plane[0].height, (MB_SIZE >> s->block_max_depth) + s->spatial_decomposition_count * 8 + 1, s->plane[0].width, s->spatial_idwt_buffer);
00384
00385 for(plane_index=0; plane_index<3; plane_index++){
00386 Plane *p= &s->plane[plane_index];
00387 p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
00388 && p->hcoeff[1]==-10
00389 && p->hcoeff[2]==2;
00390 }
00391
00392 ff_snow_alloc_blocks(s);
00393
00394 if(ff_snow_frame_start(s) < 0)
00395 return -1;
00396
00397 if(avctx->debug&FF_DEBUG_PICT_INFO)
00398 av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
00399
00400 decode_blocks(s);
00401
00402 for(plane_index=0; plane_index<3; plane_index++){
00403 Plane *p= &s->plane[plane_index];
00404 int w= p->width;
00405 int h= p->height;
00406 int x, y;
00407 int decode_state[MAX_DECOMPOSITIONS][4][1];
00408
00409 if(s->avctx->debug&2048){
00410 memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
00411 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
00412
00413 for(y=0; y<h; y++){
00414 for(x=0; x<w; x++){
00415 int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
00416 s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
00417 }
00418 }
00419 }
00420
00421 {
00422 for(level=0; level<s->spatial_decomposition_count; level++){
00423 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00424 SubBand *b= &p->band[level][orientation];
00425 unpack_coeffs(s, b, b->parent, orientation);
00426 }
00427 }
00428 }
00429
00430 {
00431 const int mb_h= s->b_height << s->block_max_depth;
00432 const int block_size = MB_SIZE >> s->block_max_depth;
00433 const int block_w = plane_index ? block_size/2 : block_size;
00434 int mb_y;
00435 DWTCompose cs[MAX_DECOMPOSITIONS];
00436 int yd=0, yq=0;
00437 int y;
00438 int end_y;
00439
00440 ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
00441 for(mb_y=0; mb_y<=mb_h; mb_y++){
00442
00443 int slice_starty = block_w*mb_y;
00444 int slice_h = block_w*(mb_y+1);
00445 if (!(s->keyframe || s->avctx->debug&512)){
00446 slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
00447 slice_h -= (block_w >> 1);
00448 }
00449
00450 for(level=0; level<s->spatial_decomposition_count; level++){
00451 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00452 SubBand *b= &p->band[level][orientation];
00453 int start_y;
00454 int end_y;
00455 int our_mb_start = mb_y;
00456 int our_mb_end = (mb_y + 1);
00457 const int extra= 3;
00458 start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
00459 end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
00460 if (!(s->keyframe || s->avctx->debug&512)){
00461 start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
00462 end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
00463 }
00464 start_y = FFMIN(b->height, start_y);
00465 end_y = FFMIN(b->height, end_y);
00466
00467 if (start_y != end_y){
00468 if (orientation == 0){
00469 SubBand * correlate_band = &p->band[0][0];
00470 int correlate_end_y = FFMIN(b->height, end_y + 1);
00471 int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
00472 decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
00473 correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
00474 dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
00475 }
00476 else
00477 decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
00478 }
00479 }
00480 }
00481
00482 for(; yd<slice_h; yd+=4){
00483 ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
00484 }
00485
00486 if(s->qlog == LOSSLESS_QLOG){
00487 for(; yq<slice_h && yq<h; yq++){
00488 IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
00489 for(x=0; x<w; x++){
00490 line[x] <<= FRAC_BITS;
00491 }
00492 }
00493 }
00494
00495 predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
00496
00497 y = FFMIN(p->height, slice_starty);
00498 end_y = FFMIN(p->height, slice_h);
00499 while(y < end_y)
00500 ff_slice_buffer_release(&s->sb, y++);
00501 }
00502
00503 ff_slice_buffer_flush(&s->sb);
00504 }
00505
00506 }
00507
00508 emms_c();
00509
00510 ff_snow_release_buffer(avctx);
00511
00512 if(!(s->avctx->debug&2048))
00513 *picture= s->current_picture;
00514 else
00515 *picture= s->mconly_picture;
00516
00517 *data_size = sizeof(AVFrame);
00518
00519 bytes_read= c->bytestream - c->bytestream_start;
00520 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n");
00521
00522 return bytes_read;
00523 }
00524
00525 static av_cold int decode_end(AVCodecContext *avctx)
00526 {
00527 SnowContext *s = avctx->priv_data;
00528
00529 ff_slice_buffer_destroy(&s->sb);
00530
00531 ff_snow_common_end(s);
00532
00533 return 0;
00534 }
00535
00536 AVCodec ff_snow_decoder = {
00537 .name = "snow",
00538 .type = AVMEDIA_TYPE_VIDEO,
00539 .id = CODEC_ID_SNOW,
00540 .priv_data_size = sizeof(SnowContext),
00541 .init = decode_init,
00542 .close = decode_end,
00543 .decode = decode_frame,
00544 .capabilities = CODEC_CAP_DR1 ,
00545 .long_name = NULL_IF_CONFIG_SMALL("Snow"),
00546 };