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.

329 lines
10KB

  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. av_log(s->avctx, AV_LOG_ERROR, "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. av_log(s->avctx, AV_LOG_ERROR, "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. av_log(s->avctx, AV_LOG_ERROR, "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. if (!s->avctx->cr_available) {
  123. block_ptr = row_ptr + pixel_ptr;
  124. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  125. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  126. pixels[block_ptr] = prev_pixels[block_ptr];
  127. block_ptr++;
  128. }
  129. block_ptr += row_inc;
  130. }
  131. }
  132. ADVANCE_BLOCK();
  133. }
  134. break;
  135. /* Fill blocks with one color */
  136. case 0xa0:
  137. colorA = BE_16 (&s->buf[stream_ptr]);
  138. stream_ptr += 2;
  139. while (n_blocks--) {
  140. block_ptr = row_ptr + pixel_ptr;
  141. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  142. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  143. pixels[block_ptr] = colorA;
  144. block_ptr++;
  145. }
  146. block_ptr += row_inc;
  147. }
  148. ADVANCE_BLOCK();
  149. }
  150. break;
  151. /* Fill blocks with 4 colors */
  152. case 0xc0:
  153. colorA = BE_16 (&s->buf[stream_ptr]);
  154. stream_ptr += 2;
  155. case 0x20:
  156. colorB = BE_16 (&s->buf[stream_ptr]);
  157. stream_ptr += 2;
  158. /* sort out the colors */
  159. color4[0] = colorB;
  160. color4[1] = 0;
  161. color4[2] = 0;
  162. color4[3] = colorA;
  163. /* red components */
  164. ta = (colorA >> 10) & 0x1F;
  165. tb = (colorB >> 10) & 0x1F;
  166. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
  167. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
  168. /* green components */
  169. ta = (colorA >> 5) & 0x1F;
  170. tb = (colorB >> 5) & 0x1F;
  171. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
  172. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
  173. /* blue components */
  174. ta = colorA & 0x1F;
  175. tb = colorB & 0x1F;
  176. color4[1] |= ((11 * ta + 21 * tb) >> 5);
  177. color4[2] |= ((21 * ta + 11 * tb) >> 5);
  178. while (n_blocks--) {
  179. block_ptr = row_ptr + pixel_ptr;
  180. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  181. index = s->buf[stream_ptr++];
  182. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  183. idx = (index >> (2 * (3 - pixel_x))) & 0x03;
  184. pixels[block_ptr] = color4[idx];
  185. block_ptr++;
  186. }
  187. block_ptr += row_inc;
  188. }
  189. ADVANCE_BLOCK();
  190. }
  191. break;
  192. /* Fill block with 16 colors */
  193. case 0x00:
  194. block_ptr = row_ptr + pixel_ptr;
  195. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  196. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  197. /* We already have color of upper left pixel */
  198. if ((pixel_y != 0) || (pixel_x !=0)) {
  199. colorA = BE_16 (&s->buf[stream_ptr]);
  200. stream_ptr += 2;
  201. }
  202. pixels[block_ptr] = colorA;
  203. block_ptr++;
  204. }
  205. block_ptr += row_inc;
  206. }
  207. ADVANCE_BLOCK();
  208. break;
  209. /* Unknown opcode */
  210. default:
  211. av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
  212. " Skip remaining %d bytes of chunk data.\n", opcode,
  213. chunk_size - stream_ptr);
  214. return;
  215. } /* Opcode switch */
  216. }
  217. }
  218. static int rpza_decode_init(AVCodecContext *avctx)
  219. {
  220. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  221. s->avctx = avctx;
  222. avctx->pix_fmt = PIX_FMT_RGB555;
  223. avctx->has_b_frames = 0;
  224. dsputil_init(&s->dsp, avctx);
  225. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  226. return 0;
  227. }
  228. static int rpza_decode_frame(AVCodecContext *avctx,
  229. void *data, int *data_size,
  230. uint8_t *buf, int buf_size)
  231. {
  232. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  233. /* no supplementary picture */
  234. if (buf_size == 0)
  235. return 0;
  236. s->buf = buf;
  237. s->size = buf_size;
  238. s->frame.reference = 1;
  239. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE;
  240. if (avctx->cr_available)
  241. s->frame.buffer_hints |= FF_BUFFER_HINTS_REUSABLE;
  242. else
  243. s->frame.buffer_hints |= FF_BUFFER_HINTS_READABLE;
  244. if (avctx->get_buffer(avctx, &s->frame)) {
  245. av_log(avctx, AV_LOG_ERROR, " RPZA Video: get_buffer() failed\n");
  246. return -1;
  247. }
  248. if (s->prev_frame.data[0] &&(s->frame.linesize[0] != s->prev_frame.linesize[0]))
  249. av_log(avctx, AV_LOG_ERROR, "Buffer linesize changed: current %u, previous %u.\n"
  250. "Expect wrong image and/or crash!\n",
  251. s->frame.linesize[0], s->prev_frame.linesize[0]);
  252. rpza_decode_stream(s);
  253. if (s->prev_frame.data[0])
  254. avctx->release_buffer(avctx, &s->prev_frame);
  255. /* shuffle frames */
  256. if (!avctx->cr_available)
  257. s->prev_frame = s->frame;
  258. *data_size = sizeof(AVFrame);
  259. *(AVFrame*)data = s->frame;
  260. /* always report that the buffer was completely consumed */
  261. return buf_size;
  262. }
  263. static int rpza_decode_end(AVCodecContext *avctx)
  264. {
  265. RpzaContext *s = (RpzaContext *)avctx->priv_data;
  266. if (s->prev_frame.data[0])
  267. avctx->release_buffer(avctx, &s->prev_frame);
  268. return 0;
  269. }
  270. AVCodec rpza_decoder = {
  271. "rpza",
  272. CODEC_TYPE_VIDEO,
  273. CODEC_ID_RPZA,
  274. sizeof(RpzaContext),
  275. rpza_decode_init,
  276. NULL,
  277. rpza_decode_end,
  278. rpza_decode_frame,
  279. CODEC_CAP_DR1 | CODEC_CAP_CR,
  280. };