You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

464 lines
16KB

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