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.

235 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 ALT_BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. typedef struct {
  29. AVCodecContext *avctx;
  30. int size;
  31. uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value.
  32. } VBLEContext;
  33. static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
  34. {
  35. int i;
  36. int allbits = 0;
  37. static const uint8_t LUT[256] = {
  38. 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,
  39. 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,
  40. 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,
  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. 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,
  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. };
  47. /* Read all the lengths in first */
  48. for (i = 0; i < ctx->size; i++) {
  49. /* At most we need to read 9 bits total to get indices up to 8 */
  50. int val = show_bits(gb, 8);
  51. // read reverse unary
  52. if (val) {
  53. val = LUT[val];
  54. skip_bits(gb, val + 1);
  55. ctx->val[i] = val;
  56. } else {
  57. skip_bits(gb, 8);
  58. if (!get_bits1(gb))
  59. return -1;
  60. ctx->val[i] = 8;
  61. }
  62. allbits += ctx->val[i];
  63. }
  64. /* Check we have enough bits left */
  65. if (get_bits_left(gb) < allbits)
  66. return -1;
  67. for (i = 0; i < ctx->size; i++) {
  68. /* get_bits can't take a length of 0 */
  69. if (ctx->val[i])
  70. ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
  71. }
  72. return 0;
  73. }
  74. static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
  75. int width, int height)
  76. {
  77. AVFrame *pic = ctx->avctx->coded_frame;
  78. uint8_t *dst = pic->data[plane];
  79. uint8_t *val = ctx->val + offset;
  80. uint8_t a, b, c;
  81. int stride = pic->linesize[plane];
  82. int i, j;
  83. for (i = 0; i < height; i++) {
  84. for (j = 0; j < width; j++) {
  85. dst[j] = (val[j] >> 1) ^ -(val[j] & 1);
  86. /* Top line and left column are not predicted */
  87. if (!j)
  88. continue;
  89. if (!i) {
  90. dst[j] += dst[j - 1];
  91. continue;
  92. }
  93. a = dst[j - 1];
  94. b = dst[j - stride];
  95. c = a + b - dst[j - 1 - stride];
  96. dst[j] += mid_pred(a, b, c);
  97. }
  98. dst += stride;
  99. val += width;
  100. }
  101. }
  102. static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  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. /* Allocate buffer */
  117. if (avctx->get_buffer(avctx, pic) < 0) {
  118. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  119. return AVERROR(ENOMEM);
  120. }
  121. /* Set flags */
  122. pic->key_frame = 1;
  123. pic->pict_type = FF_I_TYPE;
  124. /* Version should always be 1 */
  125. version = AV_RL32(src);
  126. if (version != 1) {
  127. av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
  128. return AVERROR_INVALIDDATA;
  129. }
  130. init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
  131. /* Unpack */
  132. if (vble_unpack(ctx, &gb) < 0) {
  133. av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. /* Restore planes. Should be almost identical to Huffyuv's. */
  137. vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
  138. /* Chroma */
  139. if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
  140. offset += avctx->width * avctx->height;
  141. vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
  142. offset += width_uv * height_uv;
  143. vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
  144. }
  145. *data_size = sizeof(AVFrame);
  146. *(AVFrame *)data = *pic;
  147. return avpkt->size;
  148. }
  149. static av_cold int vble_decode_close(AVCodecContext *avctx)
  150. {
  151. VBLEContext *ctx = avctx->priv_data;
  152. AVFrame *pic = avctx->coded_frame;
  153. if (pic->data[0])
  154. avctx->release_buffer(avctx, pic);
  155. av_freep(&avctx->coded_frame);
  156. av_freep(&ctx->val);
  157. return 0;
  158. }
  159. static av_cold int vble_decode_init(AVCodecContext *avctx)
  160. {
  161. VBLEContext *ctx = avctx->priv_data;
  162. /* Stash for later use */
  163. ctx->avctx = avctx;
  164. avctx->pix_fmt = 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 = 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. };