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