]> git.sesse.net Git - vlc/blob - modules/demux/mjpeg.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / demux / mjpeg.c
1 /*****************************************************************************
2  * mjpeg.c : demuxes mjpeg webcam http streams
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include <vlc/vlc.h>
32 #include <vlc_demux.h>
33
34 #include <vlc_codecs.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define FPS_TEXT N_("Frames per Second")
43 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
44     "playing MJPEG from a file. Use 0 (this is the default value) for a " \
45     "live stream (from a camera).")
46
47 vlc_module_begin();
48     set_shortname( "MJPEG");
49     set_description( _("M-JPEG camera demuxer") );
50     set_capability( "demux2", 5 );
51     set_callbacks( Open, Close );
52     set_category( CAT_INPUT );
53     set_subcategory( SUBCAT_INPUT_DEMUX );
54     add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
55         change_safe();
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int MimeDemux( demux_t * );
62 static int MjpgDemux( demux_t * );
63 static int Control( demux_t *, int i_query, va_list args );
64
65 struct demux_sys_t
66 {
67     es_format_t     fmt;
68     es_out_id_t     *p_es;
69
70     vlc_bool_t      b_still;
71     mtime_t         i_still_end;
72     mtime_t         i_still_length;
73
74     mtime_t         i_time;
75     mtime_t         i_frame_length;
76     char            *psz_separator;
77     int             i_frame_size_estimate;
78     const uint8_t   *p_peek;
79     int             i_data_peeked;
80 };
81
82 /*****************************************************************************
83  * Peek: Helper function to peek data with incremental size.
84  * \return VLC_FALSE if peek no more data, VLC_TRUE otherwise.
85  *****************************************************************************/
86 static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
87 {
88     int i_data;
89     demux_sys_t *p_sys = p_demux->p_sys;
90
91     if( b_first )
92     {
93         p_sys->i_data_peeked = 0;
94     }
95     else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
96     {
97         p_sys->i_frame_size_estimate += 5120;
98     }
99     i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
100                           p_sys->i_frame_size_estimate );
101     if( i_data == p_sys->i_data_peeked )
102     {
103         msg_Warn( p_demux, "no more data" );
104         return VLC_FALSE;
105     }
106     p_sys->i_data_peeked = i_data;
107     if( i_data <= 0 )
108     {
109         msg_Warn( p_demux, "cannot peek data" );
110         return VLC_FALSE;
111     }
112     return VLC_TRUE;
113 }
114
115 /*****************************************************************************
116  * GetLine: Internal function used to dup a line of string from the buffer
117  *****************************************************************************/
118 static char* GetLine( demux_t *p_demux, int *p_pos )
119 {
120     demux_sys_t *p_sys = p_demux->p_sys;
121     const uint8_t *p_buf;
122     int         i_size;
123     int         i;
124     char        *p_line;
125
126     while( *p_pos > p_sys->i_data_peeked )
127     {
128         if( ! Peek( p_demux, VLC_FALSE ) )
129         {
130             return NULL;
131         }
132     }
133     p_buf = p_sys->p_peek + *p_pos;
134     i_size = p_sys->i_data_peeked - *p_pos;
135     i = 0;
136     while( p_buf[i] != '\n' )
137     {
138         i++;
139         if( i == i_size )
140         {
141             if( ! Peek( p_demux, VLC_FALSE ) )
142             {
143                 return NULL;
144             }
145         }
146         p_buf = p_sys->p_peek + *p_pos;
147         i_size = p_sys->i_data_peeked - *p_pos;
148     }
149     *p_pos += ( i + 1 );
150     if( i > 0 && '\r' == p_buf[i - 1] )
151     {
152         i--;
153     }
154     p_line = malloc( i + 1 );
155     if( NULL == p_line )
156     {
157         msg_Err( p_demux, "out of memory" );
158         return NULL;
159     }
160     strncpy ( p_line, (char*)p_buf, i );
161     p_line[i] = '\0';
162 //    msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
163     return p_line;
164 }
165
166 /*****************************************************************************
167  * CheckMimeHeader: Internal function used to verify and skip mime header
168  * \param p_header_size Return size of MIME header, 0 if no MIME header
169  * detected, minus value if error
170  * \return VLC_TRUE if content type is image/jpeg, VLC_FALSE otherwise
171  *****************************************************************************/
172 static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
173 {
174     vlc_bool_t  b_jpeg = VLC_FALSE;
175     int         i_pos;
176     char        *psz_line;
177     char        *p_ch;
178     demux_sys_t *p_sys = p_demux->p_sys;
179
180     if( !Peek( p_demux, VLC_TRUE ) )
181     {
182         msg_Err( p_demux, "cannot peek" );
183         *p_header_size = -1;
184         return VLC_FALSE;
185     }
186     if( p_sys->i_data_peeked < 3)
187     {
188         msg_Err( p_demux, "data shortage" );
189         *p_header_size = -2;
190         return VLC_FALSE;
191     }
192     if( strncmp( (char *)p_sys->p_peek, "--", 2 ) )
193     {
194         *p_header_size = 0;
195         return VLC_FALSE;
196     }
197     i_pos = 2;
198     psz_line = GetLine( p_demux, &i_pos );
199     if( NULL == psz_line )
200     {
201         msg_Err( p_demux, "no EOL" );
202         *p_header_size = -3;
203         return VLC_FALSE;
204     }
205
206     /* Read the separator and remember it if not yet stored */
207     if( p_sys->psz_separator == NULL )
208     {
209         p_sys->psz_separator = psz_line;
210         msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
211                  p_sys->psz_separator );
212     }
213     else
214     {
215         if( strcmp( psz_line, p_sys->psz_separator ) )
216         {
217             msg_Warn( p_demux, "separator %s does not match %s", psz_line,
218                       p_sys->psz_separator );
219         }
220         free( psz_line );
221     }
222
223     psz_line = GetLine( p_demux, &i_pos );
224     while( psz_line && *psz_line )
225     {
226         if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
227         {
228             p_ch = psz_line + 13;
229             while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
230             if( strncasecmp( p_ch, "image/jpeg", 10 ) )
231             {
232                 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
233                 b_jpeg = VLC_FALSE;
234             }
235             else
236             {
237                 b_jpeg = VLC_TRUE;
238             }
239         }
240         else
241         {
242             msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
243         }
244         free( psz_line );
245         psz_line = GetLine( p_demux, &i_pos );
246     }
247
248     if( NULL == psz_line )
249     {
250         msg_Err( p_demux, "no EOL" );
251         *p_header_size = -3;
252         return VLC_FALSE;
253     }
254
255     free( psz_line );
256
257     *p_header_size = i_pos;
258     return b_jpeg;
259 }
260
261 static int SendBlock( demux_t *p_demux, int i )
262 {
263     demux_sys_t *p_sys = p_demux->p_sys;
264     block_t     *p_block;
265
266     if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
267     {
268         msg_Warn( p_demux, "cannot read data" );
269         return 0;
270     }
271
272     if( !p_sys->i_frame_length || !p_sys->i_time )
273     {
274         p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
275     }
276     else
277     {
278         p_block->i_dts = p_block->i_pts = p_sys->i_time;
279         p_sys->i_time += p_sys->i_frame_length;
280     }
281
282     /* set PCR */
283     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
284     es_out_Send( p_demux->out, p_sys->p_es, p_block );
285
286     if( p_sys->b_still )
287     {
288         p_sys->i_still_end = mdate() + p_sys->i_still_length;
289     }
290
291     return 1;
292 }
293
294 /*****************************************************************************
295  * Open: check file and initializes structures
296  *****************************************************************************/
297 static int Open( vlc_object_t * p_this )
298 {
299     demux_t     *p_demux = (demux_t*)p_this;
300     demux_sys_t *p_sys;
301     int         i_size;
302     int         b_matched = VLC_FALSE;
303     vlc_value_t val;
304
305     p_demux->pf_control = Control;
306     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
307     p_sys->p_es         = NULL;
308     p_sys->i_time       = 0;
309
310     p_sys->psz_separator = NULL;
311     p_sys->i_frame_size_estimate = 15 * 1024;
312
313     b_matched = CheckMimeHeader( p_demux, &i_size);
314     if( b_matched )
315     {
316         p_demux->pf_demux = MimeDemux;
317         stream_Read( p_demux->s, NULL, i_size );
318     }
319     else if( 0 == i_size )
320     {
321         /* 0xffd8 identify a JPEG SOI */
322         if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
323         {
324             msg_Dbg( p_demux, "JPEG SOI marker detected" );
325             p_demux->pf_demux = MjpgDemux;
326         }
327         else
328         {
329             goto error;
330         }
331     }
332     else
333     {
334         goto error;
335     }
336
337
338     var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
339     var_Get( p_demux, "mjpeg-fps", &val );
340     p_sys->i_frame_length = 0;
341
342     /* Check for jpeg file extension */
343     p_sys->b_still = VLC_FALSE;
344     p_sys->i_still_end = 0;
345     if( demux2_IsPathExtension( p_demux, ".jpeg" ) ||
346         demux2_IsPathExtension( p_demux, ".jpg" ) )
347     {
348         p_sys->b_still = VLC_TRUE;
349         if( val.f_float)
350         {
351             p_sys->i_still_length =1000000.0 / val.f_float;
352         }
353         else
354         {
355             /* Defaults to 1fps */
356             p_sys->i_still_length = 1000000;
357         }
358     }
359     else if ( val.f_float )
360     {
361         p_sys->i_frame_length = 1000000.0 / val.f_float;
362     }
363
364     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
365     p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
366
367     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
368     return VLC_SUCCESS;
369
370 error:
371     free( p_sys );
372     return VLC_EGENERIC;
373 }
374
375 /*****************************************************************************
376  * Demux: read packet and send them to decoders
377  *****************************************************************************
378  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
379  *****************************************************************************/
380 static int MjpgDemux( demux_t *p_demux )
381 {
382     demux_sys_t *p_sys = p_demux->p_sys;
383     int i;
384
385     if( p_sys->b_still && p_sys->i_still_end && p_sys->i_still_end < mdate() )
386     {
387         /* Still frame, wait until the pause delay is gone */
388         p_sys->i_still_end = 0;
389     }
390     else if( p_sys->b_still && p_sys->i_still_end )
391     {
392         msleep( 400 );
393         return 1;
394     }
395
396     if( !Peek( p_demux, VLC_TRUE ) )
397     {
398         msg_Warn( p_demux, "cannot peek data" );
399         return 0;
400     }
401     if( p_sys->i_data_peeked < 4 )
402     {
403         msg_Warn( p_demux, "data shortage" );
404         return 0;
405     }
406     i = 3;
407     while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
408     {
409         i++;
410         if( i >= p_sys->i_data_peeked )
411         {
412             msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
413                      p_sys->i_data_peeked );
414             if( !Peek( p_demux, VLC_FALSE ) )
415             {
416                 msg_Warn( p_demux, "no more data is available at the moment" );
417                 return 0;
418             }
419         }
420     }
421     i++;
422
423     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
424     return SendBlock( p_demux, i );
425 }
426
427 static int MimeDemux( demux_t *p_demux )
428 {
429     demux_sys_t *p_sys = p_demux->p_sys;
430     int         i_size, i;
431     vlc_bool_t  b_match;
432     vlc_bool_t  b_done;
433
434     b_match = CheckMimeHeader( p_demux, &i_size );
435     if( i_size > 0 )
436     {
437         stream_Read( p_demux->s, NULL, i_size );
438     }
439     else if( i_size < 0 )
440     {
441         return 0;
442     }
443     else
444     {
445         // No MIME header, assume OK
446         b_match = VLC_TRUE;
447     }
448
449     if( !Peek( p_demux, VLC_TRUE ) )
450     {
451         msg_Warn( p_demux, "cannot peek data" );
452         return 0;
453     }
454     i = 0;
455     i_size = strlen( p_sys->psz_separator ) + 2;
456     if( p_sys->i_data_peeked < i_size )
457     {
458         msg_Warn( p_demux, "data shortage" );
459         return 0;
460     }
461     b_done = VLC_FALSE;
462     while( !b_done )
463     {
464         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
465         {
466             i++;
467             i_size++;
468             if( i_size >= p_sys->i_data_peeked )
469             {
470                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
471                          "data", p_sys->i_data_peeked );
472
473                 if( !Peek( p_demux, VLC_FALSE ) )
474                 {
475                     msg_Warn( p_demux, "no more data is available at the "
476                               "moment" );
477                     return 0;
478                 }
479             }
480         }
481         if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
482                       strlen( p_sys->psz_separator ) ) )
483         {
484             b_done = VLC_TRUE;
485         }
486         else
487         {
488             i++;
489             i_size++;
490         }
491     }
492
493     if( !b_match )
494     {
495         msg_Err( p_demux, "discard non-JPEG part" );
496         stream_Read( p_demux->s, NULL, i );
497         return 0;
498     }
499
500     return SendBlock( p_demux, i );
501 }
502
503 /*****************************************************************************
504  * Close: frees unused data
505  *****************************************************************************/
506 static void Close ( vlc_object_t * p_this )
507 {
508     demux_t     *p_demux = (demux_t*)p_this;
509     demux_sys_t *p_sys  = p_demux->p_sys;
510
511     if( p_sys->psz_separator )
512     {
513         free( p_sys->psz_separator );
514     }
515     free( p_sys );
516 }
517
518 /*****************************************************************************
519  * Control:
520  *****************************************************************************/
521 static int Control( demux_t *p_demux, int i_query, va_list args )
522 {
523     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
524 }