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.

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