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.

1192 lines
41KB

  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 <inttypes.h>
  26. #include <stdlib.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "avcodec.h"
  29. #include "bitstream.h"
  30. #include "bswapdsp.h"
  31. #include "bytestream.h"
  32. #include "internal.h"
  33. #include "thread.h"
  34. #include "utvideo.h"
  35. #include "vlc.h"
  36. static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
  37. {
  38. int i;
  39. HuffEntry he[1024];
  40. int last;
  41. uint32_t codes[1024];
  42. uint8_t bits[1024];
  43. uint16_t syms[1024];
  44. uint32_t code;
  45. *fsym = -1;
  46. for (i = 0; i < 1024; i++) {
  47. he[i].sym = i;
  48. he[i].len = *src++;
  49. }
  50. qsort(he, 1024, sizeof(*he), ff_ut10_huff_cmp_len);
  51. if (!he[0].len) {
  52. *fsym = he[0].sym;
  53. return 0;
  54. }
  55. last = 1023;
  56. while (he[last].len == 255 && last)
  57. last--;
  58. if (he[last].len > 32) {
  59. return -1;
  60. }
  61. code = 1;
  62. for (i = last; i >= 0; i--) {
  63. codes[i] = code >> (32 - he[i].len);
  64. bits[i] = he[i].len;
  65. syms[i] = he[i].sym;
  66. code += 0x80000000u >> (he[i].len - 1);
  67. }
  68. return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
  69. bits, sizeof(*bits), sizeof(*bits),
  70. codes, sizeof(*codes), sizeof(*codes),
  71. syms, sizeof(*syms), sizeof(*syms), 0);
  72. }
  73. static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
  74. {
  75. int i;
  76. HuffEntry he[256];
  77. int last;
  78. uint32_t codes[256];
  79. uint8_t bits[256];
  80. uint8_t syms[256];
  81. uint32_t code;
  82. *fsym = -1;
  83. for (i = 0; i < 256; i++) {
  84. he[i].sym = i;
  85. he[i].len = *src++;
  86. }
  87. qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
  88. if (!he[0].len) {
  89. *fsym = he[0].sym;
  90. return 0;
  91. }
  92. if (he[0].len > 32)
  93. return -1;
  94. last = 255;
  95. while (he[last].len == 255 && last)
  96. last--;
  97. code = 1;
  98. for (i = last; i >= 0; i--) {
  99. codes[i] = code >> (32 - he[i].len);
  100. bits[i] = he[i].len;
  101. syms[i] = he[i].sym;
  102. code += 0x80000000u >> (he[i].len - 1);
  103. }
  104. return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
  105. bits, sizeof(*bits), sizeof(*bits),
  106. codes, sizeof(*codes), sizeof(*codes),
  107. syms, sizeof(*syms), sizeof(*syms), 0);
  108. }
  109. static int decode_plane10(UtvideoContext *c, int plane_no,
  110. uint16_t *dst, int step, int stride,
  111. int width, int height,
  112. const uint8_t *src, const uint8_t *huff,
  113. int use_pred)
  114. {
  115. BitstreamContext bc;
  116. int i, j, slice, pix, ret;
  117. int sstart, send;
  118. VLC vlc;
  119. int prev, fsym;
  120. if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
  121. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  122. return ret;
  123. }
  124. if (fsym >= 0) { // build_huff reported a symbol to fill slices with
  125. send = 0;
  126. for (slice = 0; slice < c->slices; slice++) {
  127. uint16_t *dest;
  128. sstart = send;
  129. send = (height * (slice + 1) / c->slices);
  130. dest = dst + sstart * stride;
  131. prev = 0x200;
  132. for (j = sstart; j < send; j++) {
  133. for (i = 0; i < width * step; i += step) {
  134. pix = fsym;
  135. if (use_pred) {
  136. prev += pix;
  137. prev &= 0x3FF;
  138. pix = prev;
  139. }
  140. dest[i] = pix;
  141. }
  142. dest += stride;
  143. }
  144. }
  145. return 0;
  146. }
  147. send = 0;
  148. for (slice = 0; slice < c->slices; slice++) {
  149. uint16_t *dest;
  150. int slice_data_start, slice_data_end, slice_size;
  151. sstart = send;
  152. send = (height * (slice + 1) / c->slices);
  153. dest = dst + sstart * stride;
  154. // slice offset and size validation was done earlier
  155. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  156. slice_data_end = AV_RL32(src + slice * 4);
  157. slice_size = slice_data_end - slice_data_start;
  158. if (!slice_size) {
  159. av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
  160. "yet a slice has a length of zero.\n");
  161. goto fail;
  162. }
  163. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  164. slice_size);
  165. memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  166. c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
  167. (uint32_t *) c->slice_bits,
  168. (slice_data_end - slice_data_start + 3) >> 2);
  169. bitstream_init8(&bc, c->slice_bits, slice_size);
  170. prev = 0x200;
  171. for (j = sstart; j < send; j++) {
  172. for (i = 0; i < width * step; i += step) {
  173. if (bitstream_bits_left(&bc) <= 0) {
  174. av_log(c->avctx, AV_LOG_ERROR,
  175. "Slice decoding ran out of bits\n");
  176. goto fail;
  177. }
  178. pix = bitstream_read_vlc(&bc, vlc.table, vlc.bits, 3);
  179. if (pix < 0) {
  180. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  181. goto fail;
  182. }
  183. if (use_pred) {
  184. prev += pix;
  185. prev &= 0x3FF;
  186. pix = prev;
  187. }
  188. dest[i] = pix;
  189. }
  190. dest += stride;
  191. }
  192. if (bitstream_bits_left(&bc) > 32)
  193. av_log(c->avctx, AV_LOG_WARNING,
  194. "%d bits left after decoding slice\n", bitstream_bits_left(&bc));
  195. }
  196. ff_free_vlc(&vlc);
  197. return 0;
  198. fail:
  199. ff_free_vlc(&vlc);
  200. return AVERROR_INVALIDDATA;
  201. }
  202. static int compute_cmask(int plane_no, int interlaced, int pix_fmt)
  203. {
  204. const int is_luma = (pix_fmt == AV_PIX_FMT_YUV420P) && !plane_no;
  205. if (interlaced)
  206. return ~(1 + 2 * is_luma);
  207. return ~is_luma;
  208. }
  209. static int decode_plane(UtvideoContext *c, int plane_no,
  210. uint8_t *dst, int step, ptrdiff_t stride,
  211. int width, int height,
  212. const uint8_t *src, int use_pred)
  213. {
  214. int i, j, slice, pix;
  215. int sstart, send;
  216. VLC vlc;
  217. BitstreamContext bc;
  218. int prev, fsym;
  219. const int cmask = compute_cmask(plane_no, c->interlaced, c->avctx->pix_fmt);
  220. if (build_huff(src, &vlc, &fsym)) {
  221. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  222. return AVERROR_INVALIDDATA;
  223. }
  224. if (fsym >= 0) { // build_huff reported a symbol to fill slices with
  225. send = 0;
  226. for (slice = 0; slice < c->slices; slice++) {
  227. uint8_t *dest;
  228. sstart = send;
  229. send = (height * (slice + 1) / c->slices) & cmask;
  230. dest = dst + sstart * stride;
  231. prev = 0x80;
  232. for (j = sstart; j < send; j++) {
  233. for (i = 0; i < width * step; i += step) {
  234. pix = fsym;
  235. if (use_pred) {
  236. prev += pix;
  237. pix = prev;
  238. }
  239. dest[i] = pix;
  240. }
  241. dest += stride;
  242. }
  243. }
  244. return 0;
  245. }
  246. src += 256;
  247. send = 0;
  248. for (slice = 0; slice < c->slices; slice++) {
  249. uint8_t *dest;
  250. int slice_data_start, slice_data_end, slice_size;
  251. sstart = send;
  252. send = (height * (slice + 1) / c->slices) & cmask;
  253. dest = dst + sstart * stride;
  254. // slice offset and size validation was done earlier
  255. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  256. slice_data_end = AV_RL32(src + slice * 4);
  257. slice_size = slice_data_end - slice_data_start;
  258. if (!slice_size) {
  259. av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
  260. "yet a slice has a length of zero.\n");
  261. goto fail;
  262. }
  263. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  264. slice_size);
  265. memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  266. c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
  267. (uint32_t *) c->slice_bits,
  268. (slice_data_end - slice_data_start + 3) >> 2);
  269. bitstream_init8(&bc, c->slice_bits, slice_size);
  270. prev = 0x80;
  271. for (j = sstart; j < send; j++) {
  272. for (i = 0; i < width * step; i += step) {
  273. if (bitstream_bits_left(&bc) <= 0) {
  274. av_log(c->avctx, AV_LOG_ERROR,
  275. "Slice decoding ran out of bits\n");
  276. goto fail;
  277. }
  278. pix = bitstream_read_vlc(&bc, vlc.table, vlc.bits, 4);
  279. if (pix < 0) {
  280. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  281. goto fail;
  282. }
  283. if (use_pred) {
  284. prev += pix;
  285. pix = prev;
  286. }
  287. dest[i] = pix;
  288. }
  289. dest += stride;
  290. }
  291. if (bitstream_bits_left(&bc) > 32)
  292. av_log(c->avctx, AV_LOG_WARNING,
  293. "%d bits left after decoding slice\n", bitstream_bits_left(&bc));
  294. }
  295. ff_free_vlc(&vlc);
  296. return 0;
  297. fail:
  298. ff_free_vlc(&vlc);
  299. return AVERROR_INVALIDDATA;
  300. }
  301. static void restore_rgb_planes(uint8_t *src, int step, ptrdiff_t stride,
  302. int width, int height)
  303. {
  304. int i, j;
  305. uint8_t r, g, b;
  306. for (j = 0; j < height; j++) {
  307. for (i = 0; i < width * step; i += step) {
  308. r = src[i];
  309. g = src[i + 1];
  310. b = src[i + 2];
  311. src[i] = r + g - 0x80;
  312. src[i + 2] = b + g - 0x80;
  313. }
  314. src += stride;
  315. }
  316. }
  317. static void restore_rgb_planes10(AVFrame *frame, int width, int height)
  318. {
  319. uint16_t *src_r = (uint16_t *)frame->data[2];
  320. uint16_t *src_g = (uint16_t *)frame->data[0];
  321. uint16_t *src_b = (uint16_t *)frame->data[1];
  322. int r, g, b;
  323. int i, j;
  324. for (j = 0; j < height; j++) {
  325. for (i = 0; i < width; i++) {
  326. r = src_r[i];
  327. g = src_g[i];
  328. b = src_b[i];
  329. src_r[i] = (r + g - 0x200) & 0x3FF;
  330. src_b[i] = (b + g - 0x200) & 0x3FF;
  331. }
  332. src_r += frame->linesize[2] / 2;
  333. src_g += frame->linesize[0] / 2;
  334. src_b += frame->linesize[1] / 2;
  335. }
  336. }
  337. static void restore_median_planar(UtvideoContext *c, uint8_t *src,
  338. ptrdiff_t stride, int width, int height,
  339. int slices, int rmode)
  340. {
  341. int i, j, slice;
  342. int A, B, C;
  343. uint8_t *bsrc;
  344. int slice_start, slice_height;
  345. const int cmask = ~rmode;
  346. for (slice = 0; slice < slices; slice++) {
  347. slice_start = ((slice * height) / slices) & cmask;
  348. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  349. slice_start;
  350. if (!slice_height)
  351. continue;
  352. bsrc = src + slice_start * stride;
  353. // first line - left neighbour prediction
  354. bsrc[0] += 0x80;
  355. c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
  356. bsrc += stride;
  357. if (slice_height <= 1)
  358. continue;
  359. // second line - first element has top prediction, the rest uses median
  360. C = bsrc[-stride];
  361. bsrc[0] += C;
  362. A = bsrc[0];
  363. for (i = 1; i < width; i++) {
  364. B = bsrc[i - stride];
  365. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  366. C = B;
  367. A = bsrc[i];
  368. }
  369. bsrc += stride;
  370. // the rest of lines use continuous median prediction
  371. for (j = 2; j < slice_height; j++) {
  372. c->hdspdec.add_hfyu_median_pred(bsrc, bsrc - stride,
  373. bsrc, width, &A, &B);
  374. bsrc += stride;
  375. }
  376. }
  377. }
  378. /* UtVideo interlaced mode treats every two lines as a single one,
  379. * so restoring function should take care of possible padding between
  380. * two parts of the same "line".
  381. */
  382. static void restore_median_planar_il(UtvideoContext *c, uint8_t *src,
  383. ptrdiff_t stride, int width, int height,
  384. int slices, int rmode)
  385. {
  386. int i, j, slice;
  387. int A, B, C;
  388. uint8_t *bsrc;
  389. int slice_start, slice_height;
  390. const int cmask = ~(rmode ? 3 : 1);
  391. const int stride2 = stride << 1;
  392. for (slice = 0; slice < slices; slice++) {
  393. slice_start = ((slice * height) / slices) & cmask;
  394. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  395. slice_start;
  396. slice_height >>= 1;
  397. if (!slice_height)
  398. continue;
  399. bsrc = src + slice_start * stride;
  400. // first line - left neighbour prediction
  401. bsrc[0] += 0x80;
  402. A = c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
  403. c->hdspdec.add_hfyu_left_pred(bsrc + stride, bsrc + stride, width, A);
  404. bsrc += stride2;
  405. if (slice_height <= 1)
  406. continue;
  407. // second line - first element has top prediction, the rest uses median
  408. C = bsrc[-stride2];
  409. bsrc[0] += C;
  410. A = bsrc[0];
  411. for (i = 1; i < width; i++) {
  412. B = bsrc[i - stride2];
  413. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  414. C = B;
  415. A = bsrc[i];
  416. }
  417. c->hdspdec.add_hfyu_median_pred(bsrc + stride, bsrc - stride,
  418. bsrc + stride, width, &A, &B);
  419. bsrc += stride2;
  420. // the rest of lines use continuous median prediction
  421. for (j = 2; j < slice_height; j++) {
  422. c->hdspdec.add_hfyu_median_pred(bsrc, bsrc - stride2,
  423. bsrc, width, &A, &B);
  424. c->hdspdec.add_hfyu_median_pred(bsrc + stride, bsrc - stride,
  425. bsrc + stride, width, &A, &B);
  426. bsrc += stride2;
  427. }
  428. }
  429. }
  430. static void restore_median_packed(uint8_t *src, int step, ptrdiff_t stride,
  431. int width, int height,
  432. int slices, int rmode)
  433. {
  434. int i, j, slice;
  435. int A, B, C;
  436. uint8_t *bsrc;
  437. int slice_start, slice_height;
  438. const int cmask = ~rmode;
  439. for (slice = 0; slice < slices; slice++) {
  440. slice_start = ((slice * height) / slices) & cmask;
  441. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  442. slice_start;
  443. if (!slice_height)
  444. continue;
  445. bsrc = src + slice_start * stride;
  446. // first line - left neighbour prediction
  447. bsrc[0] += 0x80;
  448. A = bsrc[0];
  449. for (i = step; i < width * step; i += step) {
  450. bsrc[i] += A;
  451. A = bsrc[i];
  452. }
  453. bsrc += stride;
  454. if (slice_height == 1)
  455. continue;
  456. // second line - first element has top prediction, the rest uses median
  457. C = bsrc[-stride];
  458. bsrc[0] += C;
  459. A = bsrc[0];
  460. for (i = step; i < width * step; i += step) {
  461. B = bsrc[i - stride];
  462. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  463. C = B;
  464. A = bsrc[i];
  465. }
  466. bsrc += stride;
  467. // the rest of lines use continuous median prediction
  468. for (j = 2; j < slice_height; j++) {
  469. for (i = 0; i < width * step; i += step) {
  470. B = bsrc[i - stride];
  471. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  472. C = B;
  473. A = bsrc[i];
  474. }
  475. bsrc += stride;
  476. }
  477. }
  478. }
  479. /* UtVideo interlaced mode treats every two lines as a single one,
  480. * so restoring function should take care of possible padding between
  481. * two parts of the same "line".
  482. */
  483. static void restore_median_packed_il(uint8_t *src, int step, ptrdiff_t stride,
  484. int width, int height,
  485. int slices, int rmode)
  486. {
  487. int i, j, slice;
  488. int A, B, C;
  489. uint8_t *bsrc;
  490. int slice_start, slice_height;
  491. const int cmask = ~(rmode ? 3 : 1);
  492. const ptrdiff_t stride2 = stride << 1;
  493. for (slice = 0; slice < slices; slice++) {
  494. slice_start = ((slice * height) / slices) & cmask;
  495. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  496. slice_start;
  497. slice_height >>= 1;
  498. if (!slice_height)
  499. continue;
  500. bsrc = src + slice_start * stride;
  501. // first line - left neighbour prediction
  502. bsrc[0] += 0x80;
  503. A = bsrc[0];
  504. for (i = step; i < width * step; i += step) {
  505. bsrc[i] += A;
  506. A = bsrc[i];
  507. }
  508. for (i = 0; i < width * step; i += step) {
  509. bsrc[stride + i] += A;
  510. A = bsrc[stride + i];
  511. }
  512. bsrc += stride2;
  513. if (slice_height == 1)
  514. continue;
  515. // second line - first element has top prediction, the rest uses median
  516. C = bsrc[-stride2];
  517. bsrc[0] += C;
  518. A = bsrc[0];
  519. for (i = step; i < width * step; i += step) {
  520. B = bsrc[i - stride2];
  521. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  522. C = B;
  523. A = bsrc[i];
  524. }
  525. for (i = 0; i < width * step; i += step) {
  526. B = bsrc[i - stride];
  527. bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
  528. C = B;
  529. A = bsrc[stride + i];
  530. }
  531. bsrc += stride2;
  532. // the rest of lines use continuous median prediction
  533. for (j = 2; j < slice_height; j++) {
  534. for (i = 0; i < width * step; i += step) {
  535. B = bsrc[i - stride2];
  536. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  537. C = B;
  538. A = bsrc[i];
  539. }
  540. for (i = 0; i < width * step; i += step) {
  541. B = bsrc[i - stride];
  542. bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
  543. C = B;
  544. A = bsrc[i + stride];
  545. }
  546. bsrc += stride2;
  547. }
  548. }
  549. }
  550. static void restore_gradient_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  551. int width, int height, int slices, int rmode)
  552. {
  553. int i, j, slice;
  554. int A, B, C;
  555. uint8_t *bsrc;
  556. int slice_start, slice_height;
  557. const int cmask = ~rmode;
  558. for (slice = 0; slice < slices; slice++) {
  559. slice_start = ((slice * height) / slices) & cmask;
  560. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  561. slice_start;
  562. if (!slice_height)
  563. continue;
  564. bsrc = src + slice_start * stride;
  565. // first line - left neighbour prediction
  566. bsrc[0] += 0x80;
  567. c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
  568. bsrc += stride;
  569. if (slice_height <= 1)
  570. continue;
  571. for (j = 1; j < slice_height; j++) {
  572. // second line - first element has top prediction, the rest uses gradient
  573. bsrc[0] = (bsrc[0] + bsrc[-stride]) & 0xFF;
  574. for (i = 1; i < width; i++) {
  575. A = bsrc[i - stride];
  576. B = bsrc[i - (stride + 1)];
  577. C = bsrc[i - 1];
  578. bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
  579. }
  580. bsrc += stride;
  581. }
  582. }
  583. }
  584. static void restore_gradient_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  585. int width, int height, int slices, int rmode)
  586. {
  587. int i, j, slice;
  588. int A, B, C;
  589. uint8_t *bsrc;
  590. int slice_start, slice_height;
  591. const int cmask = ~(rmode ? 3 : 1);
  592. const ptrdiff_t stride2 = stride << 1;
  593. for (slice = 0; slice < slices; slice++) {
  594. slice_start = ((slice * height) / slices) & cmask;
  595. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  596. slice_start;
  597. slice_height >>= 1;
  598. if (!slice_height)
  599. continue;
  600. bsrc = src + slice_start * stride;
  601. // first line - left neighbour prediction
  602. bsrc[0] += 0x80;
  603. A = c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
  604. c->hdspdec.add_hfyu_left_pred(bsrc + stride, bsrc + stride, width, A);
  605. bsrc += stride2;
  606. if (slice_height <= 1)
  607. continue;
  608. for (j = 1; j < slice_height; j++) {
  609. // second line - first element has top prediction, the rest uses gradient
  610. bsrc[0] = (bsrc[0] + bsrc[-stride2]) & 0xFF;
  611. for (i = 1; i < width; i++) {
  612. A = bsrc[i - stride2];
  613. B = bsrc[i - (stride2 + 1)];
  614. C = bsrc[i - 1];
  615. bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
  616. }
  617. A = bsrc[-stride];
  618. B = bsrc[-(1 + stride + stride - width)];
  619. C = bsrc[width - 1];
  620. bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
  621. for (i = 1; i < width; i++) {
  622. A = bsrc[i - stride];
  623. B = bsrc[i - (1 + stride)];
  624. C = bsrc[i - 1 + stride];
  625. bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
  626. }
  627. bsrc += stride2;
  628. }
  629. }
  630. }
  631. static void restore_gradient_packed(uint8_t *src, int step, ptrdiff_t stride,
  632. int width, int height, int slices, int rmode)
  633. {
  634. int i, j, slice;
  635. int A, B, C;
  636. uint8_t *bsrc;
  637. int slice_start, slice_height;
  638. const int cmask = ~rmode;
  639. for (slice = 0; slice < slices; slice++) {
  640. slice_start = ((slice * height) / slices) & cmask;
  641. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  642. slice_start;
  643. if (!slice_height)
  644. continue;
  645. bsrc = src + slice_start * stride;
  646. // first line - left neighbour prediction
  647. bsrc[0] += 0x80;
  648. A = bsrc[0];
  649. for (i = step; i < width * step; i += step) {
  650. bsrc[i] += A;
  651. A = bsrc[i];
  652. }
  653. bsrc += stride;
  654. if (slice_height <= 1)
  655. continue;
  656. for (j = 1; j < slice_height; j++) {
  657. // second line - first element has top prediction, the rest uses gradient
  658. C = bsrc[-stride];
  659. bsrc[0] += C;
  660. for (i = step; i < width * step; i += step) {
  661. A = bsrc[i - stride];
  662. B = bsrc[i - (stride + step)];
  663. C = bsrc[i - step];
  664. bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
  665. }
  666. bsrc += stride;
  667. }
  668. }
  669. }
  670. static void restore_gradient_packed_il(uint8_t *src, int step, ptrdiff_t stride,
  671. int width, int height, int slices, int rmode)
  672. {
  673. int i, j, slice;
  674. int A, B, C;
  675. uint8_t *bsrc;
  676. int slice_start, slice_height;
  677. const int cmask = ~(rmode ? 3 : 1);
  678. const ptrdiff_t stride2 = stride << 1;
  679. for (slice = 0; slice < slices; slice++) {
  680. slice_start = ((slice * height) / slices) & cmask;
  681. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  682. slice_start;
  683. slice_height >>= 1;
  684. if (!slice_height)
  685. continue;
  686. bsrc = src + slice_start * stride;
  687. // first line - left neighbour prediction
  688. bsrc[0] += 0x80;
  689. A = bsrc[0];
  690. for (i = step; i < width * step; i += step) {
  691. bsrc[i] += A;
  692. A = bsrc[i];
  693. }
  694. for (i = 0; i < width * step; i += step) {
  695. bsrc[stride + i] += A;
  696. A = bsrc[stride + i];
  697. }
  698. bsrc += stride2;
  699. if (slice_height <= 1)
  700. continue;
  701. for (j = 1; j < slice_height; j++) {
  702. // second line - first element has top prediction, the rest uses gradient
  703. C = bsrc[-stride2];
  704. bsrc[0] += C;
  705. for (i = step; i < width * step; i += step) {
  706. A = bsrc[i - stride2];
  707. B = bsrc[i - (stride2 + step)];
  708. C = bsrc[i - step];
  709. bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
  710. }
  711. A = bsrc[-stride];
  712. B = bsrc[-(step + stride + stride - width * step)];
  713. C = bsrc[width * step - step];
  714. bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
  715. for (i = step; i < width * step; i += step) {
  716. A = bsrc[i - stride];
  717. B = bsrc[i - (step + stride)];
  718. C = bsrc[i - step + stride];
  719. bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
  720. }
  721. bsrc += stride2;
  722. }
  723. }
  724. }
  725. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  726. AVPacket *avpkt)
  727. {
  728. const uint8_t *buf = avpkt->data;
  729. int buf_size = avpkt->size;
  730. UtvideoContext *c = avctx->priv_data;
  731. int i, j;
  732. const uint8_t *plane_start[5];
  733. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  734. int ret;
  735. GetByteContext gb;
  736. ThreadFrame frame = { .f = data };
  737. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
  738. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  739. return ret;
  740. }
  741. ff_thread_finish_setup(avctx);
  742. /* parse plane structure to get frame flags and validate slice offsets */
  743. bytestream2_init(&gb, buf, buf_size);
  744. if (c->pro) {
  745. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  746. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  747. return AVERROR_INVALIDDATA;
  748. }
  749. c->frame_info = bytestream2_get_le32u(&gb);
  750. c->slices = ((c->frame_info >> 16) & 0xff) + 1;
  751. for (i = 0; i < c->planes; i++) {
  752. plane_start[i] = gb.buffer;
  753. if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
  754. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  755. return AVERROR_INVALIDDATA;
  756. }
  757. slice_start = 0;
  758. slice_end = 0;
  759. for (j = 0; j < c->slices; j++) {
  760. slice_end = bytestream2_get_le32u(&gb);
  761. if (slice_end < 0 || slice_end < slice_start ||
  762. bytestream2_get_bytes_left(&gb) < slice_end) {
  763. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  764. return AVERROR_INVALIDDATA;
  765. }
  766. slice_size = slice_end - slice_start;
  767. slice_start = slice_end;
  768. max_slice_size = FFMAX(max_slice_size, slice_size);
  769. }
  770. plane_size = slice_end;
  771. bytestream2_skipu(&gb, plane_size);
  772. bytestream2_skipu(&gb, 1024);
  773. }
  774. plane_start[c->planes] = gb.buffer;
  775. } else {
  776. for (i = 0; i < c->planes; i++) {
  777. plane_start[i] = gb.buffer;
  778. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  779. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  780. return AVERROR_INVALIDDATA;
  781. }
  782. bytestream2_skipu(&gb, 256);
  783. slice_start = 0;
  784. slice_end = 0;
  785. for (j = 0; j < c->slices; j++) {
  786. slice_end = bytestream2_get_le32u(&gb);
  787. if (slice_end < 0 || slice_end < slice_start ||
  788. bytestream2_get_bytes_left(&gb) < slice_end) {
  789. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  790. return AVERROR_INVALIDDATA;
  791. }
  792. slice_size = slice_end - slice_start;
  793. slice_start = slice_end;
  794. max_slice_size = FFMAX(max_slice_size, slice_size);
  795. }
  796. plane_size = slice_end;
  797. bytestream2_skipu(&gb, plane_size);
  798. }
  799. plane_start[c->planes] = gb.buffer;
  800. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  801. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  802. return AVERROR_INVALIDDATA;
  803. }
  804. c->frame_info = bytestream2_get_le32u(&gb);
  805. }
  806. av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
  807. c->frame_info);
  808. c->frame_pred = (c->frame_info >> 8) & 3;
  809. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  810. max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
  811. if (!c->slice_bits) {
  812. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  813. return AVERROR(ENOMEM);
  814. }
  815. switch (c->avctx->pix_fmt) {
  816. case AV_PIX_FMT_RGB24:
  817. case AV_PIX_FMT_RGBA:
  818. for (i = 0; i < c->planes; i++) {
  819. ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
  820. c->planes, frame.f->linesize[0], avctx->width,
  821. avctx->height, plane_start[i],
  822. c->frame_pred == PRED_LEFT);
  823. if (ret)
  824. return ret;
  825. if (c->frame_pred == PRED_MEDIAN) {
  826. if (!c->interlaced) {
  827. restore_median_packed(frame.f->data[0] + ff_ut_rgb_order[i],
  828. c->planes, frame.f->linesize[0], avctx->width,
  829. avctx->height, c->slices, 0);
  830. } else {
  831. restore_median_packed_il(frame.f->data[0] + ff_ut_rgb_order[i],
  832. c->planes, frame.f->linesize[0],
  833. avctx->width, avctx->height, c->slices,
  834. 0);
  835. }
  836. } else if (c->frame_pred == PRED_GRADIENT) {
  837. if (!c->interlaced) {
  838. restore_gradient_packed(frame.f->data[0] + ff_ut_rgb_order[i],
  839. c->planes, frame.f->linesize[0],
  840. avctx->width, avctx->height,
  841. c->slices, 0);
  842. } else {
  843. restore_gradient_packed_il(frame.f->data[0] + ff_ut_rgb_order[i],
  844. c->planes, frame.f->linesize[0],
  845. avctx->width, avctx->height,
  846. c->slices, 0);
  847. }
  848. }
  849. }
  850. restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
  851. avctx->width, avctx->height);
  852. break;
  853. case AV_PIX_FMT_GBRAP10:
  854. case AV_PIX_FMT_GBRP10:
  855. for (i = 0; i < c->planes; i++) {
  856. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
  857. frame.f->linesize[i] / 2, avctx->width,
  858. avctx->height, plane_start[i],
  859. plane_start[i + 1] - 1024,
  860. c->frame_pred == PRED_LEFT);
  861. if (ret)
  862. return ret;
  863. }
  864. restore_rgb_planes10(frame.f, avctx->width, avctx->height);
  865. break;
  866. case AV_PIX_FMT_YUV420P:
  867. for (i = 0; i < 3; i++) {
  868. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  869. avctx->width >> !!i, avctx->height >> !!i,
  870. plane_start[i], c->frame_pred == PRED_LEFT);
  871. if (ret)
  872. return ret;
  873. if (c->frame_pred == PRED_MEDIAN) {
  874. if (!c->interlaced) {
  875. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  876. avctx->width >> !!i, avctx->height >> !!i,
  877. c->slices, !i);
  878. } else {
  879. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  880. avctx->width >> !!i,
  881. avctx->height >> !!i,
  882. c->slices, !i);
  883. }
  884. } else if (c->frame_pred == PRED_GRADIENT) {
  885. if (!c->interlaced) {
  886. restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
  887. avctx->width >> !!i,
  888. avctx->height >> !!i,
  889. c->slices, !i);
  890. } else {
  891. restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  892. avctx->width >> !!i,
  893. avctx->height >> !!i,
  894. c->slices, !i);
  895. }
  896. }
  897. }
  898. break;
  899. case AV_PIX_FMT_YUV422P:
  900. for (i = 0; i < 3; i++) {
  901. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  902. avctx->width >> !!i, avctx->height,
  903. plane_start[i], c->frame_pred == PRED_LEFT);
  904. if (ret)
  905. return ret;
  906. if (c->frame_pred == PRED_MEDIAN) {
  907. if (!c->interlaced) {
  908. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  909. avctx->width >> !!i, avctx->height,
  910. c->slices, 0);
  911. } else {
  912. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  913. avctx->width >> !!i, avctx->height,
  914. c->slices, 0);
  915. }
  916. } else if (c->frame_pred == PRED_GRADIENT) {
  917. if (!c->interlaced) {
  918. restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
  919. avctx->width >> !!i, avctx->height,
  920. c->slices, 0);
  921. } else {
  922. restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  923. avctx->width >> !!i, avctx->height,
  924. c->slices, 0);
  925. }
  926. }
  927. }
  928. break;
  929. case AV_PIX_FMT_YUV444P:
  930. for (i = 0; i < 3; i++) {
  931. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  932. avctx->width, avctx->height,
  933. plane_start[i], c->frame_pred == PRED_LEFT);
  934. if (ret)
  935. return ret;
  936. if (c->frame_pred == PRED_MEDIAN) {
  937. if (!c->interlaced) {
  938. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  939. avctx->width, avctx->height,
  940. c->slices, 0);
  941. } else {
  942. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  943. avctx->width, avctx->height,
  944. c->slices, 0);
  945. }
  946. } else if (c->frame_pred == PRED_GRADIENT) {
  947. if (!c->interlaced) {
  948. restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
  949. avctx->width, avctx->height,
  950. c->slices, 0);
  951. } else {
  952. restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  953. avctx->width, avctx->height,
  954. c->slices, 0);
  955. }
  956. }
  957. }
  958. break;
  959. case AV_PIX_FMT_YUV422P10:
  960. for (i = 0; i < 3; i++) {
  961. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
  962. avctx->width >> !!i, avctx->height,
  963. plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
  964. if (ret)
  965. return ret;
  966. }
  967. break;
  968. }
  969. frame.f->key_frame = 1;
  970. frame.f->pict_type = AV_PICTURE_TYPE_I;
  971. frame.f->interlaced_frame = !!c->interlaced;
  972. *got_frame = 1;
  973. /* always report that the buffer was completely consumed */
  974. return buf_size;
  975. }
  976. static av_cold int decode_init(AVCodecContext *avctx)
  977. {
  978. UtvideoContext * const c = avctx->priv_data;
  979. c->avctx = avctx;
  980. ff_bswapdsp_init(&c->bdsp);
  981. ff_huffyuvdsp_init(&c->hdspdec);
  982. if (avctx->extradata_size >= 16) {
  983. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  984. avctx->extradata[3], avctx->extradata[2],
  985. avctx->extradata[1], avctx->extradata[0]);
  986. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  987. AV_RB32(avctx->extradata + 4));
  988. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  989. c->flags = AV_RL32(avctx->extradata + 12);
  990. if (c->frame_info_size != 4)
  991. avpriv_request_sample(avctx, "Frame info not 4 bytes");
  992. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
  993. c->slices = (c->flags >> 24) + 1;
  994. c->compression = c->flags & 1;
  995. c->interlaced = c->flags & 0x800;
  996. } else if (avctx->extradata_size == 8) {
  997. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  998. avctx->extradata[3], avctx->extradata[2],
  999. avctx->extradata[1], avctx->extradata[0]);
  1000. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  1001. AV_RB32(avctx->extradata + 4));
  1002. c->interlaced = 0;
  1003. c->pro = 1;
  1004. c->frame_info_size = 4;
  1005. } else {
  1006. av_log(avctx, AV_LOG_ERROR,
  1007. "Insufficient extradata size %d, should be at least 16\n",
  1008. avctx->extradata_size);
  1009. return AVERROR_INVALIDDATA;
  1010. }
  1011. c->slice_bits_size = 0;
  1012. switch (avctx->codec_tag) {
  1013. case MKTAG('U', 'L', 'R', 'G'):
  1014. c->planes = 3;
  1015. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  1016. break;
  1017. case MKTAG('U', 'L', 'R', 'A'):
  1018. c->planes = 4;
  1019. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  1020. break;
  1021. case MKTAG('U', 'L', 'Y', '0'):
  1022. c->planes = 3;
  1023. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  1024. avctx->colorspace = AVCOL_SPC_BT470BG;
  1025. break;
  1026. case MKTAG('U', 'L', 'Y', '2'):
  1027. c->planes = 3;
  1028. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  1029. avctx->colorspace = AVCOL_SPC_BT470BG;
  1030. break;
  1031. case MKTAG('U', 'L', 'Y', '4'):
  1032. c->planes = 3;
  1033. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  1034. avctx->colorspace = AVCOL_SPC_BT470BG;
  1035. break;
  1036. case MKTAG('U', 'Q', 'Y', '2'):
  1037. c->planes = 3;
  1038. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  1039. break;
  1040. case MKTAG('U', 'Q', 'R', 'G'):
  1041. c->planes = 3;
  1042. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  1043. break;
  1044. case MKTAG('U', 'Q', 'R', 'A'):
  1045. c->planes = 4;
  1046. avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
  1047. break;
  1048. case MKTAG('U', 'L', 'H', '0'):
  1049. c->planes = 3;
  1050. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  1051. avctx->colorspace = AVCOL_SPC_BT709;
  1052. break;
  1053. case MKTAG('U', 'L', 'H', '2'):
  1054. c->planes = 3;
  1055. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  1056. avctx->colorspace = AVCOL_SPC_BT709;
  1057. break;
  1058. case MKTAG('U', 'L', 'H', '4'):
  1059. c->planes = 3;
  1060. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  1061. avctx->colorspace = AVCOL_SPC_BT709;
  1062. break;
  1063. default:
  1064. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  1065. avctx->codec_tag);
  1066. return AVERROR_INVALIDDATA;
  1067. }
  1068. return 0;
  1069. }
  1070. static av_cold int decode_end(AVCodecContext *avctx)
  1071. {
  1072. UtvideoContext * const c = avctx->priv_data;
  1073. av_freep(&c->slice_bits);
  1074. return 0;
  1075. }
  1076. AVCodec ff_utvideo_decoder = {
  1077. .name = "utvideo",
  1078. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  1079. .type = AVMEDIA_TYPE_VIDEO,
  1080. .id = AV_CODEC_ID_UTVIDEO,
  1081. .priv_data_size = sizeof(UtvideoContext),
  1082. .init = decode_init,
  1083. .close = decode_end,
  1084. .decode = decode_frame,
  1085. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1086. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1087. };