1 /*****************************************************************************
2 * rawdv.c : raw dv input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: rawdv.c,v 1.11 2003/11/24 00:39:01 fenrir Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
32 #include <vlc/input.h>
34 #include <sys/types.h>
36 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
38 /*****************************************************************************
39 A little bit of background information (copied over from libdv glossary).
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.
44 - Video Section: Each DIF sequence contains a video section, consisting of
45 135 DIF blocks, which are further subdivided into Video Segments.
47 - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
48 to a single compressed macroblock.
50 *****************************************************************************/
53 /*****************************************************************************
55 *****************************************************************************/
56 #define DV_PAL_FRAME_SIZE 144000
57 #define DV_NTSC_FRAME_SIZE 122000
59 /*****************************************************************************
60 * Definitions of structures used by this plugin
61 *****************************************************************************/
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) */
70 int dsf; /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
84 es_descriptor_t *p_video_es;
85 es_descriptor_t *p_audio_es;
87 /* codec specific stuff */
88 BITMAPINFOHEADER *p_bih;
94 /* program clock reference (in units of 90kHz) */
98 /*****************************************************************************
100 *****************************************************************************/
101 static int Activate ( vlc_object_t * );
102 static void Deactivate( vlc_object_t * );
103 static int Demux ( input_thread_t * );
105 /*****************************************************************************
107 *****************************************************************************/
109 set_description( _("raw dv demuxer") );
110 set_capability( "demux", 2 );
111 set_callbacks( Activate, NULL );
112 add_shortcut( "rawdv" );
115 /*****************************************************************************
116 * Activate: initializes raw dv demux structures
117 *****************************************************************************/
118 static int Activate( vlc_object_t * p_this )
120 input_thread_t *p_input = (input_thread_t *)p_this;
121 byte_t *p_peek, *p_peek_backup;
123 demux_sys_t *p_rawdv;
124 dv_header_t dv_header;
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. */
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") ) )
141 p_input->pf_demux = Demux;
142 p_input->pf_demux_control = demux_vaControlDefault;
144 /* Have a peep at the show. */
145 if( input_Peek(p_input, &p_peek, DV_PAL_FRAME_SIZE) < DV_NTSC_FRAME_SIZE )
147 /* Stream too short ... */
148 msg_Err( p_input, "cannot peek()" );
151 p_peek_backup = p_peek;
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);
157 dv_id.dsn = i_dword >> (32 - 4);
159 dv_id.fsc = i_dword >> (32 - 1);
161 dv_id.dbn = i_dword >> (32 - 8);
166 msg_Warn( p_input, "not a raw dv stream header" );
170 /* fill in the dv_header_t structure */
171 dv_header.dsf = i_dword >> (32 - 1);
173 if( i_dword >> (32 - 1) ) /* incorrect bit */
175 msg_Warn( p_input, "incorrect bit" );
179 i_dword = GetDWBE( p_peek ); p_peek += 4;
181 dv_header.apt = i_dword >> (32 - 3);
183 dv_header.tf1 = i_dword >> (32 - 1);
185 dv_header.ap1 = i_dword >> (32 - 3);
187 dv_header.tf2 = i_dword >> (32 - 1);
189 dv_header.ap2 = i_dword >> (32 - 3);
191 dv_header.tf3 = i_dword >> (32 - 1);
193 dv_header.ap3 = i_dword >> (32 - 3);
195 p_peek += 72; /* skip rest of DIF block */
198 /* Setup the structures for our demuxer */
199 if( !( p_rawdv = malloc( sizeof( demux_sys_t ) ) ) )
201 msg_Err( p_input, "out of memory" );
204 memset( p_rawdv, 0, sizeof( demux_sys_t ) );
205 p_rawdv->p_bih = NULL;
206 p_rawdv->p_wf = NULL;
207 p_input->p_demux_data = p_rawdv;
209 p_rawdv->p_bih = (BITMAPINFOHEADER *) malloc( sizeof(BITMAPINFOHEADER) );
210 if( !p_rawdv->p_bih )
212 msg_Err( p_input, "out of memory" );
215 p_rawdv->p_bih->biSize = sizeof(BITMAPINFOHEADER);
216 p_rawdv->p_bih->biCompression = VLC_FOURCC( 'd','v','s','d' );
217 p_rawdv->p_bih->biSize = 40;
218 p_rawdv->p_bih->biWidth = 720;
219 p_rawdv->p_bih->biHeight = dv_header.dsf ? 576 : 480;
220 p_rawdv->p_bih->biPlanes = 1;
221 p_rawdv->p_bih->biBitCount = 24;
222 p_rawdv->p_bih->biSizeImage =
223 p_rawdv->p_bih->biWidth * p_rawdv->p_bih->biHeight
224 * (p_rawdv->p_bih->biBitCount >> 3);
226 /* Properties of our video */
229 p_rawdv->frame_size = 12 * 150 * 80;
230 p_rawdv->f_rate = 25;
234 p_rawdv->frame_size = 10 * 150 * 80;
235 p_rawdv->f_rate = 29.97;
240 p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
242 if( *p_peek != 0x50 || *p_peek != 0x51 )
244 msg_Err( p_input, "AAUX should begin with 0x50" );
248 p_rawdv->p_wf = (WAVEFORMATEX *)malloc( sizeof(WAVEFORMATEX) );
251 msg_Err( p_input, "out of memory" );
255 p_rawdv->p_wf->wFormatTag = 0;
256 p_rawdv->p_wf->nChannels = 2;
257 p_rawdv->p_wf->nSamplesPerSec = 44100; /* FIXME */
258 p_rawdv->p_wf->nAvgBytesPerSec = p_rawdv->f_rate * p_rawdv->frame_size;
259 p_rawdv->p_wf->nBlockAlign = p_rawdv->frame_size;
260 p_rawdv->p_wf->wBitsPerSample = 16;
261 p_rawdv->p_wf->cbSize = 0;
264 /* necessary because input_SplitBuffer() will only get
265 * INPUT_DEFAULT_BUFSIZE bytes at a time. */
266 p_input->i_bufsize = p_rawdv->frame_size;
268 /* Create one program */
269 vlc_mutex_lock( &p_input->stream.stream_lock );
270 if( input_InitStream( p_input, 0 ) == -1)
272 vlc_mutex_unlock( &p_input->stream.stream_lock );
273 msg_Err( p_input, "cannot init stream" );
276 if( input_AddProgram( p_input, 0, 0) == NULL )
278 vlc_mutex_unlock( &p_input->stream.stream_lock );
279 msg_Err( p_input, "cannot add program" );
282 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
283 p_input->stream.i_mux_rate = p_rawdv->frame_size * p_rawdv->f_rate;
284 vlc_mutex_unlock( &p_input->stream.stream_lock );
286 /* Add video stream */
287 vlc_mutex_lock( &p_input->stream.stream_lock );
288 p_rawdv->p_video_es = input_AddES( p_input,
289 p_input->stream.p_selected_program,
290 1, VIDEO_ES, NULL, 0 );
291 p_rawdv->p_video_es->i_stream_id = 0;
292 p_rawdv->p_video_es->i_fourcc = VLC_FOURCC( 'd','v','s','d' );
293 p_rawdv->p_video_es->p_bitmapinfoheader = (void *)p_rawdv->p_bih;
294 input_SelectES( p_input, p_rawdv->p_video_es );
295 vlc_mutex_unlock( &p_input->stream.stream_lock );
297 /* Add audio stream */
298 vlc_mutex_lock( &p_input->stream.stream_lock );
299 p_rawdv->p_audio_es = input_AddES( p_input,
300 p_input->stream.p_selected_program,
301 2, AUDIO_ES, NULL, 0 );
302 p_rawdv->p_audio_es->i_stream_id = 1;
303 p_rawdv->p_audio_es->i_fourcc = VLC_FOURCC( 'd','v','a','u' );
304 p_rawdv->p_audio_es->p_waveformatex = (void *)p_rawdv->p_wf;
305 input_SelectES( p_input, p_rawdv->p_audio_es );
306 vlc_mutex_unlock( &p_input->stream.stream_lock );
314 if( p_rawdv->p_bih ) free( p_rawdv->p_bih );
315 if( p_rawdv->p_wf ) free( p_rawdv->p_wf );
316 Deactivate( (vlc_object_t *)p_input );
320 /*****************************************************************************
321 * Deactivate: frees unused data
322 *****************************************************************************/
323 static void Deactivate( vlc_object_t *p_this )
325 input_thread_t *p_input = (input_thread_t *)p_this;
326 demux_sys_t *p_rawdv = (demux_sys_t *)p_input->p_demux_data;
331 /*****************************************************************************
332 * Demux: reads and demuxes data packets
333 *****************************************************************************
334 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
335 *****************************************************************************/
336 static int Demux( input_thread_t * p_input )
338 demux_sys_t *p_rawdv = (demux_sys_t *)p_input->p_demux_data;
339 pes_packet_t *p_pes = NULL;
340 pes_packet_t *p_audio_pes = NULL;
341 data_packet_t *p_data;
344 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
348 msg_Warn( p_input, "synchro reinit" );
350 /* If the user tried to seek in the stream, we need to make sure
351 * the new position is at a DIF block boundary. */
352 vlc_mutex_lock( &p_input->stream.stream_lock );
353 i_pos= p_input->stream.p_selected_area->i_tell;
354 vlc_mutex_unlock( &p_input->stream.stream_lock );
356 if( (i_pos % p_rawdv->frame_size) &&
357 p_input->stream.b_seekable &&
358 p_input->stream.i_method == INPUT_METHOD_FILE )
360 p_input->pf_seek( p_input, (i_pos / p_rawdv->frame_size)
361 * p_rawdv->frame_size );
362 input_AccessReinit( p_input );
366 /* Call the pace control */
367 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
370 i_read = input_SplitBuffer( p_input, &p_data, p_rawdv->frame_size );
376 /* Build video PES packet */
377 if( p_rawdv->p_video_es->p_dec )
379 p_pes = input_NewPES( p_input->p_method_data );
382 msg_Err( p_input, "out of memory" );
383 input_DeletePacket( p_input->p_method_data, p_data );
387 p_pes->i_rate = p_input->stream.control.i_rate;
388 p_pes->p_first = p_pes->p_last = p_data;
389 p_pes->i_pes_size = i_read;
390 p_pes->i_nb_data = 1;
392 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
396 /* Do the same for audio */
397 if( p_rawdv->p_audio_es->p_dec )
399 p_audio_pes = input_NewPES( p_input->p_method_data );
400 if( p_audio_pes == NULL )
402 msg_Err( p_input, "out of memory" );
403 input_DeletePacket( p_input->p_method_data, p_data );
406 p_audio_pes->i_rate = p_input->stream.control.i_rate;
408 if( p_rawdv->p_video_es->p_dec )
409 p_audio_pes->p_first = p_audio_pes->p_last =
410 input_ShareBuffer( p_input->p_method_data, p_data->p_buffer );
412 p_audio_pes->p_first = p_audio_pes->p_last = p_data;
414 p_audio_pes->p_first->p_next = p_data->p_next;
415 p_audio_pes->p_first->p_payload_start = p_data->p_payload_start;
416 p_audio_pes->p_first->p_payload_end = p_data->p_payload_end;
417 p_audio_pes->i_pes_size = i_read;
418 p_audio_pes->i_nb_data = 1;
420 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
424 /* Decode PES packets if stream is selected */
425 if( p_rawdv->p_video_es->p_dec )
426 input_DecodePES( p_rawdv->p_video_es->p_dec, p_pes );
427 if( p_rawdv->p_audio_es->p_dec )
428 input_DecodePES( p_rawdv->p_audio_es->p_dec, p_audio_pes );
430 p_rawdv->i_pcr += ( 90000 / p_rawdv->f_rate );
432 if( !p_rawdv->p_video_es->p_dec &&
433 !p_rawdv->p_audio_es->p_dec )
434 input_DeletePacket( p_input->p_method_data, p_data );