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.

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