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.

567 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, AV_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. if (!slice_height)
  191. continue;
  192. bsrc = src + slice_start * stride;
  193. // first line - left neighbour prediction
  194. bsrc[0] += 0x80;
  195. A = bsrc[0];
  196. for (i = step; i < width * step; i += step) {
  197. bsrc[i] += A;
  198. A = bsrc[i];
  199. }
  200. bsrc += stride;
  201. if (slice_height == 1)
  202. continue;
  203. // second line - first element has top prediction, the rest uses median
  204. C = bsrc[-stride];
  205. bsrc[0] += C;
  206. A = bsrc[0];
  207. for (i = step; i < width * step; i += step) {
  208. B = bsrc[i - stride];
  209. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  210. C = B;
  211. A = bsrc[i];
  212. }
  213. bsrc += stride;
  214. // the rest of lines use continuous median prediction
  215. for (j = 2; j < slice_height; j++) {
  216. for (i = 0; i < width * step; i += step) {
  217. B = bsrc[i - stride];
  218. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  219. C = B;
  220. A = bsrc[i];
  221. }
  222. bsrc += stride;
  223. }
  224. }
  225. }
  226. /* UtVideo interlaced mode treats every two lines as a single one,
  227. * so restoring function should take care of possible padding between
  228. * two parts of the same "line".
  229. */
  230. static void restore_median_il(uint8_t *src, int step, int stride,
  231. int width, int height, int slices, int rmode)
  232. {
  233. int i, j, slice;
  234. int A, B, C;
  235. uint8_t *bsrc;
  236. int slice_start, slice_height;
  237. const int cmask = ~(rmode ? 3 : 1);
  238. const int stride2 = stride << 1;
  239. for (slice = 0; slice < slices; slice++) {
  240. slice_start = ((slice * height) / slices) & cmask;
  241. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  242. slice_start;
  243. slice_height >>= 1;
  244. if (!slice_height)
  245. continue;
  246. bsrc = src + slice_start * stride;
  247. // first line - left neighbour prediction
  248. bsrc[0] += 0x80;
  249. A = bsrc[0];
  250. for (i = step; i < width * step; i += step) {
  251. bsrc[i] += A;
  252. A = bsrc[i];
  253. }
  254. for (i = 0; i < width * step; i += step) {
  255. bsrc[stride + i] += A;
  256. A = bsrc[stride + i];
  257. }
  258. bsrc += stride2;
  259. if (slice_height == 1)
  260. continue;
  261. // second line - first element has top prediction, the rest uses median
  262. C = bsrc[-stride2];
  263. bsrc[0] += C;
  264. A = bsrc[0];
  265. for (i = step; i < width * step; i += step) {
  266. B = bsrc[i - stride2];
  267. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  268. C = B;
  269. A = bsrc[i];
  270. }
  271. for (i = 0; i < width * step; i += step) {
  272. B = bsrc[i - stride];
  273. bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
  274. C = B;
  275. A = bsrc[stride + i];
  276. }
  277. bsrc += stride2;
  278. // the rest of lines use continuous median prediction
  279. for (j = 2; j < slice_height; j++) {
  280. for (i = 0; i < width * step; i += step) {
  281. B = bsrc[i - stride2];
  282. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  283. C = B;
  284. A = bsrc[i];
  285. }
  286. for (i = 0; i < width * step; i += step) {
  287. B = bsrc[i - stride];
  288. bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
  289. C = B;
  290. A = bsrc[i + stride];
  291. }
  292. bsrc += stride2;
  293. }
  294. }
  295. }
  296. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  297. AVPacket *avpkt)
  298. {
  299. const uint8_t *buf = avpkt->data;
  300. int buf_size = avpkt->size;
  301. UtvideoContext *c = avctx->priv_data;
  302. int i, j;
  303. const uint8_t *plane_start[5];
  304. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  305. int ret;
  306. GetByteContext gb;
  307. ThreadFrame frame = { .f = data };
  308. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
  309. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  310. return ret;
  311. }
  312. ff_thread_finish_setup(avctx);
  313. /* parse plane structure to get frame flags and validate slice offsets */
  314. bytestream2_init(&gb, buf, buf_size);
  315. for (i = 0; i < c->planes; i++) {
  316. plane_start[i] = gb.buffer;
  317. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  318. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  319. return AVERROR_INVALIDDATA;
  320. }
  321. bytestream2_skipu(&gb, 256);
  322. slice_start = 0;
  323. slice_end = 0;
  324. for (j = 0; j < c->slices; j++) {
  325. slice_end = bytestream2_get_le32u(&gb);
  326. slice_size = slice_end - slice_start;
  327. if (slice_end < 0 || slice_size < 0 ||
  328. bytestream2_get_bytes_left(&gb) < slice_end) {
  329. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  330. return AVERROR_INVALIDDATA;
  331. }
  332. slice_start = slice_end;
  333. max_slice_size = FFMAX(max_slice_size, slice_size);
  334. }
  335. plane_size = slice_end;
  336. bytestream2_skipu(&gb, plane_size);
  337. }
  338. plane_start[c->planes] = gb.buffer;
  339. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  340. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  341. return AVERROR_INVALIDDATA;
  342. }
  343. c->frame_info = bytestream2_get_le32u(&gb);
  344. av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
  345. c->frame_info);
  346. c->frame_pred = (c->frame_info >> 8) & 3;
  347. if (c->frame_pred == PRED_GRADIENT) {
  348. avpriv_request_sample(avctx, "Frame with gradient prediction");
  349. return AVERROR_PATCHWELCOME;
  350. }
  351. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  352. max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
  353. if (!c->slice_bits) {
  354. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  355. return AVERROR(ENOMEM);
  356. }
  357. switch (c->avctx->pix_fmt) {
  358. case AV_PIX_FMT_RGB24:
  359. case AV_PIX_FMT_RGBA:
  360. for (i = 0; i < c->planes; i++) {
  361. ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
  362. c->planes, frame.f->linesize[0], avctx->width,
  363. avctx->height, plane_start[i],
  364. c->frame_pred == PRED_LEFT);
  365. if (ret)
  366. return ret;
  367. if (c->frame_pred == PRED_MEDIAN) {
  368. if (!c->interlaced) {
  369. restore_median(frame.f->data[0] + ff_ut_rgb_order[i],
  370. c->planes, frame.f->linesize[0], avctx->width,
  371. avctx->height, c->slices, 0);
  372. } else {
  373. restore_median_il(frame.f->data[0] + ff_ut_rgb_order[i],
  374. c->planes, frame.f->linesize[0],
  375. avctx->width, avctx->height, c->slices,
  376. 0);
  377. }
  378. }
  379. }
  380. restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
  381. avctx->width, avctx->height);
  382. break;
  383. case AV_PIX_FMT_YUV420P:
  384. for (i = 0; i < 3; i++) {
  385. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  386. avctx->width >> !!i, avctx->height >> !!i,
  387. plane_start[i], c->frame_pred == PRED_LEFT);
  388. if (ret)
  389. return ret;
  390. if (c->frame_pred == PRED_MEDIAN) {
  391. if (!c->interlaced) {
  392. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  393. avctx->width >> !!i, avctx->height >> !!i,
  394. c->slices, !i);
  395. } else {
  396. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  397. avctx->width >> !!i,
  398. avctx->height >> !!i,
  399. c->slices, !i);
  400. }
  401. }
  402. }
  403. break;
  404. case AV_PIX_FMT_YUV422P:
  405. for (i = 0; i < 3; i++) {
  406. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  407. avctx->width >> !!i, avctx->height,
  408. plane_start[i], c->frame_pred == PRED_LEFT);
  409. if (ret)
  410. return ret;
  411. if (c->frame_pred == PRED_MEDIAN) {
  412. if (!c->interlaced) {
  413. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  414. avctx->width >> !!i, avctx->height,
  415. c->slices, 0);
  416. } else {
  417. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  418. avctx->width >> !!i, avctx->height,
  419. c->slices, 0);
  420. }
  421. }
  422. }
  423. break;
  424. }
  425. frame.f->key_frame = 1;
  426. frame.f->pict_type = AV_PICTURE_TYPE_I;
  427. frame.f->interlaced_frame = !!c->interlaced;
  428. *got_frame = 1;
  429. /* always report that the buffer was completely consumed */
  430. return buf_size;
  431. }
  432. static av_cold int decode_init(AVCodecContext *avctx)
  433. {
  434. UtvideoContext * const c = avctx->priv_data;
  435. c->avctx = avctx;
  436. ff_bswapdsp_init(&c->bdsp);
  437. if (avctx->extradata_size < 16) {
  438. av_log(avctx, AV_LOG_ERROR,
  439. "Insufficient extradata size %d, should be at least 16\n",
  440. avctx->extradata_size);
  441. return AVERROR_INVALIDDATA;
  442. }
  443. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  444. avctx->extradata[3], avctx->extradata[2],
  445. avctx->extradata[1], avctx->extradata[0]);
  446. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  447. AV_RB32(avctx->extradata + 4));
  448. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  449. c->flags = AV_RL32(avctx->extradata + 12);
  450. if (c->frame_info_size != 4)
  451. avpriv_request_sample(avctx, "Frame info not 4 bytes");
  452. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
  453. c->slices = (c->flags >> 24) + 1;
  454. c->compression = c->flags & 1;
  455. c->interlaced = c->flags & 0x800;
  456. c->slice_bits_size = 0;
  457. switch (avctx->codec_tag) {
  458. case MKTAG('U', 'L', 'R', 'G'):
  459. c->planes = 3;
  460. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  461. break;
  462. case MKTAG('U', 'L', 'R', 'A'):
  463. c->planes = 4;
  464. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  465. break;
  466. case MKTAG('U', 'L', 'Y', '0'):
  467. c->planes = 3;
  468. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  469. avctx->colorspace = AVCOL_SPC_BT470BG;
  470. break;
  471. case MKTAG('U', 'L', 'Y', '2'):
  472. c->planes = 3;
  473. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  474. avctx->colorspace = AVCOL_SPC_BT470BG;
  475. break;
  476. case MKTAG('U', 'L', 'H', '0'):
  477. c->planes = 3;
  478. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  479. avctx->colorspace = AVCOL_SPC_BT709;
  480. break;
  481. case MKTAG('U', 'L', 'H', '2'):
  482. c->planes = 3;
  483. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  484. avctx->colorspace = AVCOL_SPC_BT709;
  485. break;
  486. default:
  487. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  488. avctx->codec_tag);
  489. return AVERROR_INVALIDDATA;
  490. }
  491. return 0;
  492. }
  493. static av_cold int decode_end(AVCodecContext *avctx)
  494. {
  495. UtvideoContext * const c = avctx->priv_data;
  496. av_freep(&c->slice_bits);
  497. return 0;
  498. }
  499. AVCodec ff_utvideo_decoder = {
  500. .name = "utvideo",
  501. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  502. .type = AVMEDIA_TYPE_VIDEO,
  503. .id = AV_CODEC_ID_UTVIDEO,
  504. .priv_data_size = sizeof(UtvideoContext),
  505. .init = decode_init,
  506. .close = decode_end,
  507. .decode = decode_frame,
  508. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  509. };