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.

249 lines
6.5KB

  1. /*
  2. * VBLE Decoder
  3. * Copyright (c) 2011 Derek Buitenhuis
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * VBLE Decoder
  24. */
  25. #define ALT_BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. typedef struct {
  29. AVCodecContext *avctx;
  30. int size;
  31. int flags;
  32. uint8_t *len;
  33. uint8_t *val;
  34. } VBLEContext;
  35. static uint8_t vble_read_reverse_unary(GetBitContext *gb)
  36. {
  37. /* At most we need to read 9 bits total to get indices up to 8 */
  38. uint8_t val = show_bits(gb, 8);
  39. if (val) {
  40. val = 7 - av_log2_16bit(av_reverse[val]);
  41. skip_bits(gb, val + 1);
  42. return val;
  43. } else {
  44. skip_bits(gb, 8);
  45. if (get_bits1(gb))
  46. return 8;
  47. }
  48. /* Return something larger than 8 on error */
  49. return UINT8_MAX;
  50. }
  51. static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
  52. {
  53. int i;
  54. /* Read all the lengths in first */
  55. for (i = 0; i < ctx->size; i++) {
  56. ctx->len[i] = vble_read_reverse_unary(gb);
  57. if (ctx->len[i] == UINT8_MAX)
  58. return -1;
  59. }
  60. /* For any values that have length 0 */
  61. memset(ctx->val, 0, ctx->size);
  62. for (i = 0; i < ctx->size; i++) {
  63. /* Check we have enough bits left */
  64. if (get_bits_left(gb) < ctx->len[i])
  65. return -1;
  66. /* get_bits can't take a length of 0 */
  67. if (ctx->len[i])
  68. ctx->val[i] = (1 << ctx->len[i]) + get_bits(gb, ctx->len[i]) - 1;
  69. }
  70. return 0;
  71. }
  72. static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
  73. int width, int height)
  74. {
  75. AVFrame *pic = ctx->avctx->coded_frame;
  76. uint8_t *dst = pic->data[plane];
  77. uint8_t *val = ctx->val + offset;
  78. uint8_t *len = ctx->len + offset;
  79. uint8_t a, b, c;
  80. int stride = pic->linesize[plane];
  81. int i, j;
  82. for (i = 0; i < height; i++) {
  83. for (j = 0; j < width; j++) {
  84. dst[j] = (val[j] >> 1) ^ -(val[j] & 1);
  85. /* Top line and left column are not predicted */
  86. if (!j)
  87. continue;
  88. if (!i) {
  89. dst[j] += dst[j - 1];
  90. continue;
  91. }
  92. a = dst[j - 1];
  93. b = dst[j - stride];
  94. c = a + b - dst[j - 1 - stride];
  95. dst[j] += mid_pred(a, b, c);
  96. }
  97. dst += stride;
  98. val += width;
  99. len += width;
  100. }
  101. }
  102. static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  103. AVPacket *avpkt)
  104. {
  105. VBLEContext *ctx = avctx->priv_data;
  106. AVFrame *pic = avctx->coded_frame;
  107. GetBitContext gb;
  108. const uint8_t *src = avpkt->data;
  109. int version;
  110. int offset = 0;
  111. int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
  112. pic->reference = 0;
  113. /* Clear buffer if need be */
  114. if (pic->data[0])
  115. avctx->release_buffer(avctx, pic);
  116. /* Allocate buffer */
  117. if (avctx->get_buffer(avctx, pic) < 0) {
  118. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  119. return AVERROR(ENOMEM);
  120. }
  121. /* Set flags */
  122. pic->key_frame = 1;
  123. pic->pict_type = FF_I_TYPE;
  124. /* Version should always be 1 */
  125. version = AV_RL32(src);
  126. if (version != 1) {
  127. av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
  128. return AVERROR_INVALIDDATA;
  129. }
  130. init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
  131. /* Unpack */
  132. if (vble_unpack(ctx, &gb) < 0) {
  133. av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. /* Restore planes. Should be almost identical to Huffyuv's. */
  137. vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
  138. /* Chroma */
  139. if (!(ctx->flags & CODEC_FLAG_GRAY)) {
  140. offset += avctx->width * avctx->height;
  141. vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
  142. offset += width_uv * height_uv;
  143. vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
  144. }
  145. *data_size = sizeof(AVFrame);
  146. *(AVFrame *)data = *pic;
  147. return avpkt->size;
  148. }
  149. static av_cold int vble_decode_close(AVCodecContext *avctx)
  150. {
  151. VBLEContext *ctx = avctx->priv_data;
  152. AVFrame *pic = avctx->coded_frame;
  153. if (pic->data[0])
  154. avctx->release_buffer(avctx, pic);
  155. av_freep(&avctx->coded_frame);
  156. av_freep(&ctx->len);
  157. av_freep(&ctx->val);
  158. return 0;
  159. }
  160. static av_cold int vble_decode_init(AVCodecContext *avctx)
  161. {
  162. VBLEContext *ctx = avctx->priv_data;
  163. /* Stash for later use */
  164. ctx->avctx = avctx;
  165. ctx->flags = avctx->flags;
  166. avctx->pix_fmt = PIX_FMT_YUV420P;
  167. avctx->bits_per_raw_sample = 8;
  168. avctx->coded_frame = avcodec_alloc_frame();
  169. if (!avctx->coded_frame) {
  170. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  171. return AVERROR(ENOMEM);
  172. }
  173. ctx->size = avpicture_get_size(avctx->pix_fmt,
  174. avctx->width, avctx->height);
  175. ctx->len = av_malloc(ctx->size * sizeof(*ctx->len));
  176. if (!ctx->len) {
  177. av_log(avctx, AV_LOG_ERROR, "Could not allocate lengths buffer.\n");
  178. vble_decode_close(avctx);
  179. return AVERROR(ENOMEM);
  180. }
  181. ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
  182. if (!ctx->val) {
  183. av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
  184. vble_decode_close(avctx);
  185. return AVERROR(ENOMEM);
  186. }
  187. return 0;
  188. }
  189. AVCodec ff_vble_decoder = {
  190. .name = "vble",
  191. .type = AVMEDIA_TYPE_VIDEO,
  192. .id = CODEC_ID_VBLE,
  193. .priv_data_size = sizeof(VBLEContext),
  194. .init = vble_decode_init,
  195. .close = vble_decode_close,
  196. .decode = vble_decode_frame,
  197. .capabilities = CODEC_CAP_DR1,
  198. .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
  199. };