configure: Drop unused helper function test_cflags_cpp()
[libav.git] / libavformat / dfa.c
CommitLineData
42315dab
KS
1/*
2 * Chronomaster DFA Format Demuxer
3 * Copyright (c) 2011 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
d92024f1
DB
22#include <inttypes.h>
23
42315dab
KS
24#include "libavutil/intreadwrite.h"
25#include "avformat.h"
c3f9ebf7 26#include "internal.h"
42315dab
KS
27
28static int dfa_probe(AVProbeData *p)
29{
30 if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
31 return 0;
32
33 return AVPROBE_SCORE_MAX;
34}
35
6e9651d1 36static int dfa_read_header(AVFormatContext *s)
42315dab
KS
37{
38 AVIOContext *pb = s->pb;
39 AVStream *st;
40 int frames;
41 uint32_t mspf;
42
43 if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
44 av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
45 return AVERROR_INVALIDDATA;
46 }
47 avio_skip(pb, 2); // unused
48 frames = avio_rl16(pb);
49
3b3bbdd3 50 st = avformat_new_stream(s, NULL);
42315dab
KS
51 if (!st)
52 return AVERROR(ENOMEM);
53
9200514a
AK
54 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
55 st->codecpar->codec_id = AV_CODEC_ID_DFA;
56 st->codecpar->width = avio_rl16(pb);
57 st->codecpar->height = avio_rl16(pb);
42315dab
KS
58 mspf = avio_rl32(pb);
59 if (!mspf) {
60 av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
61 mspf = 100;
62 }
c3f9ebf7 63 avpriv_set_pts_info(st, 24, mspf, 1000);
42315dab
KS
64 avio_skip(pb, 128 - 16); // padding
65 st->duration = frames;
66
67 return 0;
68}
69
70static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
71{
72 AVIOContext *pb = s->pb;
73 uint32_t frame_size;
74 int ret, first = 1;
75
76 if (pb->eof_reached)
77 return AVERROR_EOF;
78
79 if (av_get_packet(pb, pkt, 12) != 12)
80 return AVERROR(EIO);
81 while (!pb->eof_reached) {
82 if (!first) {
83 ret = av_append_packet(pb, pkt, 12);
84 if (ret < 0) {
ce70f28a 85 av_packet_unref(pkt);
42315dab
KS
86 return ret;
87 }
88 } else
89 first = 0;
90 frame_size = AV_RL32(pkt->data + pkt->size - 8);
91 if (frame_size > INT_MAX - 4) {
d92024f1 92 av_log(s, AV_LOG_ERROR, "Too large chunk size: %"PRIu32"\n", frame_size);
42315dab
KS
93 return AVERROR(EIO);
94 }
95 if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
96 if (frame_size) {
d92024f1
DB
97 av_log(s, AV_LOG_WARNING,
98 "skipping %"PRIu32" bytes of end-of-frame marker chunk\n",
42315dab
KS
99 frame_size);
100 avio_skip(pb, frame_size);
101 }
102 return 0;
103 }
104 ret = av_append_packet(pb, pkt, frame_size);
105 if (ret < 0) {
ce70f28a 106 av_packet_unref(pkt);
42315dab
KS
107 return ret;
108 }
109 }
110
111 return 0;
112}
113
114AVInputFormat ff_dfa_demuxer = {
dfc2c4d9
AK
115 .name = "dfa",
116 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
117 .read_probe = dfa_probe,
118 .read_header = dfa_read_header,
119 .read_packet = dfa_read_packet,
20234a4b 120 .flags = AVFMT_GENERIC_INDEX,
42315dab 121};