FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/usmdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 3 256 1.2%
Functions: 1 6 16.7%
Branches: 1 146 0.7%

Line Branch Exec Source
1 /*
2 * USM demuxer
3 * Copyright (c) 2023 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/mem.h"
24 #include "libavcodec/bytestream.h"
25
26 #include "avformat.h"
27 #include "demux.h"
28 #include "internal.h"
29
30 #define VIDEOI 0
31 #define AUDIOI 1
32 #define ALPHAI 2
33 #define SUBTTI 3
34
35 typedef struct USMChannel {
36 int index;
37 int used;
38 int type;
39 int codec_id;
40 int nb_channels;
41 int nb_frames;
42 AVRational rate;
43 int width, height;
44 int64_t duration;
45 int64_t extradata_pos;
46 } USMChannel;
47
48 typedef struct USMDemuxContext {
49 USMChannel ch[4][256];
50 int nb_channels[4];
51 uint8_t *header;
52 unsigned header_size;
53 } USMDemuxContext;
54
55 7186 static int usm_probe(const AVProbeData *p)
56 {
57
1/2
✓ Branch 0 taken 7186 times.
✗ Branch 1 not taken.
7186 if (AV_RL32(p->buf) != MKTAG('C','R','I','D'))
58 7186 return 0;
59
60 if (AV_RN32(p->buf + 4) == 0)
61 return 0;
62
63 return AVPROBE_SCORE_MAX / 3;
64 }
65
66 static int usm_read_header(AVFormatContext *s)
67 {
68 s->ctx_flags |= AVFMTCTX_NOHEADER;
69 return 0;
70 }
71
72 static int parse_utf(AVFormatContext *s, AVIOContext *pb,
73 USMChannel *ch, int ch_type,
74 uint32_t parent_chunk_size)
75 {
76 USMDemuxContext *usm = s->priv_data;
77 GetByteContext gb, ugb, sgb;
78 uint32_t chunk_type, chunk_size, offset;
79 uint32_t unique_offset, string_offset;
80 int nb_items, unique_size, nb_dictionaries;
81 AVRational fps = { 0 };
82 int type;
83
84 chunk_type = avio_rb32(pb);
85 chunk_size = avio_rb32(pb);
86
87 if (chunk_type != MKBETAG('@','U','T','F'))
88 return AVERROR_INVALIDDATA;
89
90 if (!chunk_size || chunk_size >= parent_chunk_size)
91 return AVERROR_INVALIDDATA;
92
93 av_fast_malloc(&usm->header, &usm->header_size, chunk_size);
94 if (!usm->header)
95 return AVERROR(ENOMEM);
96
97 if (avio_read(pb, usm->header, chunk_size) != chunk_size)
98 return AVERROR_EOF;
99
100 bytestream2_init(&gb, usm->header, chunk_size);
101 ugb = gb;
102 sgb = gb;
103 unique_offset = bytestream2_get_be32(&gb);
104 string_offset = bytestream2_get_be32(&gb);
105 /*byte_offset =*/ bytestream2_get_be32(&gb);
106 /*payload_name_offset =*/ bytestream2_get_be32(&gb);
107 nb_items = bytestream2_get_be16(&gb);
108 unique_size = bytestream2_get_be16(&gb);
109 nb_dictionaries = bytestream2_get_be32(&gb);
110 if (nb_dictionaries == 0)
111 return AVERROR_INVALIDDATA;
112
113 bytestream2_skip(&ugb, unique_offset);
114 if (bytestream2_get_bytes_left(&ugb) < unique_size)
115 return AVERROR_INVALIDDATA;
116 bytestream2_init(&ugb, ugb.buffer, unique_size);
117
118 bytestream2_skip(&sgb, string_offset);
119
120 for (int i = 0; i < nb_items; i++) {
121 GetByteContext *xgb;
122 uint8_t key[256];
123 int64_t value = -1;
124 int n = 0;
125
126 type = bytestream2_get_byte(&gb);
127 offset = bytestream2_get_be32(&gb);
128
129 bytestream2_seek(&sgb, string_offset + offset, SEEK_SET);
130 while (bytestream2_get_bytes_left(&sgb) > 0) {
131 key[n] = bytestream2_get_byte(&sgb);
132 if (!key[n])
133 break;
134 if (n >= sizeof(key) - 1)
135 break;
136 n++;
137 }
138 key[n] = '\0';
139
140 if ((type >> 5) == 1)
141 xgb = &gb;
142 else
143 xgb = &ugb;
144
145 switch (type & 0x1F) {
146 case 0x10:
147 case 0x11:
148 value = bytestream2_get_byte(xgb);
149 break;
150 case 0x12:
151 case 0x13:
152 value = bytestream2_get_be16(xgb);
153 break;
154 case 0x14:
155 case 0x15:
156 value = bytestream2_get_be32(xgb);
157 break;
158 case 0x16:
159 case 0x17:
160 value = bytestream2_get_be64(xgb);
161 break;
162 case 0x18:
163 value = av_int2float(bytestream2_get_be32(xgb));
164 break;
165 case 0x19:
166 value = av_int2double(bytestream2_get_be64(xgb));
167 break;
168 case 0x1A:
169 break;
170 }
171
172 if (ch_type == AUDIOI) {
173 if (!strcmp(key, "sampling_rate")) {
174 ch->rate.num = value;
175 ch->rate.den = 1;
176 } else if (!strcmp(key, "num_channels")) {
177 ch->nb_channels = value;
178 } else if (!strcmp(key, "total_samples")) {
179 ch->duration = value;
180 } else if (!strcmp(key, "audio_codec")) {
181 switch (value) {
182 case 2:
183 ch->codec_id = AV_CODEC_ID_ADPCM_ADX;
184 break;
185 case 4:
186 ch->codec_id = AV_CODEC_ID_HCA;
187 break;
188 default:
189 av_log(s, AV_LOG_ERROR, "unsupported audio: %d\n", (int)value);
190 break;
191 }
192 }
193 } else if (ch_type == VIDEOI || ch_type == ALPHAI) {
194 if (!strcmp(key, "width")) {
195 ch->width = value;
196 } else if (!strcmp(key, "height")) {
197 ch->height = value;
198 } else if (!strcmp(key, "total_frames")) {
199 ch->nb_frames = value;
200 } else if (!strcmp(key, "framerate_n")) {
201 fps.num = value;
202 } else if (!strcmp(key, "framerate_d")) {
203 fps.den = value;
204 } else if (!strcmp(key, "mpeg_codec")) {
205 switch (value) {
206 case 1:
207 ch->codec_id = AV_CODEC_ID_MPEG1VIDEO;
208 break;
209 case 5:
210 ch->codec_id = AV_CODEC_ID_H264;
211 break;
212 case 9:
213 ch->codec_id = AV_CODEC_ID_VP9;
214 break;
215 default:
216 av_log(s, AV_LOG_ERROR, "unsupported video: %d\n", (int)value);
217 break;
218 }
219 }
220 }
221 }
222
223 if (ch_type == VIDEOI && fps.num && fps.den)
224 ch->rate = fps;
225
226 return 0;
227 }
228
229 static int64_t parse_chunk(AVFormatContext *s, AVIOContext *pb,
230 uint32_t chunk_type, uint32_t chunk_size,
231 AVPacket *pkt)
232 {
233 const int is_audio = chunk_type == MKBETAG('@','S','F','A');
234 const int is_alpha = chunk_type == MKBETAG('@','A','L','P');
235 const int is_subtt = chunk_type == MKBETAG('@','S','B','T');
236 USMDemuxContext *usm = s->priv_data;
237 int padding_size, payload_type, payload_offset;
238 const int ch_type = is_subtt ? SUBTTI : is_audio ? AUDIOI : is_alpha ? ALPHAI : VIDEOI;
239 int stream_index, frame_rate;
240 int64_t chunk_start, ret;
241
242 ret = avio_tell(pb);
243 if (ret < 0)
244 return ret;
245 chunk_start = ret;
246 avio_skip(pb, 1);
247 payload_offset = avio_r8(pb);
248 padding_size = avio_rb16(pb);
249 stream_index = avio_r8(pb);
250 avio_skip(pb, 2);
251 payload_type = avio_r8(pb);
252 /*frame_time =*/ avio_rb32(pb);
253 frame_rate = avio_rb32(pb);
254 avio_skip(pb, 8);
255 ret = avio_tell(pb);
256 if (ret < 0)
257 return ret;
258 ret = avio_skip(pb, FFMAX(0, (ret - chunk_start) - payload_offset));
259 if (ret < 0)
260 return ret;
261
262 if (payload_type == 1) {
263 if (usm->ch[ch_type][stream_index].used == 0) {
264 USMChannel *ch = &usm->ch[ch_type][stream_index];
265
266 switch (ch_type) {
267 case ALPHAI:
268 case VIDEOI:
269 ch->type = AVMEDIA_TYPE_VIDEO;
270 break;
271 case AUDIOI:
272 ch->type = AVMEDIA_TYPE_AUDIO;
273 break;
274 case SUBTTI:
275 ch->type = AVMEDIA_TYPE_SUBTITLE;
276 break;
277 default:
278 return AVERROR_INVALIDDATA;
279 }
280
281 ch->used = 1;
282 ch->index = -1;
283 usm->nb_channels[ch_type]++;
284
285 ret = parse_utf(s, pb, ch, ch_type, chunk_size);
286 if (ret < 0)
287 return ret;
288 }
289 } else if (payload_type == 0) {
290 if (usm->ch[ch_type][stream_index].used == 1) {
291 USMChannel *ch = &usm->ch[ch_type][stream_index];
292 int get_extradata = 0;
293 uint32_t pkt_size;
294 AVStream *st;
295
296 if (ch->index < 0) {
297 AVCodecParameters *par;
298 st = avformat_new_stream(s, NULL);
299 if (!st)
300 return AVERROR(ENOMEM);
301 par = st->codecpar;
302 par->codec_type = ch->type;
303 par->codec_id = ch->codec_id;
304 st->start_time = 0;
305
306 switch (ch->type) {
307 case AVMEDIA_TYPE_VIDEO:
308 par->width = ch->width;
309 par->height = ch->height;
310 st->nb_frames = ch->nb_frames;
311 break;
312 case AVMEDIA_TYPE_AUDIO:
313 par->sample_rate = ch->rate.num;
314 par->ch_layout.nb_channels = ch->nb_channels;
315 st->duration = ch->duration;
316 break;
317 }
318
319 ch->index = st->index;
320 if (!ch->rate.num || !ch->rate.den)
321 ch->rate = av_make_q(frame_rate, 100);
322 avpriv_set_pts_info(st, 64, ch->rate.den, ch->rate.num);
323
324 ffstream(st)->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
325 get_extradata = ch->codec_id == AV_CODEC_ID_ADPCM_ADX;
326 ch->extradata_pos = avio_tell(pb);
327 }
328
329 ret = avio_tell(pb);
330 if (ret < 0)
331 return ret;
332
333 pkt_size = chunk_size - (ret - chunk_start) - padding_size;
334 if (get_extradata) {
335 if ((ret = ff_get_extradata(s, st->codecpar, pb, pkt_size)) < 0)
336 return ret;
337 } else {
338 if (ret == ch->extradata_pos && ch->codec_id == AV_CODEC_ID_ADPCM_ADX) {
339 avio_skip(pb, pkt_size);
340 ret = 0;
341 } else {
342 ret = av_get_packet(pb, pkt, pkt_size);
343 if (ret < 0)
344 return ret;
345
346 pkt->stream_index = ch->index;
347 }
348 }
349
350 avio_skip(pb, padding_size);
351
352 if (ret != pkt_size)
353 return AVERROR_EOF;
354 if (get_extradata == 0)
355 return ret;
356 }
357 }
358
359 ret = avio_tell(pb);
360 if (ret < 0)
361 return ret;
362 ret = avio_skip(pb, FFMAX(0, chunk_size - (ret - chunk_start)));
363 if (ret < 0)
364 return ret;
365 return FFERROR_REDO;
366 }
367
368 static int usm_read_packet(AVFormatContext *s, AVPacket *pkt)
369 {
370 AVIOContext *pb = s->pb;
371 int64_t ret = AVERROR_EOF;
372
373 while (!avio_feof(pb)) {
374 uint32_t chunk_type, chunk_size;
375 int got_packet = 0;
376 int64_t pos;
377
378 pos = avio_tell(pb);
379 if (pos < 0)
380 return pos;
381 chunk_type = avio_rb32(pb);
382 chunk_size = avio_rb32(pb);
383 if (!chunk_size)
384 return AVERROR_INVALIDDATA;
385
386 switch (chunk_type) {
387 case MKBETAG('C','R','I','D'):
388 default:
389 ret = avio_skip(pb, chunk_size);
390 break;
391 case MKBETAG('@','A','L','P'):
392 case MKBETAG('@','S','B','T'):
393 case MKBETAG('@','S','F','A'):
394 case MKBETAG('@','S','F','V'):
395 ret = parse_chunk(s, pb, chunk_type, chunk_size, pkt);
396 got_packet = ret > 0;
397 break;
398 }
399
400 if (got_packet)
401 pkt->pos = pos;
402
403 if (got_packet || ret < 0)
404 break;
405 }
406
407 return ret;
408 }
409
410 static int usm_read_close(AVFormatContext *s)
411 {
412 USMDemuxContext *usm = s->priv_data;
413 av_freep(&usm->header);
414 usm->header_size = 0;
415 return 0;
416 }
417
418 const FFInputFormat ff_usm_demuxer = {
419 .p.name = "usm",
420 .p.long_name = NULL_IF_CONFIG_SMALL("CRI USM"),
421 .p.extensions = "usm",
422 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOBINSEARCH,
423 .priv_data_size = sizeof(USMDemuxContext),
424 .read_probe = usm_probe,
425 .read_header = usm_read_header,
426 .read_packet = usm_read_packet,
427 .read_close = usm_read_close,
428 };
429