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