]> git.sesse.net Git - vlc/blob - modules/demux/mjpeg.c
* modules/demux/wav.c: misc improvements.
[vlc] / modules / demux / mjpeg.c
1 /*****************************************************************************
2  * mjpeg.c : demuxes mjpeg webcam http streams
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: mjpeg.c 7196 2004-03-29 21:29:31Z fenrir $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34
35 #include <codecs.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 #define FPS_TEXT N_("Frames per Second")
44 #define FPS_LONGTEXT N_("Allows you to set the desired frame rate when " \
45     "playing from files, use 0 for live.")
46
47 vlc_module_begin();
48     set_description( _("JPEG camera demuxer") );
49     set_capability( "demux2", 5 );
50     set_callbacks( Open, Close );
51     add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
52 vlc_module_end();
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int MimeDemux( demux_t * );
58 static int MjpgDemux( demux_t * );
59 static int Control( demux_t *, int i_query, va_list args );
60
61 struct demux_sys_t
62 {
63     es_format_t     fmt;
64     es_out_id_t     *p_es;
65
66     vlc_bool_t      b_still;
67     mtime_t         i_still_end;
68
69     mtime_t         i_time;
70     mtime_t         i_frame_length;
71     char            *psz_separator;
72     int             i_frame_size_estimate;
73     uint8_t         *p_peek;
74     int             i_data_peeked;
75 };
76
77 /*****************************************************************************
78  * Peek: Helper function to peek data with incremental size. 
79  * \return VLC_FALSE if peek no more data, VLC_TRUE otherwise.
80  *****************************************************************************/
81 static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
82 {
83     int i_data;
84     demux_sys_t *p_sys = p_demux->p_sys;
85
86     if( b_first )
87     {
88         p_sys->i_data_peeked = 0;
89     }
90     else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
91     {
92         p_sys->i_frame_size_estimate += 5120;
93     }
94     i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
95                           p_sys->i_frame_size_estimate );
96     if( i_data == p_sys->i_data_peeked )
97     {
98         msg_Warn( p_demux, "no more data" );
99         return VLC_FALSE;
100     }
101     p_sys->i_data_peeked = i_data;
102     if( i_data <= 0 )
103     {
104         msg_Warn( p_demux, "cannot peek data" );
105         return VLC_FALSE;
106     }
107     return VLC_TRUE;
108 }
109
110 /*****************************************************************************
111  * GetLine: Internal function used to dup a line of string from the buffer
112  *****************************************************************************/
113 static char* GetLine( demux_t *p_demux, int *p_pos )
114 {
115     demux_sys_t *p_sys = p_demux->p_sys;
116     uint8_t     *p_buf;
117     int         i_size;
118     int         i;
119     char        *p_line;
120
121     while( *p_pos > p_sys->i_data_peeked )
122     {
123         if( ! Peek( p_demux, VLC_FALSE ) )
124         {
125             return NULL;
126         }
127     }
128     p_buf = p_sys->p_peek + *p_pos;
129     i_size = p_sys->i_data_peeked - *p_pos;
130     i = 0;
131     while( p_buf[i] != '\n' )
132     {
133         i++;
134         if( i == i_size )
135         {
136             if( ! Peek( p_demux, VLC_FALSE ) )
137             {
138                 return NULL;
139             }
140         }
141         p_buf = p_sys->p_peek + *p_pos;
142         i_size = p_sys->i_data_peeked - *p_pos;
143     }
144     *p_pos += ( i + 1 );
145     if( i > 0 && '\r' == p_buf[i - 1] )
146     {
147         i--;
148     }
149     p_line = malloc( i + 1 );
150     if( NULL == p_line )
151     {
152         msg_Err( p_demux, "out of memory" );
153         return NULL;
154     }
155     strncpy ( p_line, p_buf, i );
156     p_line[i] = '\0';
157 //    msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
158     return p_line;
159 }
160
161 /*****************************************************************************
162  * CheckMimeHeader: Internal function used to verify and skip mime header
163  * \param p_header_size Return size of MIME header, 0 if no MIME header
164  * detected, minus value if error
165  * \return VLC_TRUE if content type is image/jpeg, VLC_FALSE otherwise
166  *****************************************************************************/
167 static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
168 {
169     vlc_bool_t  b_jpeg = VLC_FALSE;
170     int         i_pos;
171     char        *psz_line;
172     char        *p_ch;
173     demux_sys_t *p_sys = p_demux->p_sys;
174
175     if( !Peek( p_demux, VLC_TRUE ) )
176     {
177         msg_Err( p_demux, "cannot peek" );
178         *p_header_size = -1;
179         return VLC_FALSE;
180     }
181     if( p_sys->i_data_peeked < 3)
182     {
183         msg_Err( p_demux, "data shortage" );
184         *p_header_size = -2;
185         return VLC_FALSE;
186     }
187     if( strncmp( p_sys->p_peek, "--", 2 ) )
188     {
189         *p_header_size = 0;
190         return VLC_FALSE;
191     }
192     i_pos = 2;
193     psz_line = GetLine( p_demux, &i_pos );
194     if( NULL == psz_line )
195     {
196         msg_Err( p_demux, "no EOL" );
197         *p_header_size = -3;
198         return VLC_FALSE;
199     }
200     if( NULL == p_sys->psz_separator )
201     {
202         p_sys->psz_separator = psz_line;
203         msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
204                  p_sys->psz_separator );
205     }
206     else
207     {
208         if( strcmp( psz_line, p_sys->psz_separator ) )
209         {
210             msg_Warn( p_demux, "separator %s does not match %s", psz_line,
211                       p_sys->psz_separator );
212         }
213         free( psz_line );
214     }
215     psz_line = GetLine( p_demux, &i_pos );
216     while( psz_line && *psz_line )
217     {
218         if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
219         {
220             p_ch = psz_line + 13;
221             while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
222             if( strncasecmp( p_ch, "image/jpeg", 10 ) )
223             {
224                 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
225                 b_jpeg = VLC_FALSE;
226             }
227             else
228             {
229                 b_jpeg = VLC_TRUE;
230             }
231         }
232         else
233         {
234             msg_Dbg( p_demux, "Discard MIME header: %s", psz_line );
235         }
236         free( psz_line );
237         psz_line = GetLine( p_demux, &i_pos );
238     }
239
240     if( NULL == psz_line )
241     {
242         msg_Err( p_demux, "no EOL" );
243         *p_header_size = -3;
244         return VLC_FALSE;
245     }
246
247     free( psz_line );
248
249     *p_header_size = i_pos;
250     return b_jpeg;
251 }
252
253 static int SendBlock( demux_t *p_demux, int i )
254 {
255     demux_sys_t *p_sys = p_demux->p_sys;
256     block_t     *p_block;
257
258     if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
259     {
260         msg_Warn( p_demux, "cannot read data" );
261         return 0;
262     }
263
264     if( !p_sys->i_frame_length || !p_sys->i_time )
265     {
266         p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
267     }
268     else
269     {
270         p_block->i_dts = p_block->i_pts = p_sys->i_time;
271         p_sys->i_time += p_sys->i_frame_length;
272     }
273
274     /* set PCR */
275     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
276     es_out_Send( p_demux->out, p_sys->p_es, p_block );
277
278     if( p_sys->b_still )
279     {
280         p_sys->i_still_end = mdate() + I64C(5000000);
281     }
282
283     return 1;
284 }
285
286 /*****************************************************************************
287  * Open: check file and initializes structures
288  *****************************************************************************/
289 static int Open( vlc_object_t * p_this )
290 {
291     demux_t     *p_demux = (demux_t*)p_this;
292     demux_sys_t *p_sys;
293     int         i_size;
294     int         b_matched = VLC_FALSE;
295     vlc_value_t val;
296     char *psz_ext;
297
298     p_demux->pf_control = Control;
299     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
300     p_sys->p_es         = NULL;
301     p_sys->i_time       = 0;
302
303     p_sys->psz_separator = NULL;
304     p_sys->i_frame_size_estimate = 15 * 1024;
305
306     b_matched = CheckMimeHeader( p_demux, &i_size);
307     if( b_matched )
308     {
309         p_demux->pf_demux = MimeDemux;
310         stream_Read( p_demux->s, NULL, i_size );
311     }
312     else if( 0 == i_size )
313     {
314         /* 0xffd8 identify a JPEG SOI */
315         if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
316         {
317             msg_Dbg( p_demux, "JPEG SOI marker detected" );
318             p_demux->pf_demux = MjpgDemux;
319         }
320         else
321         {
322             goto error;
323         }
324     }
325     else
326     {
327         goto error;
328     }
329
330     /* Check for jpeg file extension */
331     p_sys->b_still = VLC_FALSE;
332     p_sys->i_still_end = 0;
333     psz_ext = strrchr( p_demux->psz_path, '.' );
334     if( psz_ext && ( !strcasecmp( psz_ext, ".jpeg" ) ||
335                      !strcasecmp( psz_ext, ".jpg" ) ) )
336     {
337         p_sys->b_still = VLC_TRUE;
338     }
339
340     var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
341     var_Get( p_demux, "mjpeg-fps", &val );
342     p_sys->i_frame_length = 0;
343     if( val.f_float )
344     {
345         p_sys->i_frame_length = 1000000.0 / val.f_float;
346     }
347
348     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
349     p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
350
351     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
352     return VLC_SUCCESS;
353
354 error:
355     free( p_sys );
356     return VLC_EGENERIC;
357 }
358
359 /*****************************************************************************
360  * Demux: read packet and send them to decoders
361  *****************************************************************************
362  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
363  *****************************************************************************/
364 static int MjpgDemux( demux_t *p_demux )
365 {
366     demux_sys_t *p_sys = p_demux->p_sys;
367     int i;
368
369     if( p_sys->b_still && p_sys->i_still_end && p_sys->i_still_end < mdate() )
370     {
371         /* Still frame, wait until the pause delay is gone */
372         p_sys->i_still_end = 0;
373     }
374     else if( p_sys->b_still && p_sys->i_still_end )
375     {
376         msleep( 40000 );
377         return 1;
378     }
379
380     if( !Peek( p_demux, VLC_TRUE ) )
381     {
382         msg_Warn( p_demux, "cannot peek data" );
383         return 0;
384     }
385     if( p_sys->i_data_peeked < 4 )
386     {
387         msg_Warn( p_demux, "data shortage" );
388         return 0;
389     }
390     i = 3;
391     while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
392     {
393         i++;
394         if( i >= p_sys->i_data_peeked )
395         {
396             msg_Dbg( p_demux, "Did not find JPEG EOI in %d bytes",
397                      p_sys->i_data_peeked );
398             if( !Peek( p_demux, VLC_FALSE ) )
399             {
400                 msg_Warn( p_demux, "No more data is available at the moment" );
401                 return 0;
402             }
403         }
404     }
405     i++;
406
407     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
408     return SendBlock( p_demux, i );
409 }
410
411 static int MimeDemux( demux_t *p_demux )
412 {
413     demux_sys_t *p_sys = p_demux->p_sys;
414     int         i_size, i;
415     vlc_bool_t  b_match;
416     vlc_bool_t  b_done;
417
418     b_match = CheckMimeHeader( p_demux, &i_size );
419     if( i_size > 0 )
420     {
421         stream_Read( p_demux->s, NULL, i_size );
422     }
423     else if( i_size < 0 )
424     {
425         return 0;
426     }
427     else
428     {
429         // No MIME header, assume OK
430         b_match = VLC_TRUE;
431     }
432
433     if( !Peek( p_demux, VLC_TRUE ) )
434     {
435         msg_Warn( p_demux, "cannot peek data" );
436         return 0;
437     }
438     i = 0;
439     i_size = strlen( p_sys->psz_separator ) + 2;
440     if( p_sys->i_data_peeked < i_size )
441     {
442         msg_Warn( p_demux, "data shortage" );
443         return 0;
444     }
445     b_done = VLC_FALSE;
446     while( !b_done )
447     {
448         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
449         {
450             i++;
451             i_size++;
452             if( i_size >= p_sys->i_data_peeked )
453             {
454                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
455                          "data", p_sys->i_data_peeked );
456
457                 if( !Peek( p_demux, VLC_FALSE ) )
458                 {
459                     msg_Warn( p_demux, "No more data is available at the "
460                               "moment" );
461                     return 0;
462                 }
463             }
464         }
465         if( !strncmp( p_sys->psz_separator, p_sys->p_peek + i + 2,
466                       strlen( p_sys->psz_separator ) ) )
467         {
468             b_done = VLC_TRUE;
469         }
470         else
471         {
472             i++;
473             i_size++;
474         }
475     }
476
477     if( !b_match )
478     {
479         msg_Err( p_demux, "Discard non-JPEG part" );
480         stream_Read( p_demux->s, NULL, i );
481         return 0;
482     }
483
484     return SendBlock( p_demux, i );
485 }
486
487 /*****************************************************************************
488  * Close: frees unused data
489  *****************************************************************************/
490 static void Close ( vlc_object_t * p_this )
491 {
492     demux_t     *p_demux = (demux_t*)p_this;
493     demux_sys_t *p_sys  = p_demux->p_sys;
494
495     if( p_sys->psz_separator )
496     {
497         free( p_sys->psz_separator );
498     }
499     free( p_sys );
500 }
501
502 /*****************************************************************************
503  * Control:
504  *****************************************************************************/
505 static int Control( demux_t *p_demux, int i_query, va_list args )
506 {
507     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
508 }