/src/ffmpeg/libavformat/dfa.c
Line | Count | Source |
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 <inttypes.h> |
23 | | |
24 | | #include "libavutil/intreadwrite.h" |
25 | | #include "avformat.h" |
26 | | #include "demux.h" |
27 | | #include "internal.h" |
28 | | |
29 | | static int dfa_probe(const AVProbeData *p) |
30 | 942k | { |
31 | 942k | if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A')) |
32 | 942k | return 0; |
33 | | |
34 | 365 | if (AV_RL32(p->buf + 16) != 0x80) |
35 | 282 | return AVPROBE_SCORE_MAX / 4; |
36 | | |
37 | 83 | return AVPROBE_SCORE_MAX; |
38 | 365 | } |
39 | | |
40 | | static int dfa_read_header(AVFormatContext *s) |
41 | 2.16k | { |
42 | 2.16k | AVIOContext *pb = s->pb; |
43 | 2.16k | AVStream *st; |
44 | 2.16k | int frames, ret; |
45 | 2.16k | int version; |
46 | 2.16k | uint32_t mspf; |
47 | | |
48 | 2.16k | if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) { |
49 | 152 | av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n"); |
50 | 152 | return AVERROR_INVALIDDATA; |
51 | 152 | } |
52 | | |
53 | 2.01k | version = avio_rl16(pb); |
54 | 2.01k | frames = avio_rl16(pb); |
55 | | |
56 | 2.01k | st = avformat_new_stream(s, NULL); |
57 | 2.01k | if (!st) |
58 | 0 | return AVERROR(ENOMEM); |
59 | | |
60 | 2.01k | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
61 | 2.01k | st->codecpar->codec_id = AV_CODEC_ID_DFA; |
62 | 2.01k | st->codecpar->width = avio_rl16(pb); |
63 | 2.01k | st->codecpar->height = avio_rl16(pb); |
64 | 2.01k | mspf = avio_rl32(pb); |
65 | 2.01k | if (!mspf) { |
66 | 173 | av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n"); |
67 | 173 | mspf = 100; |
68 | 173 | } |
69 | 2.01k | avpriv_set_pts_info(st, 24, mspf, 1000); |
70 | 2.01k | avio_skip(pb, 128 - 16); // padding |
71 | 2.01k | st->duration = frames; |
72 | | |
73 | 2.01k | if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0) |
74 | 0 | return ret; |
75 | 2.01k | AV_WL16(st->codecpar->extradata, version); |
76 | 2.01k | if (version == 0x100) |
77 | 61 | st->sample_aspect_ratio = (AVRational){2, 1}; |
78 | | |
79 | 2.01k | return 0; |
80 | 2.01k | } |
81 | | |
82 | | static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt) |
83 | 166k | { |
84 | 166k | AVIOContext *pb = s->pb; |
85 | 166k | uint32_t frame_size; |
86 | 166k | int ret, first = 1; |
87 | | |
88 | 166k | if (avio_feof(pb)) |
89 | 2.57k | return AVERROR_EOF; |
90 | | |
91 | 163k | if (av_get_packet(pb, pkt, 12) != 12) |
92 | 546 | return AVERROR_INVALIDDATA; |
93 | 439k | while (!avio_feof(pb)) { |
94 | 439k | if (!first) { |
95 | 275k | ret = av_append_packet(pb, pkt, 12); |
96 | 275k | if (ret < 0) { |
97 | 20 | return ret; |
98 | 20 | } |
99 | 275k | } else |
100 | 163k | first = 0; |
101 | 439k | frame_size = AV_RL32(pkt->data + pkt->size - 8); |
102 | 439k | if (frame_size > INT_MAX - 4) { |
103 | 139 | av_log(s, AV_LOG_ERROR, "Too large chunk size: %"PRIu32"\n", frame_size); |
104 | 139 | return AVERROR_INVALIDDATA; |
105 | 139 | } |
106 | 438k | if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) { |
107 | 162k | if (frame_size) { |
108 | 1.78k | av_log(s, AV_LOG_WARNING, |
109 | 1.78k | "skipping %"PRIu32" bytes of end-of-frame marker chunk\n", |
110 | 1.78k | frame_size); |
111 | 1.78k | avio_skip(pb, frame_size); |
112 | 1.78k | } |
113 | 162k | return 0; |
114 | 162k | } |
115 | 276k | ret = av_append_packet(pb, pkt, frame_size); |
116 | 276k | if (ret < 0) { |
117 | 432 | return ret; |
118 | 432 | } |
119 | 276k | } |
120 | | |
121 | 440 | return 0; |
122 | 163k | } |
123 | | |
124 | | const FFInputFormat ff_dfa_demuxer = { |
125 | | .p.name = "dfa", |
126 | | .p.long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"), |
127 | | .p.flags = AVFMT_GENERIC_INDEX, |
128 | | .read_probe = dfa_probe, |
129 | | .read_header = dfa_read_header, |
130 | | .read_packet = dfa_read_packet, |
131 | | }; |