Mercurial > libavcodec.hg
comparison snow.c @ 2156:f06fb6fc0ff9 libavcodec
cleanup
author | michael |
---|---|
date | Mon, 02 Aug 2004 01:06:55 +0000 |
parents | 274a01d80f4a |
children | 7f42295c1517 |
comparison
equal
deleted
inserted
replaced
2155:274a01d80f4a | 2156:f06fb6fc0ff9 |
---|---|
324 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, | 324 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
325 //error:0.000022 | 325 //error:0.000022 |
326 }; | 326 }; |
327 #endif | 327 #endif |
328 | 328 |
329 typedef struct QTree{ | |
330 int treedim[MAX_DECOMPOSITIONS][2]; | |
331 uint8_t *tree[MAX_DECOMPOSITIONS]; | |
332 int max_level; | |
333 int stride; | |
334 }QTree; | |
335 | |
336 typedef struct SubBand{ | 329 typedef struct SubBand{ |
337 int level; | 330 int level; |
338 int stride; | 331 int stride; |
339 int width; | 332 int width; |
340 int height; | 333 int height; |
341 int qlog; ///< log(qscale)/log[2^(1/6)] | 334 int qlog; ///< log(qscale)/log[2^(1/6)] |
342 DWTELEM *buf; | 335 DWTELEM *buf; |
343 QTree tree; | |
344 struct SubBand *parent; | 336 struct SubBand *parent; |
345 uint8_t state[/*7*2*/ 7 + 512][32]; | 337 uint8_t state[/*7*2*/ 7 + 512][32]; |
346 }SubBand; | 338 }SubBand; |
347 | 339 |
348 typedef struct Plane{ | 340 typedef struct Plane{ |
1315 | 1307 |
1316 if(da[1] != db[1]) return da[1] - db[1]; | 1308 if(da[1] != db[1]) return da[1] - db[1]; |
1317 else return da[0] - db[0]; | 1309 else return da[0] - db[0]; |
1318 } | 1310 } |
1319 | 1311 |
1320 static int alloc_qtree(QTree *t, int w, int h){ | |
1321 int lev, x, y, tree_h; | |
1322 int w2= w; | |
1323 int h2= h; | |
1324 | |
1325 t->stride=0; | |
1326 t->max_level= av_log2(2*FFMAX(w,h)-1); | |
1327 | |
1328 for(lev=t->max_level; lev>=0; lev--){ | |
1329 if(lev!=t->max_level) | |
1330 t->stride += w2; | |
1331 t->treedim[lev][0]= w2; | |
1332 t->treedim[lev][1]= h2; | |
1333 av_log(NULL, AV_LOG_DEBUG, "alloc %p %d %d %d\n", t, w2, h2, t->max_level); | |
1334 w2= (w2+1)>>1; | |
1335 h2= (h2+1)>>1; | |
1336 } | |
1337 t->stride= FFMAX(t->stride, w); | |
1338 tree_h= h + t->treedim[t->max_level-1][1]; | |
1339 | |
1340 t->tree[t->max_level]= av_mallocz(t->stride * tree_h); | |
1341 t->tree[t->max_level-1]= t->tree[t->max_level] + h*t->stride; | |
1342 | |
1343 for(lev=t->max_level-2; lev>=0; lev--){ | |
1344 t->tree[lev]= t->tree[lev+1] + t->treedim[lev+1][0]; | |
1345 } | |
1346 return 0; | |
1347 } | |
1348 | |
1349 static void free_qtree(QTree *t){ | |
1350 if(t && t->tree); | |
1351 av_freep(&t->tree[t->max_level]); | |
1352 } | |
1353 | |
1354 static void init_quandtree(QTree *t, DWTELEM *src, int w, int h, int stride){ | |
1355 const int max_level= t->max_level; | |
1356 const int tree_stride= t->stride; | |
1357 uint8_t **tree= t->tree; | |
1358 int lev, x, y, tree_h, w2, h2; | |
1359 | |
1360 //av_log(NULL, AV_LOG_DEBUG, "init %p %d %d %d %d %d\n", t, w, h, t->max_level, t->treedim[max_level][0], t->treedim[max_level][1]); | |
1361 assert(w==t->treedim[max_level][0]); | |
1362 assert(h==t->treedim[max_level][1]); | |
1363 | |
1364 for(y=0; y<h; y++){ | |
1365 for(x=0; x<w; x++){ | |
1366 tree[max_level][x + y*tree_stride]= clip(ABS(src[x + y*stride]), 0, 16); | |
1367 } | |
1368 } | |
1369 | |
1370 for(lev=max_level-1; lev>=0; lev--){ | |
1371 w2= t->treedim[lev+1][0]>>1; | |
1372 h2= t->treedim[lev+1][1]>>1; | |
1373 for(y=0; y<h2; y++){ | |
1374 for(x=0; x<w2; x++){ | |
1375 tree[lev][x + y*tree_stride]=clip( (tree[lev+1][2*x + 2*y *tree_stride]) | |
1376 + (tree[lev+1][2*x + 1 + 2*y *tree_stride]) | |
1377 + (tree[lev+1][2*x + (2*y+1)*tree_stride]) | |
1378 + (tree[lev+1][2*x + 1 + (2*y+1)*tree_stride])+3, 0, 64)/4; | |
1379 } | |
1380 } | |
1381 if(w2 != t->treedim[lev][0]){ | |
1382 for(y=0; y<h2; y++){ | |
1383 tree[lev][w2 + y*tree_stride]=clip( (tree[lev+1][2*w2 + 2*y *tree_stride]) | |
1384 +(tree[lev+1][2*w2 + (2*y+1)*tree_stride])+3, 0, 64)/4; | |
1385 } | |
1386 } | |
1387 if(h2 != t->treedim[lev][1]){ | |
1388 for(x=0; x<w2; x++){ | |
1389 tree[lev][x + h2*tree_stride]=clip( (tree[lev+1][2*x + 2*h2*tree_stride]) | |
1390 +(tree[lev+1][2*x + 1 + 2*h2*tree_stride])+3, 0, 64)/4; | |
1391 } | |
1392 } | |
1393 if(w2 != t->treedim[lev][0] && h2 != t->treedim[lev][1]){ | |
1394 tree[lev][w2 + h2*tree_stride]= tree[lev+1][2*w2 + 2*h2*tree_stride]; | |
1395 } | |
1396 } | |
1397 } | |
1398 | |
1399 int white_leaf, gray_leaf; | |
1400 | |
1401 static void encode_branch(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int lev, int x, int y, int first){ | |
1402 const int max_level= b->tree.max_level; | |
1403 const int pmax_level= b->parent ? b->parent->tree.max_level : 0; | |
1404 const int tree_stride= b->tree.stride; | |
1405 const int ptree_stride= b->parent ? b->parent->tree.stride : 0; | |
1406 int (*treedim)[2]= b->tree.treedim; | |
1407 int (*ptreedim)[2]= b->parent ? b->parent->tree.treedim : NULL; | |
1408 uint8_t **tree= b->tree.tree; | |
1409 uint8_t **ptree= b->parent ? b->parent->tree.tree : NULL; | |
1410 // int w2=w, h2=h; | |
1411 | |
1412 int l=0, t=0, lt=0, p=0; | |
1413 int v= tree[lev][x + y*tree_stride]; | |
1414 int context, sig; | |
1415 if(!first && !tree[lev-1][x/2 + y/2*tree_stride]) | |
1416 return; | |
1417 | |
1418 if(x) l= tree[lev][x - 1 + y*tree_stride]; | |
1419 if(y){ | |
1420 t= tree[lev][x + (y-1)*tree_stride]; | |
1421 if(x) lt= tree[lev][x - 1 + (y-1)*tree_stride]; | |
1422 } | |
1423 if(lev < max_level && parent && x<ptreedim[lev][0] && y<ptreedim[lev][1]) | |
1424 p= ptree[lev - max_level + pmax_level + 1][x + y*ptree_stride]; | |
1425 | |
1426 if(lev != max_level) | |
1427 context= lev + 32*av_log2(2*(3*(l) + 2*(t) + (lt) + 2*(p))); | |
1428 else{ | |
1429 int p=0, l=0, lt=0, t=0, rt=0; | |
1430 | |
1431 if(y){ | |
1432 t= src[x + (y-1)*stride]; | |
1433 if(x) | |
1434 lt= src[x - 1 + (y-1)*stride]; | |
1435 } | |
1436 if(x) | |
1437 l= src[x - 1 + y*stride]; | |
1438 | |
1439 | |
1440 if(parent){ | |
1441 int px= x>>1; | |
1442 int py= y>>1; | |
1443 if(px<b->parent->width && py<b->parent->height){ | |
1444 p= parent[px + py*2*stride]; | |
1445 } | |
1446 } | |
1447 context= lev + 32*av_log2(2*(3*ABS(l) + 2*ABS(t) + ABS(lt) + ABS(p))); | |
1448 } | |
1449 | |
1450 if( (x&1) && l) sig=1; | |
1451 else if((y&1) && t) sig=1; | |
1452 else if((x&1) && (y&1) && lt) sig=1; | |
1453 else sig=0; | |
1454 | |
1455 if(!first){ | |
1456 if(sig) context+= 8+16; | |
1457 else context+= 8*(x&1) + 16*(y&1); | |
1458 } | |
1459 | |
1460 if(l||t||lt||(x&1)==0||(y&1)==0||first){ | |
1461 put_cabac(&s->c, &b->state[98][context], !!v); | |
1462 }else | |
1463 assert(v); | |
1464 | |
1465 if(v){ | |
1466 if(lev==max_level){ | |
1467 int p=0; | |
1468 int /*ll=0, */l=0, lt=0, t=0; | |
1469 int v= src[x + y*stride]; | |
1470 | |
1471 if(y){ | |
1472 t= src[x + (y-1)*stride]; | |
1473 if(x){ | |
1474 lt= src[x - 1 + (y-1)*stride]; | |
1475 } | |
1476 } | |
1477 if(x){ | |
1478 l= src[x - 1 + y*stride]; | |
1479 } | |
1480 | |
1481 if(parent){ | |
1482 int px= x>>1; | |
1483 int py= y>>1; | |
1484 if(px<b->parent->width && py<b->parent->height){ | |
1485 p= parent[px + py*2*stride]; | |
1486 } | |
1487 } | |
1488 { | |
1489 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(p) | |
1490 /*+ 3*(!!r) + 2*(!!d)*/); | |
1491 | |
1492 put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0); | |
1493 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0); | |
1494 assert(tree[max_level][x + y*tree_stride]); | |
1495 assert(tree[max_level-1][x/2 + y/2*tree_stride]); | |
1496 } | |
1497 gray_leaf++; | |
1498 }else{ | |
1499 int r= 2*x+1 < treedim[lev+1][0]; | |
1500 int d= 2*y+1 < treedim[lev+1][1]; | |
1501 encode_branch (s, b, src, parent, stride, lev+1, 2*x , 2*y , 0); | |
1502 if(r) encode_branch(s, b, src, parent, stride, lev+1, 2*x+1, 2*y , 0); | |
1503 if(d) encode_branch(s, b, src, parent, stride, lev+1, 2*x , 2*y+1, 0); | |
1504 if(r&&d)encode_branch(s, b, src, parent, stride, lev+1, 2*x+1, 2*y+1, 0); | |
1505 } | |
1506 } | |
1507 } | |
1508 | |
1509 static void encode_subband_qtree(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){ | |
1510 const int level= b->level; | |
1511 const int w= b->width; | |
1512 const int h= b->height; | |
1513 int x, y, i; | |
1514 | |
1515 init_quandtree(&b->tree, src, b->width, b->height, stride); | |
1516 | |
1517 if(parent){ | |
1518 init_quandtree(&b->parent->tree, parent, b->parent->width, b->parent->height, 2*stride); | |
1519 } | |
1520 | |
1521 for(i=0; i<b->tree.max_level; i++){ | |
1522 int count=0; | |
1523 for(y=0; y<b->tree.treedim[i][1]; y++){ | |
1524 for(x=0; x<b->tree.treedim[i][0]; x++){ | |
1525 if(b->tree.tree[i][x + y*b->tree.stride]) | |
1526 count++; | |
1527 } | |
1528 } | |
1529 if(2*count < b->tree.treedim[i][1]*b->tree.treedim[i][0]) | |
1530 break; | |
1531 } | |
1532 | |
1533 //FIXME try recursive scan | |
1534 for(y=0; y<b->tree.treedim[i][1]; y++){ | |
1535 for(x=0; x<b->tree.treedim[i][0]; x++){ | |
1536 encode_branch(s, b, src, parent, stride, i, x, y, 1); | |
1537 } | |
1538 } | |
1539 // encode_branch(s, b, src, parent, stride, 0, 0, 0, 1); | |
1540 // av_log(NULL, AV_LOG_DEBUG, "%d %d\n", gray_leaf, white_leaf); | |
1541 #if 0 | |
1542 for(lev=0; lev<=max_level; lev++){ | |
1543 w2= treedim[lev][0]; | |
1544 h2= treedim[lev][1]; | |
1545 for(y=0; y<h2; y++){ | |
1546 for(x=0; x<w2; x++){ | |
1547 int l= 0, t=0, rt=0, lt=0, p=0; | |
1548 int v= tree[lev][x + y*tree_stride]; | |
1549 int context, sig; | |
1550 if(lev && !tree[lev-1][x/2 + y/2*tree_stride]) | |
1551 continue; | |
1552 | |
1553 if(x) l= tree[lev][x - 1 + y*tree_stride]; | |
1554 if(y){ | |
1555 t= tree[lev][x + (y-1)*tree_stride]; | |
1556 if(x) lt= tree[lev][x - 1 + (y-1)*tree_stride]; | |
1557 if(x+1<w2) rt= tree[lev][x + 1 + (y-1)*tree_stride]; | |
1558 } | |
1559 if(lev < max_level && parent && x<ptreedim[lev][0] && y<ptreedim[lev][1]) | |
1560 p= ptree[lev][x + y*ptree_stride]; | |
1561 | |
1562 context= lev + 32*av_log2(2*(3*l + 2*t + lt + rt + 8*p)); | |
1563 | |
1564 if( (x&1) && l) sig=1; | |
1565 else if((y&1) && t) sig=1; | |
1566 else if((x&1) && (y&1) && lt) sig=1; | |
1567 else sig=0; | |
1568 | |
1569 if(sig) context+= 8+16; | |
1570 else context+= 8*(x&1) + 16*(y&1); | |
1571 | |
1572 if(l||t||lt||(x&1)==0||(y&1)==0) | |
1573 put_cabac(&s->c, &b->state[98][context], !!v); | |
1574 else | |
1575 assert(v); | |
1576 if(v && lev==max_level){ | |
1577 int p=0; | |
1578 int /*ll=0, */l=0, lt=0, t=0, rt=0; | |
1579 int v= src[x + y*stride]; | |
1580 | |
1581 if(y){ | |
1582 t= src[x + (y-1)*stride]; | |
1583 if(x){ | |
1584 lt= src[x - 1 + (y-1)*stride]; | |
1585 } | |
1586 if(x + 1 < w){ | |
1587 rt= src[x + 1 + (y-1)*stride]; | |
1588 } | |
1589 } | |
1590 if(x){ | |
1591 l= src[x - 1 + y*stride]; | |
1592 /*if(x > 1){ | |
1593 if(orientation==1) ll= src[y + (x-2)*stride]; | |
1594 else ll= src[x - 2 + y*stride]; | |
1595 }*/ | |
1596 } | |
1597 | |
1598 if(parent){ | |
1599 int px= x>>1; | |
1600 int py= y>>1; | |
1601 if(px<b->parent->width && py<b->parent->height){ | |
1602 p= parent[px + py*2*stride]; | |
1603 } | |
1604 } | |
1605 if(v){ | |
1606 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(p) | |
1607 /*+ 3*(!!r) + 2*(!!d)*/); | |
1608 | |
1609 put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0); | |
1610 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0); | |
1611 assert(tree[max_level][x + y*tree_stride]); | |
1612 assert(tree[max_level-1][x/2 + y/2*tree_stride]); | |
1613 }else | |
1614 assert(0); | |
1615 } | |
1616 } | |
1617 } | |
1618 } | |
1619 #endif | |
1620 } | |
1621 | |
1622 static int deint(unsigned int a){ | 1312 static int deint(unsigned int a){ |
1623 a &= 0x55555555; //0 1 2 3 4 5 6 7 8 9 A B C D E F | 1313 a &= 0x55555555; //0 1 2 3 4 5 6 7 8 9 A B C D E F |
1624 a += a & 0x11111111; // 01 23 45 67 89 AB CD EF | 1314 a += a & 0x11111111; // 01 23 45 67 89 AB CD EF |
1625 a += 3*(a & 0x0F0F0F0F);// 0123 4567 89AB CDEF | 1315 a += 3*(a & 0x0F0F0F0F);// 0123 4567 89AB CDEF |
1626 a += 15*(a & 0x00FF00FF);// 01234567 89ABCDEF | 1316 a += 15*(a & 0x00FF00FF);// 01234567 89ABCDEF |
3090 b->height= (h + !(orientation>1))>>1; | 2780 b->height= (h + !(orientation>1))>>1; |
3091 | 2781 |
3092 if(orientation&1) b->buf += (w+1)>>1; | 2782 if(orientation&1) b->buf += (w+1)>>1; |
3093 if(orientation>1) b->buf += b->stride>>1; | 2783 if(orientation>1) b->buf += b->stride>>1; |
3094 | 2784 |
3095 // alloc_qtree(&b->tree, b->width, b->height); | |
3096 | |
3097 if(level) | 2785 if(level) |
3098 b->parent= &s->plane[plane_index].band[level-1][orientation]; | 2786 b->parent= &s->plane[plane_index].band[level-1][orientation]; |
3099 } | 2787 } |
3100 w= (w+1)>>1; | 2788 w= (w+1)>>1; |
3101 h= (h+1)>>1; | 2789 h= (h+1)>>1; |
3107 s->mb_band.width = s->mv_band[0].width = s->mv_band[1].width = (s->avctx->width + 15)>>4; | 2795 s->mb_band.width = s->mv_band[0].width = s->mv_band[1].width = (s->avctx->width + 15)>>4; |
3108 s->mb_band.height= s->mv_band[0].height= s->mv_band[1].height= (s->avctx->height+ 15)>>4; | 2796 s->mb_band.height= s->mv_band[0].height= s->mv_band[1].height= (s->avctx->height+ 15)>>4; |
3109 s->mb_band .buf= av_mallocz(s->mb_band .stride * s->mb_band .height*sizeof(DWTELEM)); | 2797 s->mb_band .buf= av_mallocz(s->mb_band .stride * s->mb_band .height*sizeof(DWTELEM)); |
3110 s->mv_band[0].buf= av_mallocz(s->mv_band[0].stride * s->mv_band[0].height*sizeof(DWTELEM)); | 2798 s->mv_band[0].buf= av_mallocz(s->mv_band[0].stride * s->mv_band[0].height*sizeof(DWTELEM)); |
3111 s->mv_band[1].buf= av_mallocz(s->mv_band[1].stride * s->mv_band[1].height*sizeof(DWTELEM)); | 2799 s->mv_band[1].buf= av_mallocz(s->mv_band[1].stride * s->mv_band[1].height*sizeof(DWTELEM)); |
3112 /* alloc_qtree(&s->mb_band .tree, s->mb_band .width, s->mb_band .height); //FIXME free these 3 | |
3113 alloc_qtree(&s->mv_band[0].tree, s->mv_band[0].width, s->mv_band[0].height); | |
3114 alloc_qtree(&s->mv_band[1].tree, s->mv_band[0].width, s->mv_band[1].height);*/ | |
3115 | 2800 |
3116 reset_contexts(s); | 2801 reset_contexts(s); |
3117 /* | 2802 /* |
3118 width= s->width= avctx->width; | 2803 width= s->width= avctx->width; |
3119 height= s->height= avctx->height; | 2804 height= s->height= avctx->height; |
3540 | 3225 |
3541 return put_cabac_terminate(c, 1); | 3226 return put_cabac_terminate(c, 1); |
3542 } | 3227 } |
3543 | 3228 |
3544 static void common_end(SnowContext *s){ | 3229 static void common_end(SnowContext *s){ |
3545 int plane_index, level, orientation; | |
3546 | |
3547 av_freep(&s->spatial_dwt_buffer); | 3230 av_freep(&s->spatial_dwt_buffer); |
3548 av_freep(&s->mb_band.buf); | 3231 av_freep(&s->mb_band.buf); |
3549 av_freep(&s->mv_band[0].buf); | 3232 av_freep(&s->mv_band[0].buf); |
3550 av_freep(&s->mv_band[1].buf); | 3233 av_freep(&s->mv_band[1].buf); |
3551 | 3234 |
3555 av_freep(&s->mb_type); | 3238 av_freep(&s->mb_type); |
3556 av_freep(&s->mb_mean); | 3239 av_freep(&s->mb_mean); |
3557 av_freep(&s->dummy); | 3240 av_freep(&s->dummy); |
3558 av_freep(&s->motion_val8); | 3241 av_freep(&s->motion_val8); |
3559 av_freep(&s->motion_val16); | 3242 av_freep(&s->motion_val16); |
3560 /* | |
3561 for(plane_index=0; plane_index<3; plane_index++){ | |
3562 for(level=s->spatial_decomposition_count-1; level>=0; level--){ | |
3563 for(orientation=level ? 1 : 0; orientation<4; orientation++){ | |
3564 SubBand *b= &s->plane[plane_index].band[level][orientation]; | |
3565 | |
3566 free_qtree(&b->tree); | |
3567 } | |
3568 } | |
3569 }*/ | |
3570 } | 3243 } |
3571 | 3244 |
3572 static int encode_end(AVCodecContext *avctx) | 3245 static int encode_end(AVCodecContext *avctx) |
3573 { | 3246 { |
3574 SnowContext *s = avctx->priv_data; | 3247 SnowContext *s = avctx->priv_data; |