]> git.sesse.net Git - ffmpeg/blob - libavcodec/ccaption_dec.c
lavc/ccaption_dec: clean up whitespace
[ffmpeg] / libavcodec / ccaption_dec.c
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 "libavutil/opt.h"
25
26 #define SCREEN_ROWS 15
27 #define SCREEN_COLUMNS 32
28
29 #define SET_FLAG(var, val)   ( (var) |=   ( 1 << (val)) )
30 #define UNSET_FLAG(var, val) ( (var) &=  ~( 1 << (val)) )
31 #define CHECK_FLAG(var, val) ( (var) &    ( 1 << (val)) )
32
33 /*
34  * TODO list
35  * 1) handle font and color completely
36  */
37 enum cc_mode {
38     CCMODE_POPON,
39     CCMODE_PAINTON,
40     CCMODE_ROLLUP_2,
41     CCMODE_ROLLUP_3,
42     CCMODE_ROLLUP_4,
43     CCMODE_TEXT,
44 };
45
46 enum cc_color_code {
47     CCCOL_WHITE,
48     CCCOL_GREEN,
49     CCCOL_BLUE,
50     CCCOL_CYAN,
51     CCCOL_RED,
52     CCCOL_YELLOW,
53     CCCOL_MAGENTA,
54     CCCOL_USERDEFINED,
55     CCCOL_BLACK,
56     CCCOL_TRANSPARENT,
57 };
58
59 enum cc_font {
60     CCFONT_REGULAR,
61     CCFONT_ITALICS,
62     CCFONT_UNDERLINED,
63     CCFONT_UNDERLINED_ITALICS,
64 };
65
66 static const unsigned char pac2_attribs[32][3] = // Color, font, ident
67 {
68     { CCCOL_WHITE,   CCFONT_REGULAR,            0 },  // 0x40 || 0x60
69     { CCCOL_WHITE,   CCFONT_UNDERLINED,         0 },  // 0x41 || 0x61
70     { CCCOL_GREEN,   CCFONT_REGULAR,            0 },  // 0x42 || 0x62
71     { CCCOL_GREEN,   CCFONT_UNDERLINED,         0 },  // 0x43 || 0x63
72     { CCCOL_BLUE,    CCFONT_REGULAR,            0 },  // 0x44 || 0x64
73     { CCCOL_BLUE,    CCFONT_UNDERLINED,         0 },  // 0x45 || 0x65
74     { CCCOL_CYAN,    CCFONT_REGULAR,            0 },  // 0x46 || 0x66
75     { CCCOL_CYAN,    CCFONT_UNDERLINED,         0 },  // 0x47 || 0x67
76     { CCCOL_RED,     CCFONT_REGULAR,            0 },  // 0x48 || 0x68
77     { CCCOL_RED,     CCFONT_UNDERLINED,         0 },  // 0x49 || 0x69
78     { CCCOL_YELLOW,  CCFONT_REGULAR,            0 },  // 0x4a || 0x6a
79     { CCCOL_YELLOW,  CCFONT_UNDERLINED,         0 },  // 0x4b || 0x6b
80     { CCCOL_MAGENTA, CCFONT_REGULAR,            0 },  // 0x4c || 0x6c
81     { CCCOL_MAGENTA, CCFONT_UNDERLINED,         0 },  // 0x4d || 0x6d
82     { CCCOL_WHITE,   CCFONT_ITALICS,            0 },  // 0x4e || 0x6e
83     { CCCOL_WHITE,   CCFONT_UNDERLINED_ITALICS, 0 },  // 0x4f || 0x6f
84     { CCCOL_WHITE,   CCFONT_REGULAR,            0 },  // 0x50 || 0x70
85     { CCCOL_WHITE,   CCFONT_UNDERLINED,         0 },  // 0x51 || 0x71
86     { CCCOL_WHITE,   CCFONT_REGULAR,            4 },  // 0x52 || 0x72
87     { CCCOL_WHITE,   CCFONT_UNDERLINED,         4 },  // 0x53 || 0x73
88     { CCCOL_WHITE,   CCFONT_REGULAR,            8 },  // 0x54 || 0x74
89     { CCCOL_WHITE,   CCFONT_UNDERLINED,         8 },  // 0x55 || 0x75
90     { CCCOL_WHITE,   CCFONT_REGULAR,           12 },  // 0x56 || 0x76
91     { CCCOL_WHITE,   CCFONT_UNDERLINED,        12 },  // 0x57 || 0x77
92     { CCCOL_WHITE,   CCFONT_REGULAR,           16 },  // 0x58 || 0x78
93     { CCCOL_WHITE,   CCFONT_UNDERLINED,        16 },  // 0x59 || 0x79
94     { CCCOL_WHITE,   CCFONT_REGULAR,           20 },  // 0x5a || 0x7a
95     { CCCOL_WHITE,   CCFONT_UNDERLINED,        20 },  // 0x5b || 0x7b
96     { CCCOL_WHITE,   CCFONT_REGULAR,           24 },  // 0x5c || 0x7c
97     { CCCOL_WHITE,   CCFONT_UNDERLINED,        24 },  // 0x5d || 0x7d
98     { CCCOL_WHITE,   CCFONT_REGULAR,           28 },  // 0x5e || 0x7e
99     { CCCOL_WHITE,   CCFONT_UNDERLINED,        28 }   // 0x5f || 0x7f
100     /* total 32 entries */
101 };
102
103 struct Screen {
104     /* +1 is used to compensate null character of string */
105     uint8_t characters[SCREEN_ROWS][SCREEN_COLUMNS+1];
106     uint8_t colors[SCREEN_ROWS][SCREEN_COLUMNS+1];
107     uint8_t fonts[SCREEN_ROWS][SCREEN_COLUMNS+1];
108     /*
109      * Bitmask of used rows; if a bit is not set, the
110      * corresponding row is not used.
111      * for setting row 1  use row | (1 << 0)
112      * for setting row 15 use row | (1 << 14)
113      */
114     int16_t row_used;
115 };
116
117 typedef struct CCaptionSubContext {
118     AVClass *class;
119     struct Screen screen[2];
120     int active_screen;
121     uint8_t cursor_row;
122     uint8_t cursor_column;
123     uint8_t cursor_color;
124     uint8_t cursor_font;
125     AVBPrint buffer;
126     int screen_changed;
127     int rollup;
128     enum cc_mode mode;
129     int64_t start_time;
130     /* visible screen time */
131     int64_t startv_time;
132     int64_t end_time;
133     char prev_cmd[2];
134     /* buffer to store pkt data */
135     AVBufferRef *pktbuf;
136 } CCaptionSubContext;
137
138
139 static av_cold int init_decoder(AVCodecContext *avctx)
140 {
141     int ret;
142     CCaptionSubContext *ctx = avctx->priv_data;
143
144     av_bprint_init(&ctx->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
145     /* taking by default roll up to 2 */
146     ctx->mode = CCMODE_ROLLUP_2;
147     ctx->rollup = 2;
148     ret = ff_ass_subtitle_header_default(avctx);
149     if (ret < 0) {
150         return ret;
151     }
152     /* allocate pkt buffer */
153     ctx->pktbuf = av_buffer_alloc(128);
154     if (!ctx->pktbuf) {
155         ret = AVERROR(ENOMEM);
156     }
157     return ret;
158 }
159
160 static av_cold int close_decoder(AVCodecContext *avctx)
161 {
162     CCaptionSubContext *ctx = avctx->priv_data;
163     av_bprint_finalize(&ctx->buffer, NULL);
164     av_buffer_unref(&ctx->pktbuf);
165     return 0;
166 }
167
168 /**
169  * @param ctx closed caption context just to print log
170  */
171 static int write_char(CCaptionSubContext *ctx, char *row, uint8_t col, char ch)
172 {
173     if (col < SCREEN_COLUMNS) {
174         row[col] = ch;
175         return 0;
176     }
177     /* We have extra space at end only for null character */
178     else if (col == SCREEN_COLUMNS && ch == 0) {
179         row[col] = ch;
180         return 0;
181     }
182     else {
183         av_log(ctx, AV_LOG_WARNING, "Data Ignored since exceeding screen width\n");
184         return AVERROR_INVALIDDATA;
185     }
186 }
187
188 /**
189  * This function after validating parity bit, also remove it from data pair.
190  * The first byte doesn't pass parity, we replace it with a solid blank
191  * and process the pair.
192  * If the second byte doesn't pass parity, it returns INVALIDDATA
193  * user can ignore the whole pair and pass the other pair.
194  */
195 static int validate_cc_data_pair(uint8_t *cc_data_pair)
196 {
197     uint8_t cc_valid = (*cc_data_pair & 4) >>2;
198     uint8_t cc_type = *cc_data_pair & 3;
199
200     if (!cc_valid)
201         return AVERROR_INVALIDDATA;
202
203     // if EIA-608 data then verify parity.
204     if (cc_type==0 || cc_type==1) {
205         if (!av_parity(cc_data_pair[2])) {
206             return AVERROR_INVALIDDATA;
207         }
208         if (!av_parity(cc_data_pair[1])) {
209             cc_data_pair[1]=0x7F;
210         }
211     }
212
213     //Skip non-data
214     if ((cc_data_pair[0] == 0xFA || cc_data_pair[0] == 0xFC || cc_data_pair[0] == 0xFD)
215          && (cc_data_pair[1] & 0x7F) == 0 && (cc_data_pair[2] & 0x7F) == 0)
216         return AVERROR_PATCHWELCOME;
217
218     //skip 708 data
219     if (cc_type == 3 || cc_type == 2)
220         return AVERROR_PATCHWELCOME;
221
222     /* remove parity bit */
223     cc_data_pair[1] &= 0x7F;
224     cc_data_pair[2] &= 0x7F;
225
226     return 0;
227 }
228
229 static struct Screen *get_writing_screen(CCaptionSubContext *ctx)
230 {
231     switch (ctx->mode) {
232     case CCMODE_POPON:
233         // use Inactive screen
234         return ctx->screen + !ctx->active_screen;
235     case CCMODE_PAINTON:
236     case CCMODE_ROLLUP_2:
237     case CCMODE_ROLLUP_3:
238     case CCMODE_ROLLUP_4:
239     case CCMODE_TEXT:
240         // use active screen
241         return ctx->screen + ctx->active_screen;
242     }
243     /* It was never an option */
244     return NULL;
245 }
246
247 static void roll_up(CCaptionSubContext *ctx)
248 {
249     struct Screen *screen;
250     int i, keep_lines;
251
252     if (ctx->mode == CCMODE_TEXT)
253         return;
254
255     screen = get_writing_screen(ctx);
256
257     /* +1 signify cursor_row starts from 0
258      * Can't keep lines less then row cursor pos
259      */
260     keep_lines = FFMIN(ctx->cursor_row + 1, ctx->rollup);
261
262     for (i = 0; i < ctx->cursor_row - keep_lines; i++)
263         UNSET_FLAG(screen->row_used, i);
264
265
266     for (i = 0; i < keep_lines && screen->row_used; i++) {
267         const int i_row = ctx->cursor_row - keep_lines + i + 1;
268
269         memcpy(screen->characters[i_row], screen->characters[i_row+1], SCREEN_COLUMNS);
270         memcpy(screen->colors[i_row], screen->colors[i_row+1], SCREEN_COLUMNS);
271         memcpy(screen->fonts[i_row], screen->fonts[i_row+1], SCREEN_COLUMNS);
272         if (CHECK_FLAG(screen->row_used, i_row + 1))
273             SET_FLAG(screen->row_used, i_row);
274
275     }
276     UNSET_FLAG(screen->row_used, ctx->cursor_row);
277 }
278
279 static int reap_screen(CCaptionSubContext *ctx, int64_t pts)
280 {
281     int i;
282     int ret = 0;
283     struct Screen *screen = ctx->screen + ctx->active_screen;
284     ctx->start_time = ctx->startv_time;
285
286     for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
287     {
288         if (CHECK_FLAG(screen->row_used, i)) {
289             char *str = screen->characters[i];
290             /* skip space */
291             while (*str == ' ')
292                 str++;
293
294             av_bprintf(&ctx->buffer, "%s\\N", str);
295             ret = av_bprint_is_complete(&ctx->buffer);
296             if (ret == 0) {
297                 ret = AVERROR(ENOMEM);
298                 break;
299             }
300         }
301
302     }
303     if (screen->row_used && ctx->buffer.len >= 2) {
304         ctx->buffer.len -= 2;
305         ctx->buffer.str[ctx->buffer.len] = 0;
306     }
307     ctx->startv_time = pts;
308     ctx->end_time = pts;
309     return ret;
310 }
311
312 static void handle_textattr(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
313 {
314     int i = lo - 0x20;
315     int ret;
316     struct Screen *screen = get_writing_screen(ctx);
317     char *row = screen->characters[ctx->cursor_row];
318
319     if (i >= 32)
320         return;
321
322     ctx->cursor_color = pac2_attribs[i][0];
323     ctx->cursor_font = pac2_attribs[i][1];
324
325     SET_FLAG(screen->row_used, ctx->cursor_row);
326     ret = write_char(ctx, row, ctx->cursor_column, ' ');
327     if (ret == 0)
328         ctx->cursor_column++;
329 }
330
331 static void handle_pac(CCaptionSubContext *ctx, uint8_t hi, uint8_t lo)
332 {
333     static const int8_t row_map[] = {
334         11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
335     };
336     const int index = ( (hi<<1) & 0x0e) | ( (lo>>5) & 0x01 );
337     struct Screen *screen = get_writing_screen(ctx);
338     char *row;
339     int indent, i, ret;
340
341     if (row_map[index] <= 0) {
342         av_log(ctx, AV_LOG_DEBUG, "Invalid pac index encountered\n");
343         return;
344     }
345
346     lo &= 0x1f;
347
348     ctx->cursor_row = row_map[index] - 1;
349     ctx->cursor_color =  pac2_attribs[lo][0];
350     ctx->cursor_font = pac2_attribs[lo][1];
351     ctx->cursor_column = 0;
352     indent = pac2_attribs[lo][2];
353     row = screen->characters[ctx->cursor_row];
354     for (i = 0; i < indent; i++) {
355         ret = write_char(ctx, row, ctx->cursor_column, ' ');
356         if (ret == 0)
357             ctx->cursor_column++;
358     }
359 }
360
361 /**
362  * @param pts it is required to set end time
363  */
364 static int handle_edm(CCaptionSubContext *ctx, int64_t pts)
365 {
366     int ret = 0;
367     struct Screen *screen = ctx->screen + ctx->active_screen;
368
369     reap_screen(ctx, pts);
370     screen->row_used = 0;
371     ctx->screen_changed = 1;
372     return ret;
373 }
374
375 static int handle_eoc(CCaptionSubContext *ctx, int64_t pts)
376 {
377     int ret;
378     ret = handle_edm(ctx,pts);
379     ctx->active_screen = !ctx->active_screen;
380     ctx->cursor_column = 0;
381     return ret;
382 }
383
384 static void handle_delete_end_of_row(CCaptionSubContext *ctx, char hi, char lo)
385 {
386     struct Screen *screen = get_writing_screen(ctx);
387     char *row = screen->characters[ctx->cursor_row];
388     write_char(ctx, row, ctx->cursor_column, 0);
389 }
390
391 static void handle_char(CCaptionSubContext *ctx, char hi, char lo, int64_t pts)
392 {
393     struct Screen *screen = get_writing_screen(ctx);
394     char *row = screen->characters[ctx->cursor_row];
395     int ret;
396
397     SET_FLAG(screen->row_used,ctx->cursor_row);
398
399     ret = write_char(ctx, row, ctx->cursor_column, hi);
400     if (ret == 0)
401         ctx->cursor_column++;
402
403     if (lo) {
404         ret = write_char(ctx, row, ctx->cursor_column, lo);
405         if (ret == 0)
406             ctx->cursor_column++;
407     }
408     write_char(ctx, row, ctx->cursor_column, 0);
409
410     /* reset prev command since character can repeat */
411     ctx->prev_cmd[0] = 0;
412     ctx->prev_cmd[1] = 0;
413     if (lo)
414        ff_dlog(ctx, "(%c,%c)\n", hi, lo);
415     else
416        ff_dlog(ctx, "(%c)\n", hi);
417 }
418
419 static int process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint8_t lo)
420 {
421     int ret = 0;
422     if (hi == ctx->prev_cmd[0] && lo == ctx->prev_cmd[1]) {
423         /* ignore redundant command */
424     } else if ( (hi == 0x10 && (lo >= 0x40 && lo <= 0x5f)) ||
425               ( (hi >= 0x11 && hi <= 0x17) && (lo >= 0x40 && lo <= 0x7f) ) ) {
426         handle_pac(ctx, hi, lo);
427     } else if ( ( hi == 0x11 && lo >= 0x20 && lo <= 0x2f ) ||
428                 ( hi == 0x17 && lo >= 0x2e && lo <= 0x2f) ) {
429         handle_textattr(ctx, hi, lo);
430     } else if (hi == 0x14 || hi == 0x15 || hi == 0x1c) {
431         switch (lo) {
432         case 0x20:
433             /* resume caption loading */
434             ctx->mode = CCMODE_POPON;
435             break;
436         case 0x24:
437             handle_delete_end_of_row(ctx, hi, lo);
438             break;
439         case 0x25:
440             ctx->rollup = 2;
441             ctx->mode = CCMODE_ROLLUP_2;
442             break;
443         case 0x26:
444             ctx->rollup = 3;
445             ctx->mode = CCMODE_ROLLUP_3;
446             break;
447         case 0x27:
448             ctx->rollup = 4;
449             ctx->mode = CCMODE_ROLLUP_4;
450             break;
451         case 0x29:
452             /* resume direct captioning */
453             ctx->mode = CCMODE_PAINTON;
454             break;
455         case 0x2b:
456             /* resume text display */
457             ctx->mode = CCMODE_TEXT;
458             break;
459         case 0x2c:
460             /* erase display memory */
461             ret = handle_edm(ctx, pts);
462             break;
463         case 0x2d:
464             /* carriage return */
465             ff_dlog(ctx, "carriage return\n");
466             reap_screen(ctx, pts);
467             roll_up(ctx);
468             ctx->screen_changed = 1;
469             ctx->cursor_column = 0;
470             break;
471         case 0x2f:
472             /* end of caption */
473             ff_dlog(ctx, "handle_eoc\n");
474             ret = handle_eoc(ctx, pts);
475             break;
476         default:
477             ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
478             break;
479         }
480     } else if (hi >= 0x20) {
481         /* Standard characters (always in pairs) */
482         handle_char(ctx, hi, lo, pts);
483     } else {
484         /* Ignoring all other non data code */
485         ff_dlog(ctx, "Unknown command 0x%hhx 0x%hhx\n", hi, lo);
486     }
487
488     /* set prev command */
489     ctx->prev_cmd[0] = hi;
490     ctx->prev_cmd[1] = lo;
491
492     return ret;
493 }
494
495 static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
496 {
497     CCaptionSubContext *ctx = avctx->priv_data;
498     AVSubtitle *sub = data;
499     uint8_t *bptr = NULL;
500     int len = avpkt->size;
501     int ret = 0;
502     int i;
503
504     if (ctx->pktbuf->size < len) {
505         ret = av_buffer_realloc(&ctx->pktbuf, len);
506          if (ret < 0) {
507             av_log(ctx, AV_LOG_WARNING, "Insufficient Memory of %d truncated to %d\n", len, ctx->pktbuf->size);
508             len = ctx->pktbuf->size;
509             ret = 0;
510         }
511     }
512     memcpy(ctx->pktbuf->data, avpkt->data, len);
513     bptr = ctx->pktbuf->data;
514
515     for (i  = 0; i < len; i += 3) {
516         uint8_t cc_type = *(bptr + i) & 3;
517         if (validate_cc_data_pair(bptr + i))
518             continue;
519         /* ignoring data field 1 */
520         if(cc_type == 1)
521             continue;
522         else
523             process_cc608(ctx, avpkt->pts, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
524         if (ctx->screen_changed && *ctx->buffer.str)
525         {
526             int start_time = av_rescale_q(ctx->start_time, avctx->time_base, (AVRational){ 1, 100 });
527             int end_time = av_rescale_q(ctx->end_time, avctx->time_base, (AVRational){ 1, 100 });
528             ff_dlog(ctx, "cdp writing data (%s)\n",ctx->buffer.str);
529             ret = ff_ass_add_rect_bprint(sub, &ctx->buffer, start_time, end_time - start_time);
530             if (ret < 0)
531                 return ret;
532             sub->pts = av_rescale_q(ctx->start_time, avctx->time_base, AV_TIME_BASE_Q);
533             ctx->screen_changed = 0;
534             av_bprint_clear(&ctx->buffer);
535         }
536     }
537
538     *got_sub = sub->num_rects > 0;
539     return ret;
540 }
541
542 static const AVOption options[] = {
543     {NULL}
544 };
545
546 static const AVClass ccaption_dec_class = {
547     .class_name = "Closed caption Decoder",
548     .item_name  = av_default_item_name,
549     .option     = options,
550     .version    = LIBAVUTIL_VERSION_INT,
551 };
552
553 AVCodec ff_ccaption_decoder = {
554     .name           = "cc_dec",
555     .long_name      = NULL_IF_CONFIG_SMALL("Closed Caption (EIA-608 / CEA-708) Decoder"),
556     .type           = AVMEDIA_TYPE_SUBTITLE,
557     .id             = AV_CODEC_ID_EIA_608,
558     .priv_data_size = sizeof(CCaptionSubContext),
559     .init           = init_decoder,
560     .close          = close_decoder,
561     .decode         = decode,
562     .priv_class     = &ccaption_dec_class,
563 };