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.

406 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_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  258. {
  259. const uint8_t *frame_end = frame + width * height;
  260. int segments = bytestream2_get_le32(gb);
  261. int skip, copy;
  262. while (segments--) {
  263. if (bytestream2_get_bytes_left(gb) < 2)
  264. return AVERROR_INVALIDDATA;
  265. copy = bytestream2_get_byteu(gb) * 2;
  266. skip = bytestream2_get_byteu(gb) * 2;
  267. if (frame_end - frame < copy + skip ||
  268. bytestream2_get_bytes_left(gb) < copy)
  269. return AVERROR_INVALIDDATA;
  270. frame += skip;
  271. bytestream2_get_buffer(gb, frame, copy);
  272. frame += copy;
  273. }
  274. return 0;
  275. }
  276. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  277. {
  278. memset(frame, 0, width * height);
  279. return 0;
  280. }
  281. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  282. static const chunk_decoder decoder[8] = {
  283. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  284. decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
  285. };
  286. static const char* chunk_name[8] = {
  287. "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
  288. };
  289. static int dfa_decode_frame(AVCodecContext *avctx,
  290. void *data, int *got_frame,
  291. AVPacket *avpkt)
  292. {
  293. AVFrame *frame = data;
  294. DfaContext *s = avctx->priv_data;
  295. GetByteContext gb;
  296. const uint8_t *buf = avpkt->data;
  297. uint32_t chunk_type, chunk_size;
  298. uint8_t *dst;
  299. int ret;
  300. int i, pal_elems;
  301. if ((ret = ff_get_buffer(avctx, frame, 0))) {
  302. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  303. return ret;
  304. }
  305. bytestream2_init(&gb, avpkt->data, avpkt->size);
  306. while (bytestream2_get_bytes_left(&gb) > 0) {
  307. bytestream2_skip(&gb, 4);
  308. chunk_size = bytestream2_get_le32(&gb);
  309. chunk_type = bytestream2_get_le32(&gb);
  310. if (!chunk_type)
  311. break;
  312. if (chunk_type == 1) {
  313. pal_elems = FFMIN(chunk_size / 3, 256);
  314. for (i = 0; i < pal_elems; i++) {
  315. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  316. s->pal[i] |= (s->pal[i] >> 6) & 0x333;
  317. }
  318. frame->palette_has_changed = 1;
  319. } else if (chunk_type <= 9) {
  320. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  321. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  322. chunk_name[chunk_type - 2]);
  323. return AVERROR_INVALIDDATA;
  324. }
  325. } else {
  326. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
  327. chunk_type);
  328. }
  329. buf += chunk_size;
  330. }
  331. buf = s->frame_buf;
  332. dst = frame->data[0];
  333. for (i = 0; i < avctx->height; i++) {
  334. memcpy(dst, buf, avctx->width);
  335. dst += frame->linesize[0];
  336. buf += avctx->width;
  337. }
  338. memcpy(frame->data[1], s->pal, sizeof(s->pal));
  339. *got_frame = 1;
  340. return avpkt->size;
  341. }
  342. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  343. {
  344. DfaContext *s = avctx->priv_data;
  345. av_freep(&s->frame_buf);
  346. return 0;
  347. }
  348. AVCodec ff_dfa_decoder = {
  349. .name = "dfa",
  350. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. .id = AV_CODEC_ID_DFA,
  353. .priv_data_size = sizeof(DfaContext),
  354. .init = dfa_decode_init,
  355. .close = dfa_decode_end,
  356. .decode = dfa_decode_frame,
  357. .capabilities = CODEC_CAP_DR1,
  358. };