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.

215 lines
6.3KB

  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 "get_bits.h"
  28. #include "huffyuvdsp.h"
  29. #include "internal.h"
  30. #include "mathops.h"
  31. typedef struct VBLEContext {
  32. AVCodecContext *avctx;
  33. HuffYUVDSPContext hdsp;
  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, AVFrame *pic,
  74. GetBitContext *gb, int plane,
  75. int offset, int width, int height)
  76. {
  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->hdsp.add_hfyu_median_pred(dst, dst - stride, val,
  93. width, &left, &left_top);
  94. } else {
  95. dst[0] = val[0];
  96. for (j = 1; j < width; j++)
  97. dst[j] = val[j] + dst[j - 1];
  98. }
  99. dst += stride;
  100. val += width;
  101. }
  102. }
  103. static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  104. AVPacket *avpkt)
  105. {
  106. VBLEContext *ctx = avctx->priv_data;
  107. AVFrame *pic = data;
  108. GetBitContext gb;
  109. const uint8_t *src = avpkt->data;
  110. int version;
  111. int offset = 0;
  112. int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
  113. int ret;
  114. if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
  115. av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118. /* Allocate buffer */
  119. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  120. return ret;
  121. /* Set flags */
  122. pic->key_frame = 1;
  123. pic->pict_type = AV_PICTURE_TYPE_I;
  124. /* Version should always be 1 */
  125. version = AV_RL32(src);
  126. if (version != 1)
  127. av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
  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, pic, &gb, 0, offset, avctx->width, avctx->height);
  136. /* Chroma */
  137. if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  138. offset += avctx->width * avctx->height;
  139. vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv);
  140. offset += width_uv * height_uv;
  141. vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv);
  142. }
  143. *got_frame = 1;
  144. return avpkt->size;
  145. }
  146. static av_cold int vble_decode_close(AVCodecContext *avctx)
  147. {
  148. VBLEContext *ctx = avctx->priv_data;
  149. av_freep(&ctx->val);
  150. return 0;
  151. }
  152. static av_cold int vble_decode_init(AVCodecContext *avctx)
  153. {
  154. VBLEContext *ctx = avctx->priv_data;
  155. /* Stash for later use */
  156. ctx->avctx = avctx;
  157. ff_huffyuvdsp_init(&ctx->hdsp);
  158. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  159. avctx->bits_per_raw_sample = 8;
  160. ctx->size = avpicture_get_size(avctx->pix_fmt,
  161. avctx->width, avctx->height);
  162. ctx->val = av_malloc_array(ctx->size, sizeof(*ctx->val));
  163. if (!ctx->val) {
  164. av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
  165. vble_decode_close(avctx);
  166. return AVERROR(ENOMEM);
  167. }
  168. return 0;
  169. }
  170. AVCodec ff_vble_decoder = {
  171. .name = "vble",
  172. .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
  173. .type = AVMEDIA_TYPE_VIDEO,
  174. .id = AV_CODEC_ID_VBLE,
  175. .priv_data_size = sizeof(VBLEContext),
  176. .init = vble_decode_init,
  177. .close = vble_decode_close,
  178. .decode = vble_decode_frame,
  179. .capabilities = AV_CODEC_CAP_DR1,
  180. };