yading@10
|
1 /*
|
yading@10
|
2 * Copyright (c) 2012 Clément Bœsch
|
yading@10
|
3 *
|
yading@10
|
4 * This file is part of FFmpeg.
|
yading@10
|
5 *
|
yading@10
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
8 * License as published by the Free Software Foundation; either
|
yading@10
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
10 *
|
yading@10
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
14 * Lesser General Public License for more details.
|
yading@10
|
15 *
|
yading@10
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
19 */
|
yading@10
|
20
|
yading@10
|
21 /**
|
yading@10
|
22 * @file
|
yading@10
|
23 * JACOsub subtitle decoder
|
yading@10
|
24 * @see http://unicorn.us.com/jacosub/jscripts.html
|
yading@10
|
25 */
|
yading@10
|
26
|
yading@10
|
27 #include <time.h>
|
yading@10
|
28 #include "ass.h"
|
yading@10
|
29 #include "jacosub.h"
|
yading@10
|
30 #include "libavutil/avstring.h"
|
yading@10
|
31 #include "libavutil/bprint.h"
|
yading@10
|
32
|
yading@10
|
33 #undef time
|
yading@10
|
34
|
yading@10
|
35 static int insert_text(AVBPrint *dst, const char *in, const char *arg)
|
yading@10
|
36 {
|
yading@10
|
37 av_bprintf(dst, "%s", arg);
|
yading@10
|
38 return 0;
|
yading@10
|
39 }
|
yading@10
|
40
|
yading@10
|
41 static int insert_datetime(AVBPrint *dst, const char *in, const char *arg)
|
yading@10
|
42 {
|
yading@10
|
43 char buf[16] = {0};
|
yading@10
|
44 time_t now = time(0);
|
yading@10
|
45 struct tm ltime;
|
yading@10
|
46
|
yading@10
|
47 #if HAVE_LOCALTIME_R
|
yading@10
|
48 localtime_r(&now, <ime);
|
yading@10
|
49 #else
|
yading@10
|
50 ltime = *localtime(&now);
|
yading@10
|
51 #endif
|
yading@10
|
52 strftime(buf, sizeof(buf), arg, <ime);
|
yading@10
|
53 av_bprintf(dst, "%s", buf);
|
yading@10
|
54 return 0;
|
yading@10
|
55 }
|
yading@10
|
56
|
yading@10
|
57 static int insert_color(AVBPrint *dst, const char *in, const char *arg)
|
yading@10
|
58 {
|
yading@10
|
59 return 1; // skip id
|
yading@10
|
60 }
|
yading@10
|
61
|
yading@10
|
62 static int insert_font(AVBPrint *dst, const char *in, const char *arg)
|
yading@10
|
63 {
|
yading@10
|
64 return 1; // skip id
|
yading@10
|
65 }
|
yading@10
|
66
|
yading@10
|
67 static const struct {
|
yading@10
|
68 const char *from;
|
yading@10
|
69 const char *arg;
|
yading@10
|
70 int (*func)(AVBPrint *dst, const char *in, const char *arg);
|
yading@10
|
71 } ass_codes_map[] = {
|
yading@10
|
72 {"\\~", "~", insert_text}, // tilde doesn't need escaping
|
yading@10
|
73 {"~", "{\\h}", insert_text}, // hard space
|
yading@10
|
74 {"\\n", "\\N", insert_text}, // newline
|
yading@10
|
75 {"\\D", "%d %b %Y", insert_datetime}, // current date
|
yading@10
|
76 {"\\T", "%H:%M", insert_datetime}, // current time
|
yading@10
|
77 {"\\N", "{\\r}", insert_text}, // reset to default style
|
yading@10
|
78 {"\\I", "{\\i1}", insert_text}, // italic on
|
yading@10
|
79 {"\\i", "{\\i0}", insert_text}, // italic off
|
yading@10
|
80 {"\\B", "{\\b1}", insert_text}, // bold on
|
yading@10
|
81 {"\\b", "{\\b0}", insert_text}, // bold off
|
yading@10
|
82 {"\\U", "{\\u1}", insert_text}, // underline on
|
yading@10
|
83 {"\\u", "{\\u0}", insert_text}, // underline off
|
yading@10
|
84 {"\\C", "", insert_color}, // TODO: color
|
yading@10
|
85 {"\\F", "", insert_font}, // TODO: font
|
yading@10
|
86 };
|
yading@10
|
87
|
yading@10
|
88 enum {
|
yading@10
|
89 ALIGN_VB = 1<<0, // vertical bottom, default
|
yading@10
|
90 ALIGN_VM = 1<<1, // vertical middle
|
yading@10
|
91 ALIGN_VT = 1<<2, // vertical top
|
yading@10
|
92 ALIGN_JC = 1<<3, // justify center, default
|
yading@10
|
93 ALIGN_JL = 1<<4, // justify left
|
yading@10
|
94 ALIGN_JR = 1<<5, // justify right
|
yading@10
|
95 };
|
yading@10
|
96
|
yading@10
|
97 static void jacosub_to_ass(AVCodecContext *avctx, AVBPrint *dst, const char *src)
|
yading@10
|
98 {
|
yading@10
|
99 int i, valign = 0, halign = 0;
|
yading@10
|
100 char c = av_toupper(*src);
|
yading@10
|
101 char directives[128] = {0};
|
yading@10
|
102
|
yading@10
|
103 /* extract the optional directives */
|
yading@10
|
104 if ((c >= 'A' && c <= 'Z') || c == '[') {
|
yading@10
|
105 char *p = directives;
|
yading@10
|
106 char *pend = directives + sizeof(directives) - 1;
|
yading@10
|
107
|
yading@10
|
108 do *p++ = av_toupper(*src++);
|
yading@10
|
109 while (*src && !jss_whitespace(*src) && p < pend);
|
yading@10
|
110 *p = 0;
|
yading@10
|
111 src = jss_skip_whitespace(src);
|
yading@10
|
112 }
|
yading@10
|
113
|
yading@10
|
114 /* handle directives (TODO: handle more of them, and more reliably) */
|
yading@10
|
115 if (strstr(directives, "VB")) valign = ALIGN_VB;
|
yading@10
|
116 else if (strstr(directives, "VM")) valign = ALIGN_VM;
|
yading@10
|
117 else if (strstr(directives, "VT")) valign = ALIGN_VT;
|
yading@10
|
118 if (strstr(directives, "JC")) halign = ALIGN_JC;
|
yading@10
|
119 else if (strstr(directives, "JL")) halign = ALIGN_JL;
|
yading@10
|
120 else if (strstr(directives, "JR")) halign = ALIGN_JR;
|
yading@10
|
121 if (valign || halign) {
|
yading@10
|
122 if (!valign) valign = ALIGN_VB;
|
yading@10
|
123 if (!halign) halign = ALIGN_JC;
|
yading@10
|
124 switch (valign | halign) {
|
yading@10
|
125 case ALIGN_VB | ALIGN_JL: av_bprintf(dst, "{\\an1}"); break; // bottom left
|
yading@10
|
126 case ALIGN_VB | ALIGN_JC: av_bprintf(dst, "{\\an2}"); break; // bottom center
|
yading@10
|
127 case ALIGN_VB | ALIGN_JR: av_bprintf(dst, "{\\an3}"); break; // bottom right
|
yading@10
|
128 case ALIGN_VM | ALIGN_JL: av_bprintf(dst, "{\\an4}"); break; // middle left
|
yading@10
|
129 case ALIGN_VM | ALIGN_JC: av_bprintf(dst, "{\\an5}"); break; // middle center
|
yading@10
|
130 case ALIGN_VM | ALIGN_JR: av_bprintf(dst, "{\\an6}"); break; // middle right
|
yading@10
|
131 case ALIGN_VT | ALIGN_JL: av_bprintf(dst, "{\\an7}"); break; // top left
|
yading@10
|
132 case ALIGN_VT | ALIGN_JC: av_bprintf(dst, "{\\an8}"); break; // top center
|
yading@10
|
133 case ALIGN_VT | ALIGN_JR: av_bprintf(dst, "{\\an9}"); break; // top right
|
yading@10
|
134 }
|
yading@10
|
135 }
|
yading@10
|
136
|
yading@10
|
137 /* process timed line */
|
yading@10
|
138 while (*src && *src != '\n') {
|
yading@10
|
139
|
yading@10
|
140 /* text continue on the next line */
|
yading@10
|
141 if (src[0] == '\\' && src[1] == '\n') {
|
yading@10
|
142 src += 2;
|
yading@10
|
143 while (jss_whitespace(*src))
|
yading@10
|
144 src++;
|
yading@10
|
145 continue;
|
yading@10
|
146 }
|
yading@10
|
147
|
yading@10
|
148 /* special character codes */
|
yading@10
|
149 for (i = 0; i < FF_ARRAY_ELEMS(ass_codes_map); i++) {
|
yading@10
|
150 const char *from = ass_codes_map[i].from;
|
yading@10
|
151 const char *arg = ass_codes_map[i].arg;
|
yading@10
|
152 size_t codemap_len = strlen(from);
|
yading@10
|
153
|
yading@10
|
154 if (!strncmp(src, from, codemap_len)) {
|
yading@10
|
155 src += codemap_len;
|
yading@10
|
156 src += ass_codes_map[i].func(dst, src, arg);
|
yading@10
|
157 break;
|
yading@10
|
158 }
|
yading@10
|
159 }
|
yading@10
|
160
|
yading@10
|
161 /* simple char copy */
|
yading@10
|
162 if (i == FF_ARRAY_ELEMS(ass_codes_map))
|
yading@10
|
163 av_bprintf(dst, "%c", *src++);
|
yading@10
|
164 }
|
yading@10
|
165 av_bprintf(dst, "\r\n");
|
yading@10
|
166 }
|
yading@10
|
167
|
yading@10
|
168 static int jacosub_decode_frame(AVCodecContext *avctx,
|
yading@10
|
169 void *data, int *got_sub_ptr, AVPacket *avpkt)
|
yading@10
|
170 {
|
yading@10
|
171 AVSubtitle *sub = data;
|
yading@10
|
172 const char *ptr = avpkt->data;
|
yading@10
|
173
|
yading@10
|
174 if (avpkt->size <= 0)
|
yading@10
|
175 goto end;
|
yading@10
|
176
|
yading@10
|
177 if (*ptr) {
|
yading@10
|
178 AVBPrint buffer;
|
yading@10
|
179 char *dec_sub;
|
yading@10
|
180
|
yading@10
|
181 // skip timers
|
yading@10
|
182 ptr = jss_skip_whitespace(ptr);
|
yading@10
|
183 ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
|
yading@10
|
184 ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
|
yading@10
|
185
|
yading@10
|
186 av_bprint_init(&buffer, JSS_MAX_LINESIZE, JSS_MAX_LINESIZE);
|
yading@10
|
187 jacosub_to_ass(avctx, &buffer, ptr);
|
yading@10
|
188 av_bprint_finalize(&buffer, &dec_sub);
|
yading@10
|
189 ff_ass_add_rect(sub, dec_sub, avpkt->pts, avpkt->duration, 0);
|
yading@10
|
190 av_free(dec_sub);
|
yading@10
|
191 }
|
yading@10
|
192
|
yading@10
|
193 end:
|
yading@10
|
194 *got_sub_ptr = sub->num_rects > 0;
|
yading@10
|
195 return avpkt->size;
|
yading@10
|
196 }
|
yading@10
|
197
|
yading@10
|
198 AVCodec ff_jacosub_decoder = {
|
yading@10
|
199 .name = "jacosub",
|
yading@10
|
200 .long_name = NULL_IF_CONFIG_SMALL("JACOsub subtitle"),
|
yading@10
|
201 .type = AVMEDIA_TYPE_SUBTITLE,
|
yading@10
|
202 .id = AV_CODEC_ID_JACOSUB,
|
yading@10
|
203 .init = ff_ass_subtitle_header_default,
|
yading@10
|
204 .decode = jacosub_decode_frame,
|
yading@10
|
205 };
|