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.8KB

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