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.

402 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 "libavutil/avassert.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/lzo.h" // for av_memcpy_backptr
  27. typedef struct DfaContext {
  28. AVFrame pic;
  29. uint32_t pal[256];
  30. uint8_t *frame_buf;
  31. } DfaContext;
  32. static av_cold int dfa_decode_init(AVCodecContext *avctx)
  33. {
  34. DfaContext *s = avctx->priv_data;
  35. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  36. if (!avctx->width || !avctx->height)
  37. return AVERROR_INVALIDDATA;
  38. av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
  39. s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
  40. if (!s->frame_buf)
  41. return AVERROR(ENOMEM);
  42. return 0;
  43. }
  44. static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
  45. {
  46. const int size = width * height;
  47. if (bytestream2_get_buffer(gb, frame, size) != size)
  48. return AVERROR_INVALIDDATA;
  49. return 0;
  50. }
  51. static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  52. {
  53. const uint8_t *frame_start = frame;
  54. const uint8_t *frame_end = frame + width * height;
  55. int mask = 0x10000, bitbuf = 0;
  56. int v, count, segments;
  57. unsigned offset;
  58. segments = bytestream2_get_le32(gb);
  59. offset = bytestream2_get_le32(gb);
  60. if (segments == 0 && offset == frame_end - frame)
  61. return 0; // skip frame
  62. if (frame_end - frame <= offset)
  63. return AVERROR_INVALIDDATA;
  64. frame += offset;
  65. while (segments--) {
  66. if (bytestream2_get_bytes_left(gb) < 2)
  67. return AVERROR_INVALIDDATA;
  68. if (mask == 0x10000) {
  69. bitbuf = bytestream2_get_le16u(gb);
  70. mask = 1;
  71. }
  72. if (frame_end - frame < 2)
  73. return AVERROR_INVALIDDATA;
  74. if (bitbuf & mask) {
  75. v = bytestream2_get_le16(gb);
  76. offset = (v & 0x1FFF) << 1;
  77. count = ((v >> 13) + 2) << 1;
  78. if (frame - frame_start < offset || frame_end - frame < count)
  79. return AVERROR_INVALIDDATA;
  80. av_memcpy_backptr(frame, offset, count);
  81. frame += count;
  82. } else {
  83. *frame++ = bytestream2_get_byte(gb);
  84. *frame++ = bytestream2_get_byte(gb);
  85. }
  86. mask <<= 1;
  87. }
  88. return 0;
  89. }
  90. static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  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 = bytestream2_get_le16(gb);
  97. while (segments--) {
  98. if (bytestream2_get_bytes_left(gb) < 2)
  99. return AVERROR_INVALIDDATA;
  100. if (mask == 0x10000) {
  101. bitbuf = bytestream2_get_le16u(gb);
  102. mask = 1;
  103. }
  104. if (frame_end - frame < 2)
  105. return AVERROR_INVALIDDATA;
  106. if (bitbuf & mask) {
  107. v = bytestream2_get_le16(gb);
  108. offset = (v & 0x1FFF) << 1;
  109. count = ((v >> 13) + 2) << 1;
  110. if (frame - frame_start < offset || frame_end - frame < count)
  111. return AVERROR_INVALIDDATA;
  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 += bytestream2_get_le16(gb);
  118. } else {
  119. *frame++ = bytestream2_get_byte(gb);
  120. *frame++ = bytestream2_get_byte(gb);
  121. }
  122. mask <<= 2;
  123. }
  124. return 0;
  125. }
  126. static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
  127. {
  128. const uint8_t *frame_start = frame;
  129. const uint8_t *frame_end = frame + width * height;
  130. int mask = 0x10000, bitbuf = 0;
  131. int i, v, offset, count, segments;
  132. segments = bytestream2_get_le16(gb);
  133. while (segments--) {
  134. if (bytestream2_get_bytes_left(gb) < 2)
  135. return AVERROR_INVALIDDATA;
  136. if (mask == 0x10000) {
  137. bitbuf = bytestream2_get_le16u(gb);
  138. mask = 1;
  139. }
  140. if (bitbuf & mask) {
  141. v = bytestream2_get_le16(gb);
  142. offset = (v & 0x1FFF) << 2;
  143. count = ((v >> 13) + 2) << 1;
  144. if (frame - frame_start < offset || frame_end - frame < count*2 + width)
  145. return AVERROR_INVALIDDATA;
  146. for (i = 0; i < count; i++) {
  147. frame[0] = frame[1] =
  148. frame[width] = frame[width + 1] = frame[-offset];
  149. frame += 2;
  150. }
  151. } else if (bitbuf & (mask << 1)) {
  152. v = bytestream2_get_le16(gb)*2;
  153. if (frame - frame_end < v)
  154. return AVERROR_INVALIDDATA;
  155. frame += v;
  156. } else {
  157. if (frame_end - frame < width + 3)
  158. return AVERROR_INVALIDDATA;
  159. frame[0] = frame[1] =
  160. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  161. frame += 2;
  162. frame[0] = frame[1] =
  163. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  164. frame += 2;
  165. }
  166. mask <<= 2;
  167. }
  168. return 0;
  169. }
  170. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  171. {
  172. uint8_t *line_ptr;
  173. int count, lines, segments;
  174. count = bytestream2_get_le16(gb);
  175. if (count >= height)
  176. return AVERROR_INVALIDDATA;
  177. frame += width * count;
  178. lines = bytestream2_get_le16(gb);
  179. if (count + lines > height)
  180. return AVERROR_INVALIDDATA;
  181. while (lines--) {
  182. if (bytestream2_get_bytes_left(gb) < 1)
  183. return AVERROR_INVALIDDATA;
  184. line_ptr = frame;
  185. frame += width;
  186. segments = bytestream2_get_byteu(gb);
  187. while (segments--) {
  188. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  189. return AVERROR_INVALIDDATA;
  190. line_ptr += bytestream2_get_byte(gb);
  191. count = (int8_t)bytestream2_get_byte(gb);
  192. if (count >= 0) {
  193. if (frame - line_ptr < count)
  194. return AVERROR_INVALIDDATA;
  195. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  196. return AVERROR_INVALIDDATA;
  197. } else {
  198. count = -count;
  199. if (frame - line_ptr < count)
  200. return AVERROR_INVALIDDATA;
  201. memset(line_ptr, bytestream2_get_byte(gb), count);
  202. }
  203. line_ptr += count;
  204. }
  205. }
  206. return 0;
  207. }
  208. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  209. {
  210. const uint8_t *frame_end = frame + width * height;
  211. uint8_t *line_ptr;
  212. int count, i, v, lines, segments;
  213. int y = 0;
  214. lines = bytestream2_get_le16(gb);
  215. if (lines > height)
  216. return AVERROR_INVALIDDATA;
  217. while (lines--) {
  218. if (bytestream2_get_bytes_left(gb) < 2)
  219. return AVERROR_INVALIDDATA;
  220. segments = bytestream2_get_le16u(gb);
  221. while ((segments & 0xC000) == 0xC000) {
  222. unsigned skip_lines = -(int16_t)segments;
  223. unsigned delta = -((int16_t)segments * width);
  224. if (frame_end - frame <= delta || y + lines + skip_lines > height)
  225. return AVERROR_INVALIDDATA;
  226. frame += delta;
  227. y += skip_lines;
  228. segments = bytestream2_get_le16(gb);
  229. }
  230. if (frame_end <= frame)
  231. return -1;
  232. if (segments & 0x8000) {
  233. frame[width - 1] = segments & 0xFF;
  234. segments = bytestream2_get_le16(gb);
  235. }
  236. line_ptr = frame;
  237. frame += width;
  238. y++;
  239. while (segments--) {
  240. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  241. return AVERROR_INVALIDDATA;
  242. line_ptr += bytestream2_get_byte(gb);
  243. count = (int8_t)bytestream2_get_byte(gb);
  244. if (count >= 0) {
  245. if (frame - line_ptr < count * 2)
  246. return AVERROR_INVALIDDATA;
  247. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  248. return AVERROR_INVALIDDATA;
  249. line_ptr += count * 2;
  250. } else {
  251. count = -count;
  252. if (frame - line_ptr < count * 2)
  253. return AVERROR_INVALIDDATA;
  254. v = bytestream2_get_le16(gb);
  255. for (i = 0; i < count; i++)
  256. bytestream_put_le16(&line_ptr, v);
  257. }
  258. }
  259. }
  260. return 0;
  261. }
  262. static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
  263. {
  264. return AVERROR_PATCHWELCOME;
  265. }
  266. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  267. {
  268. memset(frame, 0, width * height);
  269. return 0;
  270. }
  271. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  272. static const chunk_decoder decoder[8] = {
  273. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  274. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  275. };
  276. static const char* chunk_name[8] = {
  277. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  278. };
  279. static int dfa_decode_frame(AVCodecContext *avctx,
  280. void *data, int *data_size,
  281. AVPacket *avpkt)
  282. {
  283. DfaContext *s = avctx->priv_data;
  284. GetByteContext gb;
  285. const uint8_t *buf = avpkt->data;
  286. uint32_t chunk_type, chunk_size;
  287. uint8_t *dst;
  288. int ret;
  289. int i, pal_elems;
  290. if (s->pic.data[0])
  291. avctx->release_buffer(avctx, &s->pic);
  292. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  293. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  294. return ret;
  295. }
  296. bytestream2_init(&gb, avpkt->data, avpkt->size);
  297. while (bytestream2_get_bytes_left(&gb) > 0) {
  298. bytestream2_skip(&gb, 4);
  299. chunk_size = bytestream2_get_le32(&gb);
  300. chunk_type = bytestream2_get_le32(&gb);
  301. if (!chunk_type)
  302. break;
  303. if (chunk_type == 1) {
  304. pal_elems = FFMIN(chunk_size / 3, 256);
  305. for (i = 0; i < pal_elems; i++) {
  306. s->pal[i] = bytestream2_get_be24(&gb) << 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](&gb, s->frame_buf, avctx->width, avctx->height)) {
  312. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  313. chunk_name[chunk_type - 2]);
  314. return AVERROR_INVALIDDATA;
  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. .name = "dfa",
  344. .type = AVMEDIA_TYPE_VIDEO,
  345. .id = AV_CODEC_ID_DFA,
  346. .priv_data_size = sizeof(DfaContext),
  347. .init = dfa_decode_init,
  348. .close = dfa_decode_end,
  349. .decode = dfa_decode_frame,
  350. .capabilities = CODEC_CAP_DR1,
  351. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  352. };