comparison parser.c @ 3098:eb3d3988aff2 libavcodec

adts aac parser
author mru
date Wed, 08 Feb 2006 00:50:22 +0000
parents befacb1cb573
children 78d6bfc238f3
comparison
equal deleted inserted replaced
3097:41fb9bda307d 3098:eb3d3988aff2
725 } 725 }
726 } 726 }
727 return buf_ptr - buf; 727 return buf_ptr - buf;
728 } 728 }
729 729
730 /* also used for ADTS AAC */
730 typedef struct AC3ParseContext { 731 typedef struct AC3ParseContext {
731 uint8_t inbuf[4096]; /* input buffer */ 732 uint8_t inbuf[4096]; /* input buffer */
732 uint8_t *inbuf_ptr; 733 uint8_t *inbuf_ptr;
733 int frame_size; 734 int frame_size;
735 int header_size;
736 int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
737 int *bit_rate, int *samples);
734 } AC3ParseContext; 738 } AC3ParseContext;
735 739
736 #define AC3_HEADER_SIZE 7 740 #define AC3_HEADER_SIZE 7
741 #define AAC_HEADER_SIZE 8
737 742
738 static const int ac3_sample_rates[4] = { 743 static const int ac3_sample_rates[4] = {
739 48000, 44100, 32000, 0 744 48000, 44100, 32000, 0
740 }; 745 };
741 746
788 793
789 static const int ac3_channels[8] = { 794 static const int ac3_channels[8] = {
790 2, 1, 2, 3, 3, 4, 4, 5 795 2, 1, 2, 3, 3, 4, 4, 5
791 }; 796 };
792 797
798 static int aac_sample_rates[16] = {
799 96000, 88200, 64000, 48000, 44100, 32000,
800 24000, 22050, 16000, 12000, 11025, 8000, 7350
801 };
802
803 static int aac_channels[8] = {
804 0, 1, 2, 3, 4, 5, 6, 8
805 };
806
793 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate, 807 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
794 int *bit_rate) 808 int *bit_rate, int *samples)
795 { 809 {
796 unsigned int fscod, frmsizecod, acmod, bsid, lfeon; 810 unsigned int fscod, frmsizecod, acmod, bsid, lfeon;
797 GetBitContext bits; 811 GetBitContext bits;
798 812
799 init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8); 813 init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8);
822 lfeon = get_bits(&bits, 1); 836 lfeon = get_bits(&bits, 1);
823 837
824 *sample_rate = ac3_sample_rates[fscod]; 838 *sample_rate = ac3_sample_rates[fscod];
825 *bit_rate = ac3_bitrates[frmsizecod] * 1000; 839 *bit_rate = ac3_bitrates[frmsizecod] * 1000;
826 *channels = ac3_channels[acmod] + lfeon; 840 *channels = ac3_channels[acmod] + lfeon;
841 *samples = 6 * 256;
827 842
828 return ac3_frame_sizes[frmsizecod][fscod] * 2; 843 return ac3_frame_sizes[frmsizecod][fscod] * 2;
844 }
845
846 static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
847 int *bit_rate, int *samples)
848 {
849 GetBitContext bits;
850 int size, rdb, ch, sr;
851
852 init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
853
854 if(get_bits(&bits, 12) != 0xfff)
855 return 0;
856
857 get_bits(&bits, 1);
858 get_bits(&bits, 2);
859 get_bits(&bits, 1); /* protection_absent */
860 get_bits(&bits, 2);
861 sr = get_bits(&bits, 4);
862 if(!aac_sample_rates[sr])
863 return 0;
864 get_bits(&bits, 1); /* private_bit */
865 ch = get_bits(&bits, 3);
866 if(!aac_channels[ch])
867 return 0;
868 get_bits(&bits, 1); /* original/copy */
869 get_bits(&bits, 1); /* home */
870
871 /* adts_variable_header */
872 get_bits(&bits, 1); /* copyright_identification_bit */
873 get_bits(&bits, 1); /* copyright_identification_start */
874 size = get_bits(&bits, 13);
875 get_bits(&bits, 11); /* adts_buffer_fullness */
876 rdb = get_bits(&bits, 2);
877
878 *channels = aac_channels[ch];
879 *sample_rate = aac_sample_rates[sr];
880 *samples = (rdb + 1) * 1024;
881 *bit_rate = size * 8 * *sample_rate / *samples;
882
883 return size;
829 } 884 }
830 885
831 static int ac3_parse_init(AVCodecParserContext *s1) 886 static int ac3_parse_init(AVCodecParserContext *s1)
832 { 887 {
833 AC3ParseContext *s = s1->priv_data; 888 AC3ParseContext *s = s1->priv_data;
834 s->inbuf_ptr = s->inbuf; 889 s->inbuf_ptr = s->inbuf;
890 s->header_size = AC3_HEADER_SIZE;
891 s->sync = ac3_sync;
835 return 0; 892 return 0;
836 } 893 }
837 894
895 static int aac_parse_init(AVCodecParserContext *s1)
896 {
897 AC3ParseContext *s = s1->priv_data;
898 s->inbuf_ptr = s->inbuf;
899 s->header_size = AAC_HEADER_SIZE;
900 s->sync = aac_sync;
901 return 0;
902 }
903
904 /* also used for ADTS AAC */
838 static int ac3_parse(AVCodecParserContext *s1, 905 static int ac3_parse(AVCodecParserContext *s1,
839 AVCodecContext *avctx, 906 AVCodecContext *avctx,
840 uint8_t **poutbuf, int *poutbuf_size, 907 uint8_t **poutbuf, int *poutbuf_size,
841 const uint8_t *buf, int buf_size) 908 const uint8_t *buf, int buf_size)
842 { 909 {
843 AC3ParseContext *s = s1->priv_data; 910 AC3ParseContext *s = s1->priv_data;
844 const uint8_t *buf_ptr; 911 const uint8_t *buf_ptr;
845 int len, sample_rate, bit_rate, channels; 912 int len, sample_rate, bit_rate, channels, samples;
846 913
847 *poutbuf = NULL; 914 *poutbuf = NULL;
848 *poutbuf_size = 0; 915 *poutbuf_size = 0;
849 916
850 buf_ptr = buf; 917 buf_ptr = buf;
851 while (buf_size > 0) { 918 while (buf_size > 0) {
852 len = s->inbuf_ptr - s->inbuf; 919 len = s->inbuf_ptr - s->inbuf;
853 if (s->frame_size == 0) { 920 if (s->frame_size == 0) {
854 /* no header seen : find one. We need at least 7 bytes to parse it */ 921 /* no header seen : find one. We need at least s->header_size
855 len = FFMIN(AC3_HEADER_SIZE - len, buf_size); 922 bytes to parse it */
923 len = FFMIN(s->header_size - len, buf_size);
856 924
857 memcpy(s->inbuf_ptr, buf_ptr, len); 925 memcpy(s->inbuf_ptr, buf_ptr, len);
858 buf_ptr += len; 926 buf_ptr += len;
859 s->inbuf_ptr += len; 927 s->inbuf_ptr += len;
860 buf_size -= len; 928 buf_size -= len;
861 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) { 929 if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
862 len = ac3_sync(s->inbuf, &channels, &sample_rate, &bit_rate); 930 len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
931 &samples);
863 if (len == 0) { 932 if (len == 0) {
864 /* no sync found : move by one byte (inefficient, but simple!) */ 933 /* no sync found : move by one byte (inefficient, but simple!) */
865 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1); 934 memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
866 s->inbuf_ptr--; 935 s->inbuf_ptr--;
867 } else { 936 } else {
868 s->frame_size = len; 937 s->frame_size = len;
869 /* update codec info */ 938 /* update codec info */
870 avctx->sample_rate = sample_rate; 939 avctx->sample_rate = sample_rate;
871 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ 940 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
872 if(avctx->channels!=1 && avctx->channels!=2){ 941 if(avctx->codec_id == CODEC_ID_AC3){
942 if(avctx->channels!=1 && avctx->channels!=2){
943 avctx->channels = channels;
944 }
945 } else {
873 avctx->channels = channels; 946 avctx->channels = channels;
874 } 947 }
875 avctx->bit_rate = bit_rate; 948 avctx->bit_rate = bit_rate;
876 avctx->frame_size = 6 * 256; 949 avctx->frame_size = samples;
877 } 950 }
878 } 951 }
879 } else { 952 } else {
880 len = FFMIN(s->frame_size - len, buf_size); 953 len = FFMIN(s->frame_size - len, buf_size);
881 954
927 sizeof(AC3ParseContext), 1000 sizeof(AC3ParseContext),
928 ac3_parse_init, 1001 ac3_parse_init,
929 ac3_parse, 1002 ac3_parse,
930 NULL, 1003 NULL,
931 }; 1004 };
1005
1006 AVCodecParser aac_parser = {
1007 { CODEC_ID_AAC },
1008 sizeof(AC3ParseContext),
1009 aac_parse_init,
1010 ac3_parse,
1011 NULL,
1012 };