FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac_ac3_parser.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 72 78 92.3%
Functions: 1 1 100.0%
Branches: 45 54 83.3%

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 18400 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 18400 AACAC3ParseContext *s = s1->priv_data;
38 18400 ParseContext *pc = &s->pc;
39 int len, i;
40 int new_frame_start;
41 18400 int got_frame = 0;
42
43
2/2
✓ Branch 0 taken 18388 times.
✓ Branch 1 taken 12 times.
18400 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
44 12 i = buf_size;
45 12 got_frame = 1;
46 } else {
47 18388 get_next:
48 24620 i=END_NOT_FOUND;
49
2/2
✓ Branch 0 taken 20616 times.
✓ Branch 1 taken 4004 times.
24620 if(s->remaining_size <= buf_size){
50
4/4
✓ Branch 0 taken 12334 times.
✓ Branch 1 taken 8282 times.
✓ Branch 2 taken 5855 times.
✓ Branch 3 taken 6479 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 88631 times.
✓ Branch 1 taken 2390 times.
91021 for(i=s->remaining_size; i<buf_size; i++){
56 88631 s->state = (s->state<<8) + buf[i];
57
2/2
✓ Branch 1 taken 12371 times.
✓ Branch 2 taken 76260 times.
88631 if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
58 12371 break;
59 }
60
2/2
✓ Branch 0 taken 2390 times.
✓ Branch 1 taken 12371 times.
14761 if(len<=0){
61 2390 i=END_NOT_FOUND;
62 }else{
63 12371 got_frame = 1;
64 12371 s->state=0;
65 12371 i-= s->header_size -1;
66 12371 s->remaining_size = len;
67
4/4
✓ Branch 0 taken 12057 times.
✓ Branch 1 taken 314 times.
✓ Branch 2 taken 5918 times.
✓ Branch 3 taken 6139 times.
12371 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 6139 times.
6139 else if (i < 0) {
72 s->remaining_size += i;
73 }
74 }
75 }
76 }
77
78
2/2
✓ Branch 1 taken 6243 times.
✓ Branch 2 taken 12145 times.
18388 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
79 6243 s->remaining_size -= FFMIN(s->remaining_size, buf_size);
80 6243 *poutbuf = NULL;
81 6243 *poutbuf_size = 0;
82 6243 return buf_size;
83 }
84 }
85
86 12157 *poutbuf = buf;
87 12157 *poutbuf_size = buf_size;
88
89
2/2
✓ Branch 0 taken 12006 times.
✓ Branch 1 taken 151 times.
12157 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 6151 times.
✓ Branch 1 taken 5855 times.
12006 if (avctx->codec_id != AV_CODEC_ID_AAC) {
98 #if CONFIG_AC3_PARSER
99 6151 AC3HeaderInfo hdr, *phrd = &hdr;
100 6151 int offset = ff_ac3_find_syncword(buf, buf_size);
101
102
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6145 times.
6151 if (offset < 0)
103 9 return i;
104
105 6145 buf += offset;
106 6145 buf_size -= offset;
107
1/2
✓ Branch 0 taken 6458 times.
✗ Branch 1 not taken.
6458 while (buf_size > 0) {
108 6458 int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
109
110
4/4
✓ Branch 0 taken 6457 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6455 times.
6458 if (ret < 0 || hdr.frame_size > buf_size)
111 3 return i;
112
113
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6142 times.
6455 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 6142 times.
6142 if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
121 return i;
122 6142 break;
123 }
124
125 6142 avctx->sample_rate = hdr.sample_rate;
126
127
2/2
✓ Branch 0 taken 4592 times.
✓ Branch 1 taken 1550 times.
6142 if (hdr.bitstream_id > 10)
128 4592 avctx->codec_id = AV_CODEC_ID_EAC3;
129
130
2/2
✓ Branch 0 taken 1550 times.
✓ Branch 1 taken 4592 times.
6142 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
131 1550 av_channel_layout_uninit(&avctx->ch_layout);
132
1/2
✓ Branch 0 taken 1550 times.
✗ Branch 1 not taken.
1550 if (hdr.channel_layout) {
133 1550 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 6142 s1->duration = hdr.num_blocks * 256;
140 6142 avctx->audio_service_type = hdr.bitstream_mode;
141
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6142 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6142 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
142 avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
143 6142 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 11997 s->frame_number++;
158
2/2
✓ Branch 0 taken 7405 times.
✓ Branch 1 taken 4592 times.
11997 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
159 7405 avctx->bit_rate +=
160 7405 (bit_rate - avctx->bit_rate) / s->frame_number;
161 }
162 }
163
164 12148 return i;
165 }
166