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