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 while (n_blocks--) {
00187 block_ptr = row_ptr + pixel_ptr;
00188 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00189 index = s->buf[stream_ptr++];
00190 for (pixel_x = 0; pixel_x < 4; pixel_x++){
00191 idx = (index >> (2 * (3 - pixel_x))) & 0x03;
00192 pixels[block_ptr] = color4[idx];
00193 block_ptr++;
00194 }
00195 block_ptr += row_inc;
00196 }
00197 ADVANCE_BLOCK();
00198 }
00199 break;
00200
00201
00202 case 0x00:
00203 block_ptr = row_ptr + pixel_ptr;
00204 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
00205 for (pixel_x = 0; pixel_x < 4; pixel_x++){
00206
00207 if ((pixel_y != 0) || (pixel_x !=0)) {
00208 colorA = AV_RB16 (&s->buf[stream_ptr]);
00209 stream_ptr += 2;
00210 }
00211 pixels[block_ptr] = colorA;
00212 block_ptr++;
00213 }
00214 block_ptr += row_inc;
00215 }
00216 ADVANCE_BLOCK();
00217 break;
00218
00219
00220 default:
00221 av_log(s->avctx, AV_LOG_ERROR, "Unknown opcode %d in rpza chunk."
00222 " Skip remaining %d bytes of chunk data.\n", opcode,
00223 chunk_size - stream_ptr);
00224 return;
00225 }
00226 }
00227 }
00228
00229 static av_cold int rpza_decode_init(AVCodecContext *avctx)
00230 {
00231 RpzaContext *s = avctx->priv_data;
00232
00233 s->avctx = avctx;
00234 avctx->pix_fmt = PIX_FMT_RGB555;
00235
00236 avcodec_get_frame_defaults(&s->frame);
00237 s->frame.data[0] = NULL;
00238
00239 return 0;
00240 }
00241
00242 static int rpza_decode_frame(AVCodecContext *avctx,
00243 void *data, int *data_size,
00244 AVPacket *avpkt)
00245 {
00246 const uint8_t *buf = avpkt->data;
00247 int buf_size = avpkt->size;
00248 RpzaContext *s = avctx->priv_data;
00249
00250 s->buf = buf;
00251 s->size = buf_size;
00252
00253 s->frame.reference = 3;
00254 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00255 if (avctx->reget_buffer(avctx, &s->frame)) {
00256 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00257 return -1;
00258 }
00259
00260 rpza_decode_stream(s);
00261
00262 *data_size = sizeof(AVFrame);
00263 *(AVFrame*)data = s->frame;
00264
00265
00266 return buf_size;
00267 }
00268
00269 static av_cold int rpza_decode_end(AVCodecContext *avctx)
00270 {
00271 RpzaContext *s = avctx->priv_data;
00272
00273 if (s->frame.data[0])
00274 avctx->release_buffer(avctx, &s->frame);
00275
00276 return 0;
00277 }
00278
00279 AVCodec ff_rpza_decoder = {
00280 .name = "rpza",
00281 .type = AVMEDIA_TYPE_VIDEO,
00282 .id = CODEC_ID_RPZA,
00283 .priv_data_size = sizeof(RpzaContext),
00284 .init = rpza_decode_init,
00285 .close = rpza_decode_end,
00286 .decode = rpza_decode_frame,
00287 .capabilities = CODEC_CAP_DR1,
00288 .long_name = NULL_IF_CONFIG_SMALL("QuickTime video (RPZA)"),
00289 };