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.

397 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. uint8_t *line_ptr;
  170. int count, lines, segments;
  171. count = bytestream_get_le16(&src);
  172. if (count >= height)
  173. return -1;
  174. frame += width * count;
  175. lines = bytestream_get_le16(&src);
  176. if (count + lines > height || src >= src_end)
  177. return -1;
  178. while (lines--) {
  179. line_ptr = frame;
  180. frame += width;
  181. segments = *src++;
  182. while (segments--) {
  183. if (src_end - src < 3)
  184. return -1;
  185. if (frame - line_ptr <= *src)
  186. return -1;
  187. line_ptr += *src++;
  188. count = (int8_t)*src++;
  189. if (count >= 0) {
  190. if (frame - line_ptr < count || src_end - src < count)
  191. return -1;
  192. bytestream_get_buffer(&src, line_ptr, count);
  193. } else {
  194. count = -count;
  195. if (frame - line_ptr < count || src >= src_end)
  196. return -1;
  197. memset(line_ptr, *src++, count);
  198. }
  199. line_ptr += count;
  200. }
  201. }
  202. return 0;
  203. }
  204. static int decode_wdlt(uint8_t *frame, int width, int height,
  205. const uint8_t *src, const uint8_t *src_end)
  206. {
  207. const uint8_t *frame_end = frame + width * height;
  208. uint8_t *line_ptr;
  209. int count, i, v, lines, segments;
  210. lines = bytestream_get_le16(&src);
  211. if (lines > height || src >= src_end)
  212. return -1;
  213. while (lines--) {
  214. segments = bytestream_get_le16(&src);
  215. while ((segments & 0xC000) == 0xC000) {
  216. unsigned delta = -((int16_t)segments * width);
  217. if (frame_end - frame <= delta)
  218. return -1;
  219. frame += delta;
  220. segments = bytestream_get_le16(&src);
  221. }
  222. if (segments & 0x8000) {
  223. frame[width - 1] = segments & 0xFF;
  224. segments = bytestream_get_le16(&src);
  225. }
  226. line_ptr = frame;
  227. frame += width;
  228. while (segments--) {
  229. if (src_end - src < 2)
  230. return -1;
  231. if (frame - line_ptr <= *src)
  232. return -1;
  233. line_ptr += *src++;
  234. count = (int8_t)*src++;
  235. if (count >= 0) {
  236. if (frame - line_ptr < count*2 || src_end - src < count*2)
  237. return -1;
  238. bytestream_get_buffer(&src, line_ptr, count*2);
  239. line_ptr += count * 2;
  240. } else {
  241. count = -count;
  242. if (frame - line_ptr < count*2 || src_end - src < 2)
  243. return -1;
  244. v = bytestream_get_le16(&src);
  245. for (i = 0; i < count; i++)
  246. bytestream_put_le16(&line_ptr, v);
  247. }
  248. }
  249. }
  250. return 0;
  251. }
  252. static int decode_unk6(uint8_t *frame, int width, int height,
  253. const uint8_t *src, const uint8_t *src_end)
  254. {
  255. return -1;
  256. }
  257. static int decode_blck(uint8_t *frame, int width, int height,
  258. const uint8_t *src, const uint8_t *src_end)
  259. {
  260. memset(frame, 0, width * height);
  261. return 0;
  262. }
  263. typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
  264. const uint8_t *src, const uint8_t *src_end);
  265. static const chunk_decoder decoder[8] = {
  266. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  267. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  268. };
  269. static const char* chunk_name[8] = {
  270. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  271. };
  272. static int dfa_decode_frame(AVCodecContext *avctx,
  273. void *data, int *data_size,
  274. AVPacket *avpkt)
  275. {
  276. DfaContext *s = avctx->priv_data;
  277. const uint8_t *buf = avpkt->data;
  278. const uint8_t *buf_end = avpkt->data + avpkt->size;
  279. const uint8_t *tmp_buf;
  280. uint32_t chunk_type, chunk_size;
  281. uint8_t *dst;
  282. int ret;
  283. int i, pal_elems;
  284. if (s->pic.data[0])
  285. avctx->release_buffer(avctx, &s->pic);
  286. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  287. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  288. return ret;
  289. }
  290. while (buf < buf_end) {
  291. chunk_size = AV_RL32(buf + 4);
  292. chunk_type = AV_RL32(buf + 8);
  293. buf += 12;
  294. if (buf_end - buf < chunk_size) {
  295. av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
  296. return -1;
  297. }
  298. if (!chunk_type)
  299. break;
  300. if (chunk_type == 1) {
  301. pal_elems = FFMIN(chunk_size / 3, 256);
  302. tmp_buf = buf;
  303. for (i = 0; i < pal_elems; i++) {
  304. s->pal[i] = bytestream_get_be24(&tmp_buf) << 2;
  305. s->pal[i] |= (s->pal[i] >> 6) & 0x333;
  306. }
  307. s->pic.palette_has_changed = 1;
  308. } else if (chunk_type <= 9) {
  309. if (decoder[chunk_type - 2](s->frame_buf, avctx->width, avctx->height,
  310. buf, buf + chunk_size)) {
  311. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  312. chunk_name[chunk_type - 2]);
  313. return -1;
  314. }
  315. } else {
  316. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  317. chunk_type);
  318. }
  319. buf += chunk_size;
  320. }
  321. buf = s->frame_buf;
  322. dst = s->pic.data[0];
  323. for (i = 0; i < avctx->height; i++) {
  324. memcpy(dst, buf, avctx->width);
  325. dst += s->pic.linesize[0];
  326. buf += avctx->width;
  327. }
  328. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  329. *data_size = sizeof(AVFrame);
  330. *(AVFrame*)data = s->pic;
  331. return avpkt->size;
  332. }
  333. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  334. {
  335. DfaContext *s = avctx->priv_data;
  336. if (s->pic.data[0])
  337. avctx->release_buffer(avctx, &s->pic);
  338. av_freep(&s->frame_buf);
  339. return 0;
  340. }
  341. AVCodec ff_dfa_decoder = {
  342. .name = "dfa",
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .id = CODEC_ID_DFA,
  345. .priv_data_size = sizeof(DfaContext),
  346. .init = dfa_decode_init,
  347. .close = dfa_decode_end,
  348. .decode = dfa_decode_frame,
  349. .capabilities = CODEC_CAP_DR1,
  350. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  351. };