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.

420 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 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. 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. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  36. if (!avctx->width || !avctx->height)
  37. return AVERROR_INVALIDDATA;
  38. av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
  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 (segments == 0 && offset == frame_end - frame)
  61. return 0; // skip frame
  62. if (frame_end - frame <= offset)
  63. return AVERROR_INVALIDDATA;
  64. frame += offset;
  65. while (segments--) {
  66. if (bytestream2_get_bytes_left(gb) < 2)
  67. return AVERROR_INVALIDDATA;
  68. if (mask == 0x10000) {
  69. bitbuf = bytestream2_get_le16u(gb);
  70. mask = 1;
  71. }
  72. if (frame_end - frame < 2)
  73. return AVERROR_INVALIDDATA;
  74. if (bitbuf & mask) {
  75. v = bytestream2_get_le16(gb);
  76. offset = (v & 0x1FFF) << 1;
  77. count = ((v >> 13) + 2) << 1;
  78. if (frame - frame_start < offset || frame_end - frame < count)
  79. return AVERROR_INVALIDDATA;
  80. av_memcpy_backptr(frame, offset, count);
  81. frame += count;
  82. } else {
  83. *frame++ = bytestream2_get_byte(gb);
  84. *frame++ = bytestream2_get_byte(gb);
  85. }
  86. mask <<= 1;
  87. }
  88. return 0;
  89. }
  90. static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
  91. {
  92. const uint8_t *frame_start = frame;
  93. const uint8_t *frame_end = frame + width * height;
  94. int mask = 0x10000, bitbuf = 0;
  95. int v, offset, count, segments;
  96. segments = bytestream2_get_le16(gb);
  97. while (segments--) {
  98. if (bytestream2_get_bytes_left(gb) < 2)
  99. return AVERROR_INVALIDDATA;
  100. if (mask == 0x10000) {
  101. bitbuf = bytestream2_get_le16u(gb);
  102. mask = 1;
  103. }
  104. if (frame_end - frame < 2)
  105. return AVERROR_INVALIDDATA;
  106. if (bitbuf & mask) {
  107. v = bytestream2_get_le16(gb);
  108. offset = (v & 0x1FFF) << 1;
  109. count = ((v >> 13) + 2) << 1;
  110. if (frame - frame_start < offset || frame_end - frame < count)
  111. return AVERROR_INVALIDDATA;
  112. av_memcpy_backptr(frame, offset, count);
  113. frame += count;
  114. } else if (bitbuf & (mask << 1)) {
  115. frame += bytestream2_get_le16(gb);
  116. } else {
  117. *frame++ = bytestream2_get_byte(gb);
  118. *frame++ = bytestream2_get_byte(gb);
  119. }
  120. mask <<= 2;
  121. }
  122. return 0;
  123. }
  124. static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
  125. {
  126. const uint8_t *frame_start = frame;
  127. const uint8_t *frame_end = frame + width * height;
  128. int mask = 0x10000, bitbuf = 0;
  129. int i, v, offset, count, segments;
  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 (frame_end - frame < width + 3)
  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 (frame_end <= frame)
  229. return AVERROR_INVALIDDATA;
  230. if (segments & 0x8000) {
  231. frame[width - 1] = segments & 0xFF;
  232. segments = bytestream2_get_le16(gb);
  233. }
  234. line_ptr = frame;
  235. if (frame_end - frame < width)
  236. return AVERROR_INVALIDDATA;
  237. frame += width;
  238. y++;
  239. while (segments--) {
  240. if (frame - line_ptr <= bytestream2_peek_byte(gb))
  241. return AVERROR_INVALIDDATA;
  242. line_ptr += bytestream2_get_byte(gb);
  243. count = (int8_t)bytestream2_get_byte(gb);
  244. if (count >= 0) {
  245. if (frame - line_ptr < count * 2)
  246. return AVERROR_INVALIDDATA;
  247. if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
  248. return AVERROR_INVALIDDATA;
  249. line_ptr += count * 2;
  250. } else {
  251. count = -count;
  252. if (frame - line_ptr < count * 2)
  253. return AVERROR_INVALIDDATA;
  254. v = bytestream2_get_le16(gb);
  255. for (i = 0; i < count; i++)
  256. bytestream_put_le16(&line_ptr, v);
  257. }
  258. }
  259. }
  260. return 0;
  261. }
  262. static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
  263. {
  264. const uint8_t *frame_end = frame + width * height;
  265. uint32_t segments = bytestream2_get_le32(gb);
  266. int skip, copy;
  267. while (segments--) {
  268. if (bytestream2_get_bytes_left(gb) < 2)
  269. return AVERROR_INVALIDDATA;
  270. copy = bytestream2_get_byteu(gb) * 2;
  271. skip = bytestream2_get_byteu(gb) * 2;
  272. if (frame_end - frame < copy + skip ||
  273. bytestream2_get_bytes_left(gb) < copy)
  274. return AVERROR_INVALIDDATA;
  275. frame += skip;
  276. bytestream2_get_buffer(gb, frame, copy);
  277. frame += copy;
  278. }
  279. return 0;
  280. }
  281. static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
  282. {
  283. memset(frame, 0, width * height);
  284. return 0;
  285. }
  286. typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
  287. static const chunk_decoder decoder[8] = {
  288. decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
  289. decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
  290. };
  291. static const char* chunk_name[8] = {
  292. "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
  293. };
  294. static int dfa_decode_frame(AVCodecContext *avctx,
  295. void *data, int *got_frame,
  296. AVPacket *avpkt)
  297. {
  298. AVFrame *frame = data;
  299. DfaContext *s = avctx->priv_data;
  300. GetByteContext gb;
  301. const uint8_t *buf = avpkt->data;
  302. uint32_t chunk_type, chunk_size;
  303. uint8_t *dst;
  304. int ret;
  305. int i, pal_elems;
  306. int version = avctx->extradata_size==2 ? AV_RL16(avctx->extradata) : 0;
  307. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  308. return ret;
  309. bytestream2_init(&gb, avpkt->data, avpkt->size);
  310. while (bytestream2_get_bytes_left(&gb) > 0) {
  311. bytestream2_skip(&gb, 4);
  312. chunk_size = bytestream2_get_le32(&gb);
  313. chunk_type = bytestream2_get_le32(&gb);
  314. if (!chunk_type)
  315. break;
  316. if (chunk_type == 1) {
  317. pal_elems = FFMIN(chunk_size / 3, 256);
  318. for (i = 0; i < pal_elems; i++) {
  319. s->pal[i] = bytestream2_get_be24(&gb) << 2;
  320. s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
  321. }
  322. frame->palette_has_changed = 1;
  323. } else if (chunk_type <= 9) {
  324. if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
  325. av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
  326. chunk_name[chunk_type - 2]);
  327. return AVERROR_INVALIDDATA;
  328. }
  329. } else {
  330. av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\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. if(version == 0x100) {
  339. int j;
  340. for(j = 0; j < avctx->width; j++) {
  341. dst[j] = buf[ (i&3)*(avctx->width /4) + (j/4) +
  342. ((j&3)*(avctx->height/4) + (i/4))*avctx->width];
  343. }
  344. } else {
  345. memcpy(dst, buf, avctx->width);
  346. buf += avctx->width;
  347. }
  348. dst += frame->linesize[0];
  349. }
  350. memcpy(frame->data[1], s->pal, sizeof(s->pal));
  351. *got_frame = 1;
  352. return avpkt->size;
  353. }
  354. static av_cold int dfa_decode_end(AVCodecContext *avctx)
  355. {
  356. DfaContext *s = avctx->priv_data;
  357. av_freep(&s->frame_buf);
  358. return 0;
  359. }
  360. AVCodec ff_dfa_decoder = {
  361. .name = "dfa",
  362. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  363. .type = AVMEDIA_TYPE_VIDEO,
  364. .id = AV_CODEC_ID_DFA,
  365. .priv_data_size = sizeof(DfaContext),
  366. .init = dfa_decode_init,
  367. .close = dfa_decode_end,
  368. .decode = dfa_decode_frame,
  369. .capabilities = CODEC_CAP_DR1,
  370. };