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.

624 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 %d (only %d 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 %d\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 %d instead of %d\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. avpriv_request_sample(avctx, "Not enough slice data available");
  260. }
  261. return 0;
  262. }
  263. av_always_inline
  264. static int dx2_decode_slice_5x5(GetBitContext *gb, AVFrame *frame,
  265. int line, int left, uint8_t lru[3][8],
  266. int is_565)
  267. {
  268. int x, y;
  269. int r, g, b;
  270. int width = frame->width;
  271. int stride = frame->linesize[0];
  272. uint8_t *dst = frame->data[0] + stride * line;
  273. for (y = 0; y < left && get_bits_left(gb) > 6 * width; y++) {
  274. for (x = 0; x < width; x++) {
  275. b = decode_sym_565(gb, lru[0], 5);
  276. g = decode_sym_565(gb, lru[1], is_565 ? 6 : 5);
  277. r = decode_sym_565(gb, lru[2], 5);
  278. dst[x * 3 + 0] = (r << 3) | (r >> 2);
  279. dst[x * 3 + 1] = is_565 ? (g << 2) | (g >> 4) : (g << 3) | (g >> 2);
  280. dst[x * 3 + 2] = (b << 3) | (b >> 2);
  281. }
  282. dst += stride;
  283. }
  284. return y;
  285. }
  286. static void setup_lru_555(uint8_t lru[3][8])
  287. {
  288. memcpy(lru[0], def_lru_555, 8 * sizeof(*def_lru));
  289. memcpy(lru[1], def_lru_555, 8 * sizeof(*def_lru));
  290. memcpy(lru[2], def_lru_555, 8 * sizeof(*def_lru));
  291. }
  292. static void setup_lru_565(uint8_t lru[3][8])
  293. {
  294. memcpy(lru[0], def_lru_555, 8 * sizeof(*def_lru));
  295. memcpy(lru[1], def_lru_565, 8 * sizeof(*def_lru));
  296. memcpy(lru[2], def_lru_555, 8 * sizeof(*def_lru));
  297. }
  298. static int dx2_decode_slice_555(GetBitContext *gb, AVFrame *frame,
  299. int line, int left, uint8_t lru[3][8])
  300. {
  301. return dx2_decode_slice_5x5(gb, frame, line, left, lru, 0);
  302. }
  303. static int dx2_decode_slice_565(GetBitContext *gb, AVFrame *frame,
  304. int line, int left, uint8_t lru[3][8])
  305. {
  306. return dx2_decode_slice_5x5(gb, frame, line, left, lru, 1);
  307. }
  308. static int dxtory_decode_v2_565(AVCodecContext *avctx, AVFrame *pic,
  309. const uint8_t *src, int src_size, int is_565)
  310. {
  311. enum AVPixelFormat fmt = AV_PIX_FMT_RGB24;
  312. if (is_565)
  313. return dxtory_decode_v2(avctx, pic, src, src_size,
  314. dx2_decode_slice_565,
  315. setup_lru_565,
  316. fmt);
  317. else
  318. return dxtory_decode_v2(avctx, pic, src, src_size,
  319. dx2_decode_slice_555,
  320. setup_lru_555,
  321. fmt);
  322. }
  323. static int dx2_decode_slice_rgb(GetBitContext *gb, AVFrame *frame,
  324. int line, int left, uint8_t lru[3][8])
  325. {
  326. int x, y;
  327. int width = frame->width;
  328. int stride = frame->linesize[0];
  329. uint8_t *dst = frame->data[0] + stride * line;
  330. for (y = 0; y < left && get_bits_left(gb) > 6 * width; y++) {
  331. for (x = 0; x < width; x++) {
  332. dst[x * 3 + 0] = decode_sym(gb, lru[0]);
  333. dst[x * 3 + 1] = decode_sym(gb, lru[1]);
  334. dst[x * 3 + 2] = decode_sym(gb, lru[2]);
  335. }
  336. dst += stride;
  337. }
  338. return y;
  339. }
  340. static void default_setup_lru(uint8_t lru[3][8])
  341. {
  342. int i;
  343. for (i = 0; i < 3; i++)
  344. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  345. }
  346. static int dxtory_decode_v2_rgb(AVCodecContext *avctx, AVFrame *pic,
  347. const uint8_t *src, int src_size)
  348. {
  349. return dxtory_decode_v2(avctx, pic, src, src_size,
  350. dx2_decode_slice_rgb,
  351. default_setup_lru,
  352. AV_PIX_FMT_BGR24);
  353. }
  354. static int dx2_decode_slice_410(GetBitContext *gb, AVFrame *frame,
  355. int line, int left,
  356. uint8_t lru[3][8])
  357. {
  358. int x, y, i, j;
  359. int width = frame->width;
  360. int ystride = frame->linesize[0];
  361. int ustride = frame->linesize[1];
  362. int vstride = frame->linesize[2];
  363. uint8_t *Y = frame->data[0] + ystride * line;
  364. uint8_t *U = frame->data[1] + (ustride >> 2) * line;
  365. uint8_t *V = frame->data[2] + (vstride >> 2) * line;
  366. for (y = 0; y < left - 3 && get_bits_left(gb) > 9 * width; y += 4) {
  367. for (x = 0; x < width; x += 4) {
  368. for (j = 0; j < 4; j++)
  369. for (i = 0; i < 4; i++)
  370. Y[x + i + j * ystride] = decode_sym(gb, lru[0]);
  371. U[x >> 2] = decode_sym(gb, lru[1]) ^ 0x80;
  372. V[x >> 2] = decode_sym(gb, lru[2]) ^ 0x80;
  373. }
  374. Y += ystride << 2;
  375. U += ustride;
  376. V += vstride;
  377. }
  378. return y;
  379. }
  380. static int dxtory_decode_v2_410(AVCodecContext *avctx, AVFrame *pic,
  381. const uint8_t *src, int src_size)
  382. {
  383. return dxtory_decode_v2(avctx, pic, src, src_size,
  384. dx2_decode_slice_410,
  385. default_setup_lru,
  386. AV_PIX_FMT_YUV410P);
  387. }
  388. static int dx2_decode_slice_420(GetBitContext *gb, AVFrame *frame,
  389. int line, int left,
  390. uint8_t lru[3][8])
  391. {
  392. int x, y;
  393. int width = frame->width;
  394. int ystride = frame->linesize[0];
  395. int ustride = frame->linesize[1];
  396. int vstride = frame->linesize[2];
  397. uint8_t *Y = frame->data[0] + ystride * line;
  398. uint8_t *U = frame->data[1] + (ustride >> 1) * line;
  399. uint8_t *V = frame->data[2] + (vstride >> 1) * line;
  400. for (y = 0; y < left - 1 && get_bits_left(gb) > 6 * width; y += 2) {
  401. for (x = 0; x < width; x += 2) {
  402. Y[x + 0 + 0 * ystride] = decode_sym(gb, lru[0]);
  403. Y[x + 1 + 0 * ystride] = decode_sym(gb, lru[0]);
  404. Y[x + 0 + 1 * ystride] = decode_sym(gb, lru[0]);
  405. Y[x + 1 + 1 * ystride] = decode_sym(gb, lru[0]);
  406. U[x >> 1] = decode_sym(gb, lru[1]) ^ 0x80;
  407. V[x >> 1] = decode_sym(gb, lru[2]) ^ 0x80;
  408. }
  409. Y += ystride << 1;
  410. U += ustride;
  411. V += vstride;
  412. }
  413. return y;
  414. }
  415. static int dxtory_decode_v2_420(AVCodecContext *avctx, AVFrame *pic,
  416. const uint8_t *src, int src_size)
  417. {
  418. return dxtory_decode_v2(avctx, pic, src, src_size,
  419. dx2_decode_slice_420,
  420. default_setup_lru,
  421. AV_PIX_FMT_YUV420P);
  422. }
  423. static int dx2_decode_slice_444(GetBitContext *gb, AVFrame *frame,
  424. int line, int left,
  425. uint8_t lru[3][8])
  426. {
  427. int x, y;
  428. int width = frame->width;
  429. int ystride = frame->linesize[0];
  430. int ustride = frame->linesize[1];
  431. int vstride = frame->linesize[2];
  432. uint8_t *Y = frame->data[0] + ystride * line;
  433. uint8_t *U = frame->data[1] + ustride * line;
  434. uint8_t *V = frame->data[2] + vstride * line;
  435. for (y = 0; y < left && get_bits_left(gb) > 6 * width; y++) {
  436. for (x = 0; x < width; x++) {
  437. Y[x] = decode_sym(gb, lru[0]);
  438. U[x] = decode_sym(gb, lru[1]) ^ 0x80;
  439. V[x] = decode_sym(gb, lru[2]) ^ 0x80;
  440. }
  441. Y += ystride;
  442. U += ustride;
  443. V += vstride;
  444. }
  445. return y;
  446. }
  447. static int dxtory_decode_v2_444(AVCodecContext *avctx, AVFrame *pic,
  448. const uint8_t *src, int src_size)
  449. {
  450. return dxtory_decode_v2(avctx, pic, src, src_size,
  451. dx2_decode_slice_444,
  452. default_setup_lru,
  453. AV_PIX_FMT_YUV444P);
  454. }
  455. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  456. AVPacket *avpkt)
  457. {
  458. AVFrame *pic = data;
  459. const uint8_t *src = avpkt->data;
  460. int ret;
  461. if (avpkt->size < 16) {
  462. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  463. return AVERROR_INVALIDDATA;
  464. }
  465. switch (AV_RB32(src)) {
  466. case 0x01000001:
  467. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  468. AV_PIX_FMT_BGR24, 3);
  469. break;
  470. case 0x01000009:
  471. ret = dxtory_decode_v2_rgb(avctx, pic, src + 16, avpkt->size - 16);
  472. break;
  473. case 0x02000001:
  474. ret = dxtory_decode_v1_420(avctx, pic, src + 16, avpkt->size - 16);
  475. break;
  476. case 0x02000009:
  477. ret = dxtory_decode_v2_420(avctx, pic, src + 16, avpkt->size - 16);
  478. break;
  479. case 0x03000001:
  480. ret = dxtory_decode_v1_410(avctx, pic, src + 16, avpkt->size - 16);
  481. break;
  482. case 0x03000009:
  483. ret = dxtory_decode_v2_410(avctx, pic, src + 16, avpkt->size - 16);
  484. break;
  485. case 0x04000001:
  486. ret = dxtory_decode_v1_444(avctx, pic, src + 16, avpkt->size - 16);
  487. break;
  488. case 0x04000009:
  489. ret = dxtory_decode_v2_444(avctx, pic, src + 16, avpkt->size - 16);
  490. break;
  491. case 0x17000001:
  492. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  493. AV_PIX_FMT_RGB565LE, 2);
  494. break;
  495. case 0x17000009:
  496. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 1);
  497. break;
  498. case 0x18000001:
  499. case 0x19000001:
  500. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  501. AV_PIX_FMT_RGB555LE, 2);
  502. break;
  503. case 0x18000009:
  504. case 0x19000009:
  505. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 0);
  506. break;
  507. default:
  508. avpriv_request_sample(avctx, "Frame header %"PRIX32, AV_RB32(src));
  509. return AVERROR_PATCHWELCOME;
  510. }
  511. if (ret)
  512. return ret;
  513. pic->pict_type = AV_PICTURE_TYPE_I;
  514. pic->key_frame = 1;
  515. *got_frame = 1;
  516. return avpkt->size;
  517. }
  518. AVCodec ff_dxtory_decoder = {
  519. .name = "dxtory",
  520. .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
  521. .type = AVMEDIA_TYPE_VIDEO,
  522. .id = AV_CODEC_ID_DXTORY,
  523. .decode = decode_frame,
  524. .capabilities = AV_CODEC_CAP_DR1,
  525. };