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.

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