comparison ac3_parser.c @ 6661:a409fbf1f42b libavcodec

change ff_ac3_parse_header() to take a GetBitContext instead of const char*
author bwolowiec
date Tue, 22 Apr 2008 11:14:01 +0000
parents abc8176ddf88
children 2d0b86dfe5bb
comparison
equal deleted inserted replaced
6660:582712867474 6661:a409fbf1f42b
31 31
32 static const uint8_t eac3_blocks[4] = { 32 static const uint8_t eac3_blocks[4] = {
33 1, 2, 3, 6 33 1, 2, 3, 6
34 }; 34 };
35 35
36 /**
37 * Table for center mix levels
38 * reference: Section 5.4.2.4 cmixlev
39 */
40 static const uint8_t center_levels[4] = { 2, 3, 4, 3 };
36 41
37 int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr) 42 /**
43 * Table for surround mix levels
44 * reference: Section 5.4.2.5 surmixlev
45 */
46 static const uint8_t surround_levels[4] = { 2, 4, 0, 4 };
47
48
49 int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
38 { 50 {
39 GetBitContext gbc;
40 int frame_size_code; 51 int frame_size_code;
41 int num_blocks; 52 int num_blocks;
42 53
43 memset(hdr, 0, sizeof(*hdr)); 54 memset(hdr, 0, sizeof(*hdr));
44 55
45 init_get_bits(&gbc, buf, 54); 56 hdr->sync_word = get_bits(gbc, 16);
46
47 hdr->sync_word = get_bits(&gbc, 16);
48 if(hdr->sync_word != 0x0B77) 57 if(hdr->sync_word != 0x0B77)
49 return AC3_PARSE_ERROR_SYNC; 58 return AC3_PARSE_ERROR_SYNC;
50 59
51 /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */ 60 /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
52 hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F; 61 hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
53 if(hdr->bitstream_id > 16) 62 if(hdr->bitstream_id > 16)
54 return AC3_PARSE_ERROR_BSID; 63 return AC3_PARSE_ERROR_BSID;
55 64
56 if(hdr->bitstream_id <= 10) { 65 if(hdr->bitstream_id <= 10) {
57 /* Normal AC-3 */ 66 /* Normal AC-3 */
58 hdr->crc1 = get_bits(&gbc, 16); 67 hdr->crc1 = get_bits(gbc, 16);
59 hdr->sr_code = get_bits(&gbc, 2); 68 hdr->sr_code = get_bits(gbc, 2);
60 if(hdr->sr_code == 3) 69 if(hdr->sr_code == 3)
61 return AC3_PARSE_ERROR_SAMPLE_RATE; 70 return AC3_PARSE_ERROR_SAMPLE_RATE;
62 71
63 frame_size_code = get_bits(&gbc, 6); 72 frame_size_code = get_bits(gbc, 6);
64 if(frame_size_code > 37) 73 if(frame_size_code > 37)
65 return AC3_PARSE_ERROR_FRAME_SIZE; 74 return AC3_PARSE_ERROR_FRAME_SIZE;
66 75
67 skip_bits(&gbc, 5); // skip bsid, already got it 76 skip_bits(gbc, 5); // skip bsid, already got it
68 77
69 skip_bits(&gbc, 3); // skip bitstream mode 78 skip_bits(gbc, 3); // skip bitstream mode
70 hdr->channel_mode = get_bits(&gbc, 3); 79 hdr->channel_mode = get_bits(gbc, 3);
71 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) { 80
72 skip_bits(&gbc, 2); // skip center mix level 81 /* set default mix levels */
82 hdr->center_mix_level = 3; // -4.5dB
83 hdr->surround_mix_level = 4; // -6.0dB
84
85 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
86 skip_bits(gbc, 2); // skip dsurmod
87 } else {
88 if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
89 hdr->center_mix_level = center_levels[get_bits(gbc, 2)];
90 if(hdr->channel_mode & 4)
91 hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
73 } 92 }
74 if(hdr->channel_mode & 4) { 93 hdr->lfe_on = get_bits1(gbc);
75 skip_bits(&gbc, 2); // skip surround mix level
76 }
77 if(hdr->channel_mode == AC3_CHMODE_STEREO) {
78 skip_bits(&gbc, 2); // skip dolby surround mode
79 }
80 hdr->lfe_on = get_bits1(&gbc);
81 94
82 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8; 95 hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
83 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift; 96 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
84 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift; 97 hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
85 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on; 98 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
86 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2; 99 hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
87 hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT; 100 hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
88 } else { 101 } else {
89 /* Enhanced AC-3 */ 102 /* Enhanced AC-3 */
90 hdr->crc1 = 0; 103 hdr->crc1 = 0;
91 hdr->frame_type = get_bits(&gbc, 2); 104 hdr->frame_type = get_bits(gbc, 2);
92 if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED) 105 if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
93 return AC3_PARSE_ERROR_FRAME_TYPE; 106 return AC3_PARSE_ERROR_FRAME_TYPE;
94 107
95 skip_bits(&gbc, 3); // skip substream id 108 skip_bits(gbc, 3); // skip substream id
96 109
97 hdr->frame_size = (get_bits(&gbc, 11) + 1) << 1; 110 hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
98 if(hdr->frame_size < AC3_HEADER_SIZE) 111 if(hdr->frame_size < AC3_HEADER_SIZE)
99 return AC3_PARSE_ERROR_FRAME_SIZE; 112 return AC3_PARSE_ERROR_FRAME_SIZE;
100 113
101 hdr->sr_code = get_bits(&gbc, 2); 114 hdr->sr_code = get_bits(gbc, 2);
102 if (hdr->sr_code == 3) { 115 if (hdr->sr_code == 3) {
103 int sr_code2 = get_bits(&gbc, 2); 116 int sr_code2 = get_bits(gbc, 2);
104 if(sr_code2 == 3) 117 if(sr_code2 == 3)
105 return AC3_PARSE_ERROR_SAMPLE_RATE; 118 return AC3_PARSE_ERROR_SAMPLE_RATE;
106 hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2; 119 hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
107 hdr->sr_shift = 1; 120 hdr->sr_shift = 1;
108 num_blocks = 6; 121 num_blocks = 6;
109 } else { 122 } else {
110 num_blocks = eac3_blocks[get_bits(&gbc, 2)]; 123 num_blocks = eac3_blocks[get_bits(gbc, 2)];
111 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code]; 124 hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
112 hdr->sr_shift = 0; 125 hdr->sr_shift = 0;
113 } 126 }
114 127
115 hdr->channel_mode = get_bits(&gbc, 3); 128 hdr->channel_mode = get_bits(gbc, 3);
116 hdr->lfe_on = get_bits1(&gbc); 129 hdr->lfe_on = get_bits1(gbc);
117 130
118 hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate / 131 hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
119 (num_blocks * 256.0)); 132 (num_blocks * 256.0));
120 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on; 133 hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
121 } 134 }
127 int *need_next_header, int *new_frame_start) 140 int *need_next_header, int *new_frame_start)
128 { 141 {
129 int err; 142 int err;
130 uint64_t tmp = be2me_64(state); 143 uint64_t tmp = be2me_64(state);
131 AC3HeaderInfo hdr; 144 AC3HeaderInfo hdr;
145 GetBitContext gbc;
132 146
133 err = ff_ac3_parse_header(((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, &hdr); 147 init_get_bits(&gbc, ((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, 54);
148 err = ff_ac3_parse_header(&gbc, &hdr);
134 149
135 if(err < 0) 150 if(err < 0)
136 return 0; 151 return 0;
137 152
138 hdr_info->sample_rate = hdr.sample_rate; 153 hdr_info->sample_rate = hdr.sample_rate;