]> git.sesse.net Git - vlc/blob - modules/demux/rawdv.c
Ogg: remove unused function and variable
[vlc] / modules / demux / rawdv.c
1 /*****************************************************************************
2  * rawdv.c : raw DV input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Paul Corke <paul dot corke at datatote dot co dot uk>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36
37 #include "rawdv.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 #define HURRYUP_TEXT N_( "Hurry up" )
43 #define HURRYUP_LONGTEXT N_( "The demuxer will advance timestamps if the " \
44                 "input can't keep up with the rate." )
45
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 vlc_module_begin ()
50     set_shortname( "DV" )
51     set_description( N_("DV (Digital Video) demuxer") )
52     set_capability( "demux", 3 )
53     set_category( CAT_INPUT )
54     set_subcategory( SUBCAT_INPUT_DEMUX )
55     add_bool( "rawdv-hurry-up", false, HURRYUP_TEXT, HURRYUP_LONGTEXT, false )
56     set_callbacks( Open, Close )
57     add_shortcut( "rawdv" )
58 vlc_module_end ()
59
60
61 /*****************************************************************************
62  A little bit of background information (copied over from libdv glossary).
63
64  - DIF block: A block of 80 bytes. This is the basic data framing unit of the
65        DVC tape format, analogous to sectors of hard disc.
66
67  - Video Section: Each DIF sequence contains a video section, consisting of
68        135 DIF blocks, which are further subdivided into Video Segments.
69
70  - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
71        to a single compressed macroblock.
72
73 *****************************************************************************/
74
75 /*****************************************************************************
76  * Definitions of structures used by this plugin
77  *****************************************************************************/
78 typedef struct {
79     int8_t sct;      /* Section type (header,subcode,aux,audio,video) */
80     int8_t dsn;      /* DIF sequence number (0-12) */
81     int    fsc;      /* First (0)/Second channel (1) */
82     int8_t dbn;      /* DIF block number (0-134) */
83 } dv_id_t;
84
85 typedef struct {
86     int    dsf;      /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
87     int8_t apt;
88     int    tf1;
89     int8_t ap1;
90     int    tf2;
91     int8_t ap2;
92     int    tf3;
93     int8_t ap3;
94 } dv_header_t;
95
96 struct demux_sys_t
97 {
98     int    frame_size;
99
100     es_out_id_t *p_es_video;
101     es_format_t  fmt_video;
102
103     es_out_id_t *p_es_audio;
104     es_format_t  fmt_audio;
105
106     int    i_dsf;
107     double f_rate;
108     int    i_bitrate;
109
110     /* program clock reference (in units of 90kHz) */
111     mtime_t i_pcr;
112     bool b_hurry_up;
113 };
114
115 /*****************************************************************************
116  * Local prototypes
117  *****************************************************************************/
118 static int Demux( demux_t * );
119 static int Control( demux_t *, int i_query, va_list args );
120
121 /*****************************************************************************
122  * Open: initializes raw DV demux structures
123  *****************************************************************************/
124 static int Open( vlc_object_t * p_this )
125 {
126     demux_t     *p_demux = (demux_t*)p_this;
127     demux_sys_t *p_sys;
128
129     const uint8_t *p_peek, *p_peek_backup;
130
131     uint32_t    i_dword;
132     dv_header_t dv_header;
133     dv_id_t     dv_id;
134
135     /* It isn't easy to recognize a raw DV stream. The chances that we'll
136      * mistake a stream from another type for a raw DV stream are too high, so
137      * we'll rely on the file extension to trigger this demux. Alternatively,
138      * it is possible to force this demux. */
139
140     /* Check for DV file extension */
141     if( !demux_IsPathExtension( p_demux, ".dv" ) && !p_demux->b_force )
142         return VLC_EGENERIC;
143
144     if( stream_Peek( p_demux->s, &p_peek, DV_PAL_FRAME_SIZE ) <
145         DV_NTSC_FRAME_SIZE )
146     {
147         /* Stream too short ... */
148         msg_Err( p_demux, "cannot peek()" );
149         return VLC_EGENERIC;
150     }
151     p_peek_backup = p_peek;
152
153     /* fill in the dv_id_t structure */
154     i_dword = GetDWBE( p_peek ); p_peek += 4;
155     dv_id.sct = i_dword >> (32 - 3);
156     i_dword <<= 8;
157     dv_id.dsn = i_dword >> (32 - 4);
158     i_dword <<= 4;
159     dv_id.fsc = i_dword >> (32 - 1);
160     i_dword <<= 4;
161     dv_id.dbn = i_dword >> (32 - 8);
162     i_dword <<= 8;
163
164     if( dv_id.sct != 0 )
165     {
166         msg_Warn( p_demux, "not a raw DV stream header" );
167         return VLC_EGENERIC;
168     }
169
170     /* fill in the dv_header_t structure */
171     dv_header.dsf = i_dword >> (32 - 1);
172     i_dword <<= 1;
173     if( i_dword >> (32 - 1) ) /* incorrect bit */
174     {
175         msg_Warn( p_demux, "incorrect bit" );
176         return VLC_EGENERIC;
177     }
178
179     i_dword = GetDWBE( p_peek ); p_peek += 4;
180     i_dword <<= 5;
181     dv_header.apt = i_dword >> (32 - 3);
182     i_dword <<= 3;
183     dv_header.tf1 = i_dword >> (32 - 1);
184     i_dword <<= 5;
185     dv_header.ap1 = i_dword >> (32 - 3);
186     i_dword <<= 3;
187     dv_header.tf2 = i_dword >> (32 - 1);
188     i_dword <<= 5;
189     dv_header.ap2 = i_dword >> (32 - 3);
190     i_dword <<= 3;
191     dv_header.tf3 = i_dword >> (32 - 1);
192     i_dword <<= 5;
193     dv_header.ap3 = i_dword >> (32 - 3);
194
195     p_peek += 72;                                  /* skip rest of DIF block */
196
197     /* Set p_input field */
198     p_demux->pf_demux   = Demux;
199     p_demux->pf_control = Control;
200     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
201     if( !p_sys )
202         return VLC_ENOMEM;
203
204     p_sys->b_hurry_up = var_CreateGetBool( p_demux, "rawdv-hurry-up" );
205     msg_Dbg( p_demux, "Realtime DV Source: %s", (p_sys->b_hurry_up)?"Yes":"No" );
206
207     p_sys->i_dsf = dv_header.dsf;
208     p_sys->frame_size = dv_header.dsf ? DV_PAL_FRAME_SIZE
209                                       : DV_NTSC_FRAME_SIZE;
210     p_sys->f_rate = dv_header.dsf ? 25 : 29.97;
211
212     p_sys->i_pcr = 0;
213     p_sys->p_es_video = NULL;
214     p_sys->p_es_audio = NULL;
215
216     p_sys->i_bitrate = 0;
217
218     es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_CODEC_DV );
219     p_sys->fmt_video.video.i_width = 720;
220     p_sys->fmt_video.video.i_height= dv_header.dsf ? 576 : 480;;
221
222     /* FIXME FIXME */
223 #if 0
224     /* necessary because input_SplitBuffer() will only get
225      * INPUT_DEFAULT_BUFSIZE bytes at a time. */
226     p_input->i_bufsize = p_sys->frame_size;
227 #endif
228
229     p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
230
231     /* Audio stuff */
232     p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
233     if( *p_peek == 0x50 )
234     {
235         dv_get_audio_format( &p_sys->fmt_audio, &p_peek[1] );
236         p_sys->p_es_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
237     }
238
239     return VLC_SUCCESS;
240 }
241
242 /*****************************************************************************
243  * Close: frees unused data
244  *****************************************************************************/
245 static void Close( vlc_object_t *p_this )
246 {
247     demux_t     *p_demux = (demux_t*)p_this;
248     demux_sys_t *p_sys  = p_demux->p_sys;
249
250     var_Destroy( p_demux, "rawdv-hurry-up");
251     free( p_sys );
252 }
253
254 /*****************************************************************************
255  * Demux: reads and demuxes data packets
256  *****************************************************************************
257  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
258  *****************************************************************************/
259 static int Demux( demux_t *p_demux )
260 {
261     demux_sys_t *p_sys  = p_demux->p_sys;
262     block_t     *p_block;
263     bool  b_audio = false;
264
265     if( p_sys->b_hurry_up )
266     {
267          /* 3 frames */
268         p_sys->i_pcr = mdate() + (p_sys->i_dsf ? 120000 : 90000);
269     }
270
271     /* Call the pace control */
272     es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
273     p_block = stream_Block( p_demux->s, p_sys->frame_size );
274     if( p_block == NULL )
275     {
276         /* EOF */
277         return 0;
278     }
279
280     if( p_sys->p_es_audio )
281     {
282         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
283                         p_sys->p_es_audio, &b_audio );
284     }
285
286     p_block->i_dts =
287     p_block->i_pts = VLC_TS_0 + p_sys->i_pcr;
288
289     if( b_audio )
290     {
291         block_t *p_audio_block = dv_extract_audio( p_block );
292         if( p_audio_block )
293             es_out_Send( p_demux->out, p_sys->p_es_audio, p_audio_block );
294     }
295
296     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
297
298     if( !p_sys->b_hurry_up )
299     {
300         p_sys->i_pcr += ( INT64_C(1000000) / p_sys->f_rate );
301     }
302
303     return 1;
304 }
305
306 /*****************************************************************************
307  * Control:
308  *****************************************************************************/
309 static int Control( demux_t *p_demux, int i_query, va_list args )
310 {
311     demux_sys_t *p_sys  = p_demux->p_sys;
312
313     /* XXX: DEMUX_SET_TIME is precise here */
314     return demux_vaControlHelper( p_demux->s,
315                                    0, -1,
316                                    p_sys->frame_size * p_sys->f_rate * 8,
317                                    p_sys->frame_size, i_query, args );
318 }
319