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.

956 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 "libavutil/pixdesc.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. return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
  69. bits, sizeof(*bits), sizeof(*bits),
  70. codes, sizeof(*codes), sizeof(*codes),
  71. syms, sizeof(*syms), sizeof(*syms), 0);
  72. }
  73. static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
  74. {
  75. int i;
  76. HuffEntry he[256];
  77. int last;
  78. uint32_t codes[256];
  79. uint8_t bits[256];
  80. uint8_t syms[256];
  81. uint32_t code;
  82. *fsym = -1;
  83. for (i = 0; i < 256; i++) {
  84. he[i].sym = i;
  85. he[i].len = *src++;
  86. }
  87. qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
  88. if (!he[0].len) {
  89. *fsym = he[0].sym;
  90. return 0;
  91. }
  92. last = 255;
  93. while (he[last].len == 255 && last)
  94. last--;
  95. if (he[last].len > 32)
  96. return -1;
  97. code = 1;
  98. for (i = last; i >= 0; i--) {
  99. codes[i] = code >> (32 - he[i].len);
  100. bits[i] = he[i].len;
  101. syms[i] = he[i].sym;
  102. code += 0x80000000u >> (he[i].len - 1);
  103. }
  104. return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
  105. bits, sizeof(*bits), sizeof(*bits),
  106. codes, sizeof(*codes), sizeof(*codes),
  107. syms, sizeof(*syms), sizeof(*syms), 0);
  108. }
  109. static int decode_plane10(UtvideoContext *c, int plane_no,
  110. uint16_t *dst, int step, ptrdiff_t stride,
  111. int width, int height,
  112. const uint8_t *src, const uint8_t *huff,
  113. int use_pred)
  114. {
  115. int i, j, slice, pix, ret;
  116. int sstart, send;
  117. VLC vlc;
  118. GetBitContext gb;
  119. int prev, fsym;
  120. if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
  121. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  122. return ret;
  123. }
  124. if (fsym >= 0) { // build_huff reported a symbol to fill slices with
  125. send = 0;
  126. for (slice = 0; slice < c->slices; slice++) {
  127. uint16_t *dest;
  128. sstart = send;
  129. send = (height * (slice + 1) / c->slices);
  130. dest = dst + sstart * stride;
  131. prev = 0x200;
  132. for (j = sstart; j < send; j++) {
  133. for (i = 0; i < width * step; i += step) {
  134. pix = fsym;
  135. if (use_pred) {
  136. prev += pix;
  137. prev &= 0x3FF;
  138. pix = prev;
  139. }
  140. dest[i] = pix;
  141. }
  142. dest += stride;
  143. }
  144. }
  145. return 0;
  146. }
  147. send = 0;
  148. for (slice = 0; slice < c->slices; slice++) {
  149. uint16_t *dest;
  150. int slice_data_start, slice_data_end, slice_size;
  151. sstart = send;
  152. send = (height * (slice + 1) / c->slices);
  153. dest = dst + sstart * stride;
  154. // slice offset and size validation was done earlier
  155. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  156. slice_data_end = AV_RL32(src + slice * 4);
  157. slice_size = slice_data_end - slice_data_start;
  158. if (!slice_size) {
  159. av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
  160. "yet a slice has a length of zero.\n");
  161. goto fail;
  162. }
  163. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  164. slice_size);
  165. memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  166. c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
  167. (uint32_t *) c->slice_bits,
  168. (slice_data_end - slice_data_start + 3) >> 2);
  169. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  170. prev = 0x200;
  171. for (j = sstart; j < send; j++) {
  172. for (i = 0; i < width * step; i += step) {
  173. if (get_bits_left(&gb) <= 0) {
  174. av_log(c->avctx, AV_LOG_ERROR,
  175. "Slice decoding ran out of bits\n");
  176. goto fail;
  177. }
  178. pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
  179. if (pix < 0) {
  180. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  181. goto fail;
  182. }
  183. if (use_pred) {
  184. prev += pix;
  185. prev &= 0x3FF;
  186. pix = prev;
  187. }
  188. dest[i] = pix;
  189. }
  190. dest += stride;
  191. }
  192. if (get_bits_left(&gb) > 32)
  193. av_log(c->avctx, AV_LOG_WARNING,
  194. "%d bits left after decoding slice\n", get_bits_left(&gb));
  195. }
  196. ff_free_vlc(&vlc);
  197. return 0;
  198. fail:
  199. ff_free_vlc(&vlc);
  200. return AVERROR_INVALIDDATA;
  201. }
  202. static int decode_plane(UtvideoContext *c, int plane_no,
  203. uint8_t *dst, int step, ptrdiff_t stride,
  204. int width, int height,
  205. const uint8_t *src, int use_pred)
  206. {
  207. int i, j, slice, pix;
  208. int sstart, send;
  209. VLC vlc;
  210. GetBitContext gb;
  211. int prev, fsym;
  212. const int cmask = ~(!plane_no && c->avctx->pix_fmt == AV_PIX_FMT_YUV420P);
  213. if (build_huff(src, &vlc, &fsym)) {
  214. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  215. return AVERROR_INVALIDDATA;
  216. }
  217. if (fsym >= 0) { // build_huff reported a symbol to fill slices with
  218. send = 0;
  219. for (slice = 0; slice < c->slices; slice++) {
  220. uint8_t *dest;
  221. sstart = send;
  222. send = (height * (slice + 1) / c->slices) & cmask;
  223. dest = dst + sstart * stride;
  224. prev = 0x80;
  225. for (j = sstart; j < send; j++) {
  226. for (i = 0; i < width * step; i += step) {
  227. pix = fsym;
  228. if (use_pred) {
  229. prev += pix;
  230. pix = prev;
  231. }
  232. dest[i] = pix;
  233. }
  234. dest += stride;
  235. }
  236. }
  237. return 0;
  238. }
  239. src += 256;
  240. send = 0;
  241. for (slice = 0; slice < c->slices; slice++) {
  242. uint8_t *dest;
  243. int slice_data_start, slice_data_end, slice_size;
  244. sstart = send;
  245. send = (height * (slice + 1) / c->slices) & cmask;
  246. dest = dst + sstart * stride;
  247. // slice offset and size validation was done earlier
  248. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  249. slice_data_end = AV_RL32(src + slice * 4);
  250. slice_size = slice_data_end - slice_data_start;
  251. if (!slice_size) {
  252. av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
  253. "yet a slice has a length of zero.\n");
  254. goto fail;
  255. }
  256. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  257. slice_size);
  258. memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  259. c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
  260. (uint32_t *) c->slice_bits,
  261. (slice_data_end - slice_data_start + 3) >> 2);
  262. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  263. prev = 0x80;
  264. for (j = sstart; j < send; j++) {
  265. for (i = 0; i < width * step; i += step) {
  266. if (get_bits_left(&gb) <= 0) {
  267. av_log(c->avctx, AV_LOG_ERROR,
  268. "Slice decoding ran out of bits\n");
  269. goto fail;
  270. }
  271. pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
  272. if (pix < 0) {
  273. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  274. goto fail;
  275. }
  276. if (use_pred) {
  277. prev += pix;
  278. pix = prev;
  279. }
  280. dest[i] = pix;
  281. }
  282. dest += stride;
  283. }
  284. if (get_bits_left(&gb) > 32)
  285. av_log(c->avctx, AV_LOG_WARNING,
  286. "%d bits left after decoding slice\n", get_bits_left(&gb));
  287. }
  288. ff_free_vlc(&vlc);
  289. return 0;
  290. fail:
  291. ff_free_vlc(&vlc);
  292. return AVERROR_INVALIDDATA;
  293. }
  294. static void restore_rgb_planes(uint8_t *src, int step, ptrdiff_t stride,
  295. int width, int height)
  296. {
  297. int i, j;
  298. uint8_t r, g, b;
  299. for (j = 0; j < height; j++) {
  300. for (i = 0; i < width * step; i += step) {
  301. r = src[i];
  302. g = src[i + 1];
  303. b = src[i + 2];
  304. src[i] = r + g - 0x80;
  305. src[i + 2] = b + g - 0x80;
  306. }
  307. src += stride;
  308. }
  309. }
  310. static void restore_rgb_planes10(AVFrame *frame, int width, int height)
  311. {
  312. uint16_t *src_r = (uint16_t *)frame->data[2];
  313. uint16_t *src_g = (uint16_t *)frame->data[0];
  314. uint16_t *src_b = (uint16_t *)frame->data[1];
  315. int r, g, b;
  316. int i, j;
  317. for (j = 0; j < height; j++) {
  318. for (i = 0; i < width; i++) {
  319. r = src_r[i];
  320. g = src_g[i];
  321. b = src_b[i];
  322. src_r[i] = (r + g - 0x200) & 0x3FF;
  323. src_b[i] = (b + g - 0x200) & 0x3FF;
  324. }
  325. src_r += frame->linesize[2] / 2;
  326. src_g += frame->linesize[0] / 2;
  327. src_b += frame->linesize[1] / 2;
  328. }
  329. }
  330. #undef A
  331. #undef B
  332. #undef C
  333. static void restore_median_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  334. int width, int height, int slices, int rmode)
  335. {
  336. int i, j, slice;
  337. int A, B, C;
  338. uint8_t *bsrc;
  339. int slice_start, slice_height;
  340. const int cmask = ~rmode;
  341. for (slice = 0; slice < slices; slice++) {
  342. slice_start = ((slice * height) / slices) & cmask;
  343. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  344. slice_start;
  345. if (!slice_height)
  346. continue;
  347. bsrc = src + slice_start * stride;
  348. // first line - left neighbour prediction
  349. bsrc[0] += 0x80;
  350. c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  351. bsrc += stride;
  352. if (slice_height <= 1)
  353. continue;
  354. // second line - first element has top prediction, the rest uses median
  355. C = bsrc[-stride];
  356. bsrc[0] += C;
  357. A = bsrc[0];
  358. for (i = 1; i < width; i++) {
  359. B = bsrc[i - stride];
  360. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  361. C = B;
  362. A = bsrc[i];
  363. }
  364. bsrc += stride;
  365. // the rest of lines use continuous median prediction
  366. for (j = 2; j < slice_height; j++) {
  367. c->llviddsp.add_median_pred(bsrc, bsrc - stride,
  368. bsrc, width, &A, &B);
  369. bsrc += stride;
  370. }
  371. }
  372. }
  373. /* UtVideo interlaced mode treats every two lines as a single one,
  374. * so restoring function should take care of possible padding between
  375. * two parts of the same "line".
  376. */
  377. static void restore_median_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
  378. int width, int height, int slices, int rmode)
  379. {
  380. int i, j, slice;
  381. int A, B, C;
  382. uint8_t *bsrc;
  383. int slice_start, slice_height;
  384. const int cmask = ~(rmode ? 3 : 1);
  385. const ptrdiff_t stride2 = stride << 1;
  386. for (slice = 0; slice < slices; slice++) {
  387. slice_start = ((slice * height) / slices) & cmask;
  388. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  389. slice_start;
  390. slice_height >>= 1;
  391. if (!slice_height)
  392. continue;
  393. bsrc = src + slice_start * stride;
  394. // first line - left neighbour prediction
  395. bsrc[0] += 0x80;
  396. A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
  397. c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
  398. bsrc += stride2;
  399. if (slice_height <= 1)
  400. continue;
  401. // second line - first element has top prediction, the rest uses median
  402. C = bsrc[-stride2];
  403. bsrc[0] += C;
  404. A = bsrc[0];
  405. for (i = 1; i < width; i++) {
  406. B = bsrc[i - stride2];
  407. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  408. C = B;
  409. A = bsrc[i];
  410. }
  411. c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
  412. bsrc + stride, width, &A, &B);
  413. bsrc += stride2;
  414. // the rest of lines use continuous median prediction
  415. for (j = 2; j < slice_height; j++) {
  416. c->llviddsp.add_median_pred(bsrc, bsrc - stride2,
  417. bsrc, width, &A, &B);
  418. c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
  419. bsrc + stride, width, &A, &B);
  420. bsrc += stride2;
  421. }
  422. }
  423. }
  424. static void restore_median_packed(uint8_t *src, int step, ptrdiff_t stride,
  425. int width, int height, int slices, int rmode)
  426. {
  427. int i, j, slice;
  428. int A, B, C;
  429. uint8_t *bsrc;
  430. int slice_start, slice_height;
  431. const int cmask = ~rmode;
  432. for (slice = 0; slice < slices; slice++) {
  433. slice_start = ((slice * height) / slices) & cmask;
  434. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  435. slice_start;
  436. if (!slice_height)
  437. continue;
  438. bsrc = src + slice_start * stride;
  439. // first line - left neighbour prediction
  440. bsrc[0] += 0x80;
  441. A = bsrc[0];
  442. for (i = step; i < width * step; i += step) {
  443. bsrc[i] += A;
  444. A = bsrc[i];
  445. }
  446. bsrc += stride;
  447. if (slice_height <= 1)
  448. continue;
  449. // second line - first element has top prediction, the rest uses median
  450. C = bsrc[-stride];
  451. bsrc[0] += C;
  452. A = bsrc[0];
  453. for (i = step; i < width * step; i += step) {
  454. B = bsrc[i - stride];
  455. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  456. C = B;
  457. A = bsrc[i];
  458. }
  459. bsrc += stride;
  460. // the rest of lines use continuous median prediction
  461. for (j = 2; j < slice_height; j++) {
  462. for (i = 0; i < width * step; i += step) {
  463. B = bsrc[i - stride];
  464. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  465. C = B;
  466. A = bsrc[i];
  467. }
  468. bsrc += stride;
  469. }
  470. }
  471. }
  472. /* UtVideo interlaced mode treats every two lines as a single one,
  473. * so restoring function should take care of possible padding between
  474. * two parts of the same "line".
  475. */
  476. static void restore_median_packed_il(uint8_t *src, int step, ptrdiff_t stride,
  477. int width, int height, 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. return ret;
  556. /* parse plane structure to get frame flags and validate slice offsets */
  557. bytestream2_init(&gb, buf, buf_size);
  558. if (c->pro) {
  559. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  560. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  561. return AVERROR_INVALIDDATA;
  562. }
  563. c->frame_info = bytestream2_get_le32u(&gb);
  564. c->slices = ((c->frame_info >> 16) & 0xff) + 1;
  565. for (i = 0; i < c->planes; i++) {
  566. plane_start[i] = gb.buffer;
  567. if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
  568. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  569. return AVERROR_INVALIDDATA;
  570. }
  571. slice_start = 0;
  572. slice_end = 0;
  573. for (j = 0; j < c->slices; j++) {
  574. slice_end = bytestream2_get_le32u(&gb);
  575. if (slice_end < 0 || slice_end < slice_start ||
  576. bytestream2_get_bytes_left(&gb) < slice_end + 1024LL) {
  577. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  578. return AVERROR_INVALIDDATA;
  579. }
  580. slice_size = slice_end - slice_start;
  581. slice_start = slice_end;
  582. max_slice_size = FFMAX(max_slice_size, slice_size);
  583. }
  584. plane_size = slice_end;
  585. bytestream2_skipu(&gb, plane_size);
  586. bytestream2_skipu(&gb, 1024);
  587. }
  588. plane_start[c->planes] = gb.buffer;
  589. } else {
  590. for (i = 0; i < c->planes; i++) {
  591. plane_start[i] = gb.buffer;
  592. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  593. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  594. return AVERROR_INVALIDDATA;
  595. }
  596. bytestream2_skipu(&gb, 256);
  597. slice_start = 0;
  598. slice_end = 0;
  599. for (j = 0; j < c->slices; j++) {
  600. slice_end = bytestream2_get_le32u(&gb);
  601. if (slice_end < 0 || slice_end < slice_start ||
  602. bytestream2_get_bytes_left(&gb) < slice_end) {
  603. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  604. return AVERROR_INVALIDDATA;
  605. }
  606. slice_size = slice_end - slice_start;
  607. slice_start = slice_end;
  608. max_slice_size = FFMAX(max_slice_size, slice_size);
  609. }
  610. plane_size = slice_end;
  611. bytestream2_skipu(&gb, plane_size);
  612. }
  613. plane_start[c->planes] = gb.buffer;
  614. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  615. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  616. return AVERROR_INVALIDDATA;
  617. }
  618. c->frame_info = bytestream2_get_le32u(&gb);
  619. }
  620. av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
  621. c->frame_info);
  622. c->frame_pred = (c->frame_info >> 8) & 3;
  623. if (c->frame_pred == PRED_GRADIENT) {
  624. avpriv_request_sample(avctx, "Frame with gradient prediction");
  625. return AVERROR_PATCHWELCOME;
  626. }
  627. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  628. max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
  629. if (!c->slice_bits) {
  630. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  631. return AVERROR(ENOMEM);
  632. }
  633. switch (c->avctx->pix_fmt) {
  634. case AV_PIX_FMT_RGB24:
  635. case AV_PIX_FMT_RGBA:
  636. for (i = 0; i < c->planes; i++) {
  637. ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
  638. c->planes, frame.f->linesize[0], avctx->width,
  639. avctx->height, plane_start[i],
  640. c->frame_pred == PRED_LEFT);
  641. if (ret)
  642. return ret;
  643. if (c->frame_pred == PRED_MEDIAN) {
  644. if (!c->interlaced) {
  645. restore_median_packed(frame.f->data[0] + ff_ut_rgb_order[i],
  646. c->planes, frame.f->linesize[0], avctx->width,
  647. avctx->height, c->slices, 0);
  648. } else {
  649. restore_median_packed_il(frame.f->data[0] + ff_ut_rgb_order[i],
  650. c->planes, frame.f->linesize[0],
  651. avctx->width, avctx->height, c->slices,
  652. 0);
  653. }
  654. }
  655. }
  656. restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
  657. avctx->width, avctx->height);
  658. break;
  659. case AV_PIX_FMT_GBRAP10:
  660. case AV_PIX_FMT_GBRP10:
  661. for (i = 0; i < c->planes; i++) {
  662. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
  663. frame.f->linesize[i] / 2, avctx->width,
  664. avctx->height, plane_start[i],
  665. plane_start[i + 1] - 1024,
  666. c->frame_pred == PRED_LEFT);
  667. if (ret)
  668. return ret;
  669. }
  670. restore_rgb_planes10(frame.f, avctx->width, avctx->height);
  671. break;
  672. case AV_PIX_FMT_YUV420P:
  673. for (i = 0; i < 3; i++) {
  674. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  675. avctx->width >> !!i, avctx->height >> !!i,
  676. plane_start[i], c->frame_pred == PRED_LEFT);
  677. if (ret)
  678. return ret;
  679. if (c->frame_pred == PRED_MEDIAN) {
  680. if (!c->interlaced) {
  681. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  682. avctx->width >> !!i, avctx->height >> !!i,
  683. c->slices, !i);
  684. } else {
  685. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  686. avctx->width >> !!i,
  687. avctx->height >> !!i,
  688. c->slices, !i);
  689. }
  690. }
  691. }
  692. break;
  693. case AV_PIX_FMT_YUV422P:
  694. for (i = 0; i < 3; i++) {
  695. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  696. avctx->width >> !!i, avctx->height,
  697. plane_start[i], c->frame_pred == PRED_LEFT);
  698. if (ret)
  699. return ret;
  700. if (c->frame_pred == PRED_MEDIAN) {
  701. if (!c->interlaced) {
  702. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  703. avctx->width >> !!i, avctx->height,
  704. c->slices, 0);
  705. } else {
  706. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  707. avctx->width >> !!i, avctx->height,
  708. c->slices, 0);
  709. }
  710. }
  711. }
  712. break;
  713. case AV_PIX_FMT_YUV444P:
  714. for (i = 0; i < 3; i++) {
  715. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  716. avctx->width, avctx->height,
  717. plane_start[i], c->frame_pred == PRED_LEFT);
  718. if (ret)
  719. return ret;
  720. if (c->frame_pred == PRED_MEDIAN) {
  721. if (!c->interlaced) {
  722. restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
  723. avctx->width, avctx->height,
  724. c->slices, 0);
  725. } else {
  726. restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
  727. avctx->width, avctx->height,
  728. c->slices, 0);
  729. }
  730. }
  731. }
  732. break;
  733. case AV_PIX_FMT_YUV422P10:
  734. for (i = 0; i < 3; i++) {
  735. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
  736. avctx->width >> !!i, avctx->height,
  737. plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
  738. if (ret)
  739. return ret;
  740. }
  741. break;
  742. }
  743. frame.f->key_frame = 1;
  744. frame.f->pict_type = AV_PICTURE_TYPE_I;
  745. frame.f->interlaced_frame = !!c->interlaced;
  746. *got_frame = 1;
  747. /* always report that the buffer was completely consumed */
  748. return buf_size;
  749. }
  750. static av_cold int decode_init(AVCodecContext *avctx)
  751. {
  752. UtvideoContext * const c = avctx->priv_data;
  753. int h_shift, v_shift;
  754. c->avctx = avctx;
  755. ff_bswapdsp_init(&c->bdsp);
  756. ff_llviddsp_init(&c->llviddsp);
  757. if (avctx->extradata_size >= 16) {
  758. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  759. avctx->extradata[3], avctx->extradata[2],
  760. avctx->extradata[1], avctx->extradata[0]);
  761. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  762. AV_RB32(avctx->extradata + 4));
  763. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  764. c->flags = AV_RL32(avctx->extradata + 12);
  765. if (c->frame_info_size != 4)
  766. avpriv_request_sample(avctx, "Frame info not 4 bytes");
  767. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
  768. c->slices = (c->flags >> 24) + 1;
  769. c->compression = c->flags & 1;
  770. c->interlaced = c->flags & 0x800;
  771. } else if (avctx->extradata_size == 8) {
  772. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  773. avctx->extradata[3], avctx->extradata[2],
  774. avctx->extradata[1], avctx->extradata[0]);
  775. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  776. AV_RB32(avctx->extradata + 4));
  777. c->interlaced = 0;
  778. c->pro = 1;
  779. c->frame_info_size = 4;
  780. } else {
  781. av_log(avctx, AV_LOG_ERROR,
  782. "Insufficient extradata size %d, should be at least 16\n",
  783. avctx->extradata_size);
  784. return AVERROR_INVALIDDATA;
  785. }
  786. c->slice_bits_size = 0;
  787. switch (avctx->codec_tag) {
  788. case MKTAG('U', 'L', 'R', 'G'):
  789. c->planes = 3;
  790. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  791. break;
  792. case MKTAG('U', 'L', 'R', 'A'):
  793. c->planes = 4;
  794. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  795. break;
  796. case MKTAG('U', 'L', 'Y', '0'):
  797. c->planes = 3;
  798. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  799. avctx->colorspace = AVCOL_SPC_BT470BG;
  800. break;
  801. case MKTAG('U', 'L', 'Y', '2'):
  802. c->planes = 3;
  803. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  804. avctx->colorspace = AVCOL_SPC_BT470BG;
  805. break;
  806. case MKTAG('U', 'L', 'Y', '4'):
  807. c->planes = 3;
  808. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  809. avctx->colorspace = AVCOL_SPC_BT470BG;
  810. break;
  811. case MKTAG('U', 'Q', 'Y', '2'):
  812. c->planes = 3;
  813. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  814. break;
  815. case MKTAG('U', 'Q', 'R', 'G'):
  816. c->planes = 3;
  817. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  818. break;
  819. case MKTAG('U', 'Q', 'R', 'A'):
  820. c->planes = 4;
  821. avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
  822. break;
  823. case MKTAG('U', 'L', 'H', '0'):
  824. c->planes = 3;
  825. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  826. avctx->colorspace = AVCOL_SPC_BT709;
  827. break;
  828. case MKTAG('U', 'L', 'H', '2'):
  829. c->planes = 3;
  830. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  831. avctx->colorspace = AVCOL_SPC_BT709;
  832. break;
  833. case MKTAG('U', 'L', 'H', '4'):
  834. c->planes = 3;
  835. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  836. avctx->colorspace = AVCOL_SPC_BT709;
  837. break;
  838. default:
  839. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  840. avctx->codec_tag);
  841. return AVERROR_INVALIDDATA;
  842. }
  843. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &h_shift, &v_shift);
  844. if ((avctx->width & ((1<<h_shift)-1)) ||
  845. (avctx->height & ((1<<v_shift)-1))) {
  846. avpriv_request_sample(avctx, "Odd dimensions");
  847. return AVERROR_PATCHWELCOME;
  848. }
  849. return 0;
  850. }
  851. static av_cold int decode_end(AVCodecContext *avctx)
  852. {
  853. UtvideoContext * const c = avctx->priv_data;
  854. av_freep(&c->slice_bits);
  855. return 0;
  856. }
  857. AVCodec ff_utvideo_decoder = {
  858. .name = "utvideo",
  859. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  860. .type = AVMEDIA_TYPE_VIDEO,
  861. .id = AV_CODEC_ID_UTVIDEO,
  862. .priv_data_size = sizeof(UtvideoContext),
  863. .init = decode_init,
  864. .close = decode_end,
  865. .decode = decode_frame,
  866. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  867. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  868. };