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.

936 lines
33KB

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