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.

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