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.

285 lines
8.7KB

  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 <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 = -4;
  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,
  87. "MOV chunk size %d != encoded chunk size %d\n",
  88. chunk_size,
  89. bytestream2_get_bytes_left(&s->gb) + 4
  90. );
  91. /* Number of 4x4 blocks in frame. */
  92. total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
  93. /* Process chunk data */
  94. while (bytestream2_get_bytes_left(&s->gb)) {
  95. uint8_t opcode = bytestream2_get_byte(&s->gb); /* Get opcode */
  96. int n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
  97. /* If opcode MSbit is 0, we need more data to decide what to do */
  98. if ((opcode & 0x80) == 0) {
  99. colorA = (opcode << 8) | bytestream2_get_byte(&s->gb);
  100. opcode = 0;
  101. if ((bytestream2_peek_byte(&s->gb) & 0x80) != 0) {
  102. /* Must behave as opcode 110xxxxx, using colorA computed
  103. * above. Use fake opcode 0x20 to enter switch block at
  104. * the right place */
  105. opcode = 0x20;
  106. n_blocks = 1;
  107. }
  108. }
  109. n_blocks = FFMIN(n_blocks, total_blocks);
  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 = bytestream2_get_be16(&s->gb);
  120. while (n_blocks--) {
  121. ADVANCE_BLOCK()
  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. }
  131. break;
  132. /* Fill blocks with 4 colors */
  133. case 0xc0:
  134. colorA = bytestream2_get_be16(&s->gb);
  135. case 0x20:
  136. colorB = bytestream2_get_be16(&s->gb);
  137. /* sort out the colors */
  138. color4[0] = colorB;
  139. color4[1] = 0;
  140. color4[2] = 0;
  141. color4[3] = colorA;
  142. /* red components */
  143. ta = (colorA >> 10) & 0x1F;
  144. tb = (colorB >> 10) & 0x1F;
  145. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
  146. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
  147. /* green components */
  148. ta = (colorA >> 5) & 0x1F;
  149. tb = (colorB >> 5) & 0x1F;
  150. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
  151. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
  152. /* blue components */
  153. ta = colorA & 0x1F;
  154. tb = colorB & 0x1F;
  155. color4[1] |= ((11 * ta + 21 * tb) >> 5);
  156. color4[2] |= ((21 * ta + 11 * tb) >> 5);
  157. if (bytestream2_get_bytes_left(&s->gb) < n_blocks * 4)
  158. return;
  159. while (n_blocks--) {
  160. ADVANCE_BLOCK();
  161. block_ptr = row_ptr + pixel_ptr;
  162. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  163. uint8_t index = bytestream2_get_byteu(&s->gb);
  164. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  165. uint8_t idx = (index >> (2 * (3 - pixel_x))) & 0x03;
  166. pixels[block_ptr] = color4[idx];
  167. block_ptr++;
  168. }
  169. block_ptr += row_inc;
  170. }
  171. }
  172. break;
  173. /* Fill block with 16 colors */
  174. case 0x00:
  175. if (bytestream2_get_bytes_left(&s->gb) < 30)
  176. return;
  177. ADVANCE_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. break;
  190. /* Unknown opcode */
  191. default:
  192. av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
  193. " Skip remaining %d bytes of chunk data.\n", opcode,
  194. bytestream2_get_bytes_left(&s->gb));
  195. return;
  196. } /* Opcode switch */
  197. }
  198. }
  199. static av_cold int rpza_decode_init(AVCodecContext *avctx)
  200. {
  201. RpzaContext *s = avctx->priv_data;
  202. s->avctx = avctx;
  203. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  204. s->frame = av_frame_alloc();
  205. if (!s->frame)
  206. return AVERROR(ENOMEM);
  207. return 0;
  208. }
  209. static int rpza_decode_frame(AVCodecContext *avctx,
  210. void *data, int *got_frame,
  211. AVPacket *avpkt)
  212. {
  213. RpzaContext *s = avctx->priv_data;
  214. int ret;
  215. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  216. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  217. return ret;
  218. rpza_decode_stream(s);
  219. if ((ret = av_frame_ref(data, s->frame)) < 0)
  220. return ret;
  221. *got_frame = 1;
  222. /* always report that the buffer was completely consumed */
  223. return avpkt->size;
  224. }
  225. static av_cold int rpza_decode_end(AVCodecContext *avctx)
  226. {
  227. RpzaContext *s = avctx->priv_data;
  228. av_frame_free(&s->frame);
  229. return 0;
  230. }
  231. AVCodec ff_rpza_decoder = {
  232. .name = "rpza",
  233. .long_name = NULL_IF_CONFIG_SMALL("QuickTime video (RPZA)"),
  234. .type = AVMEDIA_TYPE_VIDEO,
  235. .id = AV_CODEC_ID_RPZA,
  236. .priv_data_size = sizeof(RpzaContext),
  237. .init = rpza_decode_init,
  238. .close = rpza_decode_end,
  239. .decode = rpza_decode_frame,
  240. .capabilities = AV_CODEC_CAP_DR1,
  241. };