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.

559 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 == 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. for (j = sstart; j < send; j++) {
  120. for (i = 0; i < width * step; i += step)
  121. dest[i] = 0x80;
  122. dest += stride;
  123. }
  124. continue;
  125. }
  126. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  127. slice_size);
  128. memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  129. c->dsp.bswap_buf((uint32_t *) c->slice_bits, (uint32_t *) c->slice_bits,
  130. (slice_data_end - slice_data_start + 3) >> 2);
  131. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  132. prev = 0x80;
  133. for (j = sstart; j < send; j++) {
  134. for (i = 0; i < width * step; i += step) {
  135. if (get_bits_left(&gb) <= 0) {
  136. av_log(c->avctx, AV_LOG_ERROR,
  137. "Slice decoding ran out of bits\n");
  138. goto fail;
  139. }
  140. pix = get_vlc2(&gb, 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 (get_bits_left(&gb) > 32)
  154. av_log(c->avctx, AV_LOG_WARNING,
  155. "%d bits left after decoding slice\n", get_bits_left(&gb));
  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, int stride, int width,
  164. 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, int 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. bsrc = src + slice_start * stride;
  192. // first line - left neighbour prediction
  193. bsrc[0] += 0x80;
  194. A = bsrc[0];
  195. for (i = step; i < width * step; i += step) {
  196. bsrc[i] += A;
  197. A = bsrc[i];
  198. }
  199. bsrc += stride;
  200. if (slice_height == 1)
  201. continue;
  202. // second line - first element has top prediction, the rest uses median
  203. C = bsrc[-stride];
  204. bsrc[0] += C;
  205. A = bsrc[0];
  206. for (i = step; i < width * step; i += step) {
  207. B = bsrc[i - stride];
  208. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  209. C = B;
  210. A = bsrc[i];
  211. }
  212. bsrc += stride;
  213. // the rest of lines use continuous median prediction
  214. for (j = 2; j < slice_height; j++) {
  215. for (i = 0; i < width * step; i += step) {
  216. B = bsrc[i - stride];
  217. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  218. C = B;
  219. A = bsrc[i];
  220. }
  221. bsrc += stride;
  222. }
  223. }
  224. }
  225. /* UtVideo interlaced mode treats every two lines as a single one,
  226. * so restoring function should take care of possible padding between
  227. * two parts of the same "line".
  228. */
  229. static void restore_median_il(uint8_t *src, int step, int stride,
  230. int width, int height, int slices, int rmode)
  231. {
  232. int i, j, slice;
  233. int A, B, C;
  234. uint8_t *bsrc;
  235. int slice_start, slice_height;
  236. const int cmask = ~(rmode ? 3 : 1);
  237. const int stride2 = stride << 1;
  238. for (slice = 0; slice < slices; slice++) {
  239. slice_start = ((slice * height) / slices) & cmask;
  240. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  241. slice_start;
  242. slice_height >>= 1;
  243. bsrc = src + slice_start * stride;
  244. // first line - left neighbour prediction
  245. bsrc[0] += 0x80;
  246. A = bsrc[0];
  247. for (i = step; i < width * step; i += step) {
  248. bsrc[i] += A;
  249. A = bsrc[i];
  250. }
  251. for (i = 0; i < width * step; i += step) {
  252. bsrc[stride + i] += A;
  253. A = bsrc[stride + i];
  254. }
  255. bsrc += stride2;
  256. if (slice_height == 1)
  257. continue;
  258. // second line - first element has top prediction, the rest uses median
  259. C = bsrc[-stride2];
  260. bsrc[0] += C;
  261. A = bsrc[0];
  262. for (i = step; i < width * step; i += step) {
  263. B = bsrc[i - stride2];
  264. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  265. C = B;
  266. A = bsrc[i];
  267. }
  268. for (i = 0; i < width * step; i += step) {
  269. B = bsrc[i - stride];
  270. bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
  271. C = B;
  272. A = bsrc[stride + i];
  273. }
  274. bsrc += stride2;
  275. // the rest of lines use continuous median prediction
  276. for (j = 2; j < slice_height; j++) {
  277. for (i = 0; i < width * step; i += step) {
  278. B = bsrc[i - stride2];
  279. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  280. C = B;
  281. A = bsrc[i];
  282. }
  283. for (i = 0; i < width * step; i += step) {
  284. B = bsrc[i - stride];
  285. bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
  286. C = B;
  287. A = bsrc[i + stride];
  288. }
  289. bsrc += stride2;
  290. }
  291. }
  292. }
  293. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  294. AVPacket *avpkt)
  295. {
  296. const uint8_t *buf = avpkt->data;
  297. int buf_size = avpkt->size;
  298. UtvideoContext *c = avctx->priv_data;
  299. int i, j;
  300. const uint8_t *plane_start[5];
  301. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  302. int ret;
  303. GetByteContext gb;
  304. if (c->pic.data[0])
  305. ff_thread_release_buffer(avctx, &c->pic);
  306. c->pic.reference = 1;
  307. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  308. if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 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 %X\n", c->frame_info);
  345. c->frame_pred = (c->frame_info >> 8) & 3;
  346. if (c->frame_pred == PRED_GRADIENT) {
  347. av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
  348. return AVERROR_PATCHWELCOME;
  349. }
  350. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  351. max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
  352. if (!c->slice_bits) {
  353. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  354. return AVERROR(ENOMEM);
  355. }
  356. switch (c->avctx->pix_fmt) {
  357. case PIX_FMT_RGB24:
  358. case PIX_FMT_RGBA:
  359. for (i = 0; i < c->planes; i++) {
  360. ret = decode_plane(c, i, c->pic.data[0] + ff_ut_rgb_order[i],
  361. c->planes, c->pic.linesize[0], avctx->width,
  362. avctx->height, plane_start[i],
  363. c->frame_pred == PRED_LEFT);
  364. if (ret)
  365. return ret;
  366. if (c->frame_pred == PRED_MEDIAN) {
  367. if (!c->interlaced) {
  368. restore_median(c->pic.data[0] + ff_ut_rgb_order[i],
  369. c->planes, c->pic.linesize[0], avctx->width,
  370. avctx->height, c->slices, 0);
  371. } else {
  372. restore_median_il(c->pic.data[0] + ff_ut_rgb_order[i],
  373. c->planes, c->pic.linesize[0],
  374. avctx->width, avctx->height, c->slices,
  375. 0);
  376. }
  377. }
  378. }
  379. restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
  380. avctx->width, avctx->height);
  381. break;
  382. case PIX_FMT_YUV420P:
  383. for (i = 0; i < 3; i++) {
  384. ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
  385. avctx->width >> !!i, avctx->height >> !!i,
  386. plane_start[i], c->frame_pred == PRED_LEFT);
  387. if (ret)
  388. return ret;
  389. if (c->frame_pred == PRED_MEDIAN) {
  390. if (!c->interlaced) {
  391. restore_median(c->pic.data[i], 1, c->pic.linesize[i],
  392. avctx->width >> !!i, avctx->height >> !!i,
  393. c->slices, !i);
  394. } else {
  395. restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
  396. avctx->width >> !!i,
  397. avctx->height >> !!i,
  398. c->slices, !i);
  399. }
  400. }
  401. }
  402. break;
  403. case PIX_FMT_YUV422P:
  404. for (i = 0; i < 3; i++) {
  405. ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
  406. avctx->width >> !!i, avctx->height,
  407. plane_start[i], c->frame_pred == PRED_LEFT);
  408. if (ret)
  409. return ret;
  410. if (c->frame_pred == PRED_MEDIAN) {
  411. if (!c->interlaced) {
  412. restore_median(c->pic.data[i], 1, c->pic.linesize[i],
  413. avctx->width >> !!i, avctx->height,
  414. c->slices, 0);
  415. } else {
  416. restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
  417. avctx->width >> !!i, avctx->height,
  418. c->slices, 0);
  419. }
  420. }
  421. }
  422. break;
  423. }
  424. c->pic.key_frame = 1;
  425. c->pic.pict_type = AV_PICTURE_TYPE_I;
  426. c->pic.interlaced_frame = !!c->interlaced;
  427. *data_size = sizeof(AVFrame);
  428. *(AVFrame*)data = c->pic;
  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_dsputil_init(&c->dsp, avctx);
  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 %X\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. av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
  452. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\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 = PIX_FMT_RGB24;
  461. break;
  462. case MKTAG('U', 'L', 'R', 'A'):
  463. c->planes = 4;
  464. avctx->pix_fmt = PIX_FMT_RGBA;
  465. break;
  466. case MKTAG('U', 'L', 'Y', '0'):
  467. c->planes = 3;
  468. avctx->pix_fmt = PIX_FMT_YUV420P;
  469. break;
  470. case MKTAG('U', 'L', 'Y', '2'):
  471. c->planes = 3;
  472. avctx->pix_fmt = PIX_FMT_YUV422P;
  473. break;
  474. default:
  475. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  476. avctx->codec_tag);
  477. return AVERROR_INVALIDDATA;
  478. }
  479. return 0;
  480. }
  481. static av_cold int decode_end(AVCodecContext *avctx)
  482. {
  483. UtvideoContext * const c = avctx->priv_data;
  484. if (c->pic.data[0])
  485. ff_thread_release_buffer(avctx, &c->pic);
  486. av_freep(&c->slice_bits);
  487. return 0;
  488. }
  489. AVCodec ff_utvideo_decoder = {
  490. .name = "utvideo",
  491. .type = AVMEDIA_TYPE_VIDEO,
  492. .id = AV_CODEC_ID_UTVIDEO,
  493. .priv_data_size = sizeof(UtvideoContext),
  494. .init = decode_init,
  495. .close = decode_end,
  496. .decode = decode_frame,
  497. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  498. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  499. };