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 $
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() */
30 #include <vlc/input.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
39 set_description( _("raw dv demuxer") );
40 set_capability( "demux", 2 );
41 set_callbacks( Open, Close );
42 add_shortcut( "rawdv" );
46 /*****************************************************************************
47 A little bit of background information (copied over from libdv glossary).
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.
52 - Video Section: Each DIF sequence contains a video section, consisting of
53 135 DIF blocks, which are further subdivided into Video Segments.
55 - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
56 to a single compressed macroblock.
58 *****************************************************************************/
61 /*****************************************************************************
63 *****************************************************************************/
64 #define DV_PAL_FRAME_SIZE 144000
65 #define DV_NTSC_FRAME_SIZE 122000
67 /*****************************************************************************
68 * Definitions of structures used by this plugin
69 *****************************************************************************/
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) */
78 int dsf; /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
92 es_out_id_t *p_es_video;
93 es_format_t fmt_video;
95 es_out_id_t *p_es_audio;
96 es_format_t fmt_audio;
101 /* program clock reference (in units of 90kHz) */
105 /*****************************************************************************
107 *****************************************************************************/
108 static int Demux ( input_thread_t * );
110 /*****************************************************************************
111 * Open: initializes raw dv demux structures
112 *****************************************************************************/
113 static int Open( vlc_object_t * p_this )
115 input_thread_t *p_input = (input_thread_t *)p_this;
118 byte_t *p_peek, *p_peek_backup;
121 dv_header_t dv_header;
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. */
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") ) )
138 if( stream_Peek( p_input->s, &p_peek, DV_PAL_FRAME_SIZE ) < DV_NTSC_FRAME_SIZE )
140 /* Stream too short ... */
141 msg_Err( p_input, "cannot peek()" );
144 p_peek_backup = p_peek;
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);
150 dv_id.dsn = i_dword >> (32 - 4);
152 dv_id.fsc = i_dword >> (32 - 1);
154 dv_id.dbn = i_dword >> (32 - 8);
159 msg_Warn( p_input, "not a raw dv stream header" );
163 /* fill in the dv_header_t structure */
164 dv_header.dsf = i_dword >> (32 - 1);
166 if( i_dword >> (32 - 1) ) /* incorrect bit */
168 msg_Warn( p_input, "incorrect bit" );
172 i_dword = GetDWBE( p_peek ); p_peek += 4;
174 dv_header.apt = i_dword >> (32 - 3);
176 dv_header.tf1 = i_dword >> (32 - 1);
178 dv_header.ap1 = i_dword >> (32 - 3);
180 dv_header.tf2 = i_dword >> (32 - 1);
182 dv_header.ap2 = i_dword >> (32 - 3);
184 dv_header.tf3 = i_dword >> (32 - 1);
186 dv_header.ap3 = i_dword >> (32 - 3);
188 p_peek += 72; /* skip rest of DIF block */
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 ) );
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;
200 p_sys->p_es_video = NULL;
201 p_sys->p_es_audio = NULL;
203 p_sys->i_bitrate = 0;
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;;
211 p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
213 if( *p_peek != 0x50 || *p_peek != 0x51 )
215 msg_Err( p_input, "AAUX should begin with 0x50" );
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; /* ??? */
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;
230 vlc_mutex_lock( &p_input->stream.stream_lock );
231 if( input_InitStream( p_input, 0 ) == -1)
233 vlc_mutex_unlock( &p_input->stream.stream_lock );
234 msg_Err( p_input, "cannot init stream" );
238 p_input->stream.i_mux_rate = p_sys->frame_size * p_sys->f_rate;
239 vlc_mutex_unlock( &p_input->stream.stream_lock );
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 );
247 /*****************************************************************************
248 * Close: frees unused data
249 *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
252 input_thread_t *p_input = (input_thread_t *)p_this;
253 demux_sys_t *p_sys = p_input->p_demux_data;
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 )
265 demux_sys_t *p_sys = p_input->p_demux_data;
267 vlc_bool_t b_audio, b_video;
270 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
272 off_t i_pos = stream_Tell( p_input->s );
274 msg_Warn( p_input, "synchro reinit" );
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 )
280 i_pos += p_sys->frame_size - i_pos % p_sys->frame_size;
282 if( stream_Seek( p_input->s, i_pos ) )
284 msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
290 /* Call the pace control */
291 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
294 if( ( p_block = stream_Block( p_input->s, p_sys->frame_size ) ) == NULL )
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 );
306 p_block->i_pts = input_ClockGetTS( p_input,
307 p_input->stream.p_selected_program,
309 if( b_audio && b_video )
311 block_t *p_dup = block_Duplicate( p_block );
313 es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block );
316 es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_dup );
321 es_out_Send( p_input->p_es_out, p_sys->p_es_audio, p_block );
325 es_out_Send( p_input->p_es_out, p_sys->p_es_video, p_block );
329 block_Release( p_block );
332 p_sys->i_pcr += ( 90000 / p_sys->f_rate );