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