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.

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