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.

546 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. ThreadFrame frame = { .f = data };
  302. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
  303. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  304. return ret;
  305. }
  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 %X\n", c->frame_info);
  338. c->frame_pred = (c->frame_info >> 8) & 3;
  339. if (c->frame_pred == PRED_GRADIENT) {
  340. av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
  341. return AVERROR_PATCHWELCOME;
  342. }
  343. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  344. max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
  345. if (!c->slice_bits) {
  346. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  347. return AVERROR(ENOMEM);
  348. }
  349. switch (c->avctx->pix_fmt) {
  350. case AV_PIX_FMT_RGB24:
  351. case AV_PIX_FMT_RGBA:
  352. for (i = 0; i < c->planes; i++) {
  353. ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
  354. c->planes, frame.f->linesize[0], avctx->width,
  355. avctx->height, plane_start[i],
  356. c->frame_pred == PRED_LEFT);
  357. if (ret)
  358. return ret;
  359. if (c->frame_pred == PRED_MEDIAN) {
  360. if (!c->interlaced) {
  361. restore_median(frame.f->data[0] + ff_ut_rgb_order[i],
  362. c->planes, frame.f->linesize[0], avctx->width,
  363. avctx->height, c->slices, 0);
  364. } else {
  365. restore_median_il(frame.f->data[0] + ff_ut_rgb_order[i],
  366. c->planes, frame.f->linesize[0],
  367. avctx->width, avctx->height, c->slices,
  368. 0);
  369. }
  370. }
  371. }
  372. restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
  373. avctx->width, avctx->height);
  374. break;
  375. case AV_PIX_FMT_YUV420P:
  376. for (i = 0; i < 3; i++) {
  377. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  378. avctx->width >> !!i, avctx->height >> !!i,
  379. plane_start[i], c->frame_pred == PRED_LEFT);
  380. if (ret)
  381. return ret;
  382. if (c->frame_pred == PRED_MEDIAN) {
  383. if (!c->interlaced) {
  384. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  385. avctx->width >> !!i, avctx->height >> !!i,
  386. c->slices, !i);
  387. } else {
  388. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  389. avctx->width >> !!i,
  390. avctx->height >> !!i,
  391. c->slices, !i);
  392. }
  393. }
  394. }
  395. break;
  396. case AV_PIX_FMT_YUV422P:
  397. for (i = 0; i < 3; i++) {
  398. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  399. avctx->width >> !!i, avctx->height,
  400. plane_start[i], c->frame_pred == PRED_LEFT);
  401. if (ret)
  402. return ret;
  403. if (c->frame_pred == PRED_MEDIAN) {
  404. if (!c->interlaced) {
  405. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  406. avctx->width >> !!i, avctx->height,
  407. c->slices, 0);
  408. } else {
  409. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  410. avctx->width >> !!i, avctx->height,
  411. c->slices, 0);
  412. }
  413. }
  414. }
  415. break;
  416. }
  417. frame.f->key_frame = 1;
  418. frame.f->pict_type = AV_PICTURE_TYPE_I;
  419. frame.f->interlaced_frame = !!c->interlaced;
  420. *got_frame = 1;
  421. /* always report that the buffer was completely consumed */
  422. return buf_size;
  423. }
  424. static av_cold int decode_init(AVCodecContext *avctx)
  425. {
  426. UtvideoContext * const c = avctx->priv_data;
  427. c->avctx = avctx;
  428. ff_dsputil_init(&c->dsp, avctx);
  429. if (avctx->extradata_size < 16) {
  430. av_log(avctx, AV_LOG_ERROR,
  431. "Insufficient extradata size %d, should be at least 16\n",
  432. avctx->extradata_size);
  433. return AVERROR_INVALIDDATA;
  434. }
  435. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  436. avctx->extradata[3], avctx->extradata[2],
  437. avctx->extradata[1], avctx->extradata[0]);
  438. av_log(avctx, AV_LOG_DEBUG, "Original format %X\n",
  439. AV_RB32(avctx->extradata + 4));
  440. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  441. c->flags = AV_RL32(avctx->extradata + 12);
  442. if (c->frame_info_size != 4)
  443. av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
  444. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
  445. c->slices = (c->flags >> 24) + 1;
  446. c->compression = c->flags & 1;
  447. c->interlaced = c->flags & 0x800;
  448. c->slice_bits_size = 0;
  449. switch (avctx->codec_tag) {
  450. case MKTAG('U', 'L', 'R', 'G'):
  451. c->planes = 3;
  452. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  453. break;
  454. case MKTAG('U', 'L', 'R', 'A'):
  455. c->planes = 4;
  456. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  457. break;
  458. case MKTAG('U', 'L', 'Y', '0'):
  459. c->planes = 3;
  460. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  461. break;
  462. case MKTAG('U', 'L', 'Y', '2'):
  463. c->planes = 3;
  464. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  465. break;
  466. default:
  467. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  468. avctx->codec_tag);
  469. return AVERROR_INVALIDDATA;
  470. }
  471. return 0;
  472. }
  473. static av_cold int decode_end(AVCodecContext *avctx)
  474. {
  475. UtvideoContext * const c = avctx->priv_data;
  476. av_freep(&c->slice_bits);
  477. return 0;
  478. }
  479. AVCodec ff_utvideo_decoder = {
  480. .name = "utvideo",
  481. .type = AVMEDIA_TYPE_VIDEO,
  482. .id = AV_CODEC_ID_UTVIDEO,
  483. .priv_data_size = sizeof(UtvideoContext),
  484. .init = decode_init,
  485. .close = decode_end,
  486. .decode = decode_frame,
  487. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  488. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  489. };