annotate stream/tvi_v4l2.c @ 23347:eac98da72763

little fixes
author ptt
date Mon, 21 May 2007 15:54:08 +0000
parents d2c6e0c71550
children 452595810016
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
2 ** Video 4 Linux 2 input
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
3 **
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
4 ** This file is part of MPlayer, see http://mplayerhq.hu/ for info.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
5 **
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
6 ** (c) 2003 Martin Olschewski <olschewski@zpr.uni-koeln.de>
20646
ab2a5307c029 changed email address
henry
parents: 19431
diff changeset
7 ** (c) 2003 Jindrich Makovicka <makovick@gmail.com>
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
8 **
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
9 ** File licensed under the GPL, see http://www.fsf.org/ for more info.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
10 **
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
11 ** Some ideas are based on works from
16536
c515865e4f99 fix my email address
alex
parents: 16442
diff changeset
12 ** Alex Beregszaszi <alex@fsn.hu>
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
13 ** Gerd Knorr <kraxel@bytesex.org>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
14 **
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
15 ** CODE IS UNDER DEVELOPMENT, NO FEATURE REQUESTS PLEASE!
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
16 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
17
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
18 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
19
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
20 known issues:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
21 - norm setting isn't consistent with tvi_v4l
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
22 - the same for volume/bass/treble/balance
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
23
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
24 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
25
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
26 #include "config.h"
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
27
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
28 #include <errno.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
29 #include <fcntl.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
30 #include <pthread.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
31 #include <stdio.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
32 #include <string.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
33 #include <sys/ioctl.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
34 #include <sys/mman.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
35 #include <sys/time.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
36 #include <sys/types.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
37 #include <unistd.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
38 #ifdef HAVE_SYS_SYSINFO_H
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
39 #include <sys/sysinfo.h>
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
40 #endif
16442
11331109ecad - remove useless /dev/video* checks
henry
parents: 16289
diff changeset
41 #include <linux/types.h>
11331109ecad - remove useless /dev/video* checks
henry
parents: 16289
diff changeset
42 #include <linux/videodev2.h>
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16962
diff changeset
43 #include "mp_msg.h"
19431
ac69ba536915 Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents: 19271
diff changeset
44 #include "libmpcodecs/img_format.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16962
diff changeset
45 #include "libaf/af_format.h"
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
46 #include "tv.h"
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
47 #include "audio_in.h"
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
48
22381
6cabac4d35b5 tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents: 21587
diff changeset
49 #define info tvi_info_v4l2
6cabac4d35b5 tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents: 21587
diff changeset
50 static tvi_handle_t *tvi_init_v4l2(char *video_dev, char *audio_dev);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
51 /* information about this file */
22381
6cabac4d35b5 tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents: 21587
diff changeset
52 tvi_info_t tvi_info_v4l2 = {
6cabac4d35b5 tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents: 21587
diff changeset
53 tvi_init_v4l2,
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
54 "Video 4 Linux 2 input",
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
55 "v4l2",
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
56 "Martin Olschewski <olschewski@zpr.uni-koeln.de>",
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
57 "first try, more to come ;-)"
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
58 };
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
59
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
60 struct map {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
61 struct v4l2_buffer buf;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
62 void *addr;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
63 size_t len;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
64 };
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
65
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
66 #define BUFFER_COUNT 6
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
67
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
68 /* private data */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
69 typedef struct {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
70 /* video */
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
71 char *video_dev;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
72 int video_fd;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
73 int mp_format;
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
74 struct v4l2_capability capability;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
75 struct v4l2_input input;
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
76 struct v4l2_format format;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
77 struct v4l2_standard standard;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
78 struct v4l2_tuner tuner;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
79 struct map *map;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
80 int mapcount;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
81 int frames;
10851
41bad7ce4cc9 yet another sync fix
henry
parents: 10810
diff changeset
82 volatile long long first_frame;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
83 long long curr_frame;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
84 /* audio video interleaving ;-) */
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
85 volatile int streamon;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
86 pthread_t audio_grabber_thread;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
87 pthread_mutex_t skew_mutex;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
88
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
89 /* 2nd level video buffers */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
90 int first;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
91 int immediate_mode;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
92
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
93 int video_buffer_size_max;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
94 volatile int video_buffer_size_current;
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
95 unsigned char **video_ringbuffer;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
96 long long *video_timebuffer;
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
97 volatile int video_head;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
98 volatile int video_tail;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
99 volatile int video_cnt;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
100 pthread_t video_grabber_thread;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
101 pthread_mutex_t video_buffer_mutex;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
102
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
103 /* audio */
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
104 char *audio_dev;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
105 audio_in_t audio_in;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
106
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
107 long long audio_start_time;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
108 int audio_buffer_size;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
109 int aud_skew_cnt;
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
110 unsigned char *audio_ringbuffer;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
111 long long *audio_skew_buffer;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
112 long long *audio_skew_delta_buffer;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
113 volatile int audio_head;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
114 volatile int audio_tail;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
115 volatile int audio_cnt;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
116 volatile long long audio_skew;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
117 volatile double audio_skew_factor;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
118 volatile long long audio_skew_measure_time;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
119 volatile int audio_drop;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
120 volatile int shutdown;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
121
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
122 int audio_inited;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
123 double audio_secs_per_block;
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
124 long long audio_usecs_per_block;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
125 long long audio_skew_total;
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
126 long long audio_skew_delta_total;
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
127 long audio_recv_blocks_total;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
128 long audio_sent_blocks_total;
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
129 pthread_mutex_t audio_mutex;
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
130 int audio_insert_null_samples;
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
131 volatile long audio_null_blocks_inserted;
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
132 volatile long long dropped_frames_timeshift;
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
133 long long dropped_frames_compensated;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
134 } priv_t;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
135
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
136 #include "tvi_def.h"
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
137
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
138 static void *audio_grabber(void *data);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
139 static void *video_grabber(void *data);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
140
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
141 /**********************************************************************\
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
142
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
143 Only few of the fourccs are the same in v4l2 and mplayer:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
144
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
145 IMGFMT_YVU9 == V4L2_PIX_FMT_YVU410
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
146 IMGFMT_YV12 == V4L2_PIX_FMT_YVU420
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
147 IMGFMT_NV12 == V4L2_PIX_FMT_NV12
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
148 IMGFMT_422P == V4L2_PIX_FMT_YUV422P
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
149 IMGFMT_411P == V4L2_PIX_FMT_YUV411P
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
150 IMGFMT_UYVY == V4L2_PIX_FMT_UYVY
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
151 IMGFMT_Y41P == V4L2_PIX_FMT_Y41P
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
152
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
153 This may be an useful translation table for some others:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
154
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
155 IMGFMT_RGB8 == V4L2_PIX_FMT_RGB332
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
156 IMGFMT_BGR15 == V4L2_PIX_FMT_RGB555
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
157 IMGFMT_BGR16 == V4L2_PIX_FMT_RGB565
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
158 IMGFMT_RGB24 == V4L2_PIX_FMT_RGB24
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
159 IMGFMT_RGB32 == V4L2_PIX_FMT_RGB32
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
160 IMGFMT_BGR24 == V4L2_PIX_FMT_BGR24
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
161 IMGFMT_BGR32 == V4L2_PIX_FMT_BGR32
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
162 IMGFMT_Y800 == V4L2_PIX_FMT_GREY
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
163 IMGFMT_IF09 == V4L2_PIX_FMT_YUV410
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
164 IMGFMT_I420 == V4L2_PIX_FMT_YUV420
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
165 IMGFMT_YUY2 == V4L2_PIX_FMT_YUYV
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
166
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
167 \**********************************************************************/
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
168
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
169 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
170 ** Translate a mplayer fourcc to a video4linux2 pixel format.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
171 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
172 static int fcc_mp2vl(int fcc)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
173 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
174 switch (fcc) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
175 case IMGFMT_RGB8: return V4L2_PIX_FMT_RGB332;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
176 case IMGFMT_BGR15: return V4L2_PIX_FMT_RGB555;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
177 case IMGFMT_BGR16: return V4L2_PIX_FMT_RGB565;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
178 case IMGFMT_RGB24: return V4L2_PIX_FMT_RGB24;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
179 case IMGFMT_RGB32: return V4L2_PIX_FMT_RGB32;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
180 case IMGFMT_BGR24: return V4L2_PIX_FMT_BGR24;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
181 case IMGFMT_BGR32: return V4L2_PIX_FMT_BGR32;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
182 case IMGFMT_Y800: return V4L2_PIX_FMT_GREY;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
183 case IMGFMT_IF09: return V4L2_PIX_FMT_YUV410;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
184 case IMGFMT_I420: return V4L2_PIX_FMT_YUV420;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
185 case IMGFMT_YUY2: return V4L2_PIX_FMT_YUYV;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
186 case IMGFMT_YV12: return V4L2_PIX_FMT_YVU420;
11657
36879e2df6cd UYVY support (port from v4l1)
henry
parents: 10852
diff changeset
187 case IMGFMT_UYVY: return V4L2_PIX_FMT_UYVY;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
188 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
189 return fcc;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
190 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
191
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
192 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
193 ** Translate a video4linux2 fourcc aka pixel format to mplayer.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
194 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
195 static int fcc_vl2mp(int fcc)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
196 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
197 switch (fcc) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
198 case V4L2_PIX_FMT_RGB332: return IMGFMT_RGB8;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
199 case V4L2_PIX_FMT_RGB555: return IMGFMT_BGR15;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
200 case V4L2_PIX_FMT_RGB565: return IMGFMT_BGR16;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
201 case V4L2_PIX_FMT_RGB24: return IMGFMT_RGB24;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
202 case V4L2_PIX_FMT_RGB32: return IMGFMT_RGB32;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
203 case V4L2_PIX_FMT_BGR24: return IMGFMT_BGR24;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
204 case V4L2_PIX_FMT_BGR32: return IMGFMT_BGR32;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
205 case V4L2_PIX_FMT_GREY: return IMGFMT_Y800;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
206 case V4L2_PIX_FMT_YUV410: return IMGFMT_IF09;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
207 case V4L2_PIX_FMT_YUV420: return IMGFMT_I420;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
208 case V4L2_PIX_FMT_YVU420: return IMGFMT_YV12;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
209 case V4L2_PIX_FMT_YUYV: return IMGFMT_YUY2;
11657
36879e2df6cd UYVY support (port from v4l1)
henry
parents: 10852
diff changeset
210 case V4L2_PIX_FMT_UYVY: return IMGFMT_UYVY;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
211 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
212 return fcc;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
213 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
214
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
215 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
216 ** Translate a video4linux2 fourcc aka pixel format
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
217 ** to a human readable string.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
218 */
19108
5e767cabf4cd marks several read-only string parameters and function return-values which can only be used read-only as const. Patch by Stefan Huehner, stefan _AT huener-org
reynaldo
parents: 18958
diff changeset
219 static const char *pixfmt2name(int pixfmt)
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
220 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
221 static char unknown[24];
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
222
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
223 switch (pixfmt) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
224 case V4L2_PIX_FMT_RGB332: return "RGB332";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
225 case V4L2_PIX_FMT_RGB555: return "RGB555";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
226 case V4L2_PIX_FMT_RGB565: return "RGB565";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
227 case V4L2_PIX_FMT_RGB555X: return "RGB555X";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
228 case V4L2_PIX_FMT_RGB565X: return "RGB565X";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
229 case V4L2_PIX_FMT_BGR24: return "BGR24";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
230 case V4L2_PIX_FMT_RGB24: return "RGB24";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
231 case V4L2_PIX_FMT_BGR32: return "BGR32";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
232 case V4L2_PIX_FMT_RGB32: return "RGB32";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
233 case V4L2_PIX_FMT_GREY: return "GREY";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
234 case V4L2_PIX_FMT_YVU410: return "YVU410";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
235 case V4L2_PIX_FMT_YVU420: return "YVU420";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
236 case V4L2_PIX_FMT_YUYV: return "YUYV";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
237 case V4L2_PIX_FMT_UYVY: return "UYVY";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
238 /* case V4L2_PIX_FMT_YVU422P: return "YVU422P"; */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
239 /* case V4L2_PIX_FMT_YVU411P: return "YVU411P"; */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
240 case V4L2_PIX_FMT_YUV422P: return "YUV422P";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
241 case V4L2_PIX_FMT_YUV411P: return "YUV411P";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
242 case V4L2_PIX_FMT_Y41P: return "Y41P";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
243 case V4L2_PIX_FMT_NV12: return "NV12";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
244 case V4L2_PIX_FMT_NV21: return "NV21";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
245 case V4L2_PIX_FMT_YUV410: return "YUV410";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
246 case V4L2_PIX_FMT_YUV420: return "YUV420";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
247 case V4L2_PIX_FMT_YYUV: return "YYUV";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
248 case V4L2_PIX_FMT_HI240: return "HI240";
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
249 case V4L2_PIX_FMT_WNVA: return "WNVA";
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
250 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
251 sprintf(unknown, "unknown (0x%x)", pixfmt);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
252 return unknown;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
253 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
254
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
255
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
256 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
257 ** Gives the depth of a video4linux2 fourcc aka pixel format in bits.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
258 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
259 static int pixfmt2depth(int pixfmt)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
260 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
261 switch (pixfmt) {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
262 case V4L2_PIX_FMT_RGB332:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
263 return 8;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
264 case V4L2_PIX_FMT_RGB555:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
265 case V4L2_PIX_FMT_RGB565:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
266 case V4L2_PIX_FMT_RGB555X:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
267 case V4L2_PIX_FMT_RGB565X:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
268 return 16;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
269 case V4L2_PIX_FMT_BGR24:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
270 case V4L2_PIX_FMT_RGB24:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
271 return 24;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
272 case V4L2_PIX_FMT_BGR32:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
273 case V4L2_PIX_FMT_RGB32:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
274 return 32;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
275 case V4L2_PIX_FMT_GREY:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
276 return 8;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
277 case V4L2_PIX_FMT_YVU410:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
278 return 9;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
279 case V4L2_PIX_FMT_YVU420:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
280 return 12;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
281 case V4L2_PIX_FMT_YUYV:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
282 case V4L2_PIX_FMT_UYVY:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
283 case V4L2_PIX_FMT_YUV422P:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
284 case V4L2_PIX_FMT_YUV411P:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
285 return 16;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
286 case V4L2_PIX_FMT_Y41P:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
287 case V4L2_PIX_FMT_NV12:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
288 case V4L2_PIX_FMT_NV21:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
289 return 12;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
290 case V4L2_PIX_FMT_YUV410:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
291 return 9;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
292 case V4L2_PIX_FMT_YUV420:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
293 return 12;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
294 case V4L2_PIX_FMT_YYUV:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
295 return 16;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
296 case V4L2_PIX_FMT_HI240:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
297 return 8;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
298
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
299 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
300 return 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
301 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
302
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
303 static int amode2v4l(int amode)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
304 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
305 switch (amode) {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
306 case 0:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
307 return V4L2_TUNER_MODE_MONO;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
308 case 1:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
309 return V4L2_TUNER_MODE_STEREO;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
310 case 2:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
311 return V4L2_TUNER_MODE_LANG1;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
312 case 3:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
313 return V4L2_TUNER_MODE_LANG2;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
314 default:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
315 return -1;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
316 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
317 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
318
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
319
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
320 // sets and sanitizes audio buffer/block sizes
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
321 static void setup_audio_buffer_sizes(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
322 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
323 int bytes_per_sample = priv->audio_in.bytes_per_sample;
16274
67a685497d21 1000000000000000l
rfelker
parents: 16186
diff changeset
324 double fps = (double)priv->standard.frameperiod.denominator /
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
325 priv->standard.frameperiod.numerator;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
326 int seconds = priv->video_buffer_size_max/fps;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
327
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
328 if (seconds < 5) seconds = 5;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
329 if (seconds > 500) seconds = 500;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
330
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
331 // make the audio buffer at least as the video buffer capacity (or 5 seconds) long
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
332 priv->audio_buffer_size = 1 + seconds*priv->audio_in.samplerate
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
333 *priv->audio_in.channels
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
334 *bytes_per_sample/priv->audio_in.blocksize;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
335 if (priv->audio_buffer_size < 256) priv->audio_buffer_size = 256;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
336
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
337 // make the skew buffer at least 1 second long
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
338 priv->aud_skew_cnt = 1 + 1*priv->audio_in.samplerate
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
339 *priv->audio_in.channels
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
340 *bytes_per_sample/priv->audio_in.blocksize;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
341 if (priv->aud_skew_cnt < 16) priv->aud_skew_cnt = 16;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
342
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
343 mp_msg(MSGT_TV, MSGL_V, "Audio capture - buffer %d blocks of %d bytes, skew average from %d meas.\n",
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
344 priv->audio_buffer_size, priv->audio_in.blocksize, priv->aud_skew_cnt);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
345 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
346
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
347 static void init_audio(priv_t *priv)
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
348 {
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
349 if (priv->audio_inited) return;
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
350
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
351 if (!tv_param_noaudio) {
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
352 #if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
353 if (tv_param_alsa)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
354 audio_in_init(&priv->audio_in, AUDIO_IN_ALSA);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
355 else
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
356 audio_in_init(&priv->audio_in, AUDIO_IN_OSS);
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
357 #else
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
358 audio_in_init(&priv->audio_in, AUDIO_IN_OSS);
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
359 #endif
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
360
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
361 if (priv->audio_dev) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
362 audio_in_set_device(&priv->audio_in, priv->audio_dev);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
363 }
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
364
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
365 audio_in_set_samplerate(&priv->audio_in, 44100);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
366 if (priv->capability.capabilities & V4L2_CAP_TUNER) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
367 if (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
368 audio_in_set_channels(&priv->audio_in, 2);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
369 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
370 audio_in_set_channels(&priv->audio_in, 1);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
371 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
372 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
373 if (tv_param_forcechan >= 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
374 audio_in_set_channels(&priv->audio_in, tv_param_forcechan);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
375 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
376 audio_in_set_channels(&priv->audio_in, 2);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
377 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
378 }
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
379
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
380 if (audio_in_setup(&priv->audio_in) < 0) return;
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
381
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
382 priv->audio_inited = 1;
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
383 }
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
384 }
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
385
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
386 #if 0
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
387 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
388 ** the number of milliseconds elapsed between time0 and time1
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
389 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
390 static size_t difftv(struct timeval time1, struct timeval time0)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
391 {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
392 return (time1.tv_sec - time0.tv_sec) * 1000 +
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
393 (time1.tv_usec - time0.tv_usec) / 1000;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
394 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
395 #endif
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
396
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
397 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
398 ** Get current video capture format.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
399 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
400 static int getfmt(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
401 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
402 int i;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
403
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
404 priv->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
405 if ((i = ioctl(priv->video_fd, VIDIOC_G_FMT, &priv->format)) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
406 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get format failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
407 info.short_name, strerror(errno));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
408 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
409 return i;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
410 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
411
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
412
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
413 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
414 ** Get current video capture standard.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
415 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
416 static int getstd(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
417 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
418 v4l2_std_id id;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
419 int i=0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
420
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
421 if (ioctl(priv->video_fd, VIDIOC_G_STD, &id) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
422 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get standard failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
423 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
424 return -1;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
425 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
426 do {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
427 priv->standard.index = i++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
428 if (ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
429 return -1;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
430 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
431 } while (priv->standard.id != id);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
432 return 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
433 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
434
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
435 /***********************************************************************\
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
436 * *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
437 * *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
438 * Interface to mplayer *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
439 * *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
440 * *
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
441 \***********************************************************************/
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
442
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
443 static int set_mute(priv_t *priv, int value)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
444 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
445 struct v4l2_control control;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
446 control.id = V4L2_CID_AUDIO_MUTE;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
447 control.value = value;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
448 if (ioctl(priv->video_fd, VIDIOC_S_CTRL, &control) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
449 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl set mute failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
450 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
451 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
452 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
453 return 1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
454 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
455
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
456 /*
12860
03d3ab9f6400 embarassing typo
diego
parents: 12380
diff changeset
457 ** MPlayer uses values from -100 up to 100 for controls.
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
458 ** Here they are scaled to what the tv card needs and applied.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
459 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
460 static int set_control(priv_t *priv, struct v4l2_control *control, int val_signed) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
461 struct v4l2_queryctrl qctrl;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
462
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
463 qctrl.id = control->id;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
464 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
465 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
466 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
467 return TVI_CONTROL_FALSE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
468 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
469
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
470 if (val_signed) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
471 if (control->value < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
472 control->value = qctrl.default_value + control->value *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
473 (qctrl.default_value - qctrl.minimum) / 100;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
474 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
475 control->value = qctrl.default_value + control->value *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
476 (qctrl.maximum - qctrl.default_value) / 100;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
477 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
478 } else {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
479 if (control->value < 50) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
480 control->value = qctrl.default_value + (control->value-50) *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
481 (qctrl.default_value - qctrl.minimum) / 50;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
482 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
483 control->value = qctrl.default_value + (control->value-50) *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
484 (qctrl.maximum - qctrl.default_value) / 50;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
485 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
486 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
487
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
488
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
489 if (ioctl(priv->video_fd, VIDIOC_S_CTRL, control) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
490 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl set %s %d failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
491 info.short_name, qctrl.name, control->value, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
492 return TVI_CONTROL_FALSE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
493 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
494 mp_msg(MSGT_TV, MSGL_V, "%s: set %s: %d [%d, %d]\n", info.short_name,
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
495 qctrl.name, control->value, qctrl.minimum, qctrl.maximum);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
496
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
497 return TVI_CONTROL_TRUE;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
498 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
499
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
500
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
501 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
502 ** Scale the control values back to what mplayer needs.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
503 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
504 static int get_control(priv_t *priv, struct v4l2_control *control, int val_signed) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
505 struct v4l2_queryctrl qctrl;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
506
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
507 qctrl.id = control->id;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
508 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
509 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
510 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
511 return TVI_CONTROL_FALSE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
512 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
513
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
514 if (ioctl(priv->video_fd, VIDIOC_G_CTRL, control) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
515 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl get %s failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
516 info.short_name, qctrl.name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
517 return TVI_CONTROL_FALSE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
518 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
519 mp_msg(MSGT_TV, MSGL_V, "%s: get %s: %d [%d, %d]\n", info.short_name,
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
520 qctrl.name, control->value, qctrl.minimum, qctrl.maximum);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
521
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
522 if (val_signed) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
523 if (control->value < qctrl.default_value) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
524 control->value = (control->value - qctrl.default_value) * 100 /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
525 (qctrl.default_value - qctrl.minimum);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
526 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
527 control->value = (control->value - qctrl.default_value) * 100 /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
528 (qctrl.maximum - qctrl.default_value);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
529 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
530 } else {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
531 if (control->value < qctrl.default_value) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
532 control->value = (control->value - qctrl.default_value) * 50 /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
533 (qctrl.default_value - qctrl.minimum) + 50;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
534 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
535 control->value = (control->value - qctrl.default_value) * 50 /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
536 (qctrl.maximum - qctrl.default_value) + 50;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
537 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
538 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
539
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
540 return TVI_CONTROL_TRUE;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
541 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
542
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
543 static int control(priv_t *priv, int cmd, void *arg)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
544 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
545 struct v4l2_control control;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
546 struct v4l2_frequency frequency;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
547
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
548 switch(cmd) {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
549 case TVI_CONTROL_IS_VIDEO:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
550 return priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
551 TVI_CONTROL_TRUE: TVI_CONTROL_FALSE;
17765
7bf483eaa99a If we have a tuner, use that as a reason we have audio support, and do
aurel
parents: 17626
diff changeset
552 case TVI_CONTROL_IS_AUDIO:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
553 if (tv_param_force_audio) return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
554 case TVI_CONTROL_IS_TUNER:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
555 return priv->capability.capabilities & V4L2_CAP_TUNER?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
556 TVI_CONTROL_TRUE: TVI_CONTROL_FALSE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
557 case TVI_CONTROL_IMMEDIATE:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
558 priv->immediate_mode = 1;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
559 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
560 case TVI_CONTROL_VID_GET_FPS:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
561 *(float *)arg = (float)priv->standard.frameperiod.denominator /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
562 priv->standard.frameperiod.numerator;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
563 mp_msg(MSGT_TV, MSGL_V, "%s: get fps: %f\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
564 *(float *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
565 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
566 case TVI_CONTROL_VID_GET_BITS:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
567 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
568 *(int *)arg = pixfmt2depth(priv->format.fmt.pix.pixelformat);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
569 mp_msg(MSGT_TV, MSGL_V, "%s: get depth: %d\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
570 *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
571 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
572 case TVI_CONTROL_VID_GET_FORMAT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
573 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
574 *(int *)arg = fcc_vl2mp(priv->format.fmt.pix.pixelformat);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
575 mp_msg(MSGT_TV, MSGL_V, "%s: get format: %s\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
576 pixfmt2name(priv->format.fmt.pix.pixelformat));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
577 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
578 case TVI_CONTROL_VID_SET_FORMAT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
579 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
580 priv->format.fmt.pix.pixelformat = fcc_mp2vl(*(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
581 priv->format.fmt.pix.field = V4L2_FIELD_ANY;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
582
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
583 priv->mp_format = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
584 mp_msg(MSGT_TV, MSGL_V, "%s: set format: %s\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
585 pixfmt2name(priv->format.fmt.pix.pixelformat));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
586 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
587 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set format failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
588 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
589 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
590 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
591 /* according to the v4l2 specs VIDIOC_S_FMT should not fail, inflexible drivers
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
592 might even always return the default parameters -> update the format here*/
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
593 priv->mp_format = fcc_vl2mp(priv->format.fmt.pix.pixelformat);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
594 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
595 case TVI_CONTROL_VID_GET_WIDTH:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
596 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
597 *(int *)arg = priv->format.fmt.pix.width;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
598 mp_msg(MSGT_TV, MSGL_V, "%s: get width: %d\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
599 *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
600 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
601 case TVI_CONTROL_VID_CHK_WIDTH:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
602 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
603 case TVI_CONTROL_VID_SET_WIDTH:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
604 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
605 priv->format.fmt.pix.width = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
606 mp_msg(MSGT_TV, MSGL_V, "%s: set width: %d\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
607 *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
608 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
609 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set width failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
610 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
611 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
612 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
613 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
614 case TVI_CONTROL_VID_GET_HEIGHT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
615 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
616 *(int *)arg = priv->format.fmt.pix.height;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
617 mp_msg(MSGT_TV, MSGL_V, "%s: get height: %d\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
618 *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
619 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
620 case TVI_CONTROL_VID_CHK_HEIGHT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
621 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
622 case TVI_CONTROL_VID_SET_HEIGHT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
623 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
624 priv->format.fmt.pix.height = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
625 priv->format.fmt.pix.field = V4L2_FIELD_ANY;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
626 mp_msg(MSGT_TV, MSGL_V, "%s: set height: %d\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
627 *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
628 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
629 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set height failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
630 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
631 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
632 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
633 return TVI_CONTROL_TRUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
634 case TVI_CONTROL_VID_GET_BRIGHTNESS:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
635 control.id = V4L2_CID_BRIGHTNESS;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
636 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
637 *(int *)arg = control.value;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
638 return TVI_CONTROL_TRUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
639 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
640 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
641 case TVI_CONTROL_VID_SET_BRIGHTNESS:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
642 control.id = V4L2_CID_BRIGHTNESS;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
643 control.value = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
644 return set_control(priv, &control, 1);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
645 case TVI_CONTROL_VID_GET_HUE:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
646 control.id = V4L2_CID_HUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
647 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
648 *(int *)arg = control.value;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
649 return TVI_CONTROL_TRUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
650 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
651 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
652 case TVI_CONTROL_VID_SET_HUE:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
653 control.id = V4L2_CID_HUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
654 control.value = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
655 return set_control(priv, &control, 1);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
656 case TVI_CONTROL_VID_GET_SATURATION:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
657 control.id = V4L2_CID_SATURATION;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
658 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
659 *(int *)arg = control.value;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
660 return TVI_CONTROL_TRUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
661 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
662 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
663 case TVI_CONTROL_VID_SET_SATURATION:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
664 control.id = V4L2_CID_SATURATION;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
665 control.value = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
666 return set_control(priv, &control, 1);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
667 case TVI_CONTROL_VID_GET_CONTRAST:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
668 control.id = V4L2_CID_CONTRAST;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
669 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
670 *(int *)arg = control.value;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
671 return TVI_CONTROL_TRUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
672 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
673 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
674 case TVI_CONTROL_VID_SET_CONTRAST:
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
675 control.id = V4L2_CID_CONTRAST;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
676 control.value = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
677 return set_control(priv, &control, 1);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
678 case TVI_CONTROL_TUN_GET_FREQ:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
679 frequency.tuner = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
680 frequency.type = V4L2_TUNER_ANALOG_TV;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
681 if (ioctl(priv->video_fd, VIDIOC_G_FREQUENCY, &frequency) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
682 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl get frequency failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
683 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
684 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
685 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
686 *(int *)arg = frequency.frequency;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
687 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
688 case TVI_CONTROL_TUN_SET_FREQ:
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
689 #if 0
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
690 set_mute(priv, 1);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
691 usleep(100000); // wait to suppress noise during switching
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
692 #endif
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
693 frequency.tuner = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
694 frequency.type = V4L2_TUNER_ANALOG_TV;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
695 frequency.frequency = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
696 if (ioctl(priv->video_fd, VIDIOC_S_FREQUENCY, &frequency) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
697 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl set frequency failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
698 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
699 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
700 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
701 #if 0
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
702 usleep(100000); // wait to suppress noise during switching
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
703 set_mute(priv, 0);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
704 #endif
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
705 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
706 case TVI_CONTROL_TUN_GET_TUNER:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
707 mp_msg(MSGT_TV, MSGL_V, "%s: get tuner\n",info.short_name);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
708 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
709 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
710 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
711 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
712 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
713 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
714 case TVI_CONTROL_TUN_SET_TUNER:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
715 mp_msg(MSGT_TV, MSGL_V, "%s: set tuner\n",info.short_name);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
716 if (ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
717 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set tuner failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
718 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
719 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
720 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
721 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
722 case TVI_CONTROL_TUN_GET_NORM:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
723 *(int *)arg = priv->standard.index;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
724 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
725 case TVI_CONTROL_TUN_SET_NORM:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
726 priv->standard.index = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
727 if (ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
728 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum norm failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
729 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
730 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
731 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
732 mp_msg(MSGT_TV, MSGL_V, "%s: set norm: %s\n", info.short_name, priv->standard.name);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
733 if (ioctl(priv->video_fd, VIDIOC_S_STD, &priv->standard.id) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
734 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set norm failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
735 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
736 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
737 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
738 return TVI_CONTROL_TRUE;
13978
2a9afbfc79d7 setting the norm using text ID instead of numeric
henry
parents: 12860
diff changeset
739 case TVI_CONTROL_SPC_GET_NORMID:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
740 {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
741 int i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
742 for (i = 0;; i++) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
743 struct v4l2_standard standard;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
744 memset(&standard, 0, sizeof(standard));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
745 standard.index = i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
746 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard))
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
747 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
748 if (!strcasecmp(standard.name, (char *)arg)) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
749 *(int *)arg = i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
750 return TVI_CONTROL_TRUE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
751 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
752 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
753 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
754 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
755 case TVI_CONTROL_SPC_GET_INPUT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
756 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, (int *)arg) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
757 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
758 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
759 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
760 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
761 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
762 case TVI_CONTROL_SPC_SET_INPUT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
763 mp_msg(MSGT_TV, MSGL_V, "%s: set input: %d\n", info.short_name, *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
764 priv->input.index = *(int *)arg;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
765 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &priv->input) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
766 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum input failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
767 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
768 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
769 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
770 if (ioctl(priv->video_fd, VIDIOC_S_INPUT, (int *)arg) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
771 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set input failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
772 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
773 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
774 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
775 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
776 case TVI_CONTROL_AUD_GET_FORMAT:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
777 init_audio(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
778 if (!priv->audio_inited) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
779 *(int *)arg = AF_FORMAT_S16_LE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
780 mp_msg(MSGT_TV, MSGL_V, "%s: get audio format: %d\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
781 info.short_name, *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
782 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
783 case TVI_CONTROL_AUD_GET_SAMPLERATE:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
784 init_audio(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
785 if (!priv->audio_inited) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
786 *(int *)arg = priv->audio_in.samplerate;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
787 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplerate: %d\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
788 info.short_name, *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
789 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
790 case TVI_CONTROL_AUD_GET_SAMPLESIZE:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
791 init_audio(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
792 if (!priv->audio_inited) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
793 *(int *)arg = priv->audio_in.bytes_per_sample;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
794 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplesize: %d\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
795 info.short_name, *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
796 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
797 case TVI_CONTROL_AUD_GET_CHANNELS:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
798 init_audio(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
799 if (!priv->audio_inited) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
800 *(int *)arg = priv->audio_in.channels;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
801 mp_msg(MSGT_TV, MSGL_V, "%s: get audio channels: %d\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
802 info.short_name, *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
803 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
804 case TVI_CONTROL_AUD_SET_SAMPLERATE:
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
805 init_audio(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
806 mp_msg(MSGT_TV, MSGL_V, "%s: set audio samplerate: %d\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
807 info.short_name, *(int *)arg);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
808 if (audio_in_set_samplerate(&priv->audio_in, *(int*)arg) < 0) return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
809 // setup_audio_buffer_sizes(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
810 return TVI_CONTROL_TRUE;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
811 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
812 mp_msg(MSGT_TV, MSGL_V, "%s: unknown control: %d\n", info.short_name, cmd);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
813 return(TVI_CONTROL_UNKNOWN);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
814 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
815
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
816
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
817 #define PRIV ((priv_t *) (tvi_handle->priv))
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
818
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
819 /* handler creator - entry point ! */
22381
6cabac4d35b5 tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents: 21587
diff changeset
820 static tvi_handle_t *tvi_init_v4l2(char *video_dev, char *audio_dev)
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
821 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
822 tvi_handle_t *tvi_handle;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
823
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
824 /* new_handle initializes priv with memset 0 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
825 tvi_handle = new_handle();
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
826 if (!tvi_handle) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
827 return NULL;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
828 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
829 PRIV->video_fd = -1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
830
15246
849a6f39964f use the documented default video device /dev/video0 instead of /dev/video that is missing on most systems
iive
parents: 14245
diff changeset
831 PRIV->video_dev = strdup(video_dev? video_dev: "/dev/video0");
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
832 if (!PRIV->video_dev) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
833 free_handle(tvi_handle);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
834 return NULL;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
835 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
836
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
837 if (audio_dev) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
838 PRIV->audio_dev = strdup(audio_dev);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
839 if (!PRIV->audio_dev) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
840 free(PRIV->video_dev);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
841 free_handle(tvi_handle);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
842 return NULL;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
843 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
844 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
845
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
846 return tvi_handle;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
847 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
848
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
849 #undef PRIV
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
850
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
851
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
852 static int uninit(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
853 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
854 int i, frames, dropped = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
855
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
856 priv->shutdown = 1;
16185
973b82bf1187 do not crash when /dev/video0 is not present
faust3
parents: 15987
diff changeset
857 if(priv->video_grabber_thread)
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
858 pthread_join(priv->video_grabber_thread, NULL);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
859 pthread_mutex_destroy(&priv->video_buffer_mutex);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
860
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
861 if (priv->streamon) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
862 struct v4l2_buffer buf;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
863
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
864 /* get performance */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
865 frames = 1 + (priv->curr_frame - priv->first_frame +
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
866 priv->standard.frameperiod.numerator * 500000 /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
867 priv->standard.frameperiod.denominator) *
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
868 priv->standard.frameperiod.denominator /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
869 priv->standard.frameperiod.numerator / 1000000;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
870 dropped = frames - priv->frames;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
871
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
872 /* turn off streaming */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
873 if (ioctl(priv->video_fd, VIDIOC_STREAMOFF, &(priv->map[0].buf.type)) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
874 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamoff failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
875 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
876 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
877 priv->streamon = 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
878
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
879 /* unqueue all remaining buffers */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
880 memset(&buf,0,sizeof(buf));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
881 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
882 buf.memory = V4L2_MEMORY_MMAP;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
883 while (!ioctl(priv->video_fd, VIDIOC_DQBUF, &buf));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
884 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
885
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
886 /* unmap all buffers */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
887 for (i = 0; i < priv->mapcount; i++) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
888 if (munmap(priv->map[i].addr, priv->map[i].len) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
889 mp_msg(MSGT_TV, MSGL_ERR, "%s: munmap capture buffer failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
890 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
891 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
892 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
893
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
894 /* stop audio thread */
15987
51aafa55f44c fix a crash at v4l2 uninit
aurel
parents: 15464
diff changeset
895 if (!tv_param_noaudio && priv->audio_grabber_thread) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
896 pthread_join(priv->audio_grabber_thread, NULL);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
897 pthread_mutex_destroy(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
898 pthread_mutex_destroy(&priv->audio_mutex);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
899 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
900
17626
5627625b0cdb Don't test the v4l2_input audioset field for audio capabilities but still try changing the mute setting (patch by Jesse Allen < the3dfxdude _at_ gmail.com >)
aurel
parents: 17199
diff changeset
901 set_mute(priv, 1);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
902
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
903 /* free memory and close device */
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
904 free(priv->map); priv->map = NULL;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
905 priv->mapcount = 0;
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
906 if(priv->video_fd!=-1)close(priv->video_fd); priv->video_fd = -1;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
907 free(priv->video_dev); priv->video_dev = NULL;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
908
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
909 if (priv->video_ringbuffer) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
910 int i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
911 for (i = 0; i < priv->video_buffer_size_current; i++) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
912 free(priv->video_ringbuffer[i]);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
913 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
914 free(priv->video_ringbuffer);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
915 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
916 if (priv->video_timebuffer)
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
917 free(priv->video_timebuffer);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
918 if (!tv_param_noaudio) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
919 if (priv->audio_ringbuffer)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
920 free(priv->audio_ringbuffer);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
921 if (priv->audio_skew_buffer)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
922 free(priv->audio_skew_buffer);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
923 if (priv->audio_skew_delta_buffer)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
924 free(priv->audio_skew_delta_buffer);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
925 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
926
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
927 /* show some nice statistics ;-) */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
928 mp_msg(MSGT_TV, MSGL_INFO,
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
929 "%s: %d frames successfully processed, %d frames dropped.\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
930 info.short_name, priv->frames, dropped);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
931 mp_msg(MSGT_TV, MSGL_V, "%s: up to %u video frames buffered.\n",
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
932 info.short_name, priv->video_buffer_size_current);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
933 return 1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
934 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
935
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
936
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
937 /* initialisation */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
938 static int init(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
939 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
940 int i;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
941
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
942 priv->audio_ringbuffer = NULL;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
943 priv->audio_skew_buffer = NULL;
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
944 priv->audio_skew_delta_buffer = NULL;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
945
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
946 priv->audio_inited = 0;
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
947
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
948 /* Open the video device. */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
949 priv->video_fd = open(priv->video_dev, O_RDWR);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
950 if (priv->video_fd < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
951 mp_msg(MSGT_TV, MSGL_ERR, "%s: unable to open '%s': %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
952 info.short_name, priv->video_dev, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
953 uninit(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
954 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
955 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
956 mp_msg(MSGT_TV, MSGL_DBG2, "%s: video fd: %s: %d\n",
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
957 info.short_name, priv->video_dev, priv->video_fd);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
958
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
959 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
960 ** Query the video capabilities and current settings
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
961 ** for further control calls.
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
962 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
963 if (ioctl(priv->video_fd, VIDIOC_QUERYCAP, &priv->capability) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
964 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query capabilities failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
965 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
966 uninit(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
967 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
968 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
969
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
970 if (!(priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE))
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
971 {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
972 mp_msg(MSGT_TV, MSGL_ERR, "Device %s is not a video capture device.\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
973 priv->video_dev);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
974 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
975 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
976
18073
8ceb31f028ee make failures during e.g. setting the TV norm non-fatal.
reimar
parents: 17765
diff changeset
977 if (getfmt(priv) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
978 uninit(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
979 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
980 }
18073
8ceb31f028ee make failures during e.g. setting the TV norm non-fatal.
reimar
parents: 17765
diff changeset
981 getstd(priv);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
982 /*
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
983 ** if this device has got a tuner query it's settings
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
984 ** otherwise set some nice defaults
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
985 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
986 if (priv->capability.capabilities & V4L2_CAP_TUNER) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
987 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
988 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
989 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
990 uninit(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
991 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
992 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
993 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
994 mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.card);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
995 if (priv->capability.capabilities & V4L2_CAP_TUNER) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
996 mp_msg(MSGT_TV, MSGL_INFO, " Tuner cap:%s%s%s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
997 (priv->tuner.capability & V4L2_TUNER_CAP_STEREO) ? " STEREO" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
998 (priv->tuner.capability & V4L2_TUNER_CAP_LANG1) ? " LANG1" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
999 (priv->tuner.capability & V4L2_TUNER_CAP_LANG2) ? " LANG2" : "");
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1000 mp_msg(MSGT_TV, MSGL_INFO, " Tuner rxs:%s%s%s%s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1001 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_MONO) ? " MONO" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1002 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_STEREO) ? " STEREO" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1003 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG1) ? " LANG1" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1004 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG2) ? " LANG2" : "");
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1005 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1006 mp_msg(MSGT_TV, MSGL_INFO, " Capabilites:%s%s%s%s%s%s%s%s%s%s%s\n",
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1007 priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1008 " video capture": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1009 priv->capability.capabilities & V4L2_CAP_VIDEO_OUTPUT?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1010 " video output": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1011 priv->capability.capabilities & V4L2_CAP_VIDEO_OVERLAY?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1012 " video overlay": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1013 priv->capability.capabilities & V4L2_CAP_VBI_CAPTURE?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1014 " VBI capture device": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1015 priv->capability.capabilities & V4L2_CAP_VBI_OUTPUT?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1016 " VBI output": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1017 priv->capability.capabilities & V4L2_CAP_RDS_CAPTURE?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1018 " RDS data capture": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1019 priv->capability.capabilities & V4L2_CAP_TUNER?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1020 " tuner": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1021 priv->capability.capabilities & V4L2_CAP_AUDIO?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1022 " audio": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1023 priv->capability.capabilities & V4L2_CAP_READWRITE?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1024 " read/write": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1025 priv->capability.capabilities & V4L2_CAP_ASYNCIO?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1026 " async i/o": "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1027 priv->capability.capabilities & V4L2_CAP_STREAMING?
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1028 " streaming": "");
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1029 mp_msg(MSGT_TV, MSGL_INFO, " supported norms:");
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1030 for (i = 0;; i++) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1031 struct v4l2_standard standard;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1032 memset(&standard, 0, sizeof(standard));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1033 standard.index = i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1034 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard))
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1035 break;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1036 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, standard.name);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1037 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1038 mp_msg(MSGT_TV, MSGL_INFO, "\n inputs:");
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1039 for (i = 0; 1; i++) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1040 struct v4l2_input input;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1041
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1042 input.index = i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1043 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1044 break;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1045 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1046 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, input.name);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1047 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1048 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, &i) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1049 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1050 info.short_name, strerror(errno));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1051 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1052 mp_msg(MSGT_TV, MSGL_INFO, "\n Current input: %d\n", i);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1053 for (i = 0; ; i++) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1054 struct v4l2_fmtdesc fmtdesc;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1055
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1056 fmtdesc.index = i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1057 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1058 if (ioctl(priv->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1059 break;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1060 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1061 mp_msg(MSGT_TV, MSGL_V, " Format %-6s (%2d bits, %s): %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1062 pixfmt2name(fmtdesc.pixelformat), pixfmt2depth(fmtdesc.pixelformat),
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1063 fmtdesc.description, vo_format_name(fcc_vl2mp(fmtdesc.pixelformat)));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1064 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1065 mp_msg(MSGT_TV, MSGL_INFO, " Current format: %s\n",
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1066 pixfmt2name(priv->format.fmt.pix.pixelformat));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1067
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1068 /* set some nice defaults */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1069 if (getfmt(priv) < 0) return 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1070 priv->format.fmt.pix.width = 640;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1071 priv->format.fmt.pix.height = 480;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1072 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1073 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set format failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1074 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1075 uninit(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1076 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1077 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1078
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1079 // if (!(priv->capability.capabilities & V4L2_CAP_AUDIO) && !tv_param_force_audio) tv_param_noaudio = 1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1080
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1081 if (priv->capability.capabilities & V4L2_CAP_TUNER) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1082 struct v4l2_control control;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1083 if (tv_param_amode >= 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1084 mp_msg(MSGT_TV, MSGL_V, "%s: setting audio mode\n", info.short_name);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1085 priv->tuner.audmode = amode2v4l(tv_param_amode);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1086 if (ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1087 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set tuner failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1088 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1089 return TVI_CONTROL_FALSE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1090 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1091 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1092 mp_msg(MSGT_TV, MSGL_INFO, "%s: current audio mode is :%s%s%s%s\n", info.short_name,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1093 (priv->tuner.audmode == V4L2_TUNER_MODE_MONO) ? " MONO" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1094 (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) ? " STEREO" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1095 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG1) ? " LANG1" : "",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1096 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG2) ? " LANG2" : "");
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1097
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1098 if (tv_param_volume >= 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1099 control.id = V4L2_CID_AUDIO_VOLUME;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1100 control.value = tv_param_volume;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1101 set_control(priv, &control, 0);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1102 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1103 if (tv_param_bass >= 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1104 control.id = V4L2_CID_AUDIO_BASS;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1105 control.value = tv_param_bass;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1106 set_control(priv, &control, 0);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1107 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1108 if (tv_param_treble >= 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1109 control.id = V4L2_CID_AUDIO_TREBLE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1110 control.value = tv_param_treble;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1111 set_control(priv, &control, 0);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1112 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1113 if (tv_param_balance >= 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1114 control.id = V4L2_CID_AUDIO_BALANCE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1115 control.value = tv_param_balance;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1116 set_control(priv, &control, 0);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1117 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1118 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1119
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1120 return 1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1121 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1122
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1123 static int get_capture_buffer_size(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1124 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1125 int bufsize, cnt;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1126 int w = priv->format.fmt.pix.width;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1127 int h = priv->format.fmt.pix.height;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1128 int d = pixfmt2depth(priv->format.fmt.pix.pixelformat);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1129 int bytesperline = w*d/8;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1130
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1131 if (tv_param_buffer_size >= 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1132 bufsize = tv_param_buffer_size*1024*1024;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1133 } else {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1134 #ifdef HAVE_SYS_SYSINFO_H
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1135 struct sysinfo si;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1136
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1137 sysinfo(&si);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1138 if (si.totalram<2*1024*1024) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1139 bufsize = 1024*1024;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1140 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1141 bufsize = si.totalram/2;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1142 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1143 #else
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1144 bufsize = 16*1024*1024;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1145 #endif
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1146 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1147
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1148 cnt = bufsize/(h*bytesperline);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1149 if (cnt < 2) cnt = 2;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1150
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1151 return cnt;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1152 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1153
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1154 /* that's the real start, we'got the format parameters (checked with control) */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1155 static int start(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1156 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1157 struct v4l2_requestbuffers request;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1158 int i;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1159
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1160 /* setup audio parameters */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1161
15464
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
1162 init_audio(priv);
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
1163 if (!tv_param_noaudio && !priv->audio_inited) return 0;
a57ec42d7df1 do not modify tv_param_noaudio
henry
parents: 15462
diff changeset
1164
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1165 /* we need this to size the audio buffer properly */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1166 if (priv->immediate_mode) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1167 priv->video_buffer_size_max = 2;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1168 } else {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1169 priv->video_buffer_size_max = get_capture_buffer_size(priv);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1170 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1171
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1172 if (!tv_param_noaudio) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1173 setup_audio_buffer_sizes(priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1174 priv->audio_skew_buffer = calloc(priv->aud_skew_cnt, sizeof(long long));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1175 if (!priv->audio_skew_buffer) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1176 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1177 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1178 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1179 priv->audio_skew_delta_buffer = calloc(priv->aud_skew_cnt, sizeof(long long));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1180 if (!priv->audio_skew_delta_buffer) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1181 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1182 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1183 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1184
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1185 priv->audio_ringbuffer = calloc(priv->audio_in.blocksize, priv->audio_buffer_size);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1186 if (!priv->audio_ringbuffer) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1187 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate audio buffer: %s\n", strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1188 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1189 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1190
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1191 priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1192 *priv->audio_in.channels
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1193 *priv->audio_in.bytes_per_sample);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1194 priv->audio_usecs_per_block = 1e6*priv->audio_secs_per_block;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1195 priv->audio_head = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1196 priv->audio_tail = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1197 priv->audio_cnt = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1198 priv->audio_drop = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1199 priv->audio_skew = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1200 priv->audio_skew_total = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1201 priv->audio_skew_delta_total = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1202 priv->audio_recv_blocks_total = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1203 priv->audio_sent_blocks_total = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1204 priv->audio_null_blocks_inserted = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1205 priv->audio_insert_null_samples = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1206 priv->dropped_frames_timeshift = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1207 priv->dropped_frames_compensated = 0;
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1208
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1209 pthread_mutex_init(&priv->skew_mutex, NULL);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1210 pthread_mutex_init(&priv->audio_mutex, NULL);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1211 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1212
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1213 /* setup video parameters */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1214 if (!tv_param_noaudio) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1215 if (priv->video_buffer_size_max < (3*priv->standard.frameperiod.denominator) /
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1216 priv->standard.frameperiod.numerator
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1217 *priv->audio_secs_per_block) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1218 mp_msg(MSGT_TV, MSGL_ERR, "Video buffer shorter than 3 times audio frame duration.\n"
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1219 "You will probably experience heavy framedrops.\n");
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1220 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1221 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1222
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1223 {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1224 int bytesperline = priv->format.fmt.pix.width*pixfmt2depth(priv->format.fmt.pix.pixelformat)/8;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1225
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1226 mp_msg(MSGT_TV, MSGL_V, "Using a ring buffer for maximum %d frames, %d MB total size.\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1227 priv->video_buffer_size_max,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1228 priv->video_buffer_size_max*priv->format.fmt.pix.height*bytesperline/(1024*1024));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1229 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1230
18885
5c8acc972551 rm unnecesary casts from void* - part 4
reynaldo
parents: 18589
diff changeset
1231 priv->video_ringbuffer = calloc(priv->video_buffer_size_max, sizeof(unsigned char*));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1232 if (!priv->video_ringbuffer) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1233 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate video buffer: %s\n", strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1234 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1235 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1236 for (i = 0; i < priv->video_buffer_size_max; i++)
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1237 priv->video_ringbuffer[i] = NULL;
18885
5c8acc972551 rm unnecesary casts from void* - part 4
reynaldo
parents: 18589
diff changeset
1238 priv->video_timebuffer = calloc(priv->video_buffer_size_max, sizeof(long long));
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1239 if (!priv->video_timebuffer) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1240 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate time buffer: %s\n", strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1241 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1242 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1243
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1244 pthread_mutex_init(&priv->video_buffer_mutex, NULL);
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1245
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1246 priv->video_head = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1247 priv->video_tail = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1248 priv->video_cnt = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1249
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1250 /* request buffers */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1251 if (priv->immediate_mode) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1252 request.count = 2;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1253 } else {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1254 request.count = BUFFER_COUNT;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1255 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1256
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1257 request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1258 request.memory = V4L2_MEMORY_MMAP;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1259 if (ioctl(priv->video_fd, VIDIOC_REQBUFS, &request) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1260 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl request buffers failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1261 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1262 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1263 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1264
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1265 /* query buffers */
18558
4928dd61f136 Fix potential integer overflows in memory allocation.
rtogni
parents: 18176
diff changeset
1266 if (!(priv->map = calloc(request.count, sizeof(struct map)))) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1267 mp_msg(MSGT_TV, MSGL_ERR, "%s: malloc capture buffers failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1268 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1269 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1270 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1271
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1272 /* map and queue buffers */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1273 for (i = 0; i < request.count; i++) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1274 memset(&priv->map[i].buf,0,sizeof(priv->map[i].buf));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1275 priv->map[i].buf.index = i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1276 priv->map[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1277 priv->map[i].buf.memory = V4L2_MEMORY_MMAP;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1278 if (ioctl(priv->video_fd, VIDIOC_QUERYBUF, &(priv->map[i].buf)) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1279 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1280 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1281 free(priv->map);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1282 priv->map = NULL;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1283 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1284 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1285 priv->map[i].addr = mmap (0, priv->map[i].buf.length, PROT_READ |
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1286 PROT_WRITE, MAP_SHARED, priv->video_fd, priv->map[i].buf.m.offset);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1287 if (priv->map[i].addr == MAP_FAILED) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1288 mp_msg(MSGT_TV, MSGL_ERR, "%s: mmap capture buffer failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1289 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1290 priv->map[i].len = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1291 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1292 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1293 priv->map[i].len = priv->map[i].buf.length;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1294 /* count up to make sure this is correct everytime */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1295 priv->mapcount++;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1296
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1297 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1298 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1299 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1300 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1301 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1302 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1303
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1304 /* start audio thread */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1305 priv->shutdown = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1306 priv->audio_skew_measure_time = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1307 priv->first_frame = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1308 priv->audio_skew = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1309 priv->first = 1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1310
17626
5627625b0cdb Don't test the v4l2_input audioset field for audio capabilities but still try changing the mute setting (patch by Jesse Allen < the3dfxdude _at_ gmail.com >)
aurel
parents: 17199
diff changeset
1311 set_mute(priv, 0);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1312
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1313 return 1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1314 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1315
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1316
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1317 #ifdef HAVE_TV_BSDBT848
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1318 static double grabimmediate_video_frame(priv_t *priv, char *buffer, int len)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1319 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1320 memset(buffer, 0xCC, len);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1321 return(1);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1322 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1323 #endif /* HAVE_TV_BSDBT848 */
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1324
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1325 // copies a video frame
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1326 static inline void copy_frame(priv_t *priv, unsigned char *dest, unsigned char *source)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1327 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1328 int w = priv->format.fmt.pix.width;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1329 int h = priv->format.fmt.pix.height;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1330 int d = pixfmt2depth(priv->format.fmt.pix.pixelformat);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1331 int bytesperline = w*d/8;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1332
17199
9164ef9a1834 remove now useless YV12 plane swap hack, patch by Luc Gallant lucgallant at gmail com
henry
parents: 17012
diff changeset
1333 memcpy(dest, source, bytesperline * h);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1334 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1335
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1336 // maximum skew change, in frames
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1337 #define MAX_SKEW_DELTA 0.6
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1338 static void *video_grabber(void *data)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1339 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1340 priv_t *priv = (priv_t*)data;
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1341 long long skew, prev_skew, xskew, interval, prev_interval, delta;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1342 int i;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1343 int framesize = priv->format.fmt.pix.height*priv->format.fmt.pix.width*
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1344 pixfmt2depth(priv->format.fmt.pix.pixelformat)/8;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1345 fd_set rdset;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1346 struct timeval timeout;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1347 struct v4l2_buffer buf;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1348
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1349 xskew = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1350 skew = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1351 interval = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1352 prev_interval = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1353 prev_skew = 0;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1354
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1355 mp_msg(MSGT_TV, MSGL_V, "%s: going to capture\n", info.short_name);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1356 if (ioctl(priv->video_fd, VIDIOC_STREAMON, &(priv->format.type)) < 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1357 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamon failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1358 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1359 return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1360 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1361 priv->streamon = 1;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1362
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1363 if (!tv_param_noaudio) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1364 pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1365 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1366
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1367 for (priv->frames = 0; !priv->shutdown;)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1368 {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1369 int ret;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1370
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1371 if (priv->immediate_mode) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1372 while (priv->video_cnt == priv->video_buffer_size_max) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1373 usleep(10000);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1374 if (priv->shutdown) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1375 return NULL;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1376 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1377 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1378 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1379
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1380 FD_ZERO (&rdset);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1381 FD_SET (priv->video_fd, &rdset);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1382
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1383 timeout.tv_sec = 1;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1384 timeout.tv_usec = 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1385
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1386 i = select(priv->video_fd + 1, &rdset, NULL, NULL, &timeout);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1387 if (i < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1388 mp_msg(MSGT_TV, MSGL_ERR, "%s: select failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1389 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1390 continue;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1391 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1392 else if (i == 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1393 mp_msg(MSGT_TV, MSGL_ERR, "%s: select timeout\n", info.short_name);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1394 continue;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1395 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1396 else if (!FD_ISSET(priv->video_fd, &rdset)) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1397 continue;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1398 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1399
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1400 memset(&buf,0,sizeof(buf));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1401 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1402 buf.memory = V4L2_MEMORY_MMAP;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1403 ret = ioctl(priv->video_fd, VIDIOC_DQBUF, &buf);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1404
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1405 if (ret < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1406 /*
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1407 if there's no signal, the buffer might me dequeued
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1408 so we query all the buffers to see which one we should
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1409 put back to queue
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1410
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1411 observed with saa7134 0.2.8
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1412 don't know if is it a bug or (mis)feature
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1413 */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1414 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl dequeue buffer failed: %s, idx = %d\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1415 info.short_name, strerror(errno), buf.index);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1416 for (i = 0; i < priv->mapcount; i++) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1417 memset(&buf,0,sizeof(buf));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1418 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1419 buf.memory = V4L2_MEMORY_MMAP;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1420 buf.index = i;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1421 ret = ioctl(priv->video_fd, VIDIOC_QUERYBUF, &buf);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1422 if (ret < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1423 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s, idx = %d\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1424 info.short_name, strerror(errno), buf.index);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1425 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1426 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1427 if ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE)) == V4L2_BUF_FLAG_MAPPED) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1428 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1429 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1430 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1431 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1432 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1433 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1434 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1435 continue;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1436 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1437
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1438 /* store the timestamp of the very first frame as reference */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1439 if (!priv->frames++) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1440 if (!tv_param_noaudio) pthread_mutex_lock(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1441 priv->first_frame = (long long)1e6*buf.timestamp.tv_sec + buf.timestamp.tv_usec;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1442 if (!tv_param_noaudio) pthread_mutex_unlock(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1443 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1444 priv->curr_frame = (long long)buf.timestamp.tv_sec*1e6+buf.timestamp.tv_usec;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1445 // fprintf(stderr, "idx = %d, ts = %lf\n", buf.index, (double)(priv->curr_frame) / 1e6);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1446
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1447 interval = priv->curr_frame - priv->first_frame;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1448 delta = interval - prev_interval;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1449
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1450 if (!priv->immediate_mode) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1451 // interpolate the skew in time
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1452 if (!tv_param_noaudio) pthread_mutex_lock(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1453 xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1454 if (!tv_param_noaudio) pthread_mutex_unlock(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1455 // correct extreme skew changes to avoid (especially) moving backwards in time
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1456 if (xskew - prev_skew > delta*MAX_SKEW_DELTA) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1457 skew = prev_skew + delta*MAX_SKEW_DELTA;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1458 } else if (xskew - prev_skew < -delta*MAX_SKEW_DELTA) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1459 skew = prev_skew - delta*MAX_SKEW_DELTA;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1460 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1461 skew = xskew;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1462 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1463 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1464
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1465 mp_msg(MSGT_TV, MSGL_DBG3, "\nfps = %lf, interval = %lf, a_skew = %f, corr_skew = %f\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1466 delta ? (double)1e6/delta : -1,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1467 (double)1e-6*interval, (double)1e-6*xskew, (double)1e-6*skew);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1468 mp_msg(MSGT_TV, MSGL_DBG3, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1469
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1470 prev_skew = skew;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1471 prev_interval = interval;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1472
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1473 /* allocate a new buffer, if needed */
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1474 pthread_mutex_lock(&priv->video_buffer_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1475 if (priv->video_buffer_size_current < priv->video_buffer_size_max) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1476 if (priv->video_cnt == priv->video_buffer_size_current) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1477 unsigned char *newbuf = malloc(framesize);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1478 if (newbuf) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1479 memmove(priv->video_ringbuffer+priv->video_tail+1, priv->video_ringbuffer+priv->video_tail,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1480 (priv->video_buffer_size_current-priv->video_tail)*sizeof(unsigned char *));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1481 memmove(priv->video_timebuffer+priv->video_tail+1, priv->video_timebuffer+priv->video_tail,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1482 (priv->video_buffer_size_current-priv->video_tail)*sizeof(long long));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1483 priv->video_ringbuffer[priv->video_tail] = newbuf;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1484 if ((priv->video_head >= priv->video_tail) && (priv->video_cnt > 0)) priv->video_head++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1485 priv->video_buffer_size_current++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1486 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1487 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1488 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1489 pthread_mutex_unlock(&priv->video_buffer_mutex);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1490
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1491 if (priv->video_cnt == priv->video_buffer_size_current) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1492 if (!priv->immediate_mode) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1493 mp_msg(MSGT_TV, MSGL_ERR, "\nvideo buffer full - dropping frame\n");
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1494 if (priv->audio_insert_null_samples) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1495 pthread_mutex_lock(&priv->audio_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1496 priv->dropped_frames_timeshift += delta;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1497 pthread_mutex_unlock(&priv->audio_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1498 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1499 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1500 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1501 if (priv->immediate_mode) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1502 priv->video_timebuffer[priv->video_tail] = 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1503 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1504 // compensate for audio skew
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1505 // negative skew => there are more audio samples, increase interval
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1506 // positive skew => less samples, shorten the interval
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1507 priv->video_timebuffer[priv->video_tail] = interval - skew;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1508 if (priv->audio_insert_null_samples && priv->video_timebuffer[priv->video_tail] > 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1509 pthread_mutex_lock(&priv->audio_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1510 priv->video_timebuffer[priv->video_tail] +=
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1511 (priv->audio_null_blocks_inserted
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1512 - priv->dropped_frames_timeshift/priv->audio_usecs_per_block)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1513 *priv->audio_usecs_per_block;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1514 pthread_mutex_unlock(&priv->audio_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1515 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1516 }
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1517
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1518 copy_frame(priv, priv->video_ringbuffer[priv->video_tail], priv->map[buf.index].addr);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1519 priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size_current;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1520 priv->video_cnt++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1521 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1522 if (ioctl(priv->video_fd, VIDIOC_QBUF, &buf) < 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1523 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n",
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1524 info.short_name, strerror(errno));
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1525 return 0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1526 }
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1527 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1528 return NULL;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1529 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1530
16962
bdc218b5a49a Do not hang forever when the card delivers no new data.
reimar
parents: 16536
diff changeset
1531 #define MAX_LOOP 50
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1532 static double grab_video_frame(priv_t *priv, char *buffer, int len)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1533 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1534 double interval;
16962
bdc218b5a49a Do not hang forever when the card delivers no new data.
reimar
parents: 16536
diff changeset
1535 int loop_cnt = 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1537 if (priv->first) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1538 pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1539 priv->first = 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1540 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1541
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1542 while (priv->video_cnt == 0) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1543 usleep(10000);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1544 if (loop_cnt++ > MAX_LOOP) return 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1545 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1546
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1547 pthread_mutex_lock(&priv->video_buffer_mutex);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1548 interval = (double)priv->video_timebuffer[priv->video_head]*1e-6;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1549 memcpy(buffer, priv->video_ringbuffer[priv->video_head], len);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1550 priv->video_cnt--;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1551 priv->video_head = (priv->video_head+1)%priv->video_buffer_size_current;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1552 pthread_mutex_unlock(&priv->video_buffer_mutex);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1553
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1554 return interval;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1555 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1556
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1557 static int get_video_framesize(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1558 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1559 return priv->format.fmt.pix.sizeimage;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1560 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1561
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1562 //#define DOUBLESPEED
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1563 #ifdef DOUBLESPEED
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1564 // for testing purposes only
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1565 static void read_doublespeed(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1566 {
18885
5c8acc972551 rm unnecesary casts from void* - part 4
reynaldo
parents: 18589
diff changeset
1567 char *bufx = calloc(priv->audio_in.blocksize, 2);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1568 short *s;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1569 short *d;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1570 int i;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1571
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1572 audio_in_read_chunk(&priv->audio_in, bufx);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1573 audio_in_read_chunk(&priv->audio_in, bufx+priv->audio_in.blocksize);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1574
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1575 s = bufx;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1576 d = priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1577 for (i = 0; i < priv->audio_in.blocksize/2; i++) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1578 *d++ = *s++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1579 *s++;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1580 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1581
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1582 }
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1583 #endif
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1584
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1585 static void *audio_grabber(void *data)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1586 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1587 priv_t *priv = (priv_t*)data;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1588 struct timeval tv;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1589 int i, audio_skew_ptr = 0;
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
1590 long long current_time, prev_skew = 0, prev_skew_uncorr = 0;
10852
henry
parents: 10851
diff changeset
1591 long long start_time_avg;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1592
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1593 gettimeofday(&tv, NULL);
10852
henry
parents: 10851
diff changeset
1594 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1595 audio_in_start_capture(&priv->audio_in);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1596 for (i = 0; i < priv->aud_skew_cnt; i++)
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1597 priv->audio_skew_buffer[i] = 0;
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
1598 for (i = 0; i < priv->aud_skew_cnt; i++)
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1599 priv->audio_skew_delta_buffer[i] = 0;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1600
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1601 for (; !priv->shutdown;)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1602 {
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1603 #ifdef DOUBLESPEED
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1604 read_doublespeed(priv);
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1605 #else
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1606 if (audio_in_read_chunk(&priv->audio_in, priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize) < 0)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1607 continue;
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1608 #endif
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1609 pthread_mutex_lock(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1610 if (priv->first_frame == 0) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1611 // there is no first frame yet (unlikely to happen)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1612 gettimeofday(&tv, NULL);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1613 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1614 // fprintf(stderr, "warning - first frame not yet available!\n");
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1615 pthread_mutex_unlock(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1616 continue;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1617 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1618 pthread_mutex_unlock(&priv->skew_mutex);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1619
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1620 gettimeofday(&tv, NULL);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1621
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1622 priv->audio_recv_blocks_total++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1623 current_time = (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_start_time;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1624
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1625 if (priv->audio_recv_blocks_total < priv->aud_skew_cnt*2) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1626 start_time_avg += (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_usecs_per_block*priv->audio_recv_blocks_total;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1627 priv->audio_start_time = start_time_avg/(priv->audio_recv_blocks_total+1);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1628 }
10852
henry
parents: 10851
diff changeset
1629
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1630 // fprintf(stderr, "spb = %lf, bs = %d, skew = %lf\n", priv->audio_secs_per_block, priv->audio_in.blocksize,
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1631 // (double)(current_time - 1e6*priv->audio_secs_per_block*priv->audio_recv_blocks_total)/1e6);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1632
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1633 // put the current skew into the ring buffer
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1634 priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr];
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1635 priv->audio_skew_buffer[audio_skew_ptr] = current_time
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1636 - priv->audio_usecs_per_block*priv->audio_recv_blocks_total;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1637 priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr];
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1638
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1639 pthread_mutex_lock(&priv->skew_mutex);
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1640
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1641 // skew calculation
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1642
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1643 // compute the sliding average of the skews
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1644 if (priv->audio_recv_blocks_total > priv->aud_skew_cnt) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1645 priv->audio_skew = priv->audio_skew_total/priv->aud_skew_cnt;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1646 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1647 priv->audio_skew = priv->audio_skew_total/priv->audio_recv_blocks_total;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1648 }
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
1649
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1650 // put the current skew change (skew-prev_skew) into the ring buffer
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1651 priv->audio_skew_delta_total -= priv->audio_skew_delta_buffer[audio_skew_ptr];
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1652 priv->audio_skew_delta_buffer[audio_skew_ptr] = priv->audio_skew - prev_skew_uncorr;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1653 priv->audio_skew_delta_total += priv->audio_skew_delta_buffer[audio_skew_ptr];
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1654 prev_skew_uncorr = priv->audio_skew; // remember the _uncorrected_ average value
10704
1d991eb5cf02 a/v sync cleanups
henry
parents: 10653
diff changeset
1655
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1656 audio_skew_ptr = (audio_skew_ptr+1) % priv->aud_skew_cnt; // rotate the buffer pointer
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
1657
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1658 // sliding average approximates the value in the middle of the interval
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1659 // so interpolate the skew value further to the current time
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1660 priv->audio_skew += priv->audio_skew_delta_total/2;
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
1661
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1662 // now finally, priv->audio_skew contains fairly good approximation
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1663 // of the current value
10653
d9dfbe5e1011 a/v sync improvements
henry
parents: 10538
diff changeset
1664
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1665 // current skew factor (assuming linearity)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1666 // used for further interpolation in video_grabber
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1667 // probably overkill but seems to be necessary for
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1668 // stress testing by dropping half of the audio frames ;)
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1669 // especially when using ALSA with large block sizes
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1670 // where audio_skew remains a long while behind
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1671 if ((priv->audio_skew_measure_time != 0) && (current_time - priv->audio_skew_measure_time != 0)) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1672 priv->audio_skew_factor = (double)(priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1673 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1674 priv->audio_skew_factor = 0.0;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1675 }
10852
henry
parents: 10851
diff changeset
1676
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1677 priv->audio_skew_measure_time = current_time;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1678 prev_skew = priv->audio_skew;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1679 priv->audio_skew += priv->audio_start_time - priv->first_frame;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1680 pthread_mutex_unlock(&priv->skew_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1681
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1682 // fprintf(stderr, "audio_skew = %lf, delta = %lf\n", (double)priv->audio_skew/1e6, (double)priv->audio_skew_delta_total/1e6);
10851
41bad7ce4cc9 yet another sync fix
henry
parents: 10810
diff changeset
1683
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1684 pthread_mutex_lock(&priv->audio_mutex);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1685 if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1686 mp_msg(MSGT_TV, MSGL_ERR, "\ntoo bad - dropping audio frame !\n");
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1687 priv->audio_drop++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1688 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1689 priv->audio_tail = (priv->audio_tail+1) % priv->audio_buffer_size;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1690 priv->audio_cnt++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1691 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1692 pthread_mutex_unlock(&priv->audio_mutex);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1693 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1694 return NULL;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1695 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1696
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1697 static double grab_audio_frame(priv_t *priv, char *buffer, int len)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1698 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1699 mp_dbg(MSGT_TV, MSGL_DBG2, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n",
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1700 priv, buffer, len);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1701
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1702 // hack: if grab_audio_frame is called first, it means we are used by mplayer
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1703 // => switch to the mode which outputs audio immediately, even if
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1704 // it should be silence
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1705 if (priv->first) priv->audio_insert_null_samples = 1;
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1706
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1707 pthread_mutex_lock(&priv->audio_mutex);
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1708 while (priv->audio_insert_null_samples
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1709 && priv->dropped_frames_timeshift - priv->dropped_frames_compensated >= priv->audio_usecs_per_block) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1710 // some frames were dropped - drop the corresponding number of audio blocks
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1711 if (priv->audio_drop) {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1712 priv->audio_drop--;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1713 } else {
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1714 if (priv->audio_head == priv->audio_tail) break;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1715 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1716 }
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1717 priv->dropped_frames_compensated += priv->audio_usecs_per_block;
10776
80402283a017 Fix immediatemode with mplayer (ie playing both sound and video)
albeu
parents: 10735
diff changeset
1718 }
80402283a017 Fix immediatemode with mplayer (ie playing both sound and video)
albeu
parents: 10735
diff changeset
1719
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1720 // compensate for dropped audio frames
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1721 if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1722 priv->audio_drop--;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1723 memset(buffer, 0, len);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1724 goto out;
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1725 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1726
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1727 if (priv->audio_insert_null_samples && (priv->audio_head == priv->audio_tail)) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1728 // return silence to avoid desync and stuttering
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1729 memset(buffer, 0, len);
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1730 priv->audio_null_blocks_inserted++;
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1731 goto out;
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1732 }
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1733
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1734 pthread_mutex_unlock(&priv->audio_mutex);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1735 while (priv->audio_head == priv->audio_tail) {
23151
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1736 // this is mencoder => just wait until some audio is available
d2c6e0c71550 (cosmetics) replace tabs with spaces
voroshil
parents: 22381
diff changeset
1737 usleep(10000);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1738 }
15451
bad4378c5456 10l forgotten comment
henry
parents: 15449
diff changeset
1739 pthread_mutex_lock(&priv->audio_mutex);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1740 memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_in.blocksize, len);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1741 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1742 priv->audio_cnt--;
15449
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1743 out:
a7966365a197 improve playback with mplayer -tv immediatemode=0
henry
parents: 15418
diff changeset
1744 pthread_mutex_unlock(&priv->audio_mutex);
10536
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1745 priv->audio_sent_blocks_total++;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1746 return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block;
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1747 }
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1748
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1749 static int get_audio_framesize(priv_t *priv)
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1750 {
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1751 return(priv->audio_in.blocksize);
527e978cb0f8 v4l2 support
henry
parents:
diff changeset
1752 }