FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
34  ParseContext *pc = &s->pc;
35  int len, i;
36  int new_frame_start;
37 
38 get_next:
39  i=END_NOT_FOUND;
40  if(s->remaining_size <= buf_size){
41  if(s->remaining_size && !s->need_next_header){
42  i= s->remaining_size;
43  s->remaining_size = 0;
44  }else{ //we need a header first
45  len=0;
46  for(i=s->remaining_size; i<buf_size; i++){
47  s->state = (s->state<<8) + buf[i];
48  if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start)))
49  break;
50  }
51  if(len<=0){
52  i=END_NOT_FOUND;
53  }else{
54  s->state=0;
55  i-= s->header_size -1;
56  s->remaining_size = len;
57  if(!new_frame_start || pc->index+i<=0){
58  s->remaining_size += i;
59  goto get_next;
60  }
61  }
62  }
63  }
64 
65  if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
66  s->remaining_size -= FFMIN(s->remaining_size, buf_size);
67  *poutbuf = NULL;
68  *poutbuf_size = 0;
69  return buf_size;
70  }
71 
72  *poutbuf = buf;
73  *poutbuf_size = buf_size;
74 
75  /* update codec info */
76  if(s->codec_id)
77  avctx->codec_id = s->codec_id;
78 
79  /* Due to backwards compatible HE-AAC the sample rate, channel count,
80  and total number of samples found in an AAC ADTS header are not
81  reliable. Bit rate is still accurate because the total frame duration in
82  seconds is still correct (as is the number of bits in the frame). */
83  if (avctx->codec_id != AV_CODEC_ID_AAC) {
84  avctx->sample_rate = s->sample_rate;
85 
86  /* (E-)AC-3: allow downmixing to stereo or mono */
87 #if FF_API_REQUEST_CHANNELS
89  if (avctx->request_channels == 1)
91  else if (avctx->request_channels == 2)
94 #endif
95  if (s->channels > 1 &&
97  avctx->channels = 1;
99  } else if (s->channels > 2 &&
101  avctx->channels = 2;
103  } else {
104  avctx->channels = s->channels;
105  avctx->channel_layout = s->channel_layout;
106  }
107  s1->duration = s->samples;
108  avctx->audio_service_type = s->service_type;
109  }
110 
111  avctx->bit_rate = s->bit_rate;
112 
113  return i;
114 }
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
attribute_deprecated int request_channels
Decoder should decode to this many channels if it can (0 for default)
Definition: avcodec.h:2038
int duration
Duration of the current frame.
Definition: avcodec.h:4380
#define AV_CH_LAYOUT_STEREO
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:2060
uint8_t
int(* sync)(uint64_t state, struct AACAC3ParseContext *hdr_info, int *need_next_header, int *new_frame_start)
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:231
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
int bit_rate
the average bitrate
Definition: avcodec.h:1305
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:66
enum AVCodecID codec_id
Definition: avcodec.h:1258
int sample_rate
samples per second
Definition: avcodec.h:1985
main external API structure.
Definition: avcodec.h:1241
void * buf
Definition: avisynth_c.h:553
enum AVCodecID codec_id
#define s1
Definition: regdef.h:38
#define END_NOT_FOUND
Definition: parser.h:40
int ff_aac_ac3_parse(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
common internal and external API header
int index
Definition: parser.h:30
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
int len
int channels
number of audio channels
Definition: avcodec.h:1986
#define AV_CH_LAYOUT_MONO
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
Definition: avcodec.h:2053