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.

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