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