]> git.sesse.net Git - vlc/blob - modules/demux/rawdv.c
* modules/demux/rawdv.c: added support for DV audio now that ffmpeg can
[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.4 2003/02/18 19:42:57 gbazin 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 static uint32_t GetDWBE( uint8_t *p_buff )
106 {
107     return (uint32_t)p_buff[3] | ( ((uint32_t)p_buff[2]) << 8 ) |
108             ( ((uint32_t)p_buff[1]) << 16 ) | ( ((uint32_t)p_buff[0]) << 24 );
109 }
110
111 /*****************************************************************************
112  * Module descriptor
113  *****************************************************************************/
114 vlc_module_begin();
115     set_description( _("raw dv demux") );
116     set_capability( "demux", 2 );
117     set_callbacks( Activate, NULL );
118     add_shortcut( "rawdv" );
119 vlc_module_end();
120
121 /*****************************************************************************
122  * Activate: initializes raw dv demux structures
123  *****************************************************************************/
124 static int Activate( vlc_object_t * p_this )
125 {
126     input_thread_t *p_input = (input_thread_t *)p_this;
127     byte_t         *p_peek, *p_peek_backup;
128     uint32_t       i_dword;
129     demux_sys_t    *p_rawdv;
130     dv_header_t    dv_header;
131     dv_id_t        dv_id;
132     char           *psz_ext;
133
134     /* It isn't easy to recognize a raw dv stream. The chances that we'll
135      * mistake a stream from another type for a raw dv stream are too high, so
136      * we'll rely on the file extension to trigger this demux. Alternatively,
137      * it is possible to force this demux. */
138
139     /* Check for dv file extension */
140     psz_ext = strrchr ( p_input->psz_name, '.' );
141     if( ( !psz_ext || strcasecmp( psz_ext, ".dv") )&&
142         ( !p_input->psz_demux || strcmp(p_input->psz_demux, "rawdv") ) )
143     {
144         return -1;
145     }
146
147     p_input->pf_demux = Demux;
148
149     /* Have a peep at the show. */
150     if( input_Peek(p_input, &p_peek, DV_PAL_FRAME_SIZE) < DV_NTSC_FRAME_SIZE )
151     {
152         /* Stream too short ... */
153         msg_Err( p_input, "cannot peek()" );
154         return -1;
155     }
156     p_peek_backup = p_peek;
157
158     /* fill in the dv_id_t structure */
159     i_dword = GetDWBE( p_peek ); p_peek += 4;
160     dv_id.sct = i_dword >> (32 - 3);
161     i_dword <<= 8;
162     dv_id.dsn = i_dword >> (32 - 4);
163     i_dword <<= 4;
164     dv_id.fsc = i_dword >> (32 - 1);
165     i_dword <<= 4;
166     dv_id.dbn = i_dword >> (32 - 8);
167     i_dword <<= 8;
168
169     if( dv_id.sct != 0 )
170     {
171         msg_Warn( p_input, "not a raw dv stream header" );
172         return -1;
173     }
174
175     /* fill in the dv_header_t structure */
176     dv_header.dsf = i_dword >> (32 - 1);
177     i_dword <<= 1;
178     if( i_dword >> (32 - 1) ) /* incorrect bit */
179     {
180         msg_Warn( p_input, "incorrect bit" );
181         return -1;
182     }
183
184     i_dword = GetDWBE( p_peek ); p_peek += 4;
185     i_dword <<= 5;
186     dv_header.apt = i_dword >> (32 - 3);
187     i_dword <<= 3;
188     dv_header.tf1 = i_dword >> (32 - 1);
189     i_dword <<= 5;
190     dv_header.ap1 = i_dword >> (32 - 3);
191     i_dword <<= 3;
192     dv_header.tf2 = i_dword >> (32 - 1);
193     i_dword <<= 5;
194     dv_header.ap2 = i_dword >> (32 - 3);
195     i_dword <<= 3;
196     dv_header.tf3 = i_dword >> (32 - 1);
197     i_dword <<= 5;
198     dv_header.ap3 = i_dword >> (32 - 3);
199
200     p_peek += 72;                                  /* skip rest of DIF block */
201
202
203     /* Setup the structures for our demuxer */
204     if( !( p_rawdv = malloc( sizeof( demux_sys_t ) ) ) )
205     {
206         msg_Err( p_input, "out of memory" );
207         return -1;
208     }
209     memset( p_rawdv, 0, sizeof( demux_sys_t ) );
210     p_rawdv->p_bih = NULL;
211     p_rawdv->p_wf = NULL;
212     p_input->p_demux_data = p_rawdv;
213
214     p_rawdv->p_bih = (BITMAPINFOHEADER *) malloc( sizeof(BITMAPINFOHEADER) );
215     if( !p_rawdv->p_bih )
216     {
217         msg_Err( p_input, "out of memory" );
218         goto error;
219     }
220     p_rawdv->p_bih->biSize = sizeof(BITMAPINFOHEADER);
221     p_rawdv->p_bih->biCompression = VLC_FOURCC( 'd','v','s','d' );
222     p_rawdv->p_bih->biSize = 40;
223     p_rawdv->p_bih->biWidth = 720;
224     p_rawdv->p_bih->biHeight = dv_header.dsf ? 576 : 480;
225     p_rawdv->p_bih->biPlanes = 1;
226     p_rawdv->p_bih->biBitCount = 24;
227     p_rawdv->p_bih->biSizeImage =
228         p_rawdv->p_bih->biWidth * p_rawdv->p_bih->biHeight
229           * (p_rawdv->p_bih->biBitCount >> 3);
230
231     /* Properties of our video */
232     if( dv_header.dsf )
233     {
234         p_rawdv->frame_size = 12 * 150 * 80;
235         p_rawdv->f_rate = 25;
236     }
237     else
238     {
239         p_rawdv->frame_size = 10 * 150 * 80;
240         p_rawdv->f_rate = 29.97;
241     }
242
243     /* Audio stuff */
244 #if 0
245     p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
246
247     if( *p_peek != 0x50 || *p_peek != 0x51 )
248     {
249         msg_Err( p_input, "AAUX should begin with 0x50" );
250     }
251 #endif
252
253     p_rawdv->p_wf = (WAVEFORMATEX *)malloc( sizeof(WAVEFORMATEX) );
254     if( !p_rawdv->p_wf )
255     {
256         msg_Err( p_input, "out of memory" );
257         goto error;
258     }
259
260     p_rawdv->p_wf->wFormatTag = 0;
261     p_rawdv->p_wf->nChannels = 2;
262     p_rawdv->p_wf->nSamplesPerSec = 44100; /* FIXME */
263     p_rawdv->p_wf->nAvgBytesPerSec = p_rawdv->f_rate * p_rawdv->frame_size;
264     p_rawdv->p_wf->nBlockAlign = p_rawdv->frame_size;
265     p_rawdv->p_wf->wBitsPerSample = 16;
266     p_rawdv->p_wf->cbSize = 0;
267
268
269     /* necessary because input_SplitBuffer() will only get
270      * INPUT_DEFAULT_BUFSIZE bytes at a time. */
271     p_input->i_bufsize = p_rawdv->frame_size;
272
273     /* Create one program */
274     vlc_mutex_lock( &p_input->stream.stream_lock );
275     if( input_InitStream( p_input, 0 ) == -1)
276     {
277         vlc_mutex_unlock( &p_input->stream.stream_lock );
278         msg_Err( p_input, "cannot init stream" );
279         goto error;
280     }
281     if( input_AddProgram( p_input, 0, 0) == NULL )
282     {
283         vlc_mutex_unlock( &p_input->stream.stream_lock );
284         msg_Err( p_input, "cannot add program" );
285         goto error;
286     }
287     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
288     p_input->stream.i_mux_rate = p_rawdv->frame_size * p_rawdv->f_rate;
289     vlc_mutex_unlock( &p_input->stream.stream_lock );
290
291     /* Add video stream */
292     vlc_mutex_lock( &p_input->stream.stream_lock );
293     p_rawdv->p_video_es = input_AddES( p_input,
294                                        p_input->stream.p_selected_program,
295                                        1, 0 );
296     p_rawdv->p_video_es->i_stream_id = 0;
297     p_rawdv->p_video_es->i_fourcc = VLC_FOURCC( 'd','v','s','d' );
298     p_rawdv->p_video_es->i_cat = VIDEO_ES;
299     p_rawdv->p_video_es->p_bitmapinfoheader = (void *)p_rawdv->p_bih;
300     input_SelectES( p_input, p_rawdv->p_video_es );
301     vlc_mutex_unlock( &p_input->stream.stream_lock );
302
303     /* Add audio stream */
304     vlc_mutex_lock( &p_input->stream.stream_lock );
305     p_rawdv->p_audio_es = input_AddES( p_input,
306                                        p_input->stream.p_selected_program,
307                                        2, 0 );
308     p_rawdv->p_audio_es->i_stream_id = 1;
309     p_rawdv->p_audio_es->i_fourcc = VLC_FOURCC( 'd','v','a','u' );
310     p_rawdv->p_audio_es->i_cat = AUDIO_ES;
311     p_rawdv->p_audio_es->p_waveformatex = (void *)p_rawdv->p_wf;
312     input_SelectES( p_input, p_rawdv->p_audio_es );
313     vlc_mutex_unlock( &p_input->stream.stream_lock );
314
315     /* Init pcr */
316     p_rawdv->i_pcr = 0;
317
318     return 0;
319
320  error:
321     if( p_rawdv->p_bih ) free( p_rawdv->p_bih );
322     if( p_rawdv->p_wf ) free( p_rawdv->p_wf );
323     Deactivate( (vlc_object_t *)p_input );
324     return -1;
325 }
326
327 /*****************************************************************************
328  * Deactivate: frees unused data
329  *****************************************************************************/
330 static void Deactivate( vlc_object_t *p_this )
331 {
332     input_thread_t *p_input = (input_thread_t *)p_this;
333     demux_sys_t *p_rawdv = (demux_sys_t *)p_input->p_demux_data;
334
335     free( p_rawdv );
336 }
337
338 /*****************************************************************************
339  * Demux: reads and demuxes data packets
340  *****************************************************************************
341  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
342  *****************************************************************************/
343 static int Demux( input_thread_t * p_input )
344 {
345     demux_sys_t    *p_rawdv = (demux_sys_t *)p_input->p_demux_data;
346     pes_packet_t   *p_pes;
347     pes_packet_t   *p_audio_pes;
348     data_packet_t  *p_data;
349     ssize_t        i_read;
350
351     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
352     {
353         off_t i_pos;
354
355         msg_Warn( p_input, "synchro reinit" );
356
357         /* If the user tried to seek in the stream, we need to make sure
358          * the new position is at a DIF block boundary. */
359         vlc_mutex_lock( &p_input->stream.stream_lock );
360         i_pos= p_input->stream.p_selected_area->i_tell;
361         vlc_mutex_unlock( &p_input->stream.stream_lock );
362
363         if( (i_pos % p_rawdv->frame_size) &&
364             p_input->stream.b_seekable &&
365             p_input->stream.i_method == INPUT_METHOD_FILE )
366         {
367             p_input->pf_seek( p_input, (i_pos / p_rawdv->frame_size)
368                               * p_rawdv->frame_size );
369             input_AccessReinit( p_input );
370         }
371     }
372
373     /* Call the pace control */
374     input_ClockManageRef( p_input, p_input->stream.p_selected_program,
375                           p_rawdv->i_pcr );
376
377     i_read = input_SplitBuffer( p_input, &p_data, p_rawdv->frame_size );
378     if( i_read <= 0 )
379     {
380         return i_read;
381     }
382
383     /* Build video PES packet */
384     p_pes = input_NewPES( p_input->p_method_data );
385     if( p_pes == NULL )
386     {
387         msg_Err( p_input, "out of memory" );
388         input_DeletePacket( p_input->p_method_data, p_data );
389         return -1;
390     }
391
392     p_pes->i_rate = p_input->stream.control.i_rate;
393     p_pes->p_first = p_pes->p_last = p_data;
394     p_pes->i_pes_size = i_read;
395     p_pes->i_nb_data = 1;
396     p_pes->i_pts =
397         input_ClockGetTS( p_input, p_input->stream.p_selected_program,
398                           p_rawdv->i_pcr );
399
400     /* Do the same for audio */
401     p_audio_pes = input_NewPES( p_input->p_method_data );
402     if( p_pes == NULL )
403     {
404         msg_Err( p_input, "out of memory" );
405         input_DeletePacket( p_input->p_method_data, p_data );
406         return -1;
407     }
408     p_audio_pes->i_rate = p_input->stream.control.i_rate;
409     p_audio_pes->p_first = p_audio_pes->p_last =
410         input_ShareBuffer( p_input->p_method_data, p_data->p_buffer );
411     p_audio_pes->p_first->p_next = p_data->p_next;
412     p_audio_pes->p_first->p_payload_start = p_data->p_payload_start;
413     p_audio_pes->p_first->p_payload_end = p_data->p_payload_end;
414     p_audio_pes->i_pes_size = i_read;
415     p_audio_pes->i_nb_data = 1;
416     p_audio_pes->i_pts = p_pes->i_pts;
417
418     /* Decode PES packets */
419     input_DecodePES( p_rawdv->p_video_es->p_decoder_fifo, p_pes );
420     input_DecodePES( p_rawdv->p_audio_es->p_decoder_fifo, p_audio_pes );
421
422     p_rawdv->i_pcr += ( 90000 / p_rawdv->f_rate );
423
424     return 1;
425 }