FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac_ac3_parser.c
Date: 2026-10-29 17:07:12
Exec Total Coverage
Lines: 77 82 93.9%
Functions: 1 1 100.0%
Branches: 49 56 87.5%

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 21280 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 21280 AACAC3ParseContext *s = s1->priv_data;
38 21280 ParseContext *pc = &s->pc;
39 int len, i;
40 int new_frame_start;
41 21280 int got_frame = 0;
42
43 21280 s1->key_frame = -1;
44
45
2/2
✓ Branch 0 taken 21071 times.
✓ Branch 1 taken 209 times.
21280 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
46 209 i = buf_size;
47 209 got_frame = 1;
48 } else {
49 21071 get_next:
50 26369 i=END_NOT_FOUND;
51
2/2
✓ Branch 0 taken 21162 times.
✓ Branch 1 taken 5207 times.
26369 if(s->remaining_size <= buf_size){
52
4/4
✓ Branch 0 taken 12896 times.
✓ Branch 1 taken 8266 times.
✓ Branch 2 taken 4435 times.
✓ Branch 3 taken 8461 times.
21162 if(s->remaining_size && !s->need_next_header){
53 4435 i= s->remaining_size;
54 4435 s->remaining_size = 0;
55 }else{ //we need a header first
56 16727 len=0;
57
2/2
✓ Branch 0 taken 92600 times.
✓ Branch 1 taken 3789 times.
96389 for(i=s->remaining_size; i<buf_size; i++){
58 92600 s->state = (s->state<<8) + buf[i];
59
2/2
✓ Branch 1 taken 12938 times.
✓ Branch 2 taken 79662 times.
92600 if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
60 12938 break;
61 }
62
2/2
✓ Branch 0 taken 3789 times.
✓ Branch 1 taken 12938 times.
16727 if(len<=0){
63 3789 i=END_NOT_FOUND;
64 }else{
65 12938 got_frame = 1;
66 12938 s->state=0;
67 12938 i-= s->header_size -1;
68 12938 s->remaining_size = len;
69
4/4
✓ Branch 0 taken 12153 times.
✓ Branch 1 taken 785 times.
✓ Branch 2 taken 4513 times.
✓ Branch 3 taken 7640 times.
12938 if(!new_frame_start || pc->index+i<=0){
70 5298 s->remaining_size += i;
71 5298 goto get_next;
72 }
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7640 times.
7640 else if (i < 0) {
74 ✗ s->remaining_size += i;
75 }
76 }
77 }
78 }
79
80
2/2
✓ Branch 1 taken 8788 times.
✓ Branch 2 taken 12283 times.
21071 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
81 8788 s->remaining_size -= FFMIN(s->remaining_size, buf_size);
82 8788 *poutbuf = NULL;
83 8788 *poutbuf_size = 0;
84 8788 return buf_size;
85 }
86 }
87
88 12492 *poutbuf = buf;
89 12492 *poutbuf_size = buf_size;
90
91
2/2
✓ Branch 0 taken 12284 times.
✓ Branch 1 taken 208 times.
12492 if (got_frame) {
92 int bit_rate;
93
94 /* Due to backwards compatible HE-AAC the sample rate, channel count,
95 and total number of samples found in an AAC ADTS header are not
96 reliable. Bit rate is still accurate because the total frame
97 duration in seconds is still correct (as is the number of bits in
98 the frame). */
99
2/2
✓ Branch 0 taken 7709 times.
✓ Branch 1 taken 4575 times.
12284 if (avctx->codec_id != AV_CODEC_ID_AAC) {
100 #if CONFIG_AC3_PARSER
101 7709 AC3HeaderInfo hdr, *phrd = &hdr;
102 7709 int offset = ff_ac3_find_syncword(buf, buf_size);
103
104
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7702 times.
7709 if (offset < 0)
105 11 return i;
106
107 7702 buf += offset;
108 7702 buf_size -= offset;
109
1/2
✓ Branch 0 taken 8483 times.
✗ Branch 1 not taken.
8483 while (buf_size > 0) {
110 8483 int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
111
112
4/4
✓ Branch 0 taken 8482 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 8479 times.
8483 if (ret < 0 || hdr.frame_size > buf_size)
113 4 return i;
114
115
2/2
✓ Branch 0 taken 781 times.
✓ Branch 1 taken 7698 times.
8479 if (buf_size > hdr.frame_size) {
116 781 buf += hdr.frame_size;
117 781 buf_size -= hdr.frame_size;
118 781 continue;
119 }
120 /* Check for false positives since the syncword is not enough.
121 See section 6.1.2 of A/52. */
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7698 times.
7698 if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
123 ✗ return i;
124 7698 break;
125 }
126
127 7698 avctx->sample_rate = hdr.sample_rate;
128
129
2/2
✓ Branch 0 taken 5849 times.
✓ Branch 1 taken 1849 times.
7698 if (hdr.bitstream_id > 10)
130 5849 avctx->codec_id = AV_CODEC_ID_EAC3;
131
132
2/2
✓ Branch 0 taken 1849 times.
✓ Branch 1 taken 5849 times.
7698 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
133 1849 av_channel_layout_uninit(&avctx->ch_layout);
134
1/2
✓ Branch 0 taken 1849 times.
✗ Branch 1 not taken.
1849 if (hdr.channel_layout) {
135 1849 av_channel_layout_from_mask(&avctx->ch_layout, hdr.channel_layout);
136 } else {
137 ✗ avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
138 ✗ avctx->ch_layout.nb_channels = hdr.channels;
139 }
140 }
141 7698 s1->duration = hdr.num_blocks * 256;
142 7698 avctx->audio_service_type = hdr.bitstream_mode;
143
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7698 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7698 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
144 ✗ avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
145 7698 bit_rate = hdr.bit_rate;
146 #endif
147 } else {
148 #if CONFIG_AAC_PARSER
149 AACADTSHeaderInfo hdr;
150
151
4/4
✓ Branch 0 taken 4567 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 4521 times.
9142 if (buf_size < AV_AAC_ADTS_HEADER_SIZE ||
152 4567 ff_adts_header_parse_buf(buf, &hdr) < 0)
153 54 return i;
154
155
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 4460 times.
4521 if (avctx->profile == AV_PROFILE_UNKNOWN)
156 61 avctx->profile = hdr.object_type - 1;
157 /* ADTS does not support USAC */
158 4521 s1->key_frame = 1;
159 4521 bit_rate = hdr.bit_rate;
160 #endif
161 }
162
163 /* Calculate the average bit rate */
164 12219 s->frame_number++;
165
2/2
✓ Branch 0 taken 6370 times.
✓ Branch 1 taken 5849 times.
12219 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
166 6370 avctx->bit_rate +=
167 6370 (bit_rate - avctx->bit_rate) / s->frame_number;
168 }
169 }
170
171 12427 return i;
172 }
173