/libavcodec/huffyuv.c

http://github.com/FFmpeg/FFmpeg · C · 94 lines · 51 code · 15 blank · 28 comment · 9 complexity · 56af8bd214e9e402e173968a6c621601 MD5 · raw file

  1. /*
  2. * huffyuv codec for libavcodec
  3. *
  4. * Copyright (c) 2002-2014 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
  7. * the algorithm used
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /**
  26. * @file
  27. * huffyuv codec for libavcodec.
  28. */
  29. #include <stdint.h>
  30. #include "libavutil/mem.h"
  31. #include "avcodec.h"
  32. #include "bswapdsp.h"
  33. #include "huffyuv.h"
  34. int ff_huffyuv_generate_bits_table(uint32_t *dst, const uint8_t *len_table, int n)
  35. {
  36. int len, index;
  37. uint32_t bits = 0;
  38. for (len = 32; len > 0; len--) {
  39. for (index = 0; index < n; index++) {
  40. if (len_table[index] == len)
  41. dst[index] = bits++;
  42. }
  43. if (bits & 1) {
  44. av_log(NULL, AV_LOG_ERROR, "Error generating huffman table\n");
  45. return -1;
  46. }
  47. bits >>= 1;
  48. }
  49. return 0;
  50. }
  51. av_cold int ff_huffyuv_alloc_temp(HYuvContext *s)
  52. {
  53. int i;
  54. for (i=0; i<3; i++) {
  55. s->temp[i]= av_malloc(4*s->width + 16);
  56. if (!s->temp[i])
  57. return AVERROR(ENOMEM);
  58. s->temp16[i] = (uint16_t*)s->temp[i];
  59. }
  60. return 0;
  61. }
  62. av_cold void ff_huffyuv_common_init(AVCodecContext *avctx)
  63. {
  64. HYuvContext *s = avctx->priv_data;
  65. s->avctx = avctx;
  66. s->flags = avctx->flags;
  67. ff_bswapdsp_init(&s->bdsp);
  68. s->width = avctx->width;
  69. s->height = avctx->height;
  70. av_assert1(s->width > 0 && s->height > 0);
  71. }
  72. av_cold void ff_huffyuv_common_end(HYuvContext *s)
  73. {
  74. int i;
  75. for(i = 0; i < 3; i++) {
  76. av_freep(&s->temp[i]);
  77. s->temp16[i] = NULL;
  78. }
  79. }