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.

945 lines
32KB

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