3 * Copyright (c) 2010-2011 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
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.
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.
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
24 * Phantom Cine demuxer
25 * @author Peter Ross <pross@xvid.org>
28 #include "libavutil/intreadwrite.h"
29 #include "libavcodec/bmp.h"
39 CC_RGB = 0, /**< Gray */
40 CC_LEAD = 1, /**< LEAD (M)JPEG */
41 CC_UNINT = 2 /**< Uninterpolated color image (CFA field indicates color ordering) */
44 /** Color Filter Array */
46 CFA_NONE = 0, /**< GRAY */
47 CFA_VRI = 1, /**< GBRG/RGGB */
48 CFA_VRIV6 = 2, /**< BGGR/GRBG */
49 CFA_BAYER = 3, /**< GB/RG */
50 CFA_BAYERFLIP = 4, /**< RG/GB */
52 CFA_TLGRAY = 0x80000000,
53 CFA_TRGRAY = 0x40000000,
54 CFA_BLGRAY = 0x20000000,
55 CFA_BRGRAY = 0x10000000
58 static int cine_read_probe(AVProbeData *p)
61 if (p->buf[0] == 'C' && p->buf[1] == 'I' && // Type
62 (HeaderSize = AV_RL16(p->buf + 2)) >= 0x2C && // HeaderSize
63 AV_RL16(p->buf + 4) <= CC_UNINT && // Compression
64 AV_RL16(p->buf + 6) <= 1 && // Version
65 AV_RL32(p->buf + 20) && // ImageCount
66 AV_RL32(p->buf + 24) >= HeaderSize && // OffImageHeader
67 AV_RL32(p->buf + 28) >= HeaderSize && // OffSetup
68 AV_RL32(p->buf + 32) >= HeaderSize) // OffImageOffsets
69 return AVPROBE_SCORE_MAX;
73 static int set_metadata_int(AVDictionary **dict, const char *key, int value)
77 snprintf(buf, sizeof(buf), "%i", value);
78 return av_dict_set(dict, key, buf, 0);
83 static int cine_read_header(AVFormatContext *avctx)
85 AVIOContext *pb = avctx->pb;
87 unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
92 st = avformat_new_stream(avctx, NULL);
94 return AVERROR(ENOMEM);
95 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
96 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
97 st->codec->codec_tag = 0;
99 /* CINEFILEHEADER structure */
100 avio_skip(pb, 4); // Type, Headersize
102 compression = avio_rl16(pb);
103 version = avio_rl16(pb);
105 avpriv_request_sample(avctx, "uknown version %i", version);
106 return AVERROR_INVALIDDATA;
109 avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
111 st->duration = avio_rl32(pb);
112 offImageHeader = avio_rl32(pb);
113 offSetup = avio_rl32(pb);
114 offImageOffsets = avio_rl32(pb);
116 avio_skip(pb, 8); // TriggerTime
118 /* BITMAPINFOHEADER structure */
119 avio_seek(pb, offImageHeader, SEEK_SET);
120 avio_skip(pb, 4); //biSize
121 st->codec->width = avio_rl32(pb);
122 st->codec->height = avio_rl32(pb);
124 if (avio_rl16(pb) != 1) // biPlanes
125 return AVERROR_INVALIDDATA;
127 biBitCount = avio_rl16(pb);
128 if (biBitCount != 8 && biBitCount != 16 && biBitCount != 24 && biBitCount != 48) {
129 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
130 return AVERROR_INVALIDDATA;
133 switch (avio_rl32(pb)) {
137 case 0x100: /* BI_PACKED */
138 st->codec->codec_tag = MKTAG('B', 'I', 'T', 0);
142 avpriv_request_sample(avctx, "unknown bitmap compression");
143 return AVERROR_INVALIDDATA;
146 avio_skip(pb, 4); // biSizeImage
148 /* parse SETUP structure */
149 avio_seek(pb, offSetup, SEEK_SET);
150 avio_skip(pb, 140); // FrameRatae16 .. descriptionOld
151 if (avio_rl16(pb) != 0x5453)
152 return AVERROR_INVALIDDATA;
153 length = avio_rl16(pb);
154 if (length < 0x163C) {
155 avpriv_request_sample(avctx, "short SETUP header");
156 return AVERROR_INVALIDDATA;
159 avio_skip(pb, 616); // Binning .. bFlipH
160 if (!avio_rl32(pb) ^ vflip) {
161 st->codec->extradata = av_strdup("BottomUp");
162 st->codec->extradata_size = 9;
165 avio_skip(pb, 4); // Grid
167 avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
169 avio_skip(pb, 20); // Shutter .. bEnableColor
171 set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb));
172 set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb));
173 set_metadata_int(&st->metadata, "software_version", avio_rl32(pb));
174 set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb));
178 set_metadata_int(&st->metadata, "brightness", avio_rl32(pb));
179 set_metadata_int(&st->metadata, "contrast", avio_rl32(pb));
180 set_metadata_int(&st->metadata, "gamma", avio_rl32(pb));
182 avio_skip(pb, 72); // Reserved1 .. WBView
184 st->codec->bits_per_coded_sample = avio_rl32(pb);
186 if (compression == CC_RGB) {
187 if (biBitCount == 8) {
188 st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
189 } else if (biBitCount == 16) {
190 st->codec->pix_fmt = AV_PIX_FMT_GRAY16LE;
191 } else if (biBitCount == 24) {
192 st->codec->pix_fmt = AV_PIX_FMT_BGR24;
193 } else if (biBitCount == 48) {
194 st->codec->pix_fmt = AV_PIX_FMT_BGR48LE;
196 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
197 return AVERROR_INVALIDDATA;
199 } else if (compression == CC_UNINT) {
200 switch (CFA & 0xFFFFFF) {
202 if (biBitCount == 8) {
203 st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
204 } else if (biBitCount == 16) {
205 st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG16LE;
207 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
208 return AVERROR_INVALIDDATA;
212 if (biBitCount == 8) {
213 st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
214 } else if (biBitCount == 16) {
215 st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB16LE;
217 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
218 return AVERROR_INVALIDDATA;
222 avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
223 return AVERROR_INVALIDDATA;
226 avpriv_request_sample(avctx, "unsupported compression %i", compression);
227 return AVERROR_INVALIDDATA;
230 avio_skip(pb, 696); // Conv8Min ... ImHeightAcq
232 #define DESCRIPTION_SIZE 4096
233 description = av_malloc(DESCRIPTION_SIZE + 1);
235 return AVERROR(ENOMEM);
236 i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
237 if (i < DESCRIPTION_SIZE)
238 avio_skip(pb, DESCRIPTION_SIZE - i);
240 av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
242 av_free(description);
244 /* parse image offsets */
245 avio_seek(pb, offImageOffsets, SEEK_SET);
246 for (i = 0; i < st->duration; i++)
247 av_add_index_entry(st, avio_rl64(pb), i, 0, 0, AVINDEX_KEYFRAME);
252 static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
254 CineDemuxContext *cine = avctx->priv_data;
255 AVStream *st = avctx->streams[0];
256 AVIOContext *pb = avctx->pb;
259 if (cine->pts >= st->duration)
262 avio_seek(pb, st->index_entries[cine->pts].pos, SEEK_SET);
265 return AVERROR_INVALIDDATA;
266 avio_skip(pb, n - 8);
267 size = avio_rl32(pb);
269 ret = av_get_packet(pb, pkt, size);
273 pkt->pts = cine->pts++;
274 pkt->stream_index = 0;
275 pkt->flags |= AV_PKT_FLAG_KEY;
279 static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
281 CineDemuxContext *cine = avctx->priv_data;
283 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
284 return AVERROR(ENOSYS);
286 if (!avctx->pb->seekable)
289 cine->pts = timestamp;
293 AVInputFormat ff_cine_demuxer = {
295 .long_name = NULL_IF_CONFIG_SMALL("Phantom Cine"),
296 .priv_data_size = sizeof(CineDemuxContext),
297 .read_probe = cine_read_probe,
298 .read_header = cine_read_header,
299 .read_packet = cine_read_packet,
300 .read_seek = cine_read_seek,