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.

758 lines
25KB

  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. pix = prev;
  136. }
  137. dest[i] = pix;
  138. }
  139. dest += stride;
  140. }
  141. }
  142. return 0;
  143. }
  144. send = 0;
  145. for (slice = 0; slice < c->slices; slice++) {
  146. uint16_t *dest;
  147. int slice_data_start, slice_data_end, slice_size;
  148. sstart = send;
  149. send = (height * (slice + 1) / c->slices);
  150. dest = dst + sstart * stride;
  151. // slice offset and size validation was done earlier
  152. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  153. slice_data_end = AV_RL32(src + slice * 4);
  154. slice_size = slice_data_end - slice_data_start;
  155. if (!slice_size) {
  156. av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
  157. "yet a slice has a length of zero.\n");
  158. goto fail;
  159. }
  160. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  161. slice_size);
  162. memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  163. c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
  164. (uint32_t *) c->slice_bits,
  165. (slice_data_end - slice_data_start + 3) >> 2);
  166. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  167. prev = 0x200;
  168. for (j = sstart; j < send; j++) {
  169. for (i = 0; i < width * step; i += step) {
  170. if (get_bits_left(&gb) <= 0) {
  171. av_log(c->avctx, AV_LOG_ERROR,
  172. "Slice decoding ran out of bits\n");
  173. goto fail;
  174. }
  175. pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
  176. if (pix < 0) {
  177. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  178. goto fail;
  179. }
  180. if (use_pred) {
  181. prev += pix;
  182. prev &= 0x3FF;
  183. pix = prev;
  184. }
  185. dest[i] = pix;
  186. }
  187. dest += stride;
  188. }
  189. if (get_bits_left(&gb) > 32)
  190. av_log(c->avctx, AV_LOG_WARNING,
  191. "%d bits left after decoding slice\n", get_bits_left(&gb));
  192. }
  193. ff_free_vlc(&vlc);
  194. return 0;
  195. fail:
  196. ff_free_vlc(&vlc);
  197. return AVERROR_INVALIDDATA;
  198. }
  199. static int decode_plane(UtvideoContext *c, int plane_no,
  200. uint8_t *dst, int step, int stride,
  201. int width, int height,
  202. const uint8_t *src, int use_pred)
  203. {
  204. int i, j, slice, pix;
  205. int sstart, send;
  206. VLC vlc;
  207. GetBitContext gb;
  208. int prev, fsym;
  209. const int cmask = ~(!plane_no && c->avctx->pix_fmt == AV_PIX_FMT_YUV420P);
  210. if (build_huff(src, &vlc, &fsym)) {
  211. av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
  212. return AVERROR_INVALIDDATA;
  213. }
  214. if (fsym >= 0) { // build_huff reported a symbol to fill slices with
  215. send = 0;
  216. for (slice = 0; slice < c->slices; slice++) {
  217. uint8_t *dest;
  218. sstart = send;
  219. send = (height * (slice + 1) / c->slices) & cmask;
  220. dest = dst + sstart * stride;
  221. prev = 0x80;
  222. for (j = sstart; j < send; j++) {
  223. for (i = 0; i < width * step; i += step) {
  224. pix = fsym;
  225. if (use_pred) {
  226. prev += pix;
  227. pix = prev;
  228. }
  229. dest[i] = pix;
  230. }
  231. dest += stride;
  232. }
  233. }
  234. return 0;
  235. }
  236. src += 256;
  237. send = 0;
  238. for (slice = 0; slice < c->slices; slice++) {
  239. uint8_t *dest;
  240. int slice_data_start, slice_data_end, slice_size;
  241. sstart = send;
  242. send = (height * (slice + 1) / c->slices) & cmask;
  243. dest = dst + sstart * stride;
  244. // slice offset and size validation was done earlier
  245. slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
  246. slice_data_end = AV_RL32(src + slice * 4);
  247. slice_size = slice_data_end - slice_data_start;
  248. if (!slice_size) {
  249. av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
  250. "yet a slice has a length of zero.\n");
  251. goto fail;
  252. }
  253. memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
  254. slice_size);
  255. memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  256. c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
  257. (uint32_t *) c->slice_bits,
  258. (slice_data_end - slice_data_start + 3) >> 2);
  259. init_get_bits(&gb, c->slice_bits, slice_size * 8);
  260. prev = 0x80;
  261. for (j = sstart; j < send; j++) {
  262. for (i = 0; i < width * step; i += step) {
  263. if (get_bits_left(&gb) <= 0) {
  264. av_log(c->avctx, AV_LOG_ERROR,
  265. "Slice decoding ran out of bits\n");
  266. goto fail;
  267. }
  268. pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
  269. if (pix < 0) {
  270. av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
  271. goto fail;
  272. }
  273. if (use_pred) {
  274. prev += pix;
  275. pix = prev;
  276. }
  277. dest[i] = pix;
  278. }
  279. dest += stride;
  280. }
  281. if (get_bits_left(&gb) > 32)
  282. av_log(c->avctx, AV_LOG_WARNING,
  283. "%d bits left after decoding slice\n", get_bits_left(&gb));
  284. }
  285. ff_free_vlc(&vlc);
  286. return 0;
  287. fail:
  288. ff_free_vlc(&vlc);
  289. return AVERROR_INVALIDDATA;
  290. }
  291. static void restore_rgb_planes(uint8_t *src, int step, int stride, int width,
  292. int height)
  293. {
  294. int i, j;
  295. uint8_t r, g, b;
  296. for (j = 0; j < height; j++) {
  297. for (i = 0; i < width * step; i += step) {
  298. r = src[i];
  299. g = src[i + 1];
  300. b = src[i + 2];
  301. src[i] = r + g - 0x80;
  302. src[i + 2] = b + g - 0x80;
  303. }
  304. src += stride;
  305. }
  306. }
  307. static void restore_median(uint8_t *src, int step, int stride,
  308. int width, int height, int slices, int rmode)
  309. {
  310. int i, j, slice;
  311. int A, B, C;
  312. uint8_t *bsrc;
  313. int slice_start, slice_height;
  314. const int cmask = ~rmode;
  315. for (slice = 0; slice < slices; slice++) {
  316. slice_start = ((slice * height) / slices) & cmask;
  317. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  318. slice_start;
  319. if (!slice_height)
  320. continue;
  321. bsrc = src + slice_start * stride;
  322. // first line - left neighbour prediction
  323. bsrc[0] += 0x80;
  324. A = bsrc[0];
  325. for (i = step; i < width * step; i += step) {
  326. bsrc[i] += A;
  327. A = bsrc[i];
  328. }
  329. bsrc += stride;
  330. if (slice_height <= 1)
  331. continue;
  332. // second line - first element has top prediction, the rest uses median
  333. C = bsrc[-stride];
  334. bsrc[0] += C;
  335. A = bsrc[0];
  336. for (i = step; i < width * step; i += step) {
  337. B = bsrc[i - stride];
  338. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  339. C = B;
  340. A = bsrc[i];
  341. }
  342. bsrc += stride;
  343. // the rest of lines use continuous median prediction
  344. for (j = 2; j < slice_height; j++) {
  345. for (i = 0; i < width * step; i += step) {
  346. B = bsrc[i - stride];
  347. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  348. C = B;
  349. A = bsrc[i];
  350. }
  351. bsrc += stride;
  352. }
  353. }
  354. }
  355. /* UtVideo interlaced mode treats every two lines as a single one,
  356. * so restoring function should take care of possible padding between
  357. * two parts of the same "line".
  358. */
  359. static void restore_median_il(uint8_t *src, int step, int stride,
  360. int width, int height, int slices, int rmode)
  361. {
  362. int i, j, slice;
  363. int A, B, C;
  364. uint8_t *bsrc;
  365. int slice_start, slice_height;
  366. const int cmask = ~(rmode ? 3 : 1);
  367. const int stride2 = stride << 1;
  368. for (slice = 0; slice < slices; slice++) {
  369. slice_start = ((slice * height) / slices) & cmask;
  370. slice_height = ((((slice + 1) * height) / slices) & cmask) -
  371. slice_start;
  372. slice_height >>= 1;
  373. if (!slice_height)
  374. continue;
  375. bsrc = src + slice_start * stride;
  376. // first line - left neighbour prediction
  377. bsrc[0] += 0x80;
  378. A = bsrc[0];
  379. for (i = step; i < width * step; i += step) {
  380. bsrc[i] += A;
  381. A = bsrc[i];
  382. }
  383. for (i = 0; i < width * step; i += step) {
  384. bsrc[stride + i] += A;
  385. A = bsrc[stride + i];
  386. }
  387. bsrc += stride2;
  388. if (slice_height <= 1)
  389. continue;
  390. // second line - first element has top prediction, the rest uses median
  391. C = bsrc[-stride2];
  392. bsrc[0] += C;
  393. A = bsrc[0];
  394. for (i = step; i < width * step; i += step) {
  395. B = bsrc[i - stride2];
  396. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  397. C = B;
  398. A = bsrc[i];
  399. }
  400. for (i = 0; i < width * step; i += step) {
  401. B = bsrc[i - stride];
  402. bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
  403. C = B;
  404. A = bsrc[stride + i];
  405. }
  406. bsrc += stride2;
  407. // the rest of lines use continuous median prediction
  408. for (j = 2; j < slice_height; j++) {
  409. for (i = 0; i < width * step; i += step) {
  410. B = bsrc[i - stride2];
  411. bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
  412. C = B;
  413. A = bsrc[i];
  414. }
  415. for (i = 0; i < width * step; i += step) {
  416. B = bsrc[i - stride];
  417. bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
  418. C = B;
  419. A = bsrc[i + stride];
  420. }
  421. bsrc += stride2;
  422. }
  423. }
  424. }
  425. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  426. AVPacket *avpkt)
  427. {
  428. const uint8_t *buf = avpkt->data;
  429. int buf_size = avpkt->size;
  430. UtvideoContext *c = avctx->priv_data;
  431. int i, j;
  432. const uint8_t *plane_start[5];
  433. int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
  434. int ret;
  435. GetByteContext gb;
  436. ThreadFrame frame = { .f = data };
  437. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  438. return ret;
  439. /* parse plane structure to get frame flags and validate slice offsets */
  440. bytestream2_init(&gb, buf, buf_size);
  441. if (c->pro) {
  442. c->frame_info = bytestream2_get_le32u(&gb);
  443. c->slices = ((c->frame_info >> 24) & 0xff) + 1;
  444. for (i = 0; i < c->planes; i++) {
  445. plane_size = 0;
  446. plane_start[i] = gb.buffer;
  447. if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
  448. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  449. return AVERROR_INVALIDDATA;
  450. }
  451. for (j = 0; j < c->slices; j++) {
  452. slice_size = bytestream2_get_le32u(&gb);
  453. max_slice_size = FFMAX(max_slice_size, slice_size);
  454. plane_size += slice_size;
  455. }
  456. bytestream2_skipu(&gb, 1024);
  457. bytestream2_skipu(&gb, plane_size);
  458. }
  459. plane_start[c->planes] = gb.buffer;
  460. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  461. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  462. return AVERROR_INVALIDDATA;
  463. }
  464. } else {
  465. for (i = 0; i < c->planes; i++) {
  466. plane_start[i] = gb.buffer;
  467. if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
  468. av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
  469. return AVERROR_INVALIDDATA;
  470. }
  471. bytestream2_skipu(&gb, 256);
  472. slice_start = 0;
  473. slice_end = 0;
  474. for (j = 0; j < c->slices; j++) {
  475. slice_end = bytestream2_get_le32u(&gb);
  476. if (slice_end < 0 || slice_end < slice_start ||
  477. bytestream2_get_bytes_left(&gb) < slice_end) {
  478. av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
  479. return AVERROR_INVALIDDATA;
  480. }
  481. slice_size = slice_end - slice_start;
  482. slice_start = slice_end;
  483. max_slice_size = FFMAX(max_slice_size, slice_size);
  484. }
  485. plane_size = slice_end;
  486. bytestream2_skipu(&gb, plane_size);
  487. }
  488. plane_start[c->planes] = gb.buffer;
  489. if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
  490. av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
  491. return AVERROR_INVALIDDATA;
  492. }
  493. c->frame_info = bytestream2_get_le32u(&gb);
  494. }
  495. av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
  496. c->frame_info);
  497. c->frame_pred = (c->frame_info >> 8) & 3;
  498. if (c->frame_pred == PRED_GRADIENT) {
  499. avpriv_request_sample(avctx, "Frame with gradient prediction");
  500. return AVERROR_PATCHWELCOME;
  501. }
  502. av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
  503. max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
  504. if (!c->slice_bits) {
  505. av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
  506. return AVERROR(ENOMEM);
  507. }
  508. switch (c->avctx->pix_fmt) {
  509. case AV_PIX_FMT_RGB24:
  510. case AV_PIX_FMT_RGBA:
  511. for (i = 0; i < c->planes; i++) {
  512. ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
  513. c->planes, frame.f->linesize[0], avctx->width,
  514. avctx->height, plane_start[i],
  515. c->frame_pred == PRED_LEFT);
  516. if (ret)
  517. return ret;
  518. if (c->frame_pred == PRED_MEDIAN) {
  519. if (!c->interlaced) {
  520. restore_median(frame.f->data[0] + ff_ut_rgb_order[i],
  521. c->planes, frame.f->linesize[0], avctx->width,
  522. avctx->height, c->slices, 0);
  523. } else {
  524. restore_median_il(frame.f->data[0] + ff_ut_rgb_order[i],
  525. c->planes, frame.f->linesize[0],
  526. avctx->width, avctx->height, c->slices,
  527. 0);
  528. }
  529. }
  530. }
  531. restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
  532. avctx->width, avctx->height);
  533. break;
  534. case AV_PIX_FMT_YUV420P:
  535. for (i = 0; i < 3; i++) {
  536. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  537. avctx->width >> !!i, avctx->height >> !!i,
  538. plane_start[i], c->frame_pred == PRED_LEFT);
  539. if (ret)
  540. return ret;
  541. if (c->frame_pred == PRED_MEDIAN) {
  542. if (!c->interlaced) {
  543. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  544. avctx->width >> !!i, avctx->height >> !!i,
  545. c->slices, !i);
  546. } else {
  547. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  548. avctx->width >> !!i,
  549. avctx->height >> !!i,
  550. c->slices, !i);
  551. }
  552. }
  553. }
  554. break;
  555. case AV_PIX_FMT_YUV422P:
  556. for (i = 0; i < 3; i++) {
  557. ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
  558. avctx->width >> !!i, avctx->height,
  559. plane_start[i], c->frame_pred == PRED_LEFT);
  560. if (ret)
  561. return ret;
  562. if (c->frame_pred == PRED_MEDIAN) {
  563. if (!c->interlaced) {
  564. restore_median(frame.f->data[i], 1, frame.f->linesize[i],
  565. avctx->width >> !!i, avctx->height,
  566. c->slices, 0);
  567. } else {
  568. restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
  569. avctx->width >> !!i, avctx->height,
  570. c->slices, 0);
  571. }
  572. }
  573. }
  574. break;
  575. case AV_PIX_FMT_YUV422P10:
  576. for (i = 0; i < 3; i++) {
  577. ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
  578. avctx->width >> !!i, avctx->height,
  579. plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
  580. if (ret)
  581. return ret;
  582. }
  583. break;
  584. }
  585. frame.f->key_frame = 1;
  586. frame.f->pict_type = AV_PICTURE_TYPE_I;
  587. frame.f->interlaced_frame = !!c->interlaced;
  588. *got_frame = 1;
  589. /* always report that the buffer was completely consumed */
  590. return buf_size;
  591. }
  592. static av_cold int decode_init(AVCodecContext *avctx)
  593. {
  594. UtvideoContext * const c = avctx->priv_data;
  595. c->avctx = avctx;
  596. ff_bswapdsp_init(&c->bdsp);
  597. if (avctx->extradata_size >= 16) {
  598. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  599. avctx->extradata[3], avctx->extradata[2],
  600. avctx->extradata[1], avctx->extradata[0]);
  601. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  602. AV_RB32(avctx->extradata + 4));
  603. c->frame_info_size = AV_RL32(avctx->extradata + 8);
  604. c->flags = AV_RL32(avctx->extradata + 12);
  605. if (c->frame_info_size != 4)
  606. avpriv_request_sample(avctx, "Frame info not 4 bytes");
  607. av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
  608. c->slices = (c->flags >> 24) + 1;
  609. c->compression = c->flags & 1;
  610. c->interlaced = c->flags & 0x800;
  611. } else if (avctx->extradata_size == 8) {
  612. av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
  613. avctx->extradata[3], avctx->extradata[2],
  614. avctx->extradata[1], avctx->extradata[0]);
  615. av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
  616. AV_RB32(avctx->extradata + 4));
  617. c->interlaced = 0;
  618. c->pro = 1;
  619. } else {
  620. av_log(avctx, AV_LOG_ERROR,
  621. "Insufficient extradata size %d, should be at least 16\n",
  622. avctx->extradata_size);
  623. return AVERROR_INVALIDDATA;
  624. }
  625. c->slice_bits_size = 0;
  626. switch (avctx->codec_tag) {
  627. case MKTAG('U', 'L', 'R', 'G'):
  628. c->planes = 3;
  629. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  630. break;
  631. case MKTAG('U', 'L', 'R', 'A'):
  632. c->planes = 4;
  633. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  634. break;
  635. case MKTAG('U', 'L', 'Y', '0'):
  636. c->planes = 3;
  637. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  638. avctx->colorspace = AVCOL_SPC_BT470BG;
  639. break;
  640. case MKTAG('U', 'L', 'Y', '2'):
  641. c->planes = 3;
  642. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  643. avctx->colorspace = AVCOL_SPC_BT470BG;
  644. break;
  645. case MKTAG('U', 'Q', 'Y', '2'):
  646. c->planes = 3;
  647. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  648. break;
  649. case MKTAG('U', 'L', 'H', '0'):
  650. c->planes = 3;
  651. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  652. avctx->colorspace = AVCOL_SPC_BT709;
  653. break;
  654. case MKTAG('U', 'L', 'H', '2'):
  655. c->planes = 3;
  656. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  657. avctx->colorspace = AVCOL_SPC_BT709;
  658. break;
  659. default:
  660. av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
  661. avctx->codec_tag);
  662. return AVERROR_INVALIDDATA;
  663. }
  664. return 0;
  665. }
  666. static av_cold int decode_end(AVCodecContext *avctx)
  667. {
  668. UtvideoContext * const c = avctx->priv_data;
  669. av_freep(&c->slice_bits);
  670. return 0;
  671. }
  672. AVCodec ff_utvideo_decoder = {
  673. .name = "utvideo",
  674. .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
  675. .type = AVMEDIA_TYPE_VIDEO,
  676. .id = AV_CODEC_ID_UTVIDEO,
  677. .priv_data_size = sizeof(UtvideoContext),
  678. .init = decode_init,
  679. .close = decode_end,
  680. .decode = decode_frame,
  681. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  682. };