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.

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