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
27
28 #include <libzvbi.h>
29
30 #define TEXT_MAXSZ (25 * (56 + 1) * 4 + 2)
31 #define VBI_NB_COLORS 40
32 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
33 #define VBI_R(rgba) (((rgba) >> 0) & 0xFF)
34 #define VBI_G(rgba) (((rgba) >> 8) & 0xFF)
35 #define VBI_B(rgba) (((rgba) >> 16) & 0xFF)
36 #define VBI_A(rgba) (((rgba) >> 24) & 0xFF)
37 #define MAX_BUFFERED_PAGES 25
38 #define BITMAP_CHAR_WIDTH 12
39 #define BITMAP_CHAR_HEIGHT 10
41
43 {
49
51 {
61
67
69 #ifdef DEBUG
70 vbi_export * ex;
71 #endif
74
76 {
78 while (len > 0) {
79 if (*--t != ' ' || (len-1 > 0 && *(t-1) & 0x80))
80 break;
82 }
84 }
85
87 {
88 av_freep(&(*sub_rect)->pict.data[0]);
89 av_freep(&(*sub_rect)->pict.data[1]);
92 }
93
95 {
100
101 /* First we escape the plain text into buf. */
105
109 }
110
111 /* Then we create the ass dialog line in buf2 from the escaped text in buf. */
115
119 }
120
123
124 return 0;
125 }
126
127 /* Draw a page as text */
129 {
133 int sz;
134
135 if (!vbi_text)
137
138 sz = vbi_print_page_region(page, vbi_text,
TEXT_MAXSZ-1,
"UTF-8",
140 0, chop_top,
141 page->columns, page->rows-chop_top);
142 if (sz <= 0) {
146 }
147 vbi_text[sz] = '0円';
148 in = vbi_text;
150
152 for (;;) {
153 int nl, sz;
154
155 // skip leading spaces and newlines
156 in += strspn(in, " \n");
157 // compute end of row
158 for (nl = 0; in[nl]; ++nl)
159 if (in[nl] == '\n' && (nl==0 || !(in[nl-1] & 0x80)))
160 break;
161 if (!in[nl])
162 break;
163 // skip trailing spaces
167 in += nl;
168 }
169 } else {
171 }
173
177 }
178
179 if (buf.len) {
185 }
187 } else {
189 }
191 return 0;
192 }
193
195 int chop_top,
uint8_t transparent_color,
int resx,
int resy)
196 {
197 int iy;
198
199 // Hack for transparency, inspired by VLC code...
200 for (iy = 0; iy < resy; iy++) {
203 vbi_char *vcnext = vc + page->columns;
204 for (; vc < vcnext; vc++) {
206 switch (vc->opacity) {
207 case VBI_TRANSPARENT_SPACE:
208 memset(pixel, transparent_color, BITMAP_CHAR_WIDTH);
209 break;
210 case VBI_OPAQUE:
211 case VBI_SEMI_TRANSPARENT:
213 break;
214 case VBI_TRANSPARENT_FULL:
215 for(; pixel < pixelnext; pixel++)
216 if (*pixel == vc->background)
217 *pixel = transparent_color;
218 break;
219 }
220 pixel = pixelnext;
221 }
222 }
223 }
224
225 /* Draw a page as bitmap */
227 {
232 vbi_char *vc = page->text + (chop_top * page->columns);
233 vbi_char *vcend = page->text + (page->rows * page->columns);
234
235 for (; vc < vcend; vc++) {
236 if (vc->opacity != VBI_TRANSPARENT_SPACE) {
238 break;
239 }
240 }
241
242 if (cmax == 0) {
245 return 0;
246 }
247
250 // Yes, we want to allocate the palette on our own because AVSubtitle works this way
252
253 vbi_draw_vt_page_region(page, VBI_PIXFMT_PAL8,
255 0, chop_top, page->columns, page->rows - chop_top,
256 /*reveal*/ 1, /*flash*/ 1);
257
268 }
269 for (ci = 0; ci < cmax; ci++) {
271
272 r =
VBI_R(page->color_map[ci]);
273 g =
VBI_G(page->color_map[ci]);
274 b =
VBI_B(page->color_map[ci]);
275 a =
VBI_A(page->color_map[ci]);
276 ((uint32_t *)sub_rect->
pict.
data[1])[ci] =
RGBA(r, g, b, a);
277 av_dlog(ctx,
"palette %0x\n", ((uint32_t *)sub_rect->
pict.
data[1])[ci]);
278 }
279 ((uint32_t *)sub_rect->
pict.
data[1])[cmax] =
RGBA(0, 0, 0, 0);
281 return 0;
282 }
283
285 {
288 vbi_page page;
289 int res;
290 char pgno_str[12];
291 vbi_subno subno;
292 vbi_page_type vpt;
293 int chop_top;
294 char *lang;
295
296 snprintf(pgno_str,
sizeof pgno_str,
"%03x", ev->ev.ttx_page.pgno);
298 pgno_str, ev->ev.ttx_page.subno & 0xFF);
299
300 if (strcmp(ctx->
pgno,
"*") && !strstr(ctx->
pgno, pgno_str))
301 return;
303 return;
304
305 res = vbi_fetch_vt_page(ctx->
vbi, &page,
306 ev->ev.ttx_page.pgno,
307 ev->ev.ttx_page.subno,
308 VBI_WST_LEVEL_3p5, 25,
TRUE);
309
310 if (!res)
311 return;
312
313 #ifdef DEBUG
314 fprintf(stderr, "\nSaving res=%d dy0=%d dy1=%d...\n",
315 res, page.dirty.y0, page.dirty.y1);
316 fflush(stderr);
317
318 if (!vbi_export_stdio(ctx->ex, stderr, &page))
319 fprintf(stderr, "failed: %s\n", vbi_export_errstr(ctx->ex));
320 #endif
321
322 vpt = vbi_classify_page(ctx->
vbi, ev->ev.ttx_page.pgno, &subno, &lang);
324 ((page.rows > 1) && (vpt == VBI_SUBTITLE_PAGE));
325
327 page.columns, page.rows, chop_top);
328
332 ctx->
pages = new_pages;
335 cur_page->
pgno = ev->ev.ttx_page.pgno;
336 cur_page->
subno = ev->ev.ttx_page.subno;
341 if (res < 0) {
344 } else {
346 }
347 } else {
349 }
350 } else {
352 }
353 } else {
354 //TODO: If multiple packets contain more than one page, pages may got queued up, and this may happen...
357 }
358
359 vbi_unref_page(&page);
360 }
361
363 /* See EN 301 775 section 4.4.2. */
364 return (data_identifier >= 0x10 && data_identifier <= 0x1F ||
365 data_identifier >= 0x99 && data_identifier <= 0x9B);
366 }
367
369 {
370 int lines = 0;
372 int data_unit_id = buf[0];
373 int data_unit_length = buf[1];
374 if (data_unit_length + 2 > size)
376 if (data_unit_id == 0x02 || data_unit_id == 0x03) {
377 if (data_unit_length != 0x2c)
379 else {
380 int line_offset = buf[2] & 0x1f;
381 int field_parity = buf[2] & 0x20;
382 int i;
383 ctx->
sliced[lines].id = VBI_SLICED_TELETEXT_B;
384 ctx->
sliced[lines].line = (line_offset > 0 ? (line_offset + (field_parity ? 0 : 313)) : 0);
385 for (i = 0; i < 42; i++)
386 ctx->
sliced[lines].data[i] = vbi_rev8(buf[4 + i]);
387 lines++;
388 }
389 }
390 size -= data_unit_length + 2;
391 buf += data_unit_length + 2;
392 }
393 if (size)
395 return lines;
396 }
397
399 {
403
405 if (!(ctx->
vbi = vbi_decoder_new()))
407 if (!vbi_event_handler_add(ctx->
vbi, VBI_EVENT_TTX_PAGE,
handler, ctx)) {
408 vbi_decoder_delete(ctx->
vbi);
411 }
412 }
413
416
418 int lines;
419 const int full_pes_size = pkt->
size + 45;
/* PES header is 45 bytes */
420
421 // We allow unreasonably big packets, even if the standard only allows a max size of 1472
422 if (full_pes_size < 184 || full_pes_size > 65504 || full_pes_size % 184 != 0)
424
426
429 return lines;
430 av_dlog(avctx,
"ctx=%p buf_size=%d lines=%u pkt_pts=%7.3f\n",
431 ctx, pkt->
size, lines, (
double)pkt->
pts/90000.0);
432 if (lines > 0) {
433 #ifdef DEBUG
434 int i;
436 for(i = 0; i < lines; i++)
439 #endif
440 vbi_decode(ctx->
vbi, ctx->
sliced, lines, 0.0);
442 }
443 }
446 }
447
448 if (ret < 0)
450
451 // is there a subtitle to pass?
453 int i;
459
465 } else {
467 }
468 } else {
471 }
472 if (!sub->
rects)
// no rect was passed
474
475 for (i = 0; i < ctx->
nb_pages - 1; i++)
478
480 *data_size = 1;
481 } else
482 *data_size = 0;
483
485 }
486
488 {
490 unsigned int maj,
min, rev;
491
492 vbi_version(&maj, &min, &rev);
493 if (!(maj > 0 || min > 2 || min == 2 && rev >= 26)) {
496 }
497
501 }
502
505
506 #ifdef DEBUG
507 {
508 char *t;
509 ctx->ex = vbi_export_new("text", &t);
510 }
511 #endif
514 }
515
517 {
519
524
525 vbi_decoder_delete(ctx->
vbi);
528 return 0;
529 }
530
532 {
534 }
535
536 #define OFFSET(x) offsetof(TeletextContext, x)
537 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
541 {
"txt_format",
"format of the subtitles (bitmap or text)",
OFFSET(format_id),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
SD,
"txt_format"},
546 {
"txt_chop_spaces",
"chops leading and trailing spaces from text",
OFFSET(chop_spaces),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
SD},
547 {
"txt_duration",
"display duration of teletext pages in msecs",
OFFSET(sub_duration),
AV_OPT_TYPE_INT, {.i64 = 30000}, 0, 86400000,
SD},
548 {
"txt_transparent",
"force transparent background of the teletext",
OFFSET(transparent_bg),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
SD},
550 };
551
557 };
558
560 .
name =
"libzvbi_teletextdec",
570 .priv_class= &teletext_class,
571 };