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.

312 lines
9.8KB

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