The concat demuxer starts and ends a cut film's picture on its key frames. A clip of a list is reached by a seek to the key frame at its in point. Four things were wrong at such a join (D932, measured on 8.1.3 with films of an open GOP, which is what x265 makes by default, what most Blu-ray discs and TV recordings have, and every MPEG-2 film): - The pictures that decode after that key frame and show before it (leading pictures) came too. They belong to what was cut away and refer to pictures that are not there: they came out on top of the end of the clip before, as garbage. They are dropped, only where the seek landed on the in point's own key frame. - A key frame that is no IDR picture takes its place in the order from the pictures before it, which at a join are another clip's: frames came out twice or not at all. Such a H.264 I picture or HEVC CRA picture becomes an IDR picture, and the pictures after it, to the next IDR picture, count from it (the new bitstream filters h264_splice and hevc_splice, which change slice headers alone). A BLA picture is the standard's own way for HEVC, but FFmpeg's decoder keeps the references from before one and refuses a picture of the same order ("Duplicate POC"), and many players decode with it. - The out point was compared with decode times, and how far before its time a key frame decodes is not the same in every file: the caller guessed it. The picture now ends at the first key frame that shows at the out point or later; a file without a picture ends as it did. - A file that is neither Matroska nor MP4 (MPEG-TS, ASF, AVI) is sought by time alone and landed between two key frames: frames were lost or came from the key frame before. It is sought 2 s earlier and the picture is dropped up to the in point's key frame. The Media studio's save (lib/studio_render.php) is the only user of in and out points here. Not reported upstream: ours until a release joins such files itself. --- a/configure +++ b/configure @@ -3706,6 +3706,8 @@ h264_metadata_bsf_deps="const_nan" h264_metadata_bsf_select="cbs_h264" h264_redundant_pps_bsf_select="cbs_h264" +h264_splice_bsf_select="cbs_h264" +hevc_splice_bsf_select="cbs_h265" hevc_metadata_bsf_select="cbs_h265" lcevc_metadata_bsf_select="cbs_lcevc" mjpeg2jpeg_bsf_select="jpegtables" --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -44,6 +44,8 @@ extern const FFBitStreamFilter ff_h264_metadata_bsf; extern const FFBitStreamFilter ff_h264_mp4toannexb_bsf; extern const FFBitStreamFilter ff_h264_redundant_pps_bsf; +extern const FFBitStreamFilter ff_h264_splice_bsf; +extern const FFBitStreamFilter ff_hevc_splice_bsf; extern const FFBitStreamFilter ff_hapqa_extract_bsf; extern const FFBitStreamFilter ff_hevc_metadata_bsf; extern const FFBitStreamFilter ff_hevc_mp4toannexb_bsf; --- a/libavcodec/bsf/Makefile +++ b/libavcodec/bsf/Makefile @@ -20,6 +20,8 @@ OBJS-$(CONFIG_H264_METADATA_BSF) += bsf/h264_metadata.o OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF) += bsf/h264_mp4toannexb.o OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF) += bsf/h264_redundant_pps.o +OBJS-$(CONFIG_H264_SPLICE_BSF) += bsf/h264_splice.o +OBJS-$(CONFIG_HEVC_SPLICE_BSF) += bsf/hevc_splice.o OBJS-$(CONFIG_HAPQA_EXTRACT_BSF) += bsf/hapqa_extract.o OBJS-$(CONFIG_HEVC_METADATA_BSF) += bsf/h265_metadata.o OBJS-$(CONFIG_DOVI_RPU_BSF) += bsf/dovi_rpu.o --- a/libavcodec/bsf/h264_splice.c +++ b/libavcodec/bsf/h264_splice.c @@ -0,0 +1,173 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Makes the first picture of an H.264 stream, an I picture that is no IDR picture, an + * IDR picture, and counts frame_num and pic_order_cnt_lsb of the pictures after it from it, + * up to the stream's next IDR picture. Joined after another stream, such a picture took its + * place in the order from the pictures of that stream, and a decoder showed frames twice or + * not at all. The pictures' data are not changed; the pictures that show before the first + * one must not be in the stream. + */ + +#include "libavutil/log.h" +#include "libavutil/mem.h" + +#include "bsf.h" +#include "bsf_internal.h" +#include "cbs.h" +#include "cbs_h264.h" +#include "codec_id.h" +#include "h264.h" +#include "packet.h" + +typedef struct H264SpliceContext { + CodedBitstreamContext *input, *output; + CodedBitstreamFragment au; + int started, done; + unsigned frame_num, lsb; +} H264SpliceContext; + +static int h264_splice_init(AVBSFContext *bsf) +{ + H264SpliceContext *ctx = bsf->priv_data; + int err; + + if ((err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf)) < 0 || + (err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf)) < 0) + return err; + if (bsf->par_in->extradata_size) { + err = ff_cbs_read_extradata(ctx->input, &ctx->au, bsf->par_in); + /* the output's parameter sets are the input's */ + if (err >= 0) + err = ff_cbs_write_fragment_data(ctx->output, &ctx->au); + ff_cbs_fragment_reset(&ctx->au); + if (err < 0) + return err; + } + return 0; +} + +static int h264_splice_update(AVBSFContext *bsf) +{ + H264SpliceContext *ctx = bsf->priv_data; + const CodedBitstreamH264Context *h264 = ctx->input->priv_data; + const int started = ctx->started; + int first_bottom = -1, reset = 0; + + for (int i = 0; i < ctx->au.nb_units; i++) { + CodedBitstreamUnit *unit = &ctx->au.units[i]; + H264RawSliceHeader *sh; + const H264RawPPS *pps; + const H264RawSPS *sps; + unsigned max_frame_num, max_lsb; + + if (unit->type == H264_NAL_IDR_SLICE) { + ctx->done = 1; + return 0; + } + if (unit->type != H264_NAL_SLICE || !unit->content) + continue; + sh = &((H264RawSlice *)unit->content)->header; + pps = h264->pps[sh->pic_parameter_set_id]; + sps = pps ? h264->sps[pps->seq_parameter_set_id] : NULL; + if (!sps) + return AVERROR_INVALIDDATA; + max_frame_num = 1U << (sps->log2_max_frame_num_minus4 + 4); + max_lsb = 1U << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); + if (!ctx->started) { + ctx->started = 1; + ctx->frame_num = sh->frame_num; + ctx->lsb = sh->pic_order_cnt_lsb; + } + sh->frame_num = (sh->frame_num + max_frame_num - ctx->frame_num % max_frame_num) % max_frame_num; + sh->pic_order_cnt_lsb = (sh->pic_order_cnt_lsb + max_lsb - ctx->lsb % max_lsb) % max_lsb; + if (!started) { + /* the first picture: a frame, or the first of a frame's two fields */ + if (first_bottom < 0) + first_bottom = sh->bottom_field_flag; + if (sh->bottom_field_flag == first_bottom) { + if (sh->slice_type % 5 != 2 || !sh->nal_unit_header.nal_ref_idc) + return AVERROR_INVALIDDATA; + unit->type = sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE; + sh->idr_pic_id = 7; + sh->no_output_of_prior_pics_flag = 0; + sh->long_term_reference_flag = 0; + } + } else if (sh->nal_unit_header.nal_ref_idc && sh->adaptive_ref_pic_marking_mode_flag) { + for (int k = 0; k < H264_MAX_MMCO_COUNT && sh->mmco[k].memory_management_control_operation; k++) + reset |= sh->mmco[k].memory_management_control_operation == 5; + } + } + /* that operation counts anew from the picture that has it, as an IDR picture does */ + if (reset) + ctx->done = 1; + return 0; +} + +static int h264_splice_filter(AVBSFContext *bsf, AVPacket *pkt) +{ + H264SpliceContext *ctx = bsf->priv_data; + int err; + + if ((err = ff_bsf_get_packet_ref(bsf, pkt)) < 0 || ctx->done) + return err; + if ((err = ff_cbs_read_packet(ctx->input, &ctx->au, pkt)) >= 0 && + (err = h264_splice_update(bsf)) >= 0) + err = ff_cbs_write_packet(ctx->output, pkt, &ctx->au); + ff_cbs_fragment_reset(&ctx->au); + if (err < 0) { + /* a packet that cannot be read goes as it is; without the first picture all do */ + av_log(bsf, AV_LOG_WARNING, "A picture after a join was left as it is.\n"); + ctx->done = !ctx->started; + ctx->started = 1; + } + return 0; +} + +static void h264_splice_flush(AVBSFContext *bsf) +{ + H264SpliceContext *ctx = bsf->priv_data; + + ff_cbs_flush(ctx->input); + ff_cbs_flush(ctx->output); +} + +static void h264_splice_close(AVBSFContext *bsf) +{ + H264SpliceContext *ctx = bsf->priv_data; + + ff_cbs_fragment_free(&ctx->au); + ff_cbs_close(&ctx->input); + ff_cbs_close(&ctx->output); +} + +static const enum AVCodecID h264_splice_codec_ids[] = { + AV_CODEC_ID_H264, AV_CODEC_ID_NONE, +}; + +const FFBitStreamFilter ff_h264_splice_bsf = { + .p.name = "h264_splice", + .p.codec_ids = h264_splice_codec_ids, + .priv_data_size = sizeof(H264SpliceContext), + .init = h264_splice_init, + .flush = h264_splice_flush, + .close = h264_splice_close, + .filter = h264_splice_filter, +}; --- a/libavcodec/bsf/hevc_splice.c +++ b/libavcodec/bsf/hevc_splice.c @@ -0,0 +1,220 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Makes the first picture of an H.265 stream, a CRA picture, an IDR picture, and counts + * the picture order of the pictures after it from it, up to the stream's next IDR or BLA + * picture. Joined after another stream, such a picture took its place in the order from + * the pictures of that stream, and a decoder showed frames twice or not at all. (A BLA + * picture is the standard's own way, but FFmpeg's decoder keeps the references from before + * one and refuses a picture with the same order.) The pictures' data are not changed; the + * pictures that show before the first one must not be in the stream. + */ + +#include "libavutil/intreadwrite.h" +#include "libavutil/log.h" +#include "libavutil/mem.h" + +#include "bsf.h" +#include "bsf_internal.h" +#include "cbs.h" +#include "cbs_h265.h" +#include "codec_id.h" +#include "hevc/hevc.h" +#include "packet.h" + +typedef struct HEVCSpliceContext { + CodedBitstreamContext *input, *output; + CodedBitstreamFragment au; + int prefixed; /* the packets hold lengths of 4 bytes, not start codes */ + int started, done; + unsigned lsb; +} HEVCSpliceContext; + +static int hevc_splice_init(AVBSFContext *bsf) +{ + HEVCSpliceContext *ctx = bsf->priv_data; + const AVCodecParameters *par = bsf->par_in; + int err; + + ctx->prefixed = par->extradata_size >= 23 && par->extradata[0] == 1; + if (ctx->prefixed && (par->extradata[21] & 3) != 3) + return AVERROR_PATCHWELCOME; + if ((err = ff_cbs_init(&ctx->input, AV_CODEC_ID_HEVC, bsf)) < 0 || + (err = ff_cbs_init(&ctx->output, AV_CODEC_ID_HEVC, bsf)) < 0) + return err; + if (par->extradata_size) { + err = ff_cbs_read_extradata(ctx->input, &ctx->au, par); + /* the output's parameter sets are the input's */ + if (err >= 0) + err = ff_cbs_write_fragment_data(ctx->output, &ctx->au); + ff_cbs_fragment_reset(&ctx->au); + if (err < 0) + return err; + } + return 0; +} + +static int hevc_splice_update(AVBSFContext *bsf) +{ + HEVCSpliceContext *ctx = bsf->priv_data; + const CodedBitstreamH265Context *h265 = ctx->input->priv_data; + const int started = ctx->started; + + for (int i = 0; i < ctx->au.nb_units; i++) { + CodedBitstreamUnit *unit = &ctx->au.units[i]; + H265RawSliceHeader *sh; + const H265RawPPS *pps; + const H265RawSPS *sps; + unsigned max_lsb; + + if (unit->type > HEVC_NAL_RSV_VCL31 || !unit->content) + continue; + if (unit->type >= HEVC_NAL_BLA_W_LP && unit->type <= HEVC_NAL_IDR_N_LP) { + ctx->done = 1; + return 0; + } + sh = &((H265RawSlice *)unit->content)->header; + if (sh->nal_unit_header.nuh_layer_id) + return AVERROR_PATCHWELCOME; + if (!started) { + if (unit->type != HEVC_NAL_CRA_NUT) + return AVERROR_INVALIDDATA; + unit->type = sh->nal_unit_header.nal_unit_type = HEVC_NAL_IDR_N_LP; + } + /* a dependent slice segment has the order of the slice it depends on */ + if (sh->dependent_slice_segment_flag) + continue; + pps = h265->pps[sh->slice_pic_parameter_set_id]; + sps = pps ? h265->sps[pps->pps_seq_parameter_set_id] : NULL; + if (!sps) + return AVERROR_INVALIDDATA; + max_lsb = 1U << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); + if (!ctx->started) { + ctx->started = 1; + ctx->lsb = sh->slice_pic_order_cnt_lsb; + } + if (!started) { + if (sh->slice_type != HEVC_SLICE_I) + return AVERROR_INVALIDDATA; + continue; + } + if (sh->num_long_term_sps) + return AVERROR_PATCHWELCOME; + sh->slice_pic_order_cnt_lsb = (sh->slice_pic_order_cnt_lsb + max_lsb - ctx->lsb) % max_lsb; + for (int k = 0; k < sh->num_long_term_pics; k++) + sh->poc_lsb_lt[k] = (sh->poc_lsb_lt[k] + max_lsb - ctx->lsb) % max_lsb; + } + return 0; +} + +/* The start codes of a packet as lengths of 4 bytes, as its stream's parameter sets say. */ +static int hevc_splice_prefix(AVPacket *pkt) +{ + AVPacket *out = av_packet_alloc(); + const uint8_t *d = pkt->data; + int ret, at = 0, n = 0; + + if (!out) + return AVERROR(ENOMEM); + if ((ret = av_new_packet(out, pkt->size + pkt->size / 3 + 4)) < 0) + goto end; + while (at + 3 <= pkt->size && !(d[at] == 0 && d[at + 1] == 0 && d[at + 2] == 1)) + at++; + while (at + 3 < pkt->size) { + int from = at += 3, to; + while (at + 3 <= pkt->size && !(d[at] == 0 && d[at + 1] == 0 && d[at + 2] == 1)) + at++; + to = at + 3 <= pkt->size ? at : pkt->size; + /* a start code of four bytes: its first zero belongs to it */ + if (to < pkt->size && to > from && d[to - 1] == 0) + to--; + AV_WB32(out->data + n, to - from); + memcpy(out->data + n + 4, d + from, to - from); + n += 4 + to - from; + } + av_shrink_packet(out, n); + if ((ret = av_packet_copy_props(out, pkt)) >= 0) { + av_packet_unref(pkt); + av_packet_move_ref(pkt, out); + } +end: + av_packet_free(&out); + return ret; +} + +static int hevc_splice_filter(AVBSFContext *bsf, AVPacket *pkt) +{ + HEVCSpliceContext *ctx = bsf->priv_data; + AVPacket *was; + int err; + + if ((err = ff_bsf_get_packet_ref(bsf, pkt)) < 0 || ctx->done) + return err; + if (!(was = av_packet_clone(pkt))) { + av_packet_unref(pkt); + return AVERROR(ENOMEM); + } + if ((err = ff_cbs_read_packet(ctx->input, &ctx->au, pkt)) >= 0 && + (err = hevc_splice_update(bsf)) >= 0 && !ctx->done && + (err = ff_cbs_write_packet(ctx->output, pkt, &ctx->au)) >= 0 && ctx->prefixed) + err = hevc_splice_prefix(pkt); + ff_cbs_fragment_reset(&ctx->au); + if (err < 0) { + /* a packet that cannot be read goes as it is; without the first picture all do */ + av_log(bsf, AV_LOG_WARNING, "A picture after a join was left as it is.\n"); + av_packet_unref(pkt); + av_packet_move_ref(pkt, was); + ctx->done = !ctx->started; + ctx->started = 1; + } + av_packet_free(&was); + return 0; +} + +static void hevc_splice_flush(AVBSFContext *bsf) +{ + HEVCSpliceContext *ctx = bsf->priv_data; + + ff_cbs_flush(ctx->input); + ff_cbs_flush(ctx->output); +} + +static void hevc_splice_close(AVBSFContext *bsf) +{ + HEVCSpliceContext *ctx = bsf->priv_data; + + ff_cbs_fragment_free(&ctx->au); + ff_cbs_close(&ctx->input); + ff_cbs_close(&ctx->output); +} + +static const enum AVCodecID hevc_splice_codec_ids[] = { + AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE, +}; + +const FFBitStreamFilter ff_hevc_splice_bsf = { + .p.name = "hevc_splice", + .p.codec_ids = hevc_splice_codec_ids, + .priv_data_size = sizeof(HEVCSpliceContext), + .init = hevc_splice_init, + .flush = hevc_splice_flush, + .close = hevc_splice_close, + .filter = hevc_splice_filter, +}; --- a/libavformat/concatdec.c +++ b/libavformat/concatdec.c @@ -38,6 +38,14 @@ /* how far past an in point its seek goes, in AV_TIME_BASE units: 8 ms */ #define INPOINT_SEEK_SLACK 8000 +/* how far off an in point its key frame may be, in AV_TIME_BASE units: 25 ms, the + * rounding of a time to a quarter of a frame and of a container to milliseconds */ +#define INPOINT_KEY_SLACK 25000 +/* how long before an in point a file is read whose seek may land past the key frame there: 2 s */ +#define INPOINT_HUNT 2000000 +/* how far past its out point a file is read for what was stored late: 1 s */ +#define OUTPOINT_DRAIN 1000000 + typedef enum ConcatMatchMode { MATCH_ONE_TO_ONE, MATCH_EXACT_ID, @@ -45,6 +53,7 @@ typedef struct ConcatStream { AVBSFContext *bsf; + AVBSFContext *splice; /* H.264, HEVC: the key frame at the in point made an IDR picture */ int out_stream_index; } ConcatStream; @@ -56,6 +65,11 @@ int64_t duration; int64_t user_duration; int64_t next_dts; + int video; /* the picture's stream, -1 without one */ + int started; /* the picture's first packet was read */ + int ended; /* the out point was reached */ + int hunt; /* the picture's packets are dropped up to the key frame at the in point */ + int64_t lead; /* the key frame at the in point, AV_NOPTS_VALUE for none */ ConcatStream *streams; int64_t inpoint; int64_t outpoint; @@ -337,6 +351,30 @@ return AV_NOPTS_VALUE; } +static int main_video_stream(const AVFormatContext *avf) +{ + for (unsigned i = 0; i < avf->nb_streams; i++) + if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + !(avf->streams[i]->disposition & (AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS))) + return i; + return -1; +} + +/* Seeks the current file to its in point. Matroska and MP4 find the key frame there by + * their index; a file of another kind is sought by time alone and may land between two key + * frames, or past the one wanted, so it is sought earlier and read up to it. */ +static int seek_inpoint(ConcatContext *cat) +{ + ConcatFile *file = cat->cur_file; + const char *name = cat->avf->iformat->name; + int64_t seek = av_sat_add64(file->inpoint, INPOINT_SEEK_SLACK); + + file->hunt = file->video >= 0 && !strstr(name, "matroska") && !strstr(name, "mp4"); + if (file->hunt) + seek = FFMAX(file->inpoint - INPOINT_HUNT, file->file_start_time); + return avformat_seek_file(cat->avf, -1, INT64_MIN, seek, seek, 0); +} + static int open_file(AVFormatContext *avf, unsigned fileno) { ConcatContext *cat = avf->priv_data; @@ -387,6 +425,9 @@ file->file_start_time = (cat->avf->start_time == AV_NOPTS_VALUE) ? 0 : cat->avf->start_time; file->file_inpoint = (file->inpoint == AV_NOPTS_VALUE) ? file->file_start_time : file->inpoint; file->duration = get_best_effort_duration(file, cat->avf); + file->video = main_video_stream(cat->avf); + file->started = file->ended = 0; + file->lead = AV_NOPTS_VALUE; if (cat->segment_time_metadata) { av_dict_set_int(&file->metadata, "lavf.concatdec.start_time", file->start_time, 0); @@ -400,8 +441,7 @@ /* a few milliseconds past the in point: an MP4 whose times were rounded to * milliseconds has key frames up to 1 ms off the constant delay its seek * assumes, and a seek to the key frame's own time found the one before it */ - int64_t seek = av_sat_add64(file->inpoint, INPOINT_SEEK_SLACK); - if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, seek, seek, 0)) < 0) + if ((ret = seek_inpoint(cat)) < 0) return ret; } return 0; @@ -417,6 +457,7 @@ for (j = 0; j < cat->files[i].nb_streams; j++) { if (cat->files[i].streams[j].bsf) av_bsf_free(&cat->files[i].streams[j].bsf); + av_bsf_free(&cat->files[i].streams[j].splice); } av_freep(&cat->files[i].streams); av_dict_free(&cat->files[i].metadata); @@ -758,19 +799,91 @@ return ret; } } + if (cs->splice) { + /* one packet in, the same one out */ + if ((ret = av_bsf_send_packet(cs->splice, pkt)) < 0 || + (ret = av_bsf_receive_packet(cs->splice, pkt)) < 0) { + av_log(avf, AV_LOG_ERROR, "splice filter failed\n"); + return ret; + } + } return 0; } -/* Returns true if the packet dts is greater or equal to the specified outpoint. */ -static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt) +/* An H.264 or HEVC key frame at a join that is no IDR picture (an open GOP's) becomes one: + * from there the pictures count anew, not on from those of the file before. */ +static int splice(AVFormatContext *avf, ConcatStream *cs, const AVStream *st) { - if (cat->cur_file->outpoint != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE) { - return av_compare_ts(pkt->dts, cat->avf->streams[pkt->stream_index]->time_base, - cat->cur_file->outpoint, AV_TIME_BASE_Q) >= 0; + const char *name = st->codecpar->codec_id == AV_CODEC_ID_H264 ? "h264_splice" : "hevc_splice"; + AVBSFContext *bsf; + int ret; + + if (!(ret = av_bsf_alloc(av_bsf_get_by_name(name), &bsf))) { + bsf->time_base_in = st->time_base; + if ((ret = avcodec_parameters_copy(bsf->par_in, st->codecpar)) >= 0) + ret = av_bsf_init(bsf); + if (ret >= 0) { + cs->splice = bsf; + return 0; + } + av_bsf_free(&bsf); } + av_log(avf, AV_LOG_WARNING, "The key frame at a join stays as it is.\n"); return 0; } +/* The picture of a file with an in point: it starts on the key frame there, and a + * picture that shows before that key frame (a leading picture of an open GOP) belongs to + * what was cut away: it refers to pictures that are not there. Returns 1 for a packet to + * drop. A seek that landed on an earlier key frame changes nothing: a decoder needs it. */ +static int picture_before_inpoint(AVFormatContext *avf, AVPacket *pkt) +{ + ConcatContext *cat = avf->priv_data; + ConcatFile *file = cat->cur_file; + const AVStream *st = cat->avf->streams[pkt->stream_index]; + + if (file->inpoint == AV_NOPTS_VALUE || pkt->stream_index != file->video) + return 0; + if (file->hunt) { + const int64_t ts = pkt->pts != AV_NOPTS_VALUE ? pkt->pts : pkt->dts; + if (!(pkt->flags & AV_PKT_FLAG_KEY) || ts == AV_NOPTS_VALUE || + av_compare_ts(ts, st->time_base, file->inpoint - INPOINT_KEY_SLACK, AV_TIME_BASE_Q) < 0) + return 1; + file->hunt = 0; + } + if (pkt->pts == AV_NOPTS_VALUE) + return 0; + if (!file->started) { + file->started = 1; + if (pkt->flags & AV_PKT_FLAG_KEY && + av_compare_ts(pkt->pts, st->time_base, file->inpoint - INPOINT_KEY_SLACK, AV_TIME_BASE_Q) >= 0) { + file->lead = pkt->pts; + if (!file->streams[pkt->stream_index].splice && + (st->codecpar->codec_id == AV_CODEC_ID_H264 || + (st->codecpar->codec_id == AV_CODEC_ID_HEVC && file != cat->files))) + return splice(avf, &file->streams[pkt->stream_index], st); + } + } + return file->lead != AV_NOPTS_VALUE && pkt->pts < file->lead; +} + +/* Returns true where the file ends: at the picture's first key frame that shows at the + * out point or later. By its presentation time: how far before it a key frame decodes is + * not the same in every file, and a leading picture after it needs it. Without a picture + * the first packet at the out point, as it was. */ +static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt) +{ + const ConcatFile *file = cat->cur_file; + const AVStream *st = cat->avf->streams[pkt->stream_index]; + + if (file->outpoint == AV_NOPTS_VALUE) + return 0; + if (pkt->stream_index == file->video && pkt->flags & AV_PKT_FLAG_KEY && pkt->pts != AV_NOPTS_VALUE) + return av_compare_ts(pkt->pts, st->time_base, file->outpoint, AV_TIME_BASE_Q) >= 0; + return pkt->dts != AV_NOPTS_VALUE && + av_compare_ts(pkt->dts, st->time_base, file->outpoint + (file->video >= 0 ? OUTPOINT_DRAIN : 0), AV_TIME_BASE_Q) >= 0; +} + static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt) { ConcatContext *cat = avf->priv_data; @@ -809,6 +922,12 @@ av_packet_unref(pkt); continue; } + if ((ret = picture_before_inpoint(avf, pkt))) { + av_packet_unref(pkt); + if (ret < 0) + return ret; + continue; + } break; } if ((ret = filter_packet(avf, cs, pkt)) < 0)