libavcodec/huffyuv.c
11f18faf
 /*
  * huffyuv codec for libavcodec
  *
aaa1e4cd
  * Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at>
11f18faf
  *
7b94177e
  * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
  * the algorithm used
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
11f18faf
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
11f18faf
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
11f18faf
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
11f18faf
  */
115329f1
 
983e3246
 /**
ba87f080
  * @file
983e3246
  * huffyuv codec for libavcodec.
  */
11f18faf
 
2ca5ca29
 #include <stdint.h>
ae5873f1
 
2ca5ca29
 #include "libavutil/mem.h"
115329f1
 
2ca5ca29
 #include "avcodec.h"
 #include "huffyuv.h"
11f18faf
 
2ca5ca29
 int ff_huffyuv_generate_bits_table(uint32_t *dst, const uint8_t *len_table)
e0f7a9f6
 {
11f18faf
     int len, index;
e0f7a9f6
     uint32_t bits = 0;
11f18faf
 
e0f7a9f6
     for (len = 32; len > 0; len--) {
         for (index = 0; index < 256; index++) {
             if (len_table[index] == len)
                 dst[index] = bits++;
11f18faf
         }
e0f7a9f6
         if (bits & 1) {
9b879566
             av_log(NULL, AV_LOG_ERROR, "Error generating huffman table\n");
14b74d38
             return -1;
         }
         bits >>= 1;
11f18faf
     }
     return 0;
 }
 
2ca5ca29
 av_cold int ff_huffyuv_alloc_temp(HYuvContext *s)
e0f7a9f6
 {
ae2f1d46
     int i;
115329f1
 
e0f7a9f6
     if (s->bitstream_bpp<24) {
         for (i=0; i<3; i++) {
ae2f1d46
             s->temp[i]= av_malloc(s->width + 16);
4a722a5c
             if (!s->temp[i])
                 return AVERROR(ENOMEM);
ae2f1d46
         }
e0f7a9f6
     } else {
f267d3ac
         s->temp[0]= av_mallocz(4*s->width + 16);
4a722a5c
         if (!s->temp[0])
             return AVERROR(ENOMEM);
ae2f1d46
     }
4a722a5c
     return 0;
ae2f1d46
 }
 
2ca5ca29
 av_cold void ff_huffyuv_common_init(AVCodecContext *avctx)
e0f7a9f6
 {
11f18faf
     HYuvContext *s = avctx->priv_data;
 
e0f7a9f6
     s->avctx = avctx;
     s->flags = avctx->flags;
115329f1
 
9cf0841e
     ff_dsputil_init(&s->dsp, avctx);
115329f1
 
e0f7a9f6
     s->width = avctx->width;
     s->height = avctx->height;
115329f1
 
def18e54
     av_assert1(s->width > 0 && s->height > 0);
0ecca7a4
 }
 
def18e54
 av_cold void ff_huffyuv_common_end(HYuvContext *s)
4a722a5c
 {
     int i;
 
     for(i = 0; i < 3; i++) {
         av_freep(&s->temp[i]);
     }
7c5ab7b8
 }