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