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