qml: remove context indicator
[vlc.git] / modules / codec / telx.c
blobdc5259f4e2d6b527d431e436b8adf1a2f55c7b02
1 /*****************************************************************************
2 * telx.c : Minimalistic Teletext subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2007 Vincent Penne
5 * Some code converted from ProjectX java dvb decoder (c) 2001-2005 by dvb.matt
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
21 /*****************************************************************************
23 * information on teletext format can be found here :
24 * http://pdc.ro.nu/teletext.html
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <assert.h>
31 #include <stdint.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_bits.h>
37 #include <vlc_codec.h>
39 /* #define TELX_DEBUG */
40 #ifdef TELX_DEBUG
41 # define dbg( a ) msg_Dbg a
42 #else
43 # define dbg( a ) (void) 0
44 #endif
46 /*****************************************************************************
47 * Module descriptor.
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51 static int Decode( decoder_t *, block_t * );
53 #define OVERRIDE_PAGE_TEXT N_("Override page")
54 #define OVERRIDE_PAGE_LONGTEXT N_("Override the indicated page, try this if " \
55 "your subtitles don't appear (-1 = autodetect from TS, " \
56 "0 = autodetect from teletext, " \
57 ">0 = actual page number, usually 888 or 889).")
59 #define IGNORE_SUB_FLAG_TEXT N_("Ignore subtitle flag")
60 #define IGNORE_SUB_FLAG_LONGTEXT N_("Ignore the subtitle flag, try this if " \
61 "your subtitles don't appear.")
63 #define FRENCH_WORKAROUND_TEXT N_("Workaround for France")
64 #define FRENCH_WORKAROUND_LONGTEXT N_("Some French channels do not flag " \
65 "their subtitling pages correctly due to a historical " \
66 "interpretation mistake. Try using this wrong interpretation if " \
67 "your subtitles don't appear.")
69 vlc_module_begin ()
70 set_description( N_("Teletext subtitles decoder") )
71 set_shortname( "Teletext" )
72 set_capability( "spu decoder", 50 )
73 set_category( CAT_INPUT )
74 set_subcategory( SUBCAT_INPUT_SCODEC )
75 set_callbacks( Open, Close )
77 add_integer( "telx-override-page", -1,
78 OVERRIDE_PAGE_TEXT, OVERRIDE_PAGE_LONGTEXT, true )
79 add_bool( "telx-ignore-subtitle-flag", false,
80 IGNORE_SUB_FLAG_TEXT, IGNORE_SUB_FLAG_LONGTEXT, true )
81 add_bool( "telx-french-workaround", false,
82 FRENCH_WORKAROUND_TEXT, FRENCH_WORKAROUND_LONGTEXT, true )
84 vlc_module_end ()
86 /****************************************************************************
87 * Local structures
88 ****************************************************************************/
90 typedef struct
92 int i_align;
93 bool b_is_subtitle[9];
94 char ppsz_lines[32][128];
95 char psz_prev_text[512];
96 vlc_tick_t prev_pts;
97 int i_page[9];
98 bool b_erase[9];
99 const uint16_t * pi_active_national_set[9];
100 int i_wanted_page, i_wanted_magazine;
101 bool b_ignore_sub_flag;
102 } decoder_sys_t;
104 /****************************************************************************
105 * Local data
106 ****************************************************************************/
109 * My doc only mentions 13 national characters, but experiments show there
110 * are more, in france for example I already found two more (0x9 and 0xb).
112 * Conversion is in this order :
114 * 0x23 0x24 0x40 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x7b 0x7c 0x7d 0x7e
115 * (these are the standard ones)
116 * 0x08 0x09 0x0a 0x0b 0x0c 0x0d (apparently a control character) 0x0e 0x0f
119 static const uint16_t ppi_national_subsets[][20] =
121 { 0x00a3, 0x0024, 0x0040, 0x00ab, 0x00bd, 0x00bb, 0x005e, 0x0023,
122 0x002d, 0x00bc, 0x00a6, 0x00be, 0x00f7 }, /* english ,000 */
124 { 0x00e9, 0x00ef, 0x00e0, 0x00eb, 0x00ea, 0x00f9, 0x00ee, 0x0023,
125 0x00e8, 0x00e2, 0x00f4, 0x00fb, 0x00e7, 0, 0x00eb, 0, 0x00ef }, /* french ,001 */
127 { 0x0023, 0x00a4, 0x00c9, 0x00c4, 0x00d6, 0x00c5, 0x00dc, 0x005f,
128 0x00e9, 0x00e4, 0x00f6, 0x00e5, 0x00fc }, /* swedish,finnish,hungarian ,010 */
130 { 0x0023, 0x016f, 0x010d, 0x0165, 0x017e, 0x00fd, 0x00ed, 0x0159,
131 0x00e9, 0x00e1, 0x011b, 0x00fa, 0x0161 }, /* czech,slovak ,011 */
133 { 0x0023, 0x0024, 0x00a7, 0x00c4, 0x00d6, 0x00dc, 0x005e, 0x005f,
134 0x00b0, 0x00e4, 0x00f6, 0x00fc, 0x00df }, /* german ,100 */
136 { 0x00e7, 0x0024, 0x00a1, 0x00e1, 0x00e9, 0x00ed, 0x00f3, 0x00fa,
137 0x00bf, 0x00fc, 0x00f1, 0x00e8, 0x00e0 }, /* portuguese,spanish ,101 */
139 { 0x00a3, 0x0024, 0x00e9, 0x00b0, 0x00e7, 0x00bb, 0x005e, 0x0023,
140 0x00f9, 0x00e0, 0x00f2, 0x00e8, 0x00ec }, /* italian ,110 */
142 { 0x0023, 0x00a4, 0x0162, 0x00c2, 0x015e, 0x0102, 0x00ce, 0x0131,
143 0x0163, 0x00e2, 0x015f, 0x0103, 0x00ee }, /* rumanian ,111 */
145 /* I have these tables too, but I don't know how they can be triggered */
146 { 0x0023, 0x0024, 0x0160, 0x0117, 0x0119, 0x017d, 0x010d, 0x016b,
147 0x0161, 0x0105, 0x0173, 0x017e, 0x012f }, /* lettish,lithuanian ,1000 */
149 { 0x0023, 0x0144, 0x0105, 0x005a, 0x015a, 0x0141, 0x0107, 0x00f3,
150 0x0119, 0x017c, 0x015b, 0x0142, 0x017a }, /* polish, 1001 */
152 { 0x0023, 0x00cb, 0x010c, 0x0106, 0x017d, 0x0110, 0x0160, 0x00eb,
153 0x010d, 0x0107, 0x017e, 0x0111, 0x0161 }, /* serbian,croatian,slovenian, 1010 */
155 { 0x0023, 0x00f5, 0x0160, 0x00c4, 0x00d6, 0x017e, 0x00dc, 0x00d5,
156 0x0161, 0x00e4, 0x00f6, 0x017e, 0x00fc }, /* estonian ,1011 */
158 { 0x0054, 0x011f, 0x0130, 0x015e, 0x00d6, 0x00c7, 0x00dc, 0x011e,
159 0x0131, 0x015f, 0x00f6, 0x00e7, 0x00fc }, /* turkish ,1100 */
162 enum
164 FLAG_ERASE_PAGE = 0x000080,
165 FLAG_NEWSFLASH = 0x004000,
166 FLAG_SUBTITLE = 0x008000,
167 FLAG_SUPPRESS_HEADER = 0x010000,
168 FLAG_UPDATE = 0x020000,
169 FLAG_INTERRUPTED = 0x040000,
170 FLAG_INHIBIT_DISPLAY = 0x080000,
171 FLAG_MAGAZINE_SERIAL = 0x100000,
172 FLAG_FRAGMENT = 0x200000,
173 FLAG_PARTIAL_PAGE = 0x400000,
176 /*****************************************************************************
177 * Open: probe the decoder and return score
178 *****************************************************************************
179 * Tries to launch a decoder and return score so that the interface is able
180 * to chose.
181 *****************************************************************************/
182 static int Open( vlc_object_t *p_this )
184 decoder_t *p_dec = (decoder_t *) p_this;
185 decoder_sys_t *p_sys = NULL;
186 int i_val;
189 if( p_dec->fmt_in.i_codec != VLC_CODEC_TELETEXT)
191 return VLC_EGENERIC;
194 p_dec->pf_decode = Decode;
195 p_sys = p_dec->p_sys = calloc( 1, sizeof(*p_sys) );
196 if( p_sys == NULL )
197 return VLC_ENOMEM;
198 p_dec->fmt_out.i_codec = 0;
200 p_sys->i_align = 0;
201 for ( int i = 0; i < 9; i++ )
202 p_sys->pi_active_national_set[i] = ppi_national_subsets[1];
204 i_val = var_CreateGetInteger( p_dec, "telx-override-page" );
205 if( i_val == -1 && p_dec->fmt_in.subs.teletext.i_magazine < 9 &&
206 ( p_dec->fmt_in.subs.teletext.i_magazine != 1 ||
207 p_dec->fmt_in.subs.teletext.i_page != 0 ) ) /* ignore if TS demux wants page 100 (unlikely to be sub) */
209 bool b_val;
210 p_sys->i_wanted_magazine = p_dec->fmt_in.subs.teletext.i_magazine;
211 p_sys->i_wanted_page = p_dec->fmt_in.subs.teletext.i_page;
213 b_val = var_CreateGetBool( p_dec, "telx-french-workaround" );
214 if( p_sys->i_wanted_page < 100 &&
215 (b_val || (p_sys->i_wanted_page % 16) >= 10))
217 /* See http://www.nada.kth.se/~ragge/vdr/ttxtsubs/TROUBLESHOOTING.txt
218 * paragraph about French channels - they mix up decimal and
219 * hexadecimal */
220 p_sys->i_wanted_page = (p_sys->i_wanted_page / 10) * 16 +
221 (p_sys->i_wanted_page % 10);
224 else if( i_val <= 0 )
226 p_sys->i_wanted_magazine = -1;
227 p_sys->i_wanted_page = -1;
229 else
231 p_sys->i_wanted_magazine = i_val / 100;
232 p_sys->i_wanted_page = (((i_val % 100) / 10) << 4)
233 |((i_val % 100) % 10);
235 p_sys->b_ignore_sub_flag = var_CreateGetBool( p_dec,
236 "telx-ignore-subtitle-flag" );
238 msg_Dbg( p_dec, "starting telx on magazine %d page %02x flag %d",
239 p_sys->i_wanted_magazine, p_sys->i_wanted_page,
240 p_sys->b_ignore_sub_flag );
242 return VLC_SUCCESS;
244 /* error: */
245 /* if (p_sys) { */
246 /* free(p_sys); */
247 /* p_sys = NULL; */
248 /* } */
249 /* return VLC_EGENERIC; */
252 /*****************************************************************************
253 * Close:
254 *****************************************************************************/
255 static void Close( vlc_object_t *p_this )
257 decoder_t *p_dec = (decoder_t*) p_this;
258 decoder_sys_t *p_sys = p_dec->p_sys;
260 free( p_sys );
263 /**************************
264 * change bits endianness *
265 **************************/
266 static uint8_t bytereverse( int n )
268 n = (((n >> 1) & 0x55) | ((n << 1) & 0xaa));
269 n = (((n >> 2) & 0x33) | ((n << 2) & 0xcc));
270 n = (((n >> 4) & 0x0f) | ((n << 4) & 0xf0));
271 return n;
274 static int hamming_8_4( int a )
276 switch (a) {
277 case 0xA8:
278 return 0;
279 case 0x0B:
280 return 1;
281 case 0x26:
282 return 2;
283 case 0x85:
284 return 3;
285 case 0x92:
286 return 4;
287 case 0x31:
288 return 5;
289 case 0x1C:
290 return 6;
291 case 0xBF:
292 return 7;
293 case 0x40:
294 return 8;
295 case 0xE3:
296 return 9;
297 case 0xCE:
298 return 10;
299 case 0x6D:
300 return 11;
301 case 0x7A:
302 return 12;
303 case 0xD9:
304 return 13;
305 case 0xF4:
306 return 14;
307 case 0x57:
308 return 15;
309 default:
310 return -1; // decoding error , not yet corrected
314 // utc-2 --> utf-8
315 // this is not a general function, but it's enough for what we do here
316 // the result buffer need to be at least 4 bytes long
317 static void to_utf8( char * res, uint16_t ch )
319 if( ch >= 0x80 )
321 if( ch >= 0x800 )
323 res[0] = (ch >> 12) | 0xE0;
324 res[1] = ((ch >> 6) & 0x3F) | 0x80;
325 res[2] = (ch & 0x3F) | 0x80;
326 res[3] = 0;
328 else
330 res[0] = (ch >> 6) | 0xC0;
331 res[1] = (ch & 0x3F) | 0x80;
332 res[2] = 0;
335 else
337 res[0] = ch;
338 res[1] = 0;
342 static void decode_string( char * res, int res_len,
343 decoder_sys_t *p_sys, int magazine,
344 const uint8_t * packet, int len )
346 char utf8[7];
347 char * pt = res;
349 for ( int i = 0; i < len; i++ )
351 int in = bytereverse( packet[i] ) & 0x7f;
352 uint16_t out = 32;
353 size_t l;
355 switch ( in )
357 /* special national characters */
358 case 0x23:
359 out = p_sys->pi_active_national_set[magazine][0];
360 break;
361 case 0x24:
362 out = p_sys->pi_active_national_set[magazine][1];
363 break;
364 case 0x40:
365 out = p_sys->pi_active_national_set[magazine][2];
366 break;
367 case 0x5b:
368 out = p_sys->pi_active_national_set[magazine][3];
369 break;
370 case 0x5c:
371 out = p_sys->pi_active_national_set[magazine][4];
372 break;
373 case 0x5d:
374 out = p_sys->pi_active_national_set[magazine][5];
375 break;
376 case 0x5e:
377 out = p_sys->pi_active_national_set[magazine][6];
378 break;
379 case 0x5f:
380 out = p_sys->pi_active_national_set[magazine][7];
381 break;
382 case 0x60:
383 out = p_sys->pi_active_national_set[magazine][8];
384 break;
385 case 0x7b:
386 out = p_sys->pi_active_national_set[magazine][9];
387 break;
388 case 0x7c:
389 out = p_sys->pi_active_national_set[magazine][10];
390 break;
391 case 0x7d:
392 out = p_sys->pi_active_national_set[magazine][11];
393 break;
394 case 0x7e:
395 out = p_sys->pi_active_national_set[magazine][12];
396 break;
398 /* some special control characters (empirical) */
399 case 0x0d:
400 /* apparently this starts a sequence that ends with 0xb 0xb */
401 while ( i + 1 < len && (bytereverse( packet[i+1] ) & 0x7f) != 0x0b )
402 i++;
403 i += 2;
404 break;
405 /* goto skip; */
407 default:
408 /* non documented national range 0x08 - 0x0f */
409 if ( in >= 0x08 && in <= 0x0f )
411 out = p_sys->pi_active_national_set[magazine][13 + in - 8];
412 break;
415 /* normal ascii */
416 if ( in > 32 && in < 0x7f )
417 out = in;
420 /* handle undefined national characters */
421 if ( out == 0 )
422 out = 32;
424 /* convert to utf-8 */
425 to_utf8( utf8, out );
426 l = strlen( utf8 );
427 if ( pt + l < res + res_len - 1 )
429 strcpy(pt, utf8);
430 pt += l;
433 /* skip: ; */
435 /* end: */
436 *pt++ = 0;
439 static bool DecodePageHeaderPacket( decoder_t *p_dec, const uint8_t *packet,
440 int magazine )
442 decoder_sys_t *p_sys = p_dec->p_sys;
444 int flag = 0;
445 char psz_line[256];
447 for ( int a = 0; a < 6; a++ )
449 flag |= (0xF & (bytereverse( hamming_8_4(packet[8 + a]) ) >> 4))
450 << (a * 4);
453 /* if (!p_sys->b_ignore_sub_flag && !(flag & FLAG_SUBTITLE)) */
454 /* return false; */
456 p_sys->i_page[magazine] = (0xF0 & bytereverse( hamming_8_4(packet[7]) )) | /* tens */
457 (0x0F & (bytereverse( hamming_8_4(packet[6]) ) >> 4) ); /* units */
459 decode_string( psz_line, sizeof(psz_line), p_sys, magazine,
460 packet + 14, 40 - 14 );
462 dbg((p_dec, "mag %d flags %x page %x character set %d subtitles %d %s", magazine, flag,
463 p_sys->i_page[magazine],
464 7 & flag>>21, !!(flag & FLAG_SUBTITLE), psz_line));
466 p_sys->pi_active_national_set[magazine] =
467 ppi_national_subsets[7 & (flag >> 21)];
469 int subtitlesflags = FLAG_SUBTITLE;
470 if( !p_sys->b_ignore_sub_flag && p_sys->i_wanted_magazine != 0x07 )
471 subtitlesflags |= FLAG_SUPPRESS_HEADER;
473 p_sys->b_is_subtitle[magazine] = !((flag & subtitlesflags) != subtitlesflags);
475 dbg(( p_dec, "FLAGS%s%s%s%s%s%s%s mag_ser %d",
476 (flag & FLAG_ERASE_PAGE) ? " erase" : "",
477 (flag & FLAG_NEWSFLASH) ? " news" : "",
478 (flag & FLAG_SUBTITLE) ? " subtitle" : "",
479 (flag & FLAG_SUPPRESS_HEADER)? " suppressed_head" : "",
480 (flag & FLAG_UPDATE) ? " update" : "",
481 (flag & FLAG_INTERRUPTED) ? " interrupt" : "",
482 (flag & FLAG_INHIBIT_DISPLAY)? " inhibit" : "",
483 !!(flag & FLAG_MAGAZINE_SERIAL) ));
485 if ( (p_sys->i_wanted_page != -1
486 && p_sys->i_page[magazine] != p_sys->i_wanted_page)
487 || !p_sys->b_is_subtitle[magazine] )
488 return false;
490 p_sys->b_erase[magazine] = !!(flag & FLAG_ERASE_PAGE);
492 /* kludge here :
493 * we ignore the erase flag if it happens less than 1.5 seconds
494 * before last caption
495 * TODO make this time configurable
496 * UPDATE the kludge seems to be no more necessary
497 * so it's commented out*/
498 if ( /*p_block->i_pts > p_sys->prev_pts + 1500000 && */
499 p_sys->b_erase[magazine] )
501 dbg((p_dec, "ERASE !"));
503 p_sys->b_erase[magazine] = 0;
504 for ( int i = 1; i < 32; i++ )
506 if ( !p_sys->ppsz_lines[i][0] ) continue;
507 /* b_update = true; */
508 p_sys->ppsz_lines[i][0] = 0;
512 /* replace the row if it's different */
513 if ( strcmp(psz_line, p_sys->ppsz_lines[0]) )
515 strncpy( p_sys->ppsz_lines[0], psz_line,
516 sizeof(p_sys->ppsz_lines[0]) - 1);
519 return true;
522 static bool DecodePacketX1_X23( decoder_t *p_dec, const uint8_t *packet,
523 int magazine, int row, vlc_tick_t pts )
525 decoder_sys_t *p_sys = p_dec->p_sys;
527 bool b_update = false;
528 char psz_line[256];
529 char * t;
530 int i;
532 if ( p_sys->i_wanted_page == -1 && p_sys->i_page[magazine] > 0x99)
533 return false;
535 decode_string( psz_line, sizeof(psz_line), p_sys, magazine,
536 packet + 6, 40 );
537 t = psz_line;
539 /* remove starting spaces */
540 while ( *t == 32 ) t++;
542 /* remove trailing spaces */
543 for ( i = strlen(t) - 1; i >= 0 && t[i] == 32; i-- );
544 t[i + 1] = 0;
546 /* replace the row if it's different */
547 if ( strcmp( t, p_sys->ppsz_lines[row] ) )
549 strncpy( p_sys->ppsz_lines[row], t,
550 sizeof(p_sys->ppsz_lines[row]) - 1 );
551 b_update = true;
554 if (t[0])
555 p_sys->prev_pts = pts;
557 dbg((p_dec, "%d %d : ", magazine, row));
558 dbg((p_dec, "%s", t));
560 #ifdef TELX_DEBUG
562 char dbg[256];
563 dbg[0] = 0;
564 for ( i = 0; i < 40; i++ )
566 int in = bytereverse(packet[6 + i]) & 0x7f;
567 sprintf(dbg + strlen(dbg), "%02x ", in);
569 dbg((p_dec, "%s", dbg));
570 dbg[0] = 0;
571 for ( i = 0; i < 40; i++ )
573 decode_string( psz_line, sizeof(psz_line), p_sys, magazine,
574 packet + 6 + i, 1 );
575 sprintf( dbg + strlen(dbg), "%s ", psz_line );
577 dbg((p_dec, "%s", dbg));
579 #endif
580 return b_update;
584 static bool DecodePacketX25( decoder_t *p_dec, const uint8_t *packet,
585 int magazine )
587 decoder_sys_t *p_sys = p_dec->p_sys;
589 /* row 25 : alternate header line */
590 char psz_line[256];
591 decode_string( psz_line, sizeof(psz_line), p_sys, magazine,
592 packet + 6, 40 );
594 /* replace the row if it's different */
595 if ( strcmp( psz_line, p_sys->ppsz_lines[0] ) )
597 strncpy( p_sys->ppsz_lines[0], psz_line,
598 sizeof(p_sys->ppsz_lines[0]) - 1 );
599 /* return true; */
602 return false;
605 static bool DecodeNormalPacket( decoder_t *p_dec, const uint8_t *packet,
606 int magazine, int row, vlc_tick_t pts )
608 decoder_sys_t *p_sys = p_dec->p_sys;
610 if( (p_sys->i_wanted_page != -1 &&
611 p_sys->i_page[magazine] != p_sys->i_wanted_page)
612 || !p_sys->b_is_subtitle[magazine] )
613 return false;
615 if( row < 24 ) /* row 1-23 : normal lines */
616 return DecodePacketX1_X23( p_dec, packet, magazine, row, pts );
617 else if( row == 25 ) /* row 25 : alternate header line */
618 return DecodePacketX25( p_dec, packet, magazine );
619 else
620 return false;
624 /*****************************************************************************
625 * Decode:
626 *****************************************************************************/
627 static int Decode( decoder_t *p_dec, block_t *p_block )
629 decoder_sys_t *p_sys = p_dec->p_sys;
630 subpicture_t *p_spu = NULL;
631 video_format_t fmt;
632 /* int erase = 0; */
633 #if 0
634 int i_wanted_magazine = i_conf_wanted_page / 100;
635 int i_wanted_page = 0x10 * ((i_conf_wanted_page % 100) / 10)
636 | (i_conf_wanted_page % 10);
637 #endif
638 bool b_update = false;
639 char psz_text[512], *pt = psz_text;
640 int total;
642 if( p_block == NULL ) /* No Drain */
643 return VLCDEC_SUCCESS;
645 // dbg((p_dec, "start of telx packet with header %2x",
646 // * (uint8_t *) p_block->p_buffer));
647 for ( size_t offset = 1; offset + 46 <= p_block->i_buffer; offset += 46 )
649 const uint8_t *packet = &p_block->p_buffer[offset];
650 // int vbi = ((0x20 & packet[2]) != 0 ? 0 : 313) + (0x1F & packet[2]);
652 // dbg((p_dec, "vbi %d header %02x %02x %02x", vbi, packet[0], packet[1], packet[2]));
653 if ( packet[0] == 0xFF ) continue;
655 /* if (packet[1] != 0x2C) { */
656 /* printf("wrong header\n"); */
657 /* //goto error; */
658 /* continue; */
659 /* } */
661 /* See EN.300.706 7.1.4 */
662 int mpag = (hamming_8_4( packet[4] ) << 4) | hamming_8_4( packet[5] );
663 int row, magazine;
664 if ( mpag < 0 )
666 /* decode error */
667 dbg((p_dec, "mpag hamming error"));
668 continue;
671 /* magazine number 0-7, row 0-31 (== packet number... or Y) */
672 row = 0xFF & bytereverse(mpag);
673 magazine = (7 & row) == 0 ? 8 : (7 & row);
674 row >>= 3;
676 if ( p_sys->i_wanted_page != -1
677 && magazine != p_sys->i_wanted_magazine )
678 continue;
680 if ( row == 0 ) /* Page Header Packet */
682 /* row 0 : flags and header line */
683 b_update |= DecodePageHeaderPacket( p_dec, packet, magazine );
684 if( b_update )
685 dbg((p_dec, "%ld --> %ld", (long int) p_block->i_pts,
686 (long int)(p_sys->prev_pts+1500000)));
688 else if ( row < 26 ) /* Normal Packet */
690 b_update |= DecodeNormalPacket( p_dec, packet, magazine, row,
691 p_block->i_pts );
693 // else if (row >= 26) { /* Non Displayable Packet */
694 // // row 26 : TV listings
695 // dbg((p_dec, "%d %d : %s", magazine, row, decode_string(p_sys, magazine, packet+6, 40)));
696 // }
699 if ( !b_update )
700 goto error;
702 total = 0;
703 for ( int i = 1; i < 24; i++ )
705 size_t l = strlen( p_sys->ppsz_lines[i] );
707 if ( l > sizeof(psz_text) - total - 1 )
708 l = sizeof(psz_text) - total - 1;
710 if ( l > 0 )
712 memcpy( pt, p_sys->ppsz_lines[i], l );
713 total += l;
714 pt += l;
715 if ( sizeof(psz_text) - total - 1 > 0 )
717 *pt++ = '\n';
718 total++;
722 *pt = 0;
724 if ( !strcmp(psz_text, p_sys->psz_prev_text) )
725 goto error;
727 dbg((p_dec, "UPDATE TELETEXT PICTURE"));
729 assert( sizeof(p_sys->psz_prev_text) >= sizeof(psz_text) );
730 strcpy( p_sys->psz_prev_text, psz_text );
732 /* Create the subpicture unit */
733 p_spu = decoder_NewSubpicture( p_dec, NULL );
734 if( !p_spu )
736 msg_Warn( p_dec, "can't get spu buffer" );
737 goto error;
740 /* Create a new subpicture region */
741 video_format_Init(&fmt, VLC_CODEC_TEXT);
742 p_spu->p_region = subpicture_region_New( &fmt );
743 if( p_spu->p_region == NULL )
745 msg_Err( p_dec, "cannot allocate SPU region" );
746 goto error;
749 /* Normal text subs, easy markup */
750 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
751 p_spu->p_region->i_x = p_sys->i_align ? 20 : 0;
752 p_spu->p_region->i_y = 10;
753 p_spu->p_region->p_text = text_segment_New(psz_text);
755 p_spu->i_start = p_block->i_pts;
756 p_spu->i_stop = p_block->i_pts + p_block->i_length;
757 p_spu->b_ephemer = (p_block->i_length == 0);
758 p_spu->b_absolute = false;
759 dbg((p_dec, "%ld --> %ld", (long int) p_block->i_pts/100000, (long int)p_block->i_length/100000));
761 block_Release( p_block );
762 if( p_spu != NULL )
763 decoder_QueueSub( p_dec, p_spu );
764 return VLCDEC_SUCCESS;
766 error:
767 if ( p_spu != NULL )
769 subpicture_Delete( p_spu );
770 p_spu = NULL;
773 block_Release( p_block );
774 return VLCDEC_SUCCESS;