]> git.sesse.net Git - vlc/blob - modules/demux/rawdv.c
* Stringreview !!!
[vlc] / modules / demux / rawdv.c
1 /*****************************************************************************
2  * rawdv.c : raw dv input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: rawdv.c,v 1.14 2004/01/25 20:05:28 hartman Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
37
38 vlc_module_begin();
39     set_description( _("raw dv demuxer") );
40     set_capability( "demux", 2 );
41     set_callbacks( Open, Close );
42     add_shortcut( "rawdv" );
43 vlc_module_end();
44
45
46 /*****************************************************************************
47  A little bit of background information (copied over from libdv glossary).
48
49  - DIF block: A block of 80 bytes. This is the basic data framing unit of the
50        DVC tape format, analogous to sectors of hard disc.
51
52  - Video Section: Each DIF sequence contains a video section, consisting of
53        135 DIF blocks, which are further subdivided into Video Segments.
54
55  - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
56        to a single compressed macroblock.
57
58 *****************************************************************************/
59
60
61 /*****************************************************************************
62  * Constants
63  *****************************************************************************/
64 #define DV_PAL_FRAME_SIZE  144000
65 #define DV_NTSC_FRAME_SIZE 122000
66
67 /*****************************************************************************
68  * Definitions of structures used by this plugin
69  *****************************************************************************/
70 typedef struct {
71     int8_t sct;      /* Section type (header,subcode,aux,audio,video) */
72     int8_t dsn;      /* DIF sequence number (0-12) */
73     int    fsc;      /* First (0)/Second channel (1) */
74     int8_t dbn;      /* DIF block number (0-134) */
75 } dv_id_t;
76
77 typedef struct {
78     int    dsf;      /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
79     int8_t apt;
80     int    tf1;
81     int8_t ap1;
82     int    tf2;
83     int8_t ap2;
84     int    tf3;
85     int8_t ap3;
86 } dv_header_t;
87
88 struct demux_sys_t
89 {
90     int    frame_size;
91
92     es_out_id_t *p_es_video;
93     es_format_t  fmt_video;
94
95     es_out_id_t *p_es_audio;
96     es_format_t  fmt_audio;
97
98     double f_rate;
99     int    i_bitrate;
100
101     /* program clock reference (in units of 90kHz) */
102     mtime_t i_pcr;
103 };
104
105 /*****************************************************************************
106  * Local prototypes
107  *****************************************************************************/
108 static int  Demux     ( input_thread_t * );
109
110 /*****************************************************************************
111  * Open: initializes raw dv demux structures
112  *****************************************************************************/
113 static int Open( vlc_object_t * p_this )
114 {
115     input_thread_t *p_input = (input_thread_t *)p_this;
116     demux_sys_t    *p_sys;
117
118     byte_t         *p_peek, *p_peek_backup;
119
120     uint32_t       i_dword;
121     dv_header_t    dv_header;
122     dv_id_t        dv_id;
123     char           *psz_ext;
124
125     /* It isn't easy to recognize a raw dv stream. The chances that we'll
126      * mistake a stream from another type for a raw dv stream are too high, so
127      * we'll rely on the file extension to trigger this demux. Alternatively,
128      * it is possible to force this demux. */
129
130     /* Check for dv file extension */
131     psz_ext = strrchr ( p_input->psz_name, '.' );
132     if( ( !psz_ext || strcasecmp( psz_ext, ".dv") )&&
133         ( !p_input->psz_demux || strcmp(p_input->psz_demux, "rawdv") ) )
134     {
135         return VLC_EGENERIC;
136     }
137
138     if( stream_Peek( p_input->s, &p_peek, DV_PAL_FRAME_SIZE ) < DV_NTSC_FRAME_SIZE )
139     {
140         /* Stream too short ... */
141         msg_Err( p_input, "cannot peek()" );
142         return VLC_EGENERIC;
143     }
144     p_peek_backup = p_peek;
145
146     /* fill in the dv_id_t structure */
147     i_dword = GetDWBE( p_peek ); p_peek += 4;
148     dv_id.sct = i_dword >> (32 - 3);
149     i_dword <<= 8;
150     dv_id.dsn = i_dword >> (32 - 4);
151     i_dword <<= 4;
152     dv_id.fsc = i_dword >> (32 - 1);
153     i_dword <<= 4;
154     dv_id.dbn = i_dword >> (32 - 8);
155     i_dword <<= 8;
156
157     if( dv_id.sct != 0 )
158     {
159         msg_Warn( p_input, "not a raw dv stream header" );
160         return VLC_EGENERIC;
161     }
162
163     /* fill in the dv_header_t structure */
164     dv_header.dsf = i_dword >> (32 - 1);
165     i_dword <<= 1;
166     if( i_dword >> (32 - 1) ) /* incorrect bit */
167     {
168         msg_Warn( p_input, "incorrect bit" );
169         return VLC_EGENERIC;
170     }
171
172     i_dword = GetDWBE( p_peek ); p_peek += 4;
173     i_dword <<= 5;
174     dv_header.apt = i_dword >> (32 - 3);
175     i_dword <<= 3;
176     dv_header.tf1 = i_dword >> (32 - 1);
177     i_dword <<= 5;
178     dv_header.ap1 = i_dword >> (32 - 3);
179     i_dword <<= 3;
180     dv_header.tf2 = i_dword >> (32 - 1);
181     i_dword <<= 5;
182     dv_header.ap2 = i_dword >> (32 - 3);
183     i_dword <<= 3;
184     dv_header.tf3 = i_dword >> (32 - 1);
185     i_dword <<= 5;
186     dv_header.ap3 = i_dword >> (32 - 3);
187
188     p_peek += 72;                                  /* skip rest of DIF block */
189
190
191     /* Set p_input field */
192     p_input->pf_demux = Demux;
193     p_input->pf_demux_control = demux_vaControlDefault;
194     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
195
196     p_sys->frame_size = dv_header.dsf ? 12 * 150 * 80 : 10 * 150 * 80;
197     p_sys->f_rate = dv_header.dsf ? 25 : 29.97;
198
199     p_sys->i_pcr = 0;
200     p_sys->p_es_video = NULL;
201     p_sys->p_es_audio = NULL;
202
203     p_sys->i_bitrate = 0;
204
205     es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC( 'd','v','s','d' ) );
206     p_sys->fmt_video.video.i_width = 720;
207     p_sys->fmt_video.video.i_height= dv_header.dsf ? 576 : 480;;
208
209     /* Audio stuff */
210 #if 0
211     p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
212
213     if( *p_peek != 0x50 || *p_peek != 0x51 )
214     {
215         msg_Err( p_input, "AAUX should begin with 0x50" );
216     }
217 #endif
218
219     es_format_Init( &p_sys->fmt_audio, AUDIO_ES, VLC_FOURCC( 'd','v','a','u' ) );
220     p_sys->fmt_audio.audio.i_channels = 2;
221     p_sys->fmt_audio.audio.i_rate = 44100;  /* FIXME */
222     p_sys->fmt_audio.audio.i_bitspersample = 16;
223     p_sys->fmt_audio.audio.i_blockalign = p_sys->frame_size;    /* ??? */
224     p_sys->fmt_audio.i_bitrate = p_sys->f_rate * p_sys->frame_size; /* ??? */
225
226     /* necessary because input_SplitBuffer() will only get
227      * INPUT_DEFAULT_BUFSIZE bytes at a time. */
228     p_input->i_bufsize = p_sys->frame_size;
229
230     vlc_mutex_lock( &p_input->stream.stream_lock );
231     if( input_InitStream( p_input, 0 ) == -1)
232     {
233         vlc_mutex_unlock( &p_input->stream.stream_lock );
234         msg_Err( p_input, "cannot init stream" );
235         free( p_sys );
236         return VLC_EGENERIC;
237     }
238     p_input->stream.i_mux_rate = p_sys->frame_size * p_sys->f_rate;
239     vlc_mutex_unlock( &p_input->stream.stream_lock );
240
241     p_sys->p_es_video = es_out_Add( p_input->p_es_out, &p_sys->fmt_video );
242     p_sys->p_es_audio = es_out_Add( p_input->p_es_out, &p_sys->fmt_audio );
243
244     return VLC_SUCCESS;
245 }
246
247 /*****************************************************************************
248  * Close: frees unused data
249  *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
251 {
252     input_thread_t *p_input = (input_thread_t *)p_this;
253     demux_sys_t    *p_sys = p_input->p_demux_data;
254
255     free( p_sys );
256 }
257
258 /*****************************************************************************
259  * Demux: reads and demuxes data packets
260  *****************************************************************************
261  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
262  *****************************************************************************/
263 static int Demux( input_thread_t * p_input )
264 {
265     demux_sys_t    *p_sys = p_input->p_demux_data;
266     block_t        *p_block;
267     vlc_bool_t     b_audio, b_video;
268
269
270     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
271     {
272         off_t i_pos = stream_Tell( p_input->s );
273
274         msg_Warn( p_input, "synchro reinit" );
275
276         /* If the user tried to seek in the stream, we need to make sure
277          * the new position is at a DIF block boundary. */
278         if( i_pos % p_sys->frame_size > 0 )
279         {
280             i_pos += p_sys->frame_size - i_pos % p_sys->frame_size;
281
282             if( stream_Seek( p_input->s, i_pos ) )
283             {
284                 msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
285                 return -1;
286             }
287         }
288     }
289
290     /* Call the pace control */
291     input_ClockManageRef( p_input, p_input->stream.p_selected_program,
292                           p_sys->i_pcr );
293
294     if( ( p_block = stream_Block( p_input->s, p_sys->frame_size ) ) == NULL )
295     {
296         /* EOF */
297         return 0;
298     }
299
300     es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE,
301                     p_sys->p_es_audio, &b_audio );
302     es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE,
303                     p_sys->p_es_video, &b_video );
304
305     p_block->i_dts =
306     p_block->i_pts = input_ClockGetTS( p_input,
307                                        p_input->stream.p_selected_program,
308                                        p_sys->i_pcr );
309     if( b_audio && b_video )
310     {
311         block_t *p_dup = block_Duplicate( p_block );
312
313         es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block );
314         if( p_dup )
315         {
316             es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_dup );
317         }
318     }
319     else if( b_audio )
320     {
321         es_out_Send( p_input->p_es_out, p_sys->p_es_audio, p_block );
322     }
323     else if( b_video )
324     {
325         es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block );
326     }
327     else
328     {
329         block_Release( p_block );
330     }
331
332     p_sys->i_pcr += ( 90000 / p_sys->f_rate );
333
334     return 1;
335 }
336