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.

230 lines
6.6KB

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