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