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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_input.h>
34 #include <vlc_codec.h>
36 #include <kate/kate.h>
40 /* #define ENABLE_PACKETIZER */
41 /* #define ENABLE_FORMATTING */
42 #define ENABLE_BITMAPS
44 /*****************************************************************************
45 * decoder_sys_t : decoder descriptor
46 *****************************************************************************/
49 #ifdef ENABLE_PACKETIZER
76 #ifdef ENABLE_FORMATTING
81 /*****************************************************************************
83 *****************************************************************************/
84 static int OpenDecoder ( vlc_object_t * );
85 static void CloseDecoder ( vlc_object_t * );
86 #ifdef ENABLE_PACKETIZER
87 static int OpenPacketizer( vlc_object_t *p_this );
90 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block );
91 static int ProcessHeaders( decoder_t *p_dec );
92 static subpicture_t *ProcessPacket( decoder_t *p_dec, kate_packet *p_kp,
94 static subpicture_t *DecodePacket( decoder_t *p_dec, kate_packet *p_kp,
96 static void ParseKateComments( decoder_t * );
98 #define DEFAULT_NAME "Default"
101 /*****************************************************************************
103 *****************************************************************************/
105 #ifdef ENABLE_FORMATTING
106 #define FORMAT_TEXT N_("Formatted Subtitles")
107 #define FORMAT_LONGTEXT N_("Kate streams allow for text formatting. " \
108 "VLC partly implements this, but you can choose to disable all formatting.")
113 set_shortname( N_("Kate"));
114 set_description( N_("Kate text subtitles decoder") );
115 set_capability( "decoder", 50 );
116 set_callbacks( OpenDecoder, CloseDecoder );
117 set_category( CAT_INPUT );
118 set_subcategory( SUBCAT_INPUT_SCODEC );
119 add_shortcut( "kate" );
121 #ifdef ENABLE_PACKETIZER
123 set_description( N_("Kate text subtitles packetizer") );
124 set_capability( "packetizer", 100 );
125 set_callbacks( OpenPacketizer, CloseDecoder );
126 add_shortcut( "kate" );
129 #ifdef ENABLE_FORMATTING
130 add_bool( "kate-formatted", true, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
135 /*****************************************************************************
136 * OpenDecoder: probe the decoder and return score
137 *****************************************************************************
138 * Tries to launch a decoder and return score so that the interface is able
140 *****************************************************************************/
141 static int OpenDecoder( vlc_object_t *p_this )
143 decoder_t *p_dec = (decoder_t*)p_this;
144 decoder_sys_t *p_sys;
146 msg_Dbg( p_dec, "kate: OpenDecoder");
148 if( p_dec->fmt_in.i_codec != VLC_FOURCC('k','a','t','e') )
154 p_dec->pf_decode_sub = (subpicture_t *(*)(decoder_t *, block_t **))
156 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
159 /* Allocate the memory needed to store the decoder's structure */
160 if( ( p_dec->p_sys = p_sys =
161 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
165 #ifdef ENABLE_PACKETIZER
166 p_sys->b_packetizer = false;
168 p_sys->b_ready = false;
171 kate_comment_init( &p_sys->kc );
172 kate_info_init( &p_sys->ki );
174 p_sys->i_num_headers = 0;
175 p_sys->i_headers = 0;
177 /* retrieve options */
178 #ifdef ENABLE_FORMATTING
179 p_sys->b_formatted = var_CreateGetBool( p_dec, "kate-formatted" );
185 #ifdef ENABLE_PACKETIZER
186 static int OpenPacketizer( vlc_object_t *p_this )
188 decoder_t *p_dec = (decoder_t*)p_this;
190 int i_ret = OpenDecoder( p_this );
192 if( i_ret == VLC_SUCCESS )
194 p_dec->p_sys->b_packetizer = true;
195 p_dec->fmt_out.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
202 /****************************************************************************
203 * DecodeBlock: the whole thing
204 ****************************************************************************
205 * This function must be fed with kate packets.
206 ****************************************************************************/
207 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
209 decoder_sys_t *p_sys = p_dec->p_sys;
213 if( !pp_block || !*pp_block ) return NULL;
217 /* Block to Kate packet */
218 kate_packet_wrap(&kp, p_block->i_buffer, p_block->p_buffer);
220 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
222 /* Headers already available as extra data */
223 p_sys->i_num_headers = ((unsigned char*)p_dec->fmt_in.p_extra)[0];
224 p_sys->i_headers = p_sys->i_num_headers;
226 else if( kp.nbytes && (p_sys->i_headers==0 || p_sys->i_headers < p_sys->ki.num_headers ))
228 /* Backup headers as extra data */
231 p_dec->fmt_in.p_extra =
232 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra + kp.nbytes + 2 );
233 p_extra = (void*)(((unsigned char*)p_dec->fmt_in.p_extra) + p_dec->fmt_in.i_extra);
234 *(p_extra++) = kp.nbytes >> 8;
235 *(p_extra++) = kp.nbytes & 0xFF;
237 memcpy( p_extra, kp.data, kp.nbytes );
238 p_dec->fmt_in.i_extra += kp.nbytes + 2;
240 block_Release( *pp_block );
241 p_sys->i_num_headers = ((unsigned char*)p_dec->fmt_in.p_extra)[0];
246 if( p_sys->i_headers == p_sys->i_num_headers && p_sys->i_num_headers>0 )
248 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
250 p_sys->i_headers = 0;
251 p_dec->fmt_in.i_extra = 0;
252 block_Release( *pp_block );
255 else p_sys->i_headers++;
258 return ProcessPacket( p_dec, &kp, pp_block );
261 /*****************************************************************************
262 * ProcessHeaders: process Kate headers.
263 *****************************************************************************/
264 static int ProcessHeaders( decoder_t *p_dec )
266 decoder_sys_t *p_sys = p_dec->p_sys;
273 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
275 p_extra = p_dec->fmt_in.p_extra;
276 i_extra = p_dec->fmt_in.i_extra;
278 /* skip number of headers */
282 /* Take care of the initial Kate header */
283 kp.nbytes = *(p_extra++) << 8;
284 kp.nbytes |= (*(p_extra++) & 0xFF);
286 p_extra += kp.nbytes;
287 i_extra -= (kp.nbytes + 2);
290 msg_Err( p_dec, "header data corrupted");
294 ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
297 msg_Err( p_dec, "this bitstream does not contain Kate data (%d)", ret );
301 msg_Dbg( p_dec, "%s %s text, granule rate %f, granule shift %d",
302 p_sys->ki.language, p_sys->ki.category,
303 (double)p_sys->ki.gps_numerator/p_sys->ki.gps_denominator,
304 p_sys->ki.granule_shift);
306 /* we want markup to be removed for now */
307 kate_info_remove_markup( &p_sys->ki, 1 );
309 /* parse all remaining header packets */
310 for (headeridx=1; headeridx<p_sys->ki.num_headers; ++headeridx)
312 kp.nbytes = *(p_extra++) << 8;
313 kp.nbytes |= (*(p_extra++) & 0xFF);
315 p_extra += kp.nbytes;
316 i_extra -= (kp.nbytes + 2);
319 msg_Err( p_dec, "header %d data corrupted", headeridx);
323 ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
326 msg_Err( p_dec, "Kate header %d is corrupted: %d", headeridx, ret);
330 /* header 1 is comments */
333 ParseKateComments( p_dec );
337 #ifdef ENABLE_PACKETIZER
338 if( !p_sys->b_packetizer )
341 /* We have all the headers, initialize decoder */
342 msg_Dbg( p_dec, "we have all headers, initialize libkate for decoding" );
343 ret = kate_decode_init( &p_sys->k, &p_sys->ki );
346 msg_Err( p_dec, "Kate failed to initialize for decoding: %d", ret );
349 p_sys->b_ready = true;
351 #ifdef ENABLE_PACKETIZER
354 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
355 p_dec->fmt_out.p_extra =
356 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
357 memcpy( p_dec->fmt_out.p_extra,
358 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
365 /*****************************************************************************
366 * ProcessPacket: processes a kate packet.
367 *****************************************************************************/
368 static subpicture_t *ProcessPacket( decoder_t *p_dec, kate_packet *p_kp,
371 decoder_sys_t *p_sys = p_dec->p_sys;
372 block_t *p_block = *pp_block;
373 subpicture_t *p_buf = NULL;
375 /* Date management */
376 if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
378 p_sys->i_pts = p_block->i_pts;
381 *pp_block = NULL; /* To avoid being fed the same packet again */
383 #ifdef ENABLE_PACKETIZER
384 if( p_sys->b_packetizer )
386 /* Date management */
387 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
389 if( p_sys->i_headers >= p_sys->i_num_headers )
390 p_block->i_length = p_sys->i_pts - p_block->i_pts;
392 p_block->i_length = 0;
399 if( p_sys->i_headers >= p_sys->i_num_headers )
400 p_buf = DecodePacket( p_dec, p_kp, p_block );
404 if( p_block ) block_Release( p_block );
410 #ifdef ENABLE_BITMAPS
411 /* nicked off blend.c */
412 static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
413 int r, int g, int b )
415 *y = ( ( ( 66 * r + 129 * g + 25 * b + 128 ) >> 8 ) + 16 );
416 *u = ( ( -38 * r - 74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
417 *v = ( ( 112 * r - 94 * g - 18 * b + 128 ) >> 8 ) + 128 ;
421 /*****************************************************************************
422 * DecodePacket: decodes a Kate packet.
423 *****************************************************************************/
424 static subpicture_t *DecodePacket( decoder_t *p_dec, kate_packet *p_kp, block_t *p_block )
426 decoder_sys_t *p_sys = p_dec->p_sys;
427 const kate_event *ev=NULL;
428 subpicture_t *p_spu = NULL;
429 subpicture_region_t *p_bitmap_region = NULL;
431 #ifdef ENABLE_BITMAPS
433 picture_t *pic = NULL;
437 bool tracker_valid = false;
439 ret = kate_decode_packetin( &p_sys->k, p_kp );
442 msg_Err( p_dec, "Kate failed to decode packet: %d", ret );
446 ret = kate_decode_eventout( &p_sys->k, &ev );
449 msg_Err( p_dec, "Kate failed to retrieve event: %d", ret );
454 /* no event to go with this packet, this is normal */
458 /* we have an event */
461 p_spu = p_dec->pf_spu_buffer_new( p_dec );
464 msg_Err( p_dec, "Failed to allocate spu buffer" );
468 p_spu->b_pausable = true;
470 #ifdef ENABLE_BITMAPS
471 if (ev->bitmap && ev->bitmap->type==kate_bitmap_type_paletted && ev->palette) {
472 /* create a separate region for the bitmap */
473 memset( &fmt, 0, sizeof(video_format_t) );
474 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
476 fmt.i_width = fmt.i_visible_width = ev->bitmap->width;
477 fmt.i_height = fmt.i_visible_height = ev->bitmap->height;
478 fmt.i_x_offset = fmt.i_y_offset = 0;
480 p_bitmap_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
481 if( !p_bitmap_region )
483 msg_Err( p_dec, "cannot allocate SPU region" );
484 p_dec->pf_spu_buffer_del( p_dec, p_spu );
488 /* create the palette */
489 fmt.p_palette->i_entries = ev->palette->ncolors;
490 for (n=0; n<ev->palette->ncolors; ++n)
493 &fmt.p_palette->palette[n][0], &fmt.p_palette->palette[n][1], &fmt.p_palette->palette[n][2],
494 ev->palette->colors[n].r, ev->palette->colors[n].g, ev->palette->colors[n].b
496 fmt.p_palette->palette[n][3] = ev->palette->colors[n].a;
499 /* create the bitmap */
500 pic = &p_bitmap_region->picture;
501 for (y=0; y<ev->bitmap->height; ++y) {
502 uint8_t *dest=pic->Y_PIXELS+pic->Y_PITCH*y;
503 const uint8_t *src=ev->bitmap->pixels+y*ev->bitmap->width;
504 memcpy(dest, src, ev->bitmap->width);
507 msg_Dbg(p_dec, "Created bitmap, %dx%d, %d colors\n", ev->bitmap->width, ev->bitmap->height, ev->palette->ncolors);
511 /* Create a new subpicture region */
512 memset( &fmt, 0, sizeof(video_format_t) );
513 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
515 fmt.i_width = fmt.i_height = 0;
516 fmt.i_x_offset = fmt.i_y_offset = 0;
518 #ifdef ENABLE_FORMATTING
519 if (p_sys->b_formatted)
521 ret = kate_tracker_init( &kin, &p_sys->ki, ev);
524 msg_Err( p_dec, "failed to initialize kate tracker, event will be unformatted: %d", ret );
528 // TODO: get window/video sizes/pos - can't find where to get those !
531 ret = kate_tracker_update(&kin, 0, w, h, 0, 0, w, h);
534 kate_tracker_clear(&kin);
535 msg_Err( p_dec, "failed to update kate tracker, event will be unformatted: %d", ret );
541 fmt.i_width = kin.region_w;
542 fmt.i_height = kin.region_h;
545 // TODO: parse tracker and set style, init fmt
546 tracker_valid = true;
554 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
555 if( !p_spu->p_region )
557 msg_Err( p_dec, "cannot allocate SPU region" );
558 p_dec->pf_spu_buffer_del( p_dec, p_spu );
562 p_spu->p_region->psz_text = strdup(ev->text); /* no leak, this actually gets killed by the core */
563 p_spu->i_start = p_block->i_pts;
564 p_spu->i_stop = p_block->i_pts + INT64_C(1000000)*ev->duration*p_sys->ki.gps_denominator/p_sys->ki.gps_numerator;
565 p_spu->b_ephemer = (p_block->i_length == 0);
566 p_spu->b_absolute = false;
573 p_spu->p_region->i_x = kin.region_x;
574 p_spu->p_region->i_y = kin.region_y;
575 if (p_bitmap_region) {
576 p_bitmap_region->i_x = kin.region_x;
577 p_bitmap_region->i_y = kin.region_y;
581 kate_tracker_clear(&kin);
585 /* Normal text subs, easy markup */
586 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
587 if (p_bitmap_region) {
588 p_bitmap_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
594 #ifdef ENABLE_BITMAPS
595 /* if we have a bitmap, chain it before the text */
598 p_bitmap_region->p_next = p_spu->p_region;
599 p_spu->p_region = p_bitmap_region;
606 /*****************************************************************************
607 * ParseKateComments: FIXME should be done in demuxer
608 *****************************************************************************/
609 static void ParseKateComments( decoder_t *p_dec )
611 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
612 char *psz_name, *psz_value, *psz_comment;
615 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
617 while ( i < p_dec->p_sys->kc.comments )
619 psz_comment = strdup( p_dec->p_sys->kc.user_comments[i] );
622 msg_Warn( p_dec, "out of memory" );
625 psz_name = psz_comment;
626 psz_value = strchr( psz_comment, '=' );
631 input_Control( p_input, INPUT_ADD_INFO, _("Kate comment"),
632 psz_name, "%s", psz_value );
639 /*****************************************************************************
640 * CloseDecoder: clean up the decoder
641 *****************************************************************************/
642 static void CloseDecoder( vlc_object_t *p_this )
644 decoder_t *p_dec = (decoder_t *)p_this;
645 decoder_sys_t *p_sys = p_dec->p_sys;
648 kate_clear( &p_sys->k );
649 kate_info_clear( &p_sys->ki );
650 kate_comment_clear( &p_sys->kc );