History

Alias PIX image encoder and decoder

Vittorio Giovara committed 12 years ago

Commit e50f5d31 parent 3b5946b11 files · +279 −2Browse files

Unified · Split

11 files
  1. Changelog +1 −0
  2. doc/general.texi +2 −0
  3. libavcodec/Makefile +2 −0
  4. libavcodec/aliaspixdec.c +128 −0
  5. libavcodec/aliaspixenc.c +134 −0
  6. libavcodec/allcodecs.c +1 −0
  7. libavcodec/avcodec.h +1 −0
  8. libavcodec/codec_desc.c +7 −0
  9. libavcodec/version.h +1 −1
  10. libavformat/img2.c +1 −0
  11. libavformat/img2enc.c +1 −1
Changelog+1−0
@@ -5,6 +5,7 @@ version <next>: · 4 unmodified lines
55 - libx265 encoder
66 - shuffleplanes filter
77 - replaygain data export
8+- Alias PIX image encoder and decoder
89
910
1011 version 10:
doc/general.texi+2−0
@@ -424,6 +424,8 @@ following image formats are supported: · 423 unmodified lines
424424 @item Name @tab Encoding @tab Decoding @tab Comments
425425 @item .Y.U.V @tab X @tab X
426426 @tab one raw file per component
427+@item Alias PIX @tab X @tab X
428+ @tab Alias/Wavefront PIX image format
427429 @item animated GIF @tab X @tab X
428430 @tab Only uncompressed GIFs are generated.
429431 @item BMP @tab X @tab X
libavcodec/Makefile+2−0
@@ -90,6 +90,8 @@ OBJS-$(CONFIG_AC3_FIXED_ENCODER) += ac3enc_fixed.o ac3enc.o ac3tab.o ac3.o · 89 unmodified lines
9090 OBJS-$(CONFIG_AIC_DECODER) += aic.o
9191 OBJS-$(CONFIG_ALAC_DECODER) += alac.o alac_data.o
9292 OBJS-$(CONFIG_ALAC_ENCODER) += alacenc.o alac_data.o
93+OBJS-$(CONFIG_ALIAS_PIX_DECODER) += aliaspixdec.o
94+OBJS-$(CONFIG_ALIAS_PIX_ENCODER) += aliaspixenc.o
9395 OBJS-$(CONFIG_ALS_DECODER) += alsdec.o bgmc.o mpeg4audio.o
9496 OBJS-$(CONFIG_AMRNB_DECODER) += amrnbdec.o celp_filters.o \
9597 celp_math.o acelp_filters.o \
libavcodec/aliaspixdec.c+128−0
@@ -0,0 +1,128 @@
1+/*
2+ * Alias PIX image decoder
3+ * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@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 "libavutil/intreadwrite.h"
23+
24+#include "avcodec.h"
25+#include "bytestream.h"
26+#include "internal.h"
27+
28+#define ALIAS_HEADER_SIZE 10
29+
30+static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
31+ AVPacket *avpkt)
32+{
33+ AVFrame *f = data;
34+ GetByteContext gb;
35+ int width, height, ret, bits_pixel, pixel;
36+ uint8_t *out_buf;
37+ uint8_t count;
38+ int x, y;
39+
40+ bytestream2_init(&gb, avpkt->data, avpkt->size);
41+
42+ if (bytestream2_get_bytes_left(&gb) < ALIAS_HEADER_SIZE) {
43+ av_log(avctx, AV_LOG_ERROR, "Header too small %d.\n", avpkt->size);
44+ return AVERROR_INVALIDDATA;
45+ }
46+
47+ width = bytestream2_get_be16u(&gb);
48+ height = bytestream2_get_be16u(&gb);
49+ bytestream2_skipu(&gb, 4); // obsolete X, Y offset
50+ bits_pixel = bytestream2_get_be16u(&gb);
51+
52+ if (bits_pixel == 24)
53+ avctx->pix_fmt = AV_PIX_FMT_BGR24;
54+ else if (bits_pixel == 8)
55+ avctx->pix_fmt = AV_PIX_FMT_GRAY8;
56+ else {
57+ av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
58+ return AVERROR_INVALIDDATA;
59+ }
60+
61+ ret = ff_set_dimensions(avctx, width, height);
62+ if (ret < 0)
63+ return ret;
64+
65+ ret = ff_get_buffer(avctx, f, 0);
66+ if (ret < 0)
67+ return ret;
68+
69+ f->pict_type = AV_PICTURE_TYPE_I;
70+ f->key_frame = 1;
71+
72+ x = 0;
73+ y = 1;
74+ out_buf = f->data[0];
75+ while (bytestream2_get_bytes_left(&gb) > 0) {
76+ int i;
77+
78+ /* set buffer at the right position at every new line */
79+ if (x == avctx->width) {
80+ x = 0;
81+ out_buf = f->data[0] + f->linesize[0] * y++;
82+ if (y > avctx->height) {
83+ av_log(avctx, AV_LOG_ERROR,
84+ "Ended frame decoding with %d bytes left.\n",
85+ bytestream2_get_bytes_left(&gb));
86+ return AVERROR_INVALIDDATA;
87+ }
88+ }
89+
90+ /* read packet and copy data */
91+ count = bytestream2_get_byteu(&gb);
92+ if (!count || x + count > avctx->width) {
93+ av_log(avctx, AV_LOG_ERROR, "Invalid run length %d.\n", count);
94+ return AVERROR_INVALIDDATA;
95+ }
96+
97+ if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
98+ pixel = bytestream2_get_be24(&gb);
99+ for (i = 0; i < count; i++) {
100+ AV_WB24(out_buf, pixel);
101+ out_buf += 3;
102+ }
103+ } else { // AV_PIX_FMT_GRAY8
104+ pixel = bytestream2_get_byte(&gb);
105+ for (i = 0; i < count; i++)
106+ *out_buf++ = pixel;
107+ }
108+
109+ x += i;
110+ }
111+
112+ if (x != width || y != height) {
113+ av_log(avctx, AV_LOG_ERROR, "Picture stopped at %d,%d.\n", x, y);
114+ return AVERROR_INVALIDDATA;
115+ }
116+
117+ *got_frame = 1;
118+ return avpkt->size;
119+}
120+
121+AVCodec ff_alias_pix_decoder = {
122+ .name = "alias_pix",
123+ .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
124+ .type = AVMEDIA_TYPE_VIDEO,
125+ .id = AV_CODEC_ID_ALIAS_PIX,
126+ .decode = decode_frame,
127+ .capabilities = CODEC_CAP_DR1,
128+};
libavcodec/aliaspixenc.c+134−0
@@ -0,0 +1,134 @@
1+/*
2+ * Alias PIX image encoder
3+ * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@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 "libavutil/intreadwrite.h"
23+
24+#include "avcodec.h"
25+#include "bytestream.h"
26+#include "internal.h"
27+
28+#define ALIAS_HEADER_SIZE 10
29+
30+static av_cold int encode_init(AVCodecContext *avctx)
31+{
32+ avctx->coded_frame = av_frame_alloc();
33+ if (!avctx->coded_frame)
34+ return AVERROR(ENOMEM);
35+ return 0;
36+}
37+
38+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
39+ const AVFrame *frame, int *got_packet)
40+{
41+ int width, height, bits_pixel, i, j, length, ret;
42+ uint8_t *in_buf, *buf;
43+
44+ avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
45+ avctx->coded_frame->key_frame = 1;
46+
47+ width = avctx->width;
48+ height = avctx->height;
49+
50+ if (width > 65535 || height > 65535 ||
51+ width * height >= INT_MAX / 4 - ALIAS_HEADER_SIZE) {
52+ av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", width, height);
53+ return AVERROR_INVALIDDATA;
54+ }
55+
56+ switch (avctx->pix_fmt) {
57+ case AV_PIX_FMT_GRAY8:
58+ bits_pixel = 8;
59+ break;
60+ case AV_PIX_FMT_BGR24:
61+ bits_pixel = 24;
62+ break;
63+ default:
64+ return AVERROR(EINVAL);
65+ }
66+
67+ length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
68+ if ((ret = ff_alloc_packet(pkt, length)) < 0) {
69+ av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
70+ return ret;
71+ }
72+
73+ buf = pkt->data;
74+
75+ /* Encode header. */
76+ bytestream_put_be16(&buf, width);
77+ bytestream_put_be16(&buf, height);
78+ bytestream_put_be32(&buf, 0); /* X, Y offset */
79+ bytestream_put_be16(&buf, bits_pixel);
80+
81+ for (j = 0; j < height; j++) {
82+ in_buf = frame->data[0] + frame->linesize[0] * j;
83+ for (i = 0; i < width; ) {
84+ int count = 0;
85+ int pixel;
86+
87+ if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
88+ pixel = *in_buf;
89+ while (count < 255 && count + i < width && pixel == *in_buf) {
90+ count++;
91+ in_buf++;
92+ }
93+ bytestream_put_byte(&buf, count);
94+ bytestream_put_byte(&buf, pixel);
95+ } else { /* AV_PIX_FMT_BGR24 */
96+ pixel = AV_RB24(in_buf);
97+ while (count < 255 && count + i < width &&
98+ pixel == AV_RB24(in_buf)) {
99+ count++;
100+ in_buf += 3;
101+ }
102+ bytestream_put_byte(&buf, count);
103+ bytestream_put_be24(&buf, pixel);
104+ }
105+ i += count;
106+ }
107+ }
108+
109+ /* Total length */
110+ av_shrink_packet(pkt, buf - pkt->data);
111+ pkt->flags |= AV_PKT_FLAG_KEY;
112+ *got_packet = 1;
113+
114+ return 0;
115+}
116+
117+static av_cold int encode_close(AVCodecContext *avctx)
118+{
119+ av_frame_free(&avctx->coded_frame);
120+ return 0;
121+}
122+
123+AVCodec ff_alias_pix_encoder = {
124+ .name = "alias_pix",
125+ .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
126+ .type = AVMEDIA_TYPE_VIDEO,
127+ .id = AV_CODEC_ID_ALIAS_PIX,
128+ .init = encode_init,
129+ .encode2 = encode_frame,
130+ .close = encode_close,
131+ .pix_fmts = (const enum AVPixelFormat[]) {
132+ AV_PIX_FMT_BGR24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
133+ },
134+};
libavcodec/allcodecs.c+1−0
@@ -98,6 +98,7 @@ void avcodec_register_all(void) · 97 unmodified lines
9898 REGISTER_ENCODER(A64MULTI5, a64multi5);
9999 REGISTER_DECODER(AASC, aasc);
100100 REGISTER_DECODER(AIC, aic);
101+ REGISTER_ENCDEC (ALIAS_PIX, alias_pix);
101102 REGISTER_DECODER(AMV, amv);
102103 REGISTER_DECODER(ANM, anm);
103104 REGISTER_DECODER(ANSI, ansi);
libavcodec/avcodec.h+1−0
@@ -284,6 +284,7 @@ enum AVCodecID { · 283 unmodified lines
284284 AV_CODEC_ID_HNM4_VIDEO,
285285 AV_CODEC_ID_HEVC,
286286 AV_CODEC_ID_FIC,
287+ AV_CODEC_ID_ALIAS_PIX,
287288
288289 /* various PCM "codecs" */
289290 AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
libavcodec/codec_desc.c+7−0
@@ -1094,6 +1094,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 1093 unmodified lines
10941094 },
10951095
10961096 /* image codecs */
1097+ {
1098+ .id = AV_CODEC_ID_ALIAS_PIX,
1099+ .type = AVMEDIA_TYPE_VIDEO,
1100+ .name = "alias_pix",
1101+ .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
1102+ .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1103+ },
10971104 {
10981105 .id = AV_CODEC_ID_ANSI,
10991106 .type = AVMEDIA_TYPE_VIDEO,
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
2929 #include "libavutil/version.h"
3030
3131 #define LIBAVCODEC_VERSION_MAJOR 55
32−#define LIBAVCODEC_VERSION_MINOR 36
32+#define LIBAVCODEC_VERSION_MINOR 37
3333 #define LIBAVCODEC_VERSION_MICRO 0
3434
3535 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
libavformat/img2.c+1−0
@@ -43,6 +43,7 @@ static const IdStrMap img_tags[] = { · 42 unmodified lines
4343 { AV_CODEC_ID_PGMYUV, "pgmyuv" },
4444 { AV_CODEC_ID_PBM, "pbm" },
4545 { AV_CODEC_ID_PAM, "pam" },
46+ { AV_CODEC_ID_ALIAS_PIX, "pix" },
4647 { AV_CODEC_ID_MPEG1VIDEO, "mpg1-img" },
4748 { AV_CODEC_ID_MPEG2VIDEO, "mpg2-img" },
4849 { AV_CODEC_ID_MPEG4, "mpg4-img" },
libavformat/img2enc.c+1−1
@@ -148,7 +148,7 @@ AVOutputFormat ff_image2_muxer = { · 147 unmodified lines
148148 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
149149 .extensions = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
150150 "ppm,sgi,tga,tif,tiff,jp2,xwd,sun,ras,rs,im1,im8,im24,"
151− "sunras,webp,xbm,j2c",
151+ "sunras,webp,xbm,j2c,pix",
152152 .priv_data_size = sizeof(VideoMuxData),
153153 .video_codec = AV_CODEC_ID_MJPEG,
154154 .write_header = write_header,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t