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.

219 lines
6.5KB

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