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