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