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. * 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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/lzo.h" // for av_memcpy_backptr
  26. typedef struct DfaContext {
  27. AVFrame pic;
  28. uint32_t pal[256];
  29. uint8_t *frame_buf;
  30. } DfaContext;
  31. static av_cold int dfa_decode_init(AVCodecContext *avctx)
  32. {
  33. DfaContext *s = avctx->priv_data;
  34. avctx->pix_fmt = PIX_FMT_PAL8;
  35. s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
  36. if (!s->frame_buf)
  37. return AVERROR(ENOMEM);
  38. return 0;
  39. }
  40. static int decode_copy(uint8_t *frame, int width, int height,
  41. const uint8_t *src, const uint8_t *src_end)
  42. {
  43. const int size = width * height;
  44. if (src_end - src < size)
  45. return -1;
  46. bytestream_get_buffer(&src, frame, size);
  47. return 0;
  48. }
  49. static int decode_tsw1(uint8_t *frame, int width, int height,
  50. const uint8_t *src, const uint8_t *src_end)
  51. {
  52. const uint8_t *frame_start = frame;
  53. const uint8_t *frame_end = frame + width * height;
  54. int mask = 0x10000, bitbuf = 0;
  55. int v, count, segments;
  56. unsigned offset;
  57. segments = bytestream_get_le32(&src);
  58. offset = bytestream_get_le32(&src);
  59. if (frame_end - frame <= offset)
  60. return -1;
  61. frame += offset;
  62. while (segments--) {
  63. if (mask == 0x10000) {
  64. if (src >= src_end)
  65. return -1;
  66. bitbuf = bytestream_get_le16(&src);
  67. mask = 1;
  68. }
  69. if (src_end - src < 2 || frame_end - frame < 2)
  70. return -1;
  71. if (bitbuf & mask) {
  72. v = bytestream_get_le16(&src);
  73. offset = (v & 0x1FFF) << 1;
  74. count = ((v >> 13) + 2) << 1;
  75. if (frame - frame_start < offset || frame_end - frame < count)
  76. return -1;
  77. av_memcpy_backptr(frame, offset, count);
  78. frame += count;
  79. } else {
  80. *frame++ = *src++;
  81. *frame++ = *src++;
  82. }
  83. mask <<= 1;
  84. }
  85. return 0;
  86. }
  87. static int decode_dsw1(uint8_t *frame, int width, int height,
  88. const uint8_t *src, const uint8_t *src_end)
  89. {
  90. const uint8_t *frame_start = frame;
  91. const uint8_t *frame_end = frame + width * height;
  92. int mask = 0x10000, bitbuf = 0;
  93. int v, offset, count, segments;
  94. segments = bytestream_get_le16(&src);
  95. while (segments--) {
  96. if (mask == 0x10000) {
  97. if (src >= src_end)
  98. return -1;
  99. bitbuf = bytestream_get_le16(&src);
  100. mask = 1;
  101. }
  102. if (src_end - src < 2 || frame_end - frame < 2)
  103. return -1;
  104. if (bitbuf & mask) {
  105. v = bytestream_get_le16(&src);
  106. offset = (v & 0x1FFF) << 1;
  107. count = ((v >> 13) + 2) << 1;
  108. if (frame - frame_start < offset || frame_end - frame < count)
  109. return -1;
  110. // can't use av_memcpy_backptr() since it can overwrite following pixels
  111. for (v = 0; v < count; v++)
  112. frame[v] = frame[v - offset];
  113. frame += count;
  114. } else if (bitbuf & (mask << 1)) {
  115. frame += bytestream_get_le16(&src);
  116. } else {
  117. *frame++ = *src++;
  118. *frame++ = *src++;
  119. }
  120. mask <<= 2;
  121. }
  122. return 0;
  123. }
  124. static int decode_dds1(uint8_t *frame, int width, int height,
  125. const uint8_t *src, const uint8_t *src_end)
  126. {
  127. const uint8_t *frame_start = frame;
  128. const uint8_t *frame_end = frame + width * height;
  129. int mask = 0x10000, bitbuf = 0;
  130. int i, v, offset, count, segments;
  131. segments = bytestream_get_le16(&src);
  132. while (segments--) {
  133. if (mask == 0x10000) {
  134. if (src >= src_end)
  135. return -1;
  136. bitbuf = bytestream_get_le16(&src);
  137. mask = 1;
  138. }
  139. if (src_end - src < 2 || frame_end - frame < 2)
  140. return -1;
  141. if (bitbuf & mask) {
  142. v = bytestream_get_le16(&src);
  143. offset = (v & 0x1FFF) << 2;
  144. count = ((v >> 13) + 2) << 1;
  145. if (frame - frame_start < offset || frame_end - frame < count*2 + width)
  146. return -1;
  147. for (i = 0; i < count; i++) {
  148. frame[0] = frame[1] =
  149. frame[width] = frame[width + 1] = frame[-offset];
  150. frame += 2;
  151. }
  152. } else if (bitbuf & (mask << 1)) {
  153. frame += bytestream_get_le16(&src) * 2;
  154. } else {
  155. frame[0] = frame[1] =
  156. frame[width] = frame[width + 1] = *src++;
  157. frame += 2;
  158. frame[0] = frame[1] =
  159. frame[width] = frame[width + 1] = *src++;
  160. frame += 2;
  161. }
  162. mask <<= 2;
  163. }
  164. return 0;
  165. }
  166. static int decode_bdlt(uint8_t *frame, int width, int height,
  167. const uint8_t *src, const uint8_t *src_end)
  168. {
  169. const uint8_t *frame_end = frame + width * height;
  170. uint8_t *line_ptr;
  171. int count, lines, segments;
  172. count = bytestream_get_le16(&src);
  173. if (count >= height)
  174. return -1;
  175. frame += width * count;
  176. lines = bytestream_get_le16(&src);
  177. if (count + lines > height || src >= src_end)
  178. return -1;
  179. while (lines--) {
  180. line_ptr = frame;
  181. frame += width;
  182. segments = *src++;
  183. while (segments--) {
  184. if (src_end - src < 3)
  185. return -1;
  186. if (frame - line_ptr <= *src)
  187. return -1;
  188. line_ptr += *src++;
  189. count = (int8_t)*src++;
  190. if (count >= 0) {
  191. if (frame - line_ptr < count || src_end - src < count)
  192. return -1;
  193. bytestream_get_buffer(&src, line_ptr, count);
  194. } else {
  195. count = -count;
  196. if (frame - line_ptr < count || src >= src_end)
  197. return -1;
  198. memset(line_ptr, *src++, count);
  199. }
  200. line_ptr += count;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int decode_wdlt(uint8_t *frame, int width, int height,
  206. const uint8_t *src, const uint8_t *src_end)
  207. {
  208. const uint8_t *frame_end = frame + width * height;
  209. uint8_t *line_ptr;
  210. int count, i, v, lines, segments;
  211. lines = bytestream_get_le16(&src);
  212. if (lines > height || src >= src_end)
  213. return -1;
  214. while (lines--) {
  215. segments = bytestream_get_le16(&src);
  216. while ((segments & 0xC000) == 0xC000) {
  217. unsigned delta = -((int16_t)segments * width);
  218. if (frame_end - frame <= delta)
  219. return -1;
  220. frame += delta;
  221. segments = bytestream_get_le16(&src);
  222. }
  223. if (segments & 0x8000) {
  224. frame[width - 1] = segments & 0xFF;
  225. segments = bytestream_get_le16(&src);
  226. }
  227. line_ptr = frame;
  228. frame += width;
  229. while (segments--) {
  230. if (src_end - src < 2)
  231. return -1;
  232. if (frame - line_ptr <= *src)
  233. return -1;
  234. line_ptr += *src++;
  235. count = (int8_t)*src++;
  236. if (count >= 0) {
  237. if (frame - line_ptr < count*2 || src_end - src < count*2)
  238. return -1;
  239. bytestream_get_buffer(&src, line_ptr, count*2);
  240. line_ptr += count * 2;
  241. } else {
  242. count = -count;
  243. if (frame - line_ptr < count*2 || src_end - src < 2)
  244. return -1;
  245. v = bytestream_get_le16(&src);
  246. for (i = 0; i < count; i++)
  247. bytestream_put_le16(&line_ptr, v);
  248. }
  249. }
  250. }
  251. return 0;
  252. }
  253. static int decode_unk6(uint8_t *frame, int width, int height,
  254. const uint8_t *src, const uint8_t *src_end)
  255. {
  256. return -1;
  257. }
  258. static int decode_blck(uint8_t *frame, int width, int height,
  259. const uint8_t *src, const uint8_t *src_end)
  260. {
  261. memset(frame, 0, width * height);
  262. return 0;
  263. }
  264. typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
  265. const uint8_t *src, const uint8_t *src_end);
  266. static const chunk_decoder decoder[8] = {
  267. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  268. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  269. };
  270. static const char* chunk_name[8] = {
  271. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  272. };
  273. static int dfa_decode_frame(AVCodecContext *avctx,
  274. void *data, int *data_size,
  275. AVPacket *avpkt)
  276. {
  277. DfaContext *s = avctx->priv_data;
  278. const uint8_t *buf = avpkt->data;
  279. const uint8_t *buf_end = avpkt->data + avpkt->size;
  280. const uint8_t *tmp_buf;
  281. uint32_t chunk_type, chunk_size;
  282. uint8_t *dst;
  283. int ret;
  284. int i, pal_elems;
  285. if (s->pic.data[0])
  286. avctx->release_buffer(avctx, &s->pic);
  287. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  288. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  289. return ret;
  290. }
  291. while (buf < buf_end) {
  292. chunk_size = AV_RL32(buf + 4);
  293. chunk_type = AV_RL32(buf + 8);
  294. buf += 12;
  295. if (buf_end - buf < chunk_size) {
  296. av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
  297. return -1;
  298. }
  299. if (!chunk_type)
  300. break;
  301. if (chunk_type == 1) {
  302. pal_elems = FFMIN(chunk_size / 3, 256);
  303. tmp_buf = buf;
  304. for (i = 0; i < pal_elems; i++) {
  305. s->pal[i] = bytestream_get_be24(&tmp_buf) << 2;
  306. s->pal[i] |= (s->pal[i] >> 6) & 0x333;
  307. }
  308. s->pic.palette_has_changed = 1;
  309. } else if (chunk_type <= 9) {
  310. if (decoder[chunk_type - 2](s->frame_buf, avctx->width, avctx->height,
  311. buf, buf + chunk_size)) {
  312. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  313. chunk_name[chunk_type - 2]);
  314. return -1;
  315. }
  316. } else {
  317. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  318. chunk_type);
  319. }
  320. buf += chunk_size;
  321. }
  322. buf = s->frame_buf;
  323. dst = s->pic.data[0];
  324. for (i = 0; i < avctx->height; i++) {
  325. memcpy(dst, buf, avctx->width);
  326. dst += s->pic.linesize[0];
  327. buf += avctx->width;
  328. }
  329. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  330. *data_size = sizeof(AVFrame);
  331. *(AVFrame*)data = s->pic;
  332. return avpkt->size;
  333. }
  334. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  335. {
  336. DfaContext *s = avctx->priv_data;
  337. if (s->pic.data[0])
  338. avctx->release_buffer(avctx, &s->pic);
  339. av_freep(&s->frame_buf);
  340. return 0;
  341. }
  342. AVCodec ff_dfa_decoder = {
  343. "dfa",
  344. AVMEDIA_TYPE_VIDEO,
  345. CODEC_ID_DFA,
  346. sizeof(DfaContext),
  347. dfa_decode_init,
  348. NULL,
  349. dfa_decode_end,
  350. dfa_decode_frame,
  351. CODEC_CAP_DR1,
  352. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  353. };