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.

913 lines
32KB

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