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.

535 lines
13KB

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