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.

929 lines
33KB

  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 <inttypes.h>
  26. #include <stdlib.h>
  27. #define UNCHECKED_BITSTREAM_READER 1
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "bswapdsp.h"
  31. #include "bytestream.h"
  32. #include "get_bits.h"
  33. #include "internal.h"
  34. #include "thread.h"
  35. #include "utvideo.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. #define VLC_BITS 11
  69. return ff_init_vlc_sparse(vlc, VLC_BITS, last + 1,
  70. bits, sizeof(*bits), sizeof(*bits),
  71. codes, sizeof(*codes), sizeof(*codes),
  72. syms, sizeof(*syms), sizeof(*syms), 0);
  73. }
  74. static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
  75. {
  76. int i;
  77. HuffEntry he[256];
  78. int last;
  79. uint32_t codes[256];
  80. uint8_t bits[256];
  81. uint8_t syms[256];
  82. uint32_t code;
  83. *fsym = -1;
  84. for (i = 0; i < 256; i++) {
  85. he[i].sym = i;
  86. he[i].len = *src++;
  87. }
  88. qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
  89. if (!he[0].len) {
  90. *fsym = he[0].sym;
  91. return 0;
  92. }
  93. last = 255;
  94. while (he[last].len == 255 && last)
  95. last--;
  96. if (he[last].len > 32)
  97. return -1;
  98. code = 1;
  99. for (i = last; i >= 0; i--) {
  100. codes[i] = code >> (32 - he[i].len);
  101. bits[i] = he[i].len;
  102. syms[i] = he[i].sym;
  103. code += 0x80000000u >> (he[i].len - 1);
  104. }
  105. return ff_init_vlc_sparse(vlc, VLC_BITS, last + 1,
  106. bits, sizeof(*bits), sizeof(*bits),
  107. codes, sizeof(*codes), sizeof(*codes),
  108. syms, sizeof(*syms), sizeof(*syms), 0);
  109. }
  110. static int decode_plane10(UtvideoContext *c, int plane_no,
  111. uint16_t *dst, ptrdiff_t stride,
  112. int width, int height,
  113. const uint8_t *src, const uint8_t *huff,
  114. int use_pred)
  115. {
  116. int i, j, slice, pix, ret;
  117. int sstart, send;
  118. VLC vlc;
  119. GetBitContext gb;
  120. int prev, fsym;
  121. if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
  122. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  123. return ret;
  124. }
  125. if (fsym >= 0) { // build_huff reported a symbol to fill slices with
  126. send = 0;
  127. for (slice = 0; slice < c->slices; slice++) {
  128. uint16_t *dest;
  129. sstart = send;
  130. send = (height * (slice + 1) / c->slices);
  131. dest = dst + sstart * stride;
  132. prev = 0x200;
  133. for (j = sstart; j < send; j++) {
  134. for (i = 0; i < width; i++) {
  135. pix = fsym;
  136. if (use_pred) {
  137. prev += pix;
  138. prev &= 0x3FF;
  139. pix = prev;
  140. }
  141. dest[i] = pix;
  142. }
  143. dest += stride;
  144. }
  145. }
  146. return 0;
  147. }
  148. send = 0;
  149. for (slice = 0; slice < c->slices; slice++) {
  150. uint16_t *dest;
  151. int slice_data_start, slice_data_end, slice_size;
  152. sstart = send;
  153. send = (height * (slice + 1) / c->slices);
  154. dest = dst + sstart * stride;
  155. // slice offset and size validation was done earlier
  156. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  157. slice_data_end = AV_RL32(src + slice * 4);
  158. slice_size = slice_data_end - slice_data_start;
  159. if (!slice_size) {
  160. av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
  161. "yet a slice has a length of zero.\n");
  162. goto fail;
  163. }
  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 *)(src + slice_data_start + c->slices * 4),
  167. (slice_data_end - slice_data_start + 3) >> 2);
  168. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  169. prev = 0x200;
  170. for (j = sstart; j < send; j++) {
  171. for (i = 0; i < width; i++) {
  172. pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
  173. if (pix < 0) {
  174. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  175. goto fail;
  176. }
  177. if (use_pred) {
  178. prev += pix;
  179. prev &= 0x3FF;
  180. pix = prev;
  181. }
  182. dest[i] = pix;
  183. }
  184. dest += stride;
  185. if (get_bits_left(&gb) < 0) {
  186. av_log(c->avctx, AV_LOG_ERROR,
  187. "Slice decoding ran out of bits\n");
  188. goto fail;
  189. }
  190. }
  191. if (get_bits_left(&gb) > 32)
  192. av_log(c->avctx, AV_LOG_WARNING,
  193. "%d bits left after decoding slice\n", get_bits_left(&gb));
  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, enum AVPixelFormat 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, 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. GetBitContext gb;
  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; i++) {
  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. memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  263. c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
  264. (uint32_t *)(src + slice_data_start + c->slices * 4),
  265. (slice_data_end - slice_data_start + 3) >> 2);
  266. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  267. prev = 0x80;
  268. for (j = sstart; j < send; j++) {
  269. for (i = 0; i < width; i++) {
  270. pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
  271. if (pix < 0) {
  272. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  273. goto fail;
  274. }
  275. if (use_pred) {
  276. prev += pix;
  277. pix = prev;
  278. }
  279. dest[i] = pix;
  280. }
  281. if (get_bits_left(&gb) < 0) {
  282. av_log(c->avctx, AV_LOG_ERROR,
  283. "Slice decoding ran out of bits\n");
  284. goto fail;
  285. }
  286. dest += stride;
  287. }
  288. if (get_bits_left(&gb) > 32)
  289. av_log(c->avctx, AV_LOG_WARNING,
  290. "%d bits left after decoding slice\n", get_bits_left(&gb));
  291. }
  292. ff_free_vlc(&vlc);
  293. return 0;
  294. fail:
  295. ff_free_vlc(&vlc);
  296. return AVERROR_INVALIDDATA;
  297. }
  298. #undef A
  299. #undef B
  300. #undef C
  301. static void restore_median_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  302. int width, int height, int slices, int rmode)
  303. {
  304. int i, j, slice;
  305. int A, B, C;
  306. uint8_t *bsrc;
  307. int slice_start, slice_height;
  308. const int cmask = ~rmode;
  309. for (slice = 0; slice < slices; slice++) {
  310. slice_start = ((slice * height) / slices) & cmask;
  311. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  312. slice_start;
  313. if (!slice_height)
  314. continue;
  315. bsrc = src + slice_start * stride;
  316. // first line - left neighbour prediction
  317. bsrc[0] += 0x80;
  318. c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  319. bsrc += stride;
  320. if (slice_height <= 1)
  321. continue;
  322. // second line - first element has top prediction, the rest uses median
  323. C = bsrc[-stride];
  324. bsrc[0] += C;
  325. A = bsrc[0];
  326. for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
  327. B = bsrc[i - stride];
  328. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  329. C = B;
  330. A = bsrc[i];
  331. }
  332. if (width > 16)
  333. c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride + 16,
  334. bsrc + 16, width - 16, &A, &B);
  335. bsrc += stride;
  336. // the rest of lines use continuous median prediction
  337. for (j = 2; j < slice_height; j++) {
  338. c->llviddsp.add_median_pred(bsrc, bsrc - stride,
  339. bsrc, width, &A, &B);
  340. bsrc += stride;
  341. }
  342. }
  343. }
  344. /* UtVideo interlaced mode treats every two lines as a single one,
  345. * so restoring function should take care of possible padding between
  346. * two parts of the same "line".
  347. */
  348. static void restore_median_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  349. int width, int height, int slices, int rmode)
  350. {
  351. int i, j, slice;
  352. int A, B, C;
  353. uint8_t *bsrc;
  354. int slice_start, slice_height;
  355. const int cmask = ~(rmode ? 3 : 1);
  356. const ptrdiff_t stride2 = stride << 1;
  357. for (slice = 0; slice < slices; slice++) {
  358. slice_start = ((slice * height) / slices) & cmask;
  359. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  360. slice_start;
  361. slice_height >>= 1;
  362. if (!slice_height)
  363. continue;
  364. bsrc = src + slice_start * stride;
  365. // first line - left neighbour prediction
  366. bsrc[0] += 0x80;
  367. A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  368. c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
  369. bsrc += stride2;
  370. if (slice_height <= 1)
  371. continue;
  372. // second line - first element has top prediction, the rest uses median
  373. C = bsrc[-stride2];
  374. bsrc[0] += C;
  375. A = bsrc[0];
  376. for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
  377. B = bsrc[i - stride2];
  378. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  379. C = B;
  380. A = bsrc[i];
  381. }
  382. if (width > 16)
  383. c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride2 + 16,
  384. bsrc + 16, width - 16, &A, &B);
  385. c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
  386. bsrc + stride, width, &A, &B);
  387. bsrc += stride2;
  388. // the rest of lines use continuous median prediction
  389. for (j = 2; j < slice_height; j++) {
  390. c->llviddsp.add_median_pred(bsrc, bsrc - stride2,
  391. bsrc, width, &A, &B);
  392. c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
  393. bsrc + stride, width, &A, &B);
  394. bsrc += stride2;
  395. }
  396. }
  397. }
  398. static void restore_gradient_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  399. int width, int height, int slices, int rmode)
  400. {
  401. int i, j, slice;
  402. int A, B, C;
  403. uint8_t *bsrc;
  404. int slice_start, slice_height;
  405. const int cmask = ~rmode;
  406. for (slice = 0; slice < slices; slice++) {
  407. slice_start = ((slice * height) / slices) & cmask;
  408. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  409. slice_start;
  410. if (!slice_height)
  411. continue;
  412. bsrc = src + slice_start * stride;
  413. // first line - left neighbour prediction
  414. bsrc[0] += 0x80;
  415. c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  416. bsrc += stride;
  417. if (slice_height <= 1)
  418. continue;
  419. for (j = 1; j < slice_height; j++) {
  420. // second line - first element has top prediction, the rest uses gradient
  421. bsrc[0] = (bsrc[0] + bsrc[-stride]) & 0xFF;
  422. for (i = 1; i < width; i++) {
  423. A = bsrc[i - stride];
  424. B = bsrc[i - (stride + 1)];
  425. C = bsrc[i - 1];
  426. bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
  427. }
  428. bsrc += stride;
  429. }
  430. }
  431. }
  432. static void restore_gradient_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  433. int width, int height, int slices, int rmode)
  434. {
  435. int i, j, slice;
  436. int A, B, C;
  437. uint8_t *bsrc;
  438. int slice_start, slice_height;
  439. const int cmask = ~(rmode ? 3 : 1);
  440. const ptrdiff_t stride2 = stride << 1;
  441. for (slice = 0; slice < slices; slice++) {
  442. slice_start = ((slice * height) / slices) & cmask;
  443. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  444. slice_start;
  445. slice_height >>= 1;
  446. if (!slice_height)
  447. continue;
  448. bsrc = src + slice_start * stride;
  449. // first line - left neighbour prediction
  450. bsrc[0] += 0x80;
  451. A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  452. c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
  453. bsrc += stride2;
  454. if (slice_height <= 1)
  455. continue;
  456. for (j = 1; j < slice_height; j++) {
  457. // second line - first element has top prediction, the rest uses gradient
  458. bsrc[0] = (bsrc[0] + bsrc[-stride2]) & 0xFF;
  459. for (i = 1; i < width; i++) {
  460. A = bsrc[i - stride2];
  461. B = bsrc[i - (stride2 + 1)];
  462. C = bsrc[i - 1];
  463. bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
  464. }
  465. A = bsrc[-stride];
  466. B = bsrc[-(1 + stride + stride - width)];
  467. C = bsrc[width - 1];
  468. bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
  469. for (i = 1; i < width; i++) {
  470. A = bsrc[i - stride];
  471. B = bsrc[i - (1 + stride)];
  472. C = bsrc[i - 1 + stride];
  473. bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
  474. }
  475. bsrc += stride2;
  476. }
  477. }
  478. }
  479. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  480. AVPacket *avpkt)
  481. {
  482. const uint8_t *buf = avpkt->data;
  483. int buf_size = avpkt->size;
  484. UtvideoContext *c = avctx->priv_data;
  485. int i, j;
  486. const uint8_t *plane_start[5];
  487. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  488. int ret;
  489. GetByteContext gb;
  490. ThreadFrame frame = { .f = data };
  491. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  492. return ret;
  493. /* parse plane structure to get frame flags and validate slice offsets */
  494. bytestream2_init(&gb, buf, buf_size);
  495. if (c->pro) {
  496. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  497. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  498. return AVERROR_INVALIDDATA;
  499. }
  500. c->frame_info = bytestream2_get_le32u(&gb);
  501. c->slices = ((c->frame_info >> 16) & 0xff) + 1;
  502. for (i = 0; i < c->planes; i++) {
  503. plane_start[i] = gb.buffer;
  504. if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
  505. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  506. return AVERROR_INVALIDDATA;
  507. }
  508. slice_start = 0;
  509. slice_end = 0;
  510. for (j = 0; j < c->slices; j++) {
  511. slice_end = bytestream2_get_le32u(&gb);
  512. if (slice_end < 0 || slice_end < slice_start ||
  513. bytestream2_get_bytes_left(&gb) < slice_end) {
  514. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  515. return AVERROR_INVALIDDATA;
  516. }
  517. slice_size = slice_end - slice_start;
  518. slice_start = slice_end;
  519. max_slice_size = FFMAX(max_slice_size, slice_size);
  520. }
  521. plane_size = slice_end;
  522. bytestream2_skipu(&gb, plane_size);
  523. bytestream2_skipu(&gb, 1024);
  524. }
  525. plane_start[c->planes] = gb.buffer;
  526. } else {
  527. for (i = 0; i < c->planes; i++) {
  528. plane_start[i] = gb.buffer;
  529. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  530. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  531. return AVERROR_INVALIDDATA;
  532. }
  533. bytestream2_skipu(&gb, 256);
  534. slice_start = 0;
  535. slice_end = 0;
  536. for (j = 0; j < c->slices; j++) {
  537. slice_end = bytestream2_get_le32u(&gb);
  538. if (slice_end < 0 || slice_end < slice_start ||
  539. bytestream2_get_bytes_left(&gb) < slice_end) {
  540. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  541. return AVERROR_INVALIDDATA;
  542. }
  543. slice_size = slice_end - slice_start;
  544. slice_start = slice_end;
  545. max_slice_size = FFMAX(max_slice_size, slice_size);
  546. }
  547. plane_size = slice_end;
  548. bytestream2_skipu(&gb, plane_size);
  549. }
  550. plane_start[c->planes] = gb.buffer;
  551. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  552. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  553. return AVERROR_INVALIDDATA;
  554. }
  555. c->frame_info = bytestream2_get_le32u(&gb);
  556. }
  557. av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
  558. c->frame_info);
  559. c->frame_pred = (c->frame_info >> 8) & 3;
  560. max_slice_size += 4*avctx->width;
  561. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  562. max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
  563. if (!c->slice_bits) {
  564. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  565. return AVERROR(ENOMEM);
  566. }
  567. switch (c->avctx->pix_fmt) {
  568. case AV_PIX_FMT_GBRP:
  569. case AV_PIX_FMT_GBRAP:
  570. for (i = 0; i < c->planes; i++) {
  571. ret = decode_plane(c, i, frame.f->data[i],
  572. frame.f->linesize[i], avctx->width,
  573. avctx->height, plane_start[i],
  574. c->frame_pred == PRED_LEFT);
  575. if (ret)
  576. return ret;
  577. if (c->frame_pred == PRED_MEDIAN) {
  578. if (!c->interlaced) {
  579. restore_median_planar(c, frame.f->data[i],
  580. frame.f->linesize[i], avctx->width,
  581. avctx->height, c->slices, 0);
  582. } else {
  583. restore_median_planar_il(c, frame.f->data[i],
  584. frame.f->linesize[i],
  585. avctx->width, avctx->height, c->slices,
  586. 0);
  587. }
  588. } else if (c->frame_pred == PRED_GRADIENT) {
  589. if (!c->interlaced) {
  590. restore_gradient_planar(c, frame.f->data[i],
  591. frame.f->linesize[i], avctx->width,
  592. avctx->height, c->slices, 0);
  593. } else {
  594. restore_gradient_planar_il(c, frame.f->data[i],
  595. frame.f->linesize[i],
  596. avctx->width, avctx->height, c->slices,
  597. 0);
  598. }
  599. }
  600. }
  601. c->utdsp.restore_rgb_planes(frame.f->data[2], frame.f->data[0], frame.f->data[1],
  602. frame.f->linesize[2], frame.f->linesize[0], frame.f->linesize[1],
  603. avctx->width, avctx->height);
  604. break;
  605. case AV_PIX_FMT_GBRAP10:
  606. case AV_PIX_FMT_GBRP10:
  607. for (i = 0; i < c->planes; i++) {
  608. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i],
  609. frame.f->linesize[i] / 2, avctx->width,
  610. avctx->height, plane_start[i],
  611. plane_start[i + 1] - 1024,
  612. c->frame_pred == PRED_LEFT);
  613. if (ret)
  614. return ret;
  615. }
  616. c->utdsp.restore_rgb_planes10((uint16_t *)frame.f->data[2], (uint16_t *)frame.f->data[0], (uint16_t *)frame.f->data[1],
  617. frame.f->linesize[2] / 2, frame.f->linesize[0] / 2, frame.f->linesize[1] / 2,
  618. avctx->width, avctx->height);
  619. break;
  620. case AV_PIX_FMT_YUV420P:
  621. for (i = 0; i < 3; i++) {
  622. ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
  623. avctx->width >> !!i, avctx->height >> !!i,
  624. plane_start[i], c->frame_pred == PRED_LEFT);
  625. if (ret)
  626. return ret;
  627. if (c->frame_pred == PRED_MEDIAN) {
  628. if (!c->interlaced) {
  629. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  630. avctx->width >> !!i, avctx->height >> !!i,
  631. c->slices, !i);
  632. } else {
  633. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  634. avctx->width >> !!i,
  635. avctx->height >> !!i,
  636. c->slices, !i);
  637. }
  638. } else if (c->frame_pred == PRED_GRADIENT) {
  639. if (!c->interlaced) {
  640. restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
  641. avctx->width >> !!i, avctx->height >> !!i,
  642. c->slices, !i);
  643. } else {
  644. restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  645. avctx->width >> !!i,
  646. avctx->height >> !!i,
  647. c->slices, !i);
  648. }
  649. }
  650. }
  651. break;
  652. case AV_PIX_FMT_YUV422P:
  653. for (i = 0; i < 3; i++) {
  654. ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
  655. avctx->width >> !!i, avctx->height,
  656. plane_start[i], c->frame_pred == PRED_LEFT);
  657. if (ret)
  658. return ret;
  659. if (c->frame_pred == PRED_MEDIAN) {
  660. if (!c->interlaced) {
  661. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  662. avctx->width >> !!i, avctx->height,
  663. c->slices, 0);
  664. } else {
  665. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  666. avctx->width >> !!i, avctx->height,
  667. c->slices, 0);
  668. }
  669. } else if (c->frame_pred == PRED_GRADIENT) {
  670. if (!c->interlaced) {
  671. restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
  672. avctx->width >> !!i, avctx->height,
  673. c->slices, 0);
  674. } else {
  675. restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  676. avctx->width >> !!i, avctx->height,
  677. c->slices, 0);
  678. }
  679. }
  680. }
  681. break;
  682. case AV_PIX_FMT_YUV444P:
  683. for (i = 0; i < 3; i++) {
  684. ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
  685. avctx->width, avctx->height,
  686. plane_start[i], c->frame_pred == PRED_LEFT);
  687. if (ret)
  688. return ret;
  689. if (c->frame_pred == PRED_MEDIAN) {
  690. if (!c->interlaced) {
  691. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  692. avctx->width, avctx->height,
  693. c->slices, 0);
  694. } else {
  695. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  696. avctx->width, avctx->height,
  697. c->slices, 0);
  698. }
  699. } else if (c->frame_pred == PRED_GRADIENT) {
  700. if (!c->interlaced) {
  701. restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
  702. avctx->width, avctx->height,
  703. c->slices, 0);
  704. } else {
  705. restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  706. avctx->width, avctx->height,
  707. c->slices, 0);
  708. }
  709. }
  710. }
  711. break;
  712. case AV_PIX_FMT_YUV422P10:
  713. for (i = 0; i < 3; i++) {
  714. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], frame.f->linesize[i] / 2,
  715. avctx->width >> !!i, avctx->height,
  716. plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
  717. if (ret)
  718. return ret;
  719. }
  720. break;
  721. }
  722. frame.f->key_frame = 1;
  723. frame.f->pict_type = AV_PICTURE_TYPE_I;
  724. frame.f->interlaced_frame = !!c->interlaced;
  725. *got_frame = 1;
  726. /* always report that the buffer was completely consumed */
  727. return buf_size;
  728. }
  729. static av_cold int decode_init(AVCodecContext *avctx)
  730. {
  731. UtvideoContext * const c = avctx->priv_data;
  732. c->avctx = avctx;
  733. ff_utvideodsp_init(&c->utdsp);
  734. ff_bswapdsp_init(&c->bdsp);
  735. ff_llviddsp_init(&c->llviddsp);
  736. if (avctx->extradata_size >= 16) {
  737. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  738. avctx->extradata[3], avctx->extradata[2],
  739. avctx->extradata[1], avctx->extradata[0]);
  740. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  741. AV_RB32(avctx->extradata + 4));
  742. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  743. c->flags = AV_RL32(avctx->extradata + 12);
  744. if (c->frame_info_size != 4)
  745. avpriv_request_sample(avctx, "Frame info not 4 bytes");
  746. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
  747. c->slices = (c->flags >> 24) + 1;
  748. c->compression = c->flags & 1;
  749. c->interlaced = c->flags & 0x800;
  750. } else if (avctx->extradata_size == 8) {
  751. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  752. avctx->extradata[3], avctx->extradata[2],
  753. avctx->extradata[1], avctx->extradata[0]);
  754. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  755. AV_RB32(avctx->extradata + 4));
  756. c->interlaced = 0;
  757. c->pro = 1;
  758. c->frame_info_size = 4;
  759. } else {
  760. av_log(avctx, AV_LOG_ERROR,
  761. "Insufficient extradata size %d, should be at least 16\n",
  762. avctx->extradata_size);
  763. return AVERROR_INVALIDDATA;
  764. }
  765. c->slice_bits_size = 0;
  766. switch (avctx->codec_tag) {
  767. case MKTAG('U', 'L', 'R', 'G'):
  768. c->planes = 3;
  769. avctx->pix_fmt = AV_PIX_FMT_GBRP;
  770. break;
  771. case MKTAG('U', 'L', 'R', 'A'):
  772. c->planes = 4;
  773. avctx->pix_fmt = AV_PIX_FMT_GBRAP;
  774. break;
  775. case MKTAG('U', 'L', 'Y', '0'):
  776. c->planes = 3;
  777. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  778. avctx->colorspace = AVCOL_SPC_BT470BG;
  779. break;
  780. case MKTAG('U', 'L', 'Y', '2'):
  781. c->planes = 3;
  782. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  783. avctx->colorspace = AVCOL_SPC_BT470BG;
  784. break;
  785. case MKTAG('U', 'L', 'Y', '4'):
  786. c->planes = 3;
  787. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  788. avctx->colorspace = AVCOL_SPC_BT470BG;
  789. break;
  790. case MKTAG('U', 'Q', 'Y', '2'):
  791. c->planes = 3;
  792. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  793. break;
  794. case MKTAG('U', 'Q', 'R', 'G'):
  795. c->planes = 3;
  796. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  797. break;
  798. case MKTAG('U', 'Q', 'R', 'A'):
  799. c->planes = 4;
  800. avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
  801. break;
  802. case MKTAG('U', 'L', 'H', '0'):
  803. c->planes = 3;
  804. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  805. avctx->colorspace = AVCOL_SPC_BT709;
  806. break;
  807. case MKTAG('U', 'L', 'H', '2'):
  808. c->planes = 3;
  809. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  810. avctx->colorspace = AVCOL_SPC_BT709;
  811. break;
  812. case MKTAG('U', 'L', 'H', '4'):
  813. c->planes = 3;
  814. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  815. avctx->colorspace = AVCOL_SPC_BT709;
  816. break;
  817. default:
  818. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  819. avctx->codec_tag);
  820. return AVERROR_INVALIDDATA;
  821. }
  822. return 0;
  823. }
  824. static av_cold int decode_end(AVCodecContext *avctx)
  825. {
  826. UtvideoContext * const c = avctx->priv_data;
  827. av_freep(&c->slice_bits);
  828. return 0;
  829. }
  830. AVCodec ff_utvideo_decoder = {
  831. .name = "utvideo",
  832. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  833. .type = AVMEDIA_TYPE_VIDEO,
  834. .id = AV_CODEC_ID_UTVIDEO,
  835. .priv_data_size = sizeof(UtvideoContext),
  836. .init = decode_init,
  837. .close = decode_end,
  838. .decode = decode_frame,
  839. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  840. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  841. };