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