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.

234 lines
6.7KB

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