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.

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