FFmpeg
 All Data Structures 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 
23 #include "libavutil/common.h"
24 #include "parser.h"
25 #include "aac_ac3_parser.h"
26 
28  AVCodecContext *avctx,
29  const uint8_t **poutbuf, int *poutbuf_size,
30  const uint8_t *buf, int buf_size)
31 {
33  ParseContext *pc = &s->pc;
34  int len, i;
35  int new_frame_start;
36 
37 get_next:
38  i=END_NOT_FOUND;
39  if(s->remaining_size <= buf_size){
40  if(s->remaining_size && !s->need_next_header){
41  i= s->remaining_size;
42  s->remaining_size = 0;
43  }else{ //we need a header first
44  len=0;
45  for(i=s->remaining_size; i<buf_size; i++){
46  s->state = (s->state<<8) + buf[i];
47  if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start)))
48  break;
49  }
50  if(len<=0){
51  i=END_NOT_FOUND;
52  }else{
53  s->state=0;
54  i-= s->header_size -1;
55  s->remaining_size = len;
56  if(!new_frame_start || pc->index+i<=0){
57  s->remaining_size += i;
58  goto get_next;
59  }
60  }
61  }
62  }
63 
64  if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
65  s->remaining_size -= FFMIN(s->remaining_size, buf_size);
66  *poutbuf = NULL;
67  *poutbuf_size = 0;
68  return buf_size;
69  }
70 
71  *poutbuf = buf;
72  *poutbuf_size = buf_size;
73 
74  /* update codec info */
75  if(s->codec_id)
76  avctx->codec_id = s->codec_id;
77 
78  /* Due to backwards compatible HE-AAC the sample rate, channel count,
79  and total number of samples found in an AAC ADTS header are not
80  reliable. Bit rate is still accurate because the total frame duration in
81  seconds is still correct (as is the number of bits in the frame). */
82  if (avctx->codec_id != AV_CODEC_ID_AAC) {
83  avctx->sample_rate = s->sample_rate;
84 
85  /* allow downmixing to stereo (or mono for AC-3) */
86  if(avctx->request_channels > 0 &&
87  avctx->request_channels < s->channels &&
88  (avctx->request_channels <= 2 ||
89  (avctx->request_channels == 1 &&
90  (avctx->codec_id == AV_CODEC_ID_AC3 ||
91  avctx->codec_id == AV_CODEC_ID_EAC3)))) {
92  avctx->channels = avctx->request_channels;
93  } else {
94  avctx->channels = s->channels;
95  avctx->channel_layout = s->channel_layout;
96  }
97  s1->duration = s->samples;
98  avctx->audio_service_type = s->service_type;
99  }
100 
101  avctx->bit_rate = s->bit_rate;
102 
103  return i;
104 }