[FFmpeg-devel] [PATCH] lavc: add text encoder

Clément Bœsch u at pkh.me
Thu Dec 17 12:30:27 CET 2015


---
 libavcodec/Makefile        |   1 +
 libavcodec/allcodecs.c     |   2 +-
 libavcodec/srtenc.c        |  37 +++++++-
 libavcodec/version.h       |   2 +-
 tests/fate/subtitles.mak   |   3 +
 tests/ref/fate/sub-textenc | 213 +++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 253 insertions(+), 5 deletions(-)
 create mode 100644 tests/ref/fate/sub-textenc

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index c169135..24de0c1 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -497,6 +497,7 @@ OBJS-$(CONFIG_SVQ1_ENCODER)            += svq1enc.o svq1.o    \
                                           h263.o ituh263enc.o
 OBJS-$(CONFIG_SVQ3_DECODER)            += svq3.o svq13.o mpegutils.o
 OBJS-$(CONFIG_TEXT_DECODER)            += textdec.o ass.o
+OBJS-$(CONFIG_TEXT_ENCODER)            += srtenc.o ass_split.o
 OBJS-$(CONFIG_TAK_DECODER)             += takdec.o tak.o takdsp.o
 OBJS-$(CONFIG_TARGA_DECODER)           += targa.o
 OBJS-$(CONFIG_TARGA_ENCODER)           += targaenc.o rle.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 245332e..6361a38 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -550,7 +550,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC (SUBRIP,            subrip);
     REGISTER_DECODER(SUBVIEWER,         subviewer);
     REGISTER_DECODER(SUBVIEWER1,        subviewer1);
-    REGISTER_DECODER(TEXT,              text);
+    REGISTER_ENCDEC (TEXT,              text);
     REGISTER_DECODER(VPLAYER,           vplayer);
     REGISTER_ENCDEC (WEBVTT,            webvtt);
     REGISTER_ENCDEC (XSUB,              xsub);
diff --git a/libavcodec/srtenc.c b/libavcodec/srtenc.c
index 3287970..0a6875a 100644
--- a/libavcodec/srtenc.c
+++ b/libavcodec/srtenc.c
@@ -221,8 +221,14 @@ static const ASSCodesCallbacks srt_callbacks = {
     .end              = srt_end_cb,
 };
 
-static int srt_encode_frame(AVCodecContext *avctx,
-                            unsigned char *buf, int bufsize, const AVSubtitle *sub)
+static const ASSCodesCallbacks text_callbacks = {
+    .text             = srt_text_cb,
+    .new_line         = srt_new_line_cb,
+};
+
+static int encode_frame(AVCodecContext *avctx,
+                        unsigned char *buf, int bufsize, const AVSubtitle *sub,
+                        const ASSCodesCallbacks *cb)
 {
     SRTContext *s = avctx->priv_data;
     ASSDialog *dialog;
@@ -241,7 +247,7 @@ static int srt_encode_frame(AVCodecContext *avctx,
         for (; dialog && num--; dialog++) {
             s->alignment_applied = 0;
             srt_style_apply(s, dialog->style);
-            ff_ass_split_override_codes(&srt_callbacks, s, dialog->text);
+            ff_ass_split_override_codes(cb, s, dialog->text);
         }
     }
 
@@ -259,6 +265,18 @@ static int srt_encode_frame(AVCodecContext *avctx,
     return s->buffer.len;
 }
 
+static int srt_encode_frame(AVCodecContext *avctx,
+                               unsigned char *buf, int bufsize, const AVSubtitle *sub)
+{
+    return encode_frame(avctx, buf, bufsize, sub, &srt_callbacks);
+}
+
+static int text_encode_frame(AVCodecContext *avctx,
+                             unsigned char *buf, int bufsize, const AVSubtitle *sub)
+{
+    return encode_frame(avctx, buf, bufsize, sub, &text_callbacks);
+}
+
 static int srt_encode_close(AVCodecContext *avctx)
 {
     SRTContext *s = avctx->priv_data;
@@ -293,3 +311,16 @@ AVCodec ff_subrip_encoder = {
     .close          = srt_encode_close,
 };
 #endif
+
+#if CONFIG_TEXT_ENCODER
+AVCodec ff_text_encoder = {
+    .name           = "text",
+    .long_name      = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
+    .type           = AVMEDIA_TYPE_SUBTITLE,
+    .id             = AV_CODEC_ID_TEXT,
+    .priv_data_size = sizeof(SRTContext),
+    .init           = srt_encode_init,
+    .encode_sub     = text_encode_frame,
+    .close          = srt_encode_close,
+};
+#endif
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 3027853..6f88ee4 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR  57
-#define LIBAVCODEC_VERSION_MINOR  17
+#define LIBAVCODEC_VERSION_MINOR  18
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/tests/fate/subtitles.mak b/tests/fate/subtitles.mak
index 2d9eb50..25914da 100644
--- a/tests/fate/subtitles.mak
+++ b/tests/fate/subtitles.mak
@@ -76,6 +76,9 @@ fate-sub-webvtt2: CMD = fmtstdout ass -i $(TARGET_SAMPLES)/sub/WebVTT_extended_t
 FATE_SUBTITLES-$(call ALLYES, SRT_DEMUXER SUBRIP_DECODER WEBVTT_ENCODER WEBVTT_MUXER) += fate-sub-webvttenc
 fate-sub-webvttenc: CMD = fmtstdout webvtt -i $(TARGET_SAMPLES)/sub/SubRip_capability_tester.srt
 
+FATE_SUBTITLES-$(call ALLYES, SRT_DEMUXER SUBRIP_DECODER TEXT_ENCODER SRT_MUXER) += fate-sub-textenc
+fate-sub-textenc: CMD = fmtstdout srt -c:s text -i $(TARGET_SAMPLES)/sub/SubRip_capability_tester.srt
+
 FATE_SUBTITLES_ASS-$(call ALLYES, MICRODVD_DEMUXER MICRODVD_DECODER ICONV) += fate-sub-charenc
 fate-sub-charenc: CMD = fmtstdout ass -sub_charenc cp1251 -i $(TARGET_SAMPLES)/sub/cp1251-subtitles.sub
 
diff --git a/tests/ref/fate/sub-textenc b/tests/ref/fate/sub-textenc
new file mode 100644
index 0000000..8cf0409
--- /dev/null
+++ b/tests/ref/fate/sub-textenc
@@ -0,0 +1,213 @@
+1
+00:00:00,000 --> 00:00:00,000
+Don't show this text it may be used to insert hidden data
+
+2
+00:00:01,500 --> 00:00:04,500
+SubRip subtitles capability tester 1.3o by ale5000
+<b><i>Use VLC 1.1 or higher as reference for most things and MPC Home Cinema for others</i></b>
+<font color="#0000FF">This text should be blue</font>
+<font color="red">This text should be red</font>
+<font color="#000000">This text should be black</font>
+<font face="Webdings">If you see this with the normal font, the player don't (fully) support font face</font>
+
+3
+00:00:04,500 --> 00:00:04,500
+Hidden
+
+4
+00:00:04,501 --> 00:00:07,501
+<font size="8">This text should be small</font>
+This text should be normal
+<font size="35">This text should be big</font>
+
+5
+00:00:07,501 --> 00:00:11,501
+This should be an E with an accent: È
+日本語
+<font size=30><b><i><u>This text should be bold, italics and underline</u></i></b></font>
+<font size=9 color="00ff00">This text should be small and green</font>
+<font color=#ff0000 size=9>This text should be small and red</font>
+<font color=brown size=24>This text should be big and brown</font>
+
+6
+00:00:11,501 --> 00:00:14,501
+<b>This line should be bold</b>
+<i>This line should be italics</i>
+<u>This line should be underline</u>
+<s>This line should be strikethrough</s>
+<u>Both lines
+should be underline</u>
+
+7
+00:00:14,501 --> 00:00:17,501
+>
+It would be a good thing to
+<invalid_tag>hide invalid html tags that are closed and show the text in them</invalid_tag>
+<invalid_tag_unclosed>but show un-closed invalid html tags
+Show not opened tags</invalid_tag_not_opened>
+<
+
+8
+00:00:17,501 --> 00:00:20,501
+and also
+<invalid_tag par=5>hide invalid html tags with parameters that are closed and show the text in them</invalid_tag>
+<invalid_tag_uc par=5>but show un-closed invalid html tags
+<u>This text should be showed underlined without problems also: 2<3,5>1,4<6</u>
+This shouldn't be underlined
+
+9
+00:00:20,501 --> 00:00:21,501
+This text should be in the normal position...
+
+10
+00:00:21,501 --> 00:00:22,501
+This text should NOT be in the normal position
+
+11
+00:00:22,501 --> 00:00:24,501
+Implementation is the same of the ASS tag
+\{\an8}This text should be at the
+top and horizontally centered
+
+12
+00:00:22,501 --> 00:00:24,501
+\{\an5}This text should be at the
+middle and horizontally centered
+
+13
+00:00:22,501 --> 00:00:24,501
+\{\an2}This text should be at the
+bottom and horizontally centered
+
+14
+00:00:24,501 --> 00:00:26,501
+This text should be at the
+top and horizontally at the left\{\an7}
+
+15
+00:00:24,501 --> 00:00:26,501
+\{\an4}This text should be at the
+middle and horiz\ontally at the left
+(The second position must be ignored)
+
+16
+00:00:24,501 --> 00:00:26,501
+\{\an1}This text should be at the
+bottom and horizontally at the left
+
+17
+00:00:26,501 --> 00:00:28,501
+\{\an9}This text should be at the
+top and horizontally at the right
+
+18
+00:00:26,501 --> 00:00:28,501
+\{\an6}This text should be at the
+middle and horizontally at the right
+
+19
+00:00:26,501 --> 00:00:28,501
+\{\an3}This text should be at the
+bottom and horizontally at the right
+
+20
+00:00:28,501 --> 00:00:31,501
+<font color="#00FF00" size="6">This could be the <font size="35">m<font color="#000000">o</font>st</font> difficult thing to implement</font>
+
+21
+00:00:31,501 --> 00:00:50,501
+First text
+
+22
+00:00:33,500 --> 00:00:35,500
+Second, it shouldn't overlap first
+
+23
+00:00:35,501 --> 00:00:37,501
+Third, it should replace second
+
+24
+00:00:36,501 --> 00:00:50,501
+Fourth, it shouldn't overlap first and third
+
+25
+00:00:40,501 --> 00:00:45,501
+Fifth, it should replace third
+
+26
+00:00:45,501 --> 00:00:50,501
+Sixth, it shouldn't be
+showed overlapped
+
+27
+00:00:50,501 --> 00:00:52,501
+TEXT 1 (bottom)
+
+28
+00:00:50,501 --> 00:00:52,501
+text 2
+
+29
+00:00:52,501 --> 00:00:54,501
+Hide these tags: \
+also hide these tags: \{Y:some_letters_or_numbers_or_chars\}
+but show this: \{normal text\}
+
+30
+00:00:54,501 --> 00:01:00,501
+\{\an8}
+\\ N is a forced line break
+\\ h is a hard space
+Normal spaces at the start and at the end of the line are trimmed while hard spaces are not trimmed.
+The\\hline\\hwill\\hnever\\hbreak\\hautomatically\\hright\\hbefore\\hor\\hafter\\ha\\hhard\\hspace.\\h:-D
+
+31
+00:00:54,501 --> 00:00:56,501
+\{\an1}
+\\h\\h\\h\\h\\hA (05 hard spaces followed by a letter)
+     A (Normal  spaces followed by a letter)
+A (No hard spaces followed by a letter)
+
+32
+00:00:56,501 --> 00:00:58,501
+\\h\\h\\h\\h\\hA (05 hard spaces followed by a letter)
+     A (Normal  spaces followed by a letter)
+A (No hard spaces followed by a letter)
+Show this: \\TEST and this: \\-)
+
+33
+00:00:58,501 --> 00:01:00,501
+\{\an3}
+A letter followed by 05 hard spaces: A\\h\\h\\h\\h\\h
+A letter followed by normal  spaces: A     
+A letter followed by no hard spaces: A
+05 hard  spaces between letters: A\\h\\h\\h\\h\\hA
+5 normal spaces between letters: A     A
+\
+^--Forced line break
+
+34
+00:01:00,501 --> 00:01:02,501
+<s>Both line should be strikethrough,
+yes.</s>
+<wwww>Correctly closed tags
+should be hidden.</wwww>
+
+35
+00:01:02,501 --> 00:01:04,501
+It shouldn't be strikethrough,
+not opened tag showed as text.</s>
+Not opened tag showed as text.</xxxxx>
+
+36
+00:01:04,501 --> 00:01:06,501
+<s>Three lines should be strikethrough,
+yes.
+<yyyy>Not closed tags showed as text
+
+37
+00:01:06,501 --> 00:01:08,501
+<s>Both line should be strikethrough but
+the wrong closing tag should be showed</b>
+
-- 
2.6.4



More information about the ffmpeg-devel mailing list