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.

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