]> git.sesse.net Git - ffmpeg/blob - libavcodec/vcr1.c
Merge commit '79922d7237aba2b8c6abbd2e06a0c08e4f498ad4'
[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     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     if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
81         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
82         return AVERROR(EINVAL);
83     }
84
85     p->reference = 0;
86     if (avctx->get_buffer(avctx, p) < 0) {
87         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
88         return -1;
89     }
90     p->pict_type = AV_PICTURE_TYPE_I;
91     p->key_frame = 1;
92
93     for (i = 0; i < 16; i++) {
94         a->delta[i] = *bytestream++;
95         bytestream++;
96     }
97
98     for (y = 0; y < avctx->height; y++) {
99         int offset;
100         uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
101
102         if ((y & 3) == 0) {
103             uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
104             uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
105
106             for (i = 0; i < 4; i++)
107                 a->offset[i] = *bytestream++;
108
109             offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
110             for (x = 0; x < avctx->width; x += 4) {
111                 luma[0]     = offset += a->delta[bytestream[2] & 0xF];
112                 luma[1]     = offset += a->delta[bytestream[2] >>  4];
113                 luma[2]     = offset += a->delta[bytestream[0] & 0xF];
114                 luma[3]     = offset += a->delta[bytestream[0] >>  4];
115                 luma       += 4;
116
117                 *cb++       = bytestream[3];
118                 *cr++       = bytestream[1];
119
120                 bytestream += 4;
121             }
122         } else {
123             offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
124
125             for (x = 0; x < avctx->width; x += 8) {
126                 luma[0]     = offset += a->delta[bytestream[2] & 0xF];
127                 luma[1]     = offset += a->delta[bytestream[2] >>  4];
128                 luma[2]     = offset += a->delta[bytestream[3] & 0xF];
129                 luma[3]     = offset += a->delta[bytestream[3] >>  4];
130                 luma[4]     = offset += a->delta[bytestream[0] & 0xF];
131                 luma[5]     = offset += a->delta[bytestream[0] >>  4];
132                 luma[6]     = offset += a->delta[bytestream[1] & 0xF];
133                 luma[7]     = offset += a->delta[bytestream[1] >>  4];
134                 luma       += 8;
135                 bytestream += 4;
136             }
137         }
138     }
139
140     *picture   = a->picture;
141     *data_size = sizeof(AVPicture);
142
143     return buf_size;
144 }
145
146 AVCodec ff_vcr1_decoder = {
147     .name           = "vcr1",
148     .type           = AVMEDIA_TYPE_VIDEO,
149     .id             = AV_CODEC_ID_VCR1,
150     .priv_data_size = sizeof(VCR1Context),
151     .init           = vcr1_decode_init,
152     .close          = vcr1_decode_end,
153     .decode         = vcr1_decode_frame,
154     .capabilities   = CODEC_CAP_DR1,
155     .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
156 };
157
158 /* Disable the encoder. */
159 #undef CONFIG_VCR1_ENCODER
160 #define CONFIG_VCR1_ENCODER 0
161
162 #if CONFIG_VCR1_ENCODER
163
164 #include "put_bits.h"
165
166 static int vcr1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
167                              int buf_size, void *data)
168 {
169     VCR1Context *const a = avctx->priv_data;
170     AVFrame *pict        = data;
171     AVFrame *const p     = &a->picture;
172     int size;
173
174     *p           = *pict;
175     p->pict_type = AV_PICTURE_TYPE_I;
176     p->key_frame = 1;
177
178     avpriv_align_put_bits(&a->pb);
179     flush_put_bits(&a->pb);
180
181     size = put_bits_count(&a->pb) / 32;
182
183     return size * 4;
184 }
185
186 AVCodec ff_vcr1_encoder = {
187     .name           = "vcr1",
188     .type           = AVMEDIA_TYPE_VIDEO,
189     .id             = AV_CODEC_ID_VCR1,
190     .priv_data_size = sizeof(VCR1Context),
191     .init           = vcr1_common_init,
192     .encode         = vcr1_encode_frame,
193     .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
194 };
195 #endif /* CONFIG_VCR1_ENCODER */