FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libzvbi-teletextdec.c
Go to the documentation of this file.
1 /*
2  * Teletext decoding for ffmpeg
3  * Copyright (c) 2005-2010, 2012 Wolfram Gloger
4  * Copyright (c) 2013 Marton Balint
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "avcodec.h"
22 #include "libavcodec/ass.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/bprint.h"
25 #include "libavutil/intreadwrite.h"
26 
27 #include <libzvbi.h>
28 
29 #define TEXT_MAXSZ (25 * (56 + 1) * 4 + 2)
30 #define VBI_NB_COLORS 40
31 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
32 #define VBI_R(rgba) (((rgba) >> 0) & 0xFF)
33 #define VBI_G(rgba) (((rgba) >> 8) & 0xFF)
34 #define VBI_B(rgba) (((rgba) >> 16) & 0xFF)
35 #define VBI_A(rgba) (((rgba) >> 24) & 0xFF)
36 #define MAX_BUFFERED_PAGES 25
37 #define BITMAP_CHAR_WIDTH 12
38 #define BITMAP_CHAR_HEIGHT 10
39 
40 typedef struct TeletextPage
41 {
43  int pgno;
44  int subno;
45  int64_t pts;
46 } TeletextPage;
47 
48 typedef struct TeletextContext
49 {
50  AVClass *class;
51  char *pgno;
52  int x_offset;
53  int y_offset;
54  int format_id; /* 0 = bitmap, 1 = text/ass */
55  int chop_top;
56  int sub_duration; /* in msec */
59 
62  int nb_pages;
63  int64_t pts;
65 
66  vbi_decoder * vbi;
67  vbi_dvb_demux * dx;
68 #ifdef DEBUG
69  vbi_export * ex;
70 #endif
71  vbi_sliced sliced[64];
73 
74 static int chop_spaces_utf8(const unsigned char* t, int len)
75 {
76  t += len;
77  while (len > 0) {
78  if (*--t != ' ' || (len-1 > 0 && *(t-1) & 0x80))
79  break;
80  --len;
81  }
82  return len;
83 }
84 
85 static void subtitle_rect_free(AVSubtitleRect **sub_rect)
86 {
87  av_freep(&(*sub_rect)->pict.data[0]);
88  av_freep(&(*sub_rect)->pict.data[1]);
89  av_freep(&(*sub_rect)->ass);
90  av_freep(sub_rect);
91 }
92 
93 static int create_ass_text(TeletextContext *ctx, const char *text, char **ass)
94 {
95  int ret;
96  AVBPrint buf, buf2;
97  const int ts_start = av_rescale_q(ctx->pts, AV_TIME_BASE_Q, (AVRational){1, 100});
98  const int ts_duration = av_rescale_q(ctx->sub_duration, (AVRational){1, 1000}, (AVRational){1, 100});
99 
100  /* First we escape the plain text into buf. */
102  ff_ass_bprint_text_event(&buf, text, strlen(text), "", 0);
103 
104  if (!av_bprint_is_complete(&buf)) {
105  av_bprint_finalize(&buf, NULL);
106  return AVERROR(ENOMEM);
107  }
108 
109  /* Then we create the ass dialog line in buf2 from the escaped text in buf. */
111  ff_ass_bprint_dialog(&buf2, buf.str, ts_start, ts_duration, 0);
112  av_bprint_finalize(&buf, NULL);
113 
114  if (!av_bprint_is_complete(&buf2)) {
115  av_bprint_finalize(&buf2, NULL);
116  return AVERROR(ENOMEM);
117  }
118 
119  if ((ret = av_bprint_finalize(&buf2, ass)) < 0)
120  return ret;
121 
122  return 0;
123 }
124 
125 /* Draw a page as text */
126 static int gen_sub_text(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
127 {
128  const char *in;
129  AVBPrint buf;
130  char *vbi_text = av_malloc(TEXT_MAXSZ);
131  int sz;
132 
133  if (!vbi_text)
134  return AVERROR(ENOMEM);
135 
136  sz = vbi_print_page_region(page, vbi_text, TEXT_MAXSZ-1, "UTF-8",
137  /*table mode*/ TRUE, FALSE,
138  0, chop_top,
139  page->columns, page->rows-chop_top);
140  if (sz <= 0) {
141  av_log(ctx, AV_LOG_ERROR, "vbi_print error\n");
142  av_free(vbi_text);
143  return AVERROR_EXTERNAL;
144  }
145  vbi_text[sz] = '\0';
146  in = vbi_text;
147  av_bprint_init(&buf, 0, TEXT_MAXSZ);
148 
149  if (ctx->chop_spaces) {
150  for (;;) {
151  int nl, sz;
152 
153  // skip leading spaces and newlines
154  in += strspn(in, " \n");
155  // compute end of row
156  for (nl = 0; in[nl]; ++nl)
157  if (in[nl] == '\n' && (nl==0 || !(in[nl-1] & 0x80)))
158  break;
159  if (!in[nl])
160  break;
161  // skip trailing spaces
162  sz = chop_spaces_utf8(in, nl);
163  av_bprint_append_data(&buf, in, sz);
164  av_bprintf(&buf, "\n");
165  in += nl;
166  }
167  } else {
168  av_bprintf(&buf, "%s\n", vbi_text);
169  }
170  av_free(vbi_text);
171 
172  if (!av_bprint_is_complete(&buf)) {
173  av_bprint_finalize(&buf, NULL);
174  return AVERROR(ENOMEM);
175  }
176 
177  if (buf.len) {
178  int ret;
179  sub_rect->type = SUBTITLE_ASS;
180  if ((ret = create_ass_text(ctx, buf.str, &sub_rect->ass)) < 0) {
181  av_bprint_finalize(&buf, NULL);
182  return ret;
183  }
184  av_log(ctx, AV_LOG_DEBUG, "subtext:%s:txetbus\n", sub_rect->ass);
185  } else {
186  sub_rect->type = SUBTITLE_NONE;
187  }
188  av_bprint_finalize(&buf, NULL);
189  return 0;
190 }
191 
192 static void fix_transparency(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page,
193  int chop_top, uint8_t transparent_color, int resx, int resy)
194 {
195  int iy;
196 
197  // Hack for transparency, inspired by VLC code...
198  for (iy = 0; iy < resy; iy++) {
199  uint8_t *pixel = sub_rect->pict.data[0] + iy * sub_rect->pict.linesize[0];
200  vbi_char *vc = page->text + (iy / BITMAP_CHAR_HEIGHT + chop_top) * page->columns;
201  vbi_char *vcnext = vc + page->columns;
202  for (; vc < vcnext; vc++) {
203  uint8_t *pixelnext = pixel + BITMAP_CHAR_WIDTH;
204  switch (vc->opacity) {
205  case VBI_TRANSPARENT_SPACE:
206  memset(pixel, transparent_color, BITMAP_CHAR_WIDTH);
207  break;
208  case VBI_OPAQUE:
209  case VBI_SEMI_TRANSPARENT:
210  if (!ctx->transparent_bg)
211  break;
212  case VBI_TRANSPARENT_FULL:
213  for(; pixel < pixelnext; pixel++)
214  if (*pixel == vc->background)
215  *pixel = transparent_color;
216  break;
217  }
218  pixel = pixelnext;
219  }
220  }
221 }
222 
223 /* Draw a page as bitmap */
224 static int gen_sub_bitmap(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
225 {
226  int resx = page->columns * BITMAP_CHAR_WIDTH;
227  int resy = (page->rows - chop_top) * BITMAP_CHAR_HEIGHT;
228  uint8_t ci, cmax = 0;
229  int ret;
230  vbi_char *vc = page->text + (chop_top * page->columns);
231  vbi_char *vcend = page->text + (page->rows * page->columns);
232 
233  for (; vc < vcend; vc++) {
234  if (vc->opacity != VBI_TRANSPARENT_SPACE) {
235  cmax = VBI_NB_COLORS;
236  break;
237  }
238  }
239 
240  if (cmax == 0) {
241  av_log(ctx, AV_LOG_DEBUG, "dropping empty page %3x\n", page->pgno);
242  sub_rect->type = SUBTITLE_NONE;
243  return 0;
244  }
245 
246  if ((ret = avpicture_alloc(&sub_rect->pict, AV_PIX_FMT_PAL8, resx, resy)) < 0)
247  return ret;
248  // Yes, we want to allocate the palette on our own because AVSubtitle works this way
249  sub_rect->pict.data[1] = NULL;
250 
251  vbi_draw_vt_page_region(page, VBI_PIXFMT_PAL8,
252  sub_rect->pict.data[0], sub_rect->pict.linesize[0],
253  0, chop_top, page->columns, page->rows - chop_top,
254  /*reveal*/ 1, /*flash*/ 1);
255 
256  fix_transparency(ctx, sub_rect, page, chop_top, cmax, resx, resy);
257  sub_rect->x = ctx->x_offset;
258  sub_rect->y = ctx->y_offset + chop_top * BITMAP_CHAR_HEIGHT;
259  sub_rect->w = resx;
260  sub_rect->h = resy;
261  sub_rect->nb_colors = (int)cmax + 1;
262  sub_rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
263  if (!sub_rect->pict.data[1]) {
264  av_freep(&sub_rect->pict.data[0]);
265  return AVERROR(ENOMEM);
266  }
267  for (ci = 0; ci < cmax; ci++) {
268  int r, g, b, a;
269 
270  r = VBI_R(page->color_map[ci]);
271  g = VBI_G(page->color_map[ci]);
272  b = VBI_B(page->color_map[ci]);
273  a = VBI_A(page->color_map[ci]);
274  ((uint32_t *)sub_rect->pict.data[1])[ci] = RGBA(r, g, b, a);
275 #ifdef DEBUG
276  av_log(ctx, AV_LOG_DEBUG, "palette %0x\n",
277  ((uint32_t *)sub_rect->pict.data[1])[ci]);
278 #endif
279  }
280  ((uint32_t *)sub_rect->pict.data[1])[cmax] = RGBA(0, 0, 0, 0);
281  sub_rect->type = SUBTITLE_BITMAP;
282  return 0;
283 }
284 
285 static void handler(vbi_event *ev, void *user_data)
286 {
287  TeletextContext *ctx = user_data;
288  TeletextPage *new_pages;
289  vbi_page page;
290  int res;
291  char pgno_str[12];
292  vbi_subno subno;
293  vbi_page_type vpt;
294  int chop_top;
295  char *lang;
296 
297  snprintf(pgno_str, sizeof pgno_str, "%03x", ev->ev.ttx_page.pgno);
298  av_log(ctx, AV_LOG_DEBUG, "decoded page %s.%02x\n",
299  pgno_str, ev->ev.ttx_page.subno & 0xFF);
300 
301  if (strcmp(ctx->pgno, "*") && !strstr(ctx->pgno, pgno_str))
302  return;
303  if (ctx->handler_ret < 0)
304  return;
305 
306  res = vbi_fetch_vt_page(ctx->vbi, &page,
307  ev->ev.ttx_page.pgno,
308  ev->ev.ttx_page.subno,
309  VBI_WST_LEVEL_3p5, 25, TRUE);
310 
311  if (!res)
312  return;
313 
314 #ifdef DEBUG
315  fprintf(stderr, "\nSaving res=%d dy0=%d dy1=%d...\n",
316  res, page.dirty.y0, page.dirty.y1);
317  fflush(stderr);
318 
319  if (!vbi_export_stdio(ctx->ex, stderr, &page))
320  fprintf(stderr, "failed: %s\n", vbi_export_errstr(ctx->ex));
321 #endif
322 
323  vpt = vbi_classify_page(ctx->vbi, ev->ev.ttx_page.pgno, &subno, &lang);
324  chop_top = ctx->chop_top ||
325  ((page.rows > 1) && (vpt == VBI_SUBTITLE_PAGE));
326 
327  av_log(ctx, AV_LOG_DEBUG, "%d x %d page chop:%d\n",
328  page.columns, page.rows, chop_top);
329 
330  if (ctx->nb_pages < MAX_BUFFERED_PAGES) {
331  if ((new_pages = av_realloc_array(ctx->pages, ctx->nb_pages + 1, sizeof(TeletextPage)))) {
332  TeletextPage *cur_page = new_pages + ctx->nb_pages;
333  ctx->pages = new_pages;
334  cur_page->sub_rect = av_mallocz(sizeof(*cur_page->sub_rect));
335  cur_page->pts = ctx->pts;
336  cur_page->pgno = ev->ev.ttx_page.pgno;
337  cur_page->subno = ev->ev.ttx_page.subno;
338  if (cur_page->sub_rect) {
339  res = (ctx->format_id == 0) ?
340  gen_sub_bitmap(ctx, cur_page->sub_rect, &page, chop_top) :
341  gen_sub_text (ctx, cur_page->sub_rect, &page, chop_top);
342  if (res < 0) {
343  av_freep(&cur_page->sub_rect);
344  ctx->handler_ret = res;
345  } else {
346  ctx->pages[ctx->nb_pages++] = *cur_page;
347  }
348  } else {
349  ctx->handler_ret = AVERROR(ENOMEM);
350  }
351  } else {
352  ctx->handler_ret = AVERROR(ENOMEM);
353  }
354  } else {
355  //TODO: If multiple packets contain more than one page, pages may got queued up, and this may happen...
356  av_log(ctx, AV_LOG_ERROR, "Buffered too many pages, dropping page %s.\n", pgno_str);
357  ctx->handler_ret = AVERROR(ENOSYS);
358  }
359 
360  vbi_unref_page(&page);
361 }
362 
364 {
365  TeletextContext *ctx = avctx->priv_data;
366  AVSubtitle *sub = data;
367  const uint8_t *buf = pkt->data;
368  int left = pkt->size;
369  uint8_t pesheader[45] = {0x00, 0x00, 0x01, 0xbd, 0x00, 0x00, 0x85, 0x80, 0x24, 0x21, 0x00, 0x01, 0x00, 0x01};
370  int pesheader_size = sizeof(pesheader);
371  const uint8_t *pesheader_buf = pesheader;
372  int ret = 0;
373 
374  if (!ctx->vbi) {
375  if (!(ctx->vbi = vbi_decoder_new()))
376  return AVERROR(ENOMEM);
377  if (!vbi_event_handler_add(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx)) {
378  vbi_decoder_delete(ctx->vbi);
379  ctx->vbi = NULL;
380  return AVERROR(ENOMEM);
381  }
382  }
383  if (!ctx->dx && (!(ctx->dx = vbi_dvb_pes_demux_new (/* callback */ NULL, NULL))))
384  return AVERROR(ENOMEM);
385 
386  if (avctx->pkt_timebase.den && pkt->pts != AV_NOPTS_VALUE)
387  ctx->pts = av_rescale_q(pkt->pts, avctx->pkt_timebase, AV_TIME_BASE_Q);
388 
389  if (left) {
390  // We allow unreasonably big packets, even if the standard only allows a max size of 1472
391  if ((pesheader_size + left) < 184 || (pesheader_size + left) > 65504 || (pesheader_size + left) % 184 != 0)
392  return AVERROR_INVALIDDATA;
393 
394  memset(pesheader + 14, 0xff, pesheader_size - 14);
395  AV_WB16(pesheader + 4, left + pesheader_size - 6);
396 
397  /* PTS is deliberately left as 0 in the PES header, otherwise libzvbi uses
398  * it to detect dropped frames. Unforunatey the guessed packet PTS values
399  * (see mpegts demuxer) are not accurate enough to pass that test. */
400  vbi_dvb_demux_cor(ctx->dx, ctx->sliced, 64, NULL, &pesheader_buf, &pesheader_size);
401 
402  ctx->handler_ret = pkt->size;
403 
404  while (left > 0) {
405  int64_t pts = 0;
406  unsigned int lines = vbi_dvb_demux_cor(ctx->dx, ctx->sliced, 64, &pts, &buf, &left);
407 #ifdef DEBUG
408  av_log(avctx, AV_LOG_DEBUG,
409  "ctx=%p buf_size=%d left=%u lines=%u pts=%f pkt_pts=%f\n",
410  ctx, pkt->size, left, lines, (double)pts/90000.0, (double)pkt->pts/90000.0);
411 #endif
412  if (lines > 0) {
413 #ifdef DEBUGx
414  int i;
415  for(i=0; i<lines; ++i)
416  av_log(avctx, AV_LOG_DEBUG,
417  "lines=%d id=%x\n", i, ctx->sliced[i].id);
418 #endif
419  vbi_decode(ctx->vbi, ctx->sliced, lines, (double)pts/90000.0);
420  ctx->lines_processed += lines;
421  }
422  }
423  ctx->pts = AV_NOPTS_VALUE;
424  ret = ctx->handler_ret;
425  }
426 
427  if (ret < 0)
428  return ret;
429 
430  // is there a subtitle to pass?
431  if (ctx->nb_pages) {
432  int i;
433  sub->format = ctx->format_id;
434  sub->start_display_time = 0;
435  sub->end_display_time = ctx->sub_duration;
436  sub->num_rects = 0;
437  sub->pts = ctx->pages->pts;
438 
439  if (ctx->pages->sub_rect->type != SUBTITLE_NONE) {
440  sub->rects = av_malloc(sizeof(*sub->rects) * 1);
441  if (sub->rects) {
442  sub->num_rects = 1;
443  sub->rects[0] = ctx->pages->sub_rect;
444  } else {
445  ret = AVERROR(ENOMEM);
446  }
447  } else {
448  av_log(avctx, AV_LOG_DEBUG, "sending empty sub\n");
449  sub->rects = NULL;
450  }
451  if (!sub->rects) // no rect was passed
453 
454  for (i = 0; i < ctx->nb_pages - 1; i++)
455  ctx->pages[i] = ctx->pages[i + 1];
456  ctx->nb_pages--;
457 
458  if (ret >= 0)
459  *data_size = 1;
460  } else
461  *data_size = 0;
462 
463  return ret;
464 }
465 
467 {
468  TeletextContext *ctx = avctx->priv_data;
469  unsigned int maj, min, rev;
470 
471  vbi_version(&maj, &min, &rev);
472  if (!(maj > 0 || min > 2 || min == 2 && rev >= 26)) {
473  av_log(avctx, AV_LOG_ERROR, "decoder needs zvbi version >= 0.2.26.\n");
474  return AVERROR_EXTERNAL;
475  }
476 
477  if (ctx->format_id == 0) {
478  avctx->width = 41 * BITMAP_CHAR_WIDTH;
479  avctx->height = 25 * BITMAP_CHAR_HEIGHT;
480  }
481 
482  ctx->dx = NULL;
483  ctx->vbi = NULL;
484  ctx->pts = AV_NOPTS_VALUE;
485 
486 #ifdef DEBUG
487  {
488  char *t;
489  ctx->ex = vbi_export_new("text", &t);
490  }
491 #endif
492  av_log(avctx, AV_LOG_VERBOSE, "page filter: %s\n", ctx->pgno);
493  return (ctx->format_id == 1) ? ff_ass_subtitle_header_default(avctx) : 0;
494 }
495 
497 {
498  TeletextContext *ctx = avctx->priv_data;
499 
500 #ifdef DEBUG
501  av_log(avctx, AV_LOG_DEBUG, "lines_total=%u\n", ctx->lines_processed);
502 #endif
503  while (ctx->nb_pages)
504  subtitle_rect_free(&ctx->pages[--ctx->nb_pages].sub_rect);
505  av_freep(&ctx->pages);
506 
507  vbi_dvb_demux_delete(ctx->dx);
508  vbi_decoder_delete(ctx->vbi);
509  ctx->dx = NULL;
510  ctx->vbi = NULL;
511  ctx->pts = AV_NOPTS_VALUE;
512  return 0;
513 }
514 
515 static void teletext_flush(AVCodecContext *avctx)
516 {
517  teletext_close_decoder(avctx);
518 }
519 
520 #define OFFSET(x) offsetof(TeletextContext, x)
521 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
522 static const AVOption options[] = {
523  {"txt_page", "list of teletext page numbers to decode, * is all", OFFSET(pgno), AV_OPT_TYPE_STRING, {.str = "*"}, 0, 0, SD},
524  {"txt_chop_top", "discards the top teletext line", OFFSET(chop_top), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, SD},
525  {"txt_format", "format of the subtitles (bitmap or text)", OFFSET(format_id), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD, "txt_format"},
526  {"bitmap", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, SD, "txt_format"},
527  {"text", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, SD, "txt_format"},
528  {"txt_left", "x offset of generated bitmaps", OFFSET(x_offset), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 65535, SD},
529  {"txt_top", "y offset of generated bitmaps", OFFSET(y_offset), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 65535, SD},
530  {"txt_chop_spaces", "chops leading and trailing spaces from text", OFFSET(chop_spaces), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, SD},
531  {"txt_duration", "display duration of teletext pages in msecs", OFFSET(sub_duration), AV_OPT_TYPE_INT, {.i64 = 30000}, 0, 86400000, SD},
532  {"txt_transparent", "force transparent background of the teletext", OFFSET(transparent_bg), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
533  { NULL },
534 };
535 
536 static const AVClass teletext_class = {
537  .class_name = "libzvbi_teletextdec",
538  .item_name = av_default_item_name,
539  .option = options,
540  .version = LIBAVUTIL_VERSION_INT,
541 };
542 
544  .name = "libzvbi_teletextdec",
545  .long_name = NULL_IF_CONFIG_SMALL("Libzvbi DVB teletext decoder"),
546  .type = AVMEDIA_TYPE_SUBTITLE,
548  .priv_data_size = sizeof(TeletextContext),
552  .capabilities = CODEC_CAP_DELAY,
554  .priv_class= &teletext_class,
555 };