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.

481 lines
14KB

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