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