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