/* create AudioSpecificConfig as memory block from summary, and set it into that summary itself */ int lsmash_setup_AudioSpecificConfig( lsmash_audio_summary_t *summary ) { if( !summary || !summary->opaque ) return LSMASH_ERR_FUNCTION_PARAM; /* Remove an old one. */ for( lsmash_entry_t *entry = summary->opaque->list.head; entry; entry = entry->next ) { lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)entry->data; if( !cs || cs->type != LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG ) continue; lsmash_remove_entry_direct( &summary->opaque->list, entry, lsmash_destroy_codec_specific_data ); } /* Create and add a new one. */ uint32_t data_length; uint8_t *data = mp4a_export_AudioSpecificConfig( summary->aot, summary->frequency, summary->channels, summary->sbr_mode, NULL, /* FIXME */ 0, /* FIXME */ &data_length ); if( !data ) return LSMASH_ERR_NAMELESS; lsmash_codec_specific_t *cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED ); if( !cs ) { lsmash_free( data ); return LSMASH_ERR_MEMORY_ALLOC; } lsmash_mp4sys_decoder_parameters_t *param = (lsmash_mp4sys_decoder_parameters_t *)cs->data.structured; param->objectTypeIndication = MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3; param->streamType = MP4SYS_STREAM_TYPE_AudioStream; int err = lsmash_set_mp4sys_decoder_specific_info( param, data, data_length ); lsmash_free( data ); if( err < 0 || (err = lsmash_add_entry( &summary->opaque->list, cs )) < 0 ) { lsmash_destroy_codec_specific_data( cs ); return err; } return 0; }
int lsmash_create_reference_chapter_track( lsmash_root_t *root, uint32_t track_ID, char *file_name ) { if( !root || !root->moov || !root->moov->mvhd || !root->moov->trak_list ) goto error_message; if( !root->qt_compatible && !root->itunes_movie ) { lsmash_log( LSMASH_LOG_ERROR, "reference chapter is not available for this file.\n" ); goto error_message; } FILE *chapter = NULL; /* shut up 'uninitialized' warning */ /* Create a Track Reference Box. */ isom_trak_entry_t *trak = isom_get_trak( root, track_ID ); if( !trak ) { lsmash_log( LSMASH_LOG_ERROR, "the specified track ID to apply the chapter doesn't exist.\n" ); goto error_message; } if( isom_add_tref( trak ) ) goto error_message; /* Create a track_ID for a new chapter track. */ uint32_t *id = (uint32_t *)malloc( sizeof(uint32_t) ); if( !id ) goto error_message; uint32_t chapter_track_ID = *id = root->moov->mvhd->next_track_ID; /* Create a Track Reference Type Box. */ isom_tref_type_t *chap = isom_add_track_reference_type( trak->tref, QT_TREF_TYPE_CHAP, 1, id ); if( !chap ) goto error_message; /* no need to free id */ /* Create a reference chapter track. */ if( chapter_track_ID != lsmash_create_track( root, ISOM_MEDIA_HANDLER_TYPE_TEXT_TRACK ) ) goto error_message; /* Set track parameters. */ lsmash_track_parameters_t track_param; lsmash_initialize_track_parameters( &track_param ); track_param.mode = ISOM_TRACK_IN_MOVIE | ISOM_TRACK_IN_PREVIEW; if( lsmash_set_track_parameters( root, chapter_track_ID, &track_param ) ) goto fail; /* Set media parameters. */ uint64_t media_timescale = lsmash_get_media_timescale( root, track_ID ); if( !media_timescale ) goto fail; lsmash_media_parameters_t media_param; lsmash_initialize_media_parameters( &media_param ); media_param.timescale = media_timescale; media_param.ISO_language = root->max_3gpp_version >= 6 || root->itunes_movie ? ISOM_LANGUAGE_CODE_UNDEFINED : 0; media_param.MAC_language = 0; if( lsmash_set_media_parameters( root, chapter_track_ID, &media_param ) ) goto fail; /* Create a sample description. */ lsmash_codec_type_t sample_type = root->max_3gpp_version >= 6 || root->itunes_movie ? ISOM_CODEC_TYPE_TX3G_TEXT : QT_CODEC_TYPE_TEXT_TEXT; lsmash_summary_t summary = { .sample_type = sample_type }; uint32_t sample_entry = lsmash_add_sample_entry( root, chapter_track_ID, &summary ); if( !sample_entry ) goto fail; /* Check each line format. */ fn_get_chapter_data fnc = isom_check_chap_line( file_name ); if( !fnc ) goto fail; /* Open chapter format file. */ chapter = fopen( file_name, "rb" ); if( !chapter ) { lsmash_log( LSMASH_LOG_ERROR, "failed to open the chapter file \"%s\".\n", file_name ); goto fail; } /* Parse the file and write text samples. */ isom_chapter_entry_t data; while( !fnc( chapter, &data ) ) { /* set start_time */ data.start_time = data.start_time * 1e-9 * media_timescale + 0.5; /* write a text sample here */ int is_qt_text = lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_TEXT_TEXT ); uint16_t name_length = strlen( data.chapter_name ); lsmash_sample_t *sample = lsmash_create_sample( 2 + name_length + 12 * is_qt_text ); if( !sample ) { free( data.chapter_name ); goto fail; } sample->data[0] = (name_length >> 8) & 0xff; sample->data[1] = name_length & 0xff; memcpy( sample->data + 2, data.chapter_name, name_length ); if( is_qt_text ) { /* QuickTime Player requires Text Encoding Attribute Box ('encd') if media language is ISO language codes : undefined. * Also this box can avoid garbling if the QuickTime text sample is encoded by Unicode characters. * Note: 3GPP Timed Text supports only UTF-8 or UTF-16, so this box isn't needed. */ static const uint8_t encd[12] = { 0x00, 0x00, 0x00, 0x0C, /* size: 12 */ 0x65, 0x6E, 0x63, 0x64, /* type: 'encd' */ 0x00, 0x00, 0x01, 0x00 /* Unicode Encoding */ }; memcpy( sample->data + 2 + name_length, encd, 12 ); } sample->dts = sample->cts = data.start_time; sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC; sample->index = sample_entry; if( lsmash_append_sample( root, chapter_track_ID, sample ) ) { free( data.chapter_name ); goto fail; } free( data.chapter_name ); data.chapter_name = NULL; } if( lsmash_flush_pooled_samples( root, chapter_track_ID, 0 ) ) goto fail; isom_trak_entry_t *chapter_trak = isom_get_trak( root, chapter_track_ID ); if( !chapter_trak ) goto fail; fclose( chapter ); chapter_trak->is_chapter = 1; chapter_trak->related_track_ID = track_ID; return 0; fail: if( chapter ) fclose( chapter ); /* Remove chapter track reference. */ lsmash_remove_entry_direct( trak->tref->ref_list, trak->tref->ref_list->tail, isom_remove_track_reference_type ); if( trak->tref->ref_list->entry_count == 0 ) isom_remove_tref( trak->tref ); /* Remove the reference chapter track attached at tail of the list. */ lsmash_remove_entry_direct( root->moov->trak_list, root->moov->trak_list->tail, isom_remove_trak ); error_message: lsmash_log( LSMASH_LOG_ERROR, "failed to set reference chapter.\n" ); return -1; }