]> git.sesse.net Git - vlc/blob - modules/demux/vc1.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / demux / vc1.c
1 /*****************************************************************************
2  * vc1.c : VC1 Video demuxer
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
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 #include "vlc_codec.h"
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
37
38 #define FPS_TEXT N_("Frames per Second")
39 #define FPS_LONGTEXT N_("Desired frame rate for the VC-1 stream.")
40
41 vlc_module_begin();
42     set_shortname( "VC-1");
43     set_category( CAT_INPUT );
44     set_subcategory( SUBCAT_INPUT_DEMUX );
45     set_description( _("VC1 video demuxer" ) );
46     set_capability( "demux2", 0 );
47     add_float( "vc1-fps", 25.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
48         change_safe();
49     set_callbacks( Open, Close );
50     add_shortcut( "vc1" );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 struct demux_sys_t
57 {
58     mtime_t     i_dts;
59     es_out_id_t *p_es;
60
61     float       f_fps;
62     decoder_t *p_packetizer;
63 };
64
65 static int Demux( demux_t * );
66 static int Control( demux_t *, int, va_list );
67
68 #define VC1_PACKET_SIZE 4096
69
70 /*****************************************************************************
71  * Open: initializes demux 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     const uint8_t *p_peek;
78     vlc_value_t val;
79
80     if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
81
82     if( p_peek[0] != 0x00 || p_peek[1] != 0x00 ||
83         p_peek[2] != 0x00 || p_peek[3] != 0x01 ||
84         p_peek[4] != 0x0f ) /* Sequence header */
85     {
86         if( !p_demux->b_force )
87         {
88             msg_Warn( p_demux, "vc-1 module discarded (no startcode)" );
89             return VLC_EGENERIC;
90         }
91
92         msg_Err( p_demux, "this doesn't look like a VC-1 ES stream, "
93                  "continuing anyway" );
94     }
95
96     p_demux->pf_demux  = Demux;
97     p_demux->pf_control= Control;
98     p_demux->p_sys     = p_sys = malloc( sizeof( demux_sys_t ) );
99     p_sys->p_es        = NULL;
100     p_sys->i_dts       = 1;
101     p_sys->f_fps = var_CreateGetFloat( p_demux, "vc1-fps" );
102     if( val.f_float < 0.001 ) p_sys->f_fps = 0.0;
103
104     /* Load the packetizer */
105     INIT_VPACKETIZER( p_sys->p_packetizer,  'W', 'V', 'C', '1'  );
106     es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
107     LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "VC-1" );
108
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * Close: frees unused data
114  *****************************************************************************/
115 static void Close( vlc_object_t * p_this )
116 {
117     demux_t     *p_demux = (demux_t*)p_this;
118     demux_sys_t *p_sys = p_demux->p_sys;
119
120     DESTROY_PACKETIZER( p_sys->p_packetizer );
121
122     free( p_sys );
123 }
124
125 /*****************************************************************************
126  * Demux: reads and demuxes data packets
127  *****************************************************************************
128  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
129  *****************************************************************************/
130 static int Demux( demux_t *p_demux)
131 {
132     demux_sys_t *p_sys = p_demux->p_sys;
133     block_t *p_block_in, *p_block_out;
134
135     if( ( p_block_in = stream_Block( p_demux->s, VC1_PACKET_SIZE ) ) == NULL )
136         return 0;
137
138     /*  */
139     p_block_in->i_dts = 1;
140     p_block_in->i_pts = 1;
141
142     while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
143     {
144         while( p_block_out )
145         {
146             block_t *p_next = p_block_out->p_next;
147
148             p_block_out->p_next = NULL;
149
150             if( p_sys->p_es == NULL )
151             {
152                 p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
153                 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out);
154             }
155
156             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_dts );
157             p_block_out->i_dts = p_sys->i_dts;
158             p_block_out->i_pts = p_sys->i_dts;
159
160             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
161
162             p_block_out = p_next;
163
164             if( p_sys->p_packetizer->fmt_out.video.i_frame_rate > 0 &&
165                 p_sys->p_packetizer->fmt_out.video.i_frame_rate_base > 0 )
166                 p_sys->i_dts += I64C(1000000) *
167                     p_sys->p_packetizer->fmt_out.video.i_frame_rate_base /
168                     p_sys->p_packetizer->fmt_out.video.i_frame_rate;
169             else if( p_sys->f_fps > 0.001 )
170                 p_sys->i_dts += (int64_t)((double)1000000.0 / p_sys->f_fps);
171             else
172                 p_sys->i_dts += I64C(1000000) / 25;
173         }
174     }
175     return 1;
176 }
177
178 /*****************************************************************************
179  * Control:
180  *****************************************************************************/
181 static int Control( demux_t *p_demux, int i_query, va_list args )
182 {
183     /* demux_sys_t *p_sys  = p_demux->p_sys; */
184     /* FIXME calculate the bitrate */
185     if( i_query == DEMUX_SET_TIME )
186         return VLC_EGENERIC;
187     else
188         return demux2_vaControlHelper( p_demux->s,
189                                        0, -1,
190                                        0, 1, i_query, args );
191 }
192