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.

401 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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/mem.h"
  28. typedef struct DfaContext {
  29. AVFrame pic;
  30. uint32_t pal[256];
  31. uint8_t *frame_buf;
  32. } DfaContext;
  33. static av_cold int dfa_decode_init(AVCodecContext *avctx)
  34. {
  35. DfaContext *s = avctx->priv_data;
  36. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  37. if (!avctx->width || !avctx->height)
  38. return AVERROR_INVALIDDATA;
  39. av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
  40. s->frame_buf = av_mallocz(avctx->width * avctx->height);
  41. if (!s->frame_buf)
  42. return AVERROR(ENOMEM);
  43. return 0;
  44. }
  45. static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
  46. {
  47. const int size = width * height;
  48. if (bytestream2_get_buffer(gb, frame, size) != size)
  49. return AVERROR_INVALIDDATA;
  50. return 0;
  51. }
  52. static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  53. {
  54. const uint8_t *frame_start = frame;
  55. const uint8_t *frame_end = frame + width * height;
  56. int mask = 0x10000, bitbuf = 0;
  57. int v, count, segments;
  58. unsigned offset;
  59. segments = bytestream2_get_le32(gb);
  60. offset = bytestream2_get_le32(gb);
  61. if (segments == 0 && offset == frame_end - frame)
  62. return 0; // skip frame
  63. if (frame_end - frame <= offset)
  64. return AVERROR_INVALIDDATA;
  65. frame += offset;
  66. while (segments--) {
  67. if (bytestream2_get_bytes_left(gb) < 2)
  68. return AVERROR_INVALIDDATA;
  69. if (mask == 0x10000) {
  70. bitbuf = bytestream2_get_le16u(gb);
  71. mask = 1;
  72. }
  73. if (frame_end - frame < 2)
  74. return AVERROR_INVALIDDATA;
  75. if (bitbuf & mask) {
  76. v = bytestream2_get_le16(gb);
  77. offset = (v & 0x1FFF) << 1;
  78. count = ((v >> 13) + 2) << 1;
  79. if (frame - frame_start < offset || frame_end - frame < count)
  80. return AVERROR_INVALIDDATA;
  81. av_memcpy_backptr(frame, offset, count);
  82. frame += count;
  83. } else {
  84. *frame++ = bytestream2_get_byte(gb);
  85. *frame++ = bytestream2_get_byte(gb);
  86. }
  87. mask <<= 1;
  88. }
  89. return 0;
  90. }
  91. static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  92. {
  93. const uint8_t *frame_start = frame;
  94. const uint8_t *frame_end = frame + width * height;
  95. int mask = 0x10000, bitbuf = 0;
  96. int v, offset, count, segments;
  97. segments = bytestream2_get_le16(gb);
  98. while (segments--) {
  99. if (bytestream2_get_bytes_left(gb) < 2)
  100. return AVERROR_INVALIDDATA;
  101. if (mask == 0x10000) {
  102. bitbuf = bytestream2_get_le16u(gb);
  103. mask = 1;
  104. }
  105. if (frame_end - frame < 2)
  106. return AVERROR_INVALIDDATA;
  107. if (bitbuf & mask) {
  108. v = bytestream2_get_le16(gb);
  109. offset = (v & 0x1FFF) << 1;
  110. count = ((v >> 13) + 2) << 1;
  111. if (frame - frame_start < offset || frame_end - frame < count)
  112. return AVERROR_INVALIDDATA;
  113. av_memcpy_backptr(frame, offset, count);
  114. frame += count;
  115. } else if (bitbuf & (mask << 1)) {
  116. frame += bytestream2_get_le16(gb);
  117. } else {
  118. *frame++ = bytestream2_get_byte(gb);
  119. *frame++ = bytestream2_get_byte(gb);
  120. }
  121. mask <<= 2;
  122. }
  123. return 0;
  124. }
  125. static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
  126. {
  127. const uint8_t *frame_start = frame;
  128. const uint8_t *frame_end = frame + width * height;
  129. int mask = 0x10000, bitbuf = 0;
  130. int i, v, offset, count, segments;
  131. segments = bytestream2_get_le16(gb);
  132. while (segments--) {
  133. if (bytestream2_get_bytes_left(gb) < 2)
  134. return AVERROR_INVALIDDATA;
  135. if (mask == 0x10000) {
  136. bitbuf = bytestream2_get_le16u(gb);
  137. mask = 1;
  138. }
  139. if (bitbuf & mask) {
  140. v = bytestream2_get_le16(gb);
  141. offset = (v & 0x1FFF) << 2;
  142. count = ((v >> 13) + 2) << 1;
  143. if (frame - frame_start < offset || frame_end - frame < count*2 + width)
  144. return AVERROR_INVALIDDATA;
  145. for (i = 0; i < count; i++) {
  146. frame[0] = frame[1] =
  147. frame[width] = frame[width + 1] = frame[-offset];
  148. frame += 2;
  149. }
  150. } else if (bitbuf & (mask << 1)) {
  151. v = bytestream2_get_le16(gb)*2;
  152. if (frame - frame_end < v)
  153. return AVERROR_INVALIDDATA;
  154. frame += v;
  155. } else {
  156. if (frame_end - frame < width + 3)
  157. return AVERROR_INVALIDDATA;
  158. frame[0] = frame[1] =
  159. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  160. frame += 2;
  161. frame[0] = frame[1] =
  162. frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
  163. frame += 2;
  164. }
  165. mask <<= 2;
  166. }
  167. return 0;
  168. }
  169. static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  170. {
  171. uint8_t *line_ptr;
  172. int count, lines, segments;
  173. count = bytestream2_get_le16(gb);
  174. if (count >= height)
  175. return AVERROR_INVALIDDATA;
  176. frame += width * count;
  177. lines = bytestream2_get_le16(gb);
  178. if (count + lines > height)
  179. return AVERROR_INVALIDDATA;
  180. while (lines--) {
  181. if (bytestream2_get_bytes_left(gb) < 1)
  182. return AVERROR_INVALIDDATA;
  183. line_ptr = frame;
  184. frame += width;
  185. segments = bytestream2_get_byteu(gb);
  186. while (segments--) {
  187. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  188. return AVERROR_INVALIDDATA;
  189. line_ptr += bytestream2_get_byte(gb);
  190. count = (int8_t)bytestream2_get_byte(gb);
  191. if (count >= 0) {
  192. if (frame - line_ptr < count)
  193. return AVERROR_INVALIDDATA;
  194. if (bytestream2_get_buffer(gb, line_ptr, count) != count)
  195. return AVERROR_INVALIDDATA;
  196. } else {
  197. count = -count;
  198. if (frame - line_ptr < count)
  199. return AVERROR_INVALIDDATA;
  200. memset(line_ptr, bytestream2_get_byte(gb), count);
  201. }
  202. line_ptr += count;
  203. }
  204. }
  205. return 0;
  206. }
  207. static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  208. {
  209. const uint8_t *frame_end = frame + width * height;
  210. uint8_t *line_ptr;
  211. int count, i, v, lines, segments;
  212. int y = 0;
  213. lines = bytestream2_get_le16(gb);
  214. if (lines > height)
  215. return AVERROR_INVALIDDATA;
  216. while (lines--) {
  217. if (bytestream2_get_bytes_left(gb) < 2)
  218. return AVERROR_INVALIDDATA;
  219. segments = bytestream2_get_le16u(gb);
  220. while ((segments & 0xC000) == 0xC000) {
  221. unsigned skip_lines = -(int16_t)segments;
  222. unsigned delta = -((int16_t)segments * width);
  223. if (frame_end - frame <= delta || y + lines + skip_lines > height)
  224. return AVERROR_INVALIDDATA;
  225. frame += delta;
  226. y += skip_lines;
  227. segments = bytestream2_get_le16(gb);
  228. }
  229. if (frame_end <= frame)
  230. return -1;
  231. if (segments & 0x8000) {
  232. frame[width - 1] = segments & 0xFF;
  233. segments = bytestream2_get_le16(gb);
  234. }
  235. line_ptr = frame;
  236. frame += width;
  237. y++;
  238. while (segments--) {
  239. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  240. return AVERROR_INVALIDDATA;
  241. line_ptr += bytestream2_get_byte(gb);
  242. count = (int8_t)bytestream2_get_byte(gb);
  243. if (count >= 0) {
  244. if (frame - line_ptr < count * 2)
  245. return AVERROR_INVALIDDATA;
  246. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  247. return AVERROR_INVALIDDATA;
  248. line_ptr += count * 2;
  249. } else {
  250. count = -count;
  251. if (frame - line_ptr < count * 2)
  252. return AVERROR_INVALIDDATA;
  253. v = bytestream2_get_le16(gb);
  254. for (i = 0; i < count; i++)
  255. bytestream_put_le16(&line_ptr, v);
  256. }
  257. }
  258. }
  259. return 0;
  260. }
  261. static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
  262. {
  263. return AVERROR_PATCHWELCOME;
  264. }
  265. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  266. {
  267. memset(frame, 0, width * height);
  268. return 0;
  269. }
  270. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  271. static const chunk_decoder decoder[8] = {
  272. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  273. decode_unk6, decode_dsw1, decode_blck, decode_dds1,
  274. };
  275. static const char* chunk_name[8] = {
  276. "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
  277. };
  278. static int dfa_decode_frame(AVCodecContext *avctx,
  279. void *data, int *got_frame,
  280. AVPacket *avpkt)
  281. {
  282. DfaContext *s = avctx->priv_data;
  283. GetByteContext gb;
  284. const uint8_t *buf = avpkt->data;
  285. uint32_t chunk_type, chunk_size;
  286. uint8_t *dst;
  287. int ret;
  288. int i, pal_elems;
  289. if (s->pic.data[0])
  290. avctx->release_buffer(avctx, &s->pic);
  291. if ((ret = ff_get_buffer(avctx, &s->pic))) {
  292. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  293. return ret;
  294. }
  295. bytestream2_init(&gb, avpkt->data, avpkt->size);
  296. while (bytestream2_get_bytes_left(&gb) > 0) {
  297. bytestream2_skip(&gb, 4);
  298. chunk_size = bytestream2_get_le32(&gb);
  299. chunk_type = bytestream2_get_le32(&gb);
  300. if (!chunk_type)
  301. break;
  302. if (chunk_type == 1) {
  303. pal_elems = FFMIN(chunk_size / 3, 256);
  304. for (i = 0; i < pal_elems; i++) {
  305. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  306. s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
  307. }
  308. s->pic.palette_has_changed = 1;
  309. } else if (chunk_type <= 9) {
  310. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  311. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  312. chunk_name[chunk_type - 2]);
  313. return AVERROR_INVALIDDATA;
  314. }
  315. } else {
  316. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  317. chunk_type);
  318. }
  319. buf += chunk_size;
  320. }
  321. buf = s->frame_buf;
  322. dst = s->pic.data[0];
  323. for (i = 0; i < avctx->height; i++) {
  324. memcpy(dst, buf, avctx->width);
  325. dst += s->pic.linesize[0];
  326. buf += avctx->width;
  327. }
  328. memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
  329. *got_frame = 1;
  330. *(AVFrame*)data = s->pic;
  331. return avpkt->size;
  332. }
  333. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  334. {
  335. DfaContext *s = avctx->priv_data;
  336. if (s->pic.data[0])
  337. avctx->release_buffer(avctx, &s->pic);
  338. av_freep(&s->frame_buf);
  339. return 0;
  340. }
  341. AVCodec ff_dfa_decoder = {
  342. .name = "dfa",
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .id = AV_CODEC_ID_DFA,
  345. .priv_data_size = sizeof(DfaContext),
  346. .init = dfa_decode_init,
  347. .close = dfa_decode_end,
  348. .decode = dfa_decode_frame,
  349. .capabilities = CODEC_CAP_DR1,
  350. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  351. };