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.

294 lines
9.9KB

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