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.

756 lines
23KB

  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. #define BITSTREAM_READER_LE
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "get_bits.h"
  27. #include "internal.h"
  28. #include "unary.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/intreadwrite.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. const uint8_t def_lru[8] = { 0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xFF };
  152. const uint8_t def_lru_555[8] = { 0x00, 0x08, 0x10, 0x18, 0x1F };
  153. 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 inline uint8_t decode_sym_565(GetBitContext *gb, uint8_t lru[8],
  169. int bits)
  170. {
  171. uint8_t c, val;
  172. c = get_unary(gb, 0, bits);
  173. if (!c) {
  174. val = get_bits(gb, bits);
  175. memmove(lru + 1, lru, sizeof(*lru) * (6 - 1));
  176. } else {
  177. val = lru[c - 1];
  178. memmove(lru + 1, lru, sizeof(*lru) * (c - 1));
  179. }
  180. lru[0] = val;
  181. return val;
  182. }
  183. static int dx2_decode_slice_565(GetBitContext *gb, int width, int height,
  184. uint8_t *dst, int stride, int is_565)
  185. {
  186. int x, y;
  187. int r, g, b;
  188. uint8_t lru[3][8];
  189. memcpy(lru[0], def_lru_555, 8 * sizeof(*def_lru));
  190. memcpy(lru[1], is_565 ? def_lru_565 : def_lru_555, 8 * sizeof(*def_lru));
  191. memcpy(lru[2], def_lru_555, 8 * sizeof(*def_lru));
  192. for (y = 0; y < height; y++) {
  193. for (x = 0; x < width; x++) {
  194. b = decode_sym_565(gb, lru[0], 5);
  195. g = decode_sym_565(gb, lru[1], is_565 ? 6 : 5);
  196. r = decode_sym_565(gb, lru[2], 5);
  197. dst[x * 3 + 0] = (r << 3) | (r >> 2);
  198. dst[x * 3 + 1] = is_565 ? (g << 2) | (g >> 4) : (g << 3) | (g >> 2);
  199. dst[x * 3 + 2] = (b << 3) | (b >> 2);
  200. }
  201. dst += stride;
  202. }
  203. return 0;
  204. }
  205. static int dxtory_decode_v2_565(AVCodecContext *avctx, AVFrame *pic,
  206. const uint8_t *src, int src_size, int is_565)
  207. {
  208. GetByteContext gb;
  209. GetBitContext gb2;
  210. int nslices, slice, slice_height;
  211. uint32_t off, slice_size;
  212. uint8_t *dst;
  213. int ret;
  214. bytestream2_init(&gb, src, src_size);
  215. nslices = bytestream2_get_le16(&gb);
  216. off = FFALIGN(nslices * 4 + 2, 16);
  217. if (src_size < off) {
  218. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  219. return AVERROR_INVALIDDATA;
  220. }
  221. if (!nslices || avctx->height % nslices) {
  222. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  223. avctx->width, avctx->height);
  224. return AVERROR_PATCHWELCOME;
  225. }
  226. slice_height = avctx->height / nslices;
  227. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  228. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  229. return ret;
  230. dst = pic->data[0];
  231. for (slice = 0; slice < nslices; slice++) {
  232. slice_size = bytestream2_get_le32(&gb);
  233. if (slice_size > src_size - off) {
  234. av_log(avctx, AV_LOG_ERROR,
  235. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  236. slice_size, src_size - off);
  237. return AVERROR_INVALIDDATA;
  238. }
  239. if (slice_size <= 16) {
  240. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  241. return AVERROR_INVALIDDATA;
  242. }
  243. if (AV_RL32(src + off) != slice_size - 16) {
  244. av_log(avctx, AV_LOG_ERROR,
  245. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  246. AV_RL32(src + off), slice_size - 16);
  247. }
  248. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  249. dx2_decode_slice_565(&gb2, avctx->width, slice_height, dst,
  250. pic->linesize[0], is_565);
  251. dst += pic->linesize[0] * slice_height;
  252. off += slice_size;
  253. }
  254. return 0;
  255. }
  256. static int dx2_decode_slice_rgb(GetBitContext *gb, int width, int height,
  257. uint8_t *dst, int stride)
  258. {
  259. int x, y, i;
  260. uint8_t lru[3][8];
  261. for (i = 0; i < 3; i++)
  262. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  263. for (y = 0; y < height; y++) {
  264. for (x = 0; x < width; x++) {
  265. dst[x * 3 + 0] = decode_sym(gb, lru[0]);
  266. dst[x * 3 + 1] = decode_sym(gb, lru[1]);
  267. dst[x * 3 + 2] = decode_sym(gb, lru[2]);
  268. }
  269. dst += stride;
  270. }
  271. return 0;
  272. }
  273. static int dxtory_decode_v2_rgb(AVCodecContext *avctx, AVFrame *pic,
  274. const uint8_t *src, int src_size)
  275. {
  276. GetByteContext gb;
  277. GetBitContext gb2;
  278. int nslices, slice, slice_height;
  279. uint32_t off, slice_size;
  280. uint8_t *dst;
  281. int ret;
  282. bytestream2_init(&gb, src, src_size);
  283. nslices = bytestream2_get_le16(&gb);
  284. off = FFALIGN(nslices * 4 + 2, 16);
  285. if (src_size < off) {
  286. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  287. return AVERROR_INVALIDDATA;
  288. }
  289. if (!nslices || avctx->height % nslices) {
  290. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  291. avctx->width, avctx->height);
  292. return AVERROR_PATCHWELCOME;
  293. }
  294. slice_height = avctx->height / nslices;
  295. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  296. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  297. return ret;
  298. dst = pic->data[0];
  299. for (slice = 0; slice < nslices; slice++) {
  300. slice_size = bytestream2_get_le32(&gb);
  301. if (slice_size > src_size - off) {
  302. av_log(avctx, AV_LOG_ERROR,
  303. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  304. slice_size, src_size - off);
  305. return AVERROR_INVALIDDATA;
  306. }
  307. if (slice_size <= 16) {
  308. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n",
  309. slice_size);
  310. return AVERROR_INVALIDDATA;
  311. }
  312. if (AV_RL32(src + off) != slice_size - 16) {
  313. av_log(avctx, AV_LOG_ERROR,
  314. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  315. AV_RL32(src + off), slice_size - 16);
  316. }
  317. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  318. dx2_decode_slice_rgb(&gb2, avctx->width, slice_height, dst,
  319. pic->linesize[0]);
  320. dst += pic->linesize[0] * slice_height;
  321. off += slice_size;
  322. }
  323. return 0;
  324. }
  325. static int dx2_decode_slice_410(GetBitContext *gb, int width, int height,
  326. uint8_t *Y, uint8_t *U, uint8_t *V,
  327. int ystride, int ustride, int vstride)
  328. {
  329. int x, y, i, j;
  330. uint8_t lru[3][8];
  331. for (i = 0; i < 3; i++)
  332. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  333. for (y = 0; y < height; y += 4) {
  334. for (x = 0; x < width; x += 4) {
  335. for (j = 0; j < 4; j++)
  336. for (i = 0; i < 4; i++)
  337. Y[x + i + j * ystride] = decode_sym(gb, lru[0]);
  338. U[x >> 2] = decode_sym(gb, lru[1]) ^ 0x80;
  339. V[x >> 2] = decode_sym(gb, lru[2]) ^ 0x80;
  340. }
  341. Y += ystride << 2;
  342. U += ustride;
  343. V += vstride;
  344. }
  345. return 0;
  346. }
  347. static int dxtory_decode_v2_410(AVCodecContext *avctx, AVFrame *pic,
  348. const uint8_t *src, int src_size)
  349. {
  350. GetByteContext gb;
  351. GetBitContext gb2;
  352. int nslices, slice, slice_height, ref_slice_height;
  353. int cur_y, next_y;
  354. uint32_t off, slice_size;
  355. uint8_t *Y, *U, *V;
  356. int ret;
  357. bytestream2_init(&gb, src, src_size);
  358. nslices = bytestream2_get_le16(&gb);
  359. off = FFALIGN(nslices * 4 + 2, 16);
  360. if (src_size < off) {
  361. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  362. return AVERROR_INVALIDDATA;
  363. }
  364. if (!nslices || avctx->height % nslices) {
  365. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  366. avctx->width, avctx->height);
  367. return AVERROR_PATCHWELCOME;
  368. }
  369. ref_slice_height = avctx->height / nslices;
  370. if ((avctx->width & 3) || (avctx->height & 3)) {
  371. avpriv_request_sample(avctx, "Frame dimensions %dx%d",
  372. avctx->width, avctx->height);
  373. }
  374. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  375. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  376. return ret;
  377. Y = pic->data[0];
  378. U = pic->data[1];
  379. V = pic->data[2];
  380. cur_y = 0;
  381. next_y = ref_slice_height;
  382. for (slice = 0; slice < nslices; slice++) {
  383. slice_size = bytestream2_get_le32(&gb);
  384. slice_height = (next_y & ~3) - (cur_y & ~3);
  385. if (slice_size > src_size - off) {
  386. av_log(avctx, AV_LOG_ERROR,
  387. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  388. slice_size, src_size - off);
  389. return AVERROR_INVALIDDATA;
  390. }
  391. if (slice_size <= 16) {
  392. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  393. return AVERROR_INVALIDDATA;
  394. }
  395. if (AV_RL32(src + off) != slice_size - 16) {
  396. av_log(avctx, AV_LOG_ERROR,
  397. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  398. AV_RL32(src + off), slice_size - 16);
  399. }
  400. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  401. dx2_decode_slice_410(&gb2, avctx->width, slice_height, Y, U, V,
  402. pic->linesize[0], pic->linesize[1],
  403. pic->linesize[2]);
  404. Y += pic->linesize[0] * slice_height;
  405. U += pic->linesize[1] * (slice_height >> 2);
  406. V += pic->linesize[2] * (slice_height >> 2);
  407. off += slice_size;
  408. cur_y = next_y;
  409. next_y += ref_slice_height;
  410. }
  411. return 0;
  412. }
  413. static int dx2_decode_slice_420(GetBitContext *gb, int width, int height,
  414. uint8_t *Y, uint8_t *U, uint8_t *V,
  415. int ystride, int ustride, int vstride)
  416. {
  417. int x, y, i;
  418. uint8_t lru[3][8];
  419. for (i = 0; i < 3; i++)
  420. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  421. for (y = 0; y < height; y+=2) {
  422. for (x = 0; x < width; x += 2) {
  423. Y[x + 0 + 0 * ystride] = decode_sym(gb, lru[0]);
  424. Y[x + 1 + 0 * ystride] = decode_sym(gb, lru[0]);
  425. Y[x + 0 + 1 * ystride] = decode_sym(gb, lru[0]);
  426. Y[x + 1 + 1 * ystride] = decode_sym(gb, lru[0]);
  427. U[x >> 1] = decode_sym(gb, lru[1]) ^ 0x80;
  428. V[x >> 1] = decode_sym(gb, lru[2]) ^ 0x80;
  429. }
  430. Y += ystride << 1;
  431. U += ustride;
  432. V += vstride;
  433. }
  434. return 0;
  435. }
  436. static int dxtory_decode_v2_420(AVCodecContext *avctx, AVFrame *pic,
  437. const uint8_t *src, int src_size)
  438. {
  439. GetByteContext gb;
  440. GetBitContext gb2;
  441. int nslices, slice, slice_height, ref_slice_height;
  442. int cur_y, next_y;
  443. uint32_t off, slice_size;
  444. uint8_t *Y, *U, *V;
  445. int ret;
  446. bytestream2_init(&gb, src, src_size);
  447. nslices = bytestream2_get_le16(&gb);
  448. off = FFALIGN(nslices * 4 + 2, 16);
  449. if (src_size < off) {
  450. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  451. return AVERROR_INVALIDDATA;
  452. }
  453. if (!nslices || avctx->height % nslices) {
  454. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  455. avctx->width, avctx->height);
  456. return AVERROR_PATCHWELCOME;
  457. }
  458. ref_slice_height = avctx->height / nslices;
  459. if ((avctx->width & 1) || (avctx->height & 1)) {
  460. avpriv_request_sample(avctx, "Frame dimensions %dx%d",
  461. avctx->width, avctx->height);
  462. }
  463. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  464. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  465. return ret;
  466. Y = pic->data[0];
  467. U = pic->data[1];
  468. V = pic->data[2];
  469. cur_y = 0;
  470. next_y = ref_slice_height;
  471. for (slice = 0; slice < nslices; slice++) {
  472. slice_size = bytestream2_get_le32(&gb);
  473. slice_height = (next_y & ~1) - (cur_y & ~1);
  474. if (slice_size > src_size - off) {
  475. av_log(avctx, AV_LOG_ERROR,
  476. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  477. slice_size, src_size - off);
  478. return AVERROR_INVALIDDATA;
  479. }
  480. if (slice_size <= 16) {
  481. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  482. return AVERROR_INVALIDDATA;
  483. }
  484. if (AV_RL32(src + off) != slice_size - 16) {
  485. av_log(avctx, AV_LOG_ERROR,
  486. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  487. AV_RL32(src + off), slice_size - 16);
  488. }
  489. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  490. dx2_decode_slice_420(&gb2, avctx->width, slice_height, Y, U, V,
  491. pic->linesize[0], pic->linesize[1],
  492. pic->linesize[2]);
  493. Y += pic->linesize[0] * slice_height;
  494. U += pic->linesize[1] * (slice_height >> 1);
  495. V += pic->linesize[2] * (slice_height >> 1);
  496. off += slice_size;
  497. cur_y = next_y;
  498. next_y += ref_slice_height;
  499. }
  500. return 0;
  501. }
  502. static int dx2_decode_slice_444(GetBitContext *gb, int width, int height,
  503. uint8_t *Y, uint8_t *U, uint8_t *V,
  504. int ystride, int ustride, int vstride)
  505. {
  506. int x, y, i;
  507. uint8_t lru[3][8];
  508. for (i = 0; i < 3; i++)
  509. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  510. for (y = 0; y < height; y++) {
  511. for (x = 0; x < width; x++) {
  512. Y[x] = decode_sym(gb, lru[0]);
  513. U[x] = decode_sym(gb, lru[1]) ^ 0x80;
  514. V[x] = decode_sym(gb, lru[2]) ^ 0x80;
  515. }
  516. Y += ystride;
  517. U += ustride;
  518. V += vstride;
  519. }
  520. return 0;
  521. }
  522. static int dxtory_decode_v2_444(AVCodecContext *avctx, AVFrame *pic,
  523. const uint8_t *src, int src_size)
  524. {
  525. GetByteContext gb;
  526. GetBitContext gb2;
  527. int nslices, slice, slice_height;
  528. uint32_t off, slice_size;
  529. uint8_t *Y, *U, *V;
  530. int ret;
  531. bytestream2_init(&gb, src, src_size);
  532. nslices = bytestream2_get_le16(&gb);
  533. off = FFALIGN(nslices * 4 + 2, 16);
  534. if (src_size < off) {
  535. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  536. return AVERROR_INVALIDDATA;
  537. }
  538. if (!nslices || avctx->height % nslices) {
  539. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  540. avctx->width, avctx->height);
  541. return AVERROR_PATCHWELCOME;
  542. }
  543. slice_height = avctx->height / nslices;
  544. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  545. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  546. return ret;
  547. Y = pic->data[0];
  548. U = pic->data[1];
  549. V = pic->data[2];
  550. for (slice = 0; slice < nslices; slice++) {
  551. slice_size = bytestream2_get_le32(&gb);
  552. if (slice_size > src_size - off) {
  553. av_log(avctx, AV_LOG_ERROR,
  554. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  555. slice_size, src_size - off);
  556. return AVERROR_INVALIDDATA;
  557. }
  558. if (slice_size <= 16) {
  559. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  560. return AVERROR_INVALIDDATA;
  561. }
  562. if (AV_RL32(src + off) != slice_size - 16) {
  563. av_log(avctx, AV_LOG_ERROR,
  564. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  565. AV_RL32(src + off), slice_size - 16);
  566. }
  567. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  568. dx2_decode_slice_444(&gb2, avctx->width, slice_height, Y, U, V,
  569. pic->linesize[0], pic->linesize[1],
  570. pic->linesize[2]);
  571. Y += pic->linesize[0] * slice_height;
  572. U += pic->linesize[1] * slice_height;
  573. V += pic->linesize[2] * slice_height;
  574. off += slice_size;
  575. }
  576. return 0;
  577. }
  578. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  579. AVPacket *avpkt)
  580. {
  581. AVFrame *pic = data;
  582. const uint8_t *src = avpkt->data;
  583. int ret;
  584. if (avpkt->size < 16) {
  585. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  586. return AVERROR_INVALIDDATA;
  587. }
  588. switch (AV_RB32(src)) {
  589. case 0x01000001:
  590. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  591. AV_PIX_FMT_BGR24, 3);
  592. break;
  593. case 0x01000009:
  594. ret = dxtory_decode_v2_rgb(avctx, pic, src + 16, avpkt->size - 16);
  595. break;
  596. case 0x02000001:
  597. ret = dxtory_decode_v1_420(avctx, pic, src + 16, avpkt->size - 16);
  598. break;
  599. case 0x02000009:
  600. ret = dxtory_decode_v2_420(avctx, pic, src + 16, avpkt->size - 16);
  601. break;
  602. case 0x03000001:
  603. ret = dxtory_decode_v1_410(avctx, pic, src + 16, avpkt->size - 16);
  604. break;
  605. case 0x03000009:
  606. ret = dxtory_decode_v2_410(avctx, pic, src + 16, avpkt->size - 16);
  607. break;
  608. case 0x04000001:
  609. ret = dxtory_decode_v1_444(avctx, pic, src + 16, avpkt->size - 16);
  610. break;
  611. case 0x04000009:
  612. ret = dxtory_decode_v2_444(avctx, pic, src + 16, avpkt->size - 16);
  613. break;
  614. case 0x17000001:
  615. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  616. AV_PIX_FMT_RGB565LE, 2);
  617. break;
  618. case 0x17000009:
  619. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 1);
  620. break;
  621. case 0x18000001:
  622. case 0x19000001:
  623. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  624. AV_PIX_FMT_RGB555LE, 2);
  625. break;
  626. case 0x18000009:
  627. case 0x19000009:
  628. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 0);
  629. break;
  630. default:
  631. avpriv_request_sample(avctx, "Frame header %"PRIX32, AV_RB32(src));
  632. return AVERROR_PATCHWELCOME;
  633. }
  634. if (ret)
  635. return ret;
  636. pic->pict_type = AV_PICTURE_TYPE_I;
  637. pic->key_frame = 1;
  638. *got_frame = 1;
  639. return avpkt->size;
  640. }
  641. AVCodec ff_dxtory_decoder = {
  642. .name = "dxtory",
  643. .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
  644. .type = AVMEDIA_TYPE_VIDEO,
  645. .id = AV_CODEC_ID_DXTORY,
  646. .decode = decode_frame,
  647. .capabilities = CODEC_CAP_DR1,
  648. };