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.

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