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.

482 lines
15KB

  1. /*
  2. * MagicYUV decoder
  3. * Copyright (c) 2016 Paul B Mahol
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "libavutil/pixdesc.h"
  24. #include "avcodec.h"
  25. #include "bitstream.h"
  26. #include "bytestream.h"
  27. #include "huffyuvdsp.h"
  28. #include "internal.h"
  29. #include "thread.h"
  30. #include "vlc.h"
  31. typedef struct Slice {
  32. uint32_t start;
  33. uint32_t size;
  34. } Slice;
  35. typedef enum Prediction {
  36. LEFT = 1,
  37. GRADIENT,
  38. MEDIAN,
  39. } Prediction;
  40. typedef struct HuffEntry {
  41. uint8_t sym;
  42. uint8_t len;
  43. uint32_t code;
  44. } HuffEntry;
  45. typedef struct MagicYUVContext {
  46. AVFrame *p;
  47. int slice_height;
  48. int nb_slices;
  49. int planes; // number of encoded planes in bitstream
  50. int decorrelate; // postprocessing work
  51. int interlaced; // video is interlaced
  52. uint8_t *buf; // pointer to AVPacket->data
  53. int hshift[4];
  54. int vshift[4];
  55. Slice *slices[4]; // slice bitstream positions for each plane
  56. unsigned int slices_size[4]; // slice sizes for each plane
  57. uint8_t len[4][256]; // table of code lengths for each plane
  58. VLC vlc[4]; // VLC for each plane
  59. HuffYUVDSPContext hdsp;
  60. } MagicYUVContext;
  61. static int huff_cmp_len(const void *a, const void *b)
  62. {
  63. const HuffEntry *aa = a, *bb = b;
  64. return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
  65. }
  66. static int huff_build(VLC *vlc, uint8_t *len)
  67. {
  68. HuffEntry he[256];
  69. uint32_t codes[256];
  70. uint8_t bits[256];
  71. uint8_t syms[256];
  72. uint32_t code;
  73. int i;
  74. for (i = 0; i < 256; i++) {
  75. he[i].sym = 255 - i;
  76. he[i].len = len[i];
  77. }
  78. qsort(he, 256, sizeof(HuffEntry), huff_cmp_len);
  79. code = 1;
  80. for (i = 255; i >= 0; i--) {
  81. codes[i] = code >> (32 - he[i].len);
  82. bits[i] = he[i].len;
  83. syms[i] = he[i].sym;
  84. code += 0x80000000u >> (he[i].len - 1);
  85. }
  86. ff_free_vlc(vlc);
  87. return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
  88. bits, sizeof(*bits), sizeof(*bits),
  89. codes, sizeof(*codes), sizeof(*codes),
  90. syms, sizeof(*syms), sizeof(*syms), 0);
  91. }
  92. static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
  93. int j, int threadnr)
  94. {
  95. MagicYUVContext *s = avctx->priv_data;
  96. int interlaced = s->interlaced;
  97. AVFrame *p = s->p;
  98. int i, k, x;
  99. BitstreamContext bc;
  100. uint8_t *dst;
  101. for (i = 0; i < s->planes; i++) {
  102. int left, lefttop, top;
  103. int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->height - j * s->slice_height), s->vshift[i]);
  104. int width = AV_CEIL_RSHIFT(avctx->width, s->hshift[i]);
  105. int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
  106. ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
  107. ptrdiff_t stride = p->linesize[i];
  108. int flags, pred;
  109. int ret = bitstream_init8(&bc, s->buf + s->slices[i][j].start,
  110. s->slices[i][j].size);
  111. if (ret < 0)
  112. return ret;
  113. flags = bitstream_read(&bc, 8);
  114. pred = bitstream_read(&bc, 8);
  115. dst = p->data[i] + j * sheight * stride;
  116. if (flags & 1) {
  117. for (k = 0; k < height; k++) {
  118. for (x = 0; x < width; x++)
  119. dst[x] = bitstream_read(&bc, 8);
  120. dst += stride;
  121. }
  122. } else {
  123. for (k = 0; k < height; k++) {
  124. for (x = 0; x < width; x++) {
  125. int pix;
  126. if (bitstream_bits_left(&bc) <= 0)
  127. return AVERROR_INVALIDDATA;
  128. pix = bitstream_read_vlc(&bc, s->vlc[i].table, s->vlc[i].bits, 3);
  129. if (pix < 0)
  130. return AVERROR_INVALIDDATA;
  131. dst[x] = 255 - pix;
  132. }
  133. dst += stride;
  134. }
  135. }
  136. switch (pred) {
  137. case LEFT:
  138. dst = p->data[i] + j * sheight * stride;
  139. s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
  140. dst += stride;
  141. if (interlaced) {
  142. s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
  143. dst += stride;
  144. }
  145. for (k = 1 + interlaced; k < height; k++) {
  146. s->hdsp.add_hfyu_left_pred(dst, dst, width, dst[-fake_stride]);
  147. dst += stride;
  148. }
  149. break;
  150. case GRADIENT:
  151. dst = p->data[i] + j * sheight * stride;
  152. s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
  153. left = lefttop = 0;
  154. dst += stride;
  155. if (interlaced) {
  156. s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
  157. left = lefttop = 0;
  158. dst += stride;
  159. }
  160. for (k = 1 + interlaced; k < height; k++) {
  161. top = dst[-fake_stride];
  162. left = top + dst[0];
  163. dst[0] = left;
  164. for (x = 1; x < width; x++) {
  165. top = dst[x - fake_stride];
  166. lefttop = dst[x - (fake_stride + 1)];
  167. left += top - lefttop + dst[x];
  168. dst[x] = left;
  169. }
  170. dst += stride;
  171. }
  172. break;
  173. case MEDIAN:
  174. dst = p->data[i] + j * sheight * stride;
  175. lefttop = left = dst[0];
  176. s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
  177. dst += stride;
  178. if (interlaced) {
  179. lefttop = left = dst[0];
  180. s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
  181. dst += stride;
  182. }
  183. for (k = 1 + interlaced; k < height; k++) {
  184. s->hdsp.add_hfyu_median_pred(dst, dst - fake_stride,
  185. dst, width, &left, &lefttop);
  186. lefttop = left = dst[0];
  187. dst += stride;
  188. }
  189. break;
  190. default:
  191. avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
  192. }
  193. }
  194. if (s->decorrelate) {
  195. int height = FFMIN(s->slice_height, avctx->height - j * s->slice_height);
  196. int width = avctx->width;
  197. uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
  198. uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
  199. uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
  200. for (i = 0; i < height; i++) {
  201. s->hdsp.add_bytes(b, g, width);
  202. s->hdsp.add_bytes(r, g, width);
  203. b += p->linesize[0];
  204. g += p->linesize[1];
  205. r += p->linesize[2];
  206. }
  207. }
  208. return 0;
  209. }
  210. static int magy_decode_frame(AVCodecContext *avctx, void *data,
  211. int *got_frame, AVPacket *avpkt)
  212. {
  213. MagicYUVContext *s = avctx->priv_data;
  214. ThreadFrame frame = { .f = data };
  215. AVFrame *p = data;
  216. GetByteContext gbyte;
  217. BitstreamContext bc;
  218. uint32_t first_offset, offset, next_offset, header_size, slice_width;
  219. int width, height, format, version, table_size;
  220. int ret, i, j, k;
  221. bytestream2_init(&gbyte, avpkt->data, avpkt->size);
  222. if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
  223. return AVERROR_INVALIDDATA;
  224. header_size = bytestream2_get_le32(&gbyte);
  225. if (header_size < 32 || header_size >= avpkt->size) {
  226. av_log(avctx, AV_LOG_ERROR,
  227. "header or packet too small %"PRIu32"\n", header_size);
  228. return AVERROR_INVALIDDATA;
  229. }
  230. version = bytestream2_get_byte(&gbyte);
  231. if (version != 7) {
  232. avpriv_request_sample(avctx, "Version %d", version);
  233. return AVERROR_PATCHWELCOME;
  234. }
  235. s->hshift[1] =
  236. s->vshift[1] =
  237. s->hshift[2] =
  238. s->vshift[2] = 0;
  239. s->decorrelate = 0;
  240. format = bytestream2_get_byte(&gbyte);
  241. switch (format) {
  242. case 0x65:
  243. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  244. s->decorrelate = 1;
  245. break;
  246. case 0x66:
  247. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  248. s->decorrelate = 1;
  249. break;
  250. case 0x67:
  251. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  252. break;
  253. case 0x68:
  254. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  255. s->hshift[1] =
  256. s->hshift[2] = 1;
  257. break;
  258. case 0x69:
  259. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  260. s->hshift[1] =
  261. s->vshift[1] =
  262. s->hshift[2] =
  263. s->vshift[2] = 1;
  264. break;
  265. case 0x6a:
  266. avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  267. break;
  268. case 0x6b:
  269. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  270. break;
  271. default:
  272. avpriv_request_sample(avctx, "Format 0x%X", format);
  273. return AVERROR_PATCHWELCOME;
  274. }
  275. s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  276. bytestream2_skip(&gbyte, 2);
  277. s->interlaced = !!(bytestream2_get_byte(&gbyte) & 2);
  278. bytestream2_skip(&gbyte, 3);
  279. width = bytestream2_get_le32(&gbyte);
  280. height = bytestream2_get_le32(&gbyte);
  281. ret = ff_set_dimensions(avctx, width, height);
  282. if (ret < 0)
  283. return ret;
  284. slice_width = bytestream2_get_le32(&gbyte);
  285. if (slice_width != width) {
  286. avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
  287. return AVERROR_PATCHWELCOME;
  288. }
  289. s->slice_height = bytestream2_get_le32(&gbyte);
  290. if (s->slice_height <= 0 || s->slice_height > INT_MAX - height) {
  291. av_log(avctx, AV_LOG_ERROR,
  292. "invalid slice height: %d\n", s->slice_height);
  293. return AVERROR_INVALIDDATA;
  294. }
  295. bytestream2_skip(&gbyte, 4);
  296. s->nb_slices = (height + s->slice_height - 1) / s->slice_height;
  297. if (s->nb_slices > INT_MAX / sizeof(Slice)) {
  298. av_log(avctx, AV_LOG_ERROR,
  299. "invalid number of slices: %d\n", s->nb_slices);
  300. return AVERROR_INVALIDDATA;
  301. }
  302. for (i = 0; i < s->planes; i++) {
  303. av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
  304. if (!s->slices[i])
  305. return AVERROR(ENOMEM);
  306. offset = bytestream2_get_le32(&gbyte);
  307. if (offset >= avpkt->size - header_size)
  308. return AVERROR_INVALIDDATA;
  309. if (i == 0)
  310. first_offset = offset;
  311. for (j = 0; j < s->nb_slices - 1; j++) {
  312. s->slices[i][j].start = offset + header_size;
  313. next_offset = bytestream2_get_le32(&gbyte);
  314. if (next_offset <= offset || next_offset >= avpkt->size - header_size)
  315. return AVERROR_INVALIDDATA;
  316. s->slices[i][j].size = next_offset - offset;
  317. offset = next_offset;
  318. }
  319. s->slices[i][j].start = offset + header_size;
  320. s->slices[i][j].size = avpkt->size - s->slices[i][j].start;
  321. }
  322. if (bytestream2_get_byte(&gbyte) != s->planes)
  323. return AVERROR_INVALIDDATA;
  324. bytestream2_skip(&gbyte, s->nb_slices * s->planes);
  325. table_size = header_size + first_offset - bytestream2_tell(&gbyte);
  326. if (table_size < 2)
  327. return AVERROR_INVALIDDATA;
  328. ret = bitstream_init8(&bc, avpkt->data + bytestream2_tell(&gbyte), table_size);
  329. if (ret < 0)
  330. return ret;
  331. memset(s->len, 0, sizeof(s->len));
  332. j = i = 0;
  333. while (bitstream_bits_left(&bc) >= 8) {
  334. int b = bitstream_read(&bc, 4);
  335. int x = bitstream_read(&bc, 4);
  336. int l = bitstream_read(&bc, b) + 1;
  337. for (k = 0; k < l; k++)
  338. if (j + k < 256)
  339. s->len[i][j + k] = x;
  340. j += l;
  341. if (j == 256) {
  342. j = 0;
  343. if (huff_build(&s->vlc[i], s->len[i])) {
  344. av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  345. return AVERROR_INVALIDDATA;
  346. }
  347. i++;
  348. if (i == s->planes) {
  349. break;
  350. }
  351. } else if (j > 256) {
  352. return AVERROR_INVALIDDATA;
  353. }
  354. }
  355. if (i != s->planes) {
  356. av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
  357. return AVERROR_INVALIDDATA;
  358. }
  359. p->pict_type = AV_PICTURE_TYPE_I;
  360. p->key_frame = 1;
  361. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  362. return ret;
  363. s->buf = avpkt->data;
  364. s->p = p;
  365. avctx->execute2(avctx, magy_decode_slice, NULL, NULL, s->nb_slices);
  366. if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
  367. avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
  368. FFSWAP(uint8_t*, p->data[0], p->data[1]);
  369. FFSWAP(int, p->linesize[0], p->linesize[1]);
  370. }
  371. *got_frame = 1;
  372. return avpkt->size;
  373. }
  374. #if HAVE_THREADS
  375. static int magy_init_thread_copy(AVCodecContext *avctx)
  376. {
  377. MagicYUVContext *s = avctx->priv_data;
  378. int i;
  379. for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
  380. s->slices[i] = NULL;
  381. s->slices_size[i] = 0;
  382. }
  383. return 0;
  384. }
  385. #endif
  386. static av_cold int magy_decode_init(AVCodecContext *avctx)
  387. {
  388. MagicYUVContext *s = avctx->priv_data;
  389. ff_huffyuvdsp_init(&s->hdsp);
  390. return 0;
  391. }
  392. static av_cold int magy_decode_end(AVCodecContext *avctx)
  393. {
  394. MagicYUVContext * const s = avctx->priv_data;
  395. int i;
  396. for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
  397. av_freep(&s->slices[i]);
  398. s->slices_size[i] = 0;
  399. ff_free_vlc(&s->vlc[i]);
  400. }
  401. return 0;
  402. }
  403. AVCodec ff_magicyuv_decoder = {
  404. .name = "magicyuv",
  405. .long_name = NULL_IF_CONFIG_SMALL("MagicYUV video"),
  406. .type = AVMEDIA_TYPE_VIDEO,
  407. .id = AV_CODEC_ID_MAGICYUV,
  408. .priv_data_size = sizeof(MagicYUVContext),
  409. .init = magy_decode_init,
  410. .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
  411. .close = magy_decode_end,
  412. .decode = magy_decode_frame,
  413. .capabilities = AV_CODEC_CAP_DR1 |
  414. AV_CODEC_CAP_FRAME_THREADS |
  415. AV_CODEC_CAP_SLICE_THREADS,
  416. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  417. };