ac3_parser.c
Go to the documentation of this file.
1 /*
2  * AC-3 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 
24 #include "parser.h"
25 #include "ac3_parser.h"
26 #include "aac_ac3_parser.h"
27 #include "get_bits.h"
28 
29 
30 #define AC3_HEADER_SIZE 7
31 
32 
33 static const uint8_t eac3_blocks[4] = {
34  1, 2, 3, 6
35 };
36 
37 /**
38  * Table for center mix levels
39  * reference: Section 5.4.2.4 cmixlev
40  */
41 static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
42 
43 /**
44  * Table for surround mix levels
45  * reference: Section 5.4.2.5 surmixlev
46  */
47 static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
48 
49 
51 {
52  int frame_size_code;
53 
54  memset(hdr, 0, sizeof(*hdr));
55 
56  hdr->sync_word = get_bits(gbc, 16);
57  if(hdr->sync_word != 0x0B77)
59 
60  /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
61  hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
62  if(hdr->bitstream_id > 16)
64 
65  hdr->num_blocks = 6;
66 
67  /* set default mix levels */
68  hdr->center_mix_level = 5; // -4.5dB
69  hdr->surround_mix_level = 6; // -6.0dB
70 
71  if(hdr->bitstream_id <= 10) {
72  /* Normal AC-3 */
73  hdr->crc1 = get_bits(gbc, 16);
74  hdr->sr_code = get_bits(gbc, 2);
75  if(hdr->sr_code == 3)
77 
78  frame_size_code = get_bits(gbc, 6);
79  if(frame_size_code > 37)
81 
82  skip_bits(gbc, 5); // skip bsid, already got it
83 
84  hdr->bitstream_mode = get_bits(gbc, 3);
85  hdr->channel_mode = get_bits(gbc, 3);
86 
87  if(hdr->channel_mode == AC3_CHMODE_STEREO) {
88  skip_bits(gbc, 2); // skip dsurmod
89  } else {
90  if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
91  hdr-> center_mix_level = center_levels[get_bits(gbc, 2)];
92  if(hdr->channel_mode & 4)
94  }
95  hdr->lfe_on = get_bits1(gbc);
96 
97  hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
99  hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
100  hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
101  hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
102  hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
103  hdr->substreamid = 0;
104  } else {
105  /* Enhanced AC-3 */
106  hdr->crc1 = 0;
107  hdr->frame_type = get_bits(gbc, 2);
110 
111  hdr->substreamid = get_bits(gbc, 3);
112 
113  hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
114  if(hdr->frame_size < AC3_HEADER_SIZE)
116 
117  hdr->sr_code = get_bits(gbc, 2);
118  if (hdr->sr_code == 3) {
119  int sr_code2 = get_bits(gbc, 2);
120  if(sr_code2 == 3)
122  hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
123  hdr->sr_shift = 1;
124  } else {
125  hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
127  hdr->sr_shift = 0;
128  }
129 
130  hdr->channel_mode = get_bits(gbc, 3);
131  hdr->lfe_on = get_bits1(gbc);
132 
133  hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
134  (hdr->num_blocks * 256.0));
135  hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
136  }
138  if (hdr->lfe_on)
140 
141  return 0;
142 }
143 
144 static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
145  int *need_next_header, int *new_frame_start)
146 {
147  int err;
148  union {
149  uint64_t u64;
150  uint8_t u8[8];
151  } tmp = { av_be2ne64(state) };
152  AC3HeaderInfo hdr;
153  GetBitContext gbc;
154 
155  init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
156  err = avpriv_ac3_parse_header(&gbc, &hdr);
157 
158  if(err < 0)
159  return 0;
160 
161  hdr_info->sample_rate = hdr.sample_rate;
162  hdr_info->bit_rate = hdr.bit_rate;
163  hdr_info->channels = hdr.channels;
164  hdr_info->channel_layout = hdr.channel_layout;
165  hdr_info->samples = hdr.num_blocks * 256;
166  hdr_info->service_type = hdr.bitstream_mode;
167  if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
169  if(hdr.bitstream_id>10)
170  hdr_info->codec_id = AV_CODEC_ID_EAC3;
171  else if (hdr_info->codec_id == AV_CODEC_ID_NONE)
172  hdr_info->codec_id = AV_CODEC_ID_AC3;
173 
174  *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
175  *new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
176  return hdr.frame_size;
177 }
178 
180 {
183  s->sync = ac3_sync;
184  return 0;
185 }
186 
187 
190  .priv_data_size = sizeof(AACAC3ParseContext),
191  .parser_init = ac3_parse_init,
192  .parser_parse = ff_aac_ac3_parse,
193  .parser_close = ff_parse_close,
194 };
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:352
uint8_t bitstream_mode
Definition: ac3.h:104
uint64_t channel_layout
Definition: ac3.h:123
const char * s
Definition: avisynth_c.h:668
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
const uint16_t ff_ac3_frame_size_tab[38][3]
Possible frame sizes.
Definition: ac3tab.c:35
uint16_t crc1
Definition: ac3.h:101
static const uint8_t eac3_blocks[4]
Definition: ac3_parser.c:33
const uint16_t ff_ac3_sample_rate_tab[3]
Definition: ac3tab.c:127
uint8_t
#define av_cold
Definition: attributes.h:78
uint8_t lfe_on
Definition: ac3.h:106
static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info, int *need_next_header, int *new_frame_start)
Definition: ac3_parser.c:144
#define AV_CH_LOW_FREQUENCY
uint8_t sr_shift
Definition: ac3.h:118
bitstream reader API header.
uint8_t bitstream_id
Definition: ac3.h:103
struct AACAC3ParseContext AACAC3ParseContext
const uint16_t avpriv_ac3_channel_layout_tab[8]
Map audio coding mode (acmod) to channel layout mask.
Definition: ac3tab.c:87
uint16_t sync_word
Definition: ac3.h:100
static const uint8_t center_levels[4]
Table for center mix levels reference: Section 5.4.2.4 cmixlev.
Definition: ac3_parser.c:41
uint8_t frame_type
Definition: ac3.h:107
uint8_t sr_code
Definition: ac3.h:102
Coded AC-3 header values up to the lfeon element, plus derived values.
Definition: ac3.h:96
uint16_t sample_rate
Definition: ac3.h:119
#define av_be2ne64(x)
Definition: bswap.h:94
#define FFMAX(a, b)
Definition: common.h:56
const uint16_t ff_ac3_bitrate_tab[19]
Definition: ac3tab.c:130
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:279
audio channel layout utility functions
uint32_t bit_rate
Definition: ac3.h:120
int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
Parse AC-3 frame header.
Definition: ac3_parser.c:50
int(* sync)(uint64_t state, struct AACAC3ParseContext *hdr_info, int *need_next_header, int *new_frame_start)
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
uint16_t frame_size
Definition: ac3.h:122
enum AVCodecID codec_id
AVCodecParser ff_ac3_parser
Definition: ac3_parser.c:188
static av_cold int ac3_parse_init(AVCodecParserContext *s1)
Definition: ac3_parser.c:179
#define s1
Definition: regdef.h:38
static const uint8_t surround_levels[4]
Table for surround mix levels reference: Section 5.4.2.5 surmixlev.
Definition: ac3_parser.c:47
static uint32_t state
Definition: trasher.c:27
int ff_aac_ac3_parse(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
const uint8_t ff_ac3_channels_tab[8]
Map audio coding mode (acmod) to number of full-bandwidth channels.
Definition: ac3tab.c:80
int surround_mix_level
Surround mix level index.
Definition: ac3.h:110
int center_mix_level
Center mix level index.
Definition: ac3.h:109
int substreamid
substream identification
Definition: ac3.h:108
uint8_t channels
Definition: ac3.h:121
#define AC3_HEADER_SIZE
Definition: ac3_parser.c:30
int num_blocks
number of audio blocks
Definition: ac3.h:112
uint8_t channel_mode
Definition: ac3.h:105