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.

420 lines
10KB

  1. /*
  2. * Canopus Lossless Codec decoder
  3. *
  4. * Copyright (c) 2012 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. typedef struct CLLCContext {
  27. DSPContext dsp;
  28. AVCodecContext *avctx;
  29. uint8_t *swapped_buf;
  30. int swapped_buf_size;
  31. } CLLCContext;
  32. static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
  33. {
  34. uint8_t symbols[256];
  35. uint8_t bits[256];
  36. uint16_t codes[256];
  37. int num_lens, num_codes, num_codes_sum, prefix;
  38. int i, j, count;
  39. prefix = 0;
  40. count = 0;
  41. num_codes_sum = 0;
  42. num_lens = get_bits(gb, 5);
  43. for (i = 0; i < num_lens; i++) {
  44. num_codes = get_bits(gb, 9);
  45. num_codes_sum += num_codes;
  46. if (num_codes_sum > 256) {
  47. vlc->table = NULL;
  48. av_log(ctx->avctx, AV_LOG_ERROR,
  49. "Too many VLCs (%d) to be read.\n", num_codes_sum);
  50. return AVERROR_INVALIDDATA;
  51. }
  52. for (j = 0; j < num_codes; j++) {
  53. symbols[count] = get_bits(gb, 8);
  54. bits[count] = i + 1;
  55. codes[count] = prefix++;
  56. count++;
  57. }
  58. prefix <<= 1;
  59. }
  60. return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
  61. codes, 2, 2, symbols, 1, 1, 0);
  62. }
  63. /*
  64. * Unlike the RGB24 read/restore, which reads in a component at a time,
  65. * ARGB read/restore reads in ARGB quads.
  66. */
  67. static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
  68. VLC *vlc, uint8_t *outbuf)
  69. {
  70. uint8_t *dst;
  71. int pred[4];
  72. int code;
  73. int i;
  74. OPEN_READER(bits, gb);
  75. dst = outbuf;
  76. pred[0] = top_left[0];
  77. pred[1] = top_left[1];
  78. pred[2] = top_left[2];
  79. pred[3] = top_left[3];
  80. for (i = 0; i < ctx->avctx->width; i++) {
  81. /* Always get the alpha component */
  82. UPDATE_CACHE(bits, gb);
  83. GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
  84. pred[0] += code;
  85. dst[0] = pred[0];
  86. /* Skip the components if they are entirely transparent */
  87. if (dst[0]) {
  88. /* Red */
  89. UPDATE_CACHE(bits, gb);
  90. GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
  91. pred[1] += code;
  92. dst[1] = pred[1];
  93. /* Green */
  94. UPDATE_CACHE(bits, gb);
  95. GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
  96. pred[2] += code;
  97. dst[2] = pred[2];
  98. /* Blue */
  99. UPDATE_CACHE(bits, gb);
  100. GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
  101. pred[3] += code;
  102. dst[3] = pred[3];
  103. } else {
  104. dst[1] = 0;
  105. dst[2] = 0;
  106. dst[3] = 0;
  107. }
  108. dst += 4;
  109. }
  110. CLOSE_READER(bits, gb);
  111. dst -= 4 * ctx->avctx->width;
  112. top_left[0] = dst[0];
  113. /* Only stash components if they are not transparent */
  114. if (top_left[0]) {
  115. top_left[1] = dst[1];
  116. top_left[2] = dst[2];
  117. top_left[3] = dst[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 = dst[-3 * ctx->avctx->width];
  141. return 0;
  142. }
  143. static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
  144. {
  145. AVCodecContext *avctx = ctx->avctx;
  146. uint8_t *dst;
  147. int pred[4];
  148. int ret;
  149. int i, j;
  150. VLC vlc[4];
  151. pred[0] = 0;
  152. pred[1] = 0x80;
  153. pred[2] = 0x80;
  154. pred[3] = 0x80;
  155. dst = pic->data[0];
  156. skip_bits(gb, 16);
  157. /* Read in code table for each plane */
  158. for (i = 0; i < 4; i++) {
  159. ret = read_code_table(ctx, gb, &vlc[i]);
  160. if (ret < 0) {
  161. for (j = 0; j <= i; j++)
  162. ff_free_vlc(&vlc[j]);
  163. av_log(ctx->avctx, AV_LOG_ERROR,
  164. "Could not read code table %d.\n", i);
  165. return ret;
  166. }
  167. }
  168. /* Read in and restore every line */
  169. for (i = 0; i < avctx->height; i++) {
  170. read_argb_line(ctx, gb, pred, vlc, dst);
  171. dst += pic->linesize[0];
  172. }
  173. for (i = 0; i < 4; i++)
  174. ff_free_vlc(&vlc[i]);
  175. return 0;
  176. }
  177. static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
  178. {
  179. AVCodecContext *avctx = ctx->avctx;
  180. uint8_t *dst;
  181. int pred[3];
  182. int ret;
  183. int i, j;
  184. VLC vlc[3];
  185. pred[0] = 0x80;
  186. pred[1] = 0x80;
  187. pred[2] = 0x80;
  188. dst = pic->data[0];
  189. skip_bits(gb, 16);
  190. /* Read in code table for each plane */
  191. for (i = 0; i < 3; i++) {
  192. ret = read_code_table(ctx, gb, &vlc[i]);
  193. if (ret < 0) {
  194. for (j = 0; j <= i; j++)
  195. ff_free_vlc(&vlc[j]);
  196. av_log(ctx->avctx, AV_LOG_ERROR,
  197. "Could not read code table %d.\n", i);
  198. return ret;
  199. }
  200. }
  201. /* Read in and restore every line */
  202. for (i = 0; i < avctx->height; i++) {
  203. for (j = 0; j < 3; j++)
  204. read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
  205. dst += pic->linesize[0];
  206. }
  207. for (i = 0; i < 3; i++)
  208. ff_free_vlc(&vlc[i]);
  209. return 0;
  210. }
  211. static int cllc_decode_frame(AVCodecContext *avctx, void *data,
  212. int *got_picture_ptr, AVPacket *avpkt)
  213. {
  214. CLLCContext *ctx = avctx->priv_data;
  215. AVFrame *pic = avctx->coded_frame;
  216. uint8_t *src = avpkt->data;
  217. uint32_t info_tag, info_offset;
  218. int data_size;
  219. GetBitContext gb;
  220. int coding_type, ret;
  221. if (pic->data[0])
  222. avctx->release_buffer(avctx, pic);
  223. pic->reference = 0;
  224. /* Skip the INFO header if present */
  225. info_offset = 0;
  226. info_tag = AV_RL32(src);
  227. if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
  228. info_offset = AV_RL32(src + 4);
  229. if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
  230. av_log(avctx, AV_LOG_ERROR,
  231. "Invalid INFO header offset: 0x%08X is too large.\n",
  232. info_offset);
  233. return AVERROR_INVALIDDATA;
  234. }
  235. info_offset += 8;
  236. src += info_offset;
  237. av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
  238. }
  239. data_size = (avpkt->size - info_offset) & ~1;
  240. /* Make sure our bswap16'd buffer is big enough */
  241. av_fast_padded_malloc(&ctx->swapped_buf,
  242. &ctx->swapped_buf_size, data_size);
  243. if (!ctx->swapped_buf) {
  244. av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
  245. return AVERROR(ENOMEM);
  246. }
  247. /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
  248. ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
  249. data_size / 2);
  250. init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
  251. /*
  252. * Read in coding type. The types are as follows:
  253. *
  254. * 0 - YUY2
  255. * 1 - BGR24 (Triples)
  256. * 2 - BGR24 (Quads)
  257. * 3 - BGRA
  258. */
  259. coding_type = (AV_RL32(src) >> 8) & 0xFF;
  260. av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
  261. switch (coding_type) {
  262. case 1:
  263. case 2:
  264. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  265. avctx->bits_per_raw_sample = 8;
  266. ret = avctx->get_buffer(avctx, pic);
  267. if (ret < 0) {
  268. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  269. return ret;
  270. }
  271. ret = decode_rgb24_frame(ctx, &gb, pic);
  272. if (ret < 0)
  273. return ret;
  274. break;
  275. case 3:
  276. avctx->pix_fmt = AV_PIX_FMT_ARGB;
  277. avctx->bits_per_raw_sample = 8;
  278. ret = avctx->get_buffer(avctx, pic);
  279. if (ret < 0) {
  280. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  281. return ret;
  282. }
  283. ret = decode_argb_frame(ctx, &gb, pic);
  284. if (ret < 0)
  285. return ret;
  286. break;
  287. default:
  288. av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
  289. return AVERROR_INVALIDDATA;
  290. }
  291. pic->key_frame = 1;
  292. pic->pict_type = AV_PICTURE_TYPE_I;
  293. *got_picture_ptr = 1;
  294. *(AVFrame *)data = *pic;
  295. return avpkt->size;
  296. }
  297. static av_cold int cllc_decode_close(AVCodecContext *avctx)
  298. {
  299. CLLCContext *ctx = avctx->priv_data;
  300. if (avctx->coded_frame->data[0])
  301. avctx->release_buffer(avctx, avctx->coded_frame);
  302. av_freep(&avctx->coded_frame);
  303. av_freep(&ctx->swapped_buf);
  304. return 0;
  305. }
  306. static av_cold int cllc_decode_init(AVCodecContext *avctx)
  307. {
  308. CLLCContext *ctx = avctx->priv_data;
  309. /* Initialize various context values */
  310. ctx->avctx = avctx;
  311. ctx->swapped_buf = NULL;
  312. ctx->swapped_buf_size = 0;
  313. ff_dsputil_init(&ctx->dsp, avctx);
  314. avctx->coded_frame = avcodec_alloc_frame();
  315. if (!avctx->coded_frame) {
  316. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  317. return AVERROR(ENOMEM);
  318. }
  319. return 0;
  320. }
  321. AVCodec ff_cllc_decoder = {
  322. .name = "cllc",
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. .id = AV_CODEC_ID_CLLC,
  325. .priv_data_size = sizeof(CLLCContext),
  326. .init = cllc_decode_init,
  327. .decode = cllc_decode_frame,
  328. .close = cllc_decode_close,
  329. .capabilities = CODEC_CAP_DR1,
  330. .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
  331. };