Mercurial > mplayer.hg
annotate stream/tvi_v4l2.c @ 24533:43ff93c329d7
r24550: msglevel 5 is the default.
r24551: Clarify description of MPLAYER_VERBOSE.
author | voroshil |
---|---|
date | Mon, 17 Sep 2007 10:22:53 +0000 |
parents | 9fb68e85180a |
children | d6bba2781d01 |
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; | |
24105
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
867 case TVI_CONTROL_TUN_GET_SIGNAL: |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
868 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
869 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n", |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
870 info.short_name, strerror(errno)); |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
871 return TVI_CONTROL_FALSE; |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
872 } |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
873 *(int*)arg=100*(priv->tuner.signal>>8)/255; |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
874 return TVI_CONTROL_TRUE; |
10536 | 875 case TVI_CONTROL_TUN_SET_NORM: |
23151 | 876 priv->standard.index = *(int *)arg; |
877 if (ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) { | |
878 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum norm failed: %s\n", | |
879 info.short_name, strerror(errno)); | |
880 return TVI_CONTROL_FALSE; | |
881 } | |
882 mp_msg(MSGT_TV, MSGL_V, "%s: set norm: %s\n", info.short_name, priv->standard.name); | |
883 if (ioctl(priv->video_fd, VIDIOC_S_STD, &priv->standard.id) < 0) { | |
884 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set norm failed: %s\n", | |
885 info.short_name, strerror(errno)); | |
886 return TVI_CONTROL_FALSE; | |
887 } | |
888 return TVI_CONTROL_TRUE; | |
13978 | 889 case TVI_CONTROL_SPC_GET_NORMID: |
23151 | 890 { |
891 int i; | |
892 for (i = 0;; i++) { | |
893 struct v4l2_standard standard; | |
894 memset(&standard, 0, sizeof(standard)); | |
895 standard.index = i; | |
896 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard)) | |
897 return TVI_CONTROL_FALSE; | |
898 if (!strcasecmp(standard.name, (char *)arg)) { | |
899 *(int *)arg = i; | |
900 return TVI_CONTROL_TRUE; | |
901 } | |
902 } | |
903 return TVI_CONTROL_FALSE; | |
904 } | |
10536 | 905 case TVI_CONTROL_SPC_GET_INPUT: |
23151 | 906 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, (int *)arg) < 0) { |
907 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n", | |
908 info.short_name, strerror(errno)); | |
909 return TVI_CONTROL_FALSE; | |
910 } | |
911 return TVI_CONTROL_TRUE; | |
10536 | 912 case TVI_CONTROL_SPC_SET_INPUT: |
23151 | 913 mp_msg(MSGT_TV, MSGL_V, "%s: set input: %d\n", info.short_name, *(int *)arg); |
914 priv->input.index = *(int *)arg; | |
915 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &priv->input) < 0) { | |
916 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum input failed: %s\n", | |
917 info.short_name, strerror(errno)); | |
918 return TVI_CONTROL_FALSE; | |
919 } | |
920 if (ioctl(priv->video_fd, VIDIOC_S_INPUT, (int *)arg) < 0) { | |
921 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set input failed: %s\n", | |
922 info.short_name, strerror(errno)); | |
923 return TVI_CONTROL_FALSE; | |
924 } | |
925 return TVI_CONTROL_TRUE; | |
10536 | 926 case TVI_CONTROL_AUD_GET_FORMAT: |
23151 | 927 init_audio(priv); |
928 if (!priv->audio_inited) return TVI_CONTROL_FALSE; | |
929 *(int *)arg = AF_FORMAT_S16_LE; | |
930 mp_msg(MSGT_TV, MSGL_V, "%s: get audio format: %d\n", | |
931 info.short_name, *(int *)arg); | |
932 return TVI_CONTROL_TRUE; | |
10536 | 933 case TVI_CONTROL_AUD_GET_SAMPLERATE: |
23151 | 934 init_audio(priv); |
935 if (!priv->audio_inited) return TVI_CONTROL_FALSE; | |
936 *(int *)arg = priv->audio_in.samplerate; | |
937 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplerate: %d\n", | |
938 info.short_name, *(int *)arg); | |
939 return TVI_CONTROL_TRUE; | |
10536 | 940 case TVI_CONTROL_AUD_GET_SAMPLESIZE: |
23151 | 941 init_audio(priv); |
942 if (!priv->audio_inited) return TVI_CONTROL_FALSE; | |
943 *(int *)arg = priv->audio_in.bytes_per_sample; | |
944 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplesize: %d\n", | |
945 info.short_name, *(int *)arg); | |
946 return TVI_CONTROL_TRUE; | |
10536 | 947 case TVI_CONTROL_AUD_GET_CHANNELS: |
23151 | 948 init_audio(priv); |
949 if (!priv->audio_inited) return TVI_CONTROL_FALSE; | |
950 *(int *)arg = priv->audio_in.channels; | |
951 mp_msg(MSGT_TV, MSGL_V, "%s: get audio channels: %d\n", | |
952 info.short_name, *(int *)arg); | |
953 return TVI_CONTROL_TRUE; | |
10536 | 954 case TVI_CONTROL_AUD_SET_SAMPLERATE: |
23151 | 955 init_audio(priv); |
956 mp_msg(MSGT_TV, MSGL_V, "%s: set audio samplerate: %d\n", | |
957 info.short_name, *(int *)arg); | |
958 if (audio_in_set_samplerate(&priv->audio_in, *(int*)arg) < 0) return TVI_CONTROL_FALSE; | |
959 // setup_audio_buffer_sizes(priv); | |
960 return TVI_CONTROL_TRUE; | |
23901 | 961 #ifdef HAVE_TV_TELETEXT |
962 case TVI_CONTROL_VBI_INIT: | |
963 { | |
964 void* ptr; | |
965 tt_stream_props tsp; | |
966 | |
967 if (vbi_init(priv,*(char**)arg)!=TVI_CONTROL_TRUE) | |
968 return TVI_CONTROL_FALSE; | |
969 if(vbi_get_props(priv,&tsp)==TVI_CONTROL_TRUE) | |
970 { | |
971 ptr=&tsp; | |
972 if(teletext_control(NULL,TV_VBI_CONTROL_START,&ptr)==TVI_CONTROL_TRUE) | |
973 priv->priv_vbi=ptr; | |
974 else | |
975 priv->priv_vbi=NULL; | |
976 } | |
977 return TVI_CONTROL_TRUE; | |
978 } | |
979 default: | |
980 return teletext_control(priv->priv_vbi,cmd,arg); | |
981 #endif | |
10536 | 982 } |
983 mp_msg(MSGT_TV, MSGL_V, "%s: unknown control: %d\n", info.short_name, cmd); | |
984 return(TVI_CONTROL_UNKNOWN); | |
985 } | |
986 | |
987 | |
988 #define PRIV ((priv_t *) (tvi_handle->priv)) | |
989 | |
990 /* handler creator - entry point ! */ | |
23883 | 991 static tvi_handle_t *tvi_init_v4l2(tv_param_t* tv_param) |
10536 | 992 { |
993 tvi_handle_t *tvi_handle; | |
994 | |
995 /* new_handle initializes priv with memset 0 */ | |
996 tvi_handle = new_handle(); | |
997 if (!tvi_handle) { | |
23151 | 998 return NULL; |
10536 | 999 } |
1000 PRIV->video_fd = -1; | |
1001 | |
23883 | 1002 PRIV->video_dev = strdup(tv_param->device? tv_param->device: "/dev/video0"); |
10536 | 1003 if (!PRIV->video_dev) { |
23151 | 1004 free_handle(tvi_handle); |
1005 return NULL; | |
10536 | 1006 } |
1007 | |
23883 | 1008 if (tv_param->adevice) { |
1009 PRIV->audio_dev = strdup(tv_param->adevice); | |
23151 | 1010 if (!PRIV->audio_dev) { |
1011 free(PRIV->video_dev); | |
1012 free_handle(tvi_handle); | |
1013 return NULL; | |
1014 } | |
10536 | 1015 } |
1016 | |
23883 | 1017 PRIV->tv_param=tv_param; |
10536 | 1018 return tvi_handle; |
1019 } | |
1020 | |
1021 #undef PRIV | |
1022 | |
1023 | |
1024 static int uninit(priv_t *priv) | |
1025 { | |
1026 int i, frames, dropped = 0; | |
1027 | |
23901 | 1028 #ifdef HAVE_TV_TELETEXT |
1029 priv->vbi_shutdown=1; | |
1030 if(priv->vbi_grabber_thread) | |
1031 pthread_join(priv->vbi_grabber_thread, NULL); | |
1032 | |
1033 teletext_control(priv->priv_vbi,TV_VBI_CONTROL_STOP,(void*)1); | |
1034 priv->priv_vbi=NULL; | |
1035 | |
1036 if(priv->vbi_fd){ | |
1037 close(priv->vbi_fd); | |
1038 priv->vbi_fd=0; | |
1039 } | |
1040 | |
1041 if(priv->vbi_dev){ | |
1042 free(priv->vbi_dev); | |
1043 priv->vbi_dev=0; | |
1044 } | |
1045 | |
1046 #endif | |
1047 | |
10536 | 1048 priv->shutdown = 1; |
16185 | 1049 if(priv->video_grabber_thread) |
23151 | 1050 pthread_join(priv->video_grabber_thread, NULL); |
10536 | 1051 pthread_mutex_destroy(&priv->video_buffer_mutex); |
1052 | |
1053 if (priv->streamon) { | |
23151 | 1054 struct v4l2_buffer buf; |
10536 | 1055 |
23151 | 1056 /* get performance */ |
1057 frames = 1 + (priv->curr_frame - priv->first_frame + | |
1058 priv->standard.frameperiod.numerator * 500000 / | |
1059 priv->standard.frameperiod.denominator) * | |
1060 priv->standard.frameperiod.denominator / | |
1061 priv->standard.frameperiod.numerator / 1000000; | |
1062 dropped = frames - priv->frames; | |
10536 | 1063 |
23151 | 1064 /* turn off streaming */ |
1065 if (ioctl(priv->video_fd, VIDIOC_STREAMOFF, &(priv->map[0].buf.type)) < 0) { | |
1066 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamoff failed: %s\n", | |
1067 info.short_name, strerror(errno)); | |
1068 } | |
1069 priv->streamon = 0; | |
10536 | 1070 |
23151 | 1071 /* unqueue all remaining buffers */ |
1072 memset(&buf,0,sizeof(buf)); | |
1073 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1074 buf.memory = V4L2_MEMORY_MMAP; | |
1075 while (!ioctl(priv->video_fd, VIDIOC_DQBUF, &buf)); | |
10536 | 1076 } |
1077 | |
1078 /* unmap all buffers */ | |
1079 for (i = 0; i < priv->mapcount; i++) { | |
23151 | 1080 if (munmap(priv->map[i].addr, priv->map[i].len) < 0) { |
1081 mp_msg(MSGT_TV, MSGL_ERR, "%s: munmap capture buffer failed: %s\n", | |
1082 info.short_name, strerror(errno)); | |
1083 } | |
10536 | 1084 } |
1085 | |
1086 /* stop audio thread */ | |
23886 | 1087 if (!priv->tv_param->noaudio && priv->audio_grabber_thread) { |
23151 | 1088 pthread_join(priv->audio_grabber_thread, NULL); |
1089 pthread_mutex_destroy(&priv->skew_mutex); | |
1090 pthread_mutex_destroy(&priv->audio_mutex); | |
10536 | 1091 } |
1092 | |
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
|
1093 set_mute(priv, 1); |
10536 | 1094 |
1095 /* free memory and close device */ | |
23151 | 1096 free(priv->map); priv->map = NULL; |
10536 | 1097 priv->mapcount = 0; |
23151 | 1098 if(priv->video_fd!=-1)close(priv->video_fd); priv->video_fd = -1; |
1099 free(priv->video_dev); priv->video_dev = NULL; | |
10536 | 1100 |
1101 if (priv->video_ringbuffer) { | |
23151 | 1102 int i; |
1103 for (i = 0; i < priv->video_buffer_size_current; i++) { | |
23423 | 1104 free(priv->video_ringbuffer[i].data); |
23151 | 1105 } |
1106 free(priv->video_ringbuffer); | |
10536 | 1107 } |
23886 | 1108 if (!priv->tv_param->noaudio) { |
23151 | 1109 if (priv->audio_ringbuffer) |
1110 free(priv->audio_ringbuffer); | |
1111 if (priv->audio_skew_buffer) | |
1112 free(priv->audio_skew_buffer); | |
1113 if (priv->audio_skew_delta_buffer) | |
1114 free(priv->audio_skew_delta_buffer); | |
10536 | 1115 } |
1116 | |
1117 /* show some nice statistics ;-) */ | |
1118 mp_msg(MSGT_TV, MSGL_INFO, | |
23151 | 1119 "%s: %d frames successfully processed, %d frames dropped.\n", |
1120 info.short_name, priv->frames, dropped); | |
10536 | 1121 mp_msg(MSGT_TV, MSGL_V, "%s: up to %u video frames buffered.\n", |
23151 | 1122 info.short_name, priv->video_buffer_size_current); |
10536 | 1123 return 1; |
1124 } | |
1125 | |
1126 | |
1127 /* initialisation */ | |
1128 static int init(priv_t *priv) | |
1129 { | |
1130 int i; | |
1131 | |
1132 priv->audio_ringbuffer = NULL; | |
1133 priv->audio_skew_buffer = NULL; | |
10653 | 1134 priv->audio_skew_delta_buffer = NULL; |
10536 | 1135 |
15464 | 1136 priv->audio_inited = 0; |
1137 | |
10536 | 1138 /* Open the video device. */ |
1139 priv->video_fd = open(priv->video_dev, O_RDWR); | |
1140 if (priv->video_fd < 0) { | |
23151 | 1141 mp_msg(MSGT_TV, MSGL_ERR, "%s: unable to open '%s': %s\n", |
1142 info.short_name, priv->video_dev, strerror(errno)); | |
1143 uninit(priv); | |
1144 return 0; | |
10536 | 1145 } |
1146 mp_msg(MSGT_TV, MSGL_DBG2, "%s: video fd: %s: %d\n", | |
23151 | 1147 info.short_name, priv->video_dev, priv->video_fd); |
10536 | 1148 |
1149 /* | |
1150 ** Query the video capabilities and current settings | |
1151 ** for further control calls. | |
1152 */ | |
1153 if (ioctl(priv->video_fd, VIDIOC_QUERYCAP, &priv->capability) < 0) { | |
23151 | 1154 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query capabilities failed: %s\n", |
1155 info.short_name, strerror(errno)); | |
1156 uninit(priv); | |
1157 return 0; | |
10536 | 1158 } |
1159 | |
1160 if (!(priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE)) | |
1161 { | |
23151 | 1162 mp_msg(MSGT_TV, MSGL_ERR, "Device %s is not a video capture device.\n", |
1163 priv->video_dev); | |
1164 return 0; | |
10536 | 1165 } |
1166 | |
18073
8ceb31f028ee
make failures during e.g. setting the TV norm non-fatal.
reimar
parents:
17765
diff
changeset
|
1167 if (getfmt(priv) < 0) { |
23151 | 1168 uninit(priv); |
1169 return 0; | |
10536 | 1170 } |
18073
8ceb31f028ee
make failures during e.g. setting the TV norm non-fatal.
reimar
parents:
17765
diff
changeset
|
1171 getstd(priv); |
10536 | 1172 /* |
1173 ** if this device has got a tuner query it's settings | |
1174 ** otherwise set some nice defaults | |
1175 */ | |
1176 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1177 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { |
1178 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n", | |
1179 info.short_name, strerror(errno)); | |
1180 uninit(priv); | |
1181 return 0; | |
1182 } | |
10536 | 1183 } |
1184 mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.card); | |
1185 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1186 mp_msg(MSGT_TV, MSGL_INFO, " Tuner cap:%s%s%s\n", |
1187 (priv->tuner.capability & V4L2_TUNER_CAP_STEREO) ? " STEREO" : "", | |
1188 (priv->tuner.capability & V4L2_TUNER_CAP_LANG1) ? " LANG1" : "", | |
1189 (priv->tuner.capability & V4L2_TUNER_CAP_LANG2) ? " LANG2" : ""); | |
1190 mp_msg(MSGT_TV, MSGL_INFO, " Tuner rxs:%s%s%s%s\n", | |
1191 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_MONO) ? " MONO" : "", | |
1192 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_STEREO) ? " STEREO" : "", | |
1193 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG1) ? " LANG1" : "", | |
1194 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG2) ? " LANG2" : ""); | |
10536 | 1195 } |
1196 mp_msg(MSGT_TV, MSGL_INFO, " Capabilites:%s%s%s%s%s%s%s%s%s%s%s\n", | |
23151 | 1197 priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE? |
1198 " video capture": "", | |
1199 priv->capability.capabilities & V4L2_CAP_VIDEO_OUTPUT? | |
1200 " video output": "", | |
1201 priv->capability.capabilities & V4L2_CAP_VIDEO_OVERLAY? | |
1202 " video overlay": "", | |
1203 priv->capability.capabilities & V4L2_CAP_VBI_CAPTURE? | |
1204 " VBI capture device": "", | |
1205 priv->capability.capabilities & V4L2_CAP_VBI_OUTPUT? | |
1206 " VBI output": "", | |
1207 priv->capability.capabilities & V4L2_CAP_RDS_CAPTURE? | |
1208 " RDS data capture": "", | |
1209 priv->capability.capabilities & V4L2_CAP_TUNER? | |
1210 " tuner": "", | |
1211 priv->capability.capabilities & V4L2_CAP_AUDIO? | |
1212 " audio": "", | |
1213 priv->capability.capabilities & V4L2_CAP_READWRITE? | |
1214 " read/write": "", | |
1215 priv->capability.capabilities & V4L2_CAP_ASYNCIO? | |
1216 " async i/o": "", | |
1217 priv->capability.capabilities & V4L2_CAP_STREAMING? | |
1218 " streaming": ""); | |
10536 | 1219 mp_msg(MSGT_TV, MSGL_INFO, " supported norms:"); |
1220 for (i = 0;; i++) { | |
23151 | 1221 struct v4l2_standard standard; |
1222 memset(&standard, 0, sizeof(standard)); | |
1223 standard.index = i; | |
1224 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard)) | |
1225 break; | |
1226 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, standard.name); | |
10536 | 1227 } |
1228 mp_msg(MSGT_TV, MSGL_INFO, "\n inputs:"); | |
1229 for (i = 0; 1; i++) { | |
23151 | 1230 struct v4l2_input input; |
10536 | 1231 |
23151 | 1232 input.index = i; |
1233 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &input) < 0) { | |
1234 break; | |
1235 } | |
1236 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, input.name); | |
10536 | 1237 } |
1238 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, &i) < 0) { | |
23151 | 1239 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n", |
1240 info.short_name, strerror(errno)); | |
10536 | 1241 } |
1242 mp_msg(MSGT_TV, MSGL_INFO, "\n Current input: %d\n", i); | |
1243 for (i = 0; ; i++) { | |
23151 | 1244 struct v4l2_fmtdesc fmtdesc; |
10536 | 1245 |
23151 | 1246 fmtdesc.index = i; |
1247 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1248 if (ioctl(priv->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) { | |
1249 break; | |
1250 } | |
1251 mp_msg(MSGT_TV, MSGL_V, " Format %-6s (%2d bits, %s): %s\n", | |
1252 pixfmt2name(fmtdesc.pixelformat), pixfmt2depth(fmtdesc.pixelformat), | |
1253 fmtdesc.description, vo_format_name(fcc_vl2mp(fmtdesc.pixelformat))); | |
10536 | 1254 } |
1255 mp_msg(MSGT_TV, MSGL_INFO, " Current format: %s\n", | |
23151 | 1256 pixfmt2name(priv->format.fmt.pix.pixelformat)); |
10536 | 1257 |
1258 /* set some nice defaults */ | |
1259 if (getfmt(priv) < 0) return 0; | |
1260 priv->format.fmt.pix.width = 640; | |
1261 priv->format.fmt.pix.height = 480; | |
1262 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
23151 | 1263 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set format failed: %s\n", |
1264 info.short_name, strerror(errno)); | |
1265 uninit(priv); | |
1266 return 0; | |
10536 | 1267 } |
1268 | |
23886 | 1269 // if (!(priv->capability.capabilities & V4L2_CAP_AUDIO) && !priv->tv_param->force_audio) priv->tv_param->noaudio = 1; |
10536 | 1270 |
1271 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1272 struct v4l2_control control; |
23886 | 1273 if (priv->tv_param->amode >= 0) { |
23151 | 1274 mp_msg(MSGT_TV, MSGL_V, "%s: setting audio mode\n", info.short_name); |
23886 | 1275 priv->tuner.audmode = amode2v4l(priv->tv_param->amode); |
23151 | 1276 if (ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) { |
1277 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set tuner failed: %s\n", | |
1278 info.short_name, strerror(errno)); | |
1279 return TVI_CONTROL_FALSE; | |
1280 } | |
1281 } | |
1282 mp_msg(MSGT_TV, MSGL_INFO, "%s: current audio mode is :%s%s%s%s\n", info.short_name, | |
1283 (priv->tuner.audmode == V4L2_TUNER_MODE_MONO) ? " MONO" : "", | |
1284 (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) ? " STEREO" : "", | |
1285 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG1) ? " LANG1" : "", | |
1286 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG2) ? " LANG2" : ""); | |
10536 | 1287 |
23886 | 1288 if (priv->tv_param->volume >= 0) { |
23151 | 1289 control.id = V4L2_CID_AUDIO_VOLUME; |
23886 | 1290 control.value = priv->tv_param->volume; |
23151 | 1291 set_control(priv, &control, 0); |
1292 } | |
23886 | 1293 if (priv->tv_param->bass >= 0) { |
23151 | 1294 control.id = V4L2_CID_AUDIO_BASS; |
23886 | 1295 control.value = priv->tv_param->bass; |
23151 | 1296 set_control(priv, &control, 0); |
1297 } | |
23886 | 1298 if (priv->tv_param->treble >= 0) { |
23151 | 1299 control.id = V4L2_CID_AUDIO_TREBLE; |
23886 | 1300 control.value = priv->tv_param->treble; |
23151 | 1301 set_control(priv, &control, 0); |
1302 } | |
23886 | 1303 if (priv->tv_param->balance >= 0) { |
23151 | 1304 control.id = V4L2_CID_AUDIO_BALANCE; |
23886 | 1305 control.value = priv->tv_param->balance; |
23151 | 1306 set_control(priv, &control, 0); |
1307 } | |
10536 | 1308 } |
1309 | |
1310 return 1; | |
1311 } | |
1312 | |
1313 static int get_capture_buffer_size(priv_t *priv) | |
1314 { | |
1315 int bufsize, cnt; | |
1316 | |
23886 | 1317 if (priv->tv_param->buffer_size >= 0) { |
1318 bufsize = priv->tv_param->buffer_size*1024*1024; | |
10536 | 1319 } else { |
1320 #ifdef HAVE_SYS_SYSINFO_H | |
23151 | 1321 struct sysinfo si; |
1322 | |
1323 sysinfo(&si); | |
1324 if (si.totalram<2*1024*1024) { | |
1325 bufsize = 1024*1024; | |
1326 } else { | |
1327 bufsize = si.totalram/2; | |
1328 } | |
10536 | 1329 #else |
23151 | 1330 bufsize = 16*1024*1024; |
10536 | 1331 #endif |
1332 } | |
1333 | |
23423 | 1334 cnt = bufsize/priv->format.fmt.pix.sizeimage; |
10536 | 1335 if (cnt < 2) cnt = 2; |
1336 | |
1337 return cnt; | |
1338 } | |
1339 | |
1340 /* that's the real start, we'got the format parameters (checked with control) */ | |
1341 static int start(priv_t *priv) | |
1342 { | |
1343 struct v4l2_requestbuffers request; | |
24346 | 1344 unsigned int i; |
10536 | 1345 |
1346 /* setup audio parameters */ | |
1347 | |
15464 | 1348 init_audio(priv); |
23886 | 1349 if (!priv->tv_param->noaudio && !priv->audio_inited) return 0; |
15464 | 1350 |
10536 | 1351 /* we need this to size the audio buffer properly */ |
1352 if (priv->immediate_mode) { | |
23151 | 1353 priv->video_buffer_size_max = 2; |
10536 | 1354 } else { |
23151 | 1355 priv->video_buffer_size_max = get_capture_buffer_size(priv); |
10536 | 1356 } |
1357 | |
23886 | 1358 if (!priv->tv_param->noaudio) { |
23151 | 1359 setup_audio_buffer_sizes(priv); |
1360 priv->audio_skew_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1361 if (!priv->audio_skew_buffer) { | |
1362 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1363 return 0; | |
1364 } | |
1365 priv->audio_skew_delta_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1366 if (!priv->audio_skew_delta_buffer) { | |
1367 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1368 return 0; | |
1369 } | |
10536 | 1370 |
23151 | 1371 priv->audio_ringbuffer = calloc(priv->audio_in.blocksize, priv->audio_buffer_size); |
1372 if (!priv->audio_ringbuffer) { | |
1373 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate audio buffer: %s\n", strerror(errno)); | |
1374 return 0; | |
1375 } | |
10536 | 1376 |
23151 | 1377 priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate |
1378 *priv->audio_in.channels | |
1379 *priv->audio_in.bytes_per_sample); | |
1380 priv->audio_usecs_per_block = 1e6*priv->audio_secs_per_block; | |
1381 priv->audio_head = 0; | |
1382 priv->audio_tail = 0; | |
1383 priv->audio_cnt = 0; | |
1384 priv->audio_drop = 0; | |
1385 priv->audio_skew = 0; | |
1386 priv->audio_skew_total = 0; | |
1387 priv->audio_skew_delta_total = 0; | |
1388 priv->audio_recv_blocks_total = 0; | |
1389 priv->audio_sent_blocks_total = 0; | |
1390 priv->audio_null_blocks_inserted = 0; | |
1391 priv->audio_insert_null_samples = 0; | |
1392 priv->dropped_frames_timeshift = 0; | |
1393 priv->dropped_frames_compensated = 0; | |
15449 | 1394 |
23151 | 1395 pthread_mutex_init(&priv->skew_mutex, NULL); |
1396 pthread_mutex_init(&priv->audio_mutex, NULL); | |
10536 | 1397 } |
1398 | |
1399 /* setup video parameters */ | |
23886 | 1400 if (!priv->tv_param->noaudio) { |
23151 | 1401 if (priv->video_buffer_size_max < (3*priv->standard.frameperiod.denominator) / |
1402 priv->standard.frameperiod.numerator | |
1403 *priv->audio_secs_per_block) { | |
1404 mp_msg(MSGT_TV, MSGL_ERR, "Video buffer shorter than 3 times audio frame duration.\n" | |
1405 "You will probably experience heavy framedrops.\n"); | |
1406 } | |
10536 | 1407 } |
1408 | |
1409 { | |
23151 | 1410 int bytesperline = priv->format.fmt.pix.width*pixfmt2depth(priv->format.fmt.pix.pixelformat)/8; |
1411 | |
1412 mp_msg(MSGT_TV, MSGL_V, "Using a ring buffer for maximum %d frames, %d MB total size.\n", | |
1413 priv->video_buffer_size_max, | |
1414 priv->video_buffer_size_max*priv->format.fmt.pix.height*bytesperline/(1024*1024)); | |
10536 | 1415 } |
1416 | |
23423 | 1417 priv->video_ringbuffer = calloc(priv->video_buffer_size_max, sizeof(video_buffer_entry)); |
10536 | 1418 if (!priv->video_ringbuffer) { |
23151 | 1419 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate video buffer: %s\n", strerror(errno)); |
1420 return 0; | |
10536 | 1421 } |
23423 | 1422 memset(priv->video_ringbuffer,0,priv->video_buffer_size_max * sizeof(video_buffer_entry)); |
10536 | 1423 |
15449 | 1424 pthread_mutex_init(&priv->video_buffer_mutex, NULL); |
1425 | |
10536 | 1426 priv->video_head = 0; |
1427 priv->video_tail = 0; | |
1428 priv->video_cnt = 0; | |
1429 | |
1430 /* request buffers */ | |
1431 if (priv->immediate_mode) { | |
23151 | 1432 request.count = 2; |
10536 | 1433 } else { |
23151 | 1434 request.count = BUFFER_COUNT; |
10536 | 1435 } |
1436 | |
1437 request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1438 request.memory = V4L2_MEMORY_MMAP; | |
1439 if (ioctl(priv->video_fd, VIDIOC_REQBUFS, &request) < 0) { | |
23151 | 1440 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl request buffers failed: %s\n", |
1441 info.short_name, strerror(errno)); | |
1442 return 0; | |
10536 | 1443 } |
1444 | |
1445 /* query buffers */ | |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18176
diff
changeset
|
1446 if (!(priv->map = calloc(request.count, sizeof(struct map)))) { |
23151 | 1447 mp_msg(MSGT_TV, MSGL_ERR, "%s: malloc capture buffers failed: %s\n", |
1448 info.short_name, strerror(errno)); | |
1449 return 0; | |
10536 | 1450 } |
1451 | |
1452 /* map and queue buffers */ | |
1453 for (i = 0; i < request.count; i++) { | |
23151 | 1454 memset(&priv->map[i].buf,0,sizeof(priv->map[i].buf)); |
1455 priv->map[i].buf.index = i; | |
1456 priv->map[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1457 priv->map[i].buf.memory = V4L2_MEMORY_MMAP; | |
1458 if (ioctl(priv->video_fd, VIDIOC_QUERYBUF, &(priv->map[i].buf)) < 0) { | |
1459 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s\n", | |
1460 info.short_name, strerror(errno)); | |
1461 free(priv->map); | |
1462 priv->map = NULL; | |
1463 return 0; | |
1464 } | |
1465 priv->map[i].addr = mmap (0, priv->map[i].buf.length, PROT_READ | | |
1466 PROT_WRITE, MAP_SHARED, priv->video_fd, priv->map[i].buf.m.offset); | |
1467 if (priv->map[i].addr == MAP_FAILED) { | |
1468 mp_msg(MSGT_TV, MSGL_ERR, "%s: mmap capture buffer failed: %s\n", | |
1469 info.short_name, strerror(errno)); | |
1470 priv->map[i].len = 0; | |
1471 return 0; | |
1472 } | |
1473 priv->map[i].len = priv->map[i].buf.length; | |
1474 /* count up to make sure this is correct everytime */ | |
1475 priv->mapcount++; | |
10536 | 1476 |
23151 | 1477 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { |
1478 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1479 info.short_name, strerror(errno)); | |
1480 return 0; | |
1481 } | |
10536 | 1482 } |
1483 | |
23901 | 1484 #ifdef HAVE_TV_TELETEXT |
1485 /* start vbi thread */ | |
1486 if(priv->priv_vbi){ | |
1487 priv->vbi_shutdown = 0; | |
1488 pthread_create(&priv->vbi_grabber_thread, NULL, vbi_grabber, priv); | |
1489 } | |
1490 #endif | |
10536 | 1491 /* start audio thread */ |
1492 priv->shutdown = 0; | |
1493 priv->audio_skew_measure_time = 0; | |
1494 priv->first_frame = 0; | |
1495 priv->audio_skew = 0; | |
1496 priv->first = 1; | |
1497 | |
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
|
1498 set_mute(priv, 0); |
10536 | 1499 |
1500 return 1; | |
1501 } | |
1502 | |
1503 // copies a video frame | |
23423 | 1504 static inline void copy_frame(priv_t *priv, video_buffer_entry *dest, unsigned char *source,int len) |
10536 | 1505 { |
23423 | 1506 dest->framesize=len; |
23886 | 1507 if(priv->tv_param->automute>0){ |
23422 | 1508 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) >= 0) { |
23886 | 1509 if(priv->tv_param->automute<<8>priv->tuner.signal){ |
23423 | 1510 fill_blank_frame(dest->data,dest->framesize,fcc_vl2mp(priv->format.fmt.pix.pixelformat)); |
23422 | 1511 set_mute(priv,1); |
1512 return; | |
1513 } | |
1514 } | |
1515 set_mute(priv,0); | |
1516 } | |
23423 | 1517 memcpy(dest->data, source, len); |
10536 | 1518 } |
1519 | |
1520 // maximum skew change, in frames | |
1521 #define MAX_SKEW_DELTA 0.6 | |
1522 static void *video_grabber(void *data) | |
1523 { | |
1524 priv_t *priv = (priv_t*)data; | |
15449 | 1525 long long skew, prev_skew, xskew, interval, prev_interval, delta; |
10536 | 1526 int i; |
23423 | 1527 int framesize = priv->format.fmt.pix.sizeimage; |
10536 | 1528 fd_set rdset; |
1529 struct timeval timeout; | |
1530 struct v4l2_buffer buf; | |
1531 | |
1532 xskew = 0; | |
1533 skew = 0; | |
1534 interval = 0; | |
1535 prev_interval = 0; | |
1536 prev_skew = 0; | |
1537 | |
1538 mp_msg(MSGT_TV, MSGL_V, "%s: going to capture\n", info.short_name); | |
1539 if (ioctl(priv->video_fd, VIDIOC_STREAMON, &(priv->format.type)) < 0) { | |
23151 | 1540 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamon failed: %s\n", |
1541 info.short_name, strerror(errno)); | |
1542 return 0; | |
10536 | 1543 } |
1544 priv->streamon = 1; | |
1545 | |
23886 | 1546 if (!priv->tv_param->noaudio) { |
23151 | 1547 pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv); |
10536 | 1548 } |
1549 | |
1550 for (priv->frames = 0; !priv->shutdown;) | |
1551 { | |
23151 | 1552 int ret; |
1553 | |
1554 if (priv->immediate_mode) { | |
1555 while (priv->video_cnt == priv->video_buffer_size_max) { | |
1556 usleep(10000); | |
1557 if (priv->shutdown) { | |
1558 return NULL; | |
1559 } | |
1560 } | |
1561 } | |
1562 | |
1563 FD_ZERO (&rdset); | |
1564 FD_SET (priv->video_fd, &rdset); | |
10536 | 1565 |
23151 | 1566 timeout.tv_sec = 1; |
1567 timeout.tv_usec = 0; | |
10536 | 1568 |
23151 | 1569 i = select(priv->video_fd + 1, &rdset, NULL, NULL, &timeout); |
1570 if (i < 0) { | |
1571 mp_msg(MSGT_TV, MSGL_ERR, "%s: select failed: %s\n", | |
1572 info.short_name, strerror(errno)); | |
1573 continue; | |
1574 } | |
1575 else if (i == 0) { | |
1576 mp_msg(MSGT_TV, MSGL_ERR, "%s: select timeout\n", info.short_name); | |
1577 continue; | |
1578 } | |
1579 else if (!FD_ISSET(priv->video_fd, &rdset)) { | |
1580 continue; | |
1581 } | |
10536 | 1582 |
23151 | 1583 memset(&buf,0,sizeof(buf)); |
1584 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1585 buf.memory = V4L2_MEMORY_MMAP; | |
1586 ret = ioctl(priv->video_fd, VIDIOC_DQBUF, &buf); | |
10536 | 1587 |
23151 | 1588 if (ret < 0) { |
1589 /* | |
1590 if there's no signal, the buffer might me dequeued | |
1591 so we query all the buffers to see which one we should | |
1592 put back to queue | |
10536 | 1593 |
23151 | 1594 observed with saa7134 0.2.8 |
1595 don't know if is it a bug or (mis)feature | |
1596 */ | |
1597 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl dequeue buffer failed: %s, idx = %d\n", | |
1598 info.short_name, strerror(errno), buf.index); | |
1599 for (i = 0; i < priv->mapcount; i++) { | |
1600 memset(&buf,0,sizeof(buf)); | |
1601 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1602 buf.memory = V4L2_MEMORY_MMAP; | |
1603 buf.index = i; | |
1604 ret = ioctl(priv->video_fd, VIDIOC_QUERYBUF, &buf); | |
1605 if (ret < 0) { | |
1606 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s, idx = %d\n", | |
1607 info.short_name, strerror(errno), buf.index); | |
1608 return 0; | |
1609 } | |
1610 if ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE)) == V4L2_BUF_FLAG_MAPPED) { | |
1611 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { | |
1612 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1613 info.short_name, strerror(errno)); | |
1614 return 0; | |
1615 } | |
1616 } | |
1617 } | |
1618 continue; | |
1619 } | |
10536 | 1620 |
23151 | 1621 /* store the timestamp of the very first frame as reference */ |
1622 if (!priv->frames++) { | |
23886 | 1623 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1624 priv->first_frame = (long long)1e6*buf.timestamp.tv_sec + buf.timestamp.tv_usec; |
23886 | 1625 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1626 } |
1627 priv->curr_frame = (long long)buf.timestamp.tv_sec*1e6+buf.timestamp.tv_usec; | |
1628 // fprintf(stderr, "idx = %d, ts = %lf\n", buf.index, (double)(priv->curr_frame) / 1e6); | |
10536 | 1629 |
23151 | 1630 interval = priv->curr_frame - priv->first_frame; |
1631 delta = interval - prev_interval; | |
10536 | 1632 |
23151 | 1633 if (!priv->immediate_mode) { |
1634 // interpolate the skew in time | |
23886 | 1635 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1636 xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor; |
23886 | 1637 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1638 // correct extreme skew changes to avoid (especially) moving backwards in time |
1639 if (xskew - prev_skew > delta*MAX_SKEW_DELTA) { | |
1640 skew = prev_skew + delta*MAX_SKEW_DELTA; | |
1641 } else if (xskew - prev_skew < -delta*MAX_SKEW_DELTA) { | |
1642 skew = prev_skew - delta*MAX_SKEW_DELTA; | |
1643 } else { | |
1644 skew = xskew; | |
1645 } | |
1646 } | |
10536 | 1647 |
23151 | 1648 mp_msg(MSGT_TV, MSGL_DBG3, "\nfps = %lf, interval = %lf, a_skew = %f, corr_skew = %f\n", |
1649 delta ? (double)1e6/delta : -1, | |
1650 (double)1e-6*interval, (double)1e-6*xskew, (double)1e-6*skew); | |
1651 mp_msg(MSGT_TV, MSGL_DBG3, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt); | |
10536 | 1652 |
23151 | 1653 prev_skew = skew; |
1654 prev_interval = interval; | |
10536 | 1655 |
23151 | 1656 /* allocate a new buffer, if needed */ |
1657 pthread_mutex_lock(&priv->video_buffer_mutex); | |
1658 if (priv->video_buffer_size_current < priv->video_buffer_size_max) { | |
1659 if (priv->video_cnt == priv->video_buffer_size_current) { | |
1660 unsigned char *newbuf = malloc(framesize); | |
1661 if (newbuf) { | |
1662 memmove(priv->video_ringbuffer+priv->video_tail+1, priv->video_ringbuffer+priv->video_tail, | |
23423 | 1663 (priv->video_buffer_size_current-priv->video_tail)*sizeof(video_buffer_entry)); |
1664 priv->video_ringbuffer[priv->video_tail].data = newbuf; | |
23151 | 1665 if ((priv->video_head >= priv->video_tail) && (priv->video_cnt > 0)) priv->video_head++; |
1666 priv->video_buffer_size_current++; | |
1667 } | |
1668 } | |
1669 } | |
1670 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
10536 | 1671 |
23151 | 1672 if (priv->video_cnt == priv->video_buffer_size_current) { |
1673 if (!priv->immediate_mode) { | |
1674 mp_msg(MSGT_TV, MSGL_ERR, "\nvideo buffer full - dropping frame\n"); | |
1675 if (priv->audio_insert_null_samples) { | |
1676 pthread_mutex_lock(&priv->audio_mutex); | |
1677 priv->dropped_frames_timeshift += delta; | |
1678 pthread_mutex_unlock(&priv->audio_mutex); | |
1679 } | |
1680 } | |
1681 } else { | |
1682 if (priv->immediate_mode) { | |
23423 | 1683 priv->video_ringbuffer[priv->video_tail].timestamp = 0; |
23151 | 1684 } else { |
1685 // compensate for audio skew | |
1686 // negative skew => there are more audio samples, increase interval | |
1687 // positive skew => less samples, shorten the interval | |
23423 | 1688 priv->video_ringbuffer[priv->video_tail].timestamp = interval - skew; |
1689 if (priv->audio_insert_null_samples && priv->video_ringbuffer[priv->video_tail].timestamp > 0) { | |
23151 | 1690 pthread_mutex_lock(&priv->audio_mutex); |
23423 | 1691 priv->video_ringbuffer[priv->video_tail].timestamp += |
23151 | 1692 (priv->audio_null_blocks_inserted |
1693 - priv->dropped_frames_timeshift/priv->audio_usecs_per_block) | |
1694 *priv->audio_usecs_per_block; | |
1695 pthread_mutex_unlock(&priv->audio_mutex); | |
1696 } | |
1697 } | |
23423 | 1698 copy_frame(priv, priv->video_ringbuffer+priv->video_tail, priv->map[buf.index].addr,buf.bytesused); |
23151 | 1699 priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size_current; |
1700 priv->video_cnt++; | |
1701 } | |
1702 if (ioctl(priv->video_fd, VIDIOC_QBUF, &buf) < 0) { | |
1703 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1704 info.short_name, strerror(errno)); | |
1705 return 0; | |
1706 } | |
10536 | 1707 } |
1708 return NULL; | |
1709 } | |
1710 | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1711 #define MAX_LOOP 50 |
10536 | 1712 static double grab_video_frame(priv_t *priv, char *buffer, int len) |
1713 { | |
1714 double interval; | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1715 int loop_cnt = 0; |
10536 | 1716 |
1717 if (priv->first) { | |
23151 | 1718 pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv); |
1719 priv->first = 0; | |
10536 | 1720 } |
1721 | |
1722 while (priv->video_cnt == 0) { | |
23151 | 1723 usleep(10000); |
1724 if (loop_cnt++ > MAX_LOOP) return 0; | |
10536 | 1725 } |
1726 | |
1727 pthread_mutex_lock(&priv->video_buffer_mutex); | |
23423 | 1728 interval = (double)priv->video_ringbuffer[priv->video_head].timestamp*1e-6; |
1729 memcpy(buffer, priv->video_ringbuffer[priv->video_head].data, len); | |
10536 | 1730 priv->video_cnt--; |
1731 priv->video_head = (priv->video_head+1)%priv->video_buffer_size_current; | |
1732 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
1733 | |
1734 return interval; | |
1735 } | |
1736 | |
1737 static int get_video_framesize(priv_t *priv) | |
1738 { | |
23423 | 1739 /* |
1740 this routine will be called before grab_video_frame | |
1741 thus let's return topmost frame's size | |
1742 */ | |
1743 if (priv->video_cnt) | |
1744 return priv->video_ringbuffer[priv->video_head].framesize; | |
1745 /* | |
1746 no video frames yet available. i don't know what to do in this case, | |
1747 thus let's return some fallback result (for compressed format this will be | |
1748 maximum allowed frame size. | |
1749 */ | |
10536 | 1750 return priv->format.fmt.pix.sizeimage; |
1751 } | |
1752 | |
10704 | 1753 //#define DOUBLESPEED |
1754 #ifdef DOUBLESPEED | |
10536 | 1755 // for testing purposes only |
1756 static void read_doublespeed(priv_t *priv) | |
1757 { | |
18885 | 1758 char *bufx = calloc(priv->audio_in.blocksize, 2); |
10536 | 1759 short *s; |
1760 short *d; | |
1761 int i; | |
1762 | |
1763 audio_in_read_chunk(&priv->audio_in, bufx); | |
1764 audio_in_read_chunk(&priv->audio_in, bufx+priv->audio_in.blocksize); | |
1765 | |
1766 s = bufx; | |
1767 d = priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize; | |
1768 for (i = 0; i < priv->audio_in.blocksize/2; i++) { | |
23151 | 1769 *d++ = *s++; |
1770 *s++; | |
10536 | 1771 } |
1772 | |
1773 } | |
10704 | 1774 #endif |
10536 | 1775 |
1776 static void *audio_grabber(void *data) | |
1777 { | |
1778 priv_t *priv = (priv_t*)data; | |
1779 struct timeval tv; | |
1780 int i, audio_skew_ptr = 0; | |
10653 | 1781 long long current_time, prev_skew = 0, prev_skew_uncorr = 0; |
10852 | 1782 long long start_time_avg; |
10536 | 1783 |
1784 gettimeofday(&tv, NULL); | |
10852 | 1785 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec; |
10536 | 1786 audio_in_start_capture(&priv->audio_in); |
1787 for (i = 0; i < priv->aud_skew_cnt; i++) | |
23151 | 1788 priv->audio_skew_buffer[i] = 0; |
10653 | 1789 for (i = 0; i < priv->aud_skew_cnt; i++) |
23151 | 1790 priv->audio_skew_delta_buffer[i] = 0; |
10536 | 1791 |
1792 for (; !priv->shutdown;) | |
1793 { | |
10704 | 1794 #ifdef DOUBLESPEED |
23151 | 1795 read_doublespeed(priv); |
10704 | 1796 #else |
23151 | 1797 if (audio_in_read_chunk(&priv->audio_in, priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize) < 0) |
1798 continue; | |
10704 | 1799 #endif |
23151 | 1800 pthread_mutex_lock(&priv->skew_mutex); |
1801 if (priv->first_frame == 0) { | |
1802 // there is no first frame yet (unlikely to happen) | |
1803 gettimeofday(&tv, NULL); | |
1804 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec; | |
1805 // fprintf(stderr, "warning - first frame not yet available!\n"); | |
1806 pthread_mutex_unlock(&priv->skew_mutex); | |
1807 continue; | |
1808 } | |
1809 pthread_mutex_unlock(&priv->skew_mutex); | |
10536 | 1810 |
23151 | 1811 gettimeofday(&tv, NULL); |
10536 | 1812 |
23151 | 1813 priv->audio_recv_blocks_total++; |
1814 current_time = (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_start_time; | |
10536 | 1815 |
23151 | 1816 if (priv->audio_recv_blocks_total < priv->aud_skew_cnt*2) { |
1817 start_time_avg += (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; | |
1818 priv->audio_start_time = start_time_avg/(priv->audio_recv_blocks_total+1); | |
1819 } | |
10852 | 1820 |
23151 | 1821 // fprintf(stderr, "spb = %lf, bs = %d, skew = %lf\n", priv->audio_secs_per_block, priv->audio_in.blocksize, |
1822 // (double)(current_time - 1e6*priv->audio_secs_per_block*priv->audio_recv_blocks_total)/1e6); | |
10536 | 1823 |
23151 | 1824 // put the current skew into the ring buffer |
1825 priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr]; | |
1826 priv->audio_skew_buffer[audio_skew_ptr] = current_time | |
1827 - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; | |
1828 priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr]; | |
10536 | 1829 |
23151 | 1830 pthread_mutex_lock(&priv->skew_mutex); |
10704 | 1831 |
23151 | 1832 // skew calculation |
10704 | 1833 |
23151 | 1834 // compute the sliding average of the skews |
1835 if (priv->audio_recv_blocks_total > priv->aud_skew_cnt) { | |
1836 priv->audio_skew = priv->audio_skew_total/priv->aud_skew_cnt; | |
1837 } else { | |
1838 priv->audio_skew = priv->audio_skew_total/priv->audio_recv_blocks_total; | |
1839 } | |
10653 | 1840 |
23151 | 1841 // put the current skew change (skew-prev_skew) into the ring buffer |
1842 priv->audio_skew_delta_total -= priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1843 priv->audio_skew_delta_buffer[audio_skew_ptr] = priv->audio_skew - prev_skew_uncorr; | |
1844 priv->audio_skew_delta_total += priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1845 prev_skew_uncorr = priv->audio_skew; // remember the _uncorrected_ average value | |
10704 | 1846 |
23151 | 1847 audio_skew_ptr = (audio_skew_ptr+1) % priv->aud_skew_cnt; // rotate the buffer pointer |
10653 | 1848 |
23151 | 1849 // sliding average approximates the value in the middle of the interval |
1850 // so interpolate the skew value further to the current time | |
1851 priv->audio_skew += priv->audio_skew_delta_total/2; | |
10653 | 1852 |
23151 | 1853 // now finally, priv->audio_skew contains fairly good approximation |
1854 // of the current value | |
10653 | 1855 |
23151 | 1856 // current skew factor (assuming linearity) |
1857 // used for further interpolation in video_grabber | |
1858 // probably overkill but seems to be necessary for | |
1859 // stress testing by dropping half of the audio frames ;) | |
1860 // especially when using ALSA with large block sizes | |
1861 // where audio_skew remains a long while behind | |
1862 if ((priv->audio_skew_measure_time != 0) && (current_time - priv->audio_skew_measure_time != 0)) { | |
1863 priv->audio_skew_factor = (double)(priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time); | |
1864 } else { | |
1865 priv->audio_skew_factor = 0.0; | |
1866 } | |
10852 | 1867 |
23151 | 1868 priv->audio_skew_measure_time = current_time; |
1869 prev_skew = priv->audio_skew; | |
1870 priv->audio_skew += priv->audio_start_time - priv->first_frame; | |
1871 pthread_mutex_unlock(&priv->skew_mutex); | |
1872 | |
1873 // fprintf(stderr, "audio_skew = %lf, delta = %lf\n", (double)priv->audio_skew/1e6, (double)priv->audio_skew_delta_total/1e6); | |
10851 | 1874 |
23151 | 1875 pthread_mutex_lock(&priv->audio_mutex); |
1876 if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) { | |
1877 mp_msg(MSGT_TV, MSGL_ERR, "\ntoo bad - dropping audio frame !\n"); | |
1878 priv->audio_drop++; | |
1879 } else { | |
1880 priv->audio_tail = (priv->audio_tail+1) % priv->audio_buffer_size; | |
1881 priv->audio_cnt++; | |
1882 } | |
1883 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1884 } |
1885 return NULL; | |
1886 } | |
1887 | |
1888 static double grab_audio_frame(priv_t *priv, char *buffer, int len) | |
1889 { | |
1890 mp_dbg(MSGT_TV, MSGL_DBG2, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n", | |
23151 | 1891 priv, buffer, len); |
10536 | 1892 |
15449 | 1893 // hack: if grab_audio_frame is called first, it means we are used by mplayer |
1894 // => switch to the mode which outputs audio immediately, even if | |
1895 // it should be silence | |
1896 if (priv->first) priv->audio_insert_null_samples = 1; | |
1897 | |
1898 pthread_mutex_lock(&priv->audio_mutex); | |
1899 while (priv->audio_insert_null_samples | |
23151 | 1900 && priv->dropped_frames_timeshift - priv->dropped_frames_compensated >= priv->audio_usecs_per_block) { |
1901 // some frames were dropped - drop the corresponding number of audio blocks | |
1902 if (priv->audio_drop) { | |
1903 priv->audio_drop--; | |
1904 } else { | |
1905 if (priv->audio_head == priv->audio_tail) break; | |
1906 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
1907 } | |
1908 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
|
1909 } |
80402283a017
Fix immediatemode with mplayer (ie playing both sound and video)
albeu
parents:
10735
diff
changeset
|
1910 |
10536 | 1911 // compensate for dropped audio frames |
1912 if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) { | |
23151 | 1913 priv->audio_drop--; |
1914 memset(buffer, 0, len); | |
1915 goto out; | |
10536 | 1916 } |
1917 | |
15449 | 1918 if (priv->audio_insert_null_samples && (priv->audio_head == priv->audio_tail)) { |
23151 | 1919 // return silence to avoid desync and stuttering |
1920 memset(buffer, 0, len); | |
1921 priv->audio_null_blocks_inserted++; | |
1922 goto out; | |
15449 | 1923 } |
1924 | |
1925 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1926 while (priv->audio_head == priv->audio_tail) { |
23151 | 1927 // this is mencoder => just wait until some audio is available |
1928 usleep(10000); | |
10536 | 1929 } |
15451 | 1930 pthread_mutex_lock(&priv->audio_mutex); |
10536 | 1931 memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_in.blocksize, len); |
1932 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
1933 priv->audio_cnt--; | |
15449 | 1934 out: |
1935 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1936 priv->audio_sent_blocks_total++; |
1937 return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block; | |
1938 } | |
1939 | |
1940 static int get_audio_framesize(priv_t *priv) | |
1941 { | |
1942 return(priv->audio_in.blocksize); | |
1943 } |