00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "libavutil/intreadwrite.h"
00026 #include "avformat.h"
00027 #include "internal.h"
00028
00029 typedef struct yop_dec_context {
00030 AVPacket video_packet;
00031
00032 int odd_frame;
00033 int frame_size;
00034 int audio_block_length;
00035 int palette_size;
00036 } YopDecContext;
00037
00038 static int yop_probe(AVProbeData *probe_packet)
00039 {
00040 if (AV_RB16(probe_packet->buf) == AV_RB16("YO") &&
00041 probe_packet->buf[2]<10 &&
00042 probe_packet->buf[3]<10 &&
00043 probe_packet->buf[6] &&
00044 probe_packet->buf[7] &&
00045 !(probe_packet->buf[8] & 1) &&
00046 !(probe_packet->buf[10] & 1) &&
00047 AV_RL16(probe_packet->buf + 12 + 6) >= 920 &&
00048 AV_RL16(probe_packet->buf + 12 + 6) < probe_packet->buf[12] * 3 + 4 + probe_packet->buf[7] * 2048
00049 )
00050 return AVPROBE_SCORE_MAX * 3 / 4;
00051
00052 return 0;
00053 }
00054
00055 static int yop_read_header(AVFormatContext *s)
00056 {
00057 YopDecContext *yop = s->priv_data;
00058 AVIOContext *pb = s->pb;
00059
00060 AVCodecContext *audio_dec, *video_dec;
00061 AVStream *audio_stream, *video_stream;
00062
00063 int frame_rate, ret;
00064
00065 audio_stream = avformat_new_stream(s, NULL);
00066 video_stream = avformat_new_stream(s, NULL);
00067
00068
00069 video_stream->codec->extradata_size = 8;
00070
00071 video_stream->codec->extradata = av_mallocz(video_stream->codec->extradata_size +
00072 FF_INPUT_BUFFER_PADDING_SIZE);
00073
00074 if (!video_stream->codec->extradata)
00075 return AVERROR(ENOMEM);
00076
00077
00078 audio_dec = audio_stream->codec;
00079 audio_dec->codec_type = AVMEDIA_TYPE_AUDIO;
00080 audio_dec->codec_id = AV_CODEC_ID_ADPCM_IMA_APC;
00081 audio_dec->channels = 1;
00082 audio_dec->sample_rate = 22050;
00083
00084
00085 video_dec = video_stream->codec;
00086 video_dec->codec_type = AVMEDIA_TYPE_VIDEO;
00087 video_dec->codec_id = AV_CODEC_ID_YOP;
00088
00089 avio_skip(pb, 6);
00090
00091 frame_rate = avio_r8(pb);
00092 yop->frame_size = avio_r8(pb) * 2048;
00093 video_dec->width = avio_rl16(pb);
00094 video_dec->height = avio_rl16(pb);
00095
00096 video_stream->sample_aspect_ratio = (AVRational){1, 2};
00097
00098 ret = avio_read(pb, video_dec->extradata, 8);
00099 if (ret < 8)
00100 return ret < 0 ? ret : AVERROR_EOF;
00101
00102 yop->palette_size = video_dec->extradata[0] * 3 + 4;
00103 yop->audio_block_length = AV_RL16(video_dec->extradata + 6);
00104
00105
00106 if (yop->audio_block_length < 920 ||
00107 yop->audio_block_length + yop->palette_size >= yop->frame_size) {
00108 av_log(s, AV_LOG_ERROR, "YOP has invalid header\n");
00109 return AVERROR_INVALIDDATA;
00110 }
00111
00112 avio_seek(pb, 2048, SEEK_SET);
00113
00114 avpriv_set_pts_info(video_stream, 32, 1, frame_rate);
00115
00116 return 0;
00117 }
00118
00119 static int yop_read_packet(AVFormatContext *s, AVPacket *pkt)
00120 {
00121 YopDecContext *yop = s->priv_data;
00122 AVIOContext *pb = s->pb;
00123
00124 int ret;
00125 int actual_video_data_size = yop->frame_size -
00126 yop->audio_block_length - yop->palette_size;
00127
00128 yop->video_packet.stream_index = 1;
00129
00130 if (yop->video_packet.data) {
00131 *pkt = yop->video_packet;
00132 yop->video_packet.data = NULL;
00133 yop->video_packet.size = 0;
00134 pkt->data[0] = yop->odd_frame;
00135 pkt->flags |= AV_PKT_FLAG_KEY;
00136 yop->odd_frame ^= 1;
00137 return pkt->size;
00138 }
00139 ret = av_new_packet(&yop->video_packet,
00140 yop->frame_size - yop->audio_block_length);
00141 if (ret < 0)
00142 return ret;
00143
00144 yop->video_packet.pos = avio_tell(pb);
00145
00146 ret = avio_read(pb, yop->video_packet.data, yop->palette_size);
00147 if (ret < 0) {
00148 goto err_out;
00149 }else if (ret < yop->palette_size) {
00150 ret = AVERROR_EOF;
00151 goto err_out;
00152 }
00153
00154 ret = av_get_packet(pb, pkt, 920);
00155 if (ret < 0)
00156 goto err_out;
00157
00158
00159 pkt->pos = yop->video_packet.pos;
00160
00161 avio_skip(pb, yop->audio_block_length - ret);
00162
00163 ret = avio_read(pb, yop->video_packet.data + yop->palette_size,
00164 actual_video_data_size);
00165 if (ret < 0)
00166 goto err_out;
00167 else if (ret < actual_video_data_size)
00168 av_shrink_packet(&yop->video_packet, yop->palette_size + ret);
00169
00170
00171 return yop->audio_block_length;
00172
00173 err_out:
00174 av_free_packet(&yop->video_packet);
00175 return ret;
00176 }
00177
00178 static int yop_read_close(AVFormatContext *s)
00179 {
00180 YopDecContext *yop = s->priv_data;
00181 av_free_packet(&yop->video_packet);
00182 return 0;
00183 }
00184
00185 static int yop_read_seek(AVFormatContext *s, int stream_index,
00186 int64_t timestamp, int flags)
00187 {
00188 YopDecContext *yop = s->priv_data;
00189 int64_t frame_pos, pos_min, pos_max;
00190 int frame_count;
00191
00192 if (!stream_index)
00193 return -1;
00194
00195 pos_min = s->data_offset;
00196 pos_max = avio_size(s->pb) - yop->frame_size;
00197 frame_count = (pos_max - pos_min) / yop->frame_size;
00198
00199 timestamp = FFMAX(0, FFMIN(frame_count, timestamp));
00200
00201 frame_pos = timestamp * yop->frame_size + pos_min;
00202
00203 if (avio_seek(s->pb, frame_pos, SEEK_SET) < 0)
00204 return -1;
00205
00206 av_free_packet(&yop->video_packet);
00207 yop->odd_frame = timestamp & 1;
00208
00209 return 0;
00210 }
00211
00212 AVInputFormat ff_yop_demuxer = {
00213 .name = "yop",
00214 .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP"),
00215 .priv_data_size = sizeof(YopDecContext),
00216 .read_probe = yop_probe,
00217 .read_header = yop_read_header,
00218 .read_packet = yop_read_packet,
00219 .read_close = yop_read_close,
00220 .read_seek = yop_read_seek,
00221 .extensions = "yop",
00222 .flags = AVFMT_GENERIC_INDEX,
00223 };