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.

628 lines
18KB

  1. /*
  2. * Dxtory decoder
  3. *
  4. * Copyright (c) 2011 Konstantin Shishkov
  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 "libavutil/common.h"
  24. #include "libavutil/intreadwrite.h"
  25. #define BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "get_bits.h"
  29. #include "internal.h"
  30. #include "unary.h"
  31. static int dxtory_decode_v1_rgb(AVCodecContext *avctx, AVFrame *pic,
  32. const uint8_t *src, int src_size,
  33. int id, int bpp)
  34. {
  35. int h;
  36. uint8_t *dst;
  37. int ret;
  38. if (src_size < avctx->width * avctx->height * (int64_t)bpp) {
  39. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  40. return AVERROR_INVALIDDATA;
  41. }
  42. avctx->pix_fmt = id;
  43. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  44. return ret;
  45. dst = pic->data[0];
  46. for (h = 0; h < avctx->height; h++) {
  47. memcpy(dst, src, avctx->width * bpp);
  48. src += avctx->width * bpp;
  49. dst += pic->linesize[0];
  50. }
  51. return 0;
  52. }
  53. static int dxtory_decode_v1_410(AVCodecContext *avctx, AVFrame *pic,
  54. const uint8_t *src, int src_size)
  55. {
  56. int h, w;
  57. uint8_t *Y1, *Y2, *Y3, *Y4, *U, *V;
  58. int ret;
  59. if (src_size < FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4) * 9LL / 8) {
  60. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  61. return AVERROR_INVALIDDATA;
  62. }
  63. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  64. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  65. return ret;
  66. Y1 = pic->data[0];
  67. Y2 = pic->data[0] + pic->linesize[0];
  68. Y3 = pic->data[0] + pic->linesize[0] * 2;
  69. Y4 = pic->data[0] + pic->linesize[0] * 3;
  70. U = pic->data[1];
  71. V = pic->data[2];
  72. for (h = 0; h < avctx->height; h += 4) {
  73. for (w = 0; w < avctx->width; w += 4) {
  74. AV_COPY32U(Y1 + w, src);
  75. AV_COPY32U(Y2 + w, src + 4);
  76. AV_COPY32U(Y3 + w, src + 8);
  77. AV_COPY32U(Y4 + w, src + 12);
  78. U[w >> 2] = src[16] + 0x80;
  79. V[w >> 2] = src[17] + 0x80;
  80. src += 18;
  81. }
  82. Y1 += pic->linesize[0] << 2;
  83. Y2 += pic->linesize[0] << 2;
  84. Y3 += pic->linesize[0] << 2;
  85. Y4 += pic->linesize[0] << 2;
  86. U += pic->linesize[1];
  87. V += pic->linesize[2];
  88. }
  89. return 0;
  90. }
  91. static int dxtory_decode_v1_420(AVCodecContext *avctx, AVFrame *pic,
  92. const uint8_t *src, int src_size)
  93. {
  94. int h, w;
  95. uint8_t *Y1, *Y2, *U, *V;
  96. int ret;
  97. if (src_size < FFALIGN(avctx->width, 2) * FFALIGN(avctx->height, 2) * 3LL / 2) {
  98. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  99. return AVERROR_INVALIDDATA;
  100. }
  101. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  102. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  103. return ret;
  104. Y1 = pic->data[0];
  105. Y2 = pic->data[0] + pic->linesize[0];
  106. U = pic->data[1];
  107. V = pic->data[2];
  108. for (h = 0; h < avctx->height; h += 2) {
  109. for (w = 0; w < avctx->width; w += 2) {
  110. AV_COPY16(Y1 + w, src);
  111. AV_COPY16(Y2 + w, src + 2);
  112. U[w >> 1] = src[4] + 0x80;
  113. V[w >> 1] = src[5] + 0x80;
  114. src += 6;
  115. }
  116. Y1 += pic->linesize[0] << 1;
  117. Y2 += pic->linesize[0] << 1;
  118. U += pic->linesize[1];
  119. V += pic->linesize[2];
  120. }
  121. return 0;
  122. }
  123. static int dxtory_decode_v1_444(AVCodecContext *avctx, AVFrame *pic,
  124. const uint8_t *src, int src_size)
  125. {
  126. int h, w;
  127. uint8_t *Y, *U, *V;
  128. int ret;
  129. if (src_size < avctx->width * avctx->height * 3LL) {
  130. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  131. return AVERROR_INVALIDDATA;
  132. }
  133. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  134. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  135. return ret;
  136. Y = pic->data[0];
  137. U = pic->data[1];
  138. V = pic->data[2];
  139. for (h = 0; h < avctx->height; h++) {
  140. for (w = 0; w < avctx->width; w++) {
  141. Y[w] = *src++;
  142. U[w] = *src++ ^ 0x80;
  143. V[w] = *src++ ^ 0x80;
  144. }
  145. Y += pic->linesize[0];
  146. U += pic->linesize[1];
  147. V += pic->linesize[2];
  148. }
  149. return 0;
  150. }
  151. static const uint8_t def_lru[8] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xFF };
  152. static const uint8_t def_lru_555[8] = { 0x00, 0x08, 0x10, 0x18, 0x1F };
  153. static const uint8_t def_lru_565[8] = { 0x00, 0x08, 0x10, 0x20, 0x30, 0x3F };
  154. static inline uint8_t decode_sym(GetBitContext *gb, uint8_t lru[8])
  155. {
  156. uint8_t c, val;
  157. c = get_unary(gb, 0, 8);
  158. if (!c) {
  159. val = get_bits(gb, 8);
  160. memmove(lru + 1, lru, sizeof(*lru) * (8 - 1));
  161. } else {
  162. val = lru[c - 1];
  163. memmove(lru + 1, lru, sizeof(*lru) * (c - 1));
  164. }
  165. lru[0] = val;
  166. return val;
  167. }
  168. static int check_slice_size(AVCodecContext *avctx,
  169. const uint8_t *src, int src_size,
  170. int slice_size, int off)
  171. {
  172. int cur_slice_size;
  173. if (slice_size > src_size - off) {
  174. av_log(avctx, AV_LOG_ERROR,
  175. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  176. slice_size, src_size - off);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. if (slice_size <= 16) {
  180. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n",
  181. slice_size);
  182. return AVERROR_INVALIDDATA;
  183. }
  184. cur_slice_size = AV_RL32(src + off);
  185. if (cur_slice_size != slice_size - 16) {
  186. av_log(avctx, AV_LOG_ERROR,
  187. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  188. cur_slice_size, slice_size - 16);
  189. }
  190. return 0;
  191. }
  192. static int load_buffer(AVCodecContext *avctx,
  193. const uint8_t *src, int src_size,
  194. GetByteContext *gb,
  195. int *nslices, int *off)
  196. {
  197. bytestream2_init(gb, src, src_size);
  198. *nslices = bytestream2_get_le16(gb);
  199. *off = FFALIGN(*nslices * 4 + 2, 16);
  200. if (src_size < *off) {
  201. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  202. return AVERROR_INVALIDDATA;
  203. }
  204. if (!*nslices) {
  205. avpriv_request_sample(avctx, "%d slices for %dx%d", *nslices,
  206. avctx->width, avctx->height);
  207. return AVERROR_PATCHWELCOME;
  208. }
  209. return 0;
  210. }
  211. static inline uint8_t decode_sym_565(GetBitContext *gb, uint8_t lru[8],
  212. int bits)
  213. {
  214. uint8_t c, val;
  215. c = get_unary(gb, 0, bits);
  216. if (!c) {
  217. val = get_bits(gb, bits);
  218. memmove(lru + 1, lru, sizeof(*lru) * (6 - 1));
  219. } else {
  220. val = lru[c - 1];
  221. memmove(lru + 1, lru, sizeof(*lru) * (c - 1));
  222. }
  223. lru[0] = val;
  224. return val;
  225. }
  226. typedef int (*decode_slice_func)(GetBitContext *gb, AVFrame *frame,
  227. int line, int height, uint8_t lru[3][8]);
  228. typedef void (*setup_lru_func)(uint8_t lru[3][8]);
  229. static int dxtory_decode_v2(AVCodecContext *avctx, AVFrame *pic,
  230. const uint8_t *src, int src_size,
  231. decode_slice_func decode_slice,
  232. setup_lru_func setup_lru,
  233. enum AVPixelFormat fmt)
  234. {
  235. GetByteContext gb;
  236. GetBitContext gb2;
  237. int nslices, slice, line = 0;
  238. uint32_t off, slice_size;
  239. uint8_t lru[3][8];
  240. int ret;
  241. ret = load_buffer(avctx, src, src_size, &gb, &nslices, &off);
  242. if (ret < 0)
  243. return ret;
  244. avctx->pix_fmt = fmt;
  245. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  246. return ret;
  247. for (slice = 0; slice < nslices; slice++) {
  248. slice_size = bytestream2_get_le32(&gb);
  249. setup_lru(lru);
  250. ret = check_slice_size(avctx, src, src_size, slice_size, off);
  251. if (ret < 0)
  252. return ret;
  253. if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
  254. return ret;
  255. line += decode_slice(&gb2, pic, line, avctx->height - line, lru);
  256. off += slice_size;
  257. }
  258. if (avctx->height - line) {
  259. av_log(avctx, AV_LOG_VERBOSE,
  260. "Not enough slice data available, "
  261. "cropping the frame by %d pixels\n",
  262. avctx->height - line);
  263. avctx->height = line;
  264. }
  265. return 0;
  266. }
  267. av_always_inline
  268. static int dx2_decode_slice_5x5(GetBitContext *gb, AVFrame *frame,
  269. int line, int left, uint8_t lru[3][8],
  270. int is_565)
  271. {
  272. int x, y;
  273. int r, g, b;
  274. int width = frame->width;
  275. int stride = frame->linesize[0];
  276. uint8_t *dst = frame->data[0] + stride * line;
  277. for (y = 0; y < left && get_bits_left(gb) > 16; y++) {
  278. for (x = 0; x < width; x++) {
  279. b = decode_sym_565(gb, lru[0], 5);
  280. g = decode_sym_565(gb, lru[1], is_565 ? 6 : 5);
  281. r = decode_sym_565(gb, lru[2], 5);
  282. dst[x * 3 + 0] = (r << 3) | (r >> 2);
  283. dst[x * 3 + 1] = is_565 ? (g << 2) | (g >> 4) : (g << 3) | (g >> 2);
  284. dst[x * 3 + 2] = (b << 3) | (b >> 2);
  285. }
  286. dst += stride;
  287. }
  288. return y;
  289. }
  290. static void setup_lru_555(uint8_t lru[3][8])
  291. {
  292. memcpy(lru[0], def_lru_555, 8 * sizeof(*def_lru));
  293. memcpy(lru[1], def_lru_555, 8 * sizeof(*def_lru));
  294. memcpy(lru[2], def_lru_555, 8 * sizeof(*def_lru));
  295. }
  296. static void setup_lru_565(uint8_t lru[3][8])
  297. {
  298. memcpy(lru[0], def_lru_555, 8 * sizeof(*def_lru));
  299. memcpy(lru[1], def_lru_565, 8 * sizeof(*def_lru));
  300. memcpy(lru[2], def_lru_555, 8 * sizeof(*def_lru));
  301. }
  302. static int dx2_decode_slice_555(GetBitContext *gb, AVFrame *frame,
  303. int line, int left, uint8_t lru[3][8])
  304. {
  305. return dx2_decode_slice_5x5(gb, frame, line, left, lru, 0);
  306. }
  307. static int dx2_decode_slice_565(GetBitContext *gb, AVFrame *frame,
  308. int line, int left, uint8_t lru[3][8])
  309. {
  310. return dx2_decode_slice_5x5(gb, frame, line, left, lru, 1);
  311. }
  312. static int dxtory_decode_v2_565(AVCodecContext *avctx, AVFrame *pic,
  313. const uint8_t *src, int src_size, int is_565)
  314. {
  315. enum AVPixelFormat fmt = AV_PIX_FMT_RGB24;
  316. if (is_565)
  317. return dxtory_decode_v2(avctx, pic, src, src_size,
  318. dx2_decode_slice_565,
  319. setup_lru_565,
  320. fmt);
  321. else
  322. return dxtory_decode_v2(avctx, pic, src, src_size,
  323. dx2_decode_slice_555,
  324. setup_lru_555,
  325. fmt);
  326. }
  327. static int dx2_decode_slice_rgb(GetBitContext *gb, AVFrame *frame,
  328. int line, int left, uint8_t lru[3][8])
  329. {
  330. int x, y;
  331. int width = frame->width;
  332. int stride = frame->linesize[0];
  333. uint8_t *dst = frame->data[0] + stride * line;
  334. for (y = 0; y < left && get_bits_left(gb) > 16; y++) {
  335. for (x = 0; x < width; x++) {
  336. dst[x * 3 + 0] = decode_sym(gb, lru[0]);
  337. dst[x * 3 + 1] = decode_sym(gb, lru[1]);
  338. dst[x * 3 + 2] = decode_sym(gb, lru[2]);
  339. }
  340. dst += stride;
  341. }
  342. return y;
  343. }
  344. static void default_setup_lru(uint8_t lru[3][8])
  345. {
  346. int i;
  347. for (i = 0; i < 3; i++)
  348. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  349. }
  350. static int dxtory_decode_v2_rgb(AVCodecContext *avctx, AVFrame *pic,
  351. const uint8_t *src, int src_size)
  352. {
  353. return dxtory_decode_v2(avctx, pic, src, src_size,
  354. dx2_decode_slice_rgb,
  355. default_setup_lru,
  356. AV_PIX_FMT_BGR24);
  357. }
  358. static int dx2_decode_slice_410(GetBitContext *gb, AVFrame *frame,
  359. int line, int left,
  360. uint8_t lru[3][8])
  361. {
  362. int x, y, i, j;
  363. int width = frame->width;
  364. int ystride = frame->linesize[0];
  365. int ustride = frame->linesize[1];
  366. int vstride = frame->linesize[2];
  367. uint8_t *Y = frame->data[0] + ystride * line;
  368. uint8_t *U = frame->data[1] + (ustride >> 2) * line;
  369. uint8_t *V = frame->data[2] + (vstride >> 2) * line;
  370. for (y = 0; y < left - 3 && get_bits_left(gb) > 16; y += 4) {
  371. for (x = 0; x < width; x += 4) {
  372. for (j = 0; j < 4; j++)
  373. for (i = 0; i < 4; i++)
  374. Y[x + i + j * ystride] = decode_sym(gb, lru[0]);
  375. U[x >> 2] = decode_sym(gb, lru[1]) ^ 0x80;
  376. V[x >> 2] = decode_sym(gb, lru[2]) ^ 0x80;
  377. }
  378. Y += ystride << 2;
  379. U += ustride;
  380. V += vstride;
  381. }
  382. return y;
  383. }
  384. static int dxtory_decode_v2_410(AVCodecContext *avctx, AVFrame *pic,
  385. const uint8_t *src, int src_size)
  386. {
  387. return dxtory_decode_v2(avctx, pic, src, src_size,
  388. dx2_decode_slice_410,
  389. default_setup_lru,
  390. AV_PIX_FMT_YUV410P);
  391. }
  392. static int dx2_decode_slice_420(GetBitContext *gb, AVFrame *frame,
  393. int line, int left,
  394. uint8_t lru[3][8])
  395. {
  396. int x, y;
  397. int width = frame->width;
  398. int ystride = frame->linesize[0];
  399. int ustride = frame->linesize[1];
  400. int vstride = frame->linesize[2];
  401. uint8_t *Y = frame->data[0] + ystride * line;
  402. uint8_t *U = frame->data[1] + (ustride >> 1) * line;
  403. uint8_t *V = frame->data[2] + (vstride >> 1) * line;
  404. for (y = 0; y < left - 1 && get_bits_left(gb) > 16; y += 2) {
  405. for (x = 0; x < width; x += 2) {
  406. Y[x + 0 + 0 * ystride] = decode_sym(gb, lru[0]);
  407. Y[x + 1 + 0 * ystride] = decode_sym(gb, lru[0]);
  408. Y[x + 0 + 1 * ystride] = decode_sym(gb, lru[0]);
  409. Y[x + 1 + 1 * ystride] = decode_sym(gb, lru[0]);
  410. U[x >> 1] = decode_sym(gb, lru[1]) ^ 0x80;
  411. V[x >> 1] = decode_sym(gb, lru[2]) ^ 0x80;
  412. }
  413. Y += ystride << 1;
  414. U += ustride;
  415. V += vstride;
  416. }
  417. return y;
  418. }
  419. static int dxtory_decode_v2_420(AVCodecContext *avctx, AVFrame *pic,
  420. const uint8_t *src, int src_size)
  421. {
  422. return dxtory_decode_v2(avctx, pic, src, src_size,
  423. dx2_decode_slice_420,
  424. default_setup_lru,
  425. AV_PIX_FMT_YUV420P);
  426. }
  427. static int dx2_decode_slice_444(GetBitContext *gb, AVFrame *frame,
  428. int line, int left,
  429. uint8_t lru[3][8])
  430. {
  431. int x, y;
  432. int width = frame->width;
  433. int ystride = frame->linesize[0];
  434. int ustride = frame->linesize[1];
  435. int vstride = frame->linesize[2];
  436. uint8_t *Y = frame->data[0] + ystride * line;
  437. uint8_t *U = frame->data[1] + ustride * line;
  438. uint8_t *V = frame->data[2] + vstride * line;
  439. for (y = 0; y < left && get_bits_left(gb) > 16; y++) {
  440. for (x = 0; x < width; x++) {
  441. Y[x] = decode_sym(gb, lru[0]);
  442. U[x] = decode_sym(gb, lru[1]) ^ 0x80;
  443. V[x] = decode_sym(gb, lru[2]) ^ 0x80;
  444. }
  445. Y += ystride;
  446. U += ustride;
  447. V += vstride;
  448. }
  449. return y;
  450. }
  451. static int dxtory_decode_v2_444(AVCodecContext *avctx, AVFrame *pic,
  452. const uint8_t *src, int src_size)
  453. {
  454. return dxtory_decode_v2(avctx, pic, src, src_size,
  455. dx2_decode_slice_444,
  456. default_setup_lru,
  457. AV_PIX_FMT_YUV444P);
  458. }
  459. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  460. AVPacket *avpkt)
  461. {
  462. AVFrame *pic = data;
  463. const uint8_t *src = avpkt->data;
  464. int ret;
  465. if (avpkt->size < 16) {
  466. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  467. return AVERROR_INVALIDDATA;
  468. }
  469. switch (AV_RB32(src)) {
  470. case 0x01000001:
  471. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  472. AV_PIX_FMT_BGR24, 3);
  473. break;
  474. case 0x01000009:
  475. ret = dxtory_decode_v2_rgb(avctx, pic, src + 16, avpkt->size - 16);
  476. break;
  477. case 0x02000001:
  478. ret = dxtory_decode_v1_420(avctx, pic, src + 16, avpkt->size - 16);
  479. break;
  480. case 0x02000009:
  481. ret = dxtory_decode_v2_420(avctx, pic, src + 16, avpkt->size - 16);
  482. break;
  483. case 0x03000001:
  484. ret = dxtory_decode_v1_410(avctx, pic, src + 16, avpkt->size - 16);
  485. break;
  486. case 0x03000009:
  487. ret = dxtory_decode_v2_410(avctx, pic, src + 16, avpkt->size - 16);
  488. break;
  489. case 0x04000001:
  490. ret = dxtory_decode_v1_444(avctx, pic, src + 16, avpkt->size - 16);
  491. break;
  492. case 0x04000009:
  493. ret = dxtory_decode_v2_444(avctx, pic, src + 16, avpkt->size - 16);
  494. break;
  495. case 0x17000001:
  496. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  497. AV_PIX_FMT_RGB565LE, 2);
  498. break;
  499. case 0x17000009:
  500. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 1);
  501. break;
  502. case 0x18000001:
  503. case 0x19000001:
  504. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  505. AV_PIX_FMT_RGB555LE, 2);
  506. break;
  507. case 0x18000009:
  508. case 0x19000009:
  509. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 0);
  510. break;
  511. default:
  512. avpriv_request_sample(avctx, "Frame header %"PRIX32, AV_RB32(src));
  513. return AVERROR_PATCHWELCOME;
  514. }
  515. if (ret)
  516. return ret;
  517. pic->pict_type = AV_PICTURE_TYPE_I;
  518. pic->key_frame = 1;
  519. *got_frame = 1;
  520. return avpkt->size;
  521. }
  522. AVCodec ff_dxtory_decoder = {
  523. .name = "dxtory",
  524. .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
  525. .type = AVMEDIA_TYPE_VIDEO,
  526. .id = AV_CODEC_ID_DXTORY,
  527. .decode = decode_frame,
  528. .capabilities = AV_CODEC_CAP_DR1,
  529. };