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.

391 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 "internal.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/mem.h"
  28. typedef struct DfaContext {
  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);
  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. av_memcpy_backptr(frame, offset, count);
  113. frame += count;
  114. } else if (bitbuf & (mask << 1)) {
  115. frame += bytestream2_get_le16(gb);
  116. } else {
  117. *frame++ = bytestream2_get_byte(gb);
  118. *frame++ = bytestream2_get_byte(gb);
  119. }
  120. mask <<= 2;
  121. }
  122. return 0;
  123. }
  124. static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
  125. {
  126. const uint8_t *frame_start = frame;
  127. const uint8_t *frame_end = frame + width * height;
  128. int mask = 0x10000, bitbuf = 0;
  129. int i, v, offset, count, segments;
  130. segments = bytestream2_get_le16(gb);
  131. while (segments--) {
  132. if (bytestream2_get_bytes_left(gb) < 2)
  133. return AVERROR_INVALIDDATA;
  134. if (mask == 0x10000) {
  135. bitbuf = bytestream2_get_le16u(gb);
  136. mask = 1;
  137. }
  138. if (bitbuf & mask) {
  139. v = bytestream2_get_le16(gb);
  140. offset = (v & 0x1FFF) << 2;
  141. count = ((v >> 13) + 2) << 1;
  142. if (frame - frame_start < offset || frame_end - frame < count*2 + width)
  143. return AVERROR_INVALIDDATA;
  144. for (i = 0; i < count; i++) {
  145. frame[0] = frame[1] =
  146. frame[width] = frame[width + 1] = frame[-offset];
  147. frame += 2;
  148. }
  149. } else if (bitbuf & (mask << 1)) {
  150. v = bytestream2_get_le16(gb)*2;
  151. if (frame - frame_end < v)
  152. return AVERROR_INVALIDDATA;
  153. frame += v;
  154. } else {
  155. if (frame_end - frame < width + 3)
  156. return AVERROR_INVALIDDATA;
  157. frame[0] = frame[1] =
  158. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  159. frame += 2;
  160. frame[0] = frame[1] =
  161. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  162. frame += 2;
  163. }
  164. mask <<= 2;
  165. }
  166. return 0;
  167. }
  168. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  169. {
  170. uint8_t *line_ptr;
  171. int count, lines, segments;
  172. count = bytestream2_get_le16(gb);
  173. if (count >= height)
  174. return AVERROR_INVALIDDATA;
  175. frame += width * count;
  176. lines = bytestream2_get_le16(gb);
  177. if (count + lines > height)
  178. return AVERROR_INVALIDDATA;
  179. while (lines--) {
  180. if (bytestream2_get_bytes_left(gb) < 1)
  181. return AVERROR_INVALIDDATA;
  182. line_ptr = frame;
  183. frame += width;
  184. segments = bytestream2_get_byteu(gb);
  185. while (segments--) {
  186. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  187. return AVERROR_INVALIDDATA;
  188. line_ptr += bytestream2_get_byte(gb);
  189. count = (int8_t)bytestream2_get_byte(gb);
  190. if (count >= 0) {
  191. if (frame - line_ptr < count)
  192. return AVERROR_INVALIDDATA;
  193. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  194. return AVERROR_INVALIDDATA;
  195. } else {
  196. count = -count;
  197. if (frame - line_ptr < count)
  198. return AVERROR_INVALIDDATA;
  199. memset(line_ptr, bytestream2_get_byte(gb), count);
  200. }
  201. line_ptr += count;
  202. }
  203. }
  204. return 0;
  205. }
  206. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  207. {
  208. const uint8_t *frame_end = frame + width * height;
  209. uint8_t *line_ptr;
  210. int count, i, v, lines, segments;
  211. int y = 0;
  212. lines = bytestream2_get_le16(gb);
  213. if (lines > height)
  214. return AVERROR_INVALIDDATA;
  215. while (lines--) {
  216. if (bytestream2_get_bytes_left(gb) < 2)
  217. return AVERROR_INVALIDDATA;
  218. segments = bytestream2_get_le16u(gb);
  219. while ((segments & 0xC000) == 0xC000) {
  220. unsigned skip_lines = -(int16_t)segments;
  221. unsigned delta = -((int16_t)segments * width);
  222. if (frame_end - frame <= delta || y + lines + skip_lines > height)
  223. return AVERROR_INVALIDDATA;
  224. frame += delta;
  225. y += skip_lines;
  226. segments = bytestream2_get_le16(gb);
  227. }
  228. if (segments & 0x8000) {
  229. frame[width - 1] = segments & 0xFF;
  230. segments = bytestream2_get_le16(gb);
  231. }
  232. line_ptr = frame;
  233. if (frame_end - frame < width)
  234. return AVERROR_INVALIDDATA;
  235. frame += width;
  236. y++;
  237. while (segments--) {
  238. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  239. return AVERROR_INVALIDDATA;
  240. line_ptr += bytestream2_get_byte(gb);
  241. count = (int8_t)bytestream2_get_byte(gb);
  242. if (count >= 0) {
  243. if (frame - line_ptr < count * 2)
  244. return AVERROR_INVALIDDATA;
  245. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  246. return AVERROR_INVALIDDATA;
  247. line_ptr += count * 2;
  248. } else {
  249. count = -count;
  250. if (frame - line_ptr < count * 2)
  251. return AVERROR_INVALIDDATA;
  252. v = bytestream2_get_le16(gb);
  253. for (i = 0; i < count; i++)
  254. bytestream_put_le16(&line_ptr, v);
  255. }
  256. }
  257. }
  258. return 0;
  259. }
  260. static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
  261. {
  262. return AVERROR_PATCHWELCOME;
  263. }
  264. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  265. {
  266. memset(frame, 0, width * height);
  267. return 0;
  268. }
  269. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  270. static const chunk_decoder decoder[8] = {
  271. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  272. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  273. };
  274. static const char* chunk_name[8] = {
  275. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  276. };
  277. static int dfa_decode_frame(AVCodecContext *avctx,
  278. void *data, int *got_frame,
  279. AVPacket *avpkt)
  280. {
  281. AVFrame *frame = data;
  282. DfaContext *s = avctx->priv_data;
  283. GetByteContext gb;
  284. const uint8_t *buf = avpkt->data;
  285. uint32_t chunk_type, chunk_size;
  286. uint8_t *dst;
  287. int ret;
  288. int i, pal_elems;
  289. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  290. return ret;
  291. bytestream2_init(&gb, avpkt->data, avpkt->size);
  292. while (bytestream2_get_bytes_left(&gb) > 0) {
  293. bytestream2_skip(&gb, 4);
  294. chunk_size = bytestream2_get_le32(&gb);
  295. chunk_type = bytestream2_get_le32(&gb);
  296. if (!chunk_type)
  297. break;
  298. if (chunk_type == 1) {
  299. pal_elems = FFMIN(chunk_size / 3, 256);
  300. for (i = 0; i < pal_elems; i++) {
  301. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  302. s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
  303. }
  304. frame->palette_has_changed = 1;
  305. } else if (chunk_type <= 9) {
  306. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  307. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  308. chunk_name[chunk_type - 2]);
  309. return AVERROR_INVALIDDATA;
  310. }
  311. } else {
  312. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  313. chunk_type);
  314. }
  315. buf += chunk_size;
  316. }
  317. buf = s->frame_buf;
  318. dst = frame->data[0];
  319. for (i = 0; i < avctx->height; i++) {
  320. memcpy(dst, buf, avctx->width);
  321. dst += frame->linesize[0];
  322. buf += avctx->width;
  323. }
  324. memcpy(frame->data[1], s->pal, sizeof(s->pal));
  325. *got_frame = 1;
  326. return avpkt->size;
  327. }
  328. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  329. {
  330. DfaContext *s = avctx->priv_data;
  331. av_freep(&s->frame_buf);
  332. return 0;
  333. }
  334. AVCodec ff_dfa_decoder = {
  335. .name = "dfa",
  336. .type = AVMEDIA_TYPE_VIDEO,
  337. .id = AV_CODEC_ID_DFA,
  338. .priv_data_size = sizeof(DfaContext),
  339. .init = dfa_decode_init,
  340. .close = dfa_decode_end,
  341. .decode = dfa_decode_frame,
  342. .capabilities = CODEC_CAP_DR1,
  343. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  344. };