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.

293 lines
8.6KB

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