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.

250 lines
6.9KB

  1. /*
  2. * VBLE Decoder
  3. * Copyright (c) 2011 Derek Buitenhuis
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
  36. {
  37. int i;
  38. int allbits = 0;
  39. static const uint8_t LUT[256] = {
  40. 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  41. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  42. 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  43. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  44. 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  45. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  46. 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  47. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  48. };
  49. /* Read all the lengths in first */
  50. for (i = 0; i < ctx->size; i++) {
  51. /* At most we need to read 9 bits total to get indices up to 8 */
  52. int val = show_bits(gb, 8);
  53. // read reverse unary
  54. if (val) {
  55. val = LUT[val];
  56. skip_bits(gb, val + 1);
  57. ctx->len[i] = val;
  58. } else {
  59. skip_bits(gb, 8);
  60. if (!get_bits1(gb))
  61. return -1;
  62. ctx->len[i] = 8;
  63. }
  64. allbits += ctx->len[i];
  65. }
  66. /* Check we have enough bits left */
  67. if (get_bits_left(gb) < allbits)
  68. return -1;
  69. /* For any values that have length 0 */
  70. memset(ctx->val, 0, ctx->size);
  71. for (i = 0; i < ctx->size; i++) {
  72. /* get_bits can't take a length of 0 */
  73. if (ctx->len[i])
  74. ctx->val[i] = (1 << ctx->len[i]) + get_bits(gb, ctx->len[i]) - 1;
  75. }
  76. return 0;
  77. }
  78. static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
  79. int width, int height)
  80. {
  81. AVFrame *pic = ctx->avctx->coded_frame;
  82. uint8_t *dst = pic->data[plane];
  83. uint8_t *val = ctx->val + offset;
  84. uint8_t a, b, c;
  85. int stride = pic->linesize[plane];
  86. int i, j;
  87. for (i = 0; i < height; i++) {
  88. for (j = 0; j < width; j++) {
  89. dst[j] = (val[j] >> 1) ^ -(val[j] & 1);
  90. /* Top line and left column are not predicted */
  91. if (!j)
  92. continue;
  93. if (!i) {
  94. dst[j] += dst[j - 1];
  95. continue;
  96. }
  97. a = dst[j - 1];
  98. b = dst[j - stride];
  99. c = a + b - dst[j - 1 - stride];
  100. dst[j] += mid_pred(a, b, c);
  101. }
  102. dst += stride;
  103. val += width;
  104. }
  105. }
  106. static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  107. AVPacket *avpkt)
  108. {
  109. VBLEContext *ctx = avctx->priv_data;
  110. AVFrame *pic = avctx->coded_frame;
  111. GetBitContext gb;
  112. const uint8_t *src = avpkt->data;
  113. int version;
  114. int offset = 0;
  115. int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
  116. pic->reference = 0;
  117. /* Clear buffer if need be */
  118. if (pic->data[0])
  119. avctx->release_buffer(avctx, pic);
  120. /* Allocate buffer */
  121. if (avctx->get_buffer(avctx, pic) < 0) {
  122. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  123. return AVERROR(ENOMEM);
  124. }
  125. /* Set flags */
  126. pic->key_frame = 1;
  127. pic->pict_type = FF_I_TYPE;
  128. /* Version should always be 1 */
  129. version = AV_RL32(src);
  130. if (version != 1) {
  131. av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
  132. return AVERROR_INVALIDDATA;
  133. }
  134. init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
  135. /* Unpack */
  136. if (vble_unpack(ctx, &gb) < 0) {
  137. av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. /* Restore planes. Should be almost identical to Huffyuv's. */
  141. vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
  142. /* Chroma */
  143. if (!(ctx->flags & CODEC_FLAG_GRAY)) {
  144. offset += avctx->width * avctx->height;
  145. vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
  146. offset += width_uv * height_uv;
  147. vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
  148. }
  149. *data_size = sizeof(AVFrame);
  150. *(AVFrame *)data = *pic;
  151. return avpkt->size;
  152. }
  153. static av_cold int vble_decode_close(AVCodecContext *avctx)
  154. {
  155. VBLEContext *ctx = avctx->priv_data;
  156. AVFrame *pic = avctx->coded_frame;
  157. if (pic->data[0])
  158. avctx->release_buffer(avctx, pic);
  159. av_freep(&avctx->coded_frame);
  160. av_freep(&ctx->len);
  161. av_freep(&ctx->val);
  162. return 0;
  163. }
  164. static av_cold int vble_decode_init(AVCodecContext *avctx)
  165. {
  166. VBLEContext *ctx = avctx->priv_data;
  167. /* Stash for later use */
  168. ctx->avctx = avctx;
  169. ctx->flags = avctx->flags;
  170. avctx->pix_fmt = PIX_FMT_YUV420P;
  171. avctx->bits_per_raw_sample = 8;
  172. avctx->coded_frame = avcodec_alloc_frame();
  173. if (!avctx->coded_frame) {
  174. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  175. return AVERROR(ENOMEM);
  176. }
  177. ctx->size = avpicture_get_size(avctx->pix_fmt,
  178. avctx->width, avctx->height);
  179. ctx->len = av_malloc(ctx->size * sizeof(*ctx->len));
  180. if (!ctx->len) {
  181. av_log(avctx, AV_LOG_ERROR, "Could not allocate lengths buffer.\n");
  182. vble_decode_close(avctx);
  183. return AVERROR(ENOMEM);
  184. }
  185. ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
  186. if (!ctx->val) {
  187. av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
  188. vble_decode_close(avctx);
  189. return AVERROR(ENOMEM);
  190. }
  191. return 0;
  192. }
  193. AVCodec ff_vble_decoder = {
  194. .name = "vble",
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. .id = CODEC_ID_VBLE,
  197. .priv_data_size = sizeof(VBLEContext),
  198. .init = vble_decode_init,
  199. .close = vble_decode_close,
  200. .decode = vble_decode_frame,
  201. .capabilities = CODEC_CAP_DR1,
  202. .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
  203. };