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