]> git.sesse.net Git - ffmpeg/blob - libavcodec/cljr.c
FATE: add a test for the overlay filter
[ffmpeg] / libavcodec / cljr.c
1 /*
2  * Cirrus Logic AccuPak (CLJR) codec
3  * Copyright (c) 2003 Alex Beregszaszi
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Cirrus Logic AccuPak codec.
25  */
26
27 #include "avcodec.h"
28 #include "get_bits.h"
29 #include "internal.h"
30 #include "put_bits.h"
31
32 #if CONFIG_CLJR_DECODER
33 static int decode_frame(AVCodecContext *avctx,
34                         void *data, int *got_frame,
35                         AVPacket *avpkt)
36 {
37     const uint8_t *buf = avpkt->data;
38     int buf_size       = avpkt->size;
39     GetBitContext gb;
40     AVFrame * const p = data;
41     int x, y, ret;
42
43     if (avctx->height <= 0 || avctx->width <= 0) {
44         av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
45         return AVERROR_INVALIDDATA;
46     }
47
48     if (buf_size < avctx->height * avctx->width) {
49         av_log(avctx, AV_LOG_ERROR,
50                "Resolution larger than buffer size. Invalid header?\n");
51         return AVERROR_INVALIDDATA;
52     }
53
54     if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
55         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
56         return ret;
57     }
58     p->pict_type = AV_PICTURE_TYPE_I;
59     p->key_frame = 1;
60
61     init_get_bits(&gb, buf, buf_size * 8);
62
63     for (y = 0; y < avctx->height; y++) {
64         uint8_t *luma = &p->data[0][y * p->linesize[0]];
65         uint8_t *cb   = &p->data[1][y * p->linesize[1]];
66         uint8_t *cr   = &p->data[2][y * p->linesize[2]];
67         for (x = 0; x < avctx->width; x += 4) {
68             luma[3] = get_bits(&gb, 5) << 3;
69             luma[2] = get_bits(&gb, 5) << 3;
70             luma[1] = get_bits(&gb, 5) << 3;
71             luma[0] = get_bits(&gb, 5) << 3;
72             luma += 4;
73             *(cb++) = get_bits(&gb, 6) << 2;
74             *(cr++) = get_bits(&gb, 6) << 2;
75         }
76     }
77
78     *got_frame = 1;
79
80     return buf_size;
81 }
82
83 static av_cold int decode_init(AVCodecContext *avctx)
84 {
85     avctx->pix_fmt = AV_PIX_FMT_YUV411P;
86     return 0;
87 }
88
89 AVCodec ff_cljr_decoder = {
90     .name           = "cljr",
91     .type           = AVMEDIA_TYPE_VIDEO,
92     .id             = AV_CODEC_ID_CLJR,
93     .init           = decode_init,
94     .decode         = decode_frame,
95     .capabilities   = CODEC_CAP_DR1,
96     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
97 };
98 #endif
99
100 #if CONFIG_CLJR_ENCODER
101 typedef struct CLJRContext {
102     AVFrame         picture;
103 } CLJRContext;
104
105 static av_cold int encode_init(AVCodecContext *avctx)
106 {
107     CLJRContext * const a = avctx->priv_data;
108
109     avctx->coded_frame = &a->picture;
110
111     return 0;
112 }
113
114 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
115                         const AVFrame *p, int *got_packet)
116 {
117     PutBitContext pb;
118     int x, y, ret;
119
120     if ((ret = ff_alloc_packet(pkt, 32*avctx->height*avctx->width/4)) < 0) {
121         av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
122         return ret;
123     }
124
125     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
126     avctx->coded_frame->key_frame = 1;
127
128     init_put_bits(&pb, pkt->data, pkt->size);
129
130     for (y = 0; y < avctx->height; y++) {
131         uint8_t *luma = &p->data[0][y * p->linesize[0]];
132         uint8_t *cb   = &p->data[1][y * p->linesize[1]];
133         uint8_t *cr   = &p->data[2][y * p->linesize[2]];
134         for (x = 0; x < avctx->width; x += 4) {
135             put_bits(&pb, 5, luma[3] >> 3);
136             put_bits(&pb, 5, luma[2] >> 3);
137             put_bits(&pb, 5, luma[1] >> 3);
138             put_bits(&pb, 5, luma[0] >> 3);
139             luma += 4;
140             put_bits(&pb, 6, *(cb++) >> 2);
141             put_bits(&pb, 6, *(cr++) >> 2);
142         }
143     }
144
145     flush_put_bits(&pb);
146
147     pkt->size   = put_bits_count(&pb) / 8;
148     pkt->flags |= AV_PKT_FLAG_KEY;
149     *got_packet = 1;
150     return 0;
151 }
152
153 AVCodec ff_cljr_encoder = {
154     .name           = "cljr",
155     .type           = AVMEDIA_TYPE_VIDEO,
156     .id             = AV_CODEC_ID_CLJR,
157     .priv_data_size = sizeof(CLJRContext),
158     .init           = encode_init,
159     .encode2        = encode_frame,
160     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
161                                                    AV_PIX_FMT_NONE },
162     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
163 };
164 #endif