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.

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