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