/src/ffmpeg/libavformat/imfdec.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of FFmpeg. |
3 | | * |
4 | | * FFmpeg is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * FFmpeg is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with FFmpeg; if not, write to the Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | | */ |
18 | | |
19 | | /* |
20 | | * |
21 | | * Copyright (c) Sandflow Consulting LLC |
22 | | * |
23 | | * Redistribution and use in source and binary forms, with or without |
24 | | * modification, are permitted provided that the following conditions are met: |
25 | | * |
26 | | * * Redistributions of source code must retain the above copyright notice, this |
27 | | * list of conditions and the following disclaimer. |
28 | | * * Redistributions in binary form must reproduce the above copyright notice, |
29 | | * this list of conditions and the following disclaimer in the documentation |
30 | | * and/or other materials provided with the distribution. |
31 | | * |
32 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
33 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
34 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
35 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
36 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
37 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
38 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
39 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
40 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
41 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
42 | | * POSSIBILITY OF SUCH DAMAGE. |
43 | | */ |
44 | | |
45 | | /** |
46 | | * Demuxes an IMF Composition |
47 | | * |
48 | | * References |
49 | | * OV 2067-0:2018 - SMPTE Overview Document - Interoperable Master Format |
50 | | * ST 2067-2:2020 - SMPTE Standard - Interoperable Master Format — Core Constraints |
51 | | * ST 2067-3:2020 - SMPTE Standard - Interoperable Master Format — Composition Playlist |
52 | | * ST 2067-5:2020 - SMPTE Standard - Interoperable Master Format — Essence Component |
53 | | * ST 2067-20:2016 - SMPTE Standard - Interoperable Master Format — Application #2 |
54 | | * ST 2067-21:2020 - SMPTE Standard - Interoperable Master Format — Application #2 Extended |
55 | | * ST 2067-102:2017 - SMPTE Standard - Interoperable Master Format — Common Image Pixel Color Schemes |
56 | | * ST 429-9:2007 - SMPTE Standard - D-Cinema Packaging — Asset Mapping and File Segmentation |
57 | | * |
58 | | * @author Marc-Antoine Arnaud |
59 | | * @author Valentin Noel |
60 | | * @author Nicholas Vanderzwet |
61 | | * @file |
62 | | * @ingroup lavu_imf |
63 | | */ |
64 | | |
65 | | #include "avio_internal.h" |
66 | | #include "demux.h" |
67 | | #include "imf.h" |
68 | | #include "internal.h" |
69 | | #include "libavcodec/packet.h" |
70 | | #include "libavutil/avstring.h" |
71 | | #include "libavutil/bprint.h" |
72 | | #include "libavutil/intreadwrite.h" |
73 | | #include "libavutil/mem.h" |
74 | | #include "libavutil/opt.h" |
75 | | #include <inttypes.h> |
76 | | #include <libxml/parser.h> |
77 | | |
78 | 0 | #define AVRATIONAL_FORMAT "%d/%d" |
79 | 0 | #define AVRATIONAL_ARG(rational) rational.num, rational.den |
80 | | |
81 | | /** |
82 | | * IMF Asset locator |
83 | | */ |
84 | | typedef struct IMFAssetLocator { |
85 | | AVUUID uuid; |
86 | | char *absolute_uri; |
87 | | } IMFAssetLocator; |
88 | | |
89 | | /** |
90 | | * IMF Asset locator map |
91 | | * Results from the parsing of one or more ASSETMAP XML files |
92 | | */ |
93 | | typedef struct IMFAssetLocatorMap { |
94 | | uint32_t asset_count; |
95 | | IMFAssetLocator *assets; |
96 | | } IMFAssetLocatorMap; |
97 | | |
98 | | typedef struct IMFVirtualTrackResourcePlaybackCtx { |
99 | | IMFAssetLocator *locator; /**< Location of the resource */ |
100 | | FFIMFTrackFileResource *resource; /**< Underlying IMF CPL resource */ |
101 | | AVFormatContext *ctx; /**< Context associated with the resource */ |
102 | | AVRational start_time; /**< inclusive start time of the resource on the CPL timeline (s) */ |
103 | | AVRational end_time; /**< exclusive end time of the resource on the CPL timeline (s) */ |
104 | | AVRational ts_offset; /**< start_time minus the entry point into the resource (s) */ |
105 | | } IMFVirtualTrackResourcePlaybackCtx; |
106 | | |
107 | | typedef struct IMFVirtualTrackPlaybackCtx { |
108 | | int32_t index; /**< Track index in playlist */ |
109 | | AVRational current_timestamp; /**< Current temporal position */ |
110 | | AVRational duration; /**< Overall duration */ |
111 | | uint32_t resource_count; /**< Number of resources (<= INT32_MAX) */ |
112 | | unsigned int resources_alloc_sz; /**< Size of the buffer holding the resource */ |
113 | | IMFVirtualTrackResourcePlaybackCtx *resources; /**< Buffer holding the resources */ |
114 | | int32_t current_resource_index; /**< Index of the current resource in resources, |
115 | | or < 0 if a current resource has yet to be selected */ |
116 | | } IMFVirtualTrackPlaybackCtx; |
117 | | |
118 | | typedef struct IMFContext { |
119 | | const AVClass *class; |
120 | | const char *base_url; |
121 | | char *asset_map_paths; |
122 | | AVIOInterruptCB *interrupt_callback; |
123 | | AVDictionary *avio_opts; |
124 | | FFIMFCPL *cpl; |
125 | | IMFAssetLocatorMap asset_locator_map; |
126 | | uint32_t track_count; |
127 | | IMFVirtualTrackPlaybackCtx **tracks; |
128 | | } IMFContext; |
129 | | |
130 | | static int imf_uri_is_url(const char *string) |
131 | 0 | { |
132 | 0 | return strstr(string, "://") != NULL; |
133 | 0 | } |
134 | | |
135 | | static int imf_uri_is_unix_abs_path(const char *string) |
136 | 0 | { |
137 | 0 | return string[0] == '/'; |
138 | 0 | } |
139 | | |
140 | | static int imf_uri_is_dos_abs_path(const char *string) |
141 | 0 | { |
142 | | /* Absolute path case: `C:\path\to\somewhere` */ |
143 | 0 | if (string[1] == ':' && string[2] == '\\') |
144 | 0 | return 1; |
145 | | |
146 | | /* Absolute path case: `C:/path/to/somewhere` */ |
147 | 0 | if (string[1] == ':' && string[2] == '/') |
148 | 0 | return 1; |
149 | | |
150 | | /* Network path case: `\\path\to\somewhere` */ |
151 | 0 | if (string[0] == '\\' && string[1] == '\\') |
152 | 0 | return 1; |
153 | | |
154 | 0 | return 0; |
155 | 0 | } |
156 | | |
157 | | static int imf_time_to_ts(int64_t *ts, AVRational t, AVRational time_base) |
158 | 0 | { |
159 | 0 | int dst_num; |
160 | 0 | int dst_den; |
161 | 0 | AVRational r; |
162 | |
|
163 | 0 | r = av_div_q(t, time_base); |
164 | |
|
165 | 0 | if ((av_reduce(&dst_num, &dst_den, r.num, r.den, INT64_MAX) != 1)) |
166 | 0 | return 1; |
167 | | |
168 | 0 | if (dst_den != 1) |
169 | 0 | return 1; |
170 | | |
171 | 0 | *ts = dst_num; |
172 | |
|
173 | 0 | return 0; |
174 | 0 | } |
175 | | |
176 | | /** |
177 | | * Parse a ASSETMAP XML file to extract the UUID-URI mapping of assets. |
178 | | * @param s the current format context, if any (can be NULL). |
179 | | * @param doc the XML document to be parsed. |
180 | | * @param asset_map pointer on the IMFAssetLocatorMap to fill. |
181 | | * @param base_url the url of the asset map XML file, if any (can be NULL). |
182 | | * @return a negative value in case of error, 0 otherwise. |
183 | | */ |
184 | | static int parse_imf_asset_map_from_xml_dom(AVFormatContext *s, |
185 | | xmlDocPtr doc, |
186 | | IMFAssetLocatorMap *asset_map, |
187 | | const char *base_url) |
188 | 0 | { |
189 | 0 | xmlNodePtr asset_map_element = NULL; |
190 | 0 | xmlNodePtr node = NULL; |
191 | 0 | xmlNodePtr asset_element = NULL; |
192 | 0 | unsigned long elem_count; |
193 | 0 | char *uri; |
194 | 0 | int ret = 0; |
195 | 0 | IMFAssetLocator *asset = NULL; |
196 | 0 | void *tmp; |
197 | |
|
198 | 0 | asset_map_element = xmlDocGetRootElement(doc); |
199 | |
|
200 | 0 | if (!asset_map_element) { |
201 | 0 | av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing root node\n"); |
202 | 0 | return AVERROR_INVALIDDATA; |
203 | 0 | } |
204 | | |
205 | 0 | if (asset_map_element->type != XML_ELEMENT_NODE || av_strcasecmp(asset_map_element->name, "AssetMap")) { |
206 | 0 | av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - wrong root node name[%s] type[%d]\n", |
207 | 0 | asset_map_element->name, (int)asset_map_element->type); |
208 | 0 | return AVERROR_INVALIDDATA; |
209 | 0 | } |
210 | | |
211 | | /* parse asset locators */ |
212 | 0 | if (!(node = ff_imf_xml_get_child_element_by_name(asset_map_element, "AssetList"))) { |
213 | 0 | av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing AssetList node\n"); |
214 | 0 | return AVERROR_INVALIDDATA; |
215 | 0 | } |
216 | 0 | elem_count = xmlChildElementCount(node); |
217 | 0 | if (elem_count > UINT32_MAX |
218 | 0 | || asset_map->asset_count > UINT32_MAX - elem_count) |
219 | 0 | return AVERROR(ENOMEM); |
220 | 0 | tmp = av_realloc_array(asset_map->assets, |
221 | 0 | elem_count + asset_map->asset_count, |
222 | 0 | sizeof(IMFAssetLocator)); |
223 | 0 | if (!tmp) { |
224 | 0 | av_log(s, AV_LOG_ERROR, "Cannot allocate IMF asset locators\n"); |
225 | 0 | return AVERROR(ENOMEM); |
226 | 0 | } |
227 | 0 | asset_map->assets = tmp; |
228 | |
|
229 | 0 | asset_element = xmlFirstElementChild(node); |
230 | 0 | while (asset_element) { |
231 | 0 | if (av_strcasecmp(asset_element->name, "Asset") != 0) |
232 | 0 | continue; |
233 | | |
234 | 0 | asset = &(asset_map->assets[asset_map->asset_count]); |
235 | |
|
236 | 0 | if (!(node = ff_imf_xml_get_child_element_by_name(asset_element, "Id"))) { |
237 | 0 | av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing Id node\n"); |
238 | 0 | return AVERROR_INVALIDDATA; |
239 | 0 | } |
240 | | |
241 | 0 | if (ff_imf_xml_read_uuid(node, asset->uuid)) { |
242 | 0 | av_log(s, AV_LOG_ERROR, "Could not parse UUID from asset in asset map.\n"); |
243 | 0 | return AVERROR_INVALIDDATA; |
244 | 0 | } |
245 | | |
246 | 0 | av_log(s, AV_LOG_DEBUG, "Found asset id: " AV_PRI_URN_UUID "\n", AV_UUID_ARG(asset->uuid)); |
247 | |
|
248 | 0 | if (!(node = ff_imf_xml_get_child_element_by_name(asset_element, "ChunkList"))) { |
249 | 0 | av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing ChunkList node\n"); |
250 | 0 | return AVERROR_INVALIDDATA; |
251 | 0 | } |
252 | | |
253 | 0 | if (!(node = ff_imf_xml_get_child_element_by_name(node, "Chunk"))) { |
254 | 0 | av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing Chunk node\n"); |
255 | 0 | return AVERROR_INVALIDDATA; |
256 | 0 | } |
257 | | |
258 | 0 | uri = xmlNodeGetContent(ff_imf_xml_get_child_element_by_name(node, "Path")); |
259 | 0 | if (!imf_uri_is_url(uri) && !imf_uri_is_unix_abs_path(uri) && !imf_uri_is_dos_abs_path(uri)) |
260 | 0 | asset->absolute_uri = av_append_path_component(base_url, uri); |
261 | 0 | else |
262 | 0 | asset->absolute_uri = av_strdup(uri); |
263 | 0 | xmlFree(uri); |
264 | 0 | if (!asset->absolute_uri) |
265 | 0 | return AVERROR(ENOMEM); |
266 | | |
267 | 0 | av_log(s, AV_LOG_DEBUG, "Found asset absolute URI: %s\n", asset->absolute_uri); |
268 | |
|
269 | 0 | asset_map->asset_count++; |
270 | 0 | asset_element = xmlNextElementSibling(asset_element); |
271 | 0 | } |
272 | | |
273 | 0 | return ret; |
274 | 0 | } |
275 | | |
276 | | /** |
277 | | * Initializes an IMFAssetLocatorMap structure. |
278 | | */ |
279 | | static void imf_asset_locator_map_init(IMFAssetLocatorMap *asset_map) |
280 | 9 | { |
281 | 9 | asset_map->assets = NULL; |
282 | 9 | asset_map->asset_count = 0; |
283 | 9 | } |
284 | | |
285 | | /** |
286 | | * Free a IMFAssetLocatorMap pointer. |
287 | | */ |
288 | | static void imf_asset_locator_map_deinit(IMFAssetLocatorMap *asset_map) |
289 | 19.7k | { |
290 | 19.7k | for (uint32_t i = 0; i < asset_map->asset_count; i++) |
291 | 0 | av_freep(&asset_map->assets[i].absolute_uri); |
292 | | |
293 | 19.7k | av_freep(&asset_map->assets); |
294 | 19.7k | } |
295 | | |
296 | | static int parse_assetmap(AVFormatContext *s, const char *url) |
297 | 9 | { |
298 | 9 | IMFContext *c = s->priv_data; |
299 | 9 | AVIOContext *in = NULL; |
300 | 9 | struct AVBPrint buf; |
301 | 9 | AVDictionary *opts = NULL; |
302 | 9 | xmlDoc *doc = NULL; |
303 | 9 | const char *base_url; |
304 | 9 | char *tmp_str = NULL; |
305 | 9 | int ret; |
306 | | |
307 | 9 | av_log(s, AV_LOG_DEBUG, "Asset Map URL: %s\n", url); |
308 | | |
309 | 9 | av_dict_copy(&opts, c->avio_opts, 0); |
310 | 9 | ret = s->io_open(s, &in, url, AVIO_FLAG_READ, &opts); |
311 | 9 | av_dict_free(&opts); |
312 | 9 | if (ret < 0) |
313 | 9 | return ret; |
314 | | |
315 | 0 | av_bprint_init(&buf, 0, INT_MAX); // xmlReadMemory uses integer length |
316 | |
|
317 | 0 | ret = avio_read_to_bprint(in, &buf, SIZE_MAX); |
318 | 0 | if (ret < 0 || !avio_feof(in)) { |
319 | 0 | av_log(s, AV_LOG_ERROR, "Unable to read to asset map '%s'\n", url); |
320 | 0 | if (ret == 0) |
321 | 0 | ret = AVERROR_INVALIDDATA; |
322 | 0 | goto clean_up; |
323 | 0 | } |
324 | | |
325 | 0 | LIBXML_TEST_VERSION |
326 | |
|
327 | 0 | tmp_str = av_strdup(url); |
328 | 0 | if (!tmp_str) { |
329 | 0 | ret = AVERROR(ENOMEM); |
330 | 0 | goto clean_up; |
331 | 0 | } |
332 | 0 | base_url = av_dirname(tmp_str); |
333 | |
|
334 | 0 | doc = xmlReadMemory(buf.str, buf.len, url, NULL, 0); |
335 | |
|
336 | 0 | ret = parse_imf_asset_map_from_xml_dom(s, doc, &c->asset_locator_map, base_url); |
337 | 0 | if (!ret) |
338 | 0 | av_log(s, AV_LOG_DEBUG, "Found %d assets from %s\n", |
339 | 0 | c->asset_locator_map.asset_count, url); |
340 | |
|
341 | 0 | xmlFreeDoc(doc); |
342 | |
|
343 | 0 | clean_up: |
344 | 0 | if (tmp_str) |
345 | 0 | av_freep(&tmp_str); |
346 | 0 | ff_format_io_close(s, &in); |
347 | 0 | av_bprint_finalize(&buf, NULL); |
348 | 0 | return ret; |
349 | 0 | } |
350 | | |
351 | | static IMFAssetLocator *find_asset_map_locator(IMFAssetLocatorMap *asset_map, AVUUID uuid) |
352 | 0 | { |
353 | 0 | for (uint32_t i = 0; i < asset_map->asset_count; i++) { |
354 | 0 | if (memcmp(asset_map->assets[i].uuid, uuid, 16) == 0) |
355 | 0 | return &(asset_map->assets[i]); |
356 | 0 | } |
357 | 0 | return NULL; |
358 | 0 | } |
359 | | |
360 | | static int open_track_resource_context(AVFormatContext *s, |
361 | | IMFVirtualTrackPlaybackCtx *track, |
362 | | int32_t resource_index) |
363 | 0 | { |
364 | 0 | IMFContext *c = s->priv_data; |
365 | 0 | int ret = 0; |
366 | 0 | int64_t seek_offset = 0; |
367 | 0 | AVDictionary *opts = NULL; |
368 | 0 | AVStream *st; |
369 | 0 | IMFVirtualTrackResourcePlaybackCtx *track_resource = track->resources + resource_index; |
370 | |
|
371 | 0 | if (track_resource->ctx) { |
372 | 0 | av_log(s, AV_LOG_DEBUG, "Input context already opened for %s.\n", |
373 | 0 | track_resource->locator->absolute_uri); |
374 | 0 | return 0; |
375 | 0 | } |
376 | | |
377 | 0 | track_resource->ctx = avformat_alloc_context(); |
378 | 0 | if (!track_resource->ctx) |
379 | 0 | return AVERROR(ENOMEM); |
380 | | |
381 | 0 | track_resource->ctx->io_open = s->io_open; |
382 | 0 | track_resource->ctx->io_close2 = s->io_close2; |
383 | 0 | track_resource->ctx->opaque = s->opaque; |
384 | 0 | track_resource->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO; |
385 | |
|
386 | 0 | if ((ret = ff_copy_whiteblacklists(track_resource->ctx, s)) < 0) |
387 | 0 | goto cleanup; |
388 | | |
389 | 0 | if ((ret = av_opt_set(track_resource->ctx, "format_whitelist", "mxf", 0))) |
390 | 0 | goto cleanup; |
391 | | |
392 | 0 | if ((ret = av_dict_copy(&opts, c->avio_opts, 0)) < 0) |
393 | 0 | goto cleanup; |
394 | | |
395 | 0 | ret = avformat_open_input(&track_resource->ctx, |
396 | 0 | track_resource->locator->absolute_uri, |
397 | 0 | NULL, |
398 | 0 | &opts); |
399 | 0 | if (ret < 0) { |
400 | 0 | av_log(s, AV_LOG_ERROR, "Could not open %s input context: %s\n", |
401 | 0 | track_resource->locator->absolute_uri, av_err2str(ret)); |
402 | 0 | goto cleanup; |
403 | 0 | } |
404 | 0 | av_dict_free(&opts); |
405 | | |
406 | | /* make sure there is only one stream in the file */ |
407 | |
|
408 | 0 | if (track_resource->ctx->nb_streams != 1) { |
409 | 0 | ret = AVERROR_INVALIDDATA; |
410 | 0 | goto cleanup; |
411 | 0 | } |
412 | | |
413 | 0 | st = track_resource->ctx->streams[0]; |
414 | | |
415 | | /* Determine the seek offset into the Track File, taking into account: |
416 | | * - the current timestamp within the virtual track |
417 | | * - the entry point of the resource |
418 | | */ |
419 | 0 | if (imf_time_to_ts(&seek_offset, |
420 | 0 | av_sub_q(track->current_timestamp, track_resource->ts_offset), |
421 | 0 | st->time_base)) |
422 | 0 | av_log(s, AV_LOG_WARNING, "Incoherent stream timebase " AVRATIONAL_FORMAT |
423 | 0 | "and composition timeline position: " AVRATIONAL_FORMAT "\n", |
424 | 0 | AVRATIONAL_ARG(st->time_base), AVRATIONAL_ARG(track->current_timestamp)); |
425 | |
|
426 | 0 | if (seek_offset) { |
427 | 0 | av_log(s, AV_LOG_DEBUG, "Seek at resource %s entry point: %" PRIi64 "\n", |
428 | 0 | track_resource->locator->absolute_uri, seek_offset); |
429 | 0 | ret = avformat_seek_file(track_resource->ctx, 0, seek_offset, seek_offset, seek_offset, 0); |
430 | 0 | if (ret < 0) { |
431 | 0 | av_log(s, |
432 | 0 | AV_LOG_ERROR, |
433 | 0 | "Could not seek at %" PRId64 "on %s: %s\n", |
434 | 0 | seek_offset, |
435 | 0 | track_resource->locator->absolute_uri, |
436 | 0 | av_err2str(ret)); |
437 | 0 | avformat_close_input(&track_resource->ctx); |
438 | 0 | return ret; |
439 | 0 | } |
440 | 0 | } |
441 | | |
442 | 0 | return 0; |
443 | | |
444 | 0 | cleanup: |
445 | 0 | av_dict_free(&opts); |
446 | 0 | avformat_free_context(track_resource->ctx); |
447 | 0 | track_resource->ctx = NULL; |
448 | 0 | return ret; |
449 | 0 | } |
450 | | |
451 | | static int open_track_file_resource(AVFormatContext *s, |
452 | | FFIMFTrackFileResource *track_file_resource, |
453 | | IMFVirtualTrackPlaybackCtx *track) |
454 | 0 | { |
455 | 0 | IMFContext *c = s->priv_data; |
456 | 0 | IMFAssetLocator *asset_locator; |
457 | 0 | void *tmp; |
458 | |
|
459 | 0 | asset_locator = find_asset_map_locator(&c->asset_locator_map, track_file_resource->track_file_uuid); |
460 | 0 | if (!asset_locator) { |
461 | 0 | av_log(s, AV_LOG_ERROR, "Could not find asset locator for UUID: " AV_PRI_URN_UUID "\n", |
462 | 0 | AV_UUID_ARG(track_file_resource->track_file_uuid)); |
463 | 0 | return AVERROR_INVALIDDATA; |
464 | 0 | } |
465 | | |
466 | 0 | av_log(s, |
467 | 0 | AV_LOG_DEBUG, |
468 | 0 | "Found locator for " AV_PRI_URN_UUID ": %s\n", |
469 | 0 | AV_UUID_ARG(asset_locator->uuid), |
470 | 0 | asset_locator->absolute_uri); |
471 | |
|
472 | 0 | if (track->resource_count > INT32_MAX - track_file_resource->base.repeat_count |
473 | 0 | || (track->resource_count + track_file_resource->base.repeat_count) |
474 | 0 | > INT_MAX / sizeof(IMFVirtualTrackResourcePlaybackCtx)) |
475 | 0 | return AVERROR(ENOMEM); |
476 | 0 | tmp = av_fast_realloc(track->resources, |
477 | 0 | &track->resources_alloc_sz, |
478 | 0 | (track->resource_count + track_file_resource->base.repeat_count) |
479 | 0 | * sizeof(IMFVirtualTrackResourcePlaybackCtx)); |
480 | 0 | if (!tmp) |
481 | 0 | return AVERROR(ENOMEM); |
482 | 0 | track->resources = tmp; |
483 | |
|
484 | 0 | for (uint32_t i = 0; i < track_file_resource->base.repeat_count; i++) { |
485 | 0 | IMFVirtualTrackResourcePlaybackCtx vt_ctx; |
486 | |
|
487 | 0 | vt_ctx.locator = asset_locator; |
488 | 0 | vt_ctx.resource = track_file_resource; |
489 | 0 | vt_ctx.ctx = NULL; |
490 | 0 | vt_ctx.start_time = track->duration; |
491 | 0 | vt_ctx.ts_offset = av_sub_q(vt_ctx.start_time, |
492 | 0 | av_div_q(av_make_q((int)track_file_resource->base.entry_point, 1), |
493 | 0 | track_file_resource->base.edit_rate)); |
494 | 0 | vt_ctx.end_time = av_add_q(track->duration, |
495 | 0 | av_make_q((int)track_file_resource->base.duration |
496 | 0 | * track_file_resource->base.edit_rate.den, |
497 | 0 | track_file_resource->base.edit_rate.num)); |
498 | 0 | track->resources[track->resource_count++] = vt_ctx; |
499 | 0 | track->duration = vt_ctx.end_time; |
500 | 0 | } |
501 | |
|
502 | 0 | return 0; |
503 | 0 | } |
504 | | |
505 | | static void imf_virtual_track_playback_context_deinit(IMFVirtualTrackPlaybackCtx *track) |
506 | 0 | { |
507 | 0 | for (uint32_t i = 0; i < track->resource_count; i++) |
508 | 0 | avformat_close_input(&track->resources[i].ctx); |
509 | |
|
510 | 0 | av_freep(&track->resources); |
511 | 0 | } |
512 | | |
513 | | static int open_virtual_track(AVFormatContext *s, |
514 | | FFIMFTrackFileVirtualTrack *virtual_track, |
515 | | int32_t track_index) |
516 | 0 | { |
517 | 0 | IMFContext *c = s->priv_data; |
518 | 0 | IMFVirtualTrackPlaybackCtx *track = NULL; |
519 | 0 | void *tmp; |
520 | 0 | int ret = 0; |
521 | |
|
522 | 0 | if (!(track = av_mallocz(sizeof(IMFVirtualTrackPlaybackCtx)))) |
523 | 0 | return AVERROR(ENOMEM); |
524 | 0 | track->current_resource_index = -1; |
525 | 0 | track->index = track_index; |
526 | 0 | track->duration = av_make_q(0, 1); |
527 | |
|
528 | 0 | for (uint32_t i = 0; i < virtual_track->resource_count; i++) { |
529 | 0 | av_log(s, |
530 | 0 | AV_LOG_DEBUG, |
531 | 0 | "Open stream from file " AV_PRI_URN_UUID ", stream %d\n", |
532 | 0 | AV_UUID_ARG(virtual_track->resources[i].track_file_uuid), |
533 | 0 | i); |
534 | 0 | if ((ret = open_track_file_resource(s, &virtual_track->resources[i], track)) != 0) { |
535 | 0 | av_log(s, |
536 | 0 | AV_LOG_ERROR, |
537 | 0 | "Could not open image track resource " AV_PRI_URN_UUID "\n", |
538 | 0 | AV_UUID_ARG(virtual_track->resources[i].track_file_uuid)); |
539 | 0 | goto clean_up; |
540 | 0 | } |
541 | 0 | } |
542 | | |
543 | 0 | track->current_timestamp = av_make_q(0, track->duration.den); |
544 | |
|
545 | 0 | if (c->track_count == UINT32_MAX) { |
546 | 0 | ret = AVERROR(ENOMEM); |
547 | 0 | goto clean_up; |
548 | 0 | } |
549 | 0 | tmp = av_realloc_array(c->tracks, c->track_count + 1, sizeof(IMFVirtualTrackPlaybackCtx *)); |
550 | 0 | if (!tmp) { |
551 | 0 | ret = AVERROR(ENOMEM); |
552 | 0 | goto clean_up; |
553 | 0 | } |
554 | 0 | c->tracks = tmp; |
555 | 0 | c->tracks[c->track_count++] = track; |
556 | |
|
557 | 0 | return 0; |
558 | | |
559 | 0 | clean_up: |
560 | 0 | imf_virtual_track_playback_context_deinit(track); |
561 | 0 | av_free(track); |
562 | 0 | return ret; |
563 | 0 | } |
564 | | |
565 | | static int set_context_streams_from_tracks(AVFormatContext *s) |
566 | 0 | { |
567 | 0 | IMFContext *c = s->priv_data; |
568 | 0 | int ret = 0; |
569 | |
|
570 | 0 | for (uint32_t i = 0; i < c->track_count; i++) { |
571 | 0 | AVStream *asset_stream; |
572 | 0 | AVStream *first_resource_stream; |
573 | | |
574 | | /* Open the first resource of the track to get stream information */ |
575 | 0 | ret = open_track_resource_context(s, c->tracks[i], 0); |
576 | 0 | if (ret) |
577 | 0 | return ret; |
578 | 0 | first_resource_stream = c->tracks[i]->resources[0].ctx->streams[0]; |
579 | 0 | av_log(s, AV_LOG_DEBUG, "Open the first resource of track %d\n", c->tracks[i]->index); |
580 | |
|
581 | 0 | asset_stream = ff_stream_clone(s, first_resource_stream); |
582 | 0 | if (!asset_stream) { |
583 | 0 | av_log(s, AV_LOG_ERROR, "Could not clone stream\n"); |
584 | 0 | return AVERROR(ENOMEM); |
585 | 0 | } |
586 | | |
587 | 0 | asset_stream->id = i; |
588 | 0 | asset_stream->nb_frames = 0; |
589 | 0 | avpriv_set_pts_info(asset_stream, |
590 | 0 | first_resource_stream->pts_wrap_bits, |
591 | 0 | first_resource_stream->time_base.num, |
592 | 0 | first_resource_stream->time_base.den); |
593 | 0 | asset_stream->duration = (int64_t)av_q2d(av_mul_q(c->tracks[i]->duration, |
594 | 0 | av_inv_q(asset_stream->time_base))); |
595 | 0 | } |
596 | | |
597 | 0 | return 0; |
598 | 0 | } |
599 | | |
600 | | static int open_cpl_tracks(AVFormatContext *s) |
601 | 0 | { |
602 | 0 | IMFContext *c = s->priv_data; |
603 | 0 | int32_t track_index = 0; |
604 | 0 | int ret; |
605 | |
|
606 | 0 | if (c->cpl->main_image_2d_track) { |
607 | 0 | if ((ret = open_virtual_track(s, c->cpl->main_image_2d_track, track_index++)) != 0) { |
608 | 0 | av_log(s, AV_LOG_ERROR, "Could not open image track " AV_PRI_URN_UUID "\n", |
609 | 0 | AV_UUID_ARG(c->cpl->main_image_2d_track->base.id_uuid)); |
610 | 0 | return ret; |
611 | 0 | } |
612 | 0 | } |
613 | | |
614 | 0 | for (uint32_t i = 0; i < c->cpl->main_audio_track_count; i++) { |
615 | 0 | if ((ret = open_virtual_track(s, &c->cpl->main_audio_tracks[i], track_index++)) != 0) { |
616 | 0 | av_log(s, AV_LOG_ERROR, "Could not open audio track " AV_PRI_URN_UUID "\n", |
617 | 0 | AV_UUID_ARG(c->cpl->main_audio_tracks[i].base.id_uuid)); |
618 | 0 | return ret; |
619 | 0 | } |
620 | 0 | } |
621 | | |
622 | 0 | return set_context_streams_from_tracks(s); |
623 | 0 | } |
624 | | |
625 | | static int imf_read_header(AVFormatContext *s) |
626 | 19.7k | { |
627 | 19.7k | IMFContext *c = s->priv_data; |
628 | 19.7k | char *asset_map_path; |
629 | 19.7k | char *tmp_str; |
630 | 19.7k | AVDictionaryEntry* tcr; |
631 | 19.7k | char tc_buf[AV_TIMECODE_STR_SIZE]; |
632 | 19.7k | int ret = 0; |
633 | | |
634 | 19.7k | c->interrupt_callback = &s->interrupt_callback; |
635 | 19.7k | tmp_str = av_strdup(s->url); |
636 | 19.7k | if (!tmp_str) |
637 | 0 | return AVERROR(ENOMEM); |
638 | 19.7k | c->base_url = av_strdup(av_dirname(tmp_str)); |
639 | 19.7k | av_freep(&tmp_str); |
640 | 19.7k | if (!c->base_url) |
641 | 0 | return AVERROR(ENOMEM); |
642 | | |
643 | 19.7k | if ((ret = ffio_copy_url_options(s->pb, &c->avio_opts)) < 0) |
644 | 0 | return ret; |
645 | | |
646 | 19.7k | av_log(s, AV_LOG_DEBUG, "start parsing IMF CPL: %s\n", s->url); |
647 | | |
648 | 19.7k | if ((ret = ff_imf_parse_cpl(s, s->pb, &c->cpl)) < 0) |
649 | 19.7k | return ret; |
650 | | |
651 | 9 | tcr = av_dict_get(s->metadata, "timecode", NULL, 0); |
652 | 9 | if (!tcr && c->cpl->tc) { |
653 | 0 | ret = av_dict_set(&s->metadata, "timecode", |
654 | 0 | av_timecode_make_string(c->cpl->tc, tc_buf, 0), 0); |
655 | 0 | if (ret) |
656 | 0 | return ret; |
657 | 0 | av_log(s, AV_LOG_INFO, "Setting timecode to IMF CPL timecode %s\n", tc_buf); |
658 | 0 | } |
659 | | |
660 | 9 | av_log(s, |
661 | 9 | AV_LOG_DEBUG, |
662 | 9 | "parsed IMF CPL: " AV_PRI_URN_UUID "\n", |
663 | 9 | AV_UUID_ARG(c->cpl->id_uuid)); |
664 | | |
665 | 9 | if (!c->asset_map_paths) { |
666 | 9 | c->asset_map_paths = av_append_path_component(c->base_url, "ASSETMAP.xml"); |
667 | 9 | if (!c->asset_map_paths) { |
668 | 0 | ret = AVERROR(ENOMEM); |
669 | 0 | return ret; |
670 | 0 | } |
671 | 9 | av_log(s, AV_LOG_DEBUG, "No asset maps provided, using the default ASSETMAP.xml\n"); |
672 | 9 | } |
673 | | |
674 | | /* Parse each asset map XML file */ |
675 | 9 | imf_asset_locator_map_init(&c->asset_locator_map); |
676 | 9 | asset_map_path = av_strtok(c->asset_map_paths, ",", &tmp_str); |
677 | 9 | while (asset_map_path != NULL) { |
678 | 9 | av_log(s, AV_LOG_DEBUG, "start parsing IMF Asset Map: %s\n", asset_map_path); |
679 | | |
680 | 9 | if ((ret = parse_assetmap(s, asset_map_path))) |
681 | 9 | return ret; |
682 | | |
683 | 0 | asset_map_path = av_strtok(NULL, ",", &tmp_str); |
684 | 0 | } |
685 | | |
686 | 0 | av_log(s, AV_LOG_DEBUG, "parsed IMF Asset Maps\n"); |
687 | |
|
688 | 0 | if ((ret = open_cpl_tracks(s))) |
689 | 0 | return ret; |
690 | | |
691 | 0 | av_log(s, AV_LOG_DEBUG, "parsed IMF package\n"); |
692 | |
|
693 | 0 | return 0; |
694 | 0 | } |
695 | | |
696 | | static IMFVirtualTrackPlaybackCtx *get_next_track_with_minimum_timestamp(AVFormatContext *s) |
697 | 0 | { |
698 | 0 | IMFContext *c = s->priv_data; |
699 | 0 | IMFVirtualTrackPlaybackCtx *track = NULL; |
700 | 0 | AVRational minimum_timestamp = av_make_q(INT32_MAX, 1); |
701 | |
|
702 | 0 | for (uint32_t i = c->track_count; i > 0; i--) { |
703 | 0 | av_log(s, AV_LOG_TRACE, "Compare track %d timestamp " AVRATIONAL_FORMAT |
704 | 0 | " to minimum " AVRATIONAL_FORMAT |
705 | 0 | " (over duration: " AVRATIONAL_FORMAT ")\n", i, |
706 | 0 | AVRATIONAL_ARG(c->tracks[i - 1]->current_timestamp), |
707 | 0 | AVRATIONAL_ARG(minimum_timestamp), |
708 | 0 | AVRATIONAL_ARG(c->tracks[i - 1]->duration)); |
709 | |
|
710 | 0 | if (av_cmp_q(c->tracks[i - 1]->current_timestamp, minimum_timestamp) <= 0) { |
711 | 0 | track = c->tracks[i - 1]; |
712 | 0 | minimum_timestamp = track->current_timestamp; |
713 | 0 | } |
714 | 0 | } |
715 | |
|
716 | 0 | return track; |
717 | 0 | } |
718 | | |
719 | | static int get_resource_context_for_timestamp(AVFormatContext *s, IMFVirtualTrackPlaybackCtx *track, IMFVirtualTrackResourcePlaybackCtx **resource) |
720 | 0 | { |
721 | 0 | *resource = NULL; |
722 | |
|
723 | 0 | if (av_cmp_q(track->current_timestamp, track->duration) >= 0) { |
724 | 0 | av_log(s, AV_LOG_DEBUG, "Reached the end of the virtual track\n"); |
725 | 0 | return AVERROR_EOF; |
726 | 0 | } |
727 | | |
728 | 0 | av_log(s, |
729 | 0 | AV_LOG_TRACE, |
730 | 0 | "Looking for track %d resource for timestamp = %lf / %lf\n", |
731 | 0 | track->index, |
732 | 0 | av_q2d(track->current_timestamp), |
733 | 0 | av_q2d(track->duration)); |
734 | 0 | for (uint32_t i = 0; i < track->resource_count; i++) { |
735 | |
|
736 | 0 | if (av_cmp_q(track->resources[i].end_time, track->current_timestamp) > 0) { |
737 | 0 | av_log(s, AV_LOG_DEBUG, "Found resource %d in track %d to read at timestamp %lf: " |
738 | 0 | "entry=%" PRIu32 ", duration=%" PRIu32 ", editrate=" AVRATIONAL_FORMAT "\n", |
739 | 0 | i, track->index, av_q2d(track->current_timestamp), |
740 | 0 | track->resources[i].resource->base.entry_point, |
741 | 0 | track->resources[i].resource->base.duration, |
742 | 0 | AVRATIONAL_ARG(track->resources[i].resource->base.edit_rate)); |
743 | |
|
744 | 0 | if (track->current_resource_index != i) { |
745 | 0 | int ret; |
746 | |
|
747 | 0 | av_log(s, AV_LOG_TRACE, "Switch resource on track %d: re-open context\n", |
748 | 0 | track->index); |
749 | |
|
750 | 0 | ret = open_track_resource_context(s, track, i); |
751 | 0 | if (ret != 0) |
752 | 0 | return ret; |
753 | 0 | if (track->current_resource_index > 0) |
754 | 0 | avformat_close_input(&track->resources[track->current_resource_index].ctx); |
755 | 0 | track->current_resource_index = i; |
756 | 0 | } |
757 | | |
758 | 0 | *resource = track->resources + track->current_resource_index; |
759 | 0 | return 0; |
760 | 0 | } |
761 | 0 | } |
762 | | |
763 | 0 | av_log(s, AV_LOG_ERROR, "Could not find IMF track resource to read\n"); |
764 | 0 | return AVERROR_STREAM_NOT_FOUND; |
765 | 0 | } |
766 | | |
767 | | static int imf_read_packet(AVFormatContext *s, AVPacket *pkt) |
768 | 0 | { |
769 | 0 | IMFVirtualTrackResourcePlaybackCtx *resource = NULL; |
770 | 0 | int ret = 0; |
771 | 0 | IMFVirtualTrackPlaybackCtx *track; |
772 | 0 | int64_t delta_ts; |
773 | 0 | AVStream *st; |
774 | 0 | AVRational next_timestamp; |
775 | |
|
776 | 0 | track = get_next_track_with_minimum_timestamp(s); |
777 | |
|
778 | 0 | if (!track) { |
779 | 0 | av_log(s, AV_LOG_ERROR, "No track found for playback\n"); |
780 | 0 | return AVERROR_INVALIDDATA; |
781 | 0 | } |
782 | | |
783 | 0 | av_log(s, AV_LOG_DEBUG, "Found track %d to read at timestamp %lf\n", |
784 | 0 | track->index, av_q2d(track->current_timestamp)); |
785 | |
|
786 | 0 | ret = get_resource_context_for_timestamp(s, track, &resource); |
787 | 0 | if (ret) |
788 | 0 | return ret; |
789 | | |
790 | 0 | ret = av_read_frame(resource->ctx, pkt); |
791 | 0 | if (ret) |
792 | 0 | return ret; |
793 | | |
794 | 0 | av_log(s, AV_LOG_DEBUG, "Got packet: pts=%" PRId64 ", dts=%" PRId64 |
795 | 0 | ", duration=%" PRId64 ", stream_index=%d, pos=%" PRId64 |
796 | 0 | ", time_base=" AVRATIONAL_FORMAT "\n", pkt->pts, pkt->dts, pkt->duration, |
797 | 0 | pkt->stream_index, pkt->pos, AVRATIONAL_ARG(pkt->time_base)); |
798 | | |
799 | | /* IMF resources contain only one stream */ |
800 | |
|
801 | 0 | if (pkt->stream_index != 0) |
802 | 0 | return AVERROR_INVALIDDATA; |
803 | 0 | st = resource->ctx->streams[0]; |
804 | |
|
805 | 0 | pkt->stream_index = track->index; |
806 | | |
807 | | /* adjust the packet PTS and DTS based on the temporal position of the resource within the timeline */ |
808 | |
|
809 | 0 | ret = imf_time_to_ts(&delta_ts, resource->ts_offset, st->time_base); |
810 | |
|
811 | 0 | if (!ret) { |
812 | 0 | if (pkt->pts != AV_NOPTS_VALUE) |
813 | 0 | pkt->pts += delta_ts; |
814 | 0 | if (pkt->dts != AV_NOPTS_VALUE) |
815 | 0 | pkt->dts += delta_ts; |
816 | 0 | } else { |
817 | 0 | av_log(s, AV_LOG_WARNING, "Incoherent time stamp " AVRATIONAL_FORMAT |
818 | 0 | " for time base " AVRATIONAL_FORMAT, |
819 | 0 | AVRATIONAL_ARG(resource->ts_offset), |
820 | 0 | AVRATIONAL_ARG(pkt->time_base)); |
821 | 0 | } |
822 | | |
823 | | /* advance the track timestamp by the packet duration */ |
824 | |
|
825 | 0 | next_timestamp = av_add_q(track->current_timestamp, |
826 | 0 | av_mul_q(av_make_q((int)pkt->duration, 1), st->time_base)); |
827 | | |
828 | | /* if necessary, clamp the next timestamp to the end of the current resource */ |
829 | |
|
830 | 0 | if (av_cmp_q(next_timestamp, resource->end_time) > 0) { |
831 | |
|
832 | 0 | int64_t new_pkt_dur; |
833 | | |
834 | | /* shrink the packet duration */ |
835 | |
|
836 | 0 | ret = imf_time_to_ts(&new_pkt_dur, |
837 | 0 | av_sub_q(resource->end_time, track->current_timestamp), |
838 | 0 | st->time_base); |
839 | |
|
840 | 0 | if (!ret) |
841 | 0 | pkt->duration = new_pkt_dur; |
842 | 0 | else |
843 | 0 | av_log(s, AV_LOG_WARNING, "Incoherent time base in packet duration calculation\n"); |
844 | | |
845 | | /* shrink the packet itself for audio essence */ |
846 | |
|
847 | 0 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
848 | |
|
849 | 0 | if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) { |
850 | | /* AV_CODEC_ID_PCM_S24LE is the only PCM format supported in IMF */ |
851 | | /* in this case, explicitly shrink the packet */ |
852 | |
|
853 | 0 | int bytes_per_sample = av_get_exact_bits_per_sample(st->codecpar->codec_id) >> 3; |
854 | 0 | int64_t nbsamples = av_rescale_q(pkt->duration, |
855 | 0 | st->time_base, |
856 | 0 | av_make_q(1, st->codecpar->sample_rate)); |
857 | 0 | av_shrink_packet(pkt, nbsamples * st->codecpar->ch_layout.nb_channels * bytes_per_sample); |
858 | |
|
859 | 0 | } else { |
860 | | /* in all other cases, use side data to skip samples */ |
861 | 0 | int64_t skip_samples; |
862 | |
|
863 | 0 | ret = imf_time_to_ts(&skip_samples, |
864 | 0 | av_sub_q(next_timestamp, resource->end_time), |
865 | 0 | av_make_q(1, st->codecpar->sample_rate)); |
866 | |
|
867 | 0 | if (ret || skip_samples < 0 || skip_samples > UINT32_MAX) { |
868 | 0 | av_log(s, AV_LOG_WARNING, "Cannot skip audio samples\n"); |
869 | 0 | } else { |
870 | 0 | uint8_t *side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10); |
871 | 0 | if (!side_data) |
872 | 0 | return AVERROR(ENOMEM); |
873 | | |
874 | 0 | AV_WL32(side_data + 4, skip_samples); /* skip from end of this packet */ |
875 | 0 | side_data[6] = 1; /* reason for end is convergence */ |
876 | 0 | } |
877 | 0 | } |
878 | | |
879 | 0 | next_timestamp = resource->end_time; |
880 | |
|
881 | 0 | } else { |
882 | 0 | av_log(s, AV_LOG_WARNING, "Non-audio packet duration reduced\n"); |
883 | 0 | } |
884 | 0 | } |
885 | | |
886 | 0 | track->current_timestamp = next_timestamp; |
887 | |
|
888 | 0 | return 0; |
889 | 0 | } |
890 | | |
891 | | static int imf_close(AVFormatContext *s) |
892 | 19.7k | { |
893 | 19.7k | IMFContext *c = s->priv_data; |
894 | | |
895 | 19.7k | av_log(s, AV_LOG_DEBUG, "Close IMF package\n"); |
896 | 19.7k | av_dict_free(&c->avio_opts); |
897 | 19.7k | av_freep(&c->base_url); |
898 | 19.7k | imf_asset_locator_map_deinit(&c->asset_locator_map); |
899 | 19.7k | ff_imf_cpl_free(c->cpl); |
900 | | |
901 | 19.7k | for (uint32_t i = 0; i < c->track_count; i++) { |
902 | 0 | imf_virtual_track_playback_context_deinit(c->tracks[i]); |
903 | 0 | av_freep(&c->tracks[i]); |
904 | 0 | } |
905 | | |
906 | 19.7k | av_freep(&c->tracks); |
907 | | |
908 | 19.7k | return 0; |
909 | 19.7k | } |
910 | | |
911 | | static int imf_probe(const AVProbeData *p) |
912 | 936k | { |
913 | 936k | if (!strstr(p->buf, "<CompositionPlaylist")) |
914 | 930k | return 0; |
915 | | |
916 | | /* check for a ContentTitle element without including ContentTitleText, |
917 | | * which is used by the D-Cinema CPL. |
918 | | */ |
919 | 5.87k | if (!strstr(p->buf, "ContentTitle>")) |
920 | 1.36k | return 0; |
921 | | |
922 | 4.50k | return AVPROBE_SCORE_MAX; |
923 | 5.87k | } |
924 | | |
925 | | static int coherent_ts(int64_t ts, AVRational in_tb, AVRational out_tb) |
926 | 0 | { |
927 | 0 | int dst_num; |
928 | 0 | int dst_den; |
929 | 0 | int ret; |
930 | |
|
931 | 0 | ret = av_reduce(&dst_num, &dst_den, ts * in_tb.num * out_tb.den, |
932 | 0 | in_tb.den * out_tb.num, INT64_MAX); |
933 | 0 | if (!ret || dst_den != 1) |
934 | 0 | return 0; |
935 | | |
936 | 0 | return 1; |
937 | 0 | } |
938 | | |
939 | | static int imf_seek(AVFormatContext *s, int stream_index, int64_t min_ts, |
940 | | int64_t ts, int64_t max_ts, int flags) |
941 | 0 | { |
942 | 0 | IMFContext *c = s->priv_data; |
943 | 0 | uint32_t i; |
944 | |
|
945 | 0 | if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME)) |
946 | 0 | return AVERROR(ENOSYS); |
947 | | |
948 | | /* rescale timestamps to Composition edit units */ |
949 | 0 | if (stream_index < 0) |
950 | 0 | ff_rescale_interval(AV_TIME_BASE_Q, |
951 | 0 | av_make_q(c->cpl->edit_rate.den, c->cpl->edit_rate.num), |
952 | 0 | &min_ts, &ts, &max_ts); |
953 | 0 | else |
954 | 0 | ff_rescale_interval(s->streams[stream_index]->time_base, |
955 | 0 | av_make_q(c->cpl->edit_rate.den, c->cpl->edit_rate.num), |
956 | 0 | &min_ts, &ts, &max_ts); |
957 | | |
958 | | /* requested timestamp bounds are too close */ |
959 | 0 | if (max_ts < min_ts) |
960 | 0 | return -1; |
961 | | |
962 | | /* clamp requested timestamp to provided bounds */ |
963 | 0 | ts = FFMAX(FFMIN(ts, max_ts), min_ts); |
964 | |
|
965 | 0 | av_log(s, AV_LOG_DEBUG, "Seeking to Composition Playlist edit unit %" PRIi64 "\n", ts); |
966 | | |
967 | | /* set the dts of each stream and temporal offset of each track */ |
968 | 0 | for (i = 0; i < c->track_count; i++) { |
969 | 0 | AVStream *st = s->streams[i]; |
970 | 0 | IMFVirtualTrackPlaybackCtx *t = c->tracks[i]; |
971 | 0 | int64_t dts; |
972 | |
|
973 | 0 | if (!coherent_ts(ts, av_make_q(c->cpl->edit_rate.den, c->cpl->edit_rate.num), |
974 | 0 | st->time_base)) |
975 | 0 | av_log(s, AV_LOG_WARNING, "Seek position is not coherent across tracks\n"); |
976 | |
|
977 | 0 | dts = av_rescale(ts, |
978 | 0 | st->time_base.den * c->cpl->edit_rate.den, |
979 | 0 | st->time_base.num * c->cpl->edit_rate.num); |
980 | |
|
981 | 0 | av_log(s, AV_LOG_DEBUG, "Seeking to dts=%" PRId64 " on stream_index=%d\n", |
982 | 0 | dts, i); |
983 | |
|
984 | 0 | t->current_timestamp = av_mul_q(av_make_q(dts, 1), st->time_base); |
985 | 0 | if (t->current_resource_index >= 0) { |
986 | 0 | avformat_close_input(&t->resources[t->current_resource_index].ctx); |
987 | 0 | t->current_resource_index = -1; |
988 | 0 | } |
989 | 0 | } |
990 | |
|
991 | 0 | return 0; |
992 | 0 | } |
993 | | |
994 | | static const AVOption imf_options[] = { |
995 | | { |
996 | | .name = "assetmaps", |
997 | | .help = "Comma-separated paths to ASSETMAP files." |
998 | | "If not specified, the `ASSETMAP.xml` file in the same " |
999 | | "directory as the CPL is used.", |
1000 | | .offset = offsetof(IMFContext, asset_map_paths), |
1001 | | .type = AV_OPT_TYPE_STRING, |
1002 | | .default_val = {.str = NULL}, |
1003 | | .flags = AV_OPT_FLAG_DECODING_PARAM, |
1004 | | }, |
1005 | | {NULL}, |
1006 | | }; |
1007 | | |
1008 | | static const AVClass imf_class = { |
1009 | | .class_name = "imf", |
1010 | | .item_name = av_default_item_name, |
1011 | | .option = imf_options, |
1012 | | .version = LIBAVUTIL_VERSION_INT, |
1013 | | }; |
1014 | | |
1015 | | const FFInputFormat ff_imf_demuxer = { |
1016 | | .p.name = "imf", |
1017 | | .p.long_name = NULL_IF_CONFIG_SMALL("IMF (Interoperable Master Format)"), |
1018 | | .p.flags = AVFMT_NO_BYTE_SEEK, |
1019 | | .p.priv_class = &imf_class, |
1020 | | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, |
1021 | | .priv_data_size = sizeof(IMFContext), |
1022 | | .read_probe = imf_probe, |
1023 | | .read_header = imf_read_header, |
1024 | | .read_packet = imf_read_packet, |
1025 | | .read_close = imf_close, |
1026 | | .read_seek2 = imf_seek, |
1027 | | }; |