FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sunrastenc.c
Go to the documentation of this file.
1 /*
2  * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
3  * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
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 "sunrast.h"
26 
27 typedef struct SUNRASTContext {
30  int depth; ///< depth of pixel
31  int length; ///< length (bytes) of image
32  int type; ///< type of file
33  int maptype; ///< type of colormap
34  int maplength; ///< length (bytes) of colormap
35  int size;
37 
39 {
40  SUNRASTContext *s = avctx->priv_data;
41 
42  bytestream2_put_be32u(&s->p, RAS_MAGIC);
43  bytestream2_put_be32u(&s->p, avctx->width);
44  bytestream2_put_be32u(&s->p, avctx->height);
45  bytestream2_put_be32u(&s->p, s->depth);
46  bytestream2_put_be32u(&s->p, s->length);
47  bytestream2_put_be32u(&s->p, s->type);
48  bytestream2_put_be32u(&s->p, s->maptype);
49  bytestream2_put_be32u(&s->p, s->maplength);
50 }
51 
53  const uint8_t *pixels,
54  const uint32_t *palette_data,
55  int linesize)
56 {
57  SUNRASTContext *s = avctx->priv_data;
58  const uint8_t *ptr;
59  int len, alen, x, y;
60 
61  if (s->maplength) { // palettized
62  PutByteContext pb_r, pb_g;
63  int len = s->maplength / 3;
64 
65  pb_r = s->p;
66  bytestream2_skip_p(&s->p, len);
67  pb_g = s->p;
68  bytestream2_skip_p(&s->p, len);
69 
70  for (x = 0; x < len; x++) {
71  uint32_t pixel = palette_data[x];
72 
73  bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
74  bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
75  bytestream2_put_byteu(&s->p, pixel & 0xFF);
76  }
77  }
78 
79  len = (s->depth * avctx->width + 7) >> 3;
80  alen = len + (len & 1);
81  ptr = pixels;
82 
83  if (s->type == RT_BYTE_ENCODED) {
84  uint8_t value, value2;
85  int run;
86 
87  ptr = pixels;
88 
89 #define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
90 
91  x = 0, y = 0;
92  value2 = GET_VALUE;
93  while (y < avctx->height) {
94  run = 1;
95  value = value2;
96  x++;
97  if (x >= alen) {
98  x = 0;
99  ptr += linesize, y++;
100  }
101 
102  value2 = GET_VALUE;
103  while (value2 == value && run < 256 && y < avctx->height) {
104  x++;
105  run++;
106  if (x >= alen) {
107  x = 0;
108  ptr += linesize, y++;
109  }
110  value2 = GET_VALUE;
111  }
112 
113  if (run > 2 || value == RLE_TRIGGER) {
114  bytestream2_put_byteu(&s->p, RLE_TRIGGER);
115  bytestream2_put_byteu(&s->p, run - 1);
116  if (run > 1)
117  bytestream2_put_byteu(&s->p, value);
118  } else if (run == 1) {
119  bytestream2_put_byteu(&s->p, value);
120  } else
121  bytestream2_put_be16u(&s->p, (value << 8) | value);
122  }
123 
124  // update data length for header
125  s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
126  } else {
127  for (y = 0; y < avctx->height; y++) {
128  bytestream2_put_buffer(&s->p, ptr, len);
129  if (len < alen)
130  bytestream2_put_byteu(&s->p, 0);
131  ptr += linesize;
132  }
133  }
134 }
135 
137 {
138  SUNRASTContext *s = avctx->priv_data;
139 
140  switch (avctx->coder_type) {
141  case FF_CODER_TYPE_RLE:
142  s->type = RT_BYTE_ENCODED;
143  break;
144  case FF_CODER_TYPE_RAW:
145  s->type = RT_STANDARD;
146  break;
147  default:
148  av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
149  return AVERROR(EINVAL);
150  }
151 
152  avctx->coded_frame = &s->picture;
153  avctx->coded_frame->key_frame = 1;
155  s->maptype = RMT_NONE;
156  s->maplength = 0;
157 
158  switch (avctx->pix_fmt) {
160  s->depth = 1;
161  break;
162  case AV_PIX_FMT_PAL8 :
163  s->maptype = RMT_EQUAL_RGB;
164  s->maplength = 3 * 256;
165  case AV_PIX_FMT_GRAY8:
166  s->depth = 8;
167  break;
168  case AV_PIX_FMT_BGR24:
169  s->depth = 24;
170  break;
171  default:
172  return AVERROR_BUG;
173  }
174  s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
175  s->size = 32 + s->maplength +
176  s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
177 
178  return 0;
179 }
180 
181 static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
182  const AVFrame *frame, int *got_packet_ptr)
183 {
184  SUNRASTContext *s = avctx->priv_data;
185  int ret;
186 
187  if ((ret = ff_alloc_packet2(avctx, avpkt, s->size)) < 0)
188  return ret;
189 
190  bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
192  sunrast_image_write_image(avctx, frame->data[0],
193  (const uint32_t *)frame->data[1],
194  frame->linesize[0]);
195  // update data length in header after RLE
196  if (s->type == RT_BYTE_ENCODED)
197  AV_WB32(&avpkt->data[16], s->length);
198 
199  *got_packet_ptr = 1;
200  avpkt->flags |= AV_PKT_FLAG_KEY;
201  avpkt->size = bytestream2_tell_p(&s->p);
202  return 0;
203 }
204 
206  { "coder", "rle" },
207  { NULL },
208 };
209 
211  .name = "sunrast",
212  .type = AVMEDIA_TYPE_VIDEO,
213  .id = AV_CODEC_ID_SUNRAST,
214  .priv_data_size = sizeof(SUNRASTContext),
216  .encode2 = sunrast_encode_frame,
217  .defaults = sunrast_defaults,
218  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
222  AV_PIX_FMT_NONE },
223  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
224 };