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.

285 lines
7.4KB

  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. typedef struct CLLCContext {
  27. DSPContext dsp;
  28. AVCodecContext *avctx;
  29. uint8_t *swapped_buf;
  30. int swapped_buf_size;
  31. } CLLCContext;
  32. static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
  33. {
  34. uint8_t symbols[256];
  35. uint8_t bits[256];
  36. uint16_t codes[256];
  37. int num_lens, num_codes, num_codes_sum, prefix;
  38. int i, j, count;
  39. prefix = 0;
  40. count = 0;
  41. num_codes_sum = 0;
  42. num_lens = get_bits(gb, 5);
  43. for (i = 0; i < num_lens; i++) {
  44. num_codes = get_bits(gb, 9);
  45. num_codes_sum += num_codes;
  46. if (num_codes_sum > 256) {
  47. vlc->table = NULL;
  48. av_log(ctx->avctx, AV_LOG_ERROR,
  49. "Too many VLCs (%d) to be read.\n", num_codes_sum);
  50. return AVERROR_INVALIDDATA;
  51. }
  52. for (j = 0; j < num_codes; j++) {
  53. symbols[count] = get_bits(gb, 8);
  54. bits[count] = i + 1;
  55. codes[count] = prefix++;
  56. count++;
  57. }
  58. prefix <<= 1;
  59. }
  60. return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
  61. codes, 2, 2, symbols, 1, 1, 0);
  62. }
  63. static int read_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
  64. VLC *vlc, uint8_t *outbuf)
  65. {
  66. uint8_t *dst;
  67. int pred, code;
  68. int i;
  69. OPEN_READER(bits, gb);
  70. dst = outbuf;
  71. pred = *top_left;
  72. /* Simultaneously read and restore the line */
  73. for (i = 0; i < ctx->avctx->width; i++) {
  74. UPDATE_CACHE(bits, gb);
  75. GET_VLC(code, bits, gb, vlc->table, 7, 2);
  76. pred += code;
  77. dst[0] = pred;
  78. dst += 3;
  79. }
  80. CLOSE_READER(bits, gb);
  81. /* Stash the first pixel */
  82. *top_left = dst[-3 * ctx->avctx->width];
  83. return 0;
  84. }
  85. static int decode_bgr24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
  86. {
  87. AVCodecContext *avctx = ctx->avctx;
  88. uint8_t *dst;
  89. int pred[3];
  90. int ret;
  91. int i, j;
  92. VLC vlc[3];
  93. pred[0] = 0x80;
  94. pred[1] = 0x80;
  95. pred[2] = 0x80;
  96. dst = pic->data[0];
  97. skip_bits(gb, 16);
  98. /* Read in code table for each plane */
  99. for (i = 0; i < 3; i++) {
  100. ret = read_code_table(ctx, gb, &vlc[i]);
  101. if (ret < 0) {
  102. for (j = 0; j <= i; j++)
  103. ff_free_vlc(&vlc[j]);
  104. av_log(ctx->avctx, AV_LOG_ERROR,
  105. "Could not read code table %d.\n", i);
  106. return ret;
  107. }
  108. }
  109. /* Read in and restore every line */
  110. for (i = 0; i < avctx->height; i++) {
  111. for (j = 0; j < 3; j++)
  112. read_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
  113. dst += pic->linesize[0];
  114. }
  115. for (i = 0; i < 3; i++)
  116. ff_free_vlc(&vlc[i]);
  117. return 0;
  118. }
  119. static int cllc_decode_frame(AVCodecContext *avctx, void *data,
  120. int *got_picture_ptr, AVPacket *avpkt)
  121. {
  122. CLLCContext *ctx = avctx->priv_data;
  123. AVFrame *pic = avctx->coded_frame;
  124. uint8_t *src = avpkt->data;
  125. uint8_t *swapped_buf_new;
  126. uint32_t info_tag, info_offset;
  127. GetBitContext gb;
  128. int coding_type, ret;
  129. if (pic->data[0])
  130. avctx->release_buffer(avctx, pic);
  131. pic->reference = 0;
  132. /* Make sure our bswap16'd buffer is big enough */
  133. swapped_buf_new = av_fast_realloc(ctx->swapped_buf,
  134. &ctx->swapped_buf_size, avpkt->size);
  135. if (!swapped_buf_new) {
  136. av_log(avctx, AV_LOG_ERROR, "Could not realloc swapped buffer.\n");
  137. return AVERROR(ENOMEM);
  138. }
  139. ctx->swapped_buf = swapped_buf_new;
  140. /* Skip the INFO header if present */
  141. info_offset = 0;
  142. info_tag = AV_RL32(src);
  143. if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
  144. info_offset = AV_RL32(src + 4);
  145. if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
  146. av_log(avctx, AV_LOG_ERROR,
  147. "Invalid INFO header offset: 0x%08X is too large.\n",
  148. info_offset);
  149. return AVERROR_INVALIDDATA;
  150. }
  151. info_offset += 8;
  152. src += info_offset;
  153. av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
  154. }
  155. /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
  156. ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
  157. (avpkt->size - info_offset) / 2);
  158. init_get_bits(&gb, ctx->swapped_buf, (avpkt->size - info_offset) * 8);
  159. /*
  160. * Read in coding type. The types are as follows:
  161. *
  162. * 0 - YUY2
  163. * 1 - BGR24 (Triples)
  164. * 2 - BGR24 (Quads)
  165. * 3 - BGRA
  166. */
  167. coding_type = (AV_RL32(src) >> 8) & 0xFF;
  168. av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
  169. switch (coding_type) {
  170. case 1:
  171. avctx->pix_fmt = PIX_FMT_RGB24;
  172. avctx->bits_per_raw_sample = 8;
  173. ret = avctx->get_buffer(avctx, pic);
  174. if (ret < 0) {
  175. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  176. return ret;
  177. }
  178. ret = decode_bgr24_frame(ctx, &gb, pic);
  179. if (ret < 0)
  180. return ret;
  181. break;
  182. default:
  183. av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
  184. return AVERROR_INVALIDDATA;
  185. }
  186. pic->key_frame = 1;
  187. pic->pict_type = AV_PICTURE_TYPE_I;
  188. *got_picture_ptr = 1;
  189. *(AVFrame *)data = *pic;
  190. return avpkt->size;
  191. }
  192. static av_cold int cllc_decode_close(AVCodecContext *avctx)
  193. {
  194. CLLCContext *ctx = avctx->priv_data;
  195. if (avctx->coded_frame->data[0])
  196. avctx->release_buffer(avctx, avctx->coded_frame);
  197. av_freep(&avctx->coded_frame);
  198. av_freep(&ctx->swapped_buf);
  199. return 0;
  200. }
  201. static av_cold int cllc_decode_init(AVCodecContext *avctx)
  202. {
  203. CLLCContext *ctx = avctx->priv_data;
  204. /* Initialize various context values */
  205. ctx->avctx = avctx;
  206. ctx->swapped_buf = NULL;
  207. ctx->swapped_buf_size = 0;
  208. ff_dsputil_init(&ctx->dsp, avctx);
  209. avctx->coded_frame = avcodec_alloc_frame();
  210. if (!avctx->coded_frame) {
  211. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  212. return AVERROR(ENOMEM);
  213. }
  214. return 0;
  215. }
  216. AVCodec ff_cllc_decoder = {
  217. .name = "cllc",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. .id = AV_CODEC_ID_CLLC,
  220. .priv_data_size = sizeof(CLLCContext),
  221. .init = cllc_decode_init,
  222. .decode = cllc_decode_frame,
  223. .close = cllc_decode_close,
  224. .capabilities = CODEC_CAP_DR1,
  225. .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
  226. };