image.c 21.2 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3 4 5 6 7 8
/*****************************************************************************
 * image.c: Image demuxer
 *****************************************************************************
 * Copyright (C) 2010 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Laurent Aimar's avatar
Laurent Aimar committed
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Laurent Aimar's avatar
Laurent Aimar committed
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Laurent Aimar's avatar
Laurent Aimar committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_demux.h>
#include <vlc_image.h>
36
#include "mxpeg_helper.h"
Laurent Aimar's avatar
Laurent Aimar committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

#define ID_TEXT N_("ES ID")
#define ID_LONGTEXT N_( \
    "Set the ID of the elementary stream")

#define GROUP_TEXT N_("Group")
#define GROUP_LONGTEXT N_(\
    "Set the group of the elementary stream")

#define DECODE_TEXT N_("Decode")
#define DECODE_LONGTEXT N_( \
    "Decode at the demuxer stage")

#define CHROMA_TEXT N_("Forced chroma")
#define CHROMA_LONGTEXT N_( \
    "If non empty and image-decode is true, the image will be " \
    "converted to the specified chroma.")

61
#define DURATION_TEXT N_("Duration in seconds")
Laurent Aimar's avatar
Laurent Aimar committed
62
#define DURATION_LONGTEXT N_( \
63
    "Duration in seconds before simulating an end of file. " \
Laurent Aimar's avatar
Laurent Aimar committed
64 65 66 67 68 69 70 71 72 73 74 75 76
    "A negative value means an unlimited play time.")

#define FPS_TEXT N_("Frame rate")
#define FPS_LONGTEXT N_( \
    "Frame rate of the elementary stream produced.")

#define RT_TEXT N_("Real-time")
#define RT_LONGTEXT N_( \
    "Use real-time mode suitable for being used as a master input and " \
    "real-time input slaves.")

vlc_module_begin()
    set_description(N_("Image demuxer"))
77
    set_shortname(N_("Image"))
Laurent Aimar's avatar
Laurent Aimar committed
78 79
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_DEMUX)
80
    add_integer("image-id", -1, ID_TEXT, ID_LONGTEXT, true)
Laurent Aimar's avatar
Laurent Aimar committed
81
        change_safe()
82
    add_integer("image-group", 0, GROUP_TEXT, GROUP_LONGTEXT, true)
Laurent Aimar's avatar
Laurent Aimar committed
83
        change_safe()
84
    add_bool("image-decode", true, DECODE_TEXT, DECODE_LONGTEXT, true)
Laurent Aimar's avatar
Laurent Aimar committed
85
        change_safe()
86
    add_string("image-chroma", "", CHROMA_TEXT, CHROMA_LONGTEXT, true)
Laurent Aimar's avatar
Laurent Aimar committed
87
        change_safe()
88
    add_float("image-duration", 10, DURATION_TEXT, DURATION_LONGTEXT, false)
Laurent Aimar's avatar
Laurent Aimar committed
89
        change_safe()
90
    add_string("image-fps", "10/1", FPS_TEXT, FPS_LONGTEXT, true)
Laurent Aimar's avatar
Laurent Aimar committed
91
        change_safe()
92
    add_bool("image-realtime", false, RT_TEXT, RT_LONGTEXT, true)
Laurent Aimar's avatar
Laurent Aimar committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        change_safe()
    set_capability("demux", 10)
    set_callbacks(Open, Close)
vlc_module_end()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
struct demux_sys_t
{
    block_t     *data;
    es_out_id_t *es;
    mtime_t     duration;
    bool        is_realtime;
    mtime_t     pts_origin;
    mtime_t     pts_next;
109
    date_t        pts;
Laurent Aimar's avatar
Laurent Aimar committed
110 111 112 113
};

static block_t *Load(demux_t *demux)
{
114 115 116
    const unsigned max_size = 4096 * 4096 * 8;
    uint64_t size;

117
    if (vlc_stream_GetSize(demux->s, &size) == VLC_SUCCESS) {
118 119 120 121 122 123 124 125 126 127 128 129
        if (size > max_size) {
            msg_Err(demux, "image too large (%"PRIu64" > %u), rejected",
                    size, max_size);
            return NULL;
        }
    } else
        size = max_size;

    block_t *block = block_Alloc(size);
    if (block == NULL)
        return NULL;

130
    ssize_t val = vlc_stream_Read(demux->s, block->p_buffer, size);
131 132
    if (val < 0) {
        block_Release(block);
Laurent Aimar's avatar
Laurent Aimar committed
133 134 135
        return NULL;
    }

136 137
    block->i_buffer = val;
    return block;
Laurent Aimar's avatar
Laurent Aimar committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
}

static block_t *Decode(demux_t *demux,
                       video_format_t *fmt, vlc_fourcc_t chroma, block_t *data)
{
    image_handler_t *handler = image_HandlerCreate(demux);
    if (!handler) {
        block_Release(data);
        return NULL;
    }

    video_format_t decoded;
    video_format_Init(&decoded, chroma);

    picture_t *image = image_Read(handler, data, fmt, &decoded);
    image_HandlerDelete(handler);

    if (!image)
        return NULL;

    video_format_Clean(fmt);
    *fmt = decoded;

    size_t size = 0;
    for (int i = 0; i < image->i_planes; i++)
163
        size += image->p[i].i_pitch * image->p[i].i_lines;
Laurent Aimar's avatar
Laurent Aimar committed
164

165
    data = block_Alloc(size);
Laurent Aimar's avatar
Laurent Aimar committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    if (!data) {
        picture_Release(image);
        return NULL;
    }

    size_t offset = 0;
    for (int i = 0; i < image->i_planes; i++) {
        const plane_t *src = &image->p[i];
        for (int y = 0; y < src->i_visible_lines; y++) {
            memcpy(&data->p_buffer[offset],
                   &src->p_pixels[y * src->i_pitch],
                   src->i_visible_pitch);
            offset += src->i_visible_pitch;
        }
    }

    picture_Release(image);
    return data;
}

static int Demux(demux_t *demux)
{
    demux_sys_t *sys = demux->p_sys;

    if (!sys->data)
        return 0;

    mtime_t deadline;
    const mtime_t pts_first = sys->pts_origin + date_Get(&sys->pts);
    if (sys->pts_next > VLC_TS_INVALID) {
        deadline = sys->pts_next;
    } else if (sys->is_realtime) {
        deadline = mdate();
        const mtime_t max_wait = CLOCK_FREQ / 50;
        if (deadline + max_wait < pts_first) {
201
            es_out_SetPCR(demux->out, deadline);
Laurent Aimar's avatar
Laurent Aimar committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
            /* That's ugly, but not yet easily fixable */
            mwait(deadline + max_wait);
            return 1;
        }
    } else {
        deadline = 1 + pts_first;
    }

    for (;;) {
        const mtime_t pts = sys->pts_origin + date_Get(&sys->pts);
        if (sys->duration >= 0 && pts >= sys->pts_origin + sys->duration)
            return 0;

        if (pts >= deadline)
            return 1;

        block_t *data = block_Duplicate(sys->data);
        if (!data)
            return -1;

        data->i_dts =
        data->i_pts = VLC_TS_0 + pts;
224
        es_out_SetPCR(demux->out, data->i_pts);
Laurent Aimar's avatar
Laurent Aimar committed
225 226 227 228 229 230 231 232 233 234
        es_out_Send(demux->out, sys->es, data);

        date_Increment(&sys->pts, 1);
    }
}

static int Control(demux_t *demux, int query, va_list args)
{
    demux_sys_t *sys = demux->p_sys;

235
    switch (query) {
236 237 238
    case DEMUX_CAN_SEEK:
        *va_arg(args, bool *) = sys->duration >= 0 && !sys->is_realtime;
        return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    case DEMUX_GET_POSITION: {
        double *position = va_arg(args, double *);
        if (sys->duration > 0)
            *position = date_Get(&sys->pts) / (double)sys->duration;
        else
            *position = 0;
        return VLC_SUCCESS;
    }
    case DEMUX_SET_POSITION: {
        if (sys->duration < 0 || sys->is_realtime)
            return VLC_EGENERIC;
        double position = va_arg(args, double);
        date_Set(&sys->pts, position * sys->duration);
        return VLC_SUCCESS;
    }
    case DEMUX_GET_TIME: {
        int64_t *time = va_arg(args, int64_t *);
        *time = sys->pts_origin + date_Get(&sys->pts);
        return VLC_SUCCESS;
    }
    case DEMUX_SET_TIME: {
        if (sys->duration < 0 || sys->is_realtime)
            return VLC_EGENERIC;
        int64_t time = va_arg(args, int64_t);
263
        date_Set(&sys->pts, VLC_CLIP(time - sys->pts_origin, 0, sys->duration));
Laurent Aimar's avatar
Laurent Aimar committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        return VLC_SUCCESS;
    }
    case DEMUX_SET_NEXT_DEMUX_TIME: {
        int64_t pts_next = VLC_TS_0 + va_arg(args, int64_t);
        if (sys->pts_next <= VLC_TS_INVALID)
            sys->pts_origin = pts_next;
        sys->pts_next = pts_next;
        return VLC_SUCCESS;
    }
    case DEMUX_GET_LENGTH: {
        int64_t *length = va_arg(args, int64_t *);
        *length = __MAX(sys->duration, 0);
        return VLC_SUCCESS;
    }
    case DEMUX_GET_FPS: {
        double *fps = va_arg(args, double *);
        *fps = (double)sys->pts.i_divider_num / sys->pts.i_divider_den;
        return VLC_SUCCESS;
    }
    case DEMUX_GET_META:
    case DEMUX_HAS_UNSUPPORTED_META:
    case DEMUX_GET_ATTACHMENTS:
286 287 288
    default:
        return VLC_EGENERIC;
    }
Laurent Aimar's avatar
Laurent Aimar committed
289 290 291 292 293
}

static bool IsBmp(stream_t *s)
{
    const uint8_t *header;
294
    if (vlc_stream_Peek(s, &header, 18) < 18)
Laurent Aimar's avatar
Laurent Aimar committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        return false;
    if (memcmp(header, "BM", 2) &&
        memcmp(header, "BA", 2) &&
        memcmp(header, "CI", 2) &&
        memcmp(header, "CP", 2) &&
        memcmp(header, "IC", 2) &&
        memcmp(header, "PT", 2))
        return false;
    uint32_t file_size   = GetDWLE(&header[2]);
    uint32_t data_offset = GetDWLE(&header[10]);
    uint32_t header_size = GetDWLE(&header[14]);
    if (file_size != 14 && file_size != 14 + header_size &&
        file_size <= data_offset)
        return false;
    if (data_offset < header_size + 14)
        return false;
    if (header_size != 12 && header_size < 40)
        return false;
    return true;
}

static bool IsPcx(stream_t *s)
{
    const uint8_t *header;
319
    if (vlc_stream_Peek(s, &header, 66) < 66)
Laurent Aimar's avatar
Laurent Aimar committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        return false;
    if (header[0] != 0x0A ||                        /* marker */
        (header[1] != 0x00 && header[1] != 0x02 &&
         header[1] != 0x03 && header[1] != 0x05) || /* version */
        (header[2] != 0 && header[2] != 1) ||       /* encoding */
        (header[3] != 1 && header[3] != 2 &&
         header[3] != 4 && header[3] != 8) ||       /* bits per pixel per plane */
        header[64] != 0 ||                          /* reserved */
        header[65] == 0 || header[65] > 4)          /* plane count */
        return false;
    if (GetWLE(&header[4]) > GetWLE(&header[8]) ||  /* xmin vs xmax */
        GetWLE(&header[6]) > GetWLE(&header[10]))   /* ymin vs ymax */
        return false;
    return true;
}

static bool IsLbm(stream_t *s)
{
    const uint8_t *header;
339
    if (vlc_stream_Peek(s, &header, 12) < 12)
Laurent Aimar's avatar
Laurent Aimar committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353
        return false;
    if (memcmp(&header[0], "FORM", 4) ||
        GetDWBE(&header[4]) <= 4 ||
        (memcmp(&header[8], "ILBM", 4) && memcmp(&header[8], "PBM ", 4)))
        return false;
    return true;
}
static bool IsPnmBlank(uint8_t v)
{
    return v == ' ' || v == '\t' || v == '\r' || v == '\n';
}
static bool IsPnm(stream_t *s)
{
    const uint8_t *header;
354
    int size = vlc_stream_Peek(s, &header, 256);
Laurent Aimar's avatar
Laurent Aimar committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    if (size < 3)
        return false;
    if (header[0] != 'P' ||
        header[1] < '1' || header[1] > '6' ||
        !IsPnmBlank(header[2]))
        return false;

    int number_count = 0;
    for (int i = 3, parsing_number = 0; i < size && number_count < 2; i++) {
        if (IsPnmBlank(header[i])) {
            if (parsing_number) {
                parsing_number = 0;
                number_count++;
            }
        } else {
            if (header[i] < '0' || header[i] > '9')
                break;
            parsing_number = 1;
        }
    }
    if (number_count < 2)
        return false;
    return true;
}

static uint8_t FindJpegMarker(int *position, const uint8_t *data, int size)
{
    for (int i = *position; i + 1 < size; i++) {
        if (data[i + 0] != 0xff || data[i + 1] == 0x00)
            return 0xff;
        if (data[i + 1] != 0xff) {
            *position = i + 2;
            return data[i + 1];
        }
    }
    return 0xff;
}
static bool IsJfif(stream_t *s)
{
    const uint8_t *header;
395
    int size = vlc_stream_Peek(s, &header, 256);
Laurent Aimar's avatar
Laurent Aimar committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    int position = 0;

    if (FindJpegMarker(&position, header, size) != 0xd8)
        return false;
    if (FindJpegMarker(&position, header, size) != 0xe0)
        return false;
    position += 2;  /* Skip size */
    if (position + 5 > size)
        return false;
    if (memcmp(&header[position], "JFIF\0", 5))
        return false;
    return true;
}

static bool IsSpiff(stream_t *s)
{
    const uint8_t *header;
413
    if (vlc_stream_Peek(s, &header, 36) < 36) /* SPIFF header size */
Laurent Aimar's avatar
Laurent Aimar committed
414 415 416 417 418 419 420 421 422
        return false;
    if (header[0] != 0xff || header[1] != 0xd8 ||
        header[2] != 0xff || header[3] != 0xe8)
        return false;
    if (memcmp(&header[6], "SPIFF\0", 6))
        return false;
    return true;
}

423 424 425
static bool IsExif(stream_t *s)
{
    const uint8_t *header;
426 427 428
    ssize_t size = vlc_stream_Peek(s, &header, 256);
    if (size == -1)
        return false;
429 430 431 432 433 434 435 436 437 438 439 440 441 442
    int position = 0;

    if (FindJpegMarker(&position, header, size) != 0xd8)
        return false;
    if (FindJpegMarker(&position, header, size) != 0xe1)
        return false;
    position += 2;  /* Skip size */
    if (position + 5 > size)
        return false;
    if (memcmp(&header[position], "Exif\0", 5))
        return false;
    return true;
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
static bool FindSVGmarker(int *position, const uint8_t *data, const int size, const char *marker)
{
    for( int i = *position; i < size; i++)
    {
        if (memcmp(&data[i], marker, strlen(marker)) == 0)
        {
            *position = i;
            return true;
        }
    }
    return false;
}

static bool IsSVG(stream_t *s)
{
458 459 460
    if (s->psz_url == NULL)
        return false;

461
    char *ext = strstr(s->psz_url, ".svg");
462 463 464
    if (!ext) return false;

    const uint8_t *header;
465 466 467
    ssize_t size = vlc_stream_Peek(s, &header, 4096);
    if (size == -1)
        return false;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    int position = 0;

    const char xml[] = "<?xml version=\"";
    if (!FindSVGmarker(&position, header, size, xml))
        return false;
    if (position != 0)
        return false;

    const char endxml[] = ">\0";
    if (!FindSVGmarker(&position, header, size, endxml))
        return false;
    if (position <= 15)
        return false;

    const char svg[] = "<svg";
    if (!FindSVGmarker(&position, header, size, svg))
        return false;
    if (position < 19)
        return false;

    /* SVG Scalable Vector Graphics image */

    /* NOTE: some SVG images have the mimetype set in a meta data section
     * and some do not */
    return true;
}

495 496 497 498 499 500
static bool IsTarga(stream_t *s)
{
    /* The header is not enough to ensure proper detection, we need
     * to have a look at the footer. But doing so can be slow. So
     * try to avoid it when possible */
    const uint8_t *header;
501
    if (vlc_stream_Peek(s, &header, 18) < 18)   /* Targa fixed header */
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
        return false;
    if (header[1] > 1)                      /* Color Map Type */
        return false;
    if ((header[1] != 0 || header[3 + 4] != 0) &&
        header[3 + 4] != 8  &&
        header[3 + 4] != 15 && header[3 + 4] != 16 &&
        header[3 + 4] != 24 && header[3 + 4] != 32)
        return false;
    if ((header[2] > 3 && header[2] < 9) || header[2] > 11) /* Image Type */
        return false;
    if (GetWLE(&header[8 + 4]) <= 0 ||      /* Width */
        GetWLE(&header[8 + 6]) <= 0)        /* Height */
        return false;
    if (header[8 + 8] != 8  &&
        header[8 + 8] != 15 && header[8 + 8] != 16 &&
        header[8 + 8] != 24 && header[8 + 8] != 32)
        return false;
    if (header[8 + 9] & 0xc0)               /* Reserved bits */
        return false;

    const int64_t size = stream_Size(s);
    if (size <= 18 + 26)
        return false;
    bool can_seek;
526
    if (vlc_stream_Control(s, STREAM_CAN_SEEK, &can_seek) || !can_seek)
527 528
        return false;

529 530
    const int64_t position = vlc_stream_Tell(s);
    if (vlc_stream_Seek(s, size - 26))
531 532 533
        return false;

    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 538 539
    return is_targa;
}

Laurent Aimar's avatar
Laurent Aimar committed
540 541
typedef struct {
    vlc_fourcc_t  codec;
542
    size_t        marker_size;
Laurent Aimar's avatar
Laurent Aimar committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    const uint8_t marker[14];
    bool          (*detect)(stream_t *s);
} image_format_t;

#define VLC_CODEC_XCF VLC_FOURCC('X', 'C', 'F', ' ')
#define VLC_CODEC_LBM VLC_FOURCC('L', 'B', 'M', ' ')
static const image_format_t formats[] = {
    { .codec = VLC_CODEC_XCF,
      .marker_size = 9 + 4 + 1,
      .marker = { 'g', 'i', 'm', 'p', ' ', 'x', 'c', 'f', ' ',
                  'f', 'i', 'l', 'e', '\0' }
    },
    { .codec = VLC_CODEC_XCF,
      .marker_size = 9 + 4 + 1,
      .marker = { 'g', 'i', 'm', 'p', ' ', 'x', 'c', 'f', ' ',
                  'v', '0', '0', '1', '\0' }
    },
    { .codec = VLC_CODEC_XCF,
      .marker_size = 9 + 4 + 1,
      .marker = { 'g', 'i', 'm', 'p', ' ', 'x', 'c', 'f', ' ',
                  'v', '0', '0', '2', '\0' }
    },
    { .codec = VLC_CODEC_PNG,
      .marker_size = 8,
      .marker = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }
    },
    { .codec = VLC_CODEC_GIF,
      .marker_size = 6,
      .marker = { 'G', 'I', 'F', '8', '7', 'a' }
    },
    { .codec = VLC_CODEC_GIF,
      .marker_size = 6,
      .marker = { 'G', 'I', 'F', '8', '9', 'a' }
    },
    /* XXX TIFF detection may be a bit weak */
    { .codec = VLC_CODEC_TIFF,
      .marker_size = 4,
      .marker = { 'I', 'I', 0x2a, 0x00 },
    },
    { .codec = VLC_CODEC_TIFF,
      .marker_size = 4,
      .marker = { 'M', 'M', 0x00, 0x2a },
    },
    { .codec = VLC_CODEC_BMP,
      .detect = IsBmp,
    },
    { .codec = VLC_CODEC_PCX,
      .detect = IsPcx,
    },
    { .codec = VLC_CODEC_LBM,
      .detect = IsLbm,
    },
    { .codec = VLC_CODEC_PNM,
      .detect = IsPnm,
    },
598 599 600
    { .codec = VLC_CODEC_MXPEG,
      .detect = IsMxpeg,
    },
Laurent Aimar's avatar
Laurent Aimar committed
601 602 603 604 605 606
    { .codec = VLC_CODEC_JPEG,
      .detect = IsJfif,
    },
    { .codec = VLC_CODEC_JPEG,
      .detect = IsSpiff,
    },
607 608 609
    { .codec = VLC_CODEC_JPEG,
      .detect = IsExif,
    },
610 611 612 613
    { .codec = VLC_CODEC_BPG,
      .marker_size = 4,
      .marker = { 'B', 'P', 'G', 0xFB },
    },
614 615 616
    { .codec = VLC_CODEC_SVG,
      .detect = IsSVG,
    },
617 618 619
    { .codec = VLC_CODEC_TARGA,
      .detect = IsTarga,
    },
Laurent Aimar's avatar
Laurent Aimar committed
620 621 622 623 624 625 626 627 628 629 630
    { .codec = 0 }
};

static int Open(vlc_object_t *object)
{
    demux_t *demux = (demux_t*)object;

    /* Detect the image type */
    const image_format_t *img;

    const uint8_t *peek;
631
    ssize_t peek_size = 0;
Laurent Aimar's avatar
Laurent Aimar committed
632 633 634 635 636 637 638 639
    for (int i = 0; ; i++) {
        img = &formats[i];
        if (!img->codec)
            return VLC_EGENERIC;

        if (img->detect) {
            if (img->detect(demux->s))
                break;
640 641
            /* detect callbacks can invalidate the current peek buffer */
            peek_size = 0;
Laurent Aimar's avatar
Laurent Aimar committed
642
        } else {
643 644
            if ((size_t) peek_size < img->marker_size)
            {
645
                peek_size = vlc_stream_Peek(demux->s, &peek, img->marker_size);
646 647 648 649
                if (peek_size == -1)
                    return VLC_ENOMEM;
            }
            if ((size_t) peek_size >= img->marker_size &&
Laurent Aimar's avatar
Laurent Aimar committed
650 651 652 653 654 655 656
                !memcmp(peek, img->marker, img->marker_size))
                break;
        }
    }
    msg_Dbg(demux, "Detected image: %s",
            vlc_fourcc_GetDescription(VIDEO_ES, img->codec));

657 658 659 660 661
    if( img->codec == VLC_CODEC_MXPEG )
    {
        return VLC_EGENERIC; //let avformat demux this file
    }

Laurent Aimar's avatar
Laurent Aimar committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    /* Load and if selected decode */
    es_format_t fmt;
    es_format_Init(&fmt, VIDEO_ES, img->codec);
    fmt.video.i_chroma = fmt.i_codec;

    block_t *data = Load(demux);
    if (data && var_InheritBool(demux, "image-decode")) {
        char *string = var_InheritString(demux, "image-chroma");
        vlc_fourcc_t chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, string);
        free(string);

        data = Decode(demux, &fmt.video, chroma, data);
        fmt.i_codec = fmt.video.i_chroma;
    }
    fmt.i_id    = var_InheritInteger(demux, "image-id");
    fmt.i_group = var_InheritInteger(demux, "image-group");
    if (var_InheritURational(demux,
                             &fmt.video.i_frame_rate,
                             &fmt.video.i_frame_rate_base,
                             "image-fps") ||
        fmt.video.i_frame_rate <= 0 || fmt.video.i_frame_rate_base <= 0) {
        msg_Err(demux, "Invalid frame rate, using 10/1 instead");
        fmt.video.i_frame_rate      = 10;
        fmt.video.i_frame_rate_base = 1;
    }

    /* If loadind failed, we still continue to avoid mis-detection
     * by other demuxers. */
    if (!data)
        msg_Err(demux, "Failed to load the image");

    /* */
    demux_sys_t *sys = malloc(sizeof(*sys));
    if (!sys) {
        if (data)
            block_Release(data);
        es_format_Clean(&fmt);
        return VLC_ENOMEM;
    }

    sys->data        = data;
    sys->es          = es_out_Add(demux->out, &fmt);
    sys->duration    = CLOCK_FREQ * var_InheritFloat(demux, "image-duration");
    sys->is_realtime = var_InheritBool(demux, "image-realtime");
    sys->pts_origin  = sys->is_realtime ? mdate() : 0;
    sys->pts_next    = VLC_TS_INVALID;
    date_Init(&sys->pts, fmt.video.i_frame_rate, fmt.video.i_frame_rate_base);
    date_Set(&sys->pts, 0);

    es_format_Clean(&fmt);

    demux->pf_demux   = Demux;
    demux->pf_control = Control;
    demux->p_sys      = sys;
    return VLC_SUCCESS;
}

static void Close(vlc_object_t *object)
{
    demux_t     *demux = (demux_t*)object;
    demux_sys_t *sys   = demux->p_sys;

    if (sys->data)
        block_Release(sys->data);
    free(sys);
}