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 {
37  AACAC3ParseContext *s = s1->priv_data;
38  ParseContext *pc = &s->pc;
39  int len, i;
40  int new_frame_start;
41  int got_frame = 0;
42 
43  if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
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  AC3HeaderInfo hdr, *phrd = &hdr;
99  int offset = ff_ac3_find_syncword(buf, buf_size);
100 
101  if (offset < 0)
102  return i;
103 
104  buf += offset;
105  buf_size -= offset;
106  while (buf_size > 0) {
107  int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
108 
109  if (ret < 0 || hdr.frame_size > buf_size)
110  return i;
111 
112  if (buf_size > hdr.frame_size) {
113  buf += hdr.frame_size;
114  buf_size -= hdr.frame_size;
115  continue;
116  }
117  /* Check for false positives since the syncword is not enough.
118  See section 6.1.2 of A/52. */
119  if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
120  return i;
121  break;
122  }
123 
124  avctx->sample_rate = hdr.sample_rate;
125 
126  if (hdr.bitstream_id > 10)
127  avctx->codec_id = AV_CODEC_ID_EAC3;
128 
129  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
131  if (hdr.channel_layout) {
133  } else {
135  avctx->ch_layout.nb_channels = hdr.channels;
136  }
137 #if FF_API_OLD_CHANNEL_LAYOUT
139  avctx->channels = avctx->ch_layout.nb_channels;
140  avctx->channel_layout = hdr.channel_layout;
142 #endif
143  }
144  s1->duration = hdr.num_blocks * 256;
145  avctx->audio_service_type = hdr.bitstream_mode;
146  if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
148  bit_rate = hdr.bit_rate;
149  } else {
150  AACADTSHeaderInfo hdr, *phrd = &hdr;
151  int ret = avpriv_adts_header_parse(&phrd, buf, buf_size);
152 
153  if (ret < 0)
154  return i;
155 
156  bit_rate = hdr.bit_rate;
157  }
158 
159  /* Calculate the average bit rate */
160  s->frame_number++;
161  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
162  avctx->bit_rate +=
163  (bit_rate - avctx->bit_rate) / s->frame_number;
164  }
165  }
166 
167  return i;
168 }
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:82
AVCodecContext::audio_service_type
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:1086
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1007
aac_ac3_parser.h
AACADTSHeaderInfo::bit_rate
uint32_t bit_rate
Definition: adts_header.h:31
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:306
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
ParseContext
Definition: parser.h:28
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2059
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
ParseContext::index
int index
Definition: parser.h:30
s
#define s(width, name)
Definition: cbs_vp9.c:256
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:112
s1
#define s1
Definition: regdef.h:38
av_channel_layout_from_mask
FF_ENABLE_DEPRECATION_WARNINGS 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:391
AC3HeaderInfo::sample_rate
uint16_t sample_rate
Definition: ac3_parser_internal.h:58
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:408
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:448
AACAC3ParseContext
Definition: aac_ac3_parser.h:41
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:475
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:437
avpriv_adts_header_parse
int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_t size)
Parse the ADTS frame header contained in the buffer, which is the first 54 bits.
Definition: adts_parser.c:46
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:199
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2834
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
common.h
avpriv_ac3_parse_header
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition: ac3_parser.c:265
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:2800
ret
ret
Definition: filter_design.txt:187
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: defs.h:88
AVCodecContext
main external API structure.
Definition: avcodec.h:398
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:632
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
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
AC3HeaderInfo::bit_rate
uint32_t bit_rate
Definition: ac3_parser_internal.h:59
AACADTSHeaderInfo
Definition: adts_header.h:28