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.

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