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.

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