FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus/parser.c
Date: 2026-11-04 10:59:43
Exec Total Coverage
Lines: 74 99 74.7%
Functions: 4 4 100.0%
Branches: 40 66 60.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013-2014 Mozilla Corporation
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Opus parser
24 *
25 * Determines the duration for each packet.
26 */
27
28 #include "libavutil/mem.h"
29
30 #include "libavcodec/avcodec.h"
31 #include "libavcodec/bytestream.h"
32 #include "libavcodec/parser.h"
33 #include "libavcodec/parser_internal.h"
34
35 #include "opus.h"
36 #include "parse.h"
37
38 typedef struct OpusParserContext {
39 ParseContext pc;
40 OpusParseContext ctx;
41 OpusPacket pkt;
42 int extradata_parsed;
43 int ts_framing;
44 } OpusParserContext;
45
46 1825 static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
47 {
48 1825 const uint8_t *buf = start + 1;
49 int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length;
50 uint8_t flags;
51 uint64_t payload_len_tmp;
52
53 GetByteContext gb;
54 1825 bytestream2_init(&gb, buf, buf_len);
55
56 1825 flags = bytestream2_get_byte(&gb);
57 1825 start_trim_flag = (flags >> 4) & 1;
58 1825 end_trim_flag = (flags >> 3) & 1;
59 1825 control_extension_flag = (flags >> 2) & 1;
60
61 1825 payload_len_tmp = *payload_len = 0;
62
2/2
✓ Branch 1 taken 5263 times.
✓ Branch 2 taken 1825 times.
7088 while (bytestream2_peek_byte(&gb) == 0xff)
63 5263 payload_len_tmp += bytestream2_get_byte(&gb);
64
65 1825 payload_len_tmp += bytestream2_get_byte(&gb);
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1825 times.
1825 if (start_trim_flag)
68 ✗ bytestream2_skip(&gb, 2);
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1825 times.
1825 if (end_trim_flag)
70 ✗ bytestream2_skip(&gb, 2);
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1825 times.
1825 if (control_extension_flag) {
72 ✗ control_extension_length = bytestream2_get_byte(&gb);
73 ✗ bytestream2_skip(&gb, control_extension_length);
74 }
75
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1825 times.
1825 if (bytestream2_tell(&gb) + payload_len_tmp > buf_len)
77 ✗ return NULL;
78
79 1825 *payload_len = payload_len_tmp;
80
81 1825 return buf + bytestream2_tell(&gb);
82 }
83
84 24107 static int set_frame_duration(AVCodecParserContext *ctx, AVCodecContext *avctx,
85 const uint8_t *buf, int buf_size)
86 {
87 24107 OpusParserContext *s = ctx->priv_data;
88
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24107 times.
24107 if (ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1) < 0) {
90 ✗ av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
91 ✗ return AVERROR_INVALIDDATA;
92 }
93
94 24107 ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
95
96 24107 return 0;
97 }
98
99 /**
100 * Find the end of the current frame in the bitstream.
101 * @return the position of the first byte of the next frame, or -1
102 */
103 1826 static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx,
104 const uint8_t *buf, int buf_size, int *header_len)
105 {
106 1826 OpusParserContext *s = ctx->priv_data;
107 1826 ParseContext *pc = &s->pc;
108 1826 int ret, start_found, i = 0, payload_len = 0;
109 const uint8_t *payload;
110 uint32_t state;
111 uint16_t hdr;
112 1826 *header_len = 0;
113
114
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1825 times.
1826 if (!buf_size)
115 1 return 0;
116
117 1825 start_found = pc->frame_start_found;
118 1825 state = pc->state;
119 1825 payload = buf;
120
121 /* Check if we're using Opus in MPEG-TS framing */
122
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1801 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
1825 if (!s->ts_framing && buf_size > 2) {
123 24 hdr = AV_RB16(buf);
124
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER)
125 24 s->ts_framing = 1;
126 }
127
128
2/4
✓ Branch 0 taken 1825 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1825 times.
✗ Branch 3 not taken.
1825 if (s->ts_framing && !start_found) {
129
1/2
✓ Branch 0 taken 3650 times.
✗ Branch 1 not taken.
3650 for (i = 0; i < buf_size-2; i++) {
130 3650 state = (state << 8) | payload[i];
131
2/2
✓ Branch 0 taken 1825 times.
✓ Branch 1 taken 1825 times.
3650 if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) {
132 1825 payload = parse_opus_ts_header(payload, &payload_len, buf_size - i);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1825 times.
1825 if (!payload) {
134 ✗ av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg TS header.\n");
135 ✗ return AVERROR_INVALIDDATA;
136 }
137 1825 *header_len = payload - buf;
138 1825 start_found = 1;
139 1825 break;
140 }
141 }
142 }
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1825 times.
1825 if (!s->ts_framing)
145 ✗ payload_len = buf_size;
146
147
3/6
✓ Branch 0 taken 1825 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1825 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1825 times.
✗ Branch 5 not taken.
1825 if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
148 1825 ret = set_frame_duration(ctx, avctx, payload, payload_len);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1825 times.
1825 if (ret < 0) {
150 ✗ pc->frame_start_found = 0;
151 ✗ return AVERROR_INVALIDDATA;
152 }
153 }
154
155
1/2
✓ Branch 0 taken 1825 times.
✗ Branch 1 not taken.
1825 if (s->ts_framing) {
156
1/2
✓ Branch 0 taken 1825 times.
✗ Branch 1 not taken.
1825 if (start_found) {
157
1/2
✓ Branch 0 taken 1825 times.
✗ Branch 1 not taken.
1825 if (payload_len + *header_len <= buf_size) {
158 1825 pc->frame_start_found = 0;
159 1825 pc->state = -1;
160 1825 return payload_len + *header_len;
161 }
162 }
163
164 ✗ pc->frame_start_found = start_found;
165 ✗ pc->state = state;
166 ✗ return END_NOT_FOUND;
167 }
168
169 ✗ return buf_size;
170 }
171
172 24133 static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx,
173 const uint8_t **poutbuf, int *poutbuf_size,
174 const uint8_t *buf, int buf_size)
175 {
176 24133 OpusParserContext *s = ctx->priv_data;
177 24133 ParseContext *pc = &s->pc;
178 24133 int next, header_len = 0;
179
180 24133 avctx->sample_rate = 48000;
181
182
3/4
✓ Branch 0 taken 24133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✓ Branch 3 taken 24052 times.
24133 if (avctx->extradata && !s->extradata_parsed) {
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
81 if (ff_opus_parse_extradata(avctx, &s->ctx) < 0) {
184 ✗ av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
185 ✗ goto fail;
186 }
187 81 av_freep(&s->ctx.channel_maps);
188 81 s->extradata_parsed = 1;
189 }
190
191
2/2
✓ Branch 0 taken 22307 times.
✓ Branch 1 taken 1826 times.
24133 if (ctx->flags & PARSER_FLAG_COMPLETE_FRAMES) {
192 22307 next = buf_size;
193
194
3/4
✓ Branch 0 taken 22282 times.
✓ Branch 1 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22282 times.
22307 if (buf_size && set_frame_duration(ctx, avctx, buf, buf_size) < 0)
195 ✗ goto fail;
196 } else {
197 1826 next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
198
199
3/6
✓ Branch 0 taken 1826 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1826 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1826 times.
3652 if (s->ts_framing && next != AVERROR_INVALIDDATA &&
200 1826 ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
201 ✗ goto fail;
202 }
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1826 times.
1826 if (next == AVERROR_INVALIDDATA){
205 ✗ goto fail;
206 }
207 }
208
209 24133 *poutbuf = buf + header_len;
210 24133 *poutbuf_size = buf_size - header_len;
211 24133 return next;
212
213 ✗ fail:
214 ✗ *poutbuf = NULL;
215 ✗ *poutbuf_size = 0;
216 ✗ return buf_size;
217 }
218
219 const FFCodecParser ff_opus_parser = {
220 PARSER_CODEC_LIST(AV_CODEC_ID_OPUS),
221 .priv_data_size = sizeof(OpusParserContext),
222 .parse = opus_parse,
223 .close = ff_parse_close
224 };
225