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.

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