07f49a9958cb13cabf910f475d9bd2d73fbf9c5f
[vlc.git] / modules / codec / spudec / text.c
1 /*****************************************************************************
2  * text.c: text subtitles parser
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: text.c,v 1.3 2002/12/06 16:34:05 sam Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                    /* memcpy(), memset() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/decoder.h>
33
34 #include "spudec.h"
35
36 /*****************************************************************************
37  * Local prototypes.
38  *****************************************************************************/
39
40 /*****************************************************************************
41  * ParseText: parse an text subtitle packet and send it to the video output
42  *****************************************************************************/
43 void E_(ParseText)( spudec_thread_t *p_spudec, subtitler_font_t *p_font )
44 {
45     char         * psz_subtitle;
46     mtime_t        i_pts, i_dts;
47
48     /* We cannot display a subpicture with no date */
49     i_pts = p_spudec->bit_stream.p_pes->i_pts;
50     i_dts = p_spudec->bit_stream.p_pes->i_dts;
51     if( i_pts == 0 )
52     {
53         /* Dump the packet */
54         NextDataPacket( p_spudec->p_fifo, &p_spudec->bit_stream );
55         msg_Warn( p_spudec->p_fifo, "subtitle without a date" );
56         return;
57     }
58     else
59
60     /* Check validity of packet data */
61     if( (p_spudec->bit_stream.p_data->p_payload_end
62           - p_spudec->bit_stream.p_data->p_payload_start) <= 0
63         || (strlen(p_spudec->bit_stream.p_data->p_payload_start)
64             > (size_t)(p_spudec->bit_stream.p_data->p_payload_end
65                         - p_spudec->bit_stream.p_data->p_payload_start)) )
66     {
67         /* Dump the packet */
68         NextDataPacket( p_spudec->p_fifo, &p_spudec->bit_stream );
69         msg_Warn( p_spudec->p_fifo, "invalid subtitle" );
70         return;
71     }
72     psz_subtitle = p_spudec->bit_stream.p_data->p_payload_start;
73
74     if( psz_subtitle[0] != '\0' )
75     {
76         subtitler_PlotSubtitle( p_spudec->p_vout,
77                                 psz_subtitle, p_font,
78                                 i_pts,
79                                 i_dts );
80     }
81
82     /* Prepare for next time. No need to check that
83      * p_spudec->bit_stream->p_data is valid since we check later on
84      * for b_die and b_error */
85     NextDataPacket( p_spudec->p_fifo, &p_spudec->bit_stream );
86 }