Mercurial > libavcodec.hg
annotate ac3_parser.c @ 6116:4f8fcb40bf2c libavcodec
remove unneeded variables from AC3HeaderInfo
| author | jbr |
|---|---|
| date | Sat, 05 Jan 2008 17:04:57 +0000 |
| parents | 7d9dddd54817 |
| children | 01b1342e717b |
| rev | line source |
|---|---|
| 4941 | 1 /* |
| 2 * AC3 parser | |
| 3 * Copyright (c) 2003 Fabrice Bellard. | |
| 4 * Copyright (c) 2003 Michael Niedermayer. | |
| 5 * | |
| 6 * This file is part of FFmpeg. | |
| 7 * | |
| 8 * FFmpeg is free software; you can redistribute it and/or | |
| 9 * modify it under the terms of the GNU Lesser General Public | |
| 10 * License as published by the Free Software Foundation; either | |
| 11 * version 2.1 of the License, or (at your option) any later version. | |
| 12 * | |
| 13 * FFmpeg is distributed in the hope that it will be useful, | |
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 16 * Lesser General Public License for more details. | |
| 17 * | |
| 18 * You should have received a copy of the GNU Lesser General Public | |
| 19 * License along with FFmpeg; if not, write to the Free Software | |
| 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 21 */ | |
| 22 | |
| 23 #include "parser.h" | |
| 24 #include "ac3_parser.h" | |
| 25 #include "aac_ac3_parser.h" | |
| 26 #include "bitstream.h" | |
| 27 | |
| 28 | |
| 29 #define AC3_HEADER_SIZE 7 | |
| 30 | |
| 31 | |
| 32 static const uint8_t eac3_blocks[4] = { | |
| 33 1, 2, 3, 6 | |
| 34 }; | |
| 35 | |
| 36 | |
| 37 int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr) | |
| 38 { | |
| 39 GetBitContext gbc; | |
| 6116 | 40 int frame_size_code; |
| 4941 | 41 |
| 42 memset(hdr, 0, sizeof(*hdr)); | |
| 43 | |
| 44 init_get_bits(&gbc, buf, 54); | |
| 45 | |
| 46 hdr->sync_word = get_bits(&gbc, 16); | |
| 47 if(hdr->sync_word != 0x0B77) | |
| 5680 | 48 return AC3_PARSE_ERROR_SYNC; |
| 4941 | 49 |
| 50 /* read ahead to bsid to make sure this is AC-3, not E-AC-3 */ | |
| 6005 | 51 hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F; |
| 52 if(hdr->bitstream_id > 10) | |
| 5680 | 53 return AC3_PARSE_ERROR_BSID; |
| 4941 | 54 |
| 55 hdr->crc1 = get_bits(&gbc, 16); | |
| 6003 | 56 hdr->sr_code = get_bits(&gbc, 2); |
| 57 if(hdr->sr_code == 3) | |
| 5680 | 58 return AC3_PARSE_ERROR_SAMPLE_RATE; |
| 4941 | 59 |
| 6116 | 60 frame_size_code = get_bits(&gbc, 6); |
| 61 if(frame_size_code > 37) | |
| 5680 | 62 return AC3_PARSE_ERROR_FRAME_SIZE; |
| 4941 | 63 |
| 64 skip_bits(&gbc, 5); // skip bsid, already got it | |
| 65 | |
| 6116 | 66 skip_bits(&gbc, 3); // skip bitstream mode |
| 6005 | 67 hdr->channel_mode = get_bits(&gbc, 3); |
| 68 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) { | |
| 6116 | 69 skip_bits(&gbc, 2); // skip center mix level |
| 4941 | 70 } |
| 6005 | 71 if(hdr->channel_mode & 4) { |
| 6116 | 72 skip_bits(&gbc, 2); // skip surround mix level |
| 4941 | 73 } |
| 6005 | 74 if(hdr->channel_mode == AC3_CHMODE_STEREO) { |
| 6116 | 75 skip_bits(&gbc, 2); // skip dolby surround mode |
| 4941 | 76 } |
| 6005 | 77 hdr->lfe_on = get_bits1(&gbc); |
| 4941 | 78 |
| 6005 | 79 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8; |
| 6003 | 80 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift; |
| 6116 | 81 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift; |
| 6005 | 82 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on; |
| 6116 | 83 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2; |
| 4941 | 84 |
| 85 return 0; | |
| 86 } | |
| 87 | |
| 88 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate, | |
| 89 int *bit_rate, int *samples) | |
| 90 { | |
| 91 int err; | |
| 6005 | 92 unsigned int sr_code, channel_mode, bitstream_id, lfe_on; |
| 93 unsigned int stream_type, substream_id, frame_size, sr_code2, num_blocks_code; | |
| 4941 | 94 GetBitContext bits; |
| 95 AC3HeaderInfo hdr; | |
| 96 | |
| 97 err = ff_ac3_parse_header(buf, &hdr); | |
| 98 | |
| 99 if(err < 0 && err != -2) | |
| 100 return 0; | |
| 101 | |
| 6005 | 102 bitstream_id = hdr.bitstream_id; |
| 103 if(bitstream_id <= 10) { /* Normal AC-3 */ | |
| 4941 | 104 *sample_rate = hdr.sample_rate; |
| 105 *bit_rate = hdr.bit_rate; | |
| 106 *channels = hdr.channels; | |
| 107 *samples = AC3_FRAME_SIZE; | |
| 108 return hdr.frame_size; | |
| 6005 | 109 } else if (bitstream_id > 10 && bitstream_id <= 16) { /* Enhanced AC-3 */ |
| 4941 | 110 init_get_bits(&bits, &buf[2], (AC3_HEADER_SIZE-2) * 8); |
| 6005 | 111 stream_type = get_bits(&bits, 2); |
| 112 substream_id = get_bits(&bits, 3); | |
| 4941 | 113 |
| 6005 | 114 if (stream_type != 0 || substream_id != 0) |
| 4941 | 115 return 0; /* Currently don't support additional streams */ |
| 116 | |
| 6005 | 117 frame_size = get_bits(&bits, 11) + 1; |
| 118 if(frame_size*2 < AC3_HEADER_SIZE) | |
|
5817
ced30500e2b1
prevent infinite loop and memcpy of negative amounts
michael
parents:
5680
diff
changeset
|
119 return 0; |
|
ced30500e2b1
prevent infinite loop and memcpy of negative amounts
michael
parents:
5680
diff
changeset
|
120 |
| 6003 | 121 sr_code = get_bits(&bits, 2); |
| 122 if (sr_code == 3) { | |
| 123 sr_code2 = get_bits(&bits, 2); | |
| 6005 | 124 num_blocks_code = 3; |
| 4941 | 125 |
| 6003 | 126 if(sr_code2 == 3) |
| 4941 | 127 return 0; |
| 128 | |
| 6003 | 129 *sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2; |
| 4941 | 130 } else { |
| 6005 | 131 num_blocks_code = get_bits(&bits, 2); |
| 4941 | 132 |
| 6003 | 133 *sample_rate = ff_ac3_sample_rate_tab[sr_code]; |
| 4941 | 134 } |
| 135 | |
| 6005 | 136 channel_mode = get_bits(&bits, 3); |
| 137 lfe_on = get_bits1(&bits); | |
| 4941 | 138 |
| 6005 | 139 *samples = eac3_blocks[num_blocks_code] * 256; |
| 140 *bit_rate = frame_size * (*sample_rate) * 16 / (*samples); | |
| 141 *channels = ff_ac3_channels_tab[channel_mode] + lfe_on; | |
| 4941 | 142 |
| 6005 | 143 return frame_size * 2; |
| 4941 | 144 } |
| 145 | |
| 146 /* Unsupported bitstream version */ | |
| 147 return 0; | |
| 148 } | |
| 149 | |
| 150 static int ac3_parse_init(AVCodecParserContext *s1) | |
| 151 { | |
|
4942
b42e963c8149
cosmetics: rename for consistency after previous aac and ac3 parsers move
aurel
parents:
4941
diff
changeset
|
152 AACAC3ParseContext *s = s1->priv_data; |
| 4941 | 153 s->inbuf_ptr = s->inbuf; |
| 154 s->header_size = AC3_HEADER_SIZE; | |
| 155 s->sync = ac3_sync; | |
| 156 return 0; | |
| 157 } | |
| 158 | |
| 159 | |
| 160 AVCodecParser ac3_parser = { | |
| 161 { CODEC_ID_AC3 }, | |
|
4942
b42e963c8149
cosmetics: rename for consistency after previous aac and ac3 parsers move
aurel
parents:
4941
diff
changeset
|
162 sizeof(AACAC3ParseContext), |
| 4941 | 163 ac3_parse_init, |
|
4942
b42e963c8149
cosmetics: rename for consistency after previous aac and ac3 parsers move
aurel
parents:
4941
diff
changeset
|
164 ff_aac_ac3_parse, |
| 4941 | 165 NULL, |
| 166 }; |
