pwc-uncompress.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Linux driver for Philips webcam
  2. Decompression frontend.
  3. (C) 1999-2003 Nemosoft Unv.
  4. (C) 2004 Luc Saillard (luc@saillard.org)
  5. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  6. driver and thus may have bugs that are not present in the original version.
  7. Please send bug reports and support requests to <luc@saillard.org>.
  8. The decompression routines have been implemented by reverse-engineering the
  9. Nemosoft binary pwcx module. Caveat emptor.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program 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.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <asm/current.h>
  23. #include <asm/types.h>
  24. #include "pwc.h"
  25. #include "pwc-uncompress.h"
  26. int pwc_decompress(struct pwc_device *pdev)
  27. {
  28. struct pwc_frame_buf *fbuf;
  29. int n, line, col, stride;
  30. void *yuv, *image;
  31. u16 *src;
  32. u16 *dsty, *dstu, *dstv;
  33. if (pdev == NULL)
  34. return -EFAULT;
  35. #if defined(__KERNEL__) && defined(PWC_MAGIC)
  36. if (pdev->magic != PWC_MAGIC) {
  37. Err("pwc_decompress(): magic failed.\n");
  38. return -EFAULT;
  39. }
  40. #endif
  41. fbuf = pdev->read_frame;
  42. if (fbuf == NULL)
  43. return -EFAULT;
  44. image = pdev->image_ptr[pdev->fill_image];
  45. if (!image)
  46. return -EFAULT;
  47. yuv = fbuf->data + pdev->frame_header_size; /* Skip header */
  48. /* Raw format; that's easy... */
  49. if (pdev->vpalette == VIDEO_PALETTE_RAW)
  50. {
  51. memcpy(image, yuv, pdev->frame_size);
  52. return 0;
  53. }
  54. if (pdev->vbandlength == 0) {
  55. /* Uncompressed mode. We copy the data into the output buffer,
  56. using the viewport size (which may be larger than the image
  57. size). Unfortunately we have to do a bit of byte stuffing
  58. to get the desired output format/size.
  59. */
  60. /*
  61. * We do some byte shuffling here to go from the
  62. * native format to YUV420P.
  63. */
  64. src = (u16 *)yuv;
  65. n = pdev->view.x * pdev->view.y;
  66. /* offset in Y plane */
  67. stride = pdev->view.x * pdev->offset.y + pdev->offset.x;
  68. dsty = (u16 *)(image + stride);
  69. /* offsets in U/V planes */
  70. stride = pdev->view.x * pdev->offset.y / 4 + pdev->offset.x / 2;
  71. dstu = (u16 *)(image + n + stride);
  72. dstv = (u16 *)(image + n + n / 4 + stride);
  73. /* increment after each line */
  74. stride = (pdev->view.x - pdev->image.x) / 2; /* u16 is 2 bytes */
  75. for (line = 0; line < pdev->image.y; line++) {
  76. for (col = 0; col < pdev->image.x; col += 4) {
  77. *dsty++ = *src++;
  78. *dsty++ = *src++;
  79. if (line & 1)
  80. *dstv++ = *src++;
  81. else
  82. *dstu++ = *src++;
  83. }
  84. dsty += stride;
  85. if (line & 1)
  86. dstv += (stride >> 1);
  87. else
  88. dstu += (stride >> 1);
  89. }
  90. }
  91. else {
  92. /* Compressed; the decompressor routines will write the data
  93. in planar format immediately.
  94. */
  95. int flags;
  96. flags = PWCX_FLAG_PLANAR;
  97. if (pdev->vsize == PSZ_VGA && pdev->vframes == 5 && pdev->vsnapshot)
  98. {
  99. printk(KERN_ERR "pwc: Mode Bayer is not supported for now\n");
  100. flags |= PWCX_FLAG_BAYER;
  101. return -ENXIO; /* No such device or address: missing decompressor */
  102. }
  103. #if 0
  104. switch (pdev->type)
  105. {
  106. case 675:
  107. case 680:
  108. case 690:
  109. case 720:
  110. case 730:
  111. case 740:
  112. case 750:
  113. pwc_dec23_decompress(&pdev->image, &pdev->view,
  114. &pdev->offset, yuv, image, flags,
  115. pdev->decompress_data, pdev->vbandlength);
  116. break;
  117. case 645:
  118. case 646:
  119. /* TODO & FIXME */
  120. return -ENXIO; /* Missing decompressor */
  121. break;
  122. }
  123. #endif
  124. }
  125. return 0;
  126. }