• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/cljr.c

Go to the documentation of this file.
00001 /*
00002  * Cirrus Logic AccuPak (CLJR) codec
00003  * Copyright (c) 2003 Alex Beregszaszi
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #include "libavutil/opt.h"
00029 #include "get_bits.h"
00030 #include "put_bits.h"
00031 
00032 typedef struct CLJRContext {
00033     AVClass        *avclass;
00034     AVFrame         picture;
00035     int             dither_type;
00036 } CLJRContext;
00037 
00038 static av_cold int common_init(AVCodecContext *avctx)
00039 {
00040     CLJRContext * const a = avctx->priv_data;
00041 
00042     avcodec_get_frame_defaults(&a->picture);
00043     avctx->coded_frame = &a->picture;
00044 
00045     return 0;
00046 }
00047 
00048 #if CONFIG_CLJR_DECODER
00049 static int decode_frame(AVCodecContext *avctx,
00050                         void *data, int *data_size,
00051                         AVPacket *avpkt)
00052 {
00053     const uint8_t *buf = avpkt->data;
00054     int buf_size       = avpkt->size;
00055     CLJRContext * const a = avctx->priv_data;
00056     GetBitContext gb;
00057     AVFrame *picture = data;
00058     AVFrame * const p = &a->picture;
00059     int x, y;
00060 
00061     if (p->data[0])
00062         avctx->release_buffer(avctx, p);
00063 
00064     if (avctx->height <= 0 || avctx->width <= 0) {
00065         av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
00066         return AVERROR_INVALIDDATA;
00067     }
00068 
00069     if (buf_size / avctx->height < avctx->width) {
00070         av_log(avctx, AV_LOG_ERROR,
00071                "Resolution larger than buffer size. Invalid header?\n");
00072         return AVERROR_INVALIDDATA;
00073     }
00074 
00075     p->reference = 0;
00076     if (avctx->get_buffer(avctx, p) < 0) {
00077         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00078         return -1;
00079     }
00080     p->pict_type = AV_PICTURE_TYPE_I;
00081     p->key_frame = 1;
00082 
00083     init_get_bits(&gb, buf, buf_size * 8);
00084 
00085     for (y = 0; y < avctx->height; y++) {
00086         uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
00087         uint8_t *cb   = &a->picture.data[1][y * a->picture.linesize[1]];
00088         uint8_t *cr   = &a->picture.data[2][y * a->picture.linesize[2]];
00089         for (x = 0; x < avctx->width; x += 4) {
00090             luma[3] = (get_bits(&gb, 5)*33) >> 2;
00091             luma[2] = (get_bits(&gb, 5)*33) >> 2;
00092             luma[1] = (get_bits(&gb, 5)*33) >> 2;
00093             luma[0] = (get_bits(&gb, 5)*33) >> 2;
00094             luma += 4;
00095             *(cb++) = get_bits(&gb, 6) << 2;
00096             *(cr++) = get_bits(&gb, 6) << 2;
00097         }
00098     }
00099 
00100     *picture   = a->picture;
00101     *data_size = sizeof(AVPicture);
00102 
00103     return buf_size;
00104 }
00105 
00106 static av_cold int decode_init(AVCodecContext *avctx)
00107 {
00108     avctx->pix_fmt = PIX_FMT_YUV411P;
00109     return common_init(avctx);
00110 }
00111 
00112 static av_cold int decode_end(AVCodecContext *avctx)
00113 {
00114     CLJRContext *a = avctx->priv_data;
00115 
00116     if (a->picture.data[0])
00117         avctx->release_buffer(avctx, &a->picture);
00118     return 0;
00119 }
00120 
00121 AVCodec ff_cljr_decoder = {
00122     .name           = "cljr",
00123     .type           = AVMEDIA_TYPE_VIDEO,
00124     .id             = CODEC_ID_CLJR,
00125     .priv_data_size = sizeof(CLJRContext),
00126     .init           = decode_init,
00127     .close          = decode_end,
00128     .decode         = decode_frame,
00129     .capabilities   = CODEC_CAP_DR1,
00130     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
00131 };
00132 #endif
00133 
00134 #if CONFIG_CLJR_ENCODER
00135 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
00136                         int buf_size, void *data)
00137 {
00138     CLJRContext *a = avctx->priv_data;
00139     PutBitContext pb;
00140     AVFrame *p = data;
00141     int x, y;
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     p->pict_type = AV_PICTURE_TYPE_I;
00150     p->key_frame = 1;
00151 
00152     init_put_bits(&pb, buf, buf_size / 8);
00153 
00154     for (y = 0; y < avctx->height; y++) {
00155         uint8_t *luma = &p->data[0][y * p->linesize[0]];
00156         uint8_t *cb   = &p->data[1][y * p->linesize[1]];
00157         uint8_t *cr   = &p->data[2][y * p->linesize[2]];
00158         for (x = 0; x < avctx->width; x += 4) {
00159             switch (a->dither_type) {
00160             case 0: dither = 0x492A0000;                       break;
00161             case 1: dither = dither * 1664525 + 1013904223;    break;
00162             case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
00163             }
00164             put_bits(&pb, 5, (249*(luma[3] +  (dither>>29)   )) >> 11);
00165             put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
00166             put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
00167             put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
00168             luma += 4;
00169             put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
00170             put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
00171         }
00172     }
00173 
00174     flush_put_bits(&pb);
00175 
00176     return put_bits_count(&pb) / 8;
00177 }
00178 
00179 #define OFFSET(x) offsetof(CLJRContext, x)
00180 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00181 static const AVOption options[] = {
00182     { "dither_type",   "Dither type",   OFFSET(dither_type),        AV_OPT_TYPE_INT, { .dbl=1 }, 0, 2, VE},
00183     { NULL },
00184 };
00185 
00186 static const AVClass class = {
00187     .class_name = "cljr encoder",
00188     .item_name  = av_default_item_name,
00189     .option     = options,
00190     .version    = LIBAVUTIL_VERSION_INT,
00191 };
00192 
00193 AVCodec ff_cljr_encoder = {
00194     .name           = "cljr",
00195     .type           = AVMEDIA_TYPE_VIDEO,
00196     .id             = CODEC_ID_CLJR,
00197     .priv_data_size = sizeof(CLJRContext),
00198     .init           = common_init,
00199     .encode         = encode_frame,
00200     .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV411P,
00201                                                    PIX_FMT_NONE },
00202     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
00203     .priv_class     = &class,
00204 };
00205 #endif
Generated on Fri Feb 1 2013 14:34:31 for FFmpeg by doxygen 1.7.1