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.

287 lines
8.9KB

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