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