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