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.

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