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