]> git.sesse.net Git - vlc/blob - modules/demux/mjpeg.c
remove debug messages along the success path
[vlc] / modules / demux / mjpeg.c
1 /*****************************************************************************
2  * mjpeg.c : demuxes mjpeg webcam http streams
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: mjpeg.c 7196 2004-03-29 21:29:31Z fenrir $
6  *
7  * Authors: Henry Jen (slowhog) <henryjen@ztune.net>
8  *          Derk-Jan Hartman (thedj)
9  *          Sigmund Augdal (Dnumgis)
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34
35 #include <codecs.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 #define FPS_TEXT N_("Frames per Second")
44 #define FPS_LONGTEXT N_("Allows you to set the desired frame rate when " \
45     "playing from files, use 0 for live.")
46
47 vlc_module_begin();
48     set_description( _("JPEG camera demuxer") );
49     set_capability( "demux2", 5 );
50     set_callbacks( Open, Close );
51     add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
52 vlc_module_end();
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int MimeDemux( demux_t * );
58 static int MjpgDemux( demux_t * );
59 static int Control( demux_t *, int i_query, va_list args );
60
61 struct demux_sys_t
62 {
63     es_format_t     fmt;
64     es_out_id_t     *p_es;
65
66     mtime_t         i_time;
67     mtime_t         i_frame_length;
68     char            *psz_separator;
69     int             i_frame_size_estimate;
70     uint8_t         *p_peek;
71     int             i_data_peeked;
72 };
73
74 /*****************************************************************************
75  * Peek: Helper function to peek data with incremental size. 
76  * \return VLC_FALSE if peek no more data, VLC_TRUE otherwise.
77  *****************************************************************************/
78 static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
79 {
80     int i_data;
81     demux_sys_t *p_sys = p_demux->p_sys;
82
83     if( b_first )
84     {
85         p_sys->i_data_peeked = 0;
86     }
87     else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
88     {
89         p_sys->i_frame_size_estimate += 5120;
90     }
91     i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
92                           p_sys->i_frame_size_estimate );
93     if( i_data == p_sys->i_data_peeked )
94     {
95         msg_Warn( p_demux, "no more data" );
96         return VLC_FALSE;
97     }
98     p_sys->i_data_peeked = i_data;
99     if( i_data <= 0 )
100     {
101         msg_Warn( p_demux, "cannot peek data" );
102         return VLC_FALSE;
103     }
104     return VLC_TRUE;
105 }
106
107 /*****************************************************************************
108  * GetLine: Internal function used to dup a line of string from the buffer
109  *****************************************************************************/
110 static char* GetLine( demux_t *p_demux, int *p_pos )
111 {
112     demux_sys_t *p_sys = p_demux->p_sys;
113     uint8_t     *p_buf;
114     int         i_size;
115     int         i;
116     char        *p_line;
117
118     while( *p_pos > p_sys->i_data_peeked )
119     {
120         if( ! Peek( p_demux, VLC_FALSE ) )
121         {
122             return NULL;
123         }
124     }
125     p_buf = p_sys->p_peek + *p_pos;
126     i_size = p_sys->i_data_peeked - *p_pos;
127     i = 0;
128     while( p_buf[i] != '\n' )
129     {
130         i++;
131         if( i == i_size )
132         {
133             if( ! Peek( p_demux, VLC_FALSE ) )
134             {
135                 return NULL;
136             }
137         }
138         p_buf = p_sys->p_peek + *p_pos;
139         i_size = p_sys->i_data_peeked - *p_pos;
140     }
141     *p_pos += ( i + 1 );
142     if( i > 0 && '\r' == p_buf[i - 1] )
143     {
144         i--;
145     }
146     p_line = malloc( i + 1 );
147     if( NULL == p_line )
148     {
149         msg_Err( p_demux, "out of memory" );
150         return NULL;
151     }
152     strncpy ( p_line, p_buf, i );
153     p_line[i] = '\0';
154 //    msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
155     return p_line;
156 }
157
158 /*****************************************************************************
159  * CheckMimeHeader: Internal function used to verify and skip mime header
160  * \param p_header_size Return size of MIME header, 0 if no MIME header
161  * detected, minus value if error
162  * \return VLC_TRUE if content type is image/jpeg, VLC_FALSE otherwise
163  *****************************************************************************/
164 static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
165 {
166     vlc_bool_t  b_jpeg = VLC_FALSE;
167     int         i_pos;
168     char        *psz_line;
169     char        *p_ch;
170     demux_sys_t *p_sys = p_demux->p_sys;
171
172     if( !Peek( p_demux, VLC_TRUE ) )
173     {
174         msg_Err( p_demux, "cannot peek" );
175         *p_header_size = -1;
176         return VLC_FALSE;
177     }
178     if( p_sys->i_data_peeked < 3)
179     {
180         msg_Err( p_demux, "data shortage" );
181         *p_header_size = -2;
182         return VLC_FALSE;
183     }
184     if( strncmp( p_sys->p_peek, "--", 2 ) )
185     {
186         *p_header_size = 0;
187         return VLC_FALSE;
188     }
189     i_pos = 2;
190     psz_line = GetLine( p_demux, &i_pos );
191     if( NULL == psz_line )
192     {
193         msg_Err( p_demux, "no EOL" );
194         *p_header_size = -3;
195         return VLC_FALSE;
196     }
197     if( NULL == p_sys->psz_separator )
198     {
199         p_sys->psz_separator = psz_line;
200         msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
201                  p_sys->psz_separator );
202     }
203     else
204     {
205         if( strcmp( psz_line, p_sys->psz_separator ) )
206         {
207             msg_Warn( p_demux, "separator %s does not match %s", psz_line,
208                       p_sys->psz_separator );
209         }
210         free( psz_line );
211     }
212     psz_line = GetLine( p_demux, &i_pos );
213     while( psz_line && *psz_line )
214     {
215         if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
216         {
217             p_ch = psz_line + 13;
218             while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
219             if( strncasecmp( p_ch, "image/jpeg", 10 ) )
220             {
221                 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
222                 b_jpeg = VLC_FALSE;
223             }
224             else
225             {
226                 b_jpeg = VLC_TRUE;
227             }
228         }
229         else
230         {
231             msg_Dbg( p_demux, "Discard MIME header: %s", psz_line );
232         }
233         free( psz_line );
234         psz_line = GetLine( p_demux, &i_pos );
235     }
236
237     if( NULL == psz_line )
238     {
239         msg_Err( p_demux, "no EOL" );
240         *p_header_size = -3;
241         return VLC_FALSE;
242     }
243
244     free( psz_line );
245
246     *p_header_size = i_pos;
247     return b_jpeg;
248 }
249
250 static int SendBlock( demux_t *p_demux, int i )
251 {
252     demux_sys_t *p_sys = p_demux->p_sys;
253     block_t     *p_block;
254
255     if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
256     {
257         msg_Warn( p_demux, "cannot read data" );
258         return 0;
259     }
260
261     if( !p_sys->i_frame_length || !p_sys->i_time )
262     {
263         p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
264     }
265     else
266     {
267         p_block->i_dts = p_block->i_pts = p_sys->i_time;
268         p_sys->i_time += p_sys->i_frame_length;
269     }
270
271     /* set PCR */
272     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
273     es_out_Send( p_demux->out, p_sys->p_es, p_block );
274     return 1;
275 }
276
277 /*****************************************************************************
278  * Open: check file and initializes structures
279  *****************************************************************************/
280 static int Open( vlc_object_t * p_this )
281 {
282     demux_t     *p_demux = (demux_t*)p_this;
283     demux_sys_t *p_sys;
284     int         i_size;
285     int         b_matched = VLC_FALSE;
286     vlc_value_t val;
287
288     p_demux->pf_control = Control;
289     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
290     p_sys->p_es         = NULL;
291     p_sys->i_time       = 0;
292
293     var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
294     var_Get( p_demux, "mjpeg-fps", &val );
295     p_sys->i_frame_length = 0;
296     if( val.f_float )
297     {
298         p_sys->i_frame_length = 1000000.0 / val.f_float;
299     }
300
301     p_sys->psz_separator = NULL;
302     p_sys->i_frame_size_estimate = 15 * 1024;
303
304     b_matched = CheckMimeHeader( p_demux, &i_size);
305     if( b_matched )
306     {
307         p_demux->pf_demux = MimeDemux;
308         stream_Read( p_demux->s, NULL, i_size );
309     }
310     else if( 0 == i_size )
311     {
312         /* 0xffd8 identify a JPEG SOI */
313         if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
314         {
315             msg_Dbg( p_demux, "JPEG SOI marker detected" );
316             p_demux->pf_demux = MjpgDemux;
317         }
318         else
319         {
320             goto error;
321         }
322     }
323     else
324     {
325         goto error;
326     }
327
328     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
329     p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
330
331     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
332     return VLC_SUCCESS;
333
334 error:
335     msg_Warn( p_demux, "JPEG camera module discarded" );
336     free( p_sys );
337     return VLC_EGENERIC;
338 }
339
340 /*****************************************************************************
341  * Demux: read packet and send them to decoders
342  *****************************************************************************
343  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
344  *****************************************************************************/
345 static int MjpgDemux( demux_t *p_demux )
346 {
347     demux_sys_t *p_sys = p_demux->p_sys;
348     int         i;
349
350     if( !Peek( p_demux, VLC_TRUE ) )
351     {
352         msg_Warn( p_demux, "cannot peek data" );
353         return 0;
354     }
355     if( p_sys->i_data_peeked < 4 )
356     {
357         msg_Warn( p_demux, "data shortage" );
358         return 0;
359     }
360     i = 3;
361     while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
362     {
363         i++;
364         if( i >= p_sys->i_data_peeked )
365         {
366             msg_Dbg( p_demux, "Did not find JPEG EOI in %d bytes",
367                      p_sys->i_data_peeked );
368             if( !Peek( p_demux, VLC_FALSE ) )
369             {
370                 msg_Warn( p_demux, "No more data is available at the moment" );
371                 return 0;
372             }
373         }
374     }
375     i++;
376     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
377     return SendBlock( p_demux, i );
378 }
379
380 static int MimeDemux( demux_t *p_demux )
381 {
382     demux_sys_t *p_sys = p_demux->p_sys;
383     int         i_size, i;
384     vlc_bool_t  b_match;
385     vlc_bool_t  b_done;
386
387     b_match = CheckMimeHeader( p_demux, &i_size );
388     if( i_size > 0 )
389     {
390         stream_Read( p_demux->s, NULL, i_size );
391     }
392     else if( i_size < 0 )
393     {
394         return 0;
395     }
396     else
397     {
398         // No MIME header, assume OK
399         b_match = VLC_TRUE;
400     }
401
402     if( !Peek( p_demux, VLC_TRUE ) )
403     {
404         msg_Warn( p_demux, "cannot peek data" );
405         return 0;
406     }
407     i = 0;
408     i_size = strlen( p_sys->psz_separator ) + 2;
409     if( p_sys->i_data_peeked < i_size )
410     {
411         msg_Warn( p_demux, "data shortage" );
412         return 0;
413     }
414     b_done = VLC_FALSE;
415     while( !b_done )
416     {
417         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
418         {
419             i++;
420             i_size++;
421             if( i_size >= p_sys->i_data_peeked )
422             {
423                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
424                          "data", p_sys->i_data_peeked );
425
426                 if( !Peek( p_demux, VLC_FALSE ) )
427                 {
428                     msg_Warn( p_demux, "No more data is available at the "
429                               "moment" );
430                     return 0;
431                 }
432             }
433         }
434         if( !strncmp( p_sys->psz_separator, p_sys->p_peek + i + 2,
435                       strlen( p_sys->psz_separator ) ) )
436         {
437             b_done = VLC_TRUE;
438         }
439         else
440         {
441             i++;
442             i_size++;
443         }
444     }
445
446     if( !b_match )
447     {
448         msg_Err( p_demux, "Discard non-JPEG part" );
449         stream_Read( p_demux->s, NULL, i );
450         return 0;
451     }
452
453     return SendBlock( p_demux, i );
454 }
455
456 /*****************************************************************************
457  * Close: frees unused data
458  *****************************************************************************/
459 static void Close ( vlc_object_t * p_this )
460 {
461     demux_t     *p_demux = (demux_t*)p_this;
462     demux_sys_t *p_sys  = p_demux->p_sys;
463
464     if( p_sys->psz_separator )
465     {
466         free( p_sys->psz_separator );
467     }
468     free( p_sys );
469 }
470
471 /*****************************************************************************
472  * Control:
473  *****************************************************************************/
474 static int Control( demux_t *p_demux, int i_query, va_list args )
475 {
476     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
477 }