]> git.sesse.net Git - ffmpeg/blob - libavcodec/cljr.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / cljr.c
1 /*
2  * Cirrus Logic AccuPak (CLJR) codec
3  * Copyright (c) 2003 Alex Beregszaszi
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * 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
20  */
21
22 /**
23  * @file
24  * Cirrus Logic AccuPak codec.
25  */
26
27 #include "avcodec.h"
28 #include "libavutil/opt.h"
29 #include "get_bits.h"
30 #include "put_bits.h"
31
32 typedef struct CLJRContext {
33     AVClass        *avclass;
34     AVFrame         picture;
35     int             dither_type;
36 } CLJRContext;
37
38 static av_cold int common_init(AVCodecContext *avctx)
39 {
40     CLJRContext * const a = avctx->priv_data;
41
42     avcodec_get_frame_defaults(&a->picture);
43     avctx->coded_frame = &a->picture;
44
45     return 0;
46 }
47
48 #if CONFIG_CLJR_DECODER
49 static int decode_frame(AVCodecContext *avctx,
50                         void *data, int *data_size,
51                         AVPacket *avpkt)
52 {
53     const uint8_t *buf = avpkt->data;
54     int buf_size       = avpkt->size;
55     CLJRContext * const a = avctx->priv_data;
56     GetBitContext gb;
57     AVFrame *picture = data;
58     AVFrame * const p = &a->picture;
59     int x, y;
60
61     if (p->data[0])
62         avctx->release_buffer(avctx, p);
63
64     if (avctx->height <= 0 || avctx->width <= 0) {
65         av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
66         return AVERROR_INVALIDDATA;
67     }
68
69     if (buf_size / avctx->height < avctx->width) {
70         av_log(avctx, AV_LOG_ERROR,
71                "Resolution larger than buffer size. Invalid header?\n");
72         return AVERROR_INVALIDDATA;
73     }
74
75     p->reference = 0;
76     if (avctx->get_buffer(avctx, p) < 0) {
77         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
78         return -1;
79     }
80     p->pict_type = AV_PICTURE_TYPE_I;
81     p->key_frame = 1;
82
83     init_get_bits(&gb, buf, buf_size * 8);
84
85     for (y = 0; y < avctx->height; y++) {
86         uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
87         uint8_t *cb   = &a->picture.data[1][y * a->picture.linesize[1]];
88         uint8_t *cr   = &a->picture.data[2][y * a->picture.linesize[2]];
89         for (x = 0; x < avctx->width; x += 4) {
90             luma[3] = (get_bits(&gb, 5)*33) >> 2;
91             luma[2] = (get_bits(&gb, 5)*33) >> 2;
92             luma[1] = (get_bits(&gb, 5)*33) >> 2;
93             luma[0] = (get_bits(&gb, 5)*33) >> 2;
94             luma += 4;
95             *(cb++) = get_bits(&gb, 6) << 2;
96             *(cr++) = get_bits(&gb, 6) << 2;
97         }
98     }
99
100     *picture   = a->picture;
101     *data_size = sizeof(AVPicture);
102
103     return buf_size;
104 }
105
106 static av_cold int decode_init(AVCodecContext *avctx)
107 {
108     avctx->pix_fmt = PIX_FMT_YUV411P;
109     return common_init(avctx);
110 }
111
112 static av_cold int decode_end(AVCodecContext *avctx)
113 {
114     CLJRContext *a = avctx->priv_data;
115
116     if (a->picture.data[0])
117         avctx->release_buffer(avctx, &a->picture);
118     return 0;
119 }
120
121 AVCodec ff_cljr_decoder = {
122     .name           = "cljr",
123     .type           = AVMEDIA_TYPE_VIDEO,
124     .id             = CODEC_ID_CLJR,
125     .priv_data_size = sizeof(CLJRContext),
126     .init           = decode_init,
127     .close          = decode_end,
128     .decode         = decode_frame,
129     .capabilities   = CODEC_CAP_DR1,
130     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
131 };
132 #endif
133
134 #if CONFIG_CLJR_ENCODER
135 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
136                         int buf_size, void *data)
137 {
138     CLJRContext *a = avctx->priv_data;
139     PutBitContext pb;
140     AVFrame *p = data;
141     int x, y;
142     uint32_t dither= avctx->frame_number;
143     static const uint32_t ordered_dither[2][2] =
144     {
145         { 0x10400000, 0x104F0000 },
146         { 0xCB2A0000, 0xCB250000 },
147     };
148
149     p->pict_type = AV_PICTURE_TYPE_I;
150     p->key_frame = 1;
151
152     init_put_bits(&pb, buf, buf_size / 8);
153
154     for (y = 0; y < avctx->height; y++) {
155         uint8_t *luma = &p->data[0][y * p->linesize[0]];
156         uint8_t *cb   = &p->data[1][y * p->linesize[1]];
157         uint8_t *cr   = &p->data[2][y * p->linesize[2]];
158         for (x = 0; x < avctx->width; x += 4) {
159             switch (a->dither_type) {
160             case 0: dither = 0x492A0000;                       break;
161             case 1: dither = dither * 1664525 + 1013904223;    break;
162             case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
163             }
164             put_bits(&pb, 5, (249*(luma[3] +  (dither>>29)   )) >> 11);
165             put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
166             put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
167             put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
168             luma += 4;
169             put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
170             put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
171         }
172     }
173
174     flush_put_bits(&pb);
175
176     return put_bits_count(&pb) / 8;
177 }
178
179 #define OFFSET(x) offsetof(CLJRContext, x)
180 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
181 static const AVOption options[] = {
182     { "dither_type",   "Dither type",   OFFSET(dither_type),        AV_OPT_TYPE_INT, { .dbl=1 }, 0, 2, VE},
183     { NULL },
184 };
185
186 static const AVClass class = {
187     .class_name = "cljr encoder",
188     .item_name  = av_default_item_name,
189     .option     = options,
190     .version    = LIBAVUTIL_VERSION_INT,
191 };
192
193 AVCodec ff_cljr_encoder = {
194     .name           = "cljr",
195     .type           = AVMEDIA_TYPE_VIDEO,
196     .id             = CODEC_ID_CLJR,
197     .priv_data_size = sizeof(CLJRContext),
198     .init           = common_init,
199     .encode         = encode_frame,
200     .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV411P,
201                                                    PIX_FMT_NONE },
202     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
203     .priv_class     = &class,
204 };
205 #endif