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