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.

412 lines
12KB

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