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 
44  i = buf_size;
45  got_frame = 1;
46  } else {
47 get_next:
49  if(s->remaining_size <= buf_size){
50  if(s->remaining_size && !s->need_next_header){
51  i= s->remaining_size;
52  s->remaining_size = 0;
53  }else{ //we need a header first
54  len=0;
55  for(i=s->remaining_size; i<buf_size; i++){
56  s->state = (s->state<<8) + buf[i];
57  if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
58  break;
59  }
60  if(len<=0){
62  }else{
63  got_frame = 1;
64  s->state=0;
65  i-= s->header_size -1;
66  s->remaining_size = len;
67  if(!new_frame_start || pc->index+i<=0){
68  s->remaining_size += i;
69  goto get_next;
70  }
71  else if (i < 0) {
72  s->remaining_size += i;
73  }
74  }
75  }
76  }
77 
78  if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
79  s->remaining_size -= FFMIN(s->remaining_size, buf_size);
80  *poutbuf = NULL;
81  *poutbuf_size = 0;
82  return buf_size;
83  }
84  }
85 
86  *poutbuf = buf;
87  *poutbuf_size = buf_size;
88 
89  if (got_frame) {
90  int bit_rate;
91 
92  /* Due to backwards compatible HE-AAC the sample rate, channel count,
93  and total number of samples found in an AAC ADTS header are not
94  reliable. Bit rate is still accurate because the total frame
95  duration in seconds is still correct (as is the number of bits in
96  the frame). */
97  if (avctx->codec_id != AV_CODEC_ID_AAC) {
98 #if CONFIG_AC3_PARSER
99  AC3HeaderInfo hdr, *phrd = &hdr;
100  int offset = ff_ac3_find_syncword(buf, buf_size);
101 
102  if (offset < 0)
103  return i;
104 
105  buf += offset;
106  buf_size -= offset;
107  while (buf_size > 0) {
108  int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
109 
110  if (ret < 0 || hdr.frame_size > buf_size)
111  return i;
112 
113  if (buf_size > hdr.frame_size) {
114  buf += hdr.frame_size;
115  buf_size -= hdr.frame_size;
116  continue;
117  }
118  /* Check for false positives since the syncword is not enough.
119  See section 6.1.2 of A/52. */
120  if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
121  return i;
122  break;
123  }
124 
125  avctx->sample_rate = hdr.sample_rate;
126 
127  if (hdr.bitstream_id > 10)
128  avctx->codec_id = AV_CODEC_ID_EAC3;
129 
130  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
132  if (hdr.channel_layout) {
134  } else {
136  avctx->ch_layout.nb_channels = hdr.channels;
137  }
138  }
139  s1->duration = hdr.num_blocks * 256;
140  avctx->audio_service_type = hdr.bitstream_mode;
141  if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
143  bit_rate = hdr.bit_rate;
144 #endif
145  } else {
146 #if CONFIG_AAC_PARSER
147  AACADTSHeaderInfo hdr;
148  if (buf_size < AV_AAC_ADTS_HEADER_SIZE ||
149  ff_adts_header_parse_buf(buf, &hdr) < 0)
150  return i;
151 
152  bit_rate = hdr.bit_rate;
153 #endif
154  }
155 
156  /* Calculate the average bit rate */
157  s->frame_number++;
158  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
159  avctx->bit_rate +=
160  (bit_rate - avctx->bit_rate) / s->frame_number;
161  }
162  }
163 
164  return i;
165 }
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:316
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
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
AC3HeaderInfo
Definition: ac3_parser_internal.h:34
AC3HeaderInfo::frame_size
uint16_t frame_size
Definition: ac3_parser_internal.h:61
ff_adts_header_parse_buf
int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE+AV_INPUT_BUFFER_PADDING_SIZE], AACADTSHeaderInfo *hdr)
Wrapper around ff_adts_header_parse() for users that don't already have a suitable GetBitContext.
Definition: adts_header.c:76
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:116
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:247
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
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:480
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:442
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
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
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
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:437
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
AC3HeaderInfo::bit_rate
uint32_t bit_rate
Definition: ac3_parser_internal.h:59
AACADTSHeaderInfo
Definition: adts_header.h:35