1 /*****************************************************************************
2 * kate.c : a decoder for the kate bitstream format
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_input.h>
33 #include <vlc_codec.h>
35 #include <kate/kate.h>
39 /* #define ENABLE_PACKETIZER */
40 /* #define ENABLE_FORMATTING */
41 /* #define ENABLE_BITMAPS */
43 /*****************************************************************************
44 * decoder_sys_t : decoder descriptor
45 *****************************************************************************/
48 #ifdef ENABLE_PACKETIZER
75 #ifdef ENABLE_FORMATTING
80 /*****************************************************************************
82 *****************************************************************************/
83 static int OpenDecoder ( vlc_object_t * );
84 static void CloseDecoder ( vlc_object_t * );
85 #ifdef ENABLE_PACKETIZER
86 static int OpenPacketizer( vlc_object_t *p_this );
89 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block );
90 static int ProcessHeaders( decoder_t *p_dec );
91 static subpicture_t *ProcessPacket( decoder_t *p_dec, kate_packet *p_kp,
93 static subpicture_t *DecodePacket( decoder_t *p_dec, kate_packet *p_kp,
95 static void ParseKateComments( decoder_t * );
97 #define DEFAULT_NAME "Default"
100 /*****************************************************************************
102 *****************************************************************************/
104 #ifdef ENABLE_FORMATTING
105 #define FORMAT_TEXT N_("Formatted Subtitles")
106 #define FORMAT_LONGTEXT N_("Kate streams allow for text formatting. " \
107 "VLC partly implements this, but you can choose to disable all formatting.")
112 set_shortname( _("Kate"));
113 set_description( _("Kate text subtitles decoder") );
114 set_capability( "decoder", 50 );
115 set_callbacks( OpenDecoder, CloseDecoder );
116 set_category( CAT_INPUT );
117 set_subcategory( SUBCAT_INPUT_SCODEC );
118 add_shortcut( "kate" );
120 #ifdef ENABLE_PACKETIZER
122 set_description( _("Kate text subtitles packetizer") );
123 set_capability( "packetizer", 100 );
124 set_callbacks( OpenPacketizer, CloseDecoder );
125 add_shortcut( "kate" );
128 #ifdef ENABLE_FORMATTING
129 add_bool( "kate-formatted", true, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
134 /*****************************************************************************
135 * OpenDecoder: probe the decoder and return score
136 *****************************************************************************
137 * Tries to launch a decoder and return score so that the interface is able
139 *****************************************************************************/
140 static int OpenDecoder( vlc_object_t *p_this )
142 decoder_t *p_dec = (decoder_t*)p_this;
143 decoder_sys_t *p_sys;
145 msg_Dbg( p_dec, "kate: OpenDecoder");
147 if( p_dec->fmt_in.i_codec != VLC_FOURCC('k','a','t','e') )
153 p_dec->pf_decode_sub = (subpicture_t *(*)(decoder_t *, block_t **))
155 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
158 /* Allocate the memory needed to store the decoder's structure */
159 if( ( p_dec->p_sys = p_sys =
160 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
162 msg_Err( p_dec, "out of memory" );
167 #ifdef ENABLE_PACKETIZER
168 p_sys->b_packetizer = false;
170 p_sys->b_ready = false;
173 kate_comment_init( &p_sys->kc );
174 kate_info_init( &p_sys->ki );
176 p_sys->i_num_headers = 0;
177 p_sys->i_headers = 0;
179 /* retrieve options */
180 #ifdef ENABLE_FORMATTING
181 p_sys->b_formatted = var_CreateGetBool( p_dec, "kate-formatted" );
187 #ifdef ENABLE_PACKETIZER
188 static int OpenPacketizer( vlc_object_t *p_this )
190 decoder_t *p_dec = (decoder_t*)p_this;
192 int i_ret = OpenDecoder( p_this );
194 if( i_ret == VLC_SUCCESS )
196 p_dec->p_sys->b_packetizer = true;
197 p_dec->fmt_out.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
204 /****************************************************************************
205 * DecodeBlock: the whole thing
206 ****************************************************************************
207 * This function must be fed with kate packets.
208 ****************************************************************************/
209 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
211 decoder_sys_t *p_sys = p_dec->p_sys;
215 if( !pp_block || !*pp_block ) return NULL;
219 /* Block to Kate packet */
220 kate_packet_wrap(&kp, p_block->i_buffer, p_block->p_buffer);
222 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
224 /* Headers already available as extra data */
225 p_sys->i_num_headers = ((unsigned char*)p_dec->fmt_in.p_extra)[0];
226 p_sys->i_headers = p_sys->i_num_headers;
228 else if( kp.nbytes && (p_sys->i_headers==0 || p_sys->i_headers < p_sys->ki.num_headers ))
230 /* Backup headers as extra data */
233 p_dec->fmt_in.p_extra =
234 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra + kp.nbytes + 2 );
235 p_extra = (void*)(((unsigned char*)p_dec->fmt_in.p_extra) + p_dec->fmt_in.i_extra);
236 *(p_extra++) = kp.nbytes >> 8;
237 *(p_extra++) = kp.nbytes & 0xFF;
239 memcpy( p_extra, kp.data, kp.nbytes );
240 p_dec->fmt_in.i_extra += kp.nbytes + 2;
242 block_Release( *pp_block );
243 p_sys->i_num_headers = ((unsigned char*)p_dec->fmt_in.p_extra)[0];
248 if( p_sys->i_headers == p_sys->i_num_headers && p_sys->i_num_headers>0 )
250 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
252 p_sys->i_headers = 0;
253 p_dec->fmt_in.i_extra = 0;
254 block_Release( *pp_block );
257 else p_sys->i_headers++;
260 return ProcessPacket( p_dec, &kp, pp_block );
263 /*****************************************************************************
264 * ProcessHeaders: process Kate headers.
265 *****************************************************************************/
266 static int ProcessHeaders( decoder_t *p_dec )
268 decoder_sys_t *p_sys = p_dec->p_sys;
275 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
277 p_extra = p_dec->fmt_in.p_extra;
278 i_extra = p_dec->fmt_in.i_extra;
280 /* skip number of headers */
284 /* Take care of the initial Kate header */
285 kp.nbytes = *(p_extra++) << 8;
286 kp.nbytes |= (*(p_extra++) & 0xFF);
288 p_extra += kp.nbytes;
289 i_extra -= (kp.nbytes + 2);
292 msg_Err( p_dec, "header data corrupted");
296 ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
299 msg_Err( p_dec, "this bitstream does not contain Kate data (%d)", ret );
303 msg_Dbg( p_dec, "%s %s text, granule rate %f, granule shift %d",
304 p_sys->ki.language, p_sys->ki.category,
305 (double)p_sys->ki.gps_numerator/p_sys->ki.gps_denominator,
306 p_sys->ki.granule_shift);
308 /* we want markup to be removed for now */
309 kate_info_remove_markup( &p_sys->ki, 1 );
311 /* parse all remaining header packets */
312 for (headeridx=1; headeridx<p_sys->ki.num_headers; ++headeridx)
314 kp.nbytes = *(p_extra++) << 8;
315 kp.nbytes |= (*(p_extra++) & 0xFF);
317 p_extra += kp.nbytes;
318 i_extra -= (kp.nbytes + 2);
321 msg_Err( p_dec, "header %d data corrupted", headeridx);
325 ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
328 msg_Err( p_dec, "Kate header %d is corrupted: %d", headeridx, ret);
332 /* header 1 is comments */
335 ParseKateComments( p_dec );
339 #ifdef ENABLE_PACKETIZER
340 if( !p_sys->b_packetizer )
343 /* We have all the headers, initialize decoder */
344 msg_Dbg( p_dec, "we have all headers, initialize libkate for decoding" );
345 ret = kate_decode_init( &p_sys->k, &p_sys->ki );
348 msg_Err( p_dec, "Kate failed to initialize for decoding: %d", ret );
351 p_sys->b_ready = true;
353 #ifdef ENABLE_PACKETIZER
356 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
357 p_dec->fmt_out.p_extra =
358 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
359 memcpy( p_dec->fmt_out.p_extra,
360 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
367 /*****************************************************************************
368 * ProcessPacket: processes a kate packet.
369 *****************************************************************************/
370 static subpicture_t *ProcessPacket( decoder_t *p_dec, kate_packet *p_kp,
373 decoder_sys_t *p_sys = p_dec->p_sys;
374 block_t *p_block = *pp_block;
375 subpicture_t *p_buf = NULL;
377 /* Date management */
378 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
380 p_sys->i_pts = p_block->i_pts;
383 *pp_block = NULL; /* To avoid being fed the same packet again */
385 #ifdef ENABLE_PACKETIZER
386 if( p_sys->b_packetizer )
388 /* Date management */
389 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
391 if( p_sys->i_headers >= p_sys->i_num_headers )
392 p_block->i_length = p_sys->i_pts - p_block->i_pts;
394 p_block->i_length = 0;
401 if( p_sys->i_headers >= p_sys->i_num_headers )
402 p_buf = DecodePacket( p_dec, p_kp, p_block );
406 if( p_block ) block_Release( p_block );
412 #ifdef ENABLE_BITMAPS
413 /* nicked off blend.c */
414 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
415 int r, int g, int b )
417 *y = ( ( ( 66 * r + 129 * g + 25 * b + 128 ) >> 8 ) + 16 );
418 *u = ( ( -38 * r - 74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
419 *v = ( ( 112 * r - 94 * g - 18 * b + 128 ) >> 8 ) + 128 ;
423 /*****************************************************************************
424 * DecodePacket: decodes a Kate packet.
425 *****************************************************************************/
426 static subpicture_t *DecodePacket( decoder_t *p_dec, kate_packet *p_kp, block_t *p_block )
428 decoder_sys_t *p_sys = p_dec->p_sys;
429 const kate_event *ev=NULL;
430 subpicture_t *p_spu = NULL;
431 subpicture_region_t *p_bitmap_region = NULL;
433 #ifdef ENABLE_BITMAPS
435 picture_t *pic = NULL;
439 bool tracker_valid = false;
441 ret = kate_decode_packetin( &p_sys->k, p_kp );
444 msg_Err( p_dec, "Kate failed to decode packet: %d", ret );
448 ret = kate_decode_eventout( &p_sys->k, &ev );
451 msg_Err( p_dec, "Kate failed to retrieve event: %d", ret );
456 /* no event to go with this packet, this is normal */
460 /* we have an event */
463 p_spu = p_dec->pf_spu_buffer_new( p_dec );
466 msg_Err( p_dec, "Failed to allocate spu buffer" );
470 p_spu->b_pausable = true;
472 #ifdef ENABLE_BITMAPS
473 if (ev->bitmap && ev->palette) {
474 /* create a separate region for the bitmap */
475 memset( &fmt, 0, sizeof(video_format_t) );
476 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
478 fmt.i_width = fmt.i_visible_width = ev->bitmap->width;
479 fmt.i_height = fmt.i_visible_height = ev->bitmap->height;
480 fmt.i_x_offset = fmt.i_y_offset = 0;
482 p_bitmap_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
483 if( !p_bitmap_region )
485 msg_Err( p_dec, "cannot allocate SPU region" );
486 p_dec->pf_spu_buffer_del( p_dec, p_spu );
490 /* create the palette */
491 fmt.p_palette->i_entries = ev->palette->ncolors;
492 for (n=0; n<ev->palette->ncolors; ++n)
495 &fmt.p_palette->palette[n][0], &fmt.p_palette->palette[n][1], &fmt.p_palette->palette[n][2],
496 ev->palette->colors[n].r, ev->palette->colors[n].g, ev->palette->colors[n].b
498 fmt.p_palette->palette[n][3] = ev->palette->colors[n].a;
501 /* create the bitmap */
502 pic = &p_bitmap_region->picture;
503 for (y=0; y<ev->bitmap->height; ++y) {
504 uint8_t *dest=pic->Y_PIXELS+pic->Y_PITCH*y;
505 const uint8_t *src=ev->bitmap->pixels+y*ev->bitmap->width;
506 memcpy(dest, src, ev->bitmap->width);
509 msg_Dbg(p_dec, "Created bitmap, %dx%d, %d colors\n", ev->bitmap->width, ev->bitmap->height, ev->palette->ncolors);
513 /* Create a new subpicture region */
514 memset( &fmt, 0, sizeof(video_format_t) );
515 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
517 fmt.i_width = fmt.i_height = 0;
518 fmt.i_x_offset = fmt.i_y_offset = 0;
520 #ifdef ENABLE_FORMATTING
521 if (p_sys->b_formatted)
523 ret = kate_tracker_init( &kin, &p_sys->ki, ev);
526 msg_Err( p_dec, "failed to initialize kate tracker, event will be unformatted: %d", ret );
530 // TODO: get window/video sizes/pos - can't find where to get those !
533 ret = kate_tracker_update(&kin, 0, w, h, 0, 0, w, h);
536 kate_tracker_clear(&kin);
537 msg_Err( p_dec, "failed to update kate tracker, event will be unformatted: %d", ret );
543 fmt.i_width = kin.region_w;
544 fmt.i_height = kin.region_h;
547 // TODO: parse tracker and set style, init fmt
548 tracker_valid = true;
556 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
557 if( !p_spu->p_region )
559 msg_Err( p_dec, "cannot allocate SPU region" );
560 p_dec->pf_spu_buffer_del( p_dec, p_spu );
564 p_spu->p_region->psz_text = strdup(ev->text); /* no leak, this actually gets killed by the core */
565 p_spu->i_start = p_block->i_pts;
566 p_spu->i_stop = p_block->i_pts + I64C(1000000)*ev->duration*p_sys->ki.gps_denominator/p_sys->ki.gps_numerator;
567 p_spu->b_ephemer = (p_block->i_length == 0);
568 p_spu->b_absolute = false;
575 p_spu->p_region->i_x = kin.region_x;
576 p_spu->p_region->i_y = kin.region_y;
577 if (p_bitmap_region) {
578 p_bitmap_region->i_x = kin.region_x;
579 p_bitmap_region->i_y = kin.region_y;
583 kate_tracker_clear(&kin);
587 /* Normal text subs, easy markup */
588 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
589 if (p_bitmap_region) {
590 p_bitmap_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
596 #ifdef ENABLE_BITMAPS
597 /* if we have a bitmap, chain it before the text */
600 p_bitmap_region->p_next = p_spu->p_region;
601 p_spu->p_region = p_bitmap_region;
608 /*****************************************************************************
609 * ParseKateComments: FIXME should be done in demuxer
610 *****************************************************************************/
611 static void ParseKateComments( decoder_t *p_dec )
613 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
614 char *psz_name, *psz_value, *psz_comment;
617 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
619 while ( i < p_dec->p_sys->kc.comments )
621 psz_comment = strdup( p_dec->p_sys->kc.user_comments[i] );
624 msg_Warn( p_dec, "out of memory" );
627 psz_name = psz_comment;
628 psz_value = strchr( psz_comment, '=' );
633 input_Control( p_input, INPUT_ADD_INFO, _("Kate comment"),
634 psz_name, "%s", psz_value );
641 /*****************************************************************************
642 * CloseDecoder: clean up the decoder
643 *****************************************************************************/
644 static void CloseDecoder( vlc_object_t *p_this )
646 decoder_t *p_dec = (decoder_t *)p_this;
647 decoder_sys_t *p_sys = p_dec->p_sys;
650 kate_clear( &p_sys->k );
651 kate_info_clear( &p_sys->ki );
652 kate_comment_clear( &p_sys->kc );