/src/vlc/modules/codec/ttml/ttml.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * ttml.c : TTML helpers |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2017 VLC authors and VideoLAN |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation; either version 2.1 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program 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 |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
19 | | *****************************************************************************/ |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include "config.h" |
22 | | #endif |
23 | | |
24 | | #include <vlc_common.h> |
25 | | #include <vlc_plugin.h> |
26 | | #include <vlc_xml.h> |
27 | | #include <vlc_strings.h> |
28 | | #include <vlc_charset.h> |
29 | | |
30 | | #include <assert.h> |
31 | | #include <stdlib.h> |
32 | | #include <limits.h> |
33 | | |
34 | | #include "ttml.h" |
35 | | |
36 | | #define ALIGN_TEXT N_("Subtitle justification") |
37 | | #define ALIGN_LONGTEXT N_("Set the justification of subtitles") |
38 | | |
39 | | /***************************************************************************** |
40 | | * Modules descriptor. |
41 | | *****************************************************************************/ |
42 | | |
43 | 104 | vlc_module_begin () |
44 | 52 | set_capability( "spu decoder", 10 ) |
45 | 52 | set_shortname( N_("TTML decoder")) |
46 | 52 | set_description( N_("TTML subtitles decoder") ) |
47 | 52 | set_callback( tt_OpenDecoder ) |
48 | 52 | set_subcategory( SUBCAT_INPUT_SCODEC ) |
49 | 52 | add_integer( "ttml-align", 0, ALIGN_TEXT, ALIGN_LONGTEXT ) |
50 | | |
51 | 52 | add_submodule() |
52 | 52 | set_shortname( N_("TTML") ) |
53 | 52 | set_description( N_("TTML demuxer") ) |
54 | 52 | set_capability( "demux", 11 ) |
55 | 52 | set_subcategory( SUBCAT_INPUT_DEMUX ) |
56 | 52 | set_callbacks( tt_OpenDemux, tt_CloseDemux ) |
57 | 52 | add_shortcut( "ttml" ) |
58 | 52 | #ifdef ENABLE_SOUT |
59 | 52 | add_submodule() |
60 | 52 | set_shortname( N_("TTML") ) |
61 | 52 | set_description( N_("TTML encoder") ) |
62 | 52 | set_capability( "spu encoder", 101 ) |
63 | 52 | set_subcategory( SUBCAT_INPUT_SCODEC ) |
64 | 52 | set_callbacks( tt_OpenEncoder, NULL ) |
65 | 52 | #endif |
66 | 52 | vlc_module_end () |
67 | | |
68 | | struct tt_namespace_s |
69 | | { |
70 | | char *psz_prefix; |
71 | | char *psz_uri; |
72 | | struct vlc_list links; |
73 | | }; |
74 | | |
75 | | void tt_namespaces_Clean( tt_namespaces_t *nss ) |
76 | 22.2k | { |
77 | 22.2k | struct tt_namespace_s *ns; |
78 | 22.2k | vlc_list_foreach( ns, &nss->nodes, links ) |
79 | 49.6k | { |
80 | 49.6k | free( ns->psz_prefix ); |
81 | 49.6k | free( ns->psz_uri ); |
82 | 49.6k | free( ns ); |
83 | 49.6k | } |
84 | 22.2k | } |
85 | | |
86 | | void tt_namespaces_Init( tt_namespaces_t *nss ) |
87 | 22.2k | { |
88 | 22.2k | vlc_list_init( &nss->nodes ); |
89 | 22.2k | } |
90 | | |
91 | | const char * tt_namespaces_GetURI( const tt_namespaces_t *nss, |
92 | | const char *psz_qn ) |
93 | 581k | { |
94 | 581k | const struct tt_namespace_s *ns; |
95 | 581k | vlc_list_foreach_const( ns, &nss->nodes, links ) |
96 | 2.09M | { |
97 | | /* compares prefixed name against raw prefix */ |
98 | 2.09M | for( size_t i=0; ; i++ ) |
99 | 3.66M | { |
100 | 3.66M | if( ns->psz_prefix[i] == psz_qn[i] ) |
101 | 1.57M | { |
102 | 1.57M | if( psz_qn[i] == '\0' ) |
103 | 13.4k | return ns->psz_uri; |
104 | 1.57M | } |
105 | 2.08M | else |
106 | 2.08M | { |
107 | 2.08M | if( ns->psz_prefix[i] == '\0' && psz_qn[i] == ':' ) |
108 | 269k | return ns->psz_uri; |
109 | 1.81M | else |
110 | 1.81M | break; |
111 | 2.08M | } |
112 | 3.66M | } |
113 | 2.09M | } |
114 | 297k | return NULL; |
115 | 581k | } |
116 | | |
117 | | const char * tt_namespaces_GetPrefix( const tt_namespaces_t *nss, |
118 | | const char *psz_uri ) |
119 | 1.23M | { |
120 | 1.23M | const struct tt_namespace_s *ns; |
121 | 1.23M | vlc_list_foreach_const( ns, &nss->nodes, links ) |
122 | 3.17M | { |
123 | 3.17M | if( !strcmp( ns->psz_uri, psz_uri ) ) |
124 | 1.16M | return ns->psz_prefix; |
125 | 3.17M | } |
126 | 74.2k | return NULL; |
127 | 1.23M | } |
128 | | |
129 | | void tt_namespaces_Register( tt_namespaces_t *nss, const char *psz_prefix, |
130 | | const char *psz_uri ) |
131 | 608k | { |
132 | 608k | if( !psz_uri || tt_namespaces_GetPrefix( nss, psz_uri ) ) |
133 | 558k | return; |
134 | 49.6k | struct tt_namespace_s *ns = malloc(sizeof(*ns)); |
135 | 49.6k | if( ns ) |
136 | 49.6k | { |
137 | 49.6k | const char *sep = strchr( psz_prefix, ':' ); |
138 | 49.6k | if( sep ) |
139 | 26.8k | ns->psz_prefix = strndup( psz_prefix, sep - psz_prefix ); |
140 | 22.8k | else |
141 | 22.8k | ns->psz_prefix = strdup(""); |
142 | 49.6k | ns->psz_uri = strdup( psz_uri ); |
143 | 49.6k | if( !ns->psz_prefix || !ns->psz_uri ) |
144 | 0 | { |
145 | 0 | free( ns->psz_prefix ); |
146 | 0 | free( ns->psz_uri ); |
147 | 0 | free( ns ); |
148 | 0 | return; |
149 | 0 | } |
150 | 49.6k | vlc_list_append( &ns->links, &nss->nodes ); |
151 | 49.6k | } |
152 | 49.6k | } |
153 | | |
154 | | static const char * tt_node_InheritNS( const tt_node_t *p_node ) |
155 | 1.57M | { |
156 | 101M | for( ; p_node ; p_node = p_node->p_parent ) |
157 | 101M | { |
158 | 101M | if( p_node->psz_namespace ) |
159 | 1.51M | return p_node->psz_namespace; |
160 | 101M | } |
161 | 59.1k | return NULL; |
162 | 1.57M | } |
163 | | |
164 | | bool tt_node_Match( const tt_node_t *p_node, const char *psz_name, const char *psz_namespace ) |
165 | 14.5M | { |
166 | | /* compare local part first (should have less chars) */ |
167 | 14.5M | const char *psz_nodelocal = tt_LocalName( p_node->psz_node_name ); |
168 | 14.5M | const char *psz_namelocal = tt_LocalName( psz_name ); |
169 | 14.5M | if( strcmp( psz_namelocal, psz_nodelocal ) ) |
170 | 13.3M | return false; |
171 | | |
172 | 1.11M | const char *psz_nodens = p_node->psz_namespace; |
173 | 1.11M | if( !psz_nodens ) |
174 | 1.08M | psz_nodens = tt_node_InheritNS( p_node->p_parent ); |
175 | 1.11M | if( psz_namespace && psz_nodens ) |
176 | 1.09M | return !strcmp( psz_namespace, psz_nodens ); |
177 | 22.6k | return !!psz_namespace == !!psz_nodens; |
178 | 1.11M | } |
179 | | |
180 | | const char * tt_node_GetAttribute( tt_namespaces_t *p_nss, const tt_node_t *p_node, |
181 | | const char *psz_name, const char *psz_namespace ) |
182 | 3.01M | { |
183 | 3.01M | const void *value; |
184 | 3.01M | char *alloc = NULL; |
185 | 3.01M | if( psz_namespace ) |
186 | 656k | { |
187 | 656k | const char *psz_prefix = tt_namespaces_GetPrefix( p_nss, psz_namespace ); |
188 | 656k | if( psz_prefix == NULL || |
189 | 656k | asprintf( &alloc, "%s:%s", psz_prefix, psz_name ) < 1 ) |
190 | 24.6k | return NULL; |
191 | 631k | psz_name = alloc; |
192 | 631k | } |
193 | 2.99M | value = vlc_dictionary_value_for_key( &p_node->attr_dict, psz_name ); |
194 | 2.99M | free( alloc ); |
195 | 2.99M | return value != kVLCDictionaryNotFound ? (const char *)value : NULL; |
196 | 3.01M | } |
197 | | |
198 | | bool tt_node_HasChild( const tt_node_t *p_node ) |
199 | 451k | { |
200 | 451k | return p_node->p_child; |
201 | 451k | } |
202 | | |
203 | | static inline bool tt_ScanReset( unsigned *a, unsigned *b, unsigned *c, |
204 | | char *d, unsigned *e ) |
205 | 277k | { |
206 | 277k | *a = *b = *c = *d = *e = 0; |
207 | 277k | return false; |
208 | 277k | } |
209 | | |
210 | | static tt_time_t tt_ParseTime( const char *s ) |
211 | 477k | { |
212 | 477k | tt_time_t t = {-1, 0}; |
213 | 477k | unsigned h1 = 0, m1 = 0, s1 = 0, d1 = 0; |
214 | 477k | char c = 0; |
215 | | |
216 | | /* Clock time */ |
217 | 477k | if( sscanf( s, "%u:%2u:%2u%c%u", &h1, &m1, &s1, &c, &d1 ) == 5 || |
218 | 161k | tt_ScanReset( &h1, &m1, &s1, &c, &d1 ) || |
219 | 161k | sscanf( s, "%u:%2u:%2u", &h1, &m1, &s1 ) == 3 || |
220 | 115k | tt_ScanReset( &h1, &m1, &s1, &c, &d1 ) ) |
221 | 361k | { |
222 | 361k | t.base = vlc_tick_from_sec(h1 * 3600 + m1 * 60 + s1); |
223 | 361k | if( c == '.' && d1 > 0 ) |
224 | 281k | { |
225 | 281k | unsigned i_den = 1; |
226 | 1.03M | for( const char *p = strchr( s, '.' ) + 1; *p && (i_den < UINT_MAX / 10); p++ ) |
227 | 749k | i_den *= 10; |
228 | 281k | t.base += vlc_tick_from_samples(d1, i_den); |
229 | 281k | } |
230 | 80.7k | else if( c == ':' ) |
231 | 24.7k | { |
232 | 24.7k | t.frames = d1; |
233 | 24.7k | } |
234 | 361k | } |
235 | 115k | else /* Offset Time */ |
236 | 115k | { |
237 | 115k | char *psz_end = (char *) s; |
238 | 115k | double v = vlc_strtod_c( s, &psz_end ); |
239 | 115k | if( psz_end != s && *psz_end ) |
240 | 110k | { |
241 | 110k | if( *psz_end == 'm' ) |
242 | 880 | { |
243 | 880 | if( *(psz_end + 1) == 's' ) |
244 | 530 | t.base = VLC_TICK_FROM_MS(v); |
245 | 350 | else |
246 | 350 | t.base = vlc_tick_from_sec(60 * v); |
247 | 880 | } |
248 | 109k | else if( *psz_end == 's' ) |
249 | 98.3k | { |
250 | 98.3k | t.base = vlc_tick_from_sec(v); |
251 | 98.3k | } |
252 | 11.1k | else if( *psz_end == 'h' ) |
253 | 944 | { |
254 | 944 | t.base = vlc_tick_from_sec(v * 3600); |
255 | 944 | } |
256 | 10.1k | else if( *psz_end == 'f' ) |
257 | 2.85k | { |
258 | 2.85k | t.base = 0; |
259 | 2.85k | t.frames = v; |
260 | 2.85k | } |
261 | | //else if( *psz_end == 't' ); |
262 | 110k | } |
263 | 115k | } |
264 | | |
265 | 477k | return t; |
266 | 477k | } |
267 | | |
268 | | bool tt_timings_Contains( const tt_timings_t *p_range, const tt_time_t *time ) |
269 | 855k | { |
270 | 855k | if( tt_time_Valid( &p_range->end ) && |
271 | 555k | tt_time_Compare( &p_range->end, time ) <= 0 ) |
272 | 4.12k | return false; |
273 | | |
274 | 851k | if( tt_time_Valid( &p_range->begin ) && |
275 | 850k | tt_time_Compare( &p_range->begin, time ) > 0 ) |
276 | 25.7k | return false; |
277 | | |
278 | 825k | return true; |
279 | 851k | } |
280 | | |
281 | | static void tt_textnode_Delete( tt_textnode_t *p_node ) |
282 | 61.5k | { |
283 | 61.5k | free( p_node->psz_text ); |
284 | 61.5k | free( p_node ); |
285 | 61.5k | } |
286 | | |
287 | | static void tt_node_FreeDictValue( void* p_value, void* p_obj ) |
288 | 679k | { |
289 | 679k | VLC_UNUSED( p_obj ); |
290 | 679k | free( p_value ); |
291 | 679k | } |
292 | | |
293 | | static void tt_node_Delete( tt_node_t *p_node ) |
294 | 496k | { |
295 | 496k | free( p_node->psz_node_name ); |
296 | 496k | free( p_node->psz_namespace ); |
297 | 496k | vlc_dictionary_clear( &p_node->attr_dict, tt_node_FreeDictValue, NULL ); |
298 | 496k | free( p_node ); |
299 | 496k | } |
300 | | |
301 | | void tt_node_RecursiveDelete( tt_node_t *p_node ) |
302 | 496k | { |
303 | 1.04M | for( ; p_node->p_child ; ) |
304 | 544k | { |
305 | 544k | tt_basenode_t *p_child = p_node->p_child; |
306 | 544k | p_node->p_child = p_child->p_next; |
307 | | |
308 | 544k | if( p_child->i_type == TT_NODE_TYPE_TEXT ) |
309 | 61.5k | tt_textnode_Delete( (tt_textnode_t *) p_child ); |
310 | 482k | else |
311 | 482k | tt_node_RecursiveDelete( (tt_node_t *) p_child ); |
312 | 544k | } |
313 | 496k | tt_node_Delete( p_node ); |
314 | 496k | } |
315 | | |
316 | | void tt_node_RemoveAttribute( tt_node_t *p_node, const char *key ) |
317 | 0 | { |
318 | 0 | vlc_dictionary_remove_value_for_key( &p_node->attr_dict, key, |
319 | 0 | tt_node_FreeDictValue, NULL ); |
320 | 0 | } |
321 | | |
322 | | int tt_node_AddAttribute( tt_node_t *p_node, const char *key, const char *value ) |
323 | 0 | { |
324 | 0 | char *p_dup = strdup( value ); |
325 | 0 | if( p_dup ) |
326 | 0 | vlc_dictionary_insert( &p_node->attr_dict, key, p_dup ); |
327 | 0 | return p_dup ? VLC_SUCCESS : VLC_EGENERIC; |
328 | 0 | } |
329 | | |
330 | | static void tt_node_ParentAddChild( tt_node_t* p_parent, tt_basenode_t *p_child ) |
331 | 544k | { |
332 | 544k | tt_basenode_t **pp_node = &p_parent->p_child; |
333 | 674k | while( *pp_node != NULL ) |
334 | 130k | pp_node = &((*pp_node)->p_next); |
335 | 544k | *pp_node = p_child; |
336 | 544k | } |
337 | | |
338 | | static tt_textnode_t *tt_textnode_NewImpl( tt_node_t *p_parent, char *psz ) |
339 | 61.5k | { |
340 | 61.5k | if( !psz ) |
341 | 0 | return NULL; |
342 | 61.5k | tt_textnode_t *p_node = calloc( 1, sizeof( *p_node ) ); |
343 | 61.5k | if( !p_node ) |
344 | 0 | { |
345 | 0 | free( psz ); |
346 | 0 | return NULL; |
347 | 0 | } |
348 | 61.5k | p_node->psz_text = psz; |
349 | 61.5k | p_node->i_type = TT_NODE_TYPE_TEXT; |
350 | 61.5k | p_node->p_parent = p_parent; |
351 | 61.5k | if( p_parent ) |
352 | 61.5k | tt_node_ParentAddChild( p_parent, (tt_basenode_t *) p_node ); |
353 | 61.5k | return p_node; |
354 | 61.5k | } |
355 | | |
356 | | tt_textnode_t *tt_textnode_New( tt_node_t *p_parent, const char *psz_text ) |
357 | 61.5k | { |
358 | 61.5k | return tt_textnode_NewImpl( p_parent, strdup( psz_text ) ); |
359 | 61.5k | } |
360 | | |
361 | | tt_textnode_t *tt_subtextnode_New( tt_node_t *p_parent, const char *psz_text, size_t len ) |
362 | 0 | { |
363 | 0 | return tt_textnode_NewImpl( p_parent, strndup( psz_text, len ) ); |
364 | 0 | } |
365 | | |
366 | | tt_node_t * tt_node_New( tt_node_t* p_parent, |
367 | | const char* psz_node_name, |
368 | | const char *psz_namespace ) |
369 | 496k | { |
370 | 496k | tt_node_t *p_node = calloc( 1, sizeof( *p_node ) ); |
371 | 496k | if( !p_node ) |
372 | 0 | return NULL; |
373 | | |
374 | 496k | p_node->i_type = TT_NODE_TYPE_ELEMENT; |
375 | 496k | p_node->psz_node_name = strdup( psz_node_name ); |
376 | 496k | const char *psz_parent_ns = tt_node_InheritNS( p_parent ); |
377 | | /* set new namespace if not same as parent */ |
378 | 496k | if( psz_namespace && |
379 | 466k | (!psz_parent_ns || strcmp( psz_namespace, psz_parent_ns )) ) |
380 | 22.7k | p_node->psz_namespace = strdup( psz_namespace ); |
381 | 473k | else |
382 | 473k | p_node->psz_namespace = NULL; |
383 | 496k | if( unlikely( p_node->psz_node_name == NULL ) ) |
384 | 0 | { |
385 | 0 | free( p_node ); |
386 | 0 | return NULL; |
387 | 0 | } |
388 | 496k | vlc_dictionary_init( &p_node->attr_dict, 0 ); |
389 | 496k | tt_time_Init( &p_node->timings.begin ); |
390 | 496k | tt_time_Init( &p_node->timings.end ); |
391 | 496k | tt_time_Init( &p_node->timings.dur ); |
392 | 496k | p_node->p_parent = p_parent; |
393 | 496k | if( p_parent ) |
394 | 482k | tt_node_ParentAddChild( p_parent, (tt_basenode_t *) p_node ); |
395 | | |
396 | 496k | return p_node; |
397 | 496k | } |
398 | | |
399 | | tt_node_t * tt_node_NewRead( xml_reader_t* reader, |
400 | | tt_namespaces_t *p_nss, tt_node_t* p_parent, |
401 | | const char* psz_node_name, const char *psz_namespace ) |
402 | 496k | { |
403 | 496k | tt_node_t *p_node = tt_node_New( p_parent, psz_node_name, psz_namespace ); |
404 | 496k | if( !p_node ) |
405 | 0 | return NULL; |
406 | | |
407 | 496k | const char* psz_value = NULL, *psz_ns = NULL; |
408 | 496k | for( const char* psz_key = xml_ReaderNextAttrNS( reader, &psz_value, &psz_ns ); |
409 | 1.17M | psz_key != NULL; |
410 | 679k | psz_key = xml_ReaderNextAttrNS( reader, &psz_value, &psz_ns ) ) |
411 | 679k | { |
412 | 679k | if( psz_ns && psz_key ) |
413 | 125k | tt_namespaces_Register( p_nss, psz_key, psz_ns ); |
414 | 679k | char *psz_val = strdup( psz_value ); |
415 | 679k | if( psz_val ) |
416 | 679k | { |
417 | 679k | vlc_dictionary_insert( &p_node->attr_dict, psz_key, psz_val ); |
418 | | |
419 | 679k | if( !strcasecmp( psz_key, "begin" ) ) |
420 | 329k | p_node->timings.begin = tt_ParseTime( psz_val ); |
421 | 349k | else if( ! strcasecmp( psz_key, "end" ) ) |
422 | 146k | p_node->timings.end = tt_ParseTime( psz_val ); |
423 | 203k | else if( ! strcasecmp( psz_key, "dur" ) ) |
424 | 1.44k | p_node->timings.dur = tt_ParseTime( psz_val ); |
425 | 201k | else if( ! strcasecmp( psz_key, "timeContainer" ) ) |
426 | 288 | p_node->timings.i_type = strcmp( psz_val, "seq" ) ? TT_TIMINGS_PARALLEL |
427 | 288 | : TT_TIMINGS_SEQUENTIAL; |
428 | 679k | } |
429 | 679k | } |
430 | 496k | return p_node; |
431 | 496k | } |
432 | | #if 0 |
433 | | static int tt_node_Skip( xml_reader_t *p_reader, const char *psz_skipped ) |
434 | | { |
435 | | size_t i_depth = 1; |
436 | | const char *psz_cur; |
437 | | |
438 | | /* need a copy as psz_skipped would point to next node after NextNode */ |
439 | | char *psz_skip = strdup( psz_skipped ); |
440 | | if(!psz_skip) |
441 | | return VLC_EGENERIC; |
442 | | |
443 | | for( ;; ) |
444 | | { |
445 | | int i_type = xml_ReaderNextNode( p_reader, &psz_cur ); |
446 | | switch( i_type ) |
447 | | { |
448 | | case XML_READER_STARTELEM: |
449 | | if( i_depth == SIZE_MAX ) |
450 | | { |
451 | | free( psz_skip ); |
452 | | return VLC_EGENERIC; |
453 | | } |
454 | | if( !xml_ReaderIsEmptyElement( p_reader ) ) |
455 | | i_depth++; |
456 | | break; |
457 | | |
458 | | case XML_READER_ENDELEM: |
459 | | if( !strcmp( psz_cur, psz_skip ) ) |
460 | | { |
461 | | free( psz_skip ); |
462 | | if( i_depth != 1 ) |
463 | | return VLC_EGENERIC; |
464 | | return VLC_SUCCESS; |
465 | | } |
466 | | else |
467 | | { |
468 | | if( i_depth == 1 ) |
469 | | { |
470 | | free( psz_skip ); |
471 | | return VLC_EGENERIC; |
472 | | } |
473 | | i_depth--; |
474 | | } |
475 | | break; |
476 | | |
477 | | default: |
478 | | if( i_type <= XML_READER_NONE ) |
479 | | { |
480 | | free( psz_skip ); |
481 | | return VLC_EGENERIC; |
482 | | } |
483 | | break; |
484 | | } |
485 | | } |
486 | | vlc_assert_unreachable(); |
487 | | return VLC_EGENERIC; |
488 | | } |
489 | | #endif |
490 | | int tt_nodes_Read( xml_reader_t *p_reader, tt_namespaces_t *p_nss, tt_node_t *p_root_node ) |
491 | 13.7k | { |
492 | 13.7k | size_t i_depth = 0; |
493 | 13.7k | tt_node_t *p_node = p_root_node; |
494 | | |
495 | 13.7k | do |
496 | 984k | { |
497 | 984k | const char *psz_node_name, *psz_node_namespace; |
498 | 984k | int i_type = xml_ReaderNextNodeNS( p_reader, &psz_node_name, &psz_node_namespace ); |
499 | | /* !warn read empty state now as attributes reading will **** it up */ |
500 | 984k | bool b_empty = xml_ReaderIsEmptyElement( p_reader ); |
501 | | |
502 | 984k | if( i_type <= XML_READER_NONE ) |
503 | 13.3k | break; |
504 | | |
505 | 971k | switch( i_type ) |
506 | 971k | { |
507 | 0 | default: |
508 | 0 | break; |
509 | | |
510 | 482k | case XML_READER_STARTELEM: |
511 | 482k | { |
512 | 482k | tt_namespaces_Register( p_nss, psz_node_name, psz_node_namespace ); |
513 | 482k | tt_node_t *p_newnode = tt_node_NewRead( p_reader, p_nss, p_node, |
514 | 482k | psz_node_name, |
515 | 482k | psz_node_namespace ); |
516 | 482k | if( !p_newnode ) |
517 | 0 | return VLC_EGENERIC; |
518 | 482k | if( !b_empty ) |
519 | 455k | { |
520 | 455k | p_node = p_newnode; |
521 | 455k | i_depth++; |
522 | 455k | } |
523 | 482k | break; |
524 | 482k | } |
525 | | |
526 | 61.5k | case XML_READER_TEXT: |
527 | 61.5k | { |
528 | 61.5k | tt_textnode_t *p_textnode = tt_textnode_New( p_node, psz_node_name ); |
529 | 61.5k | VLC_UNUSED(p_textnode); |
530 | 61.5k | } |
531 | 61.5k | break; |
532 | | |
533 | 427k | case XML_READER_ENDELEM: |
534 | 427k | { |
535 | 427k | if( !tt_node_Match( p_node, psz_node_name, psz_node_namespace ) ) |
536 | 396 | return VLC_EGENERIC; |
537 | | |
538 | 427k | if( i_depth == 0 ) |
539 | 11.8k | { |
540 | 11.8k | if( p_node != p_root_node ) |
541 | 0 | return VLC_EGENERIC; |
542 | 11.8k | break; /* END */ |
543 | 11.8k | } |
544 | 415k | i_depth--; |
545 | 415k | p_node = p_node->p_parent; |
546 | 415k | break; |
547 | 427k | } |
548 | 971k | } |
549 | 971k | } while( 1 ); |
550 | | |
551 | 13.3k | return VLC_SUCCESS; |
552 | 13.7k | } |
553 | | |
554 | | |
555 | | /* Timings storage */ |
556 | | static int tt_bsearch_searchkey_Compare( const void *key, const void *other ) |
557 | 2.66M | { |
558 | 2.66M | struct tt_searchkey *p_key = (struct tt_searchkey *) key; |
559 | 2.66M | tt_time_t time = *((tt_time_t *) other); |
560 | 2.66M | p_key->p_last = (tt_time_t *) other; |
561 | 2.66M | return tt_time_Compare( &p_key->time, &time ); |
562 | 2.66M | } |
563 | | |
564 | | size_t tt_timings_FindLowerIndex( const tt_time_t *p_times, size_t i_times, tt_time_t time, bool *pb_found ) |
565 | 741k | { |
566 | 741k | size_t i_index = 0; |
567 | 741k | if( p_times ) |
568 | 728k | { |
569 | 728k | struct tt_searchkey key; |
570 | 728k | key.time = time; |
571 | 728k | key.p_last = NULL; |
572 | | |
573 | 728k | tt_time_t *lookup = bsearch( &key, p_times, i_times, |
574 | 728k | sizeof(tt_time_t), tt_bsearch_searchkey_Compare ); |
575 | 728k | if( lookup ) |
576 | 418k | key.p_last = lookup; |
577 | 728k | *pb_found = !!lookup; |
578 | | |
579 | | /* Compute index from last visited */ |
580 | 728k | i_index = (key.p_last - p_times); |
581 | 728k | if( tt_time_Compare( &p_times[i_index], &time ) < 0 ) |
582 | 198k | i_index++; |
583 | 728k | } |
584 | 13.3k | else *pb_found = false; |
585 | 741k | return i_index; |
586 | 741k | } |
587 | | |
588 | | static void tt_bsearch_Insert( tt_time_t **pp_times, size_t *pi_times, tt_time_t time ) |
589 | 741k | { |
590 | 741k | bool b_exists; |
591 | 741k | size_t i_index = tt_timings_FindLowerIndex( *pp_times, *pi_times, time, &b_exists ); |
592 | 741k | if( b_exists ) |
593 | 418k | return; |
594 | | |
595 | 323k | if( SIZE_MAX / sizeof(tt_time_t) < (*pi_times + 1) ) |
596 | 0 | return; |
597 | | |
598 | 323k | tt_time_t *p_array = realloc( *pp_times, (*pi_times + 1) * sizeof(tt_time_t) ); |
599 | 323k | if( !p_array ) |
600 | 0 | return; |
601 | 323k | *pp_times = p_array; |
602 | | |
603 | 323k | if( *pi_times > 0 ) |
604 | 310k | { |
605 | 310k | memmove( &p_array[i_index + 1], |
606 | 310k | &p_array[i_index], |
607 | 310k | (*pi_times - i_index) * sizeof(tt_time_t) ); |
608 | 310k | } |
609 | | |
610 | 323k | p_array[i_index] = time; |
611 | 323k | *pi_times += 1; |
612 | 323k | } |
613 | | |
614 | | |
615 | | /* Timings storage */ |
616 | | static void tt_timings_MergeParallel( const tt_timings_t *p_ref, tt_timings_t *p_local ) |
617 | 475k | { |
618 | 475k | if( tt_time_Valid( &p_local->begin ) ) |
619 | 303k | p_local->begin = tt_time_Add( p_local->begin, p_ref->begin ); |
620 | 171k | else |
621 | 171k | p_local->begin = p_ref->begin; |
622 | | |
623 | 475k | if( tt_time_Valid( &p_local->end ) ) |
624 | 132k | { |
625 | 132k | p_local->end = tt_time_Add( p_local->end, p_ref->begin ); |
626 | 132k | } |
627 | 342k | else if( tt_time_Valid( &p_local->dur ) && tt_time_Valid( &p_local->begin ) ) |
628 | 1.08k | { |
629 | 1.08k | p_local->end = tt_time_Add( p_local->begin, p_local->dur ); |
630 | 1.08k | } |
631 | 341k | else p_local->end = p_ref->end; |
632 | | |
633 | | /* Enforce contained duration */ |
634 | | |
635 | 475k | if( tt_time_Valid( &p_ref->end ) && tt_time_Compare( &p_local->end, &p_ref->end ) > 0 ) |
636 | 114k | p_local->end = p_ref->end; |
637 | | |
638 | | /* Just for consistency */ |
639 | 475k | if( tt_time_Valid( &p_local->begin ) && tt_time_Valid( &p_local->end ) ) |
640 | 267k | p_local->dur = tt_time_Sub( p_local->end, p_local->begin ); |
641 | 475k | } |
642 | | |
643 | | static void tt_timings_MergeSequential( const tt_timings_t *p_restrict, |
644 | | const tt_timings_t *p_prevref, tt_timings_t *p_local ) |
645 | 0 | { |
646 | 0 | if( tt_time_Valid( &p_local->begin ) ) |
647 | 0 | p_local->begin = tt_time_Add( p_local->begin, p_prevref->end ); |
648 | 0 | else |
649 | 0 | p_local->begin = p_prevref->end; |
650 | |
|
651 | 0 | if( tt_time_Valid( &p_local->end ) ) |
652 | 0 | { |
653 | 0 | p_local->end = tt_time_Add( p_local->end, p_prevref->end ); |
654 | 0 | } |
655 | 0 | else if( tt_time_Valid( &p_local->dur ) && tt_time_Valid( &p_local->begin ) ) |
656 | 0 | { |
657 | 0 | p_local->end = tt_time_Add( p_local->begin, p_local->dur ); |
658 | 0 | } |
659 | | |
660 | | /* Enforce contained duration */ |
661 | 0 | if( tt_time_Valid( &p_restrict->end ) && tt_time_Compare( &p_local->end, &p_restrict->end ) > 0 ) |
662 | 0 | p_local->end = p_restrict->end; |
663 | | |
664 | | /* Just for consistency */ |
665 | 0 | if( tt_time_Valid( &p_local->begin ) && tt_time_Valid( &p_local->end ) ) |
666 | 0 | p_local->dur = tt_time_Sub( p_local->end, p_local->begin ); |
667 | 0 | } |
668 | | |
669 | | void tt_timings_Resolve( tt_basenode_t *p_child, const tt_timings_t *p_container_timings, |
670 | | tt_time_t **pp_array, size_t *pi_count ) |
671 | 488k | { |
672 | 488k | const tt_node_t *p_prevnode = NULL; |
673 | 1.02M | for( ; p_child; p_child = p_child->p_next ) |
674 | 534k | { |
675 | 534k | if( p_child->i_type != TT_NODE_TYPE_ELEMENT ) |
676 | 58.8k | continue; |
677 | | |
678 | 475k | tt_node_t *p_childnode = (tt_node_t *) p_child; |
679 | 475k | if( p_container_timings->i_type == TT_TIMINGS_SEQUENTIAL ) |
680 | 0 | { |
681 | 0 | if( p_prevnode == NULL ) /* First */ |
682 | 0 | tt_timings_MergeParallel( p_container_timings, &p_childnode->timings ); |
683 | 0 | else |
684 | 0 | tt_timings_MergeSequential( p_container_timings, |
685 | 0 | &p_prevnode->timings, &p_childnode->timings ); |
686 | 0 | } |
687 | 475k | else |
688 | 475k | { |
689 | 475k | tt_timings_MergeParallel( p_container_timings, &p_childnode->timings ); |
690 | 475k | } |
691 | | |
692 | 475k | if( tt_time_Valid( &p_childnode->timings.begin ) ) |
693 | 474k | tt_bsearch_Insert( pp_array, pi_count, p_childnode->timings.begin ); |
694 | | |
695 | 475k | if( tt_time_Valid( &p_childnode->timings.end ) ) |
696 | 267k | tt_bsearch_Insert( pp_array, pi_count, p_childnode->timings.end ); |
697 | | |
698 | 475k | p_prevnode = p_childnode; |
699 | | |
700 | 475k | tt_timings_Resolve( p_childnode->p_child, &p_childnode->timings, |
701 | 475k | pp_array, pi_count ); |
702 | 475k | } |
703 | 488k | } |