The concat demuxer cuts subtitles at a file's in and out points. A clip of a list kept a subtitle by where it starts: one that starts before the in point came early (or, in a first clip, at a negative time that moved every stream, D927), one that shows past the out point ran on over the next clip, and one that starts before the key frame the seek lands on was never read. Now a subtitle is shown for exactly the part of it that lies between the in point and the end of the clip (D932); a piece shorter than 0.3 s is dropped. - Text (SubRip, ASS, WebVTT, mov_text): its time and duration. - PGS: the set that shows at the in point is given there (with the sets it stands on, from the epoch's start), and a set that clears the picture is made at the end of the clip. - VobSub: its time, its duration and the dates of its commands. - Any other subtitle format (DVB, XSUB) keeps the rule of before: whole, in the clip it starts in. A file that is no MP4 is read from 120 s before its in point for them, and for a second past its out point for what was stored late. Replaces ffmpeg-01-concat-subtitles-before-inpoint.patch, which only dropped what starts before the in point. Measured on 8.1.3 (D932, tools/fm-test.sh studio-render). Not reported upstream. --- b/libavformat/concatdec.c +++ c/libavformat/concatdec.c @@ -29,6 +29,7 @@ #include "libavutil/timestamp.h" #include "libavcodec/codec_desc.h" #include "libavcodec/bsf.h" +#include "libavcodec/packet_internal.h" #include "avformat.h" #include "avio_internal.h" #include "demux.h" @@ -43,6 +44,10 @@ #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 long before an in point a subtitle that still shows there may start: 120 s */ +#define SUBTITLE_PREROLL 120000000 +/* the shortest piece of a subtitle a cut leaves: 0.3 s */ +#define SUBTITLE_PIECE 300000 /* how far past its out point a file is read for what was stored late: 1 s */ #define OUTPOINT_DRAIN 1000000 @@ -55,6 +60,10 @@ AVBSFContext *bsf; AVBSFContext *splice; /* H.264, HEVC: the key frame at the in point made an IDR picture */ int out_stream_index; + PacketList epoch; /* PGS before the in point: the sets from the last one that stands alone */ + int shown; /* PGS: a picture is on the screen */ + uint8_t clear[64]; /* PGS: the set that takes it off, built from the last set */ + int clear_size; } ConcatStream; typedef struct { @@ -70,6 +79,9 @@ 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 */ + int ready; /* the subtitles from before the in point were asked for */ + int prerolled; /* ... and read */ + int finished; /* nothing more is read from it */ ConcatStream *streams; int64_t inpoint; int64_t outpoint; @@ -87,6 +99,7 @@ int safe; int seekable; int eof; + PacketList queue; /* subtitles of the current file, cut, to be returned first */ ConcatMatchMode stream_match_mode; unsigned auto_convert; int segment_time_metadata; @@ -426,7 +439,7 @@ 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->started = file->ended = file->ready = file->prerolled = file->finished = 0; file->lead = AV_NOPTS_VALUE; if (cat->segment_time_metadata) { @@ -452,12 +465,14 @@ ConcatContext *cat = avf->priv_data; unsigned i, j; + avpriv_packet_list_free(&cat->queue); for (i = 0; i < cat->nb_files; i++) { av_freep(&cat->files[i].url); 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); + avpriv_packet_list_free(&cat->files[i].streams[j].epoch); } av_freep(&cat->files[i].streams); av_dict_free(&cat->files[i].metadata); @@ -884,6 +899,390 @@ av_compare_ts(pkt->dts, st->time_base, file->outpoint + (file->video >= 0 ? OUTPOINT_DRAIN : 0), AV_TIME_BASE_Q) >= 0; } +/* How a subtitle's packets are cut: a text one by its time and duration, PGS by its + * display sets, a VobSub one by the dates of its commands; any other goes whole with the + * clip it starts in. */ +enum SubtitleCut { SUB_NONE, SUB_WHOLE, SUB_TEXT, SUB_PGS, SUB_DVD }; + +static enum SubtitleCut subtitle_cut(const ConcatFile *file, const AVStream *st) +{ + const AVCodecDescriptor *desc = avcodec_descriptor_get(st->codecpar->codec_id); + + if (st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE || + (file->inpoint == AV_NOPTS_VALUE && file->outpoint == AV_NOPTS_VALUE)) + return SUB_NONE; + switch (st->codecpar->codec_id) { + case AV_CODEC_ID_HDMV_PGS_SUBTITLE: + return SUB_PGS; + case AV_CODEC_ID_DVD_SUBTITLE: + return SUB_DVD; + default: + return desc && desc->props & AV_CODEC_PROP_TEXT_SUB ? SUB_TEXT : SUB_WHOLE; + } +} + +/* Where the file's subtitles start and end, in a stream's time base: the in point and, with + * a duration, the moment the next file starts (the out point may lie a little before it). */ +static void subtitle_range(const ConcatFile *file, const AVStream *st, int64_t *in, int64_t *out) +{ + int64_t end = file->outpoint; + + if (end != AV_NOPTS_VALUE && file->user_duration != AV_NOPTS_VALUE) + end = av_sat_add64(file->file_inpoint, file->user_duration); + *in = file->inpoint == AV_NOPTS_VALUE ? INT64_MIN : av_rescale_q(file->inpoint, AV_TIME_BASE_Q, st->time_base); + *out = end == AV_NOPTS_VALUE ? INT64_MAX : av_rescale_q(end, AV_TIME_BASE_Q, st->time_base); +} + +/* The PGS segment of a type in a packet: its body and size, or NULL. */ +static const uint8_t *pgs_segment(const AVPacket *pkt, int type, int *size) +{ + for (int64_t at = 0; at + 3 <= pkt->size;) { + int n = AV_RB16(pkt->data + at + 1); + if (at + 3 + n > pkt->size) + return NULL; + if (pkt->data[at] == type) { + *size = n; + return pkt->data + at + 3; + } + at += 3 + n; + } + return NULL; +} + +/* Keeps what a PGS set tells: whether it shows something, and the set that clears it (its + * composition with no object and the next number, its windows, an end). Returns the + * composition's body, NULL for a packet with none (a set split over packets). */ +static const uint8_t *pgs_note(ConcatStream *cs, const AVPacket *pkt) +{ + int n, w; + const uint8_t *pcs = pgs_segment(pkt, 0x16, &n), *wds; + uint8_t *c = cs->clear; + + if (!pcs || n < 11) + return NULL; + cs->shown = pcs[10] > 0; + *c++ = 0x16; + AV_WB16(c, 11); + memcpy(c + 2, pcs, 5); + AV_WB16(c + 7, AV_RB16(pcs + 5) + 1); + memset(c + 9, 0, 4); + c += 13; + wds = pgs_segment(pkt, 0x17, &w); + if (wds && w <= sizeof(cs->clear) - 14 - 3 - 3) { + *c++ = 0x17; + AV_WB16(c, w); + memcpy(c + 2, wds, w); + c += 2 + w; + } + *c++ = 0x80; + AV_WB16(c, 0); + cs->clear_size = c + 2 - cs->clear; + return pcs; +} + +/* The set that clears a stream's PGS picture, into the queue. */ +static int pgs_clear(ConcatContext *cat, int index, int64_t at) +{ + ConcatStream *cs = &cat->cur_file->streams[index]; + AVPacket *pkt; + int ret; + + cs->shown = 0; + if (!cs->clear_size) + return 0; + if (!(pkt = av_packet_alloc())) + return AVERROR(ENOMEM); + if ((ret = av_new_packet(pkt, cs->clear_size)) >= 0) { + memcpy(pkt->data, cs->clear, cs->clear_size); + pkt->pts = pkt->dts = at; + pkt->stream_index = index; + pkt->flags |= AV_PKT_FLAG_KEY; + ret = avpriv_packet_list_put(&cat->queue, pkt, NULL, 0); + } + av_packet_free(&pkt); + return ret; +} + +/* Where a VobSub packet's control sequences lie, one at a time: the place of the next one's + * date, 0 at the end, with its start and stop commands told. */ +static int dvd_sequence(const AVPacket *pkt, int at, int *start, int *stop) +{ + static const uint8_t len[] = { 0, 0, 0, 2, 2, 6, 4 }; + int next, pos = at + 4; + + *start = *stop = 0; + if (at + 4 > pkt->size) + return 0; + next = AV_RB16(pkt->data + at + 2); + while (pos < pkt->size && pkt->data[pos] != 0xff) { + int cmd = pkt->data[pos]; + if (cmd <= 1) + *start = 1; + else if (cmd == 2) + *stop = 1; + if (cmd < FF_ARRAY_ELEMS(len)) + pos += 1 + len[cmd]; + else if (cmd == 7 && pos + 3 <= pkt->size) + pos += 1 + AV_RB16(pkt->data + pos + 1); + else + break; + } + return next > at ? next : 0; +} + +/* Cuts a VobSub packet: its time moves to the in point and its commands' dates (1024/90000 s + * after it) with it, none later than the out point. Returns 1 for one to drop. */ +static int cut_dvd(const AVStream *st, AVPacket *pkt, int64_t in, int64_t out) +{ + static const AVRational tick = { 1024, 90000 }; + int64_t from = AV_NOPTS_VALUE, to = AV_NOPTS_VALUE, start, end; + int at, next, ret, a, b; + + if (pkt->size < 4) + return pkt->pts < in || pkt->pts >= out; + for (at = AV_RB16(pkt->data + 2); at; at = next) { + next = dvd_sequence(pkt, at, &a, &b); + if (at + 2 > pkt->size) + break; + if (a && from == AV_NOPTS_VALUE) + from = AV_RB16(pkt->data + at); + if (b) + to = AV_RB16(pkt->data + at); + } + start = pkt->pts + (from == AV_NOPTS_VALUE ? 0 : av_rescale_q(from, tick, st->time_base)); + end = to != AV_NOPTS_VALUE ? pkt->pts + av_rescale_q(to, tick, st->time_base) : + pkt->duration > 0 ? pkt->pts + pkt->duration : AV_NOPTS_VALUE; + if (start >= out || (end == AV_NOPTS_VALUE ? pkt->pts < in : end <= in)) + return 1; + if (end != AV_NOPTS_VALUE && (start < in || end > out) && + FFMIN(end, out) - FFMAX(start, in) < av_rescale_q(SUBTITLE_PIECE, AV_TIME_BASE_Q, st->time_base)) + return 1; + if (pkt->pts >= in && (end == AV_NOPTS_VALUE ? out == INT64_MAX : end <= out)) + return 0; + if ((ret = av_packet_make_writable(pkt)) < 0) + return ret; + start = FFMAX(pkt->pts, in); + for (at = AV_RB16(pkt->data + 2); at; at = next) { + int64_t date; + next = dvd_sequence(pkt, at, &a, &b); + if (at + 2 > pkt->size) + break; + date = pkt->pts + av_rescale_q(AV_RB16(pkt->data + at), tick, st->time_base); + date = FFMIN(FFMAX(date, start), out); + AV_WB16(pkt->data + at, av_clip_uint16(av_rescale_q_rnd(date - start, st->time_base, tick, b ? AV_ROUND_DOWN : AV_ROUND_NEAR_INF))); + } + if (pkt->duration > 0) + pkt->duration = FFMIN(pkt->pts + pkt->duration, out) - start; + pkt->pts = pkt->dts = start; + return 0; +} + +/* Cuts a text packet to the file's range. Returns 1 for one to drop: no part of it is in the + * range, or less than SUBTITLE_PIECE of one a cut goes through. */ +static int cut_text(const AVStream *st, AVPacket *pkt, int64_t in, int64_t out) +{ + int64_t start = pkt->pts, end = pkt->pts + pkt->duration; + + if (pkt->duration <= 0) + return start < in || start >= out; + if (start >= out || end <= in) + return 1; + start = FFMAX(start, in); + end = FFMIN(end, out); + if ((start > pkt->pts || end < pkt->pts + pkt->duration) && + end - start < av_rescale_q(SUBTITLE_PIECE, AV_TIME_BASE_Q, st->time_base)) + return 1; + pkt->pts = pkt->dts = start; + pkt->duration = end - start; + return 0; +} + +/* Cuts a PGS set at or after the in point. Returns 1 for one to drop. A picture that would + * show for less than SUBTITLE_PIECE before the out point does not show. */ +static int cut_pgs(ConcatContext *cat, const AVStream *st, AVPacket *pkt, int64_t out) +{ + ConcatStream *cs = &cat->cur_file->streams[pkt->stream_index]; + int was = cs->shown, ret; + + if (pkt->pts >= out) + return 1; + if (!pgs_note(cs, pkt) || !cs->shown) + return 0; + if (out != INT64_MAX) { + if (out - pkt->pts < av_rescale_q(SUBTITLE_PIECE, AV_TIME_BASE_Q, st->time_base)) { + cs->shown = 0; + if (was && (ret = pgs_clear(cat, pkt->stream_index, pkt->pts)) < 0) + return ret; + return 1; + } + if (pkt->duration > 0) + pkt->duration = FFMIN(pkt->duration, out - pkt->pts); + } + return 0; +} + +/* Cuts a subtitle packet of the current file. Returns 1 for one to drop. */ +static int cut_subtitle(ConcatContext *cat, AVPacket *pkt) +{ + const ConcatFile *file = cat->cur_file; + const AVStream *st = cat->avf->streams[pkt->stream_index]; + const enum SubtitleCut kind = subtitle_cut(file, st); + int64_t in, out; + + if (kind == SUB_NONE || pkt->pts == AV_NOPTS_VALUE) + return 0; + subtitle_range(file, st, &in, &out); + /* what started before the in point was read before the file, cut and returned */ + if (pkt->pts < in && (file->prerolled || kind == SUB_WHOLE || kind == SUB_PGS)) + return 1; + switch (kind) { + case SUB_TEXT: + return cut_text(st, pkt, in, out); + case SUB_DVD: + return cut_dvd(st, pkt, in, out); + case SUB_PGS: + return cut_pgs(cat, st, pkt, out); + default: + return pkt->pts >= out; + } +} + +/* A PGS set from before the in point: the stream keeps the sets a decoder needs to show what + * shows there, from the last one that stands alone (an epoch's start, an acquisition point). */ +static int preroll_pgs(ConcatStream *cs, AVPacket *pkt) +{ + const uint8_t *pcs = pgs_note(cs, pkt); + + if (pcs && (pcs[7] & 0xc0)) + avpriv_packet_list_free(&cs->epoch); + return avpriv_packet_list_put(&cs->epoch, pkt, NULL, 0); +} + +/* Whether a subtitle stream of the current file that can be cut is mapped. */ +static int subtitles_wanted(const AVFormatContext *avf) +{ + const ConcatContext *cat = avf->priv_data; + + for (unsigned i = 0; i < cat->cur_file->nb_streams; i++) { + const int out = cat->cur_file->streams[i].out_stream_index; + if (out >= 0 && avf->streams[out]->discard < AVDISCARD_ALL && + subtitle_cut(cat->cur_file, cat->avf->streams[i]) != SUB_NONE) + return 1; + } + return 0; +} + +/* The subtitles that show at the in point and started before it. A subtitle is stored where + * it starts, so a seek to the key frame before the in point misses one that starts before + * that: the file is read from SUBTITLE_PREROLL before the in point, subtitles and the clock + * of one other stream alone, each cut subtitle goes into the queue, and the file is put back + * on its key frame. An MP4 seeks each of its streams by itself and needs none of this. */ +static int preroll_subtitles(AVFormatContext *avf) +{ + ConcatContext *cat = avf->priv_data; + ConcatFile *file = cat->cur_file; + AVPacket *pkt; + int64_t from; + int ret = 0, clock = file->video; + + if (file->inpoint == AV_NOPTS_VALUE || strstr(cat->avf->iformat->name, "mov") || !subtitles_wanted(avf)) + return 0; + for (unsigned i = 0; clock < 0 && i < cat->avf->nb_streams; i++) + if (cat->avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) + clock = i; + if (!(pkt = av_packet_alloc())) + return AVERROR(ENOMEM); + from = FFMAX(file->inpoint - SUBTITLE_PREROLL, file->file_start_time + INPOINT_SEEK_SLACK); + if (from < file->inpoint && + avformat_seek_file(cat->avf, -1, INT64_MIN, from, from, 0) >= 0) { + file->prerolled = 1; + while ((ret = av_read_frame(cat->avf, pkt)) >= 0) { + const AVStream *st = cat->avf->streams[pkt->stream_index]; + ConcatStream *cs = pkt->stream_index < file->nb_streams ? &file->streams[pkt->stream_index] : NULL; + const enum SubtitleCut kind = subtitle_cut(file, st); + int64_t in, out, next; + int n; + + if (pkt->stream_index == clock && pkt->dts != AV_NOPTS_VALUE && + av_compare_ts(pkt->dts, st->time_base, file->inpoint + OUTPOINT_DRAIN, AV_TIME_BASE_Q) >= 0) { + av_packet_unref(pkt); + break; + } + if (!cs || cs->out_stream_index < 0 || kind <= SUB_WHOLE || pkt->pts == AV_NOPTS_VALUE) { + av_packet_unref(pkt); + continue; + } + subtitle_range(file, st, &in, &out); + if (pkt->pts >= in) { + /* the next PGS set so soon that the picture before it would only flash */ + const uint8_t *pcs = kind == SUB_PGS ? pgs_segment(pkt, 0x16, &n) : NULL; + next = in + av_rescale_q(SUBTITLE_PIECE, AV_TIME_BASE_Q, st->time_base); + if (pcs && n >= 11 && pkt->pts < next && (!pcs[10] || pcs[7] & 0xc0)) + avpriv_packet_list_free(&cs->epoch); + av_packet_unref(pkt); + continue; + } + if (kind == SUB_PGS) { + ret = preroll_pgs(cs, pkt); + } else if (!(ret = kind == SUB_TEXT ? cut_text(st, pkt, in, out) : cut_dvd(st, pkt, in, out))) { + ret = avpriv_packet_list_put(&cat->queue, pkt, NULL, 0); + } + av_packet_unref(pkt); + if (ret < 0) + break; + } + for (unsigned i = 0; ret >= 0 && i < file->nb_streams; i++) { + ConcatStream *cs = &file->streams[i]; + const AVStream *st = cat->avf->streams[i]; + int64_t in, out; + + if (!cs->epoch.head || !cs->shown) { + cs->shown = 0; + avpriv_packet_list_free(&cs->epoch); + continue; + } + subtitle_range(file, st, &in, &out); + while (avpriv_packet_list_get(&cs->epoch, pkt) >= 0) { + if (pkt->duration > 0) + pkt->duration = FFMAX(pkt->pts + pkt->duration - in, 1); + pkt->pts = pkt->dts = in; + if ((ret = avpriv_packet_list_put(&cat->queue, pkt, NULL, 0)) < 0) { + av_packet_unref(pkt); + break; + } + } + } + if (ret == AVERROR_EOF) + ret = 0; + if (ret >= 0) { + ret = seek_inpoint(cat); + } + } + av_packet_free(&pkt); + return FFMIN(ret, 0); +} + +/* The PGS pictures still on the screen at a file's out point are taken off there: the set + * that would have done it belongs to what was cut away. */ +static int clear_subtitles(ConcatContext *cat) +{ + ConcatFile *file = cat->cur_file; + + for (unsigned i = 0; i < file->nb_streams; i++) { + const AVStream *st = cat->avf->streams[i]; + int64_t in, out; + int ret; + + if (!file->streams[i].shown || subtitle_cut(file, st) != SUB_PGS) + continue; + subtitle_range(file, st, &in, &out); + if (out != INT64_MAX && (ret = pgs_clear(cat, i, out)) < 0) + return ret; + file->streams[i].shown = 0; + } + return 0; +} + static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt) { ConcatContext *cat = avf->priv_data; @@ -900,9 +1299,26 @@ return AVERROR(EIO); while (1) { + ConcatFile *file = cat->cur_file; + + if (!file->ready) { + file->ready = 1; + if ((ret = preroll_subtitles(avf)) < 0) + return ret; + } + if (avpriv_packet_list_get(&cat->queue, pkt) >= 0) { + cs = &file->streams[pkt->stream_index]; + break; + } + if (file->finished) { + if ((ret = open_next_file(avf)) < 0) + return ret; + continue; + } ret = av_read_frame(cat->avf, pkt); if (ret == AVERROR_EOF) { - if ((ret = open_next_file(avf)) < 0) + file->finished = 1; + if ((ret = clear_subtitles(cat)) < 0) return ret; continue; } @@ -911,18 +1327,28 @@ if ((ret = match_streams(avf)) < 0) { return ret; } - if (packet_after_outpoint(cat, pkt)) { - av_packet_unref(pkt); - if ((ret = open_next_file(avf)) < 0) - return ret; - continue; + if (!file->ended && packet_after_outpoint(cat, pkt)) + file->ended = 1; + if (file->ended) { + /* past the out point only a subtitle from before it, stored late, is still taken */ + const AVStream *st = cat->avf->streams[pkt->stream_index]; + if (file->video < 0 || !subtitles_wanted(avf) || + (pkt->dts != AV_NOPTS_VALUE && + av_compare_ts(pkt->dts, st->time_base, file->outpoint + OUTPOINT_DRAIN, AV_TIME_BASE_Q) >= 0)) + file->finished = 1; + if (file->finished || subtitle_cut(file, st) == SUB_NONE) { + av_packet_unref(pkt); + if (file->finished && (ret = clear_subtitles(cat)) < 0) + return ret; + continue; + } } cs = &cat->cur_file->streams[pkt->stream_index]; if (cs->out_stream_index < 0) { av_packet_unref(pkt); continue; } - if ((ret = picture_before_inpoint(avf, pkt))) { + if ((ret = picture_before_inpoint(avf, pkt)) || (ret = cut_subtitle(cat, pkt))) { av_packet_unref(pkt); if (ret < 0) return ret; @@ -1065,6 +1491,12 @@ avformat_close_input(&cur_avf_saved); } cat->eof = 0; + /* a seek of the list itself: its file is read from there on as it is */ + avpriv_packet_list_free(&cat->queue); + cat->cur_file->ended = cat->cur_file->finished = 0; + cat->cur_file->ready = cat->cur_file->started = 1; + cat->cur_file->hunt = 0; + cat->cur_file->lead = AV_NOPTS_VALUE; } return ret; }