demux.c 27.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * demux.c : V4L2 raw video demux module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2002-2011 VLC authors and VideoLAN
5 6 7 8 9 10
 *
 * Authors: Benjamin Pracht <bigben at videolan dot org>
 *          Richard Hosking <richard at hovis dot net>
 *          Antoine Cellerier <dionoea at videolan d.t org>
 *          Dennis Lou <dlou99 at yahoo dot com>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12
 * 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
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
13
 * the Free Software Foundation; either version 2.1 of the License, or
14 15 16 17
 * (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
LGPL  
Jean-Baptiste Kempf committed
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
19
 * GNU Lesser General Public License for more details.
20
 *
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
21 22 23
 * 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.
24 25 26 27 28 29
 *****************************************************************************/

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

30
#include <math.h>
31
#include <errno.h>
32
#include <assert.h>
33
#include <sys/ioctl.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
34
#include <sys/mman.h>
Rafaël Carré's avatar
Rafaël Carré committed
35 36 37
#ifndef MAP_ANONYMOUS
# define MAP_ANONYMOUS MAP_ANON
#endif
38 39
#include <poll.h>

40
#include <vlc_common.h>
41 42
#include <vlc_demux.h>

43 44
#include "v4l2.h"

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
struct demux_sys_t
{
    int fd;
    vlc_thread_t thread;

    struct buffer_t *bufv;
    union
    {
        uint32_t bufc;
        uint32_t blocksize;
    };
    uint32_t block_flags;

    es_out_id_t *es;
    vlc_v4l2_ctrl_t *controls;
60
    mtime_t start;
61 62

#ifdef ZVBI_COMPILED
63
    vlc_v4l2_vbi_t *vbi;
64
#endif
65 66
};

67 68
static void *UserPtrThread (void *);
static void *MmapThread (void *);
69
static void *ReadThread (void *);
70
static int DemuxControl( demux_t *, int, va_list );
71
static int InitVideo (demux_t *, int fd, uint32_t caps);
72 73 74 75

int DemuxOpen( vlc_object_t *obj )
{
    demux_t *demux = (demux_t *)obj;
76

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
77 78
    demux_sys_t *sys = malloc (sizeof (*sys));
    if (unlikely(sys == NULL))
79 80
        return VLC_ENOMEM;
    demux->p_sys = sys;
81
#ifdef ZVBI_COMPILED
82
    sys->vbi = NULL;
83
#endif
84 85

    ParseMRL( obj, demux->psz_location );
86 87 88 89 90

    char *path = var_InheritString (obj, CFG_PREFIX"dev");
    if (unlikely(path == NULL))
        goto error; /* probably OOM */

91 92
    uint32_t caps;
    int fd = OpenDevice (obj, path, &caps);
93 94
    free (path);
    if (fd == -1)
95
        goto error;
96
    sys->fd = fd;
97

98
    if (InitVideo (demux, fd, caps))
99
    {
100 101
        v4l2_close (fd);
        goto error;
102 103
    }

104
    sys->controls = ControlsInit (VLC_OBJECT(demux), fd);
105
    sys->start = mdate ();
106
    demux->pf_demux = NULL;
107 108 109 110 111
    demux->pf_control = DemuxControl;
    demux->info.i_update = 0;
    demux->info.i_title = 0;
    demux->info.i_seekpoint = 0;
    return VLC_SUCCESS;
112 113 114
error:
    free (sys);
    return VLC_EGENERIC;
115 116
}

117
typedef struct
118
{
119 120
    uint32_t v4l2;
    vlc_fourcc_t vlc;
121
    uint8_t bpp; /**< Bytes per pixel (largest plane) */
122 123 124 125 126
    uint32_t red;
    uint32_t green;
    uint32_t blue;
} vlc_v4l2_fmt_t;

127 128
/* NOTE: Currently vlc_v4l2_fmt_rank() assumes format are sorted in order of
 * decreasing preference. */
129
static const vlc_v4l2_fmt_t v4l2_fmts[] =
130
{
131
    /* Planar YUV 4:2:0 */
132 133 134
    { V4L2_PIX_FMT_YUV420,  VLC_CODEC_I420, 1, 0, 0, 0 },
    { V4L2_PIX_FMT_YVU420,  VLC_CODEC_YV12, 1, 0, 0, 0 },
    { V4L2_PIX_FMT_YUV422P, VLC_CODEC_I422, 1, 0, 0, 0 },
135
    /* Packed YUV 4:2:2 */
136 137 138 139
    { V4L2_PIX_FMT_YUYV,    VLC_CODEC_YUYV, 2, 0, 0, 0 },
    { V4L2_PIX_FMT_UYVY,    VLC_CODEC_UYVY, 2, 0, 0, 0 },
    { V4L2_PIX_FMT_YVYU,    VLC_CODEC_YVYU, 2, 0, 0, 0 },
    { V4L2_PIX_FMT_VYUY,    VLC_CODEC_VYUY, 2, 0, 0, 0 },
140

141
    { V4L2_PIX_FMT_YUV411P, VLC_CODEC_I411, 1, 0, 0, 0 },
142

143
    { V4L2_PIX_FMT_YUV410,  VLC_CODEC_I410, 1, 0, 0, 0 },
144 145 146 147
//  { V4L2_PIX_FMT_YVU410     },

//  { V4L2_PIX_FMT_NV24,      },
//  { V4L2_PIX_FMT_NV42,      },
148 149 150 151
//  { V4L2_PIX_FMT_NV16,    VLC_CODEC_NV16, 1, 0, 0, 0 },
//  { V4L2_PIX_FMT_NV61,    VLC_CODEC_NV61, 1, 0, 0, 0 },
    { V4L2_PIX_FMT_NV12,    VLC_CODEC_NV12, 1, 0, 0, 0 },
    { V4L2_PIX_FMT_NV21,    VLC_CODEC_NV21, 1, 0, 0, 0 },
152 153 154 155 156 157 158 159

    /* V4L2-documented but VLC-unsupported misc. YUV formats */
//  { V4L2_PIX_FMT_Y41P       },
//  { V4L2_PIX_FMT_NV12MT,    },
//  { V4L2_PIX_FMT_M420,      },

    /* Packed RGB */
#ifdef WORDS_BIGENDIAN
160 161 162 163
    { V4L2_PIX_FMT_RGB32,   VLC_CODEC_RGB32, 4, 0xFF00, 0xFF0000, 0xFF000000 },
    { V4L2_PIX_FMT_BGR32,   VLC_CODEC_RGB32, 4, 0xFF000000, 0xFF0000, 0xFF00 },
    { V4L2_PIX_FMT_RGB24,   VLC_CODEC_RGB24, 3, 0xFF0000, 0x00FF00, 0x0000FF },
    { V4L2_PIX_FMT_BGR24,   VLC_CODEC_RGB24, 3, 0x0000FF, 0x00FF00, 0xFF0000 },
164 165
//  { V4L2_PIX_FMT_BGR666,    },
//  { V4L2_PIX_FMT_RGB565,    },
166
    { V4L2_PIX_FMT_RGB565X, VLC_CODEC_RGB16, 2,  0x001F,   0x07E0,   0xF800 },
167
//  { V4L2_PIX_FMT_RGB555,    },
168 169
    { V4L2_PIX_FMT_RGB555X, VLC_CODEC_RGB15, 2,  0x001F,   0x03E0,   0x7C00 },
//  { V4L2_PIX_FMT_RGB444,  VLC_CODEC_RGB12, 2,  0x000F,   0xF000,   0x0F00 },
170
#else
171 172 173 174
    { V4L2_PIX_FMT_RGB32,   VLC_CODEC_RGB32, 4, 0x0000FF, 0x00FF00, 0xFF0000 },
    { V4L2_PIX_FMT_BGR32,   VLC_CODEC_RGB32, 4, 0xFF0000, 0x00FF00, 0x0000FF },
    { V4L2_PIX_FMT_RGB24,   VLC_CODEC_RGB24, 3, 0x0000FF, 0x00FF00, 0xFF0000 },
    { V4L2_PIX_FMT_BGR24,   VLC_CODEC_RGB24, 3, 0xFF0000, 0x00FF00, 0x0000FF },
175
//  { V4L2_PIX_FMT_BGR666,    },
176
    { V4L2_PIX_FMT_RGB565,  VLC_CODEC_RGB16, 2,   0x001F,   0x07E0,   0xF800 },
177
//  { V4L2_PIX_FMT_RGB565X,   },
178
    { V4L2_PIX_FMT_RGB555,  VLC_CODEC_RGB15, 2,   0x001F,   0x03E0,   0x7C00 },
179
//  { V4L2_PIX_FMT_RGB555X,   },
180
//  { V4L2_PIX_FMT_RGB444,  VLC_CODEC_RGB12, 2,   0x0F00,   0x00F0,   0x000F },
181
#endif
182
//  { V4L2_PIX_FMT_RGB332,  VLC_CODEC_RGB8,  1,      0xC0,     0x38,     0x07 },
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    /* Bayer (sub-sampled RGB). Not supported. */
//  { V4L2_PIX_FMT_SBGGR16,  }
//  { V4L2_PIX_FMT_SRGGB12,  }
//  { V4L2_PIX_FMT_SGRBG12,  }
//  { V4L2_PIX_FMT_SGBRG12,  }
//  { V4L2_PIX_FMT_SBGGR12,  }
//  { V4L2_PIX_FMT_SRGGB10,  }
//  { V4L2_PIX_FMT_SGRBG10,  }
//  { V4L2_PIX_FMT_SGBRG10,  }
//  { V4L2_PIX_FMT_SBGGR10,  }
//  { V4L2_PIX_FMT_SBGGR8,   }
//  { V4L2_PIX_FMT_SGBRG8,   }
//  { V4L2_PIX_FMT_SGRBG8,   }
//  { V4L2_PIX_FMT_SRGGB8,   }
198 199

    /* Compressed data types */
200 201
    { V4L2_PIX_FMT_JPEG,    VLC_CODEC_MJPG, 0, 0, 0, 0 },
    { V4L2_PIX_FMT_H264,    VLC_CODEC_H264, 0, 0, 0, 0 },
202
    /* FIXME: fill p_extra for avc1... */
203 204 205 206 207 208 209 210
//  { V4L2_PIX_FMT_H264_NO_SC, VLC_FOURCC('a','v','c','1'), 0, 0, 0, 0 }
    { V4L2_PIX_FMT_MPEG4,   VLC_CODEC_MP4V, 0, 0, 0, 0 },
    { V4L2_PIX_FMT_XVID,    VLC_CODEC_MP4V, 0, 0, 0, 0 },
    { V4L2_PIX_FMT_H263,    VLC_CODEC_H263, 0, 0, 0, 0 },
    { V4L2_PIX_FMT_MPEG2,   VLC_CODEC_MPGV, 0, 0, 0, 0 },
    { V4L2_PIX_FMT_MPEG1,   VLC_CODEC_MPGV, 0, 0, 0, 0 },
    { V4L2_PIX_FMT_VC1_ANNEX_G, VLC_CODEC_VC1, 0, 0, 0, 0 },
    { V4L2_PIX_FMT_VC1_ANNEX_L, VLC_CODEC_VC1, 0, 0, 0, 0 },
211 212 213
    //V4L2_PIX_FMT_MPEG -> use access

    /* Reserved formats */
214
    { V4L2_PIX_FMT_MJPEG,   VLC_CODEC_MJPG, 0, 0, 0, 0 },
215 216 217 218 219 220 221
    //V4L2_PIX_FMT_DV -> use access

    /* Grey scale */
//  { V4L2_PIX_FMT_Y16,       },
//  { V4L2_PIX_FMT_Y12,       },
//  { V4L2_PIX_FMT_Y10,       },
//  { V4L2_PIX_FMT_Y10BPACK,  },
222
    { V4L2_PIX_FMT_GREY,    VLC_CODEC_GREY, 1, 0, 0, 0 },
223 224
};

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static const vlc_v4l2_fmt_t *vlc_from_v4l2_fourcc (uint32_t fourcc)
{
     for (size_t i = 0; i < sizeof (v4l2_fmts) / sizeof (v4l2_fmts[0]); i++)
         if (v4l2_fmts[i].v4l2 == fourcc)
             return v4l2_fmts + i;
     return NULL;
}

static size_t vlc_v4l2_fmt_rank (const vlc_v4l2_fmt_t *fmt)
{
    if (fmt == NULL)
        return SIZE_MAX;

    ptrdiff_t d = fmt - v4l2_fmts;
    assert (d >= 0);
    assert (d < (ptrdiff_t)(sizeof (v4l2_fmts) / sizeof (v4l2_fmts[0])));
    return d;
}

static vlc_fourcc_t var_InheritFourCC (vlc_object_t *obj, const char *varname)
{
    char *str = var_InheritString (obj, varname);
    if (str == NULL)
        return 0;

    vlc_fourcc_t fourcc = vlc_fourcc_GetCodecFromString (VIDEO_ES, str);
    if (fourcc == 0)
        msg_Err (obj, "invalid codec %s", str);
    free (str);
    return fourcc;
}
#define var_InheritFourCC(o, v) var_InheritFourCC(VLC_OBJECT(o), v)

258 259 260 261 262 263 264 265 266 267 268 269 270 271
static void GetAR (int fd, unsigned *restrict num, unsigned *restrict den)
{
    struct v4l2_cropcap cropcap = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };

    /* TODO: get CROPCAP only once (see ResetCrop()). */
    if (v4l2_ioctl (fd, VIDIOC_CROPCAP, &cropcap) < 0)
    {
        *num = *den = 1;
        return;
    }
    *num = cropcap.pixelaspect.numerator;
    *den = cropcap.pixelaspect.denominator;
}

272
static int InitVideo (demux_t *demux, int fd, uint32_t caps)
273 274
{
    demux_sys_t *sys = demux->p_sys;
275
    v4l2_std_id std;
276

277
    if (!(caps & V4L2_CAP_VIDEO_CAPTURE))
278 279 280 281 282
    {
        msg_Err (demux, "not a video capture device");
        return -1;
    }

283
    if (SetupInput (VLC_OBJECT(demux), fd, &std))
284 285
        return -1;

286 287 288 289
    /* Picture format negotiation */
    const vlc_v4l2_fmt_t *selected = NULL;
    vlc_fourcc_t reqfourcc = var_InheritFourCC (demux, CFG_PREFIX"chroma");
    bool native = false;
290

291 292 293 294 295
    for (struct v4l2_fmtdesc codec = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
         v4l2_ioctl (fd, VIDIOC_ENUM_FMT, &codec) >= 0;
         codec.index++)
    {   /* Enumerate available chromas */
        const vlc_v4l2_fmt_t *dsc = vlc_from_v4l2_fourcc (codec.pixelformat);
296

297 298 299 300 301 302
        msg_Dbg (demux, " %s %s format %4.4s (%4.4s): %s",
              (codec.flags & V4L2_FMT_FLAG_EMULATED) ? "emulates" : "supports",
              (codec.flags & V4L2_FMT_FLAG_COMPRESSED) ? "compressed" : "raw",
                 (char *)&codec.pixelformat,
                 (dsc != NULL) ? (const char *)&dsc->vlc : "N.A.",
                 codec.description);
303

304 305
        if (dsc == NULL)
            continue; /* ignore VLC-unsupported codec */
306

307 308 309 310 311 312 313
        if (dsc->vlc == reqfourcc)
        {
            msg_Dbg (demux, "  matches the requested format");
            selected = dsc;
            break; /* always select the requested format if found */
        }

314 315 316 317
        if (codec.flags & V4L2_FMT_FLAG_EMULATED)
        {
            if (native)
                continue; /* ignore emulated format if possible */
318
        }
319 320
        else
            native = true;
321

322 323
        if (vlc_v4l2_fmt_rank (dsc) > vlc_v4l2_fmt_rank (selected))
            continue; /* ignore if rank is worse */
324

325
        selected = dsc;
326
    }
327 328

    if (selected == NULL)
329
    {
330 331
        msg_Err (demux, "cannot negotiate supported video format");
        return -1;
332
    }
333 334
    msg_Dbg (demux, "selected format %4.4s (%4.4s)",
             (const char *)&selected->v4l2, (const char *)&selected->vlc);
335

336 337 338 339
    /* Find best resolution and frame rate available */
    struct v4l2_format fmt;
    struct v4l2_streamparm parm;
    if (SetupFormat (demux, fd, selected->v4l2, &fmt, &parm))
340
        return -1;
341 342 343 344 345

    /* Print extra info */
    msg_Dbg (demux, "%d bytes maximum for complete image",
             fmt.fmt.pix.sizeimage);
    /* Check interlacing */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
346
    sys->block_flags = 0;
347 348 349 350 351 352 353
    switch (fmt.fmt.pix.field)
    {
        case V4L2_FIELD_NONE:
            msg_Dbg (demux, "Interlacing setting: progressive");
            break;
        case V4L2_FIELD_TOP:
            msg_Dbg (demux, "Interlacing setting: top field only");
354
            sys->block_flags = BLOCK_FLAG_TOP_FIELD_FIRST|BLOCK_FLAG_SINGLE_FIELD;
355 356 357
            break;
        case V4L2_FIELD_BOTTOM:
            msg_Dbg (demux, "Interlacing setting: bottom field only");
358
            sys->block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST|BLOCK_FLAG_SINGLE_FIELD;
359 360 361 362
            break;
        case V4L2_FIELD_INTERLACED:
            msg_Dbg (demux, "Interlacing setting: interleaved");
            /*if (NTSC)
363
                sys->block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
364
            else*/
365
                sys->block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
366 367 368 369 370 371 372 373 374
            break;
        case V4L2_FIELD_SEQ_TB:
            msg_Dbg (demux, "Interlacing setting: sequential top bottom (TODO)");
            break;
        case V4L2_FIELD_SEQ_BT:
            msg_Dbg (demux, "Interlacing setting: sequential bottom top (TODO)");
            break;
        case V4L2_FIELD_ALTERNATE:
            msg_Dbg (demux, "Interlacing setting: alternate fields (TODO)");
375
            fmt.fmt.pix.height *= 2;
376 377 378
            break;
        case V4L2_FIELD_INTERLACED_TB:
            msg_Dbg (demux, "Interlacing setting: interleaved top bottom");
379
            sys->block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
380 381 382
            break;
        case V4L2_FIELD_INTERLACED_BT:
            msg_Dbg (demux, "Interlacing setting: interleaved bottom top");
383
            sys->block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
384 385 386 387 388 389 390
            break;
        default:
            msg_Warn (demux, "Interlacing setting: unknown type (%d)",
                      fmt.fmt.pix.field);
            break;
    }

391
    /* Declare our unique elementary (video) stream */
392
    es_format_t es_fmt;
393

394 395 396 397
    es_format_Init (&es_fmt, VIDEO_ES, selected->vlc);
    es_fmt.video.i_rmask = selected->red;
    es_fmt.video.i_gmask = selected->green;
    es_fmt.video.i_bmask = selected->blue;
398 399 400 401 402
    es_fmt.video.i_visible_width = fmt.fmt.pix.width;
    if (fmt.fmt.pix.bytesperline != 0 && selected->bpp != 0)
        es_fmt.video.i_width = fmt.fmt.pix.bytesperline / selected->bpp;
    else
        es_fmt.video.i_width = fmt.fmt.pix.width;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
403
    es_fmt.video.i_visible_height =
404 405 406
    es_fmt.video.i_height = fmt.fmt.pix.height;
    es_fmt.video.i_frame_rate = parm.parm.capture.timeperframe.denominator;
    es_fmt.video.i_frame_rate_base = parm.parm.capture.timeperframe.numerator;
407
    GetAR (fd, &es_fmt.video.i_sar_num, &es_fmt.video.i_sar_den);
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    msg_Dbg (demux, "color primaries: %u", fmt.fmt.pix.colorspace);
    switch (fmt.fmt.pix.colorspace)
    {
        case V4L2_COLORSPACE_DEFAULT:
            break;
        case V4L2_COLORSPACE_SMPTE170M:
            es_fmt.video.primaries = COLOR_PRIMARIES_BT601_525;
            es_fmt.video.transfer = TRANSFER_FUNC_BT709;
            es_fmt.video.space = COLOR_SPACE_BT601;
            break;
        case V4L2_COLORSPACE_SMPTE240M: /* not supported */
            break;
        case V4L2_COLORSPACE_REC709:
            es_fmt.video.primaries = COLOR_PRIMARIES_BT709;
            es_fmt.video.transfer = TRANSFER_FUNC_BT709;
            es_fmt.video.space = COLOR_SPACE_BT709;
            break;
        case V4L2_COLORSPACE_470_SYSTEM_M:
            es_fmt.video.transfer = TRANSFER_FUNC_BT709;
            es_fmt.video.space = COLOR_SPACE_BT601;
            break;
        case V4L2_COLORSPACE_470_SYSTEM_BG:
            es_fmt.video.primaries = COLOR_PRIMARIES_BT601_625;
            es_fmt.video.transfer = TRANSFER_FUNC_BT709;
            es_fmt.video.space = COLOR_SPACE_BT601;
            break;
        case V4L2_COLORSPACE_JPEG:
            es_fmt.video.primaries = COLOR_PRIMARIES_SRGB;
            es_fmt.video.transfer = TRANSFER_FUNC_SRGB;
            es_fmt.video.space = COLOR_SPACE_BT601;
            es_fmt.video.b_color_range_full = true;
            break;
        case V4L2_COLORSPACE_SRGB:
            es_fmt.video.primaries = COLOR_PRIMARIES_SRGB;
            es_fmt.video.transfer = TRANSFER_FUNC_SRGB;
            es_fmt.video.space = COLOR_SPACE_UNDEF; /* sYCC unsupported */
            break;
        case V4L2_COLORSPACE_ADOBERGB: /* not supported */
            es_fmt.video.space = COLOR_SPACE_BT601;
            break;
        case V4L2_COLORSPACE_BT2020:
            es_fmt.video.primaries = COLOR_PRIMARIES_BT2020;
            es_fmt.video.transfer = TRANSFER_FUNC_BT2020;
            es_fmt.video.space = COLOR_SPACE_BT2020;
            break;
        case V4L2_COLORSPACE_RAW:
            es_fmt.video.transfer = TRANSFER_FUNC_LINEAR;
            break;
        case V4L2_COLORSPACE_DCI_P3:
            es_fmt.video.primaries = COLOR_PRIMARIES_DCI_P3;
            es_fmt.video.transfer = TRANSFER_FUNC_UNDEF;
            es_fmt.video.space = COLOR_SPACE_BT2020;
            break;
        default:
            msg_Warn (demux, "unknown color space %u", fmt.fmt.pix.colorspace);
            break;
    }

    msg_Dbg (demux, "transfer function: %u", fmt.fmt.pix.xfer_func);
    switch (fmt.fmt.pix.xfer_func)
    {
        case V4L2_XFER_FUNC_DEFAULT:
            /* If transfer function is default, the transfer function is
             * inferred from the colorspace value for backward compatibility.
             * See V4L2 documentation for details. */
            break;
        case V4L2_XFER_FUNC_709:
            es_fmt.video.transfer = TRANSFER_FUNC_BT709;
            break;
        case V4L2_XFER_FUNC_SRGB:
            es_fmt.video.transfer = TRANSFER_FUNC_SRGB;
            break;
        case V4L2_XFER_FUNC_ADOBERGB:
        case V4L2_XFER_FUNC_SMPTE240M:
            es_fmt.video.transfer = TRANSFER_FUNC_UNDEF;
            break;
        case V4L2_XFER_FUNC_NONE:
            es_fmt.video.transfer = TRANSFER_FUNC_LINEAR;
            break;
        case V4L2_XFER_FUNC_DCI_P3:
        case V4L2_XFER_FUNC_SMPTE2084:
            es_fmt.video.transfer = TRANSFER_FUNC_UNDEF;
            break;
        default:
            msg_Warn (demux, "unknown transfer function %u",
                      fmt.fmt.pix.xfer_func);
            break;
    }

    msg_Dbg (demux, "YCbCr encoding: %u", fmt.fmt.pix.ycbcr_enc);
    switch (fmt.fmt.pix.ycbcr_enc)
    {
        case V4L2_YCBCR_ENC_DEFAULT:
            /* Same as transfer function - use color space value */
            break;
        case V4L2_YCBCR_ENC_601:
            es_fmt.video.space = COLOR_SPACE_BT601;
            break;
        case V4L2_YCBCR_ENC_709:
            es_fmt.video.space = COLOR_SPACE_BT709;
            break;
        case V4L2_YCBCR_ENC_XV601:
        case V4L2_YCBCR_ENC_XV709:
        case V4L2_YCBCR_ENC_SYCC:
            break;
        case V4L2_YCBCR_ENC_BT2020:
            es_fmt.video.space = COLOR_SPACE_BT2020;
            break;
        case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
        case V4L2_YCBCR_ENC_SMPTE240M:
            break;
        default:
            msg_Err (demux, "unknown YCbCr encoding: %u",
                     fmt.fmt.pix.ycbcr_enc);
            break;
    }

    msg_Dbg (demux, "quantization: %u", fmt.fmt.pix.quantization);
    switch (fmt.fmt.pix.quantization)
    {
        case V4L2_QUANTIZATION_DEFAULT:
            break;
        case V4L2_QUANTIZATION_FULL_RANGE:
            es_fmt.video.b_color_range_full = true;
            break;
        case V4L2_QUANTIZATION_LIM_RANGE:
            es_fmt.video.b_color_range_full = false;
            break;
        default:
            msg_Err (demux, "unknown quantization: %u",
                     fmt.fmt.pix.quantization);
            break;
    }

543 544 545
    msg_Dbg (demux, "added new video ES %4.4s %ux%u (%ux%u)",
             (char *)&es_fmt.i_codec,
             es_fmt.video.i_visible_width, es_fmt.video.i_visible_height,
546 547 548
             es_fmt.video.i_width, es_fmt.video.i_height);
    msg_Dbg (demux, " frame rate: %u/%u", es_fmt.video.i_frame_rate,
             es_fmt.video.i_frame_rate_base);
549 550
    msg_Dbg (demux, " aspect ratio: %u/%u", es_fmt.video.i_sar_num,
             es_fmt.video.i_sar_den);
551
    sys->es = es_out_Add (demux->out, &es_fmt);
552 553

    /* Init I/O method */
554 555
    void *(*entry) (void *);
    if (caps & V4L2_CAP_STREAMING)
556
    {
557
        if (0 /* BROKEN */ && StartUserPtr (VLC_OBJECT(demux), fd) == 0)
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
        {
            /* In principles, mmap() will pad the length to a multiple of the
             * page size, so there is no need to care. Nevertheless with the
             * page size, block->i_size can be set optimally. */
            const long pagemask = sysconf (_SC_PAGE_SIZE) - 1;

            sys->blocksize = (fmt.fmt.pix.sizeimage + pagemask) & ~pagemask;
            sys->bufv = NULL;
            entry = UserPtrThread;
            msg_Dbg (demux, "streaming with %"PRIu32"-bytes user buffers",
                     sys->blocksize);
        }
        else /* fall back to memory map */
        {
            sys->bufc = 4;
            sys->bufv = StartMmap (VLC_OBJECT(demux), fd, &sys->bufc);
            if (sys->bufv == NULL)
                return -1;
            entry = MmapThread;
            msg_Dbg (demux, "streaming with %"PRIu32" memory-mapped buffers",
                     sys->bufc);
        }
580 581 582 583
    }
    else if (caps & V4L2_CAP_READWRITE)
    {
        sys->blocksize = fmt.fmt.pix.sizeimage;
584
        sys->bufv = NULL;
585
        entry = ReadThread;
Rémi Duraffort's avatar
Rémi Duraffort committed
586
        msg_Dbg (demux, "reading %"PRIu32" bytes at a time", sys->blocksize);
587 588 589
    }
    else
    {
590
        msg_Err (demux, "no supported capture method");
591
        return -1;
592 593
    }

594
#ifdef ZVBI_COMPILED
595
    if (std & V4L2_STD_NTSC_M)
596
    {
597 598 599 600
        char *vbi_path = var_InheritString (demux, CFG_PREFIX"vbidev");
        if (vbi_path != NULL)
            sys->vbi = OpenVBI (demux, vbi_path);
        free(vbi_path);
601 602 603
    }
#endif

604
    if (vlc_clone (&sys->thread, entry, demux, VLC_THREAD_PRIORITY_INPUT))
605
    {
606 607 608 609
#ifdef ZVBI_COMPILED
        if (sys->vbi != NULL)
            CloseVBI (sys->vbi);
#endif
610 611
        if (sys->bufv != NULL)
            StopMmap (sys->fd, sys->bufv, sys->bufc);
612
        return -1;
613
    }
614 615 616
    return 0;
}

617 618 619 620 621
void DemuxClose( vlc_object_t *obj )
{
    demux_t *demux = (demux_t *)obj;
    demux_sys_t *sys = demux->p_sys;

622 623
    vlc_cancel (sys->thread);
    vlc_join (sys->thread, NULL);
624
    if (sys->bufv != NULL)
625
        StopMmap (sys->fd, sys->bufv, sys->bufc);
626
    ControlsDeinit( obj, sys->controls );
627
    v4l2_close (sys->fd);
628 629

#ifdef ZVBI_COMPILED
630 631
    if (sys->vbi != NULL)
        CloseVBI (sys->vbi);
632 633
#endif

634 635 636
    free( sys );
}

637 638 639 640
/** Allocates and queue a user buffer using mmap(). */
static block_t *UserPtrQueue (vlc_object_t *obj, int fd, size_t length)
{
    void *ptr = mmap (NULL, length, PROT_READ | PROT_WRITE,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
641
                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
642 643
    if (ptr == MAP_FAILED)
    {
644 645
        msg_Err (obj, "cannot allocate %zu-bytes buffer: %s", length,
                 vlc_strerror_c(errno));
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
        return NULL;
    }

    block_t *block = block_mmap_Alloc (ptr, length);
    if (unlikely(block == NULL))
    {
        munmap (ptr, length);
        return NULL;
    }

    struct v4l2_buffer buf = {
        .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
        .memory = V4L2_MEMORY_USERPTR,
        .m = {
            .userptr = (uintptr_t)ptr,
        },
        .length = length,
    };

    if (v4l2_ioctl (fd, VIDIOC_QBUF, &buf) < 0)
    {
667
        msg_Err (obj, "cannot queue buffer: %s", vlc_strerror_c(errno));
668 669 670 671 672 673 674 675 676 677 678
        block_Release (block);
        return NULL;
    }
    return block;
}

static void *UserPtrThread (void *data)
{
    demux_t *demux = data;
    demux_sys_t *sys = demux->p_sys;
    int fd = sys->fd;
679 680
    struct pollfd ufd[2];
    nfds_t numfds = 1;
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

    ufd[0].fd = fd;
    ufd[0].events = POLLIN;

    int canc = vlc_savecancel ();
    for (;;)
    {
        struct v4l2_buffer buf = {
            .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
            .memory = V4L2_MEMORY_USERPTR,
        };
        block_t *block = UserPtrQueue (VLC_OBJECT(demux), fd, sys->blocksize);
        if (block == NULL)
            break;

        /* Wait for data */
        vlc_restorecancel (canc);
        block_cleanup_push (block);
699
        while (poll (ufd, numfds, -1) == -1)
700
           if (errno != EINTR)
701
               msg_Err (demux, "poll error: %s", vlc_strerror_c(errno));
702 703 704
        vlc_cleanup_pop ();
        canc = vlc_savecancel ();

705
        if (v4l2_ioctl (fd, VIDIOC_DQBUF, &buf) < 0)
706
        {
707 708
            msg_Err (demux, "cannot dequeue buffer: %s",
                     vlc_strerror_c(errno));
709 710
            block_Release (block);
            continue;
711
        }
712 713 714

        assert (block->p_buffer == (void *)buf.m.userptr);
        block->i_buffer = buf.length;
715
        block->i_pts = block->i_dts = GetBufferPTS (&buf);
716
        block->i_flags |= sys->block_flags;
717
        es_out_SetPCR(demux->out, block->i_pts);
718
        es_out_Send (demux->out, sys->es, block);
719 720 721 722 723 724
    }
    vlc_restorecancel (canc); /* <- hmm, this is purely cosmetic */
    return NULL;
}

static void *MmapThread (void *data)
725 726 727
{
    demux_t *demux = data;
    demux_sys_t *sys = demux->p_sys;
728
    int fd = sys->fd;
729 730
    struct pollfd ufd[2];
    nfds_t numfds = 1;
731 732

    ufd[0].fd = fd;
733
    ufd[0].events = POLLIN;
734

735
#ifdef ZVBI_COMPILED
736
    if (sys->vbi != NULL)
737
    {
738
        ufd[1].fd = GetFdVBI (sys->vbi);
739 740 741 742 743
        ufd[1].events = POLLIN;
        numfds++;
    }
#endif

744 745 746
    for (;;)
    {
        /* Wait for data */
747
        if (poll (ufd, numfds, -1) == -1)
748 749
        {
           if (errno != EINTR)
750
               msg_Err (demux, "poll error: %s", vlc_strerror_c(errno));
751 752 753
           continue;
        }

754
        if( ufd[0].revents )
755
        {
756 757 758 759 760
            int canc = vlc_savecancel ();
            block_t *block = GrabVideo (VLC_OBJECT(demux), fd, sys->bufv);
            if (block != NULL)
            {
                block->i_flags |= sys->block_flags;
761
                es_out_SetPCR(demux->out, block->i_pts);
762 763 764
                es_out_Send (demux->out, sys->es, block);
            }
            vlc_restorecancel (canc);
765
        }
766
#ifdef ZVBI_COMPILED
767 768
        if (sys->vbi != NULL && ufd[1].revents)
            GrabVBI (demux, sys->vbi);
769
#endif
770 771
    }

772
    vlc_assert_unreachable ();
773 774 775 776 777 778
}

static void *ReadThread (void *data)
{
    demux_t *demux = data;
    demux_sys_t *sys = demux->p_sys;
779
    int fd = sys->fd;
780 781
    struct pollfd ufd[2];
    nfds_t numfds = 1;
782 783

    ufd[0].fd = fd;
784
    ufd[0].events = POLLIN;
785

786
#ifdef ZVBI_COMPILED
787
    if (sys->vbi != NULL)
788
    {
789
        ufd[1].fd = GetFdVBI (sys->vbi);
790 791 792 793 794
        ufd[1].events = POLLIN;
        numfds++;
    }
#endif

795 796 797
    for (;;)
    {
        /* Wait for data */
798
        if (poll (ufd, numfds, -1) == -1)
799 800
        {
           if (errno != EINTR)
801
               msg_Err (demux, "poll error: %s", vlc_strerror_c(errno));
802 803 804
           continue;
        }

805
        if( ufd[0].revents )
806
        {
807 808 809
            block_t *block = block_Alloc (sys->blocksize);
            if (unlikely(block == NULL))
            {
810
                msg_Err (demux, "read error: %s", vlc_strerror_c(errno));
811 812 813 814 815
                v4l2_read (fd, NULL, 0); /* discard frame */
                continue;
            }
            block->i_pts = block->i_dts = mdate ();
            block->i_flags |= sys->block_flags;
816

817 818 819 820 821
            int canc = vlc_savecancel ();
            ssize_t val = v4l2_read (fd, block->p_buffer, block->i_buffer);
            if (val != -1)
            {
                block->i_buffer = val;
822
                es_out_SetPCR(demux->out, block->i_pts);
823 824 825 826 827
                es_out_Send (demux->out, sys->es, block);
            }
            else
                block_Release (block);
            vlc_restorecancel (canc);
828
        }
829
#ifdef ZVBI_COMPILED
830 831
        if (sys->vbi != NULL && ufd[1].revents)
            GrabVBI (demux, sys->vbi);
832
#endif
833
    }
834
    vlc_assert_unreachable ();
835 836
}

837 838
static int DemuxControl( demux_t *demux, int query, va_list args )
{
839 840
    demux_sys_t *sys = demux->p_sys;

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
    switch( query )
    {
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
        case DEMUX_CAN_SEEK:
        case DEMUX_CAN_CONTROL_PACE:
            *va_arg( args, bool * ) = false;
            return VLC_SUCCESS;

        case DEMUX_GET_PTS_DELAY:
            *va_arg(args,int64_t *) = INT64_C(1000)
                * var_InheritInteger( demux, "live-caching" );
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
856
            *va_arg (args, int64_t *) = mdate() - sys->start;
857 858 859 860 861 862 863 864 865
            return VLC_SUCCESS;

        /* TODO implement others */
        default:
            return VLC_EGENERIC;
    }

    return VLC_EGENERIC;
}