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