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 */
34 #include "codecs/mp4a.h"
35 #include "codecs/mp4sys.h"
36 #include "codecs/description.h"
38 #include "importer/importer.h"
40 #define NO_RANDOM_ACCESS_POINT 0xffffffff
49 isom_portable_chunk_t
*chunk
;
50 lsmash_sample_property_t prop
;
53 static const lsmash_class_t lsmash_timeline_class
=
58 struct isom_timeline_tag
60 const lsmash_class_t
*class;
62 uint32_t movie_timescale
;
63 uint32_t media_timescale
;
64 uint32_t sample_count
;
65 uint32_t max_sample_size
;
66 uint32_t ctd_shift
; /* shift from composition to decode timeline */
67 uint64_t media_duration
;
68 uint64_t track_duration
;
69 uint32_t last_accessed_sample_number
;
70 uint64_t last_accessed_sample_dts
;
71 uint32_t last_accessed_lpcm_bunch_number
;
72 uint32_t last_accessed_lpcm_bunch_duration
;
73 uint32_t last_accessed_lpcm_bunch_sample_count
;
74 uint32_t last_accessed_lpcm_bunch_first_sample_number
;
75 uint64_t last_accessed_lpcm_bunch_dts
;
76 lsmash_entry_list_t edit_list
[1]; /* list of edits */
77 lsmash_entry_list_t chunk_list
[1]; /* list of chunks */
78 lsmash_entry_list_t info_list
[1]; /* list of sample info */
79 lsmash_entry_list_t bunch_list
[1]; /* list of LPCM bunch */
80 int (*get_dts
)( isom_timeline_t
*timeline
, uint32_t sample_number
, uint64_t *dts
);
81 int (*get_cts
)( isom_timeline_t
*timeline
, uint32_t sample_number
, uint64_t *cts
);
82 int (*get_sample_duration
)( isom_timeline_t
*timeline
, uint32_t sample_number
, uint32_t *sample_duration
);
83 lsmash_sample_t
*(*get_sample
)( isom_timeline_t
*timeline
, uint32_t sample_number
);
84 int (*get_sample_info
)( isom_timeline_t
*timeline
, uint32_t sample_number
, lsmash_sample_t
*sample
);
85 int (*get_sample_property
)( isom_timeline_t
*timeline
, uint32_t sample_number
, lsmash_sample_property_t
*prop
);
86 int (*check_sample_existence
)( isom_timeline_t
*timeline
, uint32_t sample_number
);
89 isom_timeline_t
*isom_get_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
91 if( isom_check_initializer_present( root
) < 0
93 || !root
->file
->timeline
)
95 for( lsmash_entry_t
*entry
= root
->file
->timeline
->head
; entry
; entry
= entry
->next
)
97 isom_timeline_t
*timeline
= (isom_timeline_t
*)entry
->data
;
100 if( timeline
->track_ID
== track_ID
)
106 isom_timeline_t
*isom_timeline_create( void )
108 isom_timeline_t
*timeline
= lsmash_malloc_zero( sizeof(isom_timeline_t
) );
111 timeline
->class = &lsmash_timeline_class
;
112 lsmash_init_entry_list( timeline
->edit_list
);
113 lsmash_init_entry_list( timeline
->chunk_list
);
114 lsmash_init_entry_list( timeline
->info_list
);
115 lsmash_init_entry_list( timeline
->bunch_list
);
119 void isom_timeline_destroy( isom_timeline_t
*timeline
)
123 lsmash_remove_entries( timeline
->edit_list
, NULL
);
124 lsmash_remove_entries( timeline
->chunk_list
, NULL
); /* chunk data must be already freed. */
125 lsmash_remove_entries( timeline
->info_list
, NULL
);
126 lsmash_remove_entries( timeline
->bunch_list
, NULL
);
127 lsmash_free( timeline
);
130 void isom_remove_timelines( lsmash_file_t
*file
)
135 lsmash_remove_list( file
->timeline
, isom_timeline_destroy
);
138 void lsmash_destruct_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
143 || !root
->file
->timeline
)
145 for( lsmash_entry_t
*entry
= root
->file
->timeline
->head
; entry
; entry
= entry
->next
)
147 isom_timeline_t
*timeline
= (isom_timeline_t
*)entry
->data
;
150 if( timeline
->track_ID
== track_ID
)
152 lsmash_remove_entry_direct( root
->file
->timeline
, entry
, isom_timeline_destroy
);
158 int isom_timeline_set_track_ID
160 isom_timeline_t
*timeline
,
164 if( !timeline
|| track_ID
== 0 )
165 return LSMASH_ERR_FUNCTION_PARAM
;
166 timeline
->track_ID
= track_ID
;
170 int isom_timeline_set_movie_timescale
172 isom_timeline_t
*timeline
,
173 uint32_t movie_timescale
176 if( !timeline
|| movie_timescale
== 0 )
177 return LSMASH_ERR_FUNCTION_PARAM
;
178 timeline
->movie_timescale
= movie_timescale
;
182 int isom_timeline_set_media_timescale
184 isom_timeline_t
*timeline
,
185 uint32_t media_timescale
188 if( !timeline
|| media_timescale
== 0 )
189 return LSMASH_ERR_FUNCTION_PARAM
;
190 timeline
->media_timescale
= media_timescale
;
194 int isom_timeline_set_sample_count
196 isom_timeline_t
*timeline
,
197 uint32_t sample_count
200 if( !timeline
|| sample_count
== 0 )
201 return LSMASH_ERR_FUNCTION_PARAM
;
202 timeline
->sample_count
= sample_count
;
206 int isom_timeline_set_max_sample_size
208 isom_timeline_t
*timeline
,
209 uint32_t max_sample_size
212 if( !timeline
|| max_sample_size
== 0 )
213 return LSMASH_ERR_FUNCTION_PARAM
;
214 timeline
->max_sample_size
= max_sample_size
;
218 int isom_timeline_set_media_duration
220 isom_timeline_t
*timeline
,
221 uint32_t media_duration
224 if( !timeline
|| media_duration
== 0 )
225 return LSMASH_ERR_FUNCTION_PARAM
;
226 timeline
->media_duration
= media_duration
;
230 int isom_timeline_set_track_duration
232 isom_timeline_t
*timeline
,
233 uint32_t track_duration
236 if( !timeline
|| track_duration
== 0 )
237 return LSMASH_ERR_FUNCTION_PARAM
;
238 timeline
->track_duration
= track_duration
;
242 static void isom_get_qt_fixed_comp_audio_sample_quants
244 isom_timeline_t
*timeline
,
245 isom_sample_entry_t
*description
,
246 uint32_t *samples_per_packet
,
247 uint32_t *constant_sample_size
250 isom_audio_entry_t
*audio
= (isom_audio_entry_t
*)description
;
251 if( audio
->version
== 0 )
254 if( !isom_get_implicit_qt_fixed_comp_audio_sample_quants( audio
, samples_per_packet
, constant_sample_size
, &dummy
) )
257 if( !isom_is_lpcm_audio( audio
) )
258 lsmash_log( timeline
, LSMASH_LOG_WARNING
, "unsupported implicit sample table!\n" );
259 *samples_per_packet
= 1;
260 *constant_sample_size
= (audio
->samplesize
* audio
->channelcount
) / 8;
263 else if( audio
->version
== 1 )
265 *samples_per_packet
= audio
->samplesPerPacket
;
266 *constant_sample_size
= audio
->bytesPerFrame
;
268 else /* if( audio->version == 2 ) */
270 *samples_per_packet
= audio
->constLPCMFramesPerAudioPacket
;
271 *constant_sample_size
= audio
->constBytesPerAudioPacket
;
275 static int isom_is_qt_fixed_compressed_audio
277 isom_sample_entry_t
*description
280 if( (description
->manager
& LSMASH_VIDEO_DESCRIPTION
) || !isom_is_qt_audio( description
->type
) )
282 /* LPCM is a special case of fixed compression. */
283 return (((isom_audio_entry_t
*)description
)->compression_ID
!= QT_AUDIO_COMPRESSION_ID_VARIABLE_COMPRESSION
);
286 static int isom_add_sample_info_entry( isom_timeline_t
*timeline
, isom_sample_info_t
*src_info
)
288 isom_sample_info_t
*dst_info
= lsmash_malloc( sizeof(isom_sample_info_t
) );
290 return LSMASH_ERR_MEMORY_ALLOC
;
291 if( lsmash_add_entry( timeline
->info_list
, dst_info
) < 0 )
293 lsmash_free( dst_info
);
294 return LSMASH_ERR_MEMORY_ALLOC
;
296 *dst_info
= *src_info
;
300 int isom_add_lpcm_bunch_entry( isom_timeline_t
*timeline
, isom_lpcm_bunch_t
*src_bunch
)
302 isom_lpcm_bunch_t
*dst_bunch
= lsmash_malloc( sizeof(isom_lpcm_bunch_t
) );
304 return LSMASH_ERR_MEMORY_ALLOC
;
305 if( lsmash_add_entry( timeline
->bunch_list
, dst_bunch
) < 0 )
307 lsmash_free( dst_bunch
);
308 return LSMASH_ERR_MEMORY_ALLOC
;
310 *dst_bunch
= *src_bunch
;
314 static int isom_add_portable_chunk_entry( isom_timeline_t
*timeline
, isom_portable_chunk_t
*src_chunk
)
316 isom_portable_chunk_t
*dst_chunk
= lsmash_malloc( sizeof(isom_portable_chunk_t
) );
318 return LSMASH_ERR_MEMORY_ALLOC
;
319 if( lsmash_add_entry( timeline
->chunk_list
, dst_chunk
) < 0 )
321 lsmash_free( dst_chunk
);
322 return LSMASH_ERR_MEMORY_ALLOC
;
324 *dst_chunk
= *src_chunk
;
328 static int isom_compare_lpcm_sample_info( isom_lpcm_bunch_t
*bunch
, isom_sample_info_t
*info
)
330 return info
->duration
!= bunch
->duration
331 || info
->offset
!= bunch
->offset
332 || info
->length
!= bunch
->length
333 || info
->index
!= bunch
->index
334 || info
->chunk
!= bunch
->chunk
;
337 static void isom_update_bunch( isom_lpcm_bunch_t
*bunch
, isom_sample_info_t
*info
)
339 bunch
->pos
= info
->pos
;
340 bunch
->duration
= info
->duration
;
341 bunch
->offset
= info
->offset
;
342 bunch
->length
= info
->length
;
343 bunch
->index
= info
->index
;
344 bunch
->chunk
= info
->chunk
;
345 bunch
->prop
= info
->prop
;
346 bunch
->sample_count
= 1;
349 static isom_lpcm_bunch_t
*isom_get_bunch( isom_timeline_t
*timeline
, uint32_t sample_number
)
351 if( sample_number
>= timeline
->last_accessed_lpcm_bunch_first_sample_number
352 && sample_number
< timeline
->last_accessed_lpcm_bunch_first_sample_number
+ timeline
->last_accessed_lpcm_bunch_sample_count
)
353 /* Get from the last accessed LPCM bunch. */
354 return (isom_lpcm_bunch_t
*)lsmash_get_entry_data( timeline
->bunch_list
, timeline
->last_accessed_lpcm_bunch_number
);
355 uint32_t first_sample_number_in_next_bunch
;
356 uint32_t bunch_number
= 1;
358 if( timeline
->last_accessed_lpcm_bunch_first_sample_number
359 && timeline
->last_accessed_lpcm_bunch_first_sample_number
<= sample_number
)
361 first_sample_number_in_next_bunch
= timeline
->last_accessed_lpcm_bunch_first_sample_number
+ timeline
->last_accessed_lpcm_bunch_sample_count
;
362 bunch_number
+= timeline
->last_accessed_lpcm_bunch_number
;
363 bunch_dts
= timeline
->last_accessed_lpcm_bunch_dts
364 + timeline
->last_accessed_lpcm_bunch_duration
* timeline
->last_accessed_lpcm_bunch_sample_count
;
368 /* Seek from the first LPCM bunch. */
369 first_sample_number_in_next_bunch
= 1;
372 isom_lpcm_bunch_t
*bunch
= (isom_lpcm_bunch_t
*)lsmash_get_entry_data( timeline
->bunch_list
, bunch_number
++ );
375 first_sample_number_in_next_bunch
+= bunch
->sample_count
;
376 while( sample_number
>= first_sample_number_in_next_bunch
)
378 bunch_dts
+= bunch
->duration
* bunch
->sample_count
;
379 bunch
= (isom_lpcm_bunch_t
*)lsmash_get_entry_data( timeline
->bunch_list
, bunch_number
++ );
382 first_sample_number_in_next_bunch
+= bunch
->sample_count
;
384 timeline
->last_accessed_lpcm_bunch_dts
= bunch_dts
;
385 timeline
->last_accessed_lpcm_bunch_number
= bunch_number
- 1;
386 timeline
->last_accessed_lpcm_bunch_duration
= bunch
->duration
;
387 timeline
->last_accessed_lpcm_bunch_sample_count
= bunch
->sample_count
;
388 timeline
->last_accessed_lpcm_bunch_first_sample_number
= first_sample_number_in_next_bunch
- bunch
->sample_count
;
392 static int isom_get_dts_from_info_list( isom_timeline_t
*timeline
, uint32_t sample_number
, uint64_t *dts
)
394 if( sample_number
== timeline
->last_accessed_sample_number
)
395 *dts
= timeline
->last_accessed_sample_dts
;
396 else if( sample_number
== 1 )
398 else if( sample_number
== timeline
->last_accessed_sample_number
+ 1 )
400 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, timeline
->last_accessed_sample_number
);
402 return LSMASH_ERR_NAMELESS
;
403 *dts
= timeline
->last_accessed_sample_dts
+ info
->duration
;
405 else if( sample_number
== timeline
->last_accessed_sample_number
- 1 )
407 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, timeline
->last_accessed_sample_number
- 1 );
409 return LSMASH_ERR_NAMELESS
;
410 *dts
= timeline
->last_accessed_sample_dts
- info
->duration
;
415 uint32_t distance
= sample_number
- 1;
416 lsmash_entry_t
*entry
;
417 for( entry
= timeline
->info_list
->head
; entry
; entry
= entry
->next
)
419 isom_sample_info_t
*info
= (isom_sample_info_t
*)entry
->data
;
421 return LSMASH_ERR_NAMELESS
;
422 if( distance
-- == 0 )
424 *dts
+= info
->duration
;
427 return LSMASH_ERR_NAMELESS
;
429 /* Note: last_accessed_sample_number is always updated together with last_accessed_sample_dts, and vice versa. */
430 timeline
->last_accessed_sample_dts
= *dts
;
431 timeline
->last_accessed_sample_number
= sample_number
;
435 static int isom_get_cts_from_info_list( isom_timeline_t
*timeline
, uint32_t sample_number
, uint64_t *cts
)
437 int ret
= isom_get_dts_from_info_list( timeline
, sample_number
, cts
);
440 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
442 return LSMASH_ERR_NAMELESS
;
443 *cts
= timeline
->ctd_shift
? (*cts
+ (int32_t)info
->offset
) : (*cts
+ info
->offset
);
447 static int isom_get_dts_from_bunch_list( isom_timeline_t
*timeline
, uint32_t sample_number
, uint64_t *dts
)
449 isom_lpcm_bunch_t
*bunch
= isom_get_bunch( timeline
, sample_number
);
451 return LSMASH_ERR_NAMELESS
;
452 *dts
= timeline
->last_accessed_lpcm_bunch_dts
+ (sample_number
- timeline
->last_accessed_lpcm_bunch_first_sample_number
) * bunch
->duration
;
456 static int isom_get_cts_from_bunch_list( isom_timeline_t
*timeline
, uint32_t sample_number
, uint64_t *cts
)
458 isom_lpcm_bunch_t
*bunch
= isom_get_bunch( timeline
, sample_number
);
460 return LSMASH_ERR_NAMELESS
;
461 *cts
= timeline
->last_accessed_lpcm_bunch_dts
+ (sample_number
- timeline
->last_accessed_lpcm_bunch_first_sample_number
) * bunch
->duration
+ bunch
->offset
;
465 static int isom_get_sample_duration_from_info_list( isom_timeline_t
*timeline
, uint32_t sample_number
, uint32_t *sample_duration
)
467 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
469 return LSMASH_ERR_NAMELESS
;
470 *sample_duration
= info
->duration
;
474 static int isom_get_sample_duration_from_bunch_list( isom_timeline_t
*timeline
, uint32_t sample_number
, uint32_t *sample_duration
)
476 isom_lpcm_bunch_t
*bunch
= isom_get_bunch( timeline
, sample_number
);
478 return LSMASH_ERR_NAMELESS
;
479 *sample_duration
= bunch
->duration
;
483 static int isom_check_sample_existence_in_info_list( isom_timeline_t
*timeline
, uint32_t sample_number
)
485 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
486 if( !info
|| !info
->chunk
)
488 return !!info
->chunk
->file
;
491 static int isom_check_sample_existence_in_bunch_list( isom_timeline_t
*timeline
, uint32_t sample_number
)
493 isom_lpcm_bunch_t
*bunch
= isom_get_bunch( timeline
, sample_number
);
494 if( !bunch
|| !bunch
->chunk
)
496 return !!bunch
->chunk
->file
;
499 static lsmash_sample_t
*isom_read_sample_data_from_stream
502 isom_timeline_t
*timeline
,
503 uint32_t sample_length
,
507 lsmash_sample_t
*sample
= lsmash_create_sample( 0 );
510 lsmash_bs_t
*bs
= file
->bs
;
511 lsmash_bs_read_seek( bs
, sample_pos
, SEEK_SET
);
512 sample
->data
= lsmash_bs_get_bytes( bs
, sample_length
);
515 lsmash_delete_sample( sample
);
521 static lsmash_sample_t
*isom_get_lpcm_sample_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
)
523 isom_lpcm_bunch_t
*bunch
= isom_get_bunch( timeline
, sample_number
);
527 /* Get data of a sample from the stream. */
528 uint64_t sample_number_offset
= sample_number
- timeline
->last_accessed_lpcm_bunch_first_sample_number
;
529 uint64_t sample_pos
= bunch
->pos
+ sample_number_offset
* bunch
->length
;
530 lsmash_sample_t
*sample
= isom_read_sample_data_from_stream( bunch
->chunk
->file
, timeline
, bunch
->length
, sample_pos
);
533 /* Get sample info. */
534 sample
->dts
= timeline
->last_accessed_lpcm_bunch_dts
+ sample_number_offset
* bunch
->duration
;
535 sample
->cts
= timeline
->ctd_shift
? (sample
->dts
+ (int32_t)bunch
->offset
) : (sample
->dts
+ bunch
->offset
);
536 sample
->pos
= sample_pos
;
537 sample
->length
= bunch
->length
;
538 sample
->index
= bunch
->index
;
539 sample
->prop
= bunch
->prop
;
543 static lsmash_sample_t
*isom_get_sample_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
)
546 if( isom_get_dts_from_info_list( timeline
, sample_number
, &dts
) < 0 )
548 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
552 /* Get data of a sample from the stream. */
553 lsmash_sample_t
*sample
= isom_read_sample_data_from_stream( info
->chunk
->file
, timeline
, info
->length
, info
->pos
);
556 /* Get sample info. */
558 sample
->cts
= timeline
->ctd_shift
? (dts
+ (int32_t)info
->offset
) : (dts
+ info
->offset
);
559 sample
->pos
= info
->pos
;
560 sample
->length
= info
->length
;
561 sample
->index
= info
->index
;
562 sample
->prop
= info
->prop
;
566 static int isom_get_lpcm_sample_info_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
, lsmash_sample_t
*sample
)
568 isom_lpcm_bunch_t
*bunch
= isom_get_bunch( timeline
, sample_number
);
570 return LSMASH_ERR_NAMELESS
;
571 uint64_t sample_number_offset
= sample_number
- timeline
->last_accessed_lpcm_bunch_first_sample_number
;
572 sample
->dts
= timeline
->last_accessed_lpcm_bunch_dts
+ sample_number_offset
* bunch
->duration
;
573 sample
->cts
= timeline
->ctd_shift
? (sample
->dts
+ (int32_t)bunch
->offset
) : (sample
->dts
+ bunch
->offset
);
574 sample
->pos
= bunch
->pos
+ sample_number_offset
* bunch
->length
;
575 sample
->length
= bunch
->length
;
576 sample
->index
= bunch
->index
;
577 sample
->prop
= bunch
->prop
;
581 static int isom_get_sample_info_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
, lsmash_sample_t
*sample
)
584 int ret
= isom_get_dts_from_info_list( timeline
, sample_number
, &dts
);
587 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
589 return LSMASH_ERR_NAMELESS
;
591 sample
->cts
= timeline
->ctd_shift
? (dts
+ (int32_t)info
->offset
) : (dts
+ info
->offset
);
592 sample
->pos
= info
->pos
;
593 sample
->length
= info
->length
;
594 sample
->index
= info
->index
;
595 sample
->prop
= info
->prop
;
599 static int isom_get_lpcm_sample_property_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
, lsmash_sample_property_t
*prop
)
601 memset( prop
, 0, sizeof(lsmash_sample_property_t
) );
602 prop
->ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
606 static int isom_get_sample_property_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
, lsmash_sample_property_t
*prop
)
608 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, sample_number
);
610 return LSMASH_ERR_NAMELESS
;
615 static void isom_timeline_set_sample_getter_funcs
617 isom_timeline_t
*timeline
620 timeline
->get_dts
= isom_get_dts_from_info_list
;
621 timeline
->get_cts
= isom_get_cts_from_info_list
;
622 timeline
->get_sample_duration
= isom_get_sample_duration_from_info_list
;
623 timeline
->check_sample_existence
= isom_check_sample_existence_in_info_list
;
624 timeline
->get_sample
= isom_get_sample_from_media_timeline
;
625 timeline
->get_sample_info
= isom_get_sample_info_from_media_timeline
;
626 timeline
->get_sample_property
= isom_get_sample_property_from_media_timeline
;
629 void isom_timeline_set_lpcm_sample_getter_funcs
631 isom_timeline_t
*timeline
634 timeline
->get_dts
= isom_get_dts_from_bunch_list
;
635 timeline
->get_cts
= isom_get_cts_from_bunch_list
;
636 timeline
->get_sample_duration
= isom_get_sample_duration_from_bunch_list
;
637 timeline
->check_sample_existence
= isom_check_sample_existence_in_bunch_list
;
638 timeline
->get_sample
= isom_get_lpcm_sample_from_media_timeline
;
639 timeline
->get_sample_info
= isom_get_lpcm_sample_info_from_media_timeline
;
640 timeline
->get_sample_property
= isom_get_lpcm_sample_property_from_media_timeline
;
643 static inline void isom_increment_sample_number_in_entry
645 uint32_t *sample_number_in_entry
,
646 lsmash_entry_t
**entry
,
647 uint32_t sample_count
650 if( *sample_number_in_entry
== sample_count
)
652 *sample_number_in_entry
= 1;
653 *entry
= (*entry
)->next
;
656 *sample_number_in_entry
+= 1;
659 static inline isom_sgpd_t
*isom_select_appropriate_sgpd
662 isom_sgpd_t
*sgpd_frag
,
663 uint32_t *group_description_index
666 if( sgpd_frag
&& *group_description_index
>= 0x10000 )
668 /* The specification doesn't define 0x10000 explicitly, however says that there must be fewer than
669 * 65536 group definitions for this track and grouping type in the sample table in the Movie Box.
670 * So, we assume 0x10000 is equivalent to 0. */
671 *group_description_index
-= 0x10000;
678 static int isom_get_roll_recovery_grouping_info
680 isom_timeline_t
*timeline
,
681 lsmash_entry_t
**sbgp_roll_entry
,
682 isom_sgpd_t
*sgpd_roll
,
683 isom_sgpd_t
*sgpd_frag_roll
,
684 uint32_t *sample_number_in_sbgp_roll_entry
,
685 isom_sample_info_t
*info
,
686 uint32_t sample_number
689 isom_group_assignment_entry_t
*assignment
= (isom_group_assignment_entry_t
*)(*sbgp_roll_entry
)->data
;
691 return LSMASH_ERR_NAMELESS
;
692 if( assignment
->group_description_index
)
694 uint32_t group_description_index
= assignment
->group_description_index
;
695 isom_sgpd_t
*sgpd
= isom_select_appropriate_sgpd( sgpd_roll
, sgpd_frag_roll
, &group_description_index
);
696 isom_roll_entry_t
*roll_data
= (isom_roll_entry_t
*)lsmash_get_entry_data( sgpd
->list
, group_description_index
);
699 if( roll_data
->roll_distance
> 0 )
702 info
->prop
.post_roll
.complete
= sample_number
+ roll_data
->roll_distance
;
703 if( info
->prop
.ra_flags
== ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE
)
704 info
->prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START
;
706 else if( roll_data
->roll_distance
< 0 )
709 info
->prop
.pre_roll
.distance
= -roll_data
->roll_distance
;
710 if( info
->prop
.ra_flags
== ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE
)
711 info
->prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_PRE_ROLL_END
;
714 else if( *sample_number_in_sbgp_roll_entry
== 1 && group_description_index
)
715 lsmash_log( timeline
, LSMASH_LOG_WARNING
, "a description of roll recoveries is not found in the Sample Group Description Box.\n" );
717 isom_increment_sample_number_in_entry( sample_number_in_sbgp_roll_entry
, sbgp_roll_entry
, assignment
->sample_count
);
721 static int isom_get_random_access_point_grouping_info
723 isom_timeline_t
*timeline
,
724 lsmash_entry_t
**sbgp_rap_entry
,
725 isom_sgpd_t
*sgpd_rap
,
726 isom_sgpd_t
*sgpd_frag_rap
,
727 uint32_t *sample_number_in_sbgp_rap_entry
,
728 isom_sample_info_t
*info
,
732 isom_group_assignment_entry_t
*assignment
= (isom_group_assignment_entry_t
*)(*sbgp_rap_entry
)->data
;
734 return LSMASH_ERR_NAMELESS
;
735 if( assignment
->group_description_index
&& (info
->prop
.ra_flags
== ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE
) )
737 uint32_t group_description_index
= assignment
->group_description_index
;
738 isom_sgpd_t
*sgpd
= isom_select_appropriate_sgpd( sgpd_rap
, sgpd_frag_rap
, &group_description_index
);
739 isom_rap_entry_t
*rap_data
= (isom_rap_entry_t
*)lsmash_get_entry_data( sgpd
->list
, group_description_index
);
742 /* If this is not an open RAP, we treat it as an unknown RAP since non-IDR sample could make a closed GOP. */
743 info
->prop
.ra_flags
|= (rap_data
->num_leading_samples_known
&& !!rap_data
->num_leading_samples
)
744 ? ISOM_SAMPLE_RANDOM_ACCESS_FLAG_OPEN_RAP
745 : ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
748 else if( *sample_number_in_sbgp_rap_entry
== 1 && group_description_index
)
749 lsmash_log( timeline
, LSMASH_LOG_WARNING
, "a description of random access points is not found in the Sample Group Description Box.\n" );
751 isom_increment_sample_number_in_entry( sample_number_in_sbgp_rap_entry
, sbgp_rap_entry
, assignment
->sample_count
);
755 int isom_timeline_construct( lsmash_root_t
*root
, uint32_t track_ID
)
757 if( isom_check_initializer_present( root
) < 0 )
758 return LSMASH_ERR_FUNCTION_PARAM
;
759 lsmash_file_t
*file
= root
->file
;
762 || file
->moov
->mvhd
->timescale
== 0 )
763 return LSMASH_ERR_INVALID_DATA
;
764 /* Get track by track_ID. */
765 isom_trak_t
*trak
= isom_get_trak( file
, track_ID
);
770 || trak
->mdia
->mdhd
->timescale
== 0
772 || !trak
->mdia
->minf
->stbl
)
773 return LSMASH_ERR_INVALID_DATA
;
774 /* Create a timeline list if it doesn't exist. */
775 if( !file
->timeline
)
777 file
->timeline
= lsmash_create_entry_list();
778 if( !file
->timeline
)
779 return LSMASH_ERR_MEMORY_ALLOC
;
781 /* Create a timeline. */
782 isom_timeline_t
*timeline
= isom_timeline_create();
784 return LSMASH_ERR_MEMORY_ALLOC
;
785 timeline
->track_ID
= track_ID
;
786 timeline
->movie_timescale
= file
->moov
->mvhd
->timescale
;
787 timeline
->media_timescale
= trak
->mdia
->mdhd
->timescale
;
788 timeline
->track_duration
= trak
->tkhd
->duration
;
789 /* Preparation for construction. */
790 isom_elst_t
*elst
= trak
->edts
? trak
->edts
->elst
: NULL
;
791 isom_minf_t
*minf
= trak
->mdia
->minf
;
792 isom_dref_t
*dref
= minf
->dinf
->dref
;
793 isom_stbl_t
*stbl
= minf
->stbl
;
794 isom_stsd_t
*stsd
= stbl
->stsd
;
795 isom_stts_t
*stts
= stbl
->stts
;
796 isom_ctts_t
*ctts
= stbl
->ctts
;
797 isom_stss_t
*stss
= stbl
->stss
;
798 isom_stps_t
*stps
= stbl
->stps
;
799 isom_sdtp_t
*sdtp
= stbl
->sdtp
;
800 isom_stsc_t
*stsc
= stbl
->stsc
;
801 isom_stsz_t
*stsz
= stbl
->stsz
;
802 isom_stco_t
*stco
= stbl
->stco
;
803 isom_sgpd_t
*sgpd_rap
= isom_get_sample_group_description( stbl
, ISOM_GROUP_TYPE_RAP
);
804 isom_sbgp_t
*sbgp_rap
= isom_get_sample_to_group ( stbl
, ISOM_GROUP_TYPE_RAP
);
805 isom_sgpd_t
*sgpd_roll
= isom_get_roll_recovery_sample_group_description( &stbl
->sgpd_list
);
806 isom_sbgp_t
*sbgp_roll
= isom_get_roll_recovery_sample_to_group ( &stbl
->sbgp_list
);
807 lsmash_entry_t
*elst_entry
= elst
&& elst
->list
? elst
->list
->head
: NULL
;
808 lsmash_entry_t
*stts_entry
= stts
&& stts
->list
? stts
->list
->head
: NULL
;
809 lsmash_entry_t
*ctts_entry
= ctts
&& ctts
->list
? ctts
->list
->head
: NULL
;
810 lsmash_entry_t
*stss_entry
= stss
&& stss
->list
? stss
->list
->head
: NULL
;
811 lsmash_entry_t
*stps_entry
= stps
&& stps
->list
? stps
->list
->head
: NULL
;
812 lsmash_entry_t
*sdtp_entry
= sdtp
&& sdtp
->list
? sdtp
->list
->head
: NULL
;
813 lsmash_entry_t
*stsz_entry
= stsz
&& stsz
->list
? stsz
->list
->head
: NULL
;
814 lsmash_entry_t
*stsc_entry
= stsc
&& stsc
->list
? stsc
->list
->head
: NULL
;
815 lsmash_entry_t
*stco_entry
= stco
&& stco
->list
? stco
->list
->head
: NULL
;
816 lsmash_entry_t
*sbgp_roll_entry
= sbgp_roll
&& sbgp_roll
->list
? sbgp_roll
->list
->head
: NULL
;
817 lsmash_entry_t
*sbgp_rap_entry
= sbgp_rap
&& sbgp_rap
->list
? sbgp_rap
->list
->head
: NULL
;
818 lsmash_entry_t
*next_stsc_entry
= stsc_entry
? stsc_entry
->next
: NULL
;
819 isom_stsc_entry_t
*stsc_data
= stsc_entry
? (isom_stsc_entry_t
*)stsc_entry
->data
: NULL
;
820 int err
= LSMASH_ERR_INVALID_DATA
;
821 int movie_fragments_present
= (file
->moov
->mvex
&& file
->moof_list
.head
);
822 if( !movie_fragments_present
&& (!stts_entry
|| !stsc_entry
|| !stco_entry
|| !stco_entry
->data
|| (next_stsc_entry
&& !next_stsc_entry
->data
)) )
824 isom_sample_entry_t
*description
= (isom_sample_entry_t
*)lsmash_get_entry_data( &stsd
->list
, stsc_data
? stsc_data
->sample_description_index
: 1 );
827 isom_dref_entry_t
*dref_entry
= (isom_dref_entry_t
*)lsmash_get_entry_data( &dref
->list
, description
->data_reference_index
);
828 int all_sync
= !stss
;
829 int large_presentation
= stco
->large_presentation
|| lsmash_check_box_type_identical( stco
->type
, ISOM_BOX_TYPE_CO64
);
830 int is_lpcm_audio
= isom_is_lpcm_audio( description
);
831 int is_qt_fixed_comp_audio
= isom_is_qt_fixed_compressed_audio( description
);
832 int iso_sdtp
= file
->max_isom_version
>= 2 || file
->avc_extensions
;
833 int allow_negative_sample_offset
= ctts
&& ((file
->max_isom_version
>= 4 && ctts
->version
== 1) || file
->qt_compatible
);
834 uint32_t sample_number_in_stts_entry
= 1;
835 uint32_t sample_number_in_ctts_entry
= 1;
836 uint32_t sample_number_in_sbgp_roll_entry
= 1;
837 uint32_t sample_number_in_sbgp_rap_entry
= 1;
839 uint32_t chunk_number
= 1;
840 uint64_t offset_from_chunk
= 0;
841 uint64_t data_offset
= stco_entry
&& stco_entry
->data
843 ? ((isom_co64_entry_t
*)stco_entry
->data
)->chunk_offset
844 : ((isom_stco_entry_t
*)stco_entry
->data
)->chunk_offset
846 uint32_t samples_per_packet
;
847 uint32_t constant_sample_size
;
848 if( is_qt_fixed_comp_audio
)
849 isom_get_qt_fixed_comp_audio_sample_quants( timeline
, description
, &samples_per_packet
, &constant_sample_size
);
852 samples_per_packet
= 1;
853 constant_sample_size
= stsz
->sample_size
;
855 uint32_t sample_number
= samples_per_packet
;
856 uint32_t sample_number_in_chunk
= samples_per_packet
;
860 isom_elst_entry_t
*edit
= (isom_elst_entry_t
*)lsmash_memdup( elst_entry
->data
, sizeof(isom_elst_entry_t
) );
862 || lsmash_add_entry( timeline
->edit_list
, edit
) < 0 )
864 err
= LSMASH_ERR_MEMORY_ALLOC
;
867 elst_entry
= elst_entry
->next
;
869 /* Check what the first 2-bits of sample dependency means.
870 * This check is for chimera of ISO Base Media and QTFF. */
871 if( iso_sdtp
&& sdtp_entry
)
875 isom_sdtp_entry_t
*sdtp_data
= (isom_sdtp_entry_t
*)sdtp_entry
->data
;
878 if( sdtp_data
->is_leading
> 1 )
879 break; /* Apparently, it's defined under ISO Base Media. */
880 if( (sdtp_data
->is_leading
== 1) && (sdtp_data
->sample_depends_on
== ISOM_SAMPLE_IS_INDEPENDENT
) )
882 /* Obviously, it's not defined under ISO Base Media. */
886 sdtp_entry
= sdtp_entry
->next
;
888 sdtp_entry
= sdtp
->list
->head
;
890 /**--- Construct media timeline. ---**/
891 isom_portable_chunk_t chunk
;
892 chunk
.data_offset
= data_offset
;
894 chunk
.number
= chunk_number
;
895 chunk
.file
= (!dref_entry
|| !dref_entry
->ref_file
) ? NULL
: dref_entry
->ref_file
;
896 if( (err
= isom_add_portable_chunk_entry( timeline
, &chunk
)) < 0 )
898 uint32_t distance
= NO_RANDOM_ACCESS_POINT
;
899 uint32_t last_duration
= UINT32_MAX
;
900 uint32_t packet_number
= 1;
901 isom_lpcm_bunch_t bunch
= { 0 };
902 while( sample_number
<= stsz
->sample_count
)
904 isom_sample_info_t info
= { 0 };
905 /* Get sample duration and sample offset. */
906 for( uint32_t i
= 0; i
< samples_per_packet
; i
++ )
908 /* sample duration */
911 isom_stts_entry_t
*stts_data
= (isom_stts_entry_t
*)stts_entry
->data
;
914 isom_increment_sample_number_in_entry( &sample_number_in_stts_entry
, &stts_entry
, stts_data
->sample_count
);
915 last_duration
= stts_data
->sample_delta
;
917 info
.duration
+= last_duration
;
918 dts
+= last_duration
;
920 uint32_t sample_offset
;
923 isom_ctts_entry_t
*ctts_data
= (isom_ctts_entry_t
*)ctts_entry
->data
;
926 isom_increment_sample_number_in_entry( &sample_number_in_ctts_entry
, &ctts_entry
, ctts_data
->sample_count
);
927 sample_offset
= ctts_data
->sample_offset
;
928 if( allow_negative_sample_offset
)
930 uint64_t cts
= dts
+ (int32_t)sample_offset
;
931 if( (cts
+ timeline
->ctd_shift
) < dts
)
932 timeline
->ctd_shift
= dts
- cts
;
938 info
.offset
= sample_offset
;
940 timeline
->media_duration
+= info
.duration
;
941 if( !is_qt_fixed_comp_audio
)
943 /* Check whether sync sample or not. */
946 isom_stss_entry_t
*stss_data
= (isom_stss_entry_t
*)stss_entry
->data
;
949 if( sample_number
== stss_data
->sample_number
)
951 info
.prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
952 stss_entry
= stss_entry
->next
;
957 /* Don't reset distance as 0 since MDCT-based audio frames need pre-roll for correct presentation
958 * though all of them could be marked as a sync sample. */
959 info
.prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
960 /* Check whether partial sync sample or not. */
963 isom_stps_entry_t
*stps_data
= (isom_stps_entry_t
*)stps_entry
->data
;
966 if( sample_number
== stps_data
->sample_number
)
968 info
.prop
.ra_flags
|= QT_SAMPLE_RANDOM_ACCESS_FLAG_PARTIAL_SYNC
| QT_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
969 stps_entry
= stps_entry
->next
;
973 /* Get sample dependency info. */
976 isom_sdtp_entry_t
*sdtp_data
= (isom_sdtp_entry_t
*)sdtp_entry
->data
;
980 info
.prop
.leading
= sdtp_data
->is_leading
;
982 info
.prop
.allow_earlier
= sdtp_data
->is_leading
;
983 info
.prop
.independent
= sdtp_data
->sample_depends_on
;
984 info
.prop
.disposable
= sdtp_data
->sample_is_depended_on
;
985 info
.prop
.redundant
= sdtp_data
->sample_has_redundancy
;
986 sdtp_entry
= sdtp_entry
->next
;
988 /* Get roll recovery grouping info. */
990 && isom_get_roll_recovery_grouping_info( timeline
,
991 &sbgp_roll_entry
, sgpd_roll
, NULL
,
992 &sample_number_in_sbgp_roll_entry
,
993 &info
, sample_number
) < 0 )
995 info
.prop
.post_roll
.identifier
= sample_number
;
996 /* Get random access point grouping info. */
998 && isom_get_random_access_point_grouping_info( timeline
,
999 &sbgp_rap_entry
, sgpd_rap
, NULL
,
1000 &sample_number_in_sbgp_rap_entry
,
1001 &info
, &distance
) < 0 )
1003 /* Set up distance from the previous random access point. */
1004 if( distance
!= NO_RANDOM_ACCESS_POINT
)
1006 if( info
.prop
.pre_roll
.distance
== 0 )
1007 info
.prop
.pre_roll
.distance
= distance
;
1012 /* All uncompressed and non-variable compressed audio frame is a sync sample. */
1013 info
.prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1014 /* Get size of sample in the stream. */
1015 if( is_qt_fixed_comp_audio
|| !stsz_entry
)
1016 info
.length
= constant_sample_size
;
1019 if( !stsz_entry
->data
)
1021 info
.length
= ((isom_stsz_entry_t
*)stsz_entry
->data
)->entry_size
;
1022 stsz_entry
= stsz_entry
->next
;
1024 timeline
->max_sample_size
= LSMASH_MAX( timeline
->max_sample_size
, info
.length
);
1025 /* Get chunk info. */
1026 info
.pos
= data_offset
;
1027 info
.index
= stsc_data
->sample_description_index
;
1028 info
.chunk
= (isom_portable_chunk_t
*)timeline
->chunk_list
->tail
->data
;
1029 offset_from_chunk
+= info
.length
;
1030 if( sample_number_in_chunk
== stsc_data
->samples_per_chunk
)
1032 /* Set the length of the last chunk. */
1034 info
.chunk
->length
= offset_from_chunk
;
1035 /* Move the next chunk. */
1037 stco_entry
= stco_entry
->next
;
1039 && stco_entry
->data
)
1040 data_offset
= large_presentation
1041 ? ((isom_co64_entry_t
*)stco_entry
->data
)->chunk_offset
1042 : ((isom_stco_entry_t
*)stco_entry
->data
)->chunk_offset
;
1043 chunk
.data_offset
= data_offset
;
1045 chunk
.number
= ++chunk_number
;
1046 offset_from_chunk
= 0;
1047 /* Check if the next entry is broken. */
1048 while( next_stsc_entry
&& chunk_number
> ((isom_stsc_entry_t
*)next_stsc_entry
->data
)->first_chunk
)
1050 /* Just skip broken next entry. */
1051 lsmash_log( timeline
, LSMASH_LOG_WARNING
, "ignore broken entry in Sample To Chunk Box.\n" );
1052 lsmash_log( timeline
, LSMASH_LOG_WARNING
, "timeline might be corrupted.\n" );
1053 next_stsc_entry
= next_stsc_entry
->next
;
1055 && !next_stsc_entry
->data
)
1058 /* Check if the next chunk belongs to the next sequence of chunks. */
1059 if( next_stsc_entry
&& chunk_number
== ((isom_stsc_entry_t
*)next_stsc_entry
->data
)->first_chunk
)
1061 stsc_entry
= next_stsc_entry
;
1062 next_stsc_entry
= next_stsc_entry
->next
;
1064 && !next_stsc_entry
->data
)
1066 stsc_data
= (isom_stsc_entry_t
*)stsc_entry
->data
;
1067 /* Update sample description. */
1068 description
= (isom_sample_entry_t
*)lsmash_get_entry_data( &stsd
->list
, stsc_data
->sample_description_index
);
1069 is_lpcm_audio
= isom_is_lpcm_audio( description
);
1070 is_qt_fixed_comp_audio
= isom_is_qt_fixed_compressed_audio( description
);
1071 if( is_qt_fixed_comp_audio
)
1072 isom_get_qt_fixed_comp_audio_sample_quants( timeline
, description
, &samples_per_packet
, &constant_sample_size
);
1075 samples_per_packet
= 1;
1076 constant_sample_size
= stsz
->sample_size
;
1078 /* Reference media data. */
1079 dref_entry
= (isom_dref_entry_t
*)lsmash_get_entry_data( &dref
->list
, description
->data_reference_index
);
1080 chunk
.file
= (!dref_entry
|| !dref_entry
->ref_file
) ? NULL
: dref_entry
->ref_file
;
1082 sample_number_in_chunk
= samples_per_packet
;
1083 if( (err
= isom_add_portable_chunk_entry( timeline
, &chunk
)) < 0 )
1088 data_offset
+= info
.length
;
1089 sample_number_in_chunk
+= samples_per_packet
;
1091 /* OK. Let's add its info. */
1094 if( sample_number
== samples_per_packet
)
1095 isom_update_bunch( &bunch
, &info
);
1096 else if( isom_compare_lpcm_sample_info( &bunch
, &info
) )
1098 if( (err
= isom_add_lpcm_bunch_entry( timeline
, &bunch
)) < 0 )
1100 isom_update_bunch( &bunch
, &info
);
1103 ++ bunch
.sample_count
;
1105 else if( (err
= isom_add_sample_info_entry( timeline
, &info
)) < 0 )
1107 if( timeline
->info_list
->entry_count
&& timeline
->bunch_list
->entry_count
)
1109 lsmash_log( timeline
, LSMASH_LOG_ERROR
, "LPCM + non-LPCM track is not supported.\n" );
1110 err
= LSMASH_ERR_PATCH_WELCOME
;
1113 sample_number
+= samples_per_packet
;
1116 isom_portable_chunk_t
*last_chunk
= lsmash_get_entry_data( timeline
->chunk_list
, timeline
->chunk_list
->entry_count
);
1119 if( offset_from_chunk
)
1120 last_chunk
->length
= offset_from_chunk
;
1123 /* Remove the last invalid chunk. */
1124 lsmash_remove_entry( timeline
->chunk_list
, timeline
->chunk_list
->entry_count
, NULL
);
1128 uint32_t sample_count
= packet_number
- 1;
1129 if( movie_fragments_present
)
1131 isom_tfra_t
*tfra
= isom_get_tfra( file
->mfra
, track_ID
);
1132 lsmash_entry_t
*tfra_entry
= tfra
&& tfra
->list
? tfra
->list
->head
: NULL
;
1133 isom_tfra_location_time_entry_t
*rap
= tfra_entry
? (isom_tfra_location_time_entry_t
*)tfra_entry
->data
: NULL
;
1134 chunk
.data_offset
= 0;
1136 /* Movie fragments */
1137 for( lsmash_entry_t
*moof_entry
= file
->moof_list
.head
; moof_entry
; moof_entry
= moof_entry
->next
)
1139 isom_moof_t
*moof
= (isom_moof_t
*)moof_entry
->data
;
1142 uint64_t last_sample_end_pos
= 0;
1143 /* Track fragments */
1144 uint32_t traf_number
= 1;
1145 for( lsmash_entry_t
*traf_entry
= moof
->traf_list
.head
; traf_entry
; traf_entry
= traf_entry
->next
)
1147 isom_traf_t
*traf
= (isom_traf_t
*)traf_entry
->data
;
1150 isom_tfhd_t
*tfhd
= traf
->tfhd
;
1153 isom_trex_t
*trex
= isom_get_trex( file
->moov
->mvex
, tfhd
->track_ID
);
1156 /* Ignore ISOM_TF_FLAGS_DURATION_IS_EMPTY flag even if set. */
1157 if( !traf
->trun_list
.head
)
1162 /* Get base_data_offset. */
1163 uint64_t base_data_offset
;
1164 if( tfhd
->flags
& ISOM_TF_FLAGS_BASE_DATA_OFFSET_PRESENT
)
1165 base_data_offset
= tfhd
->base_data_offset
;
1166 else if( (tfhd
->flags
& ISOM_TF_FLAGS_DEFAULT_BASE_IS_MOOF
) || traf_entry
== moof
->traf_list
.head
)
1167 base_data_offset
= moof
->pos
;
1169 base_data_offset
= last_sample_end_pos
;
1170 /* sample grouping */
1171 isom_sgpd_t
*sgpd_frag_rap
;
1172 isom_sgpd_t
*sgpd_frag_roll
;
1173 sgpd_frag_rap
= isom_get_fragment_sample_group_description( traf
, ISOM_GROUP_TYPE_RAP
);
1174 sbgp_rap
= isom_get_fragment_sample_to_group ( traf
, ISOM_GROUP_TYPE_RAP
);
1175 sbgp_rap_entry
= sbgp_rap
&& sbgp_rap
->list
? sbgp_rap
->list
->head
: NULL
;
1176 sgpd_frag_roll
= isom_get_roll_recovery_sample_group_description( &traf
->sgpd_list
);
1177 sbgp_roll
= isom_get_roll_recovery_sample_to_group ( &traf
->sbgp_list
);
1178 sbgp_roll_entry
= sbgp_roll
&& sbgp_roll
->list
? sbgp_roll
->list
->head
: NULL
;
1179 int need_data_offset_only
= (tfhd
->track_ID
!= track_ID
);
1181 uint32_t trun_number
= 1;
1182 for( lsmash_entry_t
*trun_entry
= traf
->trun_list
.head
; trun_entry
; trun_entry
= trun_entry
->next
)
1184 isom_trun_t
*trun
= (isom_trun_t
*)trun_entry
->data
;
1187 if( trun
->sample_count
== 0 )
1192 /* Get data_offset. */
1193 if( trun
->flags
& ISOM_TR_FLAGS_DATA_OFFSET_PRESENT
)
1194 data_offset
= trun
->data_offset
+ base_data_offset
;
1195 else if( trun_entry
== traf
->trun_list
.head
)
1196 data_offset
= base_data_offset
;
1198 data_offset
= last_sample_end_pos
;
1200 uint32_t sample_description_index
= 0;
1201 isom_sdtp_entry_t
*sdtp_data
= NULL
;
1202 if( !need_data_offset_only
)
1204 /* Get sample_description_index of this track fragment. */
1205 if( tfhd
->flags
& ISOM_TF_FLAGS_SAMPLE_DESCRIPTION_INDEX_PRESENT
)
1206 sample_description_index
= tfhd
->sample_description_index
;
1208 sample_description_index
= trex
->default_sample_description_index
;
1209 description
= (isom_sample_entry_t
*)lsmash_get_entry_data( &stsd
->list
, sample_description_index
);
1210 is_lpcm_audio
= isom_is_lpcm_audio( description
);
1211 /* Reference media data. */
1212 dref_entry
= (isom_dref_entry_t
*)lsmash_get_entry_data( &dref
->list
, description
->data_reference_index
);
1213 lsmash_file_t
*ref_file
= (!dref_entry
|| !dref_entry
->ref_file
) ? NULL
: dref_entry
->ref_file
;
1214 /* Each track run can be considered as a chunk.
1215 * Here, we consider physically consecutive track runs as one chunk. */
1216 if( chunk
.data_offset
+ chunk
.length
!= data_offset
|| chunk
.file
!= ref_file
)
1218 chunk
.data_offset
= data_offset
;
1220 chunk
.number
= ++chunk_number
;
1221 chunk
.file
= ref_file
;
1222 if( (err
= isom_add_portable_chunk_entry( timeline
, &chunk
)) < 0 )
1225 /* Get dependency info for this track fragment. */
1226 sdtp_entry
= traf
->sdtp
&& traf
->sdtp
->list
? traf
->sdtp
->list
->head
: NULL
;
1227 sdtp_data
= sdtp_entry
&& sdtp_entry
->data
? (isom_sdtp_entry_t
*)sdtp_entry
->data
: NULL
;
1229 /* Get info of each sample. */
1230 lsmash_entry_t
*row_entry
= trun
->optional
&& trun
->optional
->head
? trun
->optional
->head
: NULL
;
1232 while( sample_number
<= trun
->sample_count
)
1234 isom_sample_info_t info
= { 0 };
1235 isom_trun_optional_row_t
*row
= row_entry
&& row_entry
->data
? (isom_trun_optional_row_t
*)row_entry
->data
: NULL
;
1236 /* Get sample_size */
1237 if( row
&& (trun
->flags
& ISOM_TR_FLAGS_SAMPLE_SIZE_PRESENT
) )
1238 info
.length
= row
->sample_size
;
1239 else if( tfhd
->flags
& ISOM_TF_FLAGS_DEFAULT_SAMPLE_SIZE_PRESENT
)
1240 info
.length
= tfhd
->default_sample_size
;
1242 info
.length
= trex
->default_sample_size
;
1243 if( !need_data_offset_only
)
1245 info
.pos
= data_offset
;
1246 info
.index
= sample_description_index
;
1247 info
.chunk
= (isom_portable_chunk_t
*)timeline
->chunk_list
->tail
->data
;
1248 info
.chunk
->length
+= info
.length
;
1249 /* Get sample_duration. */
1250 if( row
&& (trun
->flags
& ISOM_TR_FLAGS_SAMPLE_DURATION_PRESENT
) )
1251 info
.duration
= row
->sample_duration
;
1252 else if( tfhd
->flags
& ISOM_TF_FLAGS_DEFAULT_SAMPLE_DURATION_PRESENT
)
1253 info
.duration
= tfhd
->default_sample_duration
;
1255 info
.duration
= trex
->default_sample_duration
;
1256 /* Get composition time offset. */
1257 if( row
&& (trun
->flags
& ISOM_TR_FLAGS_SAMPLE_COMPOSITION_TIME_OFFSET_PRESENT
) )
1259 info
.offset
= row
->sample_composition_time_offset
;
1260 /* Check composition to decode timeline shift. */
1261 if( file
->max_isom_version
>= 6 && trun
->version
!= 0 )
1263 uint64_t cts
= dts
+ (int32_t)info
.offset
;
1264 if( (cts
+ timeline
->ctd_shift
) < dts
)
1265 timeline
->ctd_shift
= dts
- cts
;
1270 dts
+= info
.duration
;
1271 /* Update media duration and maximun sample size. */
1272 timeline
->media_duration
+= info
.duration
;
1273 timeline
->max_sample_size
= LSMASH_MAX( timeline
->max_sample_size
, info
.length
);
1274 if( !is_lpcm_audio
)
1276 /* Get sample_flags. */
1277 isom_sample_flags_t sample_flags
;
1278 if( sample_number
== 1 && (trun
->flags
& ISOM_TR_FLAGS_FIRST_SAMPLE_FLAGS_PRESENT
) )
1279 sample_flags
= trun
->first_sample_flags
;
1280 else if( row
&& (trun
->flags
& ISOM_TR_FLAGS_SAMPLE_FLAGS_PRESENT
) )
1281 sample_flags
= row
->sample_flags
;
1282 else if( tfhd
->flags
& ISOM_TF_FLAGS_DEFAULT_SAMPLE_FLAGS_PRESENT
)
1283 sample_flags
= tfhd
->default_sample_flags
;
1285 sample_flags
= trex
->default_sample_flags
;
1288 /* Independent and Disposable Samples Box overrides the information from sample_flags.
1289 * There is no description in the specification about this, but the intention should be such a thing.
1290 * The ground is that sample_flags is placed in media layer
1291 * while Independent and Disposable Samples Box is placed in track or presentation layer. */
1292 info
.prop
.leading
= sdtp_data
->is_leading
;
1293 info
.prop
.independent
= sdtp_data
->sample_depends_on
;
1294 info
.prop
.disposable
= sdtp_data
->sample_is_depended_on
;
1295 info
.prop
.redundant
= sdtp_data
->sample_has_redundancy
;
1297 sdtp_entry
= sdtp_entry
->next
;
1298 sdtp_data
= sdtp_entry
? (isom_sdtp_entry_t
*)sdtp_entry
->data
: NULL
;
1302 info
.prop
.leading
= sample_flags
.is_leading
;
1303 info
.prop
.independent
= sample_flags
.sample_depends_on
;
1304 info
.prop
.disposable
= sample_flags
.sample_is_depended_on
;
1305 info
.prop
.redundant
= sample_flags
.sample_has_redundancy
;
1307 /* Check this sample is a sync sample or not.
1308 * Note: all sync sample shall be independent. */
1309 if( !sample_flags
.sample_is_non_sync_sample
1310 && info
.prop
.independent
!= ISOM_SAMPLE_IS_NOT_INDEPENDENT
)
1312 info
.prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1315 /* Get roll recovery grouping info. */
1316 uint32_t roll_id
= sample_count
+ sample_number
;
1318 && isom_get_roll_recovery_grouping_info( timeline
,
1319 &sbgp_roll_entry
, sgpd_roll
, sgpd_frag_roll
,
1320 &sample_number_in_sbgp_roll_entry
,
1321 &info
, roll_id
) < 0 )
1323 info
.prop
.post_roll
.identifier
= roll_id
;
1324 /* Get random access point grouping info. */
1326 && isom_get_random_access_point_grouping_info( timeline
,
1327 &sbgp_rap_entry
, sgpd_rap
, sgpd_frag_rap
,
1328 &sample_number_in_sbgp_rap_entry
,
1329 &info
, &distance
) < 0 )
1331 /* Get the location of the sync sample from 'tfra' if it is not set up yet.
1332 * Note: there is no guarantee that its entries are placed in a specific order. */
1335 if( tfra
->number_of_entry
== 0
1336 && info
.prop
.ra_flags
== ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE
)
1337 info
.prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1339 && rap
->moof_offset
== moof
->pos
1340 && rap
->traf_number
== traf_number
1341 && rap
->trun_number
== trun_number
1342 && rap
->sample_number
== sample_number
)
1344 if( info
.prop
.ra_flags
== ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE
)
1345 info
.prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1347 tfra_entry
= tfra_entry
->next
;
1348 rap
= tfra_entry
? (isom_tfra_location_time_entry_t
*)tfra_entry
->data
: NULL
;
1351 /* Set up distance from the previous random access point. */
1352 if( distance
!= NO_RANDOM_ACCESS_POINT
)
1354 if( info
.prop
.pre_roll
.distance
== 0 )
1355 info
.prop
.pre_roll
.distance
= distance
;
1358 /* OK. Let's add its info. */
1359 if( (err
= isom_add_sample_info_entry( timeline
, &info
)) < 0 )
1364 /* All LPCMFrame is a sync sample. */
1365 info
.prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1366 /* OK. Let's add its info. */
1367 if( sample_count
== 0 && sample_number
== 1 )
1368 isom_update_bunch( &bunch
, &info
);
1369 else if( isom_compare_lpcm_sample_info( &bunch
, &info
) )
1371 if( (err
= isom_add_lpcm_bunch_entry( timeline
, &bunch
)) < 0 )
1373 isom_update_bunch( &bunch
, &info
);
1376 ++ bunch
.sample_count
;
1378 if( timeline
-> info_list
->entry_count
1379 && timeline
->bunch_list
->entry_count
)
1381 lsmash_log( timeline
, LSMASH_LOG_ERROR
, "LPCM + non-LPCM track is not supported.\n" );
1382 err
= LSMASH_ERR_PATCH_WELCOME
;
1386 data_offset
+= info
.length
;
1387 last_sample_end_pos
= data_offset
;
1389 row_entry
= row_entry
->next
;
1392 if( !need_data_offset_only
)
1393 sample_count
+= sample_number
- 1;
1397 } /* Track fragments */
1398 } /* Movie fragments */
1400 else if( timeline
->chunk_list
->entry_count
== 0 )
1401 goto fail
; /* No samples in this track. */
1402 if( bunch
.sample_count
&& (err
= isom_add_lpcm_bunch_entry( timeline
, &bunch
)) < 0 )
1404 if( (err
= lsmash_add_entry( file
->timeline
, timeline
)) < 0 )
1406 /* Finish timeline construction. */
1407 timeline
->sample_count
= sample_count
;
1408 if( timeline
->info_list
->entry_count
)
1409 isom_timeline_set_sample_getter_funcs( timeline
);
1411 isom_timeline_set_lpcm_sample_getter_funcs( timeline
);
1414 isom_timeline_destroy( timeline
);
1418 int lsmash_construct_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
1423 return LSMASH_ERR_FUNCTION_PARAM
;
1424 uint32_t track_number
;
1425 if( root
->file
->initializer
)
1427 if( !root
->file
->initializer
->moov
)
1428 return LSMASH_ERR_INVALID_DATA
;
1430 int track_found
= 0;
1431 for( lsmash_entry_t
*entry
= root
->file
->initializer
->moov
->trak_list
.head
; entry
; entry
= entry
->next
)
1433 isom_trak_t
*trak
= (isom_trak_t
*)entry
->data
;
1437 if( trak
->tkhd
->track_ID
== track_ID
)
1445 return LSMASH_ERR_NAMELESS
;
1448 track_number
= track_ID
;
1449 return lsmash_importer_construct_timeline( root
->file
->importer
, track_number
);
1452 int lsmash_get_dts_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t sample_number
, uint64_t *dts
)
1454 if( !sample_number
|| !dts
)
1455 return LSMASH_ERR_FUNCTION_PARAM
;
1456 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1457 if( !timeline
|| sample_number
> timeline
->sample_count
)
1458 return LSMASH_ERR_NAMELESS
;
1459 return timeline
->get_dts( timeline
, sample_number
, dts
);
1462 int lsmash_get_cts_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t sample_number
, uint64_t *cts
)
1464 if( !sample_number
|| !cts
)
1465 return LSMASH_ERR_FUNCTION_PARAM
;
1466 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1467 if( !timeline
|| sample_number
> timeline
->sample_count
)
1468 return LSMASH_ERR_NAMELESS
;
1469 return timeline
->get_cts( timeline
, sample_number
, cts
);
1472 lsmash_sample_t
*lsmash_get_sample_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t sample_number
)
1474 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1475 return timeline
? timeline
->get_sample( timeline
, sample_number
) : NULL
;
1478 int lsmash_get_sample_info_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t sample_number
, lsmash_sample_t
*sample
)
1481 return LSMASH_ERR_FUNCTION_PARAM
;
1482 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1483 return timeline
? timeline
->get_sample_info( timeline
, sample_number
, sample
) : -1;
1486 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
)
1489 return LSMASH_ERR_FUNCTION_PARAM
;
1490 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1491 return timeline
? timeline
->get_sample_property( timeline
, sample_number
, prop
) : -1;
1494 int lsmash_get_composition_to_decode_shift_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t *ctd_shift
)
1497 return LSMASH_ERR_FUNCTION_PARAM
;
1498 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1500 return LSMASH_ERR_NAMELESS
;
1501 *ctd_shift
= timeline
->ctd_shift
;
1505 static int isom_get_closest_past_random_accessible_point_from_media_timeline( isom_timeline_t
*timeline
, uint32_t sample_number
, uint32_t *rap_number
)
1507 lsmash_entry_t
*entry
= lsmash_get_entry( timeline
->info_list
, sample_number
-- );
1510 return LSMASH_ERR_NAMELESS
;
1511 isom_sample_info_t
*info
= (isom_sample_info_t
*)entry
->data
;
1512 while( info
->prop
.ra_flags
== ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE
)
1514 entry
= entry
->prev
;
1517 return LSMASH_ERR_NAMELESS
;
1518 info
= (isom_sample_info_t
*)entry
->data
;
1521 *rap_number
= sample_number
+ 1;
1525 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
)
1527 lsmash_entry_t
*entry
= lsmash_get_entry( timeline
->info_list
, sample_number
++ );
1530 return LSMASH_ERR_NAMELESS
;
1531 isom_sample_info_t
*info
= (isom_sample_info_t
*)entry
->data
;
1532 while( info
->prop
.ra_flags
== ISOM_SAMPLE_RANDOM_ACCESS_FLAG_NONE
)
1534 entry
= entry
->next
;
1537 return LSMASH_ERR_NAMELESS
;
1538 info
= (isom_sample_info_t
*)entry
->data
;
1541 *rap_number
= sample_number
- 1;
1545 static int isom_get_closest_random_accessible_point_from_media_timeline_internal( isom_timeline_t
*timeline
, uint32_t sample_number
, uint32_t *rap_number
)
1548 return LSMASH_ERR_NAMELESS
;
1550 if( (ret
= isom_get_closest_past_random_accessible_point_from_media_timeline( timeline
, sample_number
, rap_number
)) < 0
1551 && (ret
= isom_get_closest_future_random_accessible_point_from_media_timeline( timeline
, sample_number
+ 1, rap_number
)) < 0 )
1556 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
)
1558 if( sample_number
== 0 || !rap_number
)
1559 return LSMASH_ERR_FUNCTION_PARAM
;
1560 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1561 if( timeline
->info_list
->entry_count
== 0 )
1563 *rap_number
= sample_number
; /* All LPCM is sync sample. */
1566 return isom_get_closest_random_accessible_point_from_media_timeline_internal( timeline
, sample_number
, rap_number
);
1569 int lsmash_get_closest_random_accessible_point_detail_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t sample_number
,
1570 uint32_t *rap_number
, lsmash_random_access_flag
*ra_flags
, uint32_t *leading
, uint32_t *distance
)
1572 if( sample_number
== 0 )
1573 return LSMASH_ERR_FUNCTION_PARAM
;
1574 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1575 if( timeline
->info_list
->entry_count
== 0 )
1577 /* All LPCM is sync sample. */
1578 *rap_number
= sample_number
;
1580 *ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1587 int ret
= isom_get_closest_random_accessible_point_from_media_timeline_internal( timeline
, sample_number
, rap_number
);
1590 isom_sample_info_t
*info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, *rap_number
);
1592 return LSMASH_ERR_NAMELESS
;
1594 *ra_flags
= info
->prop
.ra_flags
;
1599 if( sample_number
< *rap_number
)
1600 /* Impossible to desire to decode the sample of given number correctly. */
1602 else if( !(info
->prop
.ra_flags
& ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR
) )
1606 /* Count leading samples. */
1607 uint32_t current_sample_number
= *rap_number
+ 1;
1609 if( (ret
= isom_get_dts_from_info_list( timeline
, *rap_number
, &dts
)) < 0 )
1611 uint64_t rap_cts
= timeline
->ctd_shift
? (dts
+ (int32_t)info
->offset
+ timeline
->ctd_shift
) : (dts
+ info
->offset
);
1614 dts
+= info
->duration
;
1615 if( rap_cts
<= dts
)
1616 break; /* leading samples of this random accessible point must not be present more. */
1617 info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, current_sample_number
++ );
1620 uint64_t cts
= timeline
->ctd_shift
? (dts
+ (int32_t)info
->offset
+ timeline
->ctd_shift
) : (dts
+ info
->offset
);
1625 if( !distance
|| sample_number
== *rap_number
)
1627 /* Measure distance from the first closest non-recovery random accessible point to the second. */
1628 uint32_t prev_rap_number
= *rap_number
;
1631 if( isom_get_closest_past_random_accessible_point_from_media_timeline( timeline
, prev_rap_number
- 1, &prev_rap_number
) < 0 )
1632 /* The previous random accessible point is not present. */
1634 info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, prev_rap_number
);
1636 return LSMASH_ERR_NAMELESS
;
1637 if( !(info
->prop
.ra_flags
& ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR
) )
1639 /* Decode shall already complete at the first closest non-recovery random accessible point if starting to decode from the second. */
1640 *distance
= *rap_number
- prev_rap_number
;
1647 /* Calculate roll-distance. */
1648 if( info
->prop
.pre_roll
.distance
)
1650 /* Pre-roll recovery */
1651 uint32_t prev_rap_number
= *rap_number
;
1654 if( isom_get_closest_past_random_accessible_point_from_media_timeline( timeline
, prev_rap_number
- 1, &prev_rap_number
) < 0
1655 && *rap_number
< info
->prop
.pre_roll
.distance
)
1657 /* The previous random accessible point is not present.
1658 * And sample of given number might be not able to decoded correctly. */
1662 if( prev_rap_number
+ info
->prop
.pre_roll
.distance
<= *rap_number
)
1665 * |<---- pre-roll distance ---->|
1666 * |<--------- distance -------->|
1667 * media +++++++++++++++++++++++++ *** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1669 * random accessible point starting point random accessible point given sample
1672 *distance
= info
->prop
.pre_roll
.distance
;
1675 else if( !(info
->prop
.ra_flags
& ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR
) )
1678 * |<------------ pre-roll distance ------------------>|
1679 * |<------ distance ------->|
1680 * media ++++++++++++++++ *** ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1682 * random accessible point random accessible point given sample
1683 * (starting point) (complete)
1685 *distance
= *rap_number
- prev_rap_number
;
1690 /* Post-roll recovery */
1691 if( sample_number
>= info
->prop
.post_roll
.complete
)
1693 * |<----- post-roll distance ----->|
1695 * media +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1697 * random accessible point complete given sample
1701 uint32_t prev_rap_number
= *rap_number
;
1704 if( isom_get_closest_past_random_accessible_point_from_media_timeline( timeline
, prev_rap_number
- 1, &prev_rap_number
) < 0 )
1705 /* The previous random accessible point is not present. */
1707 info
= (isom_sample_info_t
*)lsmash_get_entry_data( timeline
->info_list
, prev_rap_number
);
1709 return LSMASH_ERR_NAMELESS
;
1710 if( !(info
->prop
.ra_flags
& ISOM_SAMPLE_RANDOM_ACCESS_FLAG_GDR
) || sample_number
>= info
->prop
.post_roll
.complete
)
1712 *distance
= *rap_number
- prev_rap_number
;
1718 int lsmash_check_sample_existence_in_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t sample_number
)
1720 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1721 return timeline
? timeline
->check_sample_existence( timeline
, sample_number
) : 0;
1724 int lsmash_get_last_sample_delta_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t *last_sample_delta
)
1726 if( !last_sample_delta
)
1727 return LSMASH_ERR_FUNCTION_PARAM
;
1728 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1729 return timeline
? timeline
->get_sample_duration( timeline
, timeline
->sample_count
, last_sample_delta
) : -1;
1732 int lsmash_get_sample_delta_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
, uint32_t sample_number
, uint32_t *sample_delta
)
1735 return LSMASH_ERR_FUNCTION_PARAM
;
1736 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1737 return timeline
? timeline
->get_sample_duration( timeline
, sample_number
, sample_delta
) : -1;
1740 uint32_t lsmash_get_sample_count_in_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
1742 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1745 return timeline
->sample_count
;
1748 uint32_t lsmash_get_max_sample_size_in_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
1750 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1753 return timeline
->max_sample_size
;
1756 uint64_t lsmash_get_media_duration_from_media_timeline( lsmash_root_t
*root
, uint32_t track_ID
)
1758 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1761 return timeline
->media_duration
;
1764 int lsmash_copy_timeline_map( lsmash_root_t
*dst
, uint32_t dst_track_ID
, lsmash_root_t
*src
, uint32_t src_track_ID
)
1766 if( isom_check_initializer_present( dst
) < 0
1767 || isom_check_initializer_present( src
) < 0 )
1768 return LSMASH_ERR_FUNCTION_PARAM
;
1769 lsmash_file_t
*dst_file
= dst
->file
->initializer
;
1770 isom_trak_t
*dst_trak
= isom_get_trak( dst_file
, dst_track_ID
);
1772 || !dst_file
->moov
->mvhd
1773 || dst_file
->moov
->mvhd
->timescale
== 0
1776 || !dst_trak
->mdia
->mdhd
1777 || dst_trak
->mdia
->mdhd
->timescale
== 0
1778 || !dst_trak
->mdia
->minf
1779 || !dst_trak
->mdia
->minf
->stbl
)
1780 return LSMASH_ERR_NAMELESS
;
1782 && dst_trak
->edts
->elst
)
1783 lsmash_remove_entries( dst_trak
->edts
->elst
->list
, NULL
);
1784 uint32_t src_movie_timescale
;
1785 uint32_t src_media_timescale
;
1786 uint64_t src_track_duration
;
1787 uint64_t src_media_duration
;
1788 int32_t src_ctd_shift
; /* Add timeline shift difference between src and dst to each media_time.
1789 * Therefore, call this function as later as possible. */
1790 lsmash_entry_t
*src_entry
= NULL
;
1791 lsmash_file_t
*src_file
= src
->file
->initializer
;
1792 isom_trak_t
*src_trak
= isom_get_trak( src_file
, src_track_ID
);
1793 int src_fragmented
= !!(src_file
->flags
& LSMASH_FILE_MODE_FRAGMENTED
);
1796 || !src_trak
->edts
->elst
1797 || !src_trak
->edts
->elst
->list
1800 /* Get from constructed timeline instead of boxes. */
1801 isom_timeline_t
*src_timeline
= isom_get_timeline( src
, src_track_ID
);
1803 && src_timeline
->movie_timescale
1804 && src_timeline
->media_timescale
)
1806 src_entry
= src_timeline
->edit_list
->head
;
1809 src_movie_timescale
= src_timeline
->movie_timescale
;
1810 src_media_timescale
= src_timeline
->media_timescale
;
1811 src_track_duration
= src_timeline
->track_duration
;
1812 src_media_duration
= src_timeline
->media_duration
;
1813 src_ctd_shift
= src_timeline
->ctd_shift
;
1815 else if( !src_fragmented
)
1816 return LSMASH_ERR_NAMELESS
;
1821 || !src_file
->moov
->mvhd
1822 || src_file
->moov
->mvhd
->timescale
== 0
1825 || !src_trak
->mdia
->mdhd
1826 || src_trak
->mdia
->mdhd
->timescale
== 0
1827 || !src_trak
->mdia
->minf
1828 || !src_trak
->mdia
->minf
->stbl
)
1829 return LSMASH_ERR_NAMELESS
;
1830 src_entry
= src_trak
->edts
->elst
->list
->head
;
1833 src_movie_timescale
= src_file
->moov
->mvhd
->timescale
;
1834 src_media_timescale
= src_trak
->mdia
->mdhd
->timescale
;
1835 src_track_duration
= src_trak
->tkhd
->duration
;
1836 src_media_duration
= src_trak
->mdia
->mdhd
->duration
;
1837 src_ctd_shift
= src_trak
->mdia
->minf
->stbl
->cslg
? src_trak
->mdia
->minf
->stbl
->cslg
->compositionToDTSShift
: 0;
1839 /* Generate the edit list if absent in the destination. */
1840 if( (!dst_trak
->edts
&& !isom_add_edts( dst_trak
))
1841 || (!dst_trak
->edts
->elst
&& !isom_add_elst( dst_trak
->edts
)) )
1842 return LSMASH_ERR_NAMELESS
;
1843 uint32_t dst_movie_timescale
= dst_file
->moov
->mvhd
->timescale
;
1844 uint32_t dst_media_timescale
= dst_trak
->mdia
->mdhd
->timescale
;
1845 int32_t dst_ctd_shift
= dst_trak
->mdia
->minf
->stbl
->cslg
? dst_trak
->mdia
->minf
->stbl
->cslg
->compositionToDTSShift
: 0;
1846 int32_t media_time_shift
= src_ctd_shift
- dst_ctd_shift
;
1847 lsmash_entry_list_t
*dst_list
= dst_trak
->edts
->elst
->list
;
1850 isom_elst_entry_t
*src_data
= (isom_elst_entry_t
*)src_entry
->data
;
1852 return LSMASH_ERR_NAMELESS
;
1853 isom_elst_entry_t
*dst_data
= (isom_elst_entry_t
*)lsmash_malloc( sizeof(isom_elst_entry_t
) );
1855 return LSMASH_ERR_MEMORY_ALLOC
;
1856 uint64_t segment_duration
;
1857 if( src_data
->segment_duration
== 0 && !dst_file
->fragment
)
1858 /* The implicit duration edit is not suitable for non-fragmented movie file.
1859 * Set an appropriate duration from the source track. */
1860 segment_duration
= src_fragmented
1861 ? (uint64_t)(src_media_duration
* ((double)src_movie_timescale
/ src_media_timescale
))
1862 : src_track_duration
;
1864 segment_duration
= src_data
->segment_duration
;
1865 dst_data
->segment_duration
= segment_duration
* ((double)dst_movie_timescale
/ src_movie_timescale
) + 0.5;
1866 dst_data
->media_rate
= src_data
->media_rate
;
1867 if( src_data
->media_time
!= ISOM_EDIT_MODE_EMPTY
)
1868 dst_data
->media_time
= (src_data
->media_time
+ media_time_shift
) * ((double)dst_media_timescale
/ src_media_timescale
) + 0.5;
1870 dst_data
->media_time
= ISOM_EDIT_MODE_EMPTY
;
1871 if( lsmash_add_entry( dst_list
, dst_data
) < 0 )
1873 lsmash_free( dst_data
);
1874 return LSMASH_ERR_MEMORY_ALLOC
;
1876 src_entry
= src_entry
->next
;
1881 int lsmash_set_media_timestamps( lsmash_root_t
*root
, uint32_t track_ID
, lsmash_media_ts_list_t
*ts_list
)
1883 if( !root
|| !root
->file
|| !ts_list
)
1885 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1887 return LSMASH_ERR_NAMELESS
;
1888 if( timeline
->info_list
->entry_count
== 0 )
1890 lsmash_log( timeline
, LSMASH_LOG_ERROR
, "Changing timestamps of LPCM track is not supported.\n" );
1891 return LSMASH_ERR_PATCH_WELCOME
;
1893 if( ts_list
->sample_count
!= timeline
->info_list
->entry_count
)
1894 return LSMASH_ERR_INVALID_DATA
; /* Number of samples must be same. */
1895 lsmash_media_ts_t
*ts
= ts_list
->timestamp
;
1897 return LSMASH_ERR_INVALID_DATA
; /* DTS must start from value zero. */
1899 uint32_t sample_count
= ts_list
->sample_count
;
1901 if( timeline
->info_list
->entry_count
> 1 )
1904 lsmash_entry_t
*entry
= timeline
->info_list
->head
;
1905 isom_sample_info_t
*info
;
1906 while( i
< sample_count
)
1908 info
= (isom_sample_info_t
*)entry
->data
;
1909 if( !info
|| (ts
[i
].dts
< ts
[i
- 1].dts
) )
1910 return LSMASH_ERR_INVALID_DATA
;
1911 info
->duration
= ts
[i
].dts
- ts
[i
- 1].dts
;
1912 entry
= entry
->next
;
1919 return LSMASH_ERR_INVALID_DATA
;
1920 /* Copy the previous duration. */
1921 ((isom_sample_info_t
*)entry
->data
)->duration
= info
->duration
;
1924 return LSMASH_ERR_INVALID_DATA
; /* Irregular case: sample_count this timeline has is incorrect. */
1926 else /* still image */
1927 ((isom_sample_info_t
*)timeline
->info_list
->head
->data
)->duration
= UINT32_MAX
;
1929 * ToDo: hint track must not have any sample_offset. */
1931 timeline
->ctd_shift
= 0;
1932 for( lsmash_entry_t
*entry
= timeline
->info_list
->head
; entry
; entry
= entry
->next
)
1934 isom_sample_info_t
*info
= (isom_sample_info_t
*)entry
->data
;
1935 if( (ts
[i
].cts
+ timeline
->ctd_shift
) < ts
[i
].dts
)
1936 timeline
->ctd_shift
= ts
[i
].dts
- ts
[i
].cts
;
1937 info
->offset
= ts
[i
].cts
- ts
[i
].dts
;
1940 if( timeline
->ctd_shift
&& (!root
->file
->qt_compatible
|| root
->file
->max_isom_version
< 4) )
1941 return LSMASH_ERR_INVALID_DATA
; /* Don't allow composition to decode timeline shift. */
1945 int lsmash_get_media_timestamps( lsmash_root_t
*root
, uint32_t track_ID
, lsmash_media_ts_list_t
*ts_list
)
1948 return LSMASH_ERR_FUNCTION_PARAM
;
1949 isom_timeline_t
*timeline
= isom_get_timeline( root
, track_ID
);
1951 return LSMASH_ERR_NAMELESS
;
1952 uint32_t sample_count
= timeline
->info_list
->entry_count
;
1955 ts_list
->sample_count
= 0;
1956 ts_list
->timestamp
= NULL
;
1959 lsmash_media_ts_t
*ts
= lsmash_malloc( sample_count
* sizeof(lsmash_media_ts_t
) );
1961 return LSMASH_ERR_MEMORY_ALLOC
;
1964 if( timeline
->info_list
->entry_count
)
1965 for( lsmash_entry_t
*entry
= timeline
->info_list
->head
; entry
; entry
= entry
->next
)
1967 isom_sample_info_t
*info
= (isom_sample_info_t
*)entry
->data
;
1971 return LSMASH_ERR_NAMELESS
;
1974 ts
[i
].cts
= timeline
->ctd_shift
? (dts
+ (int32_t)info
->offset
) : (dts
+ info
->offset
);
1975 dts
+= info
->duration
;
1979 for( lsmash_entry_t
*entry
= timeline
->bunch_list
->head
; entry
; entry
= entry
->next
)
1981 isom_lpcm_bunch_t
*bunch
= (isom_lpcm_bunch_t
*)entry
->data
;
1985 return LSMASH_ERR_NAMELESS
;
1987 for( uint32_t j
= 0; j
< bunch
->sample_count
; j
++ )
1990 ts
[i
].cts
= timeline
->ctd_shift
? (dts
+ (int32_t)bunch
->offset
) : (dts
+ bunch
->offset
);
1991 dts
+= bunch
->duration
;
1995 ts_list
->sample_count
= sample_count
;
1996 ts_list
->timestamp
= ts
;
2000 void lsmash_delete_media_timestamps( lsmash_media_ts_list_t
*ts_list
)
2004 lsmash_freep( &ts_list
->timestamp
);
2005 ts_list
->sample_count
= 0;
2008 static int isom_compare_dts( const lsmash_media_ts_t
*a
, const lsmash_media_ts_t
*b
)
2010 int64_t diff
= (int64_t)(a
->dts
- b
->dts
);
2011 return diff
> 0 ? 1 : (diff
== 0 ? 0 : -1);
2014 void lsmash_sort_timestamps_decoding_order( lsmash_media_ts_list_t
*ts_list
)
2018 qsort( ts_list
->timestamp
, ts_list
->sample_count
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))isom_compare_dts
);
2021 static int isom_compare_cts( const lsmash_media_ts_t
*a
, const lsmash_media_ts_t
*b
)
2023 int64_t diff
= (int64_t)(a
->cts
- b
->cts
);
2024 return diff
> 0 ? 1 : (diff
== 0 ? 0 : -1);
2027 void lsmash_sort_timestamps_composition_order( lsmash_media_ts_list_t
*ts_list
)
2031 qsort( ts_list
->timestamp
, ts_list
->sample_count
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))isom_compare_cts
);
2034 int lsmash_get_max_sample_delay( lsmash_media_ts_list_t
*ts_list
, uint32_t *max_sample_delay
)
2036 if( !ts_list
|| !max_sample_delay
)
2037 return LSMASH_ERR_FUNCTION_PARAM
;
2038 lsmash_media_ts_t
*orig_ts
= ts_list
->timestamp
;
2039 lsmash_media_ts_t
*ts
= lsmash_malloc( ts_list
->sample_count
* sizeof(lsmash_media_ts_t
) );
2041 return LSMASH_ERR_MEMORY_ALLOC
;
2042 ts_list
->timestamp
= ts
;
2043 *max_sample_delay
= 0;
2044 for( uint32_t i
= 0; i
< ts_list
->sample_count
; i
++ )
2046 ts
[i
].cts
= orig_ts
[i
].cts
; /* for sorting */
2049 lsmash_sort_timestamps_composition_order( ts_list
);
2050 for( uint32_t i
= 0; i
< ts_list
->sample_count
; i
++ )
2053 uint32_t sample_delay
= ts
[i
].dts
- i
;
2054 *max_sample_delay
= LSMASH_MAX( *max_sample_delay
, sample_delay
);
2057 ts_list
->timestamp
= orig_ts
;
2061 #endif /* LSMASH_DEMUXER_ENABLED */