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.

502 lines
12KB

  1. /*
  2. * Canopus Lossless Codec decoder
  3. *
  4. * Copyright (c) 2012-2013 Derek Buitenhuis
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "dsputil.h"
  24. #include "get_bits.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. typedef struct CLLCContext {
  28. DSPContext dsp;
  29. AVCodecContext *avctx;
  30. uint8_t *swapped_buf;
  31. int swapped_buf_size;
  32. } CLLCContext;
  33. static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
  34. {
  35. uint8_t symbols[256];
  36. uint8_t bits[256];
  37. uint16_t codes[256];
  38. int num_lens, num_codes, num_codes_sum, prefix;
  39. int i, j, count;
  40. prefix = 0;
  41. count = 0;
  42. num_codes_sum = 0;
  43. num_lens = get_bits(gb, 5);
  44. for (i = 0; i < num_lens; i++) {
  45. num_codes = get_bits(gb, 9);
  46. num_codes_sum += num_codes;
  47. if (num_codes_sum > 256) {
  48. vlc->table = NULL;
  49. av_log(ctx->avctx, AV_LOG_ERROR,
  50. "Too many VLCs (%d) to be read.\n", num_codes_sum);
  51. return AVERROR_INVALIDDATA;
  52. }
  53. for (j = 0; j < num_codes; j++) {
  54. symbols[count] = get_bits(gb, 8);
  55. bits[count] = i + 1;
  56. codes[count] = prefix++;
  57. count++;
  58. }
  59. prefix <<= 1;
  60. }
  61. return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
  62. codes, 2, 2, symbols, 1, 1, 0);
  63. }
  64. /*
  65. * Unlike the RGB24 read/restore, which reads in a component at a time,
  66. * ARGB read/restore reads in ARGB quads.
  67. */
  68. static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
  69. VLC *vlc, uint8_t *outbuf)
  70. {
  71. uint8_t *dst;
  72. int pred[4];
  73. int code;
  74. int i;
  75. OPEN_READER(bits, gb);
  76. dst = outbuf;
  77. pred[0] = top_left[0];
  78. pred[1] = top_left[1];
  79. pred[2] = top_left[2];
  80. pred[3] = top_left[3];
  81. for (i = 0; i < ctx->avctx->width; i++) {
  82. /* Always get the alpha component */
  83. UPDATE_CACHE(bits, gb);
  84. GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
  85. pred[0] += code;
  86. dst[0] = pred[0];
  87. /* Skip the components if they are entirely transparent */
  88. if (dst[0]) {
  89. /* Red */
  90. UPDATE_CACHE(bits, gb);
  91. GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
  92. pred[1] += code;
  93. dst[1] = pred[1];
  94. /* Green */
  95. UPDATE_CACHE(bits, gb);
  96. GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
  97. pred[2] += code;
  98. dst[2] = pred[2];
  99. /* Blue */
  100. UPDATE_CACHE(bits, gb);
  101. GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
  102. pred[3] += code;
  103. dst[3] = pred[3];
  104. } else {
  105. dst[1] = 0;
  106. dst[2] = 0;
  107. dst[3] = 0;
  108. }
  109. dst += 4;
  110. }
  111. CLOSE_READER(bits, gb);
  112. top_left[0] = outbuf[0];
  113. /* Only stash components if they are not transparent */
  114. if (top_left[0]) {
  115. top_left[1] = outbuf[1];
  116. top_left[2] = outbuf[2];
  117. top_left[3] = outbuf[3];
  118. }
  119. return 0;
  120. }
  121. static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
  122. int *top_left, VLC *vlc, uint8_t *outbuf)
  123. {
  124. uint8_t *dst;
  125. int pred, code;
  126. int i;
  127. OPEN_READER(bits, gb);
  128. dst = outbuf;
  129. pred = *top_left;
  130. /* Simultaneously read and restore the line */
  131. for (i = 0; i < ctx->avctx->width; i++) {
  132. UPDATE_CACHE(bits, gb);
  133. GET_VLC(code, bits, gb, vlc->table, 7, 2);
  134. pred += code;
  135. dst[0] = pred;
  136. dst += 3;
  137. }
  138. CLOSE_READER(bits, gb);
  139. /* Stash the first pixel */
  140. *top_left = outbuf[0];
  141. return 0;
  142. }
  143. static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
  144. int *top_left, VLC *vlc, uint8_t *outbuf,
  145. int is_chroma)
  146. {
  147. int pred, code;
  148. int i;
  149. OPEN_READER(bits, gb);
  150. pred = *top_left;
  151. /* Simultaneously read and restore the line */
  152. for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
  153. UPDATE_CACHE(bits, gb);
  154. GET_VLC(code, bits, gb, vlc->table, 7, 2);
  155. pred += code;
  156. outbuf[i] = pred;
  157. }
  158. CLOSE_READER(bits, gb);
  159. /* Stash the first pixel */
  160. *top_left = outbuf[0];
  161. return 0;
  162. }
  163. static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
  164. {
  165. AVCodecContext *avctx = ctx->avctx;
  166. uint8_t *dst;
  167. int pred[4];
  168. int ret;
  169. int i, j;
  170. VLC vlc[4];
  171. pred[0] = 0;
  172. pred[1] = 0x80;
  173. pred[2] = 0x80;
  174. pred[3] = 0x80;
  175. dst = pic->data[0];
  176. skip_bits(gb, 16);
  177. /* Read in code table for each plane */
  178. for (i = 0; i < 4; i++) {
  179. ret = read_code_table(ctx, gb, &vlc[i]);
  180. if (ret < 0) {
  181. for (j = 0; j <= i; j++)
  182. ff_free_vlc(&vlc[j]);
  183. av_log(ctx->avctx, AV_LOG_ERROR,
  184. "Could not read code table %d.\n", i);
  185. return ret;
  186. }
  187. }
  188. /* Read in and restore every line */
  189. for (i = 0; i < avctx->height; i++) {
  190. read_argb_line(ctx, gb, pred, vlc, dst);
  191. dst += pic->linesize[0];
  192. }
  193. for (i = 0; i < 4; i++)
  194. ff_free_vlc(&vlc[i]);
  195. return 0;
  196. }
  197. static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
  198. {
  199. AVCodecContext *avctx = ctx->avctx;
  200. uint8_t *dst;
  201. int pred[3];
  202. int ret;
  203. int i, j;
  204. VLC vlc[3];
  205. pred[0] = 0x80;
  206. pred[1] = 0x80;
  207. pred[2] = 0x80;
  208. dst = pic->data[0];
  209. skip_bits(gb, 16);
  210. /* Read in code table for each plane */
  211. for (i = 0; i < 3; i++) {
  212. ret = read_code_table(ctx, gb, &vlc[i]);
  213. if (ret < 0) {
  214. for (j = 0; j <= i; j++)
  215. ff_free_vlc(&vlc[j]);
  216. av_log(ctx->avctx, AV_LOG_ERROR,
  217. "Could not read code table %d.\n", i);
  218. return ret;
  219. }
  220. }
  221. /* Read in and restore every line */
  222. for (i = 0; i < avctx->height; i++) {
  223. for (j = 0; j < 3; j++)
  224. read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
  225. dst += pic->linesize[0];
  226. }
  227. for (i = 0; i < 3; i++)
  228. ff_free_vlc(&vlc[i]);
  229. return 0;
  230. }
  231. static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
  232. {
  233. AVCodecContext *avctx = ctx->avctx;
  234. uint8_t block;
  235. uint8_t *dst[3];
  236. int pred[3];
  237. int ret;
  238. int i, j;
  239. VLC vlc[2];
  240. pred[0] = 0x80;
  241. pred[1] = 0x80;
  242. pred[2] = 0x80;
  243. dst[0] = pic->data[0];
  244. dst[1] = pic->data[1];
  245. dst[2] = pic->data[2];
  246. skip_bits(gb, 8);
  247. block = get_bits(gb, 8);
  248. if (block) {
  249. avpriv_request_sample(ctx->avctx, "Blocked YUV");
  250. return AVERROR_PATCHWELCOME;
  251. }
  252. /* Read in code table for luma and chroma */
  253. for (i = 0; i < 2; i++) {
  254. ret = read_code_table(ctx, gb, &vlc[i]);
  255. if (ret < 0) {
  256. for (j = 0; j <= i; j++)
  257. ff_free_vlc(&vlc[j]);
  258. av_log(ctx->avctx, AV_LOG_ERROR,
  259. "Could not read code table %d.\n", i);
  260. return ret;
  261. }
  262. }
  263. /* Read in and restore every line */
  264. for (i = 0; i < avctx->height; i++) {
  265. read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
  266. read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
  267. read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
  268. for (j = 0; j < 3; j++)
  269. dst[j] += pic->linesize[j];
  270. }
  271. for (i = 0; i < 2; i++)
  272. ff_free_vlc(&vlc[i]);
  273. return 0;
  274. }
  275. static int cllc_decode_frame(AVCodecContext *avctx, void *data,
  276. int *got_picture_ptr, AVPacket *avpkt)
  277. {
  278. CLLCContext *ctx = avctx->priv_data;
  279. AVFrame *pic = data;
  280. uint8_t *src = avpkt->data;
  281. uint32_t info_tag, info_offset;
  282. int data_size;
  283. GetBitContext gb;
  284. int coding_type, ret;
  285. /* Skip the INFO header if present */
  286. info_offset = 0;
  287. info_tag = AV_RL32(src);
  288. if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
  289. info_offset = AV_RL32(src + 4);
  290. if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
  291. av_log(avctx, AV_LOG_ERROR,
  292. "Invalid INFO header offset: 0x%08X is too large.\n",
  293. info_offset);
  294. return AVERROR_INVALIDDATA;
  295. }
  296. info_offset += 8;
  297. src += info_offset;
  298. av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
  299. }
  300. data_size = (avpkt->size - info_offset) & ~1;
  301. /* Make sure our bswap16'd buffer is big enough */
  302. av_fast_padded_malloc(&ctx->swapped_buf,
  303. &ctx->swapped_buf_size, data_size);
  304. if (!ctx->swapped_buf) {
  305. av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
  306. return AVERROR(ENOMEM);
  307. }
  308. /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
  309. ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
  310. data_size / 2);
  311. init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
  312. /*
  313. * Read in coding type. The types are as follows:
  314. *
  315. * 0 - YUY2
  316. * 1 - BGR24 (Triples)
  317. * 2 - BGR24 (Quads)
  318. * 3 - BGRA
  319. */
  320. coding_type = (AV_RL32(src) >> 8) & 0xFF;
  321. av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
  322. switch (coding_type) {
  323. case 0:
  324. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  325. avctx->bits_per_raw_sample = 8;
  326. ret = ff_get_buffer(avctx, pic, 0);
  327. if (ret < 0) {
  328. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  329. return ret;
  330. }
  331. ret = decode_yuv_frame(ctx, &gb, pic);
  332. if (ret < 0)
  333. return ret;
  334. break;
  335. case 1:
  336. case 2:
  337. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  338. avctx->bits_per_raw_sample = 8;
  339. ret = ff_get_buffer(avctx, pic, 0);
  340. if (ret < 0) {
  341. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  342. return ret;
  343. }
  344. ret = decode_rgb24_frame(ctx, &gb, pic);
  345. if (ret < 0)
  346. return ret;
  347. break;
  348. case 3:
  349. avctx->pix_fmt = AV_PIX_FMT_ARGB;
  350. avctx->bits_per_raw_sample = 8;
  351. ret = ff_get_buffer(avctx, pic, 0);
  352. if (ret < 0) {
  353. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  354. return ret;
  355. }
  356. ret = decode_argb_frame(ctx, &gb, pic);
  357. if (ret < 0)
  358. return ret;
  359. break;
  360. default:
  361. av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
  362. return AVERROR_INVALIDDATA;
  363. }
  364. pic->key_frame = 1;
  365. pic->pict_type = AV_PICTURE_TYPE_I;
  366. *got_picture_ptr = 1;
  367. return avpkt->size;
  368. }
  369. static av_cold int cllc_decode_close(AVCodecContext *avctx)
  370. {
  371. CLLCContext *ctx = avctx->priv_data;
  372. av_freep(&ctx->swapped_buf);
  373. return 0;
  374. }
  375. static av_cold int cllc_decode_init(AVCodecContext *avctx)
  376. {
  377. CLLCContext *ctx = avctx->priv_data;
  378. /* Initialize various context values */
  379. ctx->avctx = avctx;
  380. ctx->swapped_buf = NULL;
  381. ctx->swapped_buf_size = 0;
  382. ff_dsputil_init(&ctx->dsp, avctx);
  383. return 0;
  384. }
  385. AVCodec ff_cllc_decoder = {
  386. .name = "cllc",
  387. .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
  388. .type = AVMEDIA_TYPE_VIDEO,
  389. .id = AV_CODEC_ID_CLLC,
  390. .priv_data_size = sizeof(CLLCContext),
  391. .init = cllc_decode_init,
  392. .decode = cllc_decode_frame,
  393. .close = cllc_decode_close,
  394. .capabilities = CODEC_CAP_DR1,
  395. };