Browse code

add SubRip encoder

Signed-off-by: Aurelien Jacobs <aurel@gnuage.org>

Aurelien Jacobs authored on 2010/09/14 12:55:12
Showing 9 changed files
... ...
@@ -69,7 +69,7 @@ version <next>:
69 69
 - replace the ocv_smooth filter with a more generic ocv filter
70 70
 - Windows Televison (WTV) demuxer
71 71
 - FFmpeg metadata format muxer and demuxer
72
-- SubRip (srt) subtitle decoder
72
+- SubRip (srt) subtitle encoder and decoder
73 73
 - floating-point AC-3 encoder added
74 74
 - Lagarith decoder
75 75
 - ffmpeg -copytb option added
... ...
@@ -198,7 +198,7 @@ Codecs:
198 198
   smc.c                                 Mike Melanson
199 199
   snow.c                                Michael Niedermayer, Loren Merritt
200 200
   sonic.c                               Alex Beregszaszi
201
-  srtdec.c                              Aurelien Jacobs
201
+  srt*                                  Aurelien Jacobs
202 202
   sunrast.c                             Ivo van Poorten
203 203
   svq3.c                                Michael Niedermayer
204 204
   targa.c                               Kostya Shishkov
... ...
@@ -700,7 +700,7 @@ performance on systems without hardware floating point support).
700 700
 @item DVB          @tab X @tab X @tab X @tab X
701 701
 @item DVD          @tab X @tab X @tab X @tab X
702 702
 @item PGS          @tab   @tab   @tab   @tab X
703
-@item SubRip (SRT) @tab X @tab X @tab   @tab X
703
+@item SubRip (SRT) @tab X @tab X @tab X @tab X
704 704
 @item XSUB         @tab   @tab   @tab X @tab X
705 705
 @end multitable
706 706
 
... ...
@@ -344,6 +344,7 @@ OBJS-$(CONFIG_SONIC_ENCODER)           += sonic.o
344 344
 OBJS-$(CONFIG_SONIC_LS_ENCODER)        += sonic.o
345 345
 OBJS-$(CONFIG_SP5X_DECODER)            += sp5xdec.o mjpegdec.o mjpeg.o
346 346
 OBJS-$(CONFIG_SRT_DECODER)             += srtdec.o ass.o
347
+OBJS-$(CONFIG_SRT_ENCODER)             += srtenc.o ass_split.o
347 348
 OBJS-$(CONFIG_SUNRAST_DECODER)         += sunrast.o
348 349
 OBJS-$(CONFIG_SVQ1_DECODER)            += svq1dec.o svq1.o h263.o \
349 350
                                           mpegvideo.o error_resilience.o
... ...
@@ -357,7 +357,7 @@ void avcodec_register_all(void)
357 357
     REGISTER_ENCDEC  (DVBSUB, dvbsub);
358 358
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
359 359
     REGISTER_DECODER (PGSSUB, pgssub);
360
-    REGISTER_DECODER (SRT, srt);
360
+    REGISTER_ENCDEC  (SRT, srt);
361 361
     REGISTER_ENCDEC  (XSUB, xsub);
362 362
 
363 363
     /* external libraries */
364 364
new file mode 100644
... ...
@@ -0,0 +1,468 @@
0
+/*
1
+ * SSA/ASS spliting functions
2
+ * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.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 "avcodec.h"
22
+#include "ass_split.h"
23
+
24
+typedef enum {
25
+    ASS_STR,
26
+    ASS_INT,
27
+    ASS_FLT,
28
+    ASS_COLOR,
29
+    ASS_TIMESTAMP,
30
+    ASS_ALGN,
31
+} ASSFieldType;
32
+
33
+typedef struct {
34
+    const char *name;
35
+    int type;
36
+    int offset;
37
+} ASSFields;
38
+
39
+typedef struct {
40
+    const char *section;
41
+    const char *format_header;
42
+    const char *fields_header;
43
+    int         size;
44
+    int         offset;
45
+    int         offset_count;
46
+    ASSFields   fields[10];
47
+} ASSSection;
48
+
49
+static const ASSSection ass_sections[] = {
50
+    { .section       = "Script Info",
51
+      .offset        = offsetof(ASS, script_info),
52
+      .fields = {{"ScriptType", ASS_STR, offsetof(ASSScriptInfo, script_type)},
53
+                 {"Collisions", ASS_STR, offsetof(ASSScriptInfo, collisions) },
54
+                 {"PlayResX",   ASS_INT, offsetof(ASSScriptInfo, play_res_x) },
55
+                 {"PlayResY",   ASS_INT, offsetof(ASSScriptInfo, play_res_y) },
56
+                 {"Timer",      ASS_FLT, offsetof(ASSScriptInfo, timer)      },
57
+                 {0},
58
+        }
59
+    },
60
+    { .section       = "V4+ Styles",
61
+      .format_header = "Format",
62
+      .fields_header = "Style",
63
+      .size          = sizeof(ASSStyle),
64
+      .offset        = offsetof(ASS, styles),
65
+      .offset_count  = offsetof(ASS, styles_count),
66
+      .fields = {{"Name",         ASS_STR,  offsetof(ASSStyle, name)         },
67
+                 {"Fontname",     ASS_STR,  offsetof(ASSStyle, font_name)    },
68
+                 {"Fontsize",     ASS_INT,  offsetof(ASSStyle, font_size)    },
69
+                 {"PrimaryColour",ASS_COLOR,offsetof(ASSStyle, primary_color)},
70
+                 {"BackColour",   ASS_COLOR,offsetof(ASSStyle, back_color)   },
71
+                 {"Bold",         ASS_INT,  offsetof(ASSStyle, bold)         },
72
+                 {"Italic",       ASS_INT,  offsetof(ASSStyle, italic)       },
73
+                 {"Underline",    ASS_INT,  offsetof(ASSStyle, underline)    },
74
+                 {"Alignment",    ASS_INT,  offsetof(ASSStyle, alignment)    },
75
+                 {0},
76
+        }
77
+    },
78
+    { .section       = "V4 Styles",
79
+      .format_header = "Format",
80
+      .fields_header = "Style",
81
+      .size          = sizeof(ASSStyle),
82
+      .offset        = offsetof(ASS, styles),
83
+      .offset_count  = offsetof(ASS, styles_count),
84
+      .fields = {{"Name",         ASS_STR,  offsetof(ASSStyle, name)         },
85
+                 {"Fontname",     ASS_STR,  offsetof(ASSStyle, font_name)    },
86
+                 {"Fontsize",     ASS_INT,  offsetof(ASSStyle, font_size)    },
87
+                 {"PrimaryColour",ASS_COLOR,offsetof(ASSStyle, primary_color)},
88
+                 {"BackColour",   ASS_COLOR,offsetof(ASSStyle, back_color)   },
89
+                 {"Bold",         ASS_INT,  offsetof(ASSStyle, bold)         },
90
+                 {"Italic",       ASS_INT,  offsetof(ASSStyle, italic)       },
91
+                 {"Alignment",    ASS_ALGN, offsetof(ASSStyle, alignment)    },
92
+                 {0},
93
+        }
94
+    },
95
+    { .section       = "Events",
96
+      .format_header = "Format",
97
+      .fields_header = "Dialogue",
98
+      .size          = sizeof(ASSDialog),
99
+      .offset        = offsetof(ASS, dialogs),
100
+      .offset_count  = offsetof(ASS, dialogs_count),
101
+      .fields = {{"Layer",  ASS_INT,        offsetof(ASSDialog, layer)       },
102
+                 {"Start",  ASS_TIMESTAMP,  offsetof(ASSDialog, start)       },
103
+                 {"End",    ASS_TIMESTAMP,  offsetof(ASSDialog, end)         },
104
+                 {"Style",  ASS_STR,        offsetof(ASSDialog, style)       },
105
+                 {"Text",   ASS_STR,        offsetof(ASSDialog, text)        },
106
+                 {0},
107
+        }
108
+    },
109
+};
110
+
111
+
112
+typedef int (*ASSConvertFunc)(void *dest, const char *buf, int len);
113
+
114
+static int convert_str(void *dest, const char *buf, int len)
115
+{
116
+    char *str = av_malloc(len + 1);
117
+    if (str) {
118
+        memcpy(str, buf, len);
119
+        str[len] = 0;
120
+        if (*(void **)dest)
121
+            av_free(*(void **)dest);
122
+        *(char **)dest = str;
123
+    }
124
+    return !str;
125
+}
126
+static int convert_int(void *dest, const char *buf, int len)
127
+{
128
+    return sscanf(buf, "%d", (int *)dest) == 1;
129
+}
130
+static int convert_flt(void *dest, const char *buf, int len)
131
+{
132
+    return sscanf(buf, "%f", (float *)dest) == 1;
133
+}
134
+static int convert_color(void *dest, const char *buf, int len)
135
+{
136
+    return sscanf(buf, "&H%8x", (int *)dest) == 1 ||
137
+           sscanf(buf, "%d",    (int *)dest) == 1;
138
+}
139
+static int convert_timestamp(void *dest, const char *buf, int len)
140
+{
141
+    int c, h, m, s, cs;
142
+    if ((c = sscanf(buf, "%d:%02d:%02d.%02d", &h, &m, &s, &cs)) == 4)
143
+        *(int *)dest = 360000*h + 6000*m + 100*s + cs;
144
+    return c == 4;
145
+}
146
+static int convert_alignment(void *dest, const char *buf, int len)
147
+{
148
+    int a;
149
+    if (sscanf(buf, "%d", &a) == 1) {
150
+        /* convert V4 Style alignment to V4+ Style */
151
+        *(int *)dest = a + ((a&4) >> 1) - 5*!!(a&8);
152
+        return 1;
153
+    }
154
+    return 0;
155
+}
156
+
157
+static const ASSConvertFunc convert_func[] = {
158
+    [ASS_STR]       = convert_str,
159
+    [ASS_INT]       = convert_int,
160
+    [ASS_FLT]       = convert_flt,
161
+    [ASS_COLOR]     = convert_color,
162
+    [ASS_TIMESTAMP] = convert_timestamp,
163
+    [ASS_ALGN]      = convert_alignment,
164
+};
165
+
166
+
167
+struct ASSSplitContext {
168
+    ASS ass;
169
+    int current_section;
170
+    int field_number[FF_ARRAY_ELEMS(ass_sections)];
171
+    int *field_order[FF_ARRAY_ELEMS(ass_sections)];
172
+};
173
+
174
+
175
+static uint8_t *realloc_section_array(ASSSplitContext *ctx)
176
+{
177
+    const ASSSection *section = &ass_sections[ctx->current_section];
178
+    int *count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
179
+    void **section_ptr = (void **)((uint8_t *)&ctx->ass + section->offset);
180
+    uint8_t *tmp = av_realloc(*section_ptr, (*count+1)*section->size);
181
+    if (!tmp)
182
+        return NULL;
183
+    *section_ptr = tmp;
184
+    tmp += *count * section->size;
185
+    memset(tmp, 0, section->size);
186
+    (*count)++;
187
+    return tmp;
188
+}
189
+
190
+static inline int is_eol(char buf)
191
+{
192
+    return buf == '\r' || buf == '\n' || buf == 0;
193
+}
194
+
195
+static inline const char *skip_space(const char *buf)
196
+{
197
+    while (*buf == ' ')
198
+        buf++;
199
+    return buf;
200
+}
201
+
202
+static const char *ass_split_section(ASSSplitContext *ctx, const char *buf)
203
+{
204
+    const ASSSection *section = &ass_sections[ctx->current_section];
205
+    int *number = &ctx->field_number[ctx->current_section];
206
+    int *order = ctx->field_order[ctx->current_section];
207
+    int *tmp, i, len;
208
+
209
+    while (buf && *buf) {
210
+        if (buf[0] == '[') {
211
+            ctx->current_section = -1;
212
+            break;
213
+        }
214
+        if (buf[0] == ';' || (buf[0] == '!' && buf[1] == ':')) {
215
+            /* skip comments */
216
+        } else if (section->format_header && !order) {
217
+            len = strlen(section->format_header);
218
+            if (strncmp(buf, section->format_header, len) || buf[len] != ':')
219
+                return NULL;
220
+            buf += len + 1;
221
+            while (!is_eol(*buf)) {
222
+                buf = skip_space(buf);
223
+                len = strcspn(buf, ", \r\n");
224
+                if (!(tmp = av_realloc(order, (*number + 1) * sizeof(*order))))
225
+                    return NULL;
226
+                order = tmp;
227
+                order[*number] = -1;
228
+                for (i=0; section->fields[i].name; i++)
229
+                    if (!strncmp(buf, section->fields[i].name, len)) {
230
+                        order[*number] = i;
231
+                        break;
232
+                    }
233
+                (*number)++;
234
+                buf = skip_space(buf + len + 1);
235
+            }
236
+            ctx->field_order[ctx->current_section] = order;
237
+        } else if (section->fields_header) {
238
+            len = strlen(section->fields_header);
239
+            if (!strncmp(buf, section->fields_header, len) && buf[len] == ':') {
240
+                uint8_t *ptr, *struct_ptr = realloc_section_array(ctx);
241
+                if (!struct_ptr)  return NULL;
242
+                buf += len + 1;
243
+                for (i=0; !is_eol(*buf) && i < *number; i++) {
244
+                    int last = i == *number - 1;
245
+                    buf = skip_space(buf);
246
+                    len = strcspn(buf, last ? "\r\n" : ",\r\n");
247
+                    if (order[i] >= 0) {
248
+                        ASSFieldType type = section->fields[order[i]].type;
249
+                        ptr = struct_ptr + section->fields[order[i]].offset;
250
+                        convert_func[type](ptr, buf, len);
251
+                    }
252
+                    buf = skip_space(buf + len + !last);
253
+                }
254
+            }
255
+        } else {
256
+            len = strcspn(buf, ":\r\n");
257
+            if (buf[len] == ':') {
258
+                for (i=0; section->fields[i].name; i++)
259
+                    if (!strncmp(buf, section->fields[i].name, len)) {
260
+                        ASSFieldType type = section->fields[i].type;
261
+                        uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
262
+                        ptr += section->fields[i].offset;
263
+                        buf = skip_space(buf + len + 1);
264
+                        convert_func[type](ptr, buf, strcspn(buf, "\r\n"));
265
+                        break;
266
+                    }
267
+            }
268
+        }
269
+        buf += strcspn(buf, "\n") + 1;
270
+    }
271
+    return buf;
272
+}
273
+
274
+static int ass_split(ASSSplitContext *ctx, const char *buf)
275
+{
276
+    char c, section[16];
277
+    int i;
278
+
279
+    if (ctx->current_section >= 0)
280
+        buf = ass_split_section(ctx, buf);
281
+
282
+    while (buf && *buf) {
283
+        if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
284
+            buf += strcspn(buf, "\n") + 1;
285
+            for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
286
+                if (!strcmp(section, ass_sections[i].section)) {
287
+                    ctx->current_section = i;
288
+                    buf = ass_split_section(ctx, buf);
289
+                }
290
+        } else
291
+            buf += strcspn(buf, "\n") + 1;
292
+    }
293
+    return buf ? 0 : AVERROR_INVALIDDATA;
294
+}
295
+
296
+ASSSplitContext *ff_ass_split(const char *buf)
297
+{
298
+    ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
299
+    ctx->current_section = -1;
300
+    if (ass_split(ctx, buf) < 0) {
301
+        ff_ass_split_free(ctx);
302
+        return NULL;
303
+    }
304
+    return ctx;
305
+}
306
+
307
+static void free_section(ASSSplitContext *ctx, const ASSSection *section)
308
+{
309
+    uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
310
+    int i, j, *count, c = 1;
311
+
312
+    if (section->format_header) {
313
+        ptr   = *(void **)ptr;
314
+        count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
315
+    } else
316
+        count = &c;
317
+
318
+    if (ptr)
319
+        for (i=0; i<*count; i++, ptr += section->size)
320
+            for (j=0; section->fields[j].name; j++) {
321
+                const ASSFields *field = &section->fields[j];
322
+                if (field->type == ASS_STR)
323
+                    av_freep(ptr + field->offset);
324
+            }
325
+    *count = 0;
326
+
327
+    if (section->format_header)
328
+        av_freep((uint8_t *)&ctx->ass + section->offset);
329
+}
330
+
331
+ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
332
+                               int cache, int *number)
333
+{
334
+    ASSDialog *dialog = NULL;
335
+    int i, count;
336
+    if (!cache)
337
+        for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
338
+            if (!strcmp(ass_sections[i].section, "Events")) {
339
+                free_section(ctx, &ass_sections[i]);
340
+                break;
341
+            }
342
+    count = ctx->ass.dialogs_count;
343
+    if (ass_split(ctx, buf) == 0)
344
+        dialog = ctx->ass.dialogs + count;
345
+    if (number)
346
+        *number = ctx->ass.dialogs_count - count;
347
+    return dialog;
348
+}
349
+
350
+void ff_ass_split_free(ASSSplitContext *ctx)
351
+{
352
+    if (ctx) {
353
+        int i;
354
+        for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
355
+            free_section(ctx, &ass_sections[i]);
356
+        av_free(ctx);
357
+    }
358
+}
359
+
360
+
361
+int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
362
+                                const char *buf)
363
+{
364
+    const char *text = NULL;
365
+    char new_line[2];
366
+    int text_len = 0;
367
+
368
+    while (*buf) {
369
+        if (text && callbacks->text &&
370
+            (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
371
+             !strncmp(buf, "{\\", 2))) {
372
+            callbacks->text(priv, text, text_len);
373
+            text = NULL;
374
+        }
375
+        if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
376
+            if (callbacks->new_line)
377
+                callbacks->new_line(priv, new_line[0] == 'N');
378
+            buf += 2;
379
+        } else if (!strncmp(buf, "{\\", 2)) {
380
+            buf++;
381
+            while (*buf == '\\') {
382
+                char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
383
+                unsigned int color = 0xFFFFFFFF;
384
+                int len, size = -1, an = -1, alpha = -1;
385
+                int x1, y1, x2, y2, t1 = -1, t2 = -1;
386
+                if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
387
+                    int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
388
+                    len += close != -1;
389
+                    if (callbacks->style)
390
+                        callbacks->style(priv, style[0], close);
391
+                } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
392
+                           sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
393
+                           sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
394
+                           sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
395
+                    if (callbacks->color)
396
+                        callbacks->color(priv, color, c_num[0] - '0');
397
+                } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
398
+                           sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
399
+                           sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
400
+                           sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
401
+                    if (callbacks->alpha)
402
+                        callbacks->alpha(priv, alpha, c_num[0] - '0');
403
+                } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
404
+                           sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
405
+                    if (callbacks->font_name)
406
+                        callbacks->font_name(priv, tmp[0] ? tmp : NULL);
407
+                } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
408
+                           sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
409
+                    if (callbacks->font_size)
410
+                        callbacks->font_size(priv, size);
411
+                } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
412
+                           sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
413
+                           sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
414
+                           sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
415
+                    if (an != -1 && buf[2] != 'n')
416
+                        an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
417
+                    if (callbacks->alignment)
418
+                        callbacks->alignment(priv, an);
419
+                } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
420
+                           sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
421
+                    if (callbacks->cancel_overrides)
422
+                        callbacks->cancel_overrides(priv, tmp);
423
+                } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
424
+                           sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
425
+                    if (callbacks->move)
426
+                        callbacks->move(priv, x1, y1, x2, y2, t1, t2);
427
+                } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
428
+                    if (callbacks->move)
429
+                        callbacks->move(priv, x1, y1, x1, y1, -1, -1);
430
+                } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
431
+                    if (callbacks->origin)
432
+                        callbacks->origin(priv, x1, y1);
433
+                } else {
434
+                    len = strcspn(buf+1, "\\}") + 2;  /* skip unknown code */
435
+                }
436
+                buf += len - 1;
437
+            }
438
+            if (*buf++ != '}')
439
+                return AVERROR_INVALIDDATA;
440
+        } else {
441
+            if (!text) {
442
+                text = buf;
443
+                text_len = 1;
444
+            } else
445
+                text_len++;
446
+            buf++;
447
+        }
448
+    }
449
+    if (text && callbacks->text)
450
+        callbacks->text(priv, text, text_len);
451
+    if (callbacks->end)
452
+        callbacks->end(priv);
453
+    return 0;
454
+}
455
+
456
+ASSStyle *ass_style_get(ASSSplitContext *ctx, const char *style)
457
+{
458
+    ASS *ass = &ctx->ass;
459
+    int i;
460
+
461
+    if (!style || !*style)
462
+        style = "Default";
463
+    for (i=0; i<ass->styles_count; i++)
464
+        if (!strcmp(ass->styles[i].name, style))
465
+            return ass->styles + i;
466
+    return NULL;
467
+}
0 468
new file mode 100644
... ...
@@ -0,0 +1,172 @@
0
+/*
1
+ * SSA/ASS spliting functions
2
+ * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.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
+#ifndef AVCODEC_ASS_SPLIT_H
22
+#define AVCODEC_ASS_SPLIT_H
23
+
24
+/**
25
+ * fields extracted from the [Script Info] section
26
+ */
27
+typedef struct {
28
+    char *script_type;    /**< SSA script format version (eg. v4.00) */
29
+    char *collisions;     /**< how subtitles are moved to prevent collisions */
30
+    int   play_res_x;     /**< video width that ASS coords are referring to */
31
+    int   play_res_y;     /**< video height that ASS coords are referring to */
32
+    float timer;          /**< time multiplier to apply to SSA clock (in %) */
33
+} ASSScriptInfo;
34
+
35
+/**
36
+ * fields extracted from the [V4(+) Styles] section
37
+ */
38
+typedef struct {
39
+    char *name;           /**< name of the tyle (case sensitive) */
40
+    char *font_name;      /**< font face (case sensitive) */
41
+    int   font_size;      /**< font height */
42
+    int   primary_color;  /**< color that a subtitle will normally appear in */
43
+    int   back_color;     /**< color of the subtitle outline or shadow */
44
+    int   bold;           /**< whether text is bold (1) or not (0) */
45
+    int   italic;         /**< whether text is italic (1) or not (0) */
46
+    int   underline;      /**< whether text is underlined (1) or not (0) */
47
+    int   alignment;      /**< position of the text (left, center, top...),
48
+                               defined after the layout of the numpad
49
+                               (1-3 sub, 4-6 mid, 7-9 top) */
50
+} ASSStyle;
51
+
52
+/**
53
+ * fields extracted from the [Events] section
54
+ */
55
+typedef struct {
56
+    int   layer;    /**< higher numbered layers are drawn over lower numbered */
57
+    int   start;    /**< start time of the dialog in centiseconds */
58
+    int   end;      /**< end time of the dialog in centiseconds */
59
+    char *style;    /**< name of the ASSStyle to use with this dialog */
60
+    char *text;     /**< actual text which will be displayed as a subtitle,
61
+                         can include style override control codes (see
62
+                         ff_ass_split_override_codes()) */
63
+} ASSDialog;
64
+
65
+/**
66
+ * structure containing the whole split ASS data
67
+ */
68
+typedef struct {
69
+    ASSScriptInfo script_info;   /**< general information about the SSA script*/
70
+    ASSStyle     *styles;        /**< array of split out styles */
71
+    int           styles_count;  /**< number of ASSStyle in the styles array */
72
+    ASSDialog    *dialogs;       /**< array of split out dialogs */
73
+    int           dialogs_count; /**< number of ASSDialog in the dialogs array*/
74
+} ASS;
75
+
76
+/**
77
+ * This struct can be casted to ASS to access to the split data.
78
+ */
79
+typedef struct ASSSplitContext ASSSplitContext;
80
+
81
+/**
82
+ * Split a full ASS file or a ASS header from a string buffer and store
83
+ * the split structure in a newly allocated context.
84
+ *
85
+ * @param buf String containing the ASS formated data.
86
+ * @return Newly allocated struct containing split data.
87
+ */
88
+ASSSplitContext *ff_ass_split(const char *buf);
89
+
90
+/**
91
+ * Split one or several ASS "Dialogue" lines from a string buffer and store
92
+ * them in a already initialized context.
93
+ *
94
+ * @param ctx Context previously initialized by ff_ass_split().
95
+ * @param buf String containing the ASS "Dialogue" lines.
96
+ * @param cache Set to 1 to keep all the previously split ASSDialog in
97
+ *              the context, or set to 0 to free all the previously split
98
+ *              ASSDialog.
99
+ * @param number If not NULL, the pointed integer will be set to the number
100
+ *               of split ASSDialog.
101
+ * @return Pointer to the first split ASSDialog.
102
+ */
103
+ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
104
+                               int cache, int *number);
105
+
106
+/**
107
+ * Free all the memory allocated for an ASSSplitContext.
108
+ *
109
+ * @param ctx Context previously initialized by ff_ass_split().
110
+ */
111
+void ff_ass_split_free(ASSSplitContext *ctx);
112
+
113
+
114
+/**
115
+ * Set of callback functions corresponding to each override codes that can
116
+ * be encountered in a "Dialogue" Text field.
117
+ */
118
+typedef struct {
119
+    /**
120
+     * @defgroup ass_styles    ASS styles
121
+     * @{
122
+     */
123
+    void (*text)(void *priv, const char *text, int len);
124
+    void (*new_line)(void *priv, int forced);
125
+    void (*style)(void *priv, char style, int close);
126
+    void (*color)(void *priv, unsigned int color, unsigned int color_id);
127
+    void (*alpha)(void *priv, int alpha, int alpha_id);
128
+    void (*font_name)(void *priv, const char *name);
129
+    void (*font_size)(void *priv, int size);
130
+    void (*alignment)(void *priv, int alignment);
131
+    void (*cancel_overrides)(void *priv, const char *style);
132
+    /**< @} */
133
+
134
+    /**
135
+     * @defgroup ass_functions    ASS functions
136
+     * @{
137
+     */
138
+    void (*move)(void *priv, int x1, int y1, int x2, int y2, int t1, int t2);
139
+    void (*origin)(void *priv, int x, int y);
140
+    /**< @} */
141
+
142
+    /**
143
+     * @defgroup ass_end    end of Dialogue Event
144
+     * @{
145
+     */
146
+    void (*end)(void *priv);
147
+    /**< @} */
148
+} ASSCodesCallbacks;
149
+
150
+/**
151
+ * Split override codes out of a ASS "Dialogue" Text field.
152
+ *
153
+ * @param callbacks Set of callback functions called for each override code
154
+ *                  encountered.
155
+ * @param priv Opaque pointer passed to the callback functions.
156
+ * @param buf The ASS "Dialogue" Text field to split.
157
+ * @return >= 0 on success otherwise an error code <0
158
+ */
159
+int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
160
+                                const char *buf);
161
+
162
+/**
163
+ * Find an ASSStyle structure by its name.
164
+ *
165
+ * @param ctx Context previously initialized by ff_ass_split().
166
+ * @param style name of the style to search for.
167
+ * @return the ASSStyle corresponding to style, or NULL if style can't be found
168
+ */
169
+ASSStyle *ass_style_get(ASSSplitContext *ctx, const char *style);
170
+
171
+#endif /* AVCODEC_ASS_SPLIT_H */
0 172
new file mode 100644
... ...
@@ -0,0 +1,301 @@
0
+/*
1
+ * SubRip subtitle encoder
2
+ * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.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 "libavutil/avstring.h"
24
+#include "ass_split.h"
25
+#include "ass.h"
26
+
27
+
28
+#define SRT_STACK_SIZE 64
29
+
30
+typedef struct {
31
+    AVCodecContext *avctx;
32
+    ASSSplitContext *ass_ctx;
33
+    char buffer[2048];
34
+    char *ptr;
35
+    char *end;
36
+    char *dialog_start;
37
+    int count;
38
+    char stack[SRT_STACK_SIZE];
39
+    int stack_ptr;
40
+    int alignment_applied;
41
+} SRTContext;
42
+
43
+
44
+#ifdef __GNUC__
45
+__attribute__ ((__format__ (__printf__, 2, 3)))
46
+#endif
47
+static void srt_print(SRTContext *s, const char *str, ...)
48
+{
49
+    va_list vargs;
50
+    va_start(vargs, str);
51
+    s->ptr += vsnprintf(s->ptr, s->end - s->ptr, str, vargs);
52
+    va_end(vargs);
53
+}
54
+
55
+static int srt_stack_push(SRTContext *s, const char c)
56
+{
57
+    if (s->stack_ptr >= SRT_STACK_SIZE)
58
+        return -1;
59
+    s->stack[s->stack_ptr++] = c;
60
+    return 0;
61
+}
62
+
63
+static char srt_stack_pop(SRTContext *s)
64
+{
65
+    if (s->stack_ptr <= 0)
66
+        return 0;
67
+    return s->stack[--s->stack_ptr];
68
+}
69
+
70
+static int srt_stack_find(SRTContext *s, const char c)
71
+{
72
+    int i;
73
+    for (i = s->stack_ptr-1; i >= 0; i--)
74
+        if (s->stack[i] == c)
75
+            break;
76
+    return i;
77
+}
78
+
79
+static void srt_close_tag(SRTContext *s, char tag)
80
+{
81
+    srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
82
+}
83
+
84
+static void srt_stack_push_pop(SRTContext *s, const char c, int close)
85
+{
86
+    if (close) {
87
+        int i = c ? srt_stack_find(s, c) : 0;
88
+        if (i < 0)
89
+            return;
90
+        while (s->stack_ptr != i)
91
+            srt_close_tag(s, srt_stack_pop(s));
92
+    } else if (srt_stack_push(s, c) < 0)
93
+        av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
94
+}
95
+
96
+static void srt_style_apply(SRTContext *s, const char *style)
97
+{
98
+    ASSStyle *st = ass_style_get(s->ass_ctx, style);
99
+    if (st) {
100
+        int c = st->primary_color & 0xFFFFFF;
101
+        if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
102
+            st->font_size != ASS_DEFAULT_FONT_SIZE ||
103
+            c != ASS_DEFAULT_COLOR) {
104
+            srt_print(s, "<font");
105
+            if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
106
+                srt_print(s, " face=\"%s\"", st->font_name);
107
+            if (st->font_size != ASS_DEFAULT_FONT_SIZE)
108
+                srt_print(s, " size=\"%d\"", st->font_size);
109
+            if (c != ASS_DEFAULT_COLOR)
110
+                srt_print(s, " color=\"#%06x\"",
111
+                          (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
112
+            srt_print(s, ">");
113
+            srt_stack_push(s, 'f');
114
+        }
115
+        if (st->bold != ASS_DEFAULT_BOLD) {
116
+            srt_print(s, "<b>");
117
+            srt_stack_push(s, 'b');
118
+        }
119
+        if (st->italic != ASS_DEFAULT_ITALIC) {
120
+            srt_print(s, "<i>");
121
+            srt_stack_push(s, 'i');
122
+        }
123
+        if (st->underline != ASS_DEFAULT_UNDERLINE) {
124
+            srt_print(s, "<u>");
125
+            srt_stack_push(s, 'u');
126
+        }
127
+        if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
128
+            srt_print(s, "{\\an%d}", st->alignment);
129
+            s->alignment_applied = 1;
130
+        }
131
+    }
132
+}
133
+
134
+
135
+static av_cold int srt_encode_init(AVCodecContext *avctx)
136
+{
137
+    SRTContext *s = avctx->priv_data;
138
+    s->avctx = avctx;
139
+    s->ass_ctx = ff_ass_split(avctx->subtitle_header);
140
+    return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
141
+}
142
+
143
+static void srt_text_cb(void *priv, const char *text, int len)
144
+{
145
+    SRTContext *s = priv;
146
+    av_strlcpy(s->ptr, text, FFMIN(s->end-s->ptr, len+1));
147
+    s->ptr += len;
148
+}
149
+
150
+static void srt_new_line_cb(void *priv, int forced)
151
+{
152
+    srt_print(priv, "\r\n");
153
+}
154
+
155
+static void srt_style_cb(void *priv, char style, int close)
156
+{
157
+    srt_stack_push_pop(priv, style, close);
158
+    if (!close)
159
+        srt_print(priv, "<%c>", style);
160
+}
161
+
162
+static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
163
+{
164
+    if (color_id > 1)
165
+        return;
166
+    srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
167
+    if (color != 0xFFFFFFFF)
168
+        srt_print(priv, "<font color=\"#%06x\">",
169
+              (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
170
+}
171
+
172
+static void srt_font_name_cb(void *priv, const char *name)
173
+{
174
+    srt_stack_push_pop(priv, 'f', !name);
175
+    if (name)
176
+        srt_print(priv, "<font face=\"%s\">", name);
177
+}
178
+
179
+static void srt_font_size_cb(void *priv, int size)
180
+{
181
+    srt_stack_push_pop(priv, 'f', size < 0);
182
+    if (size >= 0)
183
+        srt_print(priv, "<font size=\"%d\">", size);
184
+}
185
+
186
+static void srt_alignment_cb(void *priv, int alignment)
187
+{
188
+    SRTContext *s = priv;
189
+    if (!s->alignment_applied && alignment >= 0) {
190
+        srt_print(s, "{\\an%d}", alignment);
191
+        s->alignment_applied = 1;
192
+    }
193
+}
194
+
195
+static void srt_cancel_overrides_cb(void *priv, const char *style)
196
+{
197
+    srt_stack_push_pop(priv, 0, 1);
198
+    srt_style_apply(priv, style);
199
+}
200
+
201
+static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
202
+                        int t1, int t2)
203
+{
204
+    SRTContext *s = priv;
205
+    char buffer[32];
206
+    int len = snprintf(buffer, sizeof(buffer),
207
+                       "  X1:%03u X2:%03u Y1:%03u Y2:%03u", x1, x2, y1, y2);
208
+    if (s->end - s->ptr > len) {
209
+        memmove(s->dialog_start+len, s->dialog_start, s->ptr-s->dialog_start+1);
210
+        memcpy(s->dialog_start, buffer, len);
211
+        s->ptr += len;
212
+    }
213
+}
214
+
215
+static void srt_end_cb(void *priv)
216
+{
217
+    srt_stack_push_pop(priv, 0, 1);
218
+    srt_print(priv, "\r\n\r\n");
219
+}
220
+
221
+static const ASSCodesCallbacks srt_callbacks = {
222
+    .text             = srt_text_cb,
223
+    .new_line         = srt_new_line_cb,
224
+    .style            = srt_style_cb,
225
+    .color            = srt_color_cb,
226
+    .font_name        = srt_font_name_cb,
227
+    .font_size        = srt_font_size_cb,
228
+    .alignment        = srt_alignment_cb,
229
+    .cancel_overrides = srt_cancel_overrides_cb,
230
+    .move             = srt_move_cb,
231
+    .end              = srt_end_cb,
232
+};
233
+
234
+static int srt_encode_frame(AVCodecContext *avctx,
235
+                            unsigned char *buf, int bufsize, void *data)
236
+{
237
+    SRTContext *s = avctx->priv_data;
238
+    AVSubtitle *sub = data;
239
+    ASSDialog *dialog;
240
+    int i, len, num;
241
+
242
+    s->ptr = s->buffer;
243
+    s->end = s->ptr + sizeof(s->buffer);
244
+
245
+    for (i=0; i<sub->num_rects; i++) {
246
+
247
+        if (sub->rects[i]->type != SUBTITLE_ASS) {
248
+            av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
249
+            return AVERROR(ENOSYS);
250
+        }
251
+
252
+        dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
253
+        for (; dialog && num--; dialog++) {
254
+            int sh, sm, ss, sc = 10 * dialog->start;
255
+            int eh, em, es, ec = 10 * dialog->end;
256
+            sh = sc/3600000;  sc -= 3600000*sh;
257
+            sm = sc/  60000;  sc -=   60000*sm;
258
+            ss = sc/   1000;  sc -=    1000*ss;
259
+            eh = ec/3600000;  ec -= 3600000*eh;
260
+            em = ec/  60000;  ec -=   60000*em;
261
+            es = ec/   1000;  ec -=    1000*es;
262
+            srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
263
+                      ++s->count, sh, sm, ss, sc, eh, em, es, ec);
264
+            s->alignment_applied = 0;
265
+            s->dialog_start = s->ptr - 2;
266
+            srt_style_apply(s, dialog->style);
267
+            ff_ass_split_override_codes(&srt_callbacks, s, dialog->text);
268
+        }
269
+    }
270
+
271
+    if (s->ptr == s->buffer)
272
+        return 0;
273
+
274
+    len = av_strlcpy(buf, s->buffer, bufsize);
275
+
276
+    if (len > bufsize-1) {
277
+        av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
278
+        return -1;
279
+    }
280
+
281
+    return len;
282
+}
283
+
284
+static int srt_encode_close(AVCodecContext *avctx)
285
+{
286
+    SRTContext *s = avctx->priv_data;
287
+    ff_ass_split_free(s->ass_ctx);
288
+    return 0;
289
+}
290
+
291
+AVCodec ff_srt_encoder = {
292
+    .name           = "srt",
293
+    .long_name      = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
294
+    .type           = AVMEDIA_TYPE_SUBTITLE,
295
+    .id             = CODEC_ID_SRT,
296
+    .priv_data_size = sizeof(SRTContext),
297
+    .init           = srt_encode_init,
298
+    .encode         = srt_encode_frame,
299
+    .close          = srt_encode_close,
300
+};
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 52
24
-#define LIBAVCODEC_VERSION_MINOR 115
24
+#define LIBAVCODEC_VERSION_MINOR 116
25 25
 #define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \