1 /*****************************************************************************
2 * mjpeg.c : demuxes mjpeg webcam http streams
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( 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).")
51 set_shortname( "MJPEG")
52 set_description( N_("M-JPEG camera demuxer") )
53 set_capability( "demux", 5 )
54 set_callbacks( Open, Close )
55 set_category( CAT_INPUT )
56 set_subcategory( SUBCAT_INPUT_DEMUX )
57 add_float( "mjpeg-fps", 0.0, FPS_TEXT, FPS_LONGTEXT, false )
60 /*****************************************************************************
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 );
74 mtime_t i_still_length;
77 mtime_t i_frame_length;
79 int i_frame_size_estimate;
80 const uint8_t *p_peek;
85 /*****************************************************************************
86 * Peek: Helper function to peek data with incremental size.
87 * \return false if peek no more data, true otherwise.
88 *****************************************************************************/
89 static bool Peek( demux_t *p_demux, bool b_first )
92 demux_sys_t *p_sys = p_demux->p_sys;
96 p_sys->i_data_peeked = 0;
98 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
100 p_sys->i_frame_size_estimate += 5120;
102 i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
103 p_sys->i_frame_size_estimate );
104 if( i_data == p_sys->i_data_peeked )
106 msg_Warn( p_demux, "no more data" );
109 p_sys->i_data_peeked = i_data;
112 msg_Warn( p_demux, "cannot peek data" );
118 /*****************************************************************************
119 * GetLine: Internal function used to dup a line of string from the buffer
120 *****************************************************************************/
121 static char* GetLine( demux_t *p_demux, int *p_pos )
123 demux_sys_t *p_sys = p_demux->p_sys;
124 const uint8_t *p_buf;
129 while( *p_pos >= p_sys->i_data_peeked )
131 if( ! Peek( p_demux, false ) )
136 p_buf = p_sys->p_peek + *p_pos;
137 i_size = p_sys->i_data_peeked - *p_pos;
139 while( p_buf[i] != '\n' )
144 if( ! Peek( p_demux, false ) )
148 p_buf = p_sys->p_peek + *p_pos;
149 i_size = p_sys->i_data_peeked - *p_pos;
153 if( i > 0 && '\r' == p_buf[i - 1] )
157 p_line = malloc( i + 1 );
160 strncpy ( p_line, (char*)p_buf, i );
162 // msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
166 /*****************************************************************************
167 * CheckMimeHeader: Internal function used to verify and skip mime header
168 * \param p_header_size Return size of MIME header, 0 if no MIME header
169 * detected, minus value if error
170 * \return true if content type is image/jpeg, false otherwise
171 *****************************************************************************/
172 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
178 demux_sys_t *p_sys = p_demux->p_sys;
180 if( !Peek( p_demux, true ) )
182 msg_Err( p_demux, "cannot peek" );
186 if( p_sys->i_data_peeked < 5)
188 msg_Err( p_demux, "data shortage" );
192 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
193 && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
198 i_pos = *p_sys->p_peek == '-' ? 2 : 4;
199 psz_line = GetLine( p_demux, &i_pos );
200 if( NULL == psz_line )
202 msg_Err( p_demux, "no EOL" );
207 /* Read the separator and remember it if not yet stored */
208 if( p_sys->psz_separator == NULL )
210 p_sys->psz_separator = psz_line;
211 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
212 p_sys->psz_separator );
216 if( strcmp( psz_line, p_sys->psz_separator ) )
218 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
219 p_sys->psz_separator );
224 psz_line = GetLine( p_demux, &i_pos );
225 while( psz_line && *psz_line )
227 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
229 p_ch = psz_line + 13;
230 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
231 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
233 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
243 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
246 psz_line = GetLine( p_demux, &i_pos );
249 if( NULL == psz_line )
251 msg_Err( p_demux, "no EOL" );
258 *p_header_size = i_pos;
262 static int SendBlock( demux_t *p_demux, int i )
264 demux_sys_t *p_sys = p_demux->p_sys;
267 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
269 msg_Warn( p_demux, "cannot read data" );
273 if( !p_sys->i_frame_length || !p_sys->i_time )
275 p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
279 p_block->i_dts = p_block->i_pts = VLC_TS_0 + p_sys->i_time;
280 p_sys->i_time += p_sys->i_frame_length;
284 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
285 es_out_Send( p_demux->out, p_sys->p_es, p_block );
289 p_sys->i_still_end = mdate() + p_sys->i_still_length;
295 /*****************************************************************************
296 * Open: check file and initializes structures
297 *****************************************************************************/
298 static int Open( vlc_object_t * p_this )
300 demux_t *p_demux = (demux_t*)p_this;
303 bool b_matched = false;
306 p_demux->pf_control = Control;
307 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
312 p_sys->psz_separator = NULL;
313 p_sys->i_frame_size_estimate = 15 * 1024;
315 b_matched = CheckMimeHeader( p_demux, &i_size);
318 p_demux->pf_demux = MimeDemux;
319 stream_Read( p_demux->s, NULL, i_size );
321 else if( 0 == i_size )
323 /* 0xffd8 identify a JPEG SOI */
324 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
326 msg_Dbg( p_demux, "JPEG SOI marker detected" );
327 p_demux->pf_demux = MjpgDemux;
341 f_fps = var_CreateGetFloat( p_demux, "mjpeg-fps" );
342 p_sys->i_frame_length = 0;
344 /* Check for jpeg file extension */
345 p_sys->b_still = false;
346 p_sys->i_still_end = 0;
347 if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
348 demux_IsPathExtension( p_demux, ".jpg" ) )
350 p_sys->b_still = true;
353 p_sys->i_still_length = 1000000.0 / f_fps;
357 /* Defaults to 1fps */
358 p_sys->i_still_length = 1000000;
363 p_sys->i_frame_length = 1000000.0 / f_fps;
366 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
367 p_sys->fmt.i_codec = VLC_CODEC_MJPG;
369 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
377 /*****************************************************************************
378 * Demux: read packet and send them to decoders
379 *****************************************************************************
380 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
381 *****************************************************************************/
382 static int MjpgDemux( demux_t *p_demux )
384 demux_sys_t *p_sys = p_demux->p_sys;
387 if( p_sys->b_still && p_sys->i_still_end )
389 /* Still frame, wait until the pause delay is gone */
390 mwait( p_sys->i_still_end );
391 p_sys->i_still_end = 0;
395 if( !Peek( p_demux, true ) )
397 msg_Warn( p_demux, "cannot peek data" );
400 if( p_sys->i_data_peeked < 4 )
402 msg_Warn( p_demux, "data shortage" );
407 while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
409 if( 0xFF == p_sys->p_peek[i-1] && 0xD8 == p_sys->p_peek[i] )
412 msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
415 if( i >= p_sys->i_data_peeked )
417 msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
418 p_sys->i_data_peeked );
419 if( !Peek( p_demux, false ) )
421 msg_Warn( p_demux, "no more data is available at the moment" );
428 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
431 if( p_sys->i_level > 0 )
433 return SendBlock( p_demux, i );
436 static int MimeDemux( demux_t *p_demux )
438 demux_sys_t *p_sys = p_demux->p_sys;
441 bool b_match = CheckMimeHeader( p_demux, &i_size );
445 stream_Read( p_demux->s, NULL, i_size );
447 else if( i_size < 0 )
453 // No MIME header, assume OK
457 if( !Peek( p_demux, true ) )
459 msg_Warn( p_demux, "cannot peek data" );
464 i_size = strlen( p_sys->psz_separator ) + 2;
465 if( p_sys->i_data_peeked < i_size )
467 msg_Warn( p_demux, "data shortage" );
473 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
477 if( i_size >= p_sys->i_data_peeked )
479 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
480 "data", p_sys->i_data_peeked );
482 if( !Peek( p_demux, false ) )
484 msg_Warn( p_demux, "no more data is available at the "
491 if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
492 strlen( p_sys->psz_separator ) ) )
503 msg_Err( p_demux, "discard non-JPEG part" );
504 stream_Read( p_demux->s, NULL, i );
508 return SendBlock( p_demux, i );
511 /*****************************************************************************
512 * Close: frees unused data
513 *****************************************************************************/
514 static void Close ( vlc_object_t * p_this )
516 demux_t *p_demux = (demux_t*)p_this;
517 demux_sys_t *p_sys = p_demux->p_sys;
519 free( p_sys->psz_separator );
523 /*****************************************************************************
525 *****************************************************************************/
526 static int Control( demux_t *p_demux, int i_query, va_list args )
528 return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );