]> git.sesse.net Git - vlc/blob - modules/demux/rawdv.c
b4e122e9c8fbfffd7bbb1bb8a955272b802428f3
[vlc] / modules / demux / rawdv.c
1 /*****************************************************************************
2  * rawdv.c : raw dv input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: rawdv.c,v 1.9 2003/08/17 23:02:52 fenrir 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 #include <string.h>                                              /* strdup() */
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33
34 #include <sys/types.h>
35
36 #include <codecs.h>                        /* BITMAPINFOHEADER, WAVEFORMATEX */
37
38 /*****************************************************************************
39  A little bit of background information (copied over from libdv glossary).
40
41  - DIF block: A block of 80 bytes. This is the basic data framing unit of the
42        DVC tape format, analogous to sectors of hard disc.
43  
44  - Video Section: Each DIF sequence contains a video section, consisting of
45        135 DIF blocks, which are further subdivided into Video Segments.
46
47  - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
48        to a single compressed macroblock.
49
50 *****************************************************************************/
51
52
53 /*****************************************************************************
54  * Constants
55  *****************************************************************************/
56 #define DV_PAL_FRAME_SIZE  144000
57 #define DV_NTSC_FRAME_SIZE 122000
58
59 /*****************************************************************************
60  * Definitions of structures used by this plugin
61  *****************************************************************************/
62 typedef struct {
63     int8_t sct;      /* Section type (header,subcode,aux,audio,video) */
64     int8_t dsn;      /* DIF sequence number (0-12) */
65     int    fsc;      /* First (0)/Second channel (1) */
66     int8_t dbn;      /* DIF block number (0-134) */
67 } dv_id_t;
68
69 typedef struct {
70     int    dsf;      /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
71     int8_t apt;
72     int    tf1;
73     int8_t ap1;
74     int    tf2;
75     int8_t ap2;
76     int    tf3;
77     int8_t ap3;
78 } dv_header_t;
79
80 struct demux_sys_t
81 {
82     int    frame_size;
83
84     es_descriptor_t  *p_video_es;
85     es_descriptor_t  *p_audio_es;
86
87     /* codec specific stuff */
88     BITMAPINFOHEADER *p_bih;
89     WAVEFORMATEX *p_wf;
90
91     double f_rate;
92     int    i_bitrate;
93
94     /* program clock reference (in units of 90kHz) */
95     mtime_t i_pcr;
96 };
97
98 /*****************************************************************************
99  * Local prototypes
100  *****************************************************************************/
101 static int  Activate  ( vlc_object_t * );
102 static void Deactivate( vlc_object_t * );
103 static int  Demux     ( input_thread_t * );
104
105 /*****************************************************************************
106  * Module descriptor
107  *****************************************************************************/
108 vlc_module_begin();
109     set_description( _("raw dv demuxer") );
110     set_capability( "demux", 2 );
111     set_callbacks( Activate, NULL );
112     add_shortcut( "rawdv" );
113 vlc_module_end();
114
115 /*****************************************************************************
116  * Activate: initializes raw dv demux structures
117  *****************************************************************************/
118 static int Activate( vlc_object_t * p_this )
119 {
120     input_thread_t *p_input = (input_thread_t *)p_this;
121     byte_t         *p_peek, *p_peek_backup;
122     uint32_t       i_dword;
123     demux_sys_t    *p_rawdv;
124     dv_header_t    dv_header;
125     dv_id_t        dv_id;
126     char           *psz_ext;
127
128     /* It isn't easy to recognize a raw dv stream. The chances that we'll
129      * mistake a stream from another type for a raw dv stream are too high, so
130      * we'll rely on the file extension to trigger this demux. Alternatively,
131      * it is possible to force this demux. */
132
133     /* Check for dv file extension */
134     psz_ext = strrchr ( p_input->psz_name, '.' );
135     if( ( !psz_ext || strcasecmp( psz_ext, ".dv") )&&
136         ( !p_input->psz_demux || strcmp(p_input->psz_demux, "rawdv") ) )
137     {
138         return -1;
139     }
140
141     p_input->pf_demux = Demux;
142
143     /* Have a peep at the show. */
144     if( input_Peek(p_input, &p_peek, DV_PAL_FRAME_SIZE) < DV_NTSC_FRAME_SIZE )
145     {
146         /* Stream too short ... */
147         msg_Err( p_input, "cannot peek()" );
148         return -1;
149     }
150     p_peek_backup = p_peek;
151
152     /* fill in the dv_id_t structure */
153     i_dword = GetDWBE( p_peek ); p_peek += 4;
154     dv_id.sct = i_dword >> (32 - 3);
155     i_dword <<= 8;
156     dv_id.dsn = i_dword >> (32 - 4);
157     i_dword <<= 4;
158     dv_id.fsc = i_dword >> (32 - 1);
159     i_dword <<= 4;
160     dv_id.dbn = i_dword >> (32 - 8);
161     i_dword <<= 8;
162
163     if( dv_id.sct != 0 )
164     {
165         msg_Warn( p_input, "not a raw dv stream header" );
166         return -1;
167     }
168
169     /* fill in the dv_header_t structure */
170     dv_header.dsf = i_dword >> (32 - 1);
171     i_dword <<= 1;
172     if( i_dword >> (32 - 1) ) /* incorrect bit */
173     {
174         msg_Warn( p_input, "incorrect bit" );
175         return -1;
176     }
177
178     i_dword = GetDWBE( p_peek ); p_peek += 4;
179     i_dword <<= 5;
180     dv_header.apt = i_dword >> (32 - 3);
181     i_dword <<= 3;
182     dv_header.tf1 = i_dword >> (32 - 1);
183     i_dword <<= 5;
184     dv_header.ap1 = i_dword >> (32 - 3);
185     i_dword <<= 3;
186     dv_header.tf2 = i_dword >> (32 - 1);
187     i_dword <<= 5;
188     dv_header.ap2 = i_dword >> (32 - 3);
189     i_dword <<= 3;
190     dv_header.tf3 = i_dword >> (32 - 1);
191     i_dword <<= 5;
192     dv_header.ap3 = i_dword >> (32 - 3);
193
194     p_peek += 72;                                  /* skip rest of DIF block */
195
196
197     /* Setup the structures for our demuxer */
198     if( !( p_rawdv = malloc( sizeof( demux_sys_t ) ) ) )
199     {
200         msg_Err( p_input, "out of memory" );
201         return -1;
202     }
203     memset( p_rawdv, 0, sizeof( demux_sys_t ) );
204     p_rawdv->p_bih = NULL;
205     p_rawdv->p_wf = NULL;
206     p_input->p_demux_data = p_rawdv;
207
208     p_rawdv->p_bih = (BITMAPINFOHEADER *) malloc( sizeof(BITMAPINFOHEADER) );
209     if( !p_rawdv->p_bih )
210     {
211         msg_Err( p_input, "out of memory" );
212         goto error;
213     }
214     p_rawdv->p_bih->biSize = sizeof(BITMAPINFOHEADER);
215     p_rawdv->p_bih->biCompression = VLC_FOURCC( 'd','v','s','d' );
216     p_rawdv->p_bih->biSize = 40;
217     p_rawdv->p_bih->biWidth = 720;
218     p_rawdv->p_bih->biHeight = dv_header.dsf ? 576 : 480;
219     p_rawdv->p_bih->biPlanes = 1;
220     p_rawdv->p_bih->biBitCount = 24;
221     p_rawdv->p_bih->biSizeImage =
222         p_rawdv->p_bih->biWidth * p_rawdv->p_bih->biHeight
223           * (p_rawdv->p_bih->biBitCount >> 3);
224
225     /* Properties of our video */
226     if( dv_header.dsf )
227     {
228         p_rawdv->frame_size = 12 * 150 * 80;
229         p_rawdv->f_rate = 25;
230     }
231     else
232     {
233         p_rawdv->frame_size = 10 * 150 * 80;
234         p_rawdv->f_rate = 29.97;
235     }
236
237     /* Audio stuff */
238 #if 0
239     p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
240
241     if( *p_peek != 0x50 || *p_peek != 0x51 )
242     {
243         msg_Err( p_input, "AAUX should begin with 0x50" );
244     }
245 #endif
246
247     p_rawdv->p_wf = (WAVEFORMATEX *)malloc( sizeof(WAVEFORMATEX) );
248     if( !p_rawdv->p_wf )
249     {
250         msg_Err( p_input, "out of memory" );
251         goto error;
252     }
253
254     p_rawdv->p_wf->wFormatTag = 0;
255     p_rawdv->p_wf->nChannels = 2;
256     p_rawdv->p_wf->nSamplesPerSec = 44100; /* FIXME */
257     p_rawdv->p_wf->nAvgBytesPerSec = p_rawdv->f_rate * p_rawdv->frame_size;
258     p_rawdv->p_wf->nBlockAlign = p_rawdv->frame_size;
259     p_rawdv->p_wf->wBitsPerSample = 16;
260     p_rawdv->p_wf->cbSize = 0;
261
262
263     /* necessary because input_SplitBuffer() will only get
264      * INPUT_DEFAULT_BUFSIZE bytes at a time. */
265     p_input->i_bufsize = p_rawdv->frame_size;
266
267     /* Create one program */
268     vlc_mutex_lock( &p_input->stream.stream_lock );
269     if( input_InitStream( p_input, 0 ) == -1)
270     {
271         vlc_mutex_unlock( &p_input->stream.stream_lock );
272         msg_Err( p_input, "cannot init stream" );
273         goto error;
274     }
275     if( input_AddProgram( p_input, 0, 0) == NULL )
276     {
277         vlc_mutex_unlock( &p_input->stream.stream_lock );
278         msg_Err( p_input, "cannot add program" );
279         goto error;
280     }
281     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
282     p_input->stream.i_mux_rate = p_rawdv->frame_size * p_rawdv->f_rate;
283     vlc_mutex_unlock( &p_input->stream.stream_lock );
284
285     /* Add video stream */
286     vlc_mutex_lock( &p_input->stream.stream_lock );
287     p_rawdv->p_video_es = input_AddES( p_input,
288                                        p_input->stream.p_selected_program,
289                                        1, VIDEO_ES, NULL, 0 );
290     p_rawdv->p_video_es->i_stream_id = 0;
291     p_rawdv->p_video_es->i_fourcc = VLC_FOURCC( 'd','v','s','d' );
292     p_rawdv->p_video_es->p_bitmapinfoheader = (void *)p_rawdv->p_bih;
293     input_SelectES( p_input, p_rawdv->p_video_es );
294     vlc_mutex_unlock( &p_input->stream.stream_lock );
295
296     /* Add audio stream */
297     vlc_mutex_lock( &p_input->stream.stream_lock );
298     p_rawdv->p_audio_es = input_AddES( p_input,
299                                        p_input->stream.p_selected_program,
300                                        2, AUDIO_ES, NULL, 0 );
301     p_rawdv->p_audio_es->i_stream_id = 1;
302     p_rawdv->p_audio_es->i_fourcc = VLC_FOURCC( 'd','v','a','u' );
303     p_rawdv->p_audio_es->p_waveformatex = (void *)p_rawdv->p_wf;
304     input_SelectES( p_input, p_rawdv->p_audio_es );
305     vlc_mutex_unlock( &p_input->stream.stream_lock );
306
307     /* Init pcr */
308     p_rawdv->i_pcr = 0;
309
310     return 0;
311
312  error:
313     if( p_rawdv->p_bih ) free( p_rawdv->p_bih );
314     if( p_rawdv->p_wf ) free( p_rawdv->p_wf );
315     Deactivate( (vlc_object_t *)p_input );
316     return -1;
317 }
318
319 /*****************************************************************************
320  * Deactivate: frees unused data
321  *****************************************************************************/
322 static void Deactivate( vlc_object_t *p_this )
323 {
324     input_thread_t *p_input = (input_thread_t *)p_this;
325     demux_sys_t *p_rawdv = (demux_sys_t *)p_input->p_demux_data;
326
327     free( p_rawdv );
328 }
329
330 /*****************************************************************************
331  * Demux: reads and demuxes data packets
332  *****************************************************************************
333  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
334  *****************************************************************************/
335 static int Demux( input_thread_t * p_input )
336 {
337     demux_sys_t    *p_rawdv = (demux_sys_t *)p_input->p_demux_data;
338     pes_packet_t   *p_pes = NULL;
339     pes_packet_t   *p_audio_pes = NULL;
340     data_packet_t  *p_data;
341     ssize_t        i_read;
342
343     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
344     {
345         off_t i_pos;
346
347         msg_Warn( p_input, "synchro reinit" );
348
349         /* If the user tried to seek in the stream, we need to make sure
350          * the new position is at a DIF block boundary. */
351         vlc_mutex_lock( &p_input->stream.stream_lock );
352         i_pos= p_input->stream.p_selected_area->i_tell;
353         vlc_mutex_unlock( &p_input->stream.stream_lock );
354
355         if( (i_pos % p_rawdv->frame_size) &&
356             p_input->stream.b_seekable &&
357             p_input->stream.i_method == INPUT_METHOD_FILE )
358         {
359             p_input->pf_seek( p_input, (i_pos / p_rawdv->frame_size)
360                               * p_rawdv->frame_size );
361             input_AccessReinit( p_input );
362         }
363     }
364
365     /* Call the pace control */
366     input_ClockManageRef( p_input, p_input->stream.p_selected_program,
367                           p_rawdv->i_pcr );
368
369     i_read = input_SplitBuffer( p_input, &p_data, p_rawdv->frame_size );
370     if( i_read <= 0 )
371     {
372         return i_read;
373     }
374
375     /* Build video PES packet */
376     if( p_rawdv->p_video_es->p_decoder_fifo )
377     {
378         p_pes = input_NewPES( p_input->p_method_data );
379         if( p_pes == NULL )
380         {
381             msg_Err( p_input, "out of memory" );
382             input_DeletePacket( p_input->p_method_data, p_data );
383             return -1;
384         }
385
386         p_pes->i_rate = p_input->stream.control.i_rate;
387         p_pes->p_first = p_pes->p_last = p_data;
388         p_pes->i_pes_size = i_read;
389         p_pes->i_nb_data = 1;
390         p_pes->i_pts =
391             input_ClockGetTS( p_input, p_input->stream.p_selected_program,
392                               p_rawdv->i_pcr );
393     }
394
395     /* Do the same for audio */
396     if( p_rawdv->p_audio_es->p_decoder_fifo )
397     {
398         p_audio_pes = input_NewPES( p_input->p_method_data );
399         if( p_audio_pes == NULL )
400         {
401             msg_Err( p_input, "out of memory" );
402             input_DeletePacket( p_input->p_method_data, p_data );
403             return -1;
404         }
405         p_audio_pes->i_rate = p_input->stream.control.i_rate;
406
407         if( p_rawdv->p_video_es->p_decoder_fifo )
408             p_audio_pes->p_first = p_audio_pes->p_last =
409                 input_ShareBuffer( p_input->p_method_data, p_data->p_buffer );
410         else
411             p_audio_pes->p_first = p_audio_pes->p_last = p_data;
412
413         p_audio_pes->p_first->p_next = p_data->p_next;
414         p_audio_pes->p_first->p_payload_start = p_data->p_payload_start;
415         p_audio_pes->p_first->p_payload_end = p_data->p_payload_end;
416         p_audio_pes->i_pes_size = i_read;
417         p_audio_pes->i_nb_data = 1;
418         p_audio_pes->i_pts =
419             input_ClockGetTS( p_input, p_input->stream.p_selected_program,
420                               p_rawdv->i_pcr );
421     }
422
423     /* Decode PES packets if stream is selected */
424     if( p_rawdv->p_video_es->p_decoder_fifo )
425         input_DecodePES( p_rawdv->p_video_es->p_decoder_fifo, p_pes );
426     if( p_rawdv->p_audio_es->p_decoder_fifo )
427         input_DecodePES( p_rawdv->p_audio_es->p_decoder_fifo, p_audio_pes );
428
429     p_rawdv->i_pcr += ( 90000 / p_rawdv->f_rate );
430
431     if( !p_rawdv->p_video_es->p_decoder_fifo &&
432         !p_rawdv->p_audio_es->p_decoder_fifo )
433         input_DeletePacket( p_input->p_method_data, p_data );
434
435     return 1;
436 }