Increase probe buffer size to 32kB, this makes ac3 auto-detection far more reliable.
[mplayer/glamo.git] / vidix / ivtv_vid.c
blobd190894aa3ee2d2a6f1e8ac9a6c7a31e0bbc2731
1 /*
2 * VIDIX driver for Hauppauge PVR 350
4 * Copyright 2007 Lutz Koschorreck
5 * Based on genfb_vid.c and ivtv_xv.c
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <inttypes.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <linux/types.h>
34 #include <linux/version.h>
35 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
36 #include <linux/videodev2.h>
37 #endif
38 #include <linux/ivtv.h>
39 #include <linux/fb.h>
41 #include "vidix.h"
42 #include "fourcc.h"
43 #include "dha.h"
44 #include "pci_ids.h"
45 #include "pci_names.h"
47 #define VIDIX_STATIC ivtv_
49 #define IVTV_MSG "[ivtv-vid] "
50 #define MAXLINE 128
51 #define IVTVMAXWIDTH 720
52 #define IVTVMAXHEIGHT 576
54 static int fbdev = -1;
55 static int yuvdev = -1;
56 static void *memBase = NULL;
57 static int frameSize = 0;
58 static int probed = 0;
59 static int ivtv_verbose;
60 static vidix_rect_t destVideo;
61 static vidix_rect_t srcVideo;
62 static unsigned char *outbuf = NULL;
63 double fb_width;
64 double fb_height;
65 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
66 static struct ivtvfb_ioctl_state_info fb_state_old;
67 static struct ivtvfb_ioctl_state_info fb_state_hide;
68 #else
69 struct v4l2_format format_old, format_hide;
70 #endif
71 int alpha_disable = 0;
73 /* VIDIX exports */
75 static vidix_capability_t ivtv_cap =
77 "Hauppauge PVR 350 YUV Video",
78 "Lutz Koschorreck",
79 TYPE_OUTPUT,
80 { 0, 0, 0, 0 },
81 IVTVMAXWIDTH,
82 IVTVMAXHEIGHT,
85 -1,
86 FLAG_UPSCALER|FLAG_DOWNSCALER,
87 -1,
88 -1,
89 { 0, 0, 0, 0 }
92 static void de_macro_y(unsigned char *src, unsigned char *dst,
93 unsigned int w, unsigned int h, int src_x, int src_y, int height, int width)
95 unsigned int x, y, i;
96 unsigned char *dst_2;
97 unsigned int h_tail, w_tail;
98 unsigned int h_size, w_size;
100 // Always round the origin, but compensate by increasing the size
101 if (src_x & 15) {
102 w += src_x & 15;
103 src_x &= ~15;
106 if (src_y & 15) {
107 h += src_y & 15;
108 src_y &= ~15;
111 // The right / bottom edge might not be a multiple of 16
112 h_tail = h & 15;
113 w_tail = w & 15;
115 // One block is 16 pixels high
116 h_size = 16;
118 // descramble Y plane
119 for (y = 0; y < h; y += 16) {
121 // Clip if we've reached the bottom & the size isn't a multiple of 16
122 if (y + 16 > h) h_size = h_tail;
124 for (x = 0; x < w; x += 16) {
125 if (x + 16 > w)
126 w_size = w_tail;
127 else
128 w_size = 16;
130 dst_2 = dst + (720 * y) + (720 * src_y) + (256 * (src_x>>4)) + (x * 16);
132 for (i = 0; i < h_size; i++) {
133 memcpy(dst_2, src + src_x + x + (y + i) * width + (src_y * width), w_size);
134 dst_2 += 16;
140 static void de_macro_uv(unsigned char *srcu, unsigned char *srcv,
141 unsigned char *dst, unsigned int w, unsigned int h, int src_x, int src_y,
142 int height, int width)
144 unsigned int x, y, i, f;
145 unsigned char *dst_2;
146 unsigned int h_tail, w_tail;
147 unsigned int h_size;
149 // The uv plane is half the size of the y plane, so 'correct' all dimensions.
150 w /= 2;
151 h /= 2;
152 src_x /= 2;
153 src_y /= 2;
154 height /= 2;
155 width /= 2;
157 // Always round the origin, but compensate by increasing the size
158 if (src_x & 7) {
159 w += src_x & 7;
160 src_x &= ~7;
163 if (src_y & 15) {
164 h += src_y & 15;
165 src_y &= ~15;
168 // The right / bottom edge may not be a multiple of 16
169 h_tail = h & 15;
170 w_tail = w & 7;
172 h_size = 16;
174 // descramble U/V plane
175 for (y = 0; y < h; y += 16) {
176 if ( y + 16 > h ) h_size = h_tail;
177 for (x = 0; x < w; x += 8) {
178 dst_2 = dst + (720 * y) + (720 * src_y) + (256 * (src_x>>3)) + (x * 32);
179 if (x + 8 <= w) {
180 for (i = 0; i < h_size; i++) {
181 int idx = src_x + x + ((y + i) * width) + (src_y * width);
182 dst_2[0] = srcu[idx + 0];
183 dst_2[1] = srcv[idx + 0];
184 dst_2[2] = srcu[idx + 1];
185 dst_2[3] = srcv[idx + 1];
186 dst_2[4] = srcu[idx + 2];
187 dst_2[5] = srcv[idx + 2];
188 dst_2[6] = srcu[idx + 3];
189 dst_2[7] = srcv[idx + 3];
190 dst_2[8] = srcu[idx + 4];
191 dst_2[9] = srcv[idx + 4];
192 dst_2[10] = srcu[idx + 5];
193 dst_2[11] = srcv[idx + 5];
194 dst_2[12] = srcu[idx + 6];
195 dst_2[13] = srcv[idx + 6];
196 dst_2[14] = srcu[idx + 7];
197 dst_2[15] = srcv[idx + 7];
198 dst_2 += 16;
201 else {
202 for (i = 0; i < h_size; i ++) {
203 int idx = src_x + x + ((y + i) * width) + (src_y * width);
204 for (f = 0; f < w_tail; f++) {
205 dst_2[0] = srcu[idx + f];
206 dst_2[1] = srcv[idx + f];
207 dst_2 += 2;
210 // Used for testing edge cutoff. Sets colour to Green
211 for (f = w_tail;f < 8;f ++) {
212 dst_2[0] = 0;
213 dst_2[1] = 0;
214 dst_2 += 2;
217 dst_2 += 16 - (w_tail << 1);
224 int ivtv_probe(int verbose, int force)
226 unsigned char fb_number = 0;
227 char *device_name = NULL;
228 char *alpha = NULL;
229 struct fb_var_screeninfo vinfo;
230 char fb_dev_name[] = "/dev/fb0\0";
231 pciinfo_t lst[MAX_PCI_DEVICES];
232 int err = 0;
233 unsigned int i, num_pci = 0;
235 if(verbose)
236 printf(IVTV_MSG"probe\n");
238 ivtv_verbose = verbose;
240 err = pci_scan(lst, &num_pci);
241 if(err) {
242 printf(IVTV_MSG"Error occured during pci scan: %s\n", strerror(err));
243 return err;
246 if(ivtv_verbose)
247 printf(IVTV_MSG"Found %d pci devices\n", num_pci);
249 for(i = 0; i < num_pci; i++) {
250 if(2 == ivtv_verbose)
251 printf(IVTV_MSG"Found chip [%04X:%04X] '%s' '%s'\n"
252 ,lst[i].vendor
253 ,lst[i].device
254 ,pci_vendor_name(lst[i].vendor)
255 ,pci_device_name(lst[i].vendor,lst[i].device));
256 if(VENDOR_INTERNEXT == lst[i].vendor) {
257 switch(lst[i].device)
259 case DEVICE_INTERNEXT_ITVC15_MPEG_2_ENCODER:
260 if(ivtv_verbose)
261 printf(IVTV_MSG"Found PVR 350\n");
262 goto card_found;
267 if(ivtv_verbose)
268 printf(IVTV_MSG"Can't find chip\n");
269 return ENXIO;
271 card_found:
273 device_name = getenv("FRAMEBUFFER");
274 if(NULL == device_name) {
275 device_name = fb_dev_name;
278 fb_number = atoi(device_name+strlen("/dev/fb"));
280 fbdev = open(device_name, O_RDWR);
281 if(-1 != fbdev) {
282 if(ioctl(fbdev, FBIOGET_VSCREENINFO, &vinfo) < 0) {
283 printf(IVTV_MSG"Unable to read screen info\n");
284 close(fbdev);
285 return ENXIO;
286 } else {
287 fb_width = vinfo.xres;
288 fb_height = vinfo.yres;
289 if(2 == ivtv_verbose) {
290 printf(IVTV_MSG"framebuffer width : %3.0f\n",fb_width);
291 printf(IVTV_MSG"framebuffer height: %3.0f\n",fb_height);
294 if(NULL != (alpha = getenv("VIDIXIVTVALPHA"))) {
295 if(0 == strcmp(alpha, "disable")) {
296 alpha_disable = 1;
299 } else {
300 printf(IVTV_MSG"Failed to open /dev/fb%u\n", fb_number);
301 return ENXIO;
304 /* Try to find YUV device */
305 unsigned char yuv_device_number = 48, yuv_device = 48 + fb_number;
306 char yuv_device_name[] = "/dev/videoXXX\0";
308 do {
309 sprintf(yuv_device_name, "/dev/video%u", yuv_device);
310 yuvdev = open(yuv_device_name, O_RDWR);
311 if(-1 != yuvdev) {
312 if(ivtv_verbose)
313 printf(IVTV_MSG"YUV device found /dev/video%u\n", yuv_device);
314 goto yuv_found;
315 } else {
316 if(ivtv_verbose)
317 printf(IVTV_MSG"YUV device not found: /dev/video%u\n", yuv_device);
319 } while(yuv_device-- > yuv_device_number);
320 return ENXIO;
322 yuv_found:
323 if(0 == alpha_disable) {
324 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
325 if(ioctl(fbdev, IVTVFB_IOCTL_GET_STATE, &fb_state_old) < 0) {
326 printf(IVTV_MSG"Unable to read fb state\n");
327 close(yuvdev);
328 close(fbdev);
329 return ENXIO;
330 } else {
331 if(ivtv_verbose) {
332 printf(IVTV_MSG"old alpha : %ld\n",fb_state_old.alpha);
333 printf(IVTV_MSG"old status: 0x%lx\n",fb_state_old.status);
335 fb_state_hide.alpha = 0;
336 fb_state_hide.status = fb_state_old.status | IVTVFB_STATUS_GLOBAL_ALPHA;
338 #else
339 memset(&format_old, 0, sizeof(format_old));
340 format_old.type = format_hide.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
341 if(ioctl(yuvdev, VIDIOC_G_FMT , &format_old) < 0) {
342 printf(IVTV_MSG"Unable to read fb state\n");
343 close(yuvdev);
344 close(fbdev);
345 return ENXIO;
346 } else {
347 if(ivtv_verbose) {
348 printf(IVTV_MSG"old alpha : %d\n",format_old.fmt.win.global_alpha);
350 memcpy(&format_hide, &format_old, sizeof(format_old));
351 format_hide.fmt.win.global_alpha = 0;
353 #endif
355 probed = 1;
356 return 0;
359 int ivtv_init(const char *args)
361 if(ivtv_verbose)
362 printf(IVTV_MSG"init\n");
364 if (!probed) {
365 if(ivtv_verbose)
366 printf(IVTV_MSG"Driver was not probed but is being initialized\n");
367 return EINTR;
369 outbuf = malloc((IVTVMAXHEIGHT * IVTVMAXWIDTH) + (IVTVMAXHEIGHT * IVTVMAXWIDTH / 2));
370 if(NULL == outbuf) {
371 if(ivtv_verbose)
372 printf(IVTV_MSG"Not enough memory availabe!\n");
373 return EINTR;
375 return 0;
378 void ivtv_destroy(void)
380 if(ivtv_verbose)
381 printf(IVTV_MSG"destroy\n");
382 if(-1 != yuvdev)
383 close(yuvdev);
384 if(-1 != fbdev)
385 close(fbdev);
386 if(NULL != outbuf)
387 free(outbuf);
388 if(NULL != memBase)
389 free(memBase);
392 int ivtv_get_caps(vidix_capability_t *to)
394 if(ivtv_verbose)
395 printf(IVTV_MSG"GetCap\n");
396 memcpy(to, &ivtv_cap, sizeof(vidix_capability_t));
397 return 0;
400 int ivtv_query_fourcc(vidix_fourcc_t *to)
402 if(ivtv_verbose)
403 printf(IVTV_MSG"query fourcc (%x)\n", to->fourcc);
405 int supports = 0;
406 switch(to->fourcc)
408 case IMGFMT_YV12:
409 supports = 1;
410 break;
411 default:
412 supports = 0;
415 if(!supports) {
416 to->depth = to->flags = 0;
417 return ENOTSUP;
419 to->depth = VID_DEPTH_12BPP |
420 VID_DEPTH_15BPP | VID_DEPTH_16BPP |
421 VID_DEPTH_24BPP | VID_DEPTH_32BPP;
422 to->flags = 0;
423 return 0;
426 int ivtv_config_playback(vidix_playback_t *info)
428 if(ivtv_verbose)
429 printf(IVTV_MSG"config playback\n");
431 if(2 == ivtv_verbose){
432 printf(IVTV_MSG"src : x:%d y:%d w:%d h:%d\n",
433 info->src.x, info->src.y, info->src.w, info->src.h);
434 printf(IVTV_MSG"dest: x:%d y:%d w:%d h:%d\n",
435 info->dest.x, info->dest.y, info->dest.w, info->dest.h);
438 memcpy(&destVideo, &info->dest, sizeof(vidix_rect_t));
439 memcpy(&srcVideo, &info->src, sizeof(vidix_rect_t));
441 info->num_frames = 2;
442 info->frame_size = frameSize = info->src.w*info->src.h+(info->src.w*info->src.h)/2;
443 info->dest.pitch.y = 1;
444 info->dest.pitch.u = info->dest.pitch.v = 2;
445 info->offsets[0] = 0;
446 info->offsets[1] = info->frame_size;
447 info->offset.y = 0;
448 info->offset.u = info->src.w * info->src.h;
449 info->offset.v = info->offset.u + ((info->src.w * info->src.h)/4);
450 info->dga_addr = memBase = malloc(info->num_frames*info->frame_size);
451 if(ivtv_verbose)
452 printf(IVTV_MSG"frame_size: %d, dga_addr: %p\n",
453 info->frame_size, info->dga_addr);
454 return 0;
457 int ivtv_playback_on(void)
459 if(ivtv_verbose)
460 printf(IVTV_MSG"playback on\n");
462 if(0 == alpha_disable) {
463 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
464 if (-1 != fbdev) {
465 if (ioctl(fbdev, IVTVFB_IOCTL_SET_STATE, &fb_state_hide) < 0)
466 printf (IVTV_MSG"Failed to set fb state\n");
468 #else
469 if (-1 != yuvdev) {
470 if (ioctl(yuvdev, VIDIOC_S_FMT, &format_hide) < 0)
471 printf (IVTV_MSG"Failed to set fb state\n");
473 #endif
475 return 0;
478 int ivtv_playback_off(void)
480 if(ivtv_verbose)
481 printf(IVTV_MSG"playback off\n");
483 if(0 == alpha_disable) {
484 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
485 if (-1 != fbdev) {
486 if (ioctl(fbdev, IVTVFB_IOCTL_SET_STATE, &fb_state_old) < 0)
487 printf (IVTV_MSG"Failed to restore fb state\n");
489 #else
490 if (-1 != yuvdev) {
491 if (ioctl(yuvdev, VIDIOC_S_FMT, &format_old) < 0)
492 printf (IVTV_MSG"Failed to restore fb state\n");
494 #endif
496 return 0;
499 int ivtv_frame_sel(unsigned int frame)
502 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
503 struct ivtvyuv_ioctl_dma_host_to_ivtv_args args;
504 #else
505 struct ivtv_dma_frame args;
506 #endif
507 unsigned char *curMemBase = (unsigned char *)memBase + (frame * frameSize);
509 de_macro_y(curMemBase, outbuf, srcVideo.w, srcVideo.h, srcVideo.x, srcVideo.y, srcVideo.h, srcVideo.w);
510 de_macro_uv(curMemBase + (srcVideo.w * srcVideo.h) + srcVideo.w * srcVideo.h / 4,
511 curMemBase + (srcVideo.w * srcVideo.h), outbuf + (IVTVMAXHEIGHT * IVTVMAXWIDTH),
512 srcVideo.w, srcVideo.h, srcVideo.x, srcVideo.y, srcVideo.h, srcVideo.w);
514 args.y_source = outbuf;
515 args.uv_source = outbuf + (IVTVMAXHEIGHT * IVTVMAXWIDTH);
517 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
518 args.src_x = srcVideo.x;
519 args.src_y = srcVideo.y;
520 args.dst_x = destVideo.x;
521 args.dst_y = destVideo.y;
522 args.src_w = srcVideo.w;
523 args.dst_w = destVideo.w;
524 args.srcBuf_width = srcVideo.w;
525 args.src_h = srcVideo.h;
526 args.dst_h = destVideo.h;
527 args.srcBuf_height = srcVideo.h;
528 args.yuv_type = 0;
529 #else
530 args.src.left = srcVideo.x;
531 args.src.top = srcVideo.y;
532 args.dst.left = destVideo.x;
533 args.dst.top = destVideo.y;
534 args.src.width = srcVideo.w;
535 args.dst.width = destVideo.w;
536 args.src_width = srcVideo.w;
537 args.src.height = srcVideo.h;
538 args.dst.height = destVideo.h;
539 args.src_height = srcVideo.h;
540 args.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
541 #endif
543 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
544 if(ioctl(yuvdev, IVTV_IOC_PREP_FRAME_YUV, &args) == -1) {
545 #else
546 if(ioctl(yuvdev, IVTV_IOC_DMA_FRAME, &args) == -1) {
547 #endif
548 printf("Ioctl IVTV_IOC_DMA_FRAME returned failed Error\n");
550 return 0;
553 VDXDriver ivtv_drv = {
554 "ivtv",
555 NULL,
556 .probe = ivtv_probe,
557 .get_caps = ivtv_get_caps,
558 .query_fourcc = ivtv_query_fourcc,
559 .init = ivtv_init,
560 .destroy = ivtv_destroy,
561 .config_playback = ivtv_config_playback,
562 .playback_on = ivtv_playback_on,
563 .playback_off = ivtv_playback_off,
564 .frame_sel = ivtv_frame_sel,