You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
5.7KB

  1. /*
  2. * Quicktime Planar RGB (8BPS) Video Decoder
  3. * Copyright (C) 2003 Roberto Togni
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file 8bps.c
  22. * QT 8BPS Video Decoder by Roberto Togni <rtogni at bresciaonline dot it>
  23. * For more information about the 8BPS format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. *
  26. * Supports: PAL8 (RGB 8bpp, paletted)
  27. * : BGR24 (RGB 24bpp) (can also output it as RGBA32)
  28. * : RGBA32 (RGB 32bpp, 4th plane is probably alpha and it's ignored)
  29. *
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "common.h"
  34. #include "avcodec.h"
  35. const enum PixelFormat pixfmt_rgb24[] = {PIX_FMT_BGR24, PIX_FMT_RGBA32, -1};
  36. /*
  37. * Decoder context
  38. */
  39. typedef struct EightBpsContext {
  40. AVCodecContext *avctx;
  41. AVFrame pic;
  42. unsigned char planes;
  43. unsigned char planemap[4];
  44. } EightBpsContext;
  45. /*
  46. *
  47. * Decode a frame
  48. *
  49. */
  50. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  51. {
  52. EightBpsContext * const c = (EightBpsContext *)avctx->priv_data;
  53. unsigned char *encoded = (unsigned char *)buf;
  54. unsigned char *pixptr, *pixptr_end;
  55. unsigned int height = avctx->height; // Real image height
  56. unsigned int dlen, p, row;
  57. unsigned char *lp, *dp;
  58. unsigned char count;
  59. unsigned int px_inc;
  60. unsigned int planes = c->planes;
  61. unsigned char *planemap = c->planemap;
  62. /* no supplementary picture */
  63. if (buf_size == 0)
  64. return 0;
  65. if(c->pic.data[0])
  66. avctx->release_buffer(avctx, &c->pic);
  67. c->pic.reference = 0;
  68. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  69. if(avctx->get_buffer(avctx, &c->pic) < 0){
  70. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  71. return -1;
  72. }
  73. /* Set data pointer after line lengths */
  74. dp = encoded + planes * (height << 1);
  75. /* Ignore alpha plane, don't know what to do with it */
  76. if (planes == 4)
  77. planes--;
  78. px_inc = planes + (avctx->pix_fmt == PIX_FMT_RGBA32);
  79. for (p = 0; p < planes; p++) {
  80. /* Lines length pointer for this plane */
  81. lp = encoded + p * (height << 1);
  82. /* Decode a plane */
  83. for(row = 0; row < height; row++) {
  84. pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
  85. pixptr_end = pixptr + c->pic.linesize[0];
  86. dlen = be2me_16(*(unsigned short *)(lp+row*2));
  87. /* Decode a row of this plane */
  88. while(dlen > 0) {
  89. if ((count = *dp++) <= 127) {
  90. count++;
  91. dlen -= count + 1;
  92. if (pixptr + count * px_inc > pixptr_end)
  93. break;
  94. while(count--) {
  95. *pixptr = *dp++;
  96. pixptr += px_inc;
  97. }
  98. } else {
  99. count = 257 - count;
  100. if (pixptr + count * px_inc > pixptr_end)
  101. break;
  102. while(count--) {
  103. *pixptr = *dp;
  104. pixptr += px_inc;
  105. }
  106. dp++;
  107. dlen -= 2;
  108. }
  109. }
  110. }
  111. }
  112. if (avctx->palctrl) {
  113. memcpy (c->pic.data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  114. if (avctx->palctrl->palette_changed) {
  115. c->pic.palette_has_changed = 1;
  116. avctx->palctrl->palette_changed = 0;
  117. } else
  118. c->pic.palette_has_changed = 0;
  119. }
  120. *data_size = sizeof(AVFrame);
  121. *(AVFrame*)data = c->pic;
  122. /* always report that the buffer was completely consumed */
  123. return buf_size;
  124. }
  125. /*
  126. *
  127. * Init 8BPS decoder
  128. *
  129. */
  130. static int decode_init(AVCodecContext *avctx)
  131. {
  132. EightBpsContext * const c = (EightBpsContext *)avctx->priv_data;
  133. c->avctx = avctx;
  134. avctx->has_b_frames = 0;
  135. c->pic.data[0] = NULL;
  136. // FIXME: find a better way to prevent integer overflow
  137. if (((unsigned int)avctx->width > 32000) || ((unsigned int)avctx->height > 32000)) {
  138. av_log(avctx, AV_LOG_ERROR, "Bad image size (w = %d, h = %d).\n", avctx->width, avctx->height);
  139. return 1;
  140. }
  141. switch (avctx->bits_per_sample) {
  142. case 8:
  143. avctx->pix_fmt = PIX_FMT_PAL8;
  144. c->planes = 1;
  145. c->planemap[0] = 0; // 1st plane is palette indexes
  146. if (avctx->palctrl == NULL) {
  147. av_log(avctx, AV_LOG_ERROR, "Error: PAL8 format but no palette from demuxer.\n");
  148. return -1;
  149. }
  150. break;
  151. case 24:
  152. avctx->pix_fmt = avctx->get_format(avctx, pixfmt_rgb24);
  153. c->planes = 3;
  154. c->planemap[0] = 2; // 1st plane is red
  155. c->planemap[1] = 1; // 2nd plane is green
  156. c->planemap[2] = 0; // 3rd plane is blue
  157. break;
  158. case 32:
  159. avctx->pix_fmt = PIX_FMT_RGBA32;
  160. c->planes = 4;
  161. #ifdef WORDS_BIGENDIAN
  162. c->planemap[0] = 1; // 1st plane is red
  163. c->planemap[1] = 2; // 2nd plane is green
  164. c->planemap[2] = 3; // 3rd plane is blue
  165. c->planemap[3] = 0; // 4th plane is alpha???
  166. #else
  167. c->planemap[0] = 2; // 1st plane is red
  168. c->planemap[1] = 1; // 2nd plane is green
  169. c->planemap[2] = 0; // 3rd plane is blue
  170. c->planemap[3] = 3; // 4th plane is alpha???
  171. #endif
  172. break;
  173. default:
  174. av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", avctx->bits_per_sample);
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. /*
  180. *
  181. * Uninit 8BPS decoder
  182. *
  183. */
  184. static int decode_end(AVCodecContext *avctx)
  185. {
  186. EightBpsContext * const c = (EightBpsContext *)avctx->priv_data;
  187. if (c->pic.data[0])
  188. avctx->release_buffer(avctx, &c->pic);
  189. return 0;
  190. }
  191. AVCodec eightbps_decoder = {
  192. "8bps",
  193. CODEC_TYPE_VIDEO,
  194. CODEC_ID_8BPS,
  195. sizeof(EightBpsContext),
  196. decode_init,
  197. NULL,
  198. decode_end,
  199. decode_frame,
  200. CODEC_CAP_DR1,
  201. };