FFmpeg
Loading...
Searching...
No Matches
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 {
49get_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;
143 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
145 bit_rate = hdr.bit_rate;
146#endif
147 } else {
148#if CONFIG_AAC_PARSER
150
151 if (buf_size < AV_AAC_ADTS_HEADER_SIZE ||
152 ff_adts_header_parse_buf(buf, &hdr) < 0)
153 return i;
154
155 if (avctx->profile == AV_PROFILE_UNKNOWN)
156 avctx->profile = hdr.object_type - 1;
157 /* ADTS does not support USAC */
158 s1->key_frame = 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}
int ff_aac_ac3_parse(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition ac3_parser.c:491
int ff_ac3_find_syncword(const uint8_t *buf, int buf_size)
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
#define AV_AAC_ADTS_HEADER_SIZE
Definition adts_parser.h:25
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
common internal and external API header
#define NULL
Definition coverity.c:32
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition defs.h:244
@ AV_CODEC_ID_EAC3
Definition codec_id.h:493
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
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.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
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:421
unsigned offset
Definition libaomenc.c:763
#define FFMIN(a, b)
Definition macros.h:49
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:213
#define END_NOT_FOUND
Definition parser.h:40
Coded AC-3 header values up to the lfeon element, plus derived values.
int num_blocks
number of audio blocks
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition avcodec.h:1089
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int profile
profile
Definition avcodec.h:1636
int sample_rate
samples per second
Definition avcodec.h:1040
enum AVCodecID codec_id
Definition avcodec.h:453
int duration
Duration of the current frame.
Definition avcodec.h:2731
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition avcodec.h:2666
int index
Definition parser.h:30
int len