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.

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