Statistics
| Branch: | Revision:

ffmpeg / libavcodec / motionpixels.c @ 7c7d3abf

History | View | Annotate | Download (11.2 KB)

1
/*
2
 * Motion Pixels Video Decoder
3
 * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
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 "get_bits.h"
24
#include "dsputil.h"
25

    
26
#define MAX_HUFF_CODES 16
27

    
28
typedef struct YuvPixel {
29
    int8_t y, v, u;
30
} YuvPixel;
31

    
32
typedef struct HuffCode {
33
    int code;
34
    uint8_t size;
35
    uint8_t delta;
36
} HuffCode;
37

    
38
typedef struct MotionPixelsContext {
39
    AVCodecContext *avctx;
40
    AVFrame frame;
41
    DSPContext dsp;
42
    uint8_t *changes_map;
43
    int offset_bits_len;
44
    int codes_count, current_codes_count;
45
    int max_codes_bits;
46
    HuffCode codes[MAX_HUFF_CODES];
47
    VLC vlc;
48
    YuvPixel *vpt, *hpt;
49
    uint8_t gradient_scale[3];
50
    uint8_t *bswapbuf;
51
    int bswapbuf_size;
52
} MotionPixelsContext;
53

    
54
static YuvPixel mp_rgb_yuv_table[1 << 15];
55

    
56
static int mp_yuv_to_rgb(int y, int v, int u, int clip_rgb) {
57
    static const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
58
    int r, g, b;
59

    
60
    r = (1000 * y + 701 * v) / 1000;
61
    g = (1000 * y - 357 * v - 172 * u) / 1000;
62
    b = (1000 * y + 886 * u) / 1000;
63
    if (clip_rgb)
64
        return ((cm[r * 8] & 0xF8) << 7) | ((cm[g * 8] & 0xF8) << 2) | (cm[b * 8] >> 3);
65
    if ((unsigned)r < 32 && (unsigned)g < 32 && (unsigned)b < 32)
66
        return (r << 10) | (g << 5) | b;
67
    return 1 << 15;
68
}
69

    
70
static void mp_set_zero_yuv(YuvPixel *p)
71
{
72
    int i, j;
73

    
74
    for (i = 0; i < 31; ++i) {
75
        for (j = 31; j > i; --j)
76
            if (!(p[j].u | p[j].v | p[j].y))
77
                p[j] = p[j - 1];
78
        for (j = 0; j < 31 - i; ++j)
79
            if (!(p[j].u | p[j].v | p[j].y))
80
                p[j] = p[j + 1];
81
    }
82
}
83

    
84
static void mp_build_rgb_yuv_table(YuvPixel *p)
85
{
86
    int y, v, u, i;
87

    
88
    for (y = 0; y <= 31; ++y)
89
        for (v = -31; v <= 31; ++v)
90
            for (u = -31; u <= 31; ++u) {
91
                i = mp_yuv_to_rgb(y, v, u, 0);
92
                if (i < (1 << 15) && !(p[i].u | p[i].v | p[i].y)) {
93
                    p[i].y = y;
94
                    p[i].v = v;
95
                    p[i].u = u;
96
                }
97
            }
98
    for (i = 0; i < 1024; ++i)
99
        mp_set_zero_yuv(p + i * 32);
100
}
101

    
102
static av_cold int mp_decode_init(AVCodecContext *avctx)
103
{
104
    MotionPixelsContext *mp = avctx->priv_data;
105

    
106
    if (!mp_rgb_yuv_table[0].u) {
107
        mp_build_rgb_yuv_table(mp_rgb_yuv_table);
108
    }
109
    mp->avctx = avctx;
110
    dsputil_init(&mp->dsp, avctx);
111
    mp->changes_map = av_mallocz(avctx->width * avctx->height);
112
    mp->offset_bits_len = av_log2(avctx->width * avctx->height) + 1;
113
    mp->vpt = av_mallocz(avctx->height * sizeof(YuvPixel));
114
    mp->hpt = av_mallocz(avctx->height * avctx->width / 16 * sizeof(YuvPixel));
115
    avctx->pix_fmt = PIX_FMT_RGB555;
116
    return 0;
117
}
118

    
119
static void mp_read_changes_map(MotionPixelsContext *mp, GetBitContext *gb, int count, int bits_len, int read_color)
120
{
121
    uint16_t *pixels;
122
    int offset, w, h, color = 0, x, y, i;
123

    
124
    while (count--) {
125
        offset = get_bits_long(gb, mp->offset_bits_len);
126
        w      = get_bits(gb, bits_len) + 1;
127
        h      = get_bits(gb, bits_len) + 1;
128
        if (read_color)
129
            color = get_bits(gb, 15);
130
        x = offset % mp->avctx->width;
131
        y = offset / mp->avctx->width;
132
        if (y >= mp->avctx->height)
133
            continue;
134
        w = FFMIN(w, mp->avctx->width  - x);
135
        h = FFMIN(h, mp->avctx->height - y);
136
        pixels = (uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2];
137
        while (h--) {
138
            mp->changes_map[offset] = w;
139
            if (read_color)
140
                for (i = 0; i < w; ++i)
141
                    pixels[i] = color;
142
            offset += mp->avctx->width;
143
            pixels += mp->frame.linesize[0] / 2;
144
        }
145
    }
146
}
147

    
148
static void mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size, int code)
149
{
150
    while (get_bits1(gb)) {
151
        ++size;
152
        if (size > mp->max_codes_bits) {
153
            av_log(mp->avctx, AV_LOG_ERROR, "invalid code size %d/%d\n", size, mp->max_codes_bits);
154
            return;
155
        }
156
        code <<= 1;
157
        mp_get_code(mp, gb, size, code + 1);
158
    }
159
    if (mp->current_codes_count >= MAX_HUFF_CODES) {
160
        av_log(mp->avctx, AV_LOG_ERROR, "too many codes\n");
161
        return;
162
    }
163
    mp->codes[mp->current_codes_count  ].code = code;
164
    mp->codes[mp->current_codes_count++].size = size;
165
}
166

    
167
static void mp_read_codes_table(MotionPixelsContext *mp, GetBitContext *gb)
168
{
169
    if (mp->codes_count == 1) {
170
        mp->codes[0].delta = get_bits(gb, 4);
171
    } else {
172
        int i;
173

    
174
        mp->max_codes_bits = get_bits(gb, 4);
175
        for (i = 0; i < mp->codes_count; ++i)
176
            mp->codes[i].delta = get_bits(gb, 4);
177
        mp->current_codes_count = 0;
178
        mp_get_code(mp, gb, 0, 0);
179
   }
180
}
181

    
182
static int mp_gradient(MotionPixelsContext *mp, int component, int v)
183
{
184
    int delta;
185

    
186
    delta = (v - 7) * mp->gradient_scale[component];
187
    mp->gradient_scale[component] = (v == 0 || v == 14) ? 2 : 1;
188
    return delta;
189
}
190

    
191
static YuvPixel mp_get_yuv_from_rgb(MotionPixelsContext *mp, int x, int y)
192
{
193
    int color;
194

    
195
    color = *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2];
196
    return mp_rgb_yuv_table[color];
197
}
198

    
199
static void mp_set_rgb_from_yuv(MotionPixelsContext *mp, int x, int y, const YuvPixel *p)
200
{
201
    int color;
202

    
203
    color = mp_yuv_to_rgb(p->y, p->v, p->u, 1);
204
    *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2] = color;
205
}
206

    
207
static int mp_get_vlc(MotionPixelsContext *mp, GetBitContext *gb)
208
{
209
    int i;
210

    
211
    i = (mp->codes_count == 1) ? 0 : get_vlc2(gb, mp->vlc.table, mp->max_codes_bits, 1);
212
    return mp->codes[i].delta;
213
}
214

    
215
static void mp_decode_line(MotionPixelsContext *mp, GetBitContext *gb, int y)
216
{
217
    YuvPixel p;
218
    const int y0 = y * mp->avctx->width;
219
    int w, i, x = 0;
220

    
221
    p = mp->vpt[y];
222
    if (mp->changes_map[y0 + x] == 0) {
223
        memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
224
        ++x;
225
    }
226
    while (x < mp->avctx->width) {
227
        w = mp->changes_map[y0 + x];
228
        if (w != 0) {
229
            if ((y & 3) == 0) {
230
                if (mp->changes_map[y0 + x + mp->avctx->width] < w ||
231
                    mp->changes_map[y0 + x + mp->avctx->width * 2] < w ||
232
                    mp->changes_map[y0 + x + mp->avctx->width * 3] < w) {
233
                    for (i = (x + 3) & ~3; i < x + w; i += 4) {
234
                        mp->hpt[((y / 4) * mp->avctx->width + i) / 4] = mp_get_yuv_from_rgb(mp, i, y);
235
                    }
236
                }
237
            }
238
            x += w;
239
            memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
240
            p = mp_get_yuv_from_rgb(mp, x - 1, y);
241
        } else {
242
            p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb));
243
            if ((x & 3) == 0) {
244
                if ((y & 3) == 0) {
245
                    p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
246
                    p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
247
                    mp->hpt[((y / 4) * mp->avctx->width + x) / 4] = p;
248
                } else {
249
                    p.v = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].v;
250
                    p.u = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].u;
251
                }
252
            }
253
            mp_set_rgb_from_yuv(mp, x, y, &p);
254
            ++x;
255
        }
256
    }
257
}
258

    
259
static void mp_decode_frame_helper(MotionPixelsContext *mp, GetBitContext *gb)
260
{
261
    YuvPixel p;
262
    int y, y0;
263

    
264
    for (y = 0; y < mp->avctx->height; ++y) {
265
        if (mp->changes_map[y * mp->avctx->width] != 0) {
266
            memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale));
267
            p = mp_get_yuv_from_rgb(mp, 0, y);
268
        } else {
269
            p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb));
270
            if ((y & 3) == 0) {
271
                p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb));
272
                p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb));
273
            }
274
            mp->vpt[y] = p;
275
            mp_set_rgb_from_yuv(mp, 0, y, &p);
276
        }
277
    }
278
    for (y0 = 0; y0 < 2; ++y0)
279
        for (y = y0; y < mp->avctx->height; y += 2)
280
            mp_decode_line(mp, gb, y);
281
}
282

    
283
static int mp_decode_frame(AVCodecContext *avctx,
284
                                 void *data, int *data_size,
285
                                 AVPacket *avpkt)
286
{
287
    const uint8_t *buf = avpkt->data;
288
    int buf_size = avpkt->size;
289
    MotionPixelsContext *mp = avctx->priv_data;
290
    GetBitContext gb;
291
    int i, count1, count2, sz;
292

    
293
    mp->frame.reference = 1;
294
    mp->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
295
    if (avctx->reget_buffer(avctx, &mp->frame)) {
296
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
297
        return -1;
298
    }
299

    
300
    /* le32 bitstream msb first */
301
    av_fast_malloc(&mp->bswapbuf, &mp->bswapbuf_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
302
    if (!mp->bswapbuf)
303
        return AVERROR(ENOMEM);
304
    mp->dsp.bswap_buf((uint32_t *)mp->bswapbuf, (const uint32_t *)buf, buf_size / 4);
305
    if (buf_size & 3)
306
        memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3);
307
    init_get_bits(&gb, mp->bswapbuf, buf_size * 8);
308

    
309
    memset(mp->changes_map, 0, avctx->width * avctx->height);
310
    for (i = !(avctx->extradata[1] & 2); i < 2; ++i) {
311
        count1 = get_bits(&gb, 12);
312
        count2 = get_bits(&gb, 12);
313
        mp_read_changes_map(mp, &gb, count1, 8, i);
314
        mp_read_changes_map(mp, &gb, count2, 4, i);
315
    }
316

    
317
    mp->codes_count = get_bits(&gb, 4);
318
    if (mp->codes_count == 0)
319
        goto end;
320

    
321
    if (mp->changes_map[0] == 0) {
322
        *(uint16_t *)mp->frame.data[0] = get_bits(&gb, 15);
323
        mp->changes_map[0] = 1;
324
    }
325
    mp_read_codes_table(mp, &gb);
326

    
327
    sz = get_bits(&gb, 18);
328
    if (avctx->extradata[0] != 5)
329
        sz += get_bits(&gb, 18);
330
    if (sz == 0)
331
        goto end;
332

    
333
    init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, &mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 4, 0);
334
    mp_decode_frame_helper(mp, &gb);
335
    free_vlc(&mp->vlc);
336

    
337
end:
338
    *data_size = sizeof(AVFrame);
339
    *(AVFrame *)data = mp->frame;
340
    return buf_size;
341
}
342

    
343
static av_cold int mp_decode_end(AVCodecContext *avctx)
344
{
345
    MotionPixelsContext *mp = avctx->priv_data;
346

    
347
    av_freep(&mp->changes_map);
348
    av_freep(&mp->vpt);
349
    av_freep(&mp->hpt);
350
    av_freep(&mp->bswapbuf);
351
    if (mp->frame.data[0])
352
        avctx->release_buffer(avctx, &mp->frame);
353

    
354
    return 0;
355
}
356

    
357
AVCodec motionpixels_decoder = {
358
    "motionpixels",
359
    CODEC_TYPE_VIDEO,
360
    CODEC_ID_MOTIONPIXELS,
361
    sizeof(MotionPixelsContext),
362
    mp_decode_init,
363
    NULL,
364
    mp_decode_end,
365
    mp_decode_frame,
366
    CODEC_CAP_DR1,
367
    .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels video"),
368
};