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.

295 lines
8.9KB

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