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.

554 lines
18KB

  1. /*
  2. * Ut Video decoder
  3. * Copyright (c) 2011 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (c->pic.data[0])
  302. ff_thread_release_buffer(avctx, &c->pic);
  303. c->pic.reference = 3;
  304. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  305. if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
  306. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  307. return ret;
  308. }
  309. /* parse plane structure to get frame flags and validate slice offsets */
  310. bytestream2_init(&gb, buf, buf_size);
  311. for (i = 0; i < c->planes; i++) {
  312. plane_start[i] = gb.buffer;
  313. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  314. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  315. return AVERROR_INVALIDDATA;
  316. }
  317. bytestream2_skipu(&gb, 256);
  318. slice_start = 0;
  319. slice_end = 0;
  320. for (j = 0; j < c->slices; j++) {
  321. slice_end = bytestream2_get_le32u(&gb);
  322. slice_size = slice_end - slice_start;
  323. if (slice_end < 0 || slice_size < 0 ||
  324. bytestream2_get_bytes_left(&gb) < slice_end) {
  325. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  326. return AVERROR_INVALIDDATA;
  327. }
  328. slice_start = slice_end;
  329. max_slice_size = FFMAX(max_slice_size, slice_size);
  330. }
  331. plane_size = slice_end;
  332. bytestream2_skipu(&gb, plane_size);
  333. }
  334. plane_start[c->planes] = gb.buffer;
  335. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  336. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  337. return AVERROR_INVALIDDATA;
  338. }
  339. c->frame_info = bytestream2_get_le32u(&gb);
  340. av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
  341. c->frame_pred = (c->frame_info >> 8) & 3;
  342. if (c->frame_pred == PRED_GRADIENT) {
  343. av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
  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, c->pic.data[0] + ff_ut_rgb_order[i],
  357. c->planes, c->pic.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(c->pic.data[0] + ff_ut_rgb_order[i],
  365. c->planes, c->pic.linesize[0], avctx->width,
  366. avctx->height, c->slices, 0);
  367. } else {
  368. restore_median_il(c->pic.data[0] + ff_ut_rgb_order[i],
  369. c->planes, c->pic.linesize[0],
  370. avctx->width, avctx->height, c->slices,
  371. 0);
  372. }
  373. }
  374. }
  375. restore_rgb_planes(c->pic.data[0], c->planes, c->pic.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, c->pic.data[i], 1, c->pic.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(c->pic.data[i], 1, c->pic.linesize[i],
  388. avctx->width >> !!i, avctx->height >> !!i,
  389. c->slices, !i);
  390. } else {
  391. restore_median_il(c->pic.data[i], 1, c->pic.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, c->pic.data[i], 1, c->pic.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(c->pic.data[i], 1, c->pic.linesize[i],
  409. avctx->width >> !!i, avctx->height,
  410. c->slices, 0);
  411. } else {
  412. restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
  413. avctx->width >> !!i, avctx->height,
  414. c->slices, 0);
  415. }
  416. }
  417. }
  418. break;
  419. }
  420. c->pic.key_frame = 1;
  421. c->pic.pict_type = AV_PICTURE_TYPE_I;
  422. c->pic.interlaced_frame = !!c->interlaced;
  423. *got_frame = 1;
  424. *(AVFrame*)data = c->pic;
  425. /* always report that the buffer was completely consumed */
  426. return buf_size;
  427. }
  428. static av_cold int decode_init(AVCodecContext *avctx)
  429. {
  430. UtvideoContext * const c = avctx->priv_data;
  431. c->avctx = avctx;
  432. ff_dsputil_init(&c->dsp, avctx);
  433. if (avctx->extradata_size < 16) {
  434. av_log(avctx, AV_LOG_ERROR,
  435. "Insufficient extradata size %d, should be at least 16\n",
  436. avctx->extradata_size);
  437. return AVERROR_INVALIDDATA;
  438. }
  439. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  440. avctx->extradata[3], avctx->extradata[2],
  441. avctx->extradata[1], avctx->extradata[0]);
  442. av_log(avctx, AV_LOG_DEBUG, "Original format %X\n",
  443. AV_RB32(avctx->extradata + 4));
  444. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  445. c->flags = AV_RL32(avctx->extradata + 12);
  446. if (c->frame_info_size != 4)
  447. av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
  448. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
  449. c->slices = (c->flags >> 24) + 1;
  450. c->compression = c->flags & 1;
  451. c->interlaced = c->flags & 0x800;
  452. c->slice_bits_size = 0;
  453. switch (avctx->codec_tag) {
  454. case MKTAG('U', 'L', 'R', 'G'):
  455. c->planes = 3;
  456. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  457. break;
  458. case MKTAG('U', 'L', 'R', 'A'):
  459. c->planes = 4;
  460. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  461. break;
  462. case MKTAG('U', 'L', 'Y', '0'):
  463. c->planes = 3;
  464. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  465. break;
  466. case MKTAG('U', 'L', 'Y', '2'):
  467. c->planes = 3;
  468. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  469. break;
  470. default:
  471. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  472. avctx->codec_tag);
  473. return AVERROR_INVALIDDATA;
  474. }
  475. return 0;
  476. }
  477. static av_cold int decode_end(AVCodecContext *avctx)
  478. {
  479. UtvideoContext * const c = avctx->priv_data;
  480. if (c->pic.data[0])
  481. ff_thread_release_buffer(avctx, &c->pic);
  482. av_freep(&c->slice_bits);
  483. return 0;
  484. }
  485. AVCodec ff_utvideo_decoder = {
  486. .name = "utvideo",
  487. .type = AVMEDIA_TYPE_VIDEO,
  488. .id = AV_CODEC_ID_UTVIDEO,
  489. .priv_data_size = sizeof(UtvideoContext),
  490. .init = decode_init,
  491. .close = decode_end,
  492. .decode = decode_frame,
  493. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  494. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  495. };