]> git.sesse.net Git - ffmpeg/blob - libavcodec/sgienc.c
sgienc: Use a local RLE encoding function
[ffmpeg] / libavcodec / sgienc.c
1 /*
2  * SGI image encoder
3  * Todd Kirby <doubleshot@pacbell.net>
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 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "internal.h"
25 #include "sgi.h"
26 #include "rle.h"
27
28 #define SGI_SINGLE_CHAN 2
29 #define SGI_MULTI_CHAN 3
30
31 static av_cold int encode_init(AVCodecContext *avctx)
32 {
33     if (avctx->width > 65535 || avctx->height > 65535) {
34         av_log(avctx, AV_LOG_ERROR,
35                "Unsupported resolution %dx%d.\n", avctx->width, avctx->height);
36         return AVERROR_INVALIDDATA;
37     }
38
39     return 0;
40 }
41
42 static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
43                           int w, int bpp)
44 {
45     int val, count, x, start = bytestream2_tell_p(pbc);
46     void (*bytestream2_put)(PutByteContext *, unsigned int);
47
48     bytestream2_put = bytestream2_put_byte;
49
50     for (x = 0; x < w; x += count) {
51         /* see if we can encode the next set of pixels with RLE */
52         count = ff_rle_count_pixels(src, w - x, bpp, 1);
53         if (count > 1) {
54             if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
55                 return AVERROR_INVALIDDATA;
56
57             val = *src;
58             bytestream2_put(pbc, count);
59             bytestream2_put(pbc, val);
60         } else {
61             int i;
62             /* fall back on uncompressed */
63             count = ff_rle_count_pixels(src, w - x, bpp, 0);
64             if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
65                 return AVERROR_INVALIDDATA;
66
67             bytestream2_put(pbc, count + 0x80);
68             for (i = 0; i < count; i++) {
69                 val = src[i];
70                 bytestream2_put(pbc, val);
71             }
72         }
73
74         src += count * bpp;
75     }
76
77     return bytestream2_tell_p(pbc) - start;
78 }
79
80 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
81                         const AVFrame *frame, int *got_packet)
82 {
83     const AVFrame * const p = frame;
84     PutByteContext pbc;
85     uint8_t *in_buf, *encode_buf;
86     int x, y, z, length, tablesize, ret;
87     unsigned int width, height, depth, dimension;
88     unsigned int bytes_per_channel, pixmax, put_be;
89
90 #if FF_API_CODED_FRAME
91 FF_DISABLE_DEPRECATION_WARNINGS
92     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
93     avctx->coded_frame->key_frame = 1;
94 FF_ENABLE_DEPRECATION_WARNINGS
95 #endif
96
97     width  = avctx->width;
98     height = avctx->height;
99     bytes_per_channel = 1;
100     pixmax = 0xFF;
101     put_be = HAVE_BIGENDIAN;
102
103     switch (avctx->pix_fmt) {
104     case AV_PIX_FMT_GRAY8:
105         dimension = SGI_SINGLE_CHAN;
106         depth     = SGI_GRAYSCALE;
107         break;
108     case AV_PIX_FMT_RGB24:
109         dimension = SGI_MULTI_CHAN;
110         depth     = SGI_RGB;
111         break;
112     case AV_PIX_FMT_RGBA:
113         dimension = SGI_MULTI_CHAN;
114         depth     = SGI_RGBA;
115         break;
116     case AV_PIX_FMT_GRAY16LE:
117         put_be = !HAVE_BIGENDIAN;
118     case AV_PIX_FMT_GRAY16BE:
119         avctx->coder_type = FF_CODER_TYPE_RAW;
120         bytes_per_channel = 2;
121         pixmax = 0xFFFF;
122         dimension = SGI_SINGLE_CHAN;
123         depth     = SGI_GRAYSCALE;
124         break;
125     case AV_PIX_FMT_RGB48LE:
126         put_be = !HAVE_BIGENDIAN;
127     case AV_PIX_FMT_RGB48BE:
128         avctx->coder_type = FF_CODER_TYPE_RAW;
129         bytes_per_channel = 2;
130         pixmax = 0xFFFF;
131         dimension = SGI_MULTI_CHAN;
132         depth     = SGI_RGB;
133         break;
134     case AV_PIX_FMT_RGBA64LE:
135         put_be = !HAVE_BIGENDIAN;
136     case AV_PIX_FMT_RGBA64BE:
137         avctx->coder_type = FF_CODER_TYPE_RAW;
138         bytes_per_channel = 2;
139         pixmax = 0xFFFF;
140         dimension = SGI_MULTI_CHAN;
141         depth     = SGI_RGBA;
142         break;
143     default:
144         return AVERROR_INVALIDDATA;
145     }
146
147     tablesize = depth * height * 4;
148     length = SGI_HEADER_SIZE;
149     if (avctx->coder_type == FF_CODER_TYPE_RAW)
150         length += depth * height * width;
151     else // assume sgi_rle_encode() produces at most 2x size of input
152         length += tablesize * 2 + depth * height * (2 * width + 1);
153
154     if ((ret = ff_alloc_packet(pkt, bytes_per_channel * length)) < 0) {
155         av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
156         return ret;
157     }
158
159     bytestream2_init_writer(&pbc, pkt->data, pkt->size);
160
161     /* Encode header. */
162     bytestream2_put_be16(&pbc, SGI_MAGIC);
163     bytestream2_put_byte(&pbc, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0 */
164     bytestream2_put_byte(&pbc, bytes_per_channel);
165     bytestream2_put_be16(&pbc, dimension);
166     bytestream2_put_be16(&pbc, width);
167     bytestream2_put_be16(&pbc, height);
168     bytestream2_put_be16(&pbc, depth);
169
170     bytestream2_put_be32(&pbc, 0L); /* pixmin */
171     bytestream2_put_be32(&pbc, pixmax);
172     bytestream2_put_be32(&pbc, 0L); /* dummy */
173
174     /* name */
175     bytestream2_skip_p(&pbc, 80);
176
177     /* colormap */
178     bytestream2_put_be32(&pbc, 0L);
179
180     /* The rest of the 512 byte header is unused. */
181     bytestream2_skip_p(&pbc, 404);
182
183     if (avctx->coder_type != FF_CODER_TYPE_RAW) {
184         PutByteContext taboff_pcb, tablen_pcb;
185
186         /* Skip RLE offset table. */
187         bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
188         bytestream2_skip_p(&pbc, tablesize);
189
190         /* Skip RLE length table. */
191         bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
192         bytestream2_skip_p(&pbc, tablesize);
193
194         /* Make an intermediate consecutive buffer. */
195         if (!(encode_buf = av_malloc(width)))
196             return -1;
197
198         for (z = 0; z < depth; z++) {
199             in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
200
201             for (y = 0; y < height; y++) {
202                 bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
203
204                 for (x = 0; x < width; x++)
205                     encode_buf[x] = in_buf[depth * x];
206
207                 length = sgi_rle_encode(&pbc, encode_buf, width, 1);
208                 if (length < 1) {
209                     av_free(encode_buf);
210                     return -1;
211                 }
212
213                 bytestream2_put_be32(&tablen_pcb, length);
214                 in_buf -= p->linesize[0];
215             }
216         }
217
218         av_free(encode_buf);
219     } else {
220         for (z = 0; z < depth; z++) {
221             in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
222
223             for (y = 0; y < height; y++) {
224                 for (x = 0; x < width * depth; x += depth)
225                     if (bytes_per_channel == 1)
226                         bytestream2_put_byte(&pbc, in_buf[x]);
227                     else
228                         if (put_be)
229                             bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
230                         else
231                             bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
232
233                 in_buf -= p->linesize[0];
234             }
235         }
236     }
237
238     /* total length */
239     pkt->size   = bytestream2_tell_p(&pbc);
240     pkt->flags |= AV_PKT_FLAG_KEY;
241     *got_packet = 1;
242
243     return 0;
244 }
245
246 AVCodec ff_sgi_encoder = {
247     .name      = "sgi",
248     .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
249     .type      = AVMEDIA_TYPE_VIDEO,
250     .id        = AV_CODEC_ID_SGI,
251     .init      = encode_init,
252     .encode2   = encode_frame,
253     .pix_fmts  = (const enum AVPixelFormat[]) {
254         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
255         AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
256         AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
257         AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,
258         AV_PIX_FMT_NONE
259     },
260 };