]> git.sesse.net Git - vlc/blob - modules/demux/mjpeg.c
2ae4ec79d2cbecbe5b3f92e5968abcfe7592a029
[vlc] / modules / demux / mjpeg.c
1 /*****************************************************************************
2  * mjpeg.c : demuxes mjpeg webcam http streams
3  *****************************************************************************
4  * Copyright (C) 2004 VLC authors and VideoLAN
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 it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * 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_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
38 #include "mxpeg_helper.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( N_("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, 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_time;
76     mtime_t         i_frame_length;
77     char            *psz_separator;
78     int             i_frame_size_estimate;
79     const uint8_t   *p_peek;
80     int             i_data_peeked;
81     int             i_level;
82 };
83
84 /*****************************************************************************
85  * Peek: Helper function to peek data with incremental size.
86  * \return false if peek no more data, true otherwise.
87  *****************************************************************************/
88 static bool Peek( demux_t *p_demux, bool b_first )
89 {
90     int i_data;
91     demux_sys_t *p_sys = p_demux->p_sys;
92
93     if( b_first )
94     {
95         p_sys->i_data_peeked = 0;
96     }
97     else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
98     {
99         p_sys->i_frame_size_estimate += 5120;
100     }
101     i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
102                           p_sys->i_frame_size_estimate );
103     if( i_data == p_sys->i_data_peeked )
104     {
105         msg_Warn( p_demux, "no more data" );
106         return false;
107     }
108     p_sys->i_data_peeked = i_data;
109     if( i_data <= 0 )
110     {
111         msg_Warn( p_demux, "cannot peek data" );
112         return false;
113     }
114     return true;
115 }
116
117 /*****************************************************************************
118  * GetLine: Internal function used to dup a line of string from the buffer
119  *****************************************************************************/
120 static char* GetLine( demux_t *p_demux, int *p_pos )
121 {
122     demux_sys_t *p_sys = p_demux->p_sys;
123     const uint8_t *p_buf;
124     int         i_size;
125     int         i;
126     char        *p_line;
127
128     while( *p_pos >= p_sys->i_data_peeked )
129     {
130         if( ! Peek( p_demux, false ) )
131         {
132             return NULL;
133         }
134     }
135     p_buf = p_sys->p_peek + *p_pos;
136     i_size = p_sys->i_data_peeked - *p_pos;
137     i = 0;
138     while( p_buf[i] != '\n' )
139     {
140         i++;
141         if( i == i_size )
142         {
143             if( ! Peek( p_demux, false ) )
144             {
145                 return NULL;
146             }
147             p_buf = p_sys->p_peek + *p_pos;
148             i_size = p_sys->i_data_peeked - *p_pos;
149         }
150     }
151     *p_pos += i + 1;
152     if( i > 0 && p_buf[i - 1] == '\r' )
153     {
154         i--;
155     }
156     p_line = malloc( i + 1 );
157     if( unlikely( p_line == NULL ) )
158         return NULL;
159     strncpy ( p_line, (char*)p_buf, i );
160     p_line[i] = '\0';
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 true if content type is image/jpeg, false otherwise
169  *****************************************************************************/
170 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
171 {
172     bool        b_jpeg = false;
173     int         i_pos = 0;
174     char        *psz_line;
175     char        *p_ch;
176     demux_sys_t *p_sys = p_demux->p_sys;
177
178     *p_header_size = -1;
179     if( !Peek( p_demux, true ) )
180     {
181         msg_Err( p_demux, "cannot peek" );
182         return false;
183     }
184     if( p_sys->i_data_peeked < 5)
185     {
186         msg_Err( p_demux, "data shortage" );
187         return false;
188     }
189     if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
190         && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
191     {
192         *p_header_size = 0;
193         return false;
194     }
195     else
196     {
197         i_pos = *p_sys->p_peek == '-' ? 2 : 4;
198         psz_line = GetLine( p_demux, &i_pos );
199         if( NULL == psz_line )
200         {
201             msg_Err( p_demux, "no EOL" );
202             return false;
203         }
204
205         /* Read the separator and remember it if not yet stored */
206         if( p_sys->psz_separator == NULL )
207         {
208             p_sys->psz_separator = psz_line;
209             msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
210                      p_sys->psz_separator );
211         }
212         else
213         {
214             if( strcmp( psz_line, p_sys->psz_separator ) )
215             {
216                 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
217                           p_sys->psz_separator );
218             }
219             free( psz_line );
220         }
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 = false;
234             }
235             else
236             {
237                 b_jpeg = 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         return false;
252     }
253
254     free( psz_line );
255
256     *p_header_size = i_pos;
257     return b_jpeg;
258 }
259
260 static int SendBlock( demux_t *p_demux, int i )
261 {
262     demux_sys_t *p_sys = p_demux->p_sys;
263     block_t     *p_block;
264
265     if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
266     {
267         msg_Warn( p_demux, "cannot read data" );
268         return 0;
269     }
270
271     if( p_sys->i_frame_length )
272     {
273         p_block->i_pts = p_sys->i_time;
274         p_sys->i_time += p_sys->i_frame_length;
275     }
276     else
277     {
278         p_block->i_pts = mdate();
279     }
280     p_block->i_dts = p_block->i_pts;
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         p_sys->i_still_end = mdate() + p_sys->i_frame_length;
288
289     return 1;
290 }
291
292 /*****************************************************************************
293  * Open: check file and initializes structures
294  *****************************************************************************/
295 static int Open( vlc_object_t * p_this )
296 {
297     demux_t     *p_demux = (demux_t*)p_this;
298     int         i_size;
299     bool        b_matched = false;
300
301     if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
302         // let avformat handle this case
303         return VLC_EGENERIC;
304
305     demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
306     if( unlikely(p_sys == NULL) )
307         return VLC_ENOMEM;
308
309     p_demux->pf_control = Control;
310     p_demux->p_sys      = p_sys;
311     p_sys->p_es         = NULL;
312     p_sys->i_time       = VLC_TS_0;
313     p_sys->i_level      = 0;
314
315     p_sys->psz_separator = NULL;
316     p_sys->i_frame_size_estimate = 15 * 1024;
317
318     char *content_type = stream_ContentType( p_demux->s );
319     if ( content_type )
320     {
321         //FIXME: this is not fully match to RFC
322         char* boundary = strstr( content_type, "boundary=" );
323         if( boundary )
324         {
325             p_sys->psz_separator = strdup( boundary + strlen("boundary=") );
326             if( !p_sys->psz_separator )
327             {
328                 free( content_type );
329                 goto error;
330             }
331         }
332         free( content_type );
333     }
334
335     b_matched = CheckMimeHeader( p_demux, &i_size);
336     if( b_matched )
337     {
338         p_demux->pf_demux = MimeDemux;
339         stream_Read( p_demux->s, NULL, i_size );
340     }
341     else if( i_size == 0 )
342     {
343         /* 0xffd8 identify a JPEG SOI */
344         if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
345         {
346             msg_Dbg( p_demux, "JPEG SOI marker detected" );
347             p_demux->pf_demux = MjpgDemux;
348             p_sys->i_level++;
349         }
350         else
351         {
352             goto error;
353         }
354     }
355     else
356     {
357         goto error;
358     }
359
360     /* Frame rate */
361     float f_fps = var_InheritFloat( p_demux, "mjpeg-fps" );
362
363     p_sys->i_still_end = 0;
364     if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
365         demux_IsPathExtension( p_demux, ".jpg" ) )
366     {
367         /* Plain JPEG file = single still picture */
368         p_sys->b_still = true;
369         if( f_fps == 0.f )
370             /* Defaults to 1fps */
371             f_fps = 1.f;
372     }
373     else
374         p_sys->b_still = false;
375     p_sys->i_frame_length = f_fps ? (CLOCK_FREQ / f_fps) : 0;
376
377     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
378     p_sys->fmt.i_codec = VLC_CODEC_MJPG;
379
380     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
381     return VLC_SUCCESS;
382
383 error:
384     free( p_sys->psz_separator );
385     free( p_sys );
386     return VLC_EGENERIC;
387 }
388
389 /*****************************************************************************
390  * Demux: read packet and send them to decoders
391  *****************************************************************************
392  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
393  *****************************************************************************/
394 static int MjpgDemux( demux_t *p_demux )
395 {
396     demux_sys_t *p_sys = p_demux->p_sys;
397     int i;
398
399     if( p_sys->b_still && p_sys->i_still_end )
400     {
401         /* Still frame, wait until the pause delay is gone */
402         mwait( p_sys->i_still_end );
403         p_sys->i_still_end = 0;
404         return 1;
405     }
406
407     if( !Peek( p_demux, true ) )
408     {
409         msg_Warn( p_demux, "cannot peek data" );
410         return 0;
411     }
412     if( p_sys->i_data_peeked < 4 )
413     {
414         msg_Warn( p_demux, "data shortage" );
415         return 0;
416     }
417     i = 3;
418 FIND_NEXT_EOI:
419     while( !( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 ) )
420     {
421         if( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9  )
422         {
423             p_sys->i_level++;
424             msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
425         }
426         i++;
427         if( i >= p_sys->i_data_peeked )
428         {
429             msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
430                      p_sys->i_data_peeked );
431             if( !Peek( p_demux, false ) )
432             {
433                 msg_Warn( p_demux, "no more data is available at the moment" );
434                 return 0;
435             }
436         }
437     }
438     i++;
439
440     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
441     p_sys->i_level--;
442
443     if( p_sys->i_level > 0 )
444         goto FIND_NEXT_EOI;
445     return SendBlock( p_demux, i );
446 }
447
448 static int MimeDemux( demux_t *p_demux )
449 {
450     demux_sys_t *p_sys = p_demux->p_sys;
451     int         i_size, i;
452
453     bool  b_match = CheckMimeHeader( p_demux, &i_size );
454
455     if( i_size > 0 )
456     {
457         stream_Read( p_demux->s, NULL, i_size );
458     }
459     else if( i_size < 0 )
460     {
461         return 0;
462     }
463     else
464     {
465         // No MIME header, assume OK
466         b_match = true;
467     }
468
469     if( !Peek( p_demux, true ) )
470     {
471         msg_Warn( p_demux, "cannot peek data" );
472         return 0;
473     }
474
475     i = 0;
476     i_size = strlen( p_sys->psz_separator ) + 2;
477     if( p_sys->i_data_peeked < i_size )
478     {
479         msg_Warn( p_demux, "data shortage" );
480         return 0;
481     }
482
483     for( ;; )
484     {
485         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
486         {
487             i++;
488             i_size++;
489             if( i_size >= p_sys->i_data_peeked )
490             {
491                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
492                          "data", p_sys->i_data_peeked );
493
494                 if( !Peek( p_demux, false ) )
495                 {
496                     msg_Warn( p_demux, "no more data is available at the "
497                               "moment" );
498                     return 0;
499                 }
500             }
501         }
502
503         if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
504                       strlen( p_sys->psz_separator ) ) )
505         {
506             break;
507         }
508
509         i++;
510         i_size++;
511     }
512
513     if( !b_match )
514     {
515         msg_Err( p_demux, "discard non-JPEG part" );
516         stream_Read( p_demux->s, NULL, i );
517         return 0;
518     }
519
520     return SendBlock( p_demux, i );
521 }
522
523 /*****************************************************************************
524  * Close: frees unused data
525  *****************************************************************************/
526 static void Close ( vlc_object_t * p_this )
527 {
528     demux_t     *p_demux = (demux_t*)p_this;
529     demux_sys_t *p_sys  = p_demux->p_sys;
530
531     free( p_sys->psz_separator );
532     free( p_sys );
533 }
534
535 /*****************************************************************************
536  * Control:
537  *****************************************************************************/
538 static int Control( demux_t *p_demux, int i_query, va_list args )
539 {
540     return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
541 }