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.

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