libavformat/iv8.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Michael Niedermayer
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include "avformat.h"
00022 #include "internal.h"
00023 
00024 
00025 static int probe(AVProbeData *p)
00026 {
00027     // the single file I have starts with that, I do not know if others do, too
00028     if(   p->buf[0] == 1
00029        && p->buf[1] == 1
00030        && p->buf[2] == 3
00031        && p->buf[3] == 0xB8
00032        && p->buf[4] == 0x80
00033        && p->buf[5] == 0x60
00034       )
00035         return AVPROBE_SCORE_MAX-2;
00036 
00037     return 0;
00038 }
00039 
00040 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
00041 {
00042     AVStream *st;
00043 
00044     st = avformat_new_stream(s, NULL);
00045     if (!st)
00046         return AVERROR(ENOMEM);
00047 
00048     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00049     st->codec->codec_id = CODEC_ID_MPEG4;
00050     st->need_parsing = AVSTREAM_PARSE_FULL;
00051     avpriv_set_pts_info(st, 64, 1, 90000);
00052 
00053     return 0;
00054 
00055 }
00056 
00057 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00058 {
00059     int ret, size, pts, type, flags;
00060     int first_pkt      = 0;
00061     int frame_complete = 0;
00062 
00063     while (!frame_complete) {
00064 
00065         type  = avio_rb16(s->pb); // 257 or 258
00066         size  = avio_rb16(s->pb);
00067         flags = avio_rb16(s->pb); //some flags, 0x80 indicates end of frame
00068                 avio_rb16(s->pb); //packet number
00069         pts   = avio_rb32(s->pb);
00070                 avio_rb32(s->pb); //6A 13 E3 88
00071 
00072         frame_complete = flags & 0x80;
00073 
00074         size -= 12;
00075         if (size < 1)
00076             return -1;
00077 
00078         if (type == 258) {
00079             avio_skip(s->pb, size);
00080             frame_complete = 0;
00081             continue;
00082         }
00083 
00084         if (!first_pkt) {
00085             ret = av_get_packet(s->pb, pkt, size);
00086             if (ret < 0)
00087                 return ret;
00088             first_pkt = 1;
00089             pkt->pts  = pts;
00090             pkt->pos -= 16;
00091         } else {
00092             ret = av_append_packet(s->pb, pkt, size);
00093             if (ret < 0) {
00094                 av_log(s, AV_LOG_ERROR, "failed to grow packet\n");
00095                 av_free_packet(pkt);
00096                 return ret;
00097             }
00098         }
00099         if (ret < size) {
00100             av_log(s, AV_LOG_ERROR, "Truncated packet! Read %d of %d bytes\n",
00101                    ret, size);
00102             pkt->flags |= AV_PKT_FLAG_CORRUPT;
00103             break;
00104         }
00105     }
00106     pkt->stream_index = 0;
00107 
00108     return 0;
00109 }
00110 
00111 AVInputFormat ff_iv8_demuxer = {
00112     .name           = "iv8",
00113     .long_name      = NULL_IF_CONFIG_SMALL("A format generated by IndigoVision 8000 video server"),
00114     .read_probe     = probe,
00115     .read_header    = read_header,
00116     .read_packet    = read_packet,
00117     .flags= AVFMT_GENERIC_INDEX,
00118     .value = CODEC_ID_MPEG4,
00119 };