Statistics
| Branch: | Revision:

ffmpeg / libavcodec / rpza.c @ 11e29a41

History | View | Annotate | Download (10 KB)

1 2fdf638b Mike Melanson
/*
2
 * Quicktime Video (RPZA) Video Decoder
3
 * Copyright (C) 2003 the ffmpeg project
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
21
/**
22
 * @file rpza.c
23
 * QT RPZA Video Decoder by Roberto Togni <rtogni@bresciaonline.it>
24
 * For more information about the RPZA format, visit:
25
 *   http://www.pcisys.net/~melanson/codecs/
26
 *
27
 * The RPZA decoder outputs RGB555 colorspace data.
28
 *
29
 * Note that this decoder reads big endian RGB555 pixel values from the
30
 * bytestream, arranges them in the host's endian order, and outputs
31
 * them to the final rendered map in the same host endian order. This is
32
 * intended behavior as the ffmpeg documentation states that RGB555 pixels
33
 * shall be stored in native CPU endianness.
34
 */
35
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
41
#include "common.h"
42
#include "avcodec.h"
43
#include "dsputil.h"
44
45
typedef struct RpzaContext {
46
47
    AVCodecContext *avctx;
48
    DSPContext dsp;
49
    AVFrame frame;
50
    AVFrame prev_frame;
51
52
    unsigned char *buf;
53
    int size;
54
55
} RpzaContext;
56
57
#define BE_16(x)  ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
58
#define BE_32(x)  ((((uint8_t*)(x))[0] << 24) | \
59
                   (((uint8_t*)(x))[1] << 16) | \
60
                   (((uint8_t*)(x))[2] << 8) | \
61
                    ((uint8_t*)(x))[3])
62
63
#define ADVANCE_BLOCK() \
64
{ \
65
    pixel_ptr += 4; \
66
    if (pixel_ptr >= width) \
67
    { \
68
        pixel_ptr = 0; \
69
        row_ptr += stride * 4; \
70
    } \
71
    total_blocks--; \
72
    if (total_blocks < 0) \
73
    { \
74 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, "warning: block counter just went negative (this should not happen)\n"); \
75 2fdf638b Mike Melanson
        return; \
76
    } \
77
}
78
79
static void rpza_decode_stream(RpzaContext *s)
80
{
81
    int width = s->avctx->width;
82
    int stride = s->frame.linesize[0] / 2;
83
    int row_inc = stride - 4;
84
    int stream_ptr = 0;
85
    int chunk_size;
86
    unsigned char opcode;
87
    int n_blocks;
88
    unsigned short colorA = 0, colorB;
89
    unsigned short color4[4];
90
    unsigned char index, idx;
91
    unsigned short ta, tb;
92
    unsigned short *pixels = (unsigned short *)s->frame.data[0];
93
    unsigned short *prev_pixels = (unsigned short *)s->prev_frame.data[0];
94
95
    int row_ptr = 0;
96
    int pixel_ptr = 0;
97
    int block_ptr;
98
    int pixel_x, pixel_y;
99
    int total_blocks;
100
101
    /* First byte is always 0xe1. Warn if it's different */
102
    if (s->buf[stream_ptr] != 0xe1)
103 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0x1e\n",
104 2fdf638b Mike Melanson
            s->buf[stream_ptr]);
105
106
    /* Get chunk size, ingnoring first byte */
107
    chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
108
    stream_ptr += 4;
109
110
    /* If length mismatch use size from MOV file and try to decode anyway */
111
    if (chunk_size != s->size)
112 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, "MOV chunk size != encoded chunk size; using MOV chunk size\n");
113 2fdf638b Mike Melanson
114
    chunk_size = s->size;
115
116
    /* Number of 4x4 blocks in frame. */
117
    total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
118
119
    /* Process chunk data */
120
    while (stream_ptr < chunk_size) {
121
        opcode = s->buf[stream_ptr++]; /* Get opcode */
122
123
        n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
124
125
        /* If opcode MSbit is 0, we need more data to decide what to do */
126
        if ((opcode & 0x80) == 0) {
127
            colorA = (opcode << 8) | (s->buf[stream_ptr++]);
128
            opcode = 0;
129
            if ((s->buf[stream_ptr] & 0x80) != 0) {
130
                /* Must behave as opcode 110xxxxx, using colorA computed 
131
                 * above. Use fake opcode 0x20 to enter switch block at 
132
                 * the right place */
133
                opcode = 0x20;
134
                n_blocks = 1;
135
            }
136
        }
137
138
        switch (opcode & 0xe0) {
139
140
        /* Skip blocks */
141
        case 0x80:
142
            while (n_blocks--) {
143 e02c251e Roberto Togni
              if (!s->avctx->cr_available) {
144 2fdf638b Mike Melanson
                block_ptr = row_ptr + pixel_ptr;
145
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
146
                    for (pixel_x = 0; pixel_x < 4; pixel_x++){
147
                        pixels[block_ptr] = prev_pixels[block_ptr];
148
                        block_ptr++;
149
                    }
150
                    block_ptr += row_inc;
151
                }
152 e02c251e Roberto Togni
              }
153
              ADVANCE_BLOCK();
154 2fdf638b Mike Melanson
            }
155
            break;
156
157
        /* Fill blocks with one color */
158
        case 0xa0:
159
            colorA = BE_16 (&s->buf[stream_ptr]);
160
            stream_ptr += 2;
161
            while (n_blocks--) {
162
                block_ptr = row_ptr + pixel_ptr;
163
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
164
                    for (pixel_x = 0; pixel_x < 4; pixel_x++){
165
                        pixels[block_ptr] = colorA;
166
                        block_ptr++;
167
                    }
168
                    block_ptr += row_inc;
169
                }
170
                ADVANCE_BLOCK();
171
            }
172
            break;
173
174
        /* Fill blocks with 4 colors */
175
        case 0xc0:
176
            colorA = BE_16 (&s->buf[stream_ptr]);
177
            stream_ptr += 2;
178
        case 0x20:
179
            colorB = BE_16 (&s->buf[stream_ptr]);
180
            stream_ptr += 2;
181
182
            /* sort out the colors */
183
            color4[0] = colorB;
184
            color4[1] = 0;
185
            color4[2] = 0;
186
            color4[3] = colorA;
187
188
            /* red components */
189
            ta = (colorA >> 10) & 0x1F;
190
            tb = (colorB >> 10) & 0x1F;
191
            color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
192
            color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
193
194
            /* green components */
195
            ta = (colorA >> 5) & 0x1F;
196
            tb = (colorB >> 5) & 0x1F;
197
            color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
198
            color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
199
200
            /* blue components */
201
            ta = colorA & 0x1F;
202
            tb = colorB & 0x1F;
203
            color4[1] |= ((11 * ta + 21 * tb) >> 5);
204
            color4[2] |= ((21 * ta + 11 * tb) >> 5);
205
206
            while (n_blocks--) {
207
                block_ptr = row_ptr + pixel_ptr;
208
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
209
                    index = s->buf[stream_ptr++];
210
                    for (pixel_x = 0; pixel_x < 4; pixel_x++){
211
                        idx = (index >> (2 * (3 - pixel_x))) & 0x03;
212
                        pixels[block_ptr] = color4[idx];
213
                        block_ptr++;
214
                    }
215
                    block_ptr += row_inc;
216
                }
217
                ADVANCE_BLOCK();
218
            }
219
            break;
220
221
        /* Fill block with 16 colors */
222
        case 0x00:
223
            block_ptr = row_ptr + pixel_ptr;
224
            for (pixel_y = 0; pixel_y < 4; pixel_y++) {
225
                for (pixel_x = 0; pixel_x < 4; pixel_x++){
226
                    /* We already have color of upper left pixel */
227
                    if ((pixel_y != 0) || (pixel_x !=0)) {
228
                        colorA = BE_16 (&s->buf[stream_ptr]);
229
                        stream_ptr += 2;
230
                    }
231
                    pixels[block_ptr] = colorA;
232
                    block_ptr++;
233
                }
234
                block_ptr += row_inc;
235
            }
236
            ADVANCE_BLOCK();
237
            break;
238
239
        /* Unknown opcode */
240
        default:
241 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
242 2fdf638b Mike Melanson
                 " Skip remaining %d bytes of chunk data.\n", opcode,
243
                 chunk_size - stream_ptr);
244
            return;
245
        } /* Opcode switch */
246
    }
247
}
248
249
static int rpza_decode_init(AVCodecContext *avctx)
250
{
251
    RpzaContext *s = (RpzaContext *)avctx->priv_data;
252
253
    s->avctx = avctx;
254
    avctx->pix_fmt = PIX_FMT_RGB555;
255
    avctx->has_b_frames = 0;
256
    dsputil_init(&s->dsp, avctx);
257
258
    s->frame.data[0] = s->prev_frame.data[0] = NULL;
259
260
    return 0;
261
}
262
263
static int rpza_decode_frame(AVCodecContext *avctx,
264
                             void *data, int *data_size,
265
                             uint8_t *buf, int buf_size)
266
{
267
    RpzaContext *s = (RpzaContext *)avctx->priv_data;
268
269 e02c251e Roberto Togni
        /* no supplementary picture */
270
        if (buf_size == 0)
271
                return 0;
272
273 2fdf638b Mike Melanson
    s->buf = buf;
274
    s->size = buf_size;
275
276 72385798 Mike Melanson
    s->frame.reference = 1;
277 e02c251e Roberto Togni
        s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE;
278
    if (avctx->cr_available)
279
        s->frame.buffer_hints |= FF_BUFFER_HINTS_REUSABLE;
280
    else
281
        s->frame.buffer_hints |= FF_BUFFER_HINTS_READABLE;
282 2fdf638b Mike Melanson
    if (avctx->get_buffer(avctx, &s->frame)) {
283 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "  RPZA Video: get_buffer() failed\n");
284 2fdf638b Mike Melanson
        return -1;
285
    }
286
287 e02c251e Roberto Togni
    if (s->prev_frame.data[0] &&(s->frame.linesize[0] != s->prev_frame.linesize[0]))
288
        av_log(avctx, AV_LOG_ERROR, "Buffer linesize changed: current %u, previous %u.\n"
289
                "Expect wrong image and/or crash!\n",
290
                s->frame.linesize[0], s->prev_frame.linesize[0]);
291
292 2fdf638b Mike Melanson
    rpza_decode_stream(s);
293
294
    if (s->prev_frame.data[0])
295
        avctx->release_buffer(avctx, &s->prev_frame);
296
297
    /* shuffle frames */
298 e02c251e Roberto Togni
  if (!avctx->cr_available)
299 2fdf638b Mike Melanson
    s->prev_frame = s->frame;
300
301
    *data_size = sizeof(AVFrame);
302
    *(AVFrame*)data = s->frame;
303
304
    /* always report that the buffer was completely consumed */
305
    return buf_size;
306
}
307
308
static int rpza_decode_end(AVCodecContext *avctx)
309
{
310
    RpzaContext *s = (RpzaContext *)avctx->priv_data;
311
312
    if (s->prev_frame.data[0])
313
        avctx->release_buffer(avctx, &s->prev_frame);
314
315
    return 0;
316
}
317
318
AVCodec rpza_decoder = {
319
    "rpza",
320
    CODEC_TYPE_VIDEO,
321
    CODEC_ID_RPZA,
322
    sizeof(RpzaContext),
323
    rpza_decode_init,
324
    NULL,
325
    rpza_decode_end,
326
    rpza_decode_frame,
327 e02c251e Roberto Togni
    CODEC_CAP_DR1 | CODEC_CAP_CR,
328 2fdf638b Mike Melanson
};