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.

850 lines
28KB

  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 "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, int 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, int 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, int stride, int width,
  294. 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(uint8_t *src, int step, int stride,
  330. int width, int height, int slices, int rmode)
  331. {
  332. int i, j, slice;
  333. int A, B, C;
  334. uint8_t *bsrc;
  335. int slice_start, slice_height;
  336. const int cmask = ~rmode;
  337. for (slice = 0; slice < slices; slice++) {
  338. slice_start = ((slice * height) / slices) & cmask;
  339. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  340. slice_start;
  341. if (!slice_height)
  342. continue;
  343. bsrc = src + slice_start * stride;
  344. // first line - left neighbour prediction
  345. bsrc[0] += 0x80;
  346. A = bsrc[0];
  347. for (i = step; i < width * step; i += step) {
  348. bsrc[i] += A;
  349. A = bsrc[i];
  350. }
  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 = step; i < width * step; i += step) {
  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. for (i = 0; i < width * step; i += step) {
  368. B = bsrc[i - stride];
  369. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  370. C = B;
  371. A = bsrc[i];
  372. }
  373. bsrc += stride;
  374. }
  375. }
  376. }
  377. /* UtVideo interlaced mode treats every two lines as a single one,
  378. * so restoring function should take care of possible padding between
  379. * two parts of the same "line".
  380. */
  381. static void restore_median_il(uint8_t *src, int step, int stride,
  382. int width, int height, int slices, int rmode)
  383. {
  384. int i, j, slice;
  385. int A, B, C;
  386. uint8_t *bsrc;
  387. int slice_start, slice_height;
  388. const int cmask = ~(rmode ? 3 : 1);
  389. const int stride2 = stride << 1;
  390. for (slice = 0; slice < slices; slice++) {
  391. slice_start = ((slice * height) / slices) & cmask;
  392. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  393. slice_start;
  394. slice_height >>= 1;
  395. if (!slice_height)
  396. continue;
  397. bsrc = src + slice_start * stride;
  398. // first line - left neighbour prediction
  399. bsrc[0] += 0x80;
  400. A = bsrc[0];
  401. for (i = step; i < width * step; i += step) {
  402. bsrc[i] += A;
  403. A = bsrc[i];
  404. }
  405. for (i = 0; i < width * step; i += step) {
  406. bsrc[stride + i] += A;
  407. A = bsrc[stride + i];
  408. }
  409. bsrc += stride2;
  410. if (slice_height <= 1)
  411. continue;
  412. // second line - first element has top prediction, the rest uses median
  413. C = bsrc[-stride2];
  414. bsrc[0] += C;
  415. A = bsrc[0];
  416. for (i = step; i < width * step; i += step) {
  417. B = bsrc[i - stride2];
  418. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  419. C = B;
  420. A = bsrc[i];
  421. }
  422. for (i = 0; i < width * step; i += step) {
  423. B = bsrc[i - stride];
  424. bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
  425. C = B;
  426. A = bsrc[stride + i];
  427. }
  428. bsrc += stride2;
  429. // the rest of lines use continuous median prediction
  430. for (j = 2; j < slice_height; j++) {
  431. for (i = 0; i < width * step; i += step) {
  432. B = bsrc[i - stride2];
  433. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  434. C = B;
  435. A = bsrc[i];
  436. }
  437. for (i = 0; i < width * step; i += step) {
  438. B = bsrc[i - stride];
  439. bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
  440. C = B;
  441. A = bsrc[i + stride];
  442. }
  443. bsrc += stride2;
  444. }
  445. }
  446. }
  447. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  448. AVPacket *avpkt)
  449. {
  450. const uint8_t *buf = avpkt->data;
  451. int buf_size = avpkt->size;
  452. UtvideoContext *c = avctx->priv_data;
  453. int i, j;
  454. const uint8_t *plane_start[5];
  455. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  456. int ret;
  457. GetByteContext gb;
  458. ThreadFrame frame = { .f = data };
  459. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  460. return ret;
  461. /* parse plane structure to get frame flags and validate slice offsets */
  462. bytestream2_init(&gb, buf, buf_size);
  463. if (c->pro) {
  464. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  465. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  466. return AVERROR_INVALIDDATA;
  467. }
  468. c->frame_info = bytestream2_get_le32u(&gb);
  469. c->slices = ((c->frame_info >> 16) & 0xff) + 1;
  470. for (i = 0; i < c->planes; i++) {
  471. plane_start[i] = gb.buffer;
  472. if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
  473. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  474. return AVERROR_INVALIDDATA;
  475. }
  476. slice_start = 0;
  477. slice_end = 0;
  478. for (j = 0; j < c->slices; j++) {
  479. slice_end = bytestream2_get_le32u(&gb);
  480. if (slice_end < 0 || slice_end < slice_start ||
  481. bytestream2_get_bytes_left(&gb) < slice_end + 1024LL) {
  482. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  483. return AVERROR_INVALIDDATA;
  484. }
  485. slice_size = slice_end - slice_start;
  486. slice_start = slice_end;
  487. max_slice_size = FFMAX(max_slice_size, slice_size);
  488. }
  489. plane_size = slice_end;
  490. bytestream2_skipu(&gb, plane_size);
  491. bytestream2_skipu(&gb, 1024);
  492. }
  493. plane_start[c->planes] = gb.buffer;
  494. } else {
  495. for (i = 0; i < c->planes; i++) {
  496. plane_start[i] = gb.buffer;
  497. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  498. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  499. return AVERROR_INVALIDDATA;
  500. }
  501. bytestream2_skipu(&gb, 256);
  502. slice_start = 0;
  503. slice_end = 0;
  504. for (j = 0; j < c->slices; j++) {
  505. slice_end = bytestream2_get_le32u(&gb);
  506. if (slice_end < 0 || slice_end < slice_start ||
  507. bytestream2_get_bytes_left(&gb) < slice_end) {
  508. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  509. return AVERROR_INVALIDDATA;
  510. }
  511. slice_size = slice_end - slice_start;
  512. slice_start = slice_end;
  513. max_slice_size = FFMAX(max_slice_size, slice_size);
  514. }
  515. plane_size = slice_end;
  516. bytestream2_skipu(&gb, plane_size);
  517. }
  518. plane_start[c->planes] = gb.buffer;
  519. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  520. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  521. return AVERROR_INVALIDDATA;
  522. }
  523. c->frame_info = bytestream2_get_le32u(&gb);
  524. }
  525. av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
  526. c->frame_info);
  527. c->frame_pred = (c->frame_info >> 8) & 3;
  528. if (c->frame_pred == PRED_GRADIENT) {
  529. avpriv_request_sample(avctx, "Frame with gradient prediction");
  530. return AVERROR_PATCHWELCOME;
  531. }
  532. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  533. max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
  534. if (!c->slice_bits) {
  535. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  536. return AVERROR(ENOMEM);
  537. }
  538. switch (c->avctx->pix_fmt) {
  539. case AV_PIX_FMT_RGB24:
  540. case AV_PIX_FMT_RGBA:
  541. for (i = 0; i < c->planes; i++) {
  542. ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
  543. c->planes, frame.f->linesize[0], avctx->width,
  544. avctx->height, plane_start[i],
  545. c->frame_pred == PRED_LEFT);
  546. if (ret)
  547. return ret;
  548. if (c->frame_pred == PRED_MEDIAN) {
  549. if (!c->interlaced) {
  550. restore_median(frame.f->data[0] + ff_ut_rgb_order[i],
  551. c->planes, frame.f->linesize[0], avctx->width,
  552. avctx->height, c->slices, 0);
  553. } else {
  554. restore_median_il(frame.f->data[0] + ff_ut_rgb_order[i],
  555. c->planes, frame.f->linesize[0],
  556. avctx->width, avctx->height, c->slices,
  557. 0);
  558. }
  559. }
  560. }
  561. restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
  562. avctx->width, avctx->height);
  563. break;
  564. case AV_PIX_FMT_GBRAP10:
  565. case AV_PIX_FMT_GBRP10:
  566. for (i = 0; i < c->planes; i++) {
  567. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
  568. frame.f->linesize[i] / 2, avctx->width,
  569. avctx->height, plane_start[i],
  570. plane_start[i + 1] - 1024,
  571. c->frame_pred == PRED_LEFT);
  572. if (ret)
  573. return ret;
  574. }
  575. restore_rgb_planes10(frame.f, avctx->width, avctx->height);
  576. break;
  577. case AV_PIX_FMT_YUV420P:
  578. for (i = 0; i < 3; i++) {
  579. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  580. avctx->width >> !!i, avctx->height >> !!i,
  581. plane_start[i], c->frame_pred == PRED_LEFT);
  582. if (ret)
  583. return ret;
  584. if (c->frame_pred == PRED_MEDIAN) {
  585. if (!c->interlaced) {
  586. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  587. avctx->width >> !!i, avctx->height >> !!i,
  588. c->slices, !i);
  589. } else {
  590. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  591. avctx->width >> !!i,
  592. avctx->height >> !!i,
  593. c->slices, !i);
  594. }
  595. }
  596. }
  597. break;
  598. case AV_PIX_FMT_YUV422P:
  599. for (i = 0; i < 3; i++) {
  600. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  601. avctx->width >> !!i, avctx->height,
  602. plane_start[i], c->frame_pred == PRED_LEFT);
  603. if (ret)
  604. return ret;
  605. if (c->frame_pred == PRED_MEDIAN) {
  606. if (!c->interlaced) {
  607. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  608. avctx->width >> !!i, avctx->height,
  609. c->slices, 0);
  610. } else {
  611. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  612. avctx->width >> !!i, avctx->height,
  613. c->slices, 0);
  614. }
  615. }
  616. }
  617. break;
  618. case AV_PIX_FMT_YUV444P:
  619. for (i = 0; i < 3; i++) {
  620. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  621. avctx->width, avctx->height,
  622. plane_start[i], c->frame_pred == PRED_LEFT);
  623. if (ret)
  624. return ret;
  625. if (c->frame_pred == PRED_MEDIAN) {
  626. if (!c->interlaced) {
  627. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  628. avctx->width, avctx->height,
  629. c->slices, 0);
  630. } else {
  631. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  632. avctx->width, avctx->height,
  633. c->slices, 0);
  634. }
  635. }
  636. }
  637. break;
  638. case AV_PIX_FMT_YUV422P10:
  639. for (i = 0; i < 3; i++) {
  640. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
  641. avctx->width >> !!i, avctx->height,
  642. plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
  643. if (ret)
  644. return ret;
  645. }
  646. break;
  647. }
  648. frame.f->key_frame = 1;
  649. frame.f->pict_type = AV_PICTURE_TYPE_I;
  650. frame.f->interlaced_frame = !!c->interlaced;
  651. *got_frame = 1;
  652. /* always report that the buffer was completely consumed */
  653. return buf_size;
  654. }
  655. static av_cold int decode_init(AVCodecContext *avctx)
  656. {
  657. UtvideoContext * const c = avctx->priv_data;
  658. int h_shift, v_shift;
  659. c->avctx = avctx;
  660. ff_bswapdsp_init(&c->bdsp);
  661. if (avctx->extradata_size >= 16) {
  662. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  663. avctx->extradata[3], avctx->extradata[2],
  664. avctx->extradata[1], avctx->extradata[0]);
  665. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  666. AV_RB32(avctx->extradata + 4));
  667. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  668. c->flags = AV_RL32(avctx->extradata + 12);
  669. if (c->frame_info_size != 4)
  670. avpriv_request_sample(avctx, "Frame info not 4 bytes");
  671. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
  672. c->slices = (c->flags >> 24) + 1;
  673. c->compression = c->flags & 1;
  674. c->interlaced = c->flags & 0x800;
  675. } else if (avctx->extradata_size == 8) {
  676. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  677. avctx->extradata[3], avctx->extradata[2],
  678. avctx->extradata[1], avctx->extradata[0]);
  679. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  680. AV_RB32(avctx->extradata + 4));
  681. c->interlaced = 0;
  682. c->pro = 1;
  683. c->frame_info_size = 4;
  684. } else {
  685. av_log(avctx, AV_LOG_ERROR,
  686. "Insufficient extradata size %d, should be at least 16\n",
  687. avctx->extradata_size);
  688. return AVERROR_INVALIDDATA;
  689. }
  690. c->slice_bits_size = 0;
  691. switch (avctx->codec_tag) {
  692. case MKTAG('U', 'L', 'R', 'G'):
  693. c->planes = 3;
  694. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  695. break;
  696. case MKTAG('U', 'L', 'R', 'A'):
  697. c->planes = 4;
  698. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  699. break;
  700. case MKTAG('U', 'L', 'Y', '0'):
  701. c->planes = 3;
  702. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  703. avctx->colorspace = AVCOL_SPC_BT470BG;
  704. break;
  705. case MKTAG('U', 'L', 'Y', '2'):
  706. c->planes = 3;
  707. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  708. avctx->colorspace = AVCOL_SPC_BT470BG;
  709. break;
  710. case MKTAG('U', 'L', 'Y', '4'):
  711. c->planes = 3;
  712. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  713. avctx->colorspace = AVCOL_SPC_BT470BG;
  714. break;
  715. case MKTAG('U', 'Q', 'Y', '2'):
  716. c->planes = 3;
  717. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  718. break;
  719. case MKTAG('U', 'Q', 'R', 'G'):
  720. c->planes = 3;
  721. avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  722. break;
  723. case MKTAG('U', 'Q', 'R', 'A'):
  724. c->planes = 4;
  725. avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
  726. break;
  727. case MKTAG('U', 'L', 'H', '0'):
  728. c->planes = 3;
  729. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  730. avctx->colorspace = AVCOL_SPC_BT709;
  731. break;
  732. case MKTAG('U', 'L', 'H', '2'):
  733. c->planes = 3;
  734. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  735. avctx->colorspace = AVCOL_SPC_BT709;
  736. break;
  737. case MKTAG('U', 'L', 'H', '4'):
  738. c->planes = 3;
  739. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  740. avctx->colorspace = AVCOL_SPC_BT709;
  741. break;
  742. default:
  743. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  744. avctx->codec_tag);
  745. return AVERROR_INVALIDDATA;
  746. }
  747. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &h_shift, &v_shift);
  748. if ((avctx->width & ((1<<h_shift)-1)) ||
  749. (avctx->height & ((1<<v_shift)-1))) {
  750. avpriv_request_sample(avctx, "Odd dimensions");
  751. return AVERROR_PATCHWELCOME;
  752. }
  753. return 0;
  754. }
  755. static av_cold int decode_end(AVCodecContext *avctx)
  756. {
  757. UtvideoContext * const c = avctx->priv_data;
  758. av_freep(&c->slice_bits);
  759. return 0;
  760. }
  761. AVCodec ff_utvideo_decoder = {
  762. .name = "utvideo",
  763. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  764. .type = AVMEDIA_TYPE_VIDEO,
  765. .id = AV_CODEC_ID_UTVIDEO,
  766. .priv_data_size = sizeof(UtvideoContext),
  767. .init = decode_init,
  768. .close = decode_end,
  769. .decode = decode_frame,
  770. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  771. };