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.

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