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.

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