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