• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/yop.c

Go to the documentation of this file.
00001 /*
00002  * Psygnosis YOP demuxer
00003  *
00004  * Copyright (C) 2010 Mohamed Naufal Basheer <naufal11@gmail.com>
00005  * derived from the code by
00006  * Copyright (C) 2009 Thomas P. Higdon <thomas.p.higdon@gmail.com>
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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[6]                         &&
00042         probe_packet->buf[7]                         &&
00043         !(probe_packet->buf[8] & 1)                  &&
00044         !(probe_packet->buf[10] & 1))
00045         return AVPROBE_SCORE_MAX * 3 / 4;
00046 
00047     return 0;
00048 }
00049 
00050 static int yop_read_header(AVFormatContext *s, AVFormatParameters *ap)
00051 {
00052     YopDecContext *yop = s->priv_data;
00053     AVIOContext *pb  = s->pb;
00054 
00055     AVCodecContext *audio_dec, *video_dec;
00056     AVStream *audio_stream, *video_stream;
00057 
00058     int frame_rate, ret;
00059 
00060     audio_stream = avformat_new_stream(s, NULL);
00061     video_stream = avformat_new_stream(s, NULL);
00062     if (!audio_stream || !video_stream)
00063         return AVERROR(ENOMEM);
00064 
00065     // Extra data that will be passed to the decoder
00066     video_stream->codec->extradata_size = 8;
00067 
00068     video_stream->codec->extradata = av_mallocz(video_stream->codec->extradata_size +
00069                                                 FF_INPUT_BUFFER_PADDING_SIZE);
00070 
00071     if (!video_stream->codec->extradata)
00072         return AVERROR(ENOMEM);
00073 
00074     // Audio
00075     audio_dec               = audio_stream->codec;
00076     audio_dec->codec_type   = AVMEDIA_TYPE_AUDIO;
00077     audio_dec->codec_id     = CODEC_ID_ADPCM_IMA_APC;
00078     audio_dec->channels     = 1;
00079     audio_dec->sample_rate  = 22050;
00080 
00081     // Video
00082     video_dec               = video_stream->codec;
00083     video_dec->codec_type   = AVMEDIA_TYPE_VIDEO;
00084     video_dec->codec_id     = CODEC_ID_YOP;
00085 
00086     avio_skip(pb, 6);
00087 
00088     frame_rate              = avio_r8(pb);
00089     yop->frame_size         = avio_r8(pb) * 2048;
00090     video_dec->width        = avio_rl16(pb);
00091     video_dec->height       = avio_rl16(pb);
00092 
00093     video_stream->sample_aspect_ratio = (AVRational){1, 2};
00094 
00095     ret = avio_read(pb, video_dec->extradata, 8);
00096     if (ret < 8)
00097         return ret < 0 ? ret : AVERROR_EOF;
00098 
00099     yop->palette_size       = video_dec->extradata[0] * 3 + 4;
00100     yop->audio_block_length = AV_RL16(video_dec->extradata + 6);
00101 
00102     // 1840 samples per frame, 1 nibble per sample; hence 1840/2 = 920
00103     if (yop->audio_block_length < 920 ||
00104         yop->audio_block_length + yop->palette_size >= yop->frame_size) {
00105         av_log(s, AV_LOG_ERROR, "YOP has invalid header\n");
00106         return AVERROR_INVALIDDATA;
00107     }
00108 
00109     avio_seek(pb, 2048, SEEK_SET);
00110 
00111     avpriv_set_pts_info(video_stream, 32, 1, frame_rate);
00112 
00113     return 0;
00114 }
00115 
00116 static int yop_read_packet(AVFormatContext *s, AVPacket *pkt)
00117 {
00118     YopDecContext *yop = s->priv_data;
00119     AVIOContext *pb  = s->pb;
00120 
00121     int ret;
00122     int actual_video_data_size = yop->frame_size -
00123                                  yop->audio_block_length - yop->palette_size;
00124 
00125     yop->video_packet.stream_index = 1;
00126 
00127     if (yop->video_packet.data) {
00128         *pkt                   =  yop->video_packet;
00129         yop->video_packet.data =  NULL;
00130         yop->video_packet.size =  0;
00131         pkt->data[0]           =  yop->odd_frame;
00132         pkt->flags             |= AV_PKT_FLAG_KEY;
00133         yop->odd_frame         ^= 1;
00134         return pkt->size;
00135     }
00136     ret = av_new_packet(&yop->video_packet,
00137                         yop->frame_size - yop->audio_block_length);
00138     if (ret < 0)
00139         return ret;
00140 
00141     yop->video_packet.pos = avio_tell(pb);
00142 
00143     ret = avio_read(pb, yop->video_packet.data, yop->palette_size);
00144     if (ret < 0) {
00145         goto err_out;
00146     }else if (ret < yop->palette_size) {
00147         ret = AVERROR_EOF;
00148         goto err_out;
00149     }
00150 
00151     ret = av_get_packet(pb, pkt, 920);
00152     if (ret < 0)
00153         goto err_out;
00154 
00155     // Set position to the start of the frame
00156     pkt->pos = yop->video_packet.pos;
00157 
00158     avio_skip(pb, yop->audio_block_length - ret);
00159 
00160     ret = avio_read(pb, yop->video_packet.data + yop->palette_size,
00161                      actual_video_data_size);
00162     if (ret < 0)
00163         goto err_out;
00164     else if (ret < actual_video_data_size)
00165         av_shrink_packet(&yop->video_packet, yop->palette_size + ret);
00166 
00167     // Arbitrarily return the audio data first
00168     return yop->audio_block_length;
00169 
00170 err_out:
00171     av_free_packet(&yop->video_packet);
00172     return ret;
00173 }
00174 
00175 static int yop_read_close(AVFormatContext *s)
00176 {
00177     YopDecContext *yop = s->priv_data;
00178     av_free_packet(&yop->video_packet);
00179     return 0;
00180 }
00181 
00182 static int yop_read_seek(AVFormatContext *s, int stream_index,
00183                          int64_t timestamp, int flags)
00184 {
00185     YopDecContext *yop = s->priv_data;
00186     int64_t frame_pos, pos_min, pos_max;
00187     int frame_count;
00188 
00189     if (!stream_index)
00190         return -1;
00191 
00192     pos_min        = s->data_offset;
00193     pos_max        = avio_size(s->pb) - yop->frame_size;
00194     frame_count    = (pos_max - pos_min) / yop->frame_size;
00195 
00196     timestamp      = FFMAX(0, FFMIN(frame_count, timestamp));
00197 
00198     frame_pos      = timestamp * yop->frame_size + pos_min;
00199 
00200     if (avio_seek(s->pb, frame_pos, SEEK_SET) < 0)
00201         return -1;
00202 
00203     av_free_packet(&yop->video_packet);
00204     yop->odd_frame = timestamp & 1;
00205 
00206     return 0;
00207 }
00208 
00209 AVInputFormat ff_yop_demuxer = {
00210     .name           = "yop",
00211     .long_name      = NULL_IF_CONFIG_SMALL("Psygnosis YOP Format"),
00212     .priv_data_size = sizeof(YopDecContext),
00213     .read_probe     = yop_probe,
00214     .read_header    = yop_read_header,
00215     .read_packet    = yop_read_packet,
00216     .read_close     = yop_read_close,
00217     .read_seek      = yop_read_seek,
00218     .extensions = "yop",
00219     .flags = AVFMT_GENERIC_INDEX,
00220 };
Generated on Fri Feb 1 2013 14:34:47 for FFmpeg by doxygen 1.7.1