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.

480 lines
17KB

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