Line | Branch | Exec | Source |
---|---|---|---|
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 "config_components.h" | ||
24 | |||
25 | #include "libavutil/channel_layout.h" | ||
26 | #include "libavutil/common.h" | ||
27 | #include "parser.h" | ||
28 | #include "aac_ac3_parser.h" | ||
29 | #include "ac3_parser_internal.h" | ||
30 | #include "adts_header.h" | ||
31 | |||
32 | 18410 | int ff_aac_ac3_parse(AVCodecParserContext *s1, | |
33 | AVCodecContext *avctx, | ||
34 | const uint8_t **poutbuf, int *poutbuf_size, | ||
35 | const uint8_t *buf, int buf_size) | ||
36 | { | ||
37 | 18410 | AACAC3ParseContext *s = s1->priv_data; | |
38 | 18410 | ParseContext *pc = &s->pc; | |
39 | int len, i; | ||
40 | int new_frame_start; | ||
41 | 18410 | int got_frame = 0; | |
42 | |||
43 |
2/2✓ Branch 0 taken 18398 times.
✓ Branch 1 taken 12 times.
|
18410 | if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
44 | 12 | i = buf_size; | |
45 | 12 | got_frame = 1; | |
46 | } else { | ||
47 | 18398 | get_next: | |
48 | 24635 | i=END_NOT_FOUND; | |
49 |
2/2✓ Branch 0 taken 20616 times.
✓ Branch 1 taken 4019 times.
|
24635 | if(s->remaining_size <= buf_size){ |
50 |
4/4✓ Branch 0 taken 12335 times.
✓ Branch 1 taken 8281 times.
✓ Branch 2 taken 5855 times.
✓ Branch 3 taken 6480 times.
|
20616 | if(s->remaining_size && !s->need_next_header){ |
51 | 5855 | i= s->remaining_size; | |
52 | 5855 | s->remaining_size = 0; | |
53 | }else{ //we need a header first | ||
54 | 14761 | len=0; | |
55 |
2/2✓ Branch 0 taken 88673 times.
✓ Branch 1 taken 2384 times.
|
91057 | for(i=s->remaining_size; i<buf_size; i++){ |
56 | 88673 | s->state = (s->state<<8) + buf[i]; | |
57 |
2/2✓ Branch 1 taken 12377 times.
✓ Branch 2 taken 76296 times.
|
88673 | if((len=s->sync(s->state, &s->need_next_header, &new_frame_start))) |
58 | 12377 | break; | |
59 | } | ||
60 |
2/2✓ Branch 0 taken 2384 times.
✓ Branch 1 taken 12377 times.
|
14761 | if(len<=0){ |
61 | 2384 | i=END_NOT_FOUND; | |
62 | }else{ | ||
63 | 12377 | got_frame = 1; | |
64 | 12377 | s->state=0; | |
65 | 12377 | i-= s->header_size -1; | |
66 | 12377 | s->remaining_size = len; | |
67 |
4/4✓ Branch 0 taken 12063 times.
✓ Branch 1 taken 314 times.
✓ Branch 2 taken 5923 times.
✓ Branch 3 taken 6140 times.
|
12377 | if(!new_frame_start || pc->index+i<=0){ |
68 | 6237 | s->remaining_size += i; | |
69 | 6237 | goto get_next; | |
70 | } | ||
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6140 times.
|
6140 | else if (i < 0) { |
72 | ✗ | s->remaining_size += i; | |
73 | } | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | |||
78 |
2/2✓ Branch 1 taken 6242 times.
✓ Branch 2 taken 12156 times.
|
18398 | if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ |
79 | 6242 | s->remaining_size -= FFMIN(s->remaining_size, buf_size); | |
80 | 6242 | *poutbuf = NULL; | |
81 | 6242 | *poutbuf_size = 0; | |
82 | 6242 | return buf_size; | |
83 | } | ||
84 | } | ||
85 | |||
86 | 12168 | *poutbuf = buf; | |
87 | 12168 | *poutbuf_size = buf_size; | |
88 | |||
89 |
2/2✓ Branch 0 taken 12007 times.
✓ Branch 1 taken 161 times.
|
12168 | if (got_frame) { |
90 | int bit_rate; | ||
91 | |||
92 | /* Due to backwards compatible HE-AAC the sample rate, channel count, | ||
93 | and total number of samples found in an AAC ADTS header are not | ||
94 | reliable. Bit rate is still accurate because the total frame | ||
95 | duration in seconds is still correct (as is the number of bits in | ||
96 | the frame). */ | ||
97 |
2/2✓ Branch 0 taken 6152 times.
✓ Branch 1 taken 5855 times.
|
12007 | if (avctx->codec_id != AV_CODEC_ID_AAC) { |
98 | #if CONFIG_AC3_PARSER | ||
99 | 6152 | AC3HeaderInfo hdr, *phrd = &hdr; | |
100 | 6152 | int offset = ff_ac3_find_syncword(buf, buf_size); | |
101 | |||
102 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6146 times.
|
6152 | if (offset < 0) |
103 | 9 | return i; | |
104 | |||
105 | 6146 | buf += offset; | |
106 | 6146 | buf_size -= offset; | |
107 |
1/2✓ Branch 0 taken 6459 times.
✗ Branch 1 not taken.
|
6459 | while (buf_size > 0) { |
108 | 6459 | int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size); | |
109 | |||
110 |
4/4✓ Branch 0 taken 6458 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6456 times.
|
6459 | if (ret < 0 || hdr.frame_size > buf_size) |
111 | 3 | return i; | |
112 | |||
113 |
2/2✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6143 times.
|
6456 | if (buf_size > hdr.frame_size) { |
114 | 313 | buf += hdr.frame_size; | |
115 | 313 | buf_size -= hdr.frame_size; | |
116 | 313 | continue; | |
117 | } | ||
118 | /* Check for false positives since the syncword is not enough. | ||
119 | See section 6.1.2 of A/52. */ | ||
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6143 times.
|
6143 | if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2)) |
121 | ✗ | return i; | |
122 | 6143 | break; | |
123 | } | ||
124 | |||
125 | 6143 | avctx->sample_rate = hdr.sample_rate; | |
126 | |||
127 |
2/2✓ Branch 0 taken 4592 times.
✓ Branch 1 taken 1551 times.
|
6143 | if (hdr.bitstream_id > 10) |
128 | 4592 | avctx->codec_id = AV_CODEC_ID_EAC3; | |
129 | |||
130 |
2/2✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 4592 times.
|
6143 | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { |
131 | 1551 | av_channel_layout_uninit(&avctx->ch_layout); | |
132 |
1/2✓ Branch 0 taken 1551 times.
✗ Branch 1 not taken.
|
1551 | if (hdr.channel_layout) { |
133 | 1551 | av_channel_layout_from_mask(&avctx->ch_layout, hdr.channel_layout); | |
134 | } else { | ||
135 | ✗ | avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | |
136 | ✗ | avctx->ch_layout.nb_channels = hdr.channels; | |
137 | } | ||
138 | } | ||
139 | 6143 | s1->duration = hdr.num_blocks * 256; | |
140 | 6143 | avctx->audio_service_type = hdr.bitstream_mode; | |
141 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 6143 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6143 | if (hdr.bitstream_mode == 0x7 && hdr.channels > 1) |
142 | ✗ | avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE; | |
143 | 6143 | bit_rate = hdr.bit_rate; | |
144 | #endif | ||
145 | } else { | ||
146 | #if CONFIG_AAC_PARSER | ||
147 | AACADTSHeaderInfo hdr; | ||
148 |
2/4✓ Branch 0 taken 5855 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5855 times.
|
11710 | if (buf_size < AV_AAC_ADTS_HEADER_SIZE || |
149 | 5855 | ff_adts_header_parse_buf(buf, &hdr) < 0) | |
150 | ✗ | return i; | |
151 | |||
152 | 5855 | bit_rate = hdr.bit_rate; | |
153 | #endif | ||
154 | } | ||
155 | |||
156 | /* Calculate the average bit rate */ | ||
157 | 11998 | s->frame_number++; | |
158 |
2/2✓ Branch 0 taken 7406 times.
✓ Branch 1 taken 4592 times.
|
11998 | if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { |
159 | 7406 | avctx->bit_rate += | |
160 | 7406 | (bit_rate - avctx->bit_rate) / s->frame_number; | |
161 | } | ||
162 | } | ||
163 | |||
164 | 12159 | return i; | |
165 | } | ||
166 |