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.

411 lines
13KB

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