huffyuv.c 2.29 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1 2 3
/*
 * huffyuv codec for libavcodec
 *
4
 * Copyright (c) 2002-2014 Michael Niedermayer <michaelni@gmx.at>
Michael Niedermayer's avatar
Michael Niedermayer committed
5
 *
6 7 8
 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
 * the algorithm used
 *
9 10 11
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Michael Niedermayer's avatar
Michael Niedermayer committed
12 13
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
Michael Niedermayer's avatar
Michael Niedermayer committed
15
 *
16
 * FFmpeg is distributed in the hope that it will be useful,
Michael Niedermayer's avatar
Michael Niedermayer committed
17 18 19 20 21
 * 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
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
Michael Niedermayer's avatar
Michael Niedermayer committed
24
 */
25

Michael Niedermayer's avatar
Michael Niedermayer committed
26
/**
27
 * @file
Michael Niedermayer's avatar
Michael Niedermayer committed
28 29
 * huffyuv codec for libavcodec.
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
30

31
#include <stdint.h>
32

33
#include "libavutil/mem.h"
34

35
#include "avcodec.h"
36
#include "bswapdsp.h"
37
#include "huffyuv.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
38

39
int ff_huffyuv_generate_bits_table(uint32_t *dst, const uint8_t *len_table, int n)
40
{
Michael Niedermayer's avatar
Michael Niedermayer committed
41
    int len, index;
42
    uint32_t bits = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
43

44
    for (len = 32; len > 0; len--) {
45
        for (index = 0; index < n; index++) {
46 47
            if (len_table[index] == len)
                dst[index] = bits++;
Michael Niedermayer's avatar
Michael Niedermayer committed
48
        }
49
        if (bits & 1) {
50
            av_log(NULL, AV_LOG_ERROR, "Error generating huffman table\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
51 52 53
            return -1;
        }
        bits >>= 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
54 55 56 57
    }
    return 0;
}

58
av_cold int ff_huffyuv_alloc_temp(HYuvContext *s)
59
{
Michael Niedermayer's avatar
Michael Niedermayer committed
60
    int i;
61

62 63 64
    for (i=0; i<3; i++) {
        s->temp[i]= av_malloc(4*s->width + 16);
        if (!s->temp[i])
65
            return AVERROR(ENOMEM);
66
        s->temp16[i] = (uint16_t*)s->temp[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
67
    }
68
    return 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
69 70
}

71
av_cold void ff_huffyuv_common_init(AVCodecContext *avctx)
72
{
Michael Niedermayer's avatar
Michael Niedermayer committed
73 74
    HYuvContext *s = avctx->priv_data;

75 76
    s->avctx = avctx;
    s->flags = avctx->flags;
77

78
    ff_bswapdsp_init(&s->bdsp);
79

80 81
    s->width = avctx->width;
    s->height = avctx->height;
82

83
    av_assert1(s->width > 0 && s->height > 0);
84 85
}

86
av_cold void ff_huffyuv_common_end(HYuvContext *s)
87 88 89 90 91
{
    int i;

    for(i = 0; i < 3; i++) {
        av_freep(&s->temp[i]);
92
        s->temp16[i] = NULL;
93
    }
94
}