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.

387 lines
12KB

  1. /*
  2. * Packed Animation File video decoder
  3. * Copyright (c) 2012 Paul B Mahol
  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. #include "libavutil/imgutils.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "copy_block.h"
  25. #include "internal.h"
  26. static const uint8_t block_sequences[16][8] = {
  27. { 0, 0, 0, 0, 0, 0, 0, 0 }, { 2, 0, 0, 0, 0, 0, 0, 0 },
  28. { 5, 7, 0, 0, 0, 0, 0, 0 }, { 5, 0, 0, 0, 0, 0, 0, 0 },
  29. { 6, 0, 0, 0, 0, 0, 0, 0 }, { 5, 7, 5, 7, 0, 0, 0, 0 },
  30. { 5, 7, 5, 0, 0, 0, 0, 0 }, { 5, 7, 6, 0, 0, 0, 0, 0 },
  31. { 5, 5, 0, 0, 0, 0, 0, 0 }, { 3, 0, 0, 0, 0, 0, 0, 0 },
  32. { 6, 6, 0, 0, 0, 0, 0, 0 }, { 2, 4, 0, 0, 0, 0, 0, 0 },
  33. { 2, 4, 5, 7, 0, 0, 0, 0 }, { 2, 4, 5, 0, 0, 0, 0, 0 },
  34. { 2, 4, 6, 0, 0, 0, 0, 0 }, { 2, 4, 5, 7, 5, 7, 0, 0 },
  35. };
  36. typedef struct PAFVideoDecContext {
  37. AVFrame *pic;
  38. GetByteContext gb;
  39. int width;
  40. int height;
  41. int current_frame;
  42. uint8_t *frame[4];
  43. int frame_size;
  44. int video_size;
  45. uint8_t *opcodes;
  46. } PAFVideoDecContext;
  47. static av_cold int paf_video_close(AVCodecContext *avctx)
  48. {
  49. PAFVideoDecContext *c = avctx->priv_data;
  50. int i;
  51. av_frame_free(&c->pic);
  52. for (i = 0; i < 4; i++)
  53. av_freep(&c->frame[i]);
  54. return 0;
  55. }
  56. static av_cold int paf_video_init(AVCodecContext *avctx)
  57. {
  58. PAFVideoDecContext *c = avctx->priv_data;
  59. int i;
  60. c->width = avctx->width;
  61. c->height = avctx->height;
  62. if (avctx->height & 3 || avctx->width & 3) {
  63. av_log(avctx, AV_LOG_ERROR,
  64. "width %d and height %d must be multiplie of 4.\n",
  65. avctx->width, avctx->height);
  66. return AVERROR_INVALIDDATA;
  67. }
  68. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  69. c->pic = av_frame_alloc();
  70. if (!c->pic)
  71. return AVERROR(ENOMEM);
  72. c->frame_size = avctx->width * FFALIGN(avctx->height, 256);
  73. c->video_size = avctx->width * avctx->height;
  74. for (i = 0; i < 4; i++) {
  75. c->frame[i] = av_mallocz(c->frame_size);
  76. if (!c->frame[i]) {
  77. paf_video_close(avctx);
  78. return AVERROR(ENOMEM);
  79. }
  80. }
  81. return 0;
  82. }
  83. static void read4x4block(PAFVideoDecContext *c, uint8_t *dst, int width)
  84. {
  85. int i;
  86. for (i = 0; i < 4; i++) {
  87. bytestream2_get_buffer(&c->gb, dst, 4);
  88. dst += width;
  89. }
  90. }
  91. static void copy_color_mask(uint8_t *dst, int width, uint8_t mask, uint8_t color)
  92. {
  93. int i;
  94. for (i = 0; i < 4; i++) {
  95. if (mask & (1 << 7 - i))
  96. dst[i] = color;
  97. if (mask & (1 << 3 - i))
  98. dst[width + i] = color;
  99. }
  100. }
  101. static void copy_src_mask(uint8_t *dst, int width, uint8_t mask, const uint8_t *src)
  102. {
  103. int i;
  104. for (i = 0; i < 4; i++) {
  105. if (mask & (1 << 7 - i))
  106. dst[i] = src[i];
  107. if (mask & (1 << 3 - i))
  108. dst[width + i] = src[width + i];
  109. }
  110. }
  111. static void set_src_position(PAFVideoDecContext *c,
  112. const uint8_t **p,
  113. const uint8_t **pend)
  114. {
  115. int val = bytestream2_get_be16(&c->gb);
  116. int page = val >> 14;
  117. int x = (val & 0x7F);
  118. int y = ((val >> 7) & 0x7F);
  119. *p = c->frame[page] + x * 2 + y * 2 * c->width;
  120. *pend = c->frame[page] + c->frame_size;
  121. }
  122. static int decode_0(PAFVideoDecContext *c, uint8_t *pkt, uint8_t code)
  123. {
  124. uint32_t opcode_size, offset;
  125. uint8_t *dst, *dend, mask = 0, color = 0;
  126. const uint8_t *src, *send, *opcodes;
  127. int i, j, op = 0;
  128. i = bytestream2_get_byte(&c->gb);
  129. if (i) {
  130. if (code & 0x10) {
  131. int pos = bytestream2_tell(&c->gb) & 3;
  132. if (pos)
  133. bytestream2_skip(&c->gb, 4 - pos);
  134. }
  135. do {
  136. int page, val, x, y;
  137. val = bytestream2_get_be16(&c->gb);
  138. page = val >> 14;
  139. x = (val & 0x7F) * 2;
  140. y = ((val >> 7) & 0x7F) * 2;
  141. dst = c->frame[page] + x + y * c->width;
  142. dend = c->frame[page] + c->frame_size;
  143. offset = (x & 0x7F) * 2;
  144. j = bytestream2_get_le16(&c->gb) + offset;
  145. do {
  146. offset++;
  147. if (dst + 3 * c->width + 4 > dend)
  148. return AVERROR_INVALIDDATA;
  149. read4x4block(c, dst, c->width);
  150. if ((offset & 0x3F) == 0)
  151. dst += c->width * 3;
  152. dst += 4;
  153. } while (offset < j);
  154. } while (--i);
  155. }
  156. dst = c->frame[c->current_frame];
  157. dend = c->frame[c->current_frame] + c->frame_size;
  158. do {
  159. set_src_position(c, &src, &send);
  160. if ((src + 3 * c->width + 4 > send) ||
  161. (dst + 3 * c->width + 4 > dend))
  162. return AVERROR_INVALIDDATA;
  163. copy_block4(dst, src, c->width, c->width, 4);
  164. i++;
  165. if ((i & 0x3F) == 0)
  166. dst += c->width * 3;
  167. dst += 4;
  168. } while (i < c->video_size / 16);
  169. opcode_size = bytestream2_get_le16(&c->gb);
  170. bytestream2_skip(&c->gb, 2);
  171. if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
  172. return AVERROR_INVALIDDATA;
  173. opcodes = pkt + bytestream2_tell(&c->gb);
  174. bytestream2_skipu(&c->gb, opcode_size);
  175. dst = c->frame[c->current_frame];
  176. for (i = 0; i < c->height; i += 4, dst += c->width * 3)
  177. for (j = 0; j < c->width; j += 4, dst += 4) {
  178. int opcode, k = 0;
  179. if (op > opcode_size)
  180. return AVERROR_INVALIDDATA;
  181. if (j & 4) {
  182. opcode = opcodes[op] & 15;
  183. op++;
  184. } else {
  185. opcode = opcodes[op] >> 4;
  186. }
  187. while (block_sequences[opcode][k]) {
  188. offset = c->width * 2;
  189. code = block_sequences[opcode][k++];
  190. switch (code) {
  191. case 2:
  192. offset = 0;
  193. case 3:
  194. color = bytestream2_get_byte(&c->gb);
  195. case 4:
  196. mask = bytestream2_get_byte(&c->gb);
  197. copy_color_mask(dst + offset, c->width, mask, color);
  198. break;
  199. case 5:
  200. offset = 0;
  201. case 6:
  202. set_src_position(c, &src, &send);
  203. case 7:
  204. if (src + offset + c->width + 4 > send)
  205. return AVERROR_INVALIDDATA;
  206. mask = bytestream2_get_byte(&c->gb);
  207. copy_src_mask(dst + offset, c->width, mask, src + offset);
  208. break;
  209. }
  210. }
  211. }
  212. return 0;
  213. }
  214. static int paf_video_decode(AVCodecContext *avctx, void *data,
  215. int *got_frame, AVPacket *pkt)
  216. {
  217. PAFVideoDecContext *c = avctx->priv_data;
  218. uint8_t code, *dst, *end;
  219. int i, frame, ret;
  220. if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
  221. return ret;
  222. bytestream2_init(&c->gb, pkt->data, pkt->size);
  223. code = bytestream2_get_byte(&c->gb);
  224. if (code & 0x20) { // frame is keyframe
  225. for (i = 0; i < 4; i++)
  226. memset(c->frame[i], 0, c->frame_size);
  227. memset(c->pic->data[1], 0, AVPALETTE_SIZE);
  228. c->current_frame = 0;
  229. c->pic->key_frame = 1;
  230. c->pic->pict_type = AV_PICTURE_TYPE_I;
  231. } else {
  232. c->pic->key_frame = 0;
  233. c->pic->pict_type = AV_PICTURE_TYPE_P;
  234. }
  235. if (code & 0x40) { // palette update
  236. uint32_t *out = (uint32_t *)c->pic->data[1];
  237. int index, count;
  238. index = bytestream2_get_byte(&c->gb);
  239. count = bytestream2_get_byte(&c->gb) + 1;
  240. if (index + count > 256)
  241. return AVERROR_INVALIDDATA;
  242. if (bytestream2_get_bytes_left(&c->gb) < 3 * count)
  243. return AVERROR_INVALIDDATA;
  244. out += index;
  245. for (i = 0; i < count; i++) {
  246. unsigned r, g, b;
  247. r = bytestream2_get_byteu(&c->gb);
  248. r = r << 2 | r >> 4;
  249. g = bytestream2_get_byteu(&c->gb);
  250. g = g << 2 | g >> 4;
  251. b = bytestream2_get_byteu(&c->gb);
  252. b = b << 2 | b >> 4;
  253. *out++ = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  254. }
  255. c->pic->palette_has_changed = 1;
  256. }
  257. switch (code & 0x0F) {
  258. case 0:
  259. /* Block-based motion compensation using 4x4 blocks with either
  260. * horizontal or vertical vectors; might incorporate VQ as well. */
  261. if ((ret = decode_0(c, pkt->data, code)) < 0)
  262. return ret;
  263. break;
  264. case 1:
  265. /* Uncompressed data. This mode specifies that (width * height) bytes
  266. * should be copied directly from the encoded buffer into the output. */
  267. dst = c->frame[c->current_frame];
  268. // possibly chunk length data
  269. bytestream2_skip(&c->gb, 2);
  270. if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
  271. return AVERROR_INVALIDDATA;
  272. bytestream2_get_bufferu(&c->gb, dst, c->video_size);
  273. break;
  274. case 2:
  275. /* Copy reference frame: Consume the next byte in the stream as the
  276. * reference frame (which should be 0, 1, 2, or 3, and should not be
  277. * the same as the current frame number). */
  278. frame = bytestream2_get_byte(&c->gb);
  279. if (frame > 3)
  280. return AVERROR_INVALIDDATA;
  281. if (frame != c->current_frame)
  282. memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
  283. break;
  284. case 4:
  285. /* Run length encoding.*/
  286. dst = c->frame[c->current_frame];
  287. end = dst + c->video_size;
  288. bytestream2_skip(&c->gb, 2);
  289. while (dst < end) {
  290. int8_t code;
  291. int count;
  292. if (bytestream2_get_bytes_left(&c->gb) < 2)
  293. return AVERROR_INVALIDDATA;
  294. code = bytestream2_get_byteu(&c->gb);
  295. count = FFABS(code) + 1;
  296. if (dst + count > end)
  297. return AVERROR_INVALIDDATA;
  298. if (code < 0)
  299. memset(dst, bytestream2_get_byteu(&c->gb), count);
  300. else
  301. bytestream2_get_buffer(&c->gb, dst, count);
  302. dst += count;
  303. }
  304. break;
  305. default:
  306. avpriv_request_sample(avctx, "unknown/invalid code");
  307. return AVERROR_INVALIDDATA;
  308. }
  309. av_image_copy_plane(c->pic->data[0], c->pic->linesize[0],
  310. c->frame[c->current_frame], c->width,
  311. c->width, c->height);
  312. c->current_frame = (c->current_frame + 1) & 3;
  313. if ((ret = av_frame_ref(data, c->pic)) < 0)
  314. return ret;
  315. *got_frame = 1;
  316. return pkt->size;
  317. }
  318. AVCodec ff_paf_video_decoder = {
  319. .name = "paf_video",
  320. .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .id = AV_CODEC_ID_PAF_VIDEO,
  323. .priv_data_size = sizeof(PAFVideoDecContext),
  324. .init = paf_video_init,
  325. .close = paf_video_close,
  326. .decode = paf_video_decode,
  327. .capabilities = AV_CODEC_CAP_DR1,
  328. };