1 /*****************************************************************************
2 * mjpeg.c : demuxes mjpeg webcam http streams
3 *****************************************************************************
4 * Copyright (C) 2004 VLC authors and VideoLAN
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 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
38 #include "mxpeg_helper.h"
40 /*****************************************************************************
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).")
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 )
61 /*****************************************************************************
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 );
76 mtime_t i_frame_length;
78 int i_frame_size_estimate;
79 const uint8_t *p_peek;
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 )
91 demux_sys_t *p_sys = p_demux->p_sys;
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 = 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" );
108 p_sys->i_data_peeked = i_data;
111 msg_Warn( p_demux, "cannot peek data" );
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;
128 while( *p_pos >= p_sys->i_data_peeked )
130 if( ! Peek( p_demux, false ) )
135 p_buf = p_sys->p_peek + *p_pos;
136 i_size = p_sys->i_data_peeked - *p_pos;
138 while( p_buf[i] != '\n' )
143 if( ! Peek( p_demux, false ) )
147 p_buf = p_sys->p_peek + *p_pos;
148 i_size = p_sys->i_data_peeked - *p_pos;
152 if( i > 0 && p_buf[i - 1] == '\r' )
156 p_line = malloc( i + 1 );
157 if( unlikely( p_line == NULL ) )
159 strncpy ( p_line, (char*)p_buf, i );
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 )
176 demux_sys_t *p_sys = p_demux->p_sys;
179 if( !Peek( p_demux, true ) )
181 msg_Err( p_demux, "cannot peek" );
184 if( p_sys->i_data_peeked < 5)
186 msg_Err( p_demux, "data shortage" );
189 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
190 && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
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" );
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 );
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 );
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 );
242 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
245 psz_line = GetLine( p_demux, &i_pos );
248 if( NULL == psz_line )
250 msg_Err( p_demux, "no EOL" );
256 *p_header_size = i_pos;
260 static int SendBlock( demux_t *p_demux, int i )
262 demux_sys_t *p_sys = p_demux->p_sys;
265 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
267 msg_Warn( p_demux, "cannot read data" );
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;
278 p_block->i_pts = mdate();
280 p_block->i_dts = p_block->i_pts;
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 );
287 p_sys->i_still_end = mdate() + p_sys->i_frame_length;
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;
299 bool b_matched = false;
301 if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
302 // let avformat handle this case
305 demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
306 if( unlikely(p_sys == NULL) )
309 p_demux->pf_control = Control;
310 p_demux->p_sys = p_sys;
312 p_sys->i_time = VLC_TS_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 );
321 //FIXME: this is not fully match to RFC
322 char* boundary = strstr( content_type, "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';
333 p_sys->psz_separator = strdup( boundary );
334 if( !p_sys->psz_separator )
336 free( content_type );
340 free( content_type );
343 b_matched = CheckMimeHeader( p_demux, &i_size);
346 p_demux->pf_demux = MimeDemux;
347 stream_Read( p_demux->s, NULL, i_size );
349 else if( i_size == 0 )
351 /* 0xffd8 identify a JPEG SOI */
352 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
354 msg_Dbg( p_demux, "JPEG SOI marker detected" );
355 p_demux->pf_demux = MjpgDemux;
369 float f_fps = var_InheritFloat( p_demux, "mjpeg-fps" );
371 p_sys->i_still_end = 0;
372 if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
373 demux_IsPathExtension( p_demux, ".jpg" ) )
375 /* Plain JPEG file = single still picture */
376 p_sys->b_still = true;
378 /* Defaults to 1fps */
382 p_sys->b_still = false;
383 p_sys->i_frame_length = f_fps ? (CLOCK_FREQ / f_fps) : 0;
385 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
386 p_sys->fmt.i_codec = VLC_CODEC_MJPG;
388 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
392 free( p_sys->psz_separator );
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;
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;
415 if( !Peek( p_demux, true ) )
417 msg_Warn( p_demux, "cannot peek data" );
420 if( p_sys->i_data_peeked < 4 )
422 msg_Warn( p_demux, "data shortage" );
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 )
432 msg_Dbg( p_demux, "we found another JPEG SOI at %d", 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" );
448 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
451 if( p_sys->i_level > 0 )
453 return SendBlock( p_demux, i );
456 static int MimeDemux( demux_t *p_demux )
458 demux_sys_t *p_sys = p_demux->p_sys;
461 bool b_match = CheckMimeHeader( p_demux, &i_size );
465 stream_Read( p_demux->s, NULL, i_size );
467 else if( i_size < 0 )
473 // No MIME header, assume OK
477 if( !Peek( p_demux, true ) )
479 msg_Warn( p_demux, "cannot peek data" );
484 i_size = strlen( p_sys->psz_separator ) + 2;
485 if( p_sys->i_data_peeked < i_size )
487 msg_Warn( p_demux, "data shortage" );
493 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
497 if( i_size >= p_sys->i_data_peeked )
499 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
500 "data", p_sys->i_data_peeked );
502 if( !Peek( p_demux, false ) )
504 msg_Warn( p_demux, "no more data is available at the "
511 if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
512 strlen( p_sys->psz_separator ) ) )
523 msg_Err( p_demux, "discard non-JPEG part" );
524 stream_Read( p_demux->s, NULL, i );
528 return SendBlock( p_demux, i );
531 /*****************************************************************************
532 * Close: frees unused data
533 *****************************************************************************/
534 static void Close ( vlc_object_t * p_this )
536 demux_t *p_demux = (demux_t*)p_this;
537 demux_sys_t *p_sys = p_demux->p_sys;
539 free( p_sys->psz_separator );
543 /*****************************************************************************
545 *****************************************************************************/
546 static int Control( demux_t *p_demux, int i_query, va_list args )
548 return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );