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.

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