Explicit casts to proper integer types when formatting
[vlc.git] / modules / demux / vobsub.c
1 /*****************************************************************************
2  * subtitle.c: Demux vobsub files.
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #include <vlc_demux.h>
34 #include <vlc_charset.h>
35
36 #include "ps.h"
37
38 #define MAX_LINE 8192
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t *p_this );
44 static void Close( vlc_object_t *p_this );
45
46 vlc_module_begin();
47     set_description( _("Vobsub subtitles parser") );
48     set_category( CAT_INPUT );
49     set_subcategory( SUBCAT_INPUT_DEMUX );
50     set_capability( "demux2", 1 );
51
52     set_callbacks( Open, Close );
53
54     add_shortcut( "vobsub" );
55     add_shortcut( "subtitle" );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Prototypes:
60  *****************************************************************************/
61
62 typedef struct
63 {
64     int     i_line_count;
65     int     i_line;
66     char    **line;
67 } text_t;
68 static int  TextLoad( text_t *, stream_t *s );
69 static void TextUnload( text_t * );
70
71 typedef struct
72 {
73     int64_t i_start;
74     int     i_vobsub_location;
75 } subtitle_t;
76
77 typedef struct
78 {
79     es_format_t fmt;
80     es_out_id_t *p_es;
81     int         i_track_id;
82
83     int         i_current_subtitle;
84     int         i_subtitles;
85     subtitle_t  *p_subtitles;
86
87     int64_t     i_delay;
88 } vobsub_track_t;
89
90 struct demux_sys_t
91 {
92     int64_t     i_next_demux_date;
93     int64_t     i_length;
94
95     text_t      txt;
96     stream_t    *p_vobsub_stream;
97
98     /* all tracks */
99     int            i_tracks;
100     vobsub_track_t *track;
101
102     int         i_original_frame_width;
103     int         i_original_frame_height;
104     vlc_bool_t  b_palette;
105     uint32_t    palette[16];
106 };
107
108 static int Demux( demux_t * );
109 static int Control( demux_t *, int, va_list );
110
111 static int ParseVobSubIDX( demux_t * );
112 static int DemuxVobSub( demux_t *, block_t *);
113
114 /*****************************************************************************
115  * Module initializer
116  *****************************************************************************/
117 static int Open ( vlc_object_t *p_this )
118 {
119     demux_t     *p_demux = (demux_t*)p_this;
120     demux_sys_t *p_sys;
121     char *psz_vobname, *s;
122     int i_len;
123
124     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
125     {
126         if( !strcasestr( s, "# VobSub index file" ) )
127         {
128             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
129             free( s );
130             if( stream_Seek( p_demux->s, 0 ) )
131             {
132                 msg_Warn( p_demux, "failed to rewind" );
133             }
134             return VLC_EGENERIC;
135         }
136         free( s );
137
138     }
139     else
140     {
141         msg_Dbg( p_demux, "could not read vobsub IDX file" );
142         return VLC_EGENERIC;
143     }
144
145     p_demux->pf_demux = Demux;
146     p_demux->pf_control = Control;
147     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
148     p_sys->i_length = 0;
149     p_sys->p_vobsub_stream = NULL;
150     p_sys->i_tracks = 0;
151     p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
152     p_sys->i_original_frame_width = -1;
153     p_sys->i_original_frame_height = -1;
154     p_sys->b_palette = VLC_FALSE;
155     memset( p_sys->palette, 0, 16 * sizeof( uint32_t ) );
156
157     /* Load the whole file */
158     TextLoad( &p_sys->txt, p_demux->s );
159
160     /* Parse it */
161     ParseVobSubIDX( p_demux );
162
163     /* Unload */
164     TextUnload( &p_sys->txt );
165
166     /* Find the total length of the vobsubs */
167     if( p_sys->i_tracks > 0 )
168     {
169         int i;
170         for( i = 0; i < p_sys->i_tracks; i++ )
171         {
172             if( p_sys->track[i].i_subtitles > 1 )
173             {
174                 if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
175                     p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
176             }
177         }
178     }
179
180     asprintf( &psz_vobname, "%s://%s", p_demux->psz_access, p_demux->psz_path );
181     i_len = strlen( psz_vobname );
182     if( i_len >= 4 ) memcpy( psz_vobname + i_len - 4, ".sub", 4 );
183
184     /* open file */
185     p_sys->p_vobsub_stream = stream_UrlNew( p_demux, psz_vobname );
186     if( p_sys->p_vobsub_stream == NULL )
187     {
188         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
189                  psz_vobname );
190         free( psz_vobname );
191         free( p_sys );
192         return VLC_EGENERIC;
193     }
194     free( psz_vobname );
195
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * Close: Close subtitle demux
201  *****************************************************************************/
202 static void Close( vlc_object_t *p_this )
203 {
204     int i;
205     demux_t *p_demux = (demux_t*)p_this;
206     demux_sys_t *p_sys = p_demux->p_sys;
207
208     /* Clean all subs from all tracks */
209     for( i = 0; i < p_sys->i_tracks; i++ )
210     {
211         if( p_sys->track[i].p_subtitles ) free( p_sys->track[i].p_subtitles );
212     }
213     if( p_sys->track ) free( p_sys->track );
214
215     if( p_sys->p_vobsub_stream )
216         stream_Delete( p_sys->p_vobsub_stream );
217
218     free( p_sys );
219 }
220
221 /*****************************************************************************
222  * Control:
223  *****************************************************************************/
224 static int Control( demux_t *p_demux, int i_query, va_list args )
225 {
226     demux_sys_t *p_sys = p_demux->p_sys;
227     int64_t *pi64, i64;
228     int i;
229     double *pf, f;
230
231     switch( i_query )
232     {
233         case DEMUX_GET_LENGTH:
234             pi64 = (int64_t*)va_arg( args, int64_t * );
235             *pi64 = (int64_t) p_sys->i_length;
236             return VLC_SUCCESS;
237
238         case DEMUX_GET_TIME:
239             pi64 = (int64_t*)va_arg( args, int64_t * );
240             for( i = 0; i < p_sys->i_tracks; i++ )
241             {
242                 vlc_bool_t b_selected;
243                 /* Check the ES is selected */
244                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
245                                 p_sys->track[i].p_es, &b_selected );
246                 if( b_selected ) break;
247             }
248             if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
249             {
250                 *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
251                 return VLC_SUCCESS;
252             }
253             return VLC_EGENERIC;
254
255         case DEMUX_SET_TIME:
256             i64 = (int64_t)va_arg( args, int64_t );
257             for( i = 0; i < p_sys->i_tracks; i++ )
258             {
259                 p_sys->track[i].i_current_subtitle = 0;
260                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
261                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
262                 {
263                     p_sys->track[i].i_current_subtitle++;
264                 }
265
266                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
267                     return VLC_EGENERIC;
268             }
269             return VLC_SUCCESS;
270
271         case DEMUX_GET_POSITION:
272             pf = (double*)va_arg( args, double * );
273             for( i = 0; i < p_sys->i_tracks; i++ )
274             {
275                 vlc_bool_t b_selected;
276                 /* Check the ES is selected */
277                 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
278                                 p_sys->track[i].p_es, &b_selected );
279                 if( b_selected ) break;
280             }
281             if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
282             {
283                 *pf = 1.0;
284             }
285             else if( p_sys->track[i].i_subtitles > 0 )
286             {
287                 *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
288                       (double)p_sys->i_length;
289             }
290             else
291             {
292                 *pf = 0.0;
293             }
294             return VLC_SUCCESS;
295
296         case DEMUX_SET_POSITION:
297             f = (double)va_arg( args, double );
298             i64 = (int64_t) f * p_sys->i_length;
299
300             for( i = 0; i < p_sys->i_tracks; i++ )
301             {
302                 p_sys->track[i].i_current_subtitle = 0;
303                 while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
304                        p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
305                 {
306                     p_sys->track[i].i_current_subtitle++;
307                 }
308                 if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
309                     return VLC_EGENERIC;
310             }
311             return VLC_SUCCESS;
312
313         case DEMUX_SET_NEXT_DEMUX_TIME:
314             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
315             return VLC_SUCCESS;
316
317         case DEMUX_GET_FPS:
318         case DEMUX_GET_META:
319         case DEMUX_GET_TITLE_INFO:
320             return VLC_EGENERIC;
321
322         default:
323             msg_Err( p_demux, "unknown query in subtitle control" );
324             return VLC_EGENERIC;
325     }
326 }
327
328 /*****************************************************************************
329  * Demux: Send subtitle to decoder
330  *****************************************************************************/
331 static int Demux( demux_t *p_demux )
332 {
333     demux_sys_t *p_sys = p_demux->p_sys;
334     int64_t i_maxdate;
335     int i;
336
337     for( i = 0; i < p_sys->i_tracks; i++ )
338     {
339 #define tk p_sys->track[i]
340         if( tk.i_current_subtitle >= tk.i_subtitles )
341             continue;
342
343         i_maxdate = (int64_t) p_sys->i_next_demux_date;
344         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
345         {
346             /* Should not happen */
347             i_maxdate = (int64_t) tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
348         }
349
350         while( tk.i_current_subtitle < tk.i_subtitles &&
351                tk.p_subtitles[tk.i_current_subtitle].i_start < i_maxdate )
352         {
353             int i_pos = tk.p_subtitles[tk.i_current_subtitle].i_vobsub_location;
354             block_t *p_block;
355             int i_size = 0;
356
357             /* first compute SPU size */
358             if( tk.i_current_subtitle + 1 < tk.i_subtitles )
359             {
360                 i_size = tk.p_subtitles[tk.i_current_subtitle+1].i_vobsub_location - i_pos;
361             }
362             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
363
364             /* Seek at the right place */
365             if( stream_Seek( p_sys->p_vobsub_stream, i_pos ) )
366             {
367                 msg_Warn( p_demux,
368                           "cannot seek in the VobSub to the correct time %d", i_pos );
369                 tk.i_current_subtitle++;
370                 continue;
371             }
372
373             /* allocate a packet */
374             if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
375             {
376                 tk.i_current_subtitle++;
377                 continue;
378             }
379
380             /* read data */
381             p_block->i_buffer = stream_Read( p_sys->p_vobsub_stream, p_block->p_buffer, i_size );
382             if( p_block->i_buffer <= 6 )
383             {
384                 block_Release( p_block );
385                 tk.i_current_subtitle++;
386                 continue;
387             }
388
389             /* pts */
390             p_block->i_pts = tk.p_subtitles[tk.i_current_subtitle].i_start;
391
392             /* demux this block */
393             DemuxVobSub( p_demux, p_block );
394
395             tk.i_current_subtitle++;
396         }
397 #undef tk
398     }
399
400     /* */
401     p_sys->i_next_demux_date = 0;
402
403     return 1;
404 }
405
406 static int TextLoad( text_t *txt, stream_t *s )
407 {
408     int   i_line_max;
409
410     /* init txt */
411     i_line_max          = 500;
412     txt->i_line_count   = 0;
413     txt->i_line         = 0;
414     txt->line           = calloc( i_line_max, sizeof( char * ) );
415
416     /* load the complete file */
417     for( ;; )
418     {
419         char *psz = stream_ReadLine( s );
420
421         if( psz == NULL )
422             break;
423
424         txt->line[txt->i_line_count++] = psz;
425         if( txt->i_line_count >= i_line_max )
426         {
427             i_line_max += 100;
428             txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
429         }
430     }
431
432     if( txt->i_line_count <= 0 )
433     {
434         if( txt->line ) free( txt->line );
435         return VLC_EGENERIC;
436     }
437
438     return VLC_SUCCESS;
439 }
440 static void TextUnload( text_t *txt )
441 {
442     int i;
443
444     for( i = 0; i < txt->i_line_count; i++ )
445     {
446         if( txt->line[i] ) free( txt->line[i] );
447     }
448     if( txt->line ) free( txt->line );
449     txt->i_line       = 0;
450     txt->i_line_count = 0;
451 }
452
453 static char *TextGetLine( text_t *txt )
454 {
455     if( txt->i_line >= txt->i_line_count )
456         return( NULL );
457
458     return txt->line[txt->i_line++];
459 }
460
461 static int ParseVobSubIDX( demux_t *p_demux )
462 {
463     demux_sys_t *p_sys = p_demux->p_sys;
464     text_t      *txt = &p_sys->txt;
465     char        *line;
466     vobsub_track_t *current_tk;
467
468     for( ;; )
469     {
470         if( ( line = TextGetLine( txt ) ) == NULL )
471         {
472             return( VLC_EGENERIC );
473         }
474
475         if( *line == 0 || *line == '\r' || *line == '\n' || *line == '#' )
476             continue;
477         else if( !strncmp( "size:", line, 5 ) )
478         {
479             /* Store the original size of the video */
480             if( sscanf( line, "size: %dx%d",
481                         &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
482             {
483                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
484             }
485             else
486             {
487                 msg_Warn( p_demux, "reading original frame size failed" );
488             }
489         }
490         else if( !strncmp( "palette:", line, 8 ) )
491         {
492             int i;
493
494             /* Store the palette of the subs */
495             if( sscanf( line, "palette: %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x",
496                         &p_sys->palette[0], &p_sys->palette[1], &p_sys->palette[2], &p_sys->palette[3],
497                         &p_sys->palette[4], &p_sys->palette[5], &p_sys->palette[6], &p_sys->palette[7],
498                         &p_sys->palette[8], &p_sys->palette[9], &p_sys->palette[10], &p_sys->palette[11],
499                         &p_sys->palette[12], &p_sys->palette[13], &p_sys->palette[14], &p_sys->palette[15] ) == 16 )
500             {
501                 for( i = 0; i < 16; i++ )
502                 {
503                     uint8_t r = 0, g = 0, b = 0;
504                     uint8_t y = 0, u = 0, v = 0;
505                     r = (p_sys->palette[i] >> 16) & 0xff;
506                     g = (p_sys->palette[i] >> 8) & 0xff;
507                     b = (p_sys->palette[i] >> 0) & 0xff;
508                     /* msg_Dbg( p_demux, "palette %d: R=%x, G=%x, B=%x", i, r, g, b ); */
509                     y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
510                     u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
511                     v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
512                     p_sys->palette[i] = 0;
513                     p_sys->palette[i] |= (y&0xff)<<16;
514                     p_sys->palette[i] |= (u&0xff);
515                     p_sys->palette[i] |= (v&0xff)<<8;
516                     /* msg_Dbg( p_demux, "palette %d: y=%x, u=%x, v=%x", i, y, u, v ); */
517
518                 }
519                 p_sys->b_palette = VLC_TRUE;
520                 msg_Dbg( p_demux, "vobsub palette read" );
521             }
522             else
523             {
524                 msg_Warn( p_demux, "reading original palette failed" );
525             }
526         }
527         else if( !strncmp( "id:", line, 3 ) )
528         {
529             char language[20];
530             int i_track_id;
531             es_format_t fmt;
532
533             /* Lets start a new track */
534             if( sscanf( line, "id: %2s, index: %d",
535                         language, &i_track_id ) == 2 )
536             {
537                 p_sys->i_tracks++;
538                 p_sys->track = (vobsub_track_t*)realloc( p_sys->track, sizeof( vobsub_track_t ) * (p_sys->i_tracks + 1 ) );
539
540                 /* Init the track */
541                 current_tk = &p_sys->track[p_sys->i_tracks - 1];
542                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
543                 current_tk->i_current_subtitle = 0;
544                 current_tk->i_subtitles = 0;
545                 current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
546                 current_tk->i_track_id = i_track_id;
547                 current_tk->i_delay = (int64_t)0;
548
549                 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
550                 fmt.subs.spu.i_original_frame_width = p_sys->i_original_frame_width;
551                 fmt.subs.spu.i_original_frame_height = p_sys->i_original_frame_height;
552                 fmt.psz_language = strdup( language );
553                 if( p_sys->b_palette )
554                 {
555                     fmt.subs.spu.palette[0] = 0xBeef;
556                     memcpy( &fmt.subs.spu.palette[1], p_sys->palette, 16 * sizeof( uint32_t ) );
557                 }
558
559                 current_tk->p_es = es_out_Add( p_demux->out, &fmt );
560                 msg_Dbg( p_demux, "new vobsub track detected" );
561             }
562             else
563             {
564                 msg_Warn( p_demux, "reading new track failed" );
565             }
566         }
567         else if( !strncmp( line, "timestamp:", 10 ) )
568         {
569             /*
570              * timestamp: [sign]hh:mm:ss:mss, filepos: loc
571              * loc is the hex location of the spu in the .sub file
572              */
573             int h, m, s, ms, count, loc = 0;
574             int i_sign = 1;
575             int64_t i_start, i_location = 0;
576
577             vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
578
579             if( sscanf( line, "timestamp: %d%n:%d:%d:%d, filepos: %x",
580                         &h, &count, &m, &s, &ms, &loc ) >= 5  )
581             {
582                 subtitle_t *current_sub;
583
584                 if( line[count-3] == '-' )
585                 {
586                     i_sign = -1;
587                     h = -h;
588                 }
589                 i_start = (int64_t) ( h * 3600*1000 +
590                             m * 60*1000 +
591                             s * 1000 +
592                             ms ) * 1000;
593                 i_location = loc;
594
595                 current_tk->i_subtitles++;
596                 current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
597                 current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
598
599                 current_sub->i_start = (int64_t) i_start * i_sign;
600                 current_sub->i_start += current_tk->i_delay;
601                 current_sub->i_vobsub_location = i_location;
602             }
603         }
604         else if( !strncasecmp( line, "delay:", 6 ) )
605         {
606             /*
607              * delay: [sign]hh:mm:ss:mss
608              */
609             int h, m, s, ms, count = 0;
610             int i_sign = 1;
611             int64_t i_gap = 0;
612
613             vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
614
615             if( sscanf( line, "%*celay: %d%n:%d:%d:%d",
616                         &h, &count, &m, &s, &ms ) >= 4 )
617             {
618                 if( line[count-3] == '-' )
619                 {
620                     i_sign = -1;
621                     h = -h;
622                 }
623                 i_gap = (int64_t) ( h * 3600*1000 +
624                             m * 60*1000 +
625                             s * 1000 +
626                             ms ) * 1000;
627
628                 current_tk->i_delay = current_tk->i_delay + (i_gap * i_sign);
629                 msg_Dbg( p_demux, "sign: %+d gap: %+lld global delay: %+lld",
630                          i_sign, (long long)i_gap,
631                          (long long)current_tk->i_delay  );
632             }
633         }
634     }
635     return( 0 );
636 }
637
638 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
639 {
640     demux_sys_t *p_sys = p_demux->p_sys;
641     uint8_t     *p = p_bk->p_buffer;
642     uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
643     int i;
644
645     while( p < p_end )
646     {
647         int i_size = ps_pkt_size( p, p_end - p );
648         block_t *p_pkt;
649         int      i_id;
650         int      i_spu;
651
652         if( i_size <= 0 )
653         {
654             break;
655         }
656         if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
657         {
658             msg_Warn( p_demux, "invalid PES" );
659             break;
660         }
661
662         if( p[3] != 0xbd )
663         {
664             /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
665             p += i_size;
666             continue;
667         }
668
669         /* Create a block */
670         p_pkt = block_New( p_demux, i_size );
671         memcpy( p_pkt->p_buffer, p, i_size);
672         p += i_size;
673
674         i_id = ps_pkt_id( p_pkt );
675         if( (i_id&0xffe0) != 0xbd20 ||
676             ps_pkt_parse_pes( p_pkt, 1 ) )
677         {
678             block_Release( p_pkt );
679             continue;
680         }
681         i_spu = i_id&0x1f;
682         /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
683
684         for( i = 0; i < p_sys->i_tracks; i++ )
685         {
686 #define tk p_sys->track[i]
687             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
688             p_pkt->i_length = 0;
689
690             if( tk.p_es && tk.i_track_id == i_spu )
691             {
692                 es_out_Send( p_demux->out, tk.p_es, p_pkt );
693                 p_bk->i_pts = 0;     /*only first packet has a pts */
694                 break;
695             }
696             else if( i == p_sys->i_tracks - 1 )
697             {
698                 block_Release( p_pkt );
699             }
700 #undef tk
701         }
702     }
703
704     return VLC_SUCCESS;
705 }