1 /*****************************************************************************
2 * m4v.c : MPEG-4 Video demuxer
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: m4v.c,v 1.12 2004/03/03 20:39:52 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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>
31 #include "vlc_codec.h"
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Open ( vlc_object_t * );
37 static void Close ( vlc_object_t * );
40 set_description( _("MPEG-4 video demuxer" ) );
41 set_capability( "demux", 0 );
42 set_callbacks( Open, Close );
43 add_shortcut( "m4v" );
44 add_shortcut( "mp4v" );
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Demux ( input_thread_t * );
57 decoder_t *p_packetizer;
60 #define M4V_PACKET_SIZE 4096
62 /*****************************************************************************
63 * Open: initializes demux structures
64 *****************************************************************************/
65 static int Open( vlc_object_t * p_this )
67 input_thread_t *p_input = (input_thread_t *)p_this;
69 vlc_bool_t b_forced = VLC_FALSE;
75 if( stream_Peek( p_input->s, &p_peek, 4 ) < 4 )
77 msg_Err( p_input, "cannot peek" );
81 if( p_input->psz_demux &&
82 ( !strncmp( p_input->psz_demux, "mp4v", 4 ) ||
83 !strncmp( p_input->psz_demux, "m4v", 4 ) ) )
88 if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 || p_peek[3] > 0x2f )
92 msg_Warn( p_input, "m4v module discarded (no startcode)" );
96 msg_Err( p_input, "this doesn't look like an MPEG-4 ES stream, continuing" );
99 vlc_mutex_lock( &p_input->stream.stream_lock );
100 if( input_InitStream( p_input, 0 ) == -1)
102 vlc_mutex_unlock( &p_input->stream.stream_lock );
103 msg_Err( p_input, "cannot init stream" );
106 p_input->stream.i_mux_rate = 0 / 50;
107 vlc_mutex_unlock( &p_input->stream.stream_lock );
109 p_input->pf_demux = Demux;
110 p_input->pf_rewind = NULL;
111 p_input->pf_demux_control = demux_vaControlDefault;
113 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
118 * Load the mpegvideo packetizer
120 p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
121 p_sys->p_packetizer->pf_decode_audio = NULL;
122 p_sys->p_packetizer->pf_decode_video = NULL;
123 p_sys->p_packetizer->pf_decode_sub = NULL;
124 p_sys->p_packetizer->pf_packetize = NULL;
125 es_format_Init( &p_sys->p_packetizer->fmt_in, VIDEO_ES,
126 VLC_FOURCC( 'm', 'p', '4', 'v' ) );
127 es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
128 p_sys->p_packetizer->p_module =
129 module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
131 if( p_sys->p_packetizer->p_module == NULL)
133 vlc_object_destroy( p_sys->p_packetizer );
134 msg_Err( p_input, "cannot find mp4v packetizer" );
142 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
143 p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
148 /*****************************************************************************
149 * Close: frees unused data
150 *****************************************************************************/
151 static void Close( vlc_object_t * p_this )
153 input_thread_t *p_input = (input_thread_t*)p_this;
154 demux_sys_t *p_sys = p_input->p_demux_data;
156 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
157 vlc_object_destroy( p_sys->p_packetizer );
162 /*****************************************************************************
163 * Demux: reads and demuxes data packets
164 *****************************************************************************
165 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
166 *****************************************************************************/
167 static int Demux( input_thread_t * p_input )
169 demux_sys_t *p_sys = p_input->p_demux_data;
170 block_t *p_block_in, *p_block_out;
172 if( ( p_block_in = stream_Block( p_input->s, M4V_PACKET_SIZE ) ) == NULL )
177 /* m4v demuxer doesn't set pts/dts at all */
179 p_block_in->i_dts = 1;
181 while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
185 block_t *p_next = p_block_out->p_next;
187 input_ClockManageRef( p_input,
188 p_input->stream.p_selected_program,
191 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
193 p_block_out->i_pts = 0;
195 p_block_out->p_next = NULL;
196 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
198 p_block_out = p_next;
200 /* FIXME FIXME FIXME FIXME */
201 p_sys->i_dts += (mtime_t)90000 / 25;
202 /* FIXME FIXME FIXME FIXME */