]> git.sesse.net Git - vlc/blob - modules/codec/kate.c
use C99 %zu for size_t in printf
[vlc] / modules / codec / kate.c
1 /*****************************************************************************
2  * kate.c : a decoder for the kate bitstream format
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_input.h>
34 #include <vlc_codec.h>
35
36 #include <kate/kate.h>
37
38 #include "vlc_osd.h"
39
40 /* #define ENABLE_PACKETIZER */
41 /* #define ENABLE_FORMATTING */
42 #define ENABLE_BITMAPS
43
44 /*****************************************************************************
45  * decoder_sys_t : decoder descriptor
46  *****************************************************************************/
47 struct decoder_sys_t
48 {
49 #ifdef ENABLE_PACKETIZER
50     /* Module mode */
51     bool b_packetizer;
52 #endif
53
54     /*
55      * Input properties
56      */
57     int i_num_headers;
58     int i_headers;
59
60     /*
61      * Kate properties
62      */
63     bool           b_ready;
64     kate_info      ki;
65     kate_comment   kc;
66     kate_state     k;
67
68     /*
69      * Common properties
70      */
71     mtime_t i_pts;
72
73     /*
74      * Options
75      */
76 #ifdef ENABLE_FORMATTING
77     bool   b_formatted;
78 #endif
79 };
80
81 /*****************************************************************************
82  * Local prototypes
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 );
88 #endif
89
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,
93                             block_t **pp_block );
94 static subpicture_t *DecodePacket( decoder_t *p_dec, kate_packet *p_kp,
95                             block_t *p_block );
96 static void ParseKateComments( decoder_t * );
97
98 #define DEFAULT_NAME "Default"
99 #define MAX_LINE 8192
100
101 /*****************************************************************************
102  * Module descriptor.
103  *****************************************************************************/
104
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.")
109 #endif
110
111
112 vlc_module_begin();
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" );
120
121 #ifdef ENABLE_PACKETIZER
122     add_submodule();
123     set_description( N_("Kate text subtitles packetizer") );
124     set_capability( "packetizer", 100 );
125     set_callbacks( OpenPacketizer, CloseDecoder );
126     add_shortcut( "kate" );
127 #endif
128
129 #ifdef ENABLE_FORMATTING
130     add_bool( "kate-formatted", true, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
131                  false );
132 #endif
133 vlc_module_end();
134
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
139  * to chose.
140  *****************************************************************************/
141 static int OpenDecoder( vlc_object_t *p_this )
142 {
143     decoder_t     *p_dec = (decoder_t*)p_this;
144     decoder_sys_t *p_sys;
145
146     msg_Dbg( p_dec, "kate: OpenDecoder");
147
148     if( p_dec->fmt_in.i_codec != VLC_FOURCC('k','a','t','e') )
149     {
150         return VLC_EGENERIC;
151     }
152
153     /* Set callbacks */
154     p_dec->pf_decode_sub = (subpicture_t *(*)(decoder_t *, block_t **))
155         DecodeBlock;
156     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
157         DecodeBlock;
158
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 )
162         return VLC_ENOMEM;
163
164     /* init of p_sys */
165 #ifdef ENABLE_PACKETIZER
166     p_sys->b_packetizer = false;
167 #endif
168     p_sys->b_ready = false;
169     p_sys->i_pts = 0;
170
171     kate_comment_init( &p_sys->kc );
172     kate_info_init( &p_sys->ki );
173
174     p_sys->i_num_headers = 0;
175     p_sys->i_headers = 0;
176
177     /* retrieve options */
178 #ifdef ENABLE_FORMATTING
179     p_sys->b_formatted = var_CreateGetBool( p_dec, "kate-formatted" );
180 #endif
181
182     return VLC_SUCCESS;
183 }
184
185 #ifdef ENABLE_PACKETIZER
186 static int OpenPacketizer( vlc_object_t *p_this )
187 {
188     decoder_t *p_dec = (decoder_t*)p_this;
189
190     int i_ret = OpenDecoder( p_this );
191
192     if( i_ret == VLC_SUCCESS )
193     {
194         p_dec->p_sys->b_packetizer = true;
195         p_dec->fmt_out.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
196     }
197
198     return i_ret;
199 }
200 #endif
201
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 )
208 {
209     decoder_sys_t *p_sys = p_dec->p_sys;
210     block_t *p_block;
211     kate_packet kp;
212
213     if( !pp_block || !*pp_block ) return NULL;
214
215     p_block = *pp_block;
216
217     /* Block to Kate packet */
218     kate_packet_wrap(&kp, p_block->i_buffer, p_block->p_buffer);
219
220     if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
221     {
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;
225     }
226     else if( kp.nbytes && (p_sys->i_headers==0 || p_sys->i_headers < p_sys->ki.num_headers ))
227     {
228         /* Backup headers as extra data */
229         uint8_t *p_extra;
230
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;
236
237         memcpy( p_extra, kp.data, kp.nbytes );
238         p_dec->fmt_in.i_extra += kp.nbytes + 2;
239
240         block_Release( *pp_block );
241         p_sys->i_num_headers = ((unsigned char*)p_dec->fmt_in.p_extra)[0];
242         p_sys->i_headers++;
243         return NULL;
244     }
245
246     if( p_sys->i_headers == p_sys->i_num_headers && p_sys->i_num_headers>0 )
247     {
248         if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
249         {
250             p_sys->i_headers = 0;
251             p_dec->fmt_in.i_extra = 0;
252             block_Release( *pp_block );
253             return NULL;
254         }
255         else p_sys->i_headers++;
256     }
257
258     return ProcessPacket( p_dec, &kp, pp_block );
259 }
260
261 /*****************************************************************************
262  * ProcessHeaders: process Kate headers.
263  *****************************************************************************/
264 static int ProcessHeaders( decoder_t *p_dec )
265 {
266     decoder_sys_t *p_sys = p_dec->p_sys;
267     kate_packet kp;
268     uint8_t *p_extra;
269     int i_extra;
270     int headeridx;
271     int ret;
272
273     if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
274
275     p_extra = p_dec->fmt_in.p_extra;
276     i_extra = p_dec->fmt_in.i_extra;
277
278     /* skip number of headers */
279     ++p_extra;
280     --i_extra;
281
282     /* Take care of the initial Kate header */
283     kp.nbytes = *(p_extra++) << 8;
284     kp.nbytes |= (*(p_extra++) & 0xFF);
285     kp.data = p_extra;
286     p_extra += kp.nbytes;
287     i_extra -= (kp.nbytes + 2);
288     if( i_extra < 0 )
289     {
290         msg_Err( p_dec, "header data corrupted");
291         return VLC_EGENERIC;
292     }
293
294     ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
295     if( ret < 0 )
296     {
297         msg_Err( p_dec, "this bitstream does not contain Kate data (%d)", ret );
298         return VLC_EGENERIC;
299     }
300
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);
305
306     /* we want markup to be removed for now */
307     kate_info_remove_markup( &p_sys->ki, 1 );
308
309     /* parse all remaining header packets */
310     for (headeridx=1; headeridx<p_sys->ki.num_headers; ++headeridx)
311     {
312         kp.nbytes = *(p_extra++) << 8;
313         kp.nbytes |= (*(p_extra++) & 0xFF);
314         kp.data = p_extra;
315         p_extra += kp.nbytes;
316         i_extra -= (kp.nbytes + 2);
317         if( i_extra < 0 )
318         {
319             msg_Err( p_dec, "header %d data corrupted", headeridx);
320             return VLC_EGENERIC;
321         }
322
323         ret = kate_decode_headerin( &p_sys->ki, &p_sys->kc, &kp );
324         if( ret < 0 )
325         {
326             msg_Err( p_dec, "Kate header %d is corrupted: %d", headeridx, ret);
327             return VLC_EGENERIC;
328         }
329
330         /* header 1 is comments */
331         if( headeridx == 1 )
332         {
333             ParseKateComments( p_dec );
334         }
335     }
336
337 #ifdef ENABLE_PACKETIZER
338     if( !p_sys->b_packetizer )
339 #endif
340     {
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 );
344         if (ret < 0)
345         {
346             msg_Err( p_dec, "Kate failed to initialize for decoding: %d", ret );
347             return VLC_EGENERIC;
348         }
349         p_sys->b_ready = true;
350     }
351 #ifdef ENABLE_PACKETIZER
352     else
353     {
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 );
359     }
360 #endif
361
362     return VLC_SUCCESS;
363 }
364
365 /*****************************************************************************
366  * ProcessPacket: processes a kate packet.
367  *****************************************************************************/
368 static subpicture_t *ProcessPacket( decoder_t *p_dec, kate_packet *p_kp,
369                             block_t **pp_block )
370 {
371     decoder_sys_t *p_sys = p_dec->p_sys;
372     block_t *p_block = *pp_block;
373     subpicture_t *p_buf = NULL;
374
375     /* Date management */
376     if( p_block->i_pts > 0 && p_block->i_pts != p_sys->i_pts )
377     {
378         p_sys->i_pts = p_block->i_pts;
379     }
380
381     *pp_block = NULL; /* To avoid being fed the same packet again */
382
383 #ifdef ENABLE_PACKETIZER
384     if( p_sys->b_packetizer )
385     {
386         /* Date management */
387         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
388
389         if( p_sys->i_headers >= p_sys->i_num_headers )
390             p_block->i_length = p_sys->i_pts - p_block->i_pts;
391         else
392             p_block->i_length = 0;
393
394         p_buf = p_block;
395     }
396     else
397 #endif
398     {
399         if( p_sys->i_headers >= p_sys->i_num_headers )
400             p_buf = DecodePacket( p_dec, p_kp, p_block );
401         else
402             p_buf = NULL;
403
404         if( p_block ) block_Release( p_block );
405     }
406
407     return p_buf;
408 }
409
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 )
414 {
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 ;
418 }
419 #endif
420
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 )
425 {
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;
430     int ret;
431 #ifdef ENABLE_BITMAPS
432     size_t n, y;
433     picture_t *pic = NULL;
434 #endif
435     video_format_t fmt;
436     kate_tracker kin;
437     bool tracker_valid = false;
438
439     ret = kate_decode_packetin( &p_sys->k, p_kp );
440     if( ret < 0 )
441     {
442         msg_Err( p_dec, "Kate failed to decode packet: %d", ret );
443         return NULL;
444     }
445
446     ret = kate_decode_eventout( &p_sys->k, &ev );
447     if( ret < 0 )
448     {
449         msg_Err( p_dec, "Kate failed to retrieve event: %d", ret );
450         return NULL;
451     }
452     if( ret > 0 )
453     {
454         /* no event to go with this packet, this is normal */
455         return NULL;
456     }
457
458     /* we have an event */
459
460     /* Get a new spu */
461     p_spu = p_dec->pf_spu_buffer_new( p_dec );
462     if( !p_spu )
463     {
464         msg_Err( p_dec, "Failed to allocate spu buffer" );
465         return NULL;
466     }
467
468     p_spu->b_pausable = true;
469
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');
475         fmt.i_aspect = 0;
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;
479
480         p_bitmap_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
481         if( !p_bitmap_region )
482         {
483             msg_Err( p_dec, "cannot allocate SPU region" );
484             p_dec->pf_spu_buffer_del( p_dec, p_spu );
485             return NULL;
486         }
487
488         /* create the palette */
489         fmt.p_palette->i_entries = ev->palette->ncolors;
490         for (n=0; n<ev->palette->ncolors; ++n)
491         {
492             rgb_to_yuv(
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
495             );
496             fmt.p_palette->palette[n][3] = ev->palette->colors[n].a;
497         }
498
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);
505         }
506
507         msg_Dbg(p_dec, "Created bitmap, %zux%zu, %zu colors\n", ev->bitmap->width, ev->bitmap->height, ev->palette->ncolors);
508     }
509 #endif
510
511     /* Create a new subpicture region */
512     memset( &fmt, 0, sizeof(video_format_t) );
513     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
514     fmt.i_aspect = 0;
515     fmt.i_width = fmt.i_height = 0;
516     fmt.i_x_offset = fmt.i_y_offset = 0;
517
518 #ifdef ENABLE_FORMATTING
519     if (p_sys->b_formatted)
520     {
521         ret = kate_tracker_init( &kin, &p_sys->ki, ev);
522         if( ret < 0)
523         {
524             msg_Err( p_dec, "failed to initialize kate tracker, event will be unformatted: %d", ret );
525         }
526         else
527         {
528             // TODO: get window/video sizes/pos - can't find where to get those !
529             int w = 640;
530             int h = 480;
531             ret = kate_tracker_update(&kin, 0, w, h, 0, 0, w, h);
532             if( ret < 0)
533             {
534                 kate_tracker_clear(&kin);
535                 msg_Err( p_dec, "failed to update kate tracker, event will be unformatted: %d", ret );
536             }
537             else
538             {
539                 if (kin.has.region)
540                 {
541                     fmt.i_width = kin.region_w;
542                     fmt.i_height = kin.region_h;
543                 }
544
545                 // TODO: parse tracker and set style, init fmt
546                 tracker_valid = true;
547             }
548         }
549     }
550 #endif
551
552
553
554     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
555     if( !p_spu->p_region )
556     {
557         msg_Err( p_dec, "cannot allocate SPU region" );
558         p_dec->pf_spu_buffer_del( p_dec, p_spu );
559         return NULL;
560     }
561
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;
567
568     if (tracker_valid)
569     {
570         p_spu->i_flags = 0;
571         if (kin.has.region)
572         {
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;
578             }
579         }
580
581         kate_tracker_clear(&kin);
582     }
583     else
584     {
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;
589         }
590         p_spu->i_x = 0;
591         p_spu->i_y = 10;
592     }
593
594 #ifdef ENABLE_BITMAPS
595     /* if we have a bitmap, chain it before the text */
596     if (p_bitmap_region)
597     {
598         p_bitmap_region->p_next = p_spu->p_region;
599         p_spu->p_region = p_bitmap_region;
600     }
601 #endif
602
603     return p_spu;
604 }
605
606 /*****************************************************************************
607  * ParseKateComments: FIXME should be done in demuxer
608  *****************************************************************************/
609 static void ParseKateComments( decoder_t *p_dec )
610 {
611     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
612     char *psz_name, *psz_value, *psz_comment;
613     int i = 0;
614
615     if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
616
617     while ( i < p_dec->p_sys->kc.comments )
618     {
619         psz_comment = strdup( p_dec->p_sys->kc.user_comments[i] );
620         if( !psz_comment )
621         {
622             msg_Warn( p_dec, "out of memory" );
623             break;
624         }
625         psz_name = psz_comment;
626         psz_value = strchr( psz_comment, '=' );
627         if( psz_value )
628         {
629             *psz_value = '\0';
630             psz_value++;
631             input_Control( p_input, INPUT_ADD_INFO, _("Kate comment"),
632                            psz_name, "%s", psz_value );
633         }
634         free( psz_comment );
635         i++;
636     }
637 }
638
639 /*****************************************************************************
640  * CloseDecoder: clean up the decoder
641  *****************************************************************************/
642 static void CloseDecoder( vlc_object_t *p_this )
643 {
644     decoder_t *p_dec = (decoder_t *)p_this;
645     decoder_sys_t *p_sys = p_dec->p_sys;
646
647     if (p_sys->b_ready)
648         kate_clear( &p_sys->k );
649     kate_info_clear( &p_sys->ki );
650     kate_comment_clear( &p_sys->kc );
651
652     free( p_sys );
653 }
654