3 * Copyright (c) 2003 Michael Niedermayer
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * 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
30 #include "libavutil/internal.h"
32 typedef struct VCR1Context {
38 static av_cold int vcr1_common_init(AVCodecContext *avctx)
40 VCR1Context *const a = avctx->priv_data;
42 avctx->coded_frame = &a->picture;
43 avcodec_get_frame_defaults(&a->picture);
48 static av_cold int vcr1_decode_init(AVCodecContext *avctx)
50 vcr1_common_init(avctx);
52 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
54 if (avctx->width % 8 || avctx->height%4) {
55 av_log_ask_for_sample(avctx, "odd dimensions are not supported\n");
56 return AVERROR_PATCHWELCOME;
61 static av_cold int vcr1_decode_end(AVCodecContext *avctx)
63 VCR1Context *s = avctx->priv_data;
65 if (s->picture.data[0])
66 avctx->release_buffer(avctx, &s->picture);
71 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
72 int *got_frame, AVPacket *avpkt)
74 const uint8_t *buf = avpkt->data;
75 int buf_size = avpkt->size;
76 VCR1Context *const a = avctx->priv_data;
77 AVFrame *picture = data;
78 AVFrame *const p = &a->picture;
79 const uint8_t *bytestream = buf;
83 avctx->release_buffer(avctx, p);
85 if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
86 av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
87 return AVERROR(EINVAL);
91 if (ff_get_buffer(avctx, p) < 0) {
92 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
95 p->pict_type = AV_PICTURE_TYPE_I;
98 for (i = 0; i < 16; i++) {
99 a->delta[i] = *bytestream++;
103 for (y = 0; y < avctx->height; y++) {
105 uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
108 uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
109 uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
111 for (i = 0; i < 4; i++)
112 a->offset[i] = *bytestream++;
114 offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
115 for (x = 0; x < avctx->width; x += 4) {
116 luma[0] = offset += a->delta[bytestream[2] & 0xF];
117 luma[1] = offset += a->delta[bytestream[2] >> 4];
118 luma[2] = offset += a->delta[bytestream[0] & 0xF];
119 luma[3] = offset += a->delta[bytestream[0] >> 4];
122 *cb++ = bytestream[3];
123 *cr++ = bytestream[1];
128 offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
130 for (x = 0; x < avctx->width; x += 8) {
131 luma[0] = offset += a->delta[bytestream[2] & 0xF];
132 luma[1] = offset += a->delta[bytestream[2] >> 4];
133 luma[2] = offset += a->delta[bytestream[3] & 0xF];
134 luma[3] = offset += a->delta[bytestream[3] >> 4];
135 luma[4] = offset += a->delta[bytestream[0] & 0xF];
136 luma[5] = offset += a->delta[bytestream[0] >> 4];
137 luma[6] = offset += a->delta[bytestream[1] & 0xF];
138 luma[7] = offset += a->delta[bytestream[1] >> 4];
145 *picture = a->picture;
151 AVCodec ff_vcr1_decoder = {
153 .type = AVMEDIA_TYPE_VIDEO,
154 .id = AV_CODEC_ID_VCR1,
155 .priv_data_size = sizeof(VCR1Context),
156 .init = vcr1_decode_init,
157 .close = vcr1_decode_end,
158 .decode = vcr1_decode_frame,
159 .capabilities = CODEC_CAP_DR1,
160 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
163 /* Disable the encoder. */
164 #undef CONFIG_VCR1_ENCODER
165 #define CONFIG_VCR1_ENCODER 0
167 #if CONFIG_VCR1_ENCODER
169 #include "put_bits.h"
171 static int vcr1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
172 int buf_size, void *data)
174 VCR1Context *const a = avctx->priv_data;
175 AVFrame *pict = data;
176 AVFrame *const p = &a->picture;
180 p->pict_type = AV_PICTURE_TYPE_I;
183 avpriv_align_put_bits(&a->pb);
184 flush_put_bits(&a->pb);
186 size = put_bits_count(&a->pb) / 32;
191 AVCodec ff_vcr1_encoder = {
193 .type = AVMEDIA_TYPE_VIDEO,
194 .id = AV_CODEC_ID_VCR1,
195 .priv_data_size = sizeof(VCR1Context),
196 .init = vcr1_common_init,
197 .encode = vcr1_encode_frame,
198 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
200 #endif /* CONFIG_VCR1_ENCODER */