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.

288 lines
8.8KB

  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. 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 : AV_PIX_FMT_BGRA;
  48. s->frame = av_frame_alloc();
  49. if (!s->frame)
  50. return AVERROR(ENOMEM);
  51. s->vflip = avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9);
  52. return 0;
  53. }
  54. static int decode_mvc1(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, int linesize)
  55. {
  56. uint8_t *dst;
  57. uint16_t v[8];
  58. int mask, x, y, i;
  59. x = y= 0;
  60. while (bytestream2_get_bytes_left(gb) >= 6) {
  61. mask = bytestream2_get_be16u(gb);
  62. v[0] = bytestream2_get_be16u(gb);
  63. v[1] = bytestream2_get_be16u(gb);
  64. if ((v[0] & 0x8000)) {
  65. if (bytestream2_get_bytes_left(gb) < 12) {
  66. av_log(avctx, AV_LOG_WARNING, "buffer overflow\n");
  67. return AVERROR_INVALIDDATA;
  68. }
  69. for (i = 2; i < 8; i++)
  70. v[i] = bytestream2_get_be16u(gb);
  71. } else {
  72. v[2] = v[4] = v[6] = v[0];
  73. v[3] = v[5] = v[7] = v[1];
  74. }
  75. #define PIX16(target, true, false) \
  76. i = (mask & target) ? true : false; \
  77. AV_WN16A(dst, (v[i] & 0x7C00) | (v[i] & 0x3E0) | (v[i] & 0x1F)); \
  78. dst += 2;
  79. #define ROW16(row, a1, a0, b1, b0) \
  80. dst = dst_start + (y + row) * linesize + x * 2; \
  81. PIX16(1 << (row * 4), a1, a0) \
  82. PIX16(1 << (row * 4 + 1), a1, a0) \
  83. PIX16(1 << (row * 4 + 2), b1, b0) \
  84. PIX16(1 << (row * 4 + 3), b1, b0)
  85. ROW16(0, 0, 1, 2, 3);
  86. ROW16(1, 0, 1, 2, 3);
  87. ROW16(2, 4, 5, 6, 7);
  88. ROW16(3, 4, 5, 6, 7);
  89. x += 4;
  90. if (x >= width) {
  91. y += 4;
  92. if (y >= height) {
  93. break;
  94. }
  95. x = 0;
  96. }
  97. }
  98. return 0;
  99. }
  100. static void set_4x4_block(uint8_t *dst, int linesize, uint32_t pixel)
  101. {
  102. int i, j;
  103. for (j = 0; j < 4; j++)
  104. for (i = 0; i < 4; i++)
  105. AV_WN32A(dst + j * linesize + i * 4, pixel);
  106. }
  107. #define PIX32(target, true, false) \
  108. AV_WN32A(dst, (mask & target) ? v[true] : v[false]); \
  109. dst += 4;
  110. #define ROW32(row, a1, a0, b1, b0) \
  111. dst = dst_start + (y + row) * linesize + x * 4; \
  112. PIX32(1 << (row * 4), a1, a0) \
  113. PIX32(1 << (row * 4 + 1), a1, a0) \
  114. PIX32(1 << (row * 4 + 2), b1, b0) \
  115. PIX32(1 << (row * 4 + 3), b1, b0)
  116. #define MVC2_BLOCK \
  117. ROW32(0, 1, 0, 3, 2); \
  118. ROW32(1, 1, 0, 3, 2); \
  119. ROW32(2, 5, 4, 7, 6); \
  120. ROW32(3, 5, 4, 7, 6);
  121. static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, 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, 0xFF000000 | (p0 << 16) | (p0 << 8) | p0);
  155. } else {
  156. int g, r;
  157. p0 &= 0x3F;
  158. p0 = (p0 << 2) | (p0 >> 4);
  159. if (bytestream2_get_bytes_left(gb) < 2)
  160. return AVERROR_INVALIDDATA;
  161. g = bytestream2_get_byteu(gb);
  162. r = bytestream2_get_byteu(gb);
  163. set_4x4_block(dst_start + y * linesize + x * 4, linesize, 0xFF000000 | (r << 16) | (g << 8) | p0);
  164. }
  165. } else {
  166. if (bytestream2_get_bytes_left(gb) < 1)
  167. return AVERROR_INVALIDDATA;
  168. p1 = bytestream2_get_byteu(gb);
  169. if ((p1 & 0x80)) {
  170. if ((p0 & 0x7F) == (p1 & 0x7F)) {
  171. set_4x4_block(dst_start + y * linesize + x * 4, linesize, color[p0 & 0x7F]);
  172. } else {
  173. if (bytestream2_get_bytes_left(gb) < 2)
  174. return AVERROR_INVALIDDATA;
  175. v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F];
  176. v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F];
  177. mask = bytestream2_get_le16u(gb);
  178. MVC2_BLOCK
  179. }
  180. } else {
  181. if (bytestream2_get_bytes_left(gb) < 8)
  182. return AVERROR_INVALIDDATA;
  183. v[0] = color[p0 & 0x7F];
  184. v[1] = color[p1 & 0x7F];
  185. for (i = 2; i < 8; i++)
  186. v[i] = color[bytestream2_get_byteu(gb) & 0x7F];
  187. mask = bytestream2_get_le16u(gb);
  188. MVC2_BLOCK
  189. }
  190. }
  191. x += 4;
  192. if (x >= width) {
  193. y += 4;
  194. if (y >= height)
  195. break;
  196. x = 0;
  197. }
  198. }
  199. return 0;
  200. }
  201. static int mvc_decode_frame(AVCodecContext *avctx,
  202. void *data, int *got_frame,
  203. AVPacket *avpkt)
  204. {
  205. MvcContext *s = avctx->priv_data;
  206. GetByteContext gb;
  207. int ret;
  208. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  209. return ret;
  210. bytestream2_init(&gb, avpkt->data, avpkt->size);
  211. if (avctx->codec_id == AV_CODEC_ID_MVC1)
  212. ret = decode_mvc1(avctx, &gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
  213. else
  214. ret = decode_mvc2(avctx, &gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0], s->vflip);
  215. if (ret < 0)
  216. return ret;
  217. *got_frame = 1;
  218. if ((ret = av_frame_ref(data, s->frame)) < 0)
  219. return ret;
  220. return avpkt->size;
  221. }
  222. static av_cold int mvc_decode_end(AVCodecContext *avctx)
  223. {
  224. MvcContext *s = avctx->priv_data;
  225. av_frame_free(&s->frame);
  226. return 0;
  227. }
  228. #if CONFIG_MVC1_DECODER
  229. AVCodec ff_mvc1_decoder = {
  230. .name = "mvc1",
  231. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 1"),
  232. .type = AVMEDIA_TYPE_VIDEO,
  233. .id = AV_CODEC_ID_MVC1,
  234. .priv_data_size = sizeof(MvcContext),
  235. .init = mvc_decode_init,
  236. .close = mvc_decode_end,
  237. .decode = mvc_decode_frame,
  238. .capabilities = CODEC_CAP_DR1,
  239. };
  240. #endif
  241. #if CONFIG_MVC2_DECODER
  242. AVCodec ff_mvc2_decoder = {
  243. .name = "mvc2",
  244. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 2"),
  245. .type = AVMEDIA_TYPE_VIDEO,
  246. .id = AV_CODEC_ID_MVC2,
  247. .priv_data_size = sizeof(MvcContext),
  248. .init = mvc_decode_init,
  249. .close = mvc_decode_end,
  250. .decode = mvc_decode_frame,
  251. .capabilities = CODEC_CAP_DR1,
  252. };
  253. #endif