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.

283 lines
8.7KB

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