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.

461 lines
14KB

  1. /*
  2. * Ut Video decoder
  3. * Copyright (c) 2011 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. enum {
  32. PRED_NONE = 0,
  33. PRED_LEFT,
  34. PRED_GRADIENT,
  35. PRED_MEDIAN,
  36. };
  37. typedef struct UtvideoContext {
  38. AVCodecContext *avctx;
  39. AVFrame pic;
  40. DSPContext dsp;
  41. uint32_t frame_info_size, flags, frame_info;
  42. int planes;
  43. int slices;
  44. int compression;
  45. int interlaced;
  46. int frame_pred;
  47. uint8_t *slice_bits;
  48. int slice_bits_size;
  49. } UtvideoContext;
  50. typedef struct HuffEntry {
  51. uint8_t sym;
  52. uint8_t len;
  53. } HuffEntry;
  54. static int huff_cmp(const void *a, const void *b)
  55. {
  56. const HuffEntry *aa = a, *bb = b;
  57. return (aa->len - bb->len)*256 + aa->sym - bb->sym;
  58. }
  59. static int build_huff(const uint8_t *src, VLC *vlc)
  60. {
  61. int i;
  62. HuffEntry he[256];
  63. int last;
  64. uint32_t codes[256];
  65. uint8_t bits[256];
  66. uint8_t syms[256];
  67. uint32_t code;
  68. for (i = 0; i < 256; i++) {
  69. he[i].sym = i;
  70. he[i].len = *src++;
  71. }
  72. qsort(he, 256, sizeof(*he), huff_cmp);
  73. if (!he[0].len || he[0].len > 32)
  74. return -1;
  75. last = 255;
  76. while (he[last].len == 255 && last)
  77. last--;
  78. code = 1;
  79. for (i = last; i >= 0; i--) {
  80. codes[i] = code >> (32 - he[i].len);
  81. bits[i] = he[i].len;
  82. syms[i] = he[i].sym;
  83. code += 0x80000000u >> (he[i].len - 1);
  84. }
  85. return init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
  86. bits, sizeof(*bits), sizeof(*bits),
  87. codes, sizeof(*codes), sizeof(*codes),
  88. syms, sizeof(*syms), sizeof(*syms), 0);
  89. }
  90. static int decode_plane(UtvideoContext *c, int plane_no,
  91. uint8_t *dst, int step, int stride,
  92. int width, int height,
  93. const uint8_t *src, int src_size, int use_pred)
  94. {
  95. int i, j, slice, pix;
  96. int sstart, send;
  97. VLC vlc;
  98. GetBitContext gb;
  99. int prev;
  100. if (build_huff(src, &vlc)) {
  101. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. src += 256;
  105. src_size -= 256;
  106. send = 0;
  107. for (slice = 0; slice < c->slices; slice++) {
  108. uint8_t *dest;
  109. int slice_data_start, slice_data_end, slice_size;
  110. sstart = send;
  111. send = height * (slice + 1) / c->slices;
  112. dest = dst + sstart * stride;
  113. // slice offset and size validation was done earlier
  114. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  115. slice_data_end = AV_RL32(src + slice * 4);
  116. slice_size = slice_data_end - slice_data_start;
  117. if (!slice_size) {
  118. for (j = sstart; j < send; j++) {
  119. for (i = 0; i < width * step; i += step)
  120. dest[i] = 0x80;
  121. dest += stride;
  122. }
  123. continue;
  124. }
  125. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4, slice_size);
  126. memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  127. c->dsp.bswap_buf((uint32_t*)c->slice_bits, (uint32_t*)c->slice_bits,
  128. (slice_data_end - slice_data_start + 3) >> 2);
  129. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  130. prev = 0x80;
  131. for (j = sstart; j < send; j++) {
  132. for (i = 0; i < width * step; i += step) {
  133. if (get_bits_left(&gb) <= 0) {
  134. av_log(c->avctx, AV_LOG_ERROR, "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, "%d bits left after decoding slice\n",
  152. get_bits_left(&gb));
  153. }
  154. free_vlc(&vlc);
  155. return 0;
  156. fail:
  157. free_vlc(&vlc);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. static const int rgb_order[4] = { 1, 2, 0, 3 };
  161. static void restore_rgb_planes(uint8_t *src, int step, int stride, int width, 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)
  178. {
  179. int i, j, slice;
  180. int A, B, C;
  181. uint8_t *bsrc;
  182. int slice_start, slice_height;
  183. for (slice = 0; slice < slices; slice++) {
  184. slice_start = (slice * height) / slices;
  185. slice_height = ((slice + 1) * height) / slices - slice_start;
  186. bsrc = src + slice_start * stride;
  187. // first line - left neighbour prediction
  188. bsrc[0] += 0x80;
  189. A = bsrc[0];
  190. for (i = step; i < width * step; i += step) {
  191. bsrc[i] += A;
  192. A = bsrc[i];
  193. }
  194. bsrc += stride;
  195. if (slice_height == 1)
  196. continue;
  197. // second line - first element has top predition, the rest uses median
  198. C = bsrc[-stride];
  199. bsrc[0] += C;
  200. A = bsrc[0];
  201. for (i = step; i < width * step; i += step) {
  202. B = bsrc[i - stride];
  203. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  204. C = B;
  205. A = bsrc[i];
  206. }
  207. bsrc += stride;
  208. // the rest of lines use continuous median prediction
  209. for (j = 2; j < slice_height; j++) {
  210. for (i = 0; i < width * step; i += step) {
  211. B = bsrc[i - stride];
  212. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  213. C = B;
  214. A = bsrc[i];
  215. }
  216. bsrc += stride;
  217. }
  218. }
  219. }
  220. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  221. {
  222. const uint8_t *buf = avpkt->data;
  223. int buf_size = avpkt->size;
  224. const uint8_t *buf_end = buf + buf_size;
  225. UtvideoContext *c = avctx->priv_data;
  226. const uint8_t *ptr;
  227. int i, j;
  228. const uint8_t *plane_start[5];
  229. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  230. int ret;
  231. if (c->pic.data[0])
  232. avctx->release_buffer(avctx, &c->pic);
  233. c->pic.reference = 1;
  234. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  235. if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
  236. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  237. return ret;
  238. }
  239. /* parse plane structure to retrieve frame flags and validate slice offsets */
  240. ptr = buf;
  241. for (i = 0; i < c->planes; i++) {
  242. plane_start[i] = ptr;
  243. if (buf_end - ptr < 256 + 4 * c->slices) {
  244. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  245. return AVERROR_INVALIDDATA;
  246. }
  247. ptr += 256;
  248. slice_start = 0;
  249. slice_end = 0;
  250. for (j = 0; j < c->slices; j++) {
  251. slice_end = bytestream_get_le32(&ptr);
  252. slice_size = slice_end - slice_start;
  253. if (slice_size < 0) {
  254. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  255. return AVERROR_INVALIDDATA;
  256. }
  257. slice_start = slice_end;
  258. max_slice_size = FFMAX(max_slice_size, slice_size);
  259. }
  260. plane_size = slice_end;
  261. if (buf_end - ptr < plane_size) {
  262. av_log(avctx, AV_LOG_ERROR, "Plane size is bigger than available data\n");
  263. return AVERROR_INVALIDDATA;
  264. }
  265. ptr += plane_size;
  266. }
  267. plane_start[c->planes] = ptr;
  268. if (buf_end - ptr < c->frame_info_size) {
  269. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  270. return AVERROR_INVALIDDATA;
  271. }
  272. c->frame_info = AV_RL32(ptr);
  273. av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
  274. c->frame_pred = (c->frame_info >> 8) & 3;
  275. if (c->frame_pred == PRED_GRADIENT) {
  276. av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
  277. return AVERROR_PATCHWELCOME;
  278. }
  279. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  280. max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
  281. if (!c->slice_bits) {
  282. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  283. return AVERROR(ENOMEM);
  284. }
  285. switch (c->avctx->pix_fmt) {
  286. case PIX_FMT_RGB24:
  287. case PIX_FMT_RGBA:
  288. for (i = 0; i < c->planes; i++) {
  289. ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
  290. c->pic.linesize[0], avctx->width, avctx->height,
  291. plane_start[i], plane_start[i + 1] - plane_start[i],
  292. c->frame_pred == PRED_LEFT);
  293. if (ret)
  294. return ret;
  295. if (c->frame_pred == PRED_MEDIAN)
  296. restore_median(c->pic.data[0] + rgb_order[i], c->planes,
  297. c->pic.linesize[0], avctx->width, avctx->height,
  298. c->slices);
  299. }
  300. restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
  301. avctx->width, avctx->height);
  302. break;
  303. case PIX_FMT_YUV420P:
  304. for (i = 0; i < 3; i++) {
  305. ret = decode_plane(c, i, c->pic.data[i], 1,
  306. c->pic.linesize[i], avctx->width >> !!i, avctx->height >> !!i,
  307. plane_start[i], plane_start[i + 1] - plane_start[i],
  308. c->frame_pred == PRED_LEFT);
  309. if (ret)
  310. return ret;
  311. if (c->frame_pred == PRED_MEDIAN)
  312. restore_median(c->pic.data[i], 1, c->pic.linesize[i],
  313. avctx->width >> !!i, avctx->height >> !!i,
  314. c->slices);
  315. }
  316. break;
  317. case PIX_FMT_YUV422P:
  318. for (i = 0; i < 3; i++) {
  319. ret = decode_plane(c, i, c->pic.data[i], 1,
  320. c->pic.linesize[i], avctx->width >> !!i, avctx->height,
  321. plane_start[i], plane_start[i + 1] - plane_start[i],
  322. c->frame_pred == PRED_LEFT);
  323. if (ret)
  324. return ret;
  325. if (c->frame_pred == PRED_MEDIAN)
  326. restore_median(c->pic.data[i], 1, c->pic.linesize[i],
  327. avctx->width >> !!i, avctx->height, c->slices);
  328. }
  329. break;
  330. }
  331. *data_size = sizeof(AVFrame);
  332. *(AVFrame*)data = c->pic;
  333. /* always report that the buffer was completely consumed */
  334. return buf_size;
  335. }
  336. static av_cold int decode_init(AVCodecContext *avctx)
  337. {
  338. UtvideoContext * const c = avctx->priv_data;
  339. c->avctx = avctx;
  340. dsputil_init(&c->dsp, avctx);
  341. if (avctx->extradata_size < 16) {
  342. av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d, should be at least 16\n",
  343. avctx->extradata_size);
  344. return AVERROR_INVALIDDATA;
  345. }
  346. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  347. avctx->extradata[3], avctx->extradata[2],
  348. avctx->extradata[1], avctx->extradata[0]);
  349. av_log(avctx, AV_LOG_DEBUG, "Original format %X\n", AV_RB32(avctx->extradata + 4));
  350. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  351. c->flags = AV_RL32(avctx->extradata + 12);
  352. if (c->frame_info_size != 4)
  353. av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
  354. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
  355. c->slices = (c->flags >> 24) + 1;
  356. c->compression = c->flags & 1;
  357. c->interlaced = c->flags & 0x800;
  358. c->slice_bits_size = 0;
  359. switch (avctx->codec_tag) {
  360. case MKTAG('U', 'L', 'R', 'G'):
  361. c->planes = 3;
  362. avctx->pix_fmt = PIX_FMT_RGB24;
  363. break;
  364. case MKTAG('U', 'L', 'R', 'A'):
  365. c->planes = 4;
  366. avctx->pix_fmt = PIX_FMT_RGBA;
  367. break;
  368. case MKTAG('U', 'L', 'Y', '0'):
  369. c->planes = 3;
  370. avctx->pix_fmt = PIX_FMT_YUV420P;
  371. break;
  372. case MKTAG('U', 'L', 'Y', '2'):
  373. c->planes = 3;
  374. avctx->pix_fmt = PIX_FMT_YUV422P;
  375. break;
  376. default:
  377. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  378. avctx->codec_tag);
  379. return AVERROR_INVALIDDATA;
  380. }
  381. return 0;
  382. }
  383. static av_cold int decode_end(AVCodecContext *avctx)
  384. {
  385. UtvideoContext * const c = avctx->priv_data;
  386. if (c->pic.data[0])
  387. avctx->release_buffer(avctx, &c->pic);
  388. av_freep(&c->slice_bits);
  389. return 0;
  390. }
  391. AVCodec ff_utvideo_decoder = {
  392. .name = "utvideo",
  393. .type = AVMEDIA_TYPE_VIDEO,
  394. .id = CODEC_ID_UTVIDEO,
  395. .priv_data_size = sizeof(UtvideoContext),
  396. .init = decode_init,
  397. .close = decode_end,
  398. .decode = decode_frame,
  399. .capabilities = CODEC_CAP_DR1,
  400. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  401. };