00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040
00041 #include "common.h"
00042 #include "avcodec.h"
00043 #include "dsputil.h"
00044
00045 typedef struct RpzaContext {
00046
00047 AVCodecContext *avctx;
00048 DSPContext dsp;
00049 AVFrame frame;
00050
00051 unsigned char *buf;
00052 int size;
00053
00054 } RpzaContext;
00055
00056 #define ADVANCE_BLOCK() \
00057 { \
00058 pixel_ptr += 4; \
00059 if (pixel_ptr >= width) \
00060 { \
00061 pixel_ptr = 0; \
00062 row_ptr += stride * 4; \
00063 } \
00064 total_blocks--; \
00065 if (total_blocks < 0) \
00066 { \
00067 av_log(s->avctx, AV_LOG_ERROR, "warning: block counter just went negative (this should not happen)\n"); \
00068 return; \
00069 } \
00070 }
00071
00072 static void rpza_decode_stream(RpzaContext *s)
00073 {
00074 int width = s->avctx->width;
00075 int stride = s->frame.linesize[0] / 2;
00076 int row_inc = stride - 4;
00077 int stream_ptr = 0;
00078 int chunk_size;
00079 unsigned char opcode;
00080 int n_blocks;
00081 unsigned short colorA = 0, colorB;
00082 unsigned short color4[4];
00083 unsigned char index, idx;
00084 unsigned short ta, tb;
00085 unsigned short *pixels = (unsigned short *)s->frame.data[0];
00086
00087 int row_ptr = 0;
00088 int pixel_ptr = 0;
00089 int block_ptr;
00090 int pixel_x, pixel_y;
00091 int total_blocks;
00092
00093
00094 if (s->buf[stream_ptr] != 0xe1)
00095 av_log(s->avctx, AV_LOG_ERROR, "First chunk byte is 0x%02x instead of 0xe1\n",
00096 s->buf[stream_ptr]);
00097
00098
00099 chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
00100 stream_ptr += 4;
00101
00102
00103 if (chunk_size != s->size)
00104 av_log(s->avctx, AV_LOG_ERROR, "MOV chunk size != encoded chunk size; using MOV chunk size\n");
00105
00106 chunk_size = s->size;
00107
00108
00109 total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4);
00110
00111
00112 while (stream_ptr < chunk_size) {
00113 opcode = s->buf[stream_ptr++];
00114
00115 n_blocks = (opcode & 0x1f) + 1;
00116
00117
00118 if ((opcode & 0x80) == 0) {
00119 colorA = (opcode << 8) | (s->buf[stream_ptr++]);
00120 opcode = 0;
00121 if ((s->buf[stream_ptr] & 0x80) != 0) {
00122
00123
00124
00125 opcode = 0x20;
00126 n_blocks = 1;
00127 }
00128 }
00129
00130 switch (opcode & 0xe0) {
00131
00132
00133 case 0x80:
00134 while (n_blocks--) {
00135 ADVANCE_BLOCK();
00136 }
00137 break;
00138
00139
00140 case 0xa0:
00141 colorA = BE_16 (&s->buf[stream_ptr]);
00142 stream_ptr += 2;
00143 while (n_blocks--) {
00144 block_ptr = row_ptr + pixel_ptr;
00145 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00146 for (pixel_x = 0; pixel_x < 4; pixel_x++){
00147 pixels[block_ptr] = colorA;
00148 block_ptr++;
00149 }
00150 block_ptr += row_inc;
00151 }
00152 ADVANCE_BLOCK();
00153 }
00154 break;
00155
00156
00157 case 0xc0:
00158 colorA = BE_16 (&s->buf[stream_ptr]);
00159 stream_ptr += 2;
00160 case 0x20:
00161 colorB = BE_16 (&s->buf[stream_ptr]);
00162 stream_ptr += 2;
00163
00164
00165 color4[0] = colorB;
00166 color4[1] = 0;
00167 color4[2] = 0;
00168 color4[3] = colorA;
00169
00170
00171 ta = (colorA >> 10) & 0x1F;
00172 tb = (colorB >> 10) & 0x1F;
00173 color4[1] |= ((11 * ta + 21 * tb) >> 5) << 10;
00174 color4[2] |= ((21 * ta + 11 * tb) >> 5) << 10;
00175
00176
00177 ta = (colorA >> 5) & 0x1F;
00178 tb = (colorB >> 5) & 0x1F;
00179 color4[1] |= ((11 * ta + 21 * tb) >> 5) << 5;
00180 color4[2] |= ((21 * ta + 11 * tb) >> 5) << 5;
00181
00182
00183 ta = colorA & 0x1F;
00184 tb = colorB & 0x1F;
00185 color4[1] |= ((11 * ta + 21 * tb) >> 5);
00186 color4[2] |= ((21 * ta + 11 * tb) >> 5);
00187
00188 while (n_blocks--) {
00189 block_ptr = row_ptr + pixel_ptr;
00190 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00191 index = s->buf[stream_ptr++];
00192 for (pixel_x = 0; pixel_x < 4; pixel_x++){
00193 idx = (index >> (2 * (3 - pixel_x))) & 0x03;
00194 pixels[block_ptr] = color4[idx];
00195 block_ptr++;
00196 }
00197 block_ptr += row_inc;
00198 }
00199 ADVANCE_BLOCK();
00200 }
00201 break;
00202
00203
00204 case 0x00:
00205 block_ptr = row_ptr + pixel_ptr;
00206 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00207 for (pixel_x = 0; pixel_x < 4; pixel_x++){
00208
00209 if ((pixel_y != 0) || (pixel_x !=0)) {
00210 colorA = BE_16 (&s->buf[stream_ptr]);
00211 stream_ptr += 2;
00212 }
00213 pixels[block_ptr] = colorA;
00214 block_ptr++;
00215 }
00216 block_ptr += row_inc;
00217 }
00218 ADVANCE_BLOCK();
00219 break;
00220
00221
00222 default:
00223 av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
00224 " Skip remaining %d bytes of chunk data.\n", opcode,
00225 chunk_size - stream_ptr);
00226 return;
00227 }
00228 }
00229 }
00230
00231 static int rpza_decode_init(AVCodecContext *avctx)
00232 {
00233 RpzaContext *s = (RpzaContext *)avctx->priv_data;
00234
00235 s->avctx = avctx;
00236 avctx->pix_fmt = PIX_FMT_RGB555;
00237 avctx->has_b_frames = 0;
00238 dsputil_init(&s->dsp, avctx);
00239
00240 s->frame.data[0] = NULL;
00241
00242 return 0;
00243 }
00244
00245 static int rpza_decode_frame(AVCodecContext *avctx,
00246 void *data, int *data_size,
00247 uint8_t *buf, int buf_size)
00248 {
00249 RpzaContext *s = (RpzaContext *)avctx->priv_data;
00250
00251 s->buf = buf;
00252 s->size = buf_size;
00253
00254 s->frame.reference = 1;
00255 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00256 if (avctx->reget_buffer(avctx, &s->frame)) {
00257 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00258 return -1;
00259 }
00260
00261 rpza_decode_stream(s);
00262
00263 *data_size = sizeof(AVFrame);
00264 *(AVFrame*)data = s->frame;
00265
00266
00267 return buf_size;
00268 }
00269
00270 static int rpza_decode_end(AVCodecContext *avctx)
00271 {
00272 RpzaContext *s = (RpzaContext *)avctx->priv_data;
00273
00274 if (s->frame.data[0])
00275 avctx->release_buffer(avctx, &s->frame);
00276
00277 return 0;
00278 }
00279
00280 AVCodec rpza_decoder = {
00281 "rpza",
00282 CODEC_TYPE_VIDEO,
00283 CODEC_ID_RPZA,
00284 sizeof(RpzaContext),
00285 rpza_decode_init,
00286 NULL,
00287 rpza_decode_end,
00288 rpza_decode_frame,
00289 CODEC_CAP_DR1,
00290 };