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