annotate libmpdemux/tvi_v4l2.c @ 16823:850947f65258

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