FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mlvdec.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 236 414 57.0%
Functions: 10 17 58.8%
Branches: 94 216 43.5%

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