00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavcodec/dsputil.h"
00024 #include "libavcodec/paf.h"
00025 #include "bytestream.h"
00026 #include "avcodec.h"
00027
00028 static const uint8_t block_sequences[16][8] =
00029 {
00030 { 0, 0, 0, 0, 0, 0, 0, 0 },
00031 { 2, 0, 0, 0, 0, 0, 0, 0 },
00032 { 5, 7, 0, 0, 0, 0, 0, 0 },
00033 { 5, 0, 0, 0, 0, 0, 0, 0 },
00034 { 6, 0, 0, 0, 0, 0, 0, 0 },
00035 { 5, 7, 5, 7, 0, 0, 0, 0 },
00036 { 5, 7, 5, 0, 0, 0, 0, 0 },
00037 { 5, 7, 6, 0, 0, 0, 0, 0 },
00038 { 5, 5, 0, 0, 0, 0, 0, 0 },
00039 { 3, 0, 0, 0, 0, 0, 0, 0 },
00040 { 6, 6, 0, 0, 0, 0, 0, 0 },
00041 { 2, 4, 0, 0, 0, 0, 0, 0 },
00042 { 2, 4, 5, 7, 0, 0, 0, 0 },
00043 { 2, 4, 5, 0, 0, 0, 0, 0 },
00044 { 2, 4, 6, 0, 0, 0, 0, 0 },
00045 { 2, 4, 5, 7, 5, 7, 0, 0 }
00046 };
00047
00048 typedef struct PAFVideoDecContext {
00049 AVFrame pic;
00050 GetByteContext gb;
00051
00052 int current_frame;
00053 uint8_t *frame[4];
00054 int frame_size;
00055 int video_size;
00056
00057 uint8_t *opcodes;
00058 } PAFVideoDecContext;
00059
00060 static av_cold int paf_vid_init(AVCodecContext *avctx)
00061 {
00062 PAFVideoDecContext *c = avctx->priv_data;
00063 int i;
00064
00065 if (avctx->height & 3 || avctx->width & 3) {
00066 av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
00067 return AVERROR_INVALIDDATA;
00068 }
00069
00070 avctx->pix_fmt = PIX_FMT_PAL8;
00071
00072 avcodec_get_frame_defaults(&c->pic);
00073 c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
00074 c->video_size = avctx->height * avctx->width;
00075 for (i = 0; i < 4; i++) {
00076 c->frame[i] = av_mallocz(c->frame_size);
00077 if (!c->frame[i])
00078 return AVERROR(ENOMEM);
00079 }
00080
00081 return 0;
00082 }
00083
00084 static int get_video_page_offset(AVCodecContext *avctx, uint8_t a, uint8_t b)
00085 {
00086 int x, y;
00087
00088 x = b & 0x7F;
00089 y = ((a & 0x3F) << 1) | (b >> 7 & 1);
00090
00091 return y * 2 * avctx->width + x * 2;
00092 }
00093
00094 static void copy4h(AVCodecContext *avctx, uint8_t *dst)
00095 {
00096 PAFVideoDecContext *c = avctx->priv_data;
00097 int i;
00098
00099 for (i = 0; i < 4; i++) {
00100 bytestream2_get_buffer(&c->gb, dst, 4);
00101 dst += avctx->width;
00102 }
00103 }
00104
00105 static void copy_color_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, uint8_t color)
00106 {
00107 int i;
00108
00109 for (i = 0; i < 4; i++) {
00110 if ((mask >> 4) & (1 << (3 - i)))
00111 dst[i] = color;
00112 if ((mask & 15) & (1 << (3 - i)))
00113 dst[avctx->width + i] = color;
00114 }
00115 }
00116
00117 static void copy_src_mask(AVCodecContext *avctx, uint8_t mask, uint8_t *dst, const uint8_t *src)
00118 {
00119 int i;
00120
00121 for (i = 0; i < 4; i++) {
00122 if ((mask >> 4) & (1 << (3 - i)))
00123 dst[i] = src[i];
00124 if ((mask & 15) & (1 << (3 - i)))
00125 dst[avctx->width + i] = src[avctx->width + i];
00126 }
00127 }
00128
00129 static int decode_0(AVCodecContext *avctx, uint8_t code, uint8_t *pkt)
00130 {
00131 PAFVideoDecContext *c = avctx->priv_data;
00132 uint32_t opcode_size, offset;
00133 uint8_t *dst, *dend, mask = 0, color = 0, a, b, p;
00134 const uint8_t *src, *send, *opcodes;
00135 int i, j, x = 0;
00136
00137 i = bytestream2_get_byte(&c->gb);
00138 if (i) {
00139 if (code & 0x10) {
00140 int align;
00141
00142 align = bytestream2_tell(&c->gb) & 3;
00143 if (align)
00144 bytestream2_skip(&c->gb, 4 - align);
00145 }
00146 do {
00147 a = bytestream2_get_byte(&c->gb);
00148 b = bytestream2_get_byte(&c->gb);
00149 p = (a & 0xC0) >> 6;
00150 dst = c->frame[p] + get_video_page_offset(avctx, a, b);
00151 dend = c->frame[p] + c->frame_size;
00152 offset = (b & 0x7F) * 2;
00153 j = bytestream2_get_le16(&c->gb) + offset;
00154
00155 do {
00156 offset++;
00157 if (dst + 3 * avctx->width + 4 > dend)
00158 return AVERROR_INVALIDDATA;
00159 copy4h(avctx, dst);
00160 if ((offset & 0x3F) == 0)
00161 dst += avctx->width * 3;
00162 dst += 4;
00163 } while (offset < j);
00164 } while (--i);
00165 }
00166
00167 dst = c->frame[c->current_frame];
00168 dend = c->frame[c->current_frame] + c->frame_size;
00169 do {
00170 a = bytestream2_get_byte(&c->gb);
00171 b = bytestream2_get_byte(&c->gb);
00172 p = (a & 0xC0) >> 6;
00173 src = c->frame[p] + get_video_page_offset(avctx, a, b);
00174 send = c->frame[p] + c->frame_size;
00175 if ((src + 3 * avctx->width + 4 > send) ||
00176 (dst + 3 * avctx->width + 4 > dend))
00177 return AVERROR_INVALIDDATA;
00178 copy_block4(dst, src, avctx->width, avctx->width, 4);
00179 i++;
00180 if ((i & 0x3F) == 0)
00181 dst += avctx->width * 3;
00182 dst += 4;
00183 } while (i < c->video_size / 16);
00184
00185 opcode_size = bytestream2_get_le16(&c->gb);
00186 bytestream2_skip(&c->gb, 2);
00187
00188 if (bytestream2_get_bytes_left(&c->gb) < opcode_size)
00189 return AVERROR_INVALIDDATA;
00190
00191 opcodes = pkt + bytestream2_tell(&c->gb);
00192 bytestream2_skipu(&c->gb, opcode_size);
00193
00194 dst = c->frame[c->current_frame];
00195
00196 for (i = 0; i < avctx->height; i += 4, dst += avctx->width * 3) {
00197 for (j = 0; j < avctx->width; j += 4, dst += 4) {
00198 int opcode, k = 0;
00199
00200 if (x > opcode_size)
00201 return AVERROR_INVALIDDATA;
00202 if (j & 4) {
00203 opcode = opcodes[x] & 15;
00204 x++;
00205 } else {
00206 opcode = opcodes[x] >> 4;
00207 }
00208
00209 while (block_sequences[opcode][k]) {
00210
00211 offset = avctx->width * 2;
00212 code = block_sequences[opcode][k++];
00213
00214 switch (code) {
00215 case 2:
00216 offset = 0;
00217 case 3:
00218 color = bytestream2_get_byte(&c->gb);
00219 case 4:
00220 mask = bytestream2_get_byte(&c->gb);
00221 copy_color_mask(avctx, mask, dst + offset, color);
00222 break;
00223 case 5:
00224 offset = 0;
00225 case 6:
00226 a = bytestream2_get_byte(&c->gb);
00227 b = bytestream2_get_byte(&c->gb);
00228 p = (a & 0xC0) >> 6;
00229 src = c->frame[p] + get_video_page_offset(avctx, a, b);
00230 send = c->frame[p] + c->frame_size;
00231 case 7:
00232 if (src + offset + avctx->width + 4 > send)
00233 return AVERROR_INVALIDDATA;
00234 mask = bytestream2_get_byte(&c->gb);
00235 copy_src_mask(avctx, mask, dst + offset, src + offset);
00236 break;
00237 }
00238 }
00239 }
00240 }
00241
00242 return 0;
00243 }
00244
00245 static int paf_vid_decode(AVCodecContext *avctx, void *data,
00246 int *data_size, AVPacket *pkt)
00247 {
00248 PAFVideoDecContext *c = avctx->priv_data;
00249 uint8_t code, *dst, *src, *end;
00250 int i, frame, ret;
00251
00252 c->pic.reference = 3;
00253 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
00254 return ret;
00255
00256 bytestream2_init(&c->gb, pkt->data, pkt->size);
00257
00258 code = bytestream2_get_byte(&c->gb);
00259 if (code & 0x20) {
00260 for (i = 0; i < 4; i++)
00261 memset(c->frame[i], 0, c->frame_size);
00262
00263 memset(c->pic.data[1], 0, AVPALETTE_SIZE);
00264 c->current_frame = 0;
00265 c->pic.key_frame = 1;
00266 c->pic.pict_type = AV_PICTURE_TYPE_I;
00267 } else {
00268 c->pic.key_frame = 0;
00269 c->pic.pict_type = AV_PICTURE_TYPE_P;
00270 }
00271
00272 if (code & 0x40) {
00273 uint32_t *out = (uint32_t *)c->pic.data[1];
00274 int index, count;
00275
00276 index = bytestream2_get_byte(&c->gb);
00277 count = bytestream2_get_byte(&c->gb) + 1;
00278
00279 if (index + count > 256)
00280 return AVERROR_INVALIDDATA;
00281 if (bytestream2_get_bytes_left(&c->gb) < 3*count)
00282 return AVERROR_INVALIDDATA;
00283
00284 out += index;
00285 for (i = 0; i < count; i++) {
00286 unsigned r, g, b;
00287
00288 r = bytestream2_get_byteu(&c->gb);
00289 r = r << 2 | r >> 4;
00290 g = bytestream2_get_byteu(&c->gb);
00291 g = g << 2 | g >> 4;
00292 b = bytestream2_get_byteu(&c->gb);
00293 b = b << 2 | b >> 4;
00294 *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
00295 }
00296 c->pic.palette_has_changed = 1;
00297 }
00298
00299 switch (code & 0x0F) {
00300 case 0:
00301 if ((ret = decode_0(avctx, code, pkt->data)) < 0)
00302 return ret;
00303 break;
00304 case 1:
00305 dst = c->frame[c->current_frame];
00306 bytestream2_skip(&c->gb, 2);
00307 if (bytestream2_get_bytes_left(&c->gb) < c->video_size)
00308 return AVERROR_INVALIDDATA;
00309 bytestream2_get_bufferu(&c->gb, dst, c->video_size);
00310 break;
00311 case 2:
00312 frame = bytestream2_get_byte(&c->gb);
00313 if (frame > 3)
00314 return AVERROR_INVALIDDATA;
00315 if (frame != c->current_frame)
00316 memcpy(c->frame[c->current_frame], c->frame[frame], c->frame_size);
00317 break;
00318 case 4:
00319 dst = c->frame[c->current_frame];
00320 end = dst + c->video_size;
00321
00322 bytestream2_skip(&c->gb, 2);
00323
00324 while (dst < end) {
00325 int8_t code;
00326 int count;
00327
00328 if (bytestream2_get_bytes_left(&c->gb) < 2)
00329 return AVERROR_INVALIDDATA;
00330
00331 code = bytestream2_get_byteu(&c->gb);
00332 count = FFABS(code) + 1;
00333
00334 if (dst + count > end)
00335 return AVERROR_INVALIDDATA;
00336 if (code < 0)
00337 memset(dst, bytestream2_get_byteu(&c->gb), count);
00338 else
00339 bytestream2_get_buffer(&c->gb, dst, count);
00340 dst += count;
00341 }
00342 break;
00343 default:
00344 av_log_ask_for_sample(avctx, "unknown/invalid code\n");
00345 return AVERROR_INVALIDDATA;
00346 }
00347
00348 dst = c->pic.data[0];
00349 src = c->frame[c->current_frame];
00350 for (i = 0; i < avctx->height; i++) {
00351 memcpy(dst, src, avctx->width);
00352 dst += c->pic.linesize[0];
00353 src += avctx->width;
00354 }
00355
00356 c->current_frame = (c->current_frame + 1) & 3;
00357
00358 *data_size = sizeof(AVFrame);
00359 *(AVFrame *)data = c->pic;
00360
00361 return pkt->size;
00362 }
00363
00364 static av_cold int paf_vid_close(AVCodecContext *avctx)
00365 {
00366 PAFVideoDecContext *c = avctx->priv_data;
00367 int i;
00368
00369 if (c->pic.data[0])
00370 avctx->release_buffer(avctx, &c->pic);
00371
00372 for (i = 0; i < 4; i++)
00373 av_freep(&c->frame[i]);
00374
00375 return 0;
00376 }
00377
00378 typedef struct PAFAudioDecContext {
00379 AVFrame frame;
00380 } PAFAudioDecContext;
00381
00382 static av_cold int paf_aud_init(AVCodecContext *avctx)
00383 {
00384 PAFAudioDecContext *c = avctx->priv_data;
00385
00386 if (avctx->channels != 2) {
00387 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
00388 return AVERROR_INVALIDDATA;
00389 }
00390
00391 avcodec_get_frame_defaults(&c->frame);
00392 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
00393 avctx->coded_frame = &c->frame;
00394 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00395
00396 return 0;
00397 }
00398
00399 static int paf_aud_decode(AVCodecContext *avctx, void *data,
00400 int *got_frame_ptr, AVPacket *pkt)
00401 {
00402 PAFAudioDecContext *c = avctx->priv_data;
00403 uint8_t *buf = pkt->data;
00404 int16_t *output_samples;
00405 const uint8_t *t;
00406 int frames, ret, i, j, k;
00407
00408 frames = pkt->size / PAF_SOUND_FRAME_SIZE;
00409 if (frames < 1)
00410 return AVERROR_INVALIDDATA;
00411
00412 c->frame.nb_samples = PAF_SOUND_SAMPLES * frames;
00413 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0)
00414 return ret;
00415
00416 output_samples = (int16_t *)c->frame.data[0];
00417 for (i = 0; i < frames; i++) {
00418 t = buf + 256 * sizeof(uint16_t);
00419 for (j = 0; j < PAF_SOUND_SAMPLES; j++) {
00420 for (k = 0; k < 2; k++) {
00421 *output_samples++ = AV_RL16(buf + *t * 2);
00422 t++;
00423 }
00424 }
00425 buf += PAF_SOUND_FRAME_SIZE;
00426 }
00427
00428 *got_frame_ptr = 1;
00429 *(AVFrame *)data = c->frame;
00430
00431 return pkt->size;
00432 }
00433
00434 AVCodec ff_paf_video_decoder = {
00435 .name = "paf_video",
00436 .type = AVMEDIA_TYPE_VIDEO,
00437 .id = AV_CODEC_ID_PAF_VIDEO,
00438 .priv_data_size = sizeof(PAFVideoDecContext),
00439 .init = paf_vid_init,
00440 .close = paf_vid_close,
00441 .decode = paf_vid_decode,
00442 .capabilities = CODEC_CAP_DR1,
00443 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Video"),
00444 };
00445
00446 AVCodec ff_paf_audio_decoder = {
00447 .name = "paf_audio",
00448 .type = AVMEDIA_TYPE_AUDIO,
00449 .id = AV_CODEC_ID_PAF_AUDIO,
00450 .priv_data_size = sizeof(PAFAudioDecContext),
00451 .init = paf_aud_init,
00452 .decode = paf_aud_decode,
00453 .capabilities = CODEC_CAP_DR1,
00454 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File Audio"),
00455 };