cljr.c 5.97 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
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 "libavutil/opt.h"
29
#include "get_bits.h"
Anton Khirnov's avatar
Anton Khirnov committed
30
#include "internal.h"
Paul B Mahol's avatar
Paul B Mahol committed
31
#include "put_bits.h"
Alex Beregszaszi's avatar
Alex Beregszaszi committed
32

33
#if CONFIG_CLJR_DECODER
34
static int decode_frame(AVCodecContext *avctx,
35
                        void *data, int *got_frame,
36
                        AVPacket *avpkt)
Alex Beregszaszi's avatar
Alex Beregszaszi committed
37
{
38
    const uint8_t *buf = avpkt->data;
Diego Biurrun's avatar
Diego Biurrun committed
39
    int buf_size       = avpkt->size;
40
    GetBitContext gb;
41
    AVFrame * const p = data;
42
    int x, y, ret;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
43

44 45 46 47 48
    if (avctx->height <= 0 || avctx->width <= 0) {
        av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
        return AVERROR_INVALIDDATA;
    }

Diego Biurrun's avatar
Diego Biurrun committed
49 50 51
    if (buf_size / avctx->height < avctx->width) {
        av_log(avctx, AV_LOG_ERROR,
               "Resolution larger than buffer size. Invalid header?\n");
52
        return AVERROR_INVALIDDATA;
53 54
    }

55
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
56
        return ret;
Diego Biurrun's avatar
Diego Biurrun committed
57 58
    p->pict_type = AV_PICTURE_TYPE_I;
    p->key_frame = 1;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
59

60
    init_get_bits(&gb, buf, buf_size * 8);
Alex Beregszaszi's avatar
Alex Beregszaszi committed
61

Diego Biurrun's avatar
Diego Biurrun committed
62
    for (y = 0; y < avctx->height; y++) {
63 64 65
        uint8_t *luma = &p->data[0][y * p->linesize[0]];
        uint8_t *cb   = &p->data[1][y * p->linesize[1]];
        uint8_t *cr   = &p->data[2][y * p->linesize[2]];
Diego Biurrun's avatar
Diego Biurrun committed
66
        for (x = 0; x < avctx->width; x += 4) {
67 68 69 70
            luma[3] = (get_bits(&gb, 5)*33) >> 2;
            luma[2] = (get_bits(&gb, 5)*33) >> 2;
            luma[1] = (get_bits(&gb, 5)*33) >> 2;
            luma[0] = (get_bits(&gb, 5)*33) >> 2;
Diego Biurrun's avatar
Diego Biurrun committed
71
            luma += 4;
72 73
            *(cb++) = get_bits(&gb, 6) << 2;
            *(cr++) = get_bits(&gb, 6) << 2;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
74 75 76
        }
    }

77
    *got_frame = 1;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
78 79 80 81

    return buf_size;
}

82 83
static av_cold int decode_init(AVCodecContext *avctx)
{
84
    avctx->pix_fmt = AV_PIX_FMT_YUV411P;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
85 86 87
    return 0;
}

88
AVCodec ff_cljr_decoder = {
89
    .name           = "cljr",
90
    .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
91
    .type           = AVMEDIA_TYPE_VIDEO,
92
    .id             = AV_CODEC_ID_CLJR,
93 94 95
    .init           = decode_init,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
96
};
97
#endif
98

99
#if CONFIG_CLJR_ENCODER
100
typedef struct CLJRContext {
101 102
    AVClass        *avclass;
    int             dither_type;
103 104 105 106
} CLJRContext;

static av_cold int encode_init(AVCodecContext *avctx)
{
107 108 109
    avctx->coded_frame = av_frame_alloc();
    if (!avctx->coded_frame)
        return AVERROR(ENOMEM);
110

111 112
    return 0;
}
113

114 115 116
static av_cold int encode_close(AVCodecContext *avctx)
{
    av_frame_free(&avctx->coded_frame);
117 118 119
    return 0;
}

Anton Khirnov's avatar
Anton Khirnov committed
120 121
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                        const AVFrame *p, int *got_packet)
Diego Biurrun's avatar
Diego Biurrun committed
122
{
123
    CLJRContext *a = avctx->priv_data;
Paul B Mahol's avatar
Paul B Mahol committed
124
    PutBitContext pb;
Anton Khirnov's avatar
Anton Khirnov committed
125
    int x, y, ret;
126
    uint32_t dither= avctx->frame_number;
127 128 129 130 131
    static const uint32_t ordered_dither[2][2] =
    {
        { 0x10400000, 0x104F0000 },
        { 0xCB2A0000, 0xCB250000 },
    };
Alex Beregszaszi's avatar
Alex Beregszaszi committed
132

133
    if ((ret = ff_alloc_packet2(avctx, pkt, 32*avctx->height*avctx->width/4)) < 0)
Anton Khirnov's avatar
Anton Khirnov committed
134
        return ret;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
135

136 137
    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
    avctx->coded_frame->key_frame = 1;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
138

Anton Khirnov's avatar
Anton Khirnov committed
139
    init_put_bits(&pb, pkt->data, pkt->size);
Paul B Mahol's avatar
Paul B Mahol committed
140 141 142 143 144 145

    for (y = 0; y < avctx->height; y++) {
        uint8_t *luma = &p->data[0][y * p->linesize[0]];
        uint8_t *cb   = &p->data[1][y * p->linesize[1]];
        uint8_t *cr   = &p->data[2][y * p->linesize[2]];
        for (x = 0; x < avctx->width; x += 4) {
146 147 148
            switch (a->dither_type) {
            case 0: dither = 0x492A0000;                       break;
            case 1: dither = dither * 1664525 + 1013904223;    break;
149
            case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
150
            }
151 152 153 154
            put_bits(&pb, 5, (249*(luma[3] +  (dither>>29)   )) >> 11);
            put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
            put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
            put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
Paul B Mahol's avatar
Paul B Mahol committed
155
            luma += 4;
156 157
            put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
            put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
Paul B Mahol's avatar
Paul B Mahol committed
158 159
        }
    }
160

Paul B Mahol's avatar
Paul B Mahol committed
161
    flush_put_bits(&pb);
162

Anton Khirnov's avatar
Anton Khirnov committed
163 164 165 166
    pkt->size   = put_bits_count(&pb) / 8;
    pkt->flags |= AV_PKT_FLAG_KEY;
    *got_packet = 1;
    return 0;
Alex Beregszaszi's avatar
Alex Beregszaszi committed
167 168
}

169 170 171
#define OFFSET(x) offsetof(CLJRContext, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
172
    { "dither_type",   "Dither type",   OFFSET(dither_type),        AV_OPT_TYPE_INT, { .i64=1 }, 0, 2, VE},
173 174 175
    { NULL },
};

176
static const AVClass cljr_class = {
177 178 179 180 181 182
    .class_name = "cljr encoder",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

183
AVCodec ff_cljr_encoder = {
184
    .name           = "cljr",
185
    .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
186
    .type           = AVMEDIA_TYPE_VIDEO,
187
    .id             = AV_CODEC_ID_CLJR,
188
    .priv_data_size = sizeof(CLJRContext),
189
    .init           = encode_init,
Anton Khirnov's avatar
Anton Khirnov committed
190
    .encode2        = encode_frame,
191
    .close          = encode_close,
192 193
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
                                                   AV_PIX_FMT_NONE },
194
    .priv_class     = &cljr_class,
Alex Beregszaszi's avatar
Alex Beregszaszi committed
195
};
196
#endif