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