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