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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 "get_bits.h"
00029 #include "put_bits.h"
00030 
00031 typedef struct CLJRContext {
00032     AVFrame         picture;
00033 } CLJRContext;
00034 
00035 static av_cold int common_init(AVCodecContext *avctx)
00036 {
00037     CLJRContext * const a = avctx->priv_data;
00038 
00039     avctx->coded_frame = &a->picture;
00040 
00041     return 0;
00042 }
00043 
00044 #if CONFIG_CLJR_DECODER
00045 static int decode_frame(AVCodecContext *avctx,
00046                         void *data, int *data_size,
00047                         AVPacket *avpkt)
00048 {
00049     const uint8_t *buf = avpkt->data;
00050     int buf_size       = avpkt->size;
00051     CLJRContext * const a = avctx->priv_data;
00052     GetBitContext gb;
00053     AVFrame *picture = data;
00054     AVFrame * const p = &a->picture;
00055     int x, y;
00056 
00057     if (p->data[0])
00058         avctx->release_buffer(avctx, p);
00059 
00060     if (avctx->height <= 0 || avctx->width <= 0) {
00061         av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
00062         return AVERROR_INVALIDDATA;
00063     }
00064 
00065     if (buf_size < avctx->height * avctx->width) {
00066         av_log(avctx, AV_LOG_ERROR,
00067                "Resolution larger than buffer size. Invalid header?\n");
00068         return AVERROR_INVALIDDATA;
00069     }
00070 
00071     p->reference = 0;
00072     if (avctx->get_buffer(avctx, p) < 0) {
00073         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00074         return -1;
00075     }
00076     p->pict_type = AV_PICTURE_TYPE_I;
00077     p->key_frame = 1;
00078 
00079     init_get_bits(&gb, buf, buf_size * 8);
00080 
00081     for (y = 0; y < avctx->height; y++) {
00082         uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
00083         uint8_t *cb   = &a->picture.data[1][y * a->picture.linesize[1]];
00084         uint8_t *cr   = &a->picture.data[2][y * a->picture.linesize[2]];
00085         for (x = 0; x < avctx->width; x += 4) {
00086             luma[3] = get_bits(&gb, 5) << 3;
00087             luma[2] = get_bits(&gb, 5) << 3;
00088             luma[1] = get_bits(&gb, 5) << 3;
00089             luma[0] = get_bits(&gb, 5) << 3;
00090             luma += 4;
00091             *(cb++) = get_bits(&gb, 6) << 2;
00092             *(cr++) = get_bits(&gb, 6) << 2;
00093         }
00094     }
00095 
00096     *picture   = a->picture;
00097     *data_size = sizeof(AVPicture);
00098 
00099     return buf_size;
00100 }
00101 
00102 static av_cold int decode_init(AVCodecContext *avctx)
00103 {
00104     avctx->pix_fmt = PIX_FMT_YUV411P;
00105     return common_init(avctx);
00106 }
00107 
00108 static av_cold int decode_end(AVCodecContext *avctx)
00109 {
00110     CLJRContext *a = avctx->priv_data;
00111 
00112     if (a->picture.data[0])
00113         avctx->release_buffer(avctx, &a->picture);
00114     return 0;
00115 }
00116 
00117 AVCodec ff_cljr_decoder = {
00118     .name           = "cljr",
00119     .type           = AVMEDIA_TYPE_VIDEO,
00120     .id             = CODEC_ID_CLJR,
00121     .priv_data_size = sizeof(CLJRContext),
00122     .init           = decode_init,
00123     .close          = decode_end,
00124     .decode         = decode_frame,
00125     .capabilities   = CODEC_CAP_DR1,
00126     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
00127 };
00128 #endif
00129 
00130 #if CONFIG_CLJR_ENCODER
00131 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
00132                         int buf_size, void *data)
00133 {
00134     PutBitContext pb;
00135     AVFrame *p = data;
00136     int x, y;
00137 
00138     p->pict_type = AV_PICTURE_TYPE_I;
00139     p->key_frame = 1;
00140 
00141     init_put_bits(&pb, buf, buf_size / 8);
00142 
00143     for (y = 0; y < avctx->height; y++) {
00144         uint8_t *luma = &p->data[0][y * p->linesize[0]];
00145         uint8_t *cb   = &p->data[1][y * p->linesize[1]];
00146         uint8_t *cr   = &p->data[2][y * p->linesize[2]];
00147         for (x = 0; x < avctx->width; x += 4) {
00148             put_bits(&pb, 5, luma[3] >> 3);
00149             put_bits(&pb, 5, luma[2] >> 3);
00150             put_bits(&pb, 5, luma[1] >> 3);
00151             put_bits(&pb, 5, luma[0] >> 3);
00152             luma += 4;
00153             put_bits(&pb, 6, *(cb++) >> 2);
00154             put_bits(&pb, 6, *(cr++) >> 2);
00155         }
00156     }
00157 
00158     flush_put_bits(&pb);
00159 
00160     return put_bits_count(&pb) / 8;
00161 }
00162 
00163 AVCodec ff_cljr_encoder = {
00164     .name           = "cljr",
00165     .type           = AVMEDIA_TYPE_VIDEO,
00166     .id             = CODEC_ID_CLJR,
00167     .priv_data_size = sizeof(CLJRContext),
00168     .init           = common_init,
00169     .encode         = encode_frame,
00170     .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV411P,
00171                                                    PIX_FMT_NONE },
00172     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
00173 };
00174 #endif