Based on a patch by nyanmisaka.
Showing the changes to libavcodec (6 of 7 files). Show all
Unified · Split
| @@ -73,6 +73,217 @@ int ff_ac3_find_syncword(const uint8_t *buf, int buf_size) · 72 unmodified lines | |||
| 73 | return i; | 73 | return i; |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | /** | ||
| 77 | * Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream. | ||
| 78 | * GetBitContext within AC3DecodeContext must point to | ||
| 79 | * the start of the synchronized AC-3 bitstream. | ||
| 80 | */ | ||
| 81 | static int ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr) | ||
| 82 | { | ||
| 83 | /* read the rest of the bsi. read twice for dual mono mode. */ | ||
| 84 | for (int i = 0; i < (hdr->channel_mode ? 1 : 2); i++) { | ||
| 85 | hdr->dialog_normalization[i] = -get_bits(gbc, 5); | ||
| 86 | hdr->compression_exists[i] = get_bits1(gbc); | ||
| 87 | if (hdr->compression_exists[i]) | ||
| 88 | hdr->heavy_dynamic_range[i] = get_bits(gbc, 8); | ||
| 89 | if (get_bits1(gbc)) | ||
| 90 | skip_bits(gbc, 8); //skip language code | ||
| 91 | if (get_bits1(gbc)) | ||
| 92 | skip_bits(gbc, 7); //skip audio production information | ||
| 93 | } | ||
| 94 | |||
| 95 | skip_bits(gbc, 2); //skip copyright bit and original bitstream bit | ||
| 96 | |||
| 97 | /* skip the timecodes or parse the Alternate Bit Stream Syntax */ | ||
| 98 | if (hdr->bitstream_id != 6) { | ||
| 99 | if (get_bits1(gbc)) | ||
| 100 | skip_bits(gbc, 14); //skip timecode1 | ||
| 101 | if (get_bits1(gbc)) | ||
| 102 | skip_bits(gbc, 14); //skip timecode2 | ||
| 103 | } else { | ||
| 104 | if (get_bits1(gbc)) { | ||
| 105 | hdr->preferred_downmix = get_bits(gbc, 2); | ||
| 106 | hdr->center_mix_level_ltrt = get_bits(gbc, 3); | ||
| 107 | hdr->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 108 | hdr->center_mix_level = get_bits(gbc, 3); | ||
| 109 | hdr->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 110 | } | ||
| 111 | if (get_bits1(gbc)) { | ||
| 112 | hdr->dolby_surround_ex_mode = get_bits(gbc, 2); | ||
| 113 | hdr->dolby_headphone_mode = get_bits(gbc, 2); | ||
| 114 | skip_bits(gbc, 10); // skip adconvtyp (1), xbsi2 (8), encinfo (1) | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | /* skip additional bitstream info */ | ||
| 119 | if (get_bits1(gbc)) { | ||
| 120 | int i = get_bits(gbc, 6); | ||
| 121 | do { | ||
| 122 | skip_bits(gbc, 8); | ||
| 123 | } while (i--); | ||
| 124 | } | ||
| 125 | |||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | static int eac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr) | ||
| 130 | { | ||
| 131 | if (hdr->frame_type == EAC3_FRAME_TYPE_RESERVED) | ||
| 132 | return AC3_PARSE_ERROR_FRAME_TYPE; | ||
| 133 | if (hdr->substreamid) | ||
| 134 | return AC3_PARSE_ERROR_FRAME_TYPE; | ||
| 135 | |||
| 136 | skip_bits(gbc, 5); // skip bitstream id | ||
| 137 | |||
| 138 | /* volume control params */ | ||
| 139 | for (int i = 0; i < (hdr->channel_mode ? 1 : 2); i++) { | ||
| 140 | hdr->dialog_normalization[i] = -get_bits(gbc, 5); | ||
| 141 | hdr->compression_exists[i] = get_bits1(gbc); | ||
| 142 | if (hdr->compression_exists[i]) | ||
| 143 | hdr->heavy_dynamic_range[i] = get_bits(gbc, 8); | ||
| 144 | } | ||
| 145 | |||
| 146 | /* dependent stream channel map */ | ||
| 147 | if (hdr->frame_type == EAC3_FRAME_TYPE_DEPENDENT) { | ||
| 148 | hdr->channel_map_present = get_bits1(gbc); | ||
| 149 | if (hdr->channel_map_present) { | ||
| 150 | int64_t channel_layout = 0; | ||
| 151 | int channel_map = get_bits(gbc, 16); | ||
| 152 | |||
| 153 | for (int i = 0; i < 16; i++) | ||
| 154 | if (channel_map & (1 << (EAC3_MAX_CHANNELS - i - 1))) | ||
| 155 | channel_layout |= ff_eac3_custom_channel_map_locations[i][1]; | ||
| 156 | |||
| 157 | if (av_popcount64(channel_layout) > EAC3_MAX_CHANNELS) { | ||
| 158 | return AC3_PARSE_ERROR_CHANNEL_MAP; | ||
| 159 | } | ||
| 160 | hdr->channel_map = channel_map; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | /* mixing metadata */ | ||
| 165 | if (get_bits1(gbc)) { | ||
| 166 | /* center and surround mix levels */ | ||
| 167 | if (hdr->channel_mode > AC3_CHMODE_STEREO) { | ||
| 168 | hdr->preferred_downmix = get_bits(gbc, 2); | ||
| 169 | if (hdr->channel_mode & 1) { | ||
| 170 | /* if three front channels exist */ | ||
| 171 | hdr->center_mix_level_ltrt = get_bits(gbc, 3); | ||
| 172 | hdr->center_mix_level = get_bits(gbc, 3); | ||
| 173 | } | ||
| 174 | if (hdr->channel_mode & 4) { | ||
| 175 | /* if a surround channel exists */ | ||
| 176 | hdr->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 177 | hdr->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | /* lfe mix level */ | ||
| 182 | if (hdr->lfe_on && (hdr->lfe_mix_level_exists = get_bits1(gbc))) { | ||
| 183 | hdr->lfe_mix_level = get_bits(gbc, 5); | ||
| 184 | } | ||
| 185 | |||
| 186 | /* info for mixing with other streams and substreams */ | ||
| 187 | if (hdr->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) { | ||
| 188 | for (int i = 0; i < (hdr->channel_mode ? 1 : 2); i++) { | ||
| 189 | // TODO: apply program scale factor | ||
| 190 | if (get_bits1(gbc)) { | ||
| 191 | skip_bits(gbc, 6); // skip program scale factor | ||
| 192 | } | ||
| 193 | } | ||
| 194 | if (get_bits1(gbc)) { | ||
| 195 | skip_bits(gbc, 6); // skip external program scale factor | ||
| 196 | } | ||
| 197 | /* skip mixing parameter data */ | ||
| 198 | switch(get_bits(gbc, 2)) { | ||
| 199 | case 1: skip_bits(gbc, 5); break; | ||
| 200 | case 2: skip_bits(gbc, 12); break; | ||
| 201 | case 3: { | ||
| 202 | int mix_data_size = (get_bits(gbc, 5) + 2) << 3; | ||
| 203 | skip_bits_long(gbc, mix_data_size); | ||
| 204 | break; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | /* skip pan information for mono or dual mono source */ | ||
| 208 | if (hdr->channel_mode < AC3_CHMODE_STEREO) { | ||
| 209 | for (int i = 0; i < (hdr->channel_mode ? 1 : 2); i++) { | ||
| 210 | if (get_bits1(gbc)) { | ||
| 211 | /* note: this is not in the ATSC A/52B specification | ||
| 212 | reference: ETSI TS 102 366 V1.1.1 | ||
| 213 | section: E.1.3.1.25 */ | ||
| 214 | skip_bits(gbc, 8); // skip pan mean direction index | ||
| 215 | skip_bits(gbc, 6); // skip reserved paninfo bits | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | /* skip mixing configuration information */ | ||
| 220 | if (get_bits1(gbc)) { | ||
| 221 | for (int i = 0; i < hdr->num_blocks; i++) { | ||
| 222 | if (hdr->num_blocks == 1 || get_bits1(gbc)) { | ||
| 223 | skip_bits(gbc, 5); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | /* informational metadata */ | ||
| 231 | if (get_bits1(gbc)) { | ||
| 232 | hdr->bitstream_mode = get_bits(gbc, 3); | ||
| 233 | skip_bits(gbc, 2); // skip copyright bit and original bitstream bit | ||
| 234 | if (hdr->channel_mode == AC3_CHMODE_STEREO) { | ||
| 235 | hdr->dolby_surround_mode = get_bits(gbc, 2); | ||
| 236 | hdr->dolby_headphone_mode = get_bits(gbc, 2); | ||
| 237 | } | ||
| 238 | if (hdr->channel_mode >= AC3_CHMODE_2F2R) { | ||
| 239 | hdr->dolby_surround_ex_mode = get_bits(gbc, 2); | ||
| 240 | } | ||
| 241 | for (int i = 0; i < (hdr->channel_mode ? 1 : 2); i++) { | ||
| 242 | if (get_bits1(gbc)) { | ||
| 243 | skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type | ||
| 244 | } | ||
| 245 | } | ||
| 246 | if (hdr->sr_code != EAC3_SR_CODE_REDUCED) { | ||
| 247 | skip_bits1(gbc); // skip source sample rate code | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | /* converter synchronization flag | ||
| 252 | If frames are less than six blocks, this bit should be turned on | ||
| 253 | once every 6 blocks to indicate the start of a frame set. | ||
| 254 | reference: RFC 4598, Section 2.1.3 Frame Sets */ | ||
| 255 | if (hdr->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && hdr->num_blocks != 6) { | ||
| 256 | skip_bits1(gbc); // skip converter synchronization flag | ||
| 257 | } | ||
| 258 | |||
| 259 | /* original frame size code if this stream was converted from AC-3 */ | ||
| 260 | if (hdr->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT && | ||
| 261 | (hdr->num_blocks == 6 || get_bits1(gbc))) { | ||
| 262 | skip_bits(gbc, 6); // skip frame size code | ||
| 263 | } | ||
| 264 | |||
| 265 | /* additional bitstream info */ | ||
| 266 | if (get_bits1(gbc)) { | ||
| 267 | int addbsil = get_bits(gbc, 6); | ||
| 268 | for (int i = 0; i < addbsil + 1; i++) { | ||
| 269 | if (i == 0) { | ||
| 270 | /* In this 8 bit chunk, the LSB is equal to flag_ec3_extension_type_a | ||
| 271 | which can be used to detect Atmos presence */ | ||
| 272 | skip_bits(gbc, 7); | ||
| 273 | hdr->eac3_extension_type_a = get_bits1(gbc); | ||
| 274 | if (hdr->eac3_extension_type_a) { | ||
| 275 | hdr->complexity_index_type_a = get_bits(gbc, 8); | ||
| 276 | i++; | ||
| 277 | } | ||
| 278 | } else { | ||
| 279 | skip_bits(gbc, 8); // skip additional bit stream info | ||
| 280 | } | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | return 0; | ||
| 285 | } | ||
| 286 | |||
| 76 | int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr) | 287 | int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr) |
| 77 | { | 288 | { |
| 78 | int frame_size_code; | 289 | int frame_size_code; |
| @@ -133,6 +344,10 @@ int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr) · 54 unmodified lines | |||
| 133 | hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2; | 344 | hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2; |
| 134 | hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT; | 345 | hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT; |
| 135 | hdr->substreamid = 0; | 346 | hdr->substreamid = 0; |
| 347 | |||
| 348 | int ret = ac3_parse_header(gbc, hdr); | ||
| 349 | if (ret < 0) | ||
| 350 | return ret; | ||
| 136 | } else { | 351 | } else { |
| 137 | /* Enhanced AC-3 */ | 352 | /* Enhanced AC-3 */ |
| 138 | hdr->crc1 = 0; | 353 | hdr->crc1 = 0; |
| @@ -165,6 +380,10 @@ int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr) · 26 unmodified lines | |||
| 165 | hdr->bit_rate = 8LL * hdr->frame_size * hdr->sample_rate / | 380 | hdr->bit_rate = 8LL * hdr->frame_size * hdr->sample_rate / |
| 166 | (hdr->num_blocks * 256); | 381 | (hdr->num_blocks * 256); |
| 167 | hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on; | 382 | hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on; |
| 383 | |||
| 384 | int ret = eac3_parse_header(gbc, hdr); | ||
| 385 | if (ret < 0) | ||
| 386 | return ret; | ||
| 168 | } | 387 | } |
| 169 | hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode]; | 388 | hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode]; |
| 170 | if (hdr->lfe_on) | 389 | if (hdr->lfe_on) |
| @@ -46,6 +46,7 @@ typedef struct AC3HeaderInfo { · 45 unmodified lines | |||
| 46 | int substreamid; ///< substream identification | 46 | int substreamid; ///< substream identification |
| 47 | int center_mix_level; ///< Center mix level index | 47 | int center_mix_level; ///< Center mix level index |
| 48 | int surround_mix_level; ///< Surround mix level index | 48 | int surround_mix_level; ///< Surround mix level index |
| 49 | uint8_t channel_map_present; | ||
| 49 | uint16_t channel_map; | 50 | uint16_t channel_map; |
| 50 | int num_blocks; ///< number of audio blocks | 51 | int num_blocks; ///< number of audio blocks |
| 51 | int dolby_surround_mode; | 52 | int dolby_surround_mode; |
| @@ -62,6 +63,23 @@ typedef struct AC3HeaderInfo { · 10 unmodified lines | |||
| 62 | uint64_t channel_layout; | 63 | uint64_t channel_layout; |
| 63 | int8_t ac3_bit_rate_code; | 64 | int8_t ac3_bit_rate_code; |
| 64 | /** @} */ | 65 | /** @} */ |
| 66 | |||
| 67 | /** @name enhanced eac3 extension coded elements | ||
| 68 | * @{ | ||
| 69 | */ | ||
| 70 | int8_t dialog_normalization[2]; | ||
| 71 | uint8_t compression_exists[2]; | ||
| 72 | uint8_t heavy_dynamic_range[2]; | ||
| 73 | uint8_t center_mix_level_ltrt; ///< Center mix level index | ||
| 74 | uint8_t surround_mix_level_ltrt; ///< Surround mix level index | ||
| 75 | uint8_t dolby_headphone_mode; | ||
| 76 | uint8_t dolby_surround_ex_mode; | ||
| 77 | uint8_t lfe_mix_level_exists; | ||
| 78 | uint8_t lfe_mix_level; | ||
| 79 | uint8_t preferred_downmix; | ||
| 80 | uint8_t eac3_extension_type_a; | ||
| 81 | uint8_t complexity_index_type_a; | ||
| 82 | /** @} */ | ||
| 65 | } AC3HeaderInfo; | 83 | } AC3HeaderInfo; |
| 66 | 84 | ||
| 67 | typedef enum { | 85 | typedef enum { |
| @@ -71,6 +89,7 @@ typedef enum { · 3 unmodified lines | |||
| 71 | AC3_PARSE_ERROR_FRAME_SIZE = -0x4030c0a, | 89 | AC3_PARSE_ERROR_FRAME_SIZE = -0x4030c0a, |
| 72 | AC3_PARSE_ERROR_FRAME_TYPE = -0x5030c0a, | 90 | AC3_PARSE_ERROR_FRAME_TYPE = -0x5030c0a, |
| 73 | AC3_PARSE_ERROR_CRC = -0x6030c0a, | 91 | AC3_PARSE_ERROR_CRC = -0x6030c0a, |
| 92 | AC3_PARSE_ERROR_CHANNEL_MAP = -0x7030c0a, | ||
| 74 | } AC3ParseError; | 93 | } AC3ParseError; |
| 75 | 94 | ||
| 76 | /** | 95 | /** |
| @@ -156,72 +156,6 @@ static av_cold void ac3_decode_flush(AVCodecContext *avctx) · 155 unmodified lines | |||
| 156 | av_lfg_init(&s->dith_state, 0); | 156 | av_lfg_init(&s->dith_state, 0); |
| 157 | } | 157 | } |
| 158 | 158 | ||
| 159 | /** | ||
| 160 | * Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream. | ||
| 161 | * GetBitContext within AC3DecodeContext must point to | ||
| 162 | * the start of the synchronized AC-3 bitstream. | ||
| 163 | */ | ||
| 164 | static int ac3_parse_header(AC3DecodeContext *s) | ||
| 165 | { | ||
| 166 | GetBitContext *gbc = &s->gbc; | ||
| 167 | int i; | ||
| 168 | |||
| 169 | /* read the rest of the bsi. read twice for dual mono mode. */ | ||
| 170 | i = !s->channel_mode; | ||
| 171 | do { | ||
| 172 | s->dialog_normalization[(!s->channel_mode)-i] = -get_bits(gbc, 5); | ||
| 173 | if (s->dialog_normalization[(!s->channel_mode)-i] == 0) { | ||
| 174 | s->dialog_normalization[(!s->channel_mode)-i] = -31; | ||
| 175 | } | ||
| 176 | if (s->target_level != 0) { | ||
| 177 | s->level_gain[(!s->channel_mode)-i] = powf(2.0f, | ||
| 178 | (float)(s->target_level - | ||
| 179 | s->dialog_normalization[(!s->channel_mode)-i])/6.0f); | ||
| 180 | } | ||
| 181 | if (s->compression_exists[(!s->channel_mode)-i] = get_bits1(gbc)) { | ||
| 182 | s->heavy_dynamic_range[(!s->channel_mode)-i] = | ||
| 183 | AC3_HEAVY_RANGE(get_bits(gbc, 8)); | ||
| 184 | } | ||
| 185 | if (get_bits1(gbc)) | ||
| 186 | skip_bits(gbc, 8); //skip language code | ||
| 187 | if (get_bits1(gbc)) | ||
| 188 | skip_bits(gbc, 7); //skip audio production information | ||
| 189 | } while (i--); | ||
| 190 | |||
| 191 | skip_bits(gbc, 2); //skip copyright bit and original bitstream bit | ||
| 192 | |||
| 193 | /* skip the timecodes or parse the Alternate Bit Stream Syntax */ | ||
| 194 | if (s->bitstream_id != 6) { | ||
| 195 | if (get_bits1(gbc)) | ||
| 196 | skip_bits(gbc, 14); //skip timecode1 | ||
| 197 | if (get_bits1(gbc)) | ||
| 198 | skip_bits(gbc, 14); //skip timecode2 | ||
| 199 | } else { | ||
| 200 | if (get_bits1(gbc)) { | ||
| 201 | s->preferred_downmix = get_bits(gbc, 2); | ||
| 202 | s->center_mix_level_ltrt = get_bits(gbc, 3); | ||
| 203 | s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 204 | s->center_mix_level = get_bits(gbc, 3); | ||
| 205 | s->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 206 | } | ||
| 207 | if (get_bits1(gbc)) { | ||
| 208 | s->dolby_surround_ex_mode = get_bits(gbc, 2); | ||
| 209 | s->dolby_headphone_mode = get_bits(gbc, 2); | ||
| 210 | skip_bits(gbc, 10); // skip adconvtyp (1), xbsi2 (8), encinfo (1) | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | /* skip additional bitstream info */ | ||
| 215 | if (get_bits1(gbc)) { | ||
| 216 | i = get_bits(gbc, 6); | ||
| 217 | do { | ||
| 218 | skip_bits(gbc, 8); | ||
| 219 | } while (i--); | ||
| 220 | } | ||
| 221 | |||
| 222 | return 0; | ||
| 223 | } | ||
| 224 | |||
| 225 | /** | 159 | /** |
| 226 | * Common function to parse AC-3 or E-AC-3 frame header | 160 | * Common function to parse AC-3 or E-AC-3 frame header |
| 227 | */ | 161 | */ |
| @@ -281,10 +215,25 @@ static int parse_frame_header(AC3DecodeContext *s) · 53 unmodified lines | |||
| 281 | s->dba_syntax = 1; | 215 | s->dba_syntax = 1; |
| 282 | s->skip_syntax = 1; | 216 | s->skip_syntax = 1; |
| 283 | memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht)); | 217 | memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht)); |
| 284 | return ac3_parse_header(s); | 218 | /* volume control params */ |
| 219 | for (int i = 0; i < (s->channel_mode ? 1 : 2); i++) { | ||
| 220 | s->dialog_normalization[i] = hdr.dialog_normalization[i]; | ||
| 221 | if (s->dialog_normalization[i] == 0) { | ||
| 222 | s->dialog_normalization[i] = -31; | ||
| 223 | } | ||
| 224 | if (s->target_level != 0) { | ||
| 225 | s->level_gain[i] = powf(2.0f, | ||
| 226 | (float)(s->target_level - s->dialog_normalization[i])/6.0f); | ||
| 227 | } | ||
| 228 | s->compression_exists[i] = hdr.compression_exists[i]; | ||
| 229 | if (s->compression_exists[i]) { | ||
| 230 | s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(hdr.heavy_dynamic_range[i]); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | return 0; | ||
| 285 | } else if (CONFIG_EAC3_DECODER) { | 234 | } else if (CONFIG_EAC3_DECODER) { |
| 286 | s->eac3 = 1; | 235 | s->eac3 = 1; |
| 287 | return ff_eac3_parse_header(s); | 236 | return ff_eac3_parse_header(s, &hdr); |
| 288 | } else { | 237 | } else { |
| 289 | av_log(s->avctx, AV_LOG_ERROR, "E-AC-3 support not compiled in\n"); | 238 | av_log(s->avctx, AV_LOG_ERROR, "E-AC-3 support not compiled in\n"); |
| 290 | return AVERROR(ENOSYS); | 239 | return AVERROR(ENOSYS); |
| @@ -1469,6 +1418,9 @@ dependent_frame: · 1178 unmodified lines | |||
| 1469 | av_log(avctx, AV_LOG_ERROR, "invalid frame type\n"); | 1418 | av_log(avctx, AV_LOG_ERROR, "invalid frame type\n"); |
| 1470 | } | 1419 | } |
| 1471 | break; | 1420 | break; |
| 1421 | case AC3_PARSE_ERROR_CHANNEL_MAP: | ||
| 1422 | av_log(avctx, AV_LOG_ERROR, "invalid channel map\n"); | ||
| 1423 | return AVERROR_INVALIDDATA; | ||
| 1472 | case AC3_PARSE_ERROR_CRC: | 1424 | case AC3_PARSE_ERROR_CRC: |
| 1473 | break; | 1425 | break; |
| 1474 | default: // Normal AVERROR do not try to recover. | 1426 | default: // Normal AVERROR do not try to recover. |
| @@ -260,11 +260,13 @@ typedef struct AC3DecodeContext { · 259 unmodified lines | |||
| 260 | ///@} | 260 | ///@} |
| 261 | } AC3DecodeContext; | 261 | } AC3DecodeContext; |
| 262 | 262 | ||
| 263 | struct AC3HeaderInfo; | ||
| 264 | |||
| 263 | /** | 265 | /** |
| 264 | * Parse the E-AC-3 frame header. | 266 | * Parse the E-AC-3 frame header. |
| 265 | * This parses both the bit stream info and audio frame header. | 267 | * This parses both the bit stream info and audio frame header. |
| 266 | */ | 268 | */ |
| 267 | static int ff_eac3_parse_header(AC3DecodeContext *s); | 269 | static int ff_eac3_parse_header(AC3DecodeContext *s, const struct AC3HeaderInfo *hdr); |
| 268 | 270 | ||
| 269 | /** | 271 | /** |
| 270 | * Decode mantissas in a single channel for the entire frame. | 272 | * Decode mantissas in a single channel for the entire frame. |
| @@ -34,6 +34,8 @@ · 33 unmodified lines | |||
| 34 | #define AC3_CRITICAL_BANDS 50 | 34 | #define AC3_CRITICAL_BANDS 50 |
| 35 | #define AC3_MAX_CPL_BANDS 18 | 35 | #define AC3_MAX_CPL_BANDS 18 |
| 36 | 36 | ||
| 37 | #define EAC3_SR_CODE_REDUCED 3 | ||
| 38 | |||
| 37 | /* pre-defined gain values */ | 39 | /* pre-defined gain values */ |
| 38 | #define LEVEL_PLUS_3DB M_SQRT2 | 40 | #define LEVEL_PLUS_3DB M_SQRT2 |
| 39 | #define LEVEL_PLUS_1POINT5DB 1.1892071150027209 | 41 | #define LEVEL_PLUS_1POINT5DB 1.1892071150027209 |
| @@ -53,8 +53,6 @@ typedef enum { · 52 unmodified lines | |||
| 53 | EAC3_GAQ_124 | 53 | EAC3_GAQ_124 |
| 54 | } EAC3GaqMode; | 54 | } EAC3GaqMode; |
| 55 | 55 | ||
| 56 | #define EAC3_SR_CODE_REDUCED 3 | ||
| 57 | |||
| 58 | static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s) | 56 | static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s) |
| 59 | { | 57 | { |
| 60 | int bin, bnd, ch, i; | 58 | int bin, bnd, ch, i; |
| @@ -287,7 +285,7 @@ static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch) · 226 unmodified lines | |||
| 287 | } | 285 | } |
| 288 | } | 286 | } |
| 289 | 287 | ||
| 290 | static int ff_eac3_parse_header(AC3DecodeContext *s) | 288 | static int ff_eac3_parse_header(AC3DecodeContext *s, const AC3HeaderInfo *hdr) |
| 291 | { | 289 | { |
| 292 | int i, blk, ch; | 290 | int i, blk, ch; |
| 293 | int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data; | 291 | int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data; |
| @@ -323,11 +321,10 @@ static int ff_eac3_parse_header(AC3DecodeContext *s) · 29 unmodified lines | |||
| 323 | avpriv_request_sample(s->avctx, "Reduced sampling rate"); | 321 | avpriv_request_sample(s->avctx, "Reduced sampling rate"); |
| 324 | return AVERROR_PATCHWELCOME; | 322 | return AVERROR_PATCHWELCOME; |
| 325 | } | 323 | } |
| 326 | skip_bits(gbc, 5); // skip bitstream id | ||
| 327 | 324 | ||
| 328 | /* volume control params */ | 325 | /* volume control params */ |
| 329 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { | 326 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { |
| 330 | s->dialog_normalization[i] = -get_bits(gbc, 5); | 327 | s->dialog_normalization[i] = hdr->dialog_normalization[i]; |
| 331 | if (s->dialog_normalization[i] == 0) { | 328 | if (s->dialog_normalization[i] == 0) { |
| 332 | s->dialog_normalization[i] = -31; | 329 | s->dialog_normalization[i] = -31; |
| 333 | } | 330 | } |
| @@ -335,147 +332,30 @@ static int ff_eac3_parse_header(AC3DecodeContext *s) · 1 unmodified line | |||
| 335 | s->level_gain[i] = powf(2.0f, | 332 | s->level_gain[i] = powf(2.0f, |
| 336 | (float)(s->target_level - s->dialog_normalization[i])/6.0f); | 333 | (float)(s->target_level - s->dialog_normalization[i])/6.0f); |
| 337 | } | 334 | } |
| 338 | s->compression_exists[i] = get_bits1(gbc); | 335 | if (hdr->compression_exists[i]) { |
| 339 | if (s->compression_exists[i]) { | 336 | s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(hdr->heavy_dynamic_range[i]); |
| 340 | s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(get_bits(gbc, 8)); | ||
| 341 | } | 337 | } |
| 342 | } | 338 | } |
| 343 | 339 | ||
| 344 | /* dependent stream channel map */ | 340 | s->channel_map = hdr->channel_map; |
| 345 | if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) { | ||
| 346 | if (get_bits1(gbc)) { | ||
| 347 | int64_t channel_layout = 0; | ||
| 348 | int channel_map = get_bits(gbc, 16); | ||
| 349 | av_log(s->avctx, AV_LOG_DEBUG, "channel_map: %0X\n", channel_map); | ||
| 350 | |||
| 351 | for (i = 0; i < 16; i++) | ||
| 352 | if (channel_map & (1 << (EAC3_MAX_CHANNELS - i - 1))) | ||
| 353 | channel_layout |= ff_eac3_custom_channel_map_locations[i][1]; | ||
| 354 | |||
| 355 | if (av_popcount64(channel_layout) > EAC3_MAX_CHANNELS) { | ||
| 356 | return AVERROR_INVALIDDATA; | ||
| 357 | } | ||
| 358 | s->channel_map = channel_map; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | 341 | ||
| 362 | /* mixing metadata */ | 342 | /* mixing metadata */ |
| 363 | if (get_bits1(gbc)) { | 343 | s->preferred_downmix = hdr->preferred_downmix; |
| 364 | /* center and surround mix levels */ | 344 | s->center_mix_level_ltrt = hdr->center_mix_level_ltrt; |
| 365 | if (s->channel_mode > AC3_CHMODE_STEREO) { | 345 | s->center_mix_level = hdr->center_mix_level; |
| 366 | s->preferred_downmix = get_bits(gbc, 2); | 346 | s->surround_mix_level_ltrt = hdr->surround_mix_level_ltrt; |
| 367 | if (s->channel_mode & 1) { | 347 | s->surround_mix_level = hdr->surround_mix_level; |
| 368 | /* if three front channels exist */ | 348 | s->lfe_mix_level_exists = hdr->lfe_mix_level_exists; |
| 369 | s->center_mix_level_ltrt = get_bits(gbc, 3); | 349 | s->lfe_mix_level = hdr->lfe_mix_level; |
| 370 | s->center_mix_level = get_bits(gbc, 3); | 350 | s->dolby_surround_mode = hdr->dolby_surround_mode; |
| 371 | } | 351 | s->dolby_headphone_mode = hdr->dolby_headphone_mode; |
| 372 | if (s->channel_mode & 4) { | 352 | s->dolby_surround_ex_mode = hdr->dolby_surround_ex_mode; |
| 373 | /* if a surround channel exists */ | ||
| 374 | s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 375 | s->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | /* lfe mix level */ | ||
| 380 | if (s->lfe_on && (s->lfe_mix_level_exists = get_bits1(gbc))) { | ||
| 381 | s->lfe_mix_level = get_bits(gbc, 5); | ||
| 382 | } | ||
| 383 | |||
| 384 | /* info for mixing with other streams and substreams */ | ||
| 385 | if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) { | ||
| 386 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { | ||
| 387 | // TODO: apply program scale factor | ||
| 388 | if (get_bits1(gbc)) { | ||
| 389 | skip_bits(gbc, 6); // skip program scale factor | ||
| 390 | } | ||
| 391 | } | ||
| 392 | if (get_bits1(gbc)) { | ||
| 393 | skip_bits(gbc, 6); // skip external program scale factor | ||
| 394 | } | ||
| 395 | /* skip mixing parameter data */ | ||
| 396 | switch(get_bits(gbc, 2)) { | ||
| 397 | case 1: skip_bits(gbc, 5); break; | ||
| 398 | case 2: skip_bits(gbc, 12); break; | ||
| 399 | case 3: { | ||
| 400 | int mix_data_size = (get_bits(gbc, 5) + 2) << 3; | ||
| 401 | skip_bits_long(gbc, mix_data_size); | ||
| 402 | break; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | /* skip pan information for mono or dual mono source */ | ||
| 406 | if (s->channel_mode < AC3_CHMODE_STEREO) { | ||
| 407 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { | ||
| 408 | if (get_bits1(gbc)) { | ||
| 409 | /* note: this is not in the ATSC A/52B specification | ||
| 410 | reference: ETSI TS 102 366 V1.1.1 | ||
| 411 | section: E.1.3.1.25 */ | ||
| 412 | skip_bits(gbc, 8); // skip pan mean direction index | ||
| 413 | skip_bits(gbc, 6); // skip reserved paninfo bits | ||
| 414 | } | ||
| 415 | } | ||
| 416 | } | ||
| 417 | /* skip mixing configuration information */ | ||
| 418 | if (get_bits1(gbc)) { | ||
| 419 | for (blk = 0; blk < s->num_blocks; blk++) { | ||
| 420 | if (s->num_blocks == 1 || get_bits1(gbc)) { | ||
| 421 | skip_bits(gbc, 5); | ||
| 422 | } | ||
| 423 | } | ||
| 424 | } | ||
| 425 | } | ||
| 426 | } | ||
| 427 | 353 | ||
| 428 | /* informational metadata */ | 354 | /* informational metadata */ |
| 429 | if (get_bits1(gbc)) { | 355 | s->bitstream_mode = hdr->bitstream_mode; |
| 430 | s->bitstream_mode = get_bits(gbc, 3); | ||
| 431 | skip_bits(gbc, 2); // skip copyright bit and original bitstream bit | ||
| 432 | if (s->channel_mode == AC3_CHMODE_STEREO) { | ||
| 433 | s->dolby_surround_mode = get_bits(gbc, 2); | ||
| 434 | s->dolby_headphone_mode = get_bits(gbc, 2); | ||
| 435 | } | ||
| 436 | if (s->channel_mode >= AC3_CHMODE_2F2R) { | ||
| 437 | s->dolby_surround_ex_mode = get_bits(gbc, 2); | ||
| 438 | } | ||
| 439 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { | ||
| 440 | if (get_bits1(gbc)) { | ||
| 441 | skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type | ||
| 442 | } | ||
| 443 | } | ||
| 444 | if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) { | ||
| 445 | skip_bits1(gbc); // skip source sample rate code | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | /* converter synchronization flag | ||
| 450 | If frames are less than six blocks, this bit should be turned on | ||
| 451 | once every 6 blocks to indicate the start of a frame set. | ||
| 452 | reference: RFC 4598, Section 2.1.3 Frame Sets */ | ||
| 453 | if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) { | ||
| 454 | skip_bits1(gbc); // skip converter synchronization flag | ||
| 455 | } | ||
| 456 | |||
| 457 | /* original frame size code if this stream was converted from AC-3 */ | ||
| 458 | if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT && | ||
| 459 | (s->num_blocks == 6 || get_bits1(gbc))) { | ||
| 460 | skip_bits(gbc, 6); // skip frame size code | ||
| 461 | } | ||
| 462 | 356 | ||
| 463 | /* additional bitstream info */ | 357 | /* additional bitstream info */ |
| 464 | if (get_bits1(gbc)) { | 358 | s->eac3_extension_type_a = hdr->eac3_extension_type_a; |
| 465 | int addbsil = get_bits(gbc, 6); | ||
| 466 | for (i = 0; i < addbsil + 1; i++) { | ||
| 467 | if (i == 0) { | ||
| 468 | /* In this 8 bit chunk, the LSB is equal to flag_ec3_extension_type_a | ||
| 469 | which can be used to detect Atmos presence */ | ||
| 470 | skip_bits(gbc, 7); | ||
| 471 | if (get_bits1(gbc)) { | ||
| 472 | s->eac3_extension_type_a = 1; | ||
| 473 | } | ||
| 474 | } else { | ||
| 475 | skip_bits(gbc, 8); // skip additional bit stream info | ||
| 476 | } | ||
| 477 | } | ||
| 478 | } | ||
| 479 | 359 | ||
| 480 | /* audio frame syntax flags, strategy data, and per-frame data */ | 360 | /* audio frame syntax flags, strategy data, and per-frame data */ |
| 481 | 361 | ||