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.

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