]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/m4v.c
* ALL: changed the prototype of module_Need() to accept a "strict" boolean argument.
[vlc] / modules / demux / mpeg / m4v.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include "vlc_codec.h"
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int  Open    ( vlc_object_t * );
37 static void Close  ( vlc_object_t * );
38
39 vlc_module_begin();
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" );
45 vlc_module_end();
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  Demux       ( input_thread_t * );
51
52 struct demux_sys_t
53 {
54     mtime_t     i_dts;
55     es_out_id_t *p_es;
56
57     decoder_t *p_packetizer;
58 };
59
60 #define M4V_PACKET_SIZE 4096
61
62 /*****************************************************************************
63  * Open: initializes demux structures
64  *****************************************************************************/
65 static int Open( vlc_object_t * p_this )
66 {
67     input_thread_t *p_input = (input_thread_t *)p_this;
68     demux_sys_t    *p_sys;
69     vlc_bool_t     b_forced = VLC_FALSE;
70
71     uint8_t        *p_peek;
72
73     es_format_t    fmt;
74
75     if( stream_Peek( p_input->s, &p_peek, 4 ) < 4 )
76     {
77         msg_Err( p_input, "cannot peek" );
78         return VLC_EGENERIC;
79     }
80
81     if( p_input->psz_demux &&
82         ( !strncmp( p_input->psz_demux, "mp4v", 4 ) ||
83           !strncmp( p_input->psz_demux, "m4v", 4 ) ) )
84     {
85         b_forced = VLC_TRUE;
86     }
87
88     if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 || p_peek[3] > 0x2f )
89     {
90         if( !b_forced )
91         {
92             msg_Warn( p_input, "m4v module discarded (no startcode)" );
93             return VLC_EGENERIC;
94         }
95
96         msg_Err( p_input, "this doesn't look like an MPEG-4 ES stream, continuing" );
97     }
98
99     vlc_mutex_lock( &p_input->stream.stream_lock );
100     if( input_InitStream( p_input, 0 ) == -1)
101     {
102         vlc_mutex_unlock( &p_input->stream.stream_lock );
103         msg_Err( p_input, "cannot init stream" );
104         return VLC_EGENERIC;
105     }
106     p_input->stream.i_mux_rate = 0 / 50;
107     vlc_mutex_unlock( &p_input->stream.stream_lock );
108
109     p_input->pf_demux  = Demux;
110     p_input->pf_rewind = NULL;
111     p_input->pf_demux_control = demux_vaControlDefault;
112
113     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
114     p_sys->p_es    = NULL;
115     p_sys->i_dts   = 0;
116
117     /*
118      * Load the mpegvideo packetizer
119      */
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 );
130
131     if( p_sys->p_packetizer->p_module == NULL)
132     {
133         vlc_object_destroy( p_sys->p_packetizer );
134         msg_Err( p_input, "cannot find mp4v packetizer" );
135         free( p_sys );
136         return VLC_EGENERIC;
137     }
138
139     /*
140      * create the output
141      */
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 );
144
145     return VLC_SUCCESS;
146 }
147
148 /*****************************************************************************
149  * Close: frees unused data
150  *****************************************************************************/
151 static void Close( vlc_object_t * p_this )
152 {
153     input_thread_t *p_input = (input_thread_t*)p_this;
154     demux_sys_t    *p_sys = p_input->p_demux_data;
155
156     module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
157     vlc_object_destroy( p_sys->p_packetizer );
158
159     free( p_sys );
160 }
161
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 )
168 {
169     demux_sys_t  *p_sys = p_input->p_demux_data;
170     block_t *p_block_in, *p_block_out;
171
172     if( ( p_block_in = stream_Block( p_input->s, M4V_PACKET_SIZE ) ) == NULL )
173     {
174         return 0;
175     }
176
177     /* m4v demuxer doesn't set pts/dts at all */
178     p_block_in->i_pts =
179     p_block_in->i_dts = 1;
180
181     while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in )) )
182     {
183         while( p_block_out )
184         {
185             block_t *p_next = p_block_out->p_next;
186
187             input_ClockManageRef( p_input,
188                                   p_input->stream.p_selected_program,
189                                   p_sys->i_dts );
190             p_block_out->i_dts =
191                 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
192                                   p_sys->i_dts );
193             p_block_out->i_pts = 0;
194
195             p_block_out->p_next = NULL;
196             es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
197
198             p_block_out = p_next;
199
200             /* FIXME FIXME FIXME FIXME */
201             p_sys->i_dts += (mtime_t)90000 / 25;
202             /* FIXME FIXME FIXME FIXME */
203         }
204     }
205     return 1;
206 }
207