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.

579 lines
18KB

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