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.

301 lines
9.2KB

  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)
  66. {
  67. if (value) {
  68. char buf[64];
  69. snprintf(buf, sizeof(buf - 1), "%i", value);
  70. buf[sizeof(buf) - 1] = 0;
  71. return av_dict_set(dict, key, buf, 0);
  72. }
  73. return 0;
  74. }
  75. static int cine_read_header(AVFormatContext *avctx)
  76. {
  77. AVIOContext *pb = avctx->pb;
  78. AVStream *st;
  79. unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
  80. int vflip;
  81. char *description;
  82. uint64_t i;
  83. st = avformat_new_stream(avctx, NULL);
  84. if (!st)
  85. return AVERROR(ENOMEM);
  86. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  87. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  88. st->codec->codec_tag = 0;
  89. /* CINEFILEHEADER structure */
  90. avio_skip(pb, 4); // Type, Headersize
  91. compression = avio_rl16(pb);
  92. version = avio_rl16(pb);
  93. if (version != 1) {
  94. avpriv_request_sample(avctx, "uknown version %i", version);
  95. return AVERROR_INVALIDDATA;
  96. }
  97. avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
  98. st->duration = avio_rl32(pb);
  99. offImageHeader = avio_rl32(pb);
  100. offSetup = avio_rl32(pb);
  101. offImageOffsets = avio_rl32(pb);
  102. avio_skip(pb, 8); // TriggerTime
  103. /* BITMAPINFOHEADER structure */
  104. avio_seek(pb, offImageHeader, SEEK_SET);
  105. avio_skip(pb, 4); //biSize
  106. st->codec->width = avio_rl32(pb);
  107. st->codec->height = avio_rl32(pb);
  108. if (avio_rl16(pb) != 1) // biPlanes
  109. return AVERROR_INVALIDDATA;
  110. biBitCount = avio_rl16(pb);
  111. if (biBitCount != 8 && biBitCount != 16 && biBitCount != 24 && biBitCount != 48)
  112. return AVERROR_INVALIDDATA;
  113. switch (avio_rl32(pb)) {
  114. case BMP_RGB:
  115. vflip = 0;
  116. break;
  117. case 0x100: /* BI_PACKED */
  118. st->codec->codec_tag = MKTAG('B', 'I', 'T', 0);
  119. vflip = 1;
  120. break;
  121. default:
  122. avpriv_request_sample(avctx, "unknown bitmap compression");
  123. return AVERROR_INVALIDDATA;
  124. }
  125. avio_skip(pb, 4); // biSizeImage
  126. /* parse SETUP structure */
  127. avio_seek(pb, offSetup, SEEK_SET);
  128. avio_skip(pb, 140); // FrameRatae16 .. descriptionOld
  129. if (avio_rl16(pb) != 0x5453)
  130. return AVERROR_INVALIDDATA;
  131. length = avio_rl16(pb);
  132. if (length < 0x163C) {
  133. avpriv_request_sample(avctx, "short SETUP header");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. avio_skip(pb, 616); // Binning .. bFlipH
  137. if (!avio_rl32(pb) ^ vflip) {
  138. st->codec->extradata = av_strdup("BottomUp");
  139. st->codec->extradata_size = 9;
  140. }
  141. avio_skip(pb, 4); // Grid
  142. avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
  143. avio_skip(pb, 20); // Shutter .. bEnableColor
  144. set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb));
  145. set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb));
  146. set_metadata_int(&st->metadata, "software_version", avio_rl32(pb));
  147. set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb));
  148. CFA = avio_rl32(pb);
  149. set_metadata_int(&st->metadata, "brightness", avio_rl32(pb));
  150. set_metadata_int(&st->metadata, "contrast", avio_rl32(pb));
  151. set_metadata_int(&st->metadata, "gamma", avio_rl32(pb));
  152. avio_skip(pb, 72); // Reserved1 .. WBView
  153. st->codec->bits_per_coded_sample = avio_rl32(pb);
  154. if (compression == CC_RGB) {
  155. if (biBitCount == 8) {
  156. st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
  157. } else if (biBitCount == 16) {
  158. st->codec->pix_fmt = AV_PIX_FMT_GRAY16LE;
  159. } else if (biBitCount == 24) {
  160. st->codec->pix_fmt = AV_PIX_FMT_BGR24;
  161. } else if (biBitCount == 48) {
  162. st->codec->pix_fmt = AV_PIX_FMT_BGR48LE;
  163. } else {
  164. avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
  165. return AVERROR_INVALIDDATA;
  166. }
  167. } else if (compression == CC_UNINT) {
  168. switch (CFA & 0xFFFFFF) {
  169. case CFA_BAYER:
  170. if (biBitCount == 8) {
  171. st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
  172. } else if (biBitCount == 16) {
  173. st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG16LE;
  174. } else {
  175. avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. break;
  179. case CFA_BAYERFLIP:
  180. if (biBitCount == 8) {
  181. st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
  182. } else if (biBitCount == 16) {
  183. st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB16LE;
  184. } else {
  185. avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
  186. return AVERROR_INVALIDDATA;
  187. }
  188. break;
  189. default:
  190. avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
  191. return AVERROR_INVALIDDATA;
  192. }
  193. } else { //CC_LEAD
  194. avpriv_request_sample(avctx, "unsupported compression %i", compression);
  195. return AVERROR_INVALIDDATA;
  196. }
  197. avio_skip(pb, 696); // Conv8Min ... ImHeightAcq
  198. #define DESCRIPTION_SIZE 4096
  199. description = av_malloc(DESCRIPTION_SIZE + 1);
  200. if (!description)
  201. return AVERROR(ENOMEM);
  202. i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
  203. if (i < DESCRIPTION_SIZE)
  204. avio_skip(pb, DESCRIPTION_SIZE - i);
  205. if (description[0])
  206. av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
  207. else
  208. av_free(description);
  209. /* parse image offsets */
  210. avio_seek(pb, offImageOffsets, SEEK_SET);
  211. for (i = 0; i < st->duration; i++)
  212. av_add_index_entry(st, avio_rl64(pb), i, 0, 0, AVINDEX_KEYFRAME);
  213. return 0;
  214. }
  215. static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  216. {
  217. CineDemuxContext *cine = avctx->priv_data;
  218. AVStream *st = avctx->streams[0];
  219. AVIOContext *pb = avctx->pb;
  220. int n, size, ret;
  221. if (cine->pts >= st->duration)
  222. return AVERROR_EOF;
  223. avio_seek(pb, st->index_entries[cine->pts].pos, SEEK_SET);
  224. n = avio_rl32(pb);
  225. if (n < 8)
  226. return AVERROR_INVALIDDATA;
  227. avio_skip(pb, n - 8);
  228. size = avio_rl32(pb);
  229. ret = av_get_packet(pb, pkt, size);
  230. if (ret < 0)
  231. return ret;
  232. pkt->pts = cine->pts++;
  233. pkt->stream_index = 0;
  234. pkt->flags |= AV_PKT_FLAG_KEY;
  235. return 0;
  236. }
  237. static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
  238. {
  239. CineDemuxContext *cine = avctx->priv_data;
  240. if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
  241. return AVERROR(ENOSYS);
  242. if (!avctx->pb->seekable)
  243. return AVERROR(EIO);
  244. cine->pts = timestamp;
  245. return 0;
  246. }
  247. AVInputFormat ff_cine_demuxer = {
  248. .name = "cine",
  249. .long_name = NULL_IF_CONFIG_SMALL("Phantom Cine"),
  250. .priv_data_size = sizeof(CineDemuxContext),
  251. .read_probe = cine_read_probe,
  252. .read_header = cine_read_header,
  253. .read_packet = cine_read_packet,
  254. .read_seek = cine_read_seek,
  255. };