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.

311 lines
9.2KB

  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. AVFrame prev_frame;
  46. unsigned char *buf;
  47. int size;
  48. } RpzaContext;
  49. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  50. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  51. (((uint8_t*)(x))[1] << 16) | \
  52. (((uint8_t*)(x))[2] << 8) | \
  53. ((uint8_t*)(x))[3])
  54. #define ADVANCE_BLOCK() \
  55. { \
  56. pixel_ptr += 4; \
  57. if (pixel_ptr >= width) \
  58. { \
  59. pixel_ptr = 0; \
  60. row_ptr += stride * 4; \
  61. } \
  62. total_blocks--; \
  63. if (total_blocks < 0) \
  64. { \
  65. printf("warning: block counter just went negative (this should not happen)\n"); \
  66. return; \
  67. } \
  68. }
  69. static void rpza_decode_stream(RpzaContext *s)
  70. {
  71. int width = s->avctx->width;
  72. int stride = s->frame.linesize[0] / 2;
  73. int row_inc = stride - 4;
  74. int stream_ptr = 0;
  75. int chunk_size;
  76. unsigned char opcode;
  77. int n_blocks;
  78. unsigned short colorA = 0, colorB;
  79. unsigned short color4[4];
  80. unsigned char index, idx;
  81. unsigned short ta, tb;
  82. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  83. unsigned short *prev_pixels = (unsigned short *)s->prev_frame.data[0];
  84. int row_ptr = 0;
  85. int pixel_ptr = 0;
  86. int block_ptr;
  87. int pixel_x, pixel_y;
  88. int total_blocks;
  89. /* First byte is always 0xe1. Warn if it's different */
  90. if (s->buf[stream_ptr] != 0xe1)
  91. printf("First chunk byte is 0x%02x instead of 0x1e\n",
  92. s->buf[stream_ptr]);
  93. /* Get chunk size, ingnoring first byte */
  94. chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
  95. stream_ptr += 4;
  96. /* If length mismatch use size from MOV file and try to decode anyway */
  97. if (chunk_size != s->size)
  98. printf("MOV chunk size != encoded chunk size; using MOV chunk size\n");
  99. chunk_size = s->size;
  100. /* Number of 4x4 blocks in frame. */
  101. total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
  102. /* Process chunk data */
  103. while (stream_ptr < chunk_size) {
  104. opcode = s->buf[stream_ptr++]; /* Get opcode */
  105. n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
  106. /* If opcode MSbit is 0, we need more data to decide what to do */
  107. if ((opcode & 0x80) == 0) {
  108. colorA = (opcode << 8) | (s->buf[stream_ptr++]);
  109. opcode = 0;
  110. if ((s->buf[stream_ptr] & 0x80) != 0) {
  111. /* Must behave as opcode 110xxxxx, using colorA computed
  112. * above. Use fake opcode 0x20 to enter switch block at
  113. * the right place */
  114. opcode = 0x20;
  115. n_blocks = 1;
  116. }
  117. }
  118. switch (opcode & 0xe0) {
  119. /* Skip blocks */
  120. case 0x80:
  121. while (n_blocks--) {
  122. block_ptr = row_ptr + pixel_ptr;
  123. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  124. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  125. pixels[block_ptr] = prev_pixels[block_ptr];
  126. block_ptr++;
  127. }
  128. block_ptr += row_inc;
  129. }
  130. ADVANCE_BLOCK();
  131. }
  132. break;
  133. /* Fill blocks with one color */
  134. case 0xa0:
  135. colorA = BE_16 (&s->buf[stream_ptr]);
  136. stream_ptr += 2;
  137. while (n_blocks--) {
  138. block_ptr = row_ptr + pixel_ptr;
  139. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  140. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  141. pixels[block_ptr] = colorA;
  142. block_ptr++;
  143. }
  144. block_ptr += row_inc;
  145. }
  146. ADVANCE_BLOCK();
  147. }
  148. break;
  149. /* Fill blocks with 4 colors */
  150. case 0xc0:
  151. colorA = BE_16 (&s->buf[stream_ptr]);
  152. stream_ptr += 2;
  153. case 0x20:
  154. colorB = BE_16 (&s->buf[stream_ptr]);
  155. stream_ptr += 2;
  156. /* sort out the colors */
  157. color4[0] = colorB;
  158. color4[1] = 0;
  159. color4[2] = 0;
  160. color4[3] = colorA;
  161. /* red components */
  162. ta = (colorA >> 10) & 0x1F;
  163. tb = (colorB >> 10) & 0x1F;
  164. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
  165. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
  166. /* green components */
  167. ta = (colorA >> 5) & 0x1F;
  168. tb = (colorB >> 5) & 0x1F;
  169. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
  170. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
  171. /* blue components */
  172. ta = colorA & 0x1F;
  173. tb = colorB & 0x1F;
  174. color4[1] |= ((11 * ta + 21 * tb) >> 5);
  175. color4[2] |= ((21 * ta + 11 * tb) >> 5);
  176. while (n_blocks--) {
  177. block_ptr = row_ptr + pixel_ptr;
  178. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  179. index = s->buf[stream_ptr++];
  180. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  181. idx = (index >> (2 * (3 - pixel_x))) & 0x03;
  182. pixels[block_ptr] = color4[idx];
  183. block_ptr++;
  184. }
  185. block_ptr += row_inc;
  186. }
  187. ADVANCE_BLOCK();
  188. }
  189. break;
  190. /* Fill block with 16 colors */
  191. case 0x00:
  192. block_ptr = row_ptr + pixel_ptr;
  193. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  194. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  195. /* We already have color of upper left pixel */
  196. if ((pixel_y != 0) || (pixel_x !=0)) {
  197. colorA = BE_16 (&s->buf[stream_ptr]);
  198. stream_ptr += 2;
  199. }
  200. pixels[block_ptr] = colorA;
  201. block_ptr++;
  202. }
  203. block_ptr += row_inc;
  204. }
  205. ADVANCE_BLOCK();
  206. break;
  207. /* Unknown opcode */
  208. default:
  209. printf("Unknown opcode %d in rpza chunk."
  210. " Skip remaining %d bytes of chunk data.\n", opcode,
  211. chunk_size - stream_ptr);
  212. return;
  213. } /* Opcode switch */
  214. }
  215. }
  216. static int rpza_decode_init(AVCodecContext *avctx)
  217. {
  218. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  219. s->avctx = avctx;
  220. avctx->pix_fmt = PIX_FMT_RGB555;
  221. avctx->has_b_frames = 0;
  222. dsputil_init(&s->dsp, avctx);
  223. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  224. return 0;
  225. }
  226. static int rpza_decode_frame(AVCodecContext *avctx,
  227. void *data, int *data_size,
  228. uint8_t *buf, int buf_size)
  229. {
  230. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  231. s->buf = buf;
  232. s->size = buf_size;
  233. if (avctx->get_buffer(avctx, &s->frame)) {
  234. printf (" RPZA Video: get_buffer() failed\n");
  235. return -1;
  236. }
  237. rpza_decode_stream(s);
  238. if (s->prev_frame.data[0])
  239. avctx->release_buffer(avctx, &s->prev_frame);
  240. /* shuffle frames */
  241. s->prev_frame = s->frame;
  242. *data_size = sizeof(AVFrame);
  243. *(AVFrame*)data = s->frame;
  244. /* always report that the buffer was completely consumed */
  245. return buf_size;
  246. }
  247. static int rpza_decode_end(AVCodecContext *avctx)
  248. {
  249. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  250. if (s->prev_frame.data[0])
  251. avctx->release_buffer(avctx, &s->prev_frame);
  252. return 0;
  253. }
  254. AVCodec rpza_decoder = {
  255. "rpza",
  256. CODEC_TYPE_VIDEO,
  257. CODEC_ID_RPZA,
  258. sizeof(RpzaContext),
  259. rpza_decode_init,
  260. NULL,
  261. rpza_decode_end,
  262. rpza_decode_frame,
  263. CODEC_CAP_DR1,
  264. };