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.

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