]> git.sesse.net Git - vlc/blob - modules/demux/mjpeg.c
mediacodec: don't loop in GetOutput
[vlc] / modules / demux / mjpeg.c
1 /*****************************************************************************
2  * mjpeg.c : demuxes mjpeg webcam http streams
3  *****************************************************************************
4  * Copyright (C) 2004 VLC authors and VideoLAN
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 it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * 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 #include "mxpeg_helper.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
46 #define FPS_TEXT N_("Frames per Second")
47 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
48     "playing MJPEG from a file. Use 0 (this is the default value) for a " \
49     "live stream (from a camera).")
50
51 vlc_module_begin ()
52     set_shortname( "MJPEG")
53     set_description( N_("M-JPEG camera demuxer") )
54     set_capability( "demux", 5 )
55     set_callbacks( Open, Close )
56     set_category( CAT_INPUT )
57     set_subcategory( SUBCAT_INPUT_DEMUX )
58     add_float( "mjpeg-fps", 0.0, FPS_TEXT, FPS_LONGTEXT, false )
59 vlc_module_end ()
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int MimeDemux( demux_t * );
65 static int MjpgDemux( demux_t * );
66 static int Control( demux_t *, int i_query, va_list args );
67
68 struct demux_sys_t
69 {
70     es_format_t     fmt;
71     es_out_id_t     *p_es;
72
73     bool            b_still;
74     mtime_t         i_still_end;
75     mtime_t         i_time;
76     mtime_t         i_frame_length;
77     char            *psz_separator;
78     int             i_frame_size_estimate;
79     const uint8_t   *p_peek;
80     int             i_data_peeked;
81     int             i_level;
82 };
83
84 /*****************************************************************************
85  * Peek: Helper function to peek data with incremental size.
86  * \return false if peek no more data, true otherwise.
87  *****************************************************************************/
88 static bool Peek( demux_t *p_demux, bool b_first )
89 {
90     int i_data;
91     demux_sys_t *p_sys = p_demux->p_sys;
92
93     if( b_first )
94     {
95         p_sys->i_data_peeked = 0;
96     }
97     else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
98     {
99         p_sys->i_frame_size_estimate += 5120;
100     }
101     i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
102                           p_sys->i_frame_size_estimate );
103     if( i_data == p_sys->i_data_peeked )
104     {
105         msg_Warn( p_demux, "no more data" );
106         return false;
107     }
108     p_sys->i_data_peeked = i_data;
109     if( i_data <= 0 )
110     {
111         msg_Warn( p_demux, "cannot peek data" );
112         return false;
113     }
114     return true;
115 }
116
117 /*****************************************************************************
118  * GetLine: Internal function used to dup a line of string from the buffer
119  *****************************************************************************/
120 static char* GetLine( demux_t *p_demux, int *p_pos )
121 {
122     demux_sys_t *p_sys = p_demux->p_sys;
123     const uint8_t *p_buf;
124     int         i_size;
125     int         i;
126     char        *p_line;
127
128     while( *p_pos >= p_sys->i_data_peeked )
129     {
130         if( ! Peek( p_demux, false ) )
131         {
132             return NULL;
133         }
134     }
135     p_buf = p_sys->p_peek + *p_pos;
136     i_size = p_sys->i_data_peeked - *p_pos;
137     i = 0;
138     while( p_buf[i] != '\n' )
139     {
140         i++;
141         if( i == i_size )
142         {
143             if( ! Peek( p_demux, false ) )
144             {
145                 return NULL;
146             }
147             p_buf = p_sys->p_peek + *p_pos;
148             i_size = p_sys->i_data_peeked - *p_pos;
149         }
150     }
151     *p_pos += i + 1;
152     if( i > 0 && p_buf[i - 1] == '\r' )
153     {
154         i--;
155     }
156     p_line = malloc( i + 1 );
157     if( unlikely( p_line == NULL ) )
158         return NULL;
159     strncpy ( p_line, (char*)p_buf, i );
160     p_line[i] = '\0';
161     return p_line;
162 }
163
164 /*****************************************************************************
165  * CheckMimeHeader: Internal function used to verify and skip mime header
166  * \param p_header_size Return size of MIME header, 0 if no MIME header
167  * detected, minus value if error
168  * \return true if content type is image/jpeg, false otherwise
169  *****************************************************************************/
170 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
171 {
172     bool        b_jpeg = false;
173     int         i_pos = 0;
174     char        *psz_line;
175     char        *p_ch;
176     demux_sys_t *p_sys = p_demux->p_sys;
177
178     *p_header_size = -1;
179     if( !Peek( p_demux, true ) )
180     {
181         msg_Err( p_demux, "cannot peek" );
182         return false;
183     }
184     if( p_sys->i_data_peeked < 5)
185     {
186         msg_Err( p_demux, "data shortage" );
187         return false;
188     }
189     if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
190         && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
191     {
192         *p_header_size = 0;
193         return false;
194     }
195     else
196     {
197         i_pos = *p_sys->p_peek == '-' ? 2 : 4;
198         psz_line = GetLine( p_demux, &i_pos );
199         if( NULL == psz_line )
200         {
201             msg_Err( p_demux, "no EOL" );
202             return false;
203         }
204
205         /* Read the separator and remember it if not yet stored */
206         if( p_sys->psz_separator == NULL )
207         {
208             p_sys->psz_separator = psz_line;
209             msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
210                      p_sys->psz_separator );
211         }
212         else
213         {
214             if( strcmp( psz_line, p_sys->psz_separator ) )
215             {
216                 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
217                           p_sys->psz_separator );
218             }
219             free( psz_line );
220         }
221     }
222
223     psz_line = GetLine( p_demux, &i_pos );
224     while( psz_line && *psz_line )
225     {
226         if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
227         {
228             p_ch = psz_line + 13;
229             while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
230             if( strncasecmp( p_ch, "image/jpeg", 10 ) )
231             {
232                 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
233                 b_jpeg = false;
234             }
235             else
236             {
237                 b_jpeg = true;
238             }
239         }
240         else
241         {
242             msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
243         }
244         free( psz_line );
245         psz_line = GetLine( p_demux, &i_pos );
246     }
247
248     if( NULL == psz_line )
249     {
250         msg_Err( p_demux, "no EOL" );
251         return false;
252     }
253
254     free( psz_line );
255
256     *p_header_size = i_pos;
257     return b_jpeg;
258 }
259
260 static int SendBlock( demux_t *p_demux, int i )
261 {
262     demux_sys_t *p_sys = p_demux->p_sys;
263     block_t     *p_block;
264
265     if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
266     {
267         msg_Warn( p_demux, "cannot read data" );
268         return 0;
269     }
270
271     if( p_sys->i_frame_length )
272     {
273         p_block->i_pts = p_sys->i_time;
274         p_sys->i_time += p_sys->i_frame_length;
275     }
276     else
277     {
278         p_block->i_pts = mdate();
279     }
280     p_block->i_dts = p_block->i_pts;
281
282     /* set PCR */
283     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
284     es_out_Send( p_demux->out, p_sys->p_es, p_block );
285
286     if( p_sys->b_still )
287         p_sys->i_still_end = mdate() + p_sys->i_frame_length;
288
289     return 1;
290 }
291
292 /*****************************************************************************
293  * Open: check file and initializes structures
294  *****************************************************************************/
295 static int Open( vlc_object_t * p_this )
296 {
297     demux_t     *p_demux = (demux_t*)p_this;
298     int         i_size;
299     bool        b_matched = false;
300
301     if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
302         // let avformat handle this case
303         return VLC_EGENERIC;
304
305     demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
306     if( unlikely(p_sys == NULL) )
307         return VLC_ENOMEM;
308
309     p_demux->pf_control = Control;
310     p_demux->p_sys      = p_sys;
311     p_sys->p_es         = NULL;
312     p_sys->i_time       = VLC_TS_0;
313     p_sys->i_level      = 0;
314
315     p_sys->psz_separator = NULL;
316     p_sys->i_frame_size_estimate = 15 * 1024;
317
318     char *content_type = stream_ContentType( p_demux->s );
319     if ( content_type )
320     {
321         //FIXME: this is not fully match to RFC
322         char* boundary = strstr( content_type, "boundary=" );
323         if( boundary )
324         {
325             boundary += strlen( "boundary=" );
326             size_t len = strlen( boundary );
327             if( len > 2 && boundary[0] == '"'
328                 && boundary[len-1] == '"' )
329             {
330                 boundary[len-1] = '\0';
331                 boundary++;
332             }
333             p_sys->psz_separator = strdup( boundary );
334             if( !p_sys->psz_separator )
335             {
336                 free( content_type );
337                 goto error;
338             }
339         }
340         free( content_type );
341     }
342
343     b_matched = CheckMimeHeader( p_demux, &i_size);
344     if( b_matched )
345     {
346         p_demux->pf_demux = MimeDemux;
347         stream_Read( p_demux->s, NULL, i_size );
348     }
349     else if( i_size == 0 )
350     {
351         /* 0xffd8 identify a JPEG SOI */
352         if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
353         {
354             msg_Dbg( p_demux, "JPEG SOI marker detected" );
355             p_demux->pf_demux = MjpgDemux;
356             p_sys->i_level++;
357         }
358         else
359         {
360             goto error;
361         }
362     }
363     else
364     {
365         goto error;
366     }
367
368     /* Frame rate */
369     float f_fps = var_InheritFloat( p_demux, "mjpeg-fps" );
370
371     p_sys->i_still_end = 0;
372     if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
373         demux_IsPathExtension( p_demux, ".jpg" ) )
374     {
375         /* Plain JPEG file = single still picture */
376         p_sys->b_still = true;
377         if( f_fps == 0.f )
378             /* Defaults to 1fps */
379             f_fps = 1.f;
380     }
381     else
382         p_sys->b_still = false;
383     p_sys->i_frame_length = f_fps ? (CLOCK_FREQ / f_fps) : 0;
384
385     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
386     p_sys->fmt.i_codec = VLC_CODEC_MJPG;
387
388     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
389     return VLC_SUCCESS;
390
391 error:
392     free( p_sys->psz_separator );
393     free( p_sys );
394     return VLC_EGENERIC;
395 }
396
397 /*****************************************************************************
398  * Demux: read packet and send them to decoders
399  *****************************************************************************
400  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
401  *****************************************************************************/
402 static int MjpgDemux( demux_t *p_demux )
403 {
404     demux_sys_t *p_sys = p_demux->p_sys;
405     int i;
406
407     if( p_sys->b_still && p_sys->i_still_end )
408     {
409         /* Still frame, wait until the pause delay is gone */
410         mwait( p_sys->i_still_end );
411         p_sys->i_still_end = 0;
412         return 1;
413     }
414
415     if( !Peek( p_demux, true ) )
416     {
417         msg_Warn( p_demux, "cannot peek data" );
418         return 0;
419     }
420     if( p_sys->i_data_peeked < 4 )
421     {
422         msg_Warn( p_demux, "data shortage" );
423         return 0;
424     }
425     i = 3;
426 FIND_NEXT_EOI:
427     while( !( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 ) )
428     {
429         if( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9  )
430         {
431             p_sys->i_level++;
432             msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
433         }
434         i++;
435         if( i >= p_sys->i_data_peeked )
436         {
437             msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
438                      p_sys->i_data_peeked );
439             if( !Peek( p_demux, false ) )
440             {
441                 msg_Warn( p_demux, "no more data is available at the moment" );
442                 return 0;
443             }
444         }
445     }
446     i++;
447
448     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
449     p_sys->i_level--;
450
451     if( p_sys->i_level > 0 )
452         goto FIND_NEXT_EOI;
453     return SendBlock( p_demux, i );
454 }
455
456 static int MimeDemux( demux_t *p_demux )
457 {
458     demux_sys_t *p_sys = p_demux->p_sys;
459     int         i_size, i;
460
461     bool  b_match = CheckMimeHeader( p_demux, &i_size );
462
463     if( i_size > 0 )
464     {
465         if( stream_Read( p_demux->s, NULL, i_size ) != i_size )
466             return 0;
467     }
468     else if( i_size < 0 )
469     {
470         return 0;
471     }
472     else
473     {
474         // No MIME header, assume OK
475         b_match = true;
476     }
477
478     if( !Peek( p_demux, true ) )
479     {
480         msg_Warn( p_demux, "cannot peek data" );
481         return 0;
482     }
483
484     i = 0;
485     i_size = strlen( p_sys->psz_separator ) + 2;
486     if( p_sys->i_data_peeked < i_size )
487     {
488         msg_Warn( p_demux, "data shortage" );
489         return 0;
490     }
491
492     for( ;; )
493     {
494         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
495         {
496             i++;
497             i_size++;
498             if( i_size >= p_sys->i_data_peeked )
499             {
500                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
501                          "data", p_sys->i_data_peeked );
502
503                 if( !Peek( p_demux, false ) )
504                 {
505                     msg_Warn( p_demux, "no more data is available at the "
506                               "moment" );
507                     return 0;
508                 }
509             }
510         }
511
512         /* Handle old and new style of separators */
513         if (!strncmp(p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
514                      strlen( p_sys->psz_separator ))
515          || ((strlen(p_sys->psz_separator) > 4)
516           && !strncmp(p_sys->psz_separator, "--", 2)
517           && !strncmp(p_sys->psz_separator, (char *)(p_sys->p_peek + i),
518                       strlen( p_sys->psz_separator))))
519         {
520             break;
521         }
522
523         i++;
524         i_size++;
525     }
526
527     if( !b_match )
528     {
529         msg_Err( p_demux, "discard non-JPEG part" );
530         stream_Read( p_demux->s, NULL, i );
531         return 0;
532     }
533
534     return SendBlock( p_demux, i );
535 }
536
537 /*****************************************************************************
538  * Close: frees unused data
539  *****************************************************************************/
540 static void Close ( vlc_object_t * p_this )
541 {
542     demux_t     *p_demux = (demux_t*)p_this;
543     demux_sys_t *p_sys  = p_demux->p_sys;
544
545     free( p_sys->psz_separator );
546     free( p_sys );
547 }
548
549 /*****************************************************************************
550  * Control:
551  *****************************************************************************/
552 static int Control( demux_t *p_demux, int i_query, va_list args )
553 {
554     return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
555 }