Browse code

XBM decoder

Signed-off-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Paul B Mahol authored on 2012/03/24 07:54:09
Showing 5 changed files
... ...
@@ -15,7 +15,7 @@ version next:
15 15
 - libutvideo encoder wrapper (--enable-libutvideo)
16 16
 - swapuv filter
17 17
 - bbox filter
18
-- XBM encoder
18
+- XBM encoder and decoder
19 19
 - RealAudio Lossless decoder
20 20
 - ZeroCodec decoder
21 21
 - tile video filter
... ...
@@ -403,7 +403,7 @@ following image formats are supported:
403 403
     @tab YUV, JPEG and some extension is not supported yet.
404 404
 @item Truevision Targa  @tab X @tab X
405 405
     @tab Targa (.TGA) image format
406
-@item XBM  @tab X @tab
406
+@item XBM  @tab X @tab X
407 407
     @tab X BitMap image format
408 408
 @item XWD  @tab X @tab X
409 409
     @tab X Window Dump image format
... ...
@@ -486,6 +486,7 @@ OBJS-$(CONFIG_XAN_DPCM_DECODER)        += dpcm.o
486 486
 OBJS-$(CONFIG_XAN_WC3_DECODER)         += xan.o
487 487
 OBJS-$(CONFIG_XAN_WC4_DECODER)         += xxan.o
488 488
 OBJS-$(CONFIG_XBIN_DECODER)            += bintext.o cga_data.o
489
+OBJS-$(CONFIG_XBM_DECODER)             += xbmdec.o
489 490
 OBJS-$(CONFIG_XBM_ENCODER)             += xbmenc.o
490 491
 OBJS-$(CONFIG_XL_DECODER)              += xl.o
491 492
 OBJS-$(CONFIG_XSUB_DECODER)            += xsubdec.o
... ...
@@ -249,7 +249,7 @@ void avcodec_register_all(void)
249 249
     REGISTER_DECODER (WNV1, wnv1);
250 250
     REGISTER_DECODER (XAN_WC3, xan_wc3);
251 251
     REGISTER_DECODER (XAN_WC4, xan_wc4);
252
-    REGISTER_ENCODER (XBM, xbm);
252
+    REGISTER_ENCDEC  (XBM, xbm);
253 253
     REGISTER_DECODER (XL, xl);
254 254
     REGISTER_ENCDEC  (XWD, xwd);
255 255
     REGISTER_ENCDEC  (Y41P, y41p);
256 256
new file mode 100644
... ...
@@ -0,0 +1,130 @@
0
+/*
1
+ * XBM image format
2
+ *
3
+ * Copyright (c) 2012 Paul B Mahol
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
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.
11
+ *
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.
16
+ *
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
20
+ */
21
+
22
+#include "avcodec.h"
23
+#include "internal.h"
24
+
25
+static av_cold int xbm_decode_init(AVCodecContext *avctx)
26
+{
27
+    avctx->coded_frame = avcodec_alloc_frame();
28
+    if (!avctx->coded_frame)
29
+        return AVERROR(ENOMEM);
30
+
31
+    return 0;
32
+}
33
+
34
+static int convert(uint8_t x)
35
+{
36
+    if (x >= 'a')
37
+        x -= 87;
38
+    else if (x >= 'A')
39
+        x -= 55;
40
+    else
41
+        x -= '0';
42
+    return x;
43
+}
44
+
45
+static int xbm_decode_frame(AVCodecContext *avctx, void *data,
46
+                            int *data_size, AVPacket *avpkt)
47
+{
48
+    AVFrame *p = avctx->coded_frame;
49
+    const uint8_t *end, *ptr = avpkt->data;
50
+    uint8_t *dst;
51
+    int ret, linesize, i, j;
52
+
53
+    end = avpkt->data + avpkt->size;
54
+    while (!avctx->width || !avctx->height) {
55
+        char name[256];
56
+        int number, len;
57
+
58
+        ptr += strcspn(ptr, "#");
59
+        if (sscanf(ptr, "#define %256s %u", name, &number) != 2)
60
+            return AVERROR_INVALIDDATA;
61
+
62
+        len = strlen(name);
63
+        if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
64
+                avctx->height = number;
65
+        } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
66
+                avctx->width = number;
67
+        } else {
68
+            return AVERROR_INVALIDDATA;
69
+        }
70
+        ptr += strcspn(ptr, "\n\r") + 1;
71
+    }
72
+
73
+    avctx->pix_fmt = PIX_FMT_MONOWHITE;
74
+
75
+    if (p->data[0])
76
+        avctx->release_buffer(avctx, p);
77
+
78
+    p->reference = 0;
79
+    if ((ret = avctx->get_buffer(avctx, p)) < 0)
80
+        return ret;
81
+
82
+    linesize = (avctx->width + 7) / 8;
83
+    for (i = 0; i < avctx->height; i++) {
84
+        dst = p->data[0] + i * p->linesize[0];
85
+        for (j = 0; j < linesize; j++) {
86
+            uint8_t val;
87
+
88
+            ptr += strcspn(ptr, "x") + 1;
89
+            if (ptr < end && isxdigit(*ptr)) {
90
+                val = convert(*ptr);
91
+                ptr++;
92
+                if (isxdigit(*ptr))
93
+                    val = (val << 4) + convert(*ptr);
94
+                *dst++ = av_reverse[val];
95
+            } else {
96
+                return AVERROR_INVALIDDATA;
97
+            }
98
+        }
99
+    }
100
+
101
+    p->key_frame = 1;
102
+    p->pict_type = AV_PICTURE_TYPE_I;
103
+
104
+    *data_size       = sizeof(AVFrame);
105
+    *(AVFrame *)data = *p;
106
+
107
+    return avpkt->size;
108
+}
109
+
110
+static av_cold int xbm_decode_close(AVCodecContext *avctx)
111
+{
112
+    if (avctx->coded_frame->data[0])
113
+        avctx->release_buffer(avctx, avctx->coded_frame);
114
+
115
+    av_freep(&avctx->coded_frame);
116
+
117
+    return 0;
118
+}
119
+
120
+AVCodec ff_xbm_decoder = {
121
+    .name         = "xbm",
122
+    .type         = AVMEDIA_TYPE_VIDEO,
123
+    .id           = CODEC_ID_XBM,
124
+    .init         = xbm_decode_init,
125
+    .close        = xbm_decode_close,
126
+    .decode       = xbm_decode_frame,
127
+    .capabilities = CODEC_CAP_DR1,
128
+    .long_name    = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
129
+};