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