FFmpeg
aac_ac3_parser.c
Go to the documentation of this file.
1 /*
2  * Common AAC and 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 
23 #include "config_components.h"
24 
26 #include "libavutil/common.h"
27 #include "parser.h"
28 #include "aac_ac3_parser.h"
29 #include "ac3_parser_internal.h"
30 #include "adts_header.h"
31 
33  AVCodecContext *avctx,
34  const uint8_t **poutbuf, int *poutbuf_size,
35  const uint8_t *buf, int buf_size)
36 {
38  ParseContext *pc = &s->pc;
39  int len, i;
40  int new_frame_start;
41  int got_frame = 0;
42 
43  s1->key_frame = -1;
44 
46  i = buf_size;
47  got_frame = 1;
48  } else {
49 get_next:
51  if(s->remaining_size <= buf_size){
52  if(s->remaining_size && !s->need_next_header){
53  i= s->remaining_size;
54  s->remaining_size = 0;
55  }else{ //we need a header first
56  len=0;
57  for(i=s->remaining_size; i<buf_size; i++){
58  s->state = (s->state<<8) + buf[i];
59  if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
60  break;
61  }
62  if(len<=0){
64  }else{
65  got_frame = 1;
66  s->state=0;
67  i-= s->header_size -1;
68  s->remaining_size = len;
69  if(!new_frame_start || pc->index+i<=0){
70  s->remaining_size += i;
71  goto get_next;
72  }
73  else if (i < 0) {
74  s->remaining_size += i;
75  }
76  }
77  }
78  }
79 
80  if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
81  s->remaining_size -= FFMIN(s->remaining_size, buf_size);
82  *poutbuf = NULL;
83  *poutbuf_size = 0;
84  return buf_size;
85  }
86  }
87 
88  *poutbuf = buf;
89  *poutbuf_size = buf_size;
90 
91  if (got_frame) {
92  int bit_rate;
93 
94  /* Due to backwards compatible HE-AAC the sample rate, channel count,
95  and total number of samples found in an AAC ADTS header are not
96  reliable. Bit rate is still accurate because the total frame
97  duration in seconds is still correct (as is the number of bits in
98  the frame). */
99  if (avctx->codec_id != AV_CODEC_ID_AAC) {
100 #if CONFIG_AC3_PARSER
101  AC3HeaderInfo hdr, *phrd = &hdr;
102  int offset = ff_ac3_find_syncword(buf, buf_size);
103 
104  if (offset < 0)
105  return i;
106 
107  buf += offset;
108  buf_size -= offset;
109  while (buf_size > 0) {
110  int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
111 
112  if (ret < 0 || hdr.frame_size > buf_size)
113  return i;
114 
115  if (buf_size > hdr.frame_size) {
116  buf += hdr.frame_size;
117  buf_size -= hdr.frame_size;
118  continue;
119  }
120  /* Check for false positives since the syncword is not enough.
121  See section 6.1.2 of A/52. */
122  if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
123  return i;
124  break;
125  }
126 
127  avctx->sample_rate = hdr.sample_rate;
128 
129  if (hdr.bitstream_id > 10)
130  avctx->codec_id = AV_CODEC_ID_EAC3;
131 
132  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
134  if (hdr.channel_layout) {
136  } else {
138  avctx->ch_layout.nb_channels = hdr.channels;
139  }
140  }
141  s1->duration = hdr.num_blocks * 256;
142  avctx->audio_service_type = hdr.bitstream_mode;
143  if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
145  bit_rate = hdr.bit_rate;
146 #endif
147  } else {
148 #if CONFIG_AAC_PARSER
149  AACADTSHeaderInfo hdr;
150  GetBitContext gb;
151  int profile;
152  init_get_bits8(&gb, buf, buf_size);
153  if (buf_size < AV_AAC_ADTS_HEADER_SIZE ||
154  ff_adts_header_parse(&gb, &hdr) < 0)
155  return i;
156 
157  avctx->profile = hdr.object_type - 1;
158  s1->key_frame = (avctx->profile == AV_PROFILE_AAC_USAC) ? get_bits1(&gb) : 1;
159  bit_rate = hdr.bit_rate;
160 #endif
161  }
162 
163  /* Calculate the average bit rate */
164  s->frame_number++;
165  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
166  avctx->bit_rate +=
167  (bit_rate - avctx->bit_rate) / s->frame_number;
168  }
169  }
170 
171  return i;
172 }
AVCodecContext::audio_service_type
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:1103
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
AVCodecParserContext::duration
int duration
Duration of the current frame.
Definition: avcodec.h:2862
AV_AAC_ADTS_HEADER_SIZE
#define AV_AAC_ADTS_HEADER_SIZE
Definition: adts_parser.h:25
aac_ac3_parser.h
AACADTSHeaderInfo::bit_rate
uint32_t bit_rate
Definition: adts_header.h:38
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:322
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
AVCodecParserContext::key_frame
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:2797
ParseContext
Definition: parser.h:28
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
AC3HeaderInfo::channel_layout
uint64_t channel_layout
Definition: ac3_parser_internal.h:62
ff_adts_header_parse
int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse the ADTS frame header to the end of the variable header, which is the first 54 bits.
Definition: adts_header.c:30
GetBitContext
Definition: get_bits.h:108
AC3HeaderInfo
Definition: ac3_parser_internal.h:34
AC3HeaderInfo::frame_size
uint16_t frame_size
Definition: ac3_parser_internal.h:61
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
ParseContext::index
int index
Definition: parser.h:30
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:250
AC3HeaderInfo::sample_rate
uint16_t sample_rate
Definition: ac3_parser_internal.h:58
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:461
ff_aac_ac3_parse
int ff_aac_ac3_parse(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: aac_ac3_parser.c:32
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:501
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
AVCodecParserContext::flags
int flags
Definition: avcodec.h:2781
AACAC3ParseContext
Definition: aac_ac3_parser.h:31
AC3HeaderInfo::num_blocks
int num_blocks
number of audio blocks
Definition: ac3_parser_internal.h:50
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:486
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:448
AC3HeaderInfo::channels
uint8_t channels
Definition: ac3_parser_internal.h:60
ac3_parser_internal.h
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AC3HeaderInfo::bitstream_mode
uint8_t bitstream_mode
Definition: ac3_parser_internal.h:42
AACADTSHeaderInfo::object_type
uint8_t object_type
Definition: adts_header.h:40
ff_combine_frame
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:203
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2782
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
common.h
avpriv_ac3_parse_header
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition: ac3_parser.c:267
ff_ac3_find_syncword
int ff_ac3_find_syncword(const uint8_t *buf, int buf_size)
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
parser.h
len
int len
Definition: vorbis_enc_data.h:426
profile
int profile
Definition: mxfenc.c:2233
AVCodecParserContext
Definition: avcodec.h:2748
ret
ret
Definition: filter_design.txt:187
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: defs.h:233
AVCodecContext
main external API structure.
Definition: avcodec.h:451
channel_layout.h
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1650
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:440
AC3HeaderInfo::bitstream_id
uint8_t bitstream_id
Definition: ac3_parser_internal.h:41
adts_header.h
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParserContext::priv_data
void * priv_data
Definition: avcodec.h:2749
AV_PROFILE_AAC_USAC
#define AV_PROFILE_AAC_USAC
Definition: defs.h:76
AC3HeaderInfo::bit_rate
uint32_t bit_rate
Definition: ac3_parser_internal.h:59
AACADTSHeaderInfo
Definition: adts_header.h:35