Mercurial > mplayer.hg
annotate stream/tvi_v4l2.c @ 26877:4ab1cfcee430
Add a slave command to stop stream playback.
Mostly useful when used with -idle mode.
Patch by Mathieu Schroeter ( mathieu dot schroeter at gamesover dot ch )
author | ben |
---|---|
date | Tue, 27 May 2008 18:39:57 +0000 |
parents | c43ce7268677 |
children | 62916b06a055 |
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 */ |
25689 | 52 const tvi_info_t tvi_info_v4l2 = { |
22381
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 | |
25962 | 136 int audio_initialized; |
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 { | |
25962 | 368 if (priv->audio_initialized) return; |
15464 | 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 |
25962 | 401 priv->audio_initialized = 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 qctrl.id = control->id; |
482 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) { | |
23151 | 483 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n", |
484 info.short_name, strerror(errno)); | |
485 return TVI_CONTROL_FALSE; | |
10536 | 486 } |
487 | |
488 if (val_signed) { | |
23151 | 489 if (control->value < 0) { |
490 control->value = qctrl.default_value + control->value * | |
491 (qctrl.default_value - qctrl.minimum) / 100; | |
492 } else { | |
493 control->value = qctrl.default_value + control->value * | |
494 (qctrl.maximum - qctrl.default_value) / 100; | |
495 } | |
10536 | 496 } else { |
23151 | 497 if (control->value < 50) { |
498 control->value = qctrl.default_value + (control->value-50) * | |
499 (qctrl.default_value - qctrl.minimum) / 50; | |
500 } else { | |
501 control->value = qctrl.default_value + (control->value-50) * | |
502 (qctrl.maximum - qctrl.default_value) / 50; | |
503 } | |
10536 | 504 } |
505 | |
506 | |
507 if (ioctl(priv->video_fd, VIDIOC_S_CTRL, control) < 0) { | |
23151 | 508 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl set %s %d failed: %s\n", |
509 info.short_name, qctrl.name, control->value, strerror(errno)); | |
510 return TVI_CONTROL_FALSE; | |
10536 | 511 } |
512 mp_msg(MSGT_TV, MSGL_V, "%s: set %s: %d [%d, %d]\n", info.short_name, | |
513 qctrl.name, control->value, qctrl.minimum, qctrl.maximum); | |
514 | |
515 return TVI_CONTROL_TRUE; | |
516 } | |
517 | |
518 | |
519 /* | |
520 ** Scale the control values back to what mplayer needs. | |
521 */ | |
522 static int get_control(priv_t *priv, struct v4l2_control *control, int val_signed) { | |
23151 | 523 struct v4l2_queryctrl qctrl; |
10536 | 524 |
525 qctrl.id = control->id; | |
526 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) { | |
23151 | 527 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n", |
528 info.short_name, strerror(errno)); | |
529 return TVI_CONTROL_FALSE; | |
10536 | 530 } |
531 | |
532 if (ioctl(priv->video_fd, VIDIOC_G_CTRL, control) < 0) { | |
23151 | 533 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl get %s failed: %s\n", |
534 info.short_name, qctrl.name, strerror(errno)); | |
535 return TVI_CONTROL_FALSE; | |
10536 | 536 } |
537 mp_msg(MSGT_TV, MSGL_V, "%s: get %s: %d [%d, %d]\n", info.short_name, | |
538 qctrl.name, control->value, qctrl.minimum, qctrl.maximum); | |
539 | |
540 if (val_signed) { | |
23151 | 541 if (control->value < qctrl.default_value) { |
542 control->value = (control->value - qctrl.default_value) * 100 / | |
543 (qctrl.default_value - qctrl.minimum); | |
544 } else { | |
545 control->value = (control->value - qctrl.default_value) * 100 / | |
546 (qctrl.maximum - qctrl.default_value); | |
547 } | |
10536 | 548 } else { |
23151 | 549 if (control->value < qctrl.default_value) { |
550 control->value = (control->value - qctrl.default_value) * 50 / | |
551 (qctrl.default_value - qctrl.minimum) + 50; | |
552 } else { | |
553 control->value = (control->value - qctrl.default_value) * 50 / | |
554 (qctrl.maximum - qctrl.default_value) + 50; | |
555 } | |
10536 | 556 } |
557 | |
558 return TVI_CONTROL_TRUE; | |
559 } | |
560 | |
23901 | 561 #ifdef HAVE_TV_TELETEXT |
562 static int vbi_init(priv_t* priv,char* device) | |
563 { | |
564 int vbi_fd=0; | |
565 struct v4l2_capability cap; | |
566 struct v4l2_format fmt; | |
567 int res; | |
568 | |
569 if(!device) | |
570 return TVI_CONTROL_FALSE; | |
571 | |
572 priv->vbi_dev=strdup(device); | |
573 | |
574 vbi_fd=open(priv->vbi_dev,O_RDWR); | |
575 if(vbi_fd<0){ | |
576 mp_msg(MSGT_TV,MSGL_ERR,"vbi: could not open device %s\n",priv->vbi_dev); | |
577 return TVI_CONTROL_FALSE; | |
578 } | |
579 | |
580 if(ioctl(vbi_fd,VIDIOC_QUERYCAP,&cap)<0){ | |
581 mp_msg(MSGT_TV,MSGL_ERR,"vbi: Query capatibilities failed for %s\n",priv->vbi_dev); | |
582 close(vbi_fd); | |
583 return TVI_CONTROL_FALSE; | |
584 } | |
585 if(!cap.capabilities & V4L2_CAP_VBI_CAPTURE){ | |
586 mp_msg(MSGT_TV,MSGL_ERR,"vbi: %s does not support VBI capture\n",priv->vbi_dev); | |
587 close(vbi_fd); | |
588 return TVI_CONTROL_FALSE; | |
589 } | |
590 | |
591 memset(&fmt,0,sizeof(struct v4l2_format)); | |
592 fmt.type=V4L2_BUF_TYPE_VBI_CAPTURE; | |
593 if((res=ioctl(vbi_fd,VIDIOC_G_FMT,&fmt))<0){ | |
594 mp_msg(MSGT_TV,MSGL_ERR,"vbi: Query format failed: %x\n",res); | |
595 close(vbi_fd); | |
596 return TVI_CONTROL_FALSE; | |
597 } | |
598 if(fmt.fmt.vbi.sample_format!=V4L2_PIX_FMT_GREY){ | |
599 mp_msg(MSGT_TV,MSGL_ERR,"vbi: format 0x%x is not supported\n",fmt.fmt.vbi.sample_format); | |
600 close(vbi_fd); | |
601 return TVI_CONTROL_FALSE; | |
602 } | |
603 priv->vbi_fd=vbi_fd; | |
604 mp_msg(MSGT_TV,MSGL_DBG3,"vbi: init ok\n"); | |
605 return TVI_CONTROL_TRUE; | |
606 } | |
607 | |
608 static int vbi_get_props(priv_t* priv,tt_stream_props* ptsp) | |
609 { | |
610 struct v4l2_format fmt; | |
611 int res; | |
612 if(!priv || !ptsp) | |
613 return TVI_CONTROL_FALSE; | |
614 | |
615 memset(&fmt,0,sizeof(struct v4l2_format)); | |
616 fmt.type=V4L2_BUF_TYPE_VBI_CAPTURE; | |
617 if((res=ioctl(priv->vbi_fd,VIDIOC_G_FMT,&fmt))<0){ | |
618 mp_msg(MSGT_TV,MSGL_ERR,"vbi_get_props: Query format failed: %x\n",res); | |
619 return TVI_CONTROL_FALSE; | |
620 } | |
621 | |
622 ptsp->interlaced=(fmt.fmt.vbi.flags& V4L2_VBI_INTERLACED?1:0); | |
623 | |
624 ptsp->offset=fmt.fmt.vbi.offset; | |
625 ptsp->sampling_rate=fmt.fmt.vbi.sampling_rate; | |
626 ptsp->samples_per_line=fmt.fmt.vbi.samples_per_line, | |
627 | |
628 ptsp->count[0]=fmt.fmt.vbi.count[0]; | |
629 ptsp->count[1]=fmt.fmt.vbi.count[1]; | |
630 ptsp->bufsize = ptsp->samples_per_line * (ptsp->count[0] + ptsp->count[1]); | |
631 | |
632 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", | |
633 ptsp->sampling_rate, | |
634 ptsp->offset, | |
635 ptsp->samples_per_line, | |
636 ptsp->interlaced?"Yes":"No", | |
637 ptsp->count[0], | |
638 ptsp->count[1]); | |
639 | |
640 return TVI_CONTROL_TRUE; | |
641 } | |
642 | |
643 static void *vbi_grabber(void *data) | |
644 { | |
645 priv_t *priv = (priv_t *) data; | |
646 int bytes,seq,prev_seq; | |
647 unsigned char* buf; | |
648 tt_stream_props tsp; | |
649 | |
650 if(!priv->priv_vbi){ | |
651 mp_msg(MSGT_TV,MSGL_WARN,"vbi: vbi not initialized. stopping thread.\n"); | |
652 return NULL; | |
653 } | |
654 | |
655 if(vbi_get_props(priv,&tsp)!=TVI_CONTROL_TRUE) | |
656 return NULL; | |
657 | |
658 buf=malloc(tsp.bufsize); | |
659 seq=0; | |
660 prev_seq=0; | |
661 mp_msg(MSGT_TV,MSGL_V,"vbi: vbi capture thread started.\n"); | |
662 | |
663 while (!priv->vbi_shutdown){ | |
664 bytes=read(priv->vbi_fd,buf,tsp.bufsize); | |
24763
5d7f6e5e0847
After receiving EINTR 'read' syscall should be restarted.
voroshil
parents:
24553
diff
changeset
|
665 if(bytes<0 && errno==EINTR) |
5d7f6e5e0847
After receiving EINTR 'read' syscall should be restarted.
voroshil
parents:
24553
diff
changeset
|
666 continue; |
23901 | 667 if (bytes!=tsp.bufsize){ |
23979 | 668 mp_msg(MSGT_TV,MSGL_WARN,"vbi: expecting bytes: %d, got: %d\n",tsp.bufsize,bytes); |
23901 | 669 break; |
670 } | |
671 seq=*(int*)(buf+bytes-4); | |
672 if(seq<=1) continue; | |
673 if (prev_seq && seq!=prev_seq+1){ | |
674 prev_seq=0; | |
675 seq=0; | |
676 } | |
677 prev_seq=seq; | |
678 teletext_control(priv->priv_vbi,TV_VBI_CONTROL_DECODE_PAGE,&buf); | |
679 mp_msg(MSGT_TV,MSGL_DBG3,"grabber: seq:%d\n",seq); | |
680 } | |
681 free(buf); | |
682 return NULL; | |
683 } | |
684 #endif //HAVE_TV_TELETEXT | |
685 | |
10536 | 686 static int control(priv_t *priv, int cmd, void *arg) |
687 { | |
688 struct v4l2_control control; | |
689 struct v4l2_frequency frequency; | |
690 | |
691 switch(cmd) { | |
692 case TVI_CONTROL_IS_VIDEO: | |
23151 | 693 return priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE? |
694 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
|
695 case TVI_CONTROL_IS_AUDIO: |
23886 | 696 if (priv->tv_param->force_audio) return TVI_CONTROL_TRUE; |
10536 | 697 case TVI_CONTROL_IS_TUNER: |
23151 | 698 return priv->capability.capabilities & V4L2_CAP_TUNER? |
699 TVI_CONTROL_TRUE: TVI_CONTROL_FALSE; | |
10536 | 700 case TVI_CONTROL_IMMEDIATE: |
23151 | 701 priv->immediate_mode = 1; |
702 return TVI_CONTROL_TRUE; | |
10536 | 703 case TVI_CONTROL_VID_GET_FPS: |
23151 | 704 *(float *)arg = (float)priv->standard.frameperiod.denominator / |
705 priv->standard.frameperiod.numerator; | |
706 mp_msg(MSGT_TV, MSGL_V, "%s: get fps: %f\n", info.short_name, | |
707 *(float *)arg); | |
708 return TVI_CONTROL_TRUE; | |
10536 | 709 case TVI_CONTROL_VID_GET_BITS: |
23151 | 710 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
711 *(int *)arg = pixfmt2depth(priv->format.fmt.pix.pixelformat); | |
712 mp_msg(MSGT_TV, MSGL_V, "%s: get depth: %d\n", info.short_name, | |
713 *(int *)arg); | |
714 return TVI_CONTROL_TRUE; | |
10536 | 715 case TVI_CONTROL_VID_GET_FORMAT: |
23151 | 716 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
717 *(int *)arg = fcc_vl2mp(priv->format.fmt.pix.pixelformat); | |
718 mp_msg(MSGT_TV, MSGL_V, "%s: get format: %s\n", info.short_name, | |
719 pixfmt2name(priv->format.fmt.pix.pixelformat)); | |
720 return TVI_CONTROL_TRUE; | |
10536 | 721 case TVI_CONTROL_VID_SET_FORMAT: |
23151 | 722 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
723 priv->format.fmt.pix.pixelformat = fcc_mp2vl(*(int *)arg); | |
724 priv->format.fmt.pix.field = V4L2_FIELD_ANY; | |
725 | |
726 priv->mp_format = *(int *)arg; | |
727 mp_msg(MSGT_TV, MSGL_V, "%s: set format: %s\n", info.short_name, | |
728 pixfmt2name(priv->format.fmt.pix.pixelformat)); | |
729 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
730 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set format failed: %s\n", | |
731 info.short_name, strerror(errno)); | |
732 return TVI_CONTROL_FALSE; | |
733 } | |
734 /* according to the v4l2 specs VIDIOC_S_FMT should not fail, inflexible drivers | |
735 might even always return the default parameters -> update the format here*/ | |
736 priv->mp_format = fcc_vl2mp(priv->format.fmt.pix.pixelformat); | |
737 return TVI_CONTROL_TRUE; | |
10536 | 738 case TVI_CONTROL_VID_GET_WIDTH: |
23151 | 739 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
740 *(int *)arg = priv->format.fmt.pix.width; | |
741 mp_msg(MSGT_TV, MSGL_V, "%s: get width: %d\n", info.short_name, | |
742 *(int *)arg); | |
743 return TVI_CONTROL_TRUE; | |
10536 | 744 case TVI_CONTROL_VID_CHK_WIDTH: |
23151 | 745 return TVI_CONTROL_TRUE; |
10536 | 746 case TVI_CONTROL_VID_SET_WIDTH: |
23151 | 747 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
748 priv->format.fmt.pix.width = *(int *)arg; | |
749 mp_msg(MSGT_TV, MSGL_V, "%s: set width: %d\n", info.short_name, | |
750 *(int *)arg); | |
751 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
752 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set width failed: %s\n", | |
753 info.short_name, strerror(errno)); | |
754 return TVI_CONTROL_FALSE; | |
755 } | |
756 return TVI_CONTROL_TRUE; | |
10536 | 757 case TVI_CONTROL_VID_GET_HEIGHT: |
23151 | 758 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
759 *(int *)arg = priv->format.fmt.pix.height; | |
760 mp_msg(MSGT_TV, MSGL_V, "%s: get height: %d\n", info.short_name, | |
761 *(int *)arg); | |
762 return TVI_CONTROL_TRUE; | |
10536 | 763 case TVI_CONTROL_VID_CHK_HEIGHT: |
23151 | 764 return TVI_CONTROL_TRUE; |
10536 | 765 case TVI_CONTROL_VID_SET_HEIGHT: |
23151 | 766 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
767 priv->format.fmt.pix.height = *(int *)arg; | |
768 priv->format.fmt.pix.field = V4L2_FIELD_ANY; | |
769 mp_msg(MSGT_TV, MSGL_V, "%s: set height: %d\n", info.short_name, | |
770 *(int *)arg); | |
771 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
772 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set height failed: %s\n", | |
773 info.short_name, strerror(errno)); | |
774 return TVI_CONTROL_FALSE; | |
775 } | |
776 return TVI_CONTROL_TRUE; | |
777 case TVI_CONTROL_VID_GET_BRIGHTNESS: | |
778 control.id = V4L2_CID_BRIGHTNESS; | |
779 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
780 *(int *)arg = control.value; | |
781 return TVI_CONTROL_TRUE; | |
782 } | |
783 return TVI_CONTROL_FALSE; | |
784 case TVI_CONTROL_VID_SET_BRIGHTNESS: | |
785 control.id = V4L2_CID_BRIGHTNESS; | |
786 control.value = *(int *)arg; | |
787 return set_control(priv, &control, 1); | |
788 case TVI_CONTROL_VID_GET_HUE: | |
789 control.id = V4L2_CID_HUE; | |
790 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
791 *(int *)arg = control.value; | |
792 return TVI_CONTROL_TRUE; | |
793 } | |
794 return TVI_CONTROL_FALSE; | |
795 case TVI_CONTROL_VID_SET_HUE: | |
796 control.id = V4L2_CID_HUE; | |
797 control.value = *(int *)arg; | |
798 return set_control(priv, &control, 1); | |
799 case TVI_CONTROL_VID_GET_SATURATION: | |
800 control.id = V4L2_CID_SATURATION; | |
801 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
802 *(int *)arg = control.value; | |
803 return TVI_CONTROL_TRUE; | |
804 } | |
805 return TVI_CONTROL_FALSE; | |
806 case TVI_CONTROL_VID_SET_SATURATION: | |
807 control.id = V4L2_CID_SATURATION; | |
808 control.value = *(int *)arg; | |
809 return set_control(priv, &control, 1); | |
24553
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
810 case TVI_CONTROL_VID_GET_GAIN: |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
811 { |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
812 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
813 control.id = V4L2_CID_AUTOGAIN; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
814 if(get_control(priv,&control,0)!=TVI_CONTROL_TRUE) |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
815 return TVI_CONTROL_FALSE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
816 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
817 if(control.value){ //Auto Gain control is enabled |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
818 *(int*)arg=0; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
819 return TVI_CONTROL_TRUE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
820 } |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
821 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
822 //Manual Gain control |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
823 control.id = V4L2_CID_GAIN; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
824 if(get_control(priv,&control,0)!=TVI_CONTROL_TRUE) |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
825 return TVI_CONTROL_FALSE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
826 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
827 *(int*)arg=control.value?control.value:1; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
828 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
829 return TVI_CONTROL_TRUE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
830 } |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
831 case TVI_CONTROL_VID_SET_GAIN: |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
832 { |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
833 //value==0 means automatic gain control |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
834 int value=*(int*)arg; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
835 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
836 if (value < 0 || value>100) |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
837 return TVI_CONTROL_FALSE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
838 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
839 control.id=value?V4L2_CID_GAIN:V4L2_CID_AUTOGAIN; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
840 control.value=value?value:1; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
841 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
842 return set_control(priv,&control,0); |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
843 } |
23151 | 844 case TVI_CONTROL_VID_GET_CONTRAST: |
845 control.id = V4L2_CID_CONTRAST; | |
846 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
847 *(int *)arg = control.value; | |
848 return TVI_CONTROL_TRUE; | |
849 } | |
850 return TVI_CONTROL_FALSE; | |
851 case TVI_CONTROL_VID_SET_CONTRAST: | |
852 control.id = V4L2_CID_CONTRAST; | |
853 control.value = *(int *)arg; | |
854 return set_control(priv, &control, 1); | |
10536 | 855 case TVI_CONTROL_TUN_GET_FREQ: |
23151 | 856 frequency.tuner = 0; |
857 frequency.type = V4L2_TUNER_ANALOG_TV; | |
858 if (ioctl(priv->video_fd, VIDIOC_G_FREQUENCY, &frequency) < 0) { | |
859 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl get frequency failed: %s\n", | |
860 info.short_name, strerror(errno)); | |
861 return TVI_CONTROL_FALSE; | |
862 } | |
863 *(int *)arg = frequency.frequency; | |
864 return TVI_CONTROL_TRUE; | |
10536 | 865 case TVI_CONTROL_TUN_SET_FREQ: |
866 #if 0 | |
23151 | 867 set_mute(priv, 1); |
868 usleep(100000); // wait to suppress noise during switching | |
10536 | 869 #endif |
23151 | 870 frequency.tuner = 0; |
871 frequency.type = V4L2_TUNER_ANALOG_TV; | |
872 frequency.frequency = *(int *)arg; | |
873 if (ioctl(priv->video_fd, VIDIOC_S_FREQUENCY, &frequency) < 0) { | |
874 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl set frequency failed: %s\n", | |
875 info.short_name, strerror(errno)); | |
876 return TVI_CONTROL_FALSE; | |
877 } | |
10536 | 878 #if 0 |
23151 | 879 usleep(100000); // wait to suppress noise during switching |
880 set_mute(priv, 0); | |
10536 | 881 #endif |
23151 | 882 return TVI_CONTROL_TRUE; |
10536 | 883 case TVI_CONTROL_TUN_GET_TUNER: |
23151 | 884 mp_msg(MSGT_TV, MSGL_V, "%s: get tuner\n",info.short_name); |
885 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { | |
886 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n", | |
887 info.short_name, strerror(errno)); | |
888 return TVI_CONTROL_FALSE; | |
889 } | |
890 return TVI_CONTROL_TRUE; | |
10536 | 891 case TVI_CONTROL_TUN_SET_TUNER: |
23151 | 892 mp_msg(MSGT_TV, MSGL_V, "%s: set tuner\n",info.short_name); |
893 if (ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) { | |
894 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set tuner failed: %s\n", | |
895 info.short_name, strerror(errno)); | |
896 return TVI_CONTROL_FALSE; | |
897 } | |
898 return TVI_CONTROL_TRUE; | |
10536 | 899 case TVI_CONTROL_TUN_GET_NORM: |
23151 | 900 *(int *)arg = priv->standard.index; |
901 return TVI_CONTROL_TRUE; | |
24105
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
902 case TVI_CONTROL_TUN_GET_SIGNAL: |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
903 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
904 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
|
905 info.short_name, strerror(errno)); |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
906 return TVI_CONTROL_FALSE; |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
907 } |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
908 *(int*)arg=100*(priv->tuner.signal>>8)/255; |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
909 return TVI_CONTROL_TRUE; |
10536 | 910 case TVI_CONTROL_TUN_SET_NORM: |
23151 | 911 priv->standard.index = *(int *)arg; |
912 if (ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) { | |
913 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum norm failed: %s\n", | |
914 info.short_name, strerror(errno)); | |
915 return TVI_CONTROL_FALSE; | |
916 } | |
917 mp_msg(MSGT_TV, MSGL_V, "%s: set norm: %s\n", info.short_name, priv->standard.name); | |
918 if (ioctl(priv->video_fd, VIDIOC_S_STD, &priv->standard.id) < 0) { | |
919 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set norm failed: %s\n", | |
920 info.short_name, strerror(errno)); | |
921 return TVI_CONTROL_FALSE; | |
922 } | |
923 return TVI_CONTROL_TRUE; | |
13978 | 924 case TVI_CONTROL_SPC_GET_NORMID: |
23151 | 925 { |
926 int i; | |
927 for (i = 0;; i++) { | |
928 struct v4l2_standard standard; | |
929 memset(&standard, 0, sizeof(standard)); | |
930 standard.index = i; | |
931 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard)) | |
932 return TVI_CONTROL_FALSE; | |
933 if (!strcasecmp(standard.name, (char *)arg)) { | |
934 *(int *)arg = i; | |
935 return TVI_CONTROL_TRUE; | |
936 } | |
937 } | |
938 return TVI_CONTROL_FALSE; | |
939 } | |
10536 | 940 case TVI_CONTROL_SPC_GET_INPUT: |
23151 | 941 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, (int *)arg) < 0) { |
942 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n", | |
943 info.short_name, strerror(errno)); | |
944 return TVI_CONTROL_FALSE; | |
945 } | |
946 return TVI_CONTROL_TRUE; | |
10536 | 947 case TVI_CONTROL_SPC_SET_INPUT: |
23151 | 948 mp_msg(MSGT_TV, MSGL_V, "%s: set input: %d\n", info.short_name, *(int *)arg); |
949 priv->input.index = *(int *)arg; | |
950 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &priv->input) < 0) { | |
951 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum input failed: %s\n", | |
952 info.short_name, strerror(errno)); | |
953 return TVI_CONTROL_FALSE; | |
954 } | |
955 if (ioctl(priv->video_fd, VIDIOC_S_INPUT, (int *)arg) < 0) { | |
956 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set input failed: %s\n", | |
957 info.short_name, strerror(errno)); | |
958 return TVI_CONTROL_FALSE; | |
959 } | |
960 return TVI_CONTROL_TRUE; | |
10536 | 961 case TVI_CONTROL_AUD_GET_FORMAT: |
23151 | 962 init_audio(priv); |
25962 | 963 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 964 *(int *)arg = AF_FORMAT_S16_LE; |
965 mp_msg(MSGT_TV, MSGL_V, "%s: get audio format: %d\n", | |
966 info.short_name, *(int *)arg); | |
967 return TVI_CONTROL_TRUE; | |
10536 | 968 case TVI_CONTROL_AUD_GET_SAMPLERATE: |
23151 | 969 init_audio(priv); |
25962 | 970 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 971 *(int *)arg = priv->audio_in.samplerate; |
972 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplerate: %d\n", | |
973 info.short_name, *(int *)arg); | |
974 return TVI_CONTROL_TRUE; | |
10536 | 975 case TVI_CONTROL_AUD_GET_SAMPLESIZE: |
23151 | 976 init_audio(priv); |
25962 | 977 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 978 *(int *)arg = priv->audio_in.bytes_per_sample; |
979 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplesize: %d\n", | |
980 info.short_name, *(int *)arg); | |
981 return TVI_CONTROL_TRUE; | |
10536 | 982 case TVI_CONTROL_AUD_GET_CHANNELS: |
23151 | 983 init_audio(priv); |
25962 | 984 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 985 *(int *)arg = priv->audio_in.channels; |
986 mp_msg(MSGT_TV, MSGL_V, "%s: get audio channels: %d\n", | |
987 info.short_name, *(int *)arg); | |
988 return TVI_CONTROL_TRUE; | |
10536 | 989 case TVI_CONTROL_AUD_SET_SAMPLERATE: |
23151 | 990 init_audio(priv); |
991 mp_msg(MSGT_TV, MSGL_V, "%s: set audio samplerate: %d\n", | |
992 info.short_name, *(int *)arg); | |
993 if (audio_in_set_samplerate(&priv->audio_in, *(int*)arg) < 0) return TVI_CONTROL_FALSE; | |
994 // setup_audio_buffer_sizes(priv); | |
995 return TVI_CONTROL_TRUE; | |
23901 | 996 #ifdef HAVE_TV_TELETEXT |
997 case TVI_CONTROL_VBI_INIT: | |
998 { | |
999 void* ptr; | |
1000 tt_stream_props tsp; | |
1001 | |
1002 if (vbi_init(priv,*(char**)arg)!=TVI_CONTROL_TRUE) | |
1003 return TVI_CONTROL_FALSE; | |
1004 if(vbi_get_props(priv,&tsp)==TVI_CONTROL_TRUE) | |
1005 { | |
1006 ptr=&tsp; | |
1007 if(teletext_control(NULL,TV_VBI_CONTROL_START,&ptr)==TVI_CONTROL_TRUE) | |
1008 priv->priv_vbi=ptr; | |
1009 else | |
1010 priv->priv_vbi=NULL; | |
1011 } | |
1012 return TVI_CONTROL_TRUE; | |
1013 } | |
1014 default: | |
1015 return teletext_control(priv->priv_vbi,cmd,arg); | |
1016 #endif | |
10536 | 1017 } |
1018 mp_msg(MSGT_TV, MSGL_V, "%s: unknown control: %d\n", info.short_name, cmd); | |
26756
c43ce7268677
cosmetics: Remove useless parentheses from return statements.
diego
parents:
25962
diff
changeset
|
1019 return TVI_CONTROL_UNKNOWN; |
10536 | 1020 } |
1021 | |
1022 | |
1023 #define PRIV ((priv_t *) (tvi_handle->priv)) | |
1024 | |
1025 /* handler creator - entry point ! */ | |
23883 | 1026 static tvi_handle_t *tvi_init_v4l2(tv_param_t* tv_param) |
10536 | 1027 { |
1028 tvi_handle_t *tvi_handle; | |
1029 | |
1030 /* new_handle initializes priv with memset 0 */ | |
1031 tvi_handle = new_handle(); | |
1032 if (!tvi_handle) { | |
23151 | 1033 return NULL; |
10536 | 1034 } |
1035 PRIV->video_fd = -1; | |
1036 | |
23883 | 1037 PRIV->video_dev = strdup(tv_param->device? tv_param->device: "/dev/video0"); |
10536 | 1038 if (!PRIV->video_dev) { |
23151 | 1039 free_handle(tvi_handle); |
1040 return NULL; | |
10536 | 1041 } |
1042 | |
23883 | 1043 if (tv_param->adevice) { |
1044 PRIV->audio_dev = strdup(tv_param->adevice); | |
23151 | 1045 if (!PRIV->audio_dev) { |
1046 free(PRIV->video_dev); | |
1047 free_handle(tvi_handle); | |
1048 return NULL; | |
1049 } | |
10536 | 1050 } |
1051 | |
23883 | 1052 PRIV->tv_param=tv_param; |
10536 | 1053 return tvi_handle; |
1054 } | |
1055 | |
1056 #undef PRIV | |
1057 | |
1058 | |
1059 static int uninit(priv_t *priv) | |
1060 { | |
1061 int i, frames, dropped = 0; | |
1062 | |
23901 | 1063 #ifdef HAVE_TV_TELETEXT |
1064 priv->vbi_shutdown=1; | |
1065 if(priv->vbi_grabber_thread) | |
1066 pthread_join(priv->vbi_grabber_thread, NULL); | |
1067 | |
1068 teletext_control(priv->priv_vbi,TV_VBI_CONTROL_STOP,(void*)1); | |
1069 priv->priv_vbi=NULL; | |
1070 | |
1071 if(priv->vbi_fd){ | |
1072 close(priv->vbi_fd); | |
1073 priv->vbi_fd=0; | |
1074 } | |
1075 | |
1076 if(priv->vbi_dev){ | |
1077 free(priv->vbi_dev); | |
1078 priv->vbi_dev=0; | |
1079 } | |
1080 | |
1081 #endif | |
1082 | |
10536 | 1083 priv->shutdown = 1; |
16185 | 1084 if(priv->video_grabber_thread) |
23151 | 1085 pthread_join(priv->video_grabber_thread, NULL); |
10536 | 1086 pthread_mutex_destroy(&priv->video_buffer_mutex); |
1087 | |
1088 if (priv->streamon) { | |
23151 | 1089 struct v4l2_buffer buf; |
10536 | 1090 |
23151 | 1091 /* get performance */ |
1092 frames = 1 + (priv->curr_frame - priv->first_frame + | |
1093 priv->standard.frameperiod.numerator * 500000 / | |
1094 priv->standard.frameperiod.denominator) * | |
1095 priv->standard.frameperiod.denominator / | |
1096 priv->standard.frameperiod.numerator / 1000000; | |
1097 dropped = frames - priv->frames; | |
10536 | 1098 |
23151 | 1099 /* turn off streaming */ |
1100 if (ioctl(priv->video_fd, VIDIOC_STREAMOFF, &(priv->map[0].buf.type)) < 0) { | |
1101 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamoff failed: %s\n", | |
1102 info.short_name, strerror(errno)); | |
1103 } | |
1104 priv->streamon = 0; | |
10536 | 1105 |
23151 | 1106 /* unqueue all remaining buffers */ |
1107 memset(&buf,0,sizeof(buf)); | |
1108 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1109 buf.memory = V4L2_MEMORY_MMAP; | |
1110 while (!ioctl(priv->video_fd, VIDIOC_DQBUF, &buf)); | |
10536 | 1111 } |
1112 | |
1113 /* unmap all buffers */ | |
1114 for (i = 0; i < priv->mapcount; i++) { | |
23151 | 1115 if (munmap(priv->map[i].addr, priv->map[i].len) < 0) { |
1116 mp_msg(MSGT_TV, MSGL_ERR, "%s: munmap capture buffer failed: %s\n", | |
1117 info.short_name, strerror(errno)); | |
1118 } | |
10536 | 1119 } |
1120 | |
1121 /* stop audio thread */ | |
23886 | 1122 if (!priv->tv_param->noaudio && priv->audio_grabber_thread) { |
23151 | 1123 pthread_join(priv->audio_grabber_thread, NULL); |
1124 pthread_mutex_destroy(&priv->skew_mutex); | |
1125 pthread_mutex_destroy(&priv->audio_mutex); | |
10536 | 1126 } |
1127 | |
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
|
1128 set_mute(priv, 1); |
10536 | 1129 |
1130 /* free memory and close device */ | |
23151 | 1131 free(priv->map); priv->map = NULL; |
10536 | 1132 priv->mapcount = 0; |
23151 | 1133 if(priv->video_fd!=-1)close(priv->video_fd); priv->video_fd = -1; |
1134 free(priv->video_dev); priv->video_dev = NULL; | |
10536 | 1135 |
1136 if (priv->video_ringbuffer) { | |
23151 | 1137 int i; |
1138 for (i = 0; i < priv->video_buffer_size_current; i++) { | |
23423 | 1139 free(priv->video_ringbuffer[i].data); |
23151 | 1140 } |
1141 free(priv->video_ringbuffer); | |
10536 | 1142 } |
23886 | 1143 if (!priv->tv_param->noaudio) { |
23151 | 1144 if (priv->audio_ringbuffer) |
1145 free(priv->audio_ringbuffer); | |
1146 if (priv->audio_skew_buffer) | |
1147 free(priv->audio_skew_buffer); | |
1148 if (priv->audio_skew_delta_buffer) | |
1149 free(priv->audio_skew_delta_buffer); | |
25005
1635b2b31bdd
Add missing call to audio_in_uninit in v4l2 tv driver.
voroshil
parents:
24763
diff
changeset
|
1150 |
1635b2b31bdd
Add missing call to audio_in_uninit in v4l2 tv driver.
voroshil
parents:
24763
diff
changeset
|
1151 audio_in_uninit(&priv->audio_in); |
10536 | 1152 } |
1153 | |
1154 /* show some nice statistics ;-) */ | |
1155 mp_msg(MSGT_TV, MSGL_INFO, | |
23151 | 1156 "%s: %d frames successfully processed, %d frames dropped.\n", |
1157 info.short_name, priv->frames, dropped); | |
10536 | 1158 mp_msg(MSGT_TV, MSGL_V, "%s: up to %u video frames buffered.\n", |
23151 | 1159 info.short_name, priv->video_buffer_size_current); |
10536 | 1160 return 1; |
1161 } | |
1162 | |
1163 | |
1164 /* initialisation */ | |
1165 static int init(priv_t *priv) | |
1166 { | |
1167 int i; | |
1168 | |
1169 priv->audio_ringbuffer = NULL; | |
1170 priv->audio_skew_buffer = NULL; | |
10653 | 1171 priv->audio_skew_delta_buffer = NULL; |
10536 | 1172 |
25962 | 1173 priv->audio_initialized = 0; |
15464 | 1174 |
10536 | 1175 /* Open the video device. */ |
1176 priv->video_fd = open(priv->video_dev, O_RDWR); | |
1177 if (priv->video_fd < 0) { | |
23151 | 1178 mp_msg(MSGT_TV, MSGL_ERR, "%s: unable to open '%s': %s\n", |
1179 info.short_name, priv->video_dev, strerror(errno)); | |
1180 uninit(priv); | |
1181 return 0; | |
10536 | 1182 } |
1183 mp_msg(MSGT_TV, MSGL_DBG2, "%s: video fd: %s: %d\n", | |
23151 | 1184 info.short_name, priv->video_dev, priv->video_fd); |
10536 | 1185 |
1186 /* | |
1187 ** Query the video capabilities and current settings | |
1188 ** for further control calls. | |
1189 */ | |
1190 if (ioctl(priv->video_fd, VIDIOC_QUERYCAP, &priv->capability) < 0) { | |
23151 | 1191 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query capabilities failed: %s\n", |
1192 info.short_name, strerror(errno)); | |
1193 uninit(priv); | |
1194 return 0; | |
10536 | 1195 } |
1196 | |
1197 if (!(priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE)) | |
1198 { | |
23151 | 1199 mp_msg(MSGT_TV, MSGL_ERR, "Device %s is not a video capture device.\n", |
1200 priv->video_dev); | |
1201 return 0; | |
10536 | 1202 } |
1203 | |
18073
8ceb31f028ee
make failures during e.g. setting the TV norm non-fatal.
reimar
parents:
17765
diff
changeset
|
1204 if (getfmt(priv) < 0) { |
23151 | 1205 uninit(priv); |
1206 return 0; | |
10536 | 1207 } |
18073
8ceb31f028ee
make failures during e.g. setting the TV norm non-fatal.
reimar
parents:
17765
diff
changeset
|
1208 getstd(priv); |
10536 | 1209 /* |
1210 ** if this device has got a tuner query it's settings | |
1211 ** otherwise set some nice defaults | |
1212 */ | |
1213 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1214 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { |
1215 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n", | |
1216 info.short_name, strerror(errno)); | |
1217 uninit(priv); | |
1218 return 0; | |
1219 } | |
10536 | 1220 } |
1221 mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.card); | |
1222 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1223 mp_msg(MSGT_TV, MSGL_INFO, " Tuner cap:%s%s%s\n", |
1224 (priv->tuner.capability & V4L2_TUNER_CAP_STEREO) ? " STEREO" : "", | |
1225 (priv->tuner.capability & V4L2_TUNER_CAP_LANG1) ? " LANG1" : "", | |
1226 (priv->tuner.capability & V4L2_TUNER_CAP_LANG2) ? " LANG2" : ""); | |
1227 mp_msg(MSGT_TV, MSGL_INFO, " Tuner rxs:%s%s%s%s\n", | |
1228 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_MONO) ? " MONO" : "", | |
1229 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_STEREO) ? " STEREO" : "", | |
1230 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG1) ? " LANG1" : "", | |
1231 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG2) ? " LANG2" : ""); | |
10536 | 1232 } |
1233 mp_msg(MSGT_TV, MSGL_INFO, " Capabilites:%s%s%s%s%s%s%s%s%s%s%s\n", | |
23151 | 1234 priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE? |
1235 " video capture": "", | |
1236 priv->capability.capabilities & V4L2_CAP_VIDEO_OUTPUT? | |
1237 " video output": "", | |
1238 priv->capability.capabilities & V4L2_CAP_VIDEO_OVERLAY? | |
1239 " video overlay": "", | |
1240 priv->capability.capabilities & V4L2_CAP_VBI_CAPTURE? | |
1241 " VBI capture device": "", | |
1242 priv->capability.capabilities & V4L2_CAP_VBI_OUTPUT? | |
1243 " VBI output": "", | |
1244 priv->capability.capabilities & V4L2_CAP_RDS_CAPTURE? | |
1245 " RDS data capture": "", | |
1246 priv->capability.capabilities & V4L2_CAP_TUNER? | |
1247 " tuner": "", | |
1248 priv->capability.capabilities & V4L2_CAP_AUDIO? | |
1249 " audio": "", | |
1250 priv->capability.capabilities & V4L2_CAP_READWRITE? | |
1251 " read/write": "", | |
1252 priv->capability.capabilities & V4L2_CAP_ASYNCIO? | |
1253 " async i/o": "", | |
1254 priv->capability.capabilities & V4L2_CAP_STREAMING? | |
1255 " streaming": ""); | |
10536 | 1256 mp_msg(MSGT_TV, MSGL_INFO, " supported norms:"); |
1257 for (i = 0;; i++) { | |
23151 | 1258 struct v4l2_standard standard; |
1259 memset(&standard, 0, sizeof(standard)); | |
1260 standard.index = i; | |
1261 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard)) | |
1262 break; | |
1263 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, standard.name); | |
10536 | 1264 } |
1265 mp_msg(MSGT_TV, MSGL_INFO, "\n inputs:"); | |
1266 for (i = 0; 1; i++) { | |
23151 | 1267 struct v4l2_input input; |
10536 | 1268 |
23151 | 1269 input.index = i; |
1270 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &input) < 0) { | |
1271 break; | |
1272 } | |
1273 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, input.name); | |
10536 | 1274 } |
1275 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, &i) < 0) { | |
23151 | 1276 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n", |
1277 info.short_name, strerror(errno)); | |
10536 | 1278 } |
1279 mp_msg(MSGT_TV, MSGL_INFO, "\n Current input: %d\n", i); | |
1280 for (i = 0; ; i++) { | |
23151 | 1281 struct v4l2_fmtdesc fmtdesc; |
10536 | 1282 |
23151 | 1283 fmtdesc.index = i; |
1284 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1285 if (ioctl(priv->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) { | |
1286 break; | |
1287 } | |
1288 mp_msg(MSGT_TV, MSGL_V, " Format %-6s (%2d bits, %s): %s\n", | |
1289 pixfmt2name(fmtdesc.pixelformat), pixfmt2depth(fmtdesc.pixelformat), | |
1290 fmtdesc.description, vo_format_name(fcc_vl2mp(fmtdesc.pixelformat))); | |
10536 | 1291 } |
1292 mp_msg(MSGT_TV, MSGL_INFO, " Current format: %s\n", | |
23151 | 1293 pixfmt2name(priv->format.fmt.pix.pixelformat)); |
10536 | 1294 |
1295 /* set some nice defaults */ | |
1296 if (getfmt(priv) < 0) return 0; | |
1297 priv->format.fmt.pix.width = 640; | |
1298 priv->format.fmt.pix.height = 480; | |
1299 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
23151 | 1300 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set format failed: %s\n", |
1301 info.short_name, strerror(errno)); | |
1302 uninit(priv); | |
1303 return 0; | |
10536 | 1304 } |
1305 | |
23886 | 1306 // if (!(priv->capability.capabilities & V4L2_CAP_AUDIO) && !priv->tv_param->force_audio) priv->tv_param->noaudio = 1; |
10536 | 1307 |
1308 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1309 struct v4l2_control control; |
23886 | 1310 if (priv->tv_param->amode >= 0) { |
23151 | 1311 mp_msg(MSGT_TV, MSGL_V, "%s: setting audio mode\n", info.short_name); |
23886 | 1312 priv->tuner.audmode = amode2v4l(priv->tv_param->amode); |
23151 | 1313 if (ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) { |
1314 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set tuner failed: %s\n", | |
1315 info.short_name, strerror(errno)); | |
1316 return TVI_CONTROL_FALSE; | |
1317 } | |
1318 } | |
1319 mp_msg(MSGT_TV, MSGL_INFO, "%s: current audio mode is :%s%s%s%s\n", info.short_name, | |
1320 (priv->tuner.audmode == V4L2_TUNER_MODE_MONO) ? " MONO" : "", | |
1321 (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) ? " STEREO" : "", | |
1322 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG1) ? " LANG1" : "", | |
1323 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG2) ? " LANG2" : ""); | |
10536 | 1324 |
23886 | 1325 if (priv->tv_param->volume >= 0) { |
23151 | 1326 control.id = V4L2_CID_AUDIO_VOLUME; |
23886 | 1327 control.value = priv->tv_param->volume; |
23151 | 1328 set_control(priv, &control, 0); |
1329 } | |
23886 | 1330 if (priv->tv_param->bass >= 0) { |
23151 | 1331 control.id = V4L2_CID_AUDIO_BASS; |
23886 | 1332 control.value = priv->tv_param->bass; |
23151 | 1333 set_control(priv, &control, 0); |
1334 } | |
23886 | 1335 if (priv->tv_param->treble >= 0) { |
23151 | 1336 control.id = V4L2_CID_AUDIO_TREBLE; |
23886 | 1337 control.value = priv->tv_param->treble; |
23151 | 1338 set_control(priv, &control, 0); |
1339 } | |
23886 | 1340 if (priv->tv_param->balance >= 0) { |
23151 | 1341 control.id = V4L2_CID_AUDIO_BALANCE; |
23886 | 1342 control.value = priv->tv_param->balance; |
23151 | 1343 set_control(priv, &control, 0); |
1344 } | |
10536 | 1345 } |
1346 | |
1347 return 1; | |
1348 } | |
1349 | |
1350 static int get_capture_buffer_size(priv_t *priv) | |
1351 { | |
1352 int bufsize, cnt; | |
1353 | |
23886 | 1354 if (priv->tv_param->buffer_size >= 0) { |
1355 bufsize = priv->tv_param->buffer_size*1024*1024; | |
10536 | 1356 } else { |
1357 #ifdef HAVE_SYS_SYSINFO_H | |
23151 | 1358 struct sysinfo si; |
1359 | |
1360 sysinfo(&si); | |
1361 if (si.totalram<2*1024*1024) { | |
1362 bufsize = 1024*1024; | |
1363 } else { | |
1364 bufsize = si.totalram/2; | |
1365 } | |
10536 | 1366 #else |
23151 | 1367 bufsize = 16*1024*1024; |
10536 | 1368 #endif |
1369 } | |
1370 | |
23423 | 1371 cnt = bufsize/priv->format.fmt.pix.sizeimage; |
10536 | 1372 if (cnt < 2) cnt = 2; |
1373 | |
1374 return cnt; | |
1375 } | |
1376 | |
1377 /* that's the real start, we'got the format parameters (checked with control) */ | |
1378 static int start(priv_t *priv) | |
1379 { | |
1380 struct v4l2_requestbuffers request; | |
24346 | 1381 unsigned int i; |
10536 | 1382 |
1383 /* setup audio parameters */ | |
1384 | |
15464 | 1385 init_audio(priv); |
25962 | 1386 if (!priv->tv_param->noaudio && !priv->audio_initialized) return 0; |
15464 | 1387 |
10536 | 1388 /* we need this to size the audio buffer properly */ |
1389 if (priv->immediate_mode) { | |
23151 | 1390 priv->video_buffer_size_max = 2; |
10536 | 1391 } else { |
23151 | 1392 priv->video_buffer_size_max = get_capture_buffer_size(priv); |
10536 | 1393 } |
1394 | |
23886 | 1395 if (!priv->tv_param->noaudio) { |
23151 | 1396 setup_audio_buffer_sizes(priv); |
1397 priv->audio_skew_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1398 if (!priv->audio_skew_buffer) { | |
1399 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1400 return 0; | |
1401 } | |
1402 priv->audio_skew_delta_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1403 if (!priv->audio_skew_delta_buffer) { | |
1404 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1405 return 0; | |
1406 } | |
10536 | 1407 |
23151 | 1408 priv->audio_ringbuffer = calloc(priv->audio_in.blocksize, priv->audio_buffer_size); |
1409 if (!priv->audio_ringbuffer) { | |
1410 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate audio buffer: %s\n", strerror(errno)); | |
1411 return 0; | |
1412 } | |
10536 | 1413 |
23151 | 1414 priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate |
1415 *priv->audio_in.channels | |
1416 *priv->audio_in.bytes_per_sample); | |
1417 priv->audio_usecs_per_block = 1e6*priv->audio_secs_per_block; | |
1418 priv->audio_head = 0; | |
1419 priv->audio_tail = 0; | |
1420 priv->audio_cnt = 0; | |
1421 priv->audio_drop = 0; | |
1422 priv->audio_skew = 0; | |
1423 priv->audio_skew_total = 0; | |
1424 priv->audio_skew_delta_total = 0; | |
1425 priv->audio_recv_blocks_total = 0; | |
1426 priv->audio_sent_blocks_total = 0; | |
1427 priv->audio_null_blocks_inserted = 0; | |
1428 priv->audio_insert_null_samples = 0; | |
1429 priv->dropped_frames_timeshift = 0; | |
1430 priv->dropped_frames_compensated = 0; | |
15449 | 1431 |
23151 | 1432 pthread_mutex_init(&priv->skew_mutex, NULL); |
1433 pthread_mutex_init(&priv->audio_mutex, NULL); | |
10536 | 1434 } |
1435 | |
1436 /* setup video parameters */ | |
23886 | 1437 if (!priv->tv_param->noaudio) { |
23151 | 1438 if (priv->video_buffer_size_max < (3*priv->standard.frameperiod.denominator) / |
1439 priv->standard.frameperiod.numerator | |
1440 *priv->audio_secs_per_block) { | |
1441 mp_msg(MSGT_TV, MSGL_ERR, "Video buffer shorter than 3 times audio frame duration.\n" | |
1442 "You will probably experience heavy framedrops.\n"); | |
1443 } | |
10536 | 1444 } |
1445 | |
1446 { | |
23151 | 1447 int bytesperline = priv->format.fmt.pix.width*pixfmt2depth(priv->format.fmt.pix.pixelformat)/8; |
1448 | |
1449 mp_msg(MSGT_TV, MSGL_V, "Using a ring buffer for maximum %d frames, %d MB total size.\n", | |
1450 priv->video_buffer_size_max, | |
1451 priv->video_buffer_size_max*priv->format.fmt.pix.height*bytesperline/(1024*1024)); | |
10536 | 1452 } |
1453 | |
23423 | 1454 priv->video_ringbuffer = calloc(priv->video_buffer_size_max, sizeof(video_buffer_entry)); |
10536 | 1455 if (!priv->video_ringbuffer) { |
23151 | 1456 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate video buffer: %s\n", strerror(errno)); |
1457 return 0; | |
10536 | 1458 } |
23423 | 1459 memset(priv->video_ringbuffer,0,priv->video_buffer_size_max * sizeof(video_buffer_entry)); |
10536 | 1460 |
15449 | 1461 pthread_mutex_init(&priv->video_buffer_mutex, NULL); |
1462 | |
10536 | 1463 priv->video_head = 0; |
1464 priv->video_tail = 0; | |
1465 priv->video_cnt = 0; | |
1466 | |
1467 /* request buffers */ | |
1468 if (priv->immediate_mode) { | |
23151 | 1469 request.count = 2; |
10536 | 1470 } else { |
23151 | 1471 request.count = BUFFER_COUNT; |
10536 | 1472 } |
1473 | |
1474 request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1475 request.memory = V4L2_MEMORY_MMAP; | |
1476 if (ioctl(priv->video_fd, VIDIOC_REQBUFS, &request) < 0) { | |
23151 | 1477 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl request buffers failed: %s\n", |
1478 info.short_name, strerror(errno)); | |
1479 return 0; | |
10536 | 1480 } |
1481 | |
1482 /* query buffers */ | |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18176
diff
changeset
|
1483 if (!(priv->map = calloc(request.count, sizeof(struct map)))) { |
23151 | 1484 mp_msg(MSGT_TV, MSGL_ERR, "%s: malloc capture buffers failed: %s\n", |
1485 info.short_name, strerror(errno)); | |
1486 return 0; | |
10536 | 1487 } |
1488 | |
1489 /* map and queue buffers */ | |
1490 for (i = 0; i < request.count; i++) { | |
23151 | 1491 memset(&priv->map[i].buf,0,sizeof(priv->map[i].buf)); |
1492 priv->map[i].buf.index = i; | |
1493 priv->map[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1494 priv->map[i].buf.memory = V4L2_MEMORY_MMAP; | |
1495 if (ioctl(priv->video_fd, VIDIOC_QUERYBUF, &(priv->map[i].buf)) < 0) { | |
1496 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s\n", | |
1497 info.short_name, strerror(errno)); | |
1498 free(priv->map); | |
1499 priv->map = NULL; | |
1500 return 0; | |
1501 } | |
1502 priv->map[i].addr = mmap (0, priv->map[i].buf.length, PROT_READ | | |
1503 PROT_WRITE, MAP_SHARED, priv->video_fd, priv->map[i].buf.m.offset); | |
1504 if (priv->map[i].addr == MAP_FAILED) { | |
1505 mp_msg(MSGT_TV, MSGL_ERR, "%s: mmap capture buffer failed: %s\n", | |
1506 info.short_name, strerror(errno)); | |
1507 priv->map[i].len = 0; | |
1508 return 0; | |
1509 } | |
1510 priv->map[i].len = priv->map[i].buf.length; | |
1511 /* count up to make sure this is correct everytime */ | |
1512 priv->mapcount++; | |
10536 | 1513 |
23151 | 1514 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { |
1515 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1516 info.short_name, strerror(errno)); | |
1517 return 0; | |
1518 } | |
10536 | 1519 } |
1520 | |
23901 | 1521 #ifdef HAVE_TV_TELETEXT |
1522 /* start vbi thread */ | |
1523 if(priv->priv_vbi){ | |
1524 priv->vbi_shutdown = 0; | |
1525 pthread_create(&priv->vbi_grabber_thread, NULL, vbi_grabber, priv); | |
1526 } | |
1527 #endif | |
10536 | 1528 /* start audio thread */ |
1529 priv->shutdown = 0; | |
1530 priv->audio_skew_measure_time = 0; | |
1531 priv->first_frame = 0; | |
1532 priv->audio_skew = 0; | |
1533 priv->first = 1; | |
1534 | |
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
|
1535 set_mute(priv, 0); |
10536 | 1536 |
1537 return 1; | |
1538 } | |
1539 | |
1540 // copies a video frame | |
23423 | 1541 static inline void copy_frame(priv_t *priv, video_buffer_entry *dest, unsigned char *source,int len) |
10536 | 1542 { |
23423 | 1543 dest->framesize=len; |
23886 | 1544 if(priv->tv_param->automute>0){ |
23422 | 1545 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) >= 0) { |
23886 | 1546 if(priv->tv_param->automute<<8>priv->tuner.signal){ |
23423 | 1547 fill_blank_frame(dest->data,dest->framesize,fcc_vl2mp(priv->format.fmt.pix.pixelformat)); |
23422 | 1548 set_mute(priv,1); |
1549 return; | |
1550 } | |
1551 } | |
1552 set_mute(priv,0); | |
1553 } | |
23423 | 1554 memcpy(dest->data, source, len); |
10536 | 1555 } |
1556 | |
1557 // maximum skew change, in frames | |
1558 #define MAX_SKEW_DELTA 0.6 | |
1559 static void *video_grabber(void *data) | |
1560 { | |
1561 priv_t *priv = (priv_t*)data; | |
15449 | 1562 long long skew, prev_skew, xskew, interval, prev_interval, delta; |
10536 | 1563 int i; |
23423 | 1564 int framesize = priv->format.fmt.pix.sizeimage; |
10536 | 1565 fd_set rdset; |
1566 struct timeval timeout; | |
1567 struct v4l2_buffer buf; | |
1568 | |
1569 xskew = 0; | |
1570 skew = 0; | |
1571 interval = 0; | |
1572 prev_interval = 0; | |
1573 prev_skew = 0; | |
1574 | |
1575 mp_msg(MSGT_TV, MSGL_V, "%s: going to capture\n", info.short_name); | |
1576 if (ioctl(priv->video_fd, VIDIOC_STREAMON, &(priv->format.type)) < 0) { | |
23151 | 1577 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamon failed: %s\n", |
1578 info.short_name, strerror(errno)); | |
1579 return 0; | |
10536 | 1580 } |
1581 priv->streamon = 1; | |
1582 | |
23886 | 1583 if (!priv->tv_param->noaudio) { |
23151 | 1584 pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv); |
10536 | 1585 } |
1586 | |
1587 for (priv->frames = 0; !priv->shutdown;) | |
1588 { | |
23151 | 1589 int ret; |
1590 | |
1591 if (priv->immediate_mode) { | |
1592 while (priv->video_cnt == priv->video_buffer_size_max) { | |
1593 usleep(10000); | |
1594 if (priv->shutdown) { | |
1595 return NULL; | |
1596 } | |
1597 } | |
1598 } | |
1599 | |
1600 FD_ZERO (&rdset); | |
1601 FD_SET (priv->video_fd, &rdset); | |
10536 | 1602 |
23151 | 1603 timeout.tv_sec = 1; |
1604 timeout.tv_usec = 0; | |
10536 | 1605 |
23151 | 1606 i = select(priv->video_fd + 1, &rdset, NULL, NULL, &timeout); |
1607 if (i < 0) { | |
1608 mp_msg(MSGT_TV, MSGL_ERR, "%s: select failed: %s\n", | |
1609 info.short_name, strerror(errno)); | |
1610 continue; | |
1611 } | |
1612 else if (i == 0) { | |
1613 mp_msg(MSGT_TV, MSGL_ERR, "%s: select timeout\n", info.short_name); | |
1614 continue; | |
1615 } | |
1616 else if (!FD_ISSET(priv->video_fd, &rdset)) { | |
1617 continue; | |
1618 } | |
10536 | 1619 |
23151 | 1620 memset(&buf,0,sizeof(buf)); |
1621 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1622 buf.memory = V4L2_MEMORY_MMAP; | |
1623 ret = ioctl(priv->video_fd, VIDIOC_DQBUF, &buf); | |
10536 | 1624 |
23151 | 1625 if (ret < 0) { |
1626 /* | |
1627 if there's no signal, the buffer might me dequeued | |
1628 so we query all the buffers to see which one we should | |
1629 put back to queue | |
10536 | 1630 |
23151 | 1631 observed with saa7134 0.2.8 |
1632 don't know if is it a bug or (mis)feature | |
1633 */ | |
1634 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl dequeue buffer failed: %s, idx = %d\n", | |
1635 info.short_name, strerror(errno), buf.index); | |
1636 for (i = 0; i < priv->mapcount; i++) { | |
1637 memset(&buf,0,sizeof(buf)); | |
1638 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1639 buf.memory = V4L2_MEMORY_MMAP; | |
1640 buf.index = i; | |
1641 ret = ioctl(priv->video_fd, VIDIOC_QUERYBUF, &buf); | |
1642 if (ret < 0) { | |
1643 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s, idx = %d\n", | |
1644 info.short_name, strerror(errno), buf.index); | |
1645 return 0; | |
1646 } | |
1647 if ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE)) == V4L2_BUF_FLAG_MAPPED) { | |
1648 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { | |
1649 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1650 info.short_name, strerror(errno)); | |
1651 return 0; | |
1652 } | |
1653 } | |
1654 } | |
1655 continue; | |
1656 } | |
10536 | 1657 |
23151 | 1658 /* store the timestamp of the very first frame as reference */ |
1659 if (!priv->frames++) { | |
23886 | 1660 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1661 priv->first_frame = (long long)1e6*buf.timestamp.tv_sec + buf.timestamp.tv_usec; |
23886 | 1662 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1663 } |
1664 priv->curr_frame = (long long)buf.timestamp.tv_sec*1e6+buf.timestamp.tv_usec; | |
1665 // fprintf(stderr, "idx = %d, ts = %lf\n", buf.index, (double)(priv->curr_frame) / 1e6); | |
10536 | 1666 |
23151 | 1667 interval = priv->curr_frame - priv->first_frame; |
1668 delta = interval - prev_interval; | |
10536 | 1669 |
23151 | 1670 if (!priv->immediate_mode) { |
1671 // interpolate the skew in time | |
23886 | 1672 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1673 xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor; |
23886 | 1674 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1675 // correct extreme skew changes to avoid (especially) moving backwards in time |
1676 if (xskew - prev_skew > delta*MAX_SKEW_DELTA) { | |
1677 skew = prev_skew + delta*MAX_SKEW_DELTA; | |
1678 } else if (xskew - prev_skew < -delta*MAX_SKEW_DELTA) { | |
1679 skew = prev_skew - delta*MAX_SKEW_DELTA; | |
1680 } else { | |
1681 skew = xskew; | |
1682 } | |
1683 } | |
10536 | 1684 |
23151 | 1685 mp_msg(MSGT_TV, MSGL_DBG3, "\nfps = %lf, interval = %lf, a_skew = %f, corr_skew = %f\n", |
1686 delta ? (double)1e6/delta : -1, | |
1687 (double)1e-6*interval, (double)1e-6*xskew, (double)1e-6*skew); | |
1688 mp_msg(MSGT_TV, MSGL_DBG3, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt); | |
10536 | 1689 |
23151 | 1690 prev_skew = skew; |
1691 prev_interval = interval; | |
10536 | 1692 |
23151 | 1693 /* allocate a new buffer, if needed */ |
1694 pthread_mutex_lock(&priv->video_buffer_mutex); | |
1695 if (priv->video_buffer_size_current < priv->video_buffer_size_max) { | |
1696 if (priv->video_cnt == priv->video_buffer_size_current) { | |
1697 unsigned char *newbuf = malloc(framesize); | |
1698 if (newbuf) { | |
1699 memmove(priv->video_ringbuffer+priv->video_tail+1, priv->video_ringbuffer+priv->video_tail, | |
23423 | 1700 (priv->video_buffer_size_current-priv->video_tail)*sizeof(video_buffer_entry)); |
1701 priv->video_ringbuffer[priv->video_tail].data = newbuf; | |
23151 | 1702 if ((priv->video_head >= priv->video_tail) && (priv->video_cnt > 0)) priv->video_head++; |
1703 priv->video_buffer_size_current++; | |
1704 } | |
1705 } | |
1706 } | |
1707 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
10536 | 1708 |
23151 | 1709 if (priv->video_cnt == priv->video_buffer_size_current) { |
1710 if (!priv->immediate_mode) { | |
1711 mp_msg(MSGT_TV, MSGL_ERR, "\nvideo buffer full - dropping frame\n"); | |
1712 if (priv->audio_insert_null_samples) { | |
1713 pthread_mutex_lock(&priv->audio_mutex); | |
1714 priv->dropped_frames_timeshift += delta; | |
1715 pthread_mutex_unlock(&priv->audio_mutex); | |
1716 } | |
1717 } | |
1718 } else { | |
1719 if (priv->immediate_mode) { | |
23423 | 1720 priv->video_ringbuffer[priv->video_tail].timestamp = 0; |
23151 | 1721 } else { |
1722 // compensate for audio skew | |
1723 // negative skew => there are more audio samples, increase interval | |
1724 // positive skew => less samples, shorten the interval | |
23423 | 1725 priv->video_ringbuffer[priv->video_tail].timestamp = interval - skew; |
1726 if (priv->audio_insert_null_samples && priv->video_ringbuffer[priv->video_tail].timestamp > 0) { | |
23151 | 1727 pthread_mutex_lock(&priv->audio_mutex); |
23423 | 1728 priv->video_ringbuffer[priv->video_tail].timestamp += |
23151 | 1729 (priv->audio_null_blocks_inserted |
1730 - priv->dropped_frames_timeshift/priv->audio_usecs_per_block) | |
1731 *priv->audio_usecs_per_block; | |
1732 pthread_mutex_unlock(&priv->audio_mutex); | |
1733 } | |
1734 } | |
23423 | 1735 copy_frame(priv, priv->video_ringbuffer+priv->video_tail, priv->map[buf.index].addr,buf.bytesused); |
23151 | 1736 priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size_current; |
1737 priv->video_cnt++; | |
1738 } | |
1739 if (ioctl(priv->video_fd, VIDIOC_QBUF, &buf) < 0) { | |
1740 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1741 info.short_name, strerror(errno)); | |
1742 return 0; | |
1743 } | |
10536 | 1744 } |
1745 return NULL; | |
1746 } | |
1747 | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1748 #define MAX_LOOP 50 |
10536 | 1749 static double grab_video_frame(priv_t *priv, char *buffer, int len) |
1750 { | |
1751 double interval; | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1752 int loop_cnt = 0; |
10536 | 1753 |
1754 if (priv->first) { | |
23151 | 1755 pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv); |
1756 priv->first = 0; | |
10536 | 1757 } |
1758 | |
1759 while (priv->video_cnt == 0) { | |
23151 | 1760 usleep(10000); |
1761 if (loop_cnt++ > MAX_LOOP) return 0; | |
10536 | 1762 } |
1763 | |
1764 pthread_mutex_lock(&priv->video_buffer_mutex); | |
23423 | 1765 interval = (double)priv->video_ringbuffer[priv->video_head].timestamp*1e-6; |
1766 memcpy(buffer, priv->video_ringbuffer[priv->video_head].data, len); | |
10536 | 1767 priv->video_cnt--; |
1768 priv->video_head = (priv->video_head+1)%priv->video_buffer_size_current; | |
1769 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
1770 | |
1771 return interval; | |
1772 } | |
1773 | |
1774 static int get_video_framesize(priv_t *priv) | |
1775 { | |
23423 | 1776 /* |
1777 this routine will be called before grab_video_frame | |
1778 thus let's return topmost frame's size | |
1779 */ | |
1780 if (priv->video_cnt) | |
1781 return priv->video_ringbuffer[priv->video_head].framesize; | |
1782 /* | |
1783 no video frames yet available. i don't know what to do in this case, | |
1784 thus let's return some fallback result (for compressed format this will be | |
1785 maximum allowed frame size. | |
1786 */ | |
10536 | 1787 return priv->format.fmt.pix.sizeimage; |
1788 } | |
1789 | |
10704 | 1790 //#define DOUBLESPEED |
1791 #ifdef DOUBLESPEED | |
10536 | 1792 // for testing purposes only |
1793 static void read_doublespeed(priv_t *priv) | |
1794 { | |
18885 | 1795 char *bufx = calloc(priv->audio_in.blocksize, 2); |
10536 | 1796 short *s; |
1797 short *d; | |
1798 int i; | |
1799 | |
1800 audio_in_read_chunk(&priv->audio_in, bufx); | |
1801 audio_in_read_chunk(&priv->audio_in, bufx+priv->audio_in.blocksize); | |
1802 | |
1803 s = bufx; | |
1804 d = priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize; | |
1805 for (i = 0; i < priv->audio_in.blocksize/2; i++) { | |
23151 | 1806 *d++ = *s++; |
1807 *s++; | |
10536 | 1808 } |
1809 | |
1810 } | |
10704 | 1811 #endif |
10536 | 1812 |
1813 static void *audio_grabber(void *data) | |
1814 { | |
1815 priv_t *priv = (priv_t*)data; | |
1816 struct timeval tv; | |
1817 int i, audio_skew_ptr = 0; | |
10653 | 1818 long long current_time, prev_skew = 0, prev_skew_uncorr = 0; |
10852 | 1819 long long start_time_avg; |
10536 | 1820 |
1821 gettimeofday(&tv, NULL); | |
10852 | 1822 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec; |
10536 | 1823 audio_in_start_capture(&priv->audio_in); |
1824 for (i = 0; i < priv->aud_skew_cnt; i++) | |
23151 | 1825 priv->audio_skew_buffer[i] = 0; |
10653 | 1826 for (i = 0; i < priv->aud_skew_cnt; i++) |
23151 | 1827 priv->audio_skew_delta_buffer[i] = 0; |
10536 | 1828 |
1829 for (; !priv->shutdown;) | |
1830 { | |
10704 | 1831 #ifdef DOUBLESPEED |
23151 | 1832 read_doublespeed(priv); |
10704 | 1833 #else |
23151 | 1834 if (audio_in_read_chunk(&priv->audio_in, priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize) < 0) |
1835 continue; | |
10704 | 1836 #endif |
23151 | 1837 pthread_mutex_lock(&priv->skew_mutex); |
1838 if (priv->first_frame == 0) { | |
1839 // there is no first frame yet (unlikely to happen) | |
1840 gettimeofday(&tv, NULL); | |
1841 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec; | |
1842 // fprintf(stderr, "warning - first frame not yet available!\n"); | |
1843 pthread_mutex_unlock(&priv->skew_mutex); | |
1844 continue; | |
1845 } | |
1846 pthread_mutex_unlock(&priv->skew_mutex); | |
10536 | 1847 |
23151 | 1848 gettimeofday(&tv, NULL); |
10536 | 1849 |
23151 | 1850 priv->audio_recv_blocks_total++; |
1851 current_time = (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_start_time; | |
10536 | 1852 |
23151 | 1853 if (priv->audio_recv_blocks_total < priv->aud_skew_cnt*2) { |
1854 start_time_avg += (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; | |
1855 priv->audio_start_time = start_time_avg/(priv->audio_recv_blocks_total+1); | |
1856 } | |
10852 | 1857 |
23151 | 1858 // fprintf(stderr, "spb = %lf, bs = %d, skew = %lf\n", priv->audio_secs_per_block, priv->audio_in.blocksize, |
1859 // (double)(current_time - 1e6*priv->audio_secs_per_block*priv->audio_recv_blocks_total)/1e6); | |
10536 | 1860 |
23151 | 1861 // put the current skew into the ring buffer |
1862 priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr]; | |
1863 priv->audio_skew_buffer[audio_skew_ptr] = current_time | |
1864 - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; | |
1865 priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr]; | |
10536 | 1866 |
23151 | 1867 pthread_mutex_lock(&priv->skew_mutex); |
10704 | 1868 |
23151 | 1869 // skew calculation |
10704 | 1870 |
23151 | 1871 // compute the sliding average of the skews |
1872 if (priv->audio_recv_blocks_total > priv->aud_skew_cnt) { | |
1873 priv->audio_skew = priv->audio_skew_total/priv->aud_skew_cnt; | |
1874 } else { | |
1875 priv->audio_skew = priv->audio_skew_total/priv->audio_recv_blocks_total; | |
1876 } | |
10653 | 1877 |
23151 | 1878 // put the current skew change (skew-prev_skew) into the ring buffer |
1879 priv->audio_skew_delta_total -= priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1880 priv->audio_skew_delta_buffer[audio_skew_ptr] = priv->audio_skew - prev_skew_uncorr; | |
1881 priv->audio_skew_delta_total += priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1882 prev_skew_uncorr = priv->audio_skew; // remember the _uncorrected_ average value | |
10704 | 1883 |
23151 | 1884 audio_skew_ptr = (audio_skew_ptr+1) % priv->aud_skew_cnt; // rotate the buffer pointer |
10653 | 1885 |
23151 | 1886 // sliding average approximates the value in the middle of the interval |
1887 // so interpolate the skew value further to the current time | |
1888 priv->audio_skew += priv->audio_skew_delta_total/2; | |
10653 | 1889 |
23151 | 1890 // now finally, priv->audio_skew contains fairly good approximation |
1891 // of the current value | |
10653 | 1892 |
23151 | 1893 // current skew factor (assuming linearity) |
1894 // used for further interpolation in video_grabber | |
1895 // probably overkill but seems to be necessary for | |
1896 // stress testing by dropping half of the audio frames ;) | |
1897 // especially when using ALSA with large block sizes | |
1898 // where audio_skew remains a long while behind | |
1899 if ((priv->audio_skew_measure_time != 0) && (current_time - priv->audio_skew_measure_time != 0)) { | |
1900 priv->audio_skew_factor = (double)(priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time); | |
1901 } else { | |
1902 priv->audio_skew_factor = 0.0; | |
1903 } | |
10852 | 1904 |
23151 | 1905 priv->audio_skew_measure_time = current_time; |
1906 prev_skew = priv->audio_skew; | |
1907 priv->audio_skew += priv->audio_start_time - priv->first_frame; | |
1908 pthread_mutex_unlock(&priv->skew_mutex); | |
1909 | |
1910 // fprintf(stderr, "audio_skew = %lf, delta = %lf\n", (double)priv->audio_skew/1e6, (double)priv->audio_skew_delta_total/1e6); | |
10851 | 1911 |
23151 | 1912 pthread_mutex_lock(&priv->audio_mutex); |
1913 if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) { | |
1914 mp_msg(MSGT_TV, MSGL_ERR, "\ntoo bad - dropping audio frame !\n"); | |
1915 priv->audio_drop++; | |
1916 } else { | |
1917 priv->audio_tail = (priv->audio_tail+1) % priv->audio_buffer_size; | |
1918 priv->audio_cnt++; | |
1919 } | |
1920 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1921 } |
1922 return NULL; | |
1923 } | |
1924 | |
1925 static double grab_audio_frame(priv_t *priv, char *buffer, int len) | |
1926 { | |
1927 mp_dbg(MSGT_TV, MSGL_DBG2, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n", | |
23151 | 1928 priv, buffer, len); |
10536 | 1929 |
15449 | 1930 // hack: if grab_audio_frame is called first, it means we are used by mplayer |
1931 // => switch to the mode which outputs audio immediately, even if | |
1932 // it should be silence | |
1933 if (priv->first) priv->audio_insert_null_samples = 1; | |
1934 | |
1935 pthread_mutex_lock(&priv->audio_mutex); | |
1936 while (priv->audio_insert_null_samples | |
23151 | 1937 && priv->dropped_frames_timeshift - priv->dropped_frames_compensated >= priv->audio_usecs_per_block) { |
1938 // some frames were dropped - drop the corresponding number of audio blocks | |
1939 if (priv->audio_drop) { | |
1940 priv->audio_drop--; | |
1941 } else { | |
1942 if (priv->audio_head == priv->audio_tail) break; | |
1943 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
1944 } | |
1945 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
|
1946 } |
80402283a017
Fix immediatemode with mplayer (ie playing both sound and video)
albeu
parents:
10735
diff
changeset
|
1947 |
10536 | 1948 // compensate for dropped audio frames |
1949 if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) { | |
23151 | 1950 priv->audio_drop--; |
1951 memset(buffer, 0, len); | |
1952 goto out; | |
10536 | 1953 } |
1954 | |
15449 | 1955 if (priv->audio_insert_null_samples && (priv->audio_head == priv->audio_tail)) { |
23151 | 1956 // return silence to avoid desync and stuttering |
1957 memset(buffer, 0, len); | |
1958 priv->audio_null_blocks_inserted++; | |
1959 goto out; | |
15449 | 1960 } |
1961 | |
1962 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1963 while (priv->audio_head == priv->audio_tail) { |
23151 | 1964 // this is mencoder => just wait until some audio is available |
1965 usleep(10000); | |
10536 | 1966 } |
15451 | 1967 pthread_mutex_lock(&priv->audio_mutex); |
10536 | 1968 memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_in.blocksize, len); |
1969 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
1970 priv->audio_cnt--; | |
15449 | 1971 out: |
1972 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1973 priv->audio_sent_blocks_total++; |
1974 return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block; | |
1975 } | |
1976 | |
1977 static int get_audio_framesize(priv_t *priv) | |
1978 { | |
26756
c43ce7268677
cosmetics: Remove useless parentheses from return statements.
diego
parents:
25962
diff
changeset
|
1979 return priv->audio_in.blocksize; |
10536 | 1980 } |