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.

487 lines
17KB

  1. /*
  2. * YUY2 Lossless Codec
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #define YLC_VLC_BITS 10
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mem.h"
  28. #include "avcodec.h"
  29. #include "bswapdsp.h"
  30. #include "get_bits.h"
  31. #include "huffyuvdsp.h"
  32. #include "internal.h"
  33. #include "thread.h"
  34. #include "unary.h"
  35. typedef struct YLCContext {
  36. VLC vlc[4];
  37. uint32_t table[1024];
  38. uint8_t *table_bits;
  39. uint8_t *bitstream_bits;
  40. int table_bits_size;
  41. int bitstream_bits_size;
  42. BswapDSPContext bdsp;
  43. } YLCContext;
  44. static av_cold int decode_init(AVCodecContext *avctx)
  45. {
  46. YLCContext *s = avctx->priv_data;
  47. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  48. ff_bswapdsp_init(&s->bdsp);
  49. return 0;
  50. }
  51. typedef struct Node {
  52. int16_t sym;
  53. int16_t n0;
  54. uint32_t count;
  55. int16_t l, r;
  56. } Node;
  57. static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat,
  58. Node *nodes, int node,
  59. uint32_t pfx, int pl, int *pos)
  60. {
  61. int s;
  62. s = nodes[node].sym;
  63. if (s != -1) {
  64. bits[*pos] = (~pfx) & ((1ULL << FFMAX(pl, 1)) - 1);
  65. lens[*pos] = FFMAX(pl, 1);
  66. xlat[*pos] = s + (pl == 0);
  67. (*pos)++;
  68. } else {
  69. pfx <<= 1;
  70. pl++;
  71. get_tree_codes(bits, lens, xlat, nodes, nodes[node].l, pfx, pl,
  72. pos);
  73. pfx |= 1;
  74. get_tree_codes(bits, lens, xlat, nodes, nodes[node].r, pfx, pl,
  75. pos);
  76. }
  77. }
  78. static int build_vlc(AVCodecContext *avctx, VLC *vlc, const uint32_t *table)
  79. {
  80. Node nodes[512];
  81. uint32_t bits[256];
  82. int16_t lens[256];
  83. uint8_t xlat[256];
  84. int cur_node, i, j, pos = 0;
  85. ff_free_vlc(vlc);
  86. for (i = 0; i < 256; i++) {
  87. nodes[i].count = table[i];
  88. nodes[i].sym = i;
  89. nodes[i].n0 = -2;
  90. nodes[i].l = i;
  91. nodes[i].r = i;
  92. }
  93. cur_node = 256;
  94. j = 0;
  95. do {
  96. for (i = 0; ; i++) {
  97. int new_node = j;
  98. int first_node = cur_node;
  99. int second_node = cur_node;
  100. unsigned nd, st;
  101. nodes[cur_node].count = -1;
  102. do {
  103. int val = nodes[new_node].count;
  104. if (val && (val < nodes[first_node].count)) {
  105. if (val >= nodes[second_node].count) {
  106. first_node = new_node;
  107. } else {
  108. first_node = second_node;
  109. second_node = new_node;
  110. }
  111. }
  112. new_node += 1;
  113. } while (new_node != cur_node);
  114. if (first_node == cur_node)
  115. break;
  116. nd = nodes[second_node].count;
  117. st = nodes[first_node].count;
  118. nodes[second_node].count = 0;
  119. nodes[first_node].count = 0;
  120. if (nd >= UINT32_MAX - st) {
  121. av_log(avctx, AV_LOG_ERROR, "count overflow\n");
  122. return AVERROR_INVALIDDATA;
  123. }
  124. nodes[cur_node].count = nd + st;
  125. nodes[cur_node].sym = -1;
  126. nodes[cur_node].n0 = cur_node;
  127. nodes[cur_node].l = first_node;
  128. nodes[cur_node].r = second_node;
  129. cur_node++;
  130. }
  131. j++;
  132. } while (cur_node - 256 == j);
  133. get_tree_codes(bits, lens, xlat, nodes, cur_node - 1, 0, 0, &pos);
  134. return ff_init_vlc_sparse(vlc, YLC_VLC_BITS, pos, lens, 2, 2,
  135. bits, 4, 4, xlat, 1, 1, 0);
  136. }
  137. static const uint8_t table_y1[] = {
  138. 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
  139. 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
  140. 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
  141. 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
  142. 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
  143. 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
  144. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  145. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  146. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  147. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  148. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  149. 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  150. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  155. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  156. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  157. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  158. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  159. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  160. 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
  161. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  162. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  163. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  164. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  165. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  166. 0x02, 0x00,
  167. };
  168. static const uint8_t table_u[] = {
  169. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  170. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
  171. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  173. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  174. 0x01, 0x01, 0x01, 0x01, 0x01, 0xFF, 0xFF, 0xFF,
  175. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  176. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
  179. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  180. 0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  181. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  182. 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  183. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  185. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFF,
  186. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  187. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
  188. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
  190. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  191. 0x01, 0x01, 0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
  192. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  193. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  195. 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  196. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  197. 0x01, 0x00,
  198. };
  199. static const uint8_t table_y2[] = {
  200. 0xFC, 0xFC, 0xFC, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE,
  201. 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFC,
  202. 0xFC, 0xFC, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE,
  203. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFC, 0xFC,
  204. 0xFC, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF,
  205. 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFD, 0xFD, 0xFD,
  206. 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
  207. 0x00, 0x01, 0x01, 0x01, 0xFD, 0xFD, 0xFD, 0xFE,
  208. 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
  209. 0x01, 0x01, 0x01, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE,
  210. 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
  211. 0x01, 0x01, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
  212. 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02,
  213. 0x02, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00,
  214. 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
  215. 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
  216. 0x00, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0xFF,
  217. 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
  218. 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0xFF, 0xFF,
  219. 0xFF, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02,
  220. 0x02, 0x02, 0x03, 0x03, 0x03, 0xFF, 0xFF, 0xFF,
  221. 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02,
  222. 0x02, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x01,
  223. 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03,
  224. 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01,
  225. 0x01, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x04,
  226. 0x04, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
  227. 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x04, 0x04,
  228. 0x04, 0x00,
  229. };
  230. static const uint8_t table_v[] = {
  231. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  232. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  233. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  234. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  235. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  236. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  237. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  238. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  239. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  240. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  241. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  242. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  243. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  244. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  245. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  246. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  247. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  248. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  249. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  250. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  251. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  252. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  253. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  254. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  255. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  256. 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF,
  257. 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01,
  258. 0xFF, 0x00, 0x01, 0xFF, 0x00, 0x01, 0xFF, 0x00,
  259. 0x01, 0x00,
  260. };
  261. static int decode_frame(AVCodecContext *avctx,
  262. void *data, int *got_frame,
  263. AVPacket *avpkt)
  264. {
  265. int TL[4] = { 128, 128, 128, 128 };
  266. int L[4] = { 128, 128, 128, 128 };
  267. YLCContext *s = avctx->priv_data;
  268. ThreadFrame frame = { .f = data };
  269. const uint8_t *buf = avpkt->data;
  270. int ret, x, y, toffset, boffset;
  271. AVFrame * const p = data;
  272. GetBitContext gb;
  273. uint8_t *dst;
  274. if (avpkt->size <= 16)
  275. return AVERROR_INVALIDDATA;
  276. if (AV_RL32(buf) != MKTAG('Y', 'L', 'C', '0') ||
  277. AV_RL32(buf + 4) != 0)
  278. return AVERROR_INVALIDDATA;
  279. toffset = AV_RL32(buf + 8);
  280. if (toffset < 16 || toffset >= avpkt->size)
  281. return AVERROR_INVALIDDATA;
  282. boffset = AV_RL32(buf + 12);
  283. if (toffset >= boffset || boffset >= avpkt->size)
  284. return AVERROR_INVALIDDATA;
  285. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  286. return ret;
  287. av_fast_malloc(&s->table_bits, &s->table_bits_size,
  288. boffset - toffset + AV_INPUT_BUFFER_PADDING_SIZE);
  289. if (!s->table_bits)
  290. return AVERROR(ENOMEM);
  291. memcpy(s->table_bits, avpkt->data + toffset, boffset - toffset);
  292. memset(s->table_bits + boffset - toffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  293. s->bdsp.bswap_buf((uint32_t *) s->table_bits,
  294. (uint32_t *) s->table_bits,
  295. (boffset - toffset + 3) >> 2);
  296. if ((ret = init_get_bits8(&gb, s->table_bits, boffset - toffset)) < 0)
  297. return ret;
  298. for (x = 0; x < 1024; x++) {
  299. unsigned len = get_unary(&gb, 1, 31);
  300. uint32_t val = ((1U << len) - 1) + get_bits_long(&gb, len);
  301. s->table[x] = val;
  302. }
  303. ret = build_vlc(avctx, &s->vlc[0], &s->table[0 ]);
  304. if (ret < 0)
  305. return ret;
  306. ret = build_vlc(avctx, &s->vlc[1], &s->table[256]);
  307. if (ret < 0)
  308. return ret;
  309. ret = build_vlc(avctx, &s->vlc[2], &s->table[512]);
  310. if (ret < 0)
  311. return ret;
  312. ret = build_vlc(avctx, &s->vlc[3], &s->table[768]);
  313. if (ret < 0)
  314. return ret;
  315. av_fast_malloc(&s->bitstream_bits, &s->bitstream_bits_size,
  316. avpkt->size - boffset + AV_INPUT_BUFFER_PADDING_SIZE);
  317. if (!s->bitstream_bits)
  318. return AVERROR(ENOMEM);
  319. memcpy(s->bitstream_bits, avpkt->data + boffset, avpkt->size - boffset);
  320. memset(s->bitstream_bits + avpkt->size - boffset, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  321. s->bdsp.bswap_buf((uint32_t *) s->bitstream_bits,
  322. (uint32_t *) s->bitstream_bits,
  323. (avpkt->size - boffset) >> 2);
  324. if ((ret = init_get_bits8(&gb, s->bitstream_bits, avpkt->size - boffset)) < 0)
  325. return ret;
  326. dst = p->data[0];
  327. for (y = 0; y < avctx->height; y++) {
  328. memset(dst, 0, avctx->width * 2);
  329. dst += p->linesize[0];
  330. }
  331. dst = p->data[0];
  332. for (y = 0; y < avctx->height; y++) {
  333. for (x = 0; x < avctx->width * 2 && y < avctx->height;) {
  334. if (get_bits_left(&gb) <= 0)
  335. return AVERROR_INVALIDDATA;
  336. if (get_bits1(&gb)) {
  337. int val = get_vlc2(&gb, s->vlc[0].table, YLC_VLC_BITS, 3);
  338. if (val < 0) {
  339. return AVERROR_INVALIDDATA;
  340. } else if (val < 0xE1) {
  341. dst[x ] = table_y1[val];
  342. dst[x + 1] = table_u[val];
  343. dst[x + 2] = table_y2[val];
  344. dst[x + 3] = table_v[val];
  345. x += 4;
  346. } else {
  347. int incr = (val - 0xDF) * 4;
  348. if (x + incr >= avctx->width * 2) {
  349. int iy = ((x + incr) / (avctx->width * 2));
  350. x = (x + incr) % (avctx->width * 2);
  351. y += iy;
  352. dst += iy * p->linesize[0];
  353. } else {
  354. x += incr;
  355. }
  356. }
  357. } else {
  358. int y1, y2, u, v;
  359. y1 = get_vlc2(&gb, s->vlc[1].table, YLC_VLC_BITS, 3);
  360. u = get_vlc2(&gb, s->vlc[2].table, YLC_VLC_BITS, 3);
  361. y2 = get_vlc2(&gb, s->vlc[1].table, YLC_VLC_BITS, 3);
  362. v = get_vlc2(&gb, s->vlc[3].table, YLC_VLC_BITS, 3);
  363. if (y1 < 0 || y2 < 0 || u < 0 || v < 0)
  364. return AVERROR_INVALIDDATA;
  365. dst[x ] = y1;
  366. dst[x + 1] = u;
  367. dst[x + 2] = y1 + y2;
  368. dst[x + 3] = v;
  369. x += 4;
  370. }
  371. }
  372. dst += p->linesize[0];
  373. }
  374. dst = p->data[0];
  375. for (x = 0; x < avctx->width * 2; x += 4) {
  376. dst[x ] = dst[x ] + L[0];
  377. dst[x + 2] = L[0] = dst[x + 2] + L[0];
  378. L[1] = dst[x + 1] + L[1];
  379. dst[x + 1] = L[1];
  380. L[2] = dst[x + 3] + L[2];
  381. dst[x + 3] = L[2];
  382. }
  383. dst += p->linesize[0];
  384. for (y = 1; y < avctx->height; y++) {
  385. x = 0;
  386. dst[x ] = dst[x ] + L[0] + dst[x + 0 - p->linesize[0]] - TL[0];
  387. dst[x + 2] = L[0] = dst[x + 2] + L[0] + dst[x + 2 - p->linesize[0]] - TL[0];
  388. TL[0] = dst[x + 2 - p->linesize[0]];
  389. L[1] = dst[x + 1] + L[1] + dst[x + 1 - p->linesize[0]] - TL[1];
  390. dst[x + 1] = L[1];
  391. TL[1] = dst[x + 1 - p->linesize[0]];
  392. L[2] = dst[x + 3] + L[2] + dst[x + 3 - p->linesize[0]] - TL[2];
  393. dst[x + 3] = L[2];
  394. TL[2] = dst[x + 3 - p->linesize[0]];
  395. for (x = 4; x < avctx->width * 2; x += 4) {
  396. dst[x ] = dst[x ] + L[0] + dst[x + 0 - p->linesize[0]] - TL[0];
  397. dst[x + 2] = L[0] = dst[x + 2] + L[0] + dst[x + 2 - p->linesize[0]] - TL[0];
  398. TL[0] = dst[x + 2 - p->linesize[0]];
  399. L[1] = dst[x + 1] + L[1] + dst[x + 1 - p->linesize[0]] - TL[1];
  400. dst[x + 1] = L[1];
  401. TL[1] = dst[x + 1 - p->linesize[0]];
  402. L[2] = dst[x + 3] + L[2] + dst[x + 3 - p->linesize[0]] - TL[2];
  403. dst[x + 3] = L[2];
  404. TL[2] = dst[x + 3 - p->linesize[0]];
  405. }
  406. dst += p->linesize[0];
  407. }
  408. p->pict_type = AV_PICTURE_TYPE_I;
  409. p->key_frame = 1;
  410. *got_frame = 1;
  411. return avpkt->size;
  412. }
  413. static av_cold int decode_end(AVCodecContext *avctx)
  414. {
  415. YLCContext *s = avctx->priv_data;
  416. ff_free_vlc(&s->vlc[0]);
  417. ff_free_vlc(&s->vlc[1]);
  418. ff_free_vlc(&s->vlc[2]);
  419. ff_free_vlc(&s->vlc[3]);
  420. av_freep(&s->table_bits);
  421. s->table_bits_size = 0;
  422. av_freep(&s->bitstream_bits);
  423. s->bitstream_bits_size = 0;
  424. return 0;
  425. }
  426. AVCodec ff_ylc_decoder = {
  427. .name = "ylc",
  428. .long_name = NULL_IF_CONFIG_SMALL("YUY2 Lossless Codec"),
  429. .type = AVMEDIA_TYPE_VIDEO,
  430. .id = AV_CODEC_ID_YLC,
  431. .priv_data_size = sizeof(YLCContext),
  432. .init = decode_init,
  433. .close = decode_end,
  434. .decode = decode_frame,
  435. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  436. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  437. };