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.

282 lines
8.4KB

  1. /*
  2. * Beam Software VB decoder
  3. * Copyright (c) 2007 Konstantin Shishkov
  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. * VB Video decoder
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "internal.h"
  30. enum VBFlags {
  31. VB_HAS_GMC = 0x01,
  32. VB_HAS_AUDIO = 0x04,
  33. VB_HAS_VIDEO = 0x08,
  34. VB_HAS_PALETTE = 0x10,
  35. VB_HAS_LENGTH = 0x20
  36. };
  37. typedef struct VBDecContext {
  38. AVCodecContext *avctx;
  39. AVFrame pic;
  40. uint8_t *frame, *prev_frame;
  41. uint32_t pal[AVPALETTE_COUNT];
  42. GetByteContext stream;
  43. } VBDecContext;
  44. static const uint16_t vb_patterns[64] = {
  45. 0x0660, 0xFF00, 0xCCCC, 0xF000, 0x8888, 0x000F, 0x1111, 0xFEC8,
  46. 0x8CEF, 0x137F, 0xF731, 0xC800, 0x008C, 0x0013, 0x3100, 0xCC00,
  47. 0x00CC, 0x0033, 0x3300, 0x0FF0, 0x6666, 0x00F0, 0x0F00, 0x2222,
  48. 0x4444, 0xF600, 0x8CC8, 0x006F, 0x1331, 0x318C, 0xC813, 0x33CC,
  49. 0x6600, 0x0CC0, 0x0066, 0x0330, 0xF900, 0xC88C, 0x009F, 0x3113,
  50. 0x6000, 0x0880, 0x0006, 0x0110, 0xCC88, 0xFC00, 0x00CF, 0x88CC,
  51. 0x003F, 0x1133, 0x3311, 0xF300, 0x6FF6, 0x0603, 0x08C6, 0x8C63,
  52. 0xC631, 0x6310, 0xC060, 0x0136, 0x136C, 0x36C8, 0x6C80, 0x324C
  53. };
  54. static void vb_decode_palette(VBDecContext *c, int data_size)
  55. {
  56. int start, size, i;
  57. start = bytestream2_get_byte(&c->stream);
  58. size = (bytestream2_get_byte(&c->stream) - 1) & 0xFF;
  59. if (start + size > 255) {
  60. av_log(c->avctx, AV_LOG_ERROR, "Palette change runs beyond entry 256\n");
  61. return;
  62. }
  63. if (size*3+2 > data_size) {
  64. av_log(c->avctx, AV_LOG_ERROR, "Palette data runs beyond chunk size\n");
  65. return;
  66. }
  67. for (i = start; i <= start + size; i++)
  68. c->pal[i] = bytestream2_get_be24(&c->stream);
  69. }
  70. static inline int check_pixel(uint8_t *buf, uint8_t *start, uint8_t *end)
  71. {
  72. return buf >= start && buf < end;
  73. }
  74. static inline int check_line(uint8_t *buf, uint8_t *start, uint8_t *end)
  75. {
  76. return buf >= start && (buf + 4) <= end;
  77. }
  78. static int vb_decode_framedata(VBDecContext *c, int offset)
  79. {
  80. GetByteContext g;
  81. uint8_t *prev, *cur;
  82. int blk, blocks, t, blk2;
  83. int blocktypes = 0;
  84. int x, y, a, b;
  85. int pattype, pattern;
  86. const int width = c->avctx->width;
  87. uint8_t *pstart = c->prev_frame;
  88. uint8_t *pend = c->prev_frame + width*c->avctx->height;
  89. g = c->stream;
  90. prev = c->prev_frame + offset;
  91. cur = c->frame;
  92. blocks = (c->avctx->width >> 2) * (c->avctx->height >> 2);
  93. blk2 = 0;
  94. for (blk = 0; blk < blocks; blk++) {
  95. if (!(blk & 3)) {
  96. blocktypes = bytestream2_get_byte(&g);
  97. }
  98. switch (blocktypes & 0xC0) {
  99. case 0x00: //skip
  100. for (y = 0; y < 4; y++)
  101. if (check_line(prev + y*width, pstart, pend))
  102. memcpy(cur + y*width, prev + y*width, 4);
  103. else
  104. memset(cur + y*width, 0, 4);
  105. break;
  106. case 0x40:
  107. t = bytestream2_get_byte(&g);
  108. if (!t) { //raw block
  109. if (bytestream2_get_bytes_left(&g) < 16) {
  110. av_log(c->avctx, AV_LOG_ERROR, "Insufficient data\n");
  111. return AVERROR_INVALIDDATA;
  112. }
  113. for (y = 0; y < 4; y++)
  114. bytestream2_get_buffer(&g, cur + y * width, 4);
  115. } else { // motion compensation
  116. x = ((t & 0xF)^8) - 8;
  117. y = ((t >> 4) ^8) - 8;
  118. t = x + y*width;
  119. for (y = 0; y < 4; y++)
  120. if (check_line(prev + t + y*width, pstart, pend))
  121. memcpy(cur + y*width, prev + t + y*width, 4);
  122. else
  123. memset(cur + y*width, 0, 4);
  124. }
  125. break;
  126. case 0x80: // fill
  127. t = bytestream2_get_byte(&g);
  128. for (y = 0; y < 4; y++)
  129. memset(cur + y*width, t, 4);
  130. break;
  131. case 0xC0: // pattern fill
  132. t = bytestream2_get_byte(&g);
  133. pattype = t >> 6;
  134. pattern = vb_patterns[t & 0x3F];
  135. switch (pattype) {
  136. case 0:
  137. a = bytestream2_get_byte(&g);
  138. b = bytestream2_get_byte(&g);
  139. for (y = 0; y < 4; y++)
  140. for (x = 0; x < 4; x++, pattern >>= 1)
  141. cur[x + y*width] = (pattern & 1) ? b : a;
  142. break;
  143. case 1:
  144. pattern = ~pattern;
  145. case 2:
  146. a = bytestream2_get_byte(&g);
  147. for (y = 0; y < 4; y++)
  148. for (x = 0; x < 4; x++, pattern >>= 1)
  149. if (pattern & 1 && check_pixel(prev + x + y*width, pstart, pend))
  150. cur[x + y*width] = prev[x + y*width];
  151. else
  152. cur[x + y*width] = a;
  153. break;
  154. case 3:
  155. av_log(c->avctx, AV_LOG_ERROR, "Invalid opcode seen @%d\n", blk);
  156. return AVERROR_INVALIDDATA;
  157. }
  158. break;
  159. }
  160. blocktypes <<= 2;
  161. cur += 4;
  162. prev += 4;
  163. blk2++;
  164. if (blk2 == (width >> 2)) {
  165. blk2 = 0;
  166. cur += width * 3;
  167. prev += width * 3;
  168. }
  169. }
  170. return 0;
  171. }
  172. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  173. AVPacket *avpkt)
  174. {
  175. VBDecContext * const c = avctx->priv_data;
  176. uint8_t *outptr, *srcptr;
  177. int i, j, ret;
  178. int flags;
  179. uint32_t size;
  180. int offset = 0;
  181. bytestream2_init(&c->stream, avpkt->data, avpkt->size);
  182. if (c->pic.data[0])
  183. avctx->release_buffer(avctx, &c->pic);
  184. c->pic.reference = 1;
  185. if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
  186. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  187. return ret;
  188. }
  189. flags = bytestream2_get_le16(&c->stream);
  190. if (flags & VB_HAS_GMC) {
  191. i = (int16_t)bytestream2_get_le16(&c->stream);
  192. j = (int16_t)bytestream2_get_le16(&c->stream);
  193. offset = i + j * avctx->width;
  194. }
  195. if (flags & VB_HAS_VIDEO) {
  196. size = bytestream2_get_le32(&c->stream);
  197. vb_decode_framedata(c, offset);
  198. bytestream2_skip(&c->stream, size - 4);
  199. }
  200. if (flags & VB_HAS_PALETTE) {
  201. size = bytestream2_get_le32(&c->stream);
  202. vb_decode_palette(c, size);
  203. }
  204. memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
  205. c->pic.palette_has_changed = flags & VB_HAS_PALETTE;
  206. outptr = c->pic.data[0];
  207. srcptr = c->frame;
  208. for (i = 0; i < avctx->height; i++) {
  209. memcpy(outptr, srcptr, avctx->width);
  210. srcptr += avctx->width;
  211. outptr += c->pic.linesize[0];
  212. }
  213. FFSWAP(uint8_t*, c->frame, c->prev_frame);
  214. *got_frame = 1;
  215. *(AVFrame*)data = c->pic;
  216. /* always report that the buffer was completely consumed */
  217. return avpkt->size;
  218. }
  219. static av_cold int decode_init(AVCodecContext *avctx)
  220. {
  221. VBDecContext * const c = avctx->priv_data;
  222. c->avctx = avctx;
  223. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  224. c->frame = av_mallocz(avctx->width * avctx->height);
  225. c->prev_frame = av_mallocz(avctx->width * avctx->height);
  226. return 0;
  227. }
  228. static av_cold int decode_end(AVCodecContext *avctx)
  229. {
  230. VBDecContext *c = avctx->priv_data;
  231. av_freep(&c->frame);
  232. av_freep(&c->prev_frame);
  233. if(c->pic.data[0])
  234. avctx->release_buffer(avctx, &c->pic);
  235. return 0;
  236. }
  237. AVCodec ff_vb_decoder = {
  238. .name = "vb",
  239. .type = AVMEDIA_TYPE_VIDEO,
  240. .id = AV_CODEC_ID_VB,
  241. .priv_data_size = sizeof(VBDecContext),
  242. .init = decode_init,
  243. .close = decode_end,
  244. .decode = decode_frame,
  245. .long_name = NULL_IF_CONFIG_SMALL("Beam Software VB"),
  246. .capabilities = CODEC_CAP_DR1,
  247. };