2 * Cirrus Logic AccuPak (CLJR) codec
3 * Copyright (c) 2003 Alex Beregszaszi
5 * This file is part of FFmpeg.
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.
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.
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
24 * Cirrus Logic AccuPak codec.
28 #include "libavutil/opt.h"
33 #if CONFIG_CLJR_DECODER
34 static int decode_frame(AVCodecContext *avctx,
35 void *data, int *got_frame,
38 const uint8_t *buf = avpkt->data;
39 int buf_size = avpkt->size;
41 AVFrame * const p = data;
44 if (avctx->height <= 0 || avctx->width <= 0) {
45 av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
46 return AVERROR_INVALIDDATA;
49 if (buf_size / avctx->height < avctx->width) {
50 av_log(avctx, AV_LOG_ERROR,
51 "Resolution larger than buffer size. Invalid header?\n");
52 return AVERROR_INVALIDDATA;
55 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
57 p->pict_type = AV_PICTURE_TYPE_I;
60 init_get_bits(&gb, buf, buf_size * 8);
62 for (y = 0; y < avctx->height; y++) {
63 uint8_t *luma = &p->data[0][y * p->linesize[0]];
64 uint8_t *cb = &p->data[1][y * p->linesize[1]];
65 uint8_t *cr = &p->data[2][y * p->linesize[2]];
66 for (x = 0; x < avctx->width; x += 4) {
67 luma[3] = (get_bits(&gb, 5)*33) >> 2;
68 luma[2] = (get_bits(&gb, 5)*33) >> 2;
69 luma[1] = (get_bits(&gb, 5)*33) >> 2;
70 luma[0] = (get_bits(&gb, 5)*33) >> 2;
72 *(cb++) = get_bits(&gb, 6) << 2;
73 *(cr++) = get_bits(&gb, 6) << 2;
82 static av_cold int decode_init(AVCodecContext *avctx)
84 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
88 AVCodec ff_cljr_decoder = {
90 .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
91 .type = AVMEDIA_TYPE_VIDEO,
92 .id = AV_CODEC_ID_CLJR,
94 .decode = decode_frame,
95 .capabilities = CODEC_CAP_DR1,
99 #if CONFIG_CLJR_ENCODER
100 typedef struct CLJRContext {
106 static av_cold int encode_init(AVCodecContext *avctx)
108 CLJRContext * const a = avctx->priv_data;
110 avctx->coded_frame = &a->picture;
115 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
116 const AVFrame *p, int *got_packet)
118 CLJRContext *a = avctx->priv_data;
121 uint32_t dither= avctx->frame_number;
122 static const uint32_t ordered_dither[2][2] =
124 { 0x10400000, 0x104F0000 },
125 { 0xCB2A0000, 0xCB250000 },
128 if ((ret = ff_alloc_packet2(avctx, pkt, 32*avctx->height*avctx->width/4)) < 0)
131 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
132 avctx->coded_frame->key_frame = 1;
134 init_put_bits(&pb, pkt->data, pkt->size);
136 for (y = 0; y < avctx->height; y++) {
137 uint8_t *luma = &p->data[0][y * p->linesize[0]];
138 uint8_t *cb = &p->data[1][y * p->linesize[1]];
139 uint8_t *cr = &p->data[2][y * p->linesize[2]];
140 for (x = 0; x < avctx->width; x += 4) {
141 switch (a->dither_type) {
142 case 0: dither = 0x492A0000; break;
143 case 1: dither = dither * 1664525 + 1013904223; break;
144 case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
146 put_bits(&pb, 5, (249*(luma[3] + (dither>>29) )) >> 11);
147 put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
148 put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
149 put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
151 put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
152 put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
158 pkt->size = put_bits_count(&pb) / 8;
159 pkt->flags |= AV_PKT_FLAG_KEY;
164 #define OFFSET(x) offsetof(CLJRContext, x)
165 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
166 static const AVOption options[] = {
167 { "dither_type", "Dither type", OFFSET(dither_type), AV_OPT_TYPE_INT, { .i64=1 }, 0, 2, VE},
171 static const AVClass cljr_class = {
172 .class_name = "cljr encoder",
173 .item_name = av_default_item_name,
175 .version = LIBAVUTIL_VERSION_INT,
178 AVCodec ff_cljr_encoder = {
180 .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
181 .type = AVMEDIA_TYPE_VIDEO,
182 .id = AV_CODEC_ID_CLJR,
183 .priv_data_size = sizeof(CLJRContext),
185 .encode2 = encode_frame,
186 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
188 .priv_class = &cljr_class,