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.

627 lines
18KB

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