FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac_ac3_parser.c
Date: 2022-12-09 07:38:14
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 16102 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 16102 AACAC3ParseContext *s = s1->priv_data;
38 16102 ParseContext *pc = &s->pc;
39 int len, i;
40 int new_frame_start;
41 16102 int got_frame = 0;
42
43
2/2
✓ Branch 0 taken 16090 times.
✓ Branch 1 taken 12 times.
16102 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
44 12 i = buf_size;
45 12 got_frame = 1;
46 } else {
47 16090 get_next:
48 22322 i=END_NOT_FOUND;
49
2/2
✓ Branch 0 taken 18643 times.
✓ Branch 1 taken 3679 times.
22322 if(s->remaining_size <= buf_size){
50
4/4
✓ Branch 0 taken 11025 times.
✓ Branch 1 taken 7618 times.
✓ Branch 2 taken 5855 times.
✓ Branch 3 taken 5170 times.
18643 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 12788 len=0;
55
2/2
✓ Branch 0 taken 78764 times.
✓ Branch 1 taken 1726 times.
80490 for(i=s->remaining_size; i<buf_size; i++){
56 78764 s->state = (s->state<<8) + buf[i];
57
2/2
✓ Branch 1 taken 11062 times.
✓ Branch 2 taken 67702 times.
78764 if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
58 11062 break;
59 }
60
2/2
✓ Branch 0 taken 1726 times.
✓ Branch 1 taken 11062 times.
12788 if(len<=0){
61 1726 i=END_NOT_FOUND;
62 }else{
63 11062 got_frame = 1;
64 11062 s->state=0;
65 11062 i-= s->header_size -1;
66 11062 s->remaining_size = len;
67
4/4
✓ Branch 0 taken 10748 times.
✓ Branch 1 taken 314 times.
✓ Branch 2 taken 5918 times.
✓ Branch 3 taken 4830 times.
11062 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 4830 times.
4830 else if (i < 0) {
72 s->remaining_size += i;
73 }
74 }
75 }
76 }
77
78
2/2
✓ Branch 1 taken 5258 times.
✓ Branch 2 taken 10832 times.
16090 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){
79 5258 s->remaining_size -= FFMIN(s->remaining_size, buf_size);
80 5258 *poutbuf = NULL;
81 5258 *poutbuf_size = 0;
82 5258 return buf_size;
83 }
84 }
85
86 10844 *poutbuf = buf;
87 10844 *poutbuf_size = buf_size;
88
89
2/2
✓ Branch 0 taken 10697 times.
✓ Branch 1 taken 147 times.
10844 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 4842 times.
✓ Branch 1 taken 5855 times.
10697 if (avctx->codec_id != AV_CODEC_ID_AAC) {
98 4842 AC3HeaderInfo hdr, *phrd = &hdr;
99 4842 int offset = ff_ac3_find_syncword(buf, buf_size);
100
101
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4838 times.
4842 if (offset < 0)
102 7 return i;
103
104 4838 buf += offset;
105 4838 buf_size -= offset;
106
1/2
✓ Branch 0 taken 5151 times.
✗ Branch 1 not taken.
5151 while (buf_size > 0) {
107 5151 int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
108
109
4/4
✓ Branch 0 taken 5150 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5148 times.
5151 if (ret < 0 || hdr.frame_size > buf_size)
110 3 return i;
111
112
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 4835 times.
5148 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 4835 times.
4835 if (av_crc(s->crc_ctx, 0, buf + 2, hdr.frame_size - 2))
120 return i;
121 4835 break;
122 }
123
124 4835 avctx->sample_rate = hdr.sample_rate;
125
126
2/2
✓ Branch 0 taken 3292 times.
✓ Branch 1 taken 1543 times.
4835 if (hdr.bitstream_id > 10)
127 3292 avctx->codec_id = AV_CODEC_ID_EAC3;
128
129
2/2
✓ Branch 0 taken 1543 times.
✓ Branch 1 taken 3292 times.
4835 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 4835 s1->duration = hdr.num_blocks * 256;
145 4835 avctx->audio_service_type = hdr.bitstream_mode;
146
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4835 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4835 if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
147 avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
148 4835 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 10690 s->frame_number++;
161
2/2
✓ Branch 0 taken 7398 times.
✓ Branch 1 taken 3292 times.
10690 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 10837 return i;
168 }
169