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.

774 lines
26KB

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