demux: libavi: force chunk type check
[vlc.git] / modules / demux / image.c
blob90cfc3ccb4f0802370aeedf467293cbfe6d4aac1
1 /*****************************************************************************
2 * image.c: Image demuxer
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_image.h>
36 #include "mxpeg_helper.h"
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 static int Open (vlc_object_t *);
42 static void Close(vlc_object_t *);
44 #define ID_TEXT N_("ES ID")
45 #define ID_LONGTEXT N_( \
46 "Set the ID of the elementary stream")
48 #define GROUP_TEXT N_("Group")
49 #define GROUP_LONGTEXT N_(\
50 "Set the group of the elementary stream")
52 #define DECODE_TEXT N_("Decode")
53 #define DECODE_LONGTEXT N_( \
54 "Decode at the demuxer stage")
56 #define CHROMA_TEXT N_("Forced chroma")
57 #define CHROMA_LONGTEXT N_( \
58 "If non empty and image-decode is true, the image will be " \
59 "converted to the specified chroma.")
61 #define DURATION_TEXT N_("Duration in seconds")
62 #define DURATION_LONGTEXT N_( \
63 "Duration in seconds before simulating an end of file. " \
64 "A negative value means an unlimited play time.")
66 #define FPS_TEXT N_("Frame rate")
67 #define FPS_LONGTEXT N_( \
68 "Frame rate of the elementary stream produced.")
70 #define RT_TEXT N_("Real-time")
71 #define RT_LONGTEXT N_( \
72 "Use real-time mode suitable for being used as a master input and " \
73 "real-time input slaves.")
75 vlc_module_begin()
76 set_description(N_("Image demuxer"))
77 set_shortname(N_("Image"))
78 set_category(CAT_INPUT)
79 set_subcategory(SUBCAT_INPUT_DEMUX)
80 add_integer("image-id", -1, ID_TEXT, ID_LONGTEXT, true)
81 change_safe()
82 add_integer("image-group", 0, GROUP_TEXT, GROUP_LONGTEXT, true)
83 change_safe()
84 add_bool("image-decode", true, DECODE_TEXT, DECODE_LONGTEXT, true)
85 change_safe()
86 add_string("image-chroma", "", CHROMA_TEXT, CHROMA_LONGTEXT, true)
87 change_safe()
88 add_float("image-duration", 10, DURATION_TEXT, DURATION_LONGTEXT, false)
89 change_safe()
90 add_string("image-fps", "10/1", FPS_TEXT, FPS_LONGTEXT, true)
91 change_safe()
92 add_bool("image-realtime", false, RT_TEXT, RT_LONGTEXT, true)
93 change_safe()
94 set_capability("demux", 10)
95 set_callbacks(Open, Close)
96 vlc_module_end()
98 /*****************************************************************************
99 * Local prototypes
100 *****************************************************************************/
101 struct demux_sys_t
103 block_t *data;
104 es_out_id_t *es;
105 mtime_t duration;
106 bool is_realtime;
107 mtime_t pts_origin;
108 mtime_t pts_next;
109 date_t pts;
112 static block_t *Load(demux_t *demux)
114 const unsigned max_size = 4096 * 4096 * 8;
115 uint64_t size;
117 if (vlc_stream_GetSize(demux->s, &size) == VLC_SUCCESS) {
118 if (size > max_size) {
119 msg_Err(demux, "image too large (%"PRIu64" > %u), rejected",
120 size, max_size);
121 return NULL;
123 } else
124 size = max_size;
126 block_t *block = block_Alloc(size);
127 if (block == NULL)
128 return NULL;
130 ssize_t val = vlc_stream_Read(demux->s, block->p_buffer, size);
131 if (val < 0) {
132 block_Release(block);
133 return NULL;
136 block->i_buffer = val;
137 return block;
140 static block_t *Decode(demux_t *demux,
141 video_format_t *fmt, vlc_fourcc_t chroma, block_t *data)
143 image_handler_t *handler = image_HandlerCreate(demux);
144 if (!handler) {
145 block_Release(data);
146 return NULL;
149 video_format_t decoded;
150 video_format_Init(&decoded, chroma);
152 picture_t *image = image_Read(handler, data, fmt, &decoded);
153 image_HandlerDelete(handler);
155 if (!image)
156 return NULL;
158 video_format_Clean(fmt);
159 *fmt = decoded;
161 size_t size = 0;
162 for (int i = 0; i < image->i_planes; i++)
163 size += image->p[i].i_pitch * image->p[i].i_lines;
165 data = block_Alloc(size);
166 if (!data) {
167 picture_Release(image);
168 return NULL;
171 size_t offset = 0;
172 for (int i = 0; i < image->i_planes; i++) {
173 const plane_t *src = &image->p[i];
174 for (int y = 0; y < src->i_visible_lines; y++) {
175 memcpy(&data->p_buffer[offset],
176 &src->p_pixels[y * src->i_pitch],
177 src->i_visible_pitch);
178 offset += src->i_visible_pitch;
182 picture_Release(image);
183 return data;
186 static int Demux(demux_t *demux)
188 demux_sys_t *sys = demux->p_sys;
190 if (!sys->data)
191 return 0;
193 mtime_t deadline;
194 const mtime_t pts_first = sys->pts_origin + date_Get(&sys->pts);
195 if (sys->pts_next > VLC_TS_INVALID) {
196 deadline = sys->pts_next;
197 } else if (sys->is_realtime) {
198 deadline = mdate();
199 const mtime_t max_wait = CLOCK_FREQ / 50;
200 if (deadline + max_wait < pts_first) {
201 es_out_SetPCR(demux->out, deadline);
202 /* That's ugly, but not yet easily fixable */
203 mwait(deadline + max_wait);
204 return 1;
206 } else {
207 deadline = 1 + pts_first;
210 for (;;) {
211 const mtime_t pts = sys->pts_origin + date_Get(&sys->pts);
212 if (sys->duration >= 0 && pts >= sys->pts_origin + sys->duration)
213 return 0;
215 if (pts >= deadline)
216 return 1;
218 block_t *data = block_Duplicate(sys->data);
219 if (!data)
220 return -1;
222 data->i_dts =
223 data->i_pts = VLC_TS_0 + pts;
224 es_out_SetPCR(demux->out, data->i_pts);
225 es_out_Send(demux->out, sys->es, data);
227 date_Increment(&sys->pts, 1);
231 static int Control(demux_t *demux, int query, va_list args)
233 demux_sys_t *sys = demux->p_sys;
235 switch (query) {
236 case DEMUX_CAN_SEEK:
237 *va_arg(args, bool *) = sys->duration >= 0 && !sys->is_realtime;
238 return VLC_SUCCESS;
239 case DEMUX_GET_POSITION: {
240 double *position = va_arg(args, double *);
241 if (sys->duration > 0)
242 *position = date_Get(&sys->pts) / (double)sys->duration;
243 else
244 *position = 0;
245 return VLC_SUCCESS;
247 case DEMUX_SET_POSITION: {
248 if (sys->duration < 0 || sys->is_realtime)
249 return VLC_EGENERIC;
250 double position = va_arg(args, double);
251 date_Set(&sys->pts, position * sys->duration);
252 return VLC_SUCCESS;
254 case DEMUX_GET_TIME: {
255 int64_t *time = va_arg(args, int64_t *);
256 *time = sys->pts_origin + date_Get(&sys->pts);
257 return VLC_SUCCESS;
259 case DEMUX_SET_TIME: {
260 if (sys->duration < 0 || sys->is_realtime)
261 return VLC_EGENERIC;
262 int64_t time = va_arg(args, int64_t);
263 date_Set(&sys->pts, VLC_CLIP(time - sys->pts_origin, 0, sys->duration));
264 return VLC_SUCCESS;
266 case DEMUX_SET_NEXT_DEMUX_TIME: {
267 int64_t pts_next = VLC_TS_0 + va_arg(args, int64_t);
268 if (sys->pts_next <= VLC_TS_INVALID)
269 sys->pts_origin = pts_next;
270 sys->pts_next = pts_next;
271 return VLC_SUCCESS;
273 case DEMUX_GET_LENGTH: {
274 int64_t *length = va_arg(args, int64_t *);
275 *length = __MAX(sys->duration, 0);
276 return VLC_SUCCESS;
278 case DEMUX_GET_FPS: {
279 double *fps = va_arg(args, double *);
280 *fps = (double)sys->pts.i_divider_num / sys->pts.i_divider_den;
281 return VLC_SUCCESS;
283 case DEMUX_GET_META:
284 case DEMUX_HAS_UNSUPPORTED_META:
285 case DEMUX_GET_ATTACHMENTS:
286 default:
287 return VLC_EGENERIC;
291 static bool IsBmp(stream_t *s)
293 const uint8_t *header;
294 if (vlc_stream_Peek(s, &header, 18) < 18)
295 return false;
296 if (memcmp(header, "BM", 2) &&
297 memcmp(header, "BA", 2) &&
298 memcmp(header, "CI", 2) &&
299 memcmp(header, "CP", 2) &&
300 memcmp(header, "IC", 2) &&
301 memcmp(header, "PT", 2))
302 return false;
303 uint32_t file_size = GetDWLE(&header[2]);
304 uint32_t data_offset = GetDWLE(&header[10]);
305 uint32_t header_size = GetDWLE(&header[14]);
306 if (file_size != 14 && file_size != 14 + header_size &&
307 file_size <= data_offset)
308 return false;
309 if (data_offset < header_size + 14)
310 return false;
311 if (header_size != 12 && header_size < 40)
312 return false;
313 return true;
316 static bool IsPcx(stream_t *s)
318 const uint8_t *header;
319 if (vlc_stream_Peek(s, &header, 66) < 66)
320 return false;
321 if (header[0] != 0x0A || /* marker */
322 (header[1] != 0x00 && header[1] != 0x02 &&
323 header[1] != 0x03 && header[1] != 0x05) || /* version */
324 (header[2] != 0 && header[2] != 1) || /* encoding */
325 (header[3] != 1 && header[3] != 2 &&
326 header[3] != 4 && header[3] != 8) || /* bits per pixel per plane */
327 header[64] != 0 || /* reserved */
328 header[65] == 0 || header[65] > 4) /* plane count */
329 return false;
330 if (GetWLE(&header[4]) > GetWLE(&header[8]) || /* xmin vs xmax */
331 GetWLE(&header[6]) > GetWLE(&header[10])) /* ymin vs ymax */
332 return false;
333 return true;
336 static bool IsLbm(stream_t *s)
338 const uint8_t *header;
339 if (vlc_stream_Peek(s, &header, 12) < 12)
340 return false;
341 if (memcmp(&header[0], "FORM", 4) ||
342 GetDWBE(&header[4]) <= 4 ||
343 (memcmp(&header[8], "ILBM", 4) && memcmp(&header[8], "PBM ", 4)))
344 return false;
345 return true;
347 static bool IsPnmBlank(uint8_t v)
349 return v == ' ' || v == '\t' || v == '\r' || v == '\n';
351 static bool IsPnm(stream_t *s)
353 const uint8_t *header;
354 int size = vlc_stream_Peek(s, &header, 256);
355 if (size < 3)
356 return false;
357 if (header[0] != 'P' ||
358 header[1] < '1' || header[1] > '6' ||
359 !IsPnmBlank(header[2]))
360 return false;
362 int number_count = 0;
363 for (int i = 3, parsing_number = 0; i < size && number_count < 2; i++) {
364 if (IsPnmBlank(header[i])) {
365 if (parsing_number) {
366 parsing_number = 0;
367 number_count++;
369 } else {
370 if (header[i] < '0' || header[i] > '9')
371 break;
372 parsing_number = 1;
375 if (number_count < 2)
376 return false;
377 return true;
380 static uint8_t FindJpegMarker(int *position, const uint8_t *data, int size)
382 for (int i = *position; i + 1 < size; i++) {
383 if (data[i + 0] != 0xff || data[i + 1] == 0x00)
384 return 0xff;
385 if (data[i + 1] != 0xff) {
386 *position = i + 2;
387 return data[i + 1];
390 return 0xff;
392 static bool IsJfif(stream_t *s)
394 const uint8_t *header;
395 int size = vlc_stream_Peek(s, &header, 256);
396 int position = 0;
398 if (FindJpegMarker(&position, header, size) != 0xd8)
399 return false;
400 if (FindJpegMarker(&position, header, size) != 0xe0)
401 return false;
402 position += 2; /* Skip size */
403 if (position + 5 > size)
404 return false;
405 if (memcmp(&header[position], "JFIF\0", 5))
406 return false;
407 return true;
410 static bool IsSpiff(stream_t *s)
412 const uint8_t *header;
413 if (vlc_stream_Peek(s, &header, 36) < 36) /* SPIFF header size */
414 return false;
415 if (header[0] != 0xff || header[1] != 0xd8 ||
416 header[2] != 0xff || header[3] != 0xe8)
417 return false;
418 if (memcmp(&header[6], "SPIFF\0", 6))
419 return false;
420 return true;
423 static bool IsExif(stream_t *s)
425 const uint8_t *header;
426 ssize_t size = vlc_stream_Peek(s, &header, 256);
427 if (size == -1)
428 return false;
429 int position = 0;
431 if (FindJpegMarker(&position, header, size) != 0xd8)
432 return false;
433 if (FindJpegMarker(&position, header, size) != 0xe1)
434 return false;
435 position += 2; /* Skip size */
436 if (position + 5 > size)
437 return false;
438 if (memcmp(&header[position], "Exif\0", 5))
439 return false;
440 return true;
443 static bool FindSVGmarker(int *position, const uint8_t *data, const int size, const char *marker)
445 for( int i = *position; i < size; i++)
447 if (memcmp(&data[i], marker, strlen(marker)) == 0)
449 *position = i;
450 return true;
453 return false;
456 static bool IsSVG(stream_t *s)
458 if (s->psz_url == NULL)
459 return false;
461 char *ext = strstr(s->psz_url, ".svg");
462 if (!ext) return false;
464 const uint8_t *header;
465 ssize_t size = vlc_stream_Peek(s, &header, 4096);
466 if (size == -1)
467 return false;
468 int position = 0;
470 const char xml[] = "<?xml version=\"";
471 if (!FindSVGmarker(&position, header, size, xml))
472 return false;
473 if (position != 0)
474 return false;
476 const char endxml[] = ">\0";
477 if (!FindSVGmarker(&position, header, size, endxml))
478 return false;
479 if (position <= 15)
480 return false;
482 const char svg[] = "<svg";
483 if (!FindSVGmarker(&position, header, size, svg))
484 return false;
485 if (position < 19)
486 return false;
488 /* SVG Scalable Vector Graphics image */
490 /* NOTE: some SVG images have the mimetype set in a meta data section
491 * and some do not */
492 return true;
495 static bool IsTarga(stream_t *s)
497 /* The header is not enough to ensure proper detection, we need
498 * to have a look at the footer. But doing so can be slow. So
499 * try to avoid it when possible */
500 const uint8_t *header;
501 if (vlc_stream_Peek(s, &header, 18) < 18) /* Targa fixed header */
502 return false;
503 if (header[1] > 1) /* Color Map Type */
504 return false;
505 if ((header[1] != 0 || header[3 + 4] != 0) &&
506 header[3 + 4] != 8 &&
507 header[3 + 4] != 15 && header[3 + 4] != 16 &&
508 header[3 + 4] != 24 && header[3 + 4] != 32)
509 return false;
510 if ((header[2] > 3 && header[2] < 9) || header[2] > 11) /* Image Type */
511 return false;
512 if (GetWLE(&header[8 + 4]) <= 0 || /* Width */
513 GetWLE(&header[8 + 6]) <= 0) /* Height */
514 return false;
515 if (header[8 + 8] != 8 &&
516 header[8 + 8] != 15 && header[8 + 8] != 16 &&
517 header[8 + 8] != 24 && header[8 + 8] != 32)
518 return false;
519 if (header[8 + 9] & 0xc0) /* Reserved bits */
520 return false;
522 const int64_t size = stream_Size(s);
523 if (size <= 18 + 26)
524 return false;
525 bool can_seek;
526 if (vlc_stream_Control(s, STREAM_CAN_SEEK, &can_seek) || !can_seek)
527 return false;
529 const int64_t position = vlc_stream_Tell(s);
530 if (vlc_stream_Seek(s, size - 26))
531 return false;
533 const uint8_t *footer;
534 bool is_targa = vlc_stream_Peek(s, &footer, 26) >= 26 &&
535 !memcmp(&footer[8], "TRUEVISION-XFILE.\x00", 18);
536 vlc_stream_Seek(s, position);
537 return is_targa;
540 typedef struct {
541 vlc_fourcc_t codec;
542 size_t marker_size;
543 const uint8_t marker[14];
544 bool (*detect)(stream_t *s);
545 } image_format_t;
547 #define VLC_CODEC_XCF VLC_FOURCC('X', 'C', 'F', ' ')
548 #define VLC_CODEC_LBM VLC_FOURCC('L', 'B', 'M', ' ')
549 static const image_format_t formats[] = {
550 { .codec = VLC_CODEC_XCF,
551 .marker_size = 9 + 4 + 1,
552 .marker = { 'g', 'i', 'm', 'p', ' ', 'x', 'c', 'f', ' ',
553 'f', 'i', 'l', 'e', '\0' }
555 { .codec = VLC_CODEC_XCF,
556 .marker_size = 9 + 4 + 1,
557 .marker = { 'g', 'i', 'm', 'p', ' ', 'x', 'c', 'f', ' ',
558 'v', '0', '0', '1', '\0' }
560 { .codec = VLC_CODEC_XCF,
561 .marker_size = 9 + 4 + 1,
562 .marker = { 'g', 'i', 'm', 'p', ' ', 'x', 'c', 'f', ' ',
563 'v', '0', '0', '2', '\0' }
565 { .codec = VLC_CODEC_PNG,
566 .marker_size = 8,
567 .marker = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }
569 { .codec = VLC_CODEC_GIF,
570 .marker_size = 6,
571 .marker = { 'G', 'I', 'F', '8', '7', 'a' }
573 { .codec = VLC_CODEC_GIF,
574 .marker_size = 6,
575 .marker = { 'G', 'I', 'F', '8', '9', 'a' }
577 /* XXX TIFF detection may be a bit weak */
578 { .codec = VLC_CODEC_TIFF,
579 .marker_size = 4,
580 .marker = { 'I', 'I', 0x2a, 0x00 },
582 { .codec = VLC_CODEC_TIFF,
583 .marker_size = 4,
584 .marker = { 'M', 'M', 0x00, 0x2a },
586 { .codec = VLC_CODEC_BMP,
587 .detect = IsBmp,
589 { .codec = VLC_CODEC_PCX,
590 .detect = IsPcx,
592 { .codec = VLC_CODEC_LBM,
593 .detect = IsLbm,
595 { .codec = VLC_CODEC_PNM,
596 .detect = IsPnm,
598 { .codec = VLC_CODEC_MXPEG,
599 .detect = IsMxpeg,
601 { .codec = VLC_CODEC_JPEG,
602 .detect = IsJfif,
604 { .codec = VLC_CODEC_JPEG,
605 .detect = IsSpiff,
607 { .codec = VLC_CODEC_JPEG,
608 .detect = IsExif,
610 { .codec = VLC_CODEC_BPG,
611 .marker_size = 4,
612 .marker = { 'B', 'P', 'G', 0xFB },
614 { .codec = VLC_CODEC_SVG,
615 .detect = IsSVG,
617 { .codec = VLC_CODEC_TARGA,
618 .detect = IsTarga,
620 { .codec = 0 }
623 static int Open(vlc_object_t *object)
625 demux_t *demux = (demux_t*)object;
627 /* Detect the image type */
628 const image_format_t *img;
630 const uint8_t *peek;
631 ssize_t peek_size = 0;
632 for (int i = 0; ; i++) {
633 img = &formats[i];
634 if (!img->codec)
635 return VLC_EGENERIC;
637 if (img->detect) {
638 if (img->detect(demux->s))
639 break;
640 /* detect callbacks can invalidate the current peek buffer */
641 peek_size = 0;
642 } else {
643 if ((size_t) peek_size < img->marker_size)
645 peek_size = vlc_stream_Peek(demux->s, &peek, img->marker_size);
646 if (peek_size == -1)
647 return VLC_ENOMEM;
649 if ((size_t) peek_size >= img->marker_size &&
650 !memcmp(peek, img->marker, img->marker_size))
651 break;
654 msg_Dbg(demux, "Detected image: %s",
655 vlc_fourcc_GetDescription(VIDEO_ES, img->codec));
657 if( img->codec == VLC_CODEC_MXPEG )
659 return VLC_EGENERIC; //let avformat demux this file
662 /* Load and if selected decode */
663 es_format_t fmt;
664 es_format_Init(&fmt, VIDEO_ES, img->codec);
665 fmt.video.i_chroma = fmt.i_codec;
667 block_t *data = Load(demux);
668 if (data && var_InheritBool(demux, "image-decode")) {
669 char *string = var_InheritString(demux, "image-chroma");
670 vlc_fourcc_t chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, string);
671 free(string);
673 data = Decode(demux, &fmt.video, chroma, data);
674 fmt.i_codec = fmt.video.i_chroma;
676 fmt.i_id = var_InheritInteger(demux, "image-id");
677 fmt.i_group = var_InheritInteger(demux, "image-group");
678 if (var_InheritURational(demux,
679 &fmt.video.i_frame_rate,
680 &fmt.video.i_frame_rate_base,
681 "image-fps") ||
682 fmt.video.i_frame_rate <= 0 || fmt.video.i_frame_rate_base <= 0) {
683 msg_Err(demux, "Invalid frame rate, using 10/1 instead");
684 fmt.video.i_frame_rate = 10;
685 fmt.video.i_frame_rate_base = 1;
688 /* If loadind failed, we still continue to avoid mis-detection
689 * by other demuxers. */
690 if (!data)
691 msg_Err(demux, "Failed to load the image");
693 /* */
694 demux_sys_t *sys = malloc(sizeof(*sys));
695 if (!sys) {
696 if (data)
697 block_Release(data);
698 es_format_Clean(&fmt);
699 return VLC_ENOMEM;
702 sys->data = data;
703 sys->es = es_out_Add(demux->out, &fmt);
704 sys->duration = CLOCK_FREQ * var_InheritFloat(demux, "image-duration");
705 sys->is_realtime = var_InheritBool(demux, "image-realtime");
706 sys->pts_origin = sys->is_realtime ? mdate() : 0;
707 sys->pts_next = VLC_TS_INVALID;
708 date_Init(&sys->pts, fmt.video.i_frame_rate, fmt.video.i_frame_rate_base);
709 date_Set(&sys->pts, 0);
711 es_format_Clean(&fmt);
713 demux->pf_demux = Demux;
714 demux->pf_control = Control;
715 demux->p_sys = sys;
716 return VLC_SUCCESS;
719 static void Close(vlc_object_t *object)
721 demux_t *demux = (demux_t*)object;
722 demux_sys_t *sys = demux->p_sys;
724 if (sys->data)
725 block_Release(sys->data);
726 free(sys);