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