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.

387 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. frame += width;
  231. y++;
  232. while (segments--) {
  233. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  234. return AVERROR_INVALIDDATA;
  235. line_ptr += bytestream2_get_byte(gb);
  236. count = (int8_t)bytestream2_get_byte(gb);
  237. if (count >= 0) {
  238. if (frame - line_ptr < count * 2)
  239. return AVERROR_INVALIDDATA;
  240. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  241. return AVERROR_INVALIDDATA;
  242. line_ptr += count * 2;
  243. } else {
  244. count = -count;
  245. if (frame - line_ptr < count * 2)
  246. return AVERROR_INVALIDDATA;
  247. v = bytestream2_get_le16(gb);
  248. for (i = 0; i < count; i++)
  249. bytestream_put_le16(&line_ptr, v);
  250. }
  251. }
  252. }
  253. return 0;
  254. }
  255. static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
  256. {
  257. return AVERROR_PATCHWELCOME;
  258. }
  259. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  260. {
  261. memset(frame, 0, width * height);
  262. return 0;
  263. }
  264. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  265. static const chunk_decoder decoder[8] = {
  266. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  267. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  268. };
  269. static const char* chunk_name[8] = {
  270. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  271. };
  272. static int dfa_decode_frame(AVCodecContext *avctx,
  273. void *data, int *got_frame,
  274. AVPacket *avpkt)
  275. {
  276. AVFrame *frame = data;
  277. DfaContext *s = avctx->priv_data;
  278. GetByteContext gb;
  279. const uint8_t *buf = avpkt->data;
  280. uint32_t chunk_type, chunk_size;
  281. uint8_t *dst;
  282. int ret;
  283. int i, pal_elems;
  284. if ((ret = ff_get_buffer(avctx, frame, 0))) {
  285. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  286. return ret;
  287. }
  288. bytestream2_init(&gb, avpkt->data, avpkt->size);
  289. while (bytestream2_get_bytes_left(&gb) > 0) {
  290. bytestream2_skip(&gb, 4);
  291. chunk_size = bytestream2_get_le32(&gb);
  292. chunk_type = bytestream2_get_le32(&gb);
  293. if (!chunk_type)
  294. break;
  295. if (chunk_type == 1) {
  296. pal_elems = FFMIN(chunk_size / 3, 256);
  297. for (i = 0; i < pal_elems; i++) {
  298. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  299. s->pal[i] |= (s->pal[i] >> 6) & 0x333;
  300. }
  301. frame->palette_has_changed = 1;
  302. } else if (chunk_type <= 9) {
  303. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  304. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  305. chunk_name[chunk_type - 2]);
  306. return AVERROR_INVALIDDATA;
  307. }
  308. } else {
  309. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  310. chunk_type);
  311. }
  312. buf += chunk_size;
  313. }
  314. buf = s->frame_buf;
  315. dst = frame->data[0];
  316. for (i = 0; i < avctx->height; i++) {
  317. memcpy(dst, buf, avctx->width);
  318. dst += frame->linesize[0];
  319. buf += avctx->width;
  320. }
  321. memcpy(frame->data[1], s->pal, sizeof(s->pal));
  322. *got_frame = 1;
  323. return avpkt->size;
  324. }
  325. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  326. {
  327. DfaContext *s = avctx->priv_data;
  328. av_freep(&s->frame_buf);
  329. return 0;
  330. }
  331. AVCodec ff_dfa_decoder = {
  332. .name = "dfa",
  333. .type = AVMEDIA_TYPE_VIDEO,
  334. .id = AV_CODEC_ID_DFA,
  335. .priv_data_size = sizeof(DfaContext),
  336. .init = dfa_decode_init,
  337. .close = dfa_decode_end,
  338. .decode = dfa_decode_frame,
  339. .capabilities = CODEC_CAP_DR1,
  340. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  341. };