2003-08-07 14:18:04 +02:00
|
|
|
/*
|
2008-12-13 13:13:48 +01:00
|
|
|
* Video 4 Linux 2 input
|
|
|
|
*
|
|
|
|
* copyright (c) 2003 Martin Olschewski <olschewski@zpr.uni-koeln.de>
|
|
|
|
* copyright (c) 2003 Jindrich Makovicka <makovick@gmail.com>
|
|
|
|
*
|
|
|
|
* Some ideas are based on works from
|
|
|
|
* Alex Beregszaszi <alex@fsn.hu>
|
|
|
|
* Gerd Knorr <kraxel@bytesex.org>
|
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* This file is part of mpv.
|
2008-12-13 13:13:48 +01:00
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2008-12-13 13:13:48 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2008-12-13 13:13:48 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2015-04-13 09:36:54 +02:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-13 13:13:48 +01:00
|
|
|
*/
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
known issues:
|
|
|
|
- norm setting isn't consistent with tvi_v4l
|
|
|
|
- the same for volume/bass/treble/balance
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-07-10 08:28:03 +02:00
|
|
|
#include <strings.h>
|
2014-06-13 12:26:32 +02:00
|
|
|
#include <time.h>
|
2003-08-07 14:18:04 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2008-08-29 22:05:08 +02:00
|
|
|
#include <math.h>
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SYS_VIDEOIO_H
|
2011-06-29 06:19:49 +02:00
|
|
|
#include <sys/videoio.h>
|
|
|
|
#else
|
2005-09-10 20:38:23 +02:00
|
|
|
#include <linux/videodev2.h>
|
2011-06-29 06:19:49 +02:00
|
|
|
#endif
|
2013-11-09 02:15:12 +01:00
|
|
|
#if HAVE_LIBV4L2
|
|
|
|
#include <libv4l2.h>
|
|
|
|
#endif
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
2014-06-14 22:17:55 +02:00
|
|
|
#include "common/common.h"
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 20:03:30 +01:00
|
|
|
#include "video/img_fourcc.h"
|
2012-11-09 01:06:43 +01:00
|
|
|
#include "audio/format.h"
|
2003-08-07 14:18:04 +02:00
|
|
|
#include "tv.h"
|
|
|
|
#include "audio_in.h"
|
|
|
|
|
2013-11-09 02:15:12 +01:00
|
|
|
#if !HAVE_LIBV4L2
|
|
|
|
#define v4l2_open open
|
|
|
|
#define v4l2_close close
|
|
|
|
#define v4l2_ioctl ioctl
|
|
|
|
#define v4l2_mmap mmap
|
|
|
|
#define v4l2_munmap munmap
|
|
|
|
#endif
|
|
|
|
|
2014-06-10 17:41:02 +02:00
|
|
|
// flag introduced in kernel 3.10
|
|
|
|
#ifndef V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
|
|
|
|
#define V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC 0x2000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HAVE_CLOCK_GETTIME (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
|
|
|
|
|
2007-03-01 19:38:00 +01:00
|
|
|
#define info tvi_info_v4l2
|
2013-12-21 20:24:20 +01:00
|
|
|
static tvi_handle_t *tvi_init_v4l2(struct mp_log *log, tv_param_t* tv_param);
|
2003-08-07 14:18:04 +02:00
|
|
|
/* information about this file */
|
2008-01-13 13:01:57 +01:00
|
|
|
const tvi_info_t tvi_info_v4l2 = {
|
2007-03-01 19:38:00 +01:00
|
|
|
tvi_init_v4l2,
|
2003-08-07 14:18:04 +02:00
|
|
|
"Video 4 Linux 2 input",
|
|
|
|
"v4l2",
|
|
|
|
};
|
|
|
|
|
|
|
|
struct map {
|
|
|
|
struct v4l2_buffer buf;
|
|
|
|
void *addr;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BUFFER_COUNT 6
|
|
|
|
|
2007-06-01 19:29:35 +02:00
|
|
|
/** video ringbuffer entry */
|
|
|
|
typedef struct {
|
|
|
|
unsigned char *data; ///< frame contents
|
|
|
|
long long timestamp; ///< frame timestamp
|
2009-05-13 04:58:57 +02:00
|
|
|
int framesize; ///< actual frame size
|
2007-06-01 19:29:35 +02:00
|
|
|
} video_buffer_entry;
|
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
/* private data */
|
2010-06-13 19:20:29 +02:00
|
|
|
typedef struct priv {
|
2003-08-07 14:18:04 +02:00
|
|
|
/* video */
|
2013-12-21 20:24:20 +01:00
|
|
|
struct mp_log *log;
|
2007-04-29 07:17:08 +02:00
|
|
|
char *video_dev;
|
|
|
|
int video_fd;
|
2003-08-07 14:18:04 +02:00
|
|
|
int mp_format;
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_capability capability;
|
2003-08-07 14:18:04 +02:00
|
|
|
struct v4l2_input input;
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_format format;
|
|
|
|
struct v4l2_standard standard;
|
|
|
|
struct v4l2_tuner tuner;
|
|
|
|
struct map *map;
|
|
|
|
int mapcount;
|
|
|
|
int frames;
|
2014-06-10 17:41:02 +02:00
|
|
|
volatile long long first_frame; ///< number of useconds
|
|
|
|
long long curr_frame; ///< usec, using kernel timestamps
|
|
|
|
int clk_id; /**< clk_id from clock_gettime
|
|
|
|
used in frame timestamps */
|
2003-08-07 14:18:04 +02:00
|
|
|
/* audio video interleaving ;-) */
|
2007-04-29 07:17:08 +02:00
|
|
|
volatile int streamon;
|
|
|
|
pthread_t audio_grabber_thread;
|
|
|
|
pthread_mutex_t skew_mutex;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
/* 2nd level video buffers */
|
|
|
|
int first;
|
|
|
|
int immediate_mode;
|
|
|
|
|
|
|
|
int video_buffer_size_max;
|
|
|
|
volatile int video_buffer_size_current;
|
2007-06-01 19:29:35 +02:00
|
|
|
video_buffer_entry *video_ringbuffer;
|
2007-04-29 07:17:08 +02:00
|
|
|
volatile int video_head;
|
|
|
|
volatile int video_tail;
|
|
|
|
volatile int video_cnt;
|
|
|
|
pthread_t video_grabber_thread;
|
2003-08-07 14:18:04 +02:00
|
|
|
pthread_mutex_t video_buffer_mutex;
|
|
|
|
|
|
|
|
/* audio */
|
2007-04-29 07:17:08 +02:00
|
|
|
char *audio_dev;
|
2003-08-07 14:18:04 +02:00
|
|
|
audio_in_t audio_in;
|
|
|
|
|
|
|
|
long long audio_start_time;
|
|
|
|
int audio_buffer_size;
|
|
|
|
int aud_skew_cnt;
|
2007-04-29 07:17:08 +02:00
|
|
|
unsigned char *audio_ringbuffer;
|
|
|
|
long long *audio_skew_buffer;
|
|
|
|
long long *audio_skew_delta_buffer;
|
|
|
|
volatile int audio_head;
|
|
|
|
volatile int audio_tail;
|
|
|
|
volatile int audio_cnt;
|
2003-08-07 14:18:04 +02:00
|
|
|
volatile long long audio_skew;
|
|
|
|
volatile double audio_skew_factor;
|
|
|
|
volatile long long audio_skew_measure_time;
|
|
|
|
volatile int audio_drop;
|
|
|
|
volatile int shutdown;
|
|
|
|
|
2008-02-14 15:23:55 +01:00
|
|
|
int audio_initialized;
|
2003-08-07 14:18:04 +02:00
|
|
|
double audio_secs_per_block;
|
2005-05-14 07:42:37 +02:00
|
|
|
long long audio_usecs_per_block;
|
2003-08-07 14:18:04 +02:00
|
|
|
long long audio_skew_total;
|
2003-08-18 11:23:07 +02:00
|
|
|
long long audio_skew_delta_total;
|
2007-04-29 07:17:08 +02:00
|
|
|
long audio_recv_blocks_total;
|
|
|
|
long audio_sent_blocks_total;
|
2005-05-14 07:42:37 +02:00
|
|
|
pthread_mutex_t audio_mutex;
|
|
|
|
int audio_insert_null_samples;
|
|
|
|
volatile long audio_null_blocks_inserted;
|
|
|
|
volatile long long dropped_frames_timeshift;
|
|
|
|
long long dropped_frames_compensated;
|
2007-07-29 12:18:38 +02:00
|
|
|
|
|
|
|
tv_param_t *tv_param;
|
2003-08-07 14:18:04 +02:00
|
|
|
} priv_t;
|
|
|
|
|
2012-08-03 00:12:46 +02:00
|
|
|
typedef struct tt_stream_props_s{
|
|
|
|
int sampling_rate;
|
|
|
|
int samples_per_line;
|
|
|
|
int offset;
|
|
|
|
int count[2]; ///< number of lines in first and second fields
|
|
|
|
int interlaced; ///< vbi data are interlaced
|
|
|
|
int bufsize; ///< required buffer size
|
|
|
|
} tt_stream_props;
|
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
#include "tvi_def.h"
|
|
|
|
|
|
|
|
static void *audio_grabber(void *data);
|
|
|
|
static void *video_grabber(void *data);
|
|
|
|
|
|
|
|
/**********************************************************************\
|
|
|
|
|
|
|
|
Only few of the fourccs are the same in v4l2 and mplayer:
|
|
|
|
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 20:03:30 +01:00
|
|
|
MP_FOURCC_YVU9 == V4L2_PIX_FMT_YVU410
|
|
|
|
MP_FOURCC_YV12 == V4L2_PIX_FMT_YVU420
|
|
|
|
MP_FOURCC_NV12 == V4L2_PIX_FMT_NV12
|
|
|
|
MP_FOURCC_422P == V4L2_PIX_FMT_YUV422P
|
|
|
|
MP_FOURCC_411P == V4L2_PIX_FMT_YUV411P
|
|
|
|
MP_FOURCC_UYVY == V4L2_PIX_FMT_UYVY
|
|
|
|
MP_FOURCC_Y41P == V4L2_PIX_FMT_Y41P
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
This may be an useful translation table for some others:
|
|
|
|
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 20:03:30 +01:00
|
|
|
MP_FOURCC_RGB8 == V4L2_PIX_FMT_RGB332
|
|
|
|
MP_FOURCC_BGR15 == V4L2_PIX_FMT_RGB555
|
|
|
|
MP_FOURCC_BGR16 == V4L2_PIX_FMT_RGB565
|
|
|
|
MP_FOURCC_RGB24 == V4L2_PIX_FMT_RGB24
|
|
|
|
MP_FOURCC_RGB32 == V4L2_PIX_FMT_RGB32
|
|
|
|
MP_FOURCC_BGR24 == V4L2_PIX_FMT_BGR24
|
|
|
|
MP_FOURCC_BGR32 == V4L2_PIX_FMT_BGR32
|
|
|
|
MP_FOURCC_Y800 == V4L2_PIX_FMT_GREY
|
|
|
|
MP_FOURCC_YUV9 == V4L2_PIX_FMT_YUV410
|
|
|
|
MP_FOURCC_I420 == V4L2_PIX_FMT_YUV420
|
|
|
|
MP_FOURCC_YUY2 == V4L2_PIX_FMT_YUYV
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
\**********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Translate a mplayer fourcc to a video4linux2 pixel format.
|
|
|
|
*/
|
|
|
|
static int fcc_mp2vl(int fcc)
|
|
|
|
{
|
|
|
|
switch (fcc) {
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 20:03:30 +01:00
|
|
|
case MP_FOURCC_RGB8: return V4L2_PIX_FMT_RGB332;
|
|
|
|
case MP_FOURCC_BGR15: return V4L2_PIX_FMT_RGB555;
|
|
|
|
case MP_FOURCC_BGR16: return V4L2_PIX_FMT_RGB565;
|
|
|
|
case MP_FOURCC_RGB24: return V4L2_PIX_FMT_RGB24;
|
|
|
|
case MP_FOURCC_RGB32: return V4L2_PIX_FMT_RGB32;
|
|
|
|
case MP_FOURCC_BGR24: return V4L2_PIX_FMT_BGR24;
|
|
|
|
case MP_FOURCC_BGR32: return V4L2_PIX_FMT_BGR32;
|
|
|
|
case MP_FOURCC_Y800: return V4L2_PIX_FMT_GREY;
|
|
|
|
case MP_FOURCC_YUV9: return V4L2_PIX_FMT_YUV410;
|
|
|
|
case MP_FOURCC_I420: return V4L2_PIX_FMT_YUV420;
|
|
|
|
case MP_FOURCC_YUY2: return V4L2_PIX_FMT_YUYV;
|
|
|
|
case MP_FOURCC_YV12: return V4L2_PIX_FMT_YVU420;
|
|
|
|
case MP_FOURCC_UYVY: return V4L2_PIX_FMT_UYVY;
|
|
|
|
case MP_FOURCC_MJPEG: return V4L2_PIX_FMT_MJPEG;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
return fcc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Translate a video4linux2 fourcc aka pixel format to mplayer.
|
|
|
|
*/
|
|
|
|
static int fcc_vl2mp(int fcc)
|
|
|
|
{
|
|
|
|
switch (fcc) {
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 20:03:30 +01:00
|
|
|
case V4L2_PIX_FMT_RGB332: return MP_FOURCC_RGB8;
|
|
|
|
case V4L2_PIX_FMT_RGB555: return MP_FOURCC_BGR15;
|
|
|
|
case V4L2_PIX_FMT_RGB565: return MP_FOURCC_BGR16;
|
|
|
|
case V4L2_PIX_FMT_RGB24: return MP_FOURCC_RGB24;
|
|
|
|
case V4L2_PIX_FMT_RGB32: return MP_FOURCC_RGB32;
|
|
|
|
case V4L2_PIX_FMT_BGR24: return MP_FOURCC_BGR24;
|
|
|
|
case V4L2_PIX_FMT_BGR32: return MP_FOURCC_BGR32;
|
|
|
|
case V4L2_PIX_FMT_GREY: return MP_FOURCC_Y800;
|
|
|
|
case V4L2_PIX_FMT_YUV410: return MP_FOURCC_YUV9;
|
|
|
|
case V4L2_PIX_FMT_YUV420: return MP_FOURCC_I420;
|
|
|
|
case V4L2_PIX_FMT_YVU420: return MP_FOURCC_YV12;
|
|
|
|
case V4L2_PIX_FMT_YUYV: return MP_FOURCC_YUY2;
|
|
|
|
case V4L2_PIX_FMT_UYVY: return MP_FOURCC_UYVY;
|
|
|
|
case V4L2_PIX_FMT_MJPEG: return MP_FOURCC_MJPEG;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
return fcc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Translate a video4linux2 fourcc aka pixel format
|
|
|
|
** to a human readable string.
|
|
|
|
*/
|
2014-06-12 00:32:45 +02:00
|
|
|
static const char *pixfmt2name(char *buf, int pixfmt)
|
2003-08-07 14:18:04 +02:00
|
|
|
{
|
|
|
|
switch (pixfmt) {
|
2007-04-29 07:17:08 +02:00
|
|
|
case V4L2_PIX_FMT_RGB332: return "RGB332";
|
|
|
|
case V4L2_PIX_FMT_RGB555: return "RGB555";
|
|
|
|
case V4L2_PIX_FMT_RGB565: return "RGB565";
|
|
|
|
case V4L2_PIX_FMT_RGB555X: return "RGB555X";
|
|
|
|
case V4L2_PIX_FMT_RGB565X: return "RGB565X";
|
|
|
|
case V4L2_PIX_FMT_BGR24: return "BGR24";
|
|
|
|
case V4L2_PIX_FMT_RGB24: return "RGB24";
|
|
|
|
case V4L2_PIX_FMT_BGR32: return "BGR32";
|
|
|
|
case V4L2_PIX_FMT_RGB32: return "RGB32";
|
|
|
|
case V4L2_PIX_FMT_GREY: return "GREY";
|
|
|
|
case V4L2_PIX_FMT_YVU410: return "YVU410";
|
|
|
|
case V4L2_PIX_FMT_YVU420: return "YVU420";
|
|
|
|
case V4L2_PIX_FMT_YUYV: return "YUYV";
|
|
|
|
case V4L2_PIX_FMT_UYVY: return "UYVY";
|
|
|
|
/* case V4L2_PIX_FMT_YVU422P: return "YVU422P"; */
|
|
|
|
/* case V4L2_PIX_FMT_YVU411P: return "YVU411P"; */
|
|
|
|
case V4L2_PIX_FMT_YUV422P: return "YUV422P";
|
|
|
|
case V4L2_PIX_FMT_YUV411P: return "YUV411P";
|
|
|
|
case V4L2_PIX_FMT_Y41P: return "Y41P";
|
|
|
|
case V4L2_PIX_FMT_NV12: return "NV12";
|
|
|
|
case V4L2_PIX_FMT_NV21: return "NV21";
|
|
|
|
case V4L2_PIX_FMT_YUV410: return "YUV410";
|
|
|
|
case V4L2_PIX_FMT_YUV420: return "YUV420";
|
|
|
|
case V4L2_PIX_FMT_YYUV: return "YYUV";
|
|
|
|
case V4L2_PIX_FMT_HI240: return "HI240";
|
|
|
|
case V4L2_PIX_FMT_WNVA: return "WNVA";
|
2007-06-01 19:29:35 +02:00
|
|
|
case V4L2_PIX_FMT_MJPEG: return "MJPEG";
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2014-06-12 00:32:45 +02:00
|
|
|
sprintf(buf, "unknown (0x%x)", pixfmt);
|
|
|
|
return buf;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Gives the depth of a video4linux2 fourcc aka pixel format in bits.
|
|
|
|
*/
|
|
|
|
static int pixfmt2depth(int pixfmt)
|
|
|
|
{
|
|
|
|
switch (pixfmt) {
|
|
|
|
case V4L2_PIX_FMT_RGB332:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 8;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_RGB555:
|
|
|
|
case V4L2_PIX_FMT_RGB565:
|
|
|
|
case V4L2_PIX_FMT_RGB555X:
|
|
|
|
case V4L2_PIX_FMT_RGB565X:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 16;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_BGR24:
|
|
|
|
case V4L2_PIX_FMT_RGB24:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 24;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_BGR32:
|
|
|
|
case V4L2_PIX_FMT_RGB32:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 32;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_GREY:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 8;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_YVU410:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 9;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_YVU420:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 12;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_YUYV:
|
|
|
|
case V4L2_PIX_FMT_UYVY:
|
|
|
|
case V4L2_PIX_FMT_YUV422P:
|
|
|
|
case V4L2_PIX_FMT_YUV411P:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 16;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_Y41P:
|
|
|
|
case V4L2_PIX_FMT_NV12:
|
|
|
|
case V4L2_PIX_FMT_NV21:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 12;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_YUV410:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 9;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_YUV420:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 12;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_YYUV:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 16;
|
2003-08-07 14:18:04 +02:00
|
|
|
case V4L2_PIX_FMT_HI240:
|
2007-04-29 07:17:08 +02:00
|
|
|
return 8;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-13 04:58:57 +02:00
|
|
|
static int amode2v4l(int amode)
|
2003-08-07 14:18:04 +02:00
|
|
|
{
|
|
|
|
switch (amode) {
|
|
|
|
case 0:
|
2007-04-29 07:17:08 +02:00
|
|
|
return V4L2_TUNER_MODE_MONO;
|
2003-08-07 14:18:04 +02:00
|
|
|
case 1:
|
2007-04-29 07:17:08 +02:00
|
|
|
return V4L2_TUNER_MODE_STEREO;
|
2003-08-07 14:18:04 +02:00
|
|
|
case 2:
|
2007-04-29 07:17:08 +02:00
|
|
|
return V4L2_TUNER_MODE_LANG1;
|
2003-08-07 14:18:04 +02:00
|
|
|
case 3:
|
2007-04-29 07:17:08 +02:00
|
|
|
return V4L2_TUNER_MODE_LANG2;
|
2003-08-07 14:18:04 +02:00
|
|
|
default:
|
2007-04-29 07:17:08 +02:00
|
|
|
return -1;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-30 19:42:35 +02:00
|
|
|
/*
|
|
|
|
** Get current FPS.
|
|
|
|
*/
|
|
|
|
static double getfps(priv_t *priv)
|
|
|
|
{
|
|
|
|
if (priv->tv_param->fps > 0)
|
|
|
|
return priv->tv_param->fps;
|
|
|
|
if (priv->standard.frameperiod.denominator && priv->standard.frameperiod.numerator)
|
|
|
|
return (double)priv->standard.frameperiod.denominator / priv->standard.frameperiod.numerator;
|
|
|
|
return 25.0;
|
|
|
|
}
|
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
// sets and sanitizes audio buffer/block sizes
|
|
|
|
static void setup_audio_buffer_sizes(priv_t *priv)
|
|
|
|
{
|
|
|
|
int bytes_per_sample = priv->audio_in.bytes_per_sample;
|
2008-06-30 19:42:35 +02:00
|
|
|
int seconds = priv->video_buffer_size_max/getfps(priv);
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
if (seconds < 5) seconds = 5;
|
|
|
|
if (seconds > 500) seconds = 500;
|
|
|
|
|
|
|
|
// make the audio buffer at least as the video buffer capacity (or 5 seconds) long
|
|
|
|
priv->audio_buffer_size = 1 + seconds*priv->audio_in.samplerate
|
2007-04-29 07:17:08 +02:00
|
|
|
*priv->audio_in.channels
|
|
|
|
*bytes_per_sample/priv->audio_in.blocksize;
|
2003-08-07 14:18:04 +02:00
|
|
|
if (priv->audio_buffer_size < 256) priv->audio_buffer_size = 256;
|
|
|
|
|
|
|
|
// make the skew buffer at least 1 second long
|
|
|
|
priv->aud_skew_cnt = 1 + 1*priv->audio_in.samplerate
|
2007-04-29 07:17:08 +02:00
|
|
|
*priv->audio_in.channels
|
|
|
|
*bytes_per_sample/priv->audio_in.blocksize;
|
2003-08-07 14:18:04 +02:00
|
|
|
if (priv->aud_skew_cnt < 16) priv->aud_skew_cnt = 16;
|
|
|
|
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(priv, "Audio capture - buffer %d blocks of %d bytes, skew average from %d meas.\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->audio_buffer_size, priv->audio_in.blocksize, priv->aud_skew_cnt);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2005-05-14 15:54:25 +02:00
|
|
|
static void init_audio(priv_t *priv)
|
|
|
|
{
|
2008-02-14 15:23:55 +01:00
|
|
|
if (priv->audio_initialized) return;
|
2005-05-14 15:54:25 +02:00
|
|
|
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->alsa)
|
2013-12-21 20:24:20 +01:00
|
|
|
audio_in_init(&priv->audio_in, priv->log, AUDIO_IN_ALSA);
|
2007-04-29 07:17:08 +02:00
|
|
|
else
|
2013-12-21 20:24:20 +01:00
|
|
|
audio_in_init(&priv->audio_in, priv->log, AUDIO_IN_OSS);
|
2005-05-14 15:54:25 +02:00
|
|
|
#else
|
2013-12-21 20:24:20 +01:00
|
|
|
audio_in_init(&priv->audio_in, priv->log, AUDIO_IN_OSS);
|
2005-05-14 15:54:25 +02:00
|
|
|
#endif
|
|
|
|
|
2007-04-29 07:17:08 +02:00
|
|
|
if (priv->audio_dev) {
|
|
|
|
audio_in_set_device(&priv->audio_in, priv->audio_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_in_set_samplerate(&priv->audio_in, 44100);
|
|
|
|
if (priv->capability.capabilities & V4L2_CAP_TUNER) {
|
|
|
|
if (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) {
|
|
|
|
audio_in_set_channels(&priv->audio_in, 2);
|
|
|
|
} else {
|
|
|
|
audio_in_set_channels(&priv->audio_in, 1);
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->forcechan >= 0) {
|
|
|
|
audio_in_set_channels(&priv->audio_in, priv->tv_param->forcechan);
|
2007-04-29 07:17:08 +02:00
|
|
|
} else {
|
|
|
|
audio_in_set_channels(&priv->audio_in, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio_in_setup(&priv->audio_in) < 0) return;
|
|
|
|
|
2008-02-14 15:23:55 +01:00
|
|
|
priv->audio_initialized = 1;
|
2005-05-14 15:54:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
** the number of milliseconds elapsed between time0 and time1
|
|
|
|
*/
|
|
|
|
static size_t difftv(struct timeval time1, struct timeval time0)
|
|
|
|
{
|
2007-04-29 07:17:08 +02:00
|
|
|
return (time1.tv_sec - time0.tv_sec) * 1000 +
|
|
|
|
(time1.tv_usec - time0.tv_usec) / 1000;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Get current video capture format.
|
|
|
|
*/
|
|
|
|
static int getfmt(priv_t *priv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
priv->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2013-11-09 02:15:12 +01:00
|
|
|
if ((i = v4l2_ioctl(priv->video_fd, VIDIOC_G_FMT, &priv->format)) < 0) {
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_ERR(priv, "ioctl get format failed: %s\n",
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
mp_strerror(errno));
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Get current video capture standard.
|
|
|
|
*/
|
|
|
|
static int getstd(priv_t *priv)
|
|
|
|
{
|
|
|
|
v4l2_std_id id;
|
|
|
|
int i=0;
|
|
|
|
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_STD, &id) < 0) {
|
2008-06-30 19:46:27 +02:00
|
|
|
struct v4l2_streamparm parm;
|
|
|
|
|
|
|
|
parm.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2013-11-09 02:15:12 +01:00
|
|
|
if(v4l2_ioctl(priv->video_fd, VIDIOC_G_PARM, &parm) >= 0) {
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_WARN(priv, "your device driver does not support VIDIOC_G_STD ioctl,"
|
|
|
|
" VIDIOC_G_PARM was used instead.\n");
|
2008-06-30 19:46:27 +02:00
|
|
|
priv->standard.index=0;
|
|
|
|
priv->standard.id=0;
|
|
|
|
priv->standard.frameperiod=parm.parm.capture.timeperframe;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get standard failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return -1;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
do {
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->standard.index = i++;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
} while (priv->standard.id != id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-10 17:41:02 +02:00
|
|
|
#if HAVE_CLOCK_GETTIME
|
|
|
|
/*
|
|
|
|
** Gets current timestamp, using specified clock id.
|
|
|
|
** @return number of microseconds.
|
|
|
|
*/
|
|
|
|
static long long get_curr_timestamp(int clk_id)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(clk_id, &ts);
|
|
|
|
return (long long)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
** Gets current timestamp, using system time.
|
|
|
|
** @return number of microseconds.
|
|
|
|
*/
|
|
|
|
static long long get_curr_timestamp(int clk_id)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return (long long)tv.tv_sec * 1000000 + tv.tv_usec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
/***********************************************************************\
|
2007-04-29 07:17:08 +02:00
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* Interface to mplayer *
|
|
|
|
* *
|
|
|
|
* *
|
2003-08-07 14:18:04 +02:00
|
|
|
\***********************************************************************/
|
|
|
|
|
2009-05-13 04:58:57 +02:00
|
|
|
static int set_mute(priv_t *priv, int value)
|
2003-08-07 14:18:04 +02:00
|
|
|
{
|
|
|
|
struct v4l2_control control;
|
|
|
|
control.id = V4L2_CID_AUDIO_MUTE;
|
|
|
|
control.value = value;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_CTRL, &control) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set mute failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-07-20 01:19:07 +02:00
|
|
|
** MPlayer uses values from -100 up to 100 for controls.
|
2003-08-07 14:18:04 +02:00
|
|
|
** Here they are scaled to what the tv card needs and applied.
|
|
|
|
*/
|
|
|
|
static int set_control(priv_t *priv, struct v4l2_control *control, int val_signed) {
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_queryctrl qctrl;
|
2003-08-07 14:18:04 +02:00
|
|
|
qctrl.id = control->id;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl query control failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (val_signed) {
|
2007-04-29 07:17:08 +02:00
|
|
|
if (control->value < 0) {
|
|
|
|
control->value = qctrl.default_value + control->value *
|
|
|
|
(qctrl.default_value - qctrl.minimum) / 100;
|
|
|
|
} else {
|
|
|
|
control->value = qctrl.default_value + control->value *
|
|
|
|
(qctrl.maximum - qctrl.default_value) / 100;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
} else {
|
2007-04-29 07:17:08 +02:00
|
|
|
if (control->value < 50) {
|
|
|
|
control->value = qctrl.default_value + (control->value-50) *
|
|
|
|
(qctrl.default_value - qctrl.minimum) / 50;
|
|
|
|
} else {
|
|
|
|
control->value = qctrl.default_value + (control->value-50) *
|
|
|
|
(qctrl.maximum - qctrl.default_value) / 50;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_CTRL, control) < 0) {
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_ERR(priv, "ioctl set %s %d failed: %s\n",
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
qctrl.name, control->value, mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set %s: %d [%d, %d]\n",
|
2003-08-07 14:18:04 +02:00
|
|
|
qctrl.name, control->value, qctrl.minimum, qctrl.maximum);
|
|
|
|
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Scale the control values back to what mplayer needs.
|
|
|
|
*/
|
|
|
|
static int get_control(priv_t *priv, struct v4l2_control *control, int val_signed) {
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_queryctrl qctrl;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
qctrl.id = control->id;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl query control failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_CTRL, control) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get %s failed: %s\n", qctrl.name, mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get %s: %d [%d, %d]\n",
|
2003-08-07 14:18:04 +02:00
|
|
|
qctrl.name, control->value, qctrl.minimum, qctrl.maximum);
|
|
|
|
|
|
|
|
if (val_signed) {
|
2007-04-29 07:17:08 +02:00
|
|
|
if (control->value < qctrl.default_value) {
|
|
|
|
control->value = (control->value - qctrl.default_value) * 100 /
|
|
|
|
(qctrl.default_value - qctrl.minimum);
|
|
|
|
} else {
|
|
|
|
control->value = (control->value - qctrl.default_value) * 100 /
|
|
|
|
(qctrl.maximum - qctrl.default_value);
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
} else {
|
2007-04-29 07:17:08 +02:00
|
|
|
if (control->value < qctrl.default_value) {
|
|
|
|
control->value = (control->value - qctrl.default_value) * 50 /
|
|
|
|
(qctrl.default_value - qctrl.minimum) + 50;
|
|
|
|
} else {
|
|
|
|
control->value = (control->value - qctrl.default_value) * 50 /
|
|
|
|
(qctrl.maximum - qctrl.default_value) + 50;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
|
2013-11-01 17:33:11 +01:00
|
|
|
static int do_control(priv_t *priv, int cmd, void *arg)
|
2003-08-07 14:18:04 +02:00
|
|
|
{
|
|
|
|
struct v4l2_control control;
|
|
|
|
struct v4l2_frequency frequency;
|
2014-06-12 00:32:45 +02:00
|
|
|
char buf[80];
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case TVI_CONTROL_IS_VIDEO:
|
2007-04-29 07:17:08 +02:00
|
|
|
return priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE?
|
|
|
|
TVI_CONTROL_TRUE: TVI_CONTROL_FALSE;
|
2006-03-08 01:21:44 +01:00
|
|
|
case TVI_CONTROL_IS_AUDIO:
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->force_audio) return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_IS_TUNER:
|
2007-04-29 07:17:08 +02:00
|
|
|
return priv->capability.capabilities & V4L2_CAP_TUNER?
|
|
|
|
TVI_CONTROL_TRUE: TVI_CONTROL_FALSE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_IMMEDIATE:
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->immediate_mode = 1;
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_GET_FPS:
|
2011-01-05 16:11:16 +01:00
|
|
|
*(float *)arg = getfps(priv);
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get fps: %f\n", *(float *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_GET_BITS:
|
2007-04-29 07:17:08 +02:00
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
*(int *)arg = pixfmt2depth(priv->format.fmt.pix.pixelformat);
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get depth: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_GET_FORMAT:
|
2007-04-29 07:17:08 +02:00
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
*(int *)arg = fcc_vl2mp(priv->format.fmt.pix.pixelformat);
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get format: %s\n",
|
|
|
|
pixfmt2name(buf, priv->format.fmt.pix.pixelformat));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_SET_FORMAT:
|
2007-04-29 07:17:08 +02:00
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
priv->format.fmt.pix.pixelformat = fcc_mp2vl(*(int *)arg);
|
|
|
|
priv->format.fmt.pix.field = V4L2_FIELD_ANY;
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->mp_format = *(int *)arg;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set format: %s\n",
|
2014-06-12 00:32:45 +02:00
|
|
|
pixfmt2name(buf, priv->format.fmt.pix.pixelformat));
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set format failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
/* according to the v4l2 specs VIDIOC_S_FMT should not fail, inflexible drivers
|
|
|
|
might even always return the default parameters -> update the format here*/
|
|
|
|
priv->mp_format = fcc_vl2mp(priv->format.fmt.pix.pixelformat);
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_GET_WIDTH:
|
2007-04-29 07:17:08 +02:00
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
*(int *)arg = priv->format.fmt.pix.width;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get width: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_CHK_WIDTH:
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2009-03-16 18:12:29 +01:00
|
|
|
case TVI_CONTROL_VID_SET_WIDTH_HEIGHT:
|
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
priv->format.fmt.pix.width = ((int *)arg)[0];
|
|
|
|
priv->format.fmt.pix.height = ((int *)arg)[1];
|
|
|
|
priv->format.fmt.pix.field = V4L2_FIELD_ANY;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0)
|
2009-03-16 18:12:29 +01:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_SET_WIDTH:
|
2007-04-29 07:17:08 +02:00
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
priv->format.fmt.pix.width = *(int *)arg;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set width: %d\n", *(int *)arg);
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set width failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_GET_HEIGHT:
|
2007-04-29 07:17:08 +02:00
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
*(int *)arg = priv->format.fmt.pix.height;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get height: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_CHK_HEIGHT:
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_VID_SET_HEIGHT:
|
2007-04-29 07:17:08 +02:00
|
|
|
if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
priv->format.fmt.pix.height = *(int *)arg;
|
|
|
|
priv->format.fmt.pix.field = V4L2_FIELD_ANY;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set height: %d\n", *(int *)arg);
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set height failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
case TVI_CONTROL_VID_GET_BRIGHTNESS:
|
|
|
|
control.id = V4L2_CID_BRIGHTNESS;
|
|
|
|
if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
|
|
|
|
*(int *)arg = control.value;
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
case TVI_CONTROL_VID_SET_BRIGHTNESS:
|
|
|
|
control.id = V4L2_CID_BRIGHTNESS;
|
|
|
|
control.value = *(int *)arg;
|
|
|
|
return set_control(priv, &control, 1);
|
|
|
|
case TVI_CONTROL_VID_GET_HUE:
|
|
|
|
control.id = V4L2_CID_HUE;
|
|
|
|
if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
|
|
|
|
*(int *)arg = control.value;
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
case TVI_CONTROL_VID_SET_HUE:
|
|
|
|
control.id = V4L2_CID_HUE;
|
|
|
|
control.value = *(int *)arg;
|
|
|
|
return set_control(priv, &control, 1);
|
|
|
|
case TVI_CONTROL_VID_GET_SATURATION:
|
|
|
|
control.id = V4L2_CID_SATURATION;
|
|
|
|
if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
|
|
|
|
*(int *)arg = control.value;
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
case TVI_CONTROL_VID_SET_SATURATION:
|
|
|
|
control.id = V4L2_CID_SATURATION;
|
|
|
|
control.value = *(int *)arg;
|
|
|
|
return set_control(priv, &control, 1);
|
2007-09-18 18:28:39 +02:00
|
|
|
case TVI_CONTROL_VID_GET_GAIN:
|
|
|
|
{
|
|
|
|
|
|
|
|
control.id = V4L2_CID_AUTOGAIN;
|
|
|
|
if(get_control(priv,&control,0)!=TVI_CONTROL_TRUE)
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
|
|
|
|
if(control.value){ //Auto Gain control is enabled
|
|
|
|
*(int*)arg=0;
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Manual Gain control
|
|
|
|
control.id = V4L2_CID_GAIN;
|
|
|
|
if(get_control(priv,&control,0)!=TVI_CONTROL_TRUE)
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
|
|
|
|
*(int*)arg=control.value?control.value:1;
|
|
|
|
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_VID_SET_GAIN:
|
|
|
|
{
|
|
|
|
//value==0 means automatic gain control
|
2014-04-13 18:00:51 +02:00
|
|
|
int value=*(int*)arg;
|
2007-09-18 18:28:39 +02:00
|
|
|
|
|
|
|
if (value < 0 || value>100)
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
|
|
|
|
control.id=value?V4L2_CID_GAIN:V4L2_CID_AUTOGAIN;
|
|
|
|
control.value=value?value:1;
|
|
|
|
|
|
|
|
return set_control(priv,&control,0);
|
|
|
|
}
|
2007-04-29 07:17:08 +02:00
|
|
|
case TVI_CONTROL_VID_GET_CONTRAST:
|
|
|
|
control.id = V4L2_CID_CONTRAST;
|
|
|
|
if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
|
|
|
|
*(int *)arg = control.value;
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
case TVI_CONTROL_VID_SET_CONTRAST:
|
|
|
|
control.id = V4L2_CID_CONTRAST;
|
|
|
|
control.value = *(int *)arg;
|
|
|
|
return set_control(priv, &control, 1);
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_TUN_GET_FREQ:
|
2007-04-29 07:17:08 +02:00
|
|
|
frequency.tuner = 0;
|
|
|
|
frequency.type = V4L2_TUNER_ANALOG_TV;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_FREQUENCY, &frequency) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get frequency failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
*(int *)arg = frequency.frequency;
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_TUN_SET_FREQ:
|
|
|
|
#if 0
|
2007-04-29 07:17:08 +02:00
|
|
|
set_mute(priv, 1);
|
|
|
|
usleep(100000); // wait to suppress noise during switching
|
2003-08-07 14:18:04 +02:00
|
|
|
#endif
|
2007-04-29 07:17:08 +02:00
|
|
|
frequency.tuner = 0;
|
|
|
|
frequency.type = V4L2_TUNER_ANALOG_TV;
|
|
|
|
frequency.frequency = *(int *)arg;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_FREQUENCY, &frequency) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set frequency failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
#if 0
|
2007-04-29 07:17:08 +02:00
|
|
|
usleep(100000); // wait to suppress noise during switching
|
|
|
|
set_mute(priv, 0);
|
2003-08-07 14:18:04 +02:00
|
|
|
#endif
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_TUN_GET_TUNER:
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get tuner\n");
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get tuner failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_TUN_SET_TUNER:
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set tuner\n");
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set tuner failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_TUN_GET_NORM:
|
2007-04-29 07:17:08 +02:00
|
|
|
*(int *)arg = priv->standard.index;
|
|
|
|
return TVI_CONTROL_TRUE;
|
2007-08-23 18:09:30 +02:00
|
|
|
case TVI_CONTROL_TUN_GET_SIGNAL:
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get tuner failed: %s\n", mp_strerror(errno));
|
2007-08-23 18:09:30 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
*(int*)arg=100*(priv->tuner.signal>>8)/255;
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_TUN_SET_NORM:
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->standard.index = *(int *)arg;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl enum norm failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set norm: %s\n", priv->standard.name);
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_STD, &priv->standard.id) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set norm failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_TRUE;
|
2004-11-19 14:04:24 +01:00
|
|
|
case TVI_CONTROL_SPC_GET_NORMID:
|
2007-04-29 07:17:08 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
struct v4l2_standard standard;
|
|
|
|
memset(&standard, 0, sizeof(standard));
|
|
|
|
standard.index = i;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (-1 == v4l2_ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard))
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
if (!strcasecmp(standard.name, (char *)arg)) {
|
|
|
|
*(int *)arg = i;
|
|
|
|
return TVI_CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_SPC_GET_INPUT:
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_INPUT, (int *)arg) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get input failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_SPC_SET_INPUT:
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set input: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->input.index = *(int *)arg;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &priv->input) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl enum input failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_INPUT, (int *)arg) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set input failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_AUD_GET_FORMAT:
|
2007-04-29 07:17:08 +02:00
|
|
|
init_audio(priv);
|
2008-02-14 15:23:55 +01:00
|
|
|
if (!priv->audio_initialized) return TVI_CONTROL_FALSE;
|
2014-09-23 21:04:37 +02:00
|
|
|
*(int *)arg = AF_FORMAT_S16;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get audio format: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_AUD_GET_SAMPLERATE:
|
2007-04-29 07:17:08 +02:00
|
|
|
init_audio(priv);
|
2008-02-14 15:23:55 +01:00
|
|
|
if (!priv->audio_initialized) return TVI_CONTROL_FALSE;
|
2007-04-29 07:17:08 +02:00
|
|
|
*(int *)arg = priv->audio_in.samplerate;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get audio samplerate: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_AUD_GET_CHANNELS:
|
2007-04-29 07:17:08 +02:00
|
|
|
init_audio(priv);
|
2008-02-14 15:23:55 +01:00
|
|
|
if (!priv->audio_initialized) return TVI_CONTROL_FALSE;
|
2007-04-29 07:17:08 +02:00
|
|
|
*(int *)arg = priv->audio_in.channels;
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "get audio channels: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
case TVI_CONTROL_AUD_SET_SAMPLERATE:
|
2007-04-29 07:17:08 +02:00
|
|
|
init_audio(priv);
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "set audio samplerate: %d\n", *(int *)arg);
|
2007-04-29 07:17:08 +02:00
|
|
|
if (audio_in_set_samplerate(&priv->audio_in, *(int*)arg) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
// setup_audio_buffer_sizes(priv);
|
|
|
|
return TVI_CONTROL_TRUE;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "unknown control: %d\n", cmd);
|
2008-05-16 10:43:15 +02:00
|
|
|
return TVI_CONTROL_UNKNOWN;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define PRIV ((priv_t *) (tvi_handle->priv))
|
|
|
|
|
|
|
|
/* handler creator - entry point ! */
|
2013-12-21 20:24:20 +01:00
|
|
|
static tvi_handle_t *tvi_init_v4l2(struct mp_log *log, tv_param_t* tv_param)
|
2003-08-07 14:18:04 +02:00
|
|
|
{
|
|
|
|
tvi_handle_t *tvi_handle;
|
|
|
|
|
2013-12-21 20:24:20 +01:00
|
|
|
tvi_handle = tv_new_handle(sizeof(priv_t), log, &functions);
|
2003-08-07 14:18:04 +02:00
|
|
|
if (!tvi_handle) {
|
2007-04-29 07:17:08 +02:00
|
|
|
return NULL;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
PRIV->log = log;
|
2003-08-07 14:18:04 +02:00
|
|
|
PRIV->video_fd = -1;
|
|
|
|
|
2007-07-29 12:18:38 +02:00
|
|
|
PRIV->video_dev = strdup(tv_param->device? tv_param->device: "/dev/video0");
|
2003-08-07 14:18:04 +02:00
|
|
|
if (!PRIV->video_dev) {
|
2010-09-12 01:53:15 +02:00
|
|
|
tv_free_handle(tvi_handle);
|
2007-04-29 07:17:08 +02:00
|
|
|
return NULL;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 12:18:38 +02:00
|
|
|
if (tv_param->adevice) {
|
|
|
|
PRIV->audio_dev = strdup(tv_param->adevice);
|
2007-04-29 07:17:08 +02:00
|
|
|
if (!PRIV->audio_dev) {
|
|
|
|
free(PRIV->video_dev);
|
2010-09-12 01:53:15 +02:00
|
|
|
tv_free_handle(tvi_handle);
|
2007-04-29 07:17:08 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 12:18:38 +02:00
|
|
|
PRIV->tv_param=tv_param;
|
2003-08-07 14:18:04 +02:00
|
|
|
return tvi_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef PRIV
|
|
|
|
|
|
|
|
|
|
|
|
static int uninit(priv_t *priv)
|
|
|
|
{
|
|
|
|
int i, frames, dropped = 0;
|
|
|
|
|
|
|
|
priv->shutdown = 1;
|
2005-08-06 20:19:58 +02:00
|
|
|
if(priv->video_grabber_thread)
|
2007-04-29 07:17:08 +02:00
|
|
|
pthread_join(priv->video_grabber_thread, NULL);
|
2003-08-07 14:18:04 +02:00
|
|
|
pthread_mutex_destroy(&priv->video_buffer_mutex);
|
|
|
|
|
|
|
|
if (priv->streamon) {
|
2007-04-29 07:17:08 +02:00
|
|
|
/* get performance */
|
2009-03-15 17:12:05 +01:00
|
|
|
frames = 1 + lrintf((double)(priv->curr_frame - priv->first_frame) / 1e6 * getfps(priv));
|
2007-04-29 07:17:08 +02:00
|
|
|
dropped = frames - priv->frames;
|
|
|
|
|
|
|
|
/* turn off streaming */
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_STREAMOFF, &(priv->map[0].buf.type)) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl streamoff failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
|
|
|
priv->streamon = 0;
|
|
|
|
|
2014-10-25 16:34:56 +02:00
|
|
|
/* unqueue all remaining buffers (not sure if this code is correct) */
|
|
|
|
for (i = 0; i < priv->mapcount; i++) {
|
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_DQBUF, &priv->map[i].buf) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "VIDIOC_DQBUF failed: %s\n", mp_strerror(errno));
|
2014-10-25 16:34:56 +02:00
|
|
|
}
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unmap all buffers */
|
|
|
|
for (i = 0; i < priv->mapcount; i++) {
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_munmap(priv->map[i].addr, priv->map[i].len) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "munmap capture buffer failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stop audio thread */
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio && priv->audio_grabber_thread) {
|
2007-04-29 07:17:08 +02:00
|
|
|
pthread_join(priv->audio_grabber_thread, NULL);
|
|
|
|
pthread_mutex_destroy(&priv->skew_mutex);
|
|
|
|
pthread_mutex_destroy(&priv->audio_mutex);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2006-02-14 21:13:55 +01:00
|
|
|
set_mute(priv, 1);
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
/* free memory and close device */
|
2007-04-29 07:17:08 +02:00
|
|
|
free(priv->map); priv->map = NULL;
|
2003-08-07 14:18:04 +02:00
|
|
|
priv->mapcount = 0;
|
2013-11-09 02:15:12 +01:00
|
|
|
if(priv->video_fd!=-1)v4l2_close(priv->video_fd); priv->video_fd = -1;
|
2007-04-29 07:17:08 +02:00
|
|
|
free(priv->video_dev); priv->video_dev = NULL;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
if (priv->video_ringbuffer) {
|
2013-07-23 00:45:23 +02:00
|
|
|
for (int n = 0; n < priv->video_buffer_size_current; n++) {
|
|
|
|
free(priv->video_ringbuffer[n].data);
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
|
|
|
free(priv->video_ringbuffer);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio) {
|
2010-11-07 13:47:40 +01:00
|
|
|
free(priv->audio_ringbuffer);
|
|
|
|
free(priv->audio_skew_buffer);
|
|
|
|
free(priv->audio_skew_delta_buffer);
|
2007-11-13 12:34:45 +01:00
|
|
|
|
|
|
|
audio_in_uninit(&priv->audio_in);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* show some nice statistics ;-) */
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_INFO(priv, "%d frames successfully processed, %d frames dropped.\n",
|
|
|
|
priv->frames, dropped);
|
|
|
|
MP_VERBOSE(priv, "up to %u video frames buffered.\n",
|
|
|
|
priv->video_buffer_size_current);
|
2003-08-07 14:18:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* initialisation */
|
|
|
|
static int init(priv_t *priv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
priv->audio_ringbuffer = NULL;
|
|
|
|
priv->audio_skew_buffer = NULL;
|
2003-08-18 11:23:07 +02:00
|
|
|
priv->audio_skew_delta_buffer = NULL;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
2008-02-14 15:23:55 +01:00
|
|
|
priv->audio_initialized = 0;
|
2005-05-14 15:54:25 +02:00
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
/* Open the video device. */
|
2013-11-09 02:15:12 +01:00
|
|
|
priv->video_fd = v4l2_open(priv->video_dev, O_RDWR);
|
2003-08-07 14:18:04 +02:00
|
|
|
if (priv->video_fd < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "unable to open '%s': %s\n", priv->video_dev, mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
uninit(priv);
|
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_DBG(priv, "video fd: %s: %d\n", priv->video_dev, priv->video_fd);
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Query the video capabilities and current settings
|
|
|
|
** for further control calls.
|
|
|
|
*/
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_QUERYCAP, &priv->capability) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl query capabilities failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
uninit(priv);
|
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE))
|
|
|
|
{
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_ERR(priv, "Device %s is not a video capture device.\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->video_dev);
|
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2006-04-11 12:19:53 +02:00
|
|
|
if (getfmt(priv) < 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
uninit(priv);
|
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2006-04-11 12:19:53 +02:00
|
|
|
getstd(priv);
|
2003-08-07 14:18:04 +02:00
|
|
|
/*
|
|
|
|
** if this device has got a tuner query it's settings
|
|
|
|
** otherwise set some nice defaults
|
|
|
|
*/
|
|
|
|
if (priv->capability.capabilities & V4L2_CAP_TUNER) {
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get tuner failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
uninit(priv);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, "Selected device: %s\n", priv->capability.card);
|
2003-08-07 14:18:04 +02:00
|
|
|
if (priv->capability.capabilities & V4L2_CAP_TUNER) {
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, " Tuner cap:%s%s%s\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
(priv->tuner.capability & V4L2_TUNER_CAP_STEREO) ? " STEREO" : "",
|
|
|
|
(priv->tuner.capability & V4L2_TUNER_CAP_LANG1) ? " LANG1" : "",
|
|
|
|
(priv->tuner.capability & V4L2_TUNER_CAP_LANG2) ? " LANG2" : "");
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, " Tuner rxs:%s%s%s%s\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
(priv->tuner.rxsubchans & V4L2_TUNER_SUB_MONO) ? " MONO" : "",
|
|
|
|
(priv->tuner.rxsubchans & V4L2_TUNER_SUB_STEREO) ? " STEREO" : "",
|
|
|
|
(priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG1) ? " LANG1" : "",
|
|
|
|
(priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG2) ? " LANG2" : "");
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, " Capabilities:%s%s%s%s%s%s%s%s%s%s%s\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE?
|
|
|
|
" video capture": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_VIDEO_OUTPUT?
|
|
|
|
" video output": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_VIDEO_OVERLAY?
|
|
|
|
" video overlay": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_VBI_CAPTURE?
|
|
|
|
" VBI capture device": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_VBI_OUTPUT?
|
|
|
|
" VBI output": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_RDS_CAPTURE?
|
|
|
|
" RDS data capture": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_TUNER?
|
|
|
|
" tuner": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_AUDIO?
|
|
|
|
" audio": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_READWRITE?
|
|
|
|
" read/write": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_ASYNCIO?
|
|
|
|
" async i/o": "",
|
|
|
|
priv->capability.capabilities & V4L2_CAP_STREAMING?
|
|
|
|
" streaming": "");
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, " supported norms:");
|
2003-08-07 14:18:04 +02:00
|
|
|
for (i = 0;; i++) {
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_standard standard;
|
|
|
|
memset(&standard, 0, sizeof(standard));
|
|
|
|
standard.index = i;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (-1 == v4l2_ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard))
|
2007-04-29 07:17:08 +02:00
|
|
|
break;
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, " %d = %s;", i, standard.name);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, "\n inputs:");
|
2003-08-07 14:18:04 +02:00
|
|
|
for (i = 0; 1; i++) {
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_input input;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
2007-04-29 07:17:08 +02:00
|
|
|
input.index = i;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, " %d = %s;", i, input.name);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2009-04-10 12:13:41 +02:00
|
|
|
i = -1;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_INPUT, &i) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl get input failed: %s\n", mp_strerror(errno));
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2014-06-12 00:32:45 +02:00
|
|
|
char buf[80];
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, "\n Current input: %d\n", i);
|
2003-08-07 14:18:04 +02:00
|
|
|
for (i = 0; ; i++) {
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_fmtdesc fmtdesc;
|
|
|
|
|
|
|
|
fmtdesc.index = i;
|
|
|
|
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(priv, " Format %-6s (%2d bits, %s)\n",
|
2014-06-12 00:32:45 +02:00
|
|
|
pixfmt2name(buf, fmtdesc.pixelformat), pixfmt2depth(fmtdesc.pixelformat),
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 20:03:30 +01:00
|
|
|
fmtdesc.description);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_INFO(priv, " Current format: %s\n",
|
2014-06-12 00:32:45 +02:00
|
|
|
pixfmt2name(buf, priv->format.fmt.pix.pixelformat));
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
/* set some nice defaults */
|
|
|
|
if (getfmt(priv) < 0) return 0;
|
|
|
|
priv->format.fmt.pix.width = 640;
|
|
|
|
priv->format.fmt.pix.height = 480;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set format failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
uninit(priv);
|
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2007-07-29 12:21:48 +02:00
|
|
|
// if (!(priv->capability.capabilities & V4L2_CAP_AUDIO) && !priv->tv_param->force_audio) priv->tv_param->noaudio = 1;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
if (priv->capability.capabilities & V4L2_CAP_TUNER) {
|
2007-04-29 07:17:08 +02:00
|
|
|
struct v4l2_control control;
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->amode >= 0) {
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "setting audio mode\n");
|
2007-07-29 12:21:48 +02:00
|
|
|
priv->tuner.audmode = amode2v4l(priv->tv_param->amode);
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl set tuner failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return TVI_CONTROL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_INFO(priv, "current audio mode is :%s%s%s%s\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
(priv->tuner.audmode == V4L2_TUNER_MODE_MONO) ? " MONO" : "",
|
|
|
|
(priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) ? " STEREO" : "",
|
|
|
|
(priv->tuner.audmode == V4L2_TUNER_MODE_LANG1) ? " LANG1" : "",
|
|
|
|
(priv->tuner.audmode == V4L2_TUNER_MODE_LANG2) ? " LANG2" : "");
|
|
|
|
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->volume >= 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
control.id = V4L2_CID_AUDIO_VOLUME;
|
2007-07-29 12:21:48 +02:00
|
|
|
control.value = priv->tv_param->volume;
|
2007-04-29 07:17:08 +02:00
|
|
|
set_control(priv, &control, 0);
|
|
|
|
}
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->bass >= 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
control.id = V4L2_CID_AUDIO_BASS;
|
2007-07-29 12:21:48 +02:00
|
|
|
control.value = priv->tv_param->bass;
|
2007-04-29 07:17:08 +02:00
|
|
|
set_control(priv, &control, 0);
|
|
|
|
}
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->treble >= 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
control.id = V4L2_CID_AUDIO_TREBLE;
|
2007-07-29 12:21:48 +02:00
|
|
|
control.value = priv->tv_param->treble;
|
2007-04-29 07:17:08 +02:00
|
|
|
set_control(priv, &control, 0);
|
|
|
|
}
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->balance >= 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
control.id = V4L2_CID_AUDIO_BALANCE;
|
2007-07-29 12:21:48 +02:00
|
|
|
control.value = priv->tv_param->balance;
|
2007-04-29 07:17:08 +02:00
|
|
|
set_control(priv, &control, 0);
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_capture_buffer_size(priv_t *priv)
|
|
|
|
{
|
2011-06-26 16:42:35 +02:00
|
|
|
uint64_t bufsize;
|
|
|
|
int cnt;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
2007-07-29 12:21:48 +02:00
|
|
|
if (priv->tv_param->buffer_size >= 0) {
|
|
|
|
bufsize = priv->tv_param->buffer_size*1024*1024;
|
2003-08-07 14:18:04 +02:00
|
|
|
} else {
|
2011-06-26 16:42:35 +02:00
|
|
|
bufsize = 16*1024*1024;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2007-06-01 19:29:35 +02:00
|
|
|
cnt = bufsize/priv->format.fmt.pix.sizeimage;
|
2003-08-07 14:18:04 +02:00
|
|
|
if (cnt < 2) cnt = 2;
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* that's the real start, we'got the format parameters (checked with control) */
|
|
|
|
static int start(priv_t *priv)
|
|
|
|
{
|
|
|
|
struct v4l2_requestbuffers request;
|
2007-09-08 12:15:08 +02:00
|
|
|
unsigned int i;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
/* setup audio parameters */
|
|
|
|
|
2005-05-14 15:54:25 +02:00
|
|
|
init_audio(priv);
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio && !priv->audio_initialized) return 0;
|
2005-05-14 15:54:25 +02:00
|
|
|
|
2014-10-25 17:56:29 +02:00
|
|
|
priv->video_buffer_size_max = get_capture_buffer_size(priv);
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio) {
|
2007-04-29 07:17:08 +02:00
|
|
|
setup_audio_buffer_sizes(priv);
|
|
|
|
priv->audio_skew_buffer = calloc(priv->aud_skew_cnt, sizeof(long long));
|
|
|
|
if (!priv->audio_skew_buffer) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "cannot allocate skew buffer: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
priv->audio_skew_delta_buffer = calloc(priv->aud_skew_cnt, sizeof(long long));
|
|
|
|
if (!priv->audio_skew_delta_buffer) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "cannot allocate skew buffer: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->audio_ringbuffer = calloc(priv->audio_in.blocksize, priv->audio_buffer_size);
|
|
|
|
if (!priv->audio_ringbuffer) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "cannot allocate audio buffer: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate
|
|
|
|
*priv->audio_in.channels
|
|
|
|
*priv->audio_in.bytes_per_sample);
|
|
|
|
priv->audio_usecs_per_block = 1e6*priv->audio_secs_per_block;
|
|
|
|
priv->audio_head = 0;
|
|
|
|
priv->audio_tail = 0;
|
|
|
|
priv->audio_cnt = 0;
|
|
|
|
priv->audio_drop = 0;
|
|
|
|
priv->audio_skew = 0;
|
|
|
|
priv->audio_skew_total = 0;
|
|
|
|
priv->audio_skew_delta_total = 0;
|
|
|
|
priv->audio_recv_blocks_total = 0;
|
|
|
|
priv->audio_sent_blocks_total = 0;
|
|
|
|
priv->audio_null_blocks_inserted = 0;
|
|
|
|
priv->audio_insert_null_samples = 0;
|
|
|
|
priv->dropped_frames_timeshift = 0;
|
|
|
|
priv->dropped_frames_compensated = 0;
|
|
|
|
|
|
|
|
pthread_mutex_init(&priv->skew_mutex, NULL);
|
|
|
|
pthread_mutex_init(&priv->audio_mutex, NULL);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup video parameters */
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio) {
|
2011-01-05 16:11:16 +01:00
|
|
|
if (priv->video_buffer_size_max < 3*getfps(priv)*priv->audio_secs_per_block) {
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_ERR(priv, "Video buffer shorter than 3 times audio frame duration.\n"
|
2007-04-29 07:17:08 +02:00
|
|
|
"You will probably experience heavy framedrops.\n");
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
{
|
2007-04-29 07:17:08 +02:00
|
|
|
int bytesperline = priv->format.fmt.pix.width*pixfmt2depth(priv->format.fmt.pix.pixelformat)/8;
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(priv, "Using a ring buffer for maximum %d frames, %d MB total size.\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->video_buffer_size_max,
|
|
|
|
priv->video_buffer_size_max*priv->format.fmt.pix.height*bytesperline/(1024*1024));
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2007-06-01 19:29:35 +02:00
|
|
|
priv->video_ringbuffer = calloc(priv->video_buffer_size_max, sizeof(video_buffer_entry));
|
2003-08-07 14:18:04 +02:00
|
|
|
if (!priv->video_ringbuffer) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "cannot allocate video buffer: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2005-05-14 07:42:37 +02:00
|
|
|
pthread_mutex_init(&priv->video_buffer_mutex, NULL);
|
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
priv->video_head = 0;
|
|
|
|
priv->video_tail = 0;
|
|
|
|
priv->video_cnt = 0;
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
/* request buffers */
|
2014-10-25 17:56:29 +02:00
|
|
|
request.count = BUFFER_COUNT;
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
request.memory = V4L2_MEMORY_MMAP;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_REQBUFS, &request) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl request buffers failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* query buffers */
|
2006-06-05 00:41:27 +02:00
|
|
|
if (!(priv->map = calloc(request.count, sizeof(struct map)))) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "malloc capture buffers failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* map and queue buffers */
|
|
|
|
for (i = 0; i < request.count; i++) {
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->map[i].buf.index = i;
|
|
|
|
priv->map[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
priv->map[i].buf.memory = V4L2_MEMORY_MMAP;
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_QUERYBUF, &(priv->map[i].buf)) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl query buffer failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
free(priv->map);
|
|
|
|
priv->map = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-09 02:15:12 +01:00
|
|
|
priv->map[i].addr = v4l2_mmap (0, priv->map[i].buf.length, PROT_READ |
|
2007-04-29 07:17:08 +02:00
|
|
|
PROT_WRITE, MAP_SHARED, priv->video_fd, priv->map[i].buf.m.offset);
|
|
|
|
if (priv->map[i].addr == MAP_FAILED) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "mmap capture buffer failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->map[i].len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
priv->map[i].len = priv->map[i].buf.length;
|
2014-06-10 17:41:02 +02:00
|
|
|
#ifdef HAVE_CLOCK_GETTIME
|
|
|
|
priv->clk_id = (priv->map[i].buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC)
|
|
|
|
? CLOCK_MONOTONIC : CLOCK_REALTIME;
|
|
|
|
#else
|
|
|
|
if (priv->map[i].buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC)
|
2014-06-14 21:26:17 +02:00
|
|
|
MP_WARN(priv, "compiled without clock_gettime() that is needed to handle monotone video timestamps from the kernel. Expect desync.\n");
|
2014-06-10 17:41:02 +02:00
|
|
|
#endif
|
2016-06-25 20:07:38 +02:00
|
|
|
/* count up to make sure this is correct every time */
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->mapcount++;
|
|
|
|
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl queue buffer failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* start audio thread */
|
|
|
|
priv->shutdown = 0;
|
|
|
|
priv->audio_skew_measure_time = 0;
|
|
|
|
priv->first_frame = 0;
|
|
|
|
priv->audio_skew = 0;
|
|
|
|
priv->first = 1;
|
|
|
|
|
2006-02-14 21:13:55 +01:00
|
|
|
set_mute(priv, 0);
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copies a video frame
|
2007-06-01 19:29:35 +02:00
|
|
|
static inline void copy_frame(priv_t *priv, video_buffer_entry *dest, unsigned char *source,int len)
|
2003-08-07 14:18:04 +02:00
|
|
|
{
|
2007-06-01 19:29:35 +02:00
|
|
|
dest->framesize=len;
|
2007-07-29 12:21:48 +02:00
|
|
|
if(priv->tv_param->automute>0){
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) >= 0) {
|
2007-07-29 12:21:48 +02:00
|
|
|
if(priv->tv_param->automute<<8>priv->tuner.signal){
|
2014-04-13 18:00:51 +02:00
|
|
|
fill_blank_frame(dest->data,dest->framesize,fcc_vl2mp(priv->format.fmt.pix.pixelformat));
|
|
|
|
set_mute(priv,1);
|
|
|
|
return;
|
|
|
|
}
|
2007-05-31 19:49:51 +02:00
|
|
|
}
|
|
|
|
set_mute(priv,0);
|
|
|
|
}
|
2007-06-01 19:29:35 +02:00
|
|
|
memcpy(dest->data, source, len);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// maximum skew change, in frames
|
|
|
|
#define MAX_SKEW_DELTA 0.6
|
|
|
|
static void *video_grabber(void *data)
|
|
|
|
{
|
|
|
|
priv_t *priv = (priv_t*)data;
|
2005-05-14 07:42:37 +02:00
|
|
|
long long skew, prev_skew, xskew, interval, prev_interval, delta;
|
2003-08-07 14:18:04 +02:00
|
|
|
int i;
|
2007-06-01 19:29:35 +02:00
|
|
|
int framesize = priv->format.fmt.pix.sizeimage;
|
2003-08-07 14:18:04 +02:00
|
|
|
fd_set rdset;
|
|
|
|
struct timeval timeout;
|
|
|
|
struct v4l2_buffer buf;
|
|
|
|
|
|
|
|
xskew = 0;
|
|
|
|
skew = 0;
|
|
|
|
interval = 0;
|
|
|
|
prev_interval = 0;
|
|
|
|
prev_skew = 0;
|
|
|
|
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_VERBOSE(priv, "going to capture\n");
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_STREAMON, &(priv->format.type)) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl streamon failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
priv->streamon = 1;
|
|
|
|
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio) {
|
2007-04-29 07:17:08 +02:00
|
|
|
pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (priv->frames = 0; !priv->shutdown;)
|
|
|
|
{
|
2007-04-29 07:17:08 +02:00
|
|
|
int ret;
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2014-10-25 17:56:29 +02:00
|
|
|
while (priv->video_cnt == priv->video_buffer_size_max) {
|
|
|
|
usleep(10000);
|
|
|
|
if (priv->shutdown) {
|
|
|
|
return NULL;
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2007-04-29 07:17:08 +02:00
|
|
|
FD_ZERO (&rdset);
|
|
|
|
FD_SET (priv->video_fd, &rdset);
|
|
|
|
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
i = select(priv->video_fd + 1, &rdset, NULL, NULL, &timeout);
|
|
|
|
if (i < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "select failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (i == 0) {
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_ERR(priv, "select timeout\n");
|
2007-04-29 07:17:08 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!FD_ISSET(priv->video_fd, &rdset)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&buf,0,sizeof(buf));
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
2013-11-09 02:15:12 +01:00
|
|
|
ret = v4l2_ioctl(priv->video_fd, VIDIOC_DQBUF, &buf);
|
2007-04-29 07:17:08 +02:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
/*
|
|
|
|
if there's no signal, the buffer might me dequeued
|
|
|
|
so we query all the buffers to see which one we should
|
|
|
|
put back to queue
|
|
|
|
|
|
|
|
observed with saa7134 0.2.8
|
|
|
|
don't know if is it a bug or (mis)feature
|
|
|
|
*/
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_ERR(priv, "ioctl dequeue buffer failed: %s, idx = %d\n",
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
mp_strerror(errno), buf.index);
|
2007-04-29 07:17:08 +02:00
|
|
|
for (i = 0; i < priv->mapcount; i++) {
|
|
|
|
memset(&buf,0,sizeof(buf));
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
buf.index = i;
|
2013-11-09 02:15:12 +01:00
|
|
|
ret = v4l2_ioctl(priv->video_fd, VIDIOC_QUERYBUF, &buf);
|
2007-04-29 07:17:08 +02:00
|
|
|
if (ret < 0) {
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_ERR(priv, "ioctl query buffer failed: %s, idx = %d\n",
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
mp_strerror(errno), buf.index);
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE)) == V4L2_BUF_FLAG_MAPPED) {
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) {
|
2014-10-25 16:42:14 +02:00
|
|
|
MP_ERR(priv, "ioctl queue buffer failed: %s\n",
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
2009-05-13 04:58:57 +02:00
|
|
|
}
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store the timestamp of the very first frame as reference */
|
|
|
|
if (!priv->frames++) {
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio) pthread_mutex_lock(&priv->skew_mutex);
|
2014-06-14 21:29:40 +02:00
|
|
|
priv->first_frame = buf.timestamp.tv_sec * 1000000LL + buf.timestamp.tv_usec;
|
2014-06-09 23:54:45 +02:00
|
|
|
if (priv->tv_param->audio) pthread_mutex_unlock(&priv->skew_mutex);
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
2014-06-14 21:29:40 +02:00
|
|
|
priv->curr_frame = buf.timestamp.tv_sec * 1000000LL + buf.timestamp.tv_usec;
|
2010-10-04 08:40:32 +02:00
|
|
|
// fprintf(stderr, "idx = %d, ts = %f\n", buf.index, (double)(priv->curr_frame) / 1e6);
|
2007-04-29 07:17:08 +02:00
|
|
|
|
|
|
|
interval = priv->curr_frame - priv->first_frame;
|
|
|
|
delta = interval - prev_interval;
|
|
|
|
|
2014-10-25 17:56:29 +02:00
|
|
|
// interpolate the skew in time
|
|
|
|
if (priv->tv_param->audio) pthread_mutex_lock(&priv->skew_mutex);
|
|
|
|
xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor;
|
|
|
|
if (priv->tv_param->audio) pthread_mutex_unlock(&priv->skew_mutex);
|
|
|
|
// correct extreme skew changes to avoid (especially) moving backwards in time
|
|
|
|
if (xskew - prev_skew > delta*MAX_SKEW_DELTA) {
|
|
|
|
skew = prev_skew + delta*MAX_SKEW_DELTA;
|
|
|
|
} else if (xskew - prev_skew < -delta*MAX_SKEW_DELTA) {
|
|
|
|
skew = prev_skew - delta*MAX_SKEW_DELTA;
|
|
|
|
} else {
|
|
|
|
skew = xskew;
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
|
|
|
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_TRACE(priv, "\nfps = %f, interval = %f, a_skew = %f, corr_skew = %f\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
delta ? (double)1e6/delta : -1,
|
|
|
|
(double)1e-6*interval, (double)1e-6*xskew, (double)1e-6*skew);
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_TRACE(priv, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt);
|
2007-04-29 07:17:08 +02:00
|
|
|
|
|
|
|
prev_skew = skew;
|
|
|
|
prev_interval = interval;
|
|
|
|
|
|
|
|
/* allocate a new buffer, if needed */
|
|
|
|
pthread_mutex_lock(&priv->video_buffer_mutex);
|
|
|
|
if (priv->video_buffer_size_current < priv->video_buffer_size_max) {
|
|
|
|
if (priv->video_cnt == priv->video_buffer_size_current) {
|
|
|
|
unsigned char *newbuf = malloc(framesize);
|
|
|
|
if (newbuf) {
|
|
|
|
memmove(priv->video_ringbuffer+priv->video_tail+1, priv->video_ringbuffer+priv->video_tail,
|
2007-06-01 19:29:35 +02:00
|
|
|
(priv->video_buffer_size_current-priv->video_tail)*sizeof(video_buffer_entry));
|
|
|
|
priv->video_ringbuffer[priv->video_tail].data = newbuf;
|
2007-04-29 07:17:08 +02:00
|
|
|
if ((priv->video_head >= priv->video_tail) && (priv->video_cnt > 0)) priv->video_head++;
|
|
|
|
priv->video_buffer_size_current++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&priv->video_buffer_mutex);
|
|
|
|
|
|
|
|
if (priv->video_cnt == priv->video_buffer_size_current) {
|
2014-10-25 17:56:29 +02:00
|
|
|
MP_ERR(priv, "\nvideo buffer full - dropping frame\n");
|
|
|
|
if (!priv->immediate_mode || priv->audio_insert_null_samples) {
|
|
|
|
pthread_mutex_lock(&priv->audio_mutex);
|
|
|
|
priv->dropped_frames_timeshift += delta;
|
|
|
|
pthread_mutex_unlock(&priv->audio_mutex);
|
2007-04-29 07:17:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (priv->immediate_mode) {
|
2014-10-25 17:56:29 +02:00
|
|
|
priv->video_ringbuffer[priv->video_tail].timestamp = interval - skew;
|
2007-04-29 07:17:08 +02:00
|
|
|
} else {
|
|
|
|
// compensate for audio skew
|
|
|
|
// negative skew => there are more audio samples, increase interval
|
|
|
|
// positive skew => less samples, shorten the interval
|
2007-06-01 19:29:35 +02:00
|
|
|
priv->video_ringbuffer[priv->video_tail].timestamp = interval - skew;
|
|
|
|
if (priv->audio_insert_null_samples && priv->video_ringbuffer[priv->video_tail].timestamp > 0) {
|
2007-04-29 07:17:08 +02:00
|
|
|
pthread_mutex_lock(&priv->audio_mutex);
|
2009-05-13 04:58:57 +02:00
|
|
|
priv->video_ringbuffer[priv->video_tail].timestamp +=
|
2007-04-29 07:17:08 +02:00
|
|
|
(priv->audio_null_blocks_inserted
|
|
|
|
- priv->dropped_frames_timeshift/priv->audio_usecs_per_block)
|
|
|
|
*priv->audio_usecs_per_block;
|
|
|
|
pthread_mutex_unlock(&priv->audio_mutex);
|
|
|
|
}
|
|
|
|
}
|
2007-06-01 19:29:35 +02:00
|
|
|
copy_frame(priv, priv->video_ringbuffer+priv->video_tail, priv->map[buf.index].addr,buf.bytesused);
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size_current;
|
|
|
|
priv->video_cnt++;
|
|
|
|
}
|
2013-11-09 02:15:12 +01:00
|
|
|
if (v4l2_ioctl(priv->video_fd, VIDIOC_QBUF, &buf) < 0) {
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
MP_ERR(priv, "ioctl queue buffer failed: %s\n", mp_strerror(errno));
|
2007-04-29 07:17:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-25 17:31:59 +02:00
|
|
|
#define MAX_LOOP 500
|
2003-08-07 14:18:04 +02:00
|
|
|
static double grab_video_frame(priv_t *priv, char *buffer, int len)
|
|
|
|
{
|
2005-11-10 21:32:47 +01:00
|
|
|
int loop_cnt = 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
if (priv->first) {
|
2007-04-29 07:17:08 +02:00
|
|
|
pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv);
|
|
|
|
priv->first = 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (priv->video_cnt == 0) {
|
2014-10-25 17:31:59 +02:00
|
|
|
usleep(1000);
|
2007-04-29 07:17:08 +02:00
|
|
|
if (loop_cnt++ > MAX_LOOP) return 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->video_buffer_mutex);
|
2014-06-14 22:17:55 +02:00
|
|
|
long long interval = priv->video_ringbuffer[priv->video_head].timestamp;
|
2007-06-01 19:29:35 +02:00
|
|
|
memcpy(buffer, priv->video_ringbuffer[priv->video_head].data, len);
|
2003-08-07 14:18:04 +02:00
|
|
|
priv->video_cnt--;
|
|
|
|
priv->video_head = (priv->video_head+1)%priv->video_buffer_size_current;
|
|
|
|
pthread_mutex_unlock(&priv->video_buffer_mutex);
|
|
|
|
|
2014-06-14 22:17:55 +02:00
|
|
|
return interval == -1 ? MP_NOPTS_VALUE : interval*1e-6;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_video_framesize(priv_t *priv)
|
|
|
|
{
|
2009-05-13 04:58:57 +02:00
|
|
|
/*
|
|
|
|
this routine will be called before grab_video_frame
|
2007-06-01 19:29:35 +02:00
|
|
|
thus let's return topmost frame's size
|
|
|
|
*/
|
|
|
|
if (priv->video_cnt)
|
|
|
|
return priv->video_ringbuffer[priv->video_head].framesize;
|
|
|
|
/*
|
|
|
|
no video frames yet available. i don't know what to do in this case,
|
|
|
|
thus let's return some fallback result (for compressed format this will be
|
|
|
|
maximum allowed frame size.
|
|
|
|
*/
|
2003-08-07 14:18:04 +02:00
|
|
|
return priv->format.fmt.pix.sizeimage;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *audio_grabber(void *data)
|
|
|
|
{
|
|
|
|
priv_t *priv = (priv_t*)data;
|
|
|
|
int i, audio_skew_ptr = 0;
|
2003-08-18 11:23:07 +02:00
|
|
|
long long current_time, prev_skew = 0, prev_skew_uncorr = 0;
|
2014-06-10 17:41:02 +02:00
|
|
|
long long start_time_avg, curr_timestamp;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
2014-06-10 17:41:02 +02:00
|
|
|
start_time_avg = priv->audio_start_time = get_curr_timestamp(priv->clk_id);
|
2003-08-07 14:18:04 +02:00
|
|
|
audio_in_start_capture(&priv->audio_in);
|
|
|
|
for (i = 0; i < priv->aud_skew_cnt; i++)
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->audio_skew_buffer[i] = 0;
|
2003-08-18 11:23:07 +02:00
|
|
|
for (i = 0; i < priv->aud_skew_cnt; i++)
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->audio_skew_delta_buffer[i] = 0;
|
2003-08-07 14:18:04 +02:00
|
|
|
|
|
|
|
for (; !priv->shutdown;)
|
|
|
|
{
|
2007-04-29 07:17:08 +02:00
|
|
|
if (audio_in_read_chunk(&priv->audio_in, priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize) < 0)
|
|
|
|
continue;
|
|
|
|
pthread_mutex_lock(&priv->skew_mutex);
|
|
|
|
if (priv->first_frame == 0) {
|
|
|
|
// there is no first frame yet (unlikely to happen)
|
2014-06-10 17:41:02 +02:00
|
|
|
start_time_avg = priv->audio_start_time = get_curr_timestamp(priv->clk_id);
|
2007-04-29 07:17:08 +02:00
|
|
|
// fprintf(stderr, "warning - first frame not yet available!\n");
|
|
|
|
pthread_mutex_unlock(&priv->skew_mutex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&priv->skew_mutex);
|
|
|
|
|
|
|
|
priv->audio_recv_blocks_total++;
|
2014-06-10 17:41:02 +02:00
|
|
|
curr_timestamp = get_curr_timestamp(priv->clk_id);
|
|
|
|
current_time = curr_timestamp - priv->audio_start_time;
|
2007-04-29 07:17:08 +02:00
|
|
|
|
|
|
|
if (priv->audio_recv_blocks_total < priv->aud_skew_cnt*2) {
|
2014-06-10 17:41:02 +02:00
|
|
|
start_time_avg += curr_timestamp - priv->audio_usecs_per_block*priv->audio_recv_blocks_total;
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->audio_start_time = start_time_avg/(priv->audio_recv_blocks_total+1);
|
|
|
|
}
|
|
|
|
|
2010-10-04 08:40:32 +02:00
|
|
|
// fprintf(stderr, "spb = %f, bs = %d, skew = %f\n", priv->audio_secs_per_block, priv->audio_in.blocksize,
|
2007-04-29 07:17:08 +02:00
|
|
|
// (double)(current_time - 1e6*priv->audio_secs_per_block*priv->audio_recv_blocks_total)/1e6);
|
|
|
|
|
|
|
|
// put the current skew into the ring buffer
|
|
|
|
priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr];
|
|
|
|
priv->audio_skew_buffer[audio_skew_ptr] = current_time
|
|
|
|
- priv->audio_usecs_per_block*priv->audio_recv_blocks_total;
|
|
|
|
priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr];
|
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->skew_mutex);
|
|
|
|
|
|
|
|
// skew calculation
|
|
|
|
|
|
|
|
// compute the sliding average of the skews
|
|
|
|
if (priv->audio_recv_blocks_total > priv->aud_skew_cnt) {
|
|
|
|
priv->audio_skew = priv->audio_skew_total/priv->aud_skew_cnt;
|
|
|
|
} else {
|
|
|
|
priv->audio_skew = priv->audio_skew_total/priv->audio_recv_blocks_total;
|
|
|
|
}
|
|
|
|
|
|
|
|
// put the current skew change (skew-prev_skew) into the ring buffer
|
|
|
|
priv->audio_skew_delta_total -= priv->audio_skew_delta_buffer[audio_skew_ptr];
|
|
|
|
priv->audio_skew_delta_buffer[audio_skew_ptr] = priv->audio_skew - prev_skew_uncorr;
|
|
|
|
priv->audio_skew_delta_total += priv->audio_skew_delta_buffer[audio_skew_ptr];
|
|
|
|
prev_skew_uncorr = priv->audio_skew; // remember the _uncorrected_ average value
|
|
|
|
|
|
|
|
audio_skew_ptr = (audio_skew_ptr+1) % priv->aud_skew_cnt; // rotate the buffer pointer
|
|
|
|
|
|
|
|
// sliding average approximates the value in the middle of the interval
|
|
|
|
// so interpolate the skew value further to the current time
|
|
|
|
priv->audio_skew += priv->audio_skew_delta_total/2;
|
|
|
|
|
|
|
|
// now finally, priv->audio_skew contains fairly good approximation
|
2009-05-13 04:58:57 +02:00
|
|
|
// of the current value
|
2007-04-29 07:17:08 +02:00
|
|
|
|
|
|
|
// current skew factor (assuming linearity)
|
|
|
|
// used for further interpolation in video_grabber
|
|
|
|
// probably overkill but seems to be necessary for
|
|
|
|
// stress testing by dropping half of the audio frames ;)
|
|
|
|
// especially when using ALSA with large block sizes
|
|
|
|
// where audio_skew remains a long while behind
|
|
|
|
if ((priv->audio_skew_measure_time != 0) && (current_time - priv->audio_skew_measure_time != 0)) {
|
|
|
|
priv->audio_skew_factor = (double)(priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time);
|
|
|
|
} else {
|
|
|
|
priv->audio_skew_factor = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->audio_skew_measure_time = current_time;
|
|
|
|
prev_skew = priv->audio_skew;
|
|
|
|
priv->audio_skew += priv->audio_start_time - priv->first_frame;
|
|
|
|
pthread_mutex_unlock(&priv->skew_mutex);
|
2009-05-13 04:58:57 +02:00
|
|
|
|
2010-10-04 08:40:32 +02:00
|
|
|
// fprintf(stderr, "audio_skew = %f, delta = %f\n", (double)priv->audio_skew/1e6, (double)priv->audio_skew_delta_total/1e6);
|
2007-04-29 07:17:08 +02:00
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->audio_mutex);
|
|
|
|
if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) {
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_ERR(priv, "\ntoo bad - dropping audio frame !\n");
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->audio_drop++;
|
|
|
|
} else {
|
|
|
|
priv->audio_tail = (priv->audio_tail+1) % priv->audio_buffer_size;
|
|
|
|
priv->audio_cnt++;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&priv->audio_mutex);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double grab_audio_frame(priv_t *priv, char *buffer, int len)
|
|
|
|
{
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_DBG(priv, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n",
|
2007-04-29 07:17:08 +02:00
|
|
|
priv, buffer, len);
|
2003-08-07 14:18:04 +02:00
|
|
|
|
2005-05-14 07:42:37 +02:00
|
|
|
// hack: if grab_audio_frame is called first, it means we are used by mplayer
|
|
|
|
// => switch to the mode which outputs audio immediately, even if
|
|
|
|
// it should be silence
|
|
|
|
if (priv->first) priv->audio_insert_null_samples = 1;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->audio_mutex);
|
|
|
|
while (priv->audio_insert_null_samples
|
2007-04-29 07:17:08 +02:00
|
|
|
&& priv->dropped_frames_timeshift - priv->dropped_frames_compensated >= priv->audio_usecs_per_block) {
|
|
|
|
// some frames were dropped - drop the corresponding number of audio blocks
|
|
|
|
if (priv->audio_drop) {
|
|
|
|
priv->audio_drop--;
|
|
|
|
} else {
|
|
|
|
if (priv->audio_head == priv->audio_tail) break;
|
|
|
|
priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size;
|
|
|
|
}
|
|
|
|
priv->dropped_frames_compensated += priv->audio_usecs_per_block;
|
2003-09-01 14:10:29 +02:00
|
|
|
}
|
|
|
|
|
2003-08-07 14:18:04 +02:00
|
|
|
// compensate for dropped audio frames
|
|
|
|
if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) {
|
2007-04-29 07:17:08 +02:00
|
|
|
priv->audio_drop--;
|
|
|
|
memset(buffer, 0, len);
|
|
|
|
goto out;
|
2005-05-14 07:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->audio_insert_null_samples && (priv->audio_head == priv->audio_tail)) {
|
2007-04-29 07:17:08 +02:00
|
|
|
// return silence to avoid desync and stuttering
|
|
|
|
memset(buffer, 0, len);
|
|
|
|
priv->audio_null_blocks_inserted++;
|
|
|
|
goto out;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
2005-05-14 07:42:37 +02:00
|
|
|
pthread_mutex_unlock(&priv->audio_mutex);
|
2003-08-07 14:18:04 +02:00
|
|
|
while (priv->audio_head == priv->audio_tail) {
|
2007-04-29 07:17:08 +02:00
|
|
|
// this is mencoder => just wait until some audio is available
|
|
|
|
usleep(10000);
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|
2005-05-14 09:52:47 +02:00
|
|
|
pthread_mutex_lock(&priv->audio_mutex);
|
2003-08-07 14:18:04 +02:00
|
|
|
memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_in.blocksize, len);
|
|
|
|
priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size;
|
|
|
|
priv->audio_cnt--;
|
2005-05-14 07:42:37 +02:00
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&priv->audio_mutex);
|
2003-08-07 14:18:04 +02:00
|
|
|
priv->audio_sent_blocks_total++;
|
|
|
|
return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_audio_framesize(priv_t *priv)
|
|
|
|
{
|
2008-05-16 10:43:15 +02:00
|
|
|
return priv->audio_in.blocksize;
|
2003-08-07 14:18:04 +02:00
|
|
|
}
|