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.

388 lines
11KB

  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 "bytestream.h"
  24. #include "libavutil/imgutils.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. int ret;
  35. avctx->pix_fmt = PIX_FMT_PAL8;
  36. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  37. return ret;
  38. s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
  39. if (!s->frame_buf)
  40. return AVERROR(ENOMEM);
  41. return 0;
  42. }
  43. static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
  44. {
  45. const int size = width * height;
  46. if (bytestream2_get_buffer(gb, frame, size) != size)
  47. return -1;
  48. return 0;
  49. }
  50. static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  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 = bytestream2_get_le32(gb);
  58. offset = bytestream2_get_le32(gb);
  59. if (frame_end - frame <= offset)
  60. return -1;
  61. frame += offset;
  62. while (segments--) {
  63. if (bytestream2_get_bytes_left(gb) < 2)
  64. return -1;
  65. if (mask == 0x10000) {
  66. bitbuf = bytestream2_get_le16u(gb);
  67. mask = 1;
  68. }
  69. if (frame_end - frame < 2)
  70. return -1;
  71. if (bitbuf & mask) {
  72. v = bytestream2_get_le16(gb);
  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++ = bytestream2_get_byte(gb);
  81. *frame++ = bytestream2_get_byte(gb);
  82. }
  83. mask <<= 1;
  84. }
  85. return 0;
  86. }
  87. static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  88. {
  89. const uint8_t *frame_start = frame;
  90. const uint8_t *frame_end = frame + width * height;
  91. int mask = 0x10000, bitbuf = 0;
  92. int v, offset, count, segments;
  93. segments = bytestream2_get_le16(gb);
  94. while (segments--) {
  95. if (bytestream2_get_bytes_left(gb) < 2)
  96. return -1;
  97. if (mask == 0x10000) {
  98. bitbuf = bytestream2_get_le16u(gb);
  99. mask = 1;
  100. }
  101. if (frame_end - frame < 2)
  102. return -1;
  103. if (bitbuf & mask) {
  104. v = bytestream2_get_le16(gb);
  105. offset = (v & 0x1FFF) << 1;
  106. count = ((v >> 13) + 2) << 1;
  107. if (frame - frame_start < offset || frame_end - frame < count)
  108. return -1;
  109. // can't use av_memcpy_backptr() since it can overwrite following pixels
  110. for (v = 0; v < count; v++)
  111. frame[v] = frame[v - offset];
  112. frame += count;
  113. } else if (bitbuf & (mask << 1)) {
  114. frame += bytestream2_get_le16(gb);
  115. } else {
  116. *frame++ = bytestream2_get_byte(gb);
  117. *frame++ = bytestream2_get_byte(gb);
  118. }
  119. mask <<= 2;
  120. }
  121. return 0;
  122. }
  123. static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
  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 = bytestream2_get_le16(gb);
  130. while (segments--) {
  131. if (bytestream2_get_bytes_left(gb) < 2)
  132. return -1;
  133. if (mask == 0x10000) {
  134. bitbuf = bytestream2_get_le16u(gb);
  135. mask = 1;
  136. }
  137. if (frame_end - frame < 2)
  138. return -1;
  139. if (bitbuf & mask) {
  140. v = bytestream2_get_le16(gb);
  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 += bytestream2_get_le16(gb) * 2;
  152. } else {
  153. frame[0] = frame[1] =
  154. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  155. frame += 2;
  156. frame[0] = frame[1] =
  157. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  158. frame += 2;
  159. }
  160. mask <<= 2;
  161. }
  162. return 0;
  163. }
  164. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  165. {
  166. uint8_t *line_ptr;
  167. int count, lines, segments;
  168. count = bytestream2_get_le16(gb);
  169. if (count >= height)
  170. return -1;
  171. frame += width * count;
  172. lines = bytestream2_get_le16(gb);
  173. if (count + lines > height)
  174. return -1;
  175. while (lines--) {
  176. if (bytestream2_get_bytes_left(gb) < 1)
  177. return -1;
  178. line_ptr = frame;
  179. frame += width;
  180. segments = bytestream2_get_byteu(gb);
  181. while (segments--) {
  182. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  183. return -1;
  184. line_ptr += bytestream2_get_byte(gb);
  185. count = (int8_t)bytestream2_get_byte(gb);
  186. if (count >= 0) {
  187. if (frame - line_ptr < count)
  188. return -1;
  189. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  190. return -1;
  191. } else {
  192. count = -count;
  193. if (frame - line_ptr < count)
  194. return -1;
  195. memset(line_ptr, bytestream2_get_byte(gb), count);
  196. }
  197. line_ptr += count;
  198. }
  199. }
  200. return 0;
  201. }
  202. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  203. {
  204. const uint8_t *frame_end = frame + width * height;
  205. uint8_t *line_ptr;
  206. int count, i, v, lines, segments;
  207. lines = bytestream2_get_le16(gb);
  208. if (lines > height)
  209. return -1;
  210. while (lines--) {
  211. if (bytestream2_get_bytes_left(gb) < 2)
  212. return -1;
  213. segments = bytestream2_get_le16u(gb);
  214. while ((segments & 0xC000) == 0xC000) {
  215. unsigned delta = -((int16_t)segments * width);
  216. if (frame_end - frame <= delta)
  217. return -1;
  218. frame += delta;
  219. segments = bytestream2_get_le16(gb);
  220. }
  221. if (segments & 0x8000) {
  222. frame[width - 1] = segments & 0xFF;
  223. segments = bytestream2_get_le16(gb);
  224. }
  225. line_ptr = frame;
  226. frame += width;
  227. while (segments--) {
  228. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  229. return -1;
  230. line_ptr += bytestream2_get_byte(gb);
  231. count = (int8_t)bytestream2_get_byte(gb);
  232. if (count >= 0) {
  233. if (frame - line_ptr < count * 2)
  234. return -1;
  235. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  236. return -1;
  237. line_ptr += count * 2;
  238. } else {
  239. count = -count;
  240. if (frame - line_ptr < count * 2)
  241. return -1;
  242. v = bytestream2_get_le16(gb);
  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(GetByteContext *gb, uint8_t *frame, int width, int height)
  251. {
  252. return -1;
  253. }
  254. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  255. {
  256. memset(frame, 0, width * height);
  257. return 0;
  258. }
  259. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  260. static const chunk_decoder decoder[8] = {
  261. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  262. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  263. };
  264. static const char* chunk_name[8] = {
  265. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  266. };
  267. static int dfa_decode_frame(AVCodecContext *avctx,
  268. void *data, int *data_size,
  269. AVPacket *avpkt)
  270. {
  271. DfaContext *s = avctx->priv_data;
  272. GetByteContext gb;
  273. const uint8_t *buf = avpkt->data;
  274. uint32_t chunk_type, chunk_size;
  275. uint8_t *dst;
  276. int ret;
  277. int i, pal_elems;
  278. if (s->pic.data[0])
  279. avctx->release_buffer(avctx, &s->pic);
  280. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  281. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  282. return ret;
  283. }
  284. bytestream2_init(&gb, avpkt->data, avpkt->size);
  285. while (bytestream2_get_bytes_left(&gb) > 0) {
  286. bytestream2_skip(&gb, 4);
  287. chunk_size = bytestream2_get_le32(&gb);
  288. chunk_type = bytestream2_get_le32(&gb);
  289. if (!chunk_type)
  290. break;
  291. if (chunk_type == 1) {
  292. pal_elems = FFMIN(chunk_size / 3, 256);
  293. for (i = 0; i < pal_elems; i++) {
  294. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  295. s->pal[i] |= (s->pal[i] >> 6) & 0x333;
  296. }
  297. s->pic.palette_has_changed = 1;
  298. } else if (chunk_type <= 9) {
  299. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  300. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  301. chunk_name[chunk_type - 2]);
  302. return -1;
  303. }
  304. } else {
  305. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  306. chunk_type);
  307. }
  308. buf += chunk_size;
  309. }
  310. buf = s->frame_buf;
  311. dst = s->pic.data[0];
  312. for (i = 0; i < avctx->height; i++) {
  313. memcpy(dst, buf, avctx->width);
  314. dst += s->pic.linesize[0];
  315. buf += avctx->width;
  316. }
  317. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  318. *data_size = sizeof(AVFrame);
  319. *(AVFrame*)data = s->pic;
  320. return avpkt->size;
  321. }
  322. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  323. {
  324. DfaContext *s = avctx->priv_data;
  325. if (s->pic.data[0])
  326. avctx->release_buffer(avctx, &s->pic);
  327. av_freep(&s->frame_buf);
  328. return 0;
  329. }
  330. AVCodec ff_dfa_decoder = {
  331. .name = "dfa",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .id = CODEC_ID_DFA,
  334. .priv_data_size = sizeof(DfaContext),
  335. .init = dfa_decode_init,
  336. .close = dfa_decode_end,
  337. .decode = dfa_decode_frame,
  338. .capabilities = CODEC_CAP_DR1,
  339. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  340. };