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.

947 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 "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. last = 255;
  92. while (he[last].len == 255 && last)
  93. last--;
  94. if (he[last].len > 32)
  95. return -1;
  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, 11), 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, ptrdiff_t stride,
  110. int width, int height,
  111. const uint8_t *src, const uint8_t *huff,
  112. int use_pred)
  113. {
  114. int i, j, slice, pix, ret;
  115. int sstart, send;
  116. VLC vlc;
  117. GetBitContext gb;
  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. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  169. prev = 0x200;
  170. for (j = sstart; j < send; j++) {
  171. for (i = 0; i < width * step; i += step) {
  172. if (get_bits_left(&gb) <= 0) {
  173. av_log(c->avctx, AV_LOG_ERROR,
  174. "Slice decoding ran out of bits\n");
  175. goto fail;
  176. }
  177. pix = get_vlc2(&gb, 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 (get_bits_left(&gb) > 32)
  192. av_log(c->avctx, AV_LOG_WARNING,
  193. "%d bits left after decoding slice\n", get_bits_left(&gb));
  194. }
  195. ff_free_vlc(&vlc);
  196. return 0;
  197. fail:
  198. ff_free_vlc(&vlc);
  199. return AVERROR_INVALIDDATA;
  200. }
  201. static int decode_plane(UtvideoContext *c, int plane_no,
  202. uint8_t *dst, int step, ptrdiff_t stride,
  203. int width, int height,
  204. const uint8_t *src, int use_pred)
  205. {
  206. int i, j, slice, pix;
  207. int sstart, send;
  208. VLC vlc;
  209. GetBitContext gb;
  210. int prev, fsym;
  211. const int cmask = ~(!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. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  262. prev = 0x80;
  263. for (j = sstart; j < send; j++) {
  264. for (i = 0; i < width * step; i += step) {
  265. if (get_bits_left(&gb) <= 0) {
  266. av_log(c->avctx, AV_LOG_ERROR,
  267. "Slice decoding ran out of bits\n");
  268. goto fail;
  269. }
  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. dest += stride;
  282. }
  283. if (get_bits_left(&gb) > 32)
  284. av_log(c->avctx, AV_LOG_WARNING,
  285. "%d bits left after decoding slice\n", get_bits_left(&gb));
  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. #undef A
  330. #undef B
  331. #undef C
  332. static void restore_median_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  333. int width, int height, int slices, int rmode)
  334. {
  335. int i, j, slice;
  336. int A, B, C;
  337. uint8_t *bsrc;
  338. int slice_start, slice_height;
  339. const int cmask = ~rmode;
  340. for (slice = 0; slice < slices; slice++) {
  341. slice_start = ((slice * height) / slices) & cmask;
  342. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  343. slice_start;
  344. if (!slice_height)
  345. continue;
  346. bsrc = src + slice_start * stride;
  347. // first line - left neighbour prediction
  348. bsrc[0] += 0x80;
  349. c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  350. bsrc += stride;
  351. if (slice_height <= 1)
  352. continue;
  353. // second line - first element has top prediction, the rest uses median
  354. C = bsrc[-stride];
  355. bsrc[0] += C;
  356. A = bsrc[0];
  357. for (i = 1; i < width; i++) {
  358. B = bsrc[i - stride];
  359. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  360. C = B;
  361. A = bsrc[i];
  362. }
  363. bsrc += stride;
  364. // the rest of lines use continuous median prediction
  365. for (j = 2; j < slice_height; j++) {
  366. c->llviddsp.add_median_pred(bsrc, bsrc - stride,
  367. bsrc, width, &A, &B);
  368. bsrc += stride;
  369. }
  370. }
  371. }
  372. /* UtVideo interlaced mode treats every two lines as a single one,
  373. * so restoring function should take care of possible padding between
  374. * two parts of the same "line".
  375. */
  376. static void restore_median_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  377. int width, int height, int slices, int rmode)
  378. {
  379. int i, j, slice;
  380. int A, B, C;
  381. uint8_t *bsrc;
  382. int slice_start, slice_height;
  383. const int cmask = ~(rmode ? 3 : 1);
  384. const ptrdiff_t stride2 = stride << 1;
  385. for (slice = 0; slice < slices; slice++) {
  386. slice_start = ((slice * height) / slices) & cmask;
  387. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  388. slice_start;
  389. slice_height >>= 1;
  390. if (!slice_height)
  391. continue;
  392. bsrc = src + slice_start * stride;
  393. // first line - left neighbour prediction
  394. bsrc[0] += 0x80;
  395. A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  396. c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
  397. bsrc += stride2;
  398. if (slice_height <= 1)
  399. continue;
  400. // second line - first element has top prediction, the rest uses median
  401. C = bsrc[-stride2];
  402. bsrc[0] += C;
  403. A = bsrc[0];
  404. for (i = 1; i < width; i++) {
  405. B = bsrc[i - stride2];
  406. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  407. C = B;
  408. A = bsrc[i];
  409. }
  410. c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
  411. bsrc + stride, width, &A, &B);
  412. bsrc += stride2;
  413. // the rest of lines use continuous median prediction
  414. for (j = 2; j < slice_height; j++) {
  415. c->llviddsp.add_median_pred(bsrc, bsrc - stride2,
  416. bsrc, width, &A, &B);
  417. c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
  418. bsrc + stride, width, &A, &B);
  419. bsrc += stride2;
  420. }
  421. }
  422. }
  423. static void restore_median_packed(uint8_t *src, int step, ptrdiff_t stride,
  424. int width, int height, 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, int slices, int rmode)
  477. {
  478. int i, j, slice;
  479. int A, B, C;
  480. uint8_t *bsrc;
  481. int slice_start, slice_height;
  482. const int cmask = ~(rmode ? 3 : 1);
  483. const ptrdiff_t stride2 = stride << 1;
  484. for (slice = 0; slice < slices; slice++) {
  485. slice_start = ((slice * height) / slices) & cmask;
  486. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  487. slice_start;
  488. slice_height >>= 1;
  489. if (!slice_height)
  490. continue;
  491. bsrc = src + slice_start * stride;
  492. // first line - left neighbour prediction
  493. bsrc[0] += 0x80;
  494. A = bsrc[0];
  495. for (i = step; i < width * step; i += step) {
  496. bsrc[i] += A;
  497. A = bsrc[i];
  498. }
  499. for (i = 0; i < width * step; i += step) {
  500. bsrc[stride + i] += A;
  501. A = bsrc[stride + i];
  502. }
  503. bsrc += stride2;
  504. if (slice_height <= 1)
  505. continue;
  506. // second line - first element has top prediction, the rest uses median
  507. C = bsrc[-stride2];
  508. bsrc[0] += C;
  509. A = bsrc[0];
  510. for (i = step; i < width * step; i += step) {
  511. B = bsrc[i - stride2];
  512. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  513. C = B;
  514. A = bsrc[i];
  515. }
  516. for (i = 0; i < width * step; i += step) {
  517. B = bsrc[i - stride];
  518. bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
  519. C = B;
  520. A = bsrc[stride + i];
  521. }
  522. bsrc += stride2;
  523. // the rest of lines use continuous median prediction
  524. for (j = 2; j < slice_height; j++) {
  525. for (i = 0; i < width * step; i += step) {
  526. B = bsrc[i - stride2];
  527. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  528. C = B;
  529. A = bsrc[i];
  530. }
  531. for (i = 0; i < width * step; i += step) {
  532. B = bsrc[i - stride];
  533. bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
  534. C = B;
  535. A = bsrc[i + stride];
  536. }
  537. bsrc += stride2;
  538. }
  539. }
  540. }
  541. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  542. AVPacket *avpkt)
  543. {
  544. const uint8_t *buf = avpkt->data;
  545. int buf_size = avpkt->size;
  546. UtvideoContext *c = avctx->priv_data;
  547. int i, j;
  548. const uint8_t *plane_start[5];
  549. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  550. int ret;
  551. GetByteContext gb;
  552. ThreadFrame frame = { .f = data };
  553. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  554. return ret;
  555. /* parse plane structure to get frame flags and validate slice offsets */
  556. bytestream2_init(&gb, buf, buf_size);
  557. if (c->pro) {
  558. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  559. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  560. return AVERROR_INVALIDDATA;
  561. }
  562. c->frame_info = bytestream2_get_le32u(&gb);
  563. c->slices = ((c->frame_info >> 16) & 0xff) + 1;
  564. for (i = 0; i < c->planes; i++) {
  565. plane_start[i] = gb.buffer;
  566. if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
  567. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  568. return AVERROR_INVALIDDATA;
  569. }
  570. slice_start = 0;
  571. slice_end = 0;
  572. for (j = 0; j < c->slices; j++) {
  573. slice_end = bytestream2_get_le32u(&gb);
  574. if (slice_end < 0 || slice_end < slice_start ||
  575. bytestream2_get_bytes_left(&gb) < slice_end) {
  576. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  577. return AVERROR_INVALIDDATA;
  578. }
  579. slice_size = slice_end - slice_start;
  580. slice_start = slice_end;
  581. max_slice_size = FFMAX(max_slice_size, slice_size);
  582. }
  583. plane_size = slice_end;
  584. bytestream2_skipu(&gb, plane_size);
  585. bytestream2_skipu(&gb, 1024);
  586. }
  587. plane_start[c->planes] = gb.buffer;
  588. } else {
  589. for (i = 0; i < c->planes; i++) {
  590. plane_start[i] = gb.buffer;
  591. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  592. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  593. return AVERROR_INVALIDDATA;
  594. }
  595. bytestream2_skipu(&gb, 256);
  596. slice_start = 0;
  597. slice_end = 0;
  598. for (j = 0; j < c->slices; j++) {
  599. slice_end = bytestream2_get_le32u(&gb);
  600. if (slice_end < 0 || slice_end < slice_start ||
  601. bytestream2_get_bytes_left(&gb) < slice_end) {
  602. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  603. return AVERROR_INVALIDDATA;
  604. }
  605. slice_size = slice_end - slice_start;
  606. slice_start = slice_end;
  607. max_slice_size = FFMAX(max_slice_size, slice_size);
  608. }
  609. plane_size = slice_end;
  610. bytestream2_skipu(&gb, plane_size);
  611. }
  612. plane_start[c->planes] = gb.buffer;
  613. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  614. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  615. return AVERROR_INVALIDDATA;
  616. }
  617. c->frame_info = bytestream2_get_le32u(&gb);
  618. }
  619. av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
  620. c->frame_info);
  621. c->frame_pred = (c->frame_info >> 8) & 3;
  622. if (c->frame_pred == PRED_GRADIENT) {
  623. avpriv_request_sample(avctx, "Frame with gradient prediction");
  624. return AVERROR_PATCHWELCOME;
  625. }
  626. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  627. max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
  628. if (!c->slice_bits) {
  629. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  630. return AVERROR(ENOMEM);
  631. }
  632. switch (c->avctx->pix_fmt) {
  633. case AV_PIX_FMT_RGB24:
  634. case AV_PIX_FMT_RGBA:
  635. for (i = 0; i < c->planes; i++) {
  636. ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
  637. c->planes, frame.f->linesize[0], avctx->width,
  638. avctx->height, plane_start[i],
  639. c->frame_pred == PRED_LEFT);
  640. if (ret)
  641. return ret;
  642. if (c->frame_pred == PRED_MEDIAN) {
  643. if (!c->interlaced) {
  644. restore_median_packed(frame.f->data[0] + ff_ut_rgb_order[i],
  645. c->planes, frame.f->linesize[0], avctx->width,
  646. avctx->height, c->slices, 0);
  647. } else {
  648. restore_median_packed_il(frame.f->data[0] + ff_ut_rgb_order[i],
  649. c->planes, frame.f->linesize[0],
  650. avctx->width, avctx->height, c->slices,
  651. 0);
  652. }
  653. }
  654. }
  655. restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
  656. avctx->width, avctx->height);
  657. break;
  658. case AV_PIX_FMT_GBRAP10:
  659. case AV_PIX_FMT_GBRP10:
  660. for (i = 0; i < c->planes; i++) {
  661. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
  662. frame.f->linesize[i] / 2, avctx->width,
  663. avctx->height, plane_start[i],
  664. plane_start[i + 1] - 1024,
  665. c->frame_pred == PRED_LEFT);
  666. if (ret)
  667. return ret;
  668. }
  669. restore_rgb_planes10(frame.f, avctx->width, avctx->height);
  670. break;
  671. case AV_PIX_FMT_YUV420P:
  672. for (i = 0; i < 3; i++) {
  673. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  674. avctx->width >> !!i, avctx->height >> !!i,
  675. plane_start[i], c->frame_pred == PRED_LEFT);
  676. if (ret)
  677. return ret;
  678. if (c->frame_pred == PRED_MEDIAN) {
  679. if (!c->interlaced) {
  680. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  681. avctx->width >> !!i, avctx->height >> !!i,
  682. c->slices, !i);
  683. } else {
  684. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  685. avctx->width >> !!i,
  686. avctx->height >> !!i,
  687. c->slices, !i);
  688. }
  689. }
  690. }
  691. break;
  692. case AV_PIX_FMT_YUV422P:
  693. for (i = 0; i < 3; i++) {
  694. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  695. avctx->width >> !!i, avctx->height,
  696. plane_start[i], c->frame_pred == PRED_LEFT);
  697. if (ret)
  698. return ret;
  699. if (c->frame_pred == PRED_MEDIAN) {
  700. if (!c->interlaced) {
  701. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  702. avctx->width >> !!i, avctx->height,
  703. c->slices, 0);
  704. } else {
  705. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  706. avctx->width >> !!i, avctx->height,
  707. c->slices, 0);
  708. }
  709. }
  710. }
  711. break;
  712. case AV_PIX_FMT_YUV444P:
  713. for (i = 0; i < 3; i++) {
  714. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  715. avctx->width, avctx->height,
  716. plane_start[i], c->frame_pred == PRED_LEFT);
  717. if (ret)
  718. return ret;
  719. if (c->frame_pred == PRED_MEDIAN) {
  720. if (!c->interlaced) {
  721. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  722. avctx->width, avctx->height,
  723. c->slices, 0);
  724. } else {
  725. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  726. avctx->width, avctx->height,
  727. c->slices, 0);
  728. }
  729. }
  730. }
  731. break;
  732. case AV_PIX_FMT_YUV422P10:
  733. for (i = 0; i < 3; i++) {
  734. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
  735. avctx->width >> !!i, avctx->height,
  736. plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
  737. if (ret)
  738. return ret;
  739. }
  740. break;
  741. }
  742. frame.f->key_frame = 1;
  743. frame.f->pict_type = AV_PICTURE_TYPE_I;
  744. frame.f->interlaced_frame = !!c->interlaced;
  745. *got_frame = 1;
  746. /* always report that the buffer was completely consumed */
  747. return buf_size;
  748. }
  749. static av_cold int decode_init(AVCodecContext *avctx)
  750. {
  751. UtvideoContext * const c = avctx->priv_data;
  752. c->avctx = avctx;
  753. ff_bswapdsp_init(&c->bdsp);
  754. ff_llviddsp_init(&c->llviddsp);
  755. if (avctx->extradata_size >= 16) {
  756. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  757. avctx->extradata[3], avctx->extradata[2],
  758. avctx->extradata[1], avctx->extradata[0]);
  759. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  760. AV_RB32(avctx->extradata + 4));
  761. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  762. c->flags = AV_RL32(avctx->extradata + 12);
  763. if (c->frame_info_size != 4)
  764. avpriv_request_sample(avctx, "Frame info not 4 bytes");
  765. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
  766. c->slices = (c->flags >> 24) + 1;
  767. c->compression = c->flags & 1;
  768. c->interlaced = c->flags & 0x800;
  769. } else if (avctx->extradata_size == 8) {
  770. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  771. avctx->extradata[3], avctx->extradata[2],
  772. avctx->extradata[1], avctx->extradata[0]);
  773. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  774. AV_RB32(avctx->extradata + 4));
  775. c->interlaced = 0;
  776. c->pro = 1;
  777. c->frame_info_size = 4;
  778. } else {
  779. av_log(avctx, AV_LOG_ERROR,
  780. "Insufficient extradata size %d, should be at least 16\n",
  781. avctx->extradata_size);
  782. return AVERROR_INVALIDDATA;
  783. }
  784. c->slice_bits_size = 0;
  785. switch (avctx->codec_tag) {
  786. case MKTAG('U', 'L', 'R', 'G'):
  787. c->planes = 3;
  788. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  789. break;
  790. case MKTAG('U', 'L', 'R', 'A'):
  791. c->planes = 4;
  792. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  793. break;
  794. case MKTAG('U', 'L', 'Y', '0'):
  795. c->planes = 3;
  796. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  797. avctx->colorspace = AVCOL_SPC_BT470BG;
  798. break;
  799. case MKTAG('U', 'L', 'Y', '2'):
  800. c->planes = 3;
  801. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  802. avctx->colorspace = AVCOL_SPC_BT470BG;
  803. break;
  804. case MKTAG('U', 'L', 'Y', '4'):
  805. c->planes = 3;
  806. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  807. avctx->colorspace = AVCOL_SPC_BT470BG;
  808. break;
  809. case MKTAG('U', 'Q', 'Y', '2'):
  810. c->planes = 3;
  811. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  812. break;
  813. case MKTAG('U', 'Q', 'R', 'G'):
  814. c->planes = 3;
  815. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  816. break;
  817. case MKTAG('U', 'Q', 'R', 'A'):
  818. c->planes = 4;
  819. avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
  820. break;
  821. case MKTAG('U', 'L', 'H', '0'):
  822. c->planes = 3;
  823. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  824. avctx->colorspace = AVCOL_SPC_BT709;
  825. break;
  826. case MKTAG('U', 'L', 'H', '2'):
  827. c->planes = 3;
  828. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  829. avctx->colorspace = AVCOL_SPC_BT709;
  830. break;
  831. case MKTAG('U', 'L', 'H', '4'):
  832. c->planes = 3;
  833. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  834. avctx->colorspace = AVCOL_SPC_BT709;
  835. break;
  836. default:
  837. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  838. avctx->codec_tag);
  839. return AVERROR_INVALIDDATA;
  840. }
  841. return 0;
  842. }
  843. static av_cold int decode_end(AVCodecContext *avctx)
  844. {
  845. UtvideoContext * const c = avctx->priv_data;
  846. av_freep(&c->slice_bits);
  847. return 0;
  848. }
  849. AVCodec ff_utvideo_decoder = {
  850. .name = "utvideo",
  851. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  852. .type = AVMEDIA_TYPE_VIDEO,
  853. .id = AV_CODEC_ID_UTVIDEO,
  854. .priv_data_size = sizeof(UtvideoContext),
  855. .init = decode_init,
  856. .close = decode_end,
  857. .decode = decode_frame,
  858. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  859. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  860. };