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.

386 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 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 -1;
  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 -1;
  59. frame += offset;
  60. while (segments--) {
  61. if (bytestream2_get_bytes_left(gb) < 2)
  62. return -1;
  63. if (mask == 0x10000) {
  64. bitbuf = bytestream2_get_le16u(gb);
  65. mask = 1;
  66. }
  67. if (frame_end - frame < 2)
  68. return -1;
  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 -1;
  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 -1;
  95. if (mask == 0x10000) {
  96. bitbuf = bytestream2_get_le16u(gb);
  97. mask = 1;
  98. }
  99. if (frame_end - frame < 2)
  100. return -1;
  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 -1;
  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 -1;
  131. if (mask == 0x10000) {
  132. bitbuf = bytestream2_get_le16u(gb);
  133. mask = 1;
  134. }
  135. if (frame_end - frame < width + 2)
  136. return -1;
  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 -1;
  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. frame[0] = frame[1] =
  152. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  153. frame += 2;
  154. frame[0] = frame[1] =
  155. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  156. frame += 2;
  157. }
  158. mask <<= 2;
  159. }
  160. return 0;
  161. }
  162. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  163. {
  164. uint8_t *line_ptr;
  165. int count, lines, segments;
  166. count = bytestream2_get_le16(gb);
  167. if (count >= height)
  168. return -1;
  169. frame += width * count;
  170. lines = bytestream2_get_le16(gb);
  171. if (count + lines > height)
  172. return -1;
  173. while (lines--) {
  174. if (bytestream2_get_bytes_left(gb) < 1)
  175. return -1;
  176. line_ptr = frame;
  177. frame += width;
  178. segments = bytestream2_get_byteu(gb);
  179. while (segments--) {
  180. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  181. return -1;
  182. line_ptr += bytestream2_get_byte(gb);
  183. count = (int8_t)bytestream2_get_byte(gb);
  184. if (count >= 0) {
  185. if (frame - line_ptr < count)
  186. return -1;
  187. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  188. return -1;
  189. } else {
  190. count = -count;
  191. if (frame - line_ptr < count)
  192. return -1;
  193. memset(line_ptr, bytestream2_get_byte(gb), count);
  194. }
  195. line_ptr += count;
  196. }
  197. }
  198. return 0;
  199. }
  200. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  201. {
  202. const uint8_t *frame_end = frame + width * height;
  203. uint8_t *line_ptr;
  204. int count, i, v, lines, segments;
  205. lines = bytestream2_get_le16(gb);
  206. if (lines > height)
  207. return -1;
  208. while (lines--) {
  209. if (bytestream2_get_bytes_left(gb) < 2)
  210. return -1;
  211. segments = bytestream2_get_le16u(gb);
  212. while ((segments & 0xC000) == 0xC000) {
  213. unsigned delta = -((int16_t)segments * width);
  214. if (frame_end - frame <= delta)
  215. return -1;
  216. frame += delta;
  217. segments = bytestream2_get_le16(gb);
  218. }
  219. if (frame_end <= frame)
  220. return -1;
  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] |= 0xFF << 24 | (s->pal[i] >> 6) & 0x30303;
  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. };