cljr.c 4.1 KB
Newer Older
Alex Beregszaszi's avatar
Alex Beregszaszi committed
1 2 3 4
/*
 * Cirrus Logic AccuPak (CLJR) codec
 * Copyright (c) 2003 Alex Beregszaszi
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
Alex Beregszaszi's avatar
Alex Beregszaszi committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Alex Beregszaszi's avatar
Alex Beregszaszi committed
11
 *
12
 * Libav is distributed in the hope that it will be useful,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Alex Beregszaszi's avatar
Alex Beregszaszi committed
20
 */
21

Alex Beregszaszi's avatar
Alex Beregszaszi committed
22
/**
23
 * @file
Alex Beregszaszi's avatar
Alex Beregszaszi committed
24 25
 * Cirrus Logic AccuPak codec.
 */
26

Alex Beregszaszi's avatar
Alex Beregszaszi committed
27
#include "avcodec.h"
28
#include "dsputil.h"
29
#include "get_bits.h"
Alex Beregszaszi's avatar
Alex Beregszaszi committed
30

31 32
/* Disable the encoder. */
#undef CONFIG_CLJR_ENCODER
33
#define CONFIG_CLJR_ENCODER 0
34

Alex Beregszaszi's avatar
Alex Beregszaszi committed
35 36 37 38 39 40 41 42
typedef struct CLJRContext{
    AVCodecContext *avctx;
    AVFrame picture;
    int delta[16];
    int offset[4];
    GetBitContext gb;
} CLJRContext;

43
static int decode_frame(AVCodecContext *avctx,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
44
                        void *data, int *data_size,
45
                        AVPacket *avpkt)
Alex Beregszaszi's avatar
Alex Beregszaszi committed
46
{
47 48
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
49 50 51
    CLJRContext * const a = avctx->priv_data;
    AVFrame *picture = data;
    AVFrame * const p= (AVFrame*)&a->picture;
52
    int x, y;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
53 54 55 56

    if(p->data[0])
        avctx->release_buffer(avctx, p);

57 58 59 60 61
    if(buf_size/avctx->height < avctx->width) {
        av_log(avctx, AV_LOG_ERROR, "Resolution larger than buffer size. Invalid header?\n");
        return -1;
    }

Alex Beregszaszi's avatar
Alex Beregszaszi committed
62 63
    p->reference= 0;
    if(avctx->get_buffer(avctx, p) < 0){
64
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
Alex Beregszaszi's avatar
Alex Beregszaszi committed
65 66
        return -1;
    }
67
    p->pict_type= AV_PICTURE_TYPE_I;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
68 69 70 71 72 73
    p->key_frame= 1;

    init_get_bits(&a->gb, buf, buf_size);

    for(y=0; y<avctx->height; y++){
        uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ];
Alex Beregszaszi's avatar
Alex Beregszaszi committed
74 75
        uint8_t *cb= &a->picture.data[1][ y*a->picture.linesize[1] ];
        uint8_t *cr= &a->picture.data[2][ y*a->picture.linesize[2] ];
Alex Beregszaszi's avatar
Alex Beregszaszi committed
76
        for(x=0; x<avctx->width; x+=4){
77 78 79 80 81 82 83
                luma[3] = get_bits(&a->gb, 5) << 3;
            luma[2] = get_bits(&a->gb, 5) << 3;
            luma[1] = get_bits(&a->gb, 5) << 3;
            luma[0] = get_bits(&a->gb, 5) << 3;
            luma+= 4;
            *(cb++) = get_bits(&a->gb, 6) << 2;
            *(cr++) = get_bits(&a->gb, 6) << 2;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
84 85 86 87 88 89 90
        }
    }

    *picture= *(AVFrame*)&a->picture;
    *data_size = sizeof(AVPicture);

    emms_c();
91

Alex Beregszaszi's avatar
Alex Beregszaszi committed
92 93 94
    return buf_size;
}

95
#if CONFIG_CLJR_ENCODER
Alex Beregszaszi's avatar
Alex Beregszaszi committed
96 97 98 99 100 101 102
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
    CLJRContext * const a = avctx->priv_data;
    AVFrame *pict = data;
    AVFrame * const p= (AVFrame*)&a->picture;
    int size;

    *p = *pict;
103
    p->pict_type= AV_PICTURE_TYPE_I;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
104 105 106
    p->key_frame= 1;

    emms_c();
107

Alex Beregszaszi's avatar
Alex Beregszaszi committed
108 109 110
    align_put_bits(&a->pb);
    while(get_bit_count(&a->pb)&31)
        put_bits(&a->pb, 8, 0);
111

Alex Beregszaszi's avatar
Alex Beregszaszi committed
112
    size= get_bit_count(&a->pb)/32;
113

Alex Beregszaszi's avatar
Alex Beregszaszi committed
114 115 116 117
    return size*4;
}
#endif

118
static av_cold void common_init(AVCodecContext *avctx){
Alex Beregszaszi's avatar
Alex Beregszaszi committed
119 120 121 122 123 124
    CLJRContext * const a = avctx->priv_data;

    avctx->coded_frame= (AVFrame*)&a->picture;
    a->avctx= avctx;
}

125
static av_cold int decode_init(AVCodecContext *avctx){
126

Alex Beregszaszi's avatar
Alex Beregszaszi committed
127
    common_init(avctx);
128

Alex Beregszaszi's avatar
Alex Beregszaszi committed
129
    avctx->pix_fmt= PIX_FMT_YUV411P;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
130 131 132 133

    return 0;
}

134
#if CONFIG_CLJR_ENCODER
135
static av_cold int encode_init(AVCodecContext *avctx){
136

Alex Beregszaszi's avatar
Alex Beregszaszi committed
137
    common_init(avctx);
138

Alex Beregszaszi's avatar
Alex Beregszaszi committed
139 140
    return 0;
}
141
#endif
Alex Beregszaszi's avatar
Alex Beregszaszi committed
142

143
AVCodec ff_cljr_decoder = {
Alex Beregszaszi's avatar
Alex Beregszaszi committed
144
    "cljr",
145
    AVMEDIA_TYPE_VIDEO,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
146 147 148 149
    CODEC_ID_CLJR,
    sizeof(CLJRContext),
    decode_init,
    NULL,
150
    NULL,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
151 152
    decode_frame,
    CODEC_CAP_DR1,
153
    .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
Alex Beregszaszi's avatar
Alex Beregszaszi committed
154
};
155

156
#if CONFIG_CLJR_ENCODER
157
AVCodec ff_cljr_encoder = {
Alex Beregszaszi's avatar
Alex Beregszaszi committed
158
    "cljr",
159
    AVMEDIA_TYPE_VIDEO,
160
    CODEC_ID_CLJR,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
161 162 163 164
    sizeof(CLJRContext),
    encode_init,
    encode_frame,
    //encode_end,
165
    .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
Alex Beregszaszi's avatar
Alex Beregszaszi committed
166
};
167
#endif