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.

290 lines
8.5KB

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