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