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