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.

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