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.

757 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 * 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 < avctx->width * avctx->height * 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 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. if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
  249. return ret;
  250. dx2_decode_slice_565(&gb2, avctx->width, slice_height, dst,
  251. pic->linesize[0], is_565);
  252. dst += pic->linesize[0] * slice_height;
  253. off += slice_size;
  254. }
  255. return 0;
  256. }
  257. static int dx2_decode_slice_rgb(GetBitContext *gb, int width, int height,
  258. uint8_t *dst, int stride)
  259. {
  260. int x, y, i;
  261. uint8_t lru[3][8];
  262. for (i = 0; i < 3; i++)
  263. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  264. for (y = 0; y < height; y++) {
  265. for (x = 0; x < width; x++) {
  266. dst[x * 3 + 0] = decode_sym(gb, lru[0]);
  267. dst[x * 3 + 1] = decode_sym(gb, lru[1]);
  268. dst[x * 3 + 2] = decode_sym(gb, lru[2]);
  269. }
  270. dst += stride;
  271. }
  272. return 0;
  273. }
  274. static int dxtory_decode_v2_rgb(AVCodecContext *avctx, AVFrame *pic,
  275. const uint8_t *src, int src_size)
  276. {
  277. GetByteContext gb;
  278. GetBitContext gb2;
  279. int nslices, slice, slice_height;
  280. uint32_t off, slice_size;
  281. uint8_t *dst;
  282. int ret;
  283. bytestream2_init(&gb, src, src_size);
  284. nslices = bytestream2_get_le16(&gb);
  285. off = FFALIGN(nslices * 4 + 2, 16);
  286. if (src_size < off) {
  287. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. if (!nslices || avctx->height % nslices) {
  291. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  292. avctx->width, avctx->height);
  293. return AVERROR_PATCHWELCOME;
  294. }
  295. slice_height = avctx->height / nslices;
  296. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  297. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  298. return ret;
  299. dst = pic->data[0];
  300. for (slice = 0; slice < nslices; slice++) {
  301. slice_size = bytestream2_get_le32(&gb);
  302. if (slice_size > src_size - off) {
  303. av_log(avctx, AV_LOG_ERROR,
  304. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  305. slice_size, src_size - off);
  306. return AVERROR_INVALIDDATA;
  307. }
  308. if (slice_size <= 16) {
  309. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n",
  310. slice_size);
  311. return AVERROR_INVALIDDATA;
  312. }
  313. if (AV_RL32(src + off) != slice_size - 16) {
  314. av_log(avctx, AV_LOG_ERROR,
  315. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  316. AV_RL32(src + off), slice_size - 16);
  317. }
  318. if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
  319. return ret;
  320. dx2_decode_slice_rgb(&gb2, avctx->width, slice_height, dst,
  321. pic->linesize[0]);
  322. dst += pic->linesize[0] * slice_height;
  323. off += slice_size;
  324. }
  325. return 0;
  326. }
  327. static int dx2_decode_slice_410(GetBitContext *gb, int width, int height,
  328. uint8_t *Y, uint8_t *U, uint8_t *V,
  329. int ystride, int ustride, int vstride)
  330. {
  331. int x, y, i, j;
  332. uint8_t lru[3][8];
  333. for (i = 0; i < 3; i++)
  334. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  335. for (y = 0; y < height; y += 4) {
  336. for (x = 0; x < width; x += 4) {
  337. for (j = 0; j < 4; j++)
  338. for (i = 0; i < 4; i++)
  339. Y[x + i + j * ystride] = decode_sym(gb, lru[0]);
  340. U[x >> 2] = decode_sym(gb, lru[1]) ^ 0x80;
  341. V[x >> 2] = decode_sym(gb, lru[2]) ^ 0x80;
  342. }
  343. Y += ystride << 2;
  344. U += ustride;
  345. V += vstride;
  346. }
  347. return 0;
  348. }
  349. static int dxtory_decode_v2_410(AVCodecContext *avctx, AVFrame *pic,
  350. const uint8_t *src, int src_size)
  351. {
  352. GetByteContext gb;
  353. GetBitContext gb2;
  354. int nslices, slice, slice_height;
  355. int cur_y, next_y;
  356. uint32_t off, slice_size;
  357. uint8_t *Y, *U, *V;
  358. int ret;
  359. bytestream2_init(&gb, src, src_size);
  360. nslices = bytestream2_get_le16(&gb);
  361. off = FFALIGN(nslices * 4 + 2, 16);
  362. if (src_size < off) {
  363. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  364. return AVERROR_INVALIDDATA;
  365. }
  366. if (!nslices) {
  367. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  368. avctx->width, avctx->height);
  369. return AVERROR_PATCHWELCOME;
  370. }
  371. if ((avctx->width & 3) || (avctx->height & 3)) {
  372. avpriv_request_sample(avctx, "Frame dimensions %dx%d",
  373. avctx->width, avctx->height);
  374. }
  375. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  376. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  377. return ret;
  378. Y = pic->data[0];
  379. U = pic->data[1];
  380. V = pic->data[2];
  381. cur_y = 0;
  382. for (slice = 0; slice < nslices; slice++) {
  383. slice_size = bytestream2_get_le32(&gb);
  384. next_y = ((slice + 1) * avctx->height) / nslices;
  385. slice_height = (next_y & ~3) - (cur_y & ~3);
  386. if (slice_size > src_size - off) {
  387. av_log(avctx, AV_LOG_ERROR,
  388. "invalid slice size %"PRIu32" (only %"PRIu32" bytes left)\n",
  389. slice_size, src_size - off);
  390. return AVERROR_INVALIDDATA;
  391. }
  392. if (slice_size <= 16) {
  393. av_log(avctx, AV_LOG_ERROR, "invalid slice size %"PRIu32"\n", slice_size);
  394. return AVERROR_INVALIDDATA;
  395. }
  396. if (AV_RL32(src + off) != slice_size - 16) {
  397. av_log(avctx, AV_LOG_ERROR,
  398. "Slice sizes mismatch: got %"PRIu32" instead of %"PRIu32"\n",
  399. AV_RL32(src + off), slice_size - 16);
  400. }
  401. if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
  402. return ret;
  403. dx2_decode_slice_410(&gb2, avctx->width, slice_height, Y, U, V,
  404. pic->linesize[0], pic->linesize[1],
  405. pic->linesize[2]);
  406. Y += pic->linesize[0] * slice_height;
  407. U += pic->linesize[1] * (slice_height >> 2);
  408. V += pic->linesize[2] * (slice_height >> 2);
  409. off += slice_size;
  410. cur_y = next_y;
  411. }
  412. return 0;
  413. }
  414. static int dx2_decode_slice_420(GetBitContext *gb, int width, int height,
  415. uint8_t *Y, uint8_t *U, uint8_t *V,
  416. int ystride, int ustride, int vstride)
  417. {
  418. int x, y, i;
  419. uint8_t lru[3][8];
  420. for (i = 0; i < 3; i++)
  421. memcpy(lru[i], def_lru, 8 * sizeof(*def_lru));
  422. for (y = 0; y < height; y+=2) {
  423. for (x = 0; x < width; x += 2) {
  424. Y[x + 0 + 0 * ystride] = decode_sym(gb, lru[0]);
  425. Y[x + 1 + 0 * ystride] = decode_sym(gb, lru[0]);
  426. Y[x + 0 + 1 * ystride] = decode_sym(gb, lru[0]);
  427. Y[x + 1 + 1 * ystride] = decode_sym(gb, lru[0]);
  428. U[x >> 1] = decode_sym(gb, lru[1]) ^ 0x80;
  429. V[x >> 1] = decode_sym(gb, lru[2]) ^ 0x80;
  430. }
  431. Y += ystride << 1;
  432. U += ustride;
  433. V += vstride;
  434. }
  435. return 0;
  436. }
  437. static int dxtory_decode_v2_420(AVCodecContext *avctx, AVFrame *pic,
  438. const uint8_t *src, int src_size)
  439. {
  440. GetByteContext gb;
  441. GetBitContext gb2;
  442. int nslices, slice, slice_height;
  443. int cur_y, next_y;
  444. uint32_t off, slice_size;
  445. uint8_t *Y, *U, *V;
  446. int ret;
  447. bytestream2_init(&gb, src, src_size);
  448. nslices = bytestream2_get_le16(&gb);
  449. off = FFALIGN(nslices * 4 + 2, 16);
  450. if (src_size < off) {
  451. av_log(avctx, AV_LOG_ERROR, "no slice data\n");
  452. return AVERROR_INVALIDDATA;
  453. }
  454. if (!nslices) {
  455. avpriv_request_sample(avctx, "%d slices for %dx%d", nslices,
  456. avctx->width, avctx->height);
  457. return AVERROR_PATCHWELCOME;
  458. }
  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. for (slice = 0; slice < nslices; slice++) {
  471. slice_size = bytestream2_get_le32(&gb);
  472. next_y = ((slice + 1) * avctx->height) / nslices;
  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. if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
  490. return ret;
  491. dx2_decode_slice_420(&gb2, avctx->width, slice_height, Y, U, V,
  492. pic->linesize[0], pic->linesize[1],
  493. pic->linesize[2]);
  494. Y += pic->linesize[0] * slice_height;
  495. U += pic->linesize[1] * (slice_height >> 1);
  496. V += pic->linesize[2] * (slice_height >> 1);
  497. off += slice_size;
  498. cur_y = next_y;
  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. if ((ret = init_get_bits8(&gb2, src + off + 16, slice_size - 16)) < 0)
  568. return ret;
  569. dx2_decode_slice_444(&gb2, avctx->width, slice_height, Y, U, V,
  570. pic->linesize[0], pic->linesize[1],
  571. pic->linesize[2]);
  572. Y += pic->linesize[0] * slice_height;
  573. U += pic->linesize[1] * slice_height;
  574. V += pic->linesize[2] * slice_height;
  575. off += slice_size;
  576. }
  577. return 0;
  578. }
  579. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  580. AVPacket *avpkt)
  581. {
  582. AVFrame *pic = data;
  583. const uint8_t *src = avpkt->data;
  584. int ret;
  585. if (avpkt->size < 16) {
  586. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  587. return AVERROR_INVALIDDATA;
  588. }
  589. switch (AV_RB32(src)) {
  590. case 0x01000001:
  591. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  592. AV_PIX_FMT_BGR24, 3);
  593. break;
  594. case 0x01000009:
  595. ret = dxtory_decode_v2_rgb(avctx, pic, src + 16, avpkt->size - 16);
  596. break;
  597. case 0x02000001:
  598. ret = dxtory_decode_v1_420(avctx, pic, src + 16, avpkt->size - 16);
  599. break;
  600. case 0x02000009:
  601. ret = dxtory_decode_v2_420(avctx, pic, src + 16, avpkt->size - 16);
  602. break;
  603. case 0x03000001:
  604. ret = dxtory_decode_v1_410(avctx, pic, src + 16, avpkt->size - 16);
  605. break;
  606. case 0x03000009:
  607. ret = dxtory_decode_v2_410(avctx, pic, src + 16, avpkt->size - 16);
  608. break;
  609. case 0x04000001:
  610. ret = dxtory_decode_v1_444(avctx, pic, src + 16, avpkt->size - 16);
  611. break;
  612. case 0x04000009:
  613. ret = dxtory_decode_v2_444(avctx, pic, src + 16, avpkt->size - 16);
  614. break;
  615. case 0x17000001:
  616. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  617. AV_PIX_FMT_RGB565LE, 2);
  618. break;
  619. case 0x17000009:
  620. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 1);
  621. break;
  622. case 0x18000001:
  623. case 0x19000001:
  624. ret = dxtory_decode_v1_rgb(avctx, pic, src + 16, avpkt->size - 16,
  625. AV_PIX_FMT_RGB555LE, 2);
  626. break;
  627. case 0x18000009:
  628. case 0x19000009:
  629. ret = dxtory_decode_v2_565(avctx, pic, src + 16, avpkt->size - 16, 0);
  630. break;
  631. default:
  632. avpriv_request_sample(avctx, "Frame header %"PRIX32, AV_RB32(src));
  633. return AVERROR_PATCHWELCOME;
  634. }
  635. if (ret)
  636. return ret;
  637. pic->pict_type = AV_PICTURE_TYPE_I;
  638. pic->key_frame = 1;
  639. *got_frame = 1;
  640. return avpkt->size;
  641. }
  642. AVCodec ff_dxtory_decoder = {
  643. .name = "dxtory",
  644. .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
  645. .type = AVMEDIA_TYPE_VIDEO,
  646. .id = AV_CODEC_ID_DXTORY,
  647. .decode = decode_frame,
  648. .capabilities = AV_CODEC_CAP_DR1,
  649. };