Browse code

lavc: add raw text subtitles decoder.

Clément Bœsch authored on 2012/10/14 10:08:30
Showing 5 changed files
... ...
@@ -13,6 +13,7 @@ version <next>:
13 13
 - remove -same_quant, it hasn't worked for years
14 14
 - X-Face image encoder and decoder
15 15
 - metadata (INFO tag) support in WAV muxer
16
+- subtitles raw text decoder
16 17
 
17 18
 
18 19
 version 1.0:
... ...
@@ -402,6 +402,7 @@ OBJS-$(CONFIG_SVQ3_DECODER)            += svq3.o svq13.o h263.o h264.o        \
402 402
                                           h264_loopfilter.o h264_direct.o     \
403 403
                                           h264_sei.o h264_ps.o h264_refs.o    \
404 404
                                           h264_cavlc.o h264_cabac.o cabac.o
405
+OBJS-$(CONFIG_TEXT_DECODER)            += textdec.o ass.o
405 406
 OBJS-$(CONFIG_TAK_DECODER)             += takdec.o tak.o
406 407
 OBJS-$(CONFIG_TARGA_DECODER)           += targa.o
407 408
 OBJS-$(CONFIG_TARGA_ENCODER)           += targaenc.o rle.o
... ...
@@ -425,6 +425,7 @@ void avcodec_register_all(void)
425 425
     REGISTER_ENCDEC  (SRT, srt);
426 426
     REGISTER_ENCDEC  (SUBRIP, subrip);
427 427
     REGISTER_DECODER (SUBVIEWER, subviewer);
428
+    REGISTER_DECODER (TEXT, text);
428 429
     REGISTER_DECODER (WEBVTT, webvtt);
429 430
     REGISTER_ENCDEC  (XSUB, xsub);
430 431
 
431 432
new file mode 100644
... ...
@@ -0,0 +1,125 @@
0
+/*
1
+ * Copyright (c) 2012 Clément Bœsch
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Raw subtitles decoder
23
+ */
24
+
25
+#include "avcodec.h"
26
+#include "ass.h"
27
+#include "libavutil/bprint.h"
28
+#include "libavutil/opt.h"
29
+
30
+typedef struct {
31
+    AVClass *class;
32
+    char *linebreaks;
33
+    int keep_ass_markup;
34
+} TextContext;
35
+
36
+#define OFFSET(x) offsetof(TextContext, x)
37
+#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
38
+static const AVOption options[] = {
39
+    { "linebreaks",      "Extra line breaks characters",    OFFSET(linebreaks),      AV_OPT_TYPE_STRING, {.str=NULL},    .flags=SD },
40
+    { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_INT,    {.i64=0}, 0, 1, .flags=SD },
41
+    { NULL }
42
+};
43
+
44
+static const AVClass text_decoder_class = {
45
+    .class_name = "text decoder",
46
+    .item_name  = av_default_item_name,
47
+    .option     = options,
48
+    .version    = LIBAVUTIL_VERSION_INT,
49
+};
50
+
51
+static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
52
+                             const char *p, const char *p_end)
53
+{
54
+    const TextContext *text = avctx->priv_data;
55
+
56
+    for (; p < p_end && *p; p++) {
57
+
58
+        /* forced custom line breaks, not accounted as "normal" EOL */
59
+        if (text->linebreaks && strchr(text->linebreaks, *p)) {
60
+            av_bprintf(buf, "\\N");
61
+
62
+        /* standard ASS escaping so random characters don't get mis-interpreted
63
+         * as ASS */
64
+        } else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
65
+            av_bprintf(buf, "\\%c", *p);
66
+
67
+        /* some packets might end abruptly (no \0 at the end, like for example
68
+         * in some cases of demuxing from a classic video container), some
69
+         * might be terminated with \n or \r\n which we have to remove (for
70
+         * consistency with those who haven't), and we also have to deal with
71
+         * evil cases such as \r at the end of the buffer (and no \0 terminated
72
+         * character) */
73
+        } else if (p[0] == '\n') {
74
+            /* some stuff left so we can insert a line break */
75
+            if (p < p_end - 1)
76
+                av_bprintf(buf, "\\N");
77
+        } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
78
+            /* \r followed by a \n, we can skip it. We don't insert the \N yet
79
+             * because we don't know if it is followed by more text */
80
+            continue;
81
+
82
+        /* finally, a sane character */
83
+        } else {
84
+            av_bprint_chars(buf, *p, 1);
85
+        }
86
+    }
87
+    av_bprintf(buf, "\r\n");
88
+    return 0;
89
+}
90
+
91
+static int text_decode_frame(AVCodecContext *avctx, void *data,
92
+                             int *got_sub_ptr, AVPacket *avpkt)
93
+{
94
+    AVBPrint buf;
95
+    AVSubtitle *sub = data;
96
+    const char *ptr = avpkt->data;
97
+    const int ts_start     = av_rescale_q(avpkt->pts,      avctx->time_base, (AVRational){1,100});
98
+    const int ts_duration  = avpkt->duration != -1 ?
99
+                             av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
100
+
101
+    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
102
+    if (ptr && avpkt->size > 0 && *ptr &&
103
+        !text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) {
104
+        if (!av_bprint_is_complete(&buf)) {
105
+            av_bprint_finalize(&buf, NULL);
106
+            return AVERROR(ENOMEM);
107
+        }
108
+        ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
109
+    }
110
+    *got_sub_ptr = sub->num_rects > 0;
111
+    av_bprint_finalize(&buf, NULL);
112
+    return avpkt->size;
113
+}
114
+
115
+AVCodec ff_text_decoder = {
116
+    .name           = "text",
117
+    .priv_data_size = sizeof(TextContext),
118
+    .long_name      = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
119
+    .type           = AVMEDIA_TYPE_SUBTITLE,
120
+    .id             = AV_CODEC_ID_TEXT,
121
+    .decode         = text_decode_frame,
122
+    .init           = ff_ass_subtitle_header_default,
123
+    .priv_class     = &text_decoder_class,
124
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 54
32
-#define LIBAVCODEC_VERSION_MINOR 66
32
+#define LIBAVCODEC_VERSION_MINOR 67
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \