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