2 * Cirrus Logic AccuPak (CLJR) codec
3 * Copyright (c) 2003 Alex Beregszaszi
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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.
32 typedef struct CLJRContext {
36 static av_cold int common_init(AVCodecContext *avctx)
38 CLJRContext * const a = avctx->priv_data;
40 avctx->coded_frame = &a->picture;
45 #if CONFIG_CLJR_DECODER
46 static int decode_frame(AVCodecContext *avctx,
47 void *data, int *got_frame,
50 const uint8_t *buf = avpkt->data;
51 int buf_size = avpkt->size;
52 CLJRContext * const a = avctx->priv_data;
54 AVFrame *picture = data;
55 AVFrame * const p = &a->picture;
59 avctx->release_buffer(avctx, p);
61 if (avctx->height <= 0 || avctx->width <= 0) {
62 av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
63 return AVERROR_INVALIDDATA;
66 if (buf_size < avctx->height * avctx->width) {
67 av_log(avctx, AV_LOG_ERROR,
68 "Resolution larger than buffer size. Invalid header?\n");
69 return AVERROR_INVALIDDATA;
73 if (ff_get_buffer(avctx, p) < 0) {
74 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
77 p->pict_type = AV_PICTURE_TYPE_I;
80 init_get_bits(&gb, buf, buf_size * 8);
82 for (y = 0; y < avctx->height; y++) {
83 uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
84 uint8_t *cb = &a->picture.data[1][y * a->picture.linesize[1]];
85 uint8_t *cr = &a->picture.data[2][y * a->picture.linesize[2]];
86 for (x = 0; x < avctx->width; x += 4) {
87 luma[3] = get_bits(&gb, 5) << 3;
88 luma[2] = get_bits(&gb, 5) << 3;
89 luma[1] = get_bits(&gb, 5) << 3;
90 luma[0] = get_bits(&gb, 5) << 3;
92 *(cb++) = get_bits(&gb, 6) << 2;
93 *(cr++) = get_bits(&gb, 6) << 2;
97 *picture = a->picture;
103 static av_cold int decode_init(AVCodecContext *avctx)
105 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
106 return common_init(avctx);
109 static av_cold int decode_end(AVCodecContext *avctx)
111 CLJRContext *a = avctx->priv_data;
113 if (a->picture.data[0])
114 avctx->release_buffer(avctx, &a->picture);
118 AVCodec ff_cljr_decoder = {
120 .type = AVMEDIA_TYPE_VIDEO,
121 .id = AV_CODEC_ID_CLJR,
122 .priv_data_size = sizeof(CLJRContext),
125 .decode = decode_frame,
126 .capabilities = CODEC_CAP_DR1,
127 .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
131 #if CONFIG_CLJR_ENCODER
132 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
133 const AVFrame *p, int *got_packet)
138 if ((ret = ff_alloc_packet(pkt, 32*avctx->height*avctx->width/4)) < 0) {
139 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
143 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
144 avctx->coded_frame->key_frame = 1;
146 init_put_bits(&pb, pkt->data, pkt->size);
148 for (y = 0; y < avctx->height; y++) {
149 uint8_t *luma = &p->data[0][y * p->linesize[0]];
150 uint8_t *cb = &p->data[1][y * p->linesize[1]];
151 uint8_t *cr = &p->data[2][y * p->linesize[2]];
152 for (x = 0; x < avctx->width; x += 4) {
153 put_bits(&pb, 5, luma[3] >> 3);
154 put_bits(&pb, 5, luma[2] >> 3);
155 put_bits(&pb, 5, luma[1] >> 3);
156 put_bits(&pb, 5, luma[0] >> 3);
158 put_bits(&pb, 6, *(cb++) >> 2);
159 put_bits(&pb, 6, *(cr++) >> 2);
165 pkt->size = put_bits_count(&pb) / 8;
166 pkt->flags |= AV_PKT_FLAG_KEY;
171 AVCodec ff_cljr_encoder = {
173 .type = AVMEDIA_TYPE_VIDEO,
174 .id = AV_CODEC_ID_CLJR,
175 .priv_data_size = sizeof(CLJRContext),
177 .encode2 = encode_frame,
178 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
180 .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),