FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rl2.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 108 142 76.1%
Functions: 3 4 75.0%
Branches: 42 70 60.0%

Line Branch Exec Source
1 /*
2 * RL2 Format Demuxer
3 * Copyright (c) 2008 Sascha Sommer (saschasommer@freenet.de)
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 /**
23 * RL2 file demuxer
24 * @file
25 * @author Sascha Sommer (saschasommer@freenet.de)
26 * @see http://wiki.multimedia.cx/index.php?title=RL2
27 *
28 * extradata:
29 * 2 byte le initial drawing offset within 320x200 viewport
30 * 4 byte le number of used colors
31 * 256 * 3 bytes rgb palette
32 * optional background_frame
33 */
34
35 #include <stdint.h>
36
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/mathematics.h"
39 #include "avformat.h"
40 #include "demux.h"
41 #include "internal.h"
42
43 #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr, palette
44
45 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
46 #define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
47 #define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
48
49 typedef struct Rl2DemuxContext {
50 unsigned int index_pos[2]; ///< indexes in the sample tables
51 } Rl2DemuxContext;
52
53
54 /**
55 * check if the file is in rl2 format
56 * @param p probe buffer
57 * @return 0 when the probe buffer does not contain rl2 data, > 0 otherwise
58 */
59 6782 static int rl2_probe(const AVProbeData *p)
60 {
61
62
2/2
✓ Branch 0 taken 6761 times.
✓ Branch 1 taken 21 times.
6782 if(AV_RB32(&p->buf[0]) != FORM_TAG)
63 6761 return 0;
64
65
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
66
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 AV_RB32(&p->buf[8]) != RLV3_TAG)
67 20 return 0;
68
69 1 return AVPROBE_SCORE_MAX;
70 }
71
72 /**
73 * read rl2 header data and setup the avstreams
74 * @param s demuxer context
75 * @return 0 on success, AVERROR otherwise
76 */
77 1 static av_cold int rl2_read_header(AVFormatContext *s)
78 {
79 1 AVIOContext *pb = s->pb;
80 AVStream *st;
81 unsigned int frame_count;
82 1 unsigned int audio_frame_counter = 0;
83 1 unsigned int video_frame_counter = 0;
84 unsigned int back_size;
85 unsigned short sound_rate;
86 unsigned short rate;
87 unsigned short channels;
88 unsigned short def_sound_size;
89 unsigned int signature;
90 1 unsigned int pts_den = 11025; /* video only case */
91 1 unsigned int pts_num = 1103;
92 1 unsigned int* chunk_offset = NULL;
93 1 int* chunk_size = NULL;
94 1 int* audio_size = NULL;
95 int i;
96 1 int ret = 0;
97
98 1 avio_skip(pb,4); /* skip FORM tag */
99 1 back_size = avio_rl32(pb); /**< get size of the background frame */
100 1 signature = avio_rb32(pb);
101 1 avio_skip(pb, 4); /* data size */
102 1 frame_count = avio_rl32(pb);
103
104 /* disallow back_sizes and frame_counts that may lead to overflows later */
105
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if(back_size > INT_MAX/2 || frame_count > INT_MAX / sizeof(uint32_t))
106 return AVERROR_INVALIDDATA;
107
108 1 avio_skip(pb, 2); /* encoding method */
109 1 sound_rate = avio_rl16(pb);
110 1 rate = avio_rl16(pb);
111 1 channels = avio_rl16(pb);
112 1 def_sound_size = avio_rl16(pb);
113
114 /** setup video stream */
115 1 st = avformat_new_stream(s, NULL);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(!st)
117 return AVERROR(ENOMEM);
118
119 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
120 1 st->codecpar->codec_id = AV_CODEC_ID_RL2;
121 1 st->codecpar->codec_tag = 0; /* no fourcc */
122 1 st->codecpar->width = 320;
123 1 st->codecpar->height = 200;
124
125 /** allocate and fill extradata */
126 1 st->codecpar->extradata_size = EXTRADATA1_SIZE;
127
128
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if(signature == RLV3_TAG && back_size > 0)
129 1 st->codecpar->extradata_size += back_size;
130
131 1 ret = ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
133 return ret;
134
135 /** setup audio stream if present */
136
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(sound_rate){
137
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!channels || channels > 42) {
138 av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
139 return AVERROR_INVALIDDATA;
140 }
141
142 1 pts_num = def_sound_size;
143 1 pts_den = rate;
144
145 1 st = avformat_new_stream(s, NULL);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
147 return AVERROR(ENOMEM);
148 1 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
149 1 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
150 1 st->codecpar->codec_tag = 1;
151 1 st->codecpar->ch_layout.nb_channels = channels;
152 1 st->codecpar->bits_per_coded_sample = 8;
153 1 st->codecpar->sample_rate = rate;
154 1 st->codecpar->bit_rate = channels * st->codecpar->sample_rate *
155 1 st->codecpar->bits_per_coded_sample;
156 1 st->codecpar->block_align = channels *
157 1 st->codecpar->bits_per_coded_sample / 8;
158 1 avpriv_set_pts_info(st,32,1,rate);
159 }
160
161 1 avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
162
163 1 chunk_size = av_malloc(frame_count * sizeof(uint32_t));
164 1 audio_size = av_malloc(frame_count * sizeof(uint32_t));
165 1 chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
166
167
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if(!chunk_size || !audio_size || !chunk_offset){
168 av_free(chunk_size);
169 av_free(audio_size);
170 av_free(chunk_offset);
171 return AVERROR(ENOMEM);
172 }
173
174 /** read offset and size tables */
175
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1 times.
109 for(i=0; i < frame_count;i++) {
176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
108 if (avio_feof(pb)) {
177 ret = AVERROR_INVALIDDATA;
178 goto end;
179 }
180 108 chunk_size[i] = avio_rl32(pb);
181 }
182
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1 times.
109 for(i=0; i < frame_count;i++) {
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
108 if (avio_feof(pb)) {
184 ret = AVERROR_INVALIDDATA;
185 goto end;
186 }
187 108 chunk_offset[i] = avio_rl32(pb);
188 }
189
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1 times.
109 for(i=0; i < frame_count;i++) {
190
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
108 if (avio_feof(pb)) {
191 ret = AVERROR_INVALIDDATA;
192 goto end;
193 }
194 108 audio_size[i] = avio_rl32(pb) & 0xFFFF;
195 }
196
197 /** build the sample index */
198
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1 times.
109 for(i=0;i<frame_count;i++){
199
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
108 if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
200 ret = AVERROR_INVALIDDATA;
201 break;
202 }
203
204
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 if(sound_rate && audio_size[i]){
205 108 av_add_index_entry(s->streams[1], chunk_offset[i],
206 108 audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
207 108 audio_frame_counter += audio_size[i] / channels;
208 }
209 108 av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
210 108 video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
211 108 ++video_frame_counter;
212 }
213
214 1 end:
215 1 av_free(chunk_size);
216 1 av_free(audio_size);
217 1 av_free(chunk_offset);
218
219 1 return ret;
220 }
221
222 /**
223 * read a single audio or video packet
224 * @param s demuxer context
225 * @param pkt the packet to be filled
226 * @return 0 on success, AVERROR otherwise
227 */
228 217 static int rl2_read_packet(AVFormatContext *s,
229 AVPacket *pkt)
230 {
231 217 Rl2DemuxContext *rl2 = s->priv_data;
232 217 AVIOContext *pb = s->pb;
233 217 AVIndexEntry *sample = NULL;
234 int i;
235 217 int ret = 0;
236 217 int stream_id = -1;
237 217 int64_t pos = INT64_MAX;
238
239 /** check if there is a valid video or audio entry that can be used */
240
2/2
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 217 times.
651 for(i=0; i<s->nb_streams; i++){
241 434 const FFStream *const sti = ffstream(s->streams[i]);
242
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 3 times.
434 if (rl2->index_pos[i] < sti->nb_index_entries
243
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 107 times.
431 && sti->index_entries[ rl2->index_pos[i] ].pos < pos) {
244 324 sample = &sti->index_entries[ rl2->index_pos[i] ];
245 324 pos= sample->pos;
246 324 stream_id= i;
247 }
248 }
249
250
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 216 times.
217 if(stream_id == -1)
251 1 return AVERROR_EOF;
252
253 216 ++rl2->index_pos[stream_id];
254
255 /** position the stream (will probably be there anyway) */
256 216 avio_seek(pb, sample->pos, SEEK_SET);
257
258 /** fill the packet */
259 216 ret = av_get_packet(pb, pkt, sample->size);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if(ret != sample->size){
261 return AVERROR(EIO);
262 }
263
264 216 pkt->stream_index = stream_id;
265 216 pkt->pts = sample->timestamp;
266
267 216 return ret;
268 }
269
270 /**
271 * seek to a new timestamp
272 * @param s demuxer context
273 * @param stream_index index of the stream that should be seeked
274 * @param timestamp wanted timestamp
275 * @param flags direction and seeking mode
276 * @return 0 on success, -1 otherwise
277 */
278 static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
279 {
280 AVStream *st = s->streams[stream_index];
281 Rl2DemuxContext *rl2 = s->priv_data;
282 int i;
283 int index = av_index_search_timestamp(st, timestamp, flags);
284 if(index < 0)
285 return -1;
286
287 rl2->index_pos[stream_index] = index;
288 timestamp = ffstream(st)->index_entries[index].timestamp;
289
290 for(i=0; i < s->nb_streams; i++){
291 AVStream *st2 = s->streams[i];
292 index = av_index_search_timestamp(st2,
293 av_rescale_q(timestamp, st->time_base, st2->time_base),
294 flags | AVSEEK_FLAG_BACKWARD);
295
296 if(index < 0)
297 index = 0;
298
299 rl2->index_pos[i] = index;
300 }
301
302 return 0;
303 }
304
305 const AVInputFormat ff_rl2_demuxer = {
306 .name = "rl2",
307 .long_name = NULL_IF_CONFIG_SMALL("RL2"),
308 .priv_data_size = sizeof(Rl2DemuxContext),
309 .read_probe = rl2_probe,
310 .read_header = rl2_read_header,
311 .read_packet = rl2_read_packet,
312 .read_seek = rl2_read_seek,
313 };
314