FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac_ac3_parser.c
Date: 2023-06-04 16:45:34
Exec Total Coverage
Lines: 75 81 92.6%
Functions: 1 1 100.0%
Branches: 44 52 84.6%

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 18386 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 18386 AACAC3ParseContext *s = s1->priv_data;
38 18386 ParseContext *pc = &s->pc;
39 int len, i;
40 int new_frame_start;
41 18386 int got_frame = 0;
42
43
2/2
✓ Branch 0 taken 18374 times.
✓ Branch 1 taken 12 times.
18386 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
44 12 i = buf_size;
45 12 got_frame = 1;
46 } else {
47 18374 get_next:
48 24606 i=END_NOT_FOUND;
49
2/2
✓ Branch 0 taken 20602 times.
✓ Branch 1 taken 4004 times.
24606 if(s->remaining_size <= buf_size){
50
4/4
✓ Branch 0 taken 12327 times.
✓ Branch 1 taken 8275 times.
✓ Branch 2 taken 5855 times.
✓ Branch 3 taken 6472 times.
20602 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 14747 len=0;
55
2/2
✓ Branch 0 taken 88582 times.
✓ Branch 1 taken 2383 times.
90965 for(i=s->remaining_size; i<buf_size; i++){
56 88582 s->state = (s->state<<8) + buf[i];
57
2/2
✓ Branch 1 taken 12364 times.
✓ Branch 2 taken 76218 times.
88582 if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
58 12364 break;
59 }
60
2/2
✓ Branch 0 taken 2383 times.
✓ Branch 1 taken 12364 times.
14747 if(len<=0){
61 2383 i=END_NOT_FOUND;
62 }else{
63 12364 got_frame = 1;
64 12364 s->state=0;
65 12364 i-= s->header_size -1;
66 12364 s->remaining_size = len;
67
4/4
✓ Branch 0 taken 12050 times.
✓ Branch 1 taken 314 times.
✓ Branch 2 taken 5918 times.
✓ Branch 3 taken 6132 times.
12364 if(!new_frame_start || pc->index+i<=0){
68 6232 s->remaining_size += i;
69 6232 goto get_next;
70 }
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6132 times.
6132 else if (i < 0) {
72 s->remaining_size += i;
73 }
74 }
75 }
76 }
77
78
2/2
✓ Branch 1 taken 6236 times.
✓ Branch 2 taken 12138 times.
18374 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
79 6236 s->remaining_size -= FFMIN(s->remaining_size, buf_size);
80 6236 *poutbuf = NULL;
81 6236 *poutbuf_size = 0;
82 6236 return buf_size;
83 }
84 }
85
86 12150 *poutbuf = buf;
87 12150 *poutbuf_size = buf_size;
88
89
2/2
✓ Branch 0 taken 11999 times.
✓ Branch 1 taken 151 times.
12150 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 6144 times.
✓ Branch 1 taken 5855 times.
11999 if (avctx->codec_id != AV_CODEC_ID_AAC) {
98 6144 AC3HeaderInfo hdr, *phrd = &hdr;
99 6144 int offset = ff_ac3_find_syncword(buf, buf_size);
100
101
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6138 times.
6144 if (offset < 0)
102 9 return i;
103
104 6138 buf += offset;
105 6138 buf_size -= offset;
106
1/2
✓ Branch 0 taken 6451 times.
✗ Branch 1 not taken.
6451 while (buf_size > 0) {
107 6451 int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
108
109
4/4
✓ Branch 0 taken 6450 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6448 times.
6451 if (ret < 0 || hdr.frame_size > buf_size)
110 3 return i;
111
112
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6135 times.
6448 if (buf_size > hdr.frame_size) {
113 313 buf += hdr.frame_size;
114 313 buf_size -= hdr.frame_size;
115 313 continue;
116 }
117 /* Check for false positives since the syncword is not enough.
118 See section 6.1.2 of A/52. */
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6135 times.
6135 if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
120 return i;
121 6135 break;
122 }
123
124 6135 avctx->sample_rate = hdr.sample_rate;
125
126
2/2
✓ Branch 0 taken 4592 times.
✓ Branch 1 taken 1543 times.
6135 if (hdr.bitstream_id > 10)
127 4592 avctx->codec_id = AV_CODEC_ID_EAC3;
128
129
2/2
✓ Branch 0 taken 1543 times.
✓ Branch 1 taken 4592 times.
6135 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
130 1543 av_channel_layout_uninit(&avctx->ch_layout);
131
1/2
✓ Branch 0 taken 1543 times.
✗ Branch 1 not taken.
1543 if (hdr.channel_layout) {
132 1543 av_channel_layout_from_mask(&avctx->ch_layout, hdr.channel_layout);
133 } else {
134 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
135 avctx->ch_layout.nb_channels = hdr.channels;
136 }
137 #if FF_API_OLD_CHANNEL_LAYOUT
138 FF_DISABLE_DEPRECATION_WARNINGS
139 1543 avctx->channels = avctx->ch_layout.nb_channels;
140 1543 avctx->channel_layout = hdr.channel_layout;
141 FF_ENABLE_DEPRECATION_WARNINGS
142 #endif
143 }
144 6135 s1->duration = hdr.num_blocks * 256;
145 6135 avctx->audio_service_type = hdr.bitstream_mode;
146
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6135 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6135 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
147 avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
148 6135 bit_rate = hdr.bit_rate;
149 } else {
150 5855 AACADTSHeaderInfo hdr, *phrd = &hdr;
151 5855 int ret = avpriv_adts_header_parse(&phrd, buf, buf_size);
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5855 times.
5855 if (ret < 0)
154 return i;
155
156 5855 bit_rate = hdr.bit_rate;
157 }
158
159 /* Calculate the average bit rate */
160 11990 s->frame_number++;
161
2/2
✓ Branch 0 taken 7398 times.
✓ Branch 1 taken 4592 times.
11990 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
162 7398 avctx->bit_rate +=
163 7398 (bit_rate - avctx->bit_rate) / s->frame_number;
164 }
165 }
166
167 12141 return i;
168 }
169