pwc-uncompress.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "pwc-dec1.h"
  27. #include "pwc-dec23.h"
  28. int pwc_decompress(struct pwc_device *pdev)
  29. {
  30. struct pwc_frame_buf *fbuf;
  31. int n, line, col, stride;
  32. void *yuv, *image;
  33. u16 *src;
  34. u16 *dsty, *dstu, *dstv;
  35. if (pdev == NULL)
  36. return -EFAULT;
  37. #if defined(__KERNEL__) && defined(PWC_MAGIC)
  38. if (pdev->magic != PWC_MAGIC) {
  39. Err("pwc_decompress(): magic failed.\n");
  40. return -EFAULT;
  41. }
  42. #endif
  43. fbuf = pdev->read_frame;
  44. if (fbuf == NULL)
  45. return -EFAULT;
  46. image = pdev->image_ptr[pdev->fill_image];
  47. if (!image)
  48. return -EFAULT;
  49. yuv = fbuf->data + pdev->frame_header_size; /* Skip header */
  50. /* Raw format; that's easy... */
  51. if (pdev->vpalette == VIDEO_PALETTE_RAW)
  52. {
  53. memcpy(image, yuv, pdev->frame_size);
  54. return 0;
  55. }
  56. if (pdev->vbandlength == 0) {
  57. /* Uncompressed mode. We copy the data into the output buffer,
  58. using the viewport size (which may be larger than the image
  59. size). Unfortunately we have to do a bit of byte stuffing
  60. to get the desired output format/size.
  61. */
  62. /*
  63. * We do some byte shuffling here to go from the
  64. * native format to YUV420P.
  65. */
  66. src = (u16 *)yuv;
  67. n = pdev->view.x * pdev->view.y;
  68. /* offset in Y plane */
  69. stride = pdev->view.x * pdev->offset.y + pdev->offset.x;
  70. dsty = (u16 *)(image + stride);
  71. /* offsets in U/V planes */
  72. stride = pdev->view.x * pdev->offset.y / 4 + pdev->offset.x / 2;
  73. dstu = (u16 *)(image + n + stride);
  74. dstv = (u16 *)(image + n + n / 4 + stride);
  75. /* increment after each line */
  76. stride = (pdev->view.x - pdev->image.x) / 2; /* u16 is 2 bytes */
  77. for (line = 0; line < pdev->image.y; line++) {
  78. for (col = 0; col < pdev->image.x; col += 4) {
  79. *dsty++ = *src++;
  80. *dsty++ = *src++;
  81. if (line & 1)
  82. *dstv++ = *src++;
  83. else
  84. *dstu++ = *src++;
  85. }
  86. dsty += stride;
  87. if (line & 1)
  88. dstv += (stride >> 1);
  89. else
  90. dstu += (stride >> 1);
  91. }
  92. }
  93. else {
  94. /* Compressed; the decompressor routines will write the data
  95. in planar format immediately.
  96. */
  97. int flags;
  98. flags = PWCX_FLAG_PLANAR;
  99. if (pdev->vsize == PSZ_VGA && pdev->vframes == 5 && pdev->vsnapshot)
  100. {
  101. printk(KERN_ERR "pwc: Mode Bayer is not supported for now\n");
  102. flags |= PWCX_FLAG_BAYER;
  103. return -ENXIO; /* No such device or address: missing decompressor */
  104. }
  105. switch (pdev->type)
  106. {
  107. #if 0
  108. case 675:
  109. case 680:
  110. case 690:
  111. case 720:
  112. case 730:
  113. case 740:
  114. case 750:
  115. pwc_dec23_decompress(&pdev->image, &pdev->view, &pdev->offset,
  116. yuv, image,
  117. flags,
  118. pdev->decompress_data, pdev->vbandlength);
  119. break;
  120. case 645:
  121. case 646:
  122. /* TODO & FIXME */
  123. #endif
  124. return -ENXIO; /* No such device or address: missing decompressor */
  125. break;
  126. }
  127. }
  128. return 0;
  129. }