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.

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