comparison sonic.c @ 2205:b90828c162f5 libavcodec

more decorrelation types
author alex
date Sat, 04 Sep 2004 11:19:37 +0000
parents 6d40885b03ad
children 582e635cfa08
comparison
equal deleted inserted replaced
2204:4f8da6a9e6eb 2205:b90828c162f5
34 * - selectable intlist writers/readers (bonk-style, golomb, cabac) 34 * - selectable intlist writers/readers (bonk-style, golomb, cabac)
35 */ 35 */
36 36
37 #define MAX_CHANNELS 2 37 #define MAX_CHANNELS 2
38 38
39 #define MID_SIDE 0
40 #define LEFT_SIDE 1
41 #define RIGHT_SIDE 2
42
39 typedef struct SonicContext { 43 typedef struct SonicContext {
40 int lossless, mid_side; 44 int lossless, decorrelation;
41 45
42 int num_taps, downsampling; 46 int num_taps, downsampling;
43 double quantization; 47 double quantization;
44 48
45 int channels, samplerate, block_align, frame_size; 49 int channels, samplerate, block_align, frame_size;
505 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n"); 509 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
506 return -1; /* only stereo or mono for now */ 510 return -1; /* only stereo or mono for now */
507 } 511 }
508 512
509 if (avctx->channels == 2) 513 if (avctx->channels == 2)
510 s->mid_side = 1; 514 s->decorrelation = MID_SIDE;
511 515
512 if (avctx->codec->id == CODEC_ID_SONIC_LS) 516 if (avctx->codec->id == CODEC_ID_SONIC_LS)
513 { 517 {
514 s->lossless = 1; 518 s->lossless = 1;
515 s->num_taps = 32; 519 s->num_taps = 32;
577 put_bits(&pb, 4, code_samplerate(s->samplerate)); 581 put_bits(&pb, 4, code_samplerate(s->samplerate));
578 } 582 }
579 put_bits(&pb, 1, s->lossless); 583 put_bits(&pb, 1, s->lossless);
580 if (!s->lossless) 584 if (!s->lossless)
581 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision 585 put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
582 put_bits(&pb, 1, s->mid_side); 586 put_bits(&pb, 2, s->decorrelation);
583 put_bits(&pb, 2, s->downsampling); 587 put_bits(&pb, 2, s->downsampling);
584 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024 588 put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
585 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table 589 put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
586 590
587 flush_put_bits(&pb); 591 flush_put_bits(&pb);
588 avctx->extradata_size = put_bits_count(&pb)/8; 592 avctx->extradata_size = put_bits_count(&pb)/8;
589 593
590 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d ms: %d taps: %d block: %d frame: %d downsamp: %d\n", 594 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
591 version, s->lossless, s->mid_side, s->num_taps, s->block_align, s->frame_size, s->downsampling); 595 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
592 596
593 avctx->coded_frame = avcodec_alloc_frame(); 597 avctx->coded_frame = avcodec_alloc_frame();
594 if (!avctx->coded_frame) 598 if (!avctx->coded_frame)
595 return -ENOMEM; 599 return -ENOMEM;
596 avctx->coded_frame->key_frame = 1; 600 avctx->coded_frame->key_frame = 1;
628 632
629 init_put_bits(&pb, buf, buf_size*8); 633 init_put_bits(&pb, buf, buf_size*8);
630 634
631 // short -> internal 635 // short -> internal
632 for (i = 0; i < s->frame_size; i++) 636 for (i = 0; i < s->frame_size; i++)
633 {
634 // if (samples[i] < 0)
635 // s->int_samples[i] = samples[i]+32768;
636 // else
637 // s->int_samples[i] = samples[i]-32768;
638 s->int_samples[i] = samples[i]; 637 s->int_samples[i] = samples[i];
639 // av_log(NULL, AV_LOG_INFO, "%d\n", s->int_samples[i]);
640 }
641 638
642 if (!s->lossless) 639 if (!s->lossless)
643 for (i = 0; i < s->frame_size; i++) 640 for (i = 0; i < s->frame_size; i++)
644 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT; 641 s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
645 642
646 if (s->mid_side) 643 switch(s->decorrelation)
647 for (i = 0; i < s->frame_size; i += s->channels) 644 {
648 { 645 case MID_SIDE:
649 s->int_samples[i] += s->int_samples[i+1]; 646 for (i = 0; i < s->frame_size; i += s->channels)
650 s->int_samples[i+1] -= shift(s->int_samples[i], 1); 647 {
651 } 648 s->int_samples[i] += s->int_samples[i+1];
649 s->int_samples[i+1] -= shift(s->int_samples[i], 1);
650 }
651 break;
652 case LEFT_SIDE:
653 for (i = 0; i < s->frame_size; i += s->channels)
654 s->int_samples[i+1] -= s->int_samples[i];
655 break;
656 case RIGHT_SIDE:
657 for (i = 0; i < s->frame_size; i += s->channels)
658 s->int_samples[i] -= s->int_samples[i+1];
659 break;
660 }
652 661
653 memset(s->window, 0, 4* s->window_size); 662 memset(s->window, 0, 4* s->window_size);
654 663
655 for (i = 0; i < s->tail_size; i++) 664 for (i = 0; i < s->tail_size; i++)
656 s->window[x++] = s->tail[i]; 665 s->window[x++] = s->tail[i];
775 } 784 }
776 785
777 s->lossless = get_bits1(&gb); 786 s->lossless = get_bits1(&gb);
778 if (!s->lossless) 787 if (!s->lossless)
779 skip_bits(&gb, 3); // XXX FIXME 788 skip_bits(&gb, 3); // XXX FIXME
780 s->mid_side = get_bits1(&gb); 789 s->decorrelation = get_bits(&gb, 2);
781 790
782 s->downsampling = get_bits(&gb, 2); 791 s->downsampling = get_bits(&gb, 2);
783 s->num_taps = (get_bits(&gb, 5)+1)<<5; 792 s->num_taps = (get_bits(&gb, 5)+1)<<5;
784 if (get_bits1(&gb)) // XXX FIXME 793 if (get_bits1(&gb)) // XXX FIXME
785 av_log(avctx, AV_LOG_INFO, "Custom quant table\n"); 794 av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
786 795
787 s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling; 796 s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling;
788 s->frame_size = s->channels*s->block_align*s->downsampling; 797 s->frame_size = s->channels*s->block_align*s->downsampling;
789 // avctx->frame_size = s->block_align; 798 // avctx->frame_size = s->block_align;
790 799
791 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d ms: %d taps: %d block: %d frame: %d downsamp: %d\n", 800 av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
792 version, s->lossless, s->mid_side, s->num_taps, s->block_align, s->frame_size, s->downsampling); 801 version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
793 802
794 // generate taps 803 // generate taps
795 s->tap_quant = av_mallocz(4* s->num_taps); 804 s->tap_quant = av_mallocz(4* s->num_taps);
796 for (i = 0; i < s->num_taps; i++) 805 for (i = 0; i < s->num_taps; i++)
797 s->tap_quant[i] = (int)(sqrt(i+1)); 806 s->tap_quant[i] = (int)(sqrt(i+1));
884 893
885 for (i = 0; i < s->num_taps; i++) 894 for (i = 0; i < s->num_taps; i++)
886 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels]; 895 s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
887 } 896 }
888 897
889 if (s->mid_side) 898 switch(s->decorrelation)
890 for (i = 0; i < s->frame_size; i += s->channels) 899 {
891 { 900 case MID_SIDE:
892 s->int_samples[i+1] += shift(s->int_samples[i], 1); 901 for (i = 0; i < s->frame_size; i += s->channels)
893 s->int_samples[i] -= s->int_samples[i+1]; 902 {
894 } 903 s->int_samples[i+1] += shift(s->int_samples[i], 1);
904 s->int_samples[i] -= s->int_samples[i+1];
905 }
906 break;
907 case LEFT_SIDE:
908 for (i = 0; i < s->frame_size; i += s->channels)
909 s->int_samples[i+1] += s->int_samples[i];
910 break;
911 case RIGHT_SIDE:
912 for (i = 0; i < s->frame_size; i += s->channels)
913 s->int_samples[i] += s->int_samples[i+1];
914 break;
915 }
895 916
896 if (!s->lossless) 917 if (!s->lossless)
897 for (i = 0; i < s->frame_size; i++) 918 for (i = 0; i < s->frame_size; i++)
898 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT); 919 s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
899 920
907 else 928 else
908 samples[i] = s->int_samples[i]; 929 samples[i] = s->int_samples[i];
909 } 930 }
910 931
911 align_get_bits(&gb); 932 align_get_bits(&gb);
912
913 // if (buf_size != (get_bits_count(&gb)+7)/8)
914 // av_log(NULL, AV_LOG_INFO, "buf_size (%d) and used bytes (%d) differs\n", buf_size, (get_bits_count(&gb)+7)/8);
915 933
916 *data_size = s->frame_size * 2; 934 *data_size = s->frame_size * 2;
917 935
918 return (get_bits_count(&gb)+7)/8; 936 return (get_bits_count(&gb)+7)/8;
919 } 937 }