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.

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