]> git.sesse.net Git - ffmpeg/blob - libavformat/dfa.c
Merge commit '95fd52c11bff1aad93a29aed3bd5472bd2981d1f'
[ffmpeg] / libavformat / dfa.c
1 /*
2  * Chronomaster DFA Format Demuxer
3  * Copyright (c) 2011 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25
26 static int dfa_probe(AVProbeData *p)
27 {
28     if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
29         return 0;
30
31     if (AV_RL32(p->buf + 16) != 0x80)
32         return AVPROBE_SCORE_MAX / 4;
33
34     return AVPROBE_SCORE_MAX;
35 }
36
37 static int dfa_read_header(AVFormatContext *s)
38 {
39     AVIOContext *pb = s->pb;
40     AVStream *st;
41     int frames;
42     int version;
43     uint32_t mspf;
44
45     if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
46         av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
47         return AVERROR_INVALIDDATA;
48     }
49
50     version = avio_rl16(pb);
51     frames = avio_rl16(pb);
52
53     st = avformat_new_stream(s, NULL);
54     if (!st)
55         return AVERROR(ENOMEM);
56
57     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
58     st->codec->codec_id   = AV_CODEC_ID_DFA;
59     st->codec->width      = avio_rl16(pb);
60     st->codec->height     = avio_rl16(pb);
61     mspf = avio_rl32(pb);
62     if (!mspf) {
63         av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
64         mspf = 100;
65     }
66     avpriv_set_pts_info(st, 24, mspf, 1000);
67     avio_skip(pb, 128 - 16); // padding
68     st->duration = frames;
69
70     if (ff_alloc_extradata(st->codec, 2))
71         return AVERROR(ENOMEM);
72     AV_WL16(st->codec->extradata, version);
73     if (version == 0x100)
74         st->sample_aspect_ratio = (AVRational){2, 1};
75
76     return 0;
77 }
78
79 static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
80 {
81     AVIOContext *pb = s->pb;
82     uint32_t frame_size;
83     int ret, first = 1;
84
85     if (pb->eof_reached)
86         return AVERROR_EOF;
87
88     if (av_get_packet(pb, pkt, 12) != 12)
89         return AVERROR(EIO);
90     while (!pb->eof_reached) {
91         if (!first) {
92             ret = av_append_packet(pb, pkt, 12);
93             if (ret < 0) {
94                 av_free_packet(pkt);
95                 return ret;
96             }
97         } else
98             first = 0;
99         frame_size = AV_RL32(pkt->data + pkt->size - 8);
100         if (frame_size > INT_MAX - 4) {
101             av_log(s, AV_LOG_ERROR, "Too large chunk size: %d\n", frame_size);
102             return AVERROR(EIO);
103         }
104         if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
105             if (frame_size) {
106                 av_log(s, AV_LOG_WARNING, "skipping %d bytes of end-of-frame marker chunk\n",
107                        frame_size);
108                 avio_skip(pb, frame_size);
109             }
110             return 0;
111         }
112         ret = av_append_packet(pb, pkt, frame_size);
113         if (ret < 0) {
114             av_free_packet(pkt);
115             return ret;
116         }
117     }
118
119     return 0;
120 }
121
122 AVInputFormat ff_dfa_demuxer = {
123     .name           = "dfa",
124     .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
125     .read_probe     = dfa_probe,
126     .read_header    = dfa_read_header,
127     .read_packet    = dfa_read_packet,
128     .flags          = AVFMT_GENERIC_INDEX,
129 };