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