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