demux: libavi: force chunk type check
[vlc.git] / modules / demux / mjpeg.c
blob2d7dde7c3c1d861981412a8a7d7a06d425e10c22
1 /*****************************************************************************
2 * mjpeg.c : demuxes mjpeg webcam http streams
3 *****************************************************************************
4 * Copyright (C) 2004 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Henry Jen (slowhog) <henryjen@ztune.net>
8 * Derk-Jan Hartman (thedj)
9 * Sigmund Augdal (Dnumgis)
10 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
38 #include "mxpeg_helper.h"
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
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).")
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 ()
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 );
68 struct demux_sys_t
70 es_format_t fmt;
71 es_out_id_t *p_es;
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;
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 )
90 int i_data;
91 demux_sys_t *p_sys = p_demux->p_sys;
93 if( b_first )
95 p_sys->i_data_peeked = 0;
97 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
99 p_sys->i_frame_size_estimate += 5120;
101 i_data = vlc_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 )
105 msg_Warn( p_demux, "no more data" );
106 return false;
108 p_sys->i_data_peeked = i_data;
109 if( i_data <= 0 )
111 msg_Warn( p_demux, "cannot peek data" );
112 return false;
114 return true;
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 )
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;
128 while( *p_pos >= p_sys->i_data_peeked )
130 if( ! Peek( p_demux, false ) )
132 return NULL;
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' )
140 i++;
141 if( i == i_size )
143 if( ! Peek( p_demux, false ) )
145 return NULL;
147 p_buf = p_sys->p_peek + *p_pos;
148 i_size = p_sys->i_data_peeked - *p_pos;
151 *p_pos += i + 1;
152 if( i > 0 && p_buf[i - 1] == '\r' )
154 i--;
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;
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 )
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;
178 *p_header_size = -1;
179 if( !Peek( p_demux, true ) )
181 msg_Err( p_demux, "cannot peek" );
182 return false;
184 if( p_sys->i_data_peeked < 5)
186 msg_Err( p_demux, "data shortage" );
187 return false;
189 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
190 && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
192 *p_header_size = 0;
193 return false;
195 else
197 i_pos = *p_sys->p_peek == '-' ? 2 : 4;
198 psz_line = GetLine( p_demux, &i_pos );
199 if( NULL == psz_line )
201 msg_Err( p_demux, "no EOL" );
202 return false;
205 /* Read the separator and remember it if not yet stored */
206 if( p_sys->psz_separator == NULL )
208 p_sys->psz_separator = psz_line;
209 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
210 p_sys->psz_separator );
212 else
214 if( strcmp( psz_line, p_sys->psz_separator ) )
216 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
217 p_sys->psz_separator );
219 free( psz_line );
223 psz_line = GetLine( p_demux, &i_pos );
224 while( psz_line && *psz_line )
226 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
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 ) )
232 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
233 b_jpeg = false;
235 else
237 b_jpeg = true;
240 else
242 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
244 free( psz_line );
245 psz_line = GetLine( p_demux, &i_pos );
248 if( NULL == psz_line )
250 msg_Err( p_demux, "no EOL" );
251 return false;
254 free( psz_line );
256 *p_header_size = i_pos;
257 return b_jpeg;
260 static int SendBlock( demux_t *p_demux, int i )
262 demux_sys_t *p_sys = p_demux->p_sys;
263 block_t *p_block;
265 if( ( p_block = vlc_stream_Block( p_demux->s, i ) ) == NULL )
267 msg_Warn( p_demux, "cannot read data" );
268 return 0;
271 if( p_sys->i_frame_length )
273 p_block->i_pts = p_sys->i_time;
274 p_sys->i_time += p_sys->i_frame_length;
276 else
278 p_block->i_pts = mdate();
280 p_block->i_dts = p_block->i_pts;
282 /* set PCR */
283 es_out_SetPCR( p_demux->out, p_block->i_pts );
284 es_out_Send( p_demux->out, p_sys->p_es, p_block );
286 if( p_sys->b_still )
287 p_sys->i_still_end = mdate() + p_sys->i_frame_length;
289 return 1;
292 /*****************************************************************************
293 * Open: check file and initializes structures
294 *****************************************************************************/
295 static int Open( vlc_object_t * p_this )
297 demux_t *p_demux = (demux_t*)p_this;
298 int i_size;
299 bool b_matched = false;
301 if( IsMxpeg( p_demux->s ) && !p_demux->obj.force )
302 // let avformat handle this case
303 return VLC_EGENERIC;
305 demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
306 if( unlikely(p_sys == NULL) )
307 return VLC_ENOMEM;
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;
315 p_sys->psz_separator = NULL;
316 p_sys->i_frame_size_estimate = 15 * 1024;
318 char *content_type = stream_ContentType( p_demux->s );
319 if ( content_type )
321 //FIXME: this is not fully match to RFC
322 char* boundary = strstr( content_type, "boundary=" );
323 if( boundary )
325 boundary += strlen( "boundary=" );
326 size_t len = strlen( boundary );
327 if( len > 2 && boundary[0] == '"'
328 && boundary[len-1] == '"' )
330 boundary[len-1] = '\0';
331 boundary++;
333 p_sys->psz_separator = strdup( boundary );
334 if( !p_sys->psz_separator )
336 free( content_type );
337 goto error;
340 free( content_type );
343 b_matched = CheckMimeHeader( p_demux, &i_size);
344 if( b_matched )
346 p_demux->pf_demux = MimeDemux;
347 if( vlc_stream_Read( p_demux->s, NULL, i_size ) < i_size )
348 goto error;
350 else if( i_size == 0 )
352 /* 0xffd8 identify a JPEG SOI */
353 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
355 msg_Dbg( p_demux, "JPEG SOI marker detected" );
356 p_demux->pf_demux = MjpgDemux;
357 p_sys->i_level++;
359 else
361 goto error;
364 else
366 goto error;
369 /* Frame rate */
370 float f_fps = var_InheritFloat( p_demux, "mjpeg-fps" );
372 p_sys->i_still_end = 0;
373 if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
374 demux_IsPathExtension( p_demux, ".jpg" ) )
376 /* Plain JPEG file = single still picture */
377 p_sys->b_still = true;
378 if( f_fps == 0.f )
379 /* Defaults to 1fps */
380 f_fps = 1.f;
382 else
383 p_sys->b_still = false;
384 p_sys->i_frame_length = f_fps ? (CLOCK_FREQ / f_fps) : 0;
386 es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_CODEC_MJPG );
388 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
389 return VLC_SUCCESS;
391 error:
392 free( p_sys->psz_separator );
393 free( p_sys );
394 return VLC_EGENERIC;
397 /*****************************************************************************
398 * Demux: read packet and send them to decoders
399 *****************************************************************************
400 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
401 *****************************************************************************/
402 static int MjpgDemux( demux_t *p_demux )
404 demux_sys_t *p_sys = p_demux->p_sys;
405 int i;
407 if( p_sys->b_still && p_sys->i_still_end )
409 /* Still frame, wait until the pause delay is gone */
410 mwait( p_sys->i_still_end );
411 p_sys->i_still_end = 0;
412 return 1;
415 if( !Peek( p_demux, true ) )
417 msg_Warn( p_demux, "cannot peek data" );
418 return 0;
420 if( p_sys->i_data_peeked < 4 )
422 msg_Warn( p_demux, "data shortage" );
423 return 0;
425 i = 3;
426 FIND_NEXT_EOI:
427 while( !( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 ) )
429 if( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 )
431 p_sys->i_level++;
432 msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
434 i++;
435 if( i >= p_sys->i_data_peeked )
437 msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
438 p_sys->i_data_peeked );
439 if( !Peek( p_demux, false ) )
441 msg_Warn( p_demux, "no more data is available at the moment" );
442 return 0;
446 i++;
448 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
449 p_sys->i_level--;
451 if( p_sys->i_level > 0 )
452 goto FIND_NEXT_EOI;
453 return SendBlock( p_demux, i );
456 static int MimeDemux( demux_t *p_demux )
458 demux_sys_t *p_sys = p_demux->p_sys;
459 int i_size, i;
461 bool b_match = CheckMimeHeader( p_demux, &i_size );
463 if( i_size > 0 )
465 if( vlc_stream_Read( p_demux->s, NULL, i_size ) != i_size )
466 return 0;
468 else if( i_size < 0 )
470 return 0;
472 else
474 // No MIME header, assume OK
475 b_match = true;
478 if( !Peek( p_demux, true ) )
480 msg_Warn( p_demux, "cannot peek data" );
481 return 0;
484 i = 0;
485 i_size = strlen( p_sys->psz_separator ) + 2;
486 if( p_sys->i_data_peeked < i_size )
488 msg_Warn( p_demux, "data shortage" );
489 return 0;
492 for( ;; )
494 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
496 i++;
497 i_size++;
498 if( i_size >= p_sys->i_data_peeked )
500 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
501 "data", p_sys->i_data_peeked );
503 if( !Peek( p_demux, false ) )
505 msg_Warn( p_demux, "no more data is available at the "
506 "moment" );
507 return 0;
512 /* Handle old and new style of separators */
513 if (!strncmp(p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
514 strlen( p_sys->psz_separator ))
515 || ((strlen(p_sys->psz_separator) > 4)
516 && !strncmp(p_sys->psz_separator, "--", 2)
517 && !strncmp(p_sys->psz_separator, (char *)(p_sys->p_peek + i),
518 strlen( p_sys->psz_separator))))
520 break;
523 i++;
524 i_size++;
527 if( !b_match )
529 msg_Err( p_demux, "discard non-JPEG part" );
530 return 0;
533 return SendBlock( p_demux, i );
536 /*****************************************************************************
537 * Close: frees unused data
538 *****************************************************************************/
539 static void Close ( vlc_object_t * p_this )
541 demux_t *p_demux = (demux_t*)p_this;
542 demux_sys_t *p_sys = p_demux->p_sys;
544 free( p_sys->psz_separator );
545 free( p_sys );
548 /*****************************************************************************
549 * Control:
550 *****************************************************************************/
551 static int Control( demux_t *p_demux, int i_query, va_list args )
553 return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );