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
9.2KB

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