]> git.sesse.net Git - ffmpeg/blob - libavcodec/sgienc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / sgienc.c
1 /*
2  * SGI image encoder
3  * Todd Kirby <doubleshot@pacbell.net>
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 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "sgi.h"
25 #include "rle.h"
26
27 #define SGI_SINGLE_CHAN 2
28 #define SGI_MULTI_CHAN 3
29
30 typedef struct SgiContext {
31     AVFrame picture;
32 } SgiContext;
33
34 static av_cold int encode_init(AVCodecContext *avctx)
35 {
36     SgiContext *s = avctx->priv_data;
37
38     avcodec_get_frame_defaults(&s->picture);
39     avctx->coded_frame = &s->picture;
40
41     return 0;
42 }
43
44 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
45                         int buf_size, void *data)
46 {
47     SgiContext *s = avctx->priv_data;
48     AVFrame * const p = &s->picture;
49     uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf;
50     int x, y, z, length, tablesize;
51     unsigned int width, height, depth, dimension, bytes_per_channel, pixmax, put_be;
52     unsigned char *orig_buf = buf, *end_buf = buf + buf_size;
53
54     *p = *(AVFrame*)data;
55     p->pict_type = AV_PICTURE_TYPE_I;
56     p->key_frame = 1;
57
58     width  = avctx->width;
59     height = avctx->height;
60     bytes_per_channel = 1;
61     pixmax = 0xFF;
62     put_be = HAVE_BIGENDIAN;
63
64     switch (avctx->pix_fmt) {
65     case PIX_FMT_GRAY8:
66         dimension = SGI_SINGLE_CHAN;
67         depth     = SGI_GRAYSCALE;
68         break;
69     case PIX_FMT_RGB24:
70         dimension = SGI_MULTI_CHAN;
71         depth     = SGI_RGB;
72         break;
73     case PIX_FMT_RGBA:
74         dimension = SGI_MULTI_CHAN;
75         depth     = SGI_RGBA;
76         break;
77     case PIX_FMT_GRAY16LE:
78         put_be = !HAVE_BIGENDIAN;
79     case PIX_FMT_GRAY16BE:
80         avctx->coder_type = FF_CODER_TYPE_RAW;
81         bytes_per_channel = 2;
82         pixmax = 0xFFFF;
83         dimension = SGI_SINGLE_CHAN;
84         depth     = SGI_GRAYSCALE;
85         break;
86     case PIX_FMT_RGB48LE:
87         put_be = !HAVE_BIGENDIAN;
88     case PIX_FMT_RGB48BE:
89         avctx->coder_type = FF_CODER_TYPE_RAW;
90         bytes_per_channel = 2;
91         pixmax = 0xFFFF;
92         dimension = SGI_MULTI_CHAN;
93         depth     = SGI_RGB;
94         break;
95     case PIX_FMT_RGBA64LE:
96         put_be = !HAVE_BIGENDIAN;
97     case PIX_FMT_RGBA64BE:
98         avctx->coder_type = FF_CODER_TYPE_RAW;
99         bytes_per_channel = 2;
100         pixmax = 0xFFFF;
101         dimension = SGI_MULTI_CHAN;
102         depth     = SGI_RGBA;
103         break;
104     default:
105         return AVERROR_INVALIDDATA;
106     }
107
108     tablesize = depth * height * 4;
109     length = tablesize * 2 + SGI_HEADER_SIZE;
110
111     if (buf_size < length) {
112         av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size);
113         return -1;
114     }
115
116     /* Encode header. */
117     bytestream_put_be16(&buf, SGI_MAGIC);
118     bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
119     bytestream_put_byte(&buf, bytes_per_channel);
120     bytestream_put_be16(&buf, dimension);
121     bytestream_put_be16(&buf, width);
122     bytestream_put_be16(&buf, height);
123     bytestream_put_be16(&buf, depth);
124
125     bytestream_put_be32(&buf, 0L); /* pixmin */
126     bytestream_put_be32(&buf, pixmax);
127     bytestream_put_be32(&buf, 0L); /* dummy */
128
129     /* name */
130     memset(buf, 0, SGI_HEADER_SIZE);
131     buf += 80;
132
133      /* colormap */
134     bytestream_put_be32(&buf, 0L);
135
136     /* The rest of the 512 byte header is unused. */
137     buf += 404;
138     offsettab = buf;
139
140     if (avctx->coder_type  != FF_CODER_TYPE_RAW) {
141         /* Skip RLE offset table. */
142         buf += tablesize;
143         lengthtab = buf;
144
145         /* Skip RLE length table. */
146         buf += tablesize;
147
148         /* Make an intermediate consecutive buffer. */
149         if (!(encode_buf = av_malloc(width)))
150             return -1;
151
152         for (z = 0; z < depth; z++) {
153             in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
154
155             for (y = 0; y < height; y++) {
156                 bytestream_put_be32(&offsettab, buf - orig_buf);
157
158                 for (x = 0; x < width; x++)
159                     encode_buf[x] = in_buf[depth * x];
160
161                 if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
162                     av_free(encode_buf);
163                     return -1;
164                 }
165
166                 buf += length;
167                 bytestream_put_byte(&buf, 0);
168                 bytestream_put_be32(&lengthtab, length + 1);
169                 in_buf -= p->linesize[0];
170             }
171         }
172
173         av_free(encode_buf);
174     } else {
175         for (z = 0; z < depth; z++) {
176             in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
177
178             for (y = 0; y < height; y++) {
179                 for (x = 0; x < width * depth; x += depth)
180                     if (bytes_per_channel == 1) {
181                     bytestream_put_byte(&buf, in_buf[x]);
182                     } else {
183                         if (put_be) {
184                             bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
185                         } else {
186                             bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
187                         }
188                     }
189
190                 in_buf -= p->linesize[0];
191             }
192         }
193     }
194
195     /* total length */
196     return buf - orig_buf;
197 }
198
199 AVCodec ff_sgi_encoder = {
200     .name           = "sgi",
201     .type           = AVMEDIA_TYPE_VIDEO,
202     .id             = CODEC_ID_SGI,
203     .priv_data_size = sizeof(SgiContext),
204     .init           = encode_init,
205     .encode         = encode_frame,
206     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA,
207                                           PIX_FMT_RGB48LE, PIX_FMT_RGB48BE,
208                                           PIX_FMT_RGBA64LE, PIX_FMT_RGBA64BE,
209                                           PIX_FMT_GRAY16LE, PIX_FMT_GRAY16BE,
210                                           PIX_FMT_GRAY8, PIX_FMT_NONE},
211     .long_name= NULL_IF_CONFIG_SMALL("SGI image"),
212 };