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.

421 lines
10KB

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