Unified · Split
| @@ -485,227 +485,8 @@ static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, in · 484 unmodified lines | |||
| 485 | } | 485 | } |
| 486 | } | 486 | } |
| 487 | 487 | ||
| 488 | static inline void init_ref(MotionEstContext *c, const uint8_t *const src[3], uint8_t *const ref[3], uint8_t *const ref2[3], int x, int y, int ref_index){ | ||
| 489 | SnowContext *s = c->avctx->priv_data; | ||
| 490 | const int offset[3]= { | ||
| 491 | y*c-> stride + x, | ||
| 492 | ((y*c->uvstride + x)>>s->chroma_h_shift), | ||
| 493 | ((y*c->uvstride + x)>>s->chroma_h_shift), | ||
| 494 | }; | ||
| 495 | int i; | ||
| 496 | for(i=0; i<3; i++){ | ||
| 497 | c->src[0][i]= src [i]; | ||
| 498 | c->ref[0][i]= ref [i] + offset[i]; | ||
| 499 | } | ||
| 500 | av_assert2(!ref_index); | ||
| 501 | } | ||
| 502 | |||
| 503 | |||
| 504 | /* bitstream functions */ | ||
| 505 | |||
| 506 | extern const int8_t ff_quant3bA[256]; | 488 | extern const int8_t ff_quant3bA[256]; |
| 507 | 489 | ||
| 508 | #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0 | 490 | #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0 |
| 509 | 491 | ||
| 510 | static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){ | ||
| 511 | int i; | ||
| 512 | |||
| 513 | if(v){ | ||
| 514 | const int a= FFABS(v); | ||
| 515 | const int e= av_log2(a); | ||
| 516 | const int el= FFMIN(e, 10); | ||
| 517 | put_rac(c, state+0, 0); | ||
| 518 | |||
| 519 | for(i=0; i<el; i++){ | ||
| 520 | put_rac(c, state+1+i, 1); //1..10 | ||
| 521 | } | ||
| 522 | for(; i<e; i++){ | ||
| 523 | put_rac(c, state+1+9, 1); //1..10 | ||
| 524 | } | ||
| 525 | put_rac(c, state+1+FFMIN(i,9), 0); | ||
| 526 | |||
| 527 | for(i=e-1; i>=el; i--){ | ||
| 528 | put_rac(c, state+22+9, (a>>i)&1); //22..31 | ||
| 529 | } | ||
| 530 | for(; i>=0; i--){ | ||
| 531 | put_rac(c, state+22+i, (a>>i)&1); //22..31 | ||
| 532 | } | ||
| 533 | |||
| 534 | if(is_signed) | ||
| 535 | put_rac(c, state+11 + el, v < 0); //11..21 | ||
| 536 | }else{ | ||
| 537 | put_rac(c, state+0, 1); | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 541 | static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){ | ||
| 542 | if(get_rac(c, state+0)) | ||
| 543 | return 0; | ||
| 544 | else{ | ||
| 545 | int i, e; | ||
| 546 | unsigned a; | ||
| 547 | e= 0; | ||
| 548 | while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10 | ||
| 549 | e++; | ||
| 550 | if (e > 31) | ||
| 551 | return AVERROR_INVALIDDATA; | ||
| 552 | } | ||
| 553 | |||
| 554 | a= 1; | ||
| 555 | for(i=e-1; i>=0; i--){ | ||
| 556 | a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31 | ||
| 557 | } | ||
| 558 | |||
| 559 | e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21 | ||
| 560 | return (a^e)-e; | ||
| 561 | } | ||
| 562 | } | ||
| 563 | |||
| 564 | static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){ | ||
| 565 | int i; | ||
| 566 | int r= log2>=0 ? 1<<log2 : 1; | ||
| 567 | |||
| 568 | av_assert2(v>=0); | ||
| 569 | av_assert2(log2>=-4); | ||
| 570 | |||
| 571 | while(v >= r){ | ||
| 572 | put_rac(c, state+4+log2, 1); | ||
| 573 | v -= r; | ||
| 574 | log2++; | ||
| 575 | if(log2>0) r+=r; | ||
| 576 | } | ||
| 577 | put_rac(c, state+4+log2, 0); | ||
| 578 | |||
| 579 | for(i=log2-1; i>=0; i--){ | ||
| 580 | put_rac(c, state+31-i, (v>>i)&1); | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){ | ||
| 585 | int i; | ||
| 586 | int r= log2>=0 ? 1<<log2 : 1; | ||
| 587 | int v=0; | ||
| 588 | |||
| 589 | av_assert2(log2>=-4); | ||
| 590 | |||
| 591 | while(log2<28 && get_rac(c, state+4+log2)){ | ||
| 592 | v+= r; | ||
| 593 | log2++; | ||
| 594 | if(log2>0) r+=r; | ||
| 595 | } | ||
| 596 | |||
| 597 | for(i=log2-1; i>=0; i--){ | ||
| 598 | v+= get_rac(c, state+31-i)<<i; | ||
| 599 | } | ||
| 600 | |||
| 601 | return v; | ||
| 602 | } | ||
| 603 | |||
| 604 | static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){ | ||
| 605 | const int w= b->width; | ||
| 606 | const int h= b->height; | ||
| 607 | int x,y; | ||
| 608 | |||
| 609 | int run, runs; | ||
| 610 | x_and_coeff *xc= b->x_coeff; | ||
| 611 | x_and_coeff *prev_xc= NULL; | ||
| 612 | x_and_coeff *prev2_xc= xc; | ||
| 613 | x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL; | ||
| 614 | x_and_coeff *prev_parent_xc= parent_xc; | ||
| 615 | |||
| 616 | runs= get_symbol2(&s->c, b->state[30], 0); | ||
| 617 | if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3); | ||
| 618 | else run= INT_MAX; | ||
| 619 | |||
| 620 | for(y=0; y<h; y++){ | ||
| 621 | int v=0; | ||
| 622 | int lt=0, t=0, rt=0; | ||
| 623 | |||
| 624 | if(y && prev_xc->x == 0){ | ||
| 625 | rt= prev_xc->coeff; | ||
| 626 | } | ||
| 627 | for(x=0; x<w; x++){ | ||
| 628 | int p=0; | ||
| 629 | const int l= v; | ||
| 630 | |||
| 631 | lt= t; t= rt; | ||
| 632 | |||
| 633 | if(y){ | ||
| 634 | if(prev_xc->x <= x) | ||
| 635 | prev_xc++; | ||
| 636 | if(prev_xc->x == x + 1) | ||
| 637 | rt= prev_xc->coeff; | ||
| 638 | else | ||
| 639 | rt=0; | ||
| 640 | } | ||
| 641 | if(parent_xc){ | ||
| 642 | if(x>>1 > parent_xc->x){ | ||
| 643 | parent_xc++; | ||
| 644 | } | ||
| 645 | if(x>>1 == parent_xc->x){ | ||
| 646 | p= parent_xc->coeff; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | if(/*ll|*/l|lt|t|rt|p){ | ||
| 650 | int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1)); | ||
| 651 | |||
| 652 | v=get_rac(&s->c, &b->state[0][context]); | ||
| 653 | if(v){ | ||
| 654 | v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1); | ||
| 655 | v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3*ff_quant3bA[t&0xFF]]); | ||
| 656 | if ((uint16_t)v != v) { | ||
| 657 | av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n"); | ||
| 658 | v = 1; | ||
| 659 | } | ||
| 660 | xc->x=x; | ||
| 661 | (xc++)->coeff= v; | ||
| 662 | } | ||
| 663 | }else{ | ||
| 664 | if(!run){ | ||
| 665 | if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3); | ||
| 666 | else run= INT_MAX; | ||
| 667 | v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1); | ||
| 668 | v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]); | ||
| 669 | if ((uint16_t)v != v) { | ||
| 670 | av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n"); | ||
| 671 | v = 1; | ||
| 672 | } | ||
| 673 | |||
| 674 | xc->x=x; | ||
| 675 | (xc++)->coeff= v; | ||
| 676 | }else{ | ||
| 677 | int max_run; | ||
| 678 | run--; | ||
| 679 | v=0; | ||
| 680 | av_assert2(run >= 0); | ||
| 681 | if(y) max_run= FFMIN(run, prev_xc->x - x - 2); | ||
| 682 | else max_run= FFMIN(run, w-x-1); | ||
| 683 | if(parent_xc) | ||
| 684 | max_run= FFMIN(max_run, 2*parent_xc->x - x - 1); | ||
| 685 | av_assert2(max_run >= 0 && max_run <= run); | ||
| 686 | |||
| 687 | x+= max_run; | ||
| 688 | run-= max_run; | ||
| 689 | } | ||
| 690 | } | ||
| 691 | } | ||
| 692 | (xc++)->x= w+1; //end marker | ||
| 693 | prev_xc= prev2_xc; | ||
| 694 | prev2_xc= xc; | ||
| 695 | |||
| 696 | if(parent_xc){ | ||
| 697 | if(y&1){ | ||
| 698 | while(parent_xc->x != parent->width+1) | ||
| 699 | parent_xc++; | ||
| 700 | parent_xc++; | ||
| 701 | prev_parent_xc= parent_xc; | ||
| 702 | }else{ | ||
| 703 | parent_xc= prev_parent_xc; | ||
| 704 | } | ||
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | (xc++)->x= w+1; //end marker | ||
| 709 | } | ||
| 710 | |||
| 711 | #endif /* AVCODEC_SNOW_H */ | 492 | #endif /* AVCODEC_SNOW_H */ |
| @@ -31,6 +31,153 @@ · 30 unmodified lines | |||
| 31 | #include "rangecoder.h" | 31 | #include "rangecoder.h" |
| 32 | #include "mathops.h" | 32 | #include "mathops.h" |
| 33 | 33 | ||
| 34 | static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed) | ||
| 35 | { | ||
| 36 | if (get_rac(c, state + 0)) | ||
| 37 | return 0; | ||
| 38 | else { | ||
| 39 | int e; | ||
| 40 | unsigned a; | ||
| 41 | e = 0; | ||
| 42 | while (get_rac(c, state + 1 + FFMIN(e, 9))) { //1..10 | ||
| 43 | e++; | ||
| 44 | if (e > 31) | ||
| 45 | return AVERROR_INVALIDDATA; | ||
| 46 | } | ||
| 47 | |||
| 48 | a = 1; | ||
| 49 | for (int i = e - 1; i >= 0; i--) | ||
| 50 | a += a + get_rac(c, state + 22 + FFMIN(i, 9)); //22..31 | ||
| 51 | |||
| 52 | e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); //11..21 | ||
| 53 | return (a ^ e) - e; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2) | ||
| 58 | { | ||
| 59 | int r = log2 >= 0 ? 1 << log2 : 1; | ||
| 60 | int v = 0; | ||
| 61 | |||
| 62 | av_assert2(log2 >= -4); | ||
| 63 | |||
| 64 | while (log2 < 28 && get_rac(c, state + 4 + log2)) { | ||
| 65 | v += r; | ||
| 66 | log2++; | ||
| 67 | if (log2 > 0) r += r; | ||
| 68 | } | ||
| 69 | |||
| 70 | for (int i = log2 - 1; i >= 0; i--) | ||
| 71 | v += get_rac(c, state + 31 - i) << i; | ||
| 72 | |||
| 73 | return v; | ||
| 74 | } | ||
| 75 | |||
| 76 | static void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation) | ||
| 77 | { | ||
| 78 | const int w = b->width; | ||
| 79 | const int h = b->height; | ||
| 80 | |||
| 81 | int run, runs; | ||
| 82 | x_and_coeff *xc = b->x_coeff; | ||
| 83 | x_and_coeff *prev_xc = NULL; | ||
| 84 | x_and_coeff *prev2_xc = xc; | ||
| 85 | x_and_coeff *parent_xc = parent ? parent->x_coeff : NULL; | ||
| 86 | x_and_coeff *prev_parent_xc = parent_xc; | ||
| 87 | |||
| 88 | runs = get_symbol2(&s->c, b->state[30], 0); | ||
| 89 | if (runs-- > 0) run = get_symbol2(&s->c, b->state[1], 3); | ||
| 90 | else run = INT_MAX; | ||
| 91 | |||
| 92 | for (int y = 0; y < h; y++) { | ||
| 93 | int v = 0; | ||
| 94 | int lt = 0, t = 0, rt = 0; | ||
| 95 | |||
| 96 | if (y && prev_xc->x == 0) | ||
| 97 | rt = prev_xc->coeff; | ||
| 98 | |||
| 99 | for (int x = 0; x < w; x++) { | ||
| 100 | int p = 0; | ||
| 101 | const int l = v; | ||
| 102 | |||
| 103 | lt= t; t= rt; | ||
| 104 | |||
| 105 | if (y) { | ||
| 106 | if (prev_xc->x <= x) | ||
| 107 | prev_xc++; | ||
| 108 | if (prev_xc->x == x + 1) | ||
| 109 | rt = prev_xc->coeff; | ||
| 110 | else | ||
| 111 | rt = 0; | ||
| 112 | } | ||
| 113 | if (parent_xc) { | ||
| 114 | if (x>>1 > parent_xc->x) | ||
| 115 | parent_xc++; | ||
| 116 | if (x>>1 == parent_xc->x) | ||
| 117 | p = parent_xc->coeff; | ||
| 118 | } | ||
| 119 | if (/*ll|*/l|lt|t|rt|p) { | ||
| 120 | int context = av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1)); | ||
| 121 | |||
| 122 | v = get_rac(&s->c, &b->state[0][context]); | ||
| 123 | if (v) { | ||
| 124 | v = 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1); | ||
| 125 | v += get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3 * ff_quant3bA[t&0xFF]]); | ||
| 126 | if ((uint16_t)v != v) { | ||
| 127 | av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n"); | ||
| 128 | v = 1; | ||
| 129 | } | ||
| 130 | xc->x = x; | ||
| 131 | (xc++)->coeff = v; | ||
| 132 | } | ||
| 133 | } else { | ||
| 134 | if (!run) { | ||
| 135 | if (runs-- > 0) run = get_symbol2(&s->c, b->state[1], 3); | ||
| 136 | else run = INT_MAX; | ||
| 137 | v = 2 * (get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1); | ||
| 138 | v += get_rac(&s->c, &b->state[0][16 + 1 + 3]); | ||
| 139 | if ((uint16_t)v != v) { | ||
| 140 | av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n"); | ||
| 141 | v = 1; | ||
| 142 | } | ||
| 143 | |||
| 144 | xc->x = x; | ||
| 145 | (xc++)->coeff = v; | ||
| 146 | } else { | ||
| 147 | int max_run; | ||
| 148 | run--; | ||
| 149 | v = 0; | ||
| 150 | av_assert2(run >= 0); | ||
| 151 | if (y) max_run = FFMIN(run, prev_xc->x - x - 2); | ||
| 152 | else max_run = FFMIN(run, w-x-1); | ||
| 153 | if (parent_xc) | ||
| 154 | max_run = FFMIN(max_run, 2*parent_xc->x - x - 1); | ||
| 155 | av_assert2(max_run >= 0 && max_run <= run); | ||
| 156 | |||
| 157 | x += max_run; | ||
| 158 | run -= max_run; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | (xc++)->x = w+1; //end marker | ||
| 163 | prev_xc = prev2_xc; | ||
| 164 | prev2_xc = xc; | ||
| 165 | |||
| 166 | if (parent_xc) { | ||
| 167 | if (y & 1) { | ||
| 168 | while (parent_xc->x != parent->width+1) | ||
| 169 | parent_xc++; | ||
| 170 | parent_xc++; | ||
| 171 | prev_parent_xc= parent_xc; | ||
| 172 | } else { | ||
| 173 | parent_xc= prev_parent_xc; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | (xc++)->x = w + 1; //end marker | ||
| 179 | } | ||
| 180 | |||
| 34 | static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){ | 181 | static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){ |
| 35 | Plane *p= &s->plane[plane_index]; | 182 | Plane *p= &s->plane[plane_index]; |
| 36 | const int mb_w= s->b_width << s->block_max_depth; | 183 | const int mb_w= s->b_width << s->block_max_depth; |
| @@ -39,6 +39,70 @@ · 38 unmodified lines | |||
| 39 | #include "mpegvideo.h" | 39 | #include "mpegvideo.h" |
| 40 | #include "h263enc.h" | 40 | #include "h263enc.h" |
| 41 | 41 | ||
| 42 | static void init_ref(MotionEstContext *c, const uint8_t *const src[3], | ||
| 43 | uint8_t *const ref[3], uint8_t *const ref2[3], | ||
| 44 | int x, int y, int ref_index) | ||
| 45 | { | ||
| 46 | SnowContext *s = c->avctx->priv_data; | ||
| 47 | const int offset[3] = { | ||
| 48 | y*c-> stride + x, | ||
| 49 | ((y*c->uvstride + x) >> s->chroma_h_shift), | ||
| 50 | ((y*c->uvstride + x) >> s->chroma_h_shift), | ||
| 51 | }; | ||
| 52 | for (int i = 0; i < 3; i++) { | ||
| 53 | c->src[0][i] = src [i]; | ||
| 54 | c->ref[0][i] = ref [i] + offset[i]; | ||
| 55 | } | ||
| 56 | av_assert2(!ref_index); | ||
| 57 | } | ||
| 58 | |||
| 59 | static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed) | ||
| 60 | { | ||
| 61 | if (v) { | ||
| 62 | const int a = FFABS(v); | ||
| 63 | const int e = av_log2(a); | ||
| 64 | const int el = FFMIN(e, 10); | ||
| 65 | int i; | ||
| 66 | |||
| 67 | put_rac(c, state + 0, 0); | ||
| 68 | |||
| 69 | for (i = 0; i < el; i++) | ||
| 70 | put_rac(c, state + 1 + i, 1); //1..10 | ||
| 71 | for(; i < e; i++) | ||
| 72 | put_rac(c, state + 1 + 9, 1); //1..10 | ||
| 73 | put_rac(c, state + 1 + FFMIN(i, 9), 0); | ||
| 74 | |||
| 75 | for (i = e - 1; i >= el; i--) | ||
| 76 | put_rac(c, state + 22 + 9, (a >> i) & 1); //22..31 | ||
| 77 | for(; i >= 0; i--) | ||
| 78 | put_rac(c, state + 22 + i, (a >> i) & 1); //22..31 | ||
| 79 | |||
| 80 | if (is_signed) | ||
| 81 | put_rac(c, state + 11 + el, v < 0); //11..21 | ||
| 82 | } else { | ||
| 83 | put_rac(c, state + 0, 1); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2) | ||
| 88 | { | ||
| 89 | int r = log2 >= 0 ? 1<<log2 : 1; | ||
| 90 | |||
| 91 | av_assert2(v >= 0); | ||
| 92 | av_assert2(log2 >= -4); | ||
| 93 | |||
| 94 | while (v >= r) { | ||
| 95 | put_rac(c, state + 4 + log2, 1); | ||
| 96 | v -= r; | ||
| 97 | log2++; | ||
| 98 | if (log2 > 0) r += r; | ||
| 99 | } | ||
| 100 | put_rac(c, state + 4 + log2, 0); | ||
| 101 | |||
| 102 | for (int i = log2 - 1; i >= 0; i--) | ||
| 103 | put_rac(c, state + 31 - i, (v >> i) & 1); | ||
| 104 | } | ||
| 105 | |||
| 42 | static int get_encode_buffer(SnowContext *s, AVFrame *frame) | 106 | static int get_encode_buffer(SnowContext *s, AVFrame *frame) |
| 43 | { | 107 | { |
| 44 | int ret; | 108 | int ret; |