Browse code

go LOCO, courtesy of Kostya Shishkov

Originally committed as revision 3995 to svn://svn.ffmpeg.org/ffmpeg/trunk

Mike Melanson authored on 2005/03/01 11:24:58
Showing 7 changed files
... ...
@@ -10,6 +10,7 @@ version <next>
10 10
 - QPEG video decoder
11 11
 - Nullsoft Video (NSV) file demuxer
12 12
 - Shorten audio decoder
13
+- LOCO video decoder
13 14
 
14 15
 version 0.4.9-pre1:
15 16
 
... ...
@@ -772,6 +772,7 @@ following image formats are supported:
772 772
 @item IBM Ultimotion         @tab     @tab  X @tab fourcc: ULTI
773 773
 @item Miro VideoXL           @tab     @tab  X @tab fourcc: VIXL
774 774
 @item QPEG                   @tab     @tab  X @tab fourccs: QPEG, Q1.0, Q1.1
775
+@item LOCO                   @tab     @tab  X @tab 
775 776
 @end multitable
776 777
 
777 778
 @code{X} means that the encoding (resp. decoding) is supported.
... ...
@@ -22,7 +22,7 @@ OBJS= bitstream.o utils.o mem.o allcodecs.o \
22 22
       smc.o parser.o flicvideo.o truemotion1.o vmdav.o lcl.o qtrle.o g726.o \
23 23
       flac.o vp3dsp.o integer.o snow.o tscc.o sonic.o ulti.o h264idct.o \
24 24
       qdrw.o xl.o rangecoder.o png.o pnm.o qpeg.o vc9.o h263.o h261.o \
25
-      msmpeg4.o h263dec.o svq1.o rv10.o wmadec.o indeo3.o shorten.o
25
+      msmpeg4.o h263dec.o svq1.o rv10.o wmadec.o indeo3.o shorten.o loco.o
26 26
 
27 27
 AMROBJS=
28 28
 ifeq ($(AMR_NB),yes)
... ...
@@ -125,6 +125,7 @@ void avcodec_register_all(void)
125 125
     register_avcodec(&qdraw_decoder);
126 126
     register_avcodec(&xl_decoder);
127 127
     register_avcodec(&qpeg_decoder);
128
+    register_avcodec(&loco_decoder);
128 129
 #ifdef CONFIG_FAAD
129 130
     register_avcodec(&aac_decoder);
130 131
     register_avcodec(&mpeg4aac_decoder);
... ...
@@ -17,7 +17,7 @@ extern "C" {
17 17
 
18 18
 #define FFMPEG_VERSION_INT     0x000409
19 19
 #define FFMPEG_VERSION         "0.4.9-pre1"
20
-#define LIBAVCODEC_BUILD       4743
20
+#define LIBAVCODEC_BUILD       4744
21 21
 
22 22
 #define LIBAVCODEC_VERSION_INT FFMPEG_VERSION_INT
23 23
 #define LIBAVCODEC_VERSION     FFMPEG_VERSION
... ...
@@ -104,7 +104,7 @@ enum CodecID {
104 104
     CODEC_ID_RV40,
105 105
     CODEC_ID_VC9,
106 106
     CODEC_ID_WMV3,
107
-    
107
+    CODEC_ID_LOCO,
108 108
 
109 109
     /* various pcm "codecs" */
110 110
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -2010,6 +2010,7 @@ extern AVCodec qdraw_decoder;
2010 2010
 extern AVCodec xl_decoder;
2011 2011
 extern AVCodec qpeg_decoder;
2012 2012
 extern AVCodec shorten_decoder;
2013
+extern AVCodec loco_decoder;
2013 2014
 
2014 2015
 /* pcm codecs */
2015 2016
 #define PCM_CODEC(id, name) \
2016 2017
new file mode 100644
... ...
@@ -0,0 +1,319 @@
0
+/*
1
+ * LOCO codec
2
+ * Copyright (c) 2005 Konstantin Shishkov
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
+ *
18
+ */
19
+ 
20
+/**
21
+ * @file loco.c
22
+ * LOCO codec.
23
+ */
24
+ 
25
+#include "avcodec.h"
26
+#include "common.h"
27
+#include "bitstream.h"
28
+#include "golomb.h"
29
+
30
+enum LOCO_MODE {LOCO_UNKN=0, LOCO_CYUY2=-1, LOCO_CRGB=-2, LOCO_CYV12=-3,
31
+ LOCO_YUY2=1, LOCO_UYVY=2, LOCO_RGB=3, LOCO_RGBA=4, LOCO_YV12=5};
32
+
33
+typedef struct LOCOContext{
34
+    AVCodecContext *avctx;
35
+    AVFrame pic;
36
+    int lossy;
37
+    int mode;
38
+} LOCOContext;
39
+
40
+typedef struct RICEContext{
41
+    GetBitContext gb;
42
+    int save, run, run2; /* internal rice decoder state */
43
+    int sum, count; /* sum and count for getting rice parameter */
44
+}RICEContext;
45
+
46
+/* could use get_sr_golomb() but is behaves differently on numbers like Rice(2, -64) */
47
+static inline int get_rice(GetBitContext *gb, int K)
48
+{
49
+    int i;
50
+    int V = 0;
51
+    for(i = 0; !get_bits1(gb); i++);
52
+    V = i;
53
+    for(i = 0; i < K; i++) V = (V << 1) | get_bits1(gb);
54
+    if(V & 1) return (V + 1) >> 1;
55
+    return -(V >> 1);
56
+}
57
+
58
+static inline int get_u_rice(GetBitContext *gb, int K)
59
+{
60
+    int i;
61
+    int V = 0;
62
+    for(i = 0; !get_bits1(gb); i++);
63
+    V = i;
64
+    for(i = 0; i < K; i++) V = (V << 1) | get_bits1(gb);
65
+    return V;
66
+}
67
+
68
+static int loco_get_rice_param(RICEContext *r)
69
+{
70
+    int cnt = 0;
71
+    int val = r->count;
72
+    
73
+    while(r->sum > val && cnt < 9) {
74
+        val <<= 1;
75
+        cnt++;
76
+    }
77
+    
78
+    return cnt;
79
+}
80
+
81
+static inline void loco_update_rice_param(RICEContext *r, int val)
82
+{
83
+    if (val < 0)
84
+        val = -val;
85
+    r->sum += val;
86
+    r->count++;
87
+    
88
+    if(r->count == 16) {
89
+        r->sum >>= 1;
90
+        r->count >>= 1;
91
+    }
92
+}
93
+
94
+static inline int loco_get_rice(RICEContext *r)
95
+{
96
+    int v;
97
+    
98
+    if (r->run > 0) { /* we have zero run */
99
+        r->run--;
100
+        return 0;
101
+    }
102
+    v = -get_rice(&r->gb, loco_get_rice_param(r));
103
+    if (!v) {
104
+        if (r->save >= 0) {
105
+            r->run = get_u_rice(&r->gb, 2);
106
+            if(r->run > 1)
107
+                r->save += r->run + 1;
108
+            else
109
+                r->save -= 3;
110
+        }
111
+        else
112
+            r->run2++;
113
+    } else if (r->run2 > 0) {
114
+        if (r->run2 > 2)
115
+            r->save += r->run2;
116
+        else
117
+            r->save -= 3;
118
+        r->run2 = 0;
119
+    }
120
+    
121
+    return v;
122
+}
123
+
124
+/* LOCO main predictor - LOCO-I/JPEG-LS predictor */
125
+static inline int loco_predict(uint8_t* data, int stride, int step)
126
+{
127
+    int max_ab, min_ab;
128
+    int a, b, c;
129
+    
130
+    a = data[-stride];
131
+    b = data[-step];
132
+    c = data[-stride - step];
133
+    
134
+    max_ab = (a > b) ? a : b;
135
+    min_ab = (a < b) ? a : b;
136
+    
137
+    if (c >= max_ab) return min_ab;
138
+    if (c <= min_ab) return max_ab;
139
+    return (a + b - c);
140
+}
141
+
142
+static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height,
143
+                             int stride, uint8_t *buf, int buf_size, int step)
144
+{
145
+    RICEContext rc;
146
+    int val;
147
+    int i, j;
148
+    
149
+    init_get_bits(&rc.gb, buf, buf_size*8);
150
+    rc.save = 0;
151
+    rc.run = 0;
152
+    rc.run2 = 0;
153
+    
154
+    rc.sum = 8;
155
+    rc.count = 1;
156
+    
157
+    /* restore top left pixel */
158
+    val = loco_get_rice(&rc);
159
+    loco_update_rice_param(&rc, val);
160
+    if (val < 0) val -= l->lossy;
161
+    if (val > 0) val += l->lossy;
162
+    data[0] = 128 + val;
163
+    /* restore top line */
164
+    for (i = 1; i < width; i++) {
165
+        val = loco_get_rice(&rc);
166
+        loco_update_rice_param(&rc, val);
167
+        if (val < 0) val -= l->lossy;
168
+        if (val > 0) val += l->lossy;
169
+        data[i * step] = data[i * step - step] + val;
170
+    }
171
+    data += stride;
172
+    for (j = 1; j < height; j++) {
173
+        /* restore left column */
174
+        val = loco_get_rice(&rc);
175
+        loco_update_rice_param(&rc, val);
176
+        if (val < 0) val -= l->lossy;
177
+        if (val > 0) val += l->lossy;
178
+        data[0] = data[-stride] + val;
179
+        /* restore all other pixels */
180
+        for (i = 1; i < width; i++) {
181
+            val = loco_get_rice(&rc);
182
+            loco_update_rice_param(&rc, val);
183
+            if (val < 0) val -= l->lossy;
184
+            if (val > 0) val += l->lossy;
185
+            data[i * step] = loco_predict(&data[i * step], stride, step) + val;
186
+        }
187
+        data += stride;
188
+    }
189
+    
190
+    return ((get_bits_count(&rc.gb) + 7) >> 3);
191
+}
192
+
193
+static int decode_frame(AVCodecContext *avctx, 
194
+                        void *data, int *data_size,
195
+                        uint8_t *buf, int buf_size)
196
+{
197
+    LOCOContext * const l = avctx->priv_data;
198
+    AVFrame * const p= (AVFrame*)&l->pic;
199
+    int decoded;
200
+
201
+    if(p->data[0])
202
+        avctx->release_buffer(avctx, p);
203
+
204
+    p->reference = 0;
205
+    if(avctx->get_buffer(avctx, p) < 0){
206
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
207
+        return -1;
208
+    }
209
+    p->key_frame = 1;
210
+
211
+    switch(l->mode) {
212
+    case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
213
+        decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
214
+                                    p->linesize[0], buf, buf_size, 1);
215
+        buf += decoded; buf_size -= decoded;
216
+        decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
217
+                                    p->linesize[1], buf, buf_size, 1);
218
+        buf += decoded; buf_size -= decoded;
219
+        decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
220
+                                    p->linesize[2], buf, buf_size, 1);
221
+        break;
222
+    case LOCO_CYV12: case LOCO_YV12:
223
+        decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
224
+                                    p->linesize[0], buf, buf_size, 1);
225
+        buf += decoded; buf_size -= decoded;
226
+        decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
227
+                                    p->linesize[1], buf, buf_size, 1);
228
+        buf += decoded; buf_size -= decoded;
229
+        decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
230
+                                    p->linesize[2], buf, buf_size, 1);
231
+        break;
232
+    case LOCO_CRGB: case LOCO_RGB:
233
+        decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
234
+                                    p->linesize[0], buf, buf_size, 3);
235
+        buf += decoded; buf_size -= decoded;
236
+        decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height,
237
+                                    p->linesize[0], buf, buf_size, 3);
238
+        buf += decoded; buf_size -= decoded;
239
+        decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
240
+                                    p->linesize[0], buf, buf_size, 3);
241
+        break;
242
+    case LOCO_RGBA:
243
+        decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
244
+                                    p->linesize[0], buf, buf_size, 4);
245
+        buf += decoded; buf_size -= decoded;
246
+        decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height,
247
+                                    p->linesize[0], buf, buf_size, 4);
248
+        buf += decoded; buf_size -= decoded;
249
+        decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
250
+                                    p->linesize[0], buf, buf_size, 4);
251
+        buf += decoded; buf_size -= decoded;
252
+        decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
253
+                                    p->linesize[0], buf, buf_size, 4);
254
+        break;
255
+    }
256
+
257
+    *data_size = sizeof(AVFrame);
258
+    *(AVFrame*)data = l->pic;
259
+    
260
+    return buf_size;
261
+}
262
+
263
+static int decode_init(AVCodecContext *avctx){
264
+    LOCOContext * const l = avctx->priv_data;
265
+    int version;
266
+
267
+    l->avctx = avctx;
268
+    if (avctx->extradata_size < 12) {
269
+        av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
270
+               avctx->extradata_size);
271
+        return -1;
272
+    }
273
+    version = LE_32(avctx->extradata);
274
+    switch(version) {
275
+    case 1:
276
+        l->lossy = 0;
277
+        break;
278
+    case 2:
279
+        l->lossy = LE_32(avctx->extradata + 8);
280
+        break;
281
+    default:
282
+        l->lossy = LE_32(avctx->extradata + 8);
283
+        av_log(avctx, AV_LOG_INFO, "This is LOCO codec version %i, please upload file for study\n", version);
284
+    }
285
+    
286
+    l->mode = LE_32(avctx->extradata + 4);
287
+    switch(l->mode) {
288
+    case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
289
+        avctx->pix_fmt = PIX_FMT_YUV422P;
290
+        break;
291
+    case LOCO_CRGB: case LOCO_RGB:
292
+        avctx->pix_fmt = PIX_FMT_BGR24;
293
+        break;
294
+    case LOCO_CYV12: case LOCO_YV12:
295
+        avctx->pix_fmt = PIX_FMT_YUV420P;
296
+        break;
297
+    case LOCO_RGBA:
298
+        avctx->pix_fmt = PIX_FMT_RGBA32;
299
+        break;
300
+    default:
301
+        av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
302
+        return -1;
303
+    }
304
+
305
+    return 0;
306
+}
307
+
308
+AVCodec loco_decoder = {
309
+    "loco",
310
+    CODEC_TYPE_VIDEO,
311
+    CODEC_ID_LOCO,
312
+    sizeof(LOCOContext),
313
+    decode_init,
314
+    NULL,
315
+    NULL,
316
+    decode_frame,
317
+    CODEC_CAP_DR1,
318
+};
... ...
@@ -177,6 +177,7 @@ const CodecTag codec_bmp_tags[] = {
177 177
     { CODEC_ID_QPEG, MKTAG('Q', '1', '.', '0') },
178 178
     { CODEC_ID_QPEG, MKTAG('Q', '1', '.', '1') },
179 179
     { CODEC_ID_WMV3, MKTAG('W', 'M', 'V', '3') },
180
+    { CODEC_ID_LOCO, MKTAG('L', 'O', 'C', 'O') },
180 181
     { CODEC_ID_RAWVIDEO, 0 },
181 182
     { 0, 0 },
182 183
 };