static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)

{

    AVFormatContext *is = ifile->ctx;

    AVFormatContext *os = ofile->ctx;

    AVChapter **tmp;

    int i;



    tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters));

    if (!tmp)

        return AVERROR(ENOMEM);

    os->chapters = tmp;



    for (i = 0; i < is->nb_chapters; i++) {

        AVChapter *in_ch = is->chapters[i], *out_ch;

        int64_t ts_off   = av_rescale_q(ofile->start_time - ifile->ts_offset,

                                       AV_TIME_BASE_Q, in_ch->time_base);

        int64_t rt       = (ofile->recording_time == INT64_MAX) ? INT64_MAX :

                           av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);





        if (in_ch->end < ts_off)

            continue;

        if (rt != INT64_MAX && in_ch->start > rt + ts_off)

            break;



        out_ch = av_mallocz(sizeof(AVChapter));

        if (!out_ch)

            return AVERROR(ENOMEM);



        out_ch->id        = in_ch->id;

        out_ch->time_base = in_ch->time_base;

        out_ch->start     = FFMAX(0,  in_ch->start - ts_off);

        out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);



        if (copy_metadata)

            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);



        os->chapters[os->nb_chapters++] = out_ch;

    }

    return 0;

}

static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)

{

    AVFormatContext *is = ifile->ctx;

    AVFormatContext *os = ofile->ctx;

    AVChapter **tmp;

    int i;



    tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters));

    if (!tmp)

        return AVERROR(ENOMEM);

    os->chapters = tmp;



    for (i = 0; i < is->nb_chapters; i++) {

        AVChapter *in_ch = is->chapters[i], *out_ch;

        int64_t ts_off   = av_rescale_q(ofile->start_time - ifile->ts_offset,

                                       AV_TIME_BASE_Q, in_ch->time_base);

        int64_t rt       = (ofile->recording_time == INT64_MAX) ? INT64_MAX :

                           av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);





        if (in_ch->end < ts_off)

            continue;

        if (rt != INT64_MAX && in_ch->start > rt + ts_off)

            break;



        out_ch = av_mallocz(sizeof(AVChapter));

        if (!out_ch)

            return AVERROR(ENOMEM);



        out_ch->id        = in_ch->id;

        out_ch->time_base = in_ch->time_base;

        out_ch->start     = FFMAX(0,  in_ch->start - ts_off);

        out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);



        if (copy_metadata)

            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);



        os->chapters[os->nb_chapters++] = out_ch;

    }

    return 0;

}

static int FUNC_0(InputFile *VAR_0, OutputFile *VAR_1, int VAR_2)

{

    AVFormatContext *is = VAR_0->ctx;

    AVFormatContext *os = VAR_1->ctx;

    AVChapter **tmp;

    int VAR_3;



    tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters));

    if (!tmp)

        return AVERROR(ENOMEM);

    os->chapters = tmp;



    for (VAR_3 = 0; VAR_3 < is->nb_chapters; VAR_3++) {

        AVChapter *in_ch = is->chapters[VAR_3], *out_ch;

        int64_t ts_off   = av_rescale_q(VAR_1->start_time - VAR_0->ts_offset,

                                       AV_TIME_BASE_Q, in_ch->time_base);

        int64_t rt       = (VAR_1->recording_time == INT64_MAX) ? INT64_MAX :

                           av_rescale_q(VAR_1->recording_time, AV_TIME_BASE_Q, in_ch->time_base);





        if (in_ch->end < ts_off)

            continue;

        if (rt != INT64_MAX && in_ch->start > rt + ts_off)

            break;



        out_ch = av_mallocz(sizeof(AVChapter));

        if (!out_ch)

            return AVERROR(ENOMEM);



        out_ch->id        = in_ch->id;

        out_ch->time_base = in_ch->time_base;

        out_ch->start     = FFMAX(0,  in_ch->start - ts_off);

        out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);



        if (VAR_2)

            av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);



        os->chapters[os->nb_chapters++] = out_ch;

    }

    return 0;

}

static int FUNC_0(InputFile *VAR_0, OutputFile *VAR_1, int VAR_2)
{ AVFormatContext *is = VAR_0->ctx; AVFormatContext *os = VAR_1->ctx; AVChapter **tmp; int VAR_3; tmp = av_realloc(os->chapters, sizeof(*os->chapters) * (is->nb_chapters + os->nb_chapters)); if (!tmp)
return AVERROR(ENOMEM); os->chapters = tmp; for (VAR_3 = 0; VAR_3 < is->nb_chapters; VAR_3++) { AVChapter *in_ch = is->chapters[VAR_3], *out_ch; int64_t ts_off   = av_rescale_q(VAR_1->start_time - VAR_0->ts_offset,
AV_TIME_BASE_Q, in_ch->time_base); int64_t rt       = (VAR_1->recording_time == INT64_MAX) ? INT64_MAX :
av_rescale_q(VAR_1->recording_time, AV_TIME_BASE_Q, in_ch->time_base); if (in_ch->end < ts_off)
continue; if (rt != INT64_MAX && in_ch->start > rt + ts_off)
break; out_ch = av_mallocz(sizeof(AVChapter)); if (!out_ch)
return AVERROR(ENOMEM); out_ch->id        = in_ch->id; out_ch->time_base = in_ch->time_base; out_ch->start     = FFMAX(0,  in_ch->start - ts_off); out_ch->end       = FFMIN(rt, in_ch->end   - ts_off); if (VAR_2)
av_dict_copy(&out_ch->metadata, in_ch->metadata, 0); os->chapters[os->nb_chapters++] = out_ch; } return 0; }