FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mlvdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 223 320 69.7%
Functions: 10 12 83.3%
Branches: 87 193 45.1%

Line Branch Exec Source
1 /*
2 * Magic Lantern Video (MLV) demuxer
3 * Copyright (c) 2014 Peter Ross
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 * @file
24 * Magic Lantern Video (MLV) demuxer
25 */
26
27 #include <time.h>
28
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/rational.h"
33 #include "avformat.h"
34 #include "demux.h"
35 #include "internal.h"
36 #include "riff.h"
37
38 #define MLV_VERSION "v2.0"
39
40 #define MLV_VIDEO_CLASS_RAW 1
41 #define MLV_VIDEO_CLASS_YUV 2
42 #define MLV_VIDEO_CLASS_JPEG 3
43 #define MLV_VIDEO_CLASS_H264 4
44
45 #define MLV_AUDIO_CLASS_WAV 1
46
47 #define MLV_CLASS_FLAG_DELTA 0x40
48 #define MLV_CLASS_FLAG_LZMA 0x80
49
50 typedef struct {
51 AVIOContext *pb[101];
52 int class[2];
53 int stream_index;
54 uint64_t pts;
55 } MlvContext;
56
57 7186 static int probe(const AVProbeData *p)
58 {
59
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7185 times.
7186 if (AV_RL32(p->buf) == MKTAG('M','L','V','I') &&
60
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(p->buf + 4) >= 52 &&
61
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 !memcmp(p->buf + 8, MLV_VERSION, 5))
62 1 return AVPROBE_SCORE_MAX;
63 7185 return 0;
64 }
65
66 static int check_file_header(AVIOContext *pb, uint64_t guid)
67 {
68 unsigned int size;
69 uint8_t version[8];
70
71 avio_skip(pb, 4);
72 size = avio_rl32(pb);
73 if (size < 52)
74 return AVERROR_INVALIDDATA;
75 avio_read(pb, version, 8);
76 if (memcmp(version, MLV_VERSION, 5) || avio_rl64(pb) != guid)
77 return AVERROR_INVALIDDATA;
78 avio_skip(pb, size - 24);
79 return 0;
80 }
81
82 7 static void read_string(AVFormatContext *avctx, AVIOContext *pb, const char *tag, unsigned size)
83 {
84 7 char * value = av_malloc(size + 1);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!value) {
86 avio_skip(pb, size);
87 return;
88 }
89
90 7 avio_read(pb, value, size);
91
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (!value[0]) {
92 2 av_free(value);
93 2 return;
94 }
95
96 5 value[size] = 0;
97 5 av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
98 }
99
100 4 static void read_uint8(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
101 {
102 4 av_dict_set_int(&avctx->metadata, tag, avio_r8(pb), 0);
103 4 }
104
105 6 static void read_uint16(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
106 {
107 6 av_dict_set_int(&avctx->metadata, tag, avio_rl16(pb), 0);
108 6 }
109
110 23 static void read_uint32(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
111 {
112 23 av_dict_set_int(&avctx->metadata, tag, avio_rl32(pb), 0);
113 23 }
114
115 2 static void read_uint64(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
116 {
117 2 av_dict_set_int(&avctx->metadata, tag, avio_rl64(pb), 0);
118 2 }
119
120 1 static int scan_file(AVFormatContext *avctx, AVStream *vst, AVStream *ast, int file)
121 {
122 1 FFStream *const vsti = ffstream(vst), *const asti = ffstream(ast);
123 1 MlvContext *mlv = avctx->priv_data;
124 1 AVIOContext *pb = mlv->pb[file];
125 int ret;
126
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 while (!avio_feof(pb)) {
127 int type;
128 unsigned int size;
129 13 type = avio_rl32(pb);
130 13 size = avio_rl32(pb);
131 13 avio_skip(pb, 8); //timestamp
132
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (size < 16)
133 1 break;
134 12 size -= 16;
135
4/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
12 if (vst && type == MKTAG('R','A','W','I') && size >= 164) {
136 1 unsigned width = avio_rl16(pb);
137 1 unsigned height = avio_rl16(pb);
138 unsigned bits_per_coded_sample;
139 1 ret = av_image_check_size(width, height, 0, avctx);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
141 return ret;
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_rl32(pb) != 1)
143 avpriv_request_sample(avctx, "raw api version");
144 1 avio_skip(pb, 20); // pointer, width, height, pitch, frame_size
145 1 bits_per_coded_sample = avio_rl32(pb);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (bits_per_coded_sample > (INT_MAX - 7) / (width * height)) {
147 av_log(avctx, AV_LOG_ERROR,
148 "invalid bits_per_coded_sample %u (size: %ux%u)\n",
149 bits_per_coded_sample, width, height);
150 return AVERROR_INVALIDDATA;
151 }
152 1 vst->codecpar->width = width;
153 1 vst->codecpar->height = height;
154 1 vst->codecpar->bits_per_coded_sample = bits_per_coded_sample;
155 1 avio_skip(pb, 8 + 16 + 24); // black_level, white_level, xywh, active_area, exposure_bias
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_rl32(pb) != 0x2010100) /* RGGB */
157 avpriv_request_sample(avctx, "cfa_pattern");
158 1 avio_skip(pb, 80); // calibration_illuminant1, color_matrix1, dynamic_range
159 1 vst->codecpar->format = AV_PIX_FMT_BAYER_RGGB16LE;
160 1 vst->codecpar->codec_tag = MKTAG('B', 'I', 'T', 16);
161 1 size -= 164;
162
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
11 } else if (ast && type == MKTAG('W', 'A', 'V', 'I') && size >= 16) {
163 ret = ff_get_wav_header(avctx, pb, ast->codecpar, 16, 0);
164 if (ret < 0)
165 return ret;
166 size -= 16;
167
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 } else if (type == MKTAG('I','N','F','O')) {
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size > 0)
169 read_string(avctx, pb, "info", size);
170 1 continue;
171
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
10 } else if (type == MKTAG('I','D','N','T') && size >= 36) {
172 1 read_string(avctx, pb, "cameraName", 32);
173 1 read_uint32(avctx, pb, "cameraModel", "0x%"PRIx32);
174 1 size -= 36;
175
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (size >= 32) {
176 1 read_string(avctx, pb, "cameraSerial", 32);
177 1 size -= 32;
178 }
179
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
9 } else if (type == MKTAG('L','E','N','S') && size >= 48) {
180 2 read_uint16(avctx, pb, "focalLength", "%i");
181 2 read_uint16(avctx, pb, "focalDist", "%i");
182 2 read_uint16(avctx, pb, "aperture", "%i");
183 2 read_uint8(avctx, pb, "stabilizerMode", "%i");
184 2 read_uint8(avctx, pb, "autofocusMode", "%i");
185 2 read_uint32(avctx, pb, "flags", "0x%"PRIx32);
186 2 read_uint32(avctx, pb, "lensID", "%"PRIi32);
187 2 read_string(avctx, pb, "lensName", 32);
188 2 size -= 48;
189
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (size >= 32) {
190 2 read_string(avctx, pb, "lensSerial", 32);
191 2 size -= 32;
192 }
193
4/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
7 } else if (vst && type == MKTAG('V', 'I', 'D', 'F') && size >= 4) {
194 1 uint64_t pts = avio_rl32(pb);
195 1 ff_add_index_entry(&vsti->index_entries, &vsti->nb_index_entries,
196 &vsti->index_entries_allocated_size,
197 1 avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
198 1 size -= 4;
199
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6 } else if (ast && type == MKTAG('A', 'U', 'D', 'F') && size >= 4) {
200 uint64_t pts = avio_rl32(pb);
201 ff_add_index_entry(&asti->index_entries, &asti->nb_index_entries,
202 &asti->index_entries_allocated_size,
203 avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
204 size -= 4;
205
4/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
6 } else if (vst && type == MKTAG('W','B','A','L') && size >= 28) {
206 1 read_uint32(avctx, pb, "wb_mode", "%"PRIi32);
207 1 read_uint32(avctx, pb, "kelvin", "%"PRIi32);
208 1 read_uint32(avctx, pb, "wbgain_r", "%"PRIi32);
209 1 read_uint32(avctx, pb, "wbgain_g", "%"PRIi32);
210 1 read_uint32(avctx, pb, "wbgain_b", "%"PRIi32);
211 1 read_uint32(avctx, pb, "wbs_gm", "%"PRIi32);
212 1 read_uint32(avctx, pb, "wbs_ba", "%"PRIi32);
213 1 size -= 28;
214
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
5 } else if (type == MKTAG('R','T','C','I') && size >= 20) {
215 char str[32];
216 1 struct tm time = { 0 };
217 1 time.tm_sec = avio_rl16(pb);
218 1 time.tm_min = avio_rl16(pb);
219 1 time.tm_hour = avio_rl16(pb);
220 1 time.tm_mday = avio_rl16(pb);
221 1 time.tm_mon = avio_rl16(pb);
222 1 time.tm_year = avio_rl16(pb);
223 1 time.tm_wday = avio_rl16(pb);
224 1 time.tm_yday = avio_rl16(pb);
225 1 time.tm_isdst = avio_rl16(pb);
226 1 avio_skip(pb, 2);
227
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &time))
228 1 av_dict_set(&avctx->metadata, "time", str, 0);
229 1 size -= 20;
230
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 } else if (type == MKTAG('E','X','P','O') && size >= 16) {
231
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 av_dict_set(&avctx->metadata, "isoMode", avio_rl32(pb) ? "auto" : "manual", 0);
232 2 read_uint32(avctx, pb, "isoValue", "%"PRIi32);
233 2 read_uint32(avctx, pb, "isoAnalog", "%"PRIi32);
234 2 read_uint32(avctx, pb, "digitalGain", "%"PRIi32);
235 2 size -= 16;
236
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (size >= 8) {
237 2 read_uint64(avctx, pb, "shutterValue", "%"PRIi64);
238 2 size -= 8;
239 }
240
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 } else if (type == MKTAG('S','T','Y','L') && size >= 36) {
241 1 read_uint32(avctx, pb, "picStyleId", "%"PRIi32);
242 1 read_uint32(avctx, pb, "contrast", "%"PRIi32);
243 1 read_uint32(avctx, pb, "sharpness", "%"PRIi32);
244 1 read_uint32(avctx, pb, "saturation", "%"PRIi32);
245 1 read_uint32(avctx, pb, "colortone", "%"PRIi32);
246 1 read_string(avctx, pb, "picStyleName", 16);
247 1 size -= 36;
248
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (type == MKTAG('M','A','R','K')) {
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (type == MKTAG('N','U','L','L')) {
250 } else if (type == MKTAG('M','L','V','I')) { /* occurs when MLV and Mnn files are concatenated */
251 } else {
252 av_log(avctx, AV_LOG_INFO, "unsupported tag %s, size %u\n",
253 av_fourcc2str(type), size);
254 }
255 11 avio_skip(pb, size);
256 }
257 1 return 0;
258 }
259
260 1 static int read_header(AVFormatContext *avctx)
261 {
262 1 MlvContext *mlv = avctx->priv_data;
263 1 AVIOContext *pb = avctx->pb;
264 1 AVStream *vst = NULL, *ast = NULL;
265 1 FFStream *vsti = NULL, *asti = NULL;
266 int size, ret;
267 unsigned nb_video_frames, nb_audio_frames;
268 uint64_t guid;
269 char guidstr[32];
270
271 1 avio_skip(pb, 4);
272 1 size = avio_rl32(pb);
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size < 52)
274 return AVERROR_INVALIDDATA;
275
276 1 avio_skip(pb, 8);
277
278 1 guid = avio_rl64(pb);
279 1 snprintf(guidstr, sizeof(guidstr), "0x%"PRIx64, guid);
280 1 av_dict_set(&avctx->metadata, "guid", guidstr, 0);
281
282 1 avio_skip(pb, 8); //fileNum, fileCount, fileFlags
283
284 1 mlv->class[0] = avio_rl16(pb);
285 1 mlv->class[1] = avio_rl16(pb);
286
287 1 nb_video_frames = avio_rl32(pb);
288 1 nb_audio_frames = avio_rl32(pb);
289
290
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (nb_video_frames && mlv->class[0]) {
291 1 vst = avformat_new_stream(avctx, NULL);
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!vst)
293 return AVERROR(ENOMEM);
294 1 vsti = ffstream(vst);
295
296 1 vst->id = 0;
297 1 vst->nb_frames = nb_video_frames;
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if ((mlv->class[0] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)))
299 avpriv_request_sample(avctx, "compression");
300 1 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
301
1/5
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 switch (mlv->class[0] & ~(MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)) {
302 1 case MLV_VIDEO_CLASS_RAW:
303 1 vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
304 1 break;
305 case MLV_VIDEO_CLASS_YUV:
306 vst->codecpar->format = AV_PIX_FMT_YUV420P;
307 vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
308 vst->codecpar->codec_tag = 0;
309 break;
310 case MLV_VIDEO_CLASS_JPEG:
311 vst->codecpar->codec_id = AV_CODEC_ID_MJPEG;
312 vst->codecpar->codec_tag = 0;
313 break;
314 case MLV_VIDEO_CLASS_H264:
315 vst->codecpar->codec_id = AV_CODEC_ID_H264;
316 vst->codecpar->codec_tag = 0;
317 break;
318 default:
319 avpriv_request_sample(avctx, "unknown video class");
320 }
321 }
322
323
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (nb_audio_frames && mlv->class[1]) {
324 ast = avformat_new_stream(avctx, NULL);
325 if (!ast)
326 return AVERROR(ENOMEM);
327 asti = ffstream(ast);
328 ast->id = 1;
329 ast->nb_frames = nb_audio_frames;
330 if ((mlv->class[1] & MLV_CLASS_FLAG_LZMA))
331 avpriv_request_sample(avctx, "compression");
332 if ((mlv->class[1] & ~MLV_CLASS_FLAG_LZMA) != MLV_AUDIO_CLASS_WAV)
333 avpriv_request_sample(avctx, "unknown audio class");
334
335 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
336 avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
337 }
338
339
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (vst) {
340 AVRational framerate;
341 1 framerate.num = avio_rl32(pb);
342 1 framerate.den = avio_rl32(pb);
343 1 avpriv_set_pts_info(vst, 64, framerate.den, framerate.num);
344 } else
345 avio_skip(pb, 8);
346
347 1 avio_skip(pb, size - 52);
348
349 /* scan primary file */
350 1 mlv->pb[100] = avctx->pb;
351 1 ret = scan_file(avctx, vst, ast, 100);
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
353 return ret;
354
355 /* scan secondary files */
356
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (strlen(avctx->url) > 2) {
357 int i;
358 1 char *filename = av_strdup(avctx->url);
359
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!filename)
361 return AVERROR(ENOMEM);
362
363
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (i = 0; i < 100; i++) {
364 1 snprintf(filename + strlen(filename) - 2, 3, "%02d", i);
365
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (avctx->io_open(avctx, &mlv->pb[i], filename, AVIO_FLAG_READ, NULL) < 0)
366 1 break;
367 if (check_file_header(mlv->pb[i], guid) < 0) {
368 av_log(avctx, AV_LOG_WARNING, "ignoring %s; bad format or guid mismatch\n", filename);
369 ff_format_io_close(avctx, &mlv->pb[i]);
370 continue;
371 }
372 av_log(avctx, AV_LOG_INFO, "scanning %s\n", filename);
373 ret = scan_file(avctx, vst, ast, i);
374 if (ret < 0) {
375 av_log(avctx, AV_LOG_WARNING, "ignoring %s; %s\n", filename, av_err2str(ret));
376 ff_format_io_close(avctx, &mlv->pb[i]);
377 continue;
378 }
379 }
380 1 av_free(filename);
381 }
382
383
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (vst)
384 1 vst->duration = vsti->nb_index_entries;
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ast)
386 ast->duration = asti->nb_index_entries;
387
388
3/8
✓ 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.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 if ((vst && !vsti->nb_index_entries) || (ast && !asti->nb_index_entries)) {
389 av_log(avctx, AV_LOG_ERROR, "no index entries found\n");
390 return AVERROR_INVALIDDATA;
391 }
392
393
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (vst && ast)
394 avio_seek(pb, FFMIN(vsti->index_entries[0].pos, asti->index_entries[0].pos), SEEK_SET);
395
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (vst)
396 1 avio_seek(pb, vsti->index_entries[0].pos, SEEK_SET);
397 else if (ast)
398 avio_seek(pb, asti->index_entries[0].pos, SEEK_SET);
399
400 1 return 0;
401 }
402
403 2 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
404 {
405 2 MlvContext *mlv = avctx->priv_data;
406 AVIOContext *pb;
407 AVStream *st;
408 FFStream *sti;
409 int index, ret;
410 unsigned int size, space;
411
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!avctx->nb_streams)
413 return AVERROR_EOF;
414
415 2 st = avctx->streams[mlv->stream_index];
416 2 sti = ffstream(st);
417
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mlv->pts >= st->duration)
418 1 return AVERROR_EOF;
419
420 1 index = av_index_search_timestamp(st, mlv->pts, AVSEEK_FLAG_ANY);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (index < 0) {
422 av_log(avctx, AV_LOG_ERROR, "could not find index entry for frame %"PRId64"\n", mlv->pts);
423 return AVERROR(EIO);
424 }
425
426 1 pb = mlv->pb[sti->index_entries[index].size];
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pb) {
428 ret = FFERROR_REDO;
429 goto next_packet;
430 }
431 1 avio_seek(pb, sti->index_entries[index].pos, SEEK_SET);
432
433 1 avio_skip(pb, 4); // blockType
434 1 size = avio_rl32(pb);
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size < 16)
436 return AVERROR_INVALIDDATA;
437 1 avio_skip(pb, 12); //timestamp, frameNumber
438
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
439 1 avio_skip(pb, 8); // cropPosX, cropPosY, panPosX, panPosY
440 1 space = avio_rl32(pb);
441 1 avio_skip(pb, space);
442
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if ((mlv->class[st->id] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA))) {
444 ret = AVERROR_PATCHWELCOME;
445
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
446 1 ret = av_get_packet(pb, pkt, (st->codecpar->width * st->codecpar->height * st->codecpar->bits_per_coded_sample + 7) >> 3);
447 } else { // AVMEDIA_TYPE_AUDIO
448 if (space > UINT_MAX - 24 || size < (24 + space))
449 return AVERROR_INVALIDDATA;
450 ret = av_get_packet(pb, pkt, size - (24 + space));
451 }
452
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
454 return ret;
455
456 1 pkt->stream_index = mlv->stream_index;
457 1 pkt->pts = mlv->pts;
458
459 1 ret = 0;
460 1 next_packet:
461 1 mlv->stream_index++;
462
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (mlv->stream_index == avctx->nb_streams) {
463 1 mlv->stream_index = 0;
464 1 mlv->pts++;
465 }
466 1 return ret;
467 }
468
469 static int read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
470 {
471 MlvContext *mlv = avctx->priv_data;
472
473 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
474 return AVERROR(ENOSYS);
475
476 if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
477 return AVERROR(EIO);
478
479 mlv->pts = timestamp;
480 return 0;
481 }
482
483 1 static int read_close(AVFormatContext *s)
484 {
485 1 MlvContext *mlv = s->priv_data;
486 int i;
487
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1 times.
101 for (i = 0; i < 100; i++)
488 100 ff_format_io_close(s, &mlv->pb[i]);
489 1 return 0;
490 }
491
492 const FFInputFormat ff_mlv_demuxer = {
493 .p.name = "mlv",
494 .p.long_name = NULL_IF_CONFIG_SMALL("Magic Lantern Video (MLV)"),
495 .priv_data_size = sizeof(MlvContext),
496 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
497 .read_probe = probe,
498 .read_header = read_header,
499 .read_packet = read_packet,
500 .read_close = read_close,
501 .read_seek = read_seek,
502 };
503