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.

392 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 "bytestream.h"
  24. #include "libavutil/lzo.h" // for av_memcpy_backptr
  25. typedef struct DfaContext {
  26. AVFrame pic;
  27. uint32_t pal[256];
  28. uint8_t *frame_buf;
  29. } DfaContext;
  30. static av_cold int dfa_decode_init(AVCodecContext *avctx)
  31. {
  32. DfaContext *s = avctx->priv_data;
  33. avctx->pix_fmt = PIX_FMT_PAL8;
  34. s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
  35. if (!s->frame_buf)
  36. return AVERROR(ENOMEM);
  37. return 0;
  38. }
  39. static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
  40. {
  41. const int size = width * height;
  42. if (bytestream2_get_buffer(gb, frame, size) != size)
  43. return AVERROR_INVALIDDATA;
  44. return 0;
  45. }
  46. static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  47. {
  48. const uint8_t *frame_start = frame;
  49. const uint8_t *frame_end = frame + width * height;
  50. int mask = 0x10000, bitbuf = 0;
  51. int v, count, segments;
  52. unsigned offset;
  53. segments = bytestream2_get_le32(gb);
  54. offset = bytestream2_get_le32(gb);
  55. if (segments == 0 && offset == frame_end - frame)
  56. return 0; // skip frame
  57. if (frame_end - frame <= offset)
  58. return AVERROR_INVALIDDATA;
  59. frame += offset;
  60. while (segments--) {
  61. if (bytestream2_get_bytes_left(gb) < 2)
  62. return AVERROR_INVALIDDATA;
  63. if (mask == 0x10000) {
  64. bitbuf = bytestream2_get_le16u(gb);
  65. mask = 1;
  66. }
  67. if (frame_end - frame < 2)
  68. return AVERROR_INVALIDDATA;
  69. if (bitbuf & mask) {
  70. v = bytestream2_get_le16(gb);
  71. offset = (v & 0x1FFF) << 1;
  72. count = ((v >> 13) + 2) << 1;
  73. if (frame - frame_start < offset || frame_end - frame < count)
  74. return AVERROR_INVALIDDATA;
  75. av_memcpy_backptr(frame, offset, count);
  76. frame += count;
  77. } else {
  78. *frame++ = bytestream2_get_byte(gb);
  79. *frame++ = bytestream2_get_byte(gb);
  80. }
  81. mask <<= 1;
  82. }
  83. return 0;
  84. }
  85. static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  86. {
  87. const uint8_t *frame_start = frame;
  88. const uint8_t *frame_end = frame + width * height;
  89. int mask = 0x10000, bitbuf = 0;
  90. int v, offset, count, segments;
  91. segments = bytestream2_get_le16(gb);
  92. while (segments--) {
  93. if (bytestream2_get_bytes_left(gb) < 2)
  94. return AVERROR_INVALIDDATA;
  95. if (mask == 0x10000) {
  96. bitbuf = bytestream2_get_le16u(gb);
  97. mask = 1;
  98. }
  99. if (frame_end - frame < 2)
  100. return AVERROR_INVALIDDATA;
  101. if (bitbuf & mask) {
  102. v = bytestream2_get_le16(gb);
  103. offset = (v & 0x1FFF) << 1;
  104. count = ((v >> 13) + 2) << 1;
  105. if (frame - frame_start < offset || frame_end - frame < count)
  106. return AVERROR_INVALIDDATA;
  107. // can't use av_memcpy_backptr() since it can overwrite following pixels
  108. for (v = 0; v < count; v++)
  109. frame[v] = frame[v - offset];
  110. frame += count;
  111. } else if (bitbuf & (mask << 1)) {
  112. frame += bytestream2_get_le16(gb);
  113. } else {
  114. *frame++ = bytestream2_get_byte(gb);
  115. *frame++ = bytestream2_get_byte(gb);
  116. }
  117. mask <<= 2;
  118. }
  119. return 0;
  120. }
  121. static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
  122. {
  123. const uint8_t *frame_start = frame;
  124. const uint8_t *frame_end = frame + width * height;
  125. int mask = 0x10000, bitbuf = 0;
  126. int i, v, offset, count, segments;
  127. segments = bytestream2_get_le16(gb);
  128. while (segments--) {
  129. if (bytestream2_get_bytes_left(gb) < 2)
  130. return AVERROR_INVALIDDATA;
  131. if (mask == 0x10000) {
  132. bitbuf = bytestream2_get_le16u(gb);
  133. mask = 1;
  134. }
  135. if (frame_end - frame < width + 2)
  136. return AVERROR_INVALIDDATA;
  137. if (bitbuf & mask) {
  138. v = bytestream2_get_le16(gb);
  139. offset = (v & 0x1FFF) << 2;
  140. count = ((v >> 13) + 2) << 1;
  141. if (frame - frame_start < offset || frame_end - frame < count*2 + width)
  142. return AVERROR_INVALIDDATA;
  143. for (i = 0; i < count; i++) {
  144. frame[0] = frame[1] =
  145. frame[width] = frame[width + 1] = frame[-offset];
  146. frame += 2;
  147. }
  148. } else if (bitbuf & (mask << 1)) {
  149. frame += bytestream2_get_le16(gb) * 2;
  150. } else {
  151. if (frame_end - frame < width + 2)
  152. return AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  171. frame += width * count;
  172. lines = bytestream2_get_le16(gb);
  173. if (count + lines > height)
  174. return AVERROR_INVALIDDATA;
  175. while (lines--) {
  176. if (bytestream2_get_bytes_left(gb) < 1)
  177. return AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  189. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  190. return AVERROR_INVALIDDATA;
  191. } else {
  192. count = -count;
  193. if (frame - line_ptr < count)
  194. return AVERROR_INVALIDDATA;
  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. int y = 0;
  208. lines = bytestream2_get_le16(gb);
  209. if (lines > height)
  210. return AVERROR_INVALIDDATA;
  211. while (lines--) {
  212. if (bytestream2_get_bytes_left(gb) < 2)
  213. return AVERROR_INVALIDDATA;
  214. segments = bytestream2_get_le16u(gb);
  215. while ((segments & 0xC000) == 0xC000) {
  216. unsigned skip_lines = -(int16_t)segments;
  217. unsigned delta = -((int16_t)segments * width);
  218. if (frame_end - frame <= delta || y + lines + skip_lines > height)
  219. return AVERROR_INVALIDDATA;
  220. frame += delta;
  221. y += skip_lines;
  222. segments = bytestream2_get_le16(gb);
  223. }
  224. if (frame_end <= frame)
  225. return -1;
  226. if (segments & 0x8000) {
  227. frame[width - 1] = segments & 0xFF;
  228. segments = bytestream2_get_le16(gb);
  229. }
  230. line_ptr = frame;
  231. frame += width;
  232. y++;
  233. while (segments--) {
  234. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  235. return AVERROR_INVALIDDATA;
  236. line_ptr += bytestream2_get_byte(gb);
  237. count = (int8_t)bytestream2_get_byte(gb);
  238. if (count >= 0) {
  239. if (frame - line_ptr < count * 2)
  240. return AVERROR_INVALIDDATA;
  241. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  242. return AVERROR_INVALIDDATA;
  243. line_ptr += count * 2;
  244. } else {
  245. count = -count;
  246. if (frame - line_ptr < count * 2)
  247. return AVERROR_INVALIDDATA;
  248. v = bytestream2_get_le16(gb);
  249. for (i = 0; i < count; i++)
  250. bytestream_put_le16(&line_ptr, v);
  251. }
  252. }
  253. }
  254. return 0;
  255. }
  256. static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
  257. {
  258. return AVERROR_PATCHWELCOME;
  259. }
  260. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  261. {
  262. memset(frame, 0, width * height);
  263. return 0;
  264. }
  265. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  266. static const chunk_decoder decoder[8] = {
  267. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  268. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  269. };
  270. static const char* chunk_name[8] = {
  271. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  272. };
  273. static int dfa_decode_frame(AVCodecContext *avctx,
  274. void *data, int *data_size,
  275. AVPacket *avpkt)
  276. {
  277. DfaContext *s = avctx->priv_data;
  278. GetByteContext gb;
  279. const uint8_t *buf = avpkt->data;
  280. uint32_t chunk_type, chunk_size;
  281. uint8_t *dst;
  282. int ret;
  283. int i, pal_elems;
  284. if (s->pic.data[0])
  285. avctx->release_buffer(avctx, &s->pic);
  286. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  287. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  288. return ret;
  289. }
  290. bytestream2_init(&gb, avpkt->data, avpkt->size);
  291. while (bytestream2_get_bytes_left(&gb) > 0) {
  292. bytestream2_skip(&gb, 4);
  293. chunk_size = bytestream2_get_le32(&gb);
  294. chunk_type = bytestream2_get_le32(&gb);
  295. if (!chunk_type)
  296. break;
  297. if (chunk_type == 1) {
  298. pal_elems = FFMIN(chunk_size / 3, 256);
  299. for (i = 0; i < pal_elems; i++) {
  300. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  301. s->pal[i] |= 0xFF << 24 | (s->pal[i] >> 6) & 0x30303;
  302. }
  303. s->pic.palette_has_changed = 1;
  304. } else if (chunk_type <= 9) {
  305. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  306. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  307. chunk_name[chunk_type - 2]);
  308. return AVERROR_INVALIDDATA;
  309. }
  310. } else {
  311. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  312. chunk_type);
  313. }
  314. buf += chunk_size;
  315. }
  316. buf = s->frame_buf;
  317. dst = s->pic.data[0];
  318. for (i = 0; i < avctx->height; i++) {
  319. memcpy(dst, buf, avctx->width);
  320. dst += s->pic.linesize[0];
  321. buf += avctx->width;
  322. }
  323. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  324. *data_size = sizeof(AVFrame);
  325. *(AVFrame*)data = s->pic;
  326. return avpkt->size;
  327. }
  328. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  329. {
  330. DfaContext *s = avctx->priv_data;
  331. if (s->pic.data[0])
  332. avctx->release_buffer(avctx, &s->pic);
  333. av_freep(&s->frame_buf);
  334. return 0;
  335. }
  336. AVCodec ff_dfa_decoder = {
  337. .name = "dfa",
  338. .type = AVMEDIA_TYPE_VIDEO,
  339. .id = AV_CODEC_ID_DFA,
  340. .priv_data_size = sizeof(DfaContext),
  341. .init = dfa_decode_init,
  342. .close = dfa_decode_end,
  343. .decode = dfa_decode_frame,
  344. .capabilities = CODEC_CAP_DR1,
  345. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  346. };