Mercurial > libavcodec.hg
comparison snow.c @ 2335:28eb7b1dcefc libavcodec
CABAC -> range coder
author | michael |
---|---|
date | Sat, 30 Oct 2004 02:16:27 +0000 |
parents | bfdec93a5a42 |
children | a7ac68734a91 |
comparison
equal
deleted
inserted
replaced
2334:25448d0bc924 | 2335:28eb7b1dcefc |
---|---|
17 */ | 17 */ |
18 | 18 |
19 #include "avcodec.h" | 19 #include "avcodec.h" |
20 #include "common.h" | 20 #include "common.h" |
21 #include "dsputil.h" | 21 #include "dsputil.h" |
22 #include "cabac.h" | 22 |
23 #include "rangecoder.h" | |
24 #define MID_STATE 128 | |
23 | 25 |
24 #include "mpegvideo.h" | 26 #include "mpegvideo.h" |
25 | 27 |
26 #undef NDEBUG | 28 #undef NDEBUG |
27 #include <assert.h> | 29 #include <assert.h> |
390 | 392 |
391 typedef struct SnowContext{ | 393 typedef struct SnowContext{ |
392 // MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX) | 394 // MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX) |
393 | 395 |
394 AVCodecContext *avctx; | 396 AVCodecContext *avctx; |
395 CABACContext c; | 397 RangeCoder c; |
396 DSPContext dsp; | 398 DSPContext dsp; |
397 AVFrame input_picture; | 399 AVFrame input_picture; |
398 AVFrame current_picture; | 400 AVFrame current_picture; |
399 AVFrame last_picture; | 401 AVFrame last_picture; |
400 AVFrame mconly_picture; | 402 AVFrame mconly_picture; |
441 if (v<0) return -v; | 443 if (v<0) return -v; |
442 else if(v>m) return 2*m-v; | 444 else if(v>m) return 2*m-v; |
443 else return v; | 445 else return v; |
444 } | 446 } |
445 | 447 |
446 static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){ | 448 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){ |
447 int i; | 449 int i; |
448 | 450 |
449 if(v){ | 451 if(v){ |
450 const int a= ABS(v); | 452 const int a= ABS(v); |
451 const int e= av_log2(a); | 453 const int e= av_log2(a); |
452 #if 1 | 454 #if 1 |
453 const int el= FFMIN(e, 10); | 455 const int el= FFMIN(e, 10); |
454 put_cabac(c, state+0, 0); | 456 put_rac(c, state+0, 0); |
455 | 457 |
456 for(i=0; i<el; i++){ | 458 for(i=0; i<el; i++){ |
457 put_cabac(c, state+1+i, 1); //1..10 | 459 put_rac(c, state+1+i, 1); //1..10 |
458 } | 460 } |
459 for(; i<e; i++){ | 461 for(; i<e; i++){ |
460 put_cabac(c, state+1+9, 1); //1..10 | 462 put_rac(c, state+1+9, 1); //1..10 |
461 } | 463 } |
462 put_cabac(c, state+1+FFMIN(i,9), 0); | 464 put_rac(c, state+1+FFMIN(i,9), 0); |
463 | 465 |
464 for(i=e-1; i>=el; i--){ | 466 for(i=e-1; i>=el; i--){ |
465 put_cabac(c, state+22+9, (a>>i)&1); //22..31 | 467 put_rac(c, state+22+9, (a>>i)&1); //22..31 |
466 } | 468 } |
467 for(; i>=0; i--){ | 469 for(; i>=0; i--){ |
468 put_cabac(c, state+22+i, (a>>i)&1); //22..31 | 470 put_rac(c, state+22+i, (a>>i)&1); //22..31 |
469 } | 471 } |
470 | 472 |
471 if(is_signed) | 473 if(is_signed) |
472 put_cabac(c, state+11 + el, v < 0); //11..21 | 474 put_rac(c, state+11 + el, v < 0); //11..21 |
473 #else | 475 #else |
474 | 476 |
475 put_cabac(c, state+0, 0); | 477 put_rac(c, state+0, 0); |
476 if(e<=9){ | 478 if(e<=9){ |
477 for(i=0; i<e; i++){ | 479 for(i=0; i<e; i++){ |
478 put_cabac(c, state+1+i, 1); //1..10 | 480 put_rac(c, state+1+i, 1); //1..10 |
479 } | 481 } |
480 put_cabac(c, state+1+i, 0); | 482 put_rac(c, state+1+i, 0); |
481 | 483 |
482 for(i=e-1; i>=0; i--){ | 484 for(i=e-1; i>=0; i--){ |
483 put_cabac(c, state+22+i, (a>>i)&1); //22..31 | 485 put_rac(c, state+22+i, (a>>i)&1); //22..31 |
484 } | 486 } |
485 | 487 |
486 if(is_signed) | 488 if(is_signed) |
487 put_cabac(c, state+11 + e, v < 0); //11..21 | 489 put_rac(c, state+11 + e, v < 0); //11..21 |
488 }else{ | 490 }else{ |
489 for(i=0; i<e; i++){ | 491 for(i=0; i<e; i++){ |
490 put_cabac(c, state+1+FFMIN(i,9), 1); //1..10 | 492 put_rac(c, state+1+FFMIN(i,9), 1); //1..10 |
491 } | 493 } |
492 put_cabac(c, state+1+FFMIN(i,9), 0); | 494 put_rac(c, state+1+FFMIN(i,9), 0); |
493 | 495 |
494 for(i=e-1; i>=0; i--){ | 496 for(i=e-1; i>=0; i--){ |
495 put_cabac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31 | 497 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31 |
496 } | 498 } |
497 | 499 |
498 if(is_signed) | 500 if(is_signed) |
499 put_cabac(c, state+11 + FFMIN(e,10), v < 0); //11..21 | 501 put_rac(c, state+11 + FFMIN(e,10), v < 0); //11..21 |
500 } | 502 } |
501 #endif | 503 #endif |
502 }else{ | 504 }else{ |
503 put_cabac(c, state+0, 1); | 505 put_rac(c, state+0, 1); |
504 } | 506 } |
505 } | 507 } |
506 | 508 |
507 static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed){ | 509 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){ |
508 if(get_cabac(c, state+0)) | 510 if(get_rac(c, state+0)) |
509 return 0; | 511 return 0; |
510 else{ | 512 else{ |
511 int i, e, a; | 513 int i, e, a; |
512 e= 0; | 514 e= 0; |
513 while(get_cabac(c, state+1 + FFMIN(e,9))){ //1..10 | 515 while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10 |
514 e++; | 516 e++; |
515 } | 517 } |
516 | 518 |
517 a= 1; | 519 a= 1; |
518 for(i=e-1; i>=0; i--){ | 520 for(i=e-1; i>=0; i--){ |
519 a += a + get_cabac(c, state+22 + FFMIN(i,9)); //22..31 | 521 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31 |
520 } | 522 } |
521 | 523 |
522 if(is_signed && get_cabac(c, state+11 + FFMIN(e,10))) //11..21 | 524 if(is_signed && get_rac(c, state+11 + FFMIN(e,10))) //11..21 |
523 return -a; | 525 return -a; |
524 else | 526 else |
525 return a; | 527 return a; |
526 } | 528 } |
527 } | 529 } |
528 | 530 |
529 static inline void put_symbol2(CABACContext *c, uint8_t *state, int v, int log2){ | 531 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){ |
530 int i; | 532 int i; |
531 int r= log2>=0 ? 1<<log2 : 1; | 533 int r= log2>=0 ? 1<<log2 : 1; |
532 | 534 |
533 assert(v>=0); | 535 assert(v>=0); |
534 assert(log2>=-4); | 536 assert(log2>=-4); |
535 | 537 |
536 while(v >= r){ | 538 while(v >= r){ |
537 put_cabac(c, state+4+log2, 1); | 539 put_rac(c, state+4+log2, 1); |
538 v -= r; | 540 v -= r; |
539 log2++; | 541 log2++; |
540 if(log2>0) r+=r; | 542 if(log2>0) r+=r; |
541 } | 543 } |
542 put_cabac(c, state+4+log2, 0); | 544 put_rac(c, state+4+log2, 0); |
543 | 545 |
544 for(i=log2-1; i>=0; i--){ | 546 for(i=log2-1; i>=0; i--){ |
545 put_cabac(c, state+31-i, (v>>i)&1); | 547 put_rac(c, state+31-i, (v>>i)&1); |
546 } | 548 } |
547 } | 549 } |
548 | 550 |
549 static inline int get_symbol2(CABACContext *c, uint8_t *state, int log2){ | 551 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){ |
550 int i; | 552 int i; |
551 int r= log2>=0 ? 1<<log2 : 1; | 553 int r= log2>=0 ? 1<<log2 : 1; |
552 int v=0; | 554 int v=0; |
553 | 555 |
554 assert(log2>=-4); | 556 assert(log2>=-4); |
555 | 557 |
556 while(get_cabac(c, state+4+log2)){ | 558 while(get_rac(c, state+4+log2)){ |
557 v+= r; | 559 v+= r; |
558 log2++; | 560 log2++; |
559 if(log2>0) r+=r; | 561 if(log2>0) r+=r; |
560 } | 562 } |
561 | 563 |
562 for(i=log2-1; i>=0; i--){ | 564 for(i=log2-1; i>=0; i--){ |
563 v+= get_cabac(c, state+31-i)<<i; | 565 v+= get_rac(c, state+31-i)<<i; |
564 } | 566 } |
565 | 567 |
566 return v; | 568 return v; |
567 } | 569 } |
568 | 570 |
1369 p= parent[px + py*2*stride]; | 1371 p= parent[px + py*2*stride]; |
1370 } | 1372 } |
1371 if(/*ll|*/l|lt|t|rt|p){ | 1373 if(/*ll|*/l|lt|t|rt|p){ |
1372 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); | 1374 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); |
1373 | 1375 |
1374 put_cabac(&s->c, &b->state[0][context], !!v); | 1376 put_rac(&s->c, &b->state[0][context], !!v); |
1375 }else{ | 1377 }else{ |
1376 if(!run){ | 1378 if(!run){ |
1377 run= runs[run_index++]; | 1379 run= runs[run_index++]; |
1378 | 1380 |
1379 put_symbol2(&s->c, b->state[1], run, 3); | 1381 put_symbol2(&s->c, b->state[1], run, 3); |
1385 } | 1387 } |
1386 if(v){ | 1388 if(v){ |
1387 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); | 1389 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); |
1388 | 1390 |
1389 put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4); | 1391 put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4); |
1390 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0); | 1392 put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0); |
1391 } | 1393 } |
1392 } | 1394 } |
1393 } | 1395 } |
1394 } | 1396 } |
1395 } | 1397 } |
1458 } | 1460 } |
1459 } | 1461 } |
1460 if(/*ll|*/l|lt|t|rt|p){ | 1462 if(/*ll|*/l|lt|t|rt|p){ |
1461 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); | 1463 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); |
1462 | 1464 |
1463 v=get_cabac(&s->c, &b->state[0][context]); | 1465 v=get_rac(&s->c, &b->state[0][context]); |
1464 }else{ | 1466 }else{ |
1465 if(!run){ | 1467 if(!run){ |
1466 run= get_symbol2(&s->c, b->state[1], 3); | 1468 run= get_symbol2(&s->c, b->state[1], 3); |
1467 v=1; | 1469 v=1; |
1468 }else{ | 1470 }else{ |
1480 } | 1482 } |
1481 } | 1483 } |
1482 if(v){ | 1484 if(v){ |
1483 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); | 1485 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)); |
1484 v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1; | 1486 v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1; |
1485 if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]])){ | 1487 if(get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]])){ |
1486 src[x + y*stride]=-(( v*qmul + qadd)>>(QEXPSHIFT)); | 1488 src[x + y*stride]=-(( v*qmul + qadd)>>(QEXPSHIFT)); |
1487 v= -v; | 1489 v= -v; |
1488 }else{ | 1490 }else{ |
1489 src[x + y*stride]= (( v*qmul + qadd)>>(QEXPSHIFT)); | 1491 src[x + y*stride]= (( v*qmul + qadd)>>(QEXPSHIFT)); |
1490 } | 1492 } |
1520 int plane_index, level, orientation; | 1522 int plane_index, level, orientation; |
1521 | 1523 |
1522 for(plane_index=0; plane_index<3; plane_index++){ | 1524 for(plane_index=0; plane_index<3; plane_index++){ |
1523 for(level=0; level<s->spatial_decomposition_count; level++){ | 1525 for(level=0; level<s->spatial_decomposition_count; level++){ |
1524 for(orientation=level ? 1:0; orientation<4; orientation++){ | 1526 for(orientation=level ? 1:0; orientation<4; orientation++){ |
1525 memset(s->plane[plane_index].band[level][orientation].state, 0, sizeof(s->plane[plane_index].band[level][orientation].state)); | 1527 memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state)); |
1526 } | 1528 } |
1527 } | 1529 } |
1528 } | 1530 } |
1529 memset(s->header_state, 0, sizeof(s->header_state)); | 1531 memset(s->header_state, MID_STATE, sizeof(s->header_state)); |
1530 memset(s->block_state, 0, sizeof(s->block_state)); | 1532 memset(s->block_state, MID_STATE, sizeof(s->block_state)); |
1531 } | 1533 } |
1532 | 1534 |
1533 static int alloc_blocks(SnowContext *s){ | 1535 static int alloc_blocks(SnowContext *s){ |
1534 int w= -((-s->avctx->width )>>LOG2_MB_SIZE); | 1536 int w= -((-s->avctx->width )>>LOG2_MB_SIZE); |
1535 int h= -((-s->avctx->height)>>LOG2_MB_SIZE); | 1537 int h= -((-s->avctx->height)>>LOG2_MB_SIZE); |
1539 | 1541 |
1540 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2)); | 1542 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2)); |
1541 return 0; | 1543 return 0; |
1542 } | 1544 } |
1543 | 1545 |
1544 static inline void copy_cabac_state(CABACContext *d, CABACContext *s){ | 1546 static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){ |
1545 PutBitContext bak= d->pb; | 1547 uint8_t *bytestream= d->bytestream; |
1548 uint8_t *bytestream_start= d->bytestream_start; | |
1546 *d= *s; | 1549 *d= *s; |
1547 d->pb= bak; | 1550 d->bytestream= bytestream; |
1551 d->bytestream_start= bytestream_start; | |
1548 } | 1552 } |
1549 | 1553 |
1550 //near copy & paste from dsputil, FIXME | 1554 //near copy & paste from dsputil, FIXME |
1551 static int pix_sum(uint8_t * pix, int line_size, int w) | 1555 static int pix_sum(uint8_t * pix, int line_size, int w) |
1552 { | 1556 { |
1628 static int encode_q_branch(SnowContext *s, int level, int x, int y){ | 1632 static int encode_q_branch(SnowContext *s, int level, int x, int y){ |
1629 uint8_t p_buffer[1024]; | 1633 uint8_t p_buffer[1024]; |
1630 uint8_t i_buffer[1024]; | 1634 uint8_t i_buffer[1024]; |
1631 uint8_t p_state[sizeof(s->block_state)]; | 1635 uint8_t p_state[sizeof(s->block_state)]; |
1632 uint8_t i_state[sizeof(s->block_state)]; | 1636 uint8_t i_state[sizeof(s->block_state)]; |
1633 CABACContext pc, ic; | 1637 RangeCoder pc, ic; |
1634 PutBitContext pbbak= s->c.pb; | 1638 uint8_t *pbbak= s->c.bytestream; |
1639 uint8_t *pbbak_start= s->c.bytestream_start; | |
1635 int score, score2, iscore, i_len, p_len, block_s, sum; | 1640 int score, score2, iscore, i_len, p_len, block_s, sum; |
1636 const int w= s->b_width << s->block_max_depth; | 1641 const int w= s->b_width << s->block_max_depth; |
1637 const int h= s->b_height << s->block_max_depth; | 1642 const int h= s->b_height << s->block_max_depth; |
1638 const int rem_depth= s->block_max_depth - level; | 1643 const int rem_depth= s->block_max_depth - level; |
1639 const int index= (x + y*w) << rem_depth; | 1644 const int index= (x + y*w) << rem_depth; |
1759 score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0); | 1764 score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0); |
1760 //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2 | 1765 //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2 |
1761 | 1766 |
1762 // subpel search | 1767 // subpel search |
1763 pc= s->c; | 1768 pc= s->c; |
1764 init_put_bits(&pc.pb, p_buffer, sizeof(p_buffer)); | 1769 pc.bytestream_start= |
1770 pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo | |
1765 memcpy(p_state, s->block_state, sizeof(s->block_state)); | 1771 memcpy(p_state, s->block_state, sizeof(s->block_state)); |
1766 | 1772 |
1767 if(level!=s->block_max_depth) | 1773 if(level!=s->block_max_depth) |
1768 put_cabac(&pc, &p_state[4 + s_context], 1); | 1774 put_rac(&pc, &p_state[4 + s_context], 1); |
1769 put_cabac(&pc, &p_state[1 + left->type + top->type], 0); | 1775 put_rac(&pc, &p_state[1 + left->type + top->type], 0); |
1770 put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1); | 1776 put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1); |
1771 put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1); | 1777 put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1); |
1772 p_len= put_bits_count(&pc.pb); | 1778 p_len= pc.bytestream - pc.bytestream_start; |
1773 score += (s->lambda2*(p_len + pc.outstanding_count - s->c.outstanding_count))>>FF_LAMBDA_SHIFT; | 1779 score += (s->lambda2*(p_len*8 |
1780 + (pc.outstanding_count - s->c.outstanding_count)*8 | |
1781 + (-av_log2(pc.range) + av_log2(s->c.range)) | |
1782 ))>>FF_LAMBDA_SHIFT; | |
1774 | 1783 |
1775 block_s= block_w*block_w; | 1784 block_s= block_w*block_w; |
1776 sum = pix_sum(¤t_mb[0][0], stride, block_w); | 1785 sum = pix_sum(¤t_mb[0][0], stride, block_w); |
1777 l= (sum + block_s/2)/block_s; | 1786 l= (sum + block_s/2)/block_s; |
1778 iscore = pix_norm1(¤t_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s; | 1787 iscore = pix_norm1(¤t_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s; |
1784 sum = pix_sum(¤t_mb[2][0], uvstride, block_w>>1); | 1793 sum = pix_sum(¤t_mb[2][0], uvstride, block_w>>1); |
1785 cr= (sum + block_s/2)/block_s; | 1794 cr= (sum + block_s/2)/block_s; |
1786 // iscore += pix_norm1(¤t_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s; | 1795 // iscore += pix_norm1(¤t_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s; |
1787 | 1796 |
1788 ic= s->c; | 1797 ic= s->c; |
1789 init_put_bits(&ic.pb, i_buffer, sizeof(i_buffer)); | 1798 ic.bytestream_start= |
1799 ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo | |
1790 memcpy(i_state, s->block_state, sizeof(s->block_state)); | 1800 memcpy(i_state, s->block_state, sizeof(s->block_state)); |
1791 if(level!=s->block_max_depth) | 1801 if(level!=s->block_max_depth) |
1792 put_cabac(&ic, &i_state[4 + s_context], 1); | 1802 put_rac(&ic, &i_state[4 + s_context], 1); |
1793 put_cabac(&ic, &i_state[1 + left->type + top->type], 1); | 1803 put_rac(&ic, &i_state[1 + left->type + top->type], 1); |
1794 put_symbol(&ic, &i_state[32], l-pl , 1); | 1804 put_symbol(&ic, &i_state[32], l-pl , 1); |
1795 put_symbol(&ic, &i_state[64], cb-pcb, 1); | 1805 put_symbol(&ic, &i_state[64], cb-pcb, 1); |
1796 put_symbol(&ic, &i_state[96], cr-pcr, 1); | 1806 put_symbol(&ic, &i_state[96], cr-pcr, 1); |
1797 i_len= put_bits_count(&ic.pb); | 1807 i_len= ic.bytestream - ic.bytestream_start; |
1798 iscore += (s->lambda2*(i_len + ic.outstanding_count - s->c.outstanding_count))>>FF_LAMBDA_SHIFT; | 1808 iscore += (s->lambda2*(i_len*8 |
1809 + (ic.outstanding_count - s->c.outstanding_count)*8 | |
1810 + (-av_log2(ic.range) + av_log2(s->c.range)) | |
1811 ))>>FF_LAMBDA_SHIFT; | |
1799 | 1812 |
1800 // assert(score==256*256*256*64-1); | 1813 // assert(score==256*256*256*64-1); |
1801 assert(iscore < 255*255*256 + s->lambda2*10); | 1814 assert(iscore < 255*255*256 + s->lambda2*10); |
1802 assert(iscore >= 0); | 1815 assert(iscore >= 0); |
1803 assert(l>=0 && l<=255); | 1816 assert(l>=0 && l<=255); |
1811 else | 1824 else |
1812 c->scene_change_score+= s->m.qscale; | 1825 c->scene_change_score+= s->m.qscale; |
1813 } | 1826 } |
1814 | 1827 |
1815 if(level!=s->block_max_depth){ | 1828 if(level!=s->block_max_depth){ |
1816 put_cabac(&s->c, &s->block_state[4 + s_context], 0); | 1829 put_rac(&s->c, &s->block_state[4 + s_context], 0); |
1817 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0); | 1830 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0); |
1818 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0); | 1831 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0); |
1819 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1); | 1832 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1); |
1820 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1); | 1833 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1); |
1821 score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead | 1834 score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead |
1823 if(score2 < score && score2 < iscore) | 1836 if(score2 < score && score2 < iscore) |
1824 return score2; | 1837 return score2; |
1825 } | 1838 } |
1826 | 1839 |
1827 if(iscore < score){ | 1840 if(iscore < score){ |
1828 flush_put_bits(&ic.pb); | 1841 memcpy(pbbak, i_buffer, i_len); |
1829 ff_copy_bits(&pbbak, i_buffer, i_len); | |
1830 s->c= ic; | 1842 s->c= ic; |
1831 s->c.pb= pbbak; | 1843 s->c.bytestream_start= pbbak_start; |
1844 s->c.bytestream= pbbak + i_len; | |
1832 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA); | 1845 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA); |
1833 memcpy(s->block_state, i_state, sizeof(s->block_state)); | 1846 memcpy(s->block_state, i_state, sizeof(s->block_state)); |
1834 return iscore; | 1847 return iscore; |
1835 }else{ | 1848 }else{ |
1836 flush_put_bits(&pc.pb); | 1849 memcpy(pbbak, p_buffer, p_len); |
1837 ff_copy_bits(&pbbak, p_buffer, p_len); | |
1838 s->c= pc; | 1850 s->c= pc; |
1839 s->c.pb= pbbak; | 1851 s->c.bytestream_start= pbbak_start; |
1852 s->c.bytestream= pbbak + p_len; | |
1840 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0); | 1853 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0); |
1841 memcpy(s->block_state, p_state, sizeof(s->block_state)); | 1854 memcpy(s->block_state, p_state, sizeof(s->block_state)); |
1842 return score; | 1855 return score; |
1843 } | 1856 } |
1844 } | 1857 } |
1864 if(s->keyframe){ | 1877 if(s->keyframe){ |
1865 set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, BLOCK_INTRA); | 1878 set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, BLOCK_INTRA); |
1866 return; | 1879 return; |
1867 } | 1880 } |
1868 | 1881 |
1869 if(level==s->block_max_depth || get_cabac(&s->c, &s->block_state[4 + s_context])){ | 1882 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){ |
1870 int type; | 1883 int type; |
1871 int l = left->color[0]; | 1884 int l = left->color[0]; |
1872 int cb= left->color[1]; | 1885 int cb= left->color[1]; |
1873 int cr= left->color[2]; | 1886 int cr= left->color[2]; |
1874 int mx= mid_pred(left->mx, top->mx, tr->mx); | 1887 int mx= mid_pred(left->mx, top->mx, tr->mx); |
1875 int my= mid_pred(left->my, top->my, tr->my); | 1888 int my= mid_pred(left->my, top->my, tr->my); |
1876 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx)); | 1889 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx)); |
1877 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my)); | 1890 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my)); |
1878 | 1891 |
1879 type= get_cabac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0; | 1892 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0; |
1880 | 1893 |
1881 if(type){ | 1894 if(type){ |
1882 l += get_symbol(&s->c, &s->block_state[32], 1); | 1895 l += get_symbol(&s->c, &s->block_state[32], 1); |
1883 cb+= get_symbol(&s->c, &s->block_state[64], 1); | 1896 cb+= get_symbol(&s->c, &s->block_state[64], 1); |
1884 cr+= get_symbol(&s->c, &s->block_state[96], 1); | 1897 cr+= get_symbol(&s->c, &s->block_state[96], 1); |
2369 } | 2382 } |
2370 } | 2383 } |
2371 | 2384 |
2372 static void encode_header(SnowContext *s){ | 2385 static void encode_header(SnowContext *s){ |
2373 int plane_index, level, orientation; | 2386 int plane_index, level, orientation; |
2374 uint8_t kstate[32]={0}; | 2387 uint8_t kstate[32]; |
2375 | 2388 |
2376 put_cabac(&s->c, kstate, s->keyframe); | 2389 memset(kstate, MID_STATE, sizeof(kstate)); |
2390 | |
2391 put_rac(&s->c, kstate, s->keyframe); | |
2377 if(s->keyframe || s->always_reset) | 2392 if(s->keyframe || s->always_reset) |
2378 reset_contexts(s); | 2393 reset_contexts(s); |
2379 if(s->keyframe){ | 2394 if(s->keyframe){ |
2380 put_symbol(&s->c, s->header_state, s->version, 0); | 2395 put_symbol(&s->c, s->header_state, s->version, 0); |
2381 put_cabac(&s->c, s->header_state, s->always_reset); | 2396 put_rac(&s->c, s->header_state, s->always_reset); |
2382 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0); | 2397 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0); |
2383 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0); | 2398 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0); |
2384 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0); | 2399 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0); |
2385 put_symbol(&s->c, s->header_state, s->colorspace_type, 0); | 2400 put_symbol(&s->c, s->header_state, s->colorspace_type, 0); |
2386 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0); | 2401 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0); |
2387 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0); | 2402 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0); |
2388 put_cabac(&s->c, s->header_state, s->spatial_scalability); | 2403 put_rac(&s->c, s->header_state, s->spatial_scalability); |
2389 // put_cabac(&s->c, s->header_state, s->rate_scalability); | 2404 // put_rac(&s->c, s->header_state, s->rate_scalability); |
2390 | 2405 |
2391 for(plane_index=0; plane_index<2; plane_index++){ | 2406 for(plane_index=0; plane_index<2; plane_index++){ |
2392 for(level=0; level<s->spatial_decomposition_count; level++){ | 2407 for(level=0; level<s->spatial_decomposition_count; level++){ |
2393 for(orientation=level ? 1:0; orientation<4; orientation++){ | 2408 for(orientation=level ? 1:0; orientation<4; orientation++){ |
2394 if(orientation==2) continue; | 2409 if(orientation==2) continue; |
2404 put_symbol(&s->c, s->header_state, s->block_max_depth, 0); | 2419 put_symbol(&s->c, s->header_state, s->block_max_depth, 0); |
2405 } | 2420 } |
2406 | 2421 |
2407 static int decode_header(SnowContext *s){ | 2422 static int decode_header(SnowContext *s){ |
2408 int plane_index, level, orientation; | 2423 int plane_index, level, orientation; |
2409 uint8_t kstate[32]={0}; | 2424 uint8_t kstate[32]; |
2410 | 2425 |
2411 s->keyframe= get_cabac(&s->c, kstate); | 2426 memset(kstate, MID_STATE, sizeof(kstate)); |
2427 | |
2428 s->keyframe= get_rac(&s->c, kstate); | |
2412 if(s->keyframe || s->always_reset) | 2429 if(s->keyframe || s->always_reset) |
2413 reset_contexts(s); | 2430 reset_contexts(s); |
2414 if(s->keyframe){ | 2431 if(s->keyframe){ |
2415 s->version= get_symbol(&s->c, s->header_state, 0); | 2432 s->version= get_symbol(&s->c, s->header_state, 0); |
2416 if(s->version>0){ | 2433 if(s->version>0){ |
2417 av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version); | 2434 av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version); |
2418 return -1; | 2435 return -1; |
2419 } | 2436 } |
2420 s->always_reset= get_cabac(&s->c, s->header_state); | 2437 s->always_reset= get_rac(&s->c, s->header_state); |
2421 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0); | 2438 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0); |
2422 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0); | 2439 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0); |
2423 s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0); | 2440 s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0); |
2424 s->colorspace_type= get_symbol(&s->c, s->header_state, 0); | 2441 s->colorspace_type= get_symbol(&s->c, s->header_state, 0); |
2425 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0); | 2442 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0); |
2426 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0); | 2443 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0); |
2427 s->spatial_scalability= get_cabac(&s->c, s->header_state); | 2444 s->spatial_scalability= get_rac(&s->c, s->header_state); |
2428 // s->rate_scalability= get_cabac(&s->c, s->header_state); | 2445 // s->rate_scalability= get_rac(&s->c, s->header_state); |
2429 | 2446 |
2430 for(plane_index=0; plane_index<3; plane_index++){ | 2447 for(plane_index=0; plane_index<3; plane_index++){ |
2431 for(level=0; level<s->spatial_decomposition_count; level++){ | 2448 for(level=0; level<s->spatial_decomposition_count; level++){ |
2432 for(orientation=level ? 1:0; orientation<4; orientation++){ | 2449 for(orientation=level ? 1:0; orientation<4; orientation++){ |
2433 int q; | 2450 int q; |
2664 return 0; | 2681 return 0; |
2665 } | 2682 } |
2666 | 2683 |
2667 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | 2684 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ |
2668 SnowContext *s = avctx->priv_data; | 2685 SnowContext *s = avctx->priv_data; |
2669 CABACContext * const c= &s->c; | 2686 RangeCoder * const c= &s->c; |
2670 AVFrame *pict = data; | 2687 AVFrame *pict = data; |
2671 const int width= s->avctx->width; | 2688 const int width= s->avctx->width; |
2672 const int height= s->avctx->height; | 2689 const int height= s->avctx->height; |
2673 int level, orientation, plane_index; | 2690 int level, orientation, plane_index; |
2674 | 2691 |
2675 ff_init_cabac_encoder(c, buf, buf_size); | 2692 ff_init_range_encoder(c, buf, buf_size); |
2676 ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); | 2693 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); |
2677 | 2694 |
2678 s->input_picture = *pict; | 2695 s->input_picture = *pict; |
2679 | 2696 |
2680 s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0; | 2697 s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0; |
2681 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE; | 2698 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE; |
2757 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0); | 2774 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0); |
2758 | 2775 |
2759 if( plane_index==0 | 2776 if( plane_index==0 |
2760 && pict->pict_type == P_TYPE | 2777 && pict->pict_type == P_TYPE |
2761 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){ | 2778 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){ |
2762 ff_init_cabac_encoder(c, buf, buf_size); | 2779 ff_init_range_encoder(c, buf, buf_size); |
2763 ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); | 2780 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); |
2764 pict->pict_type= FF_I_TYPE; | 2781 pict->pict_type= FF_I_TYPE; |
2765 s->keyframe=1; | 2782 s->keyframe=1; |
2766 reset_contexts(s); | 2783 reset_contexts(s); |
2767 goto redo_frame; | 2784 goto redo_frame; |
2768 } | 2785 } |
2829 if(s->last_picture.data[0]) | 2846 if(s->last_picture.data[0]) |
2830 avctx->release_buffer(avctx, &s->last_picture); | 2847 avctx->release_buffer(avctx, &s->last_picture); |
2831 | 2848 |
2832 emms_c(); | 2849 emms_c(); |
2833 | 2850 |
2834 return put_cabac_terminate(c, 1); | 2851 return ff_rac_terminate(c); |
2835 } | 2852 } |
2836 | 2853 |
2837 static void common_end(SnowContext *s){ | 2854 static void common_end(SnowContext *s){ |
2838 int plane_index, level, orientation; | 2855 int plane_index, level, orientation; |
2839 | 2856 |
2875 return 0; | 2892 return 0; |
2876 } | 2893 } |
2877 | 2894 |
2878 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ | 2895 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ |
2879 SnowContext *s = avctx->priv_data; | 2896 SnowContext *s = avctx->priv_data; |
2880 CABACContext * const c= &s->c; | 2897 RangeCoder * const c= &s->c; |
2881 int bytes_read; | 2898 int bytes_read; |
2882 AVFrame *picture = data; | 2899 AVFrame *picture = data; |
2883 int level, orientation, plane_index; | 2900 int level, orientation, plane_index; |
2884 | 2901 |
2885 | 2902 |
2886 /* no supplementary picture */ | 2903 /* no supplementary picture */ |
2887 if (buf_size == 0) | 2904 if (buf_size == 0) |
2888 return 0; | 2905 return 0; |
2889 | 2906 |
2890 ff_init_cabac_decoder(c, buf, buf_size); | 2907 ff_init_range_decoder(c, buf, buf_size); |
2891 ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); | 2908 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); |
2892 | 2909 |
2893 s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P | 2910 s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P |
2894 decode_header(s); | 2911 decode_header(s); |
2895 if(!s->block) alloc_blocks(s); | 2912 if(!s->block) alloc_blocks(s); |
2896 | 2913 |
2954 else | 2971 else |
2955 *picture= s->mconly_picture; | 2972 *picture= s->mconly_picture; |
2956 | 2973 |
2957 *data_size = sizeof(AVFrame); | 2974 *data_size = sizeof(AVFrame); |
2958 | 2975 |
2959 bytes_read= get_cabac_terminate(c); | 2976 bytes_read= c->bytestream - c->bytestream_start; |
2960 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); | 2977 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME |
2961 | 2978 |
2962 return bytes_read; | 2979 return bytes_read; |
2963 } | 2980 } |
2964 | 2981 |
2965 static int decode_end(AVCodecContext *avctx) | 2982 static int decode_end(AVCodecContext *avctx) |
3030 for(i=0; i<width*height; i++) | 3047 for(i=0; i<width*height; i++) |
3031 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]); | 3048 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]); |
3032 | 3049 |
3033 printf("testing AC coder\n"); | 3050 printf("testing AC coder\n"); |
3034 memset(s.header_state, 0, sizeof(s.header_state)); | 3051 memset(s.header_state, 0, sizeof(s.header_state)); |
3035 ff_init_cabac_encoder(&s.c, buffer[0], 256*256); | 3052 ff_init_range_encoder(&s.c, buffer[0], 256*256); |
3036 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); | 3053 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); |
3037 | 3054 |
3038 for(i=-256; i<256; i++){ | 3055 for(i=-256; i<256; i++){ |
3039 START_TIMER | 3056 START_TIMER |
3040 put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1); | 3057 put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1); |
3041 STOP_TIMER("put_symbol") | 3058 STOP_TIMER("put_symbol") |
3042 } | 3059 } |
3043 put_cabac_terminate(&s.c, 1); | 3060 ff_rac_terminate(&s.c); |
3044 | 3061 |
3045 memset(s.header_state, 0, sizeof(s.header_state)); | 3062 memset(s.header_state, 0, sizeof(s.header_state)); |
3046 ff_init_cabac_decoder(&s.c, buffer[0], 256*256); | 3063 ff_init_range_decoder(&s.c, buffer[0], 256*256); |
3047 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); | 3064 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64); |
3048 | 3065 |
3049 for(i=-256; i<256; i++){ | 3066 for(i=-256; i<256; i++){ |
3050 int j; | 3067 int j; |
3051 START_TIMER | 3068 START_TIMER |