1614dd306193dc7d2ad95ed7fe78bb3e03f66583
[libav.git] / libavcodec / vcr1.c
1 /*
2 * ATI VCR1 codec
3 * Copyright (c) 2003 Michael Niedermayer
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * ATI VCR1 codec
25 */
26
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "internal.h"
30 #include "libavutil/internal.h"
31
32 typedef struct VCR1Context {
33 AVFrame picture;
34 int delta[16];
35 int offset[4];
36 } VCR1Context;
37
38 static av_cold int vcr1_common_init(AVCodecContext *avctx)
39 {
40 VCR1Context *const a = avctx->priv_data;
41
42 avctx->coded_frame = &a->picture;
43
44 return 0;
45 }
46
47 static av_cold int vcr1_decode_init(AVCodecContext *avctx)
48 {
49 vcr1_common_init(avctx);
50
51 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
52
53 return 0;
54 }
55
56 static av_cold int vcr1_decode_end(AVCodecContext *avctx)
57 {
58 VCR1Context *s = avctx->priv_data;
59
60 if (s->picture.data[0])
61 avctx->release_buffer(avctx, &s->picture);
62
63 return 0;
64 }
65
66 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
67 int *data_size, AVPacket *avpkt)
68 {
69 const uint8_t *buf = avpkt->data;
70 int buf_size = avpkt->size;
71 VCR1Context *const a = avctx->priv_data;
72 AVFrame *picture = data;
73 AVFrame *const p = &a->picture;
74 const uint8_t *bytestream = buf;
75 int i, x, y;
76
77 if (p->data[0])
78 avctx->release_buffer(avctx, p);
79
80 p->reference = 0;
81 if (ff_get_buffer(avctx, p) < 0) {
82 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
83 return -1;
84 }
85 p->pict_type = AV_PICTURE_TYPE_I;
86 p->key_frame = 1;
87
88 for (i = 0; i < 16; i++) {
89 a->delta[i] = *bytestream++;
90 bytestream++;
91 }
92
93 for (y = 0; y < avctx->height; y++) {
94 int offset;
95 uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
96
97 if ((y & 3) == 0) {
98 uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
99 uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
100
101 for (i = 0; i < 4; i++)
102 a->offset[i] = *bytestream++;
103
104 offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
105 for (x = 0; x < avctx->width; x += 4) {
106 luma[0] = offset += a->delta[bytestream[2] & 0xF];
107 luma[1] = offset += a->delta[bytestream[2] >> 4];
108 luma[2] = offset += a->delta[bytestream[0] & 0xF];
109 luma[3] = offset += a->delta[bytestream[0] >> 4];
110 luma += 4;
111
112 *cb++ = bytestream[3];
113 *cr++ = bytestream[1];
114
115 bytestream += 4;
116 }
117 } else {
118 offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
119
120 for (x = 0; x < avctx->width; x += 8) {
121 luma[0] = offset += a->delta[bytestream[2] & 0xF];
122 luma[1] = offset += a->delta[bytestream[2] >> 4];
123 luma[2] = offset += a->delta[bytestream[3] & 0xF];
124 luma[3] = offset += a->delta[bytestream[3] >> 4];
125 luma[4] = offset += a->delta[bytestream[0] & 0xF];
126 luma[5] = offset += a->delta[bytestream[0] >> 4];
127 luma[6] = offset += a->delta[bytestream[1] & 0xF];
128 luma[7] = offset += a->delta[bytestream[1] >> 4];
129 luma += 8;
130 bytestream += 4;
131 }
132 }
133 }
134
135 *picture = a->picture;
136 *data_size = sizeof(AVPicture);
137
138 return buf_size;
139 }
140
141 AVCodec ff_vcr1_decoder = {
142 .name = "vcr1",
143 .type = AVMEDIA_TYPE_VIDEO,
144 .id = AV_CODEC_ID_VCR1,
145 .priv_data_size = sizeof(VCR1Context),
146 .init = vcr1_decode_init,
147 .close = vcr1_decode_end,
148 .decode = vcr1_decode_frame,
149 .capabilities = CODEC_CAP_DR1,
150 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
151 };
152
153 /* Disable the encoder. */
154 #undef CONFIG_VCR1_ENCODER
155 #define CONFIG_VCR1_ENCODER 0
156
157 #if CONFIG_VCR1_ENCODER
158
159 #include "put_bits.h"
160
161 static int vcr1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
162 int buf_size, void *data)
163 {
164 VCR1Context *const a = avctx->priv_data;
165 AVFrame *pict = data;
166 AVFrame *const p = &a->picture;
167 int size;
168
169 *p = *pict;
170 p->pict_type = AV_PICTURE_TYPE_I;
171 p->key_frame = 1;
172
173 avpriv_align_put_bits(&a->pb);
174 flush_put_bits(&a->pb);
175
176 size = put_bits_count(&a->pb) / 32;
177
178 return size * 4;
179 }
180
181 AVCodec ff_vcr1_encoder = {
182 .name = "vcr1",
183 .type = AVMEDIA_TYPE_VIDEO,
184 .id = AV_CODEC_ID_VCR1,
185 .priv_data_size = sizeof(VCR1Context),
186 .init = vcr1_common_init,
187 .encode = vcr1_encode_frame,
188 .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
189 };
190 #endif /* CONFIG_VCR1_ENCODER */