vcr1.c 5.26 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1 2 3 4
/*
 * ATI VCR1 codec
 * Copyright (c) 2003 Michael Niedermayer
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Michael Niedermayer's avatar
Michael Niedermayer committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Michael Niedermayer's avatar
Michael Niedermayer committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Michael Niedermayer's avatar
Michael Niedermayer committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Michael Niedermayer's avatar
Michael Niedermayer committed
20
 */
21

Michael Niedermayer's avatar
Michael Niedermayer committed
22
/**
23
 * @file
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25
 * ati vcr1 codec.
 */
26

Michael Niedermayer's avatar
Michael Niedermayer committed
27
#include "avcodec.h"
28
#include "dsputil.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
29 30 31 32

//#undef NDEBUG
//#include <assert.h>

33 34
/* Disable the encoder. */
#undef CONFIG_VCR1_ENCODER
35
#define CONFIG_VCR1_ENCODER 0
36

Michael Niedermayer's avatar
Michael Niedermayer committed
37 38 39 40 41 42 43
typedef struct VCR1Context{
    AVCodecContext *avctx;
    AVFrame picture;
    int delta[16];
    int offset[4];
} VCR1Context;

44
static int decode_frame(AVCodecContext *avctx,
Michael Niedermayer's avatar
Michael Niedermayer committed
45
                        void *data, int *data_size,
46
                        AVPacket *avpkt)
Michael Niedermayer's avatar
Michael Niedermayer committed
47
{
48 49
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
Michael Niedermayer's avatar
Michael Niedermayer committed
50 51 52
    VCR1Context * const a = avctx->priv_data;
    AVFrame *picture = data;
    AVFrame * const p= (AVFrame*)&a->picture;
Michael Niedermayer's avatar
Michael Niedermayer committed
53
    const uint8_t *bytestream= buf;
Michael Niedermayer's avatar
Michael Niedermayer committed
54 55 56 57 58 59 60
    int i, x, y;

    if(p->data[0])
        avctx->release_buffer(avctx, p);

    p->reference= 0;
    if(avctx->get_buffer(avctx, p) < 0){
61
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
Michael Niedermayer's avatar
Michael Niedermayer committed
62 63
        return -1;
    }
64
    p->pict_type= AV_PICTURE_TYPE_I;
Michael Niedermayer's avatar
Michael Niedermayer committed
65 66 67 68 69 70
    p->key_frame= 1;

    for(i=0; i<16; i++){
        a->delta[i]= *(bytestream++);
        bytestream++;
    }
71

Michael Niedermayer's avatar
Michael Niedermayer committed
72 73 74 75 76 77 78 79 80 81 82
    for(y=0; y<avctx->height; y++){
        int offset;
        uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ];

        if((y&3) == 0){
            uint8_t *cb= &a->picture.data[1][ (y>>2)*a->picture.linesize[1] ];
            uint8_t *cr= &a->picture.data[2][ (y>>2)*a->picture.linesize[2] ];

            for(i=0; i<4; i++)
                a->offset[i]= *(bytestream++);

Michael Niedermayer's avatar
Michael Niedermayer committed
83
            offset= a->offset[0] - a->delta[ bytestream[2]&0xF ];
Michael Niedermayer's avatar
Michael Niedermayer committed
84 85 86 87 88 89
            for(x=0; x<avctx->width; x+=4){
                luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
                luma[1]=( offset += a->delta[ bytestream[2]>>4  ]);
                luma[2]=( offset += a->delta[ bytestream[0]&0xF ]);
                luma[3]=( offset += a->delta[ bytestream[0]>>4  ]);
                luma += 4;
90

Michael Niedermayer's avatar
Michael Niedermayer committed
91 92
                *(cb++) = bytestream[3];
                *(cr++) = bytestream[1];
93

Michael Niedermayer's avatar
Michael Niedermayer committed
94 95 96
                bytestream+= 4;
            }
        }else{
Michael Niedermayer's avatar
Michael Niedermayer committed
97
            offset= a->offset[y&3] - a->delta[ bytestream[2]&0xF ];
Michael Niedermayer's avatar
Michael Niedermayer committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

            for(x=0; x<avctx->width; x+=8){
                luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
                luma[1]=( offset += a->delta[ bytestream[2]>>4  ]);
                luma[2]=( offset += a->delta[ bytestream[3]&0xF ]);
                luma[3]=( offset += a->delta[ bytestream[3]>>4  ]);
                luma[4]=( offset += a->delta[ bytestream[0]&0xF ]);
                luma[5]=( offset += a->delta[ bytestream[0]>>4  ]);
                luma[6]=( offset += a->delta[ bytestream[1]&0xF ]);
                luma[7]=( offset += a->delta[ bytestream[1]>>4  ]);
                luma += 8;
                bytestream+= 4;
            }
        }
    }

    *picture= *(AVFrame*)&a->picture;
    *data_size = sizeof(AVPicture);

    emms_c();
118

Michael Niedermayer's avatar
Michael Niedermayer committed
119 120 121
    return buf_size;
}

122
#if CONFIG_VCR1_ENCODER
Michael Niedermayer's avatar
Michael Niedermayer committed
123 124 125 126 127 128 129
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
    VCR1Context * const a = avctx->priv_data;
    AVFrame *pict = data;
    AVFrame * const p= (AVFrame*)&a->picture;
    int size;

    *p = *pict;
130
    p->pict_type= AV_PICTURE_TYPE_I;
Michael Niedermayer's avatar
Michael Niedermayer committed
131 132 133
    p->key_frame= 1;

    emms_c();
134

Michael Niedermayer's avatar
Michael Niedermayer committed
135 136 137
    align_put_bits(&a->pb);
    while(get_bit_count(&a->pb)&31)
        put_bits(&a->pb, 8, 0);
138

Michael Niedermayer's avatar
Michael Niedermayer committed
139
    size= get_bit_count(&a->pb)/32;
140

Michael Niedermayer's avatar
Michael Niedermayer committed
141 142 143 144
    return size*4;
}
#endif

145
static av_cold void common_init(AVCodecContext *avctx){
Michael Niedermayer's avatar
Michael Niedermayer committed
146 147 148
    VCR1Context * const a = avctx->priv_data;

    avctx->coded_frame= (AVFrame*)&a->picture;
149
    avcodec_get_frame_defaults(&a->picture);
Michael Niedermayer's avatar
Michael Niedermayer committed
150 151 152
    a->avctx= avctx;
}

153
static av_cold int decode_init(AVCodecContext *avctx){
154

Michael Niedermayer's avatar
Michael Niedermayer committed
155
    common_init(avctx);
156

Michael Niedermayer's avatar
Michael Niedermayer committed
157 158 159 160 161
    avctx->pix_fmt= PIX_FMT_YUV410P;

    return 0;
}

162 163 164 165 166 167 168 169 170
static av_cold int decode_end(AVCodecContext *avctx){
    VCR1Context *s = avctx->priv_data;

    if (s->picture.data[0])
        avctx->release_buffer(avctx, &s->picture);

    return 0;
}

171
#if CONFIG_VCR1_ENCODER
172
static av_cold int encode_init(AVCodecContext *avctx){
173

Michael Niedermayer's avatar
Michael Niedermayer committed
174
    common_init(avctx);
175

Michael Niedermayer's avatar
Michael Niedermayer committed
176 177
    return 0;
}
178
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
179

180
AVCodec ff_vcr1_decoder = {
Michael Niedermayer's avatar
Michael Niedermayer committed
181
    "vcr1",
182
    AVMEDIA_TYPE_VIDEO,
Michael Niedermayer's avatar
Michael Niedermayer committed
183 184 185 186
    CODEC_ID_VCR1,
    sizeof(VCR1Context),
    decode_init,
    NULL,
187
    decode_end,
Michael Niedermayer's avatar
Michael Niedermayer committed
188 189
    decode_frame,
    CODEC_CAP_DR1,
190
    .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
Michael Niedermayer's avatar
Michael Niedermayer committed
191
};
192

193
#if CONFIG_VCR1_ENCODER
194
AVCodec ff_vcr1_encoder = {
Michael Niedermayer's avatar
Michael Niedermayer committed
195
    "vcr1",
196
    AVMEDIA_TYPE_VIDEO,
Michael Niedermayer's avatar
Michael Niedermayer committed
197 198 199 200 201
    CODEC_ID_VCR1,
    sizeof(VCR1Context),
    encode_init,
    encode_frame,
    //encode_end,
202
    .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
Michael Niedermayer's avatar
Michael Niedermayer committed
203
};
204
#endif