libavcodec/libzvbi-teletextdec.c
b339dccb
 /*
  * Teletext decoding for ffmpeg
  * Copyright (c) 2005-2010, 2012 Wolfram Gloger
  * Copyright (c) 2013 Marton Balint
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
b89596a4
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
b339dccb
  */
 
 #include "avcodec.h"
3c4b5275
 #include "libavcodec/ass.h"
6c905dd3
 #include "libavcodec/dvbtxt.h"
b339dccb
 #include "libavutil/opt.h"
65fb59ab
 #include "libavutil/bprint.h"
229843aa
 #include "libavutil/internal.h"
b339dccb
 #include "libavutil/intreadwrite.h"
ae017c26
 #include "libavutil/log.h"
b339dccb
 
 #include <libzvbi.h>
 
 #define TEXT_MAXSZ    (25 * (56 + 1) * 4 + 2)
 #define VBI_NB_COLORS 40
fe4a2cbd
 #define VBI_TRANSPARENT_BLACK 8
b339dccb
 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
 #define VBI_R(rgba)   (((rgba) >> 0) & 0xFF)
 #define VBI_G(rgba)   (((rgba) >> 8) & 0xFF)
 #define VBI_B(rgba)   (((rgba) >> 16) & 0xFF)
 #define VBI_A(rgba)   (((rgba) >> 24) & 0xFF)
a494757b
 #define MAX_BUFFERED_PAGES 25
249a4359
 #define BITMAP_CHAR_WIDTH  12
 #define BITMAP_CHAR_HEIGHT 10
085ca7dc
 #define MAX_SLICES 64
a494757b
 
 typedef struct TeletextPage
 {
     AVSubtitleRect *sub_rect;
     int pgno;
     int subno;
     int64_t pts;
 } TeletextPage;
b339dccb
 
 typedef struct TeletextContext
 {
     AVClass        *class;
     char           *pgno;
     int             x_offset;
     int             y_offset;
3c4b5275
     int             format_id; /* 0 = bitmap, 1 = text/ass */
b339dccb
     int             chop_top;
     int             sub_duration; /* in msec */
     int             transparent_bg;
73e4565d
     int             opacity;
b339dccb
     int             chop_spaces;
 
     int             lines_processed;
a494757b
     TeletextPage    *pages;
     int             nb_pages;
     int64_t         pts;
c0479f0f
     int             handler_ret;
b339dccb
 
     vbi_decoder *   vbi;
 #ifdef DEBUG
     vbi_export *    ex;
 #endif
085ca7dc
     vbi_sliced      sliced[MAX_SLICES];
29412821
 
     int             readorder;
b339dccb
 } TeletextContext;
 
b5096816
 static int chop_spaces_utf8(const unsigned char* t, int len)
b339dccb
 {
     t += len;
     while (len > 0) {
         if (*--t != ' ' || (len-1 > 0 && *(t-1) & 0x80))
             break;
         --len;
     }
     return len;
 }
 
b5096816
 static void subtitle_rect_free(AVSubtitleRect **sub_rect)
a494757b
 {
ce973636
     av_freep(&(*sub_rect)->data[0]);
     av_freep(&(*sub_rect)->data[1]);
3c4b5275
     av_freep(&(*sub_rect)->ass);
a494757b
     av_freep(sub_rect);
 }
 
29412821
 static char *create_ass_text(TeletextContext *ctx, const char *text)
3c4b5275
 {
29412821
     char *dialog;
     AVBPrint buf;
3c4b5275
 
     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
     ff_ass_bprint_text_event(&buf, text, strlen(text), "", 0);
     if (!av_bprint_is_complete(&buf)) {
         av_bprint_finalize(&buf, NULL);
29412821
         return NULL;
3c4b5275
     }
29412821
     dialog = ff_ass_get_dialog(ctx->readorder++, 0, NULL, NULL, buf.str);
3c4b5275
     av_bprint_finalize(&buf, NULL);
29412821
     return dialog;
3c4b5275
 }
 
b5096816
 /* Draw a page as text */
 static int gen_sub_text(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
b339dccb
 {
     const char *in;
65fb59ab
     AVBPrint buf;
b339dccb
     char *vbi_text = av_malloc(TEXT_MAXSZ);
     int sz;
 
     if (!vbi_text)
         return AVERROR(ENOMEM);
 
     sz = vbi_print_page_region(page, vbi_text, TEXT_MAXSZ-1, "UTF-8",
                                    /*table mode*/ TRUE, FALSE,
                                    0,             chop_top,
                                    page->columns, page->rows-chop_top);
     if (sz <= 0) {
         av_log(ctx, AV_LOG_ERROR, "vbi_print error\n");
         av_free(vbi_text);
         return AVERROR_EXTERNAL;
     }
     vbi_text[sz] = '\0';
     in  = vbi_text;
65fb59ab
     av_bprint_init(&buf, 0, TEXT_MAXSZ);
 
b339dccb
     if (ctx->chop_spaces) {
         for (;;) {
             int nl, sz;
 
             // skip leading spaces and newlines
             in += strspn(in, " \n");
             // compute end of row
             for (nl = 0; in[nl]; ++nl)
                 if (in[nl] == '\n' && (nl==0 || !(in[nl-1] & 0x80)))
                     break;
             if (!in[nl])
                 break;
             // skip trailing spaces
             sz = chop_spaces_utf8(in, nl);
65fb59ab
             av_bprint_append_data(&buf, in, sz);
             av_bprintf(&buf, "\n");
b339dccb
             in += nl;
         }
     } else {
65fb59ab
         av_bprintf(&buf, "%s\n", vbi_text);
b339dccb
     }
     av_free(vbi_text);
65fb59ab
 
     if (!av_bprint_is_complete(&buf)) {
         av_bprint_finalize(&buf, NULL);
         return AVERROR(ENOMEM);
     }
 
     if (buf.len) {
3c4b5275
         sub_rect->type = SUBTITLE_ASS;
29412821
         sub_rect->ass = create_ass_text(ctx, buf.str);
 
         if (!sub_rect->ass) {
3c4b5275
             av_bprint_finalize(&buf, NULL);
29412821
             return AVERROR(ENOMEM);
3c4b5275
         }
         av_log(ctx, AV_LOG_DEBUG, "subtext:%s:txetbus\n", sub_rect->ass);
b339dccb
     } else {
         sub_rect->type = SUBTITLE_NONE;
     }
3c4b5275
     av_bprint_finalize(&buf, NULL);
b339dccb
     return 0;
 }
 
b5096816
 static void fix_transparency(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page,
fe4a2cbd
                              int chop_top, int resx, int resy)
b339dccb
 {
     int iy;
 
     // Hack for transparency, inspired by VLC code...
     for (iy = 0; iy < resy; iy++) {
ce973636
         uint8_t *pixel = sub_rect->data[0] + iy * sub_rect->linesize[0];
249a4359
         vbi_char *vc = page->text + (iy / BITMAP_CHAR_HEIGHT + chop_top) * page->columns;
b339dccb
         vbi_char *vcnext = vc + page->columns;
         for (; vc < vcnext; vc++) {
249a4359
             uint8_t *pixelnext = pixel + BITMAP_CHAR_WIDTH;
b339dccb
             switch (vc->opacity) {
                 case VBI_TRANSPARENT_SPACE:
fe4a2cbd
                     memset(pixel, VBI_TRANSPARENT_BLACK, BITMAP_CHAR_WIDTH);
b339dccb
                     break;
                 case VBI_OPAQUE:
                     if (!ctx->transparent_bg)
                         break;
73e4565d
                 case VBI_SEMI_TRANSPARENT:
                     if (ctx->opacity > 0) {
                         if (ctx->opacity < 255)
                             for(; pixel < pixelnext; pixel++)
                                 if (*pixel == vc->background)
                                     *pixel += VBI_NB_COLORS;
                         break;
                     }
b339dccb
                 case VBI_TRANSPARENT_FULL:
                     for(; pixel < pixelnext; pixel++)
                         if (*pixel == vc->background)
fe4a2cbd
                             *pixel = VBI_TRANSPARENT_BLACK;
b339dccb
                     break;
             }
             pixel = pixelnext;
         }
     }
 }
 
b5096816
 /* Draw a page as bitmap */
 static int gen_sub_bitmap(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
b339dccb
 {
249a4359
     int resx = page->columns * BITMAP_CHAR_WIDTH;
     int resy = (page->rows - chop_top) * BITMAP_CHAR_HEIGHT;
92f2a9db
     uint8_t ci;
b339dccb
     vbi_char *vc = page->text + (chop_top * page->columns);
     vbi_char *vcend = page->text + (page->rows * page->columns);
 
     for (; vc < vcend; vc++) {
92f2a9db
         if (vc->opacity != VBI_TRANSPARENT_SPACE)
b339dccb
             break;
     }
 
92f2a9db
     if (vc >= vcend) {
b339dccb
         av_log(ctx, AV_LOG_DEBUG, "dropping empty page %3x\n", page->pgno);
         sub_rect->type = SUBTITLE_NONE;
         return 0;
     }
 
ce973636
     sub_rect->data[0] = av_mallocz(resx * resy);
     sub_rect->linesize[0] = resx;
     if (!sub_rect->data[0])
         return AVERROR(ENOMEM);
b339dccb
 
     vbi_draw_vt_page_region(page, VBI_PIXFMT_PAL8,
ce973636
                             sub_rect->data[0], sub_rect->linesize[0],
b339dccb
                             0, chop_top, page->columns, page->rows - chop_top,
                             /*reveal*/ 1, /*flash*/ 1);
 
fe4a2cbd
     fix_transparency(ctx, sub_rect, page, chop_top, resx, resy);
b339dccb
     sub_rect->x = ctx->x_offset;
c77e0c21
     sub_rect->y = ctx->y_offset + chop_top * BITMAP_CHAR_HEIGHT;
b339dccb
     sub_rect->w = resx;
     sub_rect->h = resy;
73e4565d
     sub_rect->nb_colors = ctx->opacity > 0 && ctx->opacity < 255 ? 2 * VBI_NB_COLORS : VBI_NB_COLORS;
ce973636
     sub_rect->data[1] = av_mallocz(AVPALETTE_SIZE);
     if (!sub_rect->data[1]) {
         av_freep(&sub_rect->data[0]);
b339dccb
         return AVERROR(ENOMEM);
     }
92f2a9db
     for (ci = 0; ci < VBI_NB_COLORS; ci++) {
b339dccb
         int r, g, b, a;
 
         r = VBI_R(page->color_map[ci]);
         g = VBI_G(page->color_map[ci]);
         b = VBI_B(page->color_map[ci]);
         a = VBI_A(page->color_map[ci]);
ce973636
         ((uint32_t *)sub_rect->data[1])[ci] = RGBA(r, g, b, a);
73e4565d
         ((uint32_t *)sub_rect->data[1])[ci + VBI_NB_COLORS] = RGBA(r, g, b, ctx->opacity);
ce973636
         ff_dlog(ctx, "palette %0x\n", ((uint32_t *)sub_rect->data[1])[ci]);
b339dccb
     }
fe4a2cbd
     ((uint32_t *)sub_rect->data[1])[VBI_TRANSPARENT_BLACK] = RGBA(0, 0, 0, 0);
73e4565d
     ((uint32_t *)sub_rect->data[1])[VBI_TRANSPARENT_BLACK + VBI_NB_COLORS] = RGBA(0, 0, 0, 0);
b339dccb
     sub_rect->type = SUBTITLE_BITMAP;
     return 0;
 }
 
b5096816
 static void handler(vbi_event *ev, void *user_data)
b339dccb
 {
     TeletextContext *ctx = user_data;
a494757b
     TeletextPage *new_pages;
b339dccb
     vbi_page page;
     int res;
     char pgno_str[12];
     vbi_subno subno;
     vbi_page_type vpt;
     int chop_top;
     char *lang;
 
     snprintf(pgno_str, sizeof pgno_str, "%03x", ev->ev.ttx_page.pgno);
     av_log(ctx, AV_LOG_DEBUG, "decoded page %s.%02x\n",
            pgno_str, ev->ev.ttx_page.subno & 0xFF);
 
     if (strcmp(ctx->pgno, "*") && !strstr(ctx->pgno, pgno_str))
         return;
c0479f0f
     if (ctx->handler_ret < 0)
         return;
b339dccb
 
     res = vbi_fetch_vt_page(ctx->vbi, &page,
                             ev->ev.ttx_page.pgno,
                             ev->ev.ttx_page.subno,
                             VBI_WST_LEVEL_3p5, 25, TRUE);
 
     if (!res)
         return;
 
 #ifdef DEBUG
     fprintf(stderr, "\nSaving res=%d dy0=%d dy1=%d...\n",
             res, page.dirty.y0, page.dirty.y1);
     fflush(stderr);
 
     if (!vbi_export_stdio(ctx->ex, stderr, &page))
         fprintf(stderr, "failed: %s\n", vbi_export_errstr(ctx->ex));
 #endif
 
     vpt = vbi_classify_page(ctx->vbi, ev->ev.ttx_page.pgno, &subno, &lang);
     chop_top = ctx->chop_top ||
         ((page.rows > 1) && (vpt == VBI_SUBTITLE_PAGE));
 
     av_log(ctx, AV_LOG_DEBUG, "%d x %d page chop:%d\n",
            page.columns, page.rows, chop_top);
 
a494757b
     if (ctx->nb_pages < MAX_BUFFERED_PAGES) {
         if ((new_pages = av_realloc_array(ctx->pages, ctx->nb_pages + 1, sizeof(TeletextPage)))) {
             TeletextPage *cur_page = new_pages + ctx->nb_pages;
             ctx->pages = new_pages;
             cur_page->sub_rect = av_mallocz(sizeof(*cur_page->sub_rect));
             cur_page->pts = ctx->pts;
             cur_page->pgno = ev->ev.ttx_page.pgno;
             cur_page->subno = ev->ev.ttx_page.subno;
             if (cur_page->sub_rect) {
                 res = (ctx->format_id == 0) ?
                     gen_sub_bitmap(ctx, cur_page->sub_rect, &page, chop_top) :
                     gen_sub_text  (ctx, cur_page->sub_rect, &page, chop_top);
c0479f0f
                 if (res < 0) {
a494757b
                     av_freep(&cur_page->sub_rect);
c0479f0f
                     ctx->handler_ret = res;
                 } else {
a494757b
                     ctx->pages[ctx->nb_pages++] = *cur_page;
c0479f0f
                 }
             } else {
                 ctx->handler_ret = AVERROR(ENOMEM);
a494757b
             }
         } else {
c0479f0f
             ctx->handler_ret = AVERROR(ENOMEM);
b339dccb
         }
     } else {
a494757b
         //TODO: If multiple packets contain more than one page, pages may got queued up, and this may happen...
         av_log(ctx, AV_LOG_ERROR, "Buffered too many pages, dropping page %s.\n", pgno_str);
c0479f0f
         ctx->handler_ret = AVERROR(ENOSYS);
b339dccb
     }
 
     vbi_unref_page(&page);
 }
 
085ca7dc
 static int slice_to_vbi_lines(TeletextContext *ctx, uint8_t* buf, int size)
 {
     int lines = 0;
     while (size >= 2 && lines < MAX_SLICES) {
         int data_unit_id     = buf[0];
         int data_unit_length = buf[1];
         if (data_unit_length + 2 > size)
             return AVERROR_INVALIDDATA;
6c905dd3
         if (ff_data_unit_id_is_teletext(data_unit_id)) {
085ca7dc
             if (data_unit_length != 0x2c)
                 return AVERROR_INVALIDDATA;
             else {
                 int line_offset  = buf[2] & 0x1f;
                 int field_parity = buf[2] & 0x20;
                 int i;
                 ctx->sliced[lines].id = VBI_SLICED_TELETEXT_B;
                 ctx->sliced[lines].line = (line_offset > 0 ? (line_offset + (field_parity ? 0 : 313)) : 0);
                 for (i = 0; i < 42; i++)
                     ctx->sliced[lines].data[i] = vbi_rev8(buf[4 + i]);
                 lines++;
             }
         }
         size -= data_unit_length + 2;
         buf += data_unit_length + 2;
     }
     if (size)
         av_log(ctx, AV_LOG_WARNING, "%d bytes remained after slicing data\n", size);
     return lines;
 }
 
b5096816
 static int teletext_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
b339dccb
 {
     TeletextContext *ctx = avctx->priv_data;
     AVSubtitle      *sub = data;
a494757b
     int             ret = 0;
3f85552e
     int j;
b339dccb
 
     if (!ctx->vbi) {
         if (!(ctx->vbi = vbi_decoder_new()))
             return AVERROR(ENOMEM);
2dafbae9
         if (!vbi_event_handler_register(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx)) {
b339dccb
             vbi_decoder_delete(ctx->vbi);
             ctx->vbi = NULL;
             return AVERROR(ENOMEM);
         }
     }
 
50401f5f
     if (avctx->pkt_timebase.num && pkt->pts != AV_NOPTS_VALUE)
a494757b
         ctx->pts = av_rescale_q(pkt->pts, avctx->pkt_timebase, AV_TIME_BASE_Q);
 
085ca7dc
     if (pkt->size) {
         int lines;
         const int full_pes_size = pkt->size + 45; /* PES header is 45 bytes */
 
688652e0
         // We allow unreasonably big packets, even if the standard only allows a max size of 1472
085ca7dc
         if (full_pes_size < 184 || full_pes_size > 65504 || full_pes_size % 184 != 0)
688652e0
             return AVERROR_INVALIDDATA;
b339dccb
 
c0479f0f
         ctx->handler_ret = pkt->size;
 
6c905dd3
         if (ff_data_identifier_is_teletext(*pkt->data)) {
085ca7dc
             if ((lines = slice_to_vbi_lines(ctx, pkt->data + 1, pkt->size - 1)) < 0)
                 return lines;
229843aa
             ff_dlog(avctx, "ctx=%p buf_size=%d lines=%u pkt_pts=%7.3f\n",
085ca7dc
                     ctx, pkt->size, lines, (double)pkt->pts/90000.0);
688652e0
             if (lines > 0) {
085ca7dc
 #ifdef DEBUG
688652e0
                 int i;
085ca7dc
                 av_log(avctx, AV_LOG_DEBUG, "line numbers:");
                 for(i = 0; i < lines; i++)
                     av_log(avctx, AV_LOG_DEBUG, " %d", ctx->sliced[i].line);
                 av_log(avctx, AV_LOG_DEBUG, "\n");
b339dccb
 #endif
085ca7dc
                 vbi_decode(ctx->vbi, ctx->sliced, lines, 0.0);
688652e0
                 ctx->lines_processed += lines;
             }
b339dccb
         }
688652e0
         ctx->pts = AV_NOPTS_VALUE;
c0479f0f
         ret = ctx->handler_ret;
a494757b
     }
b339dccb
 
c0479f0f
     if (ret < 0)
         return ret;
 
b339dccb
     // is there a subtitle to pass?
a494757b
     if (ctx->nb_pages) {
         int i;
3c4b5275
         sub->format = ctx->format_id;
b339dccb
         sub->start_display_time = 0;
         sub->end_display_time = ctx->sub_duration;
         sub->num_rects = 0;
a494757b
         sub->pts = ctx->pages->pts;
b339dccb
 
a494757b
         if (ctx->pages->sub_rect->type != SUBTITLE_NONE) {
8ff06109
             sub->rects = av_malloc(sizeof(*sub->rects));
b339dccb
             if (sub->rects) {
                 sub->num_rects = 1;
a494757b
                 sub->rects[0] = ctx->pages->sub_rect;
ce973636
 #if FF_API_AVPICTURE
 FF_DISABLE_DEPRECATION_WARNINGS
5e9f14e4
                 for (j = 0; j < 4; j++) {
                     sub->rects[0]->pict.data[j] = sub->rects[0]->data[j];
                     sub->rects[0]->pict.linesize[j] = sub->rects[0]->linesize[j];
                 }
ce973636
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
5e9f14e4
             } else {
                 ret = AVERROR(ENOMEM);
             }
b339dccb
         } else {
             av_log(avctx, AV_LOG_DEBUG, "sending empty sub\n");
             sub->rects = NULL;
         }
         if (!sub->rects) // no rect was passed
a494757b
             subtitle_rect_free(&ctx->pages->sub_rect);
 
         for (i = 0; i < ctx->nb_pages - 1; i++)
             ctx->pages[i] = ctx->pages[i + 1];
         ctx->nb_pages--;
b339dccb
 
4947b062
         if (ret >= 0)
             *data_size = 1;
b339dccb
     } else
         *data_size = 0;
 
a494757b
     return ret;
b339dccb
 }
 
 static int teletext_init_decoder(AVCodecContext *avctx)
 {
     TeletextContext *ctx = avctx->priv_data;
     unsigned int maj, min, rev;
 
     vbi_version(&maj, &min, &rev);
     if (!(maj > 0 || min > 2 || min == 2 && rev >= 26)) {
         av_log(avctx, AV_LOG_ERROR, "decoder needs zvbi version >= 0.2.26.\n");
         return AVERROR_EXTERNAL;
     }
 
97f880e7
     if (ctx->format_id == 0) {
         avctx->width  = 41 * BITMAP_CHAR_WIDTH;
         avctx->height = 25 * BITMAP_CHAR_HEIGHT;
     }
 
b339dccb
     ctx->vbi = NULL;
a494757b
     ctx->pts = AV_NOPTS_VALUE;
b339dccb
 
73e4565d
     if (ctx->opacity == -1)
         ctx->opacity = ctx->transparent_bg ? 0 : 255;
 
b339dccb
 #ifdef DEBUG
     {
         char *t;
         ctx->ex = vbi_export_new("text", &t);
     }
 #endif
     av_log(avctx, AV_LOG_VERBOSE, "page filter: %s\n", ctx->pgno);
3c4b5275
     return (ctx->format_id == 1) ? ff_ass_subtitle_header_default(avctx) : 0;
b339dccb
 }
 
 static int teletext_close_decoder(AVCodecContext *avctx)
 {
     TeletextContext *ctx = avctx->priv_data;
 
229843aa
     ff_dlog(avctx, "lines_total=%u\n", ctx->lines_processed);
a494757b
     while (ctx->nb_pages)
         subtitle_rect_free(&ctx->pages[--ctx->nb_pages].sub_rect);
     av_freep(&ctx->pages);
b339dccb
 
     vbi_decoder_delete(ctx->vbi);
     ctx->vbi = NULL;
a494757b
     ctx->pts = AV_NOPTS_VALUE;
30e76853
     if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
         ctx->readorder = 0;
b339dccb
     return 0;
 }
 
 static void teletext_flush(AVCodecContext *avctx)
 {
     teletext_close_decoder(avctx);
 }
 
 #define OFFSET(x) offsetof(TeletextContext, x)
 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
     {"txt_page",        "list of teletext page numbers to decode, * is all", OFFSET(pgno),           AV_OPT_TYPE_STRING, {.str = "*"},      0, 0,        SD},
     {"txt_chop_top",    "discards the top teletext line",                    OFFSET(chop_top),       AV_OPT_TYPE_INT,    {.i64 = 1},        0, 1,        SD},
b689b1f6
     {"txt_format",      "format of the subtitles (bitmap or text)",          OFFSET(format_id),      AV_OPT_TYPE_INT,    {.i64 = 0},        0, 1,        SD,  "txt_format"},
     {"bitmap",          NULL,                                                0,                      AV_OPT_TYPE_CONST,  {.i64 = 0},        0, 0,        SD,  "txt_format"},
     {"text",            NULL,                                                0,                      AV_OPT_TYPE_CONST,  {.i64 = 1},        0, 0,        SD,  "txt_format"},
b339dccb
     {"txt_left",        "x offset of generated bitmaps",                     OFFSET(x_offset),       AV_OPT_TYPE_INT,    {.i64 = 0},        0, 65535,    SD},
     {"txt_top",         "y offset of generated bitmaps",                     OFFSET(y_offset),       AV_OPT_TYPE_INT,    {.i64 = 0},        0, 65535,    SD},
     {"txt_chop_spaces", "chops leading and trailing spaces from text",       OFFSET(chop_spaces),    AV_OPT_TYPE_INT,    {.i64 = 1},        0, 1,        SD},
     {"txt_duration",    "display duration of teletext pages in msecs",       OFFSET(sub_duration),   AV_OPT_TYPE_INT,    {.i64 = 30000},    0, 86400000, SD},
     {"txt_transparent", "force transparent background of the teletext",      OFFSET(transparent_bg), AV_OPT_TYPE_INT,    {.i64 = 0},        0, 1,        SD},
73e4565d
     {"txt_opacity",     "set opacity of the transparent background",         OFFSET(opacity),        AV_OPT_TYPE_INT,    {.i64 = -1},      -1, 255,      SD},
b339dccb
     { NULL },
 };
 
 static const AVClass teletext_class = {
     .class_name = "libzvbi_teletextdec",
     .item_name  = av_default_item_name,
     .option     = options,
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
 AVCodec ff_libzvbi_teletext_decoder = {
     .name      = "libzvbi_teletextdec",
b46f1910
     .long_name = NULL_IF_CONFIG_SMALL("Libzvbi DVB teletext decoder"),
b339dccb
     .type      = AVMEDIA_TYPE_SUBTITLE,
e510171f
     .id        = AV_CODEC_ID_DVB_TELETEXT,
b339dccb
     .priv_data_size = sizeof(TeletextContext),
     .init      = teletext_init_decoder,
     .close     = teletext_close_decoder,
     .decode    = teletext_decode_frame,
444e9874
     .capabilities = AV_CODEC_CAP_DELAY,
b339dccb
     .flush     = teletext_flush,
     .priv_class= &teletext_class,
b945fed6
     .wrapper_name = "libzvbi",
b339dccb
 };