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.

564 lines
19KB

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