2 * Teletext decoding for ffmpeg
3 * Copyright (c) 2005-2010, 2012 Wolfram Gloger
4 * Copyright (c) 2013 Marton Balint
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcodec/ass.h"
23 #include "libavcodec/dvbtxt.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/log.h"
32 #define TEXT_MAXSZ (25 * (56 + 1) * 4 + 2)
33 #define VBI_NB_COLORS 40
34 #define VBI_TRANSPARENT_BLACK 8
35 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
36 #define VBI_R(rgba) (((rgba) >> 0) & 0xFF)
37 #define VBI_G(rgba) (((rgba) >> 8) & 0xFF)
38 #define VBI_B(rgba) (((rgba) >> 16) & 0xFF)
39 #define VBI_A(rgba) (((rgba) >> 24) & 0xFF)
40 #define MAX_BUFFERED_PAGES 25
41 #define BITMAP_CHAR_WIDTH 12
42 #define BITMAP_CHAR_HEIGHT 10
45 typedef struct TeletextPage
47 AVSubtitleRect *sub_rect;
53 typedef struct TeletextContext
59 int format_id; /* 0 = bitmap, 1 = text/ass */
61 int sub_duration; /* in msec */
73 vbi_sliced sliced[MAX_SLICES];
76 uint8_t subtitle_map[2048];
79 static int chop_spaces_utf8(const unsigned char* t, int len)
83 if (*--t != ' ' || (len-1 > 0 && *(t-1) & 0x80))
90 static void subtitle_rect_free(AVSubtitleRect **sub_rect)
92 av_freep(&(*sub_rect)->data[0]);
93 av_freep(&(*sub_rect)->data[1]);
94 av_freep(&(*sub_rect)->ass);
98 static char *create_ass_text(TeletextContext *ctx, const char *text)
103 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
104 ff_ass_bprint_text_event(&buf, text, strlen(text), "", 0);
105 if (!av_bprint_is_complete(&buf)) {
106 av_bprint_finalize(&buf, NULL);
109 dialog = ff_ass_get_dialog(ctx->readorder++, 0, NULL, NULL, buf.str);
110 av_bprint_finalize(&buf, NULL);
114 /* Draw a page as text */
115 static int gen_sub_text(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
119 char *vbi_text = av_malloc(TEXT_MAXSZ);
123 return AVERROR(ENOMEM);
125 sz = vbi_print_page_region(page, vbi_text, TEXT_MAXSZ-1, "UTF-8",
126 /*table mode*/ TRUE, FALSE,
128 page->columns, page->rows-chop_top);
130 av_log(ctx, AV_LOG_ERROR, "vbi_print error\n");
132 return AVERROR_EXTERNAL;
136 av_bprint_init(&buf, 0, TEXT_MAXSZ);
138 if (ctx->chop_spaces) {
142 // skip leading spaces and newlines
143 in += strspn(in, " \n");
144 // compute end of row
145 for (nl = 0; in[nl]; ++nl)
146 if (in[nl] == '\n' && (nl==0 || !(in[nl-1] & 0x80)))
150 // skip trailing spaces
151 sz = chop_spaces_utf8(in, nl);
152 av_bprint_append_data(&buf, in, sz);
153 av_bprintf(&buf, "\n");
157 av_bprintf(&buf, "%s\n", vbi_text);
161 if (!av_bprint_is_complete(&buf)) {
162 av_bprint_finalize(&buf, NULL);
163 return AVERROR(ENOMEM);
167 sub_rect->type = SUBTITLE_ASS;
168 sub_rect->ass = create_ass_text(ctx, buf.str);
170 if (!sub_rect->ass) {
171 av_bprint_finalize(&buf, NULL);
172 return AVERROR(ENOMEM);
174 av_log(ctx, AV_LOG_DEBUG, "subtext:%s:txetbus\n", sub_rect->ass);
176 sub_rect->type = SUBTITLE_NONE;
178 av_bprint_finalize(&buf, NULL);
182 static void fix_transparency(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page,
183 int chop_top, int resx, int resy)
187 // Hack for transparency, inspired by VLC code...
188 for (iy = 0; iy < resy; iy++) {
189 uint8_t *pixel = sub_rect->data[0] + iy * sub_rect->linesize[0];
190 vbi_char *vc = page->text + (iy / BITMAP_CHAR_HEIGHT + chop_top) * page->columns;
191 vbi_char *vcnext = vc + page->columns;
192 for (; vc < vcnext; vc++) {
193 uint8_t *pixelnext = pixel + BITMAP_CHAR_WIDTH;
194 switch (vc->opacity) {
195 case VBI_TRANSPARENT_SPACE:
196 memset(pixel, VBI_TRANSPARENT_BLACK, BITMAP_CHAR_WIDTH);
199 if (!ctx->transparent_bg)
201 case VBI_SEMI_TRANSPARENT:
202 if (ctx->opacity > 0) {
203 if (ctx->opacity < 255)
204 for(; pixel < pixelnext; pixel++)
205 if (*pixel == vc->background)
206 *pixel += VBI_NB_COLORS;
209 case VBI_TRANSPARENT_FULL:
210 for(; pixel < pixelnext; pixel++)
211 if (*pixel == vc->background)
212 *pixel = VBI_TRANSPARENT_BLACK;
220 /* Draw a page as bitmap */
221 static int gen_sub_bitmap(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
223 int resx = page->columns * BITMAP_CHAR_WIDTH;
224 int resy = (page->rows - chop_top) * BITMAP_CHAR_HEIGHT;
226 vbi_char *vc = page->text + (chop_top * page->columns);
227 vbi_char *vcend = page->text + (page->rows * page->columns);
229 for (; vc < vcend; vc++) {
230 if (vc->opacity != VBI_TRANSPARENT_SPACE)
235 av_log(ctx, AV_LOG_DEBUG, "dropping empty page %3x\n", page->pgno);
236 sub_rect->type = SUBTITLE_NONE;
240 sub_rect->data[0] = av_mallocz(resx * resy);
241 sub_rect->linesize[0] = resx;
242 if (!sub_rect->data[0])
243 return AVERROR(ENOMEM);
245 vbi_draw_vt_page_region(page, VBI_PIXFMT_PAL8,
246 sub_rect->data[0], sub_rect->linesize[0],
247 0, chop_top, page->columns, page->rows - chop_top,
248 /*reveal*/ 1, /*flash*/ 1);
250 fix_transparency(ctx, sub_rect, page, chop_top, resx, resy);
251 sub_rect->x = ctx->x_offset;
252 sub_rect->y = ctx->y_offset + chop_top * BITMAP_CHAR_HEIGHT;
255 sub_rect->nb_colors = ctx->opacity > 0 && ctx->opacity < 255 ? 2 * VBI_NB_COLORS : VBI_NB_COLORS;
256 sub_rect->data[1] = av_mallocz(AVPALETTE_SIZE);
257 if (!sub_rect->data[1]) {
258 av_freep(&sub_rect->data[0]);
259 return AVERROR(ENOMEM);
261 for (ci = 0; ci < VBI_NB_COLORS; ci++) {
264 r = VBI_R(page->color_map[ci]);
265 g = VBI_G(page->color_map[ci]);
266 b = VBI_B(page->color_map[ci]);
267 a = VBI_A(page->color_map[ci]);
268 ((uint32_t *)sub_rect->data[1])[ci] = RGBA(r, g, b, a);
269 ((uint32_t *)sub_rect->data[1])[ci + VBI_NB_COLORS] = RGBA(r, g, b, ctx->opacity);
270 ff_dlog(ctx, "palette %0x\n", ((uint32_t *)sub_rect->data[1])[ci]);
272 ((uint32_t *)sub_rect->data[1])[VBI_TRANSPARENT_BLACK] = RGBA(0, 0, 0, 0);
273 ((uint32_t *)sub_rect->data[1])[VBI_TRANSPARENT_BLACK + VBI_NB_COLORS] = RGBA(0, 0, 0, 0);
274 sub_rect->type = SUBTITLE_BITMAP;
278 static void handler(vbi_event *ev, void *user_data)
280 TeletextContext *ctx = user_data;
281 TeletextPage *new_pages;
286 int is_subtitle_page = ctx->subtitle_map[ev->ev.ttx_page.pgno & 0x7ff];
288 snprintf(pgno_str, sizeof pgno_str, "%03x", ev->ev.ttx_page.pgno);
289 av_log(ctx, AV_LOG_DEBUG, "decoded page %s.%02x\n",
290 pgno_str, ev->ev.ttx_page.subno & 0xFF);
292 if (strcmp(ctx->pgno, "*") && (strcmp(ctx->pgno, "subtitle") || !is_subtitle_page) && !strstr(ctx->pgno, pgno_str))
294 if (ctx->handler_ret < 0)
297 res = vbi_fetch_vt_page(ctx->vbi, &page,
298 ev->ev.ttx_page.pgno,
299 ev->ev.ttx_page.subno,
300 VBI_WST_LEVEL_3p5, 25, TRUE);
305 chop_top = ctx->chop_top || ((page.rows > 1) && is_subtitle_page);
307 av_log(ctx, AV_LOG_DEBUG, "%d x %d page chop:%d\n",
308 page.columns, page.rows, chop_top);
310 if (ctx->nb_pages < MAX_BUFFERED_PAGES) {
311 if ((new_pages = av_realloc_array(ctx->pages, ctx->nb_pages + 1, sizeof(TeletextPage)))) {
312 TeletextPage *cur_page = new_pages + ctx->nb_pages;
313 ctx->pages = new_pages;
314 cur_page->sub_rect = av_mallocz(sizeof(*cur_page->sub_rect));
315 cur_page->pts = ctx->pts;
316 cur_page->pgno = ev->ev.ttx_page.pgno;
317 cur_page->subno = ev->ev.ttx_page.subno;
318 if (cur_page->sub_rect) {
319 res = (ctx->format_id == 0) ?
320 gen_sub_bitmap(ctx, cur_page->sub_rect, &page, chop_top) :
321 gen_sub_text (ctx, cur_page->sub_rect, &page, chop_top);
323 av_freep(&cur_page->sub_rect);
324 ctx->handler_ret = res;
326 ctx->pages[ctx->nb_pages++] = *cur_page;
329 ctx->handler_ret = AVERROR(ENOMEM);
332 ctx->handler_ret = AVERROR(ENOMEM);
335 //TODO: If multiple packets contain more than one page, pages may got queued up, and this may happen...
336 av_log(ctx, AV_LOG_ERROR, "Buffered too many pages, dropping page %s.\n", pgno_str);
337 ctx->handler_ret = AVERROR(ENOSYS);
340 vbi_unref_page(&page);
343 static int slice_to_vbi_lines(TeletextContext *ctx, uint8_t* buf, int size)
346 while (size >= 2 && lines < MAX_SLICES) {
347 int data_unit_id = buf[0];
348 int data_unit_length = buf[1];
349 if (data_unit_length + 2 > size)
350 return AVERROR_INVALIDDATA;
351 if (ff_data_unit_id_is_teletext(data_unit_id)) {
352 if (data_unit_length != 0x2c)
353 return AVERROR_INVALIDDATA;
355 int line_offset = buf[2] & 0x1f;
356 int field_parity = buf[2] & 0x20;
357 uint8_t *p = ctx->sliced[lines].data;
359 ctx->sliced[lines].id = VBI_SLICED_TELETEXT_B;
360 ctx->sliced[lines].line = (line_offset > 0 ? (line_offset + (field_parity ? 0 : 313)) : 0);
361 for (i = 0; i < 42; i++)
362 p[i] = vbi_rev8(buf[4 + i]);
363 /* Unfortunately libzvbi does not expose page flags, and
364 * vbi_classify_page only checks MIP, so we have to manually
365 * decode the page flags and store the results. */
366 pmag = vbi_unham16p(p);
367 if (pmag >= 0 && pmag >> 3 == 0) { // We found a row 0 header
368 int page = vbi_unham16p(p + 2);
369 int flags1 = vbi_unham16p(p + 6);
370 int flags2 = vbi_unham16p(p + 8);
371 if (page >= 0 && flags1 >= 0 && flags2 >= 0) {
372 int pgno = ((pmag & 7) << 8) + page;
373 // Check for disabled NEWSFLASH flag and enabled SUBTITLE and SUPRESS_HEADER flags
374 ctx->subtitle_map[pgno] = (!(flags1 & 0x40) && flags1 & 0x80 && flags2 & 0x01);
380 size -= data_unit_length + 2;
381 buf += data_unit_length + 2;
384 av_log(ctx, AV_LOG_WARNING, "%d bytes remained after slicing data\n", size);
388 static int teletext_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
390 TeletextContext *ctx = avctx->priv_data;
391 AVSubtitle *sub = data;
396 if (!(ctx->vbi = vbi_decoder_new()))
397 return AVERROR(ENOMEM);
398 if (!vbi_event_handler_register(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx)) {
399 vbi_decoder_delete(ctx->vbi);
401 return AVERROR(ENOMEM);
405 if (avctx->pkt_timebase.num && pkt->pts != AV_NOPTS_VALUE)
406 ctx->pts = av_rescale_q(pkt->pts, avctx->pkt_timebase, AV_TIME_BASE_Q);
410 const int full_pes_size = pkt->size + 45; /* PES header is 45 bytes */
412 // We allow unreasonably big packets, even if the standard only allows a max size of 1472
413 if (full_pes_size < 184 || full_pes_size > 65504 || full_pes_size % 184 != 0)
414 return AVERROR_INVALIDDATA;
416 ctx->handler_ret = pkt->size;
418 if (ff_data_identifier_is_teletext(*pkt->data)) {
419 if ((lines = slice_to_vbi_lines(ctx, pkt->data + 1, pkt->size - 1)) < 0)
421 ff_dlog(avctx, "ctx=%p buf_size=%d lines=%u pkt_pts=%7.3f\n",
422 ctx, pkt->size, lines, (double)pkt->pts/90000.0);
424 vbi_decode(ctx->vbi, ctx->sliced, lines, 0.0);
425 ctx->lines_processed += lines;
428 ctx->pts = AV_NOPTS_VALUE;
429 ret = ctx->handler_ret;
435 // is there a subtitle to pass?
438 sub->format = ctx->format_id;
439 sub->start_display_time = 0;
440 sub->end_display_time = ctx->sub_duration;
442 sub->pts = ctx->pages->pts;
444 if (ctx->pages->sub_rect->type != SUBTITLE_NONE) {
445 sub->rects = av_malloc(sizeof(*sub->rects));
448 sub->rects[0] = ctx->pages->sub_rect;
450 FF_DISABLE_DEPRECATION_WARNINGS
451 for (j = 0; j < 4; j++) {
452 sub->rects[0]->pict.data[j] = sub->rects[0]->data[j];
453 sub->rects[0]->pict.linesize[j] = sub->rects[0]->linesize[j];
455 FF_ENABLE_DEPRECATION_WARNINGS
458 ret = AVERROR(ENOMEM);
461 av_log(avctx, AV_LOG_DEBUG, "sending empty sub\n");
464 if (!sub->rects) // no rect was passed
465 subtitle_rect_free(&ctx->pages->sub_rect);
467 for (i = 0; i < ctx->nb_pages - 1; i++)
468 ctx->pages[i] = ctx->pages[i + 1];
479 static int teletext_init_decoder(AVCodecContext *avctx)
481 TeletextContext *ctx = avctx->priv_data;
482 unsigned int maj, min, rev;
484 vbi_version(&maj, &min, &rev);
485 if (!(maj > 0 || min > 2 || min == 2 && rev >= 26)) {
486 av_log(avctx, AV_LOG_ERROR, "decoder needs zvbi version >= 0.2.26.\n");
487 return AVERROR_EXTERNAL;
490 if (ctx->format_id == 0) {
491 avctx->width = 41 * BITMAP_CHAR_WIDTH;
492 avctx->height = 25 * BITMAP_CHAR_HEIGHT;
496 ctx->pts = AV_NOPTS_VALUE;
498 if (ctx->opacity == -1)
499 ctx->opacity = ctx->transparent_bg ? 0 : 255;
501 av_log(avctx, AV_LOG_VERBOSE, "page filter: %s\n", ctx->pgno);
502 return (ctx->format_id == 1) ? ff_ass_subtitle_header_default(avctx) : 0;
505 static int teletext_close_decoder(AVCodecContext *avctx)
507 TeletextContext *ctx = avctx->priv_data;
509 ff_dlog(avctx, "lines_total=%u\n", ctx->lines_processed);
510 while (ctx->nb_pages)
511 subtitle_rect_free(&ctx->pages[--ctx->nb_pages].sub_rect);
512 av_freep(&ctx->pages);
514 vbi_decoder_delete(ctx->vbi);
516 ctx->pts = AV_NOPTS_VALUE;
517 memset(ctx->subtitle_map, 0, sizeof(ctx->subtitle_map));
518 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
523 static void teletext_flush(AVCodecContext *avctx)
525 teletext_close_decoder(avctx);
528 #define OFFSET(x) offsetof(TeletextContext, x)
529 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
530 static const AVOption options[] = {
531 {"txt_page", "page numbers to decode, subtitle for subtitles, * for all", OFFSET(pgno), AV_OPT_TYPE_STRING, {.str = "*"}, 0, 0, SD},
532 {"txt_chop_top", "discards the top teletext line", OFFSET(chop_top), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, SD},
533 {"txt_format", "format of the subtitles (bitmap or text)", OFFSET(format_id), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD, "txt_format"},
534 {"bitmap", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, SD, "txt_format"},
535 {"text", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, SD, "txt_format"},
536 {"txt_left", "x offset of generated bitmaps", OFFSET(x_offset), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 65535, SD},
537 {"txt_top", "y offset of generated bitmaps", OFFSET(y_offset), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 65535, SD},
538 {"txt_chop_spaces", "chops leading and trailing spaces from text", OFFSET(chop_spaces), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, SD},
539 {"txt_duration", "display duration of teletext pages in msecs", OFFSET(sub_duration), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 86400000, SD},
540 {"txt_transparent", "force transparent background of the teletext", OFFSET(transparent_bg), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
541 {"txt_opacity", "set opacity of the transparent background", OFFSET(opacity), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, SD},
545 static const AVClass teletext_class = {
546 .class_name = "libzvbi_teletextdec",
547 .item_name = av_default_item_name,
549 .version = LIBAVUTIL_VERSION_INT,
552 AVCodec ff_libzvbi_teletext_decoder = {
553 .name = "libzvbi_teletextdec",
554 .long_name = NULL_IF_CONFIG_SMALL("Libzvbi DVB teletext decoder"),
555 .type = AVMEDIA_TYPE_SUBTITLE,
556 .id = AV_CODEC_ID_DVB_TELETEXT,
557 .priv_data_size = sizeof(TeletextContext),
558 .init = teletext_init_decoder,
559 .close = teletext_close_decoder,
560 .decode = teletext_decode_frame,
561 .capabilities = AV_CODEC_CAP_DELAY,
562 .flush = teletext_flush,
563 .priv_class= &teletext_class,
564 .wrapper_name = "libzvbi",