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 
31  AVCodecContext *avctx,
32  const uint8_t **poutbuf, int *poutbuf_size,
33  const uint8_t *buf, int buf_size)
34 {
35  AACAC3ParseContext *s = s1->priv_data;
36  ParseContext *pc = &s->pc;
37  int len, i;
38  int new_frame_start;
39  int got_frame = 0;
40 
41 get_next:
43  if(s->remaining_size <= buf_size){
44  if(s->remaining_size && !s->need_next_header){
45  i= s->remaining_size;
46  s->remaining_size = 0;
47  }else{ //we need a header first
48  len=0;
49  for(i=s->remaining_size; i<buf_size; i++){
50  s->state = (s->state<<8) + buf[i];
51  if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start)))
52  break;
53  }
54  if(len<=0){
56  }else{
57  got_frame = 1;
58  s->state=0;
59  i-= s->header_size -1;
60  s->remaining_size = len;
61  if(!new_frame_start || pc->index+i<=0){
62  s->remaining_size += i;
63  goto get_next;
64  }
65  else if (i < 0) {
66  s->remaining_size += i;
67  }
68  }
69  }
70  }
71 
72  if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
73  s->remaining_size -= FFMIN(s->remaining_size, buf_size);
74  *poutbuf = NULL;
75  *poutbuf_size = 0;
76  return buf_size;
77  }
78 
79  *poutbuf = buf;
80  *poutbuf_size = buf_size;
81 
82  /* update codec info */
83  if(s->codec_id)
84  avctx->codec_id = s->codec_id;
85 
86  if (got_frame) {
87  /* Due to backwards compatible HE-AAC the sample rate, channel count,
88  and total number of samples found in an AAC ADTS header are not
89  reliable. Bit rate is still accurate because the total frame
90  duration in seconds is still correct (as is the number of bits in
91  the frame). */
92  if (avctx->codec_id != AV_CODEC_ID_AAC) {
93  avctx->sample_rate = s->sample_rate;
94  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
96  if (s->channel_layout) {
97  av_channel_layout_from_mask(&avctx->ch_layout, s->channel_layout);
98  } else {
100  avctx->ch_layout.nb_channels = s->channels;
101  }
102 #if FF_API_OLD_CHANNEL_LAYOUT
104  avctx->channels = avctx->ch_layout.nb_channels;
105  avctx->channel_layout = s->channel_layout;
107 #endif
108  }
109  s1->duration = s->samples;
110  avctx->audio_service_type = s->service_type;
111  }
112 
113  /* Calculate the average bit rate */
114  s->frame_number++;
115  if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
116  avctx->bit_rate +=
117  (s->bit_rate - avctx->bit_rate) / s->frame_number;
118  }
119  }
120 
121  return i;
122 }
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
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
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:295
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
ParseContext
Definition: parser.h:28
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2065
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:106
s1
#define s1
Definition: regdef.h:38
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:30
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:40
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:630
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:469
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:431
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
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
common.h
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
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:389
AVCodecContext
main external API structure.
Definition: avcodec.h:398
channel_layout.h
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:82
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40