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.

453 lines
13KB

  1. /*
  2. * Packed Animation File video and audio 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/intreadwrite.h"
  22. #include "libavcodec/dsputil.h"
  23. #include "libavcodec/paf.h"
  24. #include "bytestream.h"
  25. #include "avcodec.h"
  26. static const uint8_t block_sequences[16][8] =
  27. {
  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 current_frame;
  49. uint8_t *frame[4];
  50. int frame_size;
  51. int video_size;
  52. uint8_t *opcodes;
  53. } PAFVideoDecContext;
  54. static av_cold int paf_vid_init(AVCodecContext *avctx)
  55. {
  56. PAFVideoDecContext *c = avctx->priv_data;
  57. int i;
  58. if (avctx->height & 3 || avctx->width & 3) {
  59. av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
  60. return AVERROR_INVALIDDATA;
  61. }
  62. avctx->pix_fmt = PIX_FMT_PAL8;
  63. avcodec_get_frame_defaults(&c->pic);
  64. c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
  65. c->video_size = avctx->height * avctx->width;
  66. for (i = 0; i < 4; i++) {
  67. c->frame[i] = av_mallocz(c->frame_size);
  68. if (!c->frame[i])
  69. return AVERROR(ENOMEM);
  70. }
  71. return 0;
  72. }
  73. static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
  74. {
  75. int x, y;
  76. x = b & 0x7F;
  77. y = ((a & 0x3F) << 1) | (b >> 7 & 1);
  78. return y * 2 * avctx->width + x * 2;
  79. }
  80. static void copy4h(AVCodecContext *avctx, uint8_t *dst)
  81. {
  82. PAFVideoDecContext *c = avctx->priv_data;
  83. int i;
  84. for (i = 0; i < 4; i++) {
  85. bytestream2_get_buffer(&c->gb, dst, 4);
  86. dst += avctx->width;
  87. }
  88. }
  89. static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
  90. {
  91. int i;
  92. for (i = 0; i < 4; i++) {
  93. if ((mask >> 4) & (1 << (3 - i)))
  94. dst[i] = color;
  95. if ((mask & 15) & (1 << (3 - i)))
  96. dst[avctx->width + i] = color;
  97. }
  98. }
  99. static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
  100. {
  101. int i;
  102. for (i = 0; i < 4; i++) {
  103. if ((mask >> 4) & (1 << (3 - i)))
  104. dst[i] = src[i];
  105. if ((mask & 15) & (1 << (3 - i)))
  106. dst[avctx->width + i] = src[avctx->width + i];
  107. }
  108. }
  109. static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
  110. {
  111. PAFVideoDecContext *c = avctx->priv_data;
  112. uint32_t opcode_size, offset;
  113. uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
  114. const uint8_t *src, *send, *opcodes;
  115. int i, j, x = 0;
  116. i = bytestream2_get_byte(&c->gb);
  117. if (i) {
  118. if (code & 0x10) {
  119. int align;
  120. align = bytestream2_tell(&c->gb) & 3;
  121. if (align)
  122. bytestream2_skip(&c->gb, 4 - align);
  123. }
  124. do {
  125. a = bytestream2_get_byte(&c->gb);
  126. b = bytestream2_get_byte(&c->gb);
  127. p = (a & 0xC0) >> 6;
  128. dst = c->frame[p] + get_video_page_offset(avctx, a, b);
  129. dend = c->frame[p] + c->frame_size;
  130. offset = (b & 0x7F) * 2;
  131. j = bytestream2_get_le16(&c->gb) + offset;
  132. do {
  133. offset++;
  134. if (dst + 3 * avctx->width + 4 > dend)
  135. return AVERROR_INVALIDDATA;
  136. copy4h(avctx, dst);
  137. if ((offset & 0x3F) == 0)
  138. dst += avctx->width * 3;
  139. dst += 4;
  140. } while (offset < j);
  141. } while (--i);
  142. }
  143. dst = c->frame[c->current_frame];
  144. do {
  145. a = bytestream2_get_byte(&c->gb);
  146. b = bytestream2_get_byte(&c->gb);
  147. p = (a & 0xC0) >> 6;
  148. src = c->frame[p] + get_video_page_offset(avctx, a, b);
  149. send = c->frame[p] + c->frame_size;
  150. if (src + 3 * avctx->width + 4 > send)
  151. return AVERROR_INVALIDDATA;
  152. copy_block4(dst, src, avctx->width, avctx->width, 4);
  153. i++;
  154. if ((i & 0x3F) == 0)
  155. dst += avctx->width * 3;
  156. dst += 4;
  157. } while (i < c->video_size / 16);
  158. opcode_size = bytestream2_get_le16(&c->gb);
  159. bytestream2_skip(&c->gb, 2);
  160. if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
  161. return AVERROR_INVALIDDATA;
  162. opcodes = pkt + bytestream2_tell(&c->gb);
  163. bytestream2_skipu(&c->gb, opcode_size);
  164. dst = c->frame[c->current_frame];
  165. for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
  166. for (j = 0; j < avctx->width; j += 4, dst += 4) {
  167. int opcode, k = 0;
  168. if (x > opcode_size)
  169. return AVERROR_INVALIDDATA;
  170. if (j & 4) {
  171. opcode = opcodes[x] & 15;
  172. x++;
  173. } else {
  174. opcode = opcodes[x] >> 4;
  175. }
  176. while (block_sequences[opcode][k]) {
  177. offset = avctx->width * 2;
  178. code = block_sequences[opcode][k++];
  179. switch (code) {
  180. case 2:
  181. offset = 0;
  182. case 3:
  183. color = bytestream2_get_byte(&c->gb);
  184. case 4:
  185. mask = bytestream2_get_byte(&c->gb);
  186. copy_color_mask(avctx, mask, dst + offset, color);
  187. break;
  188. case 5:
  189. offset = 0;
  190. case 6:
  191. a = bytestream2_get_byte(&c->gb);
  192. b = bytestream2_get_byte(&c->gb);
  193. p = (a & 0xC0) >> 6;
  194. src = c->frame[p] + get_video_page_offset(avctx, a, b);
  195. send = c->frame[p] + c->frame_size;
  196. case 7:
  197. if (src + offset + avctx->width + 4 > send)
  198. return AVERROR_INVALIDDATA;
  199. mask = bytestream2_get_byte(&c->gb);
  200. copy_src_mask(avctx, mask, dst + offset, src + offset);
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. return 0;
  207. }
  208. static int paf_vid_decode(AVCodecContext *avctx, void *data,
  209. int *data_size, AVPacket *pkt)
  210. {
  211. PAFVideoDecContext *c = avctx->priv_data;
  212. uint8_t code, *dst, *src, *end;
  213. int i, frame, ret;
  214. c->pic.reference = 3;
  215. if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
  216. return ret;
  217. bytestream2_init(&c->gb, pkt->data, pkt->size);
  218. code = bytestream2_get_byte(&c->gb);
  219. if (code & 0x20) {
  220. for (i = 0; i < 4; i++)
  221. memset(c->frame[i], 0, c->frame_size);
  222. memset(c->pic.data[1], 0, AVPALETTE_SIZE);
  223. c->current_frame = 0;
  224. c->pic.key_frame = 1;
  225. c->pic.pict_type = AV_PICTURE_TYPE_I;
  226. } else {
  227. c->pic.key_frame = 0;
  228. c->pic.pict_type = AV_PICTURE_TYPE_P;
  229. }
  230. if (code & 0x40) {
  231. uint32_t *out = (uint32_t *)c->pic.data[1];
  232. int index, count;
  233. index = bytestream2_get_byte(&c->gb);
  234. count = bytestream2_get_byte(&c->gb) + 1;
  235. if (index + count > AVPALETTE_SIZE)
  236. return AVERROR_INVALIDDATA;
  237. if (bytestream2_get_bytes_left(&c->gb) < 3 * AVPALETTE_SIZE)
  238. return AVERROR_INVALIDDATA;
  239. out += index;
  240. for (i = 0; i < count; i++) {
  241. unsigned r, g, b;
  242. r = bytestream2_get_byteu(&c->gb);
  243. r = r << 2 | r >> 4;
  244. g = bytestream2_get_byteu(&c->gb);
  245. g = g << 2 | g >> 4;
  246. b = bytestream2_get_byteu(&c->gb);
  247. b = b << 2 | b >> 4;
  248. *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
  249. }
  250. c->pic.palette_has_changed = 1;
  251. }
  252. switch (code & 0x0F) {
  253. case 0:
  254. if ((ret = decode_0(avctx, code, pkt->data)) < 0)
  255. return ret;
  256. break;
  257. case 1:
  258. dst = c->frame[c->current_frame];
  259. bytestream2_skip(&c->gb, 2);
  260. if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
  261. return AVERROR_INVALIDDATA;
  262. bytestream2_get_bufferu(&c->gb, dst, c->video_size);
  263. break;
  264. case 2:
  265. frame = bytestream2_get_byte(&c->gb);
  266. if (frame > 3)
  267. return AVERROR_INVALIDDATA;
  268. if (frame != c->current_frame)
  269. memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
  270. break;
  271. case 4:
  272. dst = c->frame[c->current_frame];
  273. end = dst + c->video_size;
  274. bytestream2_skip(&c->gb, 2);
  275. while (dst < end) {
  276. int8_t code;
  277. int count;
  278. if (bytestream2_get_bytes_left(&c->gb) < 2)
  279. return AVERROR_INVALIDDATA;
  280. code = bytestream2_get_byteu(&c->gb);
  281. count = FFABS(code) + 1;
  282. if (dst + count > end)
  283. return AVERROR_INVALIDDATA;
  284. if (code < 0)
  285. memset(dst, bytestream2_get_byteu(&c->gb), count);
  286. else
  287. bytestream2_get_buffer(&c->gb, dst, count);
  288. dst += count;
  289. }
  290. break;
  291. default:
  292. av_log_ask_for_sample(avctx, "unknown/invalid code\n");
  293. return AVERROR_INVALIDDATA;
  294. }
  295. dst = c->pic.data[0];
  296. src = c->frame[c->current_frame];
  297. for (i = 0; i < avctx->height; i++) {
  298. memcpy(dst, src, avctx->width);
  299. dst += c->pic.linesize[0];
  300. src += avctx->width;
  301. }
  302. c->current_frame = (c->current_frame + 1) & 3;
  303. *data_size = sizeof(AVFrame);
  304. *(AVFrame *)data = c->pic;
  305. return pkt->size;
  306. }
  307. static av_cold int paf_vid_close(AVCodecContext *avctx)
  308. {
  309. PAFVideoDecContext *c = avctx->priv_data;
  310. int i;
  311. if (c->pic.data[0])
  312. avctx->release_buffer(avctx, &c->pic);
  313. for (i = 0; i < 4; i++)
  314. av_freep(&c->frame[i]);
  315. return 0;
  316. }
  317. typedef struct PAFAudioDecContext {
  318. AVFrame frame;
  319. } PAFAudioDecContext;
  320. static av_cold int paf_aud_init(AVCodecContext *avctx)
  321. {
  322. PAFAudioDecContext *c = avctx->priv_data;
  323. if (avctx->channels != 2) {
  324. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  325. return AVERROR_INVALIDDATA;
  326. }
  327. avcodec_get_frame_defaults(&c->frame);
  328. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  329. avctx->coded_frame = &c->frame;
  330. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  331. return 0;
  332. }
  333. static int paf_aud_decode(AVCodecContext *avctx, void *data,
  334. int *got_frame_ptr, AVPacket *pkt)
  335. {
  336. PAFAudioDecContext *c = avctx->priv_data;
  337. uint8_t *buf = pkt->data;
  338. int16_t *output_samples;
  339. const uint8_t *t;
  340. int frames, ret, i, j, k;
  341. frames = pkt->size / PAF_SOUND_FRAME_SIZE;
  342. if (frames < 1)
  343. return AVERROR_INVALIDDATA;
  344. c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
  345. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0)
  346. return ret;
  347. output_samples = (int16_t *)c->frame.data[0];
  348. for (i = 0; i < frames; i++) {
  349. t = buf + 256 * sizeof(uint16_t);
  350. for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
  351. for (k = 0; k < 2; k++) {
  352. *output_samples++ = AV_RL16(buf + *t++ * 2);
  353. }
  354. }
  355. buf += PAF_SOUND_FRAME_SIZE;
  356. }
  357. *got_frame_ptr = 1;
  358. *(AVFrame *)data = c->frame;
  359. return pkt->size;
  360. }
  361. AVCodec ff_paf_video_decoder = {
  362. .name = "paf_video",
  363. .type = AVMEDIA_TYPE_VIDEO,
  364. .id = CODEC_ID_PAF_VIDEO,
  365. .priv_data_size = sizeof(PAFVideoDecContext),
  366. .init = paf_vid_init,
  367. .close = paf_vid_close,
  368. .decode = paf_vid_decode,
  369. .capabilities = CODEC_CAP_DR1,
  370. .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
  371. };
  372. AVCodec ff_paf_audio_decoder = {
  373. .name = "paf_audio",
  374. .type = AVMEDIA_TYPE_AUDIO,
  375. .id = CODEC_ID_PAF_AUDIO,
  376. .priv_data_size = sizeof(PAFAudioDecContext),
  377. .init = paf_aud_init,
  378. .decode = paf_aud_decode,
  379. .capabilities = CODEC_CAP_DR1,
  380. .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
  381. };