Esempio n. 1
0
static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
{
    mp4_hnd_t *p_mp4 = handle;

    if( !p_mp4 )
        return 0;

    if( p_mp4->p_root )
    {
        double actual_duration = 0;
        if( p_mp4->i_track )
        {
            /* Flush the rest of samples and add the last sample_delta. */
            uint32_t last_delta = largest_pts - second_largest_pts;
            MP4_LOG_IF_ERR( lsmash_flush_pooled_samples( p_mp4->p_root, p_mp4->i_track, (last_delta ? last_delta : 1) * p_mp4->i_time_inc ),
                            "failed to flush the rest of samples.\n" );

            if( p_mp4->i_movie_timescale != 0 && p_mp4->i_video_timescale != 0 )    /* avoid zero division */
                actual_duration = ((double)((largest_pts + last_delta) * p_mp4->i_time_inc) / p_mp4->i_video_timescale) * p_mp4->i_movie_timescale;
            else
                MP4_LOG_ERROR( "timescale is broken.\n" );

            /*
             * Declare the explicit time-line mapping.
             * A segment_duration is given by movie timescale, while a media_time that is the start time of this segment
             * is given by not the movie timescale but rather the media timescale.
             * The reason is that ISO media have two time-lines, presentation and media time-line,
             * and an edit maps the presentation time-line to the media time-line.
             * According to QuickTime file format specification and the actual playback in QuickTime Player,
             * if the Edit Box doesn't exist in the track, the ratio of the summation of sample durations and track's duration becomes
             * the track's media_rate so that the entire media can be used by the track.
             * So, we add Edit Box here to avoid this implicit media_rate could distort track's presentation timestamps slightly.
             * Note: Any demuxers should follow the Edit List Box if it exists.
             */
            lsmash_edit_t edit;
            edit.duration   = actual_duration;
            edit.start_time = p_mp4->i_first_cts;
            edit.rate       = ISOM_EDIT_MODE_NORMAL;
            if( !p_mp4->b_fragments )
            {
                MP4_LOG_IF_ERR( lsmash_create_explicit_timeline_map( p_mp4->p_root, p_mp4->i_track, edit ),
                                "failed to set timeline map for video.\n" );
            }
            else if( !p_mp4->b_stdout )
                MP4_LOG_IF_ERR( lsmash_modify_explicit_timeline_map( p_mp4->p_root, p_mp4->i_track, 1, edit ),
                                "failed to update timeline map for video.\n" );
        }

        MP4_LOG_IF_ERR( lsmash_finish_movie( p_mp4->p_root, NULL ), "failed to finish movie.\n" );
    }

    remove_mp4_hnd( p_mp4 ); /* including lsmash_destroy_root( p_mp4->p_root ); */

    return 0;
}
Esempio n. 2
0
bool fcMP4Muxer::mux(const Params &params)
{
    lsmash_root_t               *root;
    lsmash_file_parameters_t    mp4_stream;
    lsmash_file_parameters_t    h264_stream;
    lsmash_brand_type           major_brand = ISOM_BRAND_TYPE_MP42;
    lsmash_brand_type           compatible_brands[2] = { ISOM_BRAND_TYPE_MP42, ISOM_BRAND_TYPE_ISOM };
    uint32_t                    fps_num = params.frame_rate;
    uint32_t                    fps_den = 1;

    // 出力 mp4
    root = lsmash_create_root();
    if (lsmash_open_file(params.out_mp4_path, 0, &mp4_stream) != 0) {
        return false;
    }
    mp4_stream.major_brand = major_brand;
    mp4_stream.brands = compatible_brands;
    mp4_stream.brand_count = sizeof(compatible_brands) / sizeof(compatible_brands[0]);
    mp4_stream.minor_version = 0;
    lsmash_set_file(root, &mp4_stream);

    lsmash_movie_parameters_t movie_param;
    lsmash_initialize_movie_parameters(&movie_param);
    lsmash_set_movie_parameters(root, &movie_param);


    int track_number = 1;
    MP4TrackData track_data[2];
    int num_track_data = 0;

    if (params.in_h264_path) {
        importer_t *h264_importer = lsmash_importer_open(params.in_h264_path, "H.264");
        if (h264_importer == nullptr) { return false; }

        int h264_track_id = lsmash_create_track(root, ISOM_MEDIA_HANDLER_TYPE_VIDEO_TRACK);

        lsmash_track_parameters_t track_param;
        lsmash_initialize_track_parameters(&track_param);
        (int&)track_param.mode = ISOM_TRACK_IN_MOVIE | ISOM_TRACK_IN_PREVIEW;

        int sample_entry = 0;
        lsmash_summary_t *summary = lsmash_duplicate_summary(h264_importer, track_number);
        auto video_summary = (lsmash_video_summary_t*)summary;
        track_param.display_width = video_summary->width << 16;
        track_param.display_height = video_summary->height << 16;
        lsmash_set_track_parameters(root, h264_track_id, &track_param);
        sample_entry = lsmash_add_sample_entry(root, h264_track_id, summary);

        auto& td = track_data[num_track_data++];
        td.importer = h264_importer;
        td.summary = summary;
        td.sample_entry = sample_entry;
        td.track_number = track_number;
        td.track_id = h264_track_id;
        td.timescale = video_summary->timescale;
        td.timebase = video_summary->timebase;

        lsmash_media_parameters_t media_param;
        lsmash_initialize_media_parameters(&media_param);
        media_param.timescale = td.timescale;
        lsmash_set_media_parameters(root, h264_track_id, &media_param);
    }

    if (params.in_aac_path) {
        importer_t *aac_importer = lsmash_importer_open(params.in_aac_path, "adts");
        if (aac_importer == nullptr) { return false; }

        int aac_track_id = lsmash_create_track(root, ISOM_MEDIA_HANDLER_TYPE_AUDIO_TRACK);

        lsmash_track_parameters_t track_param;
        lsmash_initialize_track_parameters(&track_param);
        (int&)track_param.mode = ISOM_TRACK_IN_MOVIE | ISOM_TRACK_IN_PREVIEW;

        int sample_entry = 0;
        lsmash_summary_t *summary = lsmash_duplicate_summary(aac_importer, track_number);
        auto audio_summary = (lsmash_audio_summary_t*)summary;
        lsmash_set_track_parameters(root, aac_track_id, &track_param);
        sample_entry = lsmash_add_sample_entry(root, aac_track_id, summary);

        auto& td = track_data[num_track_data++];
        td.importer = aac_importer;
        td.summary = summary;
        td.sample_entry = sample_entry;
        td.track_number = track_number;
        td.track_id = aac_track_id;
        td.timescale = audio_summary->frequency;
        td.timebase = 1;

        lsmash_media_parameters_t media_param;
        lsmash_initialize_media_parameters(&media_param);
        media_param.timescale = td.timescale;
        lsmash_set_media_parameters(root, aac_track_id, &media_param);
    }

    double largest_dts = 0.0;
    uint32_t num_consecutive_sample_skip = 0;
    for (int ti = 0;;) {
        auto &td = track_data[ti];

        if (!td.sample) {
            int ret = lsmash_importer_get_access_unit(td.importer, td.track_number, &td.sample);
            if (ret <= -1) // error
            {
                lsmash_delete_sample(td.sample);
                break;
            }
            else if (ret == 1) /* a change of stream's properties */
            {
                lsmash_cleanup_summary(td.summary);
                td.summary = lsmash_duplicate_summary(td.importer, td.track_number);
                td.sample_entry = lsmash_add_sample_entry(root, td.track_id, td.summary);
                if (!td.sample_entry) { break; }
            }
            else if (ret == 2) /* EOF */
            {
                lsmash_delete_sample(td.sample);
                break;
            }

            if (td.sample)
            {
                td.sample->index = td.sample_entry;
                td.sample->dts *= td.timebase;
                td.sample->cts *= td.timebase;
                td.dts = (double)td.sample->dts / td.timescale;
            }
        }

        if (td.sample) {
            if (td.dts <= largest_dts || num_consecutive_sample_skip == num_track_data)
            {
                uint64_t sample_size = td.sample->length;
                uint64_t sample_dts = td.sample->dts;
                uint64_t sample_cts = td.sample->cts;
                if (lsmash_append_sample(root, td.track_id, td.sample)) {
                    return false;
                }
                td.prev_dts = sample_dts;
                td.sample = nullptr;
                largest_dts = std::max<double>(largest_dts, td.dts);
                num_consecutive_sample_skip = 0;
            }
            else {
                // skip
                ++num_consecutive_sample_skip;
            }
        }

        ti = (ti + 1) % num_track_data;
    }

    for (int i = 0; i < num_track_data; ++i) {
        lsmash_flush_pooled_samples(root, track_data[i].track_id, fps_den);
        lsmash_cleanup_summary(track_data[i].summary);
        lsmash_importer_close(track_data[i].importer);
    }


    lsmash_finish_movie(root, nullptr);
    lsmash_close_file(&mp4_stream);
    lsmash_destroy_root(root);

    return true;
}