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