[PATCH] avformat/segment: segment subtitle streams
From: Aman Gupta <ffmpeg@tmm1.net> Allows subtitle streams to be segmented on pts timestamps. This can be used, for example, to generate segmented wevtt subtitle tracks for HLS streaming: ffmpeg -i input.srt -c:s webvtt -f segment -segment_time 10 out%5d.vtt Signed-off-by: Aman Gupta <ffmpeg@tmm1.net> --- libavformat/segment.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index fe84f27..4a8d4d2 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -685,7 +685,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt) pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1); if (pkt->stream_index == seg->reference_stream_index && - pkt->flags & AV_PKT_FLAG_KEY && + (pkt->flags & AV_PKT_FLAG_KEY || st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) && (seg->frame_count >= start_frame || (pkt->pts != AV_NOPTS_VALUE && av_compare_ts(pkt->pts, st->time_base, -- 1.9.1
On Sun, May 11, 2014 at 03:16:30PM -0700, ffmpeg@tmm1.net wrote:
From: Aman Gupta <ffmpeg@tmm1.net>
Allows subtitle streams to be segmented on pts timestamps. This can be used, for example, to generate segmented wevtt subtitle tracks for HLS streaming:
ffmpeg -i input.srt -c:s webvtt -f segment -segment_time 10 out%5d.vtt
Signed-off-by: Aman Gupta <ffmpeg@tmm1.net> --- libavformat/segment.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libavformat/segment.c b/libavformat/segment.c index fe84f27..4a8d4d2 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -685,7 +685,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt) pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1);
if (pkt->stream_index == seg->reference_stream_index && - pkt->flags & AV_PKT_FLAG_KEY && + (pkt->flags & AV_PKT_FLAG_KEY || st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) && (seg->frame_count >= start_frame || (pkt->pts != AV_NOPTS_VALUE && av_compare_ts(pkt->pts, st->time_base,
Unless I'm mistaken the AV_PKT_FLAG_KEY should be set for subtitles packets. See in ff_subtitles_queue_insert() -- Clément B.
Unless I'm mistaken the AV_PKT_FLAG_KEY should be set for subtitles packets. See in ff_subtitles_queue_insert()
You're right, ff_subtitles_queue_insert() is definitely adding the flag. But by the time the packet reaches the seg_write_packet(), the flag is missing. I verified this via the av_dlog in that function, which reports is_key:0 on all packets. I'm using the following command to test on master: ./ffmpeg -i fate-suite/sub/WebVTT_capability_tester.vtt -c:s srt -f segment -segment_time 1 /tmp/out%d.srt Only one output file is generated (instead of 16, one per second). I'm not familiar with the extra steps between webvttdec.c and segment.c where the flag might be getting lost. If someone can provide a pointer I'm happy to investigate further and come up with a better patch. Aman
Allows subtitle streams to be segmented on pts timestamps. After this patch ffmpeg can be used to generate segmented wevtt subtitle tracks for HLS streaming: ffmpeg -i input.srt -c:s webvtt -f segment -segment_time 10 out%5d.vtt Signed-off-by: Aman Gupta <ffmpeg@tmm1.net> --- ffmpeg.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ffmpeg.c b/ffmpeg.c index 5299f0e..6e743e2 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -821,6 +821,7 @@ static void do_subtitle_out(AVFormatContext *s, } av_init_packet(&pkt); + pkt.flags |= AV_PKT_FLAG_KEY; pkt.data = subtitle_out; pkt.size = subtitle_out_size; pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); -- 1.9.1
Le septidi 7 prairial, an CCXXII, Aman Gupta a écrit :
Allows subtitle streams to be segmented on pts timestamps. After this patch ffmpeg can be used to generate segmented wevtt subtitle tracks for HLS streaming:
ffmpeg -i input.srt -c:s webvtt -f segment -segment_time 10 out%5d.vtt
Signed-off-by: Aman Gupta <ffmpeg@tmm1.net> --- ffmpeg.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/ffmpeg.c b/ffmpeg.c index 5299f0e..6e743e2 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -821,6 +821,7 @@ static void do_subtitle_out(AVFormatContext *s, }
av_init_packet(&pkt); + pkt.flags |= AV_PKT_FLAG_KEY; pkt.data = subtitle_out; pkt.size = subtitle_out_size; pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
I am sorry, but this change seems wrong to me: if the flag is supposed to be present, that is not ffmpeg.c's task to set it. Otherwise, applications using the library directly would not get it. I believe we do not currently have subtitles encoders that use non-key-frames, although some formats would make it possible. In that case, I suppose the flag can be set in avcodec_encode_subtitle(). Regards, -- Nicolas George
On Mon, May 26, 2014 at 10:31:14AM +0200, Nicolas George wrote:
Le septidi 7 prairial, an CCXXII, Aman Gupta a écrit :
Allows subtitle streams to be segmented on pts timestamps. After this patch ffmpeg can be used to generate segmented wevtt subtitle tracks for HLS streaming:
ffmpeg -i input.srt -c:s webvtt -f segment -segment_time 10 out%5d.vtt
Signed-off-by: Aman Gupta <ffmpeg@tmm1.net> --- ffmpeg.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/ffmpeg.c b/ffmpeg.c index 5299f0e..6e743e2 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -821,6 +821,7 @@ static void do_subtitle_out(AVFormatContext *s, }
av_init_packet(&pkt); + pkt.flags |= AV_PKT_FLAG_KEY; pkt.data = subtitle_out; pkt.size = subtitle_out_size; pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
I am sorry, but this change seems wrong to me: if the flag is supposed to be present, that is not ffmpeg.c's task to set it. Otherwise, applications using the library directly would not get it.
I believe we do not currently have subtitles encoders that use non-key-frames, although some formats would make it possible. In that case,
I suppose the flag can be set in avcodec_encode_subtitle().
This would be the logical thing to do, sadly avcodec_encode_subtitle() doesnt have a AVPacket parameter like the audio/video encode functions so something like amans patch or a avcodec_encode_subtitle2() would probably be needed. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB it is not once nor twice but times without number that the same ideas make their appearance in the world. -- Aristotle
Took a stab at implementing avcodec_encode_subtitle2 which takes an AVPacket argument and sets AV_PKT_FLAG_KEY on the generated packets. Signed-off-by: Aman Gupta <ffmpeg@tmm1.net> --- configure | 1 + ffmpeg.c | 30 +++++++++++++----------------- libavcodec/avcodec.h | 1 + libavcodec/utils.c | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/configure b/configure index a261fd6..b346f9d 100755 --- a/configure +++ b/configure @@ -4896,6 +4896,7 @@ enabled neon_clobber_test && -Wl,--wrap,avcodec_encode_audio2 \ -Wl,--wrap,avcodec_encode_video2 \ -Wl,--wrap,avcodec_encode_subtitle \ + -Wl,--wrap,avcodec_encode_subtitle2 \ -Wl,--wrap,swr_convert \ -Wl,--wrap,avresample_convert || disable neon_clobber_test diff --git a/ffmpeg.c b/ffmpeg.c index a0e2be2..0b02e72 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -131,8 +131,6 @@ static int64_t decode_error_stat[2]; static int current_time; AVIOContext *progress_avio = NULL; -static uint8_t *subtitle_out; - #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass" InputStream **input_streams = NULL; @@ -452,8 +450,6 @@ static void ffmpeg_cleanup(int ret) } av_freep(&filtergraphs); - av_freep(&subtitle_out); - /* close files */ for (i = 0; i < nb_output_files; i++) { OutputFile *of = output_files[i]; @@ -763,8 +759,7 @@ static void do_subtitle_out(AVFormatContext *s, InputStream *ist, AVSubtitle *sub) { - int subtitle_out_max_size = 1024 * 1024; - int subtitle_out_size, nb, i; + int ret, nb, i; AVCodecContext *enc; AVPacket pkt; int64_t pts; @@ -778,10 +773,6 @@ static void do_subtitle_out(AVFormatContext *s, enc = ost->st->codec; - if (!subtitle_out) { - subtitle_out = av_malloc(subtitle_out_max_size); - } - /* Note: DVB subtitle need one packet to draw them and one other packet to clear them */ /* XXX: signal it in the codec context ? */ @@ -809,18 +800,19 @@ static void do_subtitle_out(AVFormatContext *s, ost->frames_encoded++; - subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out, - subtitle_out_max_size, sub); - if (subtitle_out_size < 0) { + av_init_packet(&pkt); + pkt.data = NULL; + pkt.size = 0; + ret = avcodec_encode_subtitle2(enc, &pkt, sub); + if (ret < 0) { av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n"); exit_program(1); } - av_init_packet(&pkt); - pkt.data = subtitle_out; - pkt.size = subtitle_out_size; pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base); + pkt.dts = pkt.pts; + if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) { /* XXX: the pts correction is handled here. Maybe handling it in the codec would be better */ @@ -829,8 +821,12 @@ static void do_subtitle_out(AVFormatContext *s, else pkt.pts += 90 * sub->end_display_time; } - pkt.dts = pkt.pts; + write_frame(s, &pkt, ost); + if (pkt.data) { + av_freep(pkt.data); + pkt.data = NULL; + } } } diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 5bff631..d5c89ba 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -4500,6 +4500,7 @@ int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub); +int avcodec_encode_subtitle2(AVCodecContext *avctx, AVPacket *avpkt, const AVSubtitle *sub); /** * @} diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 75e07dc..01c9843 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -2071,6 +2071,23 @@ int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, return ret; } +int avcodec_encode_subtitle2(AVCodecContext *avctx, AVPacket *avpkt, const AVSubtitle *sub) +{ + int ret; + int subtitle_out_max_size = 1024 * 1024; + uint8_t *buf = av_malloc(subtitle_out_max_size); + + ret = avcodec_encode_subtitle(avctx, buf, subtitle_out_max_size, sub); + if (ret < 0) + return ret; + + avpkt->flags = AV_PKT_FLAG_KEY; + avpkt->data = buf; + avpkt->size = ret; + + return ret; +} + /** * Attempt to guess proper monotonic timestamps for decoded video frames * which might have incorrect times. Input timestamps may wrap around, in -- 1.9.1
On Tue, May 27, 2014 at 05:21:49PM -0700, Aman Gupta wrote:
Took a stab at implementing avcodec_encode_subtitle2 which takes an AVPacket argument and sets AV_PKT_FLAG_KEY on the generated packets.
breaks fate tests --- ./tests/ref/fate/sub-movtextenc 2014-06-02 05:06:24.962255005 +0200 +++ tests/data/fate/sub-movtextenc 2014-06-02 05:10:49.078260569 +0200 @@ -1 +1 @@ -3c685e807d4961924d0abcc18b3f8fa8 +21167ec4e3327fd092d63f96c160a862 Test sub-movtextenc failed. Look at tests/data/fate/sub-movtextenc.err for details. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB It is dangerous to be right in matters on which the established authorities are wrong. -- Voltaire
On Tue, May 27, 2014 at 05:21:49PM -0700, Aman Gupta wrote:
Took a stab at implementing avcodec_encode_subtitle2 which takes an AVPacket argument and sets AV_PKT_FLAG_KEY on the generated packets.
Signed-off-by: Aman Gupta <ffmpeg@tmm1.net> --- configure | 1 + ffmpeg.c | 30 +++++++++++++----------------- libavcodec/avcodec.h | 1 + libavcodec/utils.c | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 17 deletions(-)
diff --git a/configure b/configure [...] +int avcodec_encode_subtitle2(AVCodecContext *avctx, AVPacket *avpkt, const AVSubtitle *sub) +{ + int ret; + int subtitle_out_max_size = 1024 * 1024; + uint8_t *buf = av_malloc(subtitle_out_max_size); + + ret = avcodec_encode_subtitle(avctx, buf, subtitle_out_max_size, sub); + if (ret < 0) + return ret; + + avpkt->flags = AV_PKT_FLAG_KEY; + avpkt->data = buf; + avpkt->size = ret; + + return ret; +} +
Thanks for the proposition but I definitely think this need more thinking. Adding a new encoding function just for a single flag but keeping all the current problems of the sub encoding API is not a good idea. First, you're now allocating a buffer for every subtitle. This doesn't sound good, it's a regression. We probably need some kind of AVBufferRef somehow that grows when necessary. Second, I'd say that subtitles encoders should be responsible for the allocation size they need (contrary to a/v we can't estimate the size in advance). This require changes in the encoders API and their usage. Third, the AVSubtitle should be moved and/or changed in libavutil. This is hard, but it needs to be done at some point. This new structure would be able to fix all the limitations we currently have, such as: - present in libavutil so it can be used in libavfilter - common markup for text subtitles (instead of the weird ASS hack) - flexible enough to support all kind of crazy bitmap subs - be associated with frames for closed captions? - being able to deal with subtitles replacing parts of the previous events, such as what we have with dvb subs Anyway, if you're willing to improve that subtitles API that is extremely welcome, but I believe this patch is more harmful than helpful since it pollutes the namespace with a near-to-useless hack improvement. -- Clément B.
Le septidi 7 prairial, an CCXXII, Michael Niedermayer a écrit :
This would be the logical thing to do, sadly avcodec_encode_subtitle() doesnt have a AVPacket parameter like the audio/video encode functions
so something like amans patch or a avcodec_encode_subtitle2() would probably be needed.
I forgot that issue with subtitles encoding. My objection is unfounded, of course, then. Regards, -- Nicolas George
participants (5)
-
Aman Gupta -
Clément Bœsch -
ffmpeg@tmm1.net -
Michael Niedermayer -
Nicolas George