box: Remove redundant 'if's for freeing.
[L-SMASH.git] / core / timeline.c
blob21e781630a406cce6a1114203da0f0af56f78a08
1 /*****************************************************************************
2 * timeline.c:
3 *****************************************************************************
4 * Copyright (C) 2011-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #ifdef LSMASH_DEMUXER_ENABLED
25 #include "common/internal.h" /* must be placed first */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <inttypes.h>
31 #include "box.h"
33 #include "codecs/mp4a.h"
34 #include "codecs/mp4sys.h"
35 #include "codecs/description.h"
37 #define NO_RANDOM_ACCESS_POINT 0xffffffff
39 typedef struct
41 uint64_t data_offset;
42 uint64_t length;
43 uint32_t number; /* useless at present */
44 lsmash_file_t *file;
45 } isom_portable_chunk_t;
47 typedef struct
49 uint64_t pos;
50 uint32_t duration;
51 uint32_t offset;
52 uint32_t length;
53 uint32_t index;
54 isom_portable_chunk_t *chunk;
55 lsmash_sample_property_t prop;
56 } isom_sample_info_t;
58 typedef struct
60 uint64_t pos; /* position of the first sample in this bunch */
61 uint32_t duration; /* duration in media timescale each sample has */
62 uint32_t offset; /* offset between composition time and decoding time each sample has */
63 uint32_t length; /* data size each sample has */
64 uint32_t index; /* sample_description_index applied to each sample */
65 isom_portable_chunk_t *chunk; /* chunk samples belong to */
66 lsmash_sample_property_t prop; /* property applied to each sample */
67 uint32_t sample_count; /* number of samples in this bunch */
68 } isom_lpcm_bunch_t;
70 static const lsmash_class_t lsmash_timeline_class =
72 "timeline"
75 typedef struct isom_timeline_tag isom_timeline_t;
77 struct isom_timeline_tag
79 const lsmash_class_t *class;
80 uint32_t track_ID;
81 uint32_t movie_timescale;
82 uint32_t media_timescale;
83 uint32_t sample_count;
84 uint32_t max_sample_size;
85 uint32_t ctd_shift; /* shift from composition to decode timeline */
86 uint64_t media_duration;
87 uint64_t track_duration;
88 uint32_t last_accessed_sample_number;
89 uint64_t last_accessed_sample_dts;
90 uint32_t last_accessed_lpcm_bunch_number;
91 uint32_t last_accessed_lpcm_bunch_duration;
92 uint32_t last_accessed_lpcm_bunch_sample_count;
93 uint32_t last_accessed_lpcm_bunch_first_sample_number;
94 uint64_t last_accessed_lpcm_bunch_dts;
95 lsmash_entry_list_t edit_list [1]; /* list of edits */
96 lsmash_entry_list_t chunk_list[1]; /* list of chunks */
97 lsmash_entry_list_t info_list [1]; /* list of sample info */
98 lsmash_entry_list_t bunch_list[1]; /* list of LPCM bunch */
99 int (*get_dts)( isom_timeline_t *timeline, uint32_t sample_number, uint64_t *dts );
100 int (*get_cts)( isom_timeline_t *timeline, uint32_t sample_number, uint64_t *cts );
101 int (*get_sample_duration)( isom_timeline_t *timeline, uint32_t sample_number, uint32_t *sample_duration );
102 lsmash_sample_t *(*get_sample)( isom_timeline_t *timeline, uint32_t sample_number );
103 int (*get_sample_info)( isom_timeline_t *timeline, uint32_t sample_number, lsmash_sample_t *sample );
104 int (*get_sample_property)( isom_timeline_t *timeline, uint32_t sample_number, lsmash_sample_property_t *prop );
105 int (*check_sample_existence)( isom_timeline_t *timeline, uint32_t sample_number );
108 static isom_timeline_t *isom_get_timeline( lsmash_root_t *root, uint32_t track_ID )
110 if( isom_check_initializer_present( root ) < 0
111 || track_ID == 0
112 || !root->file->timeline )
113 return NULL;
114 for( lsmash_entry_t *entry = root->file->timeline->head; entry; entry = entry->next )
116 isom_timeline_t *timeline = (isom_timeline_t *)entry->data;
117 if( !timeline )
118 return NULL;
119 if( timeline->track_ID == track_ID )
120 return timeline;
122 return NULL;
125 static isom_timeline_t *isom_create_timeline( void )
127 isom_timeline_t *timeline = lsmash_malloc_zero( sizeof(isom_timeline_t) );
128 if( !timeline )
129 return NULL;
130 timeline->class = &lsmash_timeline_class;
131 lsmash_init_entry_list( timeline->edit_list );
132 lsmash_init_entry_list( timeline->chunk_list );
133 lsmash_init_entry_list( timeline->info_list );
134 lsmash_init_entry_list( timeline->bunch_list );
135 return timeline;
138 static void isom_destruct_timeline_direct( isom_timeline_t *timeline )
140 if( !timeline )
141 return;
142 lsmash_remove_entries( timeline->edit_list, NULL );
143 lsmash_remove_entries( timeline->chunk_list, NULL ); /* chunk data must be already freed. */
144 lsmash_remove_entries( timeline->info_list, NULL );
145 lsmash_remove_entries( timeline->bunch_list, NULL );
146 lsmash_free( timeline );
149 void isom_remove_timelines( lsmash_file_t *file )
151 if( !file
152 || !file->timeline )
153 return;
154 lsmash_remove_list( file->timeline, isom_destruct_timeline_direct );
157 void lsmash_destruct_timeline( lsmash_root_t *root, uint32_t track_ID )
159 if( track_ID == 0
160 || !root
161 || !root->file
162 || !root->file->timeline )
163 return;
164 for( lsmash_entry_t *entry = root->file->timeline->head; entry; entry = entry->next )
166 isom_timeline_t *timeline = (isom_timeline_t *)entry->data;
167 if( !timeline )
168 continue;
169 if( timeline->track_ID == track_ID )
171 lsmash_remove_entry_direct( root->file->timeline, entry, isom_destruct_timeline_direct );
172 break;
177 static void isom_get_qt_fixed_comp_audio_sample_quants
179 isom_timeline_t *timeline,
180 isom_sample_entry_t *description,
181 uint32_t *samples_per_packet,
182 uint32_t *constant_sample_size
185 isom_audio_entry_t *audio = (isom_audio_entry_t *)description;
186 if( audio->version == 0 )
188 uint32_t dummy;
189 if( !isom_get_implicit_qt_fixed_comp_audio_sample_quants( audio, samples_per_packet, constant_sample_size, &dummy ) )
191 /* LPCM */
192 if( !isom_is_lpcm_audio( audio ) )
193 lsmash_log( timeline, LSMASH_LOG_WARNING, "unsupported implicit sample table!\n" );
194 *samples_per_packet = 1;
195 *constant_sample_size = (audio->samplesize * audio->channelcount) / 8;
198 else if( audio->version == 1 )
200 *samples_per_packet = audio->samplesPerPacket;
201 *constant_sample_size = audio->bytesPerFrame;
203 else /* if( audio->version == 2 ) */
205 *samples_per_packet = audio->constLPCMFramesPerAudioPacket;
206 *constant_sample_size = audio->constBytesPerAudioPacket;
210 static int isom_is_qt_fixed_compressed_audio
212 isom_sample_entry_t *description
215 if( (description->manager & LSMASH_VIDEO_DESCRIPTION) || !isom_is_qt_audio( description->type ) )
216 return 0;
217 /* LPCM is a special case of fixed compression. */
218 return (((isom_audio_entry_t *)description)->compression_ID != QT_AUDIO_COMPRESSION_ID_VARIABLE_COMPRESSION);
221 static int isom_add_sample_info_entry( isom_timeline_t *timeline, isom_sample_info_t *src_info )
223 isom_sample_info_t *dst_info = lsmash_malloc( sizeof(isom_sample_info_t) );
224 if( !dst_info )
225 return -1;
226 if( lsmash_add_entry( timeline->info_list, dst_info ) < 0 )
228 lsmash_free( dst_info );
229 return -1;
231 *dst_info = *src_info;
232 return 0;
235 static int isom_add_lpcm_bunch_entry( isom_timeline_t *timeline, isom_lpcm_bunch_t *src_bunch )
237 isom_lpcm_bunch_t *dst_bunch = lsmash_malloc( sizeof(isom_lpcm_bunch_t) );
238 if( !dst_bunch )
239 return -1;
240 if( lsmash_add_entry( timeline->bunch_list, dst_bunch ) < 0 )
242 lsmash_free( dst_bunch );
243 return -1;
245 *dst_bunch = *src_bunch;
246 return 0;
249 static int isom_add_portable_chunk_entry( isom_timeline_t *timeline, isom_portable_chunk_t *src_chunk )
251 isom_portable_chunk_t *dst_chunk = lsmash_malloc( sizeof(isom_portable_chunk_t) );
252 if( !dst_chunk )
253 return -1;
254 if( lsmash_add_entry( timeline->chunk_list, dst_chunk ) < 0 )
256 lsmash_free( dst_chunk );
257 return -1;
259 *dst_chunk = *src_chunk;
260 return 0;
263 static int isom_compare_lpcm_sample_info( isom_lpcm_bunch_t *bunch, isom_sample_info_t *info )
265 return info->duration != bunch->duration
266 || info->offset != bunch->offset
267 || info->length != bunch->length
268 || info->index != bunch->index
269 || info->chunk != bunch->chunk;
272 static void isom_update_bunch( isom_lpcm_bunch_t *bunch, isom_sample_info_t *info )
274 bunch->pos = info->pos;
275 bunch->duration = info->duration;
276 bunch->offset = info->offset;
277 bunch->length = info->length;
278 bunch->index = info->index;
279 bunch->chunk = info->chunk;
280 bunch->prop = info->prop;
281 bunch->sample_count = 1;
284 static isom_lpcm_bunch_t *isom_get_bunch( isom_timeline_t *timeline, uint32_t sample_number )
286 if( sample_number >= timeline->last_accessed_lpcm_bunch_first_sample_number
287 && sample_number < timeline->last_accessed_lpcm_bunch_first_sample_number + timeline->last_accessed_lpcm_bunch_sample_count )
288 /* Get from the last accessed LPCM bunch. */
289 return (isom_lpcm_bunch_t *)lsmash_get_entry_data( timeline->bunch_list, timeline->last_accessed_lpcm_bunch_number );
290 uint32_t first_sample_number_in_next_bunch;
291 uint32_t bunch_number = 1;
292 uint64_t bunch_dts;
293 if( timeline->last_accessed_lpcm_bunch_first_sample_number
294 && timeline->last_accessed_lpcm_bunch_first_sample_number <= sample_number )
296 first_sample_number_in_next_bunch = timeline->last_accessed_lpcm_bunch_first_sample_number + timeline->last_accessed_lpcm_bunch_sample_count;
297 bunch_number += timeline->last_accessed_lpcm_bunch_number;
298 bunch_dts = timeline->last_accessed_lpcm_bunch_dts
299 + timeline->last_accessed_lpcm_bunch_duration * timeline->last_accessed_lpcm_bunch_sample_count;
301 else
303 /* Seek from the first LPCM bunch. */
304 first_sample_number_in_next_bunch = 1;
305 bunch_dts = 0;
307 isom_lpcm_bunch_t *bunch = (isom_lpcm_bunch_t *)lsmash_get_entry_data( timeline->bunch_list, bunch_number++ );
308 if( !bunch )
309 return NULL;
310 first_sample_number_in_next_bunch += bunch->sample_count;
311 while( sample_number >= first_sample_number_in_next_bunch )
313 bunch_dts += bunch->duration * bunch->sample_count;
314 bunch = (isom_lpcm_bunch_t *)lsmash_get_entry_data( timeline->bunch_list, bunch_number++ );
315 if( !bunch )
316 return NULL;
317 first_sample_number_in_next_bunch += bunch->sample_count;
319 timeline->last_accessed_lpcm_bunch_dts = bunch_dts;
320 timeline->last_accessed_lpcm_bunch_number = bunch_number - 1;
321 timeline->last_accessed_lpcm_bunch_duration = bunch->duration;
322 timeline->last_accessed_lpcm_bunch_sample_count = bunch->sample_count;
323 timeline->last_accessed_lpcm_bunch_first_sample_number = first_sample_number_in_next_bunch - bunch->sample_count;
324 return bunch;
327 static int isom_get_dts_from_info_list( isom_timeline_t *timeline, uint32_t sample_number, uint64_t *dts )
329 if( sample_number == timeline->last_accessed_sample_number )
330 *dts = timeline->last_accessed_sample_dts;
331 else if( sample_number == 1 )
332 *dts = 0;
333 else if( sample_number == timeline->last_accessed_sample_number + 1 )
335 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, timeline->last_accessed_sample_number );
336 if( !info )
337 return -1;
338 *dts = timeline->last_accessed_sample_dts + info->duration;
340 else if( sample_number == timeline->last_accessed_sample_number - 1 )
342 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, timeline->last_accessed_sample_number - 1 );
343 if( !info )
344 return -1;
345 *dts = timeline->last_accessed_sample_dts - info->duration;
347 else
349 *dts = 0;
350 uint32_t distance = sample_number - 1;
351 lsmash_entry_t *entry;
352 for( entry = timeline->info_list->head; entry; entry = entry->next )
354 isom_sample_info_t *info = (isom_sample_info_t *)entry->data;
355 if( !info )
356 return -1;
357 if( distance-- == 0 )
358 break;
359 *dts += info->duration;
361 if( !entry )
362 return -1;
364 /* Note: last_accessed_sample_number is always updated together with last_accessed_sample_dts, and vice versa. */
365 timeline->last_accessed_sample_dts = *dts;
366 timeline->last_accessed_sample_number = sample_number;
367 return 0;
370 static int isom_get_cts_from_info_list( isom_timeline_t *timeline, uint32_t sample_number, uint64_t *cts )
372 if( isom_get_dts_from_info_list( timeline, sample_number, cts ) < 0 )
373 return -1;
374 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, sample_number );
375 if( !info )
376 return -1;
377 *cts = timeline->ctd_shift ? (*cts + (int32_t)info->offset) : (*cts + info->offset);
378 return 0;
381 static int isom_get_dts_from_bunch_list( isom_timeline_t *timeline, uint32_t sample_number, uint64_t *dts )
383 isom_lpcm_bunch_t *bunch = isom_get_bunch( timeline, sample_number );
384 if( !bunch )
385 return -1;
386 *dts = timeline->last_accessed_lpcm_bunch_dts + (sample_number - timeline->last_accessed_lpcm_bunch_first_sample_number) * bunch->duration;
387 return 0;
390 static int isom_get_cts_from_bunch_list( isom_timeline_t *timeline, uint32_t sample_number, uint64_t *cts )
392 isom_lpcm_bunch_t *bunch = isom_get_bunch( timeline, sample_number );
393 if( !bunch )
394 return -1;
395 *cts = timeline->last_accessed_lpcm_bunch_dts + (sample_number - timeline->last_accessed_lpcm_bunch_first_sample_number) * bunch->duration + bunch->offset;
396 return 0;
399 static int isom_get_sample_duration_from_info_list( isom_timeline_t *timeline, uint32_t sample_number, uint32_t *sample_duration )
401 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, sample_number );
402 if( !info )
403 return -1;
404 *sample_duration = info->duration;
405 return 0;
408 static int isom_get_sample_duration_from_bunch_list( isom_timeline_t *timeline, uint32_t sample_number, uint32_t *sample_duration )
410 isom_lpcm_bunch_t *bunch = isom_get_bunch( timeline, sample_number );
411 if( !bunch )
412 return -1;
413 *sample_duration = bunch->duration;
414 return 0;
417 static int isom_check_sample_existence_in_info_list( isom_timeline_t *timeline, uint32_t sample_number )
419 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, sample_number );
420 if( !info || !info->chunk )
421 return 0;
422 return !!info->chunk->file;
425 static int isom_check_sample_existence_in_bunch_list( isom_timeline_t *timeline, uint32_t sample_number )
427 isom_lpcm_bunch_t *bunch = isom_get_bunch( timeline, sample_number );
428 if( !bunch || !bunch->chunk )
429 return 0;
430 return !!bunch->chunk->file;
433 static lsmash_sample_t *isom_read_sample_data_from_stream
435 lsmash_file_t *file,
436 isom_timeline_t *timeline,
437 uint32_t sample_length,
438 uint64_t sample_pos
441 lsmash_sample_t *sample = lsmash_create_sample( 0 );
442 if( !sample )
443 return NULL;
444 lsmash_bs_t *bs = file->bs;
445 lsmash_bs_read_seek( bs, sample_pos, SEEK_SET );
446 sample->data = lsmash_bs_get_bytes( bs, sample_length );
447 if( !sample->data )
449 lsmash_delete_sample( sample );
450 return NULL;
452 return sample;
455 static lsmash_sample_t *isom_get_lpcm_sample_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number )
457 isom_lpcm_bunch_t *bunch = isom_get_bunch( timeline, sample_number );
458 if( !bunch
459 || !bunch->chunk )
460 return NULL;
461 /* Get data of a sample from the stream. */
462 uint64_t sample_number_offset = sample_number - timeline->last_accessed_lpcm_bunch_first_sample_number;
463 uint64_t sample_pos = bunch->pos + sample_number_offset * bunch->length;
464 lsmash_sample_t *sample = isom_read_sample_data_from_stream( bunch->chunk->file, timeline, bunch->length, sample_pos );
465 if( !sample )
466 return NULL;
467 /* Get sample info. */
468 sample->dts = timeline->last_accessed_lpcm_bunch_dts + sample_number_offset * bunch->duration;
469 sample->cts = timeline->ctd_shift ? (sample->dts + (int32_t)bunch->offset) : (sample->dts + bunch->offset);
470 sample->pos = sample_pos;
471 sample->length = bunch->length;
472 sample->index = bunch->index;
473 sample->prop = bunch->prop;
474 return sample;
477 static lsmash_sample_t *isom_get_sample_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number )
479 uint64_t dts;
480 if( isom_get_dts_from_info_list( timeline, sample_number, &dts ) < 0 )
481 return NULL;
482 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, sample_number );
483 if( !info
484 || !info->chunk )
485 return NULL;
486 /* Get data of a sample from the stream. */
487 lsmash_sample_t *sample = isom_read_sample_data_from_stream( info->chunk->file, timeline, info->length, info->pos );
488 if( !sample )
489 return NULL;
490 /* Get sample info. */
491 sample->dts = dts;
492 sample->cts = timeline->ctd_shift ? (dts + (int32_t)info->offset) : (dts + info->offset);
493 sample->pos = info->pos;
494 sample->length = info->length;
495 sample->index = info->index;
496 sample->prop = info->prop;
497 return sample;
500 static int isom_get_lpcm_sample_info_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number, lsmash_sample_t *sample )
502 isom_lpcm_bunch_t *bunch = isom_get_bunch( timeline, sample_number );
503 if( !bunch )
504 return -1;
505 uint64_t sample_number_offset = sample_number - timeline->last_accessed_lpcm_bunch_first_sample_number;
506 sample->dts = timeline->last_accessed_lpcm_bunch_dts + sample_number_offset * bunch->duration;
507 sample->cts = timeline->ctd_shift ? (sample->dts + (int32_t)bunch->offset) : (sample->dts + bunch->offset);
508 sample->pos = bunch->pos + sample_number_offset * bunch->length;
509 sample->length = bunch->length;
510 sample->index = bunch->index;
511 sample->prop = bunch->prop;
512 return 0;
515 static int isom_get_sample_info_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number, lsmash_sample_t *sample )
517 uint64_t dts;
518 if( isom_get_dts_from_info_list( timeline, sample_number, &dts ) < 0 )
519 return -1;
520 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, sample_number );
521 if( !info )
522 return -1;
523 sample->dts = dts;
524 sample->cts = timeline->ctd_shift ? (dts + (int32_t)info->offset) : (dts + info->offset);
525 sample->pos = info->pos;
526 sample->length = info->length;
527 sample->index = info->index;
528 sample->prop = info->prop;
529 return 0;
532 static int isom_get_lpcm_sample_property_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number, lsmash_sample_property_t *prop )
534 memset( prop, 0, sizeof(lsmash_sample_property_t) );
535 prop->ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
536 return 0;
539 static int isom_get_sample_property_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number, lsmash_sample_property_t *prop )
541 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, sample_number );
542 if( !info )
543 return -1;
544 *prop = info->prop;
545 return 0;
548 static inline void isom_increment_sample_number_in_entry
550 uint32_t *sample_number_in_entry,
551 lsmash_entry_t **entry,
552 uint32_t sample_count
555 if( *sample_number_in_entry == sample_count )
557 *sample_number_in_entry = 1;
558 *entry = (*entry)->next;
560 else
561 *sample_number_in_entry += 1;
564 static inline isom_sgpd_t *isom_select_appropriate_sgpd
566 isom_sgpd_t *sgpd,
567 isom_sgpd_t *sgpd_frag,
568 uint32_t *group_description_index
571 if( sgpd_frag && *group_description_index >= 0x10000 )
573 /* The specification doesn't define 0x10000 explicitly, however says that there must be fewer than
574 * 65536 group definitions for this track and grouping type in the sample table in the Movie Box.
575 * So, we assume 0x10000 is equivalent to 0. */
576 *group_description_index -= 0x10000;
577 return sgpd_frag;
579 else
580 return sgpd;
583 static int isom_get_roll_recovery_grouping_info
585 isom_timeline_t *timeline,
586 lsmash_entry_t **sbgp_roll_entry,
587 isom_sgpd_t *sgpd_roll,
588 isom_sgpd_t *sgpd_frag_roll,
589 uint32_t *sample_number_in_sbgp_roll_entry,
590 isom_sample_info_t *info,
591 uint32_t sample_number
594 isom_group_assignment_entry_t *assignment = (isom_group_assignment_entry_t *)(*sbgp_roll_entry)->data;
595 if( !assignment )
596 return -1;
597 if( assignment->group_description_index )
599 uint32_t group_description_index = assignment->group_description_index;
600 isom_sgpd_t *sgpd = isom_select_appropriate_sgpd( sgpd_roll, sgpd_frag_roll, &group_description_index );
601 isom_roll_entry_t *roll_data = (isom_roll_entry_t *)lsmash_get_entry_data( sgpd->list, group_description_index );
602 if( roll_data )
604 if( roll_data->roll_distance > 0 )
606 /* post-roll */
607 info->prop.post_roll.complete = sample_number + roll_data->roll_distance;
608 if( info->prop.ra_flags == ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE )
609 info->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START;
611 else if( roll_data->roll_distance < 0 )
613 /* pre-roll */
614 info->prop.pre_roll.distance = -roll_data->roll_distance;
615 if( info->prop.ra_flags == ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE )
616 info->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_PRE_ROLL_END;
619 else if( *sample_number_in_sbgp_roll_entry == 1 && group_description_index )
620 lsmash_log( timeline, LSMASH_LOG_WARNING, "a description of roll recoveries is not found in the Sample Group Description Box.\n" );
622 isom_increment_sample_number_in_entry( sample_number_in_sbgp_roll_entry, sbgp_roll_entry, assignment->sample_count );
623 return 0;
626 static int isom_get_random_access_point_grouping_info
628 isom_timeline_t *timeline,
629 lsmash_entry_t **sbgp_rap_entry,
630 isom_sgpd_t *sgpd_rap,
631 isom_sgpd_t *sgpd_frag_rap,
632 uint32_t *sample_number_in_sbgp_rap_entry,
633 isom_sample_info_t *info,
634 uint32_t *distance
637 isom_group_assignment_entry_t *assignment = (isom_group_assignment_entry_t *)(*sbgp_rap_entry)->data;
638 if( !assignment )
639 return -1;
640 if( assignment->group_description_index && (info->prop.ra_flags == ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE) )
642 uint32_t group_description_index = assignment->group_description_index;
643 isom_sgpd_t *sgpd = isom_select_appropriate_sgpd( sgpd_rap, sgpd_frag_rap, &group_description_index );
644 isom_rap_entry_t *rap_data = (isom_rap_entry_t *)lsmash_get_entry_data( sgpd->list, group_description_index );
645 if( rap_data )
647 /* If this is not an open RAP, we treat it as an unknown RAP since non-IDR sample could make a closed GOP. */
648 info->prop.ra_flags |= (rap_data->num_leading_samples_known && !!rap_data->num_leading_samples)
649 ? ISOM_SAMPLE_RANDOM_ACCESS_FLAG_OPEN_RAP
650 : ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
651 *distance = 0;
653 else if( *sample_number_in_sbgp_rap_entry == 1 && group_description_index )
654 lsmash_log( timeline, LSMASH_LOG_WARNING, "a description of random access points is not found in the Sample Group Description Box.\n" );
656 isom_increment_sample_number_in_entry( sample_number_in_sbgp_rap_entry, sbgp_rap_entry, assignment->sample_count );
657 return 0;
660 int lsmash_construct_timeline( lsmash_root_t *root, uint32_t track_ID )
662 if( isom_check_initializer_present( root ) < 0 )
663 return -1;
664 lsmash_file_t *file = root->file;
665 if( !file->moov
666 || !file->moov->mvhd
667 || file->moov->mvhd->timescale == 0 )
668 return -1;
669 /* Get track by track_ID. */
670 isom_trak_t *trak = isom_get_trak( file, track_ID );
671 if( !trak
672 || !trak->tkhd
673 || !trak->mdia
674 || !trak->mdia->mdhd
675 || trak->mdia->mdhd->timescale == 0
676 || !trak->mdia->minf
677 || !trak->mdia->minf->stbl )
678 return -1;
679 /* Create a timeline list if it doesn't exist. */
680 if( !file->timeline )
682 file->timeline = lsmash_create_entry_list();
683 if( !file->timeline )
684 return -1;
686 /* Create a timeline. */
687 isom_timeline_t *timeline = isom_create_timeline();
688 if( !timeline )
689 return -1;
690 timeline->track_ID = track_ID;
691 timeline->movie_timescale = file->moov->mvhd->timescale;
692 timeline->media_timescale = trak->mdia->mdhd->timescale;
693 timeline->track_duration = trak->tkhd->duration;
694 /* Preparation for construction. */
695 isom_elst_t *elst = trak->edts ? trak->edts->elst : NULL;
696 isom_minf_t *minf = trak->mdia->minf;
697 isom_dref_t *dref = minf->dinf->dref;
698 isom_stbl_t *stbl = minf->stbl;
699 isom_stsd_t *stsd = stbl->stsd;
700 isom_stts_t *stts = stbl->stts;
701 isom_ctts_t *ctts = stbl->ctts;
702 isom_stss_t *stss = stbl->stss;
703 isom_stps_t *stps = stbl->stps;
704 isom_sdtp_t *sdtp = stbl->sdtp;
705 isom_stsc_t *stsc = stbl->stsc;
706 isom_stsz_t *stsz = stbl->stsz;
707 isom_stco_t *stco = stbl->stco;
708 isom_sgpd_t *sgpd_rap = isom_get_sample_group_description( stbl, ISOM_GROUP_TYPE_RAP );
709 isom_sbgp_t *sbgp_rap = isom_get_sample_to_group ( stbl, ISOM_GROUP_TYPE_RAP );
710 isom_sgpd_t *sgpd_roll = isom_get_roll_recovery_sample_group_description( &stbl->sgpd_list );
711 isom_sbgp_t *sbgp_roll = isom_get_roll_recovery_sample_to_group ( &stbl->sbgp_list );
712 lsmash_entry_t *elst_entry = elst && elst->list ? elst->list->head : NULL;
713 lsmash_entry_t *stts_entry = stts && stts->list ? stts->list->head : NULL;
714 lsmash_entry_t *ctts_entry = ctts && ctts->list ? ctts->list->head : NULL;
715 lsmash_entry_t *stss_entry = stss && stss->list ? stss->list->head : NULL;
716 lsmash_entry_t *stps_entry = stps && stps->list ? stps->list->head : NULL;
717 lsmash_entry_t *sdtp_entry = sdtp && sdtp->list ? sdtp->list->head : NULL;
718 lsmash_entry_t *stsz_entry = stsz && stsz->list ? stsz->list->head : NULL;
719 lsmash_entry_t *stsc_entry = stsc && stsc->list ? stsc->list->head : NULL;
720 lsmash_entry_t *stco_entry = stco && stco->list ? stco->list->head : NULL;
721 lsmash_entry_t *sbgp_roll_entry = sbgp_roll && sbgp_roll->list ? sbgp_roll->list->head : NULL;
722 lsmash_entry_t *sbgp_rap_entry = sbgp_rap && sbgp_rap->list ? sbgp_rap->list->head : NULL;
723 lsmash_entry_t *next_stsc_entry = stsc_entry ? stsc_entry->next : NULL;
724 isom_stsc_entry_t *stsc_data = stsc_entry ? (isom_stsc_entry_t *)stsc_entry->data : NULL;
725 int movie_fragments_present = (file->moov->mvex && file->moof_list.head);
726 if( !movie_fragments_present && (!stts_entry || !stsc_entry || !stco_entry || !stco_entry->data || (next_stsc_entry && !next_stsc_entry->data)) )
727 goto fail;
728 isom_sample_entry_t *description = (isom_sample_entry_t *)lsmash_get_entry_data( &stsd->list, stsc_data ? stsc_data->sample_description_index : 1 );
729 if( !description )
730 goto fail;
731 isom_dref_entry_t *dref_entry = (isom_dref_entry_t *)lsmash_get_entry_data( &dref->list, description->data_reference_index );
732 int all_sync = !stss;
733 int large_presentation = stco->large_presentation || lsmash_check_box_type_identical( stco->type, ISOM_BOX_TYPE_CO64 );
734 int is_lpcm_audio = isom_is_lpcm_audio( description );
735 int is_qt_fixed_comp_audio = isom_is_qt_fixed_compressed_audio( description );
736 int iso_sdtp = file->max_isom_version >= 2 || file->avc_extensions;
737 int allow_negative_sample_offset = ctts && ((file->max_isom_version >= 4 && ctts->version == 1) || file->qt_compatible);
738 uint32_t sample_number_in_stts_entry = 1;
739 uint32_t sample_number_in_ctts_entry = 1;
740 uint32_t sample_number_in_sbgp_roll_entry = 1;
741 uint32_t sample_number_in_sbgp_rap_entry = 1;
742 uint64_t dts = 0;
743 uint32_t chunk_number = 1;
744 uint64_t offset_from_chunk = 0;
745 uint64_t data_offset = stco_entry && stco_entry->data
746 ? large_presentation
747 ? ((isom_co64_entry_t *)stco_entry->data)->chunk_offset
748 : ((isom_stco_entry_t *)stco_entry->data)->chunk_offset
749 : 0;
750 uint32_t samples_per_packet;
751 uint32_t constant_sample_size;
752 if( is_qt_fixed_comp_audio )
753 isom_get_qt_fixed_comp_audio_sample_quants( timeline, description, &samples_per_packet, &constant_sample_size );
754 else
756 samples_per_packet = 1;
757 constant_sample_size = stsz->sample_size;
759 uint32_t sample_number = samples_per_packet;
760 uint32_t sample_number_in_chunk = samples_per_packet;
761 /* Copy edits. */
762 while( elst_entry )
764 isom_elst_entry_t *edit = (isom_elst_entry_t *)lsmash_memdup( elst_entry->data, sizeof(isom_elst_entry_t) );
765 if( !edit
766 || lsmash_add_entry( timeline->edit_list, edit ) < 0 )
767 goto fail;
768 elst_entry = elst_entry->next;
770 /* Check what the first 2-bits of sample dependency means.
771 * This check is for chimera of ISO Base Media and QTFF. */
772 if( iso_sdtp && sdtp_entry )
774 while( sdtp_entry )
776 isom_sdtp_entry_t *sdtp_data = (isom_sdtp_entry_t *)sdtp_entry->data;
777 if( !sdtp_data )
778 goto fail;
779 if( sdtp_data->is_leading > 1 )
780 break; /* Apparently, it's defined under ISO Base Media. */
781 if( (sdtp_data->is_leading == 1) && (sdtp_data->sample_depends_on == ISOM_SAMPLE_IS_INDEPENDENT) )
783 /* Obviously, it's not defined under ISO Base Media. */
784 iso_sdtp = 0;
785 break;
787 sdtp_entry = sdtp_entry->next;
789 sdtp_entry = sdtp->list->head;
791 /**--- Construct media timeline. ---**/
792 isom_portable_chunk_t chunk;
793 chunk.data_offset = data_offset;
794 chunk.length = 0;
795 chunk.number = chunk_number;
796 chunk.file = (!dref_entry || !dref_entry->ref_file) ? NULL : dref_entry->ref_file;
797 if( isom_add_portable_chunk_entry( timeline, &chunk ) < 0 )
798 goto fail;
799 uint32_t distance = NO_RANDOM_ACCESS_POINT;
800 uint32_t last_duration = UINT32_MAX;
801 uint32_t packet_number = 1;
802 isom_lpcm_bunch_t bunch = { 0 };
803 while( sample_number <= stsz->sample_count )
805 isom_sample_info_t info = { 0 };
806 /* Get sample duration and sample offset. */
807 for( uint32_t i = 0; i < samples_per_packet; i++ )
809 /* sample duration */
810 if( stts_entry )
812 isom_stts_entry_t *stts_data = (isom_stts_entry_t *)stts_entry->data;
813 if( !stts_data )
814 goto fail;
815 isom_increment_sample_number_in_entry( &sample_number_in_stts_entry, &stts_entry, stts_data->sample_count );
816 last_duration = stts_data->sample_delta;
818 info.duration += last_duration;
819 dts += last_duration;
820 /* sample offset */
821 uint32_t sample_offset;
822 if( ctts_entry )
824 isom_ctts_entry_t *ctts_data = (isom_ctts_entry_t *)ctts_entry->data;
825 if( !ctts_data )
826 goto fail;
827 isom_increment_sample_number_in_entry( &sample_number_in_ctts_entry, &ctts_entry, ctts_data->sample_count );
828 sample_offset = ctts_data->sample_offset;
829 if( allow_negative_sample_offset )
831 uint64_t cts = dts + (int32_t)sample_offset;
832 if( (cts + timeline->ctd_shift) < dts )
833 timeline->ctd_shift = dts - cts;
836 else
837 sample_offset = 0;
838 if( i == 0 )
839 info.offset = sample_offset;
841 timeline->media_duration += info.duration;
842 if( !is_qt_fixed_comp_audio )
844 /* Check whether sync sample or not. */
845 if( stss_entry )
847 isom_stss_entry_t *stss_data = (isom_stss_entry_t *)stss_entry->data;
848 if( !stss_data )
849 goto fail;
850 if( sample_number == stss_data->sample_number )
852 info.prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
853 stss_entry = stss_entry->next;
854 distance = 0;
857 else if( all_sync )
858 /* Don't reset distance as 0 since MDCT-based audio frames need pre-roll for correct presentation
859 * though all of them could be marked as a sync sample. */
860 info.prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
861 /* Check whether partial sync sample or not. */
862 if( stps_entry )
864 isom_stps_entry_t *stps_data = (isom_stps_entry_t *)stps_entry->data;
865 if( !stps_data )
866 goto fail;
867 if( sample_number == stps_data->sample_number )
869 info.prop.ra_flags |= QT_SAMPLE_RANDOM_ACCESS_FLAG_PARTIAL_SYNC | QT_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
870 stps_entry = stps_entry->next;
871 distance = 0;
874 /* Get sample dependency info. */
875 if( sdtp_entry )
877 isom_sdtp_entry_t *sdtp_data = (isom_sdtp_entry_t *)sdtp_entry->data;
878 if( !sdtp_data )
879 goto fail;
880 if( iso_sdtp )
881 info.prop.leading = sdtp_data->is_leading;
882 else
883 info.prop.allow_earlier = sdtp_data->is_leading;
884 info.prop.independent = sdtp_data->sample_depends_on;
885 info.prop.disposable = sdtp_data->sample_is_depended_on;
886 info.prop.redundant = sdtp_data->sample_has_redundancy;
887 sdtp_entry = sdtp_entry->next;
889 /* Get roll recovery grouping info. */
890 if( sbgp_roll_entry
891 && isom_get_roll_recovery_grouping_info( timeline,
892 &sbgp_roll_entry, sgpd_roll, NULL,
893 &sample_number_in_sbgp_roll_entry,
894 &info, sample_number ) < 0 )
895 goto fail;
896 info.prop.post_roll.identifier = sample_number;
897 /* Get random access point grouping info. */
898 if( sbgp_rap_entry
899 && isom_get_random_access_point_grouping_info( timeline,
900 &sbgp_rap_entry, sgpd_rap, NULL,
901 &sample_number_in_sbgp_rap_entry,
902 &info, &distance ) < 0 )
903 goto fail;
904 /* Set up distance from the previous random access point. */
905 if( distance != NO_RANDOM_ACCESS_POINT )
907 if( info.prop.pre_roll.distance == 0 )
908 info.prop.pre_roll.distance = distance;
909 ++distance;
912 else
913 /* All uncompressed and non-variable compressed audio frame is a sync sample. */
914 info.prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
915 /* Get size of sample in the stream. */
916 if( is_qt_fixed_comp_audio || !stsz_entry )
917 info.length = constant_sample_size;
918 else
920 if( !stsz_entry->data )
921 goto fail;
922 info.length = ((isom_stsz_entry_t *)stsz_entry->data)->entry_size;
923 stsz_entry = stsz_entry->next;
925 timeline->max_sample_size = LSMASH_MAX( timeline->max_sample_size, info.length );
926 /* Get chunk info. */
927 info.pos = data_offset;
928 info.index = stsc_data->sample_description_index;
929 info.chunk = (isom_portable_chunk_t *)timeline->chunk_list->tail->data;
930 offset_from_chunk += info.length;
931 if( sample_number_in_chunk == stsc_data->samples_per_chunk )
933 /* Set the length of the last chunk. */
934 if( info.chunk )
935 info.chunk->length = offset_from_chunk;
936 /* Move the next chunk. */
937 if( stco_entry )
938 stco_entry = stco_entry->next;
939 if( stco_entry
940 && stco_entry->data )
941 data_offset = large_presentation
942 ? ((isom_co64_entry_t *)stco_entry->data)->chunk_offset
943 : ((isom_stco_entry_t *)stco_entry->data)->chunk_offset;
944 chunk.data_offset = data_offset;
945 chunk.length = 0;
946 chunk.number = ++chunk_number;
947 offset_from_chunk = 0;
948 /* Check if the next entry is broken. */
949 while( next_stsc_entry && chunk_number > ((isom_stsc_entry_t *)next_stsc_entry->data)->first_chunk )
951 /* Just skip broken next entry. */
952 lsmash_log( timeline, LSMASH_LOG_WARNING, "ignore broken entry in Sample To Chunk Box.\n" );
953 lsmash_log( timeline, LSMASH_LOG_WARNING, "timeline might be corrupted.\n" );
954 next_stsc_entry = next_stsc_entry->next;
955 if( next_stsc_entry
956 && !next_stsc_entry->data )
957 goto fail;
959 /* Check if the next chunk belongs to the next sequence of chunks. */
960 if( next_stsc_entry && chunk_number == ((isom_stsc_entry_t *)next_stsc_entry->data)->first_chunk )
962 stsc_entry = next_stsc_entry;
963 next_stsc_entry = next_stsc_entry->next;
964 if( next_stsc_entry
965 && !next_stsc_entry->data )
966 goto fail;
967 stsc_data = (isom_stsc_entry_t *)stsc_entry->data;
968 /* Update sample description. */
969 description = (isom_sample_entry_t *)lsmash_get_entry_data( &stsd->list, stsc_data->sample_description_index );
970 is_lpcm_audio = isom_is_lpcm_audio( description );
971 is_qt_fixed_comp_audio = isom_is_qt_fixed_compressed_audio( description );
972 if( is_qt_fixed_comp_audio )
973 isom_get_qt_fixed_comp_audio_sample_quants( timeline, description, &samples_per_packet, &constant_sample_size );
974 else
976 samples_per_packet = 1;
977 constant_sample_size = stsz->sample_size;
979 /* Reference media data. */
980 dref_entry = (isom_dref_entry_t *)lsmash_get_entry_data( &dref->list, description->data_reference_index );
981 chunk.file = (!dref_entry || !dref_entry->ref_file) ? NULL : dref_entry->ref_file;
983 sample_number_in_chunk = samples_per_packet;
984 if( isom_add_portable_chunk_entry( timeline, &chunk ) < 0 )
985 goto fail;
987 else
989 data_offset += info.length;
990 sample_number_in_chunk += samples_per_packet;
992 /* OK. Let's add its info. */
993 if( is_lpcm_audio )
995 if( sample_number == samples_per_packet )
996 isom_update_bunch( &bunch, &info );
997 else if( isom_compare_lpcm_sample_info( &bunch, &info ) )
999 if( isom_add_lpcm_bunch_entry( timeline, &bunch ) < 0 )
1000 goto fail;
1001 isom_update_bunch( &bunch, &info );
1003 else
1004 ++ bunch.sample_count;
1006 else if( isom_add_sample_info_entry( timeline, &info ) < 0 )
1007 goto fail;
1008 if( timeline->info_list->entry_count && timeline->bunch_list->entry_count )
1010 lsmash_log( timeline, LSMASH_LOG_ERROR, "LPCM + non-LPCM track is not supported.\n" );
1011 goto fail;
1013 sample_number += samples_per_packet;
1014 packet_number += 1;
1016 isom_portable_chunk_t *last_chunk = lsmash_get_entry_data( timeline->chunk_list, timeline->chunk_list->entry_count );
1017 if( last_chunk )
1019 if( offset_from_chunk )
1020 last_chunk->length = offset_from_chunk;
1021 else
1023 /* Remove the last invalid chunk. */
1024 lsmash_remove_entry( timeline->chunk_list, timeline->chunk_list->entry_count, NULL );
1025 --chunk_number;
1028 uint32_t sample_count = packet_number - 1;
1029 if( movie_fragments_present )
1031 isom_tfra_t *tfra = isom_get_tfra( file->mfra, track_ID );
1032 lsmash_entry_t *tfra_entry = tfra && tfra->list ? tfra->list->head : NULL;
1033 isom_tfra_location_time_entry_t *rap = tfra_entry ? (isom_tfra_location_time_entry_t *)tfra_entry->data : NULL;
1034 chunk.data_offset = 0;
1035 chunk.length = 0;
1036 /* Movie fragments */
1037 for( lsmash_entry_t *moof_entry = file->moof_list.head; moof_entry; moof_entry = moof_entry->next )
1039 isom_moof_t *moof = (isom_moof_t *)moof_entry->data;
1040 if( !moof )
1041 goto fail;
1042 uint64_t last_sample_end_pos = 0;
1043 /* Track fragments */
1044 uint32_t traf_number = 1;
1045 for( lsmash_entry_t *traf_entry = moof->traf_list.head; traf_entry; traf_entry = traf_entry->next )
1047 isom_traf_t *traf = (isom_traf_t *)traf_entry->data;
1048 if( !traf )
1049 goto fail;
1050 isom_tfhd_t *tfhd = traf->tfhd;
1051 if( !tfhd )
1052 goto fail;
1053 isom_trex_t *trex = isom_get_trex( file->moov->mvex, tfhd->track_ID );
1054 if( !trex )
1055 goto fail;
1056 /* Ignore ISOM_TF_FLAGS_DURATION_IS_EMPTY flag even if set. */
1057 if( !traf->trun_list.head )
1059 ++traf_number;
1060 continue;
1062 /* Get base_data_offset. */
1063 uint64_t base_data_offset;
1064 if( tfhd->flags & ISOM_TF_FLAGS_BASE_DATA_OFFSET_PRESENT )
1065 base_data_offset = tfhd->base_data_offset;
1066 else if( (tfhd->flags & ISOM_TF_FLAGS_DEFAULT_BASE_IS_MOOF) || traf_entry == moof->traf_list.head )
1067 base_data_offset = moof->pos;
1068 else
1069 base_data_offset = last_sample_end_pos;
1070 /* sample grouping */
1071 isom_sgpd_t *sgpd_frag_rap;
1072 isom_sgpd_t *sgpd_frag_roll;
1073 sgpd_frag_rap = isom_get_fragment_sample_group_description( traf, ISOM_GROUP_TYPE_RAP );
1074 sbgp_rap = isom_get_fragment_sample_to_group ( traf, ISOM_GROUP_TYPE_RAP );
1075 sbgp_rap_entry = sbgp_rap && sbgp_rap->list ? sbgp_rap->list->head : NULL;
1076 sgpd_frag_roll = isom_get_roll_recovery_sample_group_description( &traf->sgpd_list );
1077 sbgp_roll = isom_get_roll_recovery_sample_to_group ( &traf->sbgp_list );
1078 sbgp_roll_entry = sbgp_roll && sbgp_roll->list ? sbgp_roll->list->head : NULL;
1079 int need_data_offset_only = (tfhd->track_ID != track_ID);
1080 /* Track runs */
1081 uint32_t trun_number = 1;
1082 for( lsmash_entry_t *trun_entry = traf->trun_list.head; trun_entry; trun_entry = trun_entry->next )
1084 isom_trun_t *trun = (isom_trun_t *)trun_entry->data;
1085 if( !trun )
1086 goto fail;
1087 if( trun->sample_count == 0 )
1089 ++trun_number;
1090 continue;
1092 /* Get data_offset. */
1093 if( trun->flags & ISOM_TR_FLAGS_DATA_OFFSET_PRESENT )
1094 data_offset = trun->data_offset + base_data_offset;
1095 else if( trun_entry == traf->trun_list.head )
1096 data_offset = base_data_offset;
1097 else
1098 data_offset = last_sample_end_pos;
1099 /* */
1100 uint32_t sample_description_index = 0;
1101 isom_sdtp_entry_t *sdtp_data = NULL;
1102 if( !need_data_offset_only )
1104 /* Get sample_description_index of this track fragment. */
1105 if( tfhd->flags & ISOM_TF_FLAGS_SAMPLE_DESCRIPTION_INDEX_PRESENT )
1106 sample_description_index = tfhd->sample_description_index;
1107 else
1108 sample_description_index = trex->default_sample_description_index;
1109 description = (isom_sample_entry_t *)lsmash_get_entry_data( &stsd->list, sample_description_index );
1110 is_lpcm_audio = isom_is_lpcm_audio( description );
1111 /* Reference media data. */
1112 dref_entry = (isom_dref_entry_t *)lsmash_get_entry_data( &dref->list, description->data_reference_index );
1113 lsmash_file_t *ref_file = (!dref_entry || !dref_entry->ref_file) ? NULL : dref_entry->ref_file;
1114 /* Each track run can be considered as a chunk.
1115 * Here, we consider physically consecutive track runs as one chunk. */
1116 if( chunk.data_offset + chunk.length != data_offset || chunk.file != ref_file )
1118 chunk.data_offset = data_offset;
1119 chunk.length = 0;
1120 chunk.number = ++chunk_number;
1121 chunk.file = ref_file;
1122 if( isom_add_portable_chunk_entry( timeline, &chunk ) < 0 )
1123 goto fail;
1125 /* Get dependency info for this track fragment. */
1126 sdtp_entry = traf->sdtp && traf->sdtp->list ? traf->sdtp->list->head : NULL;
1127 sdtp_data = sdtp_entry && sdtp_entry->data ? (isom_sdtp_entry_t *)sdtp_entry->data : NULL;
1129 /* Get info of each sample. */
1130 lsmash_entry_t *row_entry = trun->optional && trun->optional->head ? trun->optional->head : NULL;
1131 sample_number = 1;
1132 while( sample_number <= trun->sample_count )
1134 isom_sample_info_t info = { 0 };
1135 isom_trun_optional_row_t *row = row_entry && row_entry->data ? (isom_trun_optional_row_t *)row_entry->data : NULL;
1136 /* Get sample_size */
1137 if( row && (trun->flags & ISOM_TR_FLAGS_SAMPLE_SIZE_PRESENT) )
1138 info.length = row->sample_size;
1139 else if( tfhd->flags & ISOM_TF_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT )
1140 info.length = tfhd->default_sample_size;
1141 else
1142 info.length = trex->default_sample_size;
1143 if( !need_data_offset_only )
1145 info.pos = data_offset;
1146 info.index = sample_description_index;
1147 info.chunk = (isom_portable_chunk_t *)timeline->chunk_list->tail->data;
1148 info.chunk->length += info.length;
1149 /* Get sample_duration. */
1150 if( row && (trun->flags & ISOM_TR_FLAGS_SAMPLE_DURATION_PRESENT) )
1151 info.duration = row->sample_duration;
1152 else if( tfhd->flags & ISOM_TF_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT )
1153 info.duration = tfhd->default_sample_duration;
1154 else
1155 info.duration = trex->default_sample_duration;
1156 /* Get composition time offset. */
1157 if( row && (trun->flags & ISOM_TR_FLAGS_SAMPLE_COMPOSITION_TIME_OFFSET_PRESENT) )
1159 info.offset = row->sample_composition_time_offset;
1160 /* Check composition to decode timeline shift. */
1161 if( file->max_isom_version >= 6 && trun->version != 0 )
1163 uint64_t cts = dts + (int32_t)info.offset;
1164 if( (cts + timeline->ctd_shift) < dts )
1165 timeline->ctd_shift = dts - cts;
1168 else
1169 info.offset = 0;
1170 dts += info.duration;
1171 /* Update media duration and maximun sample size. */
1172 timeline->media_duration += info.duration;
1173 timeline->max_sample_size = LSMASH_MAX( timeline->max_sample_size, info.length );
1174 if( !is_lpcm_audio )
1176 /* Get sample_flags. */
1177 isom_sample_flags_t sample_flags;
1178 if( sample_number == 1 && (trun->flags & ISOM_TR_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT) )
1179 sample_flags = trun->first_sample_flags;
1180 else if( row && (trun->flags & ISOM_TR_FLAGS_SAMPLE_FLAGS_PRESENT) )
1181 sample_flags = row->sample_flags;
1182 else if( tfhd->flags & ISOM_TF_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT )
1183 sample_flags = tfhd->default_sample_flags;
1184 else
1185 sample_flags = trex->default_sample_flags;
1186 if( sdtp_data )
1188 /* Independent and Disposable Samples Box overrides the information from sample_flags.
1189 * There is no description in the specification about this, but the intention should be such a thing.
1190 * The ground is that sample_flags is placed in media layer
1191 * while Independent and Disposable Samples Box is placed in track or presentation layer. */
1192 info.prop.leading = sdtp_data->is_leading;
1193 info.prop.independent = sdtp_data->sample_depends_on;
1194 info.prop.disposable = sdtp_data->sample_is_depended_on;
1195 info.prop.redundant = sdtp_data->sample_has_redundancy;
1196 if( sdtp_entry )
1197 sdtp_entry = sdtp_entry->next;
1198 sdtp_data = sdtp_entry ? (isom_sdtp_entry_t *)sdtp_entry->data : NULL;
1200 else
1202 info.prop.leading = sample_flags.is_leading;
1203 info.prop.independent = sample_flags.sample_depends_on;
1204 info.prop.disposable = sample_flags.sample_is_depended_on;
1205 info.prop.redundant = sample_flags.sample_has_redundancy;
1207 /* Check this sample is a sync sample or not.
1208 * Note: all sync sample shall be independent. */
1209 if( !sample_flags.sample_is_non_sync_sample
1210 && info.prop.independent != ISOM_SAMPLE_IS_NOT_INDEPENDENT )
1212 info.prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1213 distance = 0;
1215 /* Get roll recovery grouping info. */
1216 uint32_t roll_id = sample_count + sample_number;
1217 if( sbgp_roll_entry
1218 && isom_get_roll_recovery_grouping_info( timeline,
1219 &sbgp_roll_entry, sgpd_roll, sgpd_frag_roll,
1220 &sample_number_in_sbgp_roll_entry,
1221 &info, roll_id ) < 0 )
1222 goto fail;
1223 info.prop.post_roll.identifier = roll_id;
1224 /* Get random access point grouping info. */
1225 if( sbgp_rap_entry
1226 && isom_get_random_access_point_grouping_info( timeline,
1227 &sbgp_rap_entry, sgpd_rap, sgpd_frag_rap,
1228 &sample_number_in_sbgp_rap_entry,
1229 &info, &distance ) < 0 )
1230 goto fail;
1231 /* Get the location of the sync sample from 'tfra' if it is not set up yet.
1232 * Note: there is no guarantee that its entries are placed in a specific order. */
1233 if( tfra )
1235 if( tfra->number_of_entry == 0
1236 && info.prop.ra_flags == ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE )
1237 info.prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1238 if( rap
1239 && rap->moof_offset == moof->pos
1240 && rap->traf_number == traf_number
1241 && rap->trun_number == trun_number
1242 && rap->sample_number == sample_number )
1244 if( info.prop.ra_flags == ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE )
1245 info.prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1246 if( tfra_entry )
1247 tfra_entry = tfra_entry->next;
1248 rap = tfra_entry ? (isom_tfra_location_time_entry_t *)tfra_entry->data : NULL;
1251 /* Set up distance from the previous random access point. */
1252 if( distance != NO_RANDOM_ACCESS_POINT )
1254 if( info.prop.pre_roll.distance == 0 )
1255 info.prop.pre_roll.distance = distance;
1256 ++distance;
1258 /* OK. Let's add its info. */
1259 if( isom_add_sample_info_entry( timeline, &info ) < 0)
1260 goto fail;
1262 else
1264 /* All LPCMFrame is a sync sample. */
1265 info.prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1266 /* OK. Let's add its info. */
1267 if( sample_count == 0 && sample_number == 1 )
1268 isom_update_bunch( &bunch, &info );
1269 else if( isom_compare_lpcm_sample_info( &bunch, &info ) )
1271 if( isom_add_lpcm_bunch_entry( timeline, &bunch ) < 0 )
1272 goto fail;
1273 isom_update_bunch( &bunch, &info );
1275 else
1276 ++ bunch.sample_count;
1278 if( timeline-> info_list->entry_count
1279 && timeline->bunch_list->entry_count )
1281 lsmash_log( timeline, LSMASH_LOG_ERROR, "LPCM + non-LPCM track is not supported.\n" );
1282 goto fail;
1285 data_offset += info.length;
1286 last_sample_end_pos = data_offset;
1287 if( row_entry )
1288 row_entry = row_entry->next;
1289 ++sample_number;
1291 if( !need_data_offset_only )
1292 sample_count += sample_number - 1;
1293 ++trun_number;
1294 } /* Track runs */
1295 ++traf_number;
1296 } /* Track fragments */
1297 } /* Movie fragments */
1299 else if( timeline->chunk_list->entry_count == 0 )
1300 goto fail; /* No samples in this track. */
1301 if( bunch.sample_count && isom_add_lpcm_bunch_entry( timeline, &bunch ) < 0 )
1302 goto fail;
1303 if( lsmash_add_entry( file->timeline, timeline ) < 0 )
1304 goto fail;
1305 /* Finish timeline construction. */
1306 timeline->sample_count = sample_count;
1307 if( timeline->info_list->entry_count )
1309 timeline->get_dts = isom_get_dts_from_info_list;
1310 timeline->get_cts = isom_get_cts_from_info_list;
1311 timeline->get_sample_duration = isom_get_sample_duration_from_info_list;
1312 timeline->check_sample_existence = isom_check_sample_existence_in_info_list;
1313 timeline->get_sample = isom_get_sample_from_media_timeline;
1314 timeline->get_sample_info = isom_get_sample_info_from_media_timeline;
1315 timeline->get_sample_property = isom_get_sample_property_from_media_timeline;
1317 else
1319 timeline->get_dts = isom_get_dts_from_bunch_list;
1320 timeline->get_cts = isom_get_cts_from_bunch_list;
1321 timeline->get_sample_duration = isom_get_sample_duration_from_bunch_list;
1322 timeline->check_sample_existence = isom_check_sample_existence_in_bunch_list;
1323 timeline->get_sample = isom_get_lpcm_sample_from_media_timeline;
1324 timeline->get_sample_info = isom_get_lpcm_sample_info_from_media_timeline;
1325 timeline->get_sample_property = isom_get_lpcm_sample_property_from_media_timeline;
1327 return 0;
1328 fail:
1329 isom_destruct_timeline_direct( timeline );
1330 return -1;
1333 int lsmash_get_dts_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number, uint64_t *dts )
1335 if( !sample_number || !dts )
1336 return -1;
1337 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1338 if( !timeline || sample_number > timeline->sample_count )
1339 return -1;
1340 return timeline->get_dts( timeline, sample_number, dts );
1343 int lsmash_get_cts_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number, uint64_t *cts )
1345 if( !sample_number || !cts )
1346 return -1;
1347 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1348 if( !timeline || sample_number > timeline->sample_count )
1349 return -1;
1350 return timeline->get_cts( timeline, sample_number, cts );
1353 lsmash_sample_t *lsmash_get_sample_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number )
1355 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1356 return timeline ? timeline->get_sample( timeline, sample_number ) : NULL;
1359 int lsmash_get_sample_info_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number, lsmash_sample_t *sample )
1361 if( !sample )
1362 return -1;
1363 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1364 return timeline ? timeline->get_sample_info( timeline, sample_number, sample ) : -1;
1367 int lsmash_get_sample_property_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number, lsmash_sample_property_t *prop )
1369 if( !prop )
1370 return -1;
1371 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1372 return timeline ? timeline->get_sample_property( timeline, sample_number, prop ) : -1;
1375 int lsmash_get_composition_to_decode_shift_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t *ctd_shift )
1377 if( !ctd_shift )
1378 return -1;
1379 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1380 if( !timeline )
1381 return -1;
1382 *ctd_shift = timeline->ctd_shift;
1383 return 0;
1386 static int isom_get_closest_past_random_accessible_point_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number, uint32_t *rap_number )
1388 lsmash_entry_t *entry = lsmash_get_entry( timeline->info_list, sample_number-- );
1389 if( !entry
1390 || !entry->data )
1391 return -1;
1392 isom_sample_info_t *info = (isom_sample_info_t *)entry->data;
1393 while( info->prop.ra_flags == ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE )
1395 entry = entry->prev;
1396 if( !entry
1397 || !entry->data )
1398 return -1;
1399 info = (isom_sample_info_t *)entry->data;
1400 --sample_number;
1402 *rap_number = sample_number + 1;
1403 return 0;
1406 static inline int isom_get_closest_future_random_accessible_point_from_media_timeline( isom_timeline_t *timeline, uint32_t sample_number, uint32_t *rap_number )
1408 lsmash_entry_t *entry = lsmash_get_entry( timeline->info_list, sample_number++ );
1409 if( !entry
1410 || !entry->data )
1411 return -1;
1412 isom_sample_info_t *info = (isom_sample_info_t *)entry->data;
1413 while( info->prop.ra_flags == ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE )
1415 entry = entry->next;
1416 if( !entry
1417 || !entry->data )
1418 return -1;
1419 info = (isom_sample_info_t *)entry->data;
1420 ++sample_number;
1422 *rap_number = sample_number - 1;
1423 return 0;
1426 static int isom_get_closest_random_accessible_point_from_media_timeline_internal( isom_timeline_t *timeline, uint32_t sample_number, uint32_t *rap_number )
1428 if( !timeline )
1429 return -1;
1430 if( isom_get_closest_past_random_accessible_point_from_media_timeline( timeline, sample_number, rap_number ) < 0
1431 && isom_get_closest_future_random_accessible_point_from_media_timeline( timeline, sample_number + 1, rap_number ) < 0 )
1432 return -1;
1433 return 0;
1436 int lsmash_get_closest_random_accessible_point_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number, uint32_t *rap_number )
1438 if( sample_number == 0 || !rap_number )
1439 return -1;
1440 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1441 if( timeline->info_list->entry_count == 0 )
1443 *rap_number = sample_number; /* All LPCM is sync sample. */
1444 return 0;
1446 return isom_get_closest_random_accessible_point_from_media_timeline_internal( timeline, sample_number, rap_number );
1449 int lsmash_get_closest_random_accessible_point_detail_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number,
1450 uint32_t *rap_number, lsmash_random_access_flag *ra_flags, uint32_t *leading, uint32_t *distance )
1452 if( sample_number == 0 )
1453 return -1;
1454 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1455 if( timeline->info_list->entry_count == 0 )
1457 /* All LPCM is sync sample. */
1458 *rap_number = sample_number;
1459 if( ra_flags )
1460 *ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1461 if( leading )
1462 *leading = 0;
1463 if( distance )
1464 *distance = 0;
1465 return 0;
1467 if( isom_get_closest_random_accessible_point_from_media_timeline_internal( timeline, sample_number, rap_number ) )
1468 return -1;
1469 isom_sample_info_t *info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, *rap_number );
1470 if( !info )
1471 return -1;
1472 if( ra_flags )
1473 *ra_flags = info->prop.ra_flags;
1474 if( leading )
1475 *leading = 0;
1476 if( distance )
1477 *distance = 0;
1478 if( sample_number < *rap_number )
1479 /* Impossible to desire to decode the sample of given number correctly. */
1480 return 0;
1481 else if( !(info->prop.ra_flags & ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR) )
1483 if( leading )
1485 /* Count leading samples. */
1486 uint32_t current_sample_number = *rap_number + 1;
1487 uint64_t dts;
1488 if( isom_get_dts_from_info_list( timeline, *rap_number, &dts ) < 0 )
1489 return -1;
1490 uint64_t rap_cts = timeline->ctd_shift ? (dts + (int32_t)info->offset + timeline->ctd_shift) : (dts + info->offset);
1493 dts += info->duration;
1494 if( rap_cts <= dts )
1495 break; /* leading samples of this random accessible point must not be present more. */
1496 info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, current_sample_number++ );
1497 if( !info )
1498 break;
1499 uint64_t cts = timeline->ctd_shift ? (dts + (int32_t)info->offset + timeline->ctd_shift) : (dts + info->offset);
1500 if( rap_cts > cts )
1501 ++ *leading;
1502 } while( 1 );
1504 if( !distance || sample_number == *rap_number )
1505 return 0;
1506 /* Measure distance from the first closest non-recovery random accessible point to the second. */
1507 uint32_t prev_rap_number = *rap_number;
1510 if( isom_get_closest_past_random_accessible_point_from_media_timeline( timeline, prev_rap_number - 1, &prev_rap_number ) < 0 )
1511 /* The previous random accessible point is not present. */
1512 return 0;
1513 info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, prev_rap_number );
1514 if( !info )
1515 return -1;
1516 if( !(info->prop.ra_flags & ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR) )
1518 /* Decode shall already complete at the first closest non-recovery random accessible point if starting to decode from the second. */
1519 *distance = *rap_number - prev_rap_number;
1520 return 0;
1522 } while( 1 );
1524 if( !distance )
1525 return 0;
1526 /* Calculate roll-distance. */
1527 if( info->prop.pre_roll.distance )
1529 /* Pre-roll recovery */
1530 uint32_t prev_rap_number = *rap_number;
1533 if( isom_get_closest_past_random_accessible_point_from_media_timeline( timeline, prev_rap_number - 1, &prev_rap_number ) < 0
1534 && *rap_number < info->prop.pre_roll.distance )
1536 /* The previous random accessible point is not present.
1537 * And sample of given number might be not able to decoded correctly. */
1538 *distance = 0;
1539 return 0;
1541 if( prev_rap_number + info->prop.pre_roll.distance <= *rap_number )
1544 * |<---- pre-roll distance ---->|
1545 * |<--------- distance -------->|
1546 * media +++++++++++++++++++++++++ *** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1547 * ^ ^ ^ ^
1548 * random accessible point starting point random accessible point given sample
1549 * (complete)
1551 *distance = info->prop.pre_roll.distance;
1552 return 0;
1554 else if( !(info->prop.ra_flags & ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR) )
1557 * |<------------ pre-roll distance ------------------>|
1558 * |<------ distance ------->|
1559 * media ++++++++++++++++ *** ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1560 * ^ ^ ^ ^
1561 * random accessible point random accessible point given sample
1562 * (starting point) (complete)
1564 *distance = *rap_number - prev_rap_number;
1565 return 0;
1567 } while( 1 );
1569 /* Post-roll recovery */
1570 if( sample_number >= info->prop.post_roll.complete )
1572 * |<----- post-roll distance ----->|
1573 * (distance = 0)
1574 * media +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1575 * ^ ^ ^
1576 * random accessible point complete given sample
1577 * (starting point)
1579 return 0;
1580 uint32_t prev_rap_number = *rap_number;
1583 if( isom_get_closest_past_random_accessible_point_from_media_timeline( timeline, prev_rap_number - 1, &prev_rap_number ) < 0 )
1584 /* The previous random accessible point is not present. */
1585 return 0;
1586 info = (isom_sample_info_t *)lsmash_get_entry_data( timeline->info_list, prev_rap_number );
1587 if( !info )
1588 return -1;
1589 if( !(info->prop.ra_flags & ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR) || sample_number >= info->prop.post_roll.complete )
1591 *distance = *rap_number - prev_rap_number;
1592 return 0;
1594 } while( 1 );
1597 int lsmash_check_sample_existence_in_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number )
1599 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1600 return timeline ? timeline->check_sample_existence( timeline, sample_number ) : 0;
1603 int lsmash_get_last_sample_delta_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t *last_sample_delta )
1605 if( !last_sample_delta )
1606 return -1;
1607 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1608 return timeline ? timeline->get_sample_duration( timeline, timeline->sample_count, last_sample_delta ) : -1;
1611 int lsmash_get_sample_delta_from_media_timeline( lsmash_root_t *root, uint32_t track_ID, uint32_t sample_number, uint32_t *sample_delta )
1613 if( !sample_delta )
1614 return -1;
1615 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1616 return timeline ? timeline->get_sample_duration( timeline, sample_number, sample_delta ) : -1;
1619 uint32_t lsmash_get_sample_count_in_media_timeline( lsmash_root_t *root, uint32_t track_ID )
1621 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1622 if( !timeline )
1623 return 0;
1624 return timeline->sample_count;
1627 uint32_t lsmash_get_max_sample_size_in_media_timeline( lsmash_root_t *root, uint32_t track_ID )
1629 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1630 if( !timeline )
1631 return 0;
1632 return timeline->max_sample_size;
1635 uint64_t lsmash_get_media_duration_from_media_timeline( lsmash_root_t *root, uint32_t track_ID )
1637 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1638 if( !timeline )
1639 return 0;
1640 return timeline->media_duration;
1643 int lsmash_copy_timeline_map( lsmash_root_t *dst, uint32_t dst_track_ID, lsmash_root_t *src, uint32_t src_track_ID )
1645 if( isom_check_initializer_present( dst ) < 0
1646 || isom_check_initializer_present( src ) < 0 )
1647 return -1;
1648 lsmash_file_t *dst_file = dst->file->initializer;
1649 isom_trak_t *dst_trak = isom_get_trak( dst_file, dst_track_ID );
1650 if( !dst_file->moov
1651 || !dst_file->moov->mvhd
1652 || dst_file->moov->mvhd->timescale == 0
1653 || !dst_trak
1654 || !dst_trak->mdia
1655 || !dst_trak->mdia->mdhd
1656 || dst_trak->mdia->mdhd->timescale == 0
1657 || !dst_trak->mdia->minf
1658 || !dst_trak->mdia->minf->stbl )
1659 return -1;
1660 if( dst_trak->edts
1661 && dst_trak->edts->elst )
1662 lsmash_remove_entries( dst_trak->edts->elst->list, NULL );
1663 uint32_t src_movie_timescale;
1664 uint32_t src_media_timescale;
1665 uint64_t src_track_duration;
1666 uint64_t src_media_duration;
1667 int32_t src_ctd_shift; /* Add timeline shift difference between src and dst to each media_time.
1668 * Therefore, call this function as later as possible. */
1669 lsmash_entry_t *src_entry = NULL;
1670 lsmash_file_t *src_file = src->file->initializer;
1671 isom_trak_t *src_trak = isom_get_trak( src_file, src_track_ID );
1672 int src_fragmented = !!(src_file->flags & LSMASH_FILE_MODE_FRAGMENTED);
1673 if( !src_trak
1674 || !src_trak->edts
1675 || !src_trak->edts->elst
1676 || !src_trak->edts->elst->list
1677 || src_fragmented )
1679 /* Get from constructed timeline instead of boxes. */
1680 isom_timeline_t *src_timeline = isom_get_timeline( src, src_track_ID );
1681 if( src_timeline
1682 && src_timeline->movie_timescale
1683 && src_timeline->media_timescale )
1685 src_entry = src_timeline->edit_list->head;
1686 if( !src_entry )
1687 return 0;
1688 src_movie_timescale = src_timeline->movie_timescale;
1689 src_media_timescale = src_timeline->media_timescale;
1690 src_track_duration = src_timeline->track_duration;
1691 src_media_duration = src_timeline->media_duration;
1692 src_ctd_shift = src_timeline->ctd_shift;
1694 else if( !src_fragmented )
1695 return -1;
1697 if( !src_entry )
1699 if( !src_file->moov
1700 || !src_file->moov->mvhd
1701 || src_file->moov->mvhd->timescale == 0
1702 || !src_trak->tkhd
1703 || !src_trak->mdia
1704 || !src_trak->mdia->mdhd
1705 || src_trak->mdia->mdhd->timescale == 0
1706 || !src_trak->mdia->minf
1707 || !src_trak->mdia->minf->stbl )
1708 return -1;
1709 src_entry = src_trak->edts->elst->list->head;
1710 if( !src_entry )
1711 return 0;
1712 src_movie_timescale = src_file->moov->mvhd->timescale;
1713 src_media_timescale = src_trak->mdia->mdhd->timescale;
1714 src_track_duration = src_trak->tkhd->duration;
1715 src_media_duration = src_trak->mdia->mdhd->duration;
1716 src_ctd_shift = src_trak->mdia->minf->stbl->cslg ? src_trak->mdia->minf->stbl->cslg->compositionToDTSShift : 0;
1718 /* Generate the edit list if absent in the destination. */
1719 if( (!dst_trak->edts && !isom_add_edts( dst_trak ))
1720 || (!dst_trak->edts->elst && !isom_add_elst( dst_trak->edts )) )
1721 return -1;
1722 uint32_t dst_movie_timescale = dst_file->moov->mvhd->timescale;
1723 uint32_t dst_media_timescale = dst_trak->mdia->mdhd->timescale;
1724 int32_t dst_ctd_shift = dst_trak->mdia->minf->stbl->cslg ? dst_trak->mdia->minf->stbl->cslg->compositionToDTSShift : 0;
1725 int32_t media_time_shift = src_ctd_shift - dst_ctd_shift;
1726 lsmash_entry_list_t *dst_list = dst_trak->edts->elst->list;
1727 while( src_entry )
1729 isom_elst_entry_t *src_data = (isom_elst_entry_t *)src_entry->data;
1730 if( !src_data )
1731 return -1;
1732 isom_elst_entry_t *dst_data = (isom_elst_entry_t *)lsmash_malloc( sizeof(isom_elst_entry_t) );
1733 if( !dst_data )
1734 return -1;
1735 uint64_t segment_duration;
1736 if( src_data->segment_duration == 0 && !dst_file->fragment )
1737 /* The implicit duration edit is not suitable for non-fragmented movie file.
1738 * Set an appropriate duration from the source track. */
1739 segment_duration = src_fragmented
1740 ? (uint64_t)(src_media_duration * ((double)src_movie_timescale / src_media_timescale))
1741 : src_track_duration;
1742 else
1743 segment_duration = src_data->segment_duration;
1744 dst_data->segment_duration = segment_duration * ((double)dst_movie_timescale / src_movie_timescale) + 0.5;
1745 dst_data->media_rate = src_data->media_rate;
1746 if( src_data->media_time != ISOM_EDIT_MODE_EMPTY )
1747 dst_data->media_time = (src_data->media_time + media_time_shift) * ((double)dst_media_timescale / src_media_timescale) + 0.5;
1748 else
1749 dst_data->media_time = ISOM_EDIT_MODE_EMPTY;
1750 if( lsmash_add_entry( dst_list, dst_data ) < 0 )
1752 lsmash_free( dst_data );
1753 return -1;
1755 src_entry = src_entry->next;
1757 return 0;
1760 int lsmash_set_media_timestamps( lsmash_root_t *root, uint32_t track_ID, lsmash_media_ts_list_t *ts_list )
1762 if( !root || !root->file || !ts_list )
1763 return -1;
1764 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1765 if( !timeline )
1766 return -1;
1767 if( timeline->info_list->entry_count == 0 )
1769 lsmash_log( timeline, LSMASH_LOG_ERROR, "Changing timestamps of LPCM track is not supported.\n" );
1770 return -1;
1772 if( ts_list->sample_count != timeline->info_list->entry_count )
1773 return -1; /* Number of samples must be same. */
1774 lsmash_media_ts_t *ts = ts_list->timestamp;
1775 if( ts[0].dts )
1776 return -1; /* DTS must start from value zero. */
1777 /* Update DTSs. */
1778 uint32_t sample_count = ts_list->sample_count;
1779 uint32_t i;
1780 if( timeline->info_list->entry_count > 1 )
1782 i = 1;
1783 lsmash_entry_t *entry = timeline->info_list->head;
1784 isom_sample_info_t *info;
1785 while( i < sample_count )
1787 info = (isom_sample_info_t *)entry->data;
1788 if( !info || (ts[i].dts < ts[i - 1].dts) )
1789 return -1;
1790 info->duration = ts[i].dts - ts[i - 1].dts;
1791 entry = entry->next;
1792 ++i;
1794 if( i > 1 )
1796 if( !entry
1797 || !entry->data )
1798 return -1;
1799 /* Copy the previous duration. */
1800 ((isom_sample_info_t *)entry->data)->duration = info->duration;
1802 else
1803 return -1; /* Irregular case: sample_count this timeline has is incorrect. */
1805 else /* still image */
1806 ((isom_sample_info_t *)timeline->info_list->head->data)->duration = UINT32_MAX;
1807 /* Update CTSs.
1808 * ToDo: hint track must not have any sample_offset. */
1809 i = 0;
1810 timeline->ctd_shift = 0;
1811 for( lsmash_entry_t *entry = timeline->info_list->head; entry; entry = entry->next )
1813 isom_sample_info_t *info = (isom_sample_info_t *)entry->data;
1814 if( (ts[i].cts + timeline->ctd_shift) < ts[i].dts )
1815 timeline->ctd_shift = ts[i].dts - ts[i].cts;
1816 info->offset = ts[i].cts - ts[i].dts;
1817 ++i;
1819 if( timeline->ctd_shift && (!root->file->qt_compatible || root->file->max_isom_version < 4) )
1820 return -1; /* Don't allow composition to decode timeline shift. */
1821 return 0;
1824 int lsmash_get_media_timestamps( lsmash_root_t *root, uint32_t track_ID, lsmash_media_ts_list_t *ts_list )
1826 if( !ts_list )
1827 return -1;
1828 isom_timeline_t *timeline = isom_get_timeline( root, track_ID );
1829 if( !timeline )
1830 return -1;
1831 uint32_t sample_count = timeline->info_list->entry_count;
1832 if( !sample_count )
1834 ts_list->sample_count = 0;
1835 ts_list->timestamp = NULL;
1836 return 0;
1838 lsmash_media_ts_t *ts = lsmash_malloc( sample_count * sizeof(lsmash_media_ts_t) );
1839 if( !ts )
1840 return -1;
1841 uint64_t dts = 0;
1842 uint32_t i = 0;
1843 if( timeline->info_list->entry_count )
1844 for( lsmash_entry_t *entry = timeline->info_list->head; entry; entry = entry->next )
1846 isom_sample_info_t *info = (isom_sample_info_t *)entry->data;
1847 if( !info )
1849 lsmash_free( ts );
1850 return -1;
1852 ts[i].dts = dts;
1853 ts[i].cts = timeline->ctd_shift ? (dts + (int32_t)info->offset) : (dts + info->offset);
1854 dts += info->duration;
1855 ++i;
1857 else
1858 for( lsmash_entry_t *entry = timeline->bunch_list->head; entry; entry = entry->next )
1860 isom_lpcm_bunch_t *bunch = (isom_lpcm_bunch_t *)entry->data;
1861 if( !bunch )
1863 lsmash_free( ts );
1864 return -1;
1866 for( uint32_t j = 0; j < bunch->sample_count; j++ )
1868 ts[i].dts = dts;
1869 ts[i].cts = timeline->ctd_shift ? (dts + (int32_t)bunch->offset) : (dts + bunch->offset);
1870 dts += bunch->duration;
1871 ++i;
1874 ts_list->sample_count = sample_count;
1875 ts_list->timestamp = ts;
1876 return 0;
1879 void lsmash_delete_media_timestamps( lsmash_media_ts_list_t *ts_list )
1881 if( !ts_list )
1882 return;
1883 lsmash_freep( &ts_list->timestamp );
1884 ts_list->sample_count = 0;
1887 static int isom_compare_dts( const lsmash_media_ts_t *a, const lsmash_media_ts_t *b )
1889 int64_t diff = (int64_t)(a->dts - b->dts);
1890 return diff > 0 ? 1 : (diff == 0 ? 0 : -1);
1893 void lsmash_sort_timestamps_decoding_order( lsmash_media_ts_list_t *ts_list )
1895 if( !ts_list )
1896 return;
1897 qsort( ts_list->timestamp, ts_list->sample_count, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))isom_compare_dts );
1900 static int isom_compare_cts( const lsmash_media_ts_t *a, const lsmash_media_ts_t *b )
1902 int64_t diff = (int64_t)(a->cts - b->cts);
1903 return diff > 0 ? 1 : (diff == 0 ? 0 : -1);
1906 void lsmash_sort_timestamps_composition_order( lsmash_media_ts_list_t *ts_list )
1908 if( !ts_list )
1909 return;
1910 qsort( ts_list->timestamp, ts_list->sample_count, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))isom_compare_cts );
1913 int lsmash_get_max_sample_delay( lsmash_media_ts_list_t *ts_list, uint32_t *max_sample_delay )
1915 if( !ts_list || !max_sample_delay )
1916 return -1;
1917 lsmash_media_ts_t *orig_ts = ts_list->timestamp;
1918 lsmash_media_ts_t *ts = lsmash_malloc( ts_list->sample_count * sizeof(lsmash_media_ts_t) );
1919 if( !ts )
1920 return -1;
1921 ts_list->timestamp = ts;
1922 *max_sample_delay = 0;
1923 for( uint32_t i = 0; i < ts_list->sample_count; i++ )
1925 ts[i].cts = orig_ts[i].cts; /* for sorting */
1926 ts[i].dts = i;
1928 lsmash_sort_timestamps_composition_order( ts_list );
1929 for( uint32_t i = 0; i < ts_list->sample_count; i++ )
1930 if( i < ts[i].dts )
1932 uint32_t sample_delay = ts[i].dts - i;
1933 *max_sample_delay = LSMASH_MAX( *max_sample_delay, sample_delay );
1935 lsmash_free( ts );
1936 ts_list->timestamp = orig_ts;
1937 return 0;
1940 #endif /* LSMASH_DEMUXER_ENABLED */