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.

280 lines
9.5KB

  1. /*
  2. * Silicon Graphics Motion Video Compressor 1 & 2 decoder
  3. * Copyright (c) 2012 Peter Ross
  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. * Silicon Graphics Motion Video Compressor 1 & 2 decoder
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "internal.h"
  29. typedef struct MvcContext {
  30. int vflip;
  31. } MvcContext;
  32. static av_cold int mvc_decode_init(AVCodecContext *avctx)
  33. {
  34. MvcContext *s = avctx->priv_data;
  35. int width = avctx->width;
  36. int height = avctx->height;
  37. int ret;
  38. if (avctx->codec_id == AV_CODEC_ID_MVC1) {
  39. width += 3;
  40. height += 3;
  41. }
  42. width &= ~3;
  43. height &= ~3;
  44. if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
  45. return ret;
  46. avctx->pix_fmt = (avctx->codec_id == AV_CODEC_ID_MVC1) ? AV_PIX_FMT_RGB555
  47. : AV_PIX_FMT_RGB32;
  48. s->vflip = avctx->extradata_size >= 9 &&
  49. !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9);
  50. return 0;
  51. }
  52. static int decode_mvc1(AVCodecContext *avctx, GetByteContext *gb,
  53. uint8_t *dst_start, int width, int height, int linesize)
  54. {
  55. uint8_t *dst;
  56. uint16_t v[8];
  57. int mask, x, y, i;
  58. for (y = 0; y < height; y += 4) {
  59. for (x = 0; x < width; x += 4) {
  60. if (bytestream2_get_bytes_left(gb) < 6)
  61. return 0;
  62. mask = bytestream2_get_be16u(gb);
  63. v[0] = bytestream2_get_be16u(gb);
  64. v[1] = bytestream2_get_be16u(gb);
  65. if ((v[0] & 0x8000)) {
  66. if (bytestream2_get_bytes_left(gb) < 12) {
  67. av_log(avctx, AV_LOG_WARNING, "buffer overflow\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. for (i = 2; i < 8; i++)
  71. v[i] = bytestream2_get_be16u(gb);
  72. } else {
  73. v[2] = v[4] = v[6] = v[0];
  74. v[3] = v[5] = v[7] = v[1];
  75. }
  76. #define PIX16(target, true, false) \
  77. i = (mask & target) ? true : false; \
  78. AV_WN16A(dst, v[i] & 0x7FFF); \
  79. dst += 2;
  80. #define ROW16(row, a1, a0, b1, b0) \
  81. dst = dst_start + (y + row) * linesize + x * 2; \
  82. PIX16(1 << (row * 4), a1, a0) \
  83. PIX16(1 << (row * 4 + 1), a1, a0) \
  84. PIX16(1 << (row * 4 + 2), b1, b0) \
  85. PIX16(1 << (row * 4 + 3), b1, b0)
  86. ROW16(0, 0, 1, 2, 3);
  87. ROW16(1, 0, 1, 2, 3);
  88. ROW16(2, 4, 5, 6, 7);
  89. ROW16(3, 4, 5, 6, 7);
  90. }
  91. }
  92. return 0;
  93. }
  94. static void set_4x4_block(uint8_t *dst, int linesize, uint32_t pixel)
  95. {
  96. int i, j;
  97. for (j = 0; j < 4; j++)
  98. for (i = 0; i < 4; i++)
  99. AV_WN32A(dst + j * linesize + i * 4, pixel);
  100. }
  101. #define PIX32(target, true, false) \
  102. AV_WN32A(dst, (mask & target) ? v[true] : v[false]); \
  103. dst += 4;
  104. #define ROW32(row, a1, a0, b1, b0) \
  105. dst = dst_start + (y + row) * linesize + x * 4; \
  106. PIX32(1 << (row * 4), a1, a0) \
  107. PIX32(1 << (row * 4 + 1), a1, a0) \
  108. PIX32(1 << (row * 4 + 2), b1, b0) \
  109. PIX32(1 << (row * 4 + 3), b1, b0)
  110. #define MVC2_BLOCK \
  111. ROW32(0, 1, 0, 3, 2); \
  112. ROW32(1, 1, 0, 3, 2); \
  113. ROW32(2, 5, 4, 7, 6); \
  114. ROW32(3, 5, 4, 7, 6);
  115. static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb,
  116. uint8_t *dst_start, int width, int height,
  117. int linesize, int vflip)
  118. {
  119. uint8_t *dst;
  120. uint32_t color[128], v[8];
  121. int w, h, nb_colors, i, x, y, p0, p1, mask;
  122. if (bytestream2_get_bytes_left(gb) < 6)
  123. return AVERROR_INVALIDDATA;
  124. w = bytestream2_get_be16u(gb);
  125. h = bytestream2_get_be16u(gb);
  126. if ((w & ~3) != width || (h & ~3) != height)
  127. av_log(avctx, AV_LOG_WARNING, "dimension mismatch\n");
  128. if (bytestream2_get_byteu(gb)) {
  129. avpriv_request_sample(avctx, "bitmap feature");
  130. return AVERROR_PATCHWELCOME;
  131. }
  132. nb_colors = bytestream2_get_byteu(gb);
  133. if (bytestream2_get_bytes_left(gb) < nb_colors * 3)
  134. return AVERROR_INVALIDDATA;
  135. for (i = 0; i < FFMIN(nb_colors, 128); i++)
  136. color[i] = 0xFF000000 | bytestream2_get_be24u(gb);
  137. if (nb_colors > 128)
  138. bytestream2_skip(gb, (nb_colors - 128) * 3);
  139. if (vflip) {
  140. dst_start += (height - 1) * linesize;
  141. linesize = -linesize;
  142. }
  143. x = y = 0;
  144. while (bytestream2_get_bytes_left(gb) >= 1) {
  145. p0 = bytestream2_get_byteu(gb);
  146. if ((p0 & 0x80)) {
  147. if ((p0 & 0x40)) {
  148. p0 &= 0x3F;
  149. p0 = (p0 << 2) | (p0 >> 4);
  150. set_4x4_block(dst_start + y * linesize + x * 4, linesize,
  151. 0xFF000000 | (p0 << 16) | (p0 << 8) | p0);
  152. } else {
  153. int g, r;
  154. p0 &= 0x3F;
  155. p0 = (p0 << 2) | (p0 >> 4);
  156. if (bytestream2_get_bytes_left(gb) < 2)
  157. return AVERROR_INVALIDDATA;
  158. g = bytestream2_get_byteu(gb);
  159. r = bytestream2_get_byteu(gb);
  160. set_4x4_block(dst_start + y * linesize + x * 4, linesize,
  161. 0xFF000000 | (r << 16) | (g << 8) | p0);
  162. }
  163. } else {
  164. if (bytestream2_get_bytes_left(gb) < 1)
  165. return AVERROR_INVALIDDATA;
  166. p1 = bytestream2_get_byteu(gb);
  167. if ((p1 & 0x80)) {
  168. if ((p0 & 0x7F) == (p1 & 0x7F)) {
  169. set_4x4_block(dst_start + y * linesize + x * 4, linesize,
  170. color[p0 & 0x7F]);
  171. } else {
  172. if (bytestream2_get_bytes_left(gb) < 2)
  173. return AVERROR_INVALIDDATA;
  174. v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F];
  175. v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F];
  176. mask = bytestream2_get_le16u(gb);
  177. MVC2_BLOCK
  178. }
  179. } else {
  180. if (bytestream2_get_bytes_left(gb) < 8)
  181. return AVERROR_INVALIDDATA;
  182. v[0] = color[p0 & 0x7F];
  183. v[1] = color[p1 & 0x7F];
  184. for (i = 2; i < 8; i++)
  185. v[i] = color[bytestream2_get_byteu(gb) & 0x7F];
  186. mask = bytestream2_get_le16u(gb);
  187. MVC2_BLOCK
  188. }
  189. }
  190. x += 4;
  191. if (x >= width) {
  192. y += 4;
  193. if (y >= height)
  194. break;
  195. x = 0;
  196. }
  197. }
  198. return 0;
  199. }
  200. static int mvc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  201. AVPacket *avpkt)
  202. {
  203. MvcContext *s = avctx->priv_data;
  204. AVFrame *frame = data;
  205. GetByteContext gb;
  206. int ret;
  207. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  208. return ret;
  209. bytestream2_init(&gb, avpkt->data, avpkt->size);
  210. if (avctx->codec_id == AV_CODEC_ID_MVC1)
  211. ret = decode_mvc1(avctx, &gb, frame->data[0],
  212. avctx->width, avctx->height, frame->linesize[0]);
  213. else
  214. ret = decode_mvc2(avctx, &gb, frame->data[0],
  215. avctx->width, avctx->height, frame->linesize[0],
  216. s->vflip);
  217. if (ret < 0)
  218. return ret;
  219. frame->pict_type = AV_PICTURE_TYPE_I;
  220. frame->key_frame = 1;
  221. *got_frame = 1;
  222. return avpkt->size;
  223. }
  224. #if CONFIG_MVC1_DECODER
  225. AVCodec ff_mvc1_decoder = {
  226. .name = "mvc1",
  227. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 1"),
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .id = AV_CODEC_ID_MVC1,
  230. .priv_data_size = sizeof(MvcContext),
  231. .init = mvc_decode_init,
  232. .decode = mvc_decode_frame,
  233. .capabilities = AV_CODEC_CAP_DR1,
  234. };
  235. #endif
  236. #if CONFIG_MVC2_DECODER
  237. AVCodec ff_mvc2_decoder = {
  238. .name = "mvc2",
  239. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 2"),
  240. .type = AVMEDIA_TYPE_VIDEO,
  241. .id = AV_CODEC_ID_MVC2,
  242. .priv_data_size = sizeof(MvcContext),
  243. .init = mvc_decode_init,
  244. .decode = mvc_decode_frame,
  245. .capabilities = AV_CODEC_CAP_DR1,
  246. };
  247. #endif