rl2.c 6.36 KB
Newer Older
Sascha Sommer's avatar
Sascha Sommer committed
1 2 3 4
/*
 * RL2 Video Decoder
 * Copyright (C) 2008 Sascha Sommer (saschasommer@freenet.de)
 *
5
 * This file is part of Libav.
Sascha Sommer's avatar
Sascha Sommer committed
6
 *
7
 * Libav is free software; you can redistribute it and/or
Sascha Sommer's avatar
Sascha Sommer committed
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
Sascha Sommer's avatar
Sascha Sommer committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
Sascha Sommer's avatar
Sascha Sommer committed
19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24
 * RL2 Video Decoder
Sascha Sommer's avatar
Sascha Sommer committed
25
 * @author Sascha Sommer (saschasommer@freenet.de)
26
 * @see http://wiki.multimedia.cx/index.php?title=RL2
Sascha Sommer's avatar
Sascha Sommer committed
27 28 29 30 31 32
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

33
#include "libavutil/internal.h"
34
#include "libavutil/intreadwrite.h"
35
#include "libavutil/mem.h"
Sascha Sommer's avatar
Sascha Sommer committed
36
#include "avcodec.h"
37
#include "internal.h"
Sascha Sommer's avatar
Sascha Sommer committed
38 39 40 41 42 43 44


#define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette

typedef struct Rl2Context {
    AVCodecContext *avctx;

45 46 47 48
    uint16_t video_base;  ///< initial drawing offset
    uint32_t clr_count;   ///< number of used colors (currently unused)
    uint8_t *back_frame;  ///< background frame
    uint32_t palette[AVPALETTE_COUNT];
Sascha Sommer's avatar
Sascha Sommer committed
49 50 51 52 53
} Rl2Context;

/**
 * Run Length Decode a single 320x200 frame
 * @param s rl2 context
54
 * @param in input buffer
Sascha Sommer's avatar
Sascha Sommer committed
55 56 57 58 59
 * @param size input buffer size
 * @param out ouput buffer
 * @param stride stride of the output buffer
 * @param video_base offset of the rle data inside the frame
 */
60 61 62
static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size,
                               uint8_t *out, int stride, int video_base)
{
Sascha Sommer's avatar
Sascha Sommer committed
63 64 65 66
    int base_x = video_base % s->avctx->width;
    int base_y = video_base / s->avctx->width;
    int stride_adj = stride - s->avctx->width;
    int i;
67
    const uint8_t *back_frame = s->back_frame;
Anton Khirnov's avatar
Anton Khirnov committed
68 69
    const uint8_t *in_end     = in + size;
    const uint8_t *out_end    = out + stride * s->avctx->height;
70
    uint8_t *line_end;
Sascha Sommer's avatar
Sascha Sommer committed
71 72

    /** copy start of the background frame */
Anton Khirnov's avatar
Anton Khirnov committed
73 74 75 76
    for (i = 0; i <= base_y; i++) {
        if (s->back_frame)
            memcpy(out, back_frame, s->avctx->width);
        out        += stride;
Sascha Sommer's avatar
Sascha Sommer committed
77 78 79
        back_frame += s->avctx->width;
    }
    back_frame += base_x - s->avctx->width;
Anton Khirnov's avatar
Anton Khirnov committed
80 81
    line_end    = out - stride_adj;
    out        += base_x - stride;
Sascha Sommer's avatar
Sascha Sommer committed
82 83

    /** decode the variable part of the frame */
Anton Khirnov's avatar
Anton Khirnov committed
84
    while (in < in_end) {
85
        uint8_t val = *in++;
Anton Khirnov's avatar
Anton Khirnov committed
86 87 88
        int len     = 1;
        if (val >= 0x80) {
            if (in >= in_end)
Sascha Sommer's avatar
Sascha Sommer committed
89 90
                break;
            len = *in++;
Anton Khirnov's avatar
Anton Khirnov committed
91
            if (!len)
Sascha Sommer's avatar
Sascha Sommer committed
92 93 94
                break;
        }

Anton Khirnov's avatar
Anton Khirnov committed
95
        if (len >= out_end - out)
Sascha Sommer's avatar
Sascha Sommer committed
96 97
            break;

Anton Khirnov's avatar
Anton Khirnov committed
98
        if (s->back_frame)
Sascha Sommer's avatar
Sascha Sommer committed
99 100 101 102
            val |= 0x80;
        else
            val &= ~0x80;

Anton Khirnov's avatar
Anton Khirnov committed
103 104
        while (len--) {
            *out++ = (val == 0x80) ? *back_frame : val;
Sascha Sommer's avatar
Sascha Sommer committed
105
            back_frame++;
Anton Khirnov's avatar
Anton Khirnov committed
106 107
            if (out == line_end) {
                 out      += stride_adj;
Sascha Sommer's avatar
Sascha Sommer committed
108
                 line_end += stride;
Anton Khirnov's avatar
Anton Khirnov committed
109
                 if (len >= out_end - out)
Sascha Sommer's avatar
Sascha Sommer committed
110 111 112 113 114 115
                     break;
            }
        }
    }

    /** copy the rest from the background frame */
Anton Khirnov's avatar
Anton Khirnov committed
116 117
    if (s->back_frame) {
        while (out < out_end) {
Sascha Sommer's avatar
Sascha Sommer committed
118 119
            memcpy(out, back_frame, line_end - out);
            back_frame += line_end - out;
Anton Khirnov's avatar
Anton Khirnov committed
120 121
            out         = line_end + stride_adj;
            line_end   += stride;
Sascha Sommer's avatar
Sascha Sommer committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }
    }
}


/**
 * Initialize the decoder
 * @param avctx decoder context
 * @return 0 success, -1 on error
 */
static av_cold int rl2_decode_init(AVCodecContext *avctx)
{
    Rl2Context *s = avctx->priv_data;
    int back_size;
    int i;
Anton Khirnov's avatar
Anton Khirnov committed
137 138

    s->avctx       = avctx;
139
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
Sascha Sommer's avatar
Sascha Sommer committed
140 141

    /** parse extra data */
Anton Khirnov's avatar
Anton Khirnov committed
142
    if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) {
Sascha Sommer's avatar
Sascha Sommer committed
143
        av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
144
        return AVERROR(EINVAL);
Sascha Sommer's avatar
Sascha Sommer committed
145 146 147 148
    }

    /** get frame_offset */
    s->video_base = AV_RL16(&avctx->extradata[0]);
Anton Khirnov's avatar
Anton Khirnov committed
149
    s->clr_count  = AV_RL32(&avctx->extradata[2]);
Sascha Sommer's avatar
Sascha Sommer committed
150

Anton Khirnov's avatar
Anton Khirnov committed
151
    if (s->video_base >= avctx->width * avctx->height) {
Sascha Sommer's avatar
Sascha Sommer committed
152
        av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
153
        return AVERROR_INVALIDDATA;
Sascha Sommer's avatar
Sascha Sommer committed
154 155 156
    }

    /** initialize palette */
Anton Khirnov's avatar
Anton Khirnov committed
157
    for (i = 0; i < AVPALETTE_COUNT; i++)
Sascha Sommer's avatar
Sascha Sommer committed
158 159 160 161 162
        s->palette[i] = AV_RB24(&avctx->extradata[6 + i * 3]);

    /** decode background frame if present */
    back_size = avctx->extradata_size - EXTRADATA1_SIZE;

Anton Khirnov's avatar
Anton Khirnov committed
163
    if (back_size > 0) {
164
        uint8_t *back_frame = av_mallocz(avctx->width*avctx->height);
Anton Khirnov's avatar
Anton Khirnov committed
165
        if (!back_frame)
166
            return AVERROR(ENOMEM);
Anton Khirnov's avatar
Anton Khirnov committed
167 168
        rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size,
                       back_frame, avctx->width, 0);
Sascha Sommer's avatar
Sascha Sommer committed
169 170 171 172 173 174 175
        s->back_frame = back_frame;
    }
    return 0;
}


static int rl2_decode_frame(AVCodecContext *avctx,
Anton Khirnov's avatar
Anton Khirnov committed
176 177
                            void *data, int *got_frame,
                            AVPacket *avpkt)
Sascha Sommer's avatar
Sascha Sommer committed
178
{
179
    AVFrame *frame     = data;
180
    const uint8_t *buf = avpkt->data;
Anton Khirnov's avatar
Anton Khirnov committed
181
    int ret, buf_size  = avpkt->size;
Sascha Sommer's avatar
Sascha Sommer committed
182 183
    Rl2Context *s = avctx->priv_data;

184
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
Sascha Sommer's avatar
Sascha Sommer committed
185
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
186
        return ret;
Sascha Sommer's avatar
Sascha Sommer committed
187 188 189
    }

    /** run length decode */
190
    rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0],
Anton Khirnov's avatar
Anton Khirnov committed
191
                   s->video_base);
Sascha Sommer's avatar
Sascha Sommer committed
192 193

    /** make the palette available on the way out */
194
    memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
Sascha Sommer's avatar
Sascha Sommer committed
195

196
    *got_frame = 1;
Sascha Sommer's avatar
Sascha Sommer committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    /** report that the buffer was completely consumed */
    return buf_size;
}


/**
 * Uninit decoder
 * @param avctx decoder context
 * @return 0 success, -1 on error
 */
static av_cold int rl2_decode_end(AVCodecContext *avctx)
{
    Rl2Context *s = avctx->priv_data;

    av_free(s->back_frame);

    return 0;
}


218
AVCodec ff_rl2_decoder = {
219
    .name           = "rl2",
220
    .long_name      = NULL_IF_CONFIG_SMALL("RL2 video"),
221
    .type           = AVMEDIA_TYPE_VIDEO,
222
    .id             = AV_CODEC_ID_RL2,
223 224 225 226
    .priv_data_size = sizeof(Rl2Context),
    .init           = rl2_decode_init,
    .close          = rl2_decode_end,
    .decode         = rl2_decode_frame,
227
    .capabilities   = AV_CODEC_CAP_DR1,
Sascha Sommer's avatar
Sascha Sommer committed
228
};