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.

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