10l: wrong operation in stereo rematrixing
[FFMpeg-mirror/lagarith.git] / libavcodec / sgienc.c
blob37bba972e09f2f367c02966c37df3da8b410ac88
1 /*
2 * SGI image encoder
3 * Todd Kirby <doubleshot@pacbell.net>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "sgi.h"
25 #include "rle.h"
27 #define SGI_SINGLE_CHAN 2
28 #define SGI_MULTI_CHAN 3
30 typedef struct SgiContext {
31 AVFrame picture;
32 } SgiContext;
34 static av_cold int encode_init(AVCodecContext *avctx){
35 SgiContext *s = avctx->priv_data;
37 avcodec_get_frame_defaults(&s->picture);
38 avctx->coded_frame = &s->picture;
40 return 0;
43 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
44 int buf_size, void *data) {
45 SgiContext *s = avctx->priv_data;
46 AVFrame * const p = &s->picture;
47 uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf;
48 int x, y, z, length, tablesize;
49 unsigned int width, height, depth, dimension;
50 unsigned char *orig_buf = buf, *end_buf = buf + buf_size;
52 *p = *(AVFrame*)data;
53 p->pict_type = FF_I_TYPE;
54 p->key_frame = 1;
56 width = avctx->width;
57 height = avctx->height;
59 switch (avctx->pix_fmt) {
60 case PIX_FMT_GRAY8:
61 dimension = SGI_SINGLE_CHAN;
62 depth = SGI_GRAYSCALE;
63 break;
64 case PIX_FMT_RGB24:
65 dimension = SGI_MULTI_CHAN;
66 depth = SGI_RGB;
67 break;
68 case PIX_FMT_RGBA:
69 dimension = SGI_MULTI_CHAN;
70 depth = SGI_RGBA;
71 break;
72 default:
73 return AVERROR_INVALIDDATA;
76 tablesize = depth * height * 4;
77 length = tablesize * 2 + SGI_HEADER_SIZE;
79 if (buf_size < length) {
80 av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size);
81 return -1;
84 /* Encode header. */
85 bytestream_put_be16(&buf, SGI_MAGIC);
86 bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
87 bytestream_put_byte(&buf, 1); /* bytes_per_channel */
88 bytestream_put_be16(&buf, dimension);
89 bytestream_put_be16(&buf, width);
90 bytestream_put_be16(&buf, height);
91 bytestream_put_be16(&buf, depth);
93 /* The rest are constant in this implementation. */
94 bytestream_put_be32(&buf, 0L); /* pixmin */
95 bytestream_put_be32(&buf, 255L); /* pixmax */
96 bytestream_put_be32(&buf, 0L); /* dummy */
98 /* name */
99 memset(buf, 0, SGI_HEADER_SIZE);
100 buf += 80;
102 /* colormap */
103 bytestream_put_be32(&buf, 0L);
105 /* The rest of the 512 byte header is unused. */
106 buf += 404;
107 offsettab = buf;
109 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
110 /* Skip RLE offset table. */
111 buf += tablesize;
112 lengthtab = buf;
114 /* Skip RLE length table. */
115 buf += tablesize;
117 /* Make an intermediate consecutive buffer. */
118 if ((encode_buf = av_malloc(width)) == NULL)
119 return -1;
121 for (z = 0; z < depth; z++) {
122 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
124 for (y = 0; y < height; y++) {
125 bytestream_put_be32(&offsettab, buf - orig_buf);
127 for (x = 0; x < width; x++)
128 encode_buf[x] = in_buf[depth * x];
130 if((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
131 av_free(encode_buf);
132 return -1;
135 buf += length;
136 bytestream_put_byte(&buf, 0);
137 bytestream_put_be32(&lengthtab, length + 1);
138 in_buf -= p->linesize[0];
142 av_free(encode_buf);
143 } else {
144 for (z = 0; z < depth; z++) {
145 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
147 for (y = 0; y < height; y++) {
148 for (x = 0; x < width * depth; x += depth)
149 bytestream_put_byte(&buf, in_buf[x]);
151 in_buf -= p->linesize[0];
156 /* total length */
157 return buf - orig_buf;
160 AVCodec sgi_encoder = {
161 "sgi",
162 CODEC_TYPE_VIDEO,
163 CODEC_ID_SGI,
164 sizeof(SgiContext),
165 encode_init,
166 encode_frame,
167 NULL,
168 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_GRAY8, PIX_FMT_NONE},
169 .long_name= NULL_IF_CONFIG_SMALL("SGI image"),