preparser: use new input event handling
[vlc.git] / modules / demux / mjpeg.c
blob7e6e029dc4c886a234a6636e2e67b26d9c4936db
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 * );
45 #define FPS_TEXT N_("Frames per Second")
46 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
47 "playing MJPEG from a file. Use 0 (this is the default value) for a " \
48 "live stream (from a camera).")
50 vlc_module_begin ()
51 set_shortname( "MJPEG")
52 set_description( N_("M-JPEG camera demuxer") )
53 set_capability( "demux", 5 )
54 set_callbacks( Open, NULL )
55 set_category( CAT_INPUT )
56 set_subcategory( SUBCAT_INPUT_DEMUX )
57 add_float( "mjpeg-fps", 0.0, FPS_TEXT, FPS_LONGTEXT, false )
58 vlc_module_end ()
60 /*****************************************************************************
61 * Local prototypes
62 *****************************************************************************/
63 static int MimeDemux( demux_t * );
64 static int MjpgDemux( demux_t * );
65 static int Control( demux_t *, int i_query, va_list args );
67 typedef struct
69 es_format_t fmt;
70 es_out_id_t *p_es;
72 bool b_still;
73 vlc_tick_t i_still_end;
74 vlc_tick_t i_time;
75 vlc_tick_t i_frame_length;
76 char *psz_separator;
77 int i_frame_size_estimate;
78 const uint8_t *p_peek;
79 int i_data_peeked;
80 int i_level;
81 } demux_sys_t;
83 /*****************************************************************************
84 * Peek: Helper function to peek data with incremental size.
85 * \return false if peek no more data, true otherwise.
86 *****************************************************************************/
87 static bool Peek( demux_t *p_demux, bool b_first )
89 int i_data;
90 demux_sys_t *p_sys = p_demux->p_sys;
92 if( b_first )
94 p_sys->i_data_peeked = 0;
96 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
98 p_sys->i_frame_size_estimate += 5120;
100 i_data = vlc_stream_Peek( p_demux->s, &p_sys->p_peek,
101 p_sys->i_frame_size_estimate );
102 if( i_data == p_sys->i_data_peeked )
104 msg_Warn( p_demux, "no more data" );
105 return false;
107 p_sys->i_data_peeked = i_data;
108 if( i_data <= 0 )
110 msg_Warn( p_demux, "cannot peek data" );
111 return false;
113 return true;
116 /*****************************************************************************
117 * GetLine: Internal function used to dup a line of string from the buffer
118 *****************************************************************************/
119 static char* GetLine( demux_t *p_demux, int *p_pos )
121 demux_sys_t *p_sys = p_demux->p_sys;
122 const uint8_t *p_buf;
123 int i_size;
124 int i;
125 char *p_line;
127 while( *p_pos >= p_sys->i_data_peeked )
129 if( ! Peek( p_demux, false ) )
131 return NULL;
134 p_buf = p_sys->p_peek + *p_pos;
135 i_size = p_sys->i_data_peeked - *p_pos;
136 i = 0;
137 while( p_buf[i] != '\n' )
139 i++;
140 if( i == i_size )
142 if( ! Peek( p_demux, false ) )
144 return NULL;
146 p_buf = p_sys->p_peek + *p_pos;
147 i_size = p_sys->i_data_peeked - *p_pos;
150 *p_pos += i + 1;
151 if( i > 0 && p_buf[i - 1] == '\r' )
153 i--;
155 p_line = malloc( i + 1 );
156 if( unlikely( p_line == NULL ) )
157 return NULL;
158 strncpy ( p_line, (char*)p_buf, i );
159 p_line[i] = '\0';
160 return p_line;
163 /*****************************************************************************
164 * CheckMimeHeader: Internal function used to verify and skip mime header
165 * \param p_header_size Return size of MIME header, 0 if no MIME header
166 * detected, minus value if error
167 * \return true if content type is image/jpeg, false otherwise
168 *****************************************************************************/
169 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
171 bool b_jpeg = false;
172 int i_pos = 0;
173 char *psz_line;
174 char *p_ch;
175 demux_sys_t *p_sys = p_demux->p_sys;
177 *p_header_size = -1;
178 if( !Peek( p_demux, true ) )
180 msg_Err( p_demux, "cannot peek" );
181 return false;
183 if( p_sys->i_data_peeked < 5)
185 msg_Err( p_demux, "data shortage" );
186 return false;
188 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
189 && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
191 *p_header_size = 0;
192 return false;
194 else
196 i_pos = *p_sys->p_peek == '-' ? 2 : 4;
197 psz_line = GetLine( p_demux, &i_pos );
198 if( NULL == psz_line )
200 msg_Err( p_demux, "no EOL" );
201 return false;
204 /* Read the separator and remember it if not yet stored */
205 if( p_sys->psz_separator == NULL )
207 p_sys->psz_separator = psz_line;
208 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
209 p_sys->psz_separator );
211 else
213 if( strcmp( psz_line, p_sys->psz_separator ) )
215 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
216 p_sys->psz_separator );
218 free( psz_line );
222 psz_line = GetLine( p_demux, &i_pos );
223 while( psz_line && *psz_line )
225 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
227 p_ch = psz_line + 13;
228 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
229 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
231 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
232 b_jpeg = false;
234 else
236 b_jpeg = true;
239 else
241 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
243 free( psz_line );
244 psz_line = GetLine( p_demux, &i_pos );
247 if( NULL == psz_line )
249 msg_Err( p_demux, "no EOL" );
250 return false;
253 free( psz_line );
255 *p_header_size = i_pos;
256 return b_jpeg;
259 static int SendBlock( demux_t *p_demux, int i )
261 demux_sys_t *p_sys = p_demux->p_sys;
262 block_t *p_block;
264 if( ( p_block = vlc_stream_Block( p_demux->s, i ) ) == NULL )
266 msg_Warn( p_demux, "cannot read data" );
267 return VLC_DEMUXER_EOF;
270 if( p_sys->i_frame_length )
272 p_block->i_pts = p_sys->i_time;
273 p_sys->i_time += p_sys->i_frame_length;
275 else
277 p_block->i_pts = vlc_tick_now();
279 p_block->i_dts = p_block->i_pts;
281 es_out_SetPCR( p_demux->out, p_block->i_pts );
282 es_out_Send( p_demux->out, p_sys->p_es, p_block );
284 if( p_sys->b_still )
285 p_sys->i_still_end = vlc_tick_now() + p_sys->i_frame_length;
287 return VLC_DEMUXER_SUCCESS;
290 /*****************************************************************************
291 * Open: check file and initializes structures
292 *****************************************************************************/
293 static int Open( vlc_object_t * p_this )
295 demux_t *p_demux = (demux_t*)p_this;
296 int i_size;
297 bool b_matched = false;
299 if( IsMxpeg( p_demux->s ) && !p_demux->obj.force )
300 // let avformat handle this case
301 return VLC_EGENERIC;
303 demux_sys_t *p_sys = vlc_obj_malloc( p_this, sizeof (*p_sys) );
304 if( unlikely(p_sys == NULL) )
305 return VLC_ENOMEM;
307 p_demux->p_sys = p_sys;
308 p_sys->p_es = NULL;
309 p_sys->i_time = VLC_TICK_0;
310 p_sys->i_level = 0;
312 p_sys->psz_separator = NULL;
313 p_sys->i_frame_size_estimate = 15 * 1024;
315 char *content_type = stream_ContentType( p_demux->s );
316 if ( content_type )
318 //FIXME: this is not fully match to RFC
319 char* boundary = strstr( content_type, "boundary=" );
320 if( boundary )
322 boundary += strlen( "boundary=" );
323 size_t len = strlen( boundary );
324 if( len > 2 && boundary[0] == '"'
325 && boundary[len-1] == '"' )
327 boundary[len-1] = '\0';
328 boundary++;
330 p_sys->psz_separator = vlc_obj_strdup( p_this, boundary );
331 if( !p_sys->psz_separator )
333 free( content_type );
334 return VLC_ENOMEM;
337 free( content_type );
340 b_matched = CheckMimeHeader( p_demux, &i_size);
341 if( b_matched )
343 p_demux->pf_demux = MimeDemux;
344 if( vlc_stream_Read( p_demux->s, NULL, i_size ) < i_size )
345 return VLC_EGENERIC;
347 else if( i_size == 0 )
349 /* 0xffd8 identify a JPEG SOI */
350 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
352 msg_Dbg( p_demux, "JPEG SOI marker detected" );
353 p_demux->pf_demux = MjpgDemux;
354 p_sys->i_level++;
356 else
358 return VLC_EGENERIC;
361 else
363 return VLC_EGENERIC;
366 /* Frame rate */
367 float f_fps = var_InheritFloat( p_demux, "mjpeg-fps" );
369 p_sys->i_still_end = 0;
370 if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
371 demux_IsPathExtension( p_demux, ".jpg" ) )
373 /* Plain JPEG file = single still picture */
374 p_sys->b_still = true;
375 if( f_fps == 0.f )
376 /* Defaults to 1fps */
377 f_fps = 1.f;
379 else
380 p_sys->b_still = false;
381 p_sys->i_frame_length = f_fps ? (CLOCK_FREQ / f_fps) : 0;
383 es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_CODEC_MJPG );
385 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
386 if( unlikely(p_sys->p_es == NULL) )
387 return VLC_ENOMEM;
389 p_demux->pf_control = Control;
390 return VLC_SUCCESS;
393 /*****************************************************************************
394 * Demux: read packet and send them to decoders
395 *****************************************************************************
396 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
397 *****************************************************************************/
398 static int MjpgDemux( demux_t *p_demux )
400 demux_sys_t *p_sys = p_demux->p_sys;
401 int i;
403 if( p_sys->b_still && p_sys->i_still_end )
405 /* Still frame, wait until the pause delay is gone */
406 vlc_tick_wait( p_sys->i_still_end );
407 p_sys->i_still_end = 0;
408 return VLC_DEMUXER_SUCCESS;
411 if( !Peek( p_demux, true ) )
413 msg_Warn( p_demux, "cannot peek data" );
414 return VLC_DEMUXER_EOF;
416 if( p_sys->i_data_peeked < 4 )
418 msg_Warn( p_demux, "data shortage" );
419 return VLC_DEMUXER_EOF;
421 i = 3;
422 FIND_NEXT_EOI:
423 while( !( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 ) )
425 if( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 )
427 p_sys->i_level++;
428 msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
430 i++;
431 if( i >= p_sys->i_data_peeked )
433 msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
434 p_sys->i_data_peeked );
435 if( !Peek( p_demux, false ) )
437 msg_Warn( p_demux, "no more data is available at the moment" );
438 return VLC_DEMUXER_EOF;
442 i++;
444 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
445 p_sys->i_level--;
447 if( p_sys->i_level > 0 )
448 goto FIND_NEXT_EOI;
449 return SendBlock( p_demux, i );
452 static int MimeDemux( demux_t *p_demux )
454 demux_sys_t *p_sys = p_demux->p_sys;
455 int i_size, i;
457 bool b_match = CheckMimeHeader( p_demux, &i_size );
459 if( i_size > 0 )
461 if( vlc_stream_Read( p_demux->s, NULL, i_size ) != i_size )
462 return VLC_DEMUXER_EOF;
464 else if( i_size < 0 )
466 return VLC_DEMUXER_EOF;
468 else
470 // No MIME header, assume OK
471 b_match = true;
474 if( !Peek( p_demux, true ) )
476 msg_Warn( p_demux, "cannot peek data" );
477 return VLC_DEMUXER_EOF;
480 i = 0;
481 i_size = strlen( p_sys->psz_separator ) + 2;
482 if( p_sys->i_data_peeked < i_size )
484 msg_Warn( p_demux, "data shortage" );
485 return VLC_DEMUXER_EOF;
488 for( ;; )
490 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
492 i++;
493 i_size++;
494 if( i_size >= p_sys->i_data_peeked )
496 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
497 "data", p_sys->i_data_peeked );
499 if( !Peek( p_demux, false ) )
501 msg_Warn( p_demux, "no more data is available at the "
502 "moment" );
503 return VLC_DEMUXER_EOF;
508 /* Handle old and new style of separators */
509 if (!strncmp(p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
510 strlen( p_sys->psz_separator ))
511 || ((strlen(p_sys->psz_separator) > 4)
512 && !strncmp(p_sys->psz_separator, "--", 2)
513 && !strncmp(p_sys->psz_separator, (char *)(p_sys->p_peek + i),
514 strlen( p_sys->psz_separator))))
516 break;
519 i++;
520 i_size++;
523 if( !b_match )
525 msg_Err( p_demux, "discard non-JPEG part" );
526 return VLC_DEMUXER_EOF;
529 return SendBlock( p_demux, i );
532 /*****************************************************************************
533 * Control:
534 *****************************************************************************/
535 static int Control( demux_t *p_demux, int i_query, va_list args )
537 return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );