2014-04-17 20:43:11 +10:00
|
|
|
/*
|
|
|
|
* Magic Lantern Video (MLV) demuxer
|
|
|
|
* Copyright (c) 2014 Peter Ross
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Magic Lantern Video (MLV) demuxer
|
|
|
|
*/
|
|
|
|
|
2023-09-02 12:11:59 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
2024-12-13 16:07:03 +11:00
|
|
|
#include "libavcodec/bytestream.h"
|
|
|
|
#include "libavcodec/tiff.h"
|
|
|
|
#include "libavcodec/tiff_common.h"
|
2015-12-19 23:45:00 +01:00
|
|
|
#include "libavutil/imgutils.h"
|
2014-04-17 20:43:11 +10:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2024-03-25 01:30:37 +01:00
|
|
|
#include "libavutil/mem.h"
|
2014-04-17 20:43:11 +10:00
|
|
|
#include "libavutil/rational.h"
|
|
|
|
#include "avformat.h"
|
2022-05-06 18:28:08 +02:00
|
|
|
#include "demux.h"
|
2014-04-17 20:43:11 +10:00
|
|
|
#include "internal.h"
|
|
|
|
#include "riff.h"
|
|
|
|
|
|
|
|
#define MLV_VERSION "v2.0"
|
|
|
|
|
|
|
|
#define MLV_VIDEO_CLASS_RAW 1
|
|
|
|
#define MLV_VIDEO_CLASS_YUV 2
|
|
|
|
#define MLV_VIDEO_CLASS_JPEG 3
|
|
|
|
#define MLV_VIDEO_CLASS_H264 4
|
|
|
|
|
|
|
|
#define MLV_AUDIO_CLASS_WAV 1
|
|
|
|
|
2024-12-13 16:07:03 +11:00
|
|
|
#define MLV_CLASS_FLAG_LJ92 0x20
|
2014-04-17 20:43:11 +10:00
|
|
|
#define MLV_CLASS_FLAG_DELTA 0x40
|
|
|
|
#define MLV_CLASS_FLAG_LZMA 0x80
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
AVIOContext *pb[101];
|
|
|
|
int class[2];
|
|
|
|
int stream_index;
|
|
|
|
uint64_t pts;
|
2024-12-13 16:07:03 +11:00
|
|
|
int black_level;
|
|
|
|
int white_level;
|
|
|
|
int color_matrix1[9][2];
|
2014-04-17 20:43:11 +10:00
|
|
|
} MlvContext;
|
|
|
|
|
2019-03-21 01:18:37 +01:00
|
|
|
static int probe(const AVProbeData *p)
|
2014-04-17 20:43:11 +10:00
|
|
|
{
|
|
|
|
if (AV_RL32(p->buf) == MKTAG('M','L','V','I') &&
|
|
|
|
AV_RL32(p->buf + 4) >= 52 &&
|
|
|
|
!memcmp(p->buf + 8, MLV_VERSION, 5))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_file_header(AVIOContext *pb, uint64_t guid)
|
|
|
|
{
|
|
|
|
unsigned int size;
|
|
|
|
uint8_t version[8];
|
|
|
|
|
|
|
|
avio_skip(pb, 4);
|
|
|
|
size = avio_rl32(pb);
|
|
|
|
if (size < 52)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_read(pb, version, 8);
|
|
|
|
if (memcmp(version, MLV_VERSION, 5) || avio_rl64(pb) != guid)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(pb, size - 24);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-16 15:36:28 +02:00
|
|
|
static void read_string(AVFormatContext *avctx, AVIOContext *pb, const char *tag, unsigned size)
|
2014-04-17 20:43:11 +10:00
|
|
|
{
|
|
|
|
char * value = av_malloc(size + 1);
|
2024-12-25 05:13:02 +01:00
|
|
|
int ret;
|
|
|
|
|
2014-04-17 20:43:11 +10:00
|
|
|
if (!value) {
|
|
|
|
avio_skip(pb, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-25 05:13:02 +01:00
|
|
|
ret = avio_read(pb, value, size);
|
|
|
|
if (ret != size || !value[0]) {
|
2014-04-17 20:43:11 +10:00
|
|
|
av_free(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[size] = 0;
|
|
|
|
av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_uint8(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
|
|
|
|
{
|
2014-07-29 21:10:39 +02:00
|
|
|
av_dict_set_int(&avctx->metadata, tag, avio_r8(pb), 0);
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_uint16(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
|
|
|
|
{
|
2014-07-29 21:10:39 +02:00
|
|
|
av_dict_set_int(&avctx->metadata, tag, avio_rl16(pb), 0);
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_uint32(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
|
|
|
|
{
|
2014-07-29 21:10:39 +02:00
|
|
|
av_dict_set_int(&avctx->metadata, tag, avio_rl32(pb), 0);
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_uint64(AVFormatContext *avctx, AVIOContext *pb, const char *tag, const char *fmt)
|
|
|
|
{
|
2014-07-29 21:10:39 +02:00
|
|
|
av_dict_set_int(&avctx->metadata, tag, avio_rl64(pb), 0);
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
|
2014-04-27 12:42:04 +10:00
|
|
|
static int scan_file(AVFormatContext *avctx, AVStream *vst, AVStream *ast, int file)
|
2014-04-17 20:43:11 +10:00
|
|
|
{
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *const vsti = ffstream(vst), *const asti = ffstream(ast);
|
2014-04-17 20:43:11 +10:00
|
|
|
MlvContext *mlv = avctx->priv_data;
|
|
|
|
AVIOContext *pb = mlv->pb[file];
|
2014-04-27 12:42:04 +10:00
|
|
|
int ret;
|
2014-08-07 17:12:41 -03:00
|
|
|
while (!avio_feof(pb)) {
|
2014-04-17 20:43:11 +10:00
|
|
|
int type;
|
|
|
|
unsigned int size;
|
|
|
|
type = avio_rl32(pb);
|
|
|
|
size = avio_rl32(pb);
|
|
|
|
avio_skip(pb, 8); //timestamp
|
|
|
|
if (size < 16)
|
|
|
|
break;
|
|
|
|
size -= 16;
|
|
|
|
if (vst && type == MKTAG('R','A','W','I') && size >= 164) {
|
2020-08-10 02:33:19 +02:00
|
|
|
unsigned width = avio_rl16(pb);
|
|
|
|
unsigned height = avio_rl16(pb);
|
|
|
|
unsigned bits_per_coded_sample;
|
|
|
|
ret = av_image_check_size(width, height, 0, avctx);
|
2015-12-19 23:45:00 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-17 20:43:11 +10:00
|
|
|
if (avio_rl32(pb) != 1)
|
|
|
|
avpriv_request_sample(avctx, "raw api version");
|
|
|
|
avio_skip(pb, 20); // pointer, width, height, pitch, frame_size
|
2020-08-10 02:33:19 +02:00
|
|
|
bits_per_coded_sample = avio_rl32(pb);
|
|
|
|
if (bits_per_coded_sample > (INT_MAX - 7) / (width * height)) {
|
2015-12-19 23:45:00 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
2020-08-10 02:33:19 +02:00
|
|
|
"invalid bits_per_coded_sample %u (size: %ux%u)\n",
|
|
|
|
bits_per_coded_sample, width, height);
|
2015-12-19 23:45:00 +01:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2020-08-10 02:33:19 +02:00
|
|
|
vst->codecpar->width = width;
|
|
|
|
vst->codecpar->height = height;
|
|
|
|
vst->codecpar->bits_per_coded_sample = bits_per_coded_sample;
|
2024-12-13 16:07:03 +11:00
|
|
|
mlv->black_level = avio_rl32(pb);
|
|
|
|
mlv->white_level = avio_rl32(pb);
|
|
|
|
avio_skip(pb, 16 + 24); // xywh, active_area, exposure_bias
|
2014-04-17 20:43:11 +10:00
|
|
|
if (avio_rl32(pb) != 0x2010100) /* RGGB */
|
|
|
|
avpriv_request_sample(avctx, "cfa_pattern");
|
2024-12-13 16:07:03 +11:00
|
|
|
avio_skip(pb, 4); // calibration_illuminant1,
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
mlv->color_matrix1[i][0] = avio_rl32(pb);
|
|
|
|
mlv->color_matrix1[i][1] = avio_rl32(pb);
|
|
|
|
}
|
|
|
|
avio_skip(pb, 4); // dynamic_range
|
2016-04-10 20:58:15 +01:00
|
|
|
vst->codecpar->format = AV_PIX_FMT_BAYER_RGGB16LE;
|
|
|
|
vst->codecpar->codec_tag = MKTAG('B', 'I', 'T', 16);
|
2014-04-17 20:43:11 +10:00
|
|
|
size -= 164;
|
|
|
|
} else if (ast && type == MKTAG('W', 'A', 'V', 'I') && size >= 16) {
|
2016-04-10 20:58:15 +01:00
|
|
|
ret = ff_get_wav_header(avctx, pb, ast->codecpar, 16, 0);
|
2014-04-27 12:42:04 +10:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-17 20:43:11 +10:00
|
|
|
size -= 16;
|
|
|
|
} else if (type == MKTAG('I','N','F','O')) {
|
|
|
|
if (size > 0)
|
|
|
|
read_string(avctx, pb, "info", size);
|
|
|
|
continue;
|
|
|
|
} else if (type == MKTAG('I','D','N','T') && size >= 36) {
|
|
|
|
read_string(avctx, pb, "cameraName", 32);
|
|
|
|
read_uint32(avctx, pb, "cameraModel", "0x%"PRIx32);
|
|
|
|
size -= 36;
|
|
|
|
if (size >= 32) {
|
|
|
|
read_string(avctx, pb, "cameraSerial", 32);
|
|
|
|
size -= 32;
|
|
|
|
}
|
|
|
|
} else if (type == MKTAG('L','E','N','S') && size >= 48) {
|
|
|
|
read_uint16(avctx, pb, "focalLength", "%i");
|
|
|
|
read_uint16(avctx, pb, "focalDist", "%i");
|
|
|
|
read_uint16(avctx, pb, "aperture", "%i");
|
|
|
|
read_uint8(avctx, pb, "stabilizerMode", "%i");
|
|
|
|
read_uint8(avctx, pb, "autofocusMode", "%i");
|
|
|
|
read_uint32(avctx, pb, "flags", "0x%"PRIx32);
|
|
|
|
read_uint32(avctx, pb, "lensID", "%"PRIi32);
|
|
|
|
read_string(avctx, pb, "lensName", 32);
|
|
|
|
size -= 48;
|
|
|
|
if (size >= 32) {
|
|
|
|
read_string(avctx, pb, "lensSerial", 32);
|
|
|
|
size -= 32;
|
|
|
|
}
|
|
|
|
} else if (vst && type == MKTAG('V', 'I', 'D', 'F') && size >= 4) {
|
|
|
|
uint64_t pts = avio_rl32(pb);
|
2021-08-24 19:41:16 +02:00
|
|
|
ff_add_index_entry(&vsti->index_entries, &vsti->nb_index_entries,
|
|
|
|
&vsti->index_entries_allocated_size,
|
2014-04-17 20:43:11 +10:00
|
|
|
avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
|
|
|
|
size -= 4;
|
|
|
|
} else if (ast && type == MKTAG('A', 'U', 'D', 'F') && size >= 4) {
|
|
|
|
uint64_t pts = avio_rl32(pb);
|
2021-08-24 19:41:16 +02:00
|
|
|
ff_add_index_entry(&asti->index_entries, &asti->nb_index_entries,
|
|
|
|
&asti->index_entries_allocated_size,
|
2014-04-17 20:43:11 +10:00
|
|
|
avio_tell(pb) - 20, pts, file, 0, AVINDEX_KEYFRAME);
|
|
|
|
size -= 4;
|
|
|
|
} else if (vst && type == MKTAG('W','B','A','L') && size >= 28) {
|
|
|
|
read_uint32(avctx, pb, "wb_mode", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "kelvin", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "wbgain_r", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "wbgain_g", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "wbgain_b", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "wbs_gm", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "wbs_ba", "%"PRIi32);
|
|
|
|
size -= 28;
|
|
|
|
} else if (type == MKTAG('R','T','C','I') && size >= 20) {
|
|
|
|
char str[32];
|
|
|
|
struct tm time = { 0 };
|
|
|
|
time.tm_sec = avio_rl16(pb);
|
|
|
|
time.tm_min = avio_rl16(pb);
|
|
|
|
time.tm_hour = avio_rl16(pb);
|
|
|
|
time.tm_mday = avio_rl16(pb);
|
|
|
|
time.tm_mon = avio_rl16(pb);
|
|
|
|
time.tm_year = avio_rl16(pb);
|
|
|
|
time.tm_wday = avio_rl16(pb);
|
|
|
|
time.tm_yday = avio_rl16(pb);
|
|
|
|
time.tm_isdst = avio_rl16(pb);
|
|
|
|
avio_skip(pb, 2);
|
2014-10-26 15:51:37 +01:00
|
|
|
if (strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &time))
|
|
|
|
av_dict_set(&avctx->metadata, "time", str, 0);
|
2014-04-17 20:43:11 +10:00
|
|
|
size -= 20;
|
|
|
|
} else if (type == MKTAG('E','X','P','O') && size >= 16) {
|
|
|
|
av_dict_set(&avctx->metadata, "isoMode", avio_rl32(pb) ? "auto" : "manual", 0);
|
|
|
|
read_uint32(avctx, pb, "isoValue", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "isoAnalog", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "digitalGain", "%"PRIi32);
|
|
|
|
size -= 16;
|
|
|
|
if (size >= 8) {
|
|
|
|
read_uint64(avctx, pb, "shutterValue", "%"PRIi64);
|
|
|
|
size -= 8;
|
|
|
|
}
|
|
|
|
} else if (type == MKTAG('S','T','Y','L') && size >= 36) {
|
|
|
|
read_uint32(avctx, pb, "picStyleId", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "contrast", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "sharpness", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "saturation", "%"PRIi32);
|
|
|
|
read_uint32(avctx, pb, "colortone", "%"PRIi32);
|
|
|
|
read_string(avctx, pb, "picStyleName", 16);
|
|
|
|
size -= 36;
|
2024-12-12 18:38:42 +11:00
|
|
|
} else if (type == MKTAG('V','E','R','S') && size >= 4) {
|
|
|
|
unsigned int length = avio_rl32(pb);
|
|
|
|
read_string(avctx, pb, "version", length);
|
|
|
|
size -= length + 4;
|
2024-12-12 18:39:50 +11:00
|
|
|
} else if (type == MKTAG('D','A','R','K')) {
|
|
|
|
} else if (type == MKTAG('D','I','S','O')) {
|
2014-04-17 20:43:11 +10:00
|
|
|
} else if (type == MKTAG('M','A','R','K')) {
|
|
|
|
} else if (type == MKTAG('N','U','L','L')) {
|
|
|
|
} else if (type == MKTAG('M','L','V','I')) { /* occurs when MLV and Mnn files are concatenated */
|
2024-12-12 18:39:50 +11:00
|
|
|
} else if (type == MKTAG('R','A','W','C')) {
|
2014-04-17 20:43:11 +10:00
|
|
|
} else {
|
2017-03-27 09:45:35 +02:00
|
|
|
av_log(avctx, AV_LOG_INFO, "unsupported tag %s, size %u\n",
|
|
|
|
av_fourcc2str(type), size);
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
avio_skip(pb, size);
|
|
|
|
}
|
2014-04-27 12:42:04 +10:00
|
|
|
return 0;
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_header(AVFormatContext *avctx)
|
|
|
|
{
|
|
|
|
MlvContext *mlv = avctx->priv_data;
|
|
|
|
AVIOContext *pb = avctx->pb;
|
|
|
|
AVStream *vst = NULL, *ast = NULL;
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *vsti = NULL, *asti = NULL;
|
2014-04-27 12:42:04 +10:00
|
|
|
int size, ret;
|
2015-02-07 16:18:02 +01:00
|
|
|
unsigned nb_video_frames, nb_audio_frames;
|
2014-04-17 20:43:11 +10:00
|
|
|
uint64_t guid;
|
|
|
|
char guidstr[32];
|
|
|
|
|
|
|
|
avio_skip(pb, 4);
|
|
|
|
size = avio_rl32(pb);
|
|
|
|
if (size < 52)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(pb, 8);
|
|
|
|
|
|
|
|
guid = avio_rl64(pb);
|
|
|
|
snprintf(guidstr, sizeof(guidstr), "0x%"PRIx64, guid);
|
|
|
|
av_dict_set(&avctx->metadata, "guid", guidstr, 0);
|
|
|
|
|
|
|
|
avio_skip(pb, 8); //fileNum, fileCount, fileFlags
|
|
|
|
|
|
|
|
mlv->class[0] = avio_rl16(pb);
|
2015-02-07 16:18:02 +01:00
|
|
|
mlv->class[1] = avio_rl16(pb);
|
|
|
|
|
|
|
|
nb_video_frames = avio_rl32(pb);
|
|
|
|
nb_audio_frames = avio_rl32(pb);
|
|
|
|
|
|
|
|
if (nb_video_frames && mlv->class[0]) {
|
2014-04-17 20:43:11 +10:00
|
|
|
vst = avformat_new_stream(avctx, NULL);
|
|
|
|
if (!vst)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-08-24 19:41:16 +02:00
|
|
|
vsti = ffstream(vst);
|
|
|
|
|
2014-04-17 20:43:11 +10:00
|
|
|
vst->id = 0;
|
2015-02-07 16:18:02 +01:00
|
|
|
vst->nb_frames = nb_video_frames;
|
2014-04-17 20:43:11 +10:00
|
|
|
if ((mlv->class[0] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)))
|
|
|
|
avpriv_request_sample(avctx, "compression");
|
2016-04-10 20:58:15 +01:00
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
2014-04-17 20:43:11 +10:00
|
|
|
switch (mlv->class[0] & ~(MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA)) {
|
|
|
|
case MLV_VIDEO_CLASS_RAW:
|
2016-04-10 20:58:15 +01:00
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
|
2014-04-17 20:43:11 +10:00
|
|
|
break;
|
|
|
|
case MLV_VIDEO_CLASS_YUV:
|
2016-04-10 20:58:15 +01:00
|
|
|
vst->codecpar->format = AV_PIX_FMT_YUV420P;
|
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
|
|
|
|
vst->codecpar->codec_tag = 0;
|
2014-04-17 20:43:11 +10:00
|
|
|
break;
|
2024-12-13 16:07:03 +11:00
|
|
|
case MLV_CLASS_FLAG_LJ92|MLV_VIDEO_CLASS_RAW:
|
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_TIFF;
|
|
|
|
break;
|
2014-04-17 20:43:11 +10:00
|
|
|
case MLV_VIDEO_CLASS_JPEG:
|
2016-04-10 20:58:15 +01:00
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_MJPEG;
|
|
|
|
vst->codecpar->codec_tag = 0;
|
2014-04-17 20:43:11 +10:00
|
|
|
break;
|
|
|
|
case MLV_VIDEO_CLASS_H264:
|
2016-04-10 20:58:15 +01:00
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_H264;
|
|
|
|
vst->codecpar->codec_tag = 0;
|
2014-04-17 20:43:11 +10:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
avpriv_request_sample(avctx, "unknown video class");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 16:18:02 +01:00
|
|
|
if (nb_audio_frames && mlv->class[1]) {
|
2014-04-17 20:43:11 +10:00
|
|
|
ast = avformat_new_stream(avctx, NULL);
|
|
|
|
if (!ast)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-08-24 19:41:16 +02:00
|
|
|
asti = ffstream(ast);
|
2014-04-17 20:43:11 +10:00
|
|
|
ast->id = 1;
|
2015-02-07 16:18:02 +01:00
|
|
|
ast->nb_frames = nb_audio_frames;
|
2014-04-17 20:43:11 +10:00
|
|
|
if ((mlv->class[1] & MLV_CLASS_FLAG_LZMA))
|
|
|
|
avpriv_request_sample(avctx, "compression");
|
|
|
|
if ((mlv->class[1] & ~MLV_CLASS_FLAG_LZMA) != MLV_AUDIO_CLASS_WAV)
|
|
|
|
avpriv_request_sample(avctx, "unknown audio class");
|
|
|
|
|
2016-04-10 20:58:15 +01:00
|
|
|
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vst) {
|
|
|
|
AVRational framerate;
|
|
|
|
framerate.num = avio_rl32(pb);
|
|
|
|
framerate.den = avio_rl32(pb);
|
|
|
|
avpriv_set_pts_info(vst, 64, framerate.den, framerate.num);
|
|
|
|
} else
|
|
|
|
avio_skip(pb, 8);
|
|
|
|
|
|
|
|
avio_skip(pb, size - 52);
|
|
|
|
|
|
|
|
/* scan primary file */
|
|
|
|
mlv->pb[100] = avctx->pb;
|
2014-04-27 12:42:04 +10:00
|
|
|
ret = scan_file(avctx, vst, ast, 100);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-17 20:43:11 +10:00
|
|
|
|
|
|
|
/* scan secondary files */
|
2017-12-29 23:30:14 +01:00
|
|
|
if (strlen(avctx->url) > 2) {
|
2014-04-17 20:43:11 +10:00
|
|
|
int i;
|
2017-12-29 23:30:14 +01:00
|
|
|
char *filename = av_strdup(avctx->url);
|
2015-05-11 22:38:21 +02:00
|
|
|
|
2014-04-17 20:43:11 +10:00
|
|
|
if (!filename)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-05-11 22:38:21 +02:00
|
|
|
|
2014-04-17 20:43:11 +10:00
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
snprintf(filename + strlen(filename) - 2, 3, "%02d", i);
|
2016-02-10 14:40:32 +00:00
|
|
|
if (avctx->io_open(avctx, &mlv->pb[i], filename, AVIO_FLAG_READ, NULL) < 0)
|
2014-04-17 20:43:11 +10:00
|
|
|
break;
|
|
|
|
if (check_file_header(mlv->pb[i], guid) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "ignoring %s; bad format or guid mismatch\n", filename);
|
2016-02-10 14:40:32 +00:00
|
|
|
ff_format_io_close(avctx, &mlv->pb[i]);
|
2014-04-17 20:43:11 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
av_log(avctx, AV_LOG_INFO, "scanning %s\n", filename);
|
2014-04-27 12:42:04 +10:00
|
|
|
ret = scan_file(avctx, vst, ast, i);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "ignoring %s; %s\n", filename, av_err2str(ret));
|
2016-02-10 14:40:32 +00:00
|
|
|
ff_format_io_close(avctx, &mlv->pb[i]);
|
2014-04-27 12:42:04 +10:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
av_free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vst)
|
2021-08-24 19:41:16 +02:00
|
|
|
vst->duration = vsti->nb_index_entries;
|
2014-04-17 20:43:11 +10:00
|
|
|
if (ast)
|
2021-08-24 19:41:16 +02:00
|
|
|
ast->duration = asti->nb_index_entries;
|
2014-04-17 20:43:11 +10:00
|
|
|
|
2021-08-24 19:41:16 +02:00
|
|
|
if ((vst && !vsti->nb_index_entries) || (ast && !asti->nb_index_entries)) {
|
2015-12-19 23:44:53 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "no index entries found\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2014-04-17 20:43:11 +10:00
|
|
|
if (vst && ast)
|
2021-08-24 19:41:16 +02:00
|
|
|
avio_seek(pb, FFMIN(vsti->index_entries[0].pos, asti->index_entries[0].pos), SEEK_SET);
|
2014-04-17 20:43:11 +10:00
|
|
|
else if (vst)
|
2021-08-24 19:41:16 +02:00
|
|
|
avio_seek(pb, vsti->index_entries[0].pos, SEEK_SET);
|
2014-04-17 20:43:11 +10:00
|
|
|
else if (ast)
|
2021-08-24 19:41:16 +02:00
|
|
|
avio_seek(pb, asti->index_entries[0].pos, SEEK_SET);
|
2014-04-17 20:43:11 +10:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-12-13 16:07:03 +11:00
|
|
|
static void write_tiff_short(PutByteContext *pb, int tag, int value)
|
|
|
|
{
|
|
|
|
bytestream2_put_le16(pb, tag);
|
|
|
|
bytestream2_put_le16(pb, TIFF_SHORT);
|
|
|
|
bytestream2_put_le32(pb, 1);
|
|
|
|
bytestream2_put_le16(pb, value);
|
|
|
|
bytestream2_put_le16(pb, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_tiff_short2(PutByteContext *pb, int tag, int v1, int v2)
|
|
|
|
{
|
|
|
|
bytestream2_put_le16(pb, tag);
|
|
|
|
bytestream2_put_le16(pb, TIFF_SHORT);
|
|
|
|
bytestream2_put_le32(pb, 2);
|
|
|
|
bytestream2_put_le16(pb, v1);
|
|
|
|
bytestream2_put_le16(pb, v2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_tiff_long(PutByteContext *pb, int tag, int value)
|
|
|
|
{
|
|
|
|
bytestream2_put_le16(pb, tag);
|
|
|
|
bytestream2_put_le16(pb, TIFF_LONG);
|
|
|
|
bytestream2_put_le32(pb, 1);
|
|
|
|
bytestream2_put_le32(pb, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_tiff_byte4(PutByteContext *pb, int tag, int v1, int v2, int v3, int v4)
|
|
|
|
{
|
|
|
|
bytestream2_put_le16(pb, tag);
|
|
|
|
bytestream2_put_le16(pb, TIFF_BYTE);
|
|
|
|
bytestream2_put_le32(pb, 4);
|
|
|
|
bytestream2_put_byte(pb, v1);
|
|
|
|
bytestream2_put_byte(pb, v2);
|
|
|
|
bytestream2_put_byte(pb, v3);
|
|
|
|
bytestream2_put_byte(pb, v4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_packet_lj92(AVFormatContext *avctx, AVStream *st, AVIOContext *pbio, AVPacket *pkt, int64_t size)
|
|
|
|
{
|
|
|
|
MlvContext *mlv = avctx->priv_data;
|
|
|
|
PutByteContext pbctx, *pb = &pbctx;
|
|
|
|
int ret, header_size;
|
|
|
|
uint8_t *stripofs, *matrixofs;
|
|
|
|
|
|
|
|
#define MAX_HEADER_SIZE 2048
|
|
|
|
if ((ret = av_new_packet(pkt, size + MAX_HEADER_SIZE)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
bytestream2_init_writer(pb, pkt->data, MAX_HEADER_SIZE);
|
|
|
|
|
|
|
|
bytestream2_put_le16(pb, 0x4949);
|
|
|
|
bytestream2_put_le16(pb, 42);
|
|
|
|
bytestream2_put_le32(pb, 8);
|
|
|
|
|
|
|
|
bytestream2_put_le16(pb, 18); /* nb_entries */
|
|
|
|
|
|
|
|
write_tiff_long(pb, TIFF_SUBFILE, 0);
|
|
|
|
write_tiff_long(pb, TIFF_WIDTH, st->codecpar->width);
|
|
|
|
write_tiff_long(pb, TIFF_HEIGHT, st->codecpar->height);
|
|
|
|
write_tiff_long(pb, TIFF_BPP, st->codecpar->bits_per_coded_sample);
|
|
|
|
write_tiff_short(pb, TIFF_COMPR, TIFF_NEWJPEG);
|
|
|
|
write_tiff_short(pb, TIFF_PHOTOMETRIC, TIFF_PHOTOMETRIC_CFA);
|
|
|
|
write_tiff_short(pb, TIFF_FILL_ORDER, 1);
|
|
|
|
write_tiff_long(pb, TIFF_STRIP_OFFS, 0); /* stripofs */
|
|
|
|
stripofs = pb->buffer - 4;
|
|
|
|
|
|
|
|
write_tiff_long(pb, TIFF_SAMPLES_PER_PIXEL, 1);
|
|
|
|
write_tiff_short(pb, TIFF_ROWSPERSTRIP, st->codecpar->height);
|
|
|
|
write_tiff_long(pb, TIFF_STRIP_SIZE, size);
|
|
|
|
write_tiff_short(pb, TIFF_PLANAR, 1);
|
|
|
|
write_tiff_short2(pb, TIFF_CFA_PATTERN_DIM, 2, 2);
|
|
|
|
write_tiff_byte4(pb, TIFF_CFA_PATTERN, 0, 1, 1, 2);
|
|
|
|
write_tiff_byte4(pb, DNG_VERSION, 1, 4, 0, 0);
|
|
|
|
write_tiff_long(pb, DNG_BLACK_LEVEL, mlv->black_level);
|
|
|
|
write_tiff_long(pb, DNG_WHITE_LEVEL, mlv->white_level);
|
|
|
|
|
|
|
|
bytestream2_put_le16(pb, DNG_COLOR_MATRIX1);
|
|
|
|
bytestream2_put_le16(pb, TIFF_SRATIONAL);
|
|
|
|
bytestream2_put_le32(pb, 9);
|
|
|
|
bytestream2_put_le32(pb, 0); /* matrixofs */
|
|
|
|
matrixofs = pb->buffer - 4;
|
|
|
|
bytestream2_put_le32(pb, 0);
|
|
|
|
|
|
|
|
AV_WL32(matrixofs, bytestream2_tell_p(pb));
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
bytestream2_put_le32(pb, mlv->color_matrix1[i][0]);
|
|
|
|
bytestream2_put_le32(pb, mlv->color_matrix1[i][1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
header_size = bytestream2_tell_p(pb);
|
|
|
|
|
|
|
|
AV_WL32(stripofs, header_size);
|
|
|
|
ret = avio_read(pbio, pkt->data + header_size, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-17 20:43:11 +10:00
|
|
|
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
MlvContext *mlv = avctx->priv_data;
|
|
|
|
AVIOContext *pb;
|
2020-05-31 14:30:43 +02:00
|
|
|
AVStream *st;
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *sti;
|
2014-04-17 20:43:11 +10:00
|
|
|
int index, ret;
|
|
|
|
unsigned int size, space;
|
|
|
|
|
2020-05-31 14:30:43 +02:00
|
|
|
if (!avctx->nb_streams)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
st = avctx->streams[mlv->stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
sti = ffstream(st);
|
2014-04-17 20:43:11 +10:00
|
|
|
if (mlv->pts >= st->duration)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
index = av_index_search_timestamp(st, mlv->pts, AVSEEK_FLAG_ANY);
|
|
|
|
if (index < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "could not find index entry for frame %"PRId64"\n", mlv->pts);
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:41:16 +02:00
|
|
|
pb = mlv->pb[sti->index_entries[index].size];
|
2020-08-10 01:32:42 +02:00
|
|
|
if (!pb) {
|
|
|
|
ret = FFERROR_REDO;
|
|
|
|
goto next_packet;
|
|
|
|
}
|
2021-08-24 19:41:16 +02:00
|
|
|
avio_seek(pb, sti->index_entries[index].pos, SEEK_SET);
|
2014-04-17 20:43:11 +10:00
|
|
|
|
|
|
|
avio_skip(pb, 4); // blockType
|
|
|
|
size = avio_rl32(pb);
|
|
|
|
if (size < 16)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(pb, 12); //timestamp, frameNumber
|
2024-12-13 16:07:03 +11:00
|
|
|
size -= 12;
|
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2014-04-17 20:43:11 +10:00
|
|
|
avio_skip(pb, 8); // cropPosX, cropPosY, panPosX, panPosY
|
2024-12-13 16:07:03 +11:00
|
|
|
size -= 8;
|
|
|
|
}
|
2014-04-17 20:43:11 +10:00
|
|
|
space = avio_rl32(pb);
|
|
|
|
avio_skip(pb, space);
|
2024-12-13 16:07:03 +11:00
|
|
|
size -= space;
|
2014-04-17 20:43:11 +10:00
|
|
|
|
|
|
|
if ((mlv->class[st->id] & (MLV_CLASS_FLAG_DELTA|MLV_CLASS_FLAG_LZMA))) {
|
|
|
|
ret = AVERROR_PATCHWELCOME;
|
2016-04-10 20:58:15 +01:00
|
|
|
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2024-12-13 16:07:03 +11:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_TIFF)
|
|
|
|
ret = get_packet_lj92(avctx, st, pb, pkt, size);
|
|
|
|
else
|
|
|
|
ret = av_get_packet(pb, pkt, (st->codecpar->width * st->codecpar->height * st->codecpar->bits_per_coded_sample + 7) >> 3);
|
2014-04-17 20:43:11 +10:00
|
|
|
} else { // AVMEDIA_TYPE_AUDIO
|
|
|
|
if (space > UINT_MAX - 24 || size < (24 + space))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
ret = av_get_packet(pb, pkt, size - (24 + space));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pkt->stream_index = mlv->stream_index;
|
|
|
|
pkt->pts = mlv->pts;
|
|
|
|
|
2020-08-10 01:32:42 +02:00
|
|
|
ret = 0;
|
|
|
|
next_packet:
|
2014-04-17 20:43:11 +10:00
|
|
|
mlv->stream_index++;
|
|
|
|
if (mlv->stream_index == avctx->nb_streams) {
|
|
|
|
mlv->stream_index = 0;
|
|
|
|
mlv->pts++;
|
|
|
|
}
|
2020-08-10 01:32:42 +02:00
|
|
|
return ret;
|
2014-04-17 20:43:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
|
|
|
|
{
|
|
|
|
MlvContext *mlv = avctx->priv_data;
|
|
|
|
|
|
|
|
if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
2017-03-21 17:02:30 -03:00
|
|
|
if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
|
2014-04-17 20:43:11 +10:00
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
mlv->pts = timestamp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
MlvContext *mlv = s->priv_data;
|
2014-04-27 12:44:16 +10:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 100; i++)
|
2019-11-09 11:27:14 +09:00
|
|
|
ff_format_io_close(s, &mlv->pb[i]);
|
2014-04-17 20:43:11 +10:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-02-10 15:50:43 +01:00
|
|
|
const FFInputFormat ff_mlv_demuxer = {
|
|
|
|
.p.name = "mlv",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Magic Lantern Video (MLV)"),
|
2014-04-17 20:43:11 +10:00
|
|
|
.priv_data_size = sizeof(MlvContext),
|
2024-03-15 18:08:11 +01:00
|
|
|
.flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
|
2014-04-17 20:43:11 +10:00
|
|
|
.read_probe = probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
.read_close = read_close,
|
|
|
|
.read_seek = read_seek,
|
|
|
|
};
|