FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/opus_parser.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 68 89 76.4%
Functions: 3 3 100.0%
Branches: 40 58 69.0%

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 "avcodec.h"
29 #include "bytestream.h"
30 #include "opus.h"
31 #include "opus_parse.h"
32 #include "parser.h"
33
34 typedef struct OpusParserContext {
35 ParseContext pc;
36 OpusParseContext ctx;
37 OpusPacket pkt;
38 int extradata_parsed;
39 int ts_framing;
40 } OpusParserContext;
41
42 765 static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
43 {
44 765 const uint8_t *buf = start + 1;
45 int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length;
46 uint8_t flags;
47 uint64_t payload_len_tmp;
48
49 GetByteContext gb;
50 765 bytestream2_init(&gb, buf, buf_len);
51
52 765 flags = bytestream2_get_byte(&gb);
53 765 start_trim_flag = (flags >> 4) & 1;
54 765 end_trim_flag = (flags >> 3) & 1;
55 765 control_extension_flag = (flags >> 2) & 1;
56
57 765 payload_len_tmp = *payload_len = 0;
58
2/2
✓ Branch 1 taken 2164 times.
✓ Branch 2 taken 765 times.
2929 while (bytestream2_peek_byte(&gb) == 0xff)
59 2164 payload_len_tmp += bytestream2_get_byte(&gb);
60
61 765 payload_len_tmp += bytestream2_get_byte(&gb);
62
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 if (start_trim_flag)
64 bytestream2_skip(&gb, 2);
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 if (end_trim_flag)
66 bytestream2_skip(&gb, 2);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 if (control_extension_flag) {
68 control_extension_length = bytestream2_get_byte(&gb);
69 bytestream2_skip(&gb, control_extension_length);
70 }
71
72
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 765 times.
765 if (bytestream2_tell(&gb) + payload_len_tmp > buf_len)
73 return NULL;
74
75 765 *payload_len = payload_len_tmp;
76
77 765 return buf + bytestream2_tell(&gb);
78 }
79
80 /**
81 * Find the end of the current frame in the bitstream.
82 * @return the position of the first byte of the next frame, or -1
83 */
84 22878 static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx,
85 const uint8_t *buf, int buf_size, int *header_len)
86 {
87 22878 OpusParserContext *s = ctx->priv_data;
88 22878 ParseContext *pc = &s->pc;
89 22878 int ret, start_found, i = 0, payload_len = 0;
90 const uint8_t *payload;
91 uint32_t state;
92 uint16_t hdr;
93 22878 *header_len = 0;
94
95
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 22855 times.
22878 if (!buf_size)
96 23 return 0;
97
98 22855 start_found = pc->frame_start_found;
99 22855 state = pc->state;
100 22855 payload = buf;
101
102 /* Check if we're using Opus in MPEG-TS framing */
103
3/4
✓ Branch 0 taken 22092 times.
✓ Branch 1 taken 763 times.
✓ Branch 2 taken 22092 times.
✗ Branch 3 not taken.
22855 if (!s->ts_framing && buf_size > 2) {
104 22092 hdr = AV_RB16(buf);
105
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22090 times.
22092 if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER)
106 2 s->ts_framing = 1;
107 }
108
109
3/4
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 22090 times.
✓ Branch 2 taken 765 times.
✗ Branch 3 not taken.
22855 if (s->ts_framing && !start_found) {
110
1/2
✓ Branch 0 taken 1530 times.
✗ Branch 1 not taken.
1530 for (i = 0; i < buf_size-2; i++) {
111 1530 state = (state << 8) | payload[i];
112
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 765 times.
1530 if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) {
113 765 payload = parse_opus_ts_header(payload, &payload_len, buf_size - i);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 if (!payload) {
115 av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg TS header.\n");
116 return AVERROR_INVALIDDATA;
117 }
118 765 *header_len = payload - buf;
119 765 start_found = 1;
120 765 break;
121 }
122 }
123 }
124
125
2/2
✓ Branch 0 taken 22090 times.
✓ Branch 1 taken 765 times.
22855 if (!s->ts_framing)
126 22090 payload_len = buf_size;
127
128
3/4
✓ Branch 0 taken 22855 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 22807 times.
22855 if (avctx->extradata && !s->extradata_parsed) {
129 48 ret = ff_opus_parse_extradata(avctx, &s->ctx);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (ret < 0) {
131 av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
132 return AVERROR_INVALIDDATA;
133 }
134 48 av_freep(&s->ctx.channel_maps);
135 48 s->extradata_parsed = 1;
136 }
137
138
4/6
✓ Branch 0 taken 22855 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 765 times.
✓ Branch 3 taken 22090 times.
✓ Branch 4 taken 765 times.
✗ Branch 5 not taken.
22855 if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
139 22855 ret = ff_opus_parse_packet(&s->pkt, payload, payload_len, s->ctx.nb_streams > 1);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22855 times.
22855 if (ret < 0) {
141 av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
142 pc->frame_start_found = 0;
143 return AVERROR_INVALIDDATA;
144 }
145
146 22855 ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
147 }
148
149
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 22090 times.
22855 if (s->ts_framing) {
150
1/2
✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
765 if (start_found) {
151
1/2
✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
765 if (payload_len + *header_len <= buf_size) {
152 765 pc->frame_start_found = 0;
153 765 pc->state = -1;
154 765 return payload_len + *header_len;
155 }
156 }
157
158 pc->frame_start_found = start_found;
159 pc->state = state;
160 return END_NOT_FOUND;
161 }
162
163 22090 return buf_size;
164 }
165
166 22878 static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx,
167 const uint8_t **poutbuf, int *poutbuf_size,
168 const uint8_t *buf, int buf_size)
169 {
170 22878 OpusParserContext *s = ctx->priv_data;
171 22878 ParseContext *pc = &s->pc;
172 int next, header_len;
173
174 22878 next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
175
176
4/6
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 22112 times.
✓ Branch 2 taken 766 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 766 times.
23644 if (s->ts_framing && next != AVERROR_INVALIDDATA &&
177 766 ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
178 *poutbuf = NULL;
179 *poutbuf_size = 0;
180 return buf_size;
181 }
182
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22878 times.
22878 if (next == AVERROR_INVALIDDATA){
184 *poutbuf = NULL;
185 *poutbuf_size = 0;
186 return buf_size;
187 }
188
189 22878 *poutbuf = buf + header_len;
190 22878 *poutbuf_size = buf_size - header_len;
191 22878 return next;
192 }
193
194 const AVCodecParser ff_opus_parser = {
195 .codec_ids = { AV_CODEC_ID_OPUS },
196 .priv_data_size = sizeof(OpusParserContext),
197 .parser_parse = opus_parse,
198 .parser_close = ff_parse_close
199 };
200