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.

410 lines
12KB

  1. /*
  2. * Chronomaster DFA Video Decoder
  3. * Copyright (c) 2011 Konstantin Shishkov
  4. * based on work by Vladimir "VAG" Gneushev
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "bytestream.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/lzo.h" // for av_memcpy_backptr
  27. typedef struct DfaContext {
  28. AVFrame pic;
  29. uint32_t pal[256];
  30. uint8_t *frame_buf;
  31. } DfaContext;
  32. static av_cold int dfa_decode_init(AVCodecContext *avctx)
  33. {
  34. DfaContext *s = avctx->priv_data;
  35. int ret;
  36. avctx->pix_fmt = PIX_FMT_PAL8;
  37. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  38. return ret;
  39. s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
  40. if (!s->frame_buf)
  41. return AVERROR(ENOMEM);
  42. return 0;
  43. }
  44. static int decode_copy(uint8_t *frame, int width, int height,
  45. const uint8_t *src, const uint8_t *src_end)
  46. {
  47. const int size = width * height;
  48. if (src_end - src < size)
  49. return -1;
  50. bytestream_get_buffer(&src, frame, size);
  51. return 0;
  52. }
  53. static int decode_tsw1(uint8_t *frame, int width, int height,
  54. const uint8_t *src, const uint8_t *src_end)
  55. {
  56. const uint8_t *frame_start = frame;
  57. const uint8_t *frame_end = frame + width * height;
  58. int mask = 0x10000, bitbuf = 0;
  59. int v, count, segments;
  60. unsigned offset;
  61. segments = bytestream_get_le32(&src);
  62. offset = bytestream_get_le32(&src);
  63. if (frame_end - frame <= offset)
  64. return -1;
  65. frame += offset;
  66. while (segments--) {
  67. if (mask == 0x10000) {
  68. if (src >= src_end)
  69. return -1;
  70. bitbuf = bytestream_get_le16(&src);
  71. mask = 1;
  72. }
  73. if (src_end - src < 2 || frame_end - frame < 2)
  74. return -1;
  75. if (bitbuf & mask) {
  76. v = bytestream_get_le16(&src);
  77. offset = (v & 0x1FFF) << 1;
  78. count = ((v >> 13) + 2) << 1;
  79. if (frame - frame_start < offset || frame_end - frame < count)
  80. return -1;
  81. av_memcpy_backptr(frame, offset, count);
  82. frame += count;
  83. } else {
  84. *frame++ = *src++;
  85. *frame++ = *src++;
  86. }
  87. mask <<= 1;
  88. }
  89. return 0;
  90. }
  91. static int decode_dsw1(uint8_t *frame, int width, int height,
  92. const uint8_t *src, const uint8_t *src_end)
  93. {
  94. const uint8_t *frame_start = frame;
  95. const uint8_t *frame_end = frame + width * height;
  96. int mask = 0x10000, bitbuf = 0;
  97. int v, offset, count, segments;
  98. segments = bytestream_get_le16(&src);
  99. while (segments--) {
  100. if (mask == 0x10000) {
  101. if (src >= src_end)
  102. return -1;
  103. bitbuf = bytestream_get_le16(&src);
  104. mask = 1;
  105. }
  106. if (src_end - src < 2 || frame_end - frame < 2)
  107. return -1;
  108. if (bitbuf & mask) {
  109. v = bytestream_get_le16(&src);
  110. offset = (v & 0x1FFF) << 1;
  111. count = ((v >> 13) + 2) << 1;
  112. if (frame - frame_start < offset || frame_end - frame < count)
  113. return -1;
  114. // can't use av_memcpy_backptr() since it can overwrite following pixels
  115. for (v = 0; v < count; v++)
  116. frame[v] = frame[v - offset];
  117. frame += count;
  118. } else if (bitbuf & (mask << 1)) {
  119. frame += bytestream_get_le16(&src);
  120. } else {
  121. *frame++ = *src++;
  122. *frame++ = *src++;
  123. }
  124. mask <<= 2;
  125. }
  126. return 0;
  127. }
  128. static int decode_dds1(uint8_t *frame, int width, int height,
  129. const uint8_t *src, const uint8_t *src_end)
  130. {
  131. const uint8_t *frame_start = frame;
  132. const uint8_t *frame_end = frame + width * height;
  133. int mask = 0x10000, bitbuf = 0;
  134. int i, v, offset, count, segments;
  135. segments = bytestream_get_le16(&src);
  136. while (segments--) {
  137. if (mask == 0x10000) {
  138. if (src >= src_end)
  139. return -1;
  140. bitbuf = bytestream_get_le16(&src);
  141. mask = 1;
  142. }
  143. if (src_end - src < 2 || frame_end - frame < 2)
  144. return -1;
  145. if (bitbuf & mask) {
  146. v = bytestream_get_le16(&src);
  147. offset = (v & 0x1FFF) << 2;
  148. count = ((v >> 13) + 2) << 1;
  149. if (frame - frame_start < offset || frame_end - frame < count*2 + width)
  150. return -1;
  151. for (i = 0; i < count; i++) {
  152. frame[0] = frame[1] =
  153. frame[width] = frame[width + 1] = frame[-offset];
  154. frame += 2;
  155. }
  156. } else if (bitbuf & (mask << 1)) {
  157. frame += bytestream_get_le16(&src) * 2;
  158. } else {
  159. if (frame_end - frame < width + 2)
  160. return AVERROR_INVALIDDATA;
  161. frame[0] = frame[1] =
  162. frame[width] = frame[width + 1] = *src++;
  163. frame += 2;
  164. frame[0] = frame[1] =
  165. frame[width] = frame[width + 1] = *src++;
  166. frame += 2;
  167. }
  168. mask <<= 2;
  169. }
  170. return 0;
  171. }
  172. static int decode_bdlt(uint8_t *frame, int width, int height,
  173. const uint8_t *src, const uint8_t *src_end)
  174. {
  175. uint8_t *line_ptr;
  176. int count, lines, segments;
  177. count = bytestream_get_le16(&src);
  178. if (count >= height)
  179. return -1;
  180. frame += width * count;
  181. lines = bytestream_get_le16(&src);
  182. if (count + lines > height || src >= src_end)
  183. return -1;
  184. while (lines--) {
  185. line_ptr = frame;
  186. frame += width;
  187. segments = *src++;
  188. while (segments--) {
  189. if (src_end - src < 3)
  190. return -1;
  191. if (frame - line_ptr <= *src)
  192. return -1;
  193. line_ptr += *src++;
  194. count = (int8_t)*src++;
  195. if (count >= 0) {
  196. if (frame - line_ptr < count || src_end - src < count)
  197. return -1;
  198. bytestream_get_buffer(&src, line_ptr, count);
  199. } else {
  200. count = -count;
  201. if (frame - line_ptr < count || src >= src_end)
  202. return -1;
  203. memset(line_ptr, *src++, count);
  204. }
  205. line_ptr += count;
  206. }
  207. }
  208. return 0;
  209. }
  210. static int decode_wdlt(uint8_t *frame, int width, int height,
  211. const uint8_t *src, const uint8_t *src_end)
  212. {
  213. const uint8_t *frame_end = frame + width * height;
  214. uint8_t *line_ptr;
  215. int count, i, v, lines, segments;
  216. int y = 0;
  217. lines = bytestream_get_le16(&src);
  218. if (lines > height || src >= src_end)
  219. return -1;
  220. while (lines--) {
  221. segments = bytestream_get_le16(&src);
  222. while ((segments & 0xC000) == 0xC000) {
  223. unsigned skip_lines = -(int16_t)segments;
  224. unsigned delta = -((int16_t)segments * width);
  225. if (frame_end - frame <= delta || y + lines + skip_lines > height)
  226. return -1;
  227. frame += delta;
  228. y += skip_lines;
  229. segments = bytestream_get_le16(&src);
  230. }
  231. if (segments & 0x8000) {
  232. frame[width - 1] = segments & 0xFF;
  233. segments = bytestream_get_le16(&src);
  234. }
  235. line_ptr = frame;
  236. frame += width;
  237. y++;
  238. while (segments--) {
  239. if (src_end - src < 2)
  240. return -1;
  241. if (frame - line_ptr <= *src)
  242. return -1;
  243. line_ptr += *src++;
  244. count = (int8_t)*src++;
  245. if (count >= 0) {
  246. if (frame - line_ptr < count*2 || src_end - src < count*2)
  247. return -1;
  248. bytestream_get_buffer(&src, line_ptr, count*2);
  249. line_ptr += count * 2;
  250. } else {
  251. count = -count;
  252. if (frame - line_ptr < count*2 || src_end - src < 2)
  253. return -1;
  254. v = bytestream_get_le16(&src);
  255. for (i = 0; i < count; i++)
  256. bytestream_put_le16(&line_ptr, v);
  257. }
  258. }
  259. }
  260. return 0;
  261. }
  262. static int decode_unk6(uint8_t *frame, int width, int height,
  263. const uint8_t *src, const uint8_t *src_end)
  264. {
  265. return -1;
  266. }
  267. static int decode_blck(uint8_t *frame, int width, int height,
  268. const uint8_t *src, const uint8_t *src_end)
  269. {
  270. memset(frame, 0, width * height);
  271. return 0;
  272. }
  273. typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
  274. const uint8_t *src, const uint8_t *src_end);
  275. static const chunk_decoder decoder[8] = {
  276. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  277. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  278. };
  279. static const char* chunk_name[8] = {
  280. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  281. };
  282. static int dfa_decode_frame(AVCodecContext *avctx,
  283. void *data, int *data_size,
  284. AVPacket *avpkt)
  285. {
  286. DfaContext *s = avctx->priv_data;
  287. const uint8_t *buf = avpkt->data;
  288. const uint8_t *buf_end = avpkt->data + avpkt->size;
  289. const uint8_t *tmp_buf;
  290. uint32_t chunk_type, chunk_size;
  291. uint8_t *dst;
  292. int ret;
  293. int i, pal_elems;
  294. if (s->pic.data[0])
  295. avctx->release_buffer(avctx, &s->pic);
  296. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  297. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  298. return ret;
  299. }
  300. while (buf < buf_end) {
  301. chunk_size = AV_RL32(buf + 4);
  302. chunk_type = AV_RL32(buf + 8);
  303. buf += 12;
  304. if (buf_end - buf < chunk_size) {
  305. av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
  306. return -1;
  307. }
  308. if (!chunk_type)
  309. break;
  310. if (chunk_type == 1) {
  311. pal_elems = FFMIN(chunk_size / 3, 256);
  312. tmp_buf = buf;
  313. for (i = 0; i < pal_elems; i++) {
  314. s->pal[i] = bytestream_get_be24(&tmp_buf) << 2;
  315. s->pal[i] |= (s->pal[i] >> 6) & 0x333;
  316. }
  317. s->pic.palette_has_changed = 1;
  318. } else if (chunk_type <= 9) {
  319. if (decoder[chunk_type - 2](s->frame_buf, avctx->width, avctx->height,
  320. buf, buf + chunk_size)) {
  321. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  322. chunk_name[chunk_type - 2]);
  323. return -1;
  324. }
  325. } else {
  326. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  327. chunk_type);
  328. }
  329. buf += chunk_size;
  330. }
  331. buf = s->frame_buf;
  332. dst = s->pic.data[0];
  333. for (i = 0; i < avctx->height; i++) {
  334. memcpy(dst, buf, avctx->width);
  335. dst += s->pic.linesize[0];
  336. buf += avctx->width;
  337. }
  338. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  339. *data_size = sizeof(AVFrame);
  340. *(AVFrame*)data = s->pic;
  341. return avpkt->size;
  342. }
  343. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  344. {
  345. DfaContext *s = avctx->priv_data;
  346. if (s->pic.data[0])
  347. avctx->release_buffer(avctx, &s->pic);
  348. av_freep(&s->frame_buf);
  349. return 0;
  350. }
  351. AVCodec ff_dfa_decoder = {
  352. "dfa",
  353. AVMEDIA_TYPE_VIDEO,
  354. CODEC_ID_DFA,
  355. sizeof(DfaContext),
  356. dfa_decode_init,
  357. NULL,
  358. dfa_decode_end,
  359. dfa_decode_frame,
  360. CODEC_CAP_DR1,
  361. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  362. };