1 /*****************************************************************************
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 */
33 #include "codecs/mp4a.h"
34 #include "codecs/mp4sys.h"
35 #include "codecs/description.h"
37 #define NO_RANDOM_ACCESS_POINT 0xffffffff
43 uint32_t number
; /* useless at present */
45 } isom_portable_chunk_t
;
54 isom_portable_chunk_t
*chunk
;
55 lsmash_sample_property_t prop
;
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 */
70 static const lsmash_class_t lsmash_timeline_class
=
75 typedef struct isom_timeline_tag isom_timeline_t
;
77 struct isom_timeline_tag
79 const lsmash_class_t
*class;
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
112 || !root
->file
->timeline
)
114 for( lsmash_entry_t
*entry
= root
->file
->timeline
->head
; entry
; entry
= entry
->next
)
116 isom_timeline_t
*timeline
= (isom_timeline_t
*)entry
->data
;
119 if( timeline
->track_ID
== track_ID
)
125 static isom_timeline_t
*isom_create_timeline( void )
127 isom_timeline_t
*timeline
= lsmash_malloc_zero( sizeof(isom_timeline_t
) );
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
);
138 static void isom_destruct_timeline_direct( isom_timeline_t
*timeline
)
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
)
154 lsmash_remove_list( file
->timeline
, isom_destruct_timeline_direct
);
157 void lsmash_destruct_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
162 || !root
->file
->timeline
)
164 for( lsmash_entry_t
*entry
= root
->file
->timeline
->head
; entry
; entry
= entry
->next
)
166 isom_timeline_t
*timeline
= (isom_timeline_t
*)entry
->data
;
169 if( timeline
->track_ID
== track_ID
)
171 lsmash_remove_entry_direct( root
->file
->timeline
, entry
, isom_destruct_timeline_direct
);
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 )
189 if( !isom_get_implicit_qt_fixed_comp_audio_sample_quants( audio
, samples_per_packet
, constant_sample_size
, &dummy
) )
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
) )
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
) );
226 if( lsmash_add_entry( timeline
->info_list
, dst_info
) < 0 )
228 lsmash_free( dst_info
);
231 *dst_info
= *src_info
;
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
) );
240 if( lsmash_add_entry( timeline
->bunch_list
, dst_bunch
) < 0 )
242 lsmash_free( dst_bunch
);
245 *dst_bunch
= *src_bunch
;
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
) );
254 if( lsmash_add_entry( timeline
->chunk_list
, dst_chunk
) < 0 )
256 lsmash_free( dst_chunk
);
259 *dst_chunk
= *src_chunk
;
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;
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
;
303 /* Seek from the first LPCM bunch. */
304 first_sample_number_in_next_bunch
= 1;
307 isom_lpcm_bunch_t
*bunch
= (isom_lpcm_bunch_t
*)lsmash_get_entry_data( timeline
->bunch_list
, bunch_number
++ );
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
++ );
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
;
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 )
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
);
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 );
345 *dts
= timeline
->last_accessed_sample_dts
- info
->duration
;
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
;
357 if( distance
-- == 0 )
359 *dts
+= info
->duration
;
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
;
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 )
374 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
377 *cts
= timeline
->ctd_shift
? (*cts
+ (int32_t)info
->offset
) : (*cts
+ info
->offset
);
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
);
386 *dts
= timeline
->last_accessed_lpcm_bunch_dts
+ (sample_number
- timeline
->last_accessed_lpcm_bunch_first_sample_number
) * bunch
->duration
;
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
);
395 *cts
= timeline
->last_accessed_lpcm_bunch_dts
+ (sample_number
- timeline
->last_accessed_lpcm_bunch_first_sample_number
) * bunch
->duration
+ bunch
->offset
;
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
);
404 *sample_duration
= info
->duration
;
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
);
413 *sample_duration
= bunch
->duration
;
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
)
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
)
430 return !!bunch
->chunk
->file
;
433 static lsmash_sample_t
*isom_read_sample_data_from_stream
436 isom_timeline_t
*timeline
,
437 uint32_t sample_length
,
441 lsmash_sample_t
*sample
= lsmash_create_sample( 0 );
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
);
449 lsmash_delete_sample( 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
);
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
);
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
;
477 static lsmash_sample_t
*isom_get_sample_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
)
480 if( isom_get_dts_from_info_list( timeline
, sample_number
, &dts
) < 0 )
482 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
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
);
490 /* Get sample info. */
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
;
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
);
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
;
515 static int isom_get_sample_info_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
, lsmash_sample_t
*sample
)
518 if( isom_get_dts_from_info_list( timeline
, sample_number
, &dts
) < 0 )
520 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
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
;
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
;
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
);
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
;
561 *sample_number_in_entry
+= 1;
564 static inline isom_sgpd_t
*isom_select_appropriate_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;
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
;
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
);
604 if( roll_data
->roll_distance
> 0 )
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 )
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
);
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
,
637 isom_group_assignment_entry_t
*assignment
= (isom_group_assignment_entry_t
*)(*sbgp_rap_entry
)->data
;
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
);
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
;
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
);
660 int lsmash_construct_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
662 if( isom_check_initializer_present( root
) < 0 )
664 lsmash_file_t
*file
= root
->file
;
667 || file
->moov
->mvhd
->timescale
== 0 )
669 /* Get track by track_ID. */
670 isom_trak_t
*trak
= isom_get_trak( file
, track_ID
);
675 || trak
->mdia
->mdhd
->timescale
== 0
677 || !trak
->mdia
->minf
->stbl
)
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
)
686 /* Create a timeline. */
687 isom_timeline_t
*timeline
= isom_create_timeline();
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
)) )
728 isom_sample_entry_t
*description
= (isom_sample_entry_t
*)lsmash_get_entry_data( &stsd
->list
, stsc_data
? stsc_data
->sample_description_index
: 1 );
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;
743 uint32_t chunk_number
= 1;
744 uint64_t offset_from_chunk
= 0;
745 uint64_t data_offset
= stco_entry
&& stco_entry
->data
747 ? ((isom_co64_entry_t
*)stco_entry
->data
)->chunk_offset
748 : ((isom_stco_entry_t
*)stco_entry
->data
)->chunk_offset
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
);
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
;
764 isom_elst_entry_t
*edit
= (isom_elst_entry_t
*)lsmash_memdup( elst_entry
->data
, sizeof(isom_elst_entry_t
) );
766 || lsmash_add_entry( timeline
->edit_list
, edit
) < 0 )
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
)
776 isom_sdtp_entry_t
*sdtp_data
= (isom_sdtp_entry_t
*)sdtp_entry
->data
;
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. */
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
;
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 )
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 */
812 isom_stts_entry_t
*stts_data
= (isom_stts_entry_t
*)stts_entry
->data
;
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
;
821 uint32_t sample_offset
;
824 isom_ctts_entry_t
*ctts_data
= (isom_ctts_entry_t
*)ctts_entry
->data
;
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
;
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. */
847 isom_stss_entry_t
*stss_data
= (isom_stss_entry_t
*)stss_entry
->data
;
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
;
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. */
864 isom_stps_entry_t
*stps_data
= (isom_stps_entry_t
*)stps_entry
->data
;
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
;
874 /* Get sample dependency info. */
877 isom_sdtp_entry_t
*sdtp_data
= (isom_sdtp_entry_t
*)sdtp_entry
->data
;
881 info
.prop
.leading
= sdtp_data
->is_leading
;
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. */
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 )
896 info
.prop
.post_roll
.identifier
= sample_number
;
897 /* Get random access point grouping info. */
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 )
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
;
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
;
920 if( !stsz_entry
->data
)
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. */
935 info
.chunk
->length
= offset_from_chunk
;
936 /* Move the next chunk. */
938 stco_entry
= stco_entry
->next
;
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
;
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
;
956 && !next_stsc_entry
->data
)
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
;
965 && !next_stsc_entry
->data
)
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
);
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 )
989 data_offset
+= info
.length
;
990 sample_number_in_chunk
+= samples_per_packet
;
992 /* OK. Let's add its info. */
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 )
1001 isom_update_bunch( &bunch
, &info
);
1004 ++ bunch
.sample_count
;
1006 else if( isom_add_sample_info_entry( timeline
, &info
) < 0 )
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" );
1013 sample_number
+= samples_per_packet
;
1016 isom_portable_chunk_t
*last_chunk
= lsmash_get_entry_data( timeline
->chunk_list
, timeline
->chunk_list
->entry_count
);
1019 if( offset_from_chunk
)
1020 last_chunk
->length
= offset_from_chunk
;
1023 /* Remove the last invalid chunk. */
1024 lsmash_remove_entry( timeline
->chunk_list
, timeline
->chunk_list
->entry_count
, NULL
);
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;
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
;
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
;
1050 isom_tfhd_t
*tfhd
= traf
->tfhd
;
1053 isom_trex_t
*trex
= isom_get_trex( file
->moov
->mvex
, tfhd
->track_ID
);
1056 /* Ignore ISOM_TF_FLAGS_DURATION_IS_EMPTY flag even if set. */
1057 if( !traf
->trun_list
.head
)
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
;
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
);
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
;
1087 if( trun
->sample_count
== 0 )
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
;
1098 data_offset
= last_sample_end_pos
;
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
;
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
;
1120 chunk
.number
= ++chunk_number
;
1121 chunk
.file
= ref_file
;
1122 if( isom_add_portable_chunk_entry( timeline
, &chunk
) < 0 )
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
;
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
;
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
;
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
;
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
;
1185 sample_flags
= trex
->default_sample_flags
;
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
;
1197 sdtp_entry
= sdtp_entry
->next
;
1198 sdtp_data
= sdtp_entry
? (isom_sdtp_entry_t
*)sdtp_entry
->data
: NULL
;
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
;
1215 /* Get roll recovery grouping info. */
1216 uint32_t roll_id
= sample_count
+ sample_number
;
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 )
1223 info
.prop
.post_roll
.identifier
= roll_id
;
1224 /* Get random access point grouping info. */
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 )
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. */
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
;
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
;
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
;
1258 /* OK. Let's add its info. */
1259 if( isom_add_sample_info_entry( timeline
, &info
) < 0)
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 )
1273 isom_update_bunch( &bunch
, &info
);
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" );
1285 data_offset
+= info
.length
;
1286 last_sample_end_pos
= data_offset
;
1288 row_entry
= row_entry
->next
;
1291 if( !need_data_offset_only
)
1292 sample_count
+= sample_number
- 1;
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 )
1303 if( lsmash_add_entry( file
->timeline
, timeline
) < 0 )
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
;
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
;
1329 isom_destruct_timeline_direct( timeline
);
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
)
1337 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1338 if( !timeline
|| sample_number
> timeline
->sample_count
)
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
)
1347 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1348 if( !timeline
|| sample_number
> timeline
->sample_count
)
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
)
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
)
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
)
1379 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1382 *ctd_shift
= timeline
->ctd_shift
;
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
-- );
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
;
1399 info
= (isom_sample_info_t
*)entry
->data
;
1402 *rap_number
= sample_number
+ 1;
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
++ );
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
;
1419 info
= (isom_sample_info_t
*)entry
->data
;
1422 *rap_number
= sample_number
- 1;
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
)
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 )
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
)
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. */
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 )
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
;
1460 *ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1467 if( isom_get_closest_random_accessible_point_from_media_timeline_internal( timeline
, sample_number
, rap_number
) )
1469 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, *rap_number
);
1473 *ra_flags
= info
->prop
.ra_flags
;
1478 if( sample_number
< *rap_number
)
1479 /* Impossible to desire to decode the sample of given number correctly. */
1481 else if( !(info
->prop
.ra_flags
& ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR
) )
1485 /* Count leading samples. */
1486 uint32_t current_sample_number
= *rap_number
+ 1;
1488 if( isom_get_dts_from_info_list( timeline
, *rap_number
, &dts
) < 0 )
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
++ );
1499 uint64_t cts
= timeline
->ctd_shift
? (dts
+ (int32_t)info
->offset
+ timeline
->ctd_shift
) : (dts
+ info
->offset
);
1504 if( !distance
|| sample_number
== *rap_number
)
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. */
1513 info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, prev_rap_number
);
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
;
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. */
1541 if( prev_rap_number
+ info
->prop
.pre_roll
.distance
<= *rap_number
)
1544 * |<---- pre-roll distance ---->|
1545 * |<--------- distance -------->|
1546 * media +++++++++++++++++++++++++ *** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1548 * random accessible point starting point random accessible point given sample
1551 *distance
= info
->prop
.pre_roll
.distance
;
1554 else if( !(info
->prop
.ra_flags
& ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR
) )
1557 * |<------------ pre-roll distance ------------------>|
1558 * |<------ distance ------->|
1559 * media ++++++++++++++++ *** ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1561 * random accessible point random accessible point given sample
1562 * (starting point) (complete)
1564 *distance
= *rap_number
- prev_rap_number
;
1569 /* Post-roll recovery */
1570 if( sample_number
>= info
->prop
.post_roll
.complete
)
1572 * |<----- post-roll distance ----->|
1574 * media +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1576 * random accessible point complete given sample
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. */
1586 info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, prev_rap_number
);
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
;
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
)
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
)
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
);
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
);
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
);
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 )
1648 lsmash_file_t
*dst_file
= dst
->file
->initializer
;
1649 isom_trak_t
*dst_trak
= isom_get_trak( dst_file
, dst_track_ID
);
1651 || !dst_file
->moov
->mvhd
1652 || dst_file
->moov
->mvhd
->timescale
== 0
1655 || !dst_trak
->mdia
->mdhd
1656 || dst_trak
->mdia
->mdhd
->timescale
== 0
1657 || !dst_trak
->mdia
->minf
1658 || !dst_trak
->mdia
->minf
->stbl
)
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
);
1675 || !src_trak
->edts
->elst
1676 || !src_trak
->edts
->elst
->list
1679 /* Get from constructed timeline instead of boxes. */
1680 isom_timeline_t
*src_timeline
= isom_get_timeline( src
, src_track_ID
);
1682 && src_timeline
->movie_timescale
1683 && src_timeline
->media_timescale
)
1685 src_entry
= src_timeline
->edit_list
->head
;
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
)
1700 || !src_file
->moov
->mvhd
1701 || src_file
->moov
->mvhd
->timescale
== 0
1704 || !src_trak
->mdia
->mdhd
1705 || src_trak
->mdia
->mdhd
->timescale
== 0
1706 || !src_trak
->mdia
->minf
1707 || !src_trak
->mdia
->minf
->stbl
)
1709 src_entry
= src_trak
->edts
->elst
->list
->head
;
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
)) )
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
;
1729 isom_elst_entry_t
*src_data
= (isom_elst_entry_t
*)src_entry
->data
;
1732 isom_elst_entry_t
*dst_data
= (isom_elst_entry_t
*)lsmash_malloc( sizeof(isom_elst_entry_t
) );
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
;
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;
1749 dst_data
->media_time
= ISOM_EDIT_MODE_EMPTY
;
1750 if( lsmash_add_entry( dst_list
, dst_data
) < 0 )
1752 lsmash_free( dst_data
);
1755 src_entry
= src_entry
->next
;
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
)
1764 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1767 if( timeline
->info_list
->entry_count
== 0 )
1769 lsmash_log( timeline
, LSMASH_LOG_ERROR
, "Changing timestamps of LPCM track is not supported.\n" );
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
;
1776 return -1; /* DTS must start from value zero. */
1778 uint32_t sample_count
= ts_list
->sample_count
;
1780 if( timeline
->info_list
->entry_count
> 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
) )
1790 info
->duration
= ts
[i
].dts
- ts
[i
- 1].dts
;
1791 entry
= entry
->next
;
1799 /* Copy the previous duration. */
1800 ((isom_sample_info_t
*)entry
->data
)->duration
= info
->duration
;
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
;
1808 * ToDo: hint track must not have any sample_offset. */
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
;
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. */
1824 int lsmash_get_media_timestamps( lsmash_root_t
*root
, uint32_t track_ID
, lsmash_media_ts_list_t
*ts_list
)
1828 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1831 uint32_t sample_count
= timeline
->info_list
->entry_count
;
1834 ts_list
->sample_count
= 0;
1835 ts_list
->timestamp
= NULL
;
1838 lsmash_media_ts_t
*ts
= lsmash_malloc( sample_count
* sizeof(lsmash_media_ts_t
) );
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
;
1853 ts
[i
].cts
= timeline
->ctd_shift
? (dts
+ (int32_t)info
->offset
) : (dts
+ info
->offset
);
1854 dts
+= info
->duration
;
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
;
1866 for( uint32_t j
= 0; j
< bunch
->sample_count
; j
++ )
1869 ts
[i
].cts
= timeline
->ctd_shift
? (dts
+ (int32_t)bunch
->offset
) : (dts
+ bunch
->offset
);
1870 dts
+= bunch
->duration
;
1874 ts_list
->sample_count
= sample_count
;
1875 ts_list
->timestamp
= ts
;
1879 void lsmash_delete_media_timestamps( lsmash_media_ts_list_t
*ts_list
)
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
)
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
)
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
)
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
) );
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 */
1928 lsmash_sort_timestamps_composition_order( ts_list
);
1929 for( uint32_t i
= 0; i
< ts_list
->sample_count
; i
++ )
1932 uint32_t sample_delay
= ts
[i
].dts
- i
;
1933 *max_sample_delay
= LSMASH_MAX( *max_sample_delay
, sample_delay
);
1936 ts_list
->timestamp
= orig_ts
;
1940 #endif /* LSMASH_DEMUXER_ENABLED */