libavformat/vc1test.c
Go to the documentation of this file.
00001 /*
00002  * VC1 Test Bitstreams Format Demuxer
00003  * Copyright (c) 2006, 2008 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00029 #include "libavutil/intreadwrite.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032 
00033 #define VC1_EXTRADATA_SIZE 4
00034 
00035 static int vc1t_probe(AVProbeData *p)
00036 {
00037     if (p->buf_size < 24)
00038         return 0;
00039     if (p->buf[3] != 0xC5 || AV_RL32(&p->buf[4]) != 4 || AV_RL32(&p->buf[20]) != 0xC)
00040         return 0;
00041 
00042     return AVPROBE_SCORE_MAX/2;
00043 }
00044 
00045 static int vc1t_read_header(AVFormatContext *s,
00046                            AVFormatParameters *ap)
00047 {
00048     AVIOContext *pb = s->pb;
00049     AVStream *st;
00050     int frames;
00051     uint32_t fps;
00052 
00053     frames = avio_rl24(pb);
00054     if(avio_r8(pb) != 0xC5 || avio_rl32(pb) != 4)
00055         return -1;
00056 
00057     /* init video codec */
00058     st = avformat_new_stream(s, NULL);
00059     if (!st)
00060         return -1;
00061 
00062     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00063     st->codec->codec_id = CODEC_ID_WMV3;
00064 
00065     st->codec->extradata = av_malloc(VC1_EXTRADATA_SIZE);
00066     st->codec->extradata_size = VC1_EXTRADATA_SIZE;
00067     avio_read(pb, st->codec->extradata, VC1_EXTRADATA_SIZE);
00068     st->codec->height = avio_rl32(pb);
00069     st->codec->width = avio_rl32(pb);
00070     if(avio_rl32(pb) != 0xC)
00071         return -1;
00072     avio_skip(pb, 8);
00073     fps = avio_rl32(pb);
00074     if(fps == 0xFFFFFFFF)
00075         avpriv_set_pts_info(st, 32, 1, 1000);
00076     else{
00077         if (!fps) {
00078             av_log(s, AV_LOG_ERROR, "Zero FPS specified, defaulting to 1 FPS\n");
00079             fps = 1;
00080         }
00081         avpriv_set_pts_info(st, 24, 1, fps);
00082         st->duration = frames;
00083     }
00084 
00085     return 0;
00086 }
00087 
00088 static int vc1t_read_packet(AVFormatContext *s,
00089                            AVPacket *pkt)
00090 {
00091     AVIOContext *pb = s->pb;
00092     int frame_size;
00093     int keyframe = 0;
00094     uint32_t pts;
00095 
00096     if(pb->eof_reached)
00097         return AVERROR(EIO);
00098 
00099     frame_size = avio_rl24(pb);
00100     if(avio_r8(pb) & 0x80)
00101         keyframe = 1;
00102     pts = avio_rl32(pb);
00103     if(av_get_packet(pb, pkt, frame_size) < 0)
00104         return AVERROR(EIO);
00105     if(s->streams[0]->time_base.den == 1000)
00106         pkt->pts = pts;
00107     pkt->flags |= keyframe ? AV_PKT_FLAG_KEY : 0;
00108     pkt->pos -= 8;
00109 
00110     return pkt->size;
00111 }
00112 
00113 AVInputFormat ff_vc1t_demuxer = {
00114     .name           = "vc1test",
00115     .long_name      = NULL_IF_CONFIG_SMALL("VC-1 test bitstream format"),
00116     .read_probe     = vc1t_probe,
00117     .read_header    = vc1t_read_header,
00118     .read_packet    = vc1t_read_packet,
00119     .flags = AVFMT_GENERIC_INDEX,
00120 };