FFmpeg coverage


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