FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sbc_parser.c
Date: 2026-04-20 20:24:43
Exec Total Coverage
Lines: 46 55 83.6%
Functions: 2 2 100.0%
Branches: 18 26 69.2%

Line Branch Exec Source
1 /*
2 * SBC parser
3 *
4 * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
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 "sbc.h"
24 #include "parser.h"
25 #include "parser_internal.h"
26
27 typedef struct SBCParseContext {
28 ParseContext pc;
29 uint8_t header[3];
30 int header_size;
31 int buffered_size;
32 } SBCParseContext;
33
34 34538 static int sbc_parse_header(AVCodecParserContext *s, AVCodecContext *avctx,
35 const uint8_t *data, size_t len)
36 {
37 static const int sample_rates[4] = { 16000, 32000, 44100, 48000 };
38 int sr, blocks, mode, subbands, bitpool, channels, joint;
39 int length;
40
41
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 34478 times.
34538 if (len < 3)
42 60 return -1;
43
44
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 34478 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
34478 if (data[0] == MSBC_SYNCWORD && data[1] == 0 && data[2] == 0) {
45 av_channel_layout_uninit(&avctx->ch_layout);
46 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
47 avctx->ch_layout.nb_channels = 1;
48 avctx->sample_rate = 16000;
49 avctx->frame_size = 120;
50 s->duration = avctx->frame_size;
51 return 57;
52 }
53
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34478 times.
34478 if (data[0] != SBC_SYNCWORD)
55 return -2;
56
57 34478 sr = (data[1] >> 6) & 0x03;
58 34478 blocks = (((data[1] >> 4) & 0x03) + 1) << 2;
59 34478 mode = (data[1] >> 2) & 0x03;
60 34478 subbands = (((data[1] >> 0) & 0x01) + 1) << 2;
61 34478 bitpool = data[2];
62
63
2/2
✓ Branch 0 taken 21423 times.
✓ Branch 1 taken 13055 times.
34478 channels = mode == SBC_MODE_MONO ? 1 : 2;
64 34478 joint = mode == SBC_MODE_JOINT_STEREO;
65
66 68956 length = 4 + (subbands * channels) / 2
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34478 times.
34478 + ((((mode == SBC_MODE_DUAL_CHANNEL) + 1) * blocks * bitpool
68 34478 + (joint * subbands)) + 7) / 8;
69
70 34478 av_channel_layout_uninit(&avctx->ch_layout);
71 34478 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
72 34478 avctx->ch_layout.nb_channels = channels;
73 34478 avctx->sample_rate = sample_rates[sr];
74 34478 avctx->frame_size = subbands * blocks;
75 34478 s->duration = avctx->frame_size;
76 34478 return length;
77 }
78
79 34538 static int sbc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
80 const uint8_t **poutbuf, int *poutbuf_size,
81 const uint8_t *buf, int buf_size)
82 {
83 34538 SBCParseContext *pc = s->priv_data;
84 int next;
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34538 times.
34538 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
87 next = buf_size;
88 } else {
89
2/2
✓ Branch 0 taken 1312 times.
✓ Branch 1 taken 33226 times.
34538 if (pc->header_size) {
90 1312 memcpy(pc->header + pc->header_size, buf,
91 1312 sizeof(pc->header) - pc->header_size);
92 1312 next = sbc_parse_header(s, avctx, pc->header, sizeof(pc->header))
93 1312 - pc->buffered_size;
94 1312 pc->header_size = 0;
95 } else {
96 33226 next = sbc_parse_header(s, avctx, buf, buf_size);
97
2/2
✓ Branch 0 taken 1259 times.
✓ Branch 1 taken 31967 times.
33226 if (next >= buf_size)
98 1259 next = -1;
99 }
100
101
2/2
✓ Branch 0 taken 1319 times.
✓ Branch 1 taken 33219 times.
34538 if (next < 0) {
102
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1259 times.
1319 pc->header_size = FFMIN(sizeof(pc->header), buf_size);
103 1319 memcpy(pc->header, buf, pc->header_size);
104 1319 pc->buffered_size = buf_size;
105 1319 next = END_NOT_FOUND;
106 }
107
108
2/2
✓ Branch 1 taken 1312 times.
✓ Branch 2 taken 33226 times.
34538 if (ff_combine_frame(&pc->pc, next, &buf, &buf_size) < 0) {
109 1312 *poutbuf = NULL;
110 1312 *poutbuf_size = 0;
111 1312 return buf_size;
112 }
113 }
114
115 33226 *poutbuf = buf;
116 33226 *poutbuf_size = buf_size;
117 33226 return next;
118 }
119
120 const FFCodecParser ff_sbc_parser = {
121 PARSER_CODEC_LIST(AV_CODEC_ID_SBC),
122 .priv_data_size = sizeof(SBCParseContext),
123 .parse = sbc_parse,
124 .close = ff_parse_close,
125 };
126