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.

259 lines
7.0KB

  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. int flags;
  32. uint8_t *len;
  33. uint8_t *val;
  34. } VBLEContext;
  35. static uint8_t vble_read_reverse_unary(GetBitContext *gb)
  36. {
  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. /* At most we need to read 9 bits total to get indices up to 8 */
  48. int val = show_bits(gb, 8);
  49. if (val) {
  50. val = LUT[val];
  51. skip_bits(gb, val + 1);
  52. return val;
  53. } else {
  54. skip_bits(gb, 8);
  55. if (get_bits1(gb))
  56. return 8;
  57. }
  58. /* Return something larger than 8 on error */
  59. return UINT8_MAX;
  60. }
  61. static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
  62. {
  63. int i;
  64. /* Read all the lengths in first */
  65. for (i = 0; i < ctx->size; i++) {
  66. ctx->len[i] = vble_read_reverse_unary(gb);
  67. if (ctx->len[i] == UINT8_MAX)
  68. return -1;
  69. }
  70. /* For any values that have length 0 */
  71. memset(ctx->val, 0, ctx->size);
  72. for (i = 0; i < ctx->size; i++) {
  73. /* Check we have enough bits left */
  74. if (get_bits_left(gb) < ctx->len[i])
  75. return -1;
  76. /* get_bits can't take a length of 0 */
  77. if (ctx->len[i])
  78. ctx->val[i] = (1 << ctx->len[i]) + get_bits(gb, ctx->len[i]) - 1;
  79. }
  80. return 0;
  81. }
  82. static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
  83. int width, int height)
  84. {
  85. AVFrame *pic = ctx->avctx->coded_frame;
  86. uint8_t *dst = pic->data[plane];
  87. uint8_t *val = ctx->val + offset;
  88. uint8_t *len = ctx->len + offset;
  89. uint8_t a, b, c;
  90. int stride = pic->linesize[plane];
  91. int i, j;
  92. for (i = 0; i < height; i++) {
  93. for (j = 0; j < width; j++) {
  94. dst[j] = (val[j] >> 1) ^ -(val[j] & 1);
  95. /* Top line and left column are not predicted */
  96. if (!j)
  97. continue;
  98. if (!i) {
  99. dst[j] += dst[j - 1];
  100. continue;
  101. }
  102. a = dst[j - 1];
  103. b = dst[j - stride];
  104. c = a + b - dst[j - 1 - stride];
  105. dst[j] += mid_pred(a, b, c);
  106. }
  107. dst += stride;
  108. val += width;
  109. len += width;
  110. }
  111. }
  112. static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  113. AVPacket *avpkt)
  114. {
  115. VBLEContext *ctx = avctx->priv_data;
  116. AVFrame *pic = avctx->coded_frame;
  117. GetBitContext gb;
  118. const uint8_t *src = avpkt->data;
  119. int version;
  120. int offset = 0;
  121. int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
  122. pic->reference = 0;
  123. /* Clear buffer if need be */
  124. if (pic->data[0])
  125. avctx->release_buffer(avctx, pic);
  126. /* Allocate buffer */
  127. if (avctx->get_buffer(avctx, pic) < 0) {
  128. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  129. return AVERROR(ENOMEM);
  130. }
  131. /* Set flags */
  132. pic->key_frame = 1;
  133. pic->pict_type = FF_I_TYPE;
  134. /* Version should always be 1 */
  135. version = AV_RL32(src);
  136. if (version != 1) {
  137. av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
  138. return AVERROR_INVALIDDATA;
  139. }
  140. init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
  141. /* Unpack */
  142. if (vble_unpack(ctx, &gb) < 0) {
  143. av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
  144. return AVERROR_INVALIDDATA;
  145. }
  146. /* Restore planes. Should be almost identical to Huffyuv's. */
  147. vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
  148. /* Chroma */
  149. if (!(ctx->flags & CODEC_FLAG_GRAY)) {
  150. offset += avctx->width * avctx->height;
  151. vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
  152. offset += width_uv * height_uv;
  153. vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
  154. }
  155. *data_size = sizeof(AVFrame);
  156. *(AVFrame *)data = *pic;
  157. return avpkt->size;
  158. }
  159. static av_cold int vble_decode_close(AVCodecContext *avctx)
  160. {
  161. VBLEContext *ctx = avctx->priv_data;
  162. AVFrame *pic = avctx->coded_frame;
  163. if (pic->data[0])
  164. avctx->release_buffer(avctx, pic);
  165. av_freep(&avctx->coded_frame);
  166. av_freep(&ctx->len);
  167. av_freep(&ctx->val);
  168. return 0;
  169. }
  170. static av_cold int vble_decode_init(AVCodecContext *avctx)
  171. {
  172. VBLEContext *ctx = avctx->priv_data;
  173. /* Stash for later use */
  174. ctx->avctx = avctx;
  175. ctx->flags = avctx->flags;
  176. avctx->pix_fmt = PIX_FMT_YUV420P;
  177. avctx->bits_per_raw_sample = 8;
  178. avctx->coded_frame = avcodec_alloc_frame();
  179. if (!avctx->coded_frame) {
  180. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  181. return AVERROR(ENOMEM);
  182. }
  183. ctx->size = avpicture_get_size(avctx->pix_fmt,
  184. avctx->width, avctx->height);
  185. ctx->len = av_malloc(ctx->size * sizeof(*ctx->len));
  186. if (!ctx->len) {
  187. av_log(avctx, AV_LOG_ERROR, "Could not allocate lengths buffer.\n");
  188. vble_decode_close(avctx);
  189. return AVERROR(ENOMEM);
  190. }
  191. ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
  192. if (!ctx->val) {
  193. av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
  194. vble_decode_close(avctx);
  195. return AVERROR(ENOMEM);
  196. }
  197. return 0;
  198. }
  199. AVCodec ff_vble_decoder = {
  200. .name = "vble",
  201. .type = AVMEDIA_TYPE_VIDEO,
  202. .id = CODEC_ID_VBLE,
  203. .priv_data_size = sizeof(VBLEContext),
  204. .init = vble_decode_init,
  205. .close = vble_decode_close,
  206. .decode = vble_decode_frame,
  207. .capabilities = CODEC_CAP_DR1,
  208. .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
  209. };