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.

326 lines
10KB

  1. /*
  2. * Phantom Cine demuxer
  3. * Copyright (c) 2010-2011 Peter Ross <pross@xvid.org>
  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. * Phantom Cine demuxer
  24. * @author Peter Ross <pross@xvid.org>
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavcodec/bmp.h"
  28. #include "libavutil/intfloat.h"
  29. #include "avformat.h"
  30. #include "internal.h"
  31. typedef struct {
  32. uint64_t pts;
  33. } CineDemuxContext;
  34. /** Compression */
  35. enum {
  36. CC_RGB = 0, /**< Gray */
  37. CC_LEAD = 1, /**< LEAD (M)JPEG */
  38. CC_UNINT = 2 /**< Uninterpolated color image (CFA field indicates color ordering) */
  39. };
  40. /** Color Filter Array */
  41. enum {
  42. CFA_NONE = 0, /**< GRAY */
  43. CFA_VRI = 1, /**< GBRG/RGGB */
  44. CFA_VRIV6 = 2, /**< BGGR/GRBG */
  45. CFA_BAYER = 3, /**< GB/RG */
  46. CFA_BAYERFLIP = 4, /**< RG/GB */
  47. CFA_TLGRAY = 0x80000000,
  48. CFA_TRGRAY = 0x40000000,
  49. CFA_BLGRAY = 0x20000000,
  50. CFA_BRGRAY = 0x10000000
  51. };
  52. static int cine_read_probe(AVProbeData *p)
  53. {
  54. int HeaderSize;
  55. if (p->buf[0] == 'C' && p->buf[1] == 'I' && // Type
  56. (HeaderSize = AV_RL16(p->buf + 2)) >= 0x2C && // HeaderSize
  57. AV_RL16(p->buf + 4) <= CC_UNINT && // Compression
  58. AV_RL16(p->buf + 6) <= 1 && // Version
  59. AV_RL32(p->buf + 20) && // ImageCount
  60. AV_RL32(p->buf + 24) >= HeaderSize && // OffImageHeader
  61. AV_RL32(p->buf + 28) >= HeaderSize && // OffSetup
  62. AV_RL32(p->buf + 32) >= HeaderSize) // OffImageOffsets
  63. return AVPROBE_SCORE_MAX;
  64. return 0;
  65. }
  66. static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
  67. {
  68. if (value || allow_zero) {
  69. return av_dict_set_int(dict, key, value, 0);
  70. }
  71. return 0;
  72. }
  73. static int set_metadata_float(AVDictionary **dict, const char *key, float value, int allow_zero)
  74. {
  75. if (value != 0 || allow_zero) {
  76. char tmp[64];
  77. snprintf(tmp, sizeof(tmp), "%f", value);
  78. return av_dict_set(dict, key, tmp, 0);
  79. }
  80. return 0;
  81. }
  82. static int cine_read_header(AVFormatContext *avctx)
  83. {
  84. AVIOContext *pb = avctx->pb;
  85. AVStream *st;
  86. unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
  87. int vflip;
  88. char *description;
  89. uint64_t i;
  90. st = avformat_new_stream(avctx, NULL);
  91. if (!st)
  92. return AVERROR(ENOMEM);
  93. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  94. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  95. st->codec->codec_tag = 0;
  96. /* CINEFILEHEADER structure */
  97. avio_skip(pb, 4); // Type, Headersize
  98. compression = avio_rl16(pb);
  99. version = avio_rl16(pb);
  100. if (version != 1) {
  101. avpriv_request_sample(avctx, "uknown version %i", version);
  102. return AVERROR_INVALIDDATA;
  103. }
  104. avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
  105. st->duration = avio_rl32(pb);
  106. offImageHeader = avio_rl32(pb);
  107. offSetup = avio_rl32(pb);
  108. offImageOffsets = avio_rl32(pb);
  109. avio_skip(pb, 8); // TriggerTime
  110. /* BITMAPINFOHEADER structure */
  111. avio_seek(pb, offImageHeader, SEEK_SET);
  112. avio_skip(pb, 4); //biSize
  113. st->codec->width = avio_rl32(pb);
  114. st->codec->height = avio_rl32(pb);
  115. if (avio_rl16(pb) != 1) // biPlanes
  116. return AVERROR_INVALIDDATA;
  117. biBitCount = avio_rl16(pb);
  118. if (biBitCount != 8 && biBitCount != 16 && biBitCount != 24 && biBitCount != 48) {
  119. avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
  120. return AVERROR_INVALIDDATA;
  121. }
  122. switch (avio_rl32(pb)) {
  123. case BMP_RGB:
  124. vflip = 0;
  125. break;
  126. case 0x100: /* BI_PACKED */
  127. st->codec->codec_tag = MKTAG('B', 'I', 'T', 0);
  128. vflip = 1;
  129. break;
  130. default:
  131. avpriv_request_sample(avctx, "unknown bitmap compression");
  132. return AVERROR_INVALIDDATA;
  133. }
  134. avio_skip(pb, 4); // biSizeImage
  135. /* parse SETUP structure */
  136. avio_seek(pb, offSetup, SEEK_SET);
  137. avio_skip(pb, 140); // FrameRatae16 .. descriptionOld
  138. if (avio_rl16(pb) != 0x5453)
  139. return AVERROR_INVALIDDATA;
  140. length = avio_rl16(pb);
  141. if (length < 0x163C) {
  142. avpriv_request_sample(avctx, "short SETUP header");
  143. return AVERROR_INVALIDDATA;
  144. }
  145. avio_skip(pb, 616); // Binning .. bFlipH
  146. if (!avio_rl32(pb) ^ vflip) {
  147. st->codec->extradata = av_strdup("BottomUp");
  148. st->codec->extradata_size = 9;
  149. }
  150. avio_skip(pb, 4); // Grid
  151. avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
  152. avio_skip(pb, 20); // Shutter .. bEnableColor
  153. set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb), 0);
  154. set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb), 0);
  155. set_metadata_int(&st->metadata, "software_version", avio_rl32(pb), 0);
  156. set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb), 0);
  157. CFA = avio_rl32(pb);
  158. set_metadata_int(&st->metadata, "brightness", avio_rl32(pb), 1);
  159. set_metadata_int(&st->metadata, "contrast", avio_rl32(pb), 1);
  160. set_metadata_int(&st->metadata, "gamma", avio_rl32(pb), 1);
  161. avio_skip(pb, 12 + 16); // Reserved1 .. AutoExpRect
  162. set_metadata_float(&st->metadata, "wbgain[0].r", av_int2float(avio_rl32(pb)), 1);
  163. set_metadata_float(&st->metadata, "wbgain[0].b", av_int2float(avio_rl32(pb)), 1);
  164. avio_skip(pb, 36); // WBGain[1].. WBView
  165. st->codec->bits_per_coded_sample = avio_rl32(pb);
  166. if (compression == CC_RGB) {
  167. if (biBitCount == 8) {
  168. st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
  169. } else if (biBitCount == 16) {
  170. st->codec->pix_fmt = AV_PIX_FMT_GRAY16LE;
  171. } else if (biBitCount == 24) {
  172. st->codec->pix_fmt = AV_PIX_FMT_BGR24;
  173. } else if (biBitCount == 48) {
  174. st->codec->pix_fmt = AV_PIX_FMT_BGR48LE;
  175. } else {
  176. avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. } else if (compression == CC_UNINT) {
  180. switch (CFA & 0xFFFFFF) {
  181. case CFA_BAYER:
  182. if (biBitCount == 8) {
  183. st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
  184. } else if (biBitCount == 16) {
  185. st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG16LE;
  186. } else {
  187. avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
  188. return AVERROR_INVALIDDATA;
  189. }
  190. break;
  191. case CFA_BAYERFLIP:
  192. if (biBitCount == 8) {
  193. st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
  194. } else if (biBitCount == 16) {
  195. st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB16LE;
  196. } else {
  197. avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
  198. return AVERROR_INVALIDDATA;
  199. }
  200. break;
  201. default:
  202. avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
  203. return AVERROR_INVALIDDATA;
  204. }
  205. } else { //CC_LEAD
  206. avpriv_request_sample(avctx, "unsupported compression %i", compression);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. avio_skip(pb, 668); // Conv8Min ... Sensor
  210. set_metadata_int(&st->metadata, "shutter_ns", avio_rl32(pb), 0);
  211. avio_skip(pb, 24); // EDRShutterNs ... ImHeightAcq
  212. #define DESCRIPTION_SIZE 4096
  213. description = av_malloc(DESCRIPTION_SIZE + 1);
  214. if (!description)
  215. return AVERROR(ENOMEM);
  216. i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
  217. if (i < DESCRIPTION_SIZE)
  218. avio_skip(pb, DESCRIPTION_SIZE - i);
  219. if (description[0])
  220. av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
  221. else
  222. av_free(description);
  223. avio_skip(pb, 1176); // RisingEdge ... cmUser
  224. set_metadata_int(&st->metadata, "enable_crop", avio_rl32(pb), 1);
  225. set_metadata_int(&st->metadata, "crop_left", avio_rl32(pb), 1);
  226. set_metadata_int(&st->metadata, "crop_top", avio_rl32(pb), 1);
  227. set_metadata_int(&st->metadata, "crop_right", avio_rl32(pb), 1);
  228. set_metadata_int(&st->metadata, "crop_bottom", avio_rl32(pb), 1);
  229. /* parse image offsets */
  230. avio_seek(pb, offImageOffsets, SEEK_SET);
  231. for (i = 0; i < st->duration; i++)
  232. av_add_index_entry(st, avio_rl64(pb), i, 0, 0, AVINDEX_KEYFRAME);
  233. return 0;
  234. }
  235. static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  236. {
  237. CineDemuxContext *cine = avctx->priv_data;
  238. AVStream *st = avctx->streams[0];
  239. AVIOContext *pb = avctx->pb;
  240. int n, size, ret;
  241. if (cine->pts >= st->duration)
  242. return AVERROR_EOF;
  243. avio_seek(pb, st->index_entries[cine->pts].pos, SEEK_SET);
  244. n = avio_rl32(pb);
  245. if (n < 8)
  246. return AVERROR_INVALIDDATA;
  247. avio_skip(pb, n - 8);
  248. size = avio_rl32(pb);
  249. ret = av_get_packet(pb, pkt, size);
  250. if (ret < 0)
  251. return ret;
  252. pkt->pts = cine->pts++;
  253. pkt->stream_index = 0;
  254. pkt->flags |= AV_PKT_FLAG_KEY;
  255. return 0;
  256. }
  257. static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
  258. {
  259. CineDemuxContext *cine = avctx->priv_data;
  260. if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
  261. return AVERROR(ENOSYS);
  262. if (!avctx->pb->seekable)
  263. return AVERROR(EIO);
  264. cine->pts = timestamp;
  265. return 0;
  266. }
  267. AVInputFormat ff_cine_demuxer = {
  268. .name = "cine",
  269. .long_name = NULL_IF_CONFIG_SMALL("Phantom Cine"),
  270. .priv_data_size = sizeof(CineDemuxContext),
  271. .read_probe = cine_read_probe,
  272. .read_header = cine_read_header,
  273. .read_packet = cine_read_packet,
  274. .read_seek = cine_read_seek,
  275. };