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.

301 lines
8.9KB

  1. /*
  2. * Quicktime Video (RPZA) Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  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 rpza.c
  22. * QT RPZA Video Decoder by Roberto Togni <rtogni@bresciaonline.it>
  23. * For more information about the RPZA format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. *
  26. * The RPZA decoder outputs RGB555 colorspace data.
  27. *
  28. * Note that this decoder reads big endian RGB555 pixel values from the
  29. * bytestream, arranges them in the host's endian order, and outputs
  30. * them to the final rendered map in the same host endian order. This is
  31. * intended behavior as the ffmpeg documentation states that RGB555 pixels
  32. * shall be stored in native CPU endianness.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include "common.h"
  39. #include "avcodec.h"
  40. #include "dsputil.h"
  41. typedef struct RpzaContext {
  42. AVCodecContext *avctx;
  43. DSPContext dsp;
  44. AVFrame frame;
  45. unsigned char *buf;
  46. int size;
  47. } RpzaContext;
  48. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  49. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  50. (((uint8_t*)(x))[1] << 16) | \
  51. (((uint8_t*)(x))[2] << 8) | \
  52. ((uint8_t*)(x))[3])
  53. #define ADVANCE_BLOCK() \
  54. { \
  55. pixel_ptr += 4; \
  56. if (pixel_ptr >= width) \
  57. { \
  58. pixel_ptr = 0; \
  59. row_ptr += stride * 4; \
  60. } \
  61. total_blocks--; \
  62. if (total_blocks < 0) \
  63. { \
  64. av_log(s->avctx, AV_LOG_ERROR, "warning: block counter just went negative (this should not happen)\n"); \
  65. return; \
  66. } \
  67. }
  68. static void rpza_decode_stream(RpzaContext *s)
  69. {
  70. int width = s->avctx->width;
  71. int stride = s->frame.linesize[0] / 2;
  72. int row_inc = stride - 4;
  73. int stream_ptr = 0;
  74. int chunk_size;
  75. unsigned char opcode;
  76. int n_blocks;
  77. unsigned short colorA = 0, colorB;
  78. unsigned short color4[4];
  79. unsigned char index, idx;
  80. unsigned short ta, tb;
  81. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  82. int row_ptr = 0;
  83. int pixel_ptr = 0;
  84. int block_ptr;
  85. int pixel_x, pixel_y;
  86. int total_blocks;
  87. /* First byte is always 0xe1. Warn if it's different */
  88. if (s->buf[stream_ptr] != 0xe1)
  89. av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0x1e\n",
  90. s->buf[stream_ptr]);
  91. /* Get chunk size, ingnoring first byte */
  92. chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
  93. stream_ptr += 4;
  94. /* If length mismatch use size from MOV file and try to decode anyway */
  95. if (chunk_size != s->size)
  96. av_log(s->avctx, AV_LOG_ERROR, "MOV chunk size != encoded chunk size; using MOV chunk size\n");
  97. chunk_size = s->size;
  98. /* Number of 4x4 blocks in frame. */
  99. total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
  100. /* Process chunk data */
  101. while (stream_ptr < chunk_size) {
  102. opcode = s->buf[stream_ptr++]; /* Get opcode */
  103. n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
  104. /* If opcode MSbit is 0, we need more data to decide what to do */
  105. if ((opcode & 0x80) == 0) {
  106. colorA = (opcode << 8) | (s->buf[stream_ptr++]);
  107. opcode = 0;
  108. if ((s->buf[stream_ptr] & 0x80) != 0) {
  109. /* Must behave as opcode 110xxxxx, using colorA computed
  110. * above. Use fake opcode 0x20 to enter switch block at
  111. * the right place */
  112. opcode = 0x20;
  113. n_blocks = 1;
  114. }
  115. }
  116. switch (opcode & 0xe0) {
  117. /* Skip blocks */
  118. case 0x80:
  119. while (n_blocks--) {
  120. ADVANCE_BLOCK();
  121. }
  122. break;
  123. /* Fill blocks with one color */
  124. case 0xa0:
  125. colorA = BE_16 (&s->buf[stream_ptr]);
  126. stream_ptr += 2;
  127. while (n_blocks--) {
  128. block_ptr = row_ptr + pixel_ptr;
  129. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  130. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  131. pixels[block_ptr] = colorA;
  132. block_ptr++;
  133. }
  134. block_ptr += row_inc;
  135. }
  136. ADVANCE_BLOCK();
  137. }
  138. break;
  139. /* Fill blocks with 4 colors */
  140. case 0xc0:
  141. colorA = BE_16 (&s->buf[stream_ptr]);
  142. stream_ptr += 2;
  143. case 0x20:
  144. colorB = BE_16 (&s->buf[stream_ptr]);
  145. stream_ptr += 2;
  146. /* sort out the colors */
  147. color4[0] = colorB;
  148. color4[1] = 0;
  149. color4[2] = 0;
  150. color4[3] = colorA;
  151. /* red components */
  152. ta = (colorA >> 10) & 0x1F;
  153. tb = (colorB >> 10) & 0x1F;
  154. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
  155. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
  156. /* green components */
  157. ta = (colorA >> 5) & 0x1F;
  158. tb = (colorB >> 5) & 0x1F;
  159. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
  160. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
  161. /* blue components */
  162. ta = colorA & 0x1F;
  163. tb = colorB & 0x1F;
  164. color4[1] |= ((11 * ta + 21 * tb) >> 5);
  165. color4[2] |= ((21 * ta + 11 * tb) >> 5);
  166. while (n_blocks--) {
  167. block_ptr = row_ptr + pixel_ptr;
  168. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  169. index = s->buf[stream_ptr++];
  170. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  171. idx = (index >> (2 * (3 - pixel_x))) & 0x03;
  172. pixels[block_ptr] = color4[idx];
  173. block_ptr++;
  174. }
  175. block_ptr += row_inc;
  176. }
  177. ADVANCE_BLOCK();
  178. }
  179. break;
  180. /* Fill block with 16 colors */
  181. case 0x00:
  182. block_ptr = row_ptr + pixel_ptr;
  183. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  184. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  185. /* We already have color of upper left pixel */
  186. if ((pixel_y != 0) || (pixel_x !=0)) {
  187. colorA = BE_16 (&s->buf[stream_ptr]);
  188. stream_ptr += 2;
  189. }
  190. pixels[block_ptr] = colorA;
  191. block_ptr++;
  192. }
  193. block_ptr += row_inc;
  194. }
  195. ADVANCE_BLOCK();
  196. break;
  197. /* Unknown opcode */
  198. default:
  199. av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
  200. " Skip remaining %d bytes of chunk data.\n", opcode,
  201. chunk_size - stream_ptr);
  202. return;
  203. } /* Opcode switch */
  204. }
  205. }
  206. static int rpza_decode_init(AVCodecContext *avctx)
  207. {
  208. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  209. s->avctx = avctx;
  210. avctx->pix_fmt = PIX_FMT_RGB555;
  211. avctx->has_b_frames = 0;
  212. dsputil_init(&s->dsp, avctx);
  213. s->frame.data[0] = NULL;
  214. return 0;
  215. }
  216. static int rpza_decode_frame(AVCodecContext *avctx,
  217. void *data, int *data_size,
  218. uint8_t *buf, int buf_size)
  219. {
  220. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  221. /* no supplementary picture */
  222. if (buf_size == 0)
  223. return 0;
  224. s->buf = buf;
  225. s->size = buf_size;
  226. s->frame.reference = 1;
  227. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  228. if (avctx->reget_buffer(avctx, &s->frame)) {
  229. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  230. return -1;
  231. }
  232. rpza_decode_stream(s);
  233. *data_size = sizeof(AVFrame);
  234. *(AVFrame*)data = s->frame;
  235. /* always report that the buffer was completely consumed */
  236. return buf_size;
  237. }
  238. static int rpza_decode_end(AVCodecContext *avctx)
  239. {
  240. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  241. if (s->frame.data[0])
  242. avctx->release_buffer(avctx, &s->frame);
  243. return 0;
  244. }
  245. AVCodec rpza_decoder = {
  246. "rpza",
  247. CODEC_TYPE_VIDEO,
  248. CODEC_ID_RPZA,
  249. sizeof(RpzaContext),
  250. rpza_decode_init,
  251. NULL,
  252. rpza_decode_end,
  253. rpza_decode_frame,
  254. CODEC_CAP_DR1,
  255. };