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
9.4KB

  1. /*
  2. * Quicktime Video (RPZA) Video Decoder
  3. * Copyright (C) 2003 The FFmpeg project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 <stdint.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "libavutil/internal.h"
  40. #include "avcodec.h"
  41. #include "bytestream.h"
  42. #include "internal.h"
  43. typedef struct RpzaContext {
  44. AVCodecContext *avctx;
  45. AVFrame *frame;
  46. GetByteContext gb;
  47. } RpzaContext;
  48. #define CHECK_BLOCK() \
  49. if (total_blocks < 1) { \
  50. av_log(s->avctx, AV_LOG_ERROR, \
  51. "Block counter just went negative (this should not happen)\n"); \
  52. return AVERROR_INVALIDDATA; \
  53. } \
  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. }
  64. static int 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 chunk_size;
  70. uint16_t colorA = 0, colorB;
  71. uint16_t color4[4];
  72. uint16_t ta, tb;
  73. uint16_t *pixels = (uint16_t *)s->frame->data[0];
  74. int row_ptr = 0;
  75. int pixel_ptr = 0;
  76. int block_ptr;
  77. int pixel_x, pixel_y;
  78. int total_blocks;
  79. /* First byte is always 0xe1. Warn if it's different */
  80. if (bytestream2_peek_byte(&s->gb) != 0xe1)
  81. av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0xe1\n",
  82. bytestream2_peek_byte(&s->gb));
  83. /* Get chunk size, ignoring first byte */
  84. chunk_size = bytestream2_get_be32(&s->gb) & 0x00FFFFFF;
  85. /* If length mismatch use size from MOV file and try to decode anyway */
  86. if (chunk_size != bytestream2_get_bytes_left(&s->gb) - 4)
  87. av_log(s->avctx, AV_LOG_WARNING, "MOV chunk size != encoded chunk size\n");
  88. /* Number of 4x4 blocks in frame. */
  89. total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
  90. /* Process chunk data */
  91. while (bytestream2_get_bytes_left(&s->gb)) {
  92. uint8_t opcode = bytestream2_get_byte(&s->gb); /* Get opcode */
  93. int n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
  94. /* If opcode MSbit is 0, we need more data to decide what to do */
  95. if ((opcode & 0x80) == 0) {
  96. colorA = (opcode << 8) | bytestream2_get_byte(&s->gb);
  97. opcode = 0;
  98. if ((bytestream2_peek_byte(&s->gb) & 0x80) != 0) {
  99. /* Must behave as opcode 110xxxxx, using colorA computed
  100. * above. Use fake opcode 0x20 to enter switch block at
  101. * the right place */
  102. opcode = 0x20;
  103. n_blocks = 1;
  104. }
  105. }
  106. n_blocks = FFMIN(n_blocks, total_blocks);
  107. switch (opcode & 0xe0) {
  108. /* Skip blocks */
  109. case 0x80:
  110. while (n_blocks--) {
  111. CHECK_BLOCK();
  112. ADVANCE_BLOCK();
  113. }
  114. break;
  115. /* Fill blocks with one color */
  116. case 0xa0:
  117. colorA = bytestream2_get_be16(&s->gb);
  118. while (n_blocks--) {
  119. CHECK_BLOCK();
  120. block_ptr = row_ptr + pixel_ptr;
  121. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  122. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  123. pixels[block_ptr] = colorA;
  124. block_ptr++;
  125. }
  126. block_ptr += row_inc;
  127. }
  128. ADVANCE_BLOCK();
  129. }
  130. break;
  131. /* Fill blocks with 4 colors */
  132. case 0xc0:
  133. colorA = bytestream2_get_be16(&s->gb);
  134. case 0x20:
  135. colorB = bytestream2_get_be16(&s->gb);
  136. /* sort out the colors */
  137. color4[0] = colorB;
  138. color4[1] = 0;
  139. color4[2] = 0;
  140. color4[3] = colorA;
  141. /* red components */
  142. ta = (colorA >> 10) & 0x1F;
  143. tb = (colorB >> 10) & 0x1F;
  144. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
  145. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
  146. /* green components */
  147. ta = (colorA >> 5) & 0x1F;
  148. tb = (colorB >> 5) & 0x1F;
  149. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
  150. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
  151. /* blue components */
  152. ta = colorA & 0x1F;
  153. tb = colorB & 0x1F;
  154. color4[1] |= ((11 * ta + 21 * tb) >> 5);
  155. color4[2] |= ((21 * ta + 11 * tb) >> 5);
  156. if (bytestream2_get_bytes_left(&s->gb) < n_blocks * 4)
  157. return AVERROR_INVALIDDATA;
  158. while (n_blocks--) {
  159. CHECK_BLOCK();
  160. block_ptr = row_ptr + pixel_ptr;
  161. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  162. uint8_t index = bytestream2_get_byteu(&s->gb);
  163. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  164. uint8_t idx = (index >> (2 * (3 - pixel_x))) & 0x03;
  165. pixels[block_ptr] = color4[idx];
  166. block_ptr++;
  167. }
  168. block_ptr += row_inc;
  169. }
  170. ADVANCE_BLOCK();
  171. }
  172. break;
  173. /* Fill block with 16 colors */
  174. case 0x00:
  175. if (bytestream2_get_bytes_left(&s->gb) < 30)
  176. return AVERROR_INVALIDDATA;
  177. CHECK_BLOCK();
  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 = bytestream2_get_be16u(&s->gb);
  184. pixels[block_ptr] = colorA;
  185. block_ptr++;
  186. }
  187. block_ptr += row_inc;
  188. }
  189. ADVANCE_BLOCK();
  190. break;
  191. /* Unknown opcode */
  192. default:
  193. av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
  194. " Skip remaining %d bytes of chunk data.\n", opcode,
  195. bytestream2_get_bytes_left(&s->gb));
  196. return AVERROR_INVALIDDATA;
  197. } /* Opcode switch */
  198. }
  199. return 0;
  200. }
  201. static av_cold int rpza_decode_init(AVCodecContext *avctx)
  202. {
  203. RpzaContext *s = avctx->priv_data;
  204. s->avctx = avctx;
  205. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  206. s->frame = av_frame_alloc();
  207. if (!s->frame)
  208. return AVERROR(ENOMEM);
  209. return 0;
  210. }
  211. static int rpza_decode_frame(AVCodecContext *avctx,
  212. void *data, int *got_frame,
  213. AVPacket *avpkt)
  214. {
  215. RpzaContext *s = avctx->priv_data;
  216. int ret;
  217. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  218. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
  219. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  220. return ret;
  221. }
  222. ret = rpza_decode_stream(s);
  223. if (ret < 0)
  224. return ret;
  225. if ((ret = av_frame_ref(data, s->frame)) < 0)
  226. return ret;
  227. *got_frame = 1;
  228. /* always report that the buffer was completely consumed */
  229. return avpkt->size;
  230. }
  231. static av_cold int rpza_decode_end(AVCodecContext *avctx)
  232. {
  233. RpzaContext *s = avctx->priv_data;
  234. av_frame_free(&s->frame);
  235. return 0;
  236. }
  237. AVCodec ff_rpza_decoder = {
  238. .name = "rpza",
  239. .long_name = NULL_IF_CONFIG_SMALL("QuickTime video (RPZA)"),
  240. .type = AVMEDIA_TYPE_VIDEO,
  241. .id = AV_CODEC_ID_RPZA,
  242. .priv_data_size = sizeof(RpzaContext),
  243. .init = rpza_decode_init,
  244. .close = rpza_decode_end,
  245. .decode = rpza_decode_frame,
  246. .capabilities = AV_CODEC_CAP_DR1,
  247. };