FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac_ac3_parser.c
Date: 2024-09-07 18:49:03
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 18390 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 18390 AACAC3ParseContext *s = s1->priv_data;
38 18390 ParseContext *pc = &s->pc;
39 int len, i;
40 int new_frame_start;
41 18390 int got_frame = 0;
42
43
2/2
✓ Branch 0 taken 18378 times.
✓ Branch 1 taken 12 times.
18390 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
44 12 i = buf_size;
45 12 got_frame = 1;
46 } else {
47 18378 get_next:
48 24610 i=END_NOT_FOUND;
49
2/2
✓ Branch 0 taken 20606 times.
✓ Branch 1 taken 4004 times.
24610 if(s->remaining_size <= buf_size){
50
4/4
✓ Branch 0 taken 12329 times.
✓ Branch 1 taken 8277 times.
✓ Branch 2 taken 5855 times.
✓ Branch 3 taken 6474 times.
20606 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 14751 len=0;
55
2/2
✓ Branch 0 taken 88596 times.
✓ Branch 1 taken 2385 times.
90981 for(i=s->remaining_size; i<buf_size; i++){
56 88596 s->state = (s->state<<8) + buf[i];
57
2/2
✓ Branch 1 taken 12366 times.
✓ Branch 2 taken 76230 times.
88596 if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
58 12366 break;
59 }
60
2/2
✓ Branch 0 taken 2385 times.
✓ Branch 1 taken 12366 times.
14751 if(len<=0){
61 2385 i=END_NOT_FOUND;
62 }else{
63 12366 got_frame = 1;
64 12366 s->state=0;
65 12366 i-= s->header_size -1;
66 12366 s->remaining_size = len;
67
4/4
✓ Branch 0 taken 12052 times.
✓ Branch 1 taken 314 times.
✓ Branch 2 taken 5918 times.
✓ Branch 3 taken 6134 times.
12366 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 6134 times.
6134 else if (i < 0) {
72 s->remaining_size += i;
73 }
74 }
75 }
76 }
77
78
2/2
✓ Branch 1 taken 6238 times.
✓ Branch 2 taken 12140 times.
18378 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
79 6238 s->remaining_size -= FFMIN(s->remaining_size, buf_size);
80 6238 *poutbuf = NULL;
81 6238 *poutbuf_size = 0;
82 6238 return buf_size;
83 }
84 }
85
86 12152 *poutbuf = buf;
87 12152 *poutbuf_size = buf_size;
88
89
2/2
✓ Branch 0 taken 12001 times.
✓ Branch 1 taken 151 times.
12152 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 6146 times.
✓ Branch 1 taken 5855 times.
12001 if (avctx->codec_id != AV_CODEC_ID_AAC) {
98 #if CONFIG_AC3_PARSER
99 6146 AC3HeaderInfo hdr, *phrd = &hdr;
100 6146 int offset = ff_ac3_find_syncword(buf, buf_size);
101
102
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6140 times.
6146 if (offset < 0)
103 9 return i;
104
105 6140 buf += offset;
106 6140 buf_size -= offset;
107
1/2
✓ Branch 0 taken 6453 times.
✗ Branch 1 not taken.
6453 while (buf_size > 0) {
108 6453 int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
109
110
4/4
✓ Branch 0 taken 6452 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6450 times.
6453 if (ret < 0 || hdr.frame_size > buf_size)
111 3 return i;
112
113
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 6137 times.
6450 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 6137 times.
6137 if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
121 return i;
122 6137 break;
123 }
124
125 6137 avctx->sample_rate = hdr.sample_rate;
126
127
2/2
✓ Branch 0 taken 4592 times.
✓ Branch 1 taken 1545 times.
6137 if (hdr.bitstream_id > 10)
128 4592 avctx->codec_id = AV_CODEC_ID_EAC3;
129
130
2/2
✓ Branch 0 taken 1545 times.
✓ Branch 1 taken 4592 times.
6137 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
131 1545 av_channel_layout_uninit(&avctx->ch_layout);
132
1/2
✓ Branch 0 taken 1545 times.
✗ Branch 1 not taken.
1545 if (hdr.channel_layout) {
133 1545 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 6137 s1->duration = hdr.num_blocks * 256;
140 6137 avctx->audio_service_type = hdr.bitstream_mode;
141
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6137 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6137 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
142 avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
143 6137 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 11992 s->frame_number++;
158
2/2
✓ Branch 0 taken 7400 times.
✓ Branch 1 taken 4592 times.
11992 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
159 7400 avctx->bit_rate +=
160 7400 (bit_rate - avctx->bit_rate) / s->frame_number;
161 }
162 }
163
164 12143 return i;
165 }
166