Browse code

Alias PIX image encoder and decoder

Vittorio Giovara authored on 2014/03/20 09:15:25
Showing 11 changed files
... ...
@@ -5,6 +5,7 @@ version <next>:
5 5
 - libx265 encoder
6 6
 - shuffleplanes filter
7 7
 - replaygain data export
8
+- Alias PIX image encoder and decoder
8 9
 
9 10
 
10 11
 version 10:
... ...
@@ -424,6 +424,8 @@ following image formats are supported:
424 424
 @item Name @tab Encoding @tab Decoding @tab Comments
425 425
 @item .Y.U.V       @tab X @tab X
426 426
     @tab one raw file per component
427
+@item Alias PIX    @tab X @tab X
428
+    @tab Alias/Wavefront PIX image format
427 429
 @item animated GIF @tab X @tab X
428 430
     @tab Only uncompressed GIFs are generated.
429 431
 @item BMP          @tab X @tab X
... ...
@@ -90,6 +90,8 @@ OBJS-$(CONFIG_AC3_FIXED_ENCODER)       += ac3enc_fixed.o ac3enc.o ac3tab.o ac3.o
90 90
 OBJS-$(CONFIG_AIC_DECODER)             += aic.o
91 91
 OBJS-$(CONFIG_ALAC_DECODER)            += alac.o alac_data.o
92 92
 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
93 95
 OBJS-$(CONFIG_ALS_DECODER)             += alsdec.o bgmc.o mpeg4audio.o
94 96
 OBJS-$(CONFIG_AMRNB_DECODER)           += amrnbdec.o celp_filters.o   \
95 97
                                           celp_math.o acelp_filters.o \
96 98
new file mode 100644
... ...
@@ -0,0 +1,128 @@
0
+/*
1
+ * Alias PIX image decoder
2
+ * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/intreadwrite.h"
22
+
23
+#include "avcodec.h"
24
+#include "bytestream.h"
25
+#include "internal.h"
26
+
27
+#define ALIAS_HEADER_SIZE 10
28
+
29
+static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
30
+                        AVPacket *avpkt)
31
+{
32
+    AVFrame *f = data;
33
+    GetByteContext gb;
34
+    int width, height, ret, bits_pixel, pixel;
35
+    uint8_t *out_buf;
36
+    uint8_t count;
37
+    int x, y;
38
+
39
+    bytestream2_init(&gb, avpkt->data, avpkt->size);
40
+
41
+    if (bytestream2_get_bytes_left(&gb) < ALIAS_HEADER_SIZE) {
42
+        av_log(avctx, AV_LOG_ERROR, "Header too small %d.\n", avpkt->size);
43
+        return AVERROR_INVALIDDATA;
44
+    }
45
+
46
+    width  = bytestream2_get_be16u(&gb);
47
+    height = bytestream2_get_be16u(&gb);
48
+    bytestream2_skipu(&gb, 4); // obsolete X, Y offset
49
+    bits_pixel = bytestream2_get_be16u(&gb);
50
+
51
+    if (bits_pixel == 24)
52
+        avctx->pix_fmt = AV_PIX_FMT_BGR24;
53
+    else if (bits_pixel == 8)
54
+        avctx->pix_fmt = AV_PIX_FMT_GRAY8;
55
+    else {
56
+        av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
57
+        return AVERROR_INVALIDDATA;
58
+    }
59
+
60
+    ret = ff_set_dimensions(avctx, width, height);
61
+    if (ret < 0)
62
+        return ret;
63
+
64
+    ret = ff_get_buffer(avctx, f, 0);
65
+    if (ret < 0)
66
+        return ret;
67
+
68
+    f->pict_type = AV_PICTURE_TYPE_I;
69
+    f->key_frame = 1;
70
+
71
+    x = 0;
72
+    y = 1;
73
+    out_buf = f->data[0];
74
+    while (bytestream2_get_bytes_left(&gb) > 0) {
75
+        int i;
76
+
77
+        /* set buffer at the right position at every new line */
78
+        if (x == avctx->width) {
79
+            x = 0;
80
+            out_buf = f->data[0] + f->linesize[0] * y++;
81
+            if (y > avctx->height) {
82
+                av_log(avctx, AV_LOG_ERROR,
83
+                       "Ended frame decoding with %d bytes left.\n",
84
+                       bytestream2_get_bytes_left(&gb));
85
+                return AVERROR_INVALIDDATA;
86
+            }
87
+        }
88
+
89
+        /* read packet and copy data */
90
+        count = bytestream2_get_byteu(&gb);
91
+        if (!count || x + count > avctx->width) {
92
+            av_log(avctx, AV_LOG_ERROR, "Invalid run length %d.\n", count);
93
+            return AVERROR_INVALIDDATA;
94
+        }
95
+
96
+        if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
97
+            pixel = bytestream2_get_be24(&gb);
98
+            for (i = 0; i < count; i++) {
99
+                AV_WB24(out_buf, pixel);
100
+                out_buf += 3;
101
+            }
102
+        } else { // AV_PIX_FMT_GRAY8
103
+            pixel = bytestream2_get_byte(&gb);
104
+            for (i = 0; i < count; i++)
105
+                *out_buf++ = pixel;
106
+        }
107
+
108
+        x += i;
109
+    }
110
+
111
+    if (x != width || y != height) {
112
+        av_log(avctx, AV_LOG_ERROR, "Picture stopped at %d,%d.\n", x, y);
113
+        return AVERROR_INVALIDDATA;
114
+    }
115
+
116
+    *got_frame = 1;
117
+    return avpkt->size;
118
+}
119
+
120
+AVCodec ff_alias_pix_decoder = {
121
+    .name         = "alias_pix",
122
+    .long_name    = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
123
+    .type         = AVMEDIA_TYPE_VIDEO,
124
+    .id           = AV_CODEC_ID_ALIAS_PIX,
125
+    .decode       = decode_frame,
126
+    .capabilities = CODEC_CAP_DR1,
127
+};
0 128
new file mode 100644
... ...
@@ -0,0 +1,134 @@
0
+/*
1
+ * Alias PIX image encoder
2
+ * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/intreadwrite.h"
22
+
23
+#include "avcodec.h"
24
+#include "bytestream.h"
25
+#include "internal.h"
26
+
27
+#define ALIAS_HEADER_SIZE 10
28
+
29
+static av_cold int encode_init(AVCodecContext *avctx)
30
+{
31
+    avctx->coded_frame = av_frame_alloc();
32
+    if (!avctx->coded_frame)
33
+        return AVERROR(ENOMEM);
34
+    return 0;
35
+}
36
+
37
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
38
+                        const AVFrame *frame, int *got_packet)
39
+{
40
+    int width, height, bits_pixel, i, j, length, ret;
41
+    uint8_t *in_buf, *buf;
42
+
43
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
44
+    avctx->coded_frame->key_frame = 1;
45
+
46
+    width  = avctx->width;
47
+    height = avctx->height;
48
+
49
+    if (width > 65535 || height > 65535 ||
50
+        width * height >= INT_MAX / 4 - ALIAS_HEADER_SIZE) {
51
+        av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", width, height);
52
+        return AVERROR_INVALIDDATA;
53
+    }
54
+
55
+    switch (avctx->pix_fmt) {
56
+    case AV_PIX_FMT_GRAY8:
57
+        bits_pixel = 8;
58
+        break;
59
+    case AV_PIX_FMT_BGR24:
60
+        bits_pixel = 24;
61
+        break;
62
+    default:
63
+        return AVERROR(EINVAL);
64
+    }
65
+
66
+    length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
67
+    if ((ret = ff_alloc_packet(pkt, length)) < 0) {
68
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
69
+        return ret;
70
+    }
71
+
72
+    buf = pkt->data;
73
+
74
+    /* Encode header. */
75
+    bytestream_put_be16(&buf, width);
76
+    bytestream_put_be16(&buf, height);
77
+    bytestream_put_be32(&buf, 0); /* X, Y offset */
78
+    bytestream_put_be16(&buf, bits_pixel);
79
+
80
+    for (j = 0; j < height; j++) {
81
+        in_buf = frame->data[0] + frame->linesize[0] * j;
82
+        for (i = 0; i < width; ) {
83
+            int count = 0;
84
+            int pixel;
85
+
86
+            if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
87
+                pixel = *in_buf;
88
+                while (count < 255 && count + i < width && pixel == *in_buf) {
89
+                    count++;
90
+                    in_buf++;
91
+                }
92
+                bytestream_put_byte(&buf, count);
93
+                bytestream_put_byte(&buf, pixel);
94
+            } else { /* AV_PIX_FMT_BGR24 */
95
+                pixel = AV_RB24(in_buf);
96
+                while (count < 255 && count + i < width &&
97
+                       pixel == AV_RB24(in_buf)) {
98
+                    count++;
99
+                    in_buf += 3;
100
+                }
101
+                bytestream_put_byte(&buf, count);
102
+                bytestream_put_be24(&buf, pixel);
103
+            }
104
+            i += count;
105
+        }
106
+    }
107
+
108
+    /* Total length */
109
+    av_shrink_packet(pkt, buf - pkt->data);
110
+    pkt->flags |= AV_PKT_FLAG_KEY;
111
+    *got_packet = 1;
112
+
113
+    return 0;
114
+}
115
+
116
+static av_cold int encode_close(AVCodecContext *avctx)
117
+{
118
+    av_frame_free(&avctx->coded_frame);
119
+    return 0;
120
+}
121
+
122
+AVCodec ff_alias_pix_encoder = {
123
+    .name      = "alias_pix",
124
+    .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
125
+    .type      = AVMEDIA_TYPE_VIDEO,
126
+    .id        = AV_CODEC_ID_ALIAS_PIX,
127
+    .init      = encode_init,
128
+    .encode2   = encode_frame,
129
+    .close     = encode_close,
130
+    .pix_fmts  = (const enum AVPixelFormat[]) {
131
+        AV_PIX_FMT_BGR24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
132
+    },
133
+};
... ...
@@ -98,6 +98,7 @@ void avcodec_register_all(void)
98 98
     REGISTER_ENCODER(A64MULTI5,         a64multi5);
99 99
     REGISTER_DECODER(AASC,              aasc);
100 100
     REGISTER_DECODER(AIC,               aic);
101
+    REGISTER_ENCDEC (ALIAS_PIX,         alias_pix);
101 102
     REGISTER_DECODER(AMV,               amv);
102 103
     REGISTER_DECODER(ANM,               anm);
103 104
     REGISTER_DECODER(ANSI,              ansi);
... ...
@@ -284,6 +284,7 @@ enum AVCodecID {
284 284
     AV_CODEC_ID_HNM4_VIDEO,
285 285
     AV_CODEC_ID_HEVC,
286 286
     AV_CODEC_ID_FIC,
287
+    AV_CODEC_ID_ALIAS_PIX,
287 288
 
288 289
     /* various PCM "codecs" */
289 290
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1095,6 +1095,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1095 1095
 
1096 1096
     /* image codecs */
1097 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
+    },
1104
+    {
1098 1105
         .id        = AV_CODEC_ID_ANSI,
1099 1106
         .type      = AVMEDIA_TYPE_VIDEO,
1100 1107
         .name      = "ansi",
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/version.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 55
32
-#define LIBAVCODEC_VERSION_MINOR 36
32
+#define LIBAVCODEC_VERSION_MINOR 37
33 33
 #define LIBAVCODEC_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -43,6 +43,7 @@ static const IdStrMap img_tags[] = {
43 43
     { AV_CODEC_ID_PGMYUV,     "pgmyuv"   },
44 44
     { AV_CODEC_ID_PBM,        "pbm"      },
45 45
     { AV_CODEC_ID_PAM,        "pam"      },
46
+    { AV_CODEC_ID_ALIAS_PIX,  "pix"      },
46 47
     { AV_CODEC_ID_MPEG1VIDEO, "mpg1-img" },
47 48
     { AV_CODEC_ID_MPEG2VIDEO, "mpg2-img" },
48 49
     { AV_CODEC_ID_MPEG4,      "mpg4-img" },
... ...
@@ -148,7 +148,7 @@ AVOutputFormat ff_image2_muxer = {
148 148
     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
149 149
     .extensions     = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
150 150
                       "ppm,sgi,tga,tif,tiff,jp2,xwd,sun,ras,rs,im1,im8,im24,"
151
-                      "sunras,webp,xbm,j2c",
151
+                      "sunras,webp,xbm,j2c,pix",
152 152
     .priv_data_size = sizeof(VideoMuxData),
153 153
     .video_codec    = AV_CODEC_ID_MJPEG,
154 154
     .write_header   = write_header,