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