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.

302 lines
9.3KB

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