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