Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Interplay C93 demuxer | ||
3 | * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com> | ||
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 "avformat.h" | ||
23 | #include "demux.h" | ||
24 | #include "internal.h" | ||
25 | #include "voc.h" | ||
26 | #include "libavutil/intreadwrite.h" | ||
27 | |||
28 | typedef struct C93BlockRecord { | ||
29 | uint16_t index; | ||
30 | uint8_t length; | ||
31 | uint8_t frames; | ||
32 | } C93BlockRecord; | ||
33 | |||
34 | typedef struct C93DemuxContext { | ||
35 | VocDecContext voc; | ||
36 | |||
37 | C93BlockRecord block_records[512]; | ||
38 | int current_block; | ||
39 | |||
40 | uint32_t frame_offsets[32]; | ||
41 | int current_frame; | ||
42 | int next_pkt_is_audio; | ||
43 | |||
44 | AVStream *audio; | ||
45 | } C93DemuxContext; | ||
46 | |||
47 | 7186 | static int probe(const AVProbeData *p) | |
48 | { | ||
49 | int i; | ||
50 | 7186 | int index = 1; | |
51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7186 times.
|
7186 | if (p->buf_size < 16) |
52 | ✗ | return 0; | |
53 |
2/2✓ Branch 0 taken 7190 times.
✓ Branch 1 taken 1 times.
|
7191 | for (i = 0; i < 16; i += 4) { |
54 |
5/6✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7175 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
|
7190 | if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3]) |
55 | 7185 | return 0; | |
56 | 5 | index += p->buf[i + 2]; | |
57 | } | ||
58 | 1 | return AVPROBE_SCORE_MAX; | |
59 | } | ||
60 | |||
61 | 1 | static int read_header(AVFormatContext *s) | |
62 | { | ||
63 | AVStream *video; | ||
64 | 1 | AVIOContext *pb = s->pb; | |
65 | 1 | C93DemuxContext *c93 = s->priv_data; | |
66 | int i; | ||
67 | 1 | int framecount = 0; | |
68 | |||
69 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 1 times.
|
513 | for (i = 0; i < 512; i++) { |
70 | 512 | c93->block_records[i].index = avio_rl16(pb); | |
71 | 512 | c93->block_records[i].length = avio_r8(pb); | |
72 | 512 | c93->block_records[i].frames = avio_r8(pb); | |
73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
|
512 | if (c93->block_records[i].frames > 32) { |
74 | ✗ | av_log(s, AV_LOG_ERROR, "too many frames in block\n"); | |
75 | ✗ | return AVERROR_INVALIDDATA; | |
76 | } | ||
77 | 512 | framecount += c93->block_records[i].frames; | |
78 | } | ||
79 | |||
80 | /* Audio streams are added if audio packets are found */ | ||
81 | 1 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
82 | |||
83 | 1 | video = avformat_new_stream(s, NULL); | |
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!video) |
85 | ✗ | return AVERROR(ENOMEM); | |
86 | |||
87 | 1 | video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
88 | 1 | video->codecpar->codec_id = AV_CODEC_ID_C93; | |
89 | 1 | video->codecpar->width = 320; | |
90 | 1 | video->codecpar->height = 192; | |
91 | /* 4:3 320x200 with 8 empty lines */ | ||
92 | 1 | video->sample_aspect_ratio = (AVRational) { 5, 6 }; | |
93 | 1 | avpriv_set_pts_info(video, 64, 2, 25); | |
94 | 1 | video->nb_frames = framecount; | |
95 | 1 | video->duration = framecount; | |
96 | 1 | video->start_time = 0; | |
97 | |||
98 | 1 | c93->current_block = 0; | |
99 | 1 | c93->current_frame = 0; | |
100 | 1 | c93->next_pkt_is_audio = 0; | |
101 | 1 | return 0; | |
102 | } | ||
103 | |||
104 | #define C93_HAS_PALETTE 0x01 | ||
105 | #define C93_FIRST_FRAME 0x02 | ||
106 | |||
107 | 157 | static int read_packet(AVFormatContext *s, AVPacket *pkt) | |
108 | { | ||
109 | 157 | AVIOContext *pb = s->pb; | |
110 | 157 | C93DemuxContext *c93 = s->priv_data; | |
111 | 157 | C93BlockRecord *br = &c93->block_records[c93->current_block]; | |
112 | int datasize; | ||
113 | int ret, i; | ||
114 | |||
115 |
2/2✓ Branch 0 taken 143 times.
✓ Branch 1 taken 14 times.
|
157 | if (c93->next_pkt_is_audio) { |
116 | 143 | c93->current_frame++; | |
117 | 143 | c93->next_pkt_is_audio = 0; | |
118 | 143 | datasize = avio_rl16(pb); | |
119 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 129 times.
|
143 | if (datasize > 42) { |
120 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
|
14 | if (!c93->audio) { |
121 | 1 | c93->audio = avformat_new_stream(s, NULL); | |
122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!c93->audio) |
123 | ✗ | return AVERROR(ENOMEM); | |
124 | 1 | c93->audio->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
125 | } | ||
126 | 14 | avio_skip(pb, 26); /* VOC header */ | |
127 | 14 | ret = ff_voc_get_packet(s, pkt, c93->audio, datasize - 26); | |
128 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (ret > 0) { |
129 | 14 | pkt->stream_index = 1; | |
130 | 14 | pkt->flags |= AV_PKT_FLAG_KEY; | |
131 | 14 | return ret; | |
132 | } | ||
133 | } | ||
134 | } | ||
135 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 130 times.
|
143 | if (c93->current_frame >= br->frames) { |
136 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
|
13 | if (c93->current_block >= 511 || !br[1].length) |
137 | ✗ | return AVERROR_EOF; | |
138 | 13 | br++; | |
139 | 13 | c93->current_block++; | |
140 | 13 | c93->current_frame = 0; | |
141 | } | ||
142 | |||
143 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 129 times.
|
143 | if (c93->current_frame == 0) { |
144 | 14 | avio_seek(pb, br->index * 2048, SEEK_SET); | |
145 |
2/2✓ Branch 0 taken 448 times.
✓ Branch 1 taken 14 times.
|
462 | for (i = 0; i < 32; i++) { |
146 | 448 | c93->frame_offsets[i] = avio_rl32(pb); | |
147 | } | ||
148 | } | ||
149 | |||
150 | 143 | avio_seek(pb,br->index * 2048 + | |
151 | 143 | c93->frame_offsets[c93->current_frame], SEEK_SET); | |
152 | 143 | datasize = avio_rl16(pb); /* video frame size */ | |
153 | |||
154 | 143 | ret = av_new_packet(pkt, datasize + 768 + 1); | |
155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
|
143 | if (ret < 0) |
156 | ✗ | return ret; | |
157 | 143 | pkt->data[0] = 0; | |
158 | 143 | pkt->size = datasize + 1; | |
159 | |||
160 | 143 | ret = avio_read(pb, pkt->data + 1, datasize); | |
161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
|
143 | if (ret < datasize) { |
162 | ✗ | return AVERROR(EIO); | |
163 | } | ||
164 | |||
165 | 143 | datasize = avio_rl16(pb); /* palette size */ | |
166 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 141 times.
|
143 | if (datasize) { |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (datasize != 768) { |
168 | ✗ | av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize); | |
169 | ✗ | return AVERROR_INVALIDDATA; | |
170 | } | ||
171 | 2 | pkt->data[0] |= C93_HAS_PALETTE; | |
172 | 2 | ret = avio_read(pb, pkt->data + pkt->size, datasize); | |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < datasize) { |
174 | ✗ | return AVERROR(EIO); | |
175 | } | ||
176 | 2 | pkt->size += 768; | |
177 | } | ||
178 | 143 | pkt->stream_index = 0; | |
179 | 143 | c93->next_pkt_is_audio = 1; | |
180 | |||
181 | /* only the first frame is guaranteed to not reference previous frames */ | ||
182 |
4/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 10 times.
|
143 | if (c93->current_block == 0 && c93->current_frame == 0) { |
183 | 1 | pkt->flags |= AV_PKT_FLAG_KEY; | |
184 | 1 | pkt->data[0] |= C93_FIRST_FRAME; | |
185 | } | ||
186 | 143 | return 0; | |
187 | } | ||
188 | |||
189 | const FFInputFormat ff_c93_demuxer = { | ||
190 | .p.name = "c93", | ||
191 | .p.long_name = NULL_IF_CONFIG_SMALL("Interplay C93"), | ||
192 | .priv_data_size = sizeof(C93DemuxContext), | ||
193 | .read_probe = probe, | ||
194 | .read_header = read_header, | ||
195 | .read_packet = read_packet, | ||
196 | }; | ||
197 |