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.

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