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