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.

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