Browse code

movtextenc: 3GPP TS 26.245 Timed Text Encoder.

This change introduces a basic encoder for 3GPP Timed Text subtitles,
also known as TX3G, Quicktime subtitles, or "movtext" in the existing
code.

This initial change doesn't attempt to write styling information,
and just writes the plain text of the subtitles. I intend to add
support for styles eventually, but it's challenging due to a lack
of existing players that support them.

Note that an additional change is required to the mov/mp4 muxer to
write empty subtitle packets to indicate subtitle duration.

Signed-off-by: Philip Langdale <philipl@overt.org>

Philip Langdale authored on 2012/06/07 01:12:24
Showing 8 changed files
... ...
@@ -30,7 +30,7 @@ version next:
30 30
 - iec61883 device
31 31
 - asettb filter
32 32
 - new option: -progress
33
-- 3GPP Timed Text decoder
33
+- 3GPP Timed Text encoder/decoder
34 34
 - GeoTIFF decoder support
35 35
 - ffmpeg -(no)stdin option
36 36
 - Opus decoder using libopus
... ...
@@ -887,7 +887,7 @@ performance on systems without hardware floating point support).
887 887
 @item SAMI             @tab   @tab X @tab   @tab X
888 888
 @item SubRip (SRT)     @tab X @tab X @tab X @tab X
889 889
 @item SubViewer        @tab   @tab X @tab   @tab X
890
-@item 3GPP Timed Text  @tab   @tab   @tab   @tab X
890
+@item 3GPP Timed Text  @tab   @tab   @tab X @tab X
891 891
 @item XSUB             @tab   @tab   @tab X @tab X
892 892
 @end multitable
893 893
 
... ...
@@ -268,6 +268,7 @@ OBJS-$(CONFIG_MLP_DECODER)             += mlpdec.o mlpdsp.o
268 268
 OBJS-$(CONFIG_MMVIDEO_DECODER)         += mmvideo.o
269 269
 OBJS-$(CONFIG_MOTIONPIXELS_DECODER)    += motionpixels.o
270 270
 OBJS-$(CONFIG_MOVTEXT_DECODER)         += movtextdec.o ass.o
271
+OBJS-$(CONFIG_MOVTEXT_ENCODER)         += movtextenc.o ass_split.o
271 272
 OBJS-$(CONFIG_MP1_DECODER)             += mpegaudiodec.o mpegaudiodecheader.o \
272 273
                                           mpegaudio.o mpegaudiodata.o
273 274
 OBJS-$(CONFIG_MP1FLOAT_DECODER)        += mpegaudiodec_float.o mpegaudiodecheader.o \
... ...
@@ -410,7 +410,7 @@ void avcodec_register_all(void)
410 410
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
411 411
     REGISTER_DECODER (JACOSUB, jacosub);
412 412
     REGISTER_DECODER (MICRODVD, microdvd);
413
-    REGISTER_DECODER (MOVTEXT, movtext);
413
+    REGISTER_ENCDEC  (MOVTEXT, movtext);
414 414
     REGISTER_DECODER (PGSSUB, pgssub);
415 415
     REGISTER_DECODER (REALTEXT, realtext);
416 416
     REGISTER_DECODER (SAMI, sami);
417 417
new file mode 100644
... ...
@@ -0,0 +1,162 @@
0
+/*
1
+ * 3GPP TS 26.245 Timed Text encoder
2
+ * Copyright (c) 2012  Philip Langdale <philipl@overt.org>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <stdarg.h>
22
+#include "avcodec.h"
23
+#include "movtext.h"
24
+#include "libavutil/avstring.h"
25
+#include "ass_split.h"
26
+#include "ass.h"
27
+
28
+typedef struct {
29
+    ASSSplitContext *ass_ctx;
30
+    char buffer[2048];
31
+    char *ptr;
32
+    char *end;
33
+} MovTextContext;
34
+
35
+
36
+static av_cold int mov_text_encode_init(AVCodecContext *avctx)
37
+{
38
+    /*
39
+     * For now, we'll use a fixed default style. When we add styling
40
+     * support, this will be generated from the ASS style.
41
+     */
42
+    static uint8_t text_sample_entry[] = {
43
+        0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
44
+        0x01,                   // int8_t horizontal-justification
45
+        0xFF,                   // int8_t vertical-justification
46
+        0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
47
+        // BoxRecord {
48
+        0x00, 0x00,             // int16_t top
49
+        0x00, 0x00,             // int16_t left
50
+        0x00, 0x00,             // int16_t bottom
51
+        0x00, 0x00,             // int16_t right
52
+        // };
53
+        // StyleRecord {
54
+        0x00, 0x00,             // uint16_t startChar
55
+        0x00, 0x00,             // uint16_t endChar
56
+        0x00, 0x01,             // uint16_t font-ID
57
+        0x00,                   // uint8_t face-style-flags
58
+        0x12,                   // uint8_t font-size
59
+        0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
60
+        // };
61
+        // FontTableBox {
62
+        0x00, 0x00, 0x00, 0x12, // uint32_t size
63
+        'f', 't', 'a', 'b',     // uint8_t name[4]
64
+        0x00, 0x01,             // uint16_t entry-count
65
+        // FontRecord {
66
+        0x00, 0x01,             // uint16_t font-ID
67
+        0x05,                   // uint8_t font-name-length
68
+        'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
69
+        // };
70
+        // };
71
+    };
72
+
73
+    MovTextContext *s = avctx->priv_data;
74
+
75
+    avctx->extradata_size = sizeof text_sample_entry;
76
+    avctx->extradata = av_mallocz(avctx->extradata_size);
77
+    if (!avctx->extradata)
78
+        return AVERROR(ENOMEM);
79
+
80
+    memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size);
81
+
82
+    s->ass_ctx = ff_ass_split(avctx->subtitle_header);
83
+    return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
84
+}
85
+
86
+static void mov_text_text_cb(void *priv, const char *text, int len)
87
+{
88
+    MovTextContext *s = priv;
89
+    av_strlcpy(s->ptr, text, FFMIN(s->end - s->ptr, len + 1));
90
+    s->ptr += len;
91
+}
92
+
93
+static void mov_text_new_line_cb(void *priv, int forced)
94
+{
95
+    MovTextContext *s = priv;
96
+    av_strlcpy(s->ptr, "\n", FFMIN(s->end - s->ptr, 2));
97
+    s->ptr++;
98
+}
99
+
100
+static const ASSCodesCallbacks mov_text_callbacks = {
101
+    .text     = mov_text_text_cb,
102
+    .new_line = mov_text_new_line_cb,
103
+};
104
+
105
+static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
106
+                                 int bufsize, void *data)
107
+{
108
+    MovTextContext *s = avctx->priv_data;
109
+    AVSubtitle *sub = data;
110
+    ASSDialog *dialog;
111
+    int i, len, num;
112
+
113
+    s->ptr = s->buffer;
114
+    s->end = s->ptr + sizeof(s->buffer);
115
+
116
+    for (i = 0; i < sub->num_rects; i++) {
117
+
118
+        if (sub->rects[i]->type != SUBTITLE_ASS) {
119
+            av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
120
+            return AVERROR(ENOSYS);
121
+        }
122
+
123
+        dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
124
+        for (; dialog && num--; dialog++) {
125
+            ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
126
+        }
127
+    }
128
+
129
+    if (s->ptr == s->buffer)
130
+        return 0;
131
+
132
+    AV_WB16(buf, strlen(s->buffer));
133
+    buf += 2;
134
+
135
+    len = av_strlcpy(buf, s->buffer, bufsize - 2);
136
+
137
+    if (len > bufsize-3) {
138
+        av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
139
+        return AVERROR(EINVAL);
140
+    }
141
+
142
+    return len + 2;
143
+}
144
+
145
+static int mov_text_encode_close(AVCodecContext *avctx)
146
+{
147
+    MovTextContext *s = avctx->priv_data;
148
+    ff_ass_split_free(s->ass_ctx);
149
+    return 0;
150
+}
151
+
152
+AVCodec ff_movtext_encoder = {
153
+    .name           = "mov_text",
154
+    .long_name      = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
155
+    .type           = AVMEDIA_TYPE_SUBTITLE,
156
+    .id             = CODEC_ID_MOV_TEXT,
157
+    .priv_data_size = sizeof(MovTextContext),
158
+    .init           = mov_text_encode_init,
159
+    .encode         = mov_text_encode_frame,
160
+    .close          = mov_text_encode_close,
161
+};
... ...
@@ -27,7 +27,7 @@
27 27
  */
28 28
 
29 29
 #define LIBAVCODEC_VERSION_MAJOR 54
30
-#define LIBAVCODEC_VERSION_MINOR 48
30
+#define LIBAVCODEC_VERSION_MINOR 49
31 31
 #define LIBAVCODEC_VERSION_MICRO 100
32 32
 
33 33
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -7,6 +7,9 @@ fate-sub-microdvd: CMD = md5 -i $(SAMPLES)/sub/MicroDVD_capability_tester.sub -f
7 7
 FATE_SUBTITLES += fate-sub-movtext
8 8
 fate-sub-movtext: CMD = md5 -i $(SAMPLES)/sub/MovText_capability_tester.mp4 -f ass
9 9
 
10
+FATE_SUBTITLES += fate-sub-movtextenc
11
+fate-sub-movtextenc: CMD = md5 -i $(SAMPLES)/sub/MovText_capability_tester.mp4 -map 0 -scodec mov_text -f mp4 -movflags frag_keyframe+empty_moov
12
+
10 13
 FATE_SUBTITLES += fate-sub-realtext
11 14
 fate-sub-realtext: CMD = md5 -i $(SAMPLES)/sub/RealText_capability_tester.rt -f ass
12 15
 
13 16
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+ea719ca95c36d0da638282ccd017dace