2 * Magic Lantern Video (MLV) demuxer
3 * Copyright (c) 2014 Peter Ross
5 * This file is part of FFmpeg.
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.
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.
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
24 * Magic Lantern Video (MLV) demuxer
27 #include "libavutil/eval.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/rational.h"
32 #include "avio_internal.h"
36 #define MLV_VERSION "v2.0"
38 #define MLV_VIDEO_CLASS_RAW 1
39 #define MLV_VIDEO_CLASS_YUV 2
40 #define MLV_VIDEO_CLASS_JPEG 3
41 #define MLV_VIDEO_CLASS_H264 4
43 #define MLV_AUDIO_CLASS_WAV 1
45 #define MLV_CLASS_FLAG_DELTA 0x40
46 #define MLV_CLASS_FLAG_LZMA 0x80
55 static int read_close(AVFormatContext *s);
57 static int probe(const AVProbeData *p)
59 if (AV_RL32(p->buf) == MKTAG('M','L','V','I') &&
60 AV_RL32(p->buf + 4) >= 52 &&
61 !memcmp(p->buf + 8, MLV_VERSION, 5))
62 return AVPROBE_SCORE_MAX;
66 static int check_file_header(AVIOContext *pb, uint64_t guid)
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);
82 static void read_string(AVFormatContext *avctx, AVIOContext *pb, const char *tag, unsigned size)
84 char * value = av_malloc(size + 1);
90 avio_read(pb, value, size);
97 av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
100 static void read_uint8(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
102 av_dict_set_int(&avctx->metadata, tag, avio_r8(pb), 0);
105 static void read_uint16(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
107 av_dict_set_int(&avctx->metadata, tag, avio_rl16(pb), 0);
110 static void read_uint32(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
112 av_dict_set_int(&avctx->metadata, tag, avio_rl32(pb), 0);
115 static void read_uint64(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
117 av_dict_set_int(&avctx->metadata, tag, avio_rl64(pb), 0);
120 static int scan_file(AVFormatContext *avctx, AVStream *vst, AVStream *ast, int file)
122 MlvContext *mlv = avctx->priv_data;
123 AVIOContext *pb = mlv->pb[file];
125 while (!avio_feof(pb)) {
128 type = avio_rl32(pb);
129 size = avio_rl32(pb);
130 avio_skip(pb, 8); //timestamp
134 if (vst && type == MKTAG('R','A','W','I') && size >= 164) {
135 unsigned width = avio_rl16(pb);
136 unsigned height = avio_rl16(pb);
137 unsigned bits_per_coded_sample;
138 ret = av_image_check_size(width, height, 0, avctx);
141 if (avio_rl32(pb) != 1)
142 avpriv_request_sample(avctx, "raw api version");
143 avio_skip(pb, 20); // pointer, width, height, pitch, frame_size
144 bits_per_coded_sample = avio_rl32(pb);
145 if (bits_per_coded_sample > (INT_MAX - 7) / (width * height)) {
146 av_log(avctx, AV_LOG_ERROR,
147 "invalid bits_per_coded_sample %u (size: %ux%u)\n",
148 bits_per_coded_sample, width, height);
149 return AVERROR_INVALIDDATA;
151 vst->codecpar->width = width;
152 vst->codecpar->height = height;
153 vst->codecpar->bits_per_coded_sample = bits_per_coded_sample;
154 avio_skip(pb, 8 + 16 + 24); // black_level, white_level, xywh, active_area, exposure_bias
155 if (avio_rl32(pb) != 0x2010100) /* RGGB */
156 avpriv_request_sample(avctx, "cfa_pattern");
157 avio_skip(pb, 80); // calibration_illuminant1, color_matrix1, dynamic_range
158 vst->codecpar->format = AV_PIX_FMT_BAYER_RGGB16LE;
159 vst->codecpar->codec_tag = MKTAG('B', 'I', 'T', 16);
161 } else if (ast && type == MKTAG('W', 'A', 'V', 'I') && size >= 16) {
162 ret = ff_get_wav_header(avctx, pb, ast->codecpar, 16, 0);
166 } else if (type == MKTAG('I','N','F','O')) {
168 read_string(avctx, pb, "info", size);
170 } else if (type == MKTAG('I','D','N','T') && size >= 36) {
171 read_string(avctx, pb, "cameraName", 32);
172 read_uint32(avctx, pb, "cameraModel", "0x%"PRIx32);
175 read_string(avctx, pb, "cameraSerial", 32);
178 } else if (type == MKTAG('L','E','N','S') && size >= 48) {
179 read_uint16(avctx, pb, "focalLength", "%i");
180 read_uint16(avctx, pb, "focalDist", "%i");
181 read_uint16(avctx, pb, "aperture", "%i");
182 read_uint8(avctx, pb, "stabilizerMode", "%i");
183 read_uint8(avctx, pb, "autofocusMode", "%i");
184 read_uint32(avctx, pb, "flags", "0x%"PRIx32);
185 read_uint32(avctx, pb, "lensID", "%"PRIi32);
186 read_string(avctx, pb, "lensName", 32);
189 read_string(avctx, pb, "lensSerial", 32);
192 } else if (vst && type == MKTAG('V', 'I', 'D', 'F') && size >= 4) {
193 uint64_t pts = avio_rl32(pb);
194 ff_add_index_entry(&vst->index_entries, &vst->nb_index_entries, &vst->index_entries_allocated_size,
195 avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
197 } else if (ast && type == MKTAG('A', 'U', 'D', 'F') && size >= 4) {
198 uint64_t pts = avio_rl32(pb);
199 ff_add_index_entry(&ast->index_entries, &ast->nb_index_entries, &ast->index_entries_allocated_size,
200 avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
202 } else if (vst && type == MKTAG('W','B','A','L') && size >= 28) {
203 read_uint32(avctx, pb, "wb_mode", "%"PRIi32);
204 read_uint32(avctx, pb, "kelvin", "%"PRIi32);
205 read_uint32(avctx, pb, "wbgain_r", "%"PRIi32);
206 read_uint32(avctx, pb, "wbgain_g", "%"PRIi32);
207 read_uint32(avctx, pb, "wbgain_b", "%"PRIi32);
208 read_uint32(avctx, pb, "wbs_gm", "%"PRIi32);
209 read_uint32(avctx, pb, "wbs_ba", "%"PRIi32);
211 } else if (type == MKTAG('R','T','C','I') && size >= 20) {
213 struct tm time = { 0 };
214 time.tm_sec = avio_rl16(pb);
215 time.tm_min = avio_rl16(pb);
216 time.tm_hour = avio_rl16(pb);
217 time.tm_mday = avio_rl16(pb);
218 time.tm_mon = avio_rl16(pb);
219 time.tm_year = avio_rl16(pb);
220 time.tm_wday = avio_rl16(pb);
221 time.tm_yday = avio_rl16(pb);
222 time.tm_isdst = avio_rl16(pb);
224 if (strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &time))
225 av_dict_set(&avctx->metadata, "time", str, 0);
227 } else if (type == MKTAG('E','X','P','O') && size >= 16) {
228 av_dict_set(&avctx->metadata, "isoMode", avio_rl32(pb) ? "auto" : "manual", 0);
229 read_uint32(avctx, pb, "isoValue", "%"PRIi32);
230 read_uint32(avctx, pb, "isoAnalog", "%"PRIi32);
231 read_uint32(avctx, pb, "digitalGain", "%"PRIi32);
234 read_uint64(avctx, pb, "shutterValue", "%"PRIi64);
237 } else if (type == MKTAG('S','T','Y','L') && size >= 36) {
238 read_uint32(avctx, pb, "picStyleId", "%"PRIi32);
239 read_uint32(avctx, pb, "contrast", "%"PRIi32);
240 read_uint32(avctx, pb, "sharpness", "%"PRIi32);
241 read_uint32(avctx, pb, "saturation", "%"PRIi32);
242 read_uint32(avctx, pb, "colortone", "%"PRIi32);
243 read_string(avctx, pb, "picStyleName", 16);
245 } else if (type == MKTAG('M','A','R','K')) {
246 } 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 */
249 av_log(avctx, AV_LOG_INFO, "unsupported tag %s, size %u\n",
250 av_fourcc2str(type), size);
257 static int read_header(AVFormatContext *avctx)
259 MlvContext *mlv = avctx->priv_data;
260 AVIOContext *pb = avctx->pb;
261 AVStream *vst = NULL, *ast = NULL;
263 unsigned nb_video_frames, nb_audio_frames;
268 size = avio_rl32(pb);
270 return AVERROR_INVALIDDATA;
274 guid = avio_rl64(pb);
275 snprintf(guidstr, sizeof(guidstr), "0x%"PRIx64, guid);
276 av_dict_set(&avctx->metadata, "guid", guidstr, 0);
278 avio_skip(pb, 8); //fileNum, fileCount, fileFlags
280 mlv->class[0] = avio_rl16(pb);
281 mlv->class[1] = avio_rl16(pb);
283 nb_video_frames = avio_rl32(pb);
284 nb_audio_frames = avio_rl32(pb);
286 if (nb_video_frames && mlv->class[0]) {
287 vst = avformat_new_stream(avctx, NULL);
289 return AVERROR(ENOMEM);
291 vst->nb_frames = nb_video_frames;
292 if ((mlv->class[0] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)))
293 avpriv_request_sample(avctx, "compression");
294 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
295 switch (mlv->class[0] & ~(MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)) {
296 case MLV_VIDEO_CLASS_RAW:
297 vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
299 case MLV_VIDEO_CLASS_YUV:
300 vst->codecpar->format = AV_PIX_FMT_YUV420P;
301 vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
302 vst->codecpar->codec_tag = 0;
304 case MLV_VIDEO_CLASS_JPEG:
305 vst->codecpar->codec_id = AV_CODEC_ID_MJPEG;
306 vst->codecpar->codec_tag = 0;
308 case MLV_VIDEO_CLASS_H264:
309 vst->codecpar->codec_id = AV_CODEC_ID_H264;
310 vst->codecpar->codec_tag = 0;
313 avpriv_request_sample(avctx, "unknown video class");
317 if (nb_audio_frames && mlv->class[1]) {
318 ast = avformat_new_stream(avctx, NULL);
320 return AVERROR(ENOMEM);
322 ast->nb_frames = nb_audio_frames;
323 if ((mlv->class[1] & MLV_CLASS_FLAG_LZMA))
324 avpriv_request_sample(avctx, "compression");
325 if ((mlv->class[1] & ~MLV_CLASS_FLAG_LZMA) != MLV_AUDIO_CLASS_WAV)
326 avpriv_request_sample(avctx, "unknown audio class");
328 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
329 avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
333 AVRational framerate;
334 framerate.num = avio_rl32(pb);
335 framerate.den = avio_rl32(pb);
336 avpriv_set_pts_info(vst, 64, framerate.den, framerate.num);
340 avio_skip(pb, size - 52);
342 /* scan primary file */
343 mlv->pb[100] = avctx->pb;
344 ret = scan_file(avctx, vst, ast, 100);
348 /* scan secondary files */
349 if (strlen(avctx->url) > 2) {
351 char *filename = av_strdup(avctx->url);
354 return AVERROR(ENOMEM);
356 for (i = 0; i < 100; i++) {
357 snprintf(filename + strlen(filename) - 2, 3, "%02d", i);
358 if (avctx->io_open(avctx, &mlv->pb[i], filename, AVIO_FLAG_READ, NULL) < 0)
360 if (check_file_header(mlv->pb[i], guid) < 0) {
361 av_log(avctx, AV_LOG_WARNING, "ignoring %s; bad format or guid mismatch\n", filename);
362 ff_format_io_close(avctx, &mlv->pb[i]);
365 av_log(avctx, AV_LOG_INFO, "scanning %s\n", filename);
366 ret = scan_file(avctx, vst, ast, i);
368 av_log(avctx, AV_LOG_WARNING, "ignoring %s; %s\n", filename, av_err2str(ret));
369 ff_format_io_close(avctx, &mlv->pb[i]);
377 vst->duration = vst->nb_index_entries;
379 ast->duration = ast->nb_index_entries;
381 if ((vst && !vst->nb_index_entries) || (ast && !ast->nb_index_entries)) {
382 av_log(avctx, AV_LOG_ERROR, "no index entries found\n");
384 return AVERROR_INVALIDDATA;
388 avio_seek(pb, FFMIN(vst->index_entries[0].pos, ast->index_entries[0].pos), SEEK_SET);
390 avio_seek(pb, vst->index_entries[0].pos, SEEK_SET);
392 avio_seek(pb, ast->index_entries[0].pos, SEEK_SET);
397 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
399 MlvContext *mlv = avctx->priv_data;
403 unsigned int size, space;
405 if (!avctx->nb_streams)
408 st = avctx->streams[mlv->stream_index];
409 if (mlv->pts >= st->duration)
412 index = av_index_search_timestamp(st, mlv->pts, AVSEEK_FLAG_ANY);
414 av_log(avctx, AV_LOG_ERROR, "could not find index entry for frame %"PRId64"\n", mlv->pts);
418 pb = mlv->pb[st->index_entries[index].size];
423 avio_seek(pb, st->index_entries[index].pos, SEEK_SET);
425 avio_skip(pb, 4); // blockType
426 size = avio_rl32(pb);
428 return AVERROR_INVALIDDATA;
429 avio_skip(pb, 12); //timestamp, frameNumber
430 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
431 avio_skip(pb, 8); // cropPosX, cropPosY, panPosX, panPosY
432 space = avio_rl32(pb);
433 avio_skip(pb, space);
435 if ((mlv->class[st->id] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA))) {
436 ret = AVERROR_PATCHWELCOME;
437 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
438 ret = av_get_packet(pb, pkt, (st->codecpar->width * st->codecpar->height * st->codecpar->bits_per_coded_sample + 7) >> 3);
439 } else { // AVMEDIA_TYPE_AUDIO
440 if (space > UINT_MAX - 24 || size < (24 + space))
441 return AVERROR_INVALIDDATA;
442 ret = av_get_packet(pb, pkt, size - (24 + space));
448 pkt->stream_index = mlv->stream_index;
454 if (mlv->stream_index == avctx->nb_streams) {
455 mlv->stream_index = 0;
461 static int read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
463 MlvContext *mlv = avctx->priv_data;
465 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
466 return AVERROR(ENOSYS);
468 if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
471 mlv->pts = timestamp;
475 static int read_close(AVFormatContext *s)
477 MlvContext *mlv = s->priv_data;
479 for (i = 0; i < 100; i++)
480 ff_format_io_close(s, &mlv->pb[i]);
484 AVInputFormat ff_mlv_demuxer = {
486 .long_name = NULL_IF_CONFIG_SMALL("Magic Lantern Video (MLV)"),
487 .priv_data_size = sizeof(MlvContext),
489 .read_header = read_header,
490 .read_packet = read_packet,
491 .read_close = read_close,
492 .read_seek = read_seek,