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