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.

951 lines
32KB

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