]> git.sesse.net Git - ffmpeg/blob - libavcodec/vcr1.c
Merge commit 'd2a25c4032ce6ceabb0f51b5c1e6ca865395a793'
[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 "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     avcodec_get_frame_defaults(&a->picture);
44
45     return 0;
46 }
47
48 static av_cold int vcr1_decode_init(AVCodecContext *avctx)
49 {
50     vcr1_common_init(avctx);
51
52     avctx->pix_fmt = AV_PIX_FMT_YUV410P;
53
54     if (avctx->width % 8 || avctx->height%4) {
55         av_log_ask_for_sample(avctx, "odd dimensions are not supported\n");
56         return AVERROR_PATCHWELCOME;
57     }
58     return 0;
59 }
60
61 static av_cold int vcr1_decode_end(AVCodecContext *avctx)
62 {
63     VCR1Context *s = avctx->priv_data;
64
65     if (s->picture.data[0])
66         avctx->release_buffer(avctx, &s->picture);
67
68     return 0;
69 }
70
71 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
72                              int *got_frame, AVPacket *avpkt)
73 {
74     const uint8_t *buf        = avpkt->data;
75     int buf_size              = avpkt->size;
76     VCR1Context *const a      = avctx->priv_data;
77     AVFrame *picture          = data;
78     AVFrame *const p          = &a->picture;
79     const uint8_t *bytestream = buf;
80     int i, x, y, ret;
81
82     if (p->data[0])
83         avctx->release_buffer(avctx, p);
84
85     if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
86         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
87         return AVERROR(EINVAL);
88     }
89
90     p->reference = 0;
91     if ((ret = ff_get_buffer(avctx, p)) < 0) {
92         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
93         return ret;
94     }
95     p->pict_type = AV_PICTURE_TYPE_I;
96     p->key_frame = 1;
97
98     for (i = 0; i < 16; i++) {
99         a->delta[i] = *bytestream++;
100         bytestream++;
101     }
102
103     for (y = 0; y < avctx->height; y++) {
104         int offset;
105         uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
106
107         if ((y & 3) == 0) {
108             uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
109             uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
110
111             for (i = 0; i < 4; i++)
112                 a->offset[i] = *bytestream++;
113
114             offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
115             for (x = 0; x < avctx->width; x += 4) {
116                 luma[0]     = offset += a->delta[bytestream[2] & 0xF];
117                 luma[1]     = offset += a->delta[bytestream[2] >>  4];
118                 luma[2]     = offset += a->delta[bytestream[0] & 0xF];
119                 luma[3]     = offset += a->delta[bytestream[0] >>  4];
120                 luma       += 4;
121
122                 *cb++       = bytestream[3];
123                 *cr++       = bytestream[1];
124
125                 bytestream += 4;
126             }
127         } else {
128             offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
129
130             for (x = 0; x < avctx->width; x += 8) {
131                 luma[0]     = offset += a->delta[bytestream[2] & 0xF];
132                 luma[1]     = offset += a->delta[bytestream[2] >>  4];
133                 luma[2]     = offset += a->delta[bytestream[3] & 0xF];
134                 luma[3]     = offset += a->delta[bytestream[3] >>  4];
135                 luma[4]     = offset += a->delta[bytestream[0] & 0xF];
136                 luma[5]     = offset += a->delta[bytestream[0] >>  4];
137                 luma[6]     = offset += a->delta[bytestream[1] & 0xF];
138                 luma[7]     = offset += a->delta[bytestream[1] >>  4];
139                 luma       += 8;
140                 bytestream += 4;
141             }
142         }
143     }
144
145     *picture   = a->picture;
146     *got_frame = 1;
147
148     return buf_size;
149 }
150
151 AVCodec ff_vcr1_decoder = {
152     .name           = "vcr1",
153     .type           = AVMEDIA_TYPE_VIDEO,
154     .id             = AV_CODEC_ID_VCR1,
155     .priv_data_size = sizeof(VCR1Context),
156     .init           = vcr1_decode_init,
157     .close          = vcr1_decode_end,
158     .decode         = vcr1_decode_frame,
159     .capabilities   = CODEC_CAP_DR1,
160     .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
161 };