]> git.sesse.net Git - ffmpeg/blob - libavcodec/aac_ac3_parser.c
mpeg12dec: move setting first_field to mpeg_field_start()
[ffmpeg] / libavcodec / aac_ac3_parser.c
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "parser.h"
26 #include "aac_ac3_parser.h"
27
28 int ff_aac_ac3_parse(AVCodecParserContext *s1,
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
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 (s->channels > 1 &&
88             avctx->request_channel_layout == AV_CH_LAYOUT_MONO) {
89             avctx->channels       = 1;
90             avctx->channel_layout = AV_CH_LAYOUT_MONO;
91         } else if (s->channels > 2 &&
92                    avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
93             avctx->channels       = 2;
94             avctx->channel_layout = AV_CH_LAYOUT_STEREO;
95         } else {
96             avctx->channels = s->channels;
97             avctx->channel_layout = s->channel_layout;
98         }
99         s1->duration = s->samples;
100         avctx->audio_service_type = s->service_type;
101     }
102
103     avctx->bit_rate = s->bit_rate;
104
105     return i;
106 }