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.

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