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