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.

396 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 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 = AV_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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  61. frame += offset;
  62. while (segments--) {
  63. if (bytestream2_get_bytes_left(gb) < 2)
  64. return AVERROR_INVALIDDATA;
  65. if (mask == 0x10000) {
  66. bitbuf = bytestream2_get_le16u(gb);
  67. mask = 1;
  68. }
  69. if (frame_end - frame < 2)
  70. return AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  97. if (mask == 0x10000) {
  98. bitbuf = bytestream2_get_le16u(gb);
  99. mask = 1;
  100. }
  101. if (frame_end - frame < 2)
  102. return AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  133. if (mask == 0x10000) {
  134. bitbuf = bytestream2_get_le16u(gb);
  135. mask = 1;
  136. }
  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. v = bytestream2_get_le16(gb)*2;
  150. if (frame - frame_end < v)
  151. return AVERROR_INVALIDDATA;
  152. frame += v;
  153. } else {
  154. if (frame_end - frame < width + 3)
  155. return AVERROR_INVALIDDATA;
  156. frame[0] = frame[1] =
  157. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  158. frame += 2;
  159. frame[0] = frame[1] =
  160. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  161. frame += 2;
  162. }
  163. mask <<= 2;
  164. }
  165. return 0;
  166. }
  167. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  168. {
  169. uint8_t *line_ptr;
  170. int count, lines, segments;
  171. count = bytestream2_get_le16(gb);
  172. if (count >= height)
  173. return AVERROR_INVALIDDATA;
  174. frame += width * count;
  175. lines = bytestream2_get_le16(gb);
  176. if (count + lines > height)
  177. return AVERROR_INVALIDDATA;
  178. while (lines--) {
  179. if (bytestream2_get_bytes_left(gb) < 1)
  180. return AVERROR_INVALIDDATA;
  181. line_ptr = frame;
  182. frame += width;
  183. segments = bytestream2_get_byteu(gb);
  184. while (segments--) {
  185. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  186. return AVERROR_INVALIDDATA;
  187. line_ptr += bytestream2_get_byte(gb);
  188. count = (int8_t)bytestream2_get_byte(gb);
  189. if (count >= 0) {
  190. if (frame - line_ptr < count)
  191. return AVERROR_INVALIDDATA;
  192. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  193. return AVERROR_INVALIDDATA;
  194. } else {
  195. count = -count;
  196. if (frame - line_ptr < count)
  197. return AVERROR_INVALIDDATA;
  198. memset(line_ptr, bytestream2_get_byte(gb), count);
  199. }
  200. line_ptr += count;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  206. {
  207. const uint8_t *frame_end = frame + width * height;
  208. uint8_t *line_ptr;
  209. int count, i, v, lines, segments;
  210. int y = 0;
  211. lines = bytestream2_get_le16(gb);
  212. if (lines > height)
  213. return AVERROR_INVALIDDATA;
  214. while (lines--) {
  215. if (bytestream2_get_bytes_left(gb) < 2)
  216. return AVERROR_INVALIDDATA;
  217. segments = bytestream2_get_le16u(gb);
  218. while ((segments & 0xC000) == 0xC000) {
  219. unsigned skip_lines = -(int16_t)segments;
  220. unsigned delta = -((int16_t)segments * width);
  221. if (frame_end - frame <= delta || y + lines + skip_lines > height)
  222. return AVERROR_INVALIDDATA;
  223. frame += delta;
  224. y += skip_lines;
  225. segments = bytestream2_get_le16(gb);
  226. }
  227. if (segments & 0x8000) {
  228. frame[width - 1] = segments & 0xFF;
  229. segments = bytestream2_get_le16(gb);
  230. }
  231. line_ptr = frame;
  232. frame += width;
  233. y++;
  234. while (segments--) {
  235. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  236. return AVERROR_INVALIDDATA;
  237. line_ptr += bytestream2_get_byte(gb);
  238. count = (int8_t)bytestream2_get_byte(gb);
  239. if (count >= 0) {
  240. if (frame - line_ptr < count * 2)
  241. return AVERROR_INVALIDDATA;
  242. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  243. return AVERROR_INVALIDDATA;
  244. line_ptr += count * 2;
  245. } else {
  246. count = -count;
  247. if (frame - line_ptr < count * 2)
  248. return AVERROR_INVALIDDATA;
  249. v = bytestream2_get_le16(gb);
  250. for (i = 0; i < count; i++)
  251. bytestream_put_le16(&line_ptr, v);
  252. }
  253. }
  254. }
  255. return 0;
  256. }
  257. static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
  258. {
  259. return AVERROR_PATCHWELCOME;
  260. }
  261. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  262. {
  263. memset(frame, 0, width * height);
  264. return 0;
  265. }
  266. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  267. static const chunk_decoder decoder[8] = {
  268. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  269. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  270. };
  271. static const char* chunk_name[8] = {
  272. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  273. };
  274. static int dfa_decode_frame(AVCodecContext *avctx,
  275. void *data, int *data_size,
  276. AVPacket *avpkt)
  277. {
  278. DfaContext *s = avctx->priv_data;
  279. GetByteContext gb;
  280. const uint8_t *buf = avpkt->data;
  281. uint32_t chunk_type, chunk_size;
  282. uint8_t *dst;
  283. int ret;
  284. int i, pal_elems;
  285. if (s->pic.data[0])
  286. avctx->release_buffer(avctx, &s->pic);
  287. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  288. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  289. return ret;
  290. }
  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] |= (s->pal[i] >> 6) & 0x333;
  303. }
  304. s->pic.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 = s->pic.data[0];
  319. for (i = 0; i < avctx->height; i++) {
  320. memcpy(dst, buf, avctx->width);
  321. dst += s->pic.linesize[0];
  322. buf += avctx->width;
  323. }
  324. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  325. *data_size = sizeof(AVFrame);
  326. *(AVFrame*)data = s->pic;
  327. return avpkt->size;
  328. }
  329. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  330. {
  331. DfaContext *s = avctx->priv_data;
  332. if (s->pic.data[0])
  333. avctx->release_buffer(avctx, &s->pic);
  334. av_freep(&s->frame_buf);
  335. return 0;
  336. }
  337. AVCodec ff_dfa_decoder = {
  338. .name = "dfa",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .id = AV_CODEC_ID_DFA,
  341. .priv_data_size = sizeof(DfaContext),
  342. .init = dfa_decode_init,
  343. .close = dfa_decode_end,
  344. .decode = dfa_decode_frame,
  345. .capabilities = CODEC_CAP_DR1,
  346. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  347. };