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.

290 lines
8.6KB

  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 libavcodec/rpza.c
  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 ffmpeg documentation states that RGB555 pixels
  33. * shall be stored in native CPU endianness.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "libavutil/intreadwrite.h"
  39. #include "avcodec.h"
  40. typedef struct RpzaContext {
  41. AVCodecContext *avctx;
  42. AVFrame frame;
  43. const unsigned char *buf;
  44. int size;
  45. } RpzaContext;
  46. #define ADVANCE_BLOCK() \
  47. { \
  48. pixel_ptr += 4; \
  49. if (pixel_ptr >= width) \
  50. { \
  51. pixel_ptr = 0; \
  52. row_ptr += stride * 4; \
  53. } \
  54. total_blocks--; \
  55. if (total_blocks < 0) \
  56. { \
  57. av_log(s->avctx, AV_LOG_ERROR, "warning: block counter just went negative (this should not happen)\n"); \
  58. return; \
  59. } \
  60. }
  61. static void rpza_decode_stream(RpzaContext *s)
  62. {
  63. int width = s->avctx->width;
  64. int stride = s->frame.linesize[0] / 2;
  65. int row_inc = stride - 4;
  66. int stream_ptr = 0;
  67. int chunk_size;
  68. unsigned char opcode;
  69. int n_blocks;
  70. unsigned short colorA = 0, colorB;
  71. unsigned short color4[4];
  72. unsigned char index, idx;
  73. unsigned short ta, tb;
  74. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  75. int row_ptr = 0;
  76. int pixel_ptr = 0;
  77. int block_ptr;
  78. int pixel_x, pixel_y;
  79. int total_blocks;
  80. /* First byte is always 0xe1. Warn if it's different */
  81. if (s->buf[stream_ptr] != 0xe1)
  82. av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0xe1\n",
  83. s->buf[stream_ptr]);
  84. /* Get chunk size, ingnoring first byte */
  85. chunk_size = AV_RB32(&s->buf[stream_ptr]) & 0x00FFFFFF;
  86. stream_ptr += 4;
  87. /* If length mismatch use size from MOV file and try to decode anyway */
  88. if (chunk_size != s->size)
  89. av_log(s->avctx, AV_LOG_ERROR, "MOV chunk size != encoded chunk size; using MOV chunk size\n");
  90. chunk_size = s->size;
  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 (stream_ptr < chunk_size) {
  95. opcode = s->buf[stream_ptr++]; /* Get opcode */
  96. 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) | (s->buf[stream_ptr++]);
  100. opcode = 0;
  101. if ((s->buf[stream_ptr] & 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. switch (opcode & 0xe0) {
  110. /* Skip blocks */
  111. case 0x80:
  112. while (n_blocks--) {
  113. ADVANCE_BLOCK();
  114. }
  115. break;
  116. /* Fill blocks with one color */
  117. case 0xa0:
  118. colorA = AV_RB16 (&s->buf[stream_ptr]);
  119. stream_ptr += 2;
  120. while (n_blocks--) {
  121. block_ptr = row_ptr + pixel_ptr;
  122. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  123. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  124. pixels[block_ptr] = colorA;
  125. block_ptr++;
  126. }
  127. block_ptr += row_inc;
  128. }
  129. ADVANCE_BLOCK();
  130. }
  131. break;
  132. /* Fill blocks with 4 colors */
  133. case 0xc0:
  134. colorA = AV_RB16 (&s->buf[stream_ptr]);
  135. stream_ptr += 2;
  136. case 0x20:
  137. colorB = AV_RB16 (&s->buf[stream_ptr]);
  138. stream_ptr += 2;
  139. /* sort out the colors */
  140. color4[0] = colorB;
  141. color4[1] = 0;
  142. color4[2] = 0;
  143. color4[3] = colorA;
  144. /* red components */
  145. ta = (colorA >> 10) & 0x1F;
  146. tb = (colorB >> 10) & 0x1F;
  147. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
  148. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
  149. /* green components */
  150. ta = (colorA >> 5) & 0x1F;
  151. tb = (colorB >> 5) & 0x1F;
  152. color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
  153. color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
  154. /* blue components */
  155. ta = colorA & 0x1F;
  156. tb = colorB & 0x1F;
  157. color4[1] |= ((11 * ta + 21 * tb) >> 5);
  158. color4[2] |= ((21 * ta + 11 * tb) >> 5);
  159. while (n_blocks--) {
  160. block_ptr = row_ptr + pixel_ptr;
  161. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  162. index = s->buf[stream_ptr++];
  163. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  164. 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. block_ptr = row_ptr + pixel_ptr;
  176. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  177. for (pixel_x = 0; pixel_x < 4; pixel_x++){
  178. /* We already have color of upper left pixel */
  179. if ((pixel_y != 0) || (pixel_x !=0)) {
  180. colorA = AV_RB16 (&s->buf[stream_ptr]);
  181. stream_ptr += 2;
  182. }
  183. pixels[block_ptr] = colorA;
  184. block_ptr++;
  185. }
  186. block_ptr += row_inc;
  187. }
  188. ADVANCE_BLOCK();
  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. chunk_size - stream_ptr);
  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 = PIX_FMT_RGB555;
  204. s->frame.data[0] = NULL;
  205. return 0;
  206. }
  207. static int rpza_decode_frame(AVCodecContext *avctx,
  208. void *data, int *data_size,
  209. AVPacket *avpkt)
  210. {
  211. const uint8_t *buf = avpkt->data;
  212. int buf_size = avpkt->size;
  213. RpzaContext *s = avctx->priv_data;
  214. s->buf = buf;
  215. s->size = buf_size;
  216. s->frame.reference = 1;
  217. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  218. if (avctx->reget_buffer(avctx, &s->frame)) {
  219. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  220. return -1;
  221. }
  222. rpza_decode_stream(s);
  223. *data_size = sizeof(AVFrame);
  224. *(AVFrame*)data = s->frame;
  225. /* always report that the buffer was completely consumed */
  226. return buf_size;
  227. }
  228. static av_cold int rpza_decode_end(AVCodecContext *avctx)
  229. {
  230. RpzaContext *s = avctx->priv_data;
  231. if (s->frame.data[0])
  232. avctx->release_buffer(avctx, &s->frame);
  233. return 0;
  234. }
  235. AVCodec rpza_decoder = {
  236. "rpza",
  237. CODEC_TYPE_VIDEO,
  238. CODEC_ID_RPZA,
  239. sizeof(RpzaContext),
  240. rpza_decode_init,
  241. NULL,
  242. rpza_decode_end,
  243. rpza_decode_frame,
  244. CODEC_CAP_DR1,
  245. .long_name = NULL_IF_CONFIG_SMALL("QuickTime video (RPZA)"),
  246. };