00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "libavutil/opt.h"
00029 #include "get_bits.h"
00030 #include "internal.h"
00031 #include "put_bits.h"
00032
00033 typedef struct CLJRContext {
00034 AVClass *avclass;
00035 AVFrame picture;
00036 int dither_type;
00037 } CLJRContext;
00038
00039 static av_cold int common_init(AVCodecContext *avctx)
00040 {
00041 CLJRContext * const a = avctx->priv_data;
00042
00043 avcodec_get_frame_defaults(&a->picture);
00044 avctx->coded_frame = &a->picture;
00045
00046 return 0;
00047 }
00048
00049 #if CONFIG_CLJR_DECODER
00050 static int decode_frame(AVCodecContext *avctx,
00051 void *data, int *data_size,
00052 AVPacket *avpkt)
00053 {
00054 const uint8_t *buf = avpkt->data;
00055 int buf_size = avpkt->size;
00056 CLJRContext * const a = avctx->priv_data;
00057 GetBitContext gb;
00058 AVFrame *picture = data;
00059 AVFrame * const p = &a->picture;
00060 int x, y;
00061
00062 if (p->data[0])
00063 avctx->release_buffer(avctx, p);
00064
00065 if (avctx->height <= 0 || avctx->width <= 0) {
00066 av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
00067 return AVERROR_INVALIDDATA;
00068 }
00069
00070 if (buf_size / avctx->height < avctx->width) {
00071 av_log(avctx, AV_LOG_ERROR,
00072 "Resolution larger than buffer size. Invalid header?\n");
00073 return AVERROR_INVALIDDATA;
00074 }
00075
00076 p->reference = 0;
00077 if (avctx->get_buffer(avctx, p) < 0) {
00078 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00079 return -1;
00080 }
00081 p->pict_type = AV_PICTURE_TYPE_I;
00082 p->key_frame = 1;
00083
00084 init_get_bits(&gb, buf, buf_size * 8);
00085
00086 for (y = 0; y < avctx->height; y++) {
00087 uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
00088 uint8_t *cb = &a->picture.data[1][y * a->picture.linesize[1]];
00089 uint8_t *cr = &a->picture.data[2][y * a->picture.linesize[2]];
00090 for (x = 0; x < avctx->width; x += 4) {
00091 luma[3] = (get_bits(&gb, 5)*33) >> 2;
00092 luma[2] = (get_bits(&gb, 5)*33) >> 2;
00093 luma[1] = (get_bits(&gb, 5)*33) >> 2;
00094 luma[0] = (get_bits(&gb, 5)*33) >> 2;
00095 luma += 4;
00096 *(cb++) = get_bits(&gb, 6) << 2;
00097 *(cr++) = get_bits(&gb, 6) << 2;
00098 }
00099 }
00100
00101 *picture = a->picture;
00102 *data_size = sizeof(AVPicture);
00103
00104 return buf_size;
00105 }
00106
00107 static av_cold int decode_init(AVCodecContext *avctx)
00108 {
00109 avctx->pix_fmt = PIX_FMT_YUV411P;
00110 return common_init(avctx);
00111 }
00112
00113 static av_cold int decode_end(AVCodecContext *avctx)
00114 {
00115 CLJRContext *a = avctx->priv_data;
00116
00117 if (a->picture.data[0])
00118 avctx->release_buffer(avctx, &a->picture);
00119 return 0;
00120 }
00121
00122 AVCodec ff_cljr_decoder = {
00123 .name = "cljr",
00124 .type = AVMEDIA_TYPE_VIDEO,
00125 .id = CODEC_ID_CLJR,
00126 .priv_data_size = sizeof(CLJRContext),
00127 .init = decode_init,
00128 .close = decode_end,
00129 .decode = decode_frame,
00130 .capabilities = CODEC_CAP_DR1,
00131 .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
00132 };
00133 #endif
00134
00135 #if CONFIG_CLJR_ENCODER
00136 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00137 const AVFrame *p, int *got_packet)
00138 {
00139 CLJRContext *a = avctx->priv_data;
00140 PutBitContext pb;
00141 int x, y, ret;
00142 uint32_t dither= avctx->frame_number;
00143 static const uint32_t ordered_dither[2][2] =
00144 {
00145 { 0x10400000, 0x104F0000 },
00146 { 0xCB2A0000, 0xCB250000 },
00147 };
00148
00149 if ((ret = ff_alloc_packet2(avctx, pkt, 32*avctx->height*avctx->width/4)) < 0)
00150 return ret;
00151
00152 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00153 avctx->coded_frame->key_frame = 1;
00154
00155 init_put_bits(&pb, pkt->data, pkt->size);
00156
00157 for (y = 0; y < avctx->height; y++) {
00158 uint8_t *luma = &p->data[0][y * p->linesize[0]];
00159 uint8_t *cb = &p->data[1][y * p->linesize[1]];
00160 uint8_t *cr = &p->data[2][y * p->linesize[2]];
00161 for (x = 0; x < avctx->width; x += 4) {
00162 switch (a->dither_type) {
00163 case 0: dither = 0x492A0000; break;
00164 case 1: dither = dither * 1664525 + 1013904223; break;
00165 case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
00166 }
00167 put_bits(&pb, 5, (249*(luma[3] + (dither>>29) )) >> 11);
00168 put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
00169 put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
00170 put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
00171 luma += 4;
00172 put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
00173 put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
00174 }
00175 }
00176
00177 flush_put_bits(&pb);
00178
00179 pkt->size = put_bits_count(&pb) / 8;
00180 pkt->flags |= AV_PKT_FLAG_KEY;
00181 *got_packet = 1;
00182 return 0;
00183 }
00184
00185 #define OFFSET(x) offsetof(CLJRContext, x)
00186 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00187 static const AVOption options[] = {
00188 { "dither_type", "Dither type", OFFSET(dither_type), AV_OPT_TYPE_INT, { .dbl=1 }, 0, 2, VE},
00189 { NULL },
00190 };
00191
00192 static const AVClass class = {
00193 .class_name = "cljr encoder",
00194 .item_name = av_default_item_name,
00195 .option = options,
00196 .version = LIBAVUTIL_VERSION_INT,
00197 };
00198
00199 AVCodec ff_cljr_encoder = {
00200 .name = "cljr",
00201 .type = AVMEDIA_TYPE_VIDEO,
00202 .id = CODEC_ID_CLJR,
00203 .priv_data_size = sizeof(CLJRContext),
00204 .init = common_init,
00205 .encode2 = encode_frame,
00206 .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_YUV411P,
00207 PIX_FMT_NONE },
00208 .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
00209 .priv_class = &class,
00210 };
00211 #endif