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.

592 lines
19KB

  1. /*
  2. * Ut Video decoder
  3. * Copyright (c) 2011 Konstantin Shishkov
  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. /**
  22. * @file
  23. * Ut Video decoder
  24. */
  25. #include <stdlib.h>
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "get_bits.h"
  30. #include "dsputil.h"
  31. #include "thread.h"
  32. enum {
  33. PRED_NONE = 0,
  34. PRED_LEFT,
  35. PRED_GRADIENT,
  36. PRED_MEDIAN,
  37. };
  38. typedef struct UtvideoContext {
  39. AVCodecContext *avctx;
  40. AVFrame pic;
  41. DSPContext dsp;
  42. uint32_t frame_info_size, flags, frame_info;
  43. int planes;
  44. int slices;
  45. int compression;
  46. int interlaced;
  47. int frame_pred;
  48. uint8_t *slice_bits;
  49. int slice_bits_size;
  50. } UtvideoContext;
  51. typedef struct HuffEntry {
  52. uint8_t sym;
  53. uint8_t len;
  54. } HuffEntry;
  55. static int huff_cmp(const void *a, const void *b)
  56. {
  57. const HuffEntry *aa = a, *bb = b;
  58. return (aa->len - bb->len)*256 + aa->sym - bb->sym;
  59. }
  60. static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
  61. {
  62. int i;
  63. HuffEntry he[256];
  64. int last;
  65. uint32_t codes[256];
  66. uint8_t bits[256];
  67. uint8_t syms[256];
  68. uint32_t code;
  69. *fsym = -1;
  70. for (i = 0; i < 256; i++) {
  71. he[i].sym = i;
  72. he[i].len = *src++;
  73. }
  74. qsort(he, 256, sizeof(*he), huff_cmp);
  75. if (!he[0].len) {
  76. *fsym = he[0].sym;
  77. return 0;
  78. }
  79. if (he[0].len > 32)
  80. return -1;
  81. last = 255;
  82. while (he[last].len == 255 && last)
  83. last--;
  84. code = 1;
  85. for (i = last; i >= 0; i--) {
  86. codes[i] = code >> (32 - he[i].len);
  87. bits[i] = he[i].len;
  88. syms[i] = he[i].sym;
  89. code += 0x80000000u >> (he[i].len - 1);
  90. }
  91. return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
  92. bits, sizeof(*bits), sizeof(*bits),
  93. codes, sizeof(*codes), sizeof(*codes),
  94. syms, sizeof(*syms), sizeof(*syms), 0);
  95. }
  96. static int decode_plane(UtvideoContext *c, int plane_no,
  97. uint8_t *dst, int step, int stride,
  98. int width, int height,
  99. const uint8_t *src, int use_pred)
  100. {
  101. int i, j, slice, pix;
  102. int sstart, send;
  103. VLC vlc;
  104. GetBitContext gb;
  105. int prev, fsym;
  106. const int cmask = ~(!plane_no && c->avctx->pix_fmt == PIX_FMT_YUV420P);
  107. if (build_huff(src, &vlc, &fsym)) {
  108. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  109. return AVERROR_INVALIDDATA;
  110. }
  111. if (fsym >= 0) { // build_huff reported a symbol to fill slices with
  112. send = 0;
  113. for (slice = 0; slice < c->slices; slice++) {
  114. uint8_t *dest;
  115. sstart = send;
  116. send = (height * (slice + 1) / c->slices) & cmask;
  117. dest = dst + sstart * stride;
  118. prev = 0x80;
  119. for (j = sstart; j < send; j++) {
  120. for (i = 0; i < width * step; i += step) {
  121. pix = fsym;
  122. if (use_pred) {
  123. prev += pix;
  124. pix = prev;
  125. }
  126. dest[i] = pix;
  127. }
  128. dest += stride;
  129. }
  130. }
  131. return 0;
  132. }
  133. src += 256;
  134. send = 0;
  135. for (slice = 0; slice < c->slices; slice++) {
  136. uint8_t *dest;
  137. int slice_data_start, slice_data_end, slice_size;
  138. sstart = send;
  139. send = (height * (slice + 1) / c->slices) & cmask;
  140. dest = dst + sstart * stride;
  141. // slice offset and size validation was done earlier
  142. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  143. slice_data_end = AV_RL32(src + slice * 4);
  144. slice_size = slice_data_end - slice_data_start;
  145. if (!slice_size) {
  146. for (j = sstart; j < send; j++) {
  147. for (i = 0; i < width * step; i += step)
  148. dest[i] = 0x80;
  149. dest += stride;
  150. }
  151. continue;
  152. }
  153. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  154. slice_size);
  155. memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  156. c->dsp.bswap_buf((uint32_t *) c->slice_bits, (uint32_t *) c->slice_bits,
  157. (slice_data_end - slice_data_start + 3) >> 2);
  158. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  159. prev = 0x80;
  160. for (j = sstart; j < send; j++) {
  161. for (i = 0; i < width * step; i += step) {
  162. if (get_bits_left(&gb) <= 0) {
  163. av_log(c->avctx, AV_LOG_ERROR,
  164. "Slice decoding ran out of bits\n");
  165. goto fail;
  166. }
  167. pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
  168. if (pix < 0) {
  169. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  170. goto fail;
  171. }
  172. if (use_pred) {
  173. prev += pix;
  174. pix = prev;
  175. }
  176. dest[i] = pix;
  177. }
  178. dest += stride;
  179. }
  180. if (get_bits_left(&gb) > 32)
  181. av_log(c->avctx, AV_LOG_WARNING,
  182. "%d bits left after decoding slice\n", get_bits_left(&gb));
  183. }
  184. ff_free_vlc(&vlc);
  185. return 0;
  186. fail:
  187. ff_free_vlc(&vlc);
  188. return AVERROR_INVALIDDATA;
  189. }
  190. static const int rgb_order[4] = { 1, 2, 0, 3 };
  191. static void restore_rgb_planes(uint8_t *src, int step, int stride, int width,
  192. int height)
  193. {
  194. int i, j;
  195. uint8_t r, g, b;
  196. for (j = 0; j < height; j++) {
  197. for (i = 0; i < width * step; i += step) {
  198. r = src[i];
  199. g = src[i + 1];
  200. b = src[i + 2];
  201. src[i] = r + g - 0x80;
  202. src[i + 2] = b + g - 0x80;
  203. }
  204. src += stride;
  205. }
  206. }
  207. static void restore_median(uint8_t *src, int step, int stride,
  208. int width, int height, int slices, int rmode)
  209. {
  210. int i, j, slice;
  211. int A, B, C;
  212. uint8_t *bsrc;
  213. int slice_start, slice_height;
  214. const int cmask = ~rmode;
  215. for (slice = 0; slice < slices; slice++) {
  216. slice_start = ((slice * height) / slices) & cmask;
  217. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  218. slice_start;
  219. bsrc = src + slice_start * stride;
  220. // first line - left neighbour prediction
  221. bsrc[0] += 0x80;
  222. A = bsrc[0];
  223. for (i = step; i < width * step; i += step) {
  224. bsrc[i] += A;
  225. A = bsrc[i];
  226. }
  227. bsrc += stride;
  228. if (slice_height == 1)
  229. continue;
  230. // second line - first element has top prediction, the rest uses median
  231. C = bsrc[-stride];
  232. bsrc[0] += C;
  233. A = bsrc[0];
  234. for (i = step; i < width * step; i += step) {
  235. B = bsrc[i - stride];
  236. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  237. C = B;
  238. A = bsrc[i];
  239. }
  240. bsrc += stride;
  241. // the rest of lines use continuous median prediction
  242. for (j = 2; j < slice_height; j++) {
  243. for (i = 0; i < width * step; i += step) {
  244. B = bsrc[i - stride];
  245. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  246. C = B;
  247. A = bsrc[i];
  248. }
  249. bsrc += stride;
  250. }
  251. }
  252. }
  253. /* UtVideo interlaced mode treats every two lines as a single one,
  254. * so restoring function should take care of possible padding between
  255. * two parts of the same "line".
  256. */
  257. static void restore_median_il(uint8_t *src, int step, int stride,
  258. int width, int height, int slices, int rmode)
  259. {
  260. int i, j, slice;
  261. int A, B, C;
  262. uint8_t *bsrc;
  263. int slice_start, slice_height;
  264. const int cmask = ~(rmode ? 3 : 1);
  265. const int stride2 = stride << 1;
  266. for (slice = 0; slice < slices; slice++) {
  267. slice_start = ((slice * height) / slices) & cmask;
  268. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  269. slice_start;
  270. slice_height >>= 1;
  271. bsrc = src + slice_start * stride;
  272. // first line - left neighbour prediction
  273. bsrc[0] += 0x80;
  274. A = bsrc[0];
  275. for (i = step; i < width * step; i += step) {
  276. bsrc[i] += A;
  277. A = bsrc[i];
  278. }
  279. for (i = 0; i < width * step; i += step) {
  280. bsrc[stride + i] += A;
  281. A = bsrc[stride + i];
  282. }
  283. bsrc += stride2;
  284. if (slice_height == 1)
  285. continue;
  286. // second line - first element has top prediction, the rest uses median
  287. C = bsrc[-stride2];
  288. bsrc[0] += C;
  289. A = bsrc[0];
  290. for (i = step; i < width * step; i += step) {
  291. B = bsrc[i - stride2];
  292. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  293. C = B;
  294. A = bsrc[i];
  295. }
  296. for (i = 0; i < width * step; i += step) {
  297. B = bsrc[i - stride];
  298. bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
  299. C = B;
  300. A = bsrc[stride + i];
  301. }
  302. bsrc += stride2;
  303. // the rest of lines use continuous median prediction
  304. for (j = 2; j < slice_height; j++) {
  305. for (i = 0; i < width * step; i += step) {
  306. B = bsrc[i - stride2];
  307. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  308. C = B;
  309. A = bsrc[i];
  310. }
  311. for (i = 0; i < width * step; i += step) {
  312. B = bsrc[i - stride];
  313. bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
  314. C = B;
  315. A = bsrc[i + stride];
  316. }
  317. bsrc += stride2;
  318. }
  319. }
  320. }
  321. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  322. AVPacket *avpkt)
  323. {
  324. const uint8_t *buf = avpkt->data;
  325. int buf_size = avpkt->size;
  326. UtvideoContext *c = avctx->priv_data;
  327. int i, j;
  328. const uint8_t *plane_start[5];
  329. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  330. int ret;
  331. GetByteContext gb;
  332. if (c->pic.data[0])
  333. ff_thread_release_buffer(avctx, &c->pic);
  334. c->pic.reference = 1;
  335. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  336. if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
  337. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  338. return ret;
  339. }
  340. ff_thread_finish_setup(avctx);
  341. /* parse plane structure to get frame flags and validate slice offsets */
  342. bytestream2_init(&gb, buf, buf_size);
  343. for (i = 0; i < c->planes; i++) {
  344. plane_start[i] = gb.buffer;
  345. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  346. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  347. return AVERROR_INVALIDDATA;
  348. }
  349. bytestream2_skipu(&gb, 256);
  350. slice_start = 0;
  351. slice_end = 0;
  352. for (j = 0; j < c->slices; j++) {
  353. slice_end = bytestream2_get_le32u(&gb);
  354. slice_size = slice_end - slice_start;
  355. if (slice_end <= 0 || slice_size <= 0 ||
  356. bytestream2_get_bytes_left(&gb) < slice_end) {
  357. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  358. return AVERROR_INVALIDDATA;
  359. }
  360. slice_start = slice_end;
  361. max_slice_size = FFMAX(max_slice_size, slice_size);
  362. }
  363. plane_size = slice_end;
  364. bytestream2_skipu(&gb, plane_size);
  365. }
  366. plane_start[c->planes] = gb.buffer;
  367. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  368. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  369. return AVERROR_INVALIDDATA;
  370. }
  371. c->frame_info = bytestream2_get_le32u(&gb);
  372. av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
  373. c->frame_pred = (c->frame_info >> 8) & 3;
  374. if (c->frame_pred == PRED_GRADIENT) {
  375. av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
  376. return AVERROR_PATCHWELCOME;
  377. }
  378. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  379. max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
  380. if (!c->slice_bits) {
  381. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  382. return AVERROR(ENOMEM);
  383. }
  384. switch (c->avctx->pix_fmt) {
  385. case PIX_FMT_RGB24:
  386. case PIX_FMT_RGBA:
  387. for (i = 0; i < c->planes; i++) {
  388. ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
  389. c->pic.linesize[0], avctx->width, avctx->height,
  390. plane_start[i], c->frame_pred == PRED_LEFT);
  391. if (ret)
  392. return ret;
  393. if (c->frame_pred == PRED_MEDIAN) {
  394. if (!c->interlaced) {
  395. restore_median(c->pic.data[0] + rgb_order[i], c->planes,
  396. c->pic.linesize[0], avctx->width,
  397. avctx->height, c->slices, 0);
  398. } else {
  399. restore_median_il(c->pic.data[0] + rgb_order[i], c->planes,
  400. c->pic.linesize[0], avctx->width,
  401. avctx->height, c->slices, 0);
  402. }
  403. }
  404. }
  405. restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
  406. avctx->width, avctx->height);
  407. break;
  408. case PIX_FMT_YUV420P:
  409. for (i = 0; i < 3; i++) {
  410. ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
  411. avctx->width >> !!i, avctx->height >> !!i,
  412. plane_start[i], c->frame_pred == PRED_LEFT);
  413. if (ret)
  414. return ret;
  415. if (c->frame_pred == PRED_MEDIAN) {
  416. if (!c->interlaced) {
  417. restore_median(c->pic.data[i], 1, c->pic.linesize[i],
  418. avctx->width >> !!i, avctx->height >> !!i,
  419. c->slices, !i);
  420. } else {
  421. restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
  422. avctx->width >> !!i,
  423. avctx->height >> !!i,
  424. c->slices, !i);
  425. }
  426. }
  427. }
  428. break;
  429. case PIX_FMT_YUV422P:
  430. for (i = 0; i < 3; i++) {
  431. ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
  432. avctx->width >> !!i, avctx->height,
  433. plane_start[i], c->frame_pred == PRED_LEFT);
  434. if (ret)
  435. return ret;
  436. if (c->frame_pred == PRED_MEDIAN) {
  437. if (!c->interlaced) {
  438. restore_median(c->pic.data[i], 1, c->pic.linesize[i],
  439. avctx->width >> !!i, avctx->height,
  440. c->slices, 0);
  441. } else {
  442. restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
  443. avctx->width >> !!i, avctx->height,
  444. c->slices, 0);
  445. }
  446. }
  447. }
  448. break;
  449. }
  450. c->pic.key_frame = 1;
  451. c->pic.pict_type = AV_PICTURE_TYPE_I;
  452. c->pic.interlaced_frame = !!c->interlaced;
  453. *data_size = sizeof(AVFrame);
  454. *(AVFrame*)data = c->pic;
  455. /* always report that the buffer was completely consumed */
  456. return buf_size;
  457. }
  458. static av_cold int decode_init(AVCodecContext *avctx)
  459. {
  460. UtvideoContext * const c = avctx->priv_data;
  461. c->avctx = avctx;
  462. ff_dsputil_init(&c->dsp, avctx);
  463. if (avctx->extradata_size < 16) {
  464. av_log(avctx, AV_LOG_ERROR,
  465. "Insufficient extradata size %d, should be at least 16\n",
  466. avctx->extradata_size);
  467. return AVERROR_INVALIDDATA;
  468. }
  469. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  470. avctx->extradata[3], avctx->extradata[2],
  471. avctx->extradata[1], avctx->extradata[0]);
  472. av_log(avctx, AV_LOG_DEBUG, "Original format %X\n",
  473. AV_RB32(avctx->extradata + 4));
  474. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  475. c->flags = AV_RL32(avctx->extradata + 12);
  476. if (c->frame_info_size != 4)
  477. av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
  478. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
  479. c->slices = (c->flags >> 24) + 1;
  480. c->compression = c->flags & 1;
  481. c->interlaced = c->flags & 0x800;
  482. c->slice_bits_size = 0;
  483. switch (avctx->codec_tag) {
  484. case MKTAG('U', 'L', 'R', 'G'):
  485. c->planes = 3;
  486. avctx->pix_fmt = PIX_FMT_RGB24;
  487. break;
  488. case MKTAG('U', 'L', 'R', 'A'):
  489. c->planes = 4;
  490. avctx->pix_fmt = PIX_FMT_RGBA;
  491. break;
  492. case MKTAG('U', 'L', 'Y', '0'):
  493. c->planes = 3;
  494. avctx->pix_fmt = PIX_FMT_YUV420P;
  495. break;
  496. case MKTAG('U', 'L', 'Y', '2'):
  497. c->planes = 3;
  498. avctx->pix_fmt = PIX_FMT_YUV422P;
  499. break;
  500. default:
  501. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  502. avctx->codec_tag);
  503. return AVERROR_INVALIDDATA;
  504. }
  505. return 0;
  506. }
  507. static av_cold int decode_end(AVCodecContext *avctx)
  508. {
  509. UtvideoContext * const c = avctx->priv_data;
  510. if (c->pic.data[0])
  511. ff_thread_release_buffer(avctx, &c->pic);
  512. av_freep(&c->slice_bits);
  513. return 0;
  514. }
  515. AVCodec ff_utvideo_decoder = {
  516. .name = "utvideo",
  517. .type = AVMEDIA_TYPE_VIDEO,
  518. .id = CODEC_ID_UTVIDEO,
  519. .priv_data_size = sizeof(UtvideoContext),
  520. .init = decode_init,
  521. .close = decode_end,
  522. .decode = decode_frame,
  523. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  524. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  525. };