/src/ffmpeg/libavcodec/jacosubdec.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2012 Clément Bœsch |
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 | | /** |
22 | | * @file |
23 | | * JACOsub subtitle decoder |
24 | | * @see http://unicorn.us.com/jacosub/jscripts.html |
25 | | */ |
26 | | |
27 | | #include <time.h> |
28 | | #include "ass.h" |
29 | | #include "codec_internal.h" |
30 | | #include "jacosub.h" |
31 | | #include "libavutil/avstring.h" |
32 | | #include "libavutil/bprint.h" |
33 | | #include "libavutil/time_internal.h" |
34 | | |
35 | | static int insert_text(AVBPrint *dst, const char *in, const char *arg) |
36 | 2.31k | { |
37 | 2.31k | av_bprintf(dst, "%s", arg); |
38 | 2.31k | return 0; |
39 | 2.31k | } |
40 | | |
41 | | static int insert_datetime(AVBPrint *dst, const char *in, const char *arg) |
42 | 3.65M | { |
43 | 3.65M | char buf[16] = {0}; |
44 | 3.65M | time_t now = time(0); |
45 | 3.65M | struct tm ltime; |
46 | | |
47 | 3.65M | localtime_r(&now, <ime); |
48 | 3.65M | if (strftime(buf, sizeof(buf), arg, <ime)) |
49 | 3.65M | av_bprintf(dst, "%s", buf); |
50 | 3.65M | return 0; |
51 | 3.65M | } |
52 | | |
53 | | static int insert_color(AVBPrint *dst, const char *in, const char *arg) |
54 | 242 | { |
55 | 242 | return 1; // skip id |
56 | 242 | } |
57 | | |
58 | | static int insert_font(AVBPrint *dst, const char *in, const char *arg) |
59 | 431 | { |
60 | 431 | return 1; // skip id |
61 | 431 | } |
62 | | |
63 | | static const struct { |
64 | | const char *from; |
65 | | const char *arg; |
66 | | int (*func)(AVBPrint *dst, const char *in, const char *arg); |
67 | | } ass_codes_map[] = { |
68 | | {"\\~", "~", insert_text}, // tilde doesn't need escaping |
69 | | {"~", "{\\h}", insert_text}, // hard space |
70 | | {"\\n", "\\N", insert_text}, // newline |
71 | | {"\\D", "%d %b %Y", insert_datetime}, // current date |
72 | | {"\\T", "%H:%M", insert_datetime}, // current time |
73 | | {"\\N", "{\\r}", insert_text}, // reset to default style |
74 | | {"\\I", "{\\i1}", insert_text}, // italic on |
75 | | {"\\i", "{\\i0}", insert_text}, // italic off |
76 | | {"\\B", "{\\b1}", insert_text}, // bold on |
77 | | {"\\b", "{\\b0}", insert_text}, // bold off |
78 | | {"\\U", "{\\u1}", insert_text}, // underline on |
79 | | {"\\u", "{\\u0}", insert_text}, // underline off |
80 | | {"\\C", "", insert_color}, // TODO: color |
81 | | {"\\F", "", insert_font}, // TODO: font |
82 | | }; |
83 | | |
84 | | enum { |
85 | | ALIGN_VB = 1<<0, // vertical bottom, default |
86 | | ALIGN_VM = 1<<1, // vertical middle |
87 | | ALIGN_VT = 1<<2, // vertical top |
88 | | ALIGN_JC = 1<<3, // justify center, default |
89 | | ALIGN_JL = 1<<4, // justify left |
90 | | ALIGN_JR = 1<<5, // justify right |
91 | | }; |
92 | | |
93 | | static void jacosub_to_ass(AVCodecContext *avctx, AVBPrint *dst, const char *src) |
94 | 7.87k | { |
95 | 7.87k | int i, valign = 0, halign = 0; |
96 | 7.87k | char c = av_toupper(*src); |
97 | 7.87k | char directives[128] = {0}; |
98 | | |
99 | | /* extract the optional directives */ |
100 | 7.87k | if ((c >= 'A' && c <= 'Z') || c == '[') { |
101 | 3.85k | char *p = directives; |
102 | 3.85k | char *pend = directives + sizeof(directives) - 1; |
103 | | |
104 | 17.5k | do *p++ = av_toupper(*src++); |
105 | 17.5k | while (*src && !jss_whitespace(*src) && p < pend); |
106 | 3.85k | *p = 0; |
107 | 3.85k | src = jss_skip_whitespace(src); |
108 | 3.85k | } |
109 | | |
110 | | /* handle directives (TODO: handle more of them, and more reliably) */ |
111 | 7.87k | if (strstr(directives, "VB")) valign = ALIGN_VB; |
112 | 7.64k | else if (strstr(directives, "VM")) valign = ALIGN_VM; |
113 | 6.89k | else if (strstr(directives, "VT")) valign = ALIGN_VT; |
114 | 7.87k | if (strstr(directives, "JC")) halign = ALIGN_JC; |
115 | 7.62k | else if (strstr(directives, "JL")) halign = ALIGN_JL; |
116 | 6.90k | else if (strstr(directives, "JR")) halign = ALIGN_JR; |
117 | 7.87k | if (valign || halign) { |
118 | 2.54k | if (!valign) valign = ALIGN_VB; |
119 | 2.54k | if (!halign) halign = ALIGN_JC; |
120 | 2.54k | switch (valign | halign) { |
121 | 258 | case ALIGN_VB | ALIGN_JL: av_bprintf(dst, "{\\an1}"); break; // bottom left |
122 | 486 | case ALIGN_VB | ALIGN_JC: av_bprintf(dst, "{\\an2}"); break; // bottom center |
123 | 235 | case ALIGN_VB | ALIGN_JR: av_bprintf(dst, "{\\an3}"); break; // bottom right |
124 | 221 | case ALIGN_VM | ALIGN_JL: av_bprintf(dst, "{\\an4}"); break; // middle left |
125 | 307 | case ALIGN_VM | ALIGN_JC: av_bprintf(dst, "{\\an5}"); break; // middle center |
126 | 215 | case ALIGN_VM | ALIGN_JR: av_bprintf(dst, "{\\an6}"); break; // middle right |
127 | 238 | case ALIGN_VT | ALIGN_JL: av_bprintf(dst, "{\\an7}"); break; // top left |
128 | 335 | case ALIGN_VT | ALIGN_JC: av_bprintf(dst, "{\\an8}"); break; // top center |
129 | 251 | case ALIGN_VT | ALIGN_JR: av_bprintf(dst, "{\\an9}"); break; // top right |
130 | 2.54k | } |
131 | 2.54k | } |
132 | | |
133 | | /* process timed line */ |
134 | 3.71M | while (*src && *src != '\n') { |
135 | | |
136 | | /* text continue on the next line */ |
137 | 3.70M | if (src[0] == '\\' && src[1] == '\n') { |
138 | 566 | src += 2; |
139 | 1.37k | while (jss_whitespace(*src)) |
140 | 807 | src++; |
141 | 566 | continue; |
142 | 566 | } |
143 | | |
144 | | /* special character codes */ |
145 | 15.3M | for (i = 0; i < FF_ARRAY_ELEMS(ass_codes_map); i++) { |
146 | 15.3M | const char *from = ass_codes_map[i].from; |
147 | 15.3M | const char *arg = ass_codes_map[i].arg; |
148 | 15.3M | size_t codemap_len = strlen(from); |
149 | | |
150 | 15.3M | if (!strncmp(src, from, codemap_len)) { |
151 | 3.65M | src += codemap_len; |
152 | 3.65M | src += ass_codes_map[i].func(dst, src, arg); |
153 | 3.65M | break; |
154 | 3.65M | } |
155 | 15.3M | } |
156 | | |
157 | | /* simple char copy */ |
158 | 3.70M | if (i == FF_ARRAY_ELEMS(ass_codes_map)) |
159 | 48.4k | av_bprintf(dst, "%c", *src++); |
160 | 3.70M | } |
161 | 7.87k | } |
162 | | |
163 | | static int jacosub_decode_frame(AVCodecContext *avctx, AVSubtitle *sub, |
164 | | int *got_sub_ptr, const AVPacket *avpkt) |
165 | 179k | { |
166 | 179k | int ret; |
167 | 179k | const char *ptr = avpkt->data; |
168 | 179k | FFASSDecoderContext *s = avctx->priv_data; |
169 | | |
170 | 179k | if (avpkt->size <= 0) |
171 | 0 | goto end; |
172 | | |
173 | 179k | if (*ptr) { |
174 | 87.8k | AVBPrint buffer; |
175 | | |
176 | | // skip timers |
177 | 87.8k | ptr = jss_skip_whitespace(ptr); |
178 | 87.8k | ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++; |
179 | 8.74k | ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++; |
180 | | |
181 | 7.87k | av_bprint_init(&buffer, JSS_MAX_LINESIZE, JSS_MAX_LINESIZE); |
182 | 7.87k | jacosub_to_ass(avctx, &buffer, ptr); |
183 | 7.87k | ret = ff_ass_add_rect(sub, buffer.str, s->readorder++, 0, NULL, NULL); |
184 | 7.87k | av_bprint_finalize(&buffer, NULL); |
185 | 7.87k | if (ret < 0) |
186 | 0 | return ret; |
187 | 7.87k | } |
188 | | |
189 | 179k | end: |
190 | 179k | *got_sub_ptr = sub->num_rects > 0; |
191 | 179k | return avpkt->size; |
192 | 179k | } |
193 | | |
194 | | const FFCodec ff_jacosub_decoder = { |
195 | | .p.name = "jacosub", |
196 | | CODEC_LONG_NAME("JACOsub subtitle"), |
197 | | .p.type = AVMEDIA_TYPE_SUBTITLE, |
198 | | .p.id = AV_CODEC_ID_JACOSUB, |
199 | | .init = ff_ass_subtitle_header_default, |
200 | | FF_CODEC_DECODE_SUB_CB(jacosub_decode_frame), |
201 | | .flush = ff_ass_decoder_flush, |
202 | | .priv_data_size = sizeof(FFASSDecoderContext), |
203 | | }; |