]> git.sesse.net Git - vlc/blob - modules/demux/mjpeg.c
Print codec_id on unsupported track in avformat demuxer.
[vlc] / modules / demux / mjpeg.c
1 /*****************************************************************************
2  * mjpeg.c : demuxes mjpeg webcam http streams
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44
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).")
49
50 vlc_module_begin ()
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 )
58 vlc_module_end ()
59
60 /*****************************************************************************
61  * Local prototypes
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 );
66
67 struct demux_sys_t
68 {
69     es_format_t     fmt;
70     es_out_id_t     *p_es;
71
72     bool      b_still;
73     mtime_t         i_still_end;
74     mtime_t         i_still_length;
75
76     mtime_t         i_time;
77     mtime_t         i_frame_length;
78     char            *psz_separator;
79     int             i_frame_size_estimate;
80     const uint8_t   *p_peek;
81     int             i_data_peeked;
82     int             i_level;
83 };
84
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 )
90 {
91     int i_data;
92     demux_sys_t *p_sys = p_demux->p_sys;
93
94     if( b_first )
95     {
96         p_sys->i_data_peeked = 0;
97     }
98     else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
99     {
100         p_sys->i_frame_size_estimate += 5120;
101     }
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 )
105     {
106         msg_Warn( p_demux, "no more data" );
107         return false;
108     }
109     p_sys->i_data_peeked = i_data;
110     if( i_data <= 0 )
111     {
112         msg_Warn( p_demux, "cannot peek data" );
113         return false;
114     }
115     return true;
116 }
117
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 )
122 {
123     demux_sys_t *p_sys = p_demux->p_sys;
124     const uint8_t *p_buf;
125     int         i_size;
126     int         i;
127     char        *p_line;
128
129     while( *p_pos >= p_sys->i_data_peeked )
130     {
131         if( ! Peek( p_demux, false ) )
132         {
133             return NULL;
134         }
135     }
136     p_buf = p_sys->p_peek + *p_pos;
137     i_size = p_sys->i_data_peeked - *p_pos;
138     i = 0;
139     while( p_buf[i] != '\n' )
140     {
141         i++;
142         if( i == i_size )
143         {
144             if( ! Peek( p_demux, false ) )
145             {
146                 return NULL;
147             }
148             p_buf = p_sys->p_peek + *p_pos;
149             i_size = p_sys->i_data_peeked - *p_pos;
150         }
151     }
152     *p_pos += ( i + 1 );
153     if( i > 0 && '\r' == p_buf[i - 1] )
154     {
155         i--;
156     }
157     p_line = malloc( i + 1 );
158     if( p_line == NULL )
159         return NULL;
160     strncpy ( p_line, (char*)p_buf, i );
161     p_line[i] = '\0';
162 //    msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
163     return p_line;
164 }
165
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 )
173 {
174     bool  b_jpeg = false;
175     int         i_pos;
176     char        *psz_line;
177     char        *p_ch;
178     demux_sys_t *p_sys = p_demux->p_sys;
179
180     if( !Peek( p_demux, true ) )
181     {
182         msg_Err( p_demux, "cannot peek" );
183         *p_header_size = -1;
184         return false;
185     }
186     if( p_sys->i_data_peeked < 5)
187     {
188         msg_Err( p_demux, "data shortage" );
189         *p_header_size = -2;
190         return false;
191     }
192     if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
193         && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
194     {
195         *p_header_size = 0;
196         return false;
197     }
198     i_pos = *p_sys->p_peek == '-' ? 2 : 4;
199     psz_line = GetLine( p_demux, &i_pos );
200     if( NULL == psz_line )
201     {
202         msg_Err( p_demux, "no EOL" );
203         *p_header_size = -3;
204         return false;
205     }
206
207     /* Read the separator and remember it if not yet stored */
208     if( p_sys->psz_separator == NULL )
209     {
210         p_sys->psz_separator = psz_line;
211         msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
212                  p_sys->psz_separator );
213     }
214     else
215     {
216         if( strcmp( psz_line, p_sys->psz_separator ) )
217         {
218             msg_Warn( p_demux, "separator %s does not match %s", psz_line,
219                       p_sys->psz_separator );
220         }
221         free( psz_line );
222     }
223
224     psz_line = GetLine( p_demux, &i_pos );
225     while( psz_line && *psz_line )
226     {
227         if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
228         {
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 ) )
232             {
233                 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
234                 b_jpeg = false;
235             }
236             else
237             {
238                 b_jpeg = true;
239             }
240         }
241         else
242         {
243             msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
244         }
245         free( psz_line );
246         psz_line = GetLine( p_demux, &i_pos );
247     }
248
249     if( NULL == psz_line )
250     {
251         msg_Err( p_demux, "no EOL" );
252         *p_header_size = -3;
253         return false;
254     }
255
256     free( psz_line );
257
258     *p_header_size = i_pos;
259     return b_jpeg;
260 }
261
262 static int SendBlock( demux_t *p_demux, int i )
263 {
264     demux_sys_t *p_sys = p_demux->p_sys;
265     block_t     *p_block;
266
267     if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
268     {
269         msg_Warn( p_demux, "cannot read data" );
270         return 0;
271     }
272
273     if( !p_sys->i_frame_length || !p_sys->i_time )
274     {
275         p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
276     }
277     else
278     {
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;
281     }
282
283     /* set PCR */
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 );
286
287     if( p_sys->b_still )
288     {
289         p_sys->i_still_end = mdate() + p_sys->i_still_length;
290     }
291
292     return 1;
293 }
294
295 /*****************************************************************************
296  * Open: check file and initializes structures
297  *****************************************************************************/
298 static int Open( vlc_object_t * p_this )
299 {
300     demux_t     *p_demux = (demux_t*)p_this;
301     demux_sys_t *p_sys;
302     int         i_size;
303     bool        b_matched = false;
304     float       f_fps;
305
306     p_sys = malloc( sizeof( demux_sys_t ) );
307     if( unlikely(p_sys == NULL) )
308         return VLC_ENOMEM;
309
310     p_demux->pf_control = Control;
311     p_demux->p_sys      = p_sys;
312     p_sys->p_es         = NULL;
313     p_sys->i_time       = 0;
314     p_sys->i_level      = 0;
315
316     p_sys->psz_separator = NULL;
317     p_sys->i_frame_size_estimate = 15 * 1024;
318
319     b_matched = CheckMimeHeader( p_demux, &i_size);
320     if( b_matched )
321     {
322         p_demux->pf_demux = MimeDemux;
323         stream_Read( p_demux->s, NULL, i_size );
324     }
325     else if( 0 == i_size )
326     {
327         /* 0xffd8 identify a JPEG SOI */
328         if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
329         {
330             msg_Dbg( p_demux, "JPEG SOI marker detected" );
331             p_demux->pf_demux = MjpgDemux;
332             p_sys->i_level++;
333         }
334         else
335         {
336             goto error;
337         }
338     }
339     else
340     {
341         goto error;
342     }
343
344
345     f_fps = var_CreateGetFloat( p_demux, "mjpeg-fps" );
346     p_sys->i_frame_length = 0;
347
348     /* Check for jpeg file extension */
349     p_sys->b_still = false;
350     p_sys->i_still_end = 0;
351     if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
352         demux_IsPathExtension( p_demux, ".jpg" ) )
353     {
354         p_sys->b_still = true;
355         if( f_fps )
356         {
357             p_sys->i_still_length = 1000000.0 / f_fps;
358         }
359         else
360         {
361             /* Defaults to 1fps */
362             p_sys->i_still_length = 1000000;
363         }
364     }
365     else if ( f_fps )
366     {
367         p_sys->i_frame_length = 1000000.0 / f_fps;
368     }
369
370     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
371     p_sys->fmt.i_codec = VLC_CODEC_MJPG;
372
373     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
374     return VLC_SUCCESS;
375
376 error:
377     free( p_sys );
378     return VLC_EGENERIC;
379 }
380
381 /*****************************************************************************
382  * Demux: read packet and send them to decoders
383  *****************************************************************************
384  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
385  *****************************************************************************/
386 static int MjpgDemux( demux_t *p_demux )
387 {
388     demux_sys_t *p_sys = p_demux->p_sys;
389     int i;
390
391     if( p_sys->b_still && p_sys->i_still_end )
392     {
393         /* Still frame, wait until the pause delay is gone */
394         mwait( p_sys->i_still_end );
395         p_sys->i_still_end = 0;
396         return 1;
397     }
398
399     if( !Peek( p_demux, true ) )
400     {
401         msg_Warn( p_demux, "cannot peek data" );
402         return 0;
403     }
404     if( p_sys->i_data_peeked < 4 )
405     {
406         msg_Warn( p_demux, "data shortage" );
407         return 0;
408     }
409     i = 3;
410 FIND_NEXT_EOI:
411     while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
412     {
413         if( 0xFF == p_sys->p_peek[i-1] && 0xD8 == p_sys->p_peek[i] )
414         {
415             p_sys->i_level++;
416             msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
417         }
418         i++;
419         if( i >= p_sys->i_data_peeked )
420         {
421             msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
422                      p_sys->i_data_peeked );
423             if( !Peek( p_demux, false ) )
424             {
425                 msg_Warn( p_demux, "no more data is available at the moment" );
426                 return 0;
427             }
428         }
429     }
430     i++;
431
432     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
433     p_sys->i_level--;
434
435     if( p_sys->i_level > 0 )
436         goto FIND_NEXT_EOI;
437     return SendBlock( p_demux, i );
438 }
439
440 static int MimeDemux( demux_t *p_demux )
441 {
442     demux_sys_t *p_sys = p_demux->p_sys;
443     int         i_size, i;
444
445     bool  b_match = CheckMimeHeader( p_demux, &i_size );
446
447     if( i_size > 0 )
448     {
449         stream_Read( p_demux->s, NULL, i_size );
450     }
451     else if( i_size < 0 )
452     {
453         return 0;
454     }
455     else
456     {
457         // No MIME header, assume OK
458         b_match = true;
459     }
460
461     if( !Peek( p_demux, true ) )
462     {
463         msg_Warn( p_demux, "cannot peek data" );
464         return 0;
465     }
466
467     i = 0;
468     i_size = strlen( p_sys->psz_separator ) + 2;
469     if( p_sys->i_data_peeked < i_size )
470     {
471         msg_Warn( p_demux, "data shortage" );
472         return 0;
473     }
474
475     for( ;; )
476     {
477         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
478         {
479             i++;
480             i_size++;
481             if( i_size >= p_sys->i_data_peeked )
482             {
483                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
484                          "data", p_sys->i_data_peeked );
485
486                 if( !Peek( p_demux, false ) )
487                 {
488                     msg_Warn( p_demux, "no more data is available at the "
489                               "moment" );
490                     return 0;
491                 }
492             }
493         }
494
495         if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
496                       strlen( p_sys->psz_separator ) ) )
497         {
498             break;
499         }
500
501         i++;
502         i_size++;
503     }
504
505     if( !b_match )
506     {
507         msg_Err( p_demux, "discard non-JPEG part" );
508         stream_Read( p_demux->s, NULL, i );
509         return 0;
510     }
511
512     return SendBlock( p_demux, i );
513 }
514
515 /*****************************************************************************
516  * Close: frees unused data
517  *****************************************************************************/
518 static void Close ( vlc_object_t * p_this )
519 {
520     demux_t     *p_demux = (demux_t*)p_this;
521     demux_sys_t *p_sys  = p_demux->p_sys;
522
523     free( p_sys->psz_separator );
524     free( p_sys );
525 }
526
527 /*****************************************************************************
528  * Control:
529  *****************************************************************************/
530 static int Control( demux_t *p_demux, int i_query, va_list args )
531 {
532     return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
533 }