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.

382 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 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 -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 (frame_end - frame <= offset)
  56. return -1;
  57. frame += offset;
  58. while (segments--) {
  59. if (bytestream2_get_bytes_left(gb) < 2)
  60. return -1;
  61. if (mask == 0x10000) {
  62. bitbuf = bytestream2_get_le16u(gb);
  63. mask = 1;
  64. }
  65. if (frame_end - frame < 2)
  66. return -1;
  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 -1;
  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 -1;
  93. if (mask == 0x10000) {
  94. bitbuf = bytestream2_get_le16u(gb);
  95. mask = 1;
  96. }
  97. if (frame_end - frame < 2)
  98. return -1;
  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 -1;
  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 -1;
  129. if (mask == 0x10000) {
  130. bitbuf = bytestream2_get_le16u(gb);
  131. mask = 1;
  132. }
  133. if (frame_end - frame < 2)
  134. return -1;
  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 -1;
  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. frame[0] = frame[1] =
  150. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  151. frame += 2;
  152. frame[0] = frame[1] =
  153. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  154. frame += 2;
  155. }
  156. mask <<= 2;
  157. }
  158. return 0;
  159. }
  160. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  161. {
  162. uint8_t *line_ptr;
  163. int count, lines, segments;
  164. count = bytestream2_get_le16(gb);
  165. if (count >= height)
  166. return -1;
  167. frame += width * count;
  168. lines = bytestream2_get_le16(gb);
  169. if (count + lines > height)
  170. return -1;
  171. while (lines--) {
  172. if (bytestream2_get_bytes_left(gb) < 1)
  173. return -1;
  174. line_ptr = frame;
  175. frame += width;
  176. segments = bytestream2_get_byteu(gb);
  177. while (segments--) {
  178. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  179. return -1;
  180. line_ptr += bytestream2_get_byte(gb);
  181. count = (int8_t)bytestream2_get_byte(gb);
  182. if (count >= 0) {
  183. if (frame - line_ptr < count)
  184. return -1;
  185. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  186. return -1;
  187. } else {
  188. count = -count;
  189. if (frame - line_ptr < count)
  190. return -1;
  191. memset(line_ptr, bytestream2_get_byte(gb), count);
  192. }
  193. line_ptr += count;
  194. }
  195. }
  196. return 0;
  197. }
  198. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  199. {
  200. const uint8_t *frame_end = frame + width * height;
  201. uint8_t *line_ptr;
  202. int count, i, v, lines, segments;
  203. lines = bytestream2_get_le16(gb);
  204. if (lines > height)
  205. return -1;
  206. while (lines--) {
  207. if (bytestream2_get_bytes_left(gb) < 2)
  208. return -1;
  209. segments = bytestream2_get_le16u(gb);
  210. while ((segments & 0xC000) == 0xC000) {
  211. unsigned delta = -((int16_t)segments * width);
  212. if (frame_end - frame <= delta)
  213. return -1;
  214. frame += delta;
  215. segments = bytestream2_get_le16(gb);
  216. }
  217. if (segments & 0x8000) {
  218. frame[width - 1] = segments & 0xFF;
  219. segments = bytestream2_get_le16(gb);
  220. }
  221. line_ptr = frame;
  222. frame += width;
  223. while (segments--) {
  224. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  225. return -1;
  226. line_ptr += bytestream2_get_byte(gb);
  227. count = (int8_t)bytestream2_get_byte(gb);
  228. if (count >= 0) {
  229. if (frame - line_ptr < count * 2)
  230. return -1;
  231. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  232. return -1;
  233. line_ptr += count * 2;
  234. } else {
  235. count = -count;
  236. if (frame - line_ptr < count * 2)
  237. return -1;
  238. v = bytestream2_get_le16(gb);
  239. for (i = 0; i < count; i++)
  240. bytestream_put_le16(&line_ptr, v);
  241. }
  242. }
  243. }
  244. return 0;
  245. }
  246. static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
  247. {
  248. return -1;
  249. }
  250. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  251. {
  252. memset(frame, 0, width * height);
  253. return 0;
  254. }
  255. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  256. static const chunk_decoder decoder[8] = {
  257. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  258. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  259. };
  260. static const char* chunk_name[8] = {
  261. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  262. };
  263. static int dfa_decode_frame(AVCodecContext *avctx,
  264. void *data, int *data_size,
  265. AVPacket *avpkt)
  266. {
  267. DfaContext *s = avctx->priv_data;
  268. GetByteContext gb;
  269. const uint8_t *buf = avpkt->data;
  270. uint32_t chunk_type, chunk_size;
  271. uint8_t *dst;
  272. int ret;
  273. int i, pal_elems;
  274. if (s->pic.data[0])
  275. avctx->release_buffer(avctx, &s->pic);
  276. if ((ret = avctx->get_buffer(avctx, &s->pic))) {
  277. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  278. return ret;
  279. }
  280. bytestream2_init(&gb, avpkt->data, avpkt->size);
  281. while (bytestream2_get_bytes_left(&gb) > 0) {
  282. bytestream2_skip(&gb, 4);
  283. chunk_size = bytestream2_get_le32(&gb);
  284. chunk_type = bytestream2_get_le32(&gb);
  285. if (!chunk_type)
  286. break;
  287. if (chunk_type == 1) {
  288. pal_elems = FFMIN(chunk_size / 3, 256);
  289. for (i = 0; i < pal_elems; i++) {
  290. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  291. s->pal[i] |= (s->pal[i] >> 6) & 0x333;
  292. }
  293. s->pic.palette_has_changed = 1;
  294. } else if (chunk_type <= 9) {
  295. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  296. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  297. chunk_name[chunk_type - 2]);
  298. return -1;
  299. }
  300. } else {
  301. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  302. chunk_type);
  303. }
  304. buf += chunk_size;
  305. }
  306. buf = s->frame_buf;
  307. dst = s->pic.data[0];
  308. for (i = 0; i < avctx->height; i++) {
  309. memcpy(dst, buf, avctx->width);
  310. dst += s->pic.linesize[0];
  311. buf += avctx->width;
  312. }
  313. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  314. *data_size = sizeof(AVFrame);
  315. *(AVFrame*)data = s->pic;
  316. return avpkt->size;
  317. }
  318. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  319. {
  320. DfaContext *s = avctx->priv_data;
  321. if (s->pic.data[0])
  322. avctx->release_buffer(avctx, &s->pic);
  323. av_freep(&s->frame_buf);
  324. return 0;
  325. }
  326. AVCodec ff_dfa_decoder = {
  327. .name = "dfa",
  328. .type = AVMEDIA_TYPE_VIDEO,
  329. .id = CODEC_ID_DFA,
  330. .priv_data_size = sizeof(DfaContext),
  331. .init = dfa_decode_init,
  332. .close = dfa_decode_end,
  333. .decode = dfa_decode_frame,
  334. .capabilities = CODEC_CAP_DR1,
  335. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  336. };