]> git.sesse.net Git - vlc/blob - modules/demux/cdg.c
Oops, fix subtitle decoding.
[vlc] / modules / demux / cdg.c
1 /*****************************************************************************
2  * cdg.c : cdg file demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 Laurent Aimar
5  * $Id: $
6  *
7  * Authors: Laurent Aimar <fenrir # via.ecp.fr>
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
28 #include <vlc/vlc.h>
29 #include <vlc_demux.h>
30
31 #include <vlc_codecs.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int  Open ( vlc_object_t * );
37 static void Close( vlc_object_t * );
38
39 vlc_module_begin();
40     set_description( _("CDG demuxer") );
41     set_category( CAT_INPUT );
42     set_subcategory( SUBCAT_INPUT_DEMUX );
43     set_capability( "demux2", 2 );
44     set_callbacks( Open, Close );
45     add_shortcut( "cdg" );
46     add_shortcut( "subtitle" );
47 vlc_module_end();
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int Demux  ( demux_t * );
53 static int Control( demux_t *, int i_query, va_list args );
54
55 struct demux_sys_t
56 {
57     es_format_t     fmt;
58     es_out_id_t     *p_es;
59
60     date_t          pts;
61 };
62
63 #define CDG_FRAME_SIZE (96)
64 #define CDG_FRAME_RATE (75)
65
66 /*****************************************************************************
67  * Open: check file and initializes structures
68  *****************************************************************************/
69 static int Open( vlc_object_t * p_this )
70 {
71     demux_t     *p_demux = (demux_t*)p_this;
72     demux_sys_t *p_sys;
73
74     /* Identify cdg file by extension, as there is no simple way to
75      * detect it */
76     if( !demux2_IsPathExtension( p_demux, ".cdg" ) && !demux2_IsForced( p_demux, "cdg" ) )
77         return VLC_EGENERIC;
78
79     /* CDG file size has to be multiple of CDG_FRAME_SIZE (it works even
80      * if size is unknown ie 0) */
81 //    if( (stream_Size( p_demux->s ) % CDG_FRAME_SIZE) != 0 )
82 //    {
83 //        msg_Err( p_demux, "Reject CDG file based on its size" );
84 //        return VLC_EGENERIC;
85 //    }
86
87     p_demux->pf_demux   = Demux;
88     p_demux->pf_control = Control;
89     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
90
91     /* */
92     es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_FOURCC('C','D','G', ' ' ) );
93     p_sys->fmt.video.i_width  = 300-2*6;
94     p_sys->fmt.video.i_height = 216-2*12 ;
95
96     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
97
98     /* There is CDG_FRAME_RATE frames per second */
99     date_Init( &p_sys->pts, CDG_FRAME_RATE, 1 );
100     date_Set( &p_sys->pts, 1 );
101
102     return VLC_SUCCESS;
103 }
104
105 /*****************************************************************************
106  * Demux: read packet and send them to decoders
107  *****************************************************************************
108  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
109  *****************************************************************************/
110 static int Demux( demux_t *p_demux )
111 {
112     demux_sys_t *p_sys = p_demux->p_sys;
113     block_t     *p_block;
114
115     p_block = stream_Block( p_demux->s, CDG_FRAME_SIZE );
116     if( p_block == NULL )
117     {
118         msg_Dbg( p_demux, "cannot read data, eof" );
119         return 0;
120     }
121
122     p_block->i_dts =
123     p_block->i_pts = date_Increment( &p_sys->pts, 1 );
124
125     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
126
127     es_out_Send( p_demux->out, p_sys->p_es, p_block );
128     return 1;
129 }
130
131 /*****************************************************************************
132  * Close: frees unused data
133  *****************************************************************************/
134 static void Close ( vlc_object_t * p_this )
135 {
136     demux_t *p_demux = (demux_t *)p_this;
137     demux_sys_t *p_sys = p_demux->p_sys;
138
139     free( p_sys );
140 }
141
142 /*****************************************************************************
143  * Control:
144  *****************************************************************************/
145 static int Control( demux_t *p_demux, int i_query, va_list args )
146 {
147     demux_sys_t *p_sys  = p_demux->p_sys;
148     int64_t *pi64;
149
150     switch( i_query )
151     {
152 #if 0
153     case DEMUX_GET_TIME:
154         pi64 = (int64_t*)va_arg( args, int64_t * );
155         *pi64 = date_Get( &p_sys->pts );
156         return VLC_SUCCESS;
157 #endif
158     default:
159         return demux2_vaControlHelper( p_demux->s, 0, -1,
160                                        8*CDG_FRAME_SIZE*CDG_FRAME_RATE, CDG_FRAME_SIZE, i_query, args );
161     }
162 }
163