FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jacosubdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 61 79 77.2%
Functions: 3 6 50.0%
Branches: 51 70 72.9%

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