34#define TEXT_MAXSZ (25 * (56 + 1) * 4 + 2)
35#define VBI_NB_COLORS 40
36#define VBI_TRANSPARENT_BLACK 8
37#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
38#define VBI_R(rgba) (((rgba) >> 0) & 0xFF)
39#define VBI_G(rgba) (((rgba) >> 8) & 0xFF)
40#define VBI_B(rgba) (((rgba) >> 16) & 0xFF)
41#define VBI_A(rgba) (((rgba) >> 24) & 0xFF)
42#define MAX_BUFFERED_PAGES 25
43#define BITMAP_CHAR_WIDTH 12
44#define BITMAP_CHAR_HEIGHT 10
103 "&Hffffff,&Hffffff,&H0,&H0,"
113 "&Hffffff,&Hffffff,&H0,&H0,"
135 if (*--t !=
' ' || (
len-1 > 0 && *(t-1) & 0x80))
177 sz = vbi_print_page_region(page, vbi_text,
TEXT_MAXSZ-1,
"UTF-8",
180 page->columns, page->rows-chop_top);
190 if (
ctx->chop_spaces) {
195 in += strspn(in,
" \n");
197 for (nl = 0; in[nl]; ++nl)
198 if (in[nl] ==
'\n' && (nl==0 || !(in[nl-1] & 0x80)))
222 if (!sub_rect->
ass) {
236 int r =
VBI_R(page->color_map[ci]);
237 int g =
VBI_G(page->color_map[ci]);
238 int b =
VBI_B(page->color_map[ci]);
242#define IS_TXT_SPACE(ch) ((ch).unicode < 0x0020 || (ch).unicode >= 0xe000 || (ch).unicode == 0x00a0 ||\
243 (ch).size > VBI_DOUBLE_SIZE || (ch).opacity == VBI_TRANSPARENT_SPACE)
245static void get_trim_info(vbi_page *page, vbi_char *row,
int *leading,
int *trailing,
int *olen)
252 for (
i = 0;
i < page->columns;
i++) {
255 if (
out == 32 && !char_seen)
258 char_seen = 1,
len =
i - (*leading) + 1;
262 *trailing =
len > 0 ? page->columns - *leading -
len : page->columns;
266 int start,
int end, vbi_color *cur_color, vbi_color *cur_back_color)
270 for (
i = start;
i < end;
i++) {
273 if (*cur_color != row[
i].foreground) {
275 *cur_color = row[
i].foreground;
277 if (*cur_back_color != row[
i].background) {
279 *cur_back_color = row[
i].background;
284 }
else if (
out ==
'\\' ||
out ==
'{' ||
out ==
'}') {
298 int leading, trailing,
len;
299 int last_trailing = -1, last_leading = -1;
300 int min_trailing = page->columns, min_leading = page->columns;
302 int vertical_align = -1;
303 int can_align_left = 1, can_align_right = 1, can_align_center = 1;
304 int is_subtitle_page =
ctx->subtitle_map[page->pgno & 0x7ff];
306 vbi_color cur_color = VBI_WHITE;
307 vbi_color cur_back_color = VBI_BLACK;
312 for (
i = chop_top;
i < page->rows;
i++) {
313 vbi_char *row = page->text +
i * page->columns;
318 if (last_leading != -1 && last_leading != leading || leading > 5)
320 if (last_trailing != -1 && last_trailing != trailing || trailing > 2)
322 if (last_trailing != -1 && (
FFABS((trailing - leading) - (last_trailing - last_leading)) > 1) || trailing - leading > 4)
323 can_align_center = 0;
324 last_leading = leading;
325 last_trailing = trailing;
326 min_leading =
FFMIN(leading, min_leading);
327 min_trailing =
FFMIN(trailing, min_trailing);
331 if (!can_align_right && can_align_left && !can_align_center) {
332 ctx->last_ass_alignment = alignment = 1;
333 }
else if (!can_align_right && !can_align_left && can_align_center) {
334 ctx->last_ass_alignment = alignment = 2;
335 }
else if (can_align_right && !can_align_left && !can_align_center) {
336 ctx->last_ass_alignment = alignment = 3;
338 if (
ctx->last_ass_alignment == 1 && can_align_left ||
339 ctx->last_ass_alignment == 2 && can_align_center ||
340 ctx->last_ass_alignment == 3 && can_align_right)
341 alignment =
ctx->last_ass_alignment;
344 for (
i = chop_top;
i < page->rows;
i++) {
346 vbi_char *row = page->text +
i * page->columns;
347 int is_transparent_line;
349 for (j = 0; j < page->columns; j++)
350 if (row[j].opacity != VBI_TRANSPARENT_SPACE)
352 is_transparent_line = (j == page->columns);
354 len = is_transparent_line ? 0 : page->columns;
355 leading = trailing = is_transparent_line ? page->columns : 0;
357 if (is_subtitle_page) {
358 if (!is_transparent_line)
361 if (vertical_align == -1 &&
len) {
362 vertical_align = (2 - (
av_clip(
i + 1, 0, 23) / 8));
363 av_bprintf(&buf,
"{\\an%d}", alignment + vertical_align * 3);
364 if (vertical_align != 2)
368 if (
len && empty_lines > 1)
369 for (empty_lines /= 2; empty_lines > 0; empty_lines--)
372 if (alignment == 1 || alignment == 2 && !can_align_center)
373 leading = min_leading;
374 if (alignment == 3 || alignment == 2 && !can_align_center)
375 trailing = min_trailing;
378 if (
len || !is_subtitle_page) {
379 decode_string(page, row, &buf, leading, page->columns - trailing, &cur_color, &cur_back_color);
387 if (vertical_align == 0)
388 for (empty_lines = (empty_lines - 1) / 2; empty_lines > 0; empty_lines--)
400 if (!sub_rect->
ass) {
413 int chop_top,
int resx,
int resy)
418 for (iy = 0; iy < resy; iy++) {
421 vbi_char *vcnext = vc + page->columns;
422 for (; vc < vcnext; vc++) {
424 switch (vc->opacity) {
425 case VBI_TRANSPARENT_SPACE:
429 if (!
ctx->transparent_bg)
432 case VBI_SEMI_TRANSPARENT:
433 if (
ctx->opacity > 0) {
434 if (
ctx->opacity < 255)
436 if (*
pixel == vc->background)
441 case VBI_TRANSPARENT_FULL:
443 if (*
pixel == vc->background)
458 vbi_char *vc = page->text + (chop_top * page->columns);
459 vbi_char *vcend = page->text + (page->rows * page->columns);
461 for (; vc < vcend; vc++) {
462 if (vc->opacity != VBI_TRANSPARENT_SPACE)
474 if (!sub_rect->
data[0])
477 vbi_draw_vt_page_region(page, VBI_PIXFMT_PAL8,
479 0, chop_top, page->columns, page->rows - chop_top,
483 sub_rect->
x =
ctx->x_offset;
489 if (!sub_rect->
data[1]) {
496 r =
VBI_R(page->color_map[ci]);
497 g =
VBI_G(page->color_map[ci]);
498 b =
VBI_B(page->color_map[ci]);
499 a =
VBI_A(page->color_map[ci]);
502 ff_dlog(
ctx,
"palette %0x\n", ((uint32_t *)sub_rect->
data[1])[ci]);
518 int is_subtitle_page =
ctx->subtitle_map[ev->ev.ttx_page.pgno & 0x7ff];
520 snprintf(pgno_str,
sizeof pgno_str,
"%03x", ev->ev.ttx_page.pgno);
522 pgno_str, ev->ev.ttx_page.subno & 0xFF);
524 if (strcmp(
ctx->pgno,
"*") && (strcmp(
ctx->pgno,
"subtitle") || !is_subtitle_page) && !strstr(
ctx->pgno, pgno_str))
526 if (
ctx->handler_ret < 0)
529 res = vbi_fetch_vt_page(
ctx->vbi, &page,
530 ev->ev.ttx_page.pgno,
531 ev->ev.ttx_page.subno,
532 VBI_WST_LEVEL_3p5, 25, TRUE);
537 chop_top =
ctx->chop_top || ((page.rows > 1) && is_subtitle_page);
540 page.columns, page.rows, chop_top);
545 ctx->pages = new_pages;
548 cur_page->
pgno = ev->ev.ttx_page.pgno;
549 cur_page->
subno = ev->ev.ttx_page.subno;
551 switch (
ctx->format_id) {
567 ctx->handler_ret = res;
569 ctx->pages[
ctx->nb_pages++] = *cur_page;
583 vbi_unref_page(&page);
590 int data_unit_id = buf[0];
591 int data_unit_length = buf[1];
592 if (data_unit_length + 2 >
size)
595 if (data_unit_length != 0x2c)
598 int line_offset = buf[2] & 0x1f;
599 int field_parity = buf[2] & 0x20;
600 uint8_t *p =
ctx->sliced[lines].data;
602 ctx->sliced[lines].id = VBI_SLICED_TELETEXT_B;
603 ctx->sliced[lines].line = (line_offset > 0 ? (line_offset + (field_parity ? 0 : 313)) : 0);
604 for (
i = 0;
i < 42;
i++)
605 p[
i] = vbi_rev8(buf[4 +
i]);
609 pmag = vbi_unham16p(p);
610 if (pmag >= 0 && pmag >> 3 == 0) {
611 int page = vbi_unham16p(p + 2);
612 int flags1 = vbi_unham16p(p + 6);
613 int flags2 = vbi_unham16p(p + 8);
614 if (page >= 0 && flags1 >= 0 && flags2 >= 0) {
615 int pgno = ((pmag & 7) << 8) + page;
617 ctx->subtitle_map[pgno] = (!(flags1 & 0x40) && flags1 & 0x80 && flags2 & 0x01);
619 if (
ctx->subtitle_map[pgno] && pgno ==
ctx->last_pgno) {
620 int last_byte9 = vbi_unham8(
ctx->last_p5);
621 if (last_byte9 >= 0 && last_byte9 & 0x8) {
622 int byte9 = vbi_unham8(p[5]);
624 p[5] = vbi_ham8(byte9 | 0x8);
627 ctx->last_pgno = pgno;
634 size -= data_unit_length + 2;
635 buf += data_unit_length + 2;
649 if (!(
ctx->vbi = vbi_decoder_new()))
651 if (
ctx->default_region != -1) {
653 vbi_teletext_set_default_region(
ctx->vbi,
ctx->default_region);
655 if (!vbi_event_handler_register(
ctx->vbi, VBI_EVENT_TTX_PAGE,
handler,
ctx)) {
656 vbi_decoder_delete(
ctx->vbi);
667 const int full_pes_size =
pkt->size + 45;
670 if (full_pes_size < 184 || full_pes_size > 65504 || full_pes_size % 184 != 0)
673 ctx->handler_ret =
pkt->size;
678 ff_dlog(avctx,
"ctx=%p buf_size=%d lines=%u pkt_pts=%7.3f\n",
679 ctx,
pkt->size, lines, (
double)
pkt->pts/90000.0);
681 vbi_decode(
ctx->vbi,
ctx->sliced, lines, 0.0);
682 ctx->lines_processed += lines;
686 ret =
ctx->handler_ret;
699 sub->
pts =
ctx->pages->pts;
705 sub->
rects[0] =
ctx->pages->sub_rect;
716 for (
i = 0;
i <
ctx->nb_pages - 1;
i++)
731 unsigned int maj,
min, rev;
733 vbi_version(&maj, &
min, &rev);
734 if (!(maj > 0 ||
min > 2 ||
min == 2 && rev >= 26)) {
739 if (
ctx->format_id == 0) {
747 ctx->last_ass_alignment = 2;
749 if (
ctx->opacity == -1)
750 ctx->opacity =
ctx->transparent_bg ? 0 : 255;
754 switch (
ctx->format_id) {
769 ff_dlog(avctx,
"lines_total=%u\n",
ctx->lines_processed);
770 while (
ctx->nb_pages)
774 vbi_decoder_delete(
ctx->vbi);
778 ctx->last_ass_alignment = 2;
779 memset(
ctx->subtitle_map, 0,
sizeof(
ctx->subtitle_map));
790#define OFFSET(x) offsetof(TeletextContext, x)
791#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
793 {
"txt_page",
"page numbers to decode, subtitle for subtitles, * for all",
OFFSET(pgno),
AV_OPT_TYPE_STRING, {.str =
"*"}, 0, 0,
SD},
794 {
"txt_default_region",
"default G0 character set used for decoding",
OFFSET(default_region),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 87,
SD},
796 {
"txt_format",
"format of the subtitles (bitmap or text or ass)",
OFFSET(format_id),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2,
SD, .unit =
"txt_format"},
802 {
"txt_chop_spaces",
"chops leading and trailing spaces from text",
OFFSET(chop_spaces),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
SD},
803 {
"txt_duration",
"display duration of teletext pages in msecs",
OFFSET(sub_duration),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 86400000,
SD},
804 {
"txt_transparent",
"force transparent background of the teletext",
OFFSET(transparent_bg),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
SD},
805 {
"txt_opacity",
"set opacity of the transparent background",
OFFSET(opacity),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255,
SD},
810 .class_name =
"libzvbi_teletextdec",
817 .p.name =
"libzvbi_teletextdec",
823 .p.wrapper_name =
"libzvbi",
const FFCodec ff_libzvbi_teletext_decoder
static AVFormatContext * ctx
static av_cold void close(AVCodecParserContext *s)
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
char * ff_ass_get_dialog(int readorder, int layer, const char *style, const char *speaker, const char *text)
Craft an ASS dialog string.
void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size, const char *linebreaks, int keep_ass_markup)
Escape a text subtitle using ASS syntax into an AVBPrint buffer.
Libavcodec external API header.
char * av_asprintf(const char *fmt,...)
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_BPRINT_SIZE_UNLIMITED
#define i(width, name, range_min, range_max)
static int FUNC user_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawUserData *current)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define CODEC_LONG_NAME(str)
#define FF_CODEC_DECODE_SUB_CB(func)
common internal and external API header
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int(* init)(AVBSFContext *ctx)
static av_always_inline int ff_data_unit_id_is_teletext(int data_unit_id)
static av_always_inline int ff_data_identifier_is_teletext(int data_identifier)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
#define AV_CODEC_FLAG2_RO_FLUSH_NOOP
Do not reset ASS ReadOrder field on flush (subtitles decoding)
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
@ SUBTITLE_BITMAP
A bitmap, pict will be set.
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
@ AV_CODEC_ID_DVB_TELETEXT
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
#define LIBAVUTIL_VERSION_INT
common internal API header
static void fix_transparency(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top, int resx, int resy)
static int gen_sub_bitmap(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
static void teletext_flush(AVCodecContext *avctx)
#define MAX_BUFFERED_PAGES
static void get_trim_info(vbi_page *page, vbi_char *row, int *leading, int *trailing, int *olen)
static void decode_string(vbi_page *page, vbi_char *row, AVBPrint *buf, int start, int end, vbi_color *cur_color, vbi_color *cur_back_color)
static int gen_sub_ass(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
#define BITMAP_CHAR_WIDTH
static void bprint_color(const char *type, AVBPrint *buf, vbi_page *page, unsigned ci)
#define VBI_TRANSPARENT_BLACK
static void subtitle_rect_free(AVSubtitleRect **sub_rect)
static const AVClass teletext_class
static int slice_to_vbi_lines(TeletextContext *ctx, const uint8_t *buf, int size)
#define BITMAP_CHAR_HEIGHT
static int chop_spaces_utf8(const unsigned char *t, int len)
static int my_ass_subtitle_header(AVCodecContext *avctx)
static int teletext_close_decoder(AVCodecContext *avctx)
static int teletext_decode_frame(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *pkt)
static int gen_sub_text(TeletextContext *ctx, AVSubtitleRect *sub_rect, vbi_page *page, int chop_top)
static int teletext_init_decoder(AVCodecContext *avctx)
static void handler(vbi_event *ev, void *user_data)
static char * create_ass_text(TeletextContext *ctx, const char *text)
Memory handling functions.
Describe the class of an AVClass context structure.
main external API structure.
int subtitle_header_size
Header containing style information for text subtitles.
int width
picture width / height.
int flags2
AV_CODEC_FLAG2_*.
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
uint8_t * subtitle_header
This structure stores compressed data.
int x
top left corner of pict, undefined when pict is not set
char * ass
0 terminated ASS/SSA compatible event line.
int w
width of pict, undefined when pict is not set
int nb_colors
number of colors in pict, undefined when pict is not set
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
int y
top left corner of pict, undefined when pict is not set
int h
height of pict, undefined when pict is not set
uint32_t start_display_time
uint32_t end_display_time
int64_t pts
Same as packet pts, in AV_TIME_BASE.
uint8_t subtitle_map[2048]
vbi_sliced sliced[MAX_SLICES]
AVSubtitleRect * sub_rect