Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Closed Caption Decoding | ||
3 | * Copyright (c) 2015 Anshul Maheshwari | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include <assert.h> | ||
23 | #include "avcodec.h" | ||
24 | #include "ass.h" | ||
25 | #include "codec_internal.h" | ||
26 | #include "libavutil/opt.h" | ||
27 | |||
28 | #define SCREEN_ROWS 15 | ||
29 | #define SCREEN_COLUMNS 32 | ||
30 | |||
31 | #define SET_FLAG(var, val) ( (var) |= ( 1 << (val)) ) | ||
32 | #define UNSET_FLAG(var, val) ( (var) &= ~( 1 << (val)) ) | ||
33 | #define CHECK_FLAG(var, val) ( (var) & ( 1 << (val)) ) | ||
34 | |||
35 | static const AVRational ms_tb = {1, 1000}; | ||
36 | |||
37 | enum cc_mode { | ||
38 | CCMODE_POPON, | ||
39 | CCMODE_PAINTON, | ||
40 | CCMODE_ROLLUP, | ||
41 | CCMODE_TEXT, | ||
42 | }; | ||
43 | |||
44 | enum cc_color_code { | ||
45 | CCCOL_WHITE, | ||
46 | CCCOL_GREEN, | ||
47 | CCCOL_BLUE, | ||
48 | CCCOL_CYAN, | ||
49 | CCCOL_RED, | ||
50 | CCCOL_YELLOW, | ||
51 | CCCOL_MAGENTA, | ||
52 | CCCOL_USERDEFINED, | ||
53 | CCCOL_BLACK, | ||
54 | CCCOL_TRANSPARENT, | ||
55 | }; | ||
56 | |||
57 | enum cc_font { | ||
58 | CCFONT_REGULAR, | ||
59 | CCFONT_ITALICS, | ||
60 | CCFONT_UNDERLINED, | ||
61 | CCFONT_UNDERLINED_ITALICS, | ||
62 | }; | ||
63 | |||
64 | enum cc_charset { | ||
65 | CCSET_BASIC_AMERICAN, | ||
66 | CCSET_SPECIAL_AMERICAN, | ||
67 | CCSET_EXTENDED_SPANISH_FRENCH_MISC, | ||
68 | CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH, | ||
69 | }; | ||
70 | |||
71 | #define CHARSET_OVERRIDE_LIST(START_SET, ENTRY, END_SET) \ | ||
72 | START_SET(CCSET_BASIC_AMERICAN) \ | ||
73 | ENTRY(0x27, "\u2019") \ | ||
74 | ENTRY(0x2a, "\u00e1") \ | ||
75 | ENTRY(0x5c, "\u00e9") \ | ||
76 | ENTRY(0x5e, "\u00ed") \ | ||
77 | ENTRY(0x5f, "\u00f3") \ | ||
78 | ENTRY(0x60, "\u00fa") \ | ||
79 | ENTRY(0x7b, "\u00e7") \ | ||
80 | ENTRY(0x7c, "\u00f7") \ | ||
81 | ENTRY(0x7d, "\u00d1") \ | ||
82 | ENTRY(0x7e, "\u00f1") \ | ||
83 | ENTRY(0x7f, "\u2588") \ | ||
84 | END_SET \ | ||
85 | START_SET(CCSET_SPECIAL_AMERICAN) \ | ||
86 | ENTRY(0x30, "\u00ae") \ | ||
87 | ENTRY(0x31, "\u00b0") \ | ||
88 | ENTRY(0x32, "\u00bd") \ | ||
89 | ENTRY(0x33, "\u00bf") \ | ||
90 | ENTRY(0x34, "\u2122") \ | ||
91 | ENTRY(0x35, "\u00a2") \ | ||
92 | ENTRY(0x36, "\u00a3") \ | ||
93 | ENTRY(0x37, "\u266a") \ | ||
94 | ENTRY(0x38, "\u00e0") \ | ||
95 | ENTRY(0x39, "\u00A0") \ | ||
96 | ENTRY(0x3a, "\u00e8") \ | ||
97 | ENTRY(0x3b, "\u00e2") \ | ||
98 | ENTRY(0x3c, "\u00ea") \ | ||
99 | ENTRY(0x3d, "\u00ee") \ | ||
100 | ENTRY(0x3e, "\u00f4") \ | ||
101 | ENTRY(0x3f, "\u00fb") \ | ||
102 | END_SET \ | ||
103 | START_SET(CCSET_EXTENDED_SPANISH_FRENCH_MISC) \ | ||
104 | ENTRY(0x20, "\u00c1") \ | ||
105 | ENTRY(0x21, "\u00c9") \ | ||
106 | ENTRY(0x22, "\u00d3") \ | ||
107 | ENTRY(0x23, "\u00da") \ | ||
108 | ENTRY(0x24, "\u00dc") \ | ||
109 | ENTRY(0x25, "\u00fc") \ | ||
110 | ENTRY(0x26, "\u00b4") \ | ||
111 | ENTRY(0x27, "\u00a1") \ | ||
112 | ENTRY(0x28, "*") \ | ||
113 | ENTRY(0x29, "\u2018") \ | ||
114 | ENTRY(0x2a, "-") \ | ||
115 | ENTRY(0x2b, "\u00a9") \ | ||
116 | ENTRY(0x2c, "\u2120") \ | ||
117 | ENTRY(0x2d, "\u00b7") \ | ||
118 | ENTRY(0x2e, "\u201c") \ | ||
119 | ENTRY(0x2f, "\u201d") \ | ||
120 | ENTRY(0x30, "\u00c0") \ | ||
121 | ENTRY(0x31, "\u00c2") \ | ||
122 | ENTRY(0x32, "\u00c7") \ | ||
123 | ENTRY(0x33, "\u00c8") \ | ||
124 | ENTRY(0x34, "\u00ca") \ | ||
125 | ENTRY(0x35, "\u00cb") \ | ||
126 | ENTRY(0x36, "\u00eb") \ | ||
127 | ENTRY(0x37, "\u00ce") \ | ||
128 | ENTRY(0x38, "\u00cf") \ | ||
129 | ENTRY(0x39, "\u00ef") \ | ||
130 | ENTRY(0x3a, "\u00d4") \ | ||
131 | ENTRY(0x3b, "\u00d9") \ | ||
132 | ENTRY(0x3c, "\u00f9") \ | ||
133 | ENTRY(0x3d, "\u00db") \ | ||
134 | ENTRY(0x3e, "\u00ab") \ | ||
135 | ENTRY(0x3f, "\u00bb") \ | ||
136 | END_SET \ | ||
137 | START_SET(CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH) \ | ||
138 | ENTRY(0x20, "\u00c3") \ | ||
139 | ENTRY(0x21, "\u00e3") \ | ||
140 | ENTRY(0x22, "\u00cd") \ | ||
141 | ENTRY(0x23, "\u00cc") \ | ||
142 | ENTRY(0x24, "\u00ec") \ | ||
143 | ENTRY(0x25, "\u00d2") \ | ||
144 | ENTRY(0x26, "\u00f2") \ | ||
145 | ENTRY(0x27, "\u00d5") \ | ||
146 | ENTRY(0x28, "\u00f5") \ | ||
147 | ENTRY(0x29, "{") \ | ||
148 | ENTRY(0x2a, "}") \ | ||
149 | ENTRY(0x2b, "\\") \ | ||
150 | ENTRY(0x2c, "^") \ | ||
151 | ENTRY(0x2d, "_") \ | ||
152 | ENTRY(0x2e, "|") \ | ||
153 | ENTRY(0x2f, "~") \ | ||
154 | ENTRY(0x30, "\u00c4") \ | ||
155 | ENTRY(0x31, "\u00e4") \ | ||
156 | ENTRY(0x32, "\u00d6") \ | ||
157 | ENTRY(0x33, "\u00f6") \ | ||
158 | ENTRY(0x34, "\u00df") \ | ||
159 | ENTRY(0x35, "\u00a5") \ | ||
160 | ENTRY(0x36, "\u00a4") \ | ||
161 | ENTRY(0x37, "\u00a6") \ | ||
162 | ENTRY(0x38, "\u00c5") \ | ||
163 | ENTRY(0x39, "\u00e5") \ | ||
164 | ENTRY(0x3a, "\u00d8") \ | ||
165 | ENTRY(0x3b, "\u00f8") \ | ||
166 | ENTRY(0x3c, "\u250c") \ | ||
167 | ENTRY(0x3d, "\u2510") \ | ||
168 | ENTRY(0x3e, "\u2514") \ | ||
169 | ENTRY(0x3f, "\u2518") \ | ||
170 | END_SET \ | ||
171 | |||
172 | static const char charset_overrides[4][128][sizeof("\u266a")] = | ||
173 | { | ||
174 | #define START_SET(IDX) \ | ||
175 | [IDX] = { | ||
176 | #define ENTRY(idx, string) \ | ||
177 | [idx] = string, | ||
178 | #define END_SET \ | ||
179 | }, | ||
180 | CHARSET_OVERRIDE_LIST(START_SET, ENTRY, END_SET) | ||
181 | }; | ||
182 | #define EMPTY_START(IDX) | ||
183 | #define EMPTY_END | ||
184 | #define ASSERT_ENTRY(IDX, str) \ | ||
185 | static_assert(sizeof(str) <= sizeof(charset_overrides[0][0]), \ | ||
186 | "'" str "' string takes too much space"); | ||
187 | CHARSET_OVERRIDE_LIST(EMPTY_START, ASSERT_ENTRY, EMPTY_END) | ||
188 | |||
189 | static const unsigned char bg_attribs[8] = // Color | ||
190 | { | ||
191 | CCCOL_WHITE, | ||
192 | CCCOL_GREEN, | ||
193 | CCCOL_BLUE, | ||
194 | CCCOL_CYAN, | ||
195 | CCCOL_RED, | ||
196 | CCCOL_YELLOW, | ||
197 | CCCOL_MAGENTA, | ||
198 | CCCOL_BLACK, | ||
199 | }; | ||
200 | |||
201 | static const unsigned char pac2_attribs[32][3] = // Color, font, ident | ||
202 | { | ||
203 | { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x40 || 0x60 | ||
204 | { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x41 || 0x61 | ||
205 | { CCCOL_GREEN, CCFONT_REGULAR, 0 }, // 0x42 || 0x62 | ||
206 | { CCCOL_GREEN, CCFONT_UNDERLINED, 0 }, // 0x43 || 0x63 | ||
207 | { CCCOL_BLUE, CCFONT_REGULAR, 0 }, // 0x44 || 0x64 | ||
208 | { CCCOL_BLUE, CCFONT_UNDERLINED, 0 }, // 0x45 || 0x65 | ||
209 | { CCCOL_CYAN, CCFONT_REGULAR, 0 }, // 0x46 || 0x66 | ||
210 | { CCCOL_CYAN, CCFONT_UNDERLINED, 0 }, // 0x47 || 0x67 | ||
211 | { CCCOL_RED, CCFONT_REGULAR, 0 }, // 0x48 || 0x68 | ||
212 | { CCCOL_RED, CCFONT_UNDERLINED, 0 }, // 0x49 || 0x69 | ||
213 | { CCCOL_YELLOW, CCFONT_REGULAR, 0 }, // 0x4a || 0x6a | ||
214 | { CCCOL_YELLOW, CCFONT_UNDERLINED, 0 }, // 0x4b || 0x6b | ||
215 | { CCCOL_MAGENTA, CCFONT_REGULAR, 0 }, // 0x4c || 0x6c | ||
216 | { CCCOL_MAGENTA, CCFONT_UNDERLINED, 0 }, // 0x4d || 0x6d | ||
217 | { CCCOL_WHITE, CCFONT_ITALICS, 0 }, // 0x4e || 0x6e | ||
218 | { CCCOL_WHITE, CCFONT_UNDERLINED_ITALICS, 0 }, // 0x4f || 0x6f | ||
219 | { CCCOL_WHITE, CCFONT_REGULAR, 0 }, // 0x50 || 0x70 | ||
220 | { CCCOL_WHITE, CCFONT_UNDERLINED, 0 }, // 0x51 || 0x71 | ||
221 | { CCCOL_WHITE, CCFONT_REGULAR, 4 }, // 0x52 || 0x72 | ||
222 | { CCCOL_WHITE, CCFONT_UNDERLINED, 4 }, // 0x53 || 0x73 | ||
223 | { CCCOL_WHITE, CCFONT_REGULAR, 8 }, // 0x54 || 0x74 | ||
224 | { CCCOL_WHITE, CCFONT_UNDERLINED, 8 }, // 0x55 || 0x75 | ||
225 | { CCCOL_WHITE, CCFONT_REGULAR, 12 }, // 0x56 || 0x76 | ||
226 | { CCCOL_WHITE, CCFONT_UNDERLINED, 12 }, // 0x57 || 0x77 | ||
227 | { CCCOL_WHITE, CCFONT_REGULAR, 16 }, // 0x58 || 0x78 | ||
228 | { CCCOL_WHITE, CCFONT_UNDERLINED, 16 }, // 0x59 || 0x79 | ||
229 | { CCCOL_WHITE, CCFONT_REGULAR, 20 }, // 0x5a || 0x7a | ||
230 | { CCCOL_WHITE, CCFONT_UNDERLINED, 20 }, // 0x5b || 0x7b | ||
231 | { CCCOL_WHITE, CCFONT_REGULAR, 24 }, // 0x5c || 0x7c | ||
232 | { CCCOL_WHITE, CCFONT_UNDERLINED, 24 }, // 0x5d || 0x7d | ||
233 | { CCCOL_WHITE, CCFONT_REGULAR, 28 }, // 0x5e || 0x7e | ||
234 | { CCCOL_WHITE, CCFONT_UNDERLINED, 28 } // 0x5f || 0x7f | ||
235 | /* total 32 entries */ | ||
236 | }; | ||
237 | |||
238 | struct Screen { | ||
239 | /* +1 is used to compensate null character of string */ | ||
240 | uint8_t characters[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
241 | uint8_t charsets[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
242 | uint8_t colors[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
243 | uint8_t bgs[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
244 | uint8_t fonts[SCREEN_ROWS+1][SCREEN_COLUMNS+1]; | ||
245 | /* | ||
246 | * Bitmask of used rows; if a bit is not set, the | ||
247 | * corresponding row is not used. | ||
248 | * for setting row 1 use row | (1 << 0) | ||
249 | * for setting row 15 use row | (1 << 14) | ||
250 | */ | ||
251 | int16_t row_used; | ||
252 | }; | ||
253 | |||
254 | typedef struct CCaptionSubContext { | ||
255 | AVClass *class; | ||
256 | int real_time; | ||
257 | int real_time_latency_msec; | ||
258 | int data_field; | ||
259 | struct Screen screen[2]; | ||
260 | int active_screen; | ||
261 | uint8_t cursor_row; | ||
262 | uint8_t cursor_column; | ||
263 | uint8_t cursor_color; | ||
264 | uint8_t bg_color; | ||
265 | uint8_t cursor_font; | ||
266 | uint8_t cursor_charset; | ||
267 | AVBPrint buffer[2]; | ||
268 | int buffer_index; | ||
269 | int buffer_changed; | ||
270 | int rollup; | ||
271 | enum cc_mode mode; | ||
272 | int64_t buffer_time[2]; | ||
273 | int screen_touched; | ||
274 | int64_t last_real_time; | ||
275 | uint8_t prev_cmd[2]; | ||
276 | int readorder; | ||
277 | } CCaptionSubContext; | ||
278 | |||
279 | 14 | static av_cold int init_decoder(AVCodecContext *avctx) | |
280 | { | ||
281 | 14 | CCaptionSubContext *ctx = avctx->priv_data; | |
282 | |||
283 | 14 | av_bprint_init(&ctx->buffer[0], 0, AV_BPRINT_SIZE_UNLIMITED); | |
284 | 14 | av_bprint_init(&ctx->buffer[1], 0, AV_BPRINT_SIZE_UNLIMITED); | |
285 | /* taking by default roll up to 2 */ | ||
286 | 14 | ctx->mode = CCMODE_ROLLUP; | |
287 | 14 | ctx->bg_color = CCCOL_BLACK; | |
288 | 14 | ctx->rollup = 2; | |
289 | 14 | ctx->cursor_row = 10; | |
290 | 14 | return ff_ass_subtitle_header(avctx, "Monospace", | |
291 | ASS_DEFAULT_FONT_SIZE, | ||
292 | ASS_DEFAULT_COLOR, | ||
293 | ASS_DEFAULT_BACK_COLOR, | ||
294 | ASS_DEFAULT_BOLD, | ||
295 | ASS_DEFAULT_ITALIC, | ||
296 | ASS_DEFAULT_UNDERLINE, | ||
297 | 3, | ||
298 | ASS_DEFAULT_ALIGNMENT); | ||
299 | } | ||
300 | |||
301 | 14 | static av_cold int close_decoder(AVCodecContext *avctx) | |
302 | { | ||
303 | 14 | CCaptionSubContext *ctx = avctx->priv_data; | |
304 | 14 | av_bprint_finalize(&ctx->buffer[0], NULL); | |
305 | 14 | av_bprint_finalize(&ctx->buffer[1], NULL); | |
306 | 14 | return 0; | |
307 | } | ||
308 | |||
309 | ✗ | static void flush_decoder(AVCodecContext *avctx) | |
310 | { | ||
311 | ✗ | CCaptionSubContext *ctx = avctx->priv_data; | |
312 | ✗ | ctx->screen[0].row_used = 0; | |
313 | ✗ | ctx->screen[1].row_used = 0; | |
314 | ✗ | ctx->prev_cmd[0] = 0; | |
315 | ✗ | ctx->prev_cmd[1] = 0; | |
316 | ✗ | ctx->mode = CCMODE_ROLLUP; | |
317 | ✗ | ctx->rollup = 2; | |
318 | ✗ | ctx->cursor_row = 10; | |
319 | ✗ | ctx->cursor_column = 0; | |
320 | ✗ | ctx->cursor_font = 0; | |
321 | ✗ | ctx->cursor_color = 0; | |
322 | ✗ | ctx->bg_color = CCCOL_BLACK; | |
323 | ✗ | ctx->cursor_charset = 0; | |
324 | ✗ | ctx->active_screen = 0; | |
325 | ✗ | ctx->last_real_time = 0; | |
326 | ✗ | ctx->screen_touched = 0; | |
327 | ✗ | ctx->buffer_changed = 0; | |
328 | ✗ | if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP)) | |
329 | ✗ | ctx->readorder = 0; | |
330 | ✗ | av_bprint_clear(&ctx->buffer[0]); | |
331 | ✗ | av_bprint_clear(&ctx->buffer[1]); | |
332 | ✗ | } | |
333 | |||
334 | /** | ||
335 | * @param ctx closed caption context just to print log | ||
336 | */ | ||
337 | 5999 | static void write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch) | |
338 | { | ||
339 | 5999 | uint8_t col = ctx->cursor_column; | |
340 | 5999 | char *row = screen->characters[ctx->cursor_row]; | |
341 | 5999 | char *font = screen->fonts[ctx->cursor_row]; | |
342 | 5999 | char *color = screen->colors[ctx->cursor_row]; | |
343 | 5999 | char *bg = screen->bgs[ctx->cursor_row]; | |
344 | 5999 | char *charset = screen->charsets[ctx->cursor_row]; | |
345 | |||
346 |
2/2✓ Branch 0 taken 5990 times.
✓ Branch 1 taken 9 times.
|
5999 | if (col < SCREEN_COLUMNS) { |
347 | 5990 | row[col] = ch; | |
348 | 5990 | font[col] = ctx->cursor_font; | |
349 | 5990 | color[col] = ctx->cursor_color; | |
350 | 5990 | bg[col] = ctx->bg_color; | |
351 | 5990 | charset[col] = ctx->cursor_charset; | |
352 | 5990 | ctx->cursor_charset = CCSET_BASIC_AMERICAN; | |
353 |
2/2✓ Branch 0 taken 4343 times.
✓ Branch 1 taken 1647 times.
|
5990 | if (ch) ctx->cursor_column++; |
354 | 5990 | return; | |
355 | } | ||
356 | /* We have extra space at end only for null character */ | ||
357 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | else if (col == SCREEN_COLUMNS && ch == 0) { |
358 | 9 | row[col] = ch; | |
359 | 9 | return; | |
360 | } | ||
361 | else { | ||
362 | ✗ | av_log(ctx, AV_LOG_WARNING, "Data ignored due to columns exceeding screen width\n"); | |
363 | ✗ | return; | |
364 | } | ||
365 | } | ||
366 | |||
367 | /** | ||
368 | * This function after validating parity bit, also remove it from data pair. | ||
369 | * The first byte doesn't pass parity, we replace it with a solid blank | ||
370 | * and process the pair. | ||
371 | * If the second byte doesn't pass parity, it returns INVALIDDATA | ||
372 | * user can ignore the whole pair and pass the other pair. | ||
373 | */ | ||
374 | 13817 | static int validate_cc_data_pair(const uint8_t *cc_data_pair, uint8_t *hi) | |
375 | { | ||
376 | 13817 | uint8_t cc_valid = (*cc_data_pair & 4) >>2; | |
377 | 13817 | uint8_t cc_type = *cc_data_pair & 3; | |
378 | |||
379 | 13817 | *hi = cc_data_pair[1]; | |
380 | |||
381 |
2/2✓ Branch 0 taken 9312 times.
✓ Branch 1 taken 4505 times.
|
13817 | if (!cc_valid) |
382 | 9312 | return AVERROR_INVALIDDATA; | |
383 | |||
384 | // if EIA-608 data then verify parity. | ||
385 |
4/4✓ Branch 0 taken 1306 times.
✓ Branch 1 taken 3199 times.
✓ Branch 2 taken 750 times.
✓ Branch 3 taken 556 times.
|
4505 | if (cc_type==0 || cc_type==1) { |
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3949 times.
|
3949 | if (!av_parity(cc_data_pair[2])) { |
387 | ✗ | return AVERROR_INVALIDDATA; | |
388 | } | ||
389 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3949 times.
|
3949 | if (!av_parity(cc_data_pair[1])) { |
390 | ✗ | *hi = 0x7F; | |
391 | } | ||
392 | } | ||
393 | |||
394 | //Skip non-data | ||
395 |
5/6✓ Branch 0 taken 4505 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1508 times.
✓ Branch 3 taken 2997 times.
✓ Branch 4 taken 548 times.
✓ Branch 5 taken 960 times.
|
4505 | if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD) |
396 |
3/4✓ Branch 0 taken 884 times.
✓ Branch 1 taken 2661 times.
✓ Branch 2 taken 884 times.
✗ Branch 3 not taken.
|
3545 | && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0) |
397 | 884 | return AVERROR_PATCHWELCOME; | |
398 | |||
399 | //skip 708 data | ||
400 |
4/4✓ Branch 0 taken 3465 times.
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 400 times.
✓ Branch 3 taken 3065 times.
|
3621 | if (cc_type == 3 || cc_type == 2) |
401 | 556 | return AVERROR_PATCHWELCOME; | |
402 | |||
403 | 3065 | return 0; | |
404 | } | ||
405 | |||
406 | 1889 | static struct Screen *get_writing_screen(CCaptionSubContext *ctx) | |
407 | { | ||
408 |
2/3✓ Branch 0 taken 1676 times.
✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
|
1889 | switch (ctx->mode) { |
409 | 1676 | case CCMODE_POPON: | |
410 | // use Inactive screen | ||
411 |
2/2✓ Branch 0 taken 864 times.
✓ Branch 1 taken 812 times.
|
1676 | return ctx->screen + !ctx->active_screen; |
412 | 213 | case CCMODE_PAINTON: | |
413 | case CCMODE_ROLLUP: | ||
414 | case CCMODE_TEXT: | ||
415 | // use active screen | ||
416 | 213 | return ctx->screen + ctx->active_screen; | |
417 | } | ||
418 | /* It was never an option */ | ||
419 | ✗ | return NULL; | |
420 | } | ||
421 | |||
422 | 15 | static void roll_up(CCaptionSubContext *ctx) | |
423 | { | ||
424 | struct Screen *screen; | ||
425 | int i, keep_lines; | ||
426 | |||
427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (ctx->mode == CCMODE_TEXT) |
428 | ✗ | return; | |
429 | |||
430 | 15 | screen = get_writing_screen(ctx); | |
431 | |||
432 | /* +1 signify cursor_row starts from 0 | ||
433 | * Can't keep lines less then row cursor pos | ||
434 | */ | ||
435 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
|
15 | keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup); |
436 | |||
437 |
2/2✓ Branch 0 taken 225 times.
✓ Branch 1 taken 15 times.
|
240 | for (i = 0; i < SCREEN_ROWS; i++) { |
438 |
4/4✓ Branch 0 taken 160 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 130 times.
|
225 | if (i > ctx->cursor_row - keep_lines && i <= ctx->cursor_row) |
439 | 30 | continue; | |
440 | 195 | UNSET_FLAG(screen->row_used, i); | |
441 | } | ||
442 | |||
443 |
4/4✓ Branch 0 taken 26 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 4 times.
|
37 | for (i = 0; i < keep_lines && screen->row_used; i++) { |
444 | 22 | const int i_row = ctx->cursor_row - keep_lines + i + 1; | |
445 | |||
446 | 22 | memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS); | |
447 | 22 | memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS); | |
448 | 22 | memcpy(screen->bgs[i_row], screen->bgs[i_row+1], SCREEN_COLUMNS); | |
449 | 22 | memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS); | |
450 | 22 | memcpy(screen->charsets[i_row], screen->charsets[i_row+1], SCREEN_COLUMNS); | |
451 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | if (CHECK_FLAG(screen->row_used, i_row + 1)) |
452 | 11 | SET_FLAG(screen->row_used, i_row); | |
453 | } | ||
454 | |||
455 | 15 | UNSET_FLAG(screen->row_used, ctx->cursor_row); | |
456 | } | ||
457 | |||
458 | 227 | static int capture_screen(CCaptionSubContext *ctx) | |
459 | { | ||
460 | 227 | int i, j, tab = 0; | |
461 | 227 | struct Screen *screen = ctx->screen + ctx->active_screen; | |
462 | 227 | enum cc_font prev_font = CCFONT_REGULAR; | |
463 | 227 | enum cc_color_code prev_color = CCCOL_WHITE; | |
464 | 227 | enum cc_color_code prev_bg_color = CCCOL_BLACK; | |
465 | 227 | const int bidx = ctx->buffer_index; | |
466 | |||
467 | 227 | av_bprint_clear(&ctx->buffer[bidx]); | |
468 | |||
469 |
4/4✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 1815 times.
✓ Branch 3 taken 121 times.
|
2042 | for (i = 0; screen->row_used && i < SCREEN_ROWS; i++) |
470 | { | ||
471 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 1583 times.
|
1815 | if (CHECK_FLAG(screen->row_used, i)) { |
472 | 232 | const char *row = screen->characters[i]; | |
473 | 232 | const char *charset = screen->charsets[i]; | |
474 | 232 | j = 0; | |
475 |
3/4✓ Branch 0 taken 1164 times.
✓ Branch 1 taken 232 times.
✓ Branch 2 taken 1164 times.
✗ Branch 3 not taken.
|
1396 | while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN) |
476 | 1164 | j++; | |
477 |
4/4✓ Branch 0 taken 58 times.
✓ Branch 1 taken 174 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 52 times.
|
232 | if (!tab || j < tab) |
478 | 180 | tab = j; | |
479 | } | ||
480 | } | ||
481 | |||
482 |
4/4✓ Branch 0 taken 1936 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 1815 times.
✓ Branch 3 taken 121 times.
|
2042 | for (i = 0; screen->row_used && i < SCREEN_ROWS; i++) |
483 | { | ||
484 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 1583 times.
|
1815 | if (CHECK_FLAG(screen->row_used, i)) { |
485 | 232 | const char *row = screen->characters[i]; | |
486 | 232 | const char *font = screen->fonts[i]; | |
487 | 232 | const char *bg = screen->bgs[i]; | |
488 | 232 | const char *color = screen->colors[i]; | |
489 | 232 | const char *charset = screen->charsets[i]; | |
490 | const char *override; | ||
491 | 232 | int x, y, seen_char = 0; | |
492 | 232 | j = 0; | |
493 | |||
494 | /* skip leading space */ | ||
495 |
5/6✓ Branch 0 taken 1003 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 1003 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 971 times.
✓ Branch 5 taken 32 times.
|
1203 | while (row[j] == ' ' && charset[j] == CCSET_BASIC_AMERICAN && j < tab) |
496 | 971 | j++; | |
497 | |||
498 | 232 | x = ASS_DEFAULT_PLAYRESX * (0.1 + 0.0250 * j); | |
499 | 232 | y = ASS_DEFAULT_PLAYRESY * (0.1 + 0.0533 * i); | |
500 | 232 | av_bprintf(&ctx->buffer[bidx], "{\\an7}{\\pos(%d,%d)}", x, y); | |
501 | |||
502 |
2/2✓ Branch 0 taken 4186 times.
✓ Branch 1 taken 9 times.
|
4195 | for (; j < SCREEN_COLUMNS; j++) { |
503 | 4186 | const char *e_tag = "", *s_tag = "", *c_tag = "", *b_tag = ""; | |
504 | |||
505 |
2/2✓ Branch 0 taken 223 times.
✓ Branch 1 taken 3963 times.
|
4186 | if (row[j] == 0) |
506 | 223 | break; | |
507 | |||
508 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 3899 times.
|
3963 | if (prev_font != font[j]) { |
509 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
|
64 | switch (prev_font) { |
510 | 20 | case CCFONT_ITALICS: | |
511 | 20 | e_tag = "{\\i0}"; | |
512 | 20 | break; | |
513 | ✗ | case CCFONT_UNDERLINED: | |
514 | ✗ | e_tag = "{\\u0}"; | |
515 | ✗ | break; | |
516 | ✗ | case CCFONT_UNDERLINED_ITALICS: | |
517 | ✗ | e_tag = "{\\u0}{\\i0}"; | |
518 | ✗ | break; | |
519 | } | ||
520 |
2/4✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
64 | switch (font[j]) { |
521 | 44 | case CCFONT_ITALICS: | |
522 | 44 | s_tag = "{\\i1}"; | |
523 | 44 | break; | |
524 | ✗ | case CCFONT_UNDERLINED: | |
525 | ✗ | s_tag = "{\\u1}"; | |
526 | ✗ | break; | |
527 | ✗ | case CCFONT_UNDERLINED_ITALICS: | |
528 | ✗ | s_tag = "{\\u1}{\\i1}"; | |
529 | ✗ | break; | |
530 | } | ||
531 | } | ||
532 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3963 times.
|
3963 | if (prev_color != color[j]) { |
533 | ✗ | switch (color[j]) { | |
534 | ✗ | case CCCOL_WHITE: | |
535 | ✗ | c_tag = "{\\c&HFFFFFF&}"; | |
536 | ✗ | break; | |
537 | ✗ | case CCCOL_GREEN: | |
538 | ✗ | c_tag = "{\\c&H00FF00&}"; | |
539 | ✗ | break; | |
540 | ✗ | case CCCOL_BLUE: | |
541 | ✗ | c_tag = "{\\c&HFF0000&}"; | |
542 | ✗ | break; | |
543 | ✗ | case CCCOL_CYAN: | |
544 | ✗ | c_tag = "{\\c&HFFFF00&}"; | |
545 | ✗ | break; | |
546 | ✗ | case CCCOL_RED: | |
547 | ✗ | c_tag = "{\\c&H0000FF&}"; | |
548 | ✗ | break; | |
549 | ✗ | case CCCOL_YELLOW: | |
550 | ✗ | c_tag = "{\\c&H00FFFF&}"; | |
551 | ✗ | break; | |
552 | ✗ | case CCCOL_MAGENTA: | |
553 | ✗ | c_tag = "{\\c&HFF00FF&}"; | |
554 | ✗ | break; | |
555 | } | ||
556 | } | ||
557 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3963 times.
|
3963 | if (prev_bg_color != bg[j]) { |
558 | ✗ | switch (bg[j]) { | |
559 | ✗ | case CCCOL_WHITE: | |
560 | ✗ | b_tag = "{\\3c&HFFFFFF&}"; | |
561 | ✗ | break; | |
562 | ✗ | case CCCOL_GREEN: | |
563 | ✗ | b_tag = "{\\3c&H00FF00&}"; | |
564 | ✗ | break; | |
565 | ✗ | case CCCOL_BLUE: | |
566 | ✗ | b_tag = "{\\3c&HFF0000&}"; | |
567 | ✗ | break; | |
568 | ✗ | case CCCOL_CYAN: | |
569 | ✗ | b_tag = "{\\3c&HFFFF00&}"; | |
570 | ✗ | break; | |
571 | ✗ | case CCCOL_RED: | |
572 | ✗ | b_tag = "{\\3c&H0000FF&}"; | |
573 | ✗ | break; | |
574 | ✗ | case CCCOL_YELLOW: | |
575 | ✗ | b_tag = "{\\3c&H00FFFF&}"; | |
576 | ✗ | break; | |
577 | ✗ | case CCCOL_MAGENTA: | |
578 | ✗ | b_tag = "{\\3c&HFF00FF&}"; | |
579 | ✗ | break; | |
580 | ✗ | case CCCOL_BLACK: | |
581 | ✗ | b_tag = "{\\3c&H000000&}"; | |
582 | ✗ | break; | |
583 | } | ||
584 | } | ||
585 | |||
586 | 3963 | prev_font = font[j]; | |
587 | 3963 | prev_color = color[j]; | |
588 | 3963 | prev_bg_color = bg[j]; | |
589 | 3963 | override = charset_overrides[(int)charset[j]][(int)row[j]]; | |
590 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3952 times.
|
3963 | if (override[0]) { |
591 | 11 | av_bprintf(&ctx->buffer[bidx], "%s%s%s%s%s", e_tag, s_tag, c_tag, b_tag, override); | |
592 | 11 | seen_char = 1; | |
593 |
4/4✓ Branch 0 taken 823 times.
✓ Branch 1 taken 3129 times.
✓ Branch 2 taken 193 times.
✓ Branch 3 taken 630 times.
|
3952 | } else if (row[j] == ' ' && !seen_char) { |
594 | 193 | av_bprintf(&ctx->buffer[bidx], "%s%s%s%s\\h", e_tag, s_tag, c_tag, b_tag); | |
595 | } else { | ||
596 | 3759 | av_bprintf(&ctx->buffer[bidx], "%s%s%s%s%c", e_tag, s_tag, c_tag, b_tag, row[j]); | |
597 | 3759 | seen_char = 1; | |
598 | } | ||
599 | |||
600 | } | ||
601 | 232 | av_bprintf(&ctx->buffer[bidx], "\\N"); | |
602 | } | ||
603 | } | ||
604 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
|
227 | if (!av_bprint_is_complete(&ctx->buffer[bidx])) |
605 | ✗ | return AVERROR(ENOMEM); | |
606 |
3/4✓ Branch 0 taken 121 times.
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 121 times.
✗ Branch 3 not taken.
|
227 | if (screen->row_used && ctx->buffer[bidx].len >= 2) { |
607 | 121 | ctx->buffer[bidx].len -= 2; | |
608 | 121 | ctx->buffer[bidx].str[ctx->buffer[bidx].len] = 0; | |
609 | } | ||
610 | 227 | ctx->buffer_changed = 1; | |
611 | 227 | return 0; | |
612 | } | ||
613 | |||
614 | 203 | static void update_time(CCaptionSubContext *ctx, int64_t pts) | |
615 | { | ||
616 | 203 | ctx->buffer_time[0] = ctx->buffer_time[1]; | |
617 | 203 | ctx->buffer_time[1] = pts; | |
618 | 203 | } | |
619 | |||
620 | ✗ | static void handle_bgattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
621 | { | ||
622 | ✗ | const int i = (lo & 0xf) >> 1; | |
623 | |||
624 | ✗ | ctx->bg_color = bg_attribs[i]; | |
625 | ✗ | } | |
626 | |||
627 | 15 | static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
628 | { | ||
629 | 15 | int i = lo - 0x20; | |
630 | 15 | struct Screen *screen = get_writing_screen(ctx); | |
631 | |||
632 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (i >= 32) |
633 | ✗ | return; | |
634 | |||
635 | 15 | ctx->cursor_color = pac2_attribs[i][0]; | |
636 | 15 | ctx->cursor_font = pac2_attribs[i][1]; | |
637 | |||
638 | 15 | SET_FLAG(screen->row_used, ctx->cursor_row); | |
639 | 15 | write_char(ctx, screen, ' '); | |
640 | } | ||
641 | |||
642 | 203 | static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
643 | { | ||
644 | static const int8_t row_map[] = { | ||
645 | 11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10 | ||
646 | }; | ||
647 | 203 | const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 ); | |
648 | 203 | struct Screen *screen = get_writing_screen(ctx); | |
649 | int indent, i; | ||
650 | |||
651 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
|
203 | if (row_map[index] <= 0) { |
652 | ✗ | av_log(ctx, AV_LOG_DEBUG, "Invalid pac index encountered\n"); | |
653 | ✗ | return; | |
654 | } | ||
655 | |||
656 | 203 | lo &= 0x1f; | |
657 | |||
658 | 203 | ctx->cursor_row = row_map[index] - 1; | |
659 | 203 | ctx->cursor_color = pac2_attribs[lo][0]; | |
660 | 203 | ctx->cursor_font = pac2_attribs[lo][1]; | |
661 | 203 | ctx->cursor_charset = CCSET_BASIC_AMERICAN; | |
662 | 203 | ctx->cursor_column = 0; | |
663 | 203 | indent = pac2_attribs[lo][2]; | |
664 |
2/2✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 203 times.
|
1339 | for (i = 0; i < indent; i++) { |
665 | 1136 | write_char(ctx, screen, ' '); | |
666 | } | ||
667 | } | ||
668 | |||
669 | 197 | static int handle_edm(CCaptionSubContext *ctx) | |
670 | { | ||
671 | 197 | struct Screen *screen = ctx->screen + ctx->active_screen; | |
672 | int ret; | ||
673 | |||
674 | // In buffered mode, keep writing to screen until it is wiped. | ||
675 | // Before wiping the display, capture contents to emit subtitle. | ||
676 |
1/2✓ Branch 0 taken 197 times.
✗ Branch 1 not taken.
|
197 | if (!ctx->real_time) |
677 | 197 | ret = capture_screen(ctx); | |
678 | |||
679 | 197 | screen->row_used = 0; | |
680 | 197 | ctx->bg_color = CCCOL_BLACK; | |
681 | |||
682 | // In realtime mode, emit an empty caption so the last one doesn't | ||
683 | // stay on the screen. | ||
684 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
|
197 | if (ctx->real_time) |
685 | ✗ | ret = capture_screen(ctx); | |
686 | |||
687 | 197 | return ret; | |
688 | } | ||
689 | |||
690 | 93 | static int handle_eoc(CCaptionSubContext *ctx) | |
691 | { | ||
692 | int ret; | ||
693 | |||
694 | 93 | ctx->active_screen = !ctx->active_screen; | |
695 | |||
696 | // In buffered mode, we wait til the *next* EOC and | ||
697 | // capture what was already on the screen since the last EOC. | ||
698 |
1/2✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
|
93 | if (!ctx->real_time) |
699 | 93 | ret = handle_edm(ctx); | |
700 | |||
701 | 93 | ctx->cursor_column = 0; | |
702 | |||
703 | // In realtime mode, we display the buffered contents (after | ||
704 | // flipping the buffer to active above) as soon as EOC arrives. | ||
705 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ctx->real_time) |
706 | ✗ | ret = capture_screen(ctx); | |
707 | |||
708 | 93 | return ret; | |
709 | } | ||
710 | |||
711 | ✗ | static void handle_delete_end_of_row(CCaptionSubContext *ctx) | |
712 | { | ||
713 | ✗ | struct Screen *screen = get_writing_screen(ctx); | |
714 | ✗ | write_char(ctx, screen, 0); | |
715 | ✗ | } | |
716 | |||
717 | 1656 | static void handle_char(CCaptionSubContext *ctx, char hi, char lo) | |
718 | { | ||
719 | 1656 | struct Screen *screen = get_writing_screen(ctx); | |
720 | |||
721 | 1656 | SET_FLAG(screen->row_used, ctx->cursor_row); | |
722 | |||
723 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1656 times.
|
1656 | switch (hi) { |
724 | ✗ | case 0x11: | |
725 | ✗ | ctx->cursor_charset = CCSET_SPECIAL_AMERICAN; | |
726 | ✗ | break; | |
727 | ✗ | case 0x12: | |
728 | ✗ | if (ctx->cursor_column > 0) | |
729 | ✗ | ctx->cursor_column -= 1; | |
730 | ✗ | ctx->cursor_charset = CCSET_EXTENDED_SPANISH_FRENCH_MISC; | |
731 | ✗ | break; | |
732 | ✗ | case 0x13: | |
733 | ✗ | if (ctx->cursor_column > 0) | |
734 | ✗ | ctx->cursor_column -= 1; | |
735 | ✗ | ctx->cursor_charset = CCSET_EXTENDED_PORTUGUESE_GERMAN_DANISH; | |
736 | ✗ | break; | |
737 | 1656 | default: | |
738 | 1656 | ctx->cursor_charset = CCSET_BASIC_AMERICAN; | |
739 | 1656 | write_char(ctx, screen, hi); | |
740 | 1656 | break; | |
741 | } | ||
742 | |||
743 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 120 times.
|
1656 | if (lo) { |
744 | 1536 | write_char(ctx, screen, lo); | |
745 | } | ||
746 | 1656 | write_char(ctx, screen, 0); | |
747 | |||
748 |
2/2✓ Branch 0 taken 175 times.
✓ Branch 1 taken 1481 times.
|
1656 | if (ctx->mode != CCMODE_POPON) |
749 | 175 | ctx->screen_touched = 1; | |
750 | |||
751 | if (lo) | ||
752 | ff_dlog(ctx, "(%c,%c)\n", hi, lo); | ||
753 | else | ||
754 | ff_dlog(ctx, "(%c)\n", hi); | ||
755 | 1656 | } | |
756 | |||
757 | 2863 | static int process_cc608(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo) | |
758 | { | ||
759 | 2863 | int ret = 0; | |
760 | |||
761 |
4/4✓ Branch 0 taken 942 times.
✓ Branch 1 taken 1921 times.
✓ Branch 2 taken 670 times.
✓ Branch 3 taken 272 times.
|
2863 | if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) { |
762 | 670 | return 0; | |
763 | } | ||
764 | |||
765 | /* set prev command */ | ||
766 | 2193 | ctx->prev_cmd[0] = hi; | |
767 | 2193 | ctx->prev_cmd[1] = lo; | |
768 | |||
769 |
2/8✗ Branch 0 not taken.
✓ Branch 1 taken 2193 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2193 times.
✗ Branch 7 not taken.
|
2193 | if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) || |
770 |
5/6✓ Branch 0 taken 537 times.
✓ Branch 1 taken 1656 times.
✓ Branch 2 taken 203 times.
✓ Branch 3 taken 334 times.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
|
2193 | ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) { |
771 | 203 | handle_pac(ctx, hi, lo); | |
772 |
5/8✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1975 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1975 times.
|
1990 | } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) || |
773 | ✗ | ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) { | |
774 | 15 | handle_textattr(ctx, hi, lo); | |
775 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1975 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1975 | } else if ((hi == 0x10 && lo >= 0x20 && lo <= 0x2f)) { |
776 | ✗ | handle_bgattr(ctx, hi, lo); | |
777 |
4/6✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 319 times.
✓ Branch 2 taken 1656 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1656 times.
|
1975 | } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) { |
778 |
5/10✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 104 times.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 93 times.
✗ Branch 9 not taken.
|
319 | switch (lo) { |
779 | 92 | case 0x20: | |
780 | /* resume caption loading */ | ||
781 | 92 | ctx->mode = CCMODE_POPON; | |
782 | 92 | break; | |
783 | ✗ | case 0x24: | |
784 | ✗ | handle_delete_end_of_row(ctx); | |
785 | ✗ | break; | |
786 | 15 | case 0x25: | |
787 | case 0x26: | ||
788 | case 0x27: | ||
789 | 15 | ctx->rollup = lo - 0x23; | |
790 | 15 | ctx->mode = CCMODE_ROLLUP; | |
791 | 15 | break; | |
792 | ✗ | case 0x29: | |
793 | /* resume direct captioning */ | ||
794 | ✗ | ctx->mode = CCMODE_PAINTON; | |
795 | ✗ | break; | |
796 | ✗ | case 0x2b: | |
797 | /* resume text display */ | ||
798 | ✗ | ctx->mode = CCMODE_TEXT; | |
799 | ✗ | break; | |
800 | 104 | case 0x2c: | |
801 | /* erase display memory */ | ||
802 | 104 | handle_edm(ctx); | |
803 | 104 | break; | |
804 | 15 | case 0x2d: | |
805 | /* carriage return */ | ||
806 | ff_dlog(ctx, "carriage return\n"); | ||
807 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
|
15 | if (!ctx->real_time) |
808 | 6 | ret = capture_screen(ctx); | |
809 | 15 | roll_up(ctx); | |
810 | 15 | ctx->cursor_column = 0; | |
811 | 15 | break; | |
812 | ✗ | case 0x2e: | |
813 | /* erase buffered (non displayed) memory */ | ||
814 | // Only in realtime mode. In buffered mode, we re-use the inactive screen | ||
815 | // for our own buffering. | ||
816 | ✗ | if (ctx->real_time) { | |
817 | ✗ | struct Screen *screen = ctx->screen + !ctx->active_screen; | |
818 | ✗ | screen->row_used = 0; | |
819 | } | ||
820 | ✗ | break; | |
821 | 93 | case 0x2f: | |
822 | /* end of caption */ | ||
823 | ff_dlog(ctx, "handle_eoc\n"); | ||
824 | 93 | ret = handle_eoc(ctx); | |
825 | 93 | break; | |
826 | ✗ | default: | |
827 | ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo); | ||
828 | ✗ | break; | |
829 | } | ||
830 |
2/4✓ Branch 0 taken 1656 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1656 times.
|
1656 | } else if (hi >= 0x11 && hi <= 0x13) { |
831 | /* Special characters */ | ||
832 | ✗ | handle_char(ctx, hi, lo); | |
833 |
1/2✓ Branch 0 taken 1656 times.
✗ Branch 1 not taken.
|
1656 | } else if (hi >= 0x20) { |
834 | /* Standard characters (always in pairs) */ | ||
835 | 1656 | handle_char(ctx, hi, lo); | |
836 | 1656 | ctx->prev_cmd[0] = ctx->prev_cmd[1] = 0; | |
837 | ✗ | } else if (hi == 0x17 && lo >= 0x21 && lo <= 0x23) { | |
838 | int i; | ||
839 | /* Tab offsets (spacing) */ | ||
840 | ✗ | for (i = 0; i < lo - 0x20; i++) { | |
841 | ✗ | handle_char(ctx, ' ', 0); | |
842 | } | ||
843 | } else { | ||
844 | /* Ignoring all other non data code */ | ||
845 | ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo); | ||
846 | } | ||
847 | |||
848 | 2193 | return ret; | |
849 | } | ||
850 | |||
851 | 852 | static int decode(AVCodecContext *avctx, AVSubtitle *sub, | |
852 | int *got_sub, const AVPacket *avpkt) | ||
853 | { | ||
854 | 852 | CCaptionSubContext *ctx = avctx->priv_data; | |
855 | 852 | int64_t in_time = sub->pts; | |
856 | int64_t start_time; | ||
857 | int64_t end_time; | ||
858 | 852 | int bidx = ctx->buffer_index; | |
859 | 852 | const uint8_t *bptr = avpkt->data; | |
860 | 852 | int len = avpkt->size; | |
861 | 852 | int ret = 0; | |
862 | int i; | ||
863 | 852 | unsigned nb_rect_allocated = 0; | |
864 | |||
865 |
2/2✓ Branch 0 taken 13817 times.
✓ Branch 1 taken 852 times.
|
14669 | for (i = 0; i < len; i += 3) { |
866 | 13817 | uint8_t hi, cc_type = bptr[i] & 1; | |
867 | |||
868 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13811 times.
|
13817 | if (ctx->data_field < 0) |
869 | 6 | ctx->data_field = cc_type; | |
870 | |||
871 |
2/2✓ Branch 1 taken 10752 times.
✓ Branch 2 taken 3065 times.
|
13817 | if (validate_cc_data_pair(bptr + i, &hi)) |
872 | 13614 | continue; | |
873 | |||
874 |
2/2✓ Branch 0 taken 202 times.
✓ Branch 1 taken 2863 times.
|
3065 | if (cc_type != ctx->data_field) |
875 | 202 | continue; | |
876 | |||
877 | 2863 | ret = process_cc608(ctx, hi & 0x7f, bptr[i + 2] & 0x7f); | |
878 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2863 times.
|
2863 | if (ret < 0) |
879 | ✗ | return ret; | |
880 | |||
881 |
2/2✓ Branch 0 taken 2660 times.
✓ Branch 1 taken 203 times.
|
2863 | if (!ctx->buffer_changed) |
882 | 2660 | continue; | |
883 | 203 | ctx->buffer_changed = 0; | |
884 | |||
885 |
3/4✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 197 times.
✓ Branch 3 taken 6 times.
|
203 | if (!ctx->real_time && ctx->mode == CCMODE_POPON) |
886 | 197 | ctx->buffer_index = bidx = !ctx->buffer_index; | |
887 | |||
888 | 203 | update_time(ctx, in_time); | |
889 | |||
890 |
3/4✓ Branch 0 taken 106 times.
✓ Branch 1 taken 97 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 106 times.
|
203 | if (ctx->buffer[bidx].str[0] || ctx->real_time) { |
891 | ff_dlog(ctx, "cdp writing data (%s)\n", ctx->buffer[bidx].str); | ||
892 | 97 | start_time = ctx->buffer_time[0]; | |
893 | 97 | sub->pts = start_time; | |
894 | 97 | end_time = ctx->buffer_time[1]; | |
895 |
1/2✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
|
97 | if (!ctx->real_time) |
896 | 97 | sub->end_display_time = av_rescale_q(end_time - start_time, | |
897 | 97 | AV_TIME_BASE_Q, ms_tb); | |
898 | else | ||
899 | ✗ | sub->end_display_time = -1; | |
900 | 97 | ret = ff_ass_add_rect2(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL, &nb_rect_allocated); | |
901 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
|
97 | if (ret < 0) |
902 | ✗ | return ret; | |
903 | 97 | ctx->last_real_time = sub->pts; | |
904 | 97 | ctx->screen_touched = 0; | |
905 | } | ||
906 | } | ||
907 | |||
908 |
5/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 846 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
852 | if (!bptr && !ctx->real_time && ctx->buffer[!ctx->buffer_index].str[0]) { |
909 | ✗ | bidx = !ctx->buffer_index; | |
910 | ✗ | ret = ff_ass_add_rect2(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL, &nb_rect_allocated); | |
911 | ✗ | if (ret < 0) | |
912 | ✗ | return ret; | |
913 | ✗ | av_bprint_clear(&ctx->buffer[bidx]); | |
914 | ✗ | sub->pts = ctx->buffer_time[1]; | |
915 | ✗ | sub->end_display_time = av_rescale_q(ctx->buffer_time[1] - ctx->buffer_time[0], | |
916 | ✗ | AV_TIME_BASE_Q, ms_tb); | |
917 | ✗ | if (sub->end_display_time == 0) | |
918 | ✗ | sub->end_display_time = ctx->buffer[bidx].len * 20; | |
919 | } | ||
920 | |||
921 |
4/4✓ Branch 0 taken 408 times.
✓ Branch 1 taken 444 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 294 times.
|
852 | if (ctx->real_time && ctx->screen_touched && |
922 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 90 times.
|
114 | sub->pts >= ctx->last_real_time + av_rescale_q(ctx->real_time_latency_msec, ms_tb, AV_TIME_BASE_Q)) { |
923 | 24 | ctx->last_real_time = sub->pts; | |
924 | 24 | ctx->screen_touched = 0; | |
925 | |||
926 | 24 | capture_screen(ctx); | |
927 | 24 | ctx->buffer_changed = 0; | |
928 | |||
929 | 24 | ret = ff_ass_add_rect2(sub, ctx->buffer[bidx].str, ctx->readorder++, 0, NULL, NULL, &nb_rect_allocated); | |
930 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (ret < 0) |
931 | ✗ | return ret; | |
932 | 24 | sub->end_display_time = -1; | |
933 | } | ||
934 | |||
935 | 852 | *got_sub = sub->num_rects > 0; | |
936 | 852 | return avpkt->size; | |
937 | } | ||
938 | |||
939 | #define OFFSET(x) offsetof(CCaptionSubContext, x) | ||
940 | #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM | ||
941 | static const AVOption options[] = { | ||
942 | { "real_time", "emit subtitle events as they are decoded for real-time display", OFFSET(real_time), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD }, | ||
943 | { "real_time_latency_msec", "minimum elapsed time between emitting real-time subtitle events", OFFSET(real_time_latency_msec), AV_OPT_TYPE_INT, { .i64 = 200 }, 0, 500, SD }, | ||
944 | { "data_field", "select data field", OFFSET(data_field), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, SD, .unit = "data_field" }, | ||
945 | { "auto", "pick first one that appears", 0, AV_OPT_TYPE_CONST, { .i64 =-1 }, 0, 0, SD, .unit = "data_field" }, | ||
946 | { "first", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, SD, .unit = "data_field" }, | ||
947 | { "second", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, SD, .unit = "data_field" }, | ||
948 | {NULL} | ||
949 | }; | ||
950 | |||
951 | static const AVClass ccaption_dec_class = { | ||
952 | .class_name = "Closed Captions Decoder", | ||
953 | .item_name = av_default_item_name, | ||
954 | .option = options, | ||
955 | .version = LIBAVUTIL_VERSION_INT, | ||
956 | }; | ||
957 | |||
958 | const FFCodec ff_ccaption_decoder = { | ||
959 | .p.name = "cc_dec", | ||
960 | CODEC_LONG_NAME("Closed Captions (EIA-608 / CEA-708)"), | ||
961 | .p.type = AVMEDIA_TYPE_SUBTITLE, | ||
962 | .p.id = AV_CODEC_ID_EIA_608, | ||
963 | .p.priv_class = &ccaption_dec_class, | ||
964 | .p.capabilities = AV_CODEC_CAP_DELAY, | ||
965 | .priv_data_size = sizeof(CCaptionSubContext), | ||
966 | .init = init_decoder, | ||
967 | .close = close_decoder, | ||
968 | .flush = flush_decoder, | ||
969 | FF_CODEC_DECODE_SUB_CB(decode), | ||
970 | }; | ||
971 |