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.

389 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 "internal.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/mem.h"
  27. typedef struct DfaContext {
  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);
  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. av_memcpy_backptr(frame, offset, count);
  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 (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. v = bytestream2_get_le16(gb)*2;
  148. if (frame - frame_end < v)
  149. return AVERROR_INVALIDDATA;
  150. frame += v;
  151. } else {
  152. if (frame_end - frame < width + 3)
  153. return AVERROR_INVALIDDATA;
  154. frame[0] = frame[1] =
  155. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  156. frame += 2;
  157. frame[0] = frame[1] =
  158. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  159. frame += 2;
  160. }
  161. mask <<= 2;
  162. }
  163. return 0;
  164. }
  165. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  166. {
  167. uint8_t *line_ptr;
  168. int count, lines, segments;
  169. count = bytestream2_get_le16(gb);
  170. if (count >= height)
  171. return AVERROR_INVALIDDATA;
  172. frame += width * count;
  173. lines = bytestream2_get_le16(gb);
  174. if (count + lines > height)
  175. return AVERROR_INVALIDDATA;
  176. while (lines--) {
  177. if (bytestream2_get_bytes_left(gb) < 1)
  178. return AVERROR_INVALIDDATA;
  179. line_ptr = frame;
  180. frame += width;
  181. segments = bytestream2_get_byteu(gb);
  182. while (segments--) {
  183. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  184. return AVERROR_INVALIDDATA;
  185. line_ptr += bytestream2_get_byte(gb);
  186. count = (int8_t)bytestream2_get_byte(gb);
  187. if (count >= 0) {
  188. if (frame - line_ptr < count)
  189. return AVERROR_INVALIDDATA;
  190. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  191. return AVERROR_INVALIDDATA;
  192. } else {
  193. count = -count;
  194. if (frame - line_ptr < count)
  195. return AVERROR_INVALIDDATA;
  196. memset(line_ptr, bytestream2_get_byte(gb), count);
  197. }
  198. line_ptr += count;
  199. }
  200. }
  201. return 0;
  202. }
  203. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  204. {
  205. const uint8_t *frame_end = frame + width * height;
  206. uint8_t *line_ptr;
  207. int count, i, v, lines, segments;
  208. int y = 0;
  209. lines = bytestream2_get_le16(gb);
  210. if (lines > height)
  211. return AVERROR_INVALIDDATA;
  212. while (lines--) {
  213. if (bytestream2_get_bytes_left(gb) < 2)
  214. return AVERROR_INVALIDDATA;
  215. segments = bytestream2_get_le16u(gb);
  216. while ((segments & 0xC000) == 0xC000) {
  217. unsigned skip_lines = -(int16_t)segments;
  218. unsigned delta = -((int16_t)segments * width);
  219. if (frame_end - frame <= delta || y + lines + skip_lines > height)
  220. return AVERROR_INVALIDDATA;
  221. frame += delta;
  222. y += skip_lines;
  223. segments = bytestream2_get_le16(gb);
  224. }
  225. if (segments & 0x8000) {
  226. frame[width - 1] = segments & 0xFF;
  227. segments = bytestream2_get_le16(gb);
  228. }
  229. line_ptr = frame;
  230. if (frame_end - frame < width)
  231. return AVERROR_INVALIDDATA;
  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 *got_frame,
  276. AVPacket *avpkt)
  277. {
  278. AVFrame *frame = data;
  279. DfaContext *s = avctx->priv_data;
  280. GetByteContext gb;
  281. const uint8_t *buf = avpkt->data;
  282. uint32_t chunk_type, chunk_size;
  283. uint8_t *dst;
  284. int ret;
  285. int i, pal_elems;
  286. if ((ret = ff_get_buffer(avctx, frame, 0))) {
  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] |= (s->pal[i] >> 6) & 0x333;
  302. }
  303. frame->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 = frame->data[0];
  318. for (i = 0; i < avctx->height; i++) {
  319. memcpy(dst, buf, avctx->width);
  320. dst += frame->linesize[0];
  321. buf += avctx->width;
  322. }
  323. memcpy(frame->data[1], s->pal, sizeof(s->pal));
  324. *got_frame = 1;
  325. return avpkt->size;
  326. }
  327. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  328. {
  329. DfaContext *s = avctx->priv_data;
  330. av_freep(&s->frame_buf);
  331. return 0;
  332. }
  333. AVCodec ff_dfa_decoder = {
  334. .name = "dfa",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .id = AV_CODEC_ID_DFA,
  337. .priv_data_size = sizeof(DfaContext),
  338. .init = dfa_decode_init,
  339. .close = dfa_decode_end,
  340. .decode = dfa_decode_frame,
  341. .capabilities = CODEC_CAP_DR1,
  342. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  343. };