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