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 
24 #include "libavutil/common.h"
25 #include "parser.h"
26 #include "aac_ac3_parser.h"
27 
29  AVCodecContext *avctx,
30  const uint8_t **poutbuf, int *poutbuf_size,
31  const uint8_t *buf, int buf_size)
32 {
33  AACAC3ParseContext *s = s1->priv_data;
34  ParseContext *pc = &s->pc;
35  int len, i;
36  int new_frame_start;
37  int got_frame = 0;
38 
39 get_next:
41  if(s->remaining_size <= buf_size){
42  if(s->remaining_size && !s->need_next_header){
43  i= s->remaining_size;
44  s->remaining_size = 0;
45  }else{ //we need a header first
46  len=0;
47  for(i=s->remaining_size; i<buf_size; i++){
48  s->state = (s->state<<8) + buf[i];
49  if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start)))
50  break;
51  }
52  if(len<=0){
54  }else{
55  got_frame = 1;
56  s->state=0;
57  i-= s->header_size -1;
58  s->remaining_size = len;
59  if(!new_frame_start || pc->index+i<=0){
60  s->remaining_size += i;
61  goto get_next;
62  }
63  else if (i < 0) {
64  s->remaining_size += i;
65  }
66  }
67  }
68  }
69 
70  if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
71  s->remaining_size -= FFMIN(s->remaining_size, buf_size);
72  *poutbuf = NULL;
73  *poutbuf_size = 0;
74  return buf_size;
75  }
76 
77  *poutbuf = buf;
78  *poutbuf_size = buf_size;
79 
80  /* update codec info */
81  if(s->codec_id)
82  avctx->codec_id = s->codec_id;
83 
84  if (got_frame) {
85  /* Due to backwards compatible HE-AAC the sample rate, channel count,
86  and total number of samples found in an AAC ADTS header are not
87  reliable. Bit rate is still accurate because the total frame
88  duration in seconds is still correct (as is the number of bits in
89  the frame). */
90  if (avctx->codec_id != AV_CODEC_ID_AAC) {
91  avctx->sample_rate = s->sample_rate;
92  if (avctx->codec_id != AV_CODEC_ID_EAC3) {
93  avctx->channels = s->channels;
94  avctx->channel_layout = s->channel_layout;
95  }
96  s1->duration = s->samples;
97  avctx->audio_service_type = s->service_type;
98  }
99 
100  if (avctx->codec_id != AV_CODEC_ID_EAC3)
101  avctx->bit_rate = s->bit_rate;
102  }
103 
104  return i;
105 }
AVCodecContext::audio_service_type
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:1251
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1186
aac_ac3_parser.h
ParseContext
Definition: parser.h:28
ParseContext::index
int index
Definition: parser.h:30
s
#define s(width, name)
Definition: cbs_vp9.c:257
s1
#define s1
Definition: regdef.h:38
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:536
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:28
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
AACAC3ParseContext
Definition: aac_ac3_parser.h:40
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:450
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:412
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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:234
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1187
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
common.h
uint8_t
uint8_t
Definition: audio_convert.c:194
parser.h
len
int len
Definition: vorbis_enc_data.h:452
AVCodecParserContext
Definition: avcodec.h:3353
AVCodecContext
main external API structure.
Definition: avcodec.h:526
channel_layout.h
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40