1 /*
2 * next generation[tm] xawtv capture interfaces
3 *
4 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org>
5 *
6 */
7
8 #include <pthread.h>
9 #include <sys/types.h>
10
11 #include "devices.h"
12 #include "list.h"
13
14 extern int ng_debug;
15 extern int ng_chromakey;
16 extern int ng_jpeg_quality;
17 extern int ng_ratio_x;
18 extern int ng_ratio_y;
19 extern char ng_v4l_conf[256];
20
21 #define BUG_ON(condition,message) if (condition) {\
22 fprintf(stderr,"BUG: %s [%s:%d]\n",\
23 message,__FILE__,__LINE__);\
24 exit(1);}
25
26 #if __STDC_VERSION__ < 199901
27 # define restrict
28 # define bool int
29 #endif
30
31 #define UNSET (-1U)
32 #define DIMOF(array) (sizeof(array)/sizeof(array[0]))
33 #define SDIMOF(array) ((signed int)(sizeof(array)/sizeof(array[0])))
34 #define GETELEM(array,index,default) \
35 (index < sizeof(array)/sizeof(array[0]) ? array[index] : default)
36
37
38 /* --------------------------------------------------------------------- */
39 /* defines */
40
41 #define VIDEO_NONE 0
42 #define VIDEO_RGB08 1 /* bt848 dithered */
43 #define VIDEO_GRAY 2
44 #define VIDEO_RGB15_LE 3 /* 15 bpp little endian */
45 #define VIDEO_RGB16_LE 4 /* 16 bpp little endian */
46 #define VIDEO_RGB15_BE 5 /* 15 bpp big endian */
47 #define VIDEO_RGB16_BE 6 /* 16 bpp big endian */
48 #define VIDEO_BGR24 7 /* bgrbgrbgrbgr (LE) */
49 #define VIDEO_BGR32 8 /* bgr-bgr-bgr- (LE) */
50 #define VIDEO_RGB24 9 /* rgbrgbrgbrgb (BE) */
51 #define VIDEO_RGB32 10 /* -rgb-rgb-rgb (BE) */
52 #define VIDEO_LUT2 11 /* lookup-table 2 byte depth */
53 #define VIDEO_LUT4 12 /* lookup-table 4 byte depth */
54 #define VIDEO_YUYV 13 /* 4:2:2 */
55 #define VIDEO_YUV422P 14 /* YUV 4:2:2 (planar) */
56 #define VIDEO_YUV420P 15 /* YUV 4:2:0 (planar) */
57 #define VIDEO_MJPEG 16 /* MJPEG (AVI) */
58 #define VIDEO_JPEG 17 /* JPEG (JFIF) */
59 #define VIDEO_UYVY 18 /* 4:2:2 */
60 #define VIDEO_FMT_COUNT 19
61
62 #define AUDIO_NONE 0
63 #define AUDIO_U8_MONO 1
64 #define AUDIO_U8_STEREO 2
65 #define AUDIO_S16_LE_MONO 3
66 #define AUDIO_S16_LE_STEREO 4
67 #define AUDIO_S16_BE_MONO 5
68 #define AUDIO_S16_BE_STEREO 6
69 #define AUDIO_MP3 7
70 #define AUDIO_FMT_COUNT 8
71
72 #if BYTE_ORDER == BIG_ENDIAN
73 # define AUDIO_S16_NATIVE_MONO AUDIO_S16_BE_MONO
74 # define AUDIO_S16_NATIVE_STEREO AUDIO_S16_BE_STEREO
75 # define VIDEO_RGB15_NATIVE VIDEO_RGB15_BE
76 # define VIDEO_RGB16_NATIVE VIDEO_RGB16_BE
77 #endif
78 #if BYTE_ORDER == LITTLE_ENDIAN
79 # define AUDIO_S16_NATIVE_MONO AUDIO_S16_LE_MONO
80 # define AUDIO_S16_NATIVE_STEREO AUDIO_S16_LE_STEREO
81 # define VIDEO_RGB15_NATIVE VIDEO_RGB15_LE
82 # define VIDEO_RGB16_NATIVE VIDEO_RGB16_LE
83 #endif
84
85 #define ATTR_TYPE_INTEGER 1 /* range 0 - 65535 */
86 #define ATTR_TYPE_CHOICE 2 /* multiple choice */
87 #define ATTR_TYPE_BOOL 3 /* yes/no */
88
89 #define ATTR_ID_NORM 1
90 #define ATTR_ID_INPUT 2
91 #define ATTR_ID_VOLUME 3
92 #define ATTR_ID_MUTE 4
93 #define ATTR_ID_AUDIO_MODE 5
94 #define ATTR_ID_COLOR 6
95 #define ATTR_ID_BRIGHT 7
96 #define ATTR_ID_HUE 8
97 #define ATTR_ID_CONTRAST 9
98 #define ATTR_ID_COUNT 10
99
100 #define CAN_OVERLAY 1
101 #define CAN_CAPTURE 2
102 #define CAN_TUNE 4
103 #define NEEDS_CHROMAKEY 8
104
105 /* --------------------------------------------------------------------- */
106
107 extern const unsigned int ng_vfmt_to_depth[VIDEO_FMT_COUNT];
108 extern const char* ng_vfmt_to_desc[VIDEO_FMT_COUNT];
109
110 extern const unsigned int ng_afmt_to_channels[AUDIO_FMT_COUNT];
111 extern const unsigned int ng_afmt_to_bits[AUDIO_FMT_COUNT];
112 extern const char* ng_afmt_to_desc[AUDIO_FMT_COUNT];
113
114 extern const char* ng_attr_to_desc[ATTR_ID_COUNT];
115
116 /* --------------------------------------------------------------------- */
117
118 struct STRTAB {
119 long nr;
120 const char *str;
121 };
122
123 struct OVERLAY_CLIP {
124 int x1,x2,y1,y2;
125 };
126
127 /* --------------------------------------------------------------------- */
128 /* video data structures */
129
130 struct ng_video_fmt {
131 unsigned int fmtid; /* VIDEO_* */
132 unsigned int width;
133 unsigned int height;
134 unsigned int bytesperline; /* zero for compressed formats */
135 };
136
137 struct ng_video_buf {
138 struct ng_video_fmt fmt;
139 size_t size;
140 unsigned char *data;
141
142 /* meta info for frame */
143 struct {
144 int64_t ts; /* time stamp */
145 int seq;
146 int twice;
147 } info;
148
149 /*
150 * the lock is for the reference counter.
151 * if the reference counter goes down to zero release()
152 * should be called. priv is for the owner of the
153 * buffer (can be used by the release callback)
154 */
155 pthread_mutex_t lock;
156 pthread_cond_t cond;
157 int refcount;
158 void (*release)(struct ng_video_buf *buf);
159 void *priv;
160 };
161
162 void ng_init_video_buf(struct ng_video_buf *buf);
163 void ng_release_video_buf(struct ng_video_buf *buf);
164 struct ng_video_buf* ng_malloc_video_buf(struct ng_video_fmt *fmt,
165 int size);
166 void ng_wakeup_video_buf(struct ng_video_buf *buf);
167 void ng_waiton_video_buf(struct ng_video_buf *buf);
168
169
170 /* --------------------------------------------------------------------- */
171 /* audio data structures */
172
173 struct ng_audio_fmt {
174 unsigned int fmtid; /* AUDIO_* */
175 unsigned int rate;
176 };
177
178 struct ng_audio_buf {
179 struct ng_audio_fmt fmt;
180 int size;
181 int written; /* for partial writes */
182 char *data;
183
184 struct {
185 int64_t ts;
186 } info;
187 };
188
189 struct ng_audio_buf* ng_malloc_audio_buf(struct ng_audio_fmt *fmt,
190 int size);
191
192 /* --------------------------------------------------------------------- */
193 /* someone who receives video and/or audio data (writeavi, ...) */
194
195 struct ng_format_list {
196 char *name;
197 char *desc; /* if standard fmtid description doesn't work
198 because it's converted somehow */
199 char *ext;
200 unsigned int fmtid;
201 void *priv;
202 };
203
204 struct ng_writer {
205 const char *name;
206 const char *desc;
207 const struct ng_format_list *video;
208 const struct ng_format_list *audio;
209 const int combined; /* both audio + video in one file */
210
211 void* (*wr_open)(char *moviename, char *audioname,
212 struct ng_video_fmt *video, const void *priv_video, int fps,
213 struct ng_audio_fmt *audio, const void *priv_audio);
214 int (*wr_video)(void *handle, struct ng_video_buf *buf);
215 int (*wr_audio)(void *handle, struct ng_audio_buf *buf);
216 int (*wr_close)(void *handle);
217
218 struct list_head list;
219 };
220
221 struct ng_reader {
222 const char *name;
223 const char *desc;
224
225 char *magic[4];
226 int moff[4];
227 int mlen[4];
228
229 void* (*rd_open)(char *moviename);
230 struct ng_video_fmt* (*rd_vfmt)(void *handle, int *vfmt, int vn);
231 struct ng_audio_fmt* (*rd_afmt)(void *handle);
232 struct ng_video_buf* (*rd_vdata)(void *handle, unsigned int drop);
233 struct ng_audio_buf* (*rd_adata)(void *handle);
234 int64_t (*frame_time)(void *handle);
235 int (*rd_close)(void *handle);
236
237 struct list_head list;
238 };
239
240
241 /* --------------------------------------------------------------------- */
242 /* attributes */
243
244 struct ng_attribute {
245 int id;
246 const char *name;
247 int type;
248 int defval;
249 struct STRTAB *choices; /* ATTR_TYPE_CHOICE */
250 int min,max; /* ATTR_TYPE_INTEGER */
251 int points; /* ATTR_TYPE_INTEGER -- fixed point */
252 const void *priv;
253 void *handle;
254 int (*read)(struct ng_attribute*);
255 void (*write)(struct ng_attribute*, int val);
256 };
257
258 struct ng_attribute* ng_attr_byid(struct ng_attribute *attrs, int id);
259 struct ng_attribute* ng_attr_byname(struct ng_attribute *attrs, char *name);
260 const char* ng_attr_getstr(struct ng_attribute *attr, int value);
261 int ng_attr_getint(struct ng_attribute *attr, char *value);
262 void ng_attr_listchoices(struct ng_attribute *attr);
263 int ng_attr_int2percent(struct ng_attribute *attr, int value);
264 int ng_attr_percent2int(struct ng_attribute *attr, int percent);
265 int ng_attr_parse_int(struct ng_attribute *attr, char *str);
266
267 /* --------------------------------------------------------------------- */
268
269 void ng_ratio_fixup(int *width, int *height, int *xoff, int *yoff);
270 void ng_ratio_fixup2(int *width, int *height, int *xoff, int *yoff,
271 int ratio_x, int ratio_y, int up);
272
273 /* --------------------------------------------------------------------- */
274 /* device informations */
275
276 struct ng_devinfo {
277 char device[32];
278 char name[64];
279 int flags;
280 };
281
282 /* --------------------------------------------------------------------- */
283 /* capture/overlay interface driver */
284
285 struct ng_vid_driver {
286 const char *name;
287
288 /* open/close */
289 void* (*open)(char *device);
290 int (*close)(void *handle);
291
292 /* attributes */
293 char* (*get_devname)(void *handle);
294 int (*capabilities)(void *handle);
295 struct ng_attribute* (*list_attrs)(void *handle);
296
297 /* overlay */
298 int (*setupfb)(void *handle, struct ng_video_fmt *fmt, void *base);
299 int (*overlay)(void *handle, struct ng_video_fmt *fmt, int x, int y,
300 struct OVERLAY_CLIP *oc, int count, int aspect);
301
302 /* capture */
303 int (*setformat)(void *handle, struct ng_video_fmt *fmt);
304 int (*startvideo)(void *handle, int fps, unsigned int buffers);
305 void (*stopvideo)(void *handle);
306 struct ng_video_buf* (*nextframe)(void *handle); /* video frame */
307 struct ng_video_buf* (*getimage)(void *handle); /* single image */
308
309 /* tuner */
310 unsigned long (*getfreq)(void *handle);
311 void (*setfreq)(void *handle, unsigned long freq);
312 int (*is_tuned)(void *handle);
313
314 struct list_head list;
315 };
316
317
318 /* --------------------------------------------------------------------- */
319 /* sound driver */
320
321 struct ng_dsp_driver {
322 const char *name;
323 void* (*open)(char *device, struct ng_audio_fmt *fmt,
324 int record);
325 void (*close)(void *handle);
326 int (*fd)(void *handle);
327 int (*startrec)(void *handle);
328 struct ng_audio_buf* (*read)(void *handle, int64_t stopby);
329 struct ng_audio_buf* (*write)(void *handle, struct ng_audio_buf *buf);
330 int64_t (*latency)(void *handle);
331
332 struct list_head list;
333 };
334
335 struct ng_mix_driver {
336 const char *name;
337 struct ng_devinfo* (*probe)(void);
338 struct ng_devinfo* (*channels)(char *device);
339 void* (*open)(char *device);
340 struct ng_attribute* (*volctl)(void *handle, char *channel);
341 void (*close)(void *handle);
342
343 struct list_head list;
344 };
345
346
347 /* --------------------------------------------------------------------- */
348 /* color space converters */
349
350 struct ng_video_conv {
351 unsigned int fmtid_in;
352 unsigned int fmtid_out;
353 void* (*init)(struct ng_video_fmt *out,
354 void *priv);
355 void (*frame)(void *handle,
356 struct ng_video_buf *out,
357 struct ng_video_buf *in);
358 void (*fini)(void *handle);
359 void *priv;
360
361 struct list_head list;
362 };
363
364 struct ng_convert_handle {
365 struct ng_video_fmt ifmt;
366 struct ng_video_fmt ofmt;
367 int isize;
368 int osize;
369 struct ng_video_conv *conv;
370 void *chandle;
371 };
372
373 struct ng_convert_handle* ng_convert_alloc(struct ng_video_conv *conv,
374 struct ng_video_fmt *i,
375 struct ng_video_fmt *o);
376 void ng_convert_init(struct ng_convert_handle *h);
377 struct ng_video_buf* ng_convert_frame(struct ng_convert_handle *h,
378 struct ng_video_buf *dest,
379 struct ng_video_buf *buf);
380 void ng_convert_fini(struct ng_convert_handle *h);
381 struct ng_video_buf* ng_convert_single(struct ng_convert_handle *h,
382 struct ng_video_buf *in);
383
384 /* --------------------------------------------------------------------- */
385 /* audio converters */
386
387 struct ng_audio_conv {
388 unsigned int fmtid_in;
389 unsigned int fmtid_out;
390 void* (*init)(void *priv);
391 struct ng_audio_buf* (*frame)(void *handle,
392 struct ng_audio_buf *in);
393 void (*fini)(void *handle);
394 void *priv;
395
396 struct list_head list;
397 };
398
399 /* --------------------------------------------------------------------- */
400 /* filters */
401
402 struct ng_filter {
403 char *name;
404 int fmts;
405 struct ng_attribute* attrs;
406 void* (*init)(struct ng_video_fmt *fmt);
407 struct ng_video_buf* (*frame)(void *handle,
408 struct ng_video_buf *in);
409 void (*fini)(void *handle);
410
411 struct list_head list;
412 };
413
414 /* --------------------------------------------------------------------- */
415
416 /* must be changed if we break compatibility */
417 #define NG_PLUGIN_MAGIC 0x20030129
418
419 extern struct list_head ng_conv;
420 extern struct list_head ng_aconv;
421 extern struct list_head ng_filters;
422 extern struct list_head ng_writers;
423 extern struct list_head ng_readers;
424 extern struct list_head ng_vid_drivers;
425 extern struct list_head ng_dsp_drivers;
426 extern struct list_head ng_mix_drivers;
427
428 int ng_conv_register(int magic, char *plugname,
429 struct ng_video_conv *list, int count);
430 int ng_aconv_register(int magic, char *plugname,
431 struct ng_audio_conv *list, int count);
432 int ng_filter_register(int magic, char *plugname,
433 struct ng_filter *filter);
434 int ng_writer_register(int magic, char *plugname,
435 struct ng_writer *writer);
436 int ng_reader_register(int magic, char *plugname,
437 struct ng_reader *reader);
438 int ng_vid_driver_register(int magic, char *plugname,
439 struct ng_vid_driver *driver);
440 int ng_dsp_driver_register(int magic, char *plugname,
441 struct ng_dsp_driver *driver);
442 int ng_mix_driver_register(int magic, char *plugname,
443 struct ng_mix_driver *driver);
444
445 struct ng_video_conv* ng_conv_find_to(unsigned int out, int *i);
446 struct ng_video_conv* ng_conv_find_from(unsigned int out, int *i);
447 struct ng_video_conv* ng_conv_find_match(unsigned int in, unsigned int out);
448
449 const struct ng_vid_driver* ng_vid_open(char *device, char *driver,
450 struct ng_video_fmt *screen,
451 void *base, void **handle);
452 const struct ng_dsp_driver* ng_dsp_open(char *device, struct ng_audio_fmt *fmt,
453 int record, void **handle);
454 struct ng_attribute* ng_mix_init(char *device, char *channel);
455 struct ng_reader* ng_find_reader(char *filename);
456
457 int64_t ng_tofday_to_timestamp(struct timeval *tv);
458 int64_t ng_get_timestamp(void);
459 void ng_check_clipping(int width, int height, int xadjust, int yadjust,
460 struct OVERLAY_CLIP *oc, int *count);
461 struct ng_video_buf* ng_filter_single(struct ng_filter *filter,
462 struct ng_video_buf *in);
463
464 /* --------------------------------------------------------------------- */
465
466 void ng_init(void);
467 void ng_lut_init(unsigned long red_mask, unsigned long green_mask,
468 unsigned long blue_mask, unsigned int fmtid, int swap);
469
470 void ng_rgb24_to_lut2(unsigned char *dest, unsigned char *src, int p);
471 void ng_rgb24_to_lut4(unsigned char *dest, unsigned char *src, int p);
472
473 /* --------------------------------------------------------------------- */
474 /* internal stuff starts here */
475
476 #ifdef NG_PRIVATE
477
478 /* init functions */
479 void ng_color_packed_init(void);
480 void ng_color_yuv2rgb_init(void);
481 void ng_writefile_init(void);
482
483 /* for yuv2rgb using lookup tables (color_lut.c, color_yuv2rgb.c) */
484 unsigned long ng_lut_red[256];
485 unsigned long ng_lut_green[256];
486 unsigned long ng_lut_blue[256];
487 void ng_yuv422_to_lut2(unsigned char *dest, unsigned char *s, int p);
488 void ng_yuv422_to_lut4(unsigned char *dest, unsigned char *s, int p);
489 void ng_yuv420p_to_lut2(void *h, struct ng_video_buf *out,
490 struct ng_video_buf *in);
491 void ng_yuv420p_to_lut4(void *h, struct ng_video_buf *out,
492 struct ng_video_buf *in);
493 void ng_yuv422p_to_lut2(void *h, struct ng_video_buf *out,
494 struct ng_video_buf *in);
495 void ng_yuv422p_to_lut4(void *h, struct ng_video_buf *out,
496 struct ng_video_buf *in);
497
498 /* color_common.c stuff */
499 void* ng_packed_init(struct ng_video_fmt *out, void *priv);
500 void ng_packed_frame(void *handle, struct ng_video_buf *out,
501 struct ng_video_buf *in);
502 void* ng_conv_nop_init(struct ng_video_fmt *out, void *priv);
503 void ng_conv_nop_fini(void *handle);
504
505 #define NG_GENERIC_PACKED \
506 init: ng_packed_init, \
507 frame: ng_packed_frame, \
508 fini: ng_conv_nop_fini
509
510 #endif /* NG_PRIVATE */
511
512 /* --------------------------------------------------------------------- */
513 /*
514 * Local variables:
515 * compile-command: "(cd ..; make)"
516 * End:
517 */
518
|
This page was automatically generated by the
LXR engine.
|