yading@10
|
1 /*
|
yading@10
|
2 * Common AAC and AC-3 parser
|
yading@10
|
3 * Copyright (c) 2003 Fabrice Bellard
|
yading@10
|
4 * Copyright (c) 2003 Michael Niedermayer
|
yading@10
|
5 *
|
yading@10
|
6 * This file is part of FFmpeg.
|
yading@10
|
7 *
|
yading@10
|
8 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
9 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
10 * License as published by the Free Software Foundation; either
|
yading@10
|
11 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
12 *
|
yading@10
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
16 * Lesser General Public License for more details.
|
yading@10
|
17 *
|
yading@10
|
18 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
19 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
21 */
|
yading@10
|
22
|
yading@10
|
23 #include "libavutil/common.h"
|
yading@10
|
24 #include "parser.h"
|
yading@10
|
25 #include "aac_ac3_parser.h"
|
yading@10
|
26
|
yading@10
|
27 int ff_aac_ac3_parse(AVCodecParserContext *s1,
|
yading@10
|
28 AVCodecContext *avctx,
|
yading@10
|
29 const uint8_t **poutbuf, int *poutbuf_size,
|
yading@10
|
30 const uint8_t *buf, int buf_size)
|
yading@10
|
31 {
|
yading@10
|
32 AACAC3ParseContext *s = s1->priv_data;
|
yading@10
|
33 ParseContext *pc = &s->pc;
|
yading@10
|
34 int len, i;
|
yading@10
|
35 int new_frame_start;
|
yading@10
|
36
|
yading@10
|
37 get_next:
|
yading@10
|
38 i=END_NOT_FOUND;
|
yading@10
|
39 if(s->remaining_size <= buf_size){
|
yading@10
|
40 if(s->remaining_size && !s->need_next_header){
|
yading@10
|
41 i= s->remaining_size;
|
yading@10
|
42 s->remaining_size = 0;
|
yading@10
|
43 }else{ //we need a header first
|
yading@10
|
44 len=0;
|
yading@10
|
45 for(i=s->remaining_size; i<buf_size; i++){
|
yading@10
|
46 s->state = (s->state<<8) + buf[i];
|
yading@10
|
47 if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start)))
|
yading@10
|
48 break;
|
yading@10
|
49 }
|
yading@10
|
50 if(len<=0){
|
yading@10
|
51 i=END_NOT_FOUND;
|
yading@10
|
52 }else{
|
yading@10
|
53 s->state=0;
|
yading@10
|
54 i-= s->header_size -1;
|
yading@10
|
55 s->remaining_size = len;
|
yading@10
|
56 if(!new_frame_start || pc->index+i<=0){
|
yading@10
|
57 s->remaining_size += i;
|
yading@10
|
58 goto get_next;
|
yading@10
|
59 }
|
yading@10
|
60 }
|
yading@10
|
61 }
|
yading@10
|
62 }
|
yading@10
|
63
|
yading@10
|
64 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
|
yading@10
|
65 s->remaining_size -= FFMIN(s->remaining_size, buf_size);
|
yading@10
|
66 *poutbuf = NULL;
|
yading@10
|
67 *poutbuf_size = 0;
|
yading@10
|
68 return buf_size;
|
yading@10
|
69 }
|
yading@10
|
70
|
yading@10
|
71 *poutbuf = buf;
|
yading@10
|
72 *poutbuf_size = buf_size;
|
yading@10
|
73
|
yading@10
|
74 /* update codec info */
|
yading@10
|
75 if(s->codec_id)
|
yading@10
|
76 avctx->codec_id = s->codec_id;
|
yading@10
|
77
|
yading@10
|
78 /* Due to backwards compatible HE-AAC the sample rate, channel count,
|
yading@10
|
79 and total number of samples found in an AAC ADTS header are not
|
yading@10
|
80 reliable. Bit rate is still accurate because the total frame duration in
|
yading@10
|
81 seconds is still correct (as is the number of bits in the frame). */
|
yading@10
|
82 if (avctx->codec_id != AV_CODEC_ID_AAC) {
|
yading@10
|
83 avctx->sample_rate = s->sample_rate;
|
yading@10
|
84
|
yading@10
|
85 /* allow downmixing to stereo (or mono for AC-3) */
|
yading@10
|
86 if(avctx->request_channels > 0 &&
|
yading@10
|
87 avctx->request_channels < s->channels &&
|
yading@10
|
88 (avctx->request_channels <= 2 ||
|
yading@10
|
89 (avctx->request_channels == 1 &&
|
yading@10
|
90 (avctx->codec_id == AV_CODEC_ID_AC3 ||
|
yading@10
|
91 avctx->codec_id == AV_CODEC_ID_EAC3)))) {
|
yading@10
|
92 avctx->channels = avctx->request_channels;
|
yading@10
|
93 } else {
|
yading@10
|
94 avctx->channels = s->channels;
|
yading@10
|
95 avctx->channel_layout = s->channel_layout;
|
yading@10
|
96 }
|
yading@10
|
97 s1->duration = s->samples;
|
yading@10
|
98 avctx->audio_service_type = s->service_type;
|
yading@10
|
99 }
|
yading@10
|
100
|
yading@10
|
101 avctx->bit_rate = s->bit_rate;
|
yading@10
|
102
|
yading@10
|
103 return i;
|
yading@10
|
104 }
|