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.

752 lines
23KB

  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. #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 * (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 < avctx->width * avctx->height * 9L / 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_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 * 3L / 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 * 3L) {
  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 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;
  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) {
  365. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  366. avctx->width, avctx->height);
  367. return AVERROR_PATCHWELCOME;
  368. }
  369. if ((avctx->width & 3) || (avctx->height & 3)) {
  370. avpriv_request_sample(avctx, "Frame dimensions %dx%d",
  371. avctx->width, avctx->height);
  372. }
  373. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  374. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  375. return ret;
  376. Y = pic->data[0];
  377. U = pic->data[1];
  378. V = pic->data[2];
  379. cur_y = 0;
  380. for (slice = 0; slice < nslices; slice++) {
  381. slice_size = bytestream2_get_le32(&gb);
  382. next_y = ((slice + 1) * avctx->height) / nslices;
  383. slice_height = (next_y & ~3) - (cur_y & ~3);
  384. if (slice_size > src_size - off) {
  385. av_log(avctx, AV_LOG_ERROR,
  386. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  387. slice_size, src_size - off);
  388. return AVERROR_INVALIDDATA;
  389. }
  390. if (slice_size <= 16) {
  391. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  392. return AVERROR_INVALIDDATA;
  393. }
  394. if (AV_RL32(src + off) != slice_size - 16) {
  395. av_log(avctx, AV_LOG_ERROR,
  396. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  397. AV_RL32(src + off), slice_size - 16);
  398. }
  399. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  400. dx2_decode_slice_410(&gb2, avctx->width, slice_height, Y, U, V,
  401. pic->linesize[0], pic->linesize[1],
  402. pic->linesize[2]);
  403. Y += pic->linesize[0] * slice_height;
  404. U += pic->linesize[1] * (slice_height >> 2);
  405. V += pic->linesize[2] * (slice_height >> 2);
  406. off += slice_size;
  407. cur_y = next_y;
  408. }
  409. return 0;
  410. }
  411. static int dx2_decode_slice_420(GetBitContext *gb, int width, int height,
  412. uint8_t *Y, uint8_t *U, uint8_t *V,
  413. int ystride, int ustride, int vstride)
  414. {
  415. int x, y, i;
  416. uint8_t lru[3][8];
  417. for (i = 0; i < 3; i++)
  418. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  419. for (y = 0; y < height; y+=2) {
  420. for (x = 0; x < width; x += 2) {
  421. Y[x + 0 + 0 * ystride] = decode_sym(gb, lru[0]);
  422. Y[x + 1 + 0 * ystride] = decode_sym(gb, lru[0]);
  423. Y[x + 0 + 1 * ystride] = decode_sym(gb, lru[0]);
  424. Y[x + 1 + 1 * ystride] = decode_sym(gb, lru[0]);
  425. U[x >> 1] = decode_sym(gb, lru[1]) ^ 0x80;
  426. V[x >> 1] = decode_sym(gb, lru[2]) ^ 0x80;
  427. }
  428. Y += ystride << 1;
  429. U += ustride;
  430. V += vstride;
  431. }
  432. return 0;
  433. }
  434. static int dxtory_decode_v2_420(AVCodecContext *avctx, AVFrame *pic,
  435. const uint8_t *src, int src_size)
  436. {
  437. GetByteContext gb;
  438. GetBitContext gb2;
  439. int nslices, slice, slice_height;
  440. int cur_y, next_y;
  441. uint32_t off, slice_size;
  442. uint8_t *Y, *U, *V;
  443. int ret;
  444. bytestream2_init(&gb, src, src_size);
  445. nslices = bytestream2_get_le16(&gb);
  446. off = FFALIGN(nslices * 4 + 2, 16);
  447. if (src_size < off) {
  448. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  449. return AVERROR_INVALIDDATA;
  450. }
  451. if (!nslices) {
  452. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  453. avctx->width, avctx->height);
  454. return AVERROR_PATCHWELCOME;
  455. }
  456. if ((avctx->width & 1) || (avctx->height & 1)) {
  457. avpriv_request_sample(avctx, "Frame dimensions %dx%d",
  458. avctx->width, avctx->height);
  459. }
  460. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  461. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  462. return ret;
  463. Y = pic->data[0];
  464. U = pic->data[1];
  465. V = pic->data[2];
  466. cur_y = 0;
  467. for (slice = 0; slice < nslices; slice++) {
  468. slice_size = bytestream2_get_le32(&gb);
  469. next_y = ((slice + 1) * avctx->height) / nslices;
  470. slice_height = (next_y & ~1) - (cur_y & ~1);
  471. if (slice_size > src_size - off) {
  472. av_log(avctx, AV_LOG_ERROR,
  473. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  474. slice_size, src_size - off);
  475. return AVERROR_INVALIDDATA;
  476. }
  477. if (slice_size <= 16) {
  478. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  479. return AVERROR_INVALIDDATA;
  480. }
  481. if (AV_RL32(src + off) != slice_size - 16) {
  482. av_log(avctx, AV_LOG_ERROR,
  483. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  484. AV_RL32(src + off), slice_size - 16);
  485. }
  486. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  487. dx2_decode_slice_420(&gb2, avctx->width, slice_height, Y, U, V,
  488. pic->linesize[0], pic->linesize[1],
  489. pic->linesize[2]);
  490. Y += pic->linesize[0] * slice_height;
  491. U += pic->linesize[1] * (slice_height >> 1);
  492. V += pic->linesize[2] * (slice_height >> 1);
  493. off += slice_size;
  494. cur_y = next_y;
  495. }
  496. return 0;
  497. }
  498. static int dx2_decode_slice_444(GetBitContext *gb, int width, int height,
  499. uint8_t *Y, uint8_t *U, uint8_t *V,
  500. int ystride, int ustride, int vstride)
  501. {
  502. int x, y, i;
  503. uint8_t lru[3][8];
  504. for (i = 0; i < 3; i++)
  505. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  506. for (y = 0; y < height; y++) {
  507. for (x = 0; x < width; x++) {
  508. Y[x] = decode_sym(gb, lru[0]);
  509. U[x] = decode_sym(gb, lru[1]) ^ 0x80;
  510. V[x] = decode_sym(gb, lru[2]) ^ 0x80;
  511. }
  512. Y += ystride;
  513. U += ustride;
  514. V += vstride;
  515. }
  516. return 0;
  517. }
  518. static int dxtory_decode_v2_444(AVCodecContext *avctx, AVFrame *pic,
  519. const uint8_t *src, int src_size)
  520. {
  521. GetByteContext gb;
  522. GetBitContext gb2;
  523. int nslices, slice, slice_height;
  524. uint32_t off, slice_size;
  525. uint8_t *Y, *U, *V;
  526. int ret;
  527. bytestream2_init(&gb, src, src_size);
  528. nslices = bytestream2_get_le16(&gb);
  529. off = FFALIGN(nslices * 4 + 2, 16);
  530. if (src_size < off) {
  531. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  532. return AVERROR_INVALIDDATA;
  533. }
  534. if (!nslices || avctx->height % nslices) {
  535. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  536. avctx->width, avctx->height);
  537. return AVERROR_PATCHWELCOME;
  538. }
  539. slice_height = avctx->height / nslices;
  540. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  541. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  542. return ret;
  543. Y = pic->data[0];
  544. U = pic->data[1];
  545. V = pic->data[2];
  546. for (slice = 0; slice < nslices; slice++) {
  547. slice_size = bytestream2_get_le32(&gb);
  548. if (slice_size > src_size - off) {
  549. av_log(avctx, AV_LOG_ERROR,
  550. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  551. slice_size, src_size - off);
  552. return AVERROR_INVALIDDATA;
  553. }
  554. if (slice_size <= 16) {
  555. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  556. return AVERROR_INVALIDDATA;
  557. }
  558. if (AV_RL32(src + off) != slice_size - 16) {
  559. av_log(avctx, AV_LOG_ERROR,
  560. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  561. AV_RL32(src + off), slice_size - 16);
  562. }
  563. init_get_bits(&gb2, src + off + 16, (slice_size - 16) * 8);
  564. dx2_decode_slice_444(&gb2, avctx->width, slice_height, Y, U, V,
  565. pic->linesize[0], pic->linesize[1],
  566. pic->linesize[2]);
  567. Y += pic->linesize[0] * slice_height;
  568. U += pic->linesize[1] * slice_height;
  569. V += pic->linesize[2] * slice_height;
  570. off += slice_size;
  571. }
  572. return 0;
  573. }
  574. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  575. AVPacket *avpkt)
  576. {
  577. AVFrame *pic = data;
  578. const uint8_t *src = avpkt->data;
  579. int ret;
  580. if (avpkt->size < 16) {
  581. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  582. return AVERROR_INVALIDDATA;
  583. }
  584. switch (AV_RB32(src)) {
  585. case 0x01000001:
  586. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  587. AV_PIX_FMT_BGR24, 3);
  588. break;
  589. case 0x01000009:
  590. ret = dxtory_decode_v2_rgb(avctx, pic, src + 16, avpkt->size - 16);
  591. break;
  592. case 0x02000001:
  593. ret = dxtory_decode_v1_420(avctx, pic, src + 16, avpkt->size - 16);
  594. break;
  595. case 0x02000009:
  596. ret = dxtory_decode_v2_420(avctx, pic, src + 16, avpkt->size - 16);
  597. break;
  598. case 0x03000001:
  599. ret = dxtory_decode_v1_410(avctx, pic, src + 16, avpkt->size - 16);
  600. break;
  601. case 0x03000009:
  602. ret = dxtory_decode_v2_410(avctx, pic, src + 16, avpkt->size - 16);
  603. break;
  604. case 0x04000001:
  605. ret = dxtory_decode_v1_444(avctx, pic, src + 16, avpkt->size - 16);
  606. break;
  607. case 0x04000009:
  608. ret = dxtory_decode_v2_444(avctx, pic, src + 16, avpkt->size - 16);
  609. break;
  610. case 0x17000001:
  611. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  612. AV_PIX_FMT_RGB565LE, 2);
  613. break;
  614. case 0x17000009:
  615. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 1);
  616. break;
  617. case 0x18000001:
  618. case 0x19000001:
  619. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  620. AV_PIX_FMT_RGB555LE, 2);
  621. break;
  622. case 0x18000009:
  623. case 0x19000009:
  624. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 0);
  625. break;
  626. default:
  627. avpriv_request_sample(avctx, "Frame header %"PRIX32, AV_RB32(src));
  628. return AVERROR_PATCHWELCOME;
  629. }
  630. if (ret)
  631. return ret;
  632. pic->pict_type = AV_PICTURE_TYPE_I;
  633. pic->key_frame = 1;
  634. *got_frame = 1;
  635. return avpkt->size;
  636. }
  637. AVCodec ff_dxtory_decoder = {
  638. .name = "dxtory",
  639. .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
  640. .type = AVMEDIA_TYPE_VIDEO,
  641. .id = AV_CODEC_ID_DXTORY,
  642. .decode = decode_frame,
  643. .capabilities = CODEC_CAP_DR1,
  644. };