cljr.c 3.93 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 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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 libavcodec/cljr.c
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 57 58

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

    p->reference= 0;
    if(avctx->get_buffer(avctx, p) < 0){
59
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
Alex Beregszaszi's avatar
Alex Beregszaszi committed
60 61
        return -1;
    }
62
    p->pict_type= FF_I_TYPE;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
63 64 65 66 67 68
    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
69 70
        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
71
        for(x=0; x<avctx->width; x+=4){
72 73 74 75 76 77 78
                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
79 80 81 82 83 84 85
        }
    }

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

    emms_c();
86

Alex Beregszaszi's avatar
Alex Beregszaszi committed
87 88 89
    return buf_size;
}

90
#if CONFIG_CLJR_ENCODER
Alex Beregszaszi's avatar
Alex Beregszaszi committed
91 92 93 94 95 96 97
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;
98
    p->pict_type= FF_I_TYPE;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
99 100 101
    p->key_frame= 1;

    emms_c();
102

Alex Beregszaszi's avatar
Alex Beregszaszi committed
103 104 105
    align_put_bits(&a->pb);
    while(get_bit_count(&a->pb)&31)
        put_bits(&a->pb, 8, 0);
106

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

Alex Beregszaszi's avatar
Alex Beregszaszi committed
109 110 111 112
    return size*4;
}
#endif

113
static av_cold void common_init(AVCodecContext *avctx){
Alex Beregszaszi's avatar
Alex Beregszaszi committed
114 115 116 117 118 119
    CLJRContext * const a = avctx->priv_data;

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

120
static av_cold int decode_init(AVCodecContext *avctx){
121

Alex Beregszaszi's avatar
Alex Beregszaszi committed
122
    common_init(avctx);
123

Alex Beregszaszi's avatar
Alex Beregszaszi committed
124
    avctx->pix_fmt= PIX_FMT_YUV411P;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
125 126 127 128

    return 0;
}

129
#if CONFIG_CLJR_ENCODER
130
static av_cold int encode_init(AVCodecContext *avctx){
131

Alex Beregszaszi's avatar
Alex Beregszaszi committed
132
    common_init(avctx);
133

Alex Beregszaszi's avatar
Alex Beregszaszi committed
134 135
    return 0;
}
136
#endif
Alex Beregszaszi's avatar
Alex Beregszaszi committed
137 138 139 140 141 142 143 144

AVCodec cljr_decoder = {
    "cljr",
    CODEC_TYPE_VIDEO,
    CODEC_ID_CLJR,
    sizeof(CLJRContext),
    decode_init,
    NULL,
145
    NULL,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
146 147
    decode_frame,
    CODEC_CAP_DR1,
148
    .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
Alex Beregszaszi's avatar
Alex Beregszaszi committed
149
};
150

151
#if CONFIG_CLJR_ENCODER
Alex Beregszaszi's avatar
Alex Beregszaszi committed
152 153 154
AVCodec cljr_encoder = {
    "cljr",
    CODEC_TYPE_VIDEO,
155
    CODEC_ID_CLJR,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
156 157 158 159
    sizeof(CLJRContext),
    encode_init,
    encode_frame,
    //encode_end,
160
    .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
Alex Beregszaszi's avatar
Alex Beregszaszi committed
161
};
162
#endif