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