libavformat/dfa.c
Go to the documentation of this file.
00001 /*
00002  * Chronomaster DFA Format Demuxer
00003  * Copyright (c) 2011 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 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 
00026 static int dfa_probe(AVProbeData *p)
00027 {
00028     if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
00029         return 0;
00030 
00031     return AVPROBE_SCORE_MAX;
00032 }
00033 
00034 static int dfa_read_header(AVFormatContext *s,
00035                            AVFormatParameters *ap)
00036 {
00037     AVIOContext *pb = s->pb;
00038     AVStream *st;
00039     int frames;
00040     uint32_t mspf;
00041 
00042     if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
00043         av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
00044         return AVERROR_INVALIDDATA;
00045     }
00046     avio_skip(pb, 2); // unused
00047     frames = avio_rl16(pb);
00048 
00049     st = avformat_new_stream(s, NULL);
00050     if (!st)
00051         return AVERROR(ENOMEM);
00052 
00053     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00054     st->codec->codec_id   = CODEC_ID_DFA;
00055     st->codec->width      = avio_rl16(pb);
00056     st->codec->height     = avio_rl16(pb);
00057     mspf = avio_rl32(pb);
00058     if (!mspf) {
00059         av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
00060         mspf = 100;
00061     }
00062     avpriv_set_pts_info(st, 24, mspf, 1000);
00063     avio_skip(pb, 128 - 16); // padding
00064     st->duration = frames;
00065 
00066     return 0;
00067 }
00068 
00069 static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
00070 {
00071     AVIOContext *pb = s->pb;
00072     uint32_t frame_size;
00073     int ret, first = 1;
00074 
00075     if (pb->eof_reached)
00076         return AVERROR_EOF;
00077 
00078     if (av_get_packet(pb, pkt, 12) != 12)
00079         return AVERROR(EIO);
00080     while (!pb->eof_reached) {
00081         if (!first) {
00082             ret = av_append_packet(pb, pkt, 12);
00083             if (ret < 0) {
00084                 av_free_packet(pkt);
00085                 return ret;
00086             }
00087         } else
00088             first = 0;
00089         frame_size = AV_RL32(pkt->data + pkt->size - 8);
00090         if (frame_size > INT_MAX - 4) {
00091             av_log(s, AV_LOG_ERROR, "Too large chunk size: %d\n", frame_size);
00092             return AVERROR(EIO);
00093         }
00094         if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
00095             if (frame_size) {
00096                 av_log(s, AV_LOG_WARNING, "skipping %d bytes of end-of-frame marker chunk\n",
00097                        frame_size);
00098                 avio_skip(pb, frame_size);
00099             }
00100             return 0;
00101         }
00102         ret = av_append_packet(pb, pkt, frame_size);
00103         if (ret < 0) {
00104             av_free_packet(pkt);
00105             return ret;
00106         }
00107     }
00108 
00109     return 0;
00110 }
00111 
00112 AVInputFormat ff_dfa_demuxer = {
00113     .name           = "dfa",
00114     .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
00115     .read_probe     = dfa_probe,
00116     .read_header    = dfa_read_header,
00117     .read_packet    = dfa_read_packet,
00118     .flags = AVFMT_GENERIC_INDEX,
00119 };