Revision 62327e28 libavcodec/parser.c

View differences:

libavcodec/parser.c
748 748
    return buf_ptr - buf;
749 749
}
750 750

  
751
#ifdef CONFIG_AC3
752
#ifdef CONFIG_A52BIN
753
extern int ff_a52_syncinfo (AVCodecContext * avctx, const uint8_t * buf,
754
                       int * flags, int * sample_rate, int * bit_rate);
755
#else
756
extern int a52_syncinfo (const uint8_t * buf, int * flags,
757
                         int * sample_rate, int * bit_rate);
758
#endif
759

  
760 751
typedef struct AC3ParseContext {
761 752
    uint8_t inbuf[4096]; /* input buffer */
762 753
    uint8_t *inbuf_ptr;
763 754
    int frame_size;
764
    int flags;
765 755
} AC3ParseContext;
766 756

  
767 757
#define AC3_HEADER_SIZE 7
768
#define A52_LFE 16
758

  
759
static const int ac3_sample_rates[4] = {
760
    48000, 44100, 32000, 0
761
};
762

  
763
static const int ac3_frame_sizes[64][3] = {
764
    { 64,   69,   96   },  
765
    { 64,   70,   96   },  
766
    { 80,   87,   120  },  
767
    { 80,   88,   120  },  
768
    { 96,   104,  144  },  
769
    { 96,   105,  144  },  
770
    { 112,  121,  168  }, 
771
    { 112,  122,  168  }, 
772
    { 128,  139,  192  }, 
773
    { 128,  140,  192  }, 
774
    { 160,  174,  240  }, 
775
    { 160,  175,  240  }, 
776
    { 192,  208,  288  }, 
777
    { 192,  209,  288  }, 
778
    { 224,  243,  336  }, 
779
    { 224,  244,  336  }, 
780
    { 256,  278,  384  }, 
781
    { 256,  279,  384  }, 
782
    { 320,  348,  480  }, 
783
    { 320,  349,  480  }, 
784
    { 384,  417,  576  }, 
785
    { 384,  418,  576  }, 
786
    { 448,  487,  672  }, 
787
    { 448,  488,  672  }, 
788
    { 512,  557,  768  }, 
789
    { 512,  558,  768  }, 
790
    { 640,  696,  960  }, 
791
    { 640,  697,  960  }, 
792
    { 768,  835,  1152 }, 
793
    { 768,  836,  1152 }, 
794
    { 896,  975,  1344 }, 
795
    { 896,  976,  1344 }, 
796
    { 1024, 1114, 1536 },
797
    { 1024, 1115, 1536 },
798
    { 1152, 1253, 1728 },
799
    { 1152, 1254, 1728 },
800
    { 1280, 1393, 1920 },
801
    { 1280, 1394, 1920 },
802
};
803

  
804
static const int ac3_bitrates[64] = {
805
    32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112,
806
    128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384,
807
    384, 448, 448, 512, 512, 576, 576, 640, 640,
808
};
809

  
810
static const int ac3_channels[8] = {
811
    2, 1, 2, 3, 3, 4, 4, 5
812
};
813

  
814
static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
815
		    int *bit_rate)
816
{
817
    unsigned int fscod, frmsizecod, acmod, bsid, lfeon;
818
    GetBitContext bits;
819

  
820
    init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8);
821

  
822
    if(get_bits(&bits, 16) != 0x0b77)
823
	return 0;
824

  
825
    get_bits(&bits, 16);	/* crc */
826
    fscod = get_bits(&bits, 2);
827
    frmsizecod = get_bits(&bits, 6);
828

  
829
    if(!ac3_sample_rates[fscod])
830
	return 0;
831

  
832
    bsid = get_bits(&bits, 5);
833
    if(bsid > 8)
834
	return 0;
835
    get_bits(&bits, 3);	/* bsmod */
836
    acmod = get_bits(&bits, 3);
837
    if(acmod & 1 && acmod != 1)
838
	get_bits(&bits, 2); /* cmixlev */
839
    if(acmod & 4)
840
	get_bits(&bits, 2); /* surmixlev */
841
    if(acmod & 2)
842
	get_bits(&bits, 2); /* dsurmod */
843
    lfeon = get_bits(&bits, 1);
844

  
845
    *sample_rate = ac3_sample_rates[fscod];
846
    *bit_rate = ac3_bitrates[frmsizecod] * 1000;
847
    *channels = ac3_channels[acmod] + lfeon;
848

  
849
    return ac3_frame_sizes[frmsizecod][fscod] * 2;
850
}
769 851

  
770 852
static int ac3_parse_init(AVCodecParserContext *s1)
771 853
{
......
781 863
{
782 864
    AC3ParseContext *s = s1->priv_data;
783 865
    const uint8_t *buf_ptr;
784
    int len, sample_rate, bit_rate;
785
    static const int ac3_channels[8] = {
786
        2, 1, 2, 3, 3, 4, 4, 5
787
    };
866
    int len, sample_rate, bit_rate, channels;
788 867

  
789 868
    *poutbuf = NULL;
790 869
    *poutbuf_size = 0;
......
802 881
            s->inbuf_ptr += len;
803 882
            buf_size -= len;
804 883
            if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
805
#ifdef CONFIG_A52BIN
806
                len = ff_a52_syncinfo(avctx, s->inbuf, &s->flags, &sample_rate, &bit_rate);
807
#else
808
                len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
809
#endif
884
		len = ac3_sync(s->inbuf, &channels, &sample_rate, &bit_rate);
810 885
                if (len == 0) {
811 886
                    /* no sync found : move by one byte (inefficient, but simple!) */
812 887
                    memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
......
817 892
                    avctx->sample_rate = sample_rate;
818 893
                    /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
819 894
                    if(avctx->channels!=1 && avctx->channels!=2){
820
                        avctx->channels = ac3_channels[s->flags & 7];
821
                        if (s->flags & A52_LFE)
822
                            avctx->channels++;
895
			avctx->channels = channels;
823 896
                    }
824 897
                    avctx->bit_rate = bit_rate;
825 898
                    avctx->frame_size = 6 * 256;
......
844 917
    }
845 918
    return buf_ptr - buf;
846 919
}
847
#endif
848 920

  
849 921
AVCodecParser mpegvideo_parser = {
850 922
    { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
......
872 944
    NULL,
873 945
};
874 946

  
875
#ifdef CONFIG_AC3
876 947
AVCodecParser ac3_parser = {
877 948
    { CODEC_ID_AC3 },
878 949
    sizeof(AC3ParseContext),
......
880 951
    ac3_parse,
881 952
    NULL,
882 953
};
883
#endif

Also available in: Unified diff