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"
30 #include "libavutil/intfloat.h"
40 CC_RGB = 0, /**< Gray */
41 CC_LEAD = 1, /**< LEAD (M)JPEG */
42 CC_UNINT = 2 /**< Uninterpolated color image (CFA field indicates color ordering) */
45 /** Color Filter Array */
47 CFA_NONE = 0, /**< GRAY */
48 CFA_VRI = 1, /**< GBRG/RGGB */
49 CFA_VRIV6 = 2, /**< BGGR/GRBG */
50 CFA_BAYER = 3, /**< GB/RG */
51 CFA_BAYERFLIP = 4, /**< RG/GB */
54 #define CFA_TLGRAY 0x80000000U
55 #define CFA_TRGRAY 0x40000000U
56 #define CFA_BLGRAY 0x20000000U
57 #define CFA_BRGRAY 0x10000000U
59 static int cine_read_probe(const AVProbeData *p)
62 if (p->buf[0] == 'C' && p->buf[1] == 'I' && // Type
63 (HeaderSize = AV_RL16(p->buf + 2)) >= 0x2C && // HeaderSize
64 AV_RL16(p->buf + 4) <= CC_UNINT && // Compression
65 AV_RL16(p->buf + 6) <= 1 && // Version
66 AV_RL32(p->buf + 20) && // ImageCount
67 AV_RL32(p->buf + 24) >= HeaderSize && // OffImageHeader
68 AV_RL32(p->buf + 28) >= HeaderSize && // OffSetup
69 AV_RL32(p->buf + 32) >= HeaderSize) // OffImageOffsets
70 return AVPROBE_SCORE_MAX;
74 static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
76 if (value || allow_zero) {
77 return av_dict_set_int(dict, key, value, 0);
82 static int set_metadata_float(AVDictionary **dict, const char *key, float value, int allow_zero)
84 if (value != 0 || allow_zero) {
86 snprintf(tmp, sizeof(tmp), "%f", value);
87 return av_dict_set(dict, key, tmp, 0);
92 static int cine_read_header(AVFormatContext *avctx)
94 AVIOContext *pb = avctx->pb;
96 unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
101 st = avformat_new_stream(avctx, NULL);
103 return AVERROR(ENOMEM);
104 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
105 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
106 st->codecpar->codec_tag = 0;
108 /* CINEFILEHEADER structure */
109 avio_skip(pb, 4); // Type, Headersize
111 compression = avio_rl16(pb);
112 version = avio_rl16(pb);
114 avpriv_request_sample(avctx, "unknown version %i", version);
115 return AVERROR_INVALIDDATA;
118 avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
120 st->duration = avio_rl32(pb);
121 offImageHeader = avio_rl32(pb);
122 offSetup = avio_rl32(pb);
123 offImageOffsets = avio_rl32(pb);
125 avio_skip(pb, 8); // TriggerTime
127 /* BITMAPINFOHEADER structure */
128 avio_seek(pb, offImageHeader, SEEK_SET);
129 avio_skip(pb, 4); //biSize
130 st->codecpar->width = avio_rl32(pb);
131 st->codecpar->height = avio_rl32(pb);
133 if (avio_rl16(pb) != 1) // biPlanes
134 return AVERROR_INVALIDDATA;
136 biBitCount = avio_rl16(pb);
137 if (biBitCount != 8 && biBitCount != 16 && biBitCount != 24 && biBitCount != 48) {
138 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
139 return AVERROR_INVALIDDATA;
142 switch (avio_rl32(pb)) {
146 case 0x100: /* BI_PACKED */
147 st->codecpar->codec_tag = MKTAG('B', 'I', 'T', 0);
151 avpriv_request_sample(avctx, "unknown bitmap compression");
152 return AVERROR_INVALIDDATA;
155 avio_skip(pb, 4); // biSizeImage
157 /* parse SETUP structure */
158 avio_seek(pb, offSetup, SEEK_SET);
159 avio_skip(pb, 140); // FrameRatae16 .. descriptionOld
160 if (avio_rl16(pb) != 0x5453)
161 return AVERROR_INVALIDDATA;
162 length = avio_rl16(pb);
163 if (length < 0x163C) {
164 avpriv_request_sample(avctx, "short SETUP header");
165 return AVERROR_INVALIDDATA;
168 avio_skip(pb, 616); // Binning .. bFlipH
169 if (!avio_rl32(pb) ^ vflip) {
170 st->codecpar->extradata = av_strdup("BottomUp");
171 if (!st->codecpar->extradata) {
172 st->codecpar->extradata_size = 0;
173 return AVERROR(ENOMEM);
175 st->codecpar->extradata_size = 9;
178 avio_skip(pb, 4); // Grid
180 avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
182 avio_skip(pb, 20); // Shutter .. bEnableColor
184 set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb), 0);
185 set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb), 0);
186 set_metadata_int(&st->metadata, "software_version", avio_rl32(pb), 0);
187 set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb), 0);
191 set_metadata_int(&st->metadata, "brightness", avio_rl32(pb), 1);
192 set_metadata_int(&st->metadata, "contrast", avio_rl32(pb), 1);
193 set_metadata_int(&st->metadata, "gamma", avio_rl32(pb), 1);
195 avio_skip(pb, 12 + 16); // Reserved1 .. AutoExpRect
196 set_metadata_float(&st->metadata, "wbgain[0].r", av_int2float(avio_rl32(pb)), 1);
197 set_metadata_float(&st->metadata, "wbgain[0].b", av_int2float(avio_rl32(pb)), 1);
198 avio_skip(pb, 36); // WBGain[1].. WBView
200 st->codecpar->bits_per_coded_sample = avio_rl32(pb);
202 if (compression == CC_RGB) {
203 if (biBitCount == 8) {
204 st->codecpar->format = AV_PIX_FMT_GRAY8;
205 } else if (biBitCount == 16) {
206 st->codecpar->format = AV_PIX_FMT_GRAY16LE;
207 } else if (biBitCount == 24) {
208 st->codecpar->format = AV_PIX_FMT_BGR24;
209 } else if (biBitCount == 48) {
210 st->codecpar->format = AV_PIX_FMT_BGR48LE;
212 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
213 return AVERROR_INVALIDDATA;
215 } else if (compression == CC_UNINT) {
216 switch (CFA & 0xFFFFFF) {
218 if (biBitCount == 8) {
219 st->codecpar->format = AV_PIX_FMT_BAYER_GBRG8;
220 } else if (biBitCount == 16) {
221 st->codecpar->format = AV_PIX_FMT_BAYER_GBRG16LE;
223 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
224 return AVERROR_INVALIDDATA;
228 if (biBitCount == 8) {
229 st->codecpar->format = AV_PIX_FMT_BAYER_RGGB8;
230 } else if (biBitCount == 16) {
231 st->codecpar->format = AV_PIX_FMT_BAYER_RGGB16LE;
233 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
234 return AVERROR_INVALIDDATA;
238 avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
239 return AVERROR_INVALIDDATA;
242 avpriv_request_sample(avctx, "unsupported compression %i", compression);
243 return AVERROR_INVALIDDATA;
246 avio_skip(pb, 668); // Conv8Min ... Sensor
248 set_metadata_int(&st->metadata, "shutter_ns", avio_rl32(pb), 0);
250 avio_skip(pb, 24); // EDRShutterNs ... ImHeightAcq
252 #define DESCRIPTION_SIZE 4096
253 description = av_malloc(DESCRIPTION_SIZE + 1);
255 return AVERROR(ENOMEM);
256 i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
257 if (i < DESCRIPTION_SIZE)
258 avio_skip(pb, DESCRIPTION_SIZE - i);
260 av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
262 av_free(description);
264 avio_skip(pb, 1176); // RisingEdge ... cmUser
266 set_metadata_int(&st->metadata, "enable_crop", avio_rl32(pb), 1);
267 set_metadata_int(&st->metadata, "crop_left", avio_rl32(pb), 1);
268 set_metadata_int(&st->metadata, "crop_top", avio_rl32(pb), 1);
269 set_metadata_int(&st->metadata, "crop_right", avio_rl32(pb), 1);
270 set_metadata_int(&st->metadata, "crop_bottom", avio_rl32(pb), 1);
272 /* parse image offsets */
273 avio_seek(pb, offImageOffsets, SEEK_SET);
274 for (i = 0; i < st->duration; i++) {
276 return AVERROR_INVALIDDATA;
278 av_add_index_entry(st, avio_rl64(pb), i, 0, 0, AVINDEX_KEYFRAME);
284 static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
286 CineDemuxContext *cine = avctx->priv_data;
287 AVStream *st = avctx->streams[0];
288 AVIOContext *pb = avctx->pb;
291 if (cine->pts >= st->duration)
294 avio_seek(pb, st->index_entries[cine->pts].pos, SEEK_SET);
297 return AVERROR_INVALIDDATA;
298 avio_skip(pb, n - 8);
299 size = avio_rl32(pb);
301 ret = av_get_packet(pb, pkt, size);
305 pkt->pts = cine->pts++;
306 pkt->stream_index = 0;
307 pkt->flags |= AV_PKT_FLAG_KEY;
311 static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
313 CineDemuxContext *cine = avctx->priv_data;
315 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
316 return AVERROR(ENOSYS);
318 if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
321 cine->pts = timestamp;
325 AVInputFormat ff_cine_demuxer = {
327 .long_name = NULL_IF_CONFIG_SMALL("Phantom Cine"),
328 .priv_data_size = sizeof(CineDemuxContext),
329 .read_probe = cine_read_probe,
330 .read_header = cine_read_header,
331 .read_packet = cine_read_packet,
332 .read_seek = cine_read_seek,