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.

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