FFmpeg
Loading...
Searching...
No Matches
cinedec.c
Go to the documentation of this file.
1/*
2 * Phantom 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/**
23 * @file
24 * Phantom Cine demuxer
25 * @author Peter Ross <pross@xvid.org>
26 */
27
29#include "libavutil/mem.h"
30#include "libavcodec/bmp.h"
31#include "libavutil/intfloat.h"
32#include "avformat.h"
33#include "demux.h"
34#include "internal.h"
35
36typedef struct {
37 uint64_t pts;
38 uint64_t maxsize;
40
41/** Compression */
42enum {
43 CC_RGB = 0, /**< Gray */
44 CC_LEAD = 1, /**< LEAD (M)JPEG */
45 CC_UNINT = 2 /**< Uninterpolated color image (CFA field indicates color ordering) */
46};
47
48/** Color Filter Array */
49enum {
50 CFA_NONE = 0, /**< GRAY */
51 CFA_VRI = 1, /**< GBRG/RGGB */
52 CFA_VRIV6 = 2, /**< BGGR/GRBG */
53 CFA_BAYER = 3, /**< GB/RG */
54 CFA_BAYERFLIP = 4, /**< RG/GB */
55 CFA_BAYERFLIPB = 5, /**< GR/BG */
56 CFA_BAYERFLIPH = 6, /**< BG/GR */
57};
58
59#define CFA_TLGRAY 0x80000000U
60#define CFA_TRGRAY 0x40000000U
61#define CFA_BLGRAY 0x20000000U
62#define CFA_BRGRAY 0x10000000U
63
64static int cine_read_probe(const AVProbeData *p)
65{
66 int HeaderSize;
67 if (p->buf[0] == 'C' && p->buf[1] == 'I' && // Type
68 (HeaderSize = AV_RL16(p->buf + 2)) >= 0x2C && // HeaderSize
69 AV_RL16(p->buf + 4) <= CC_UNINT && // Compression
70 AV_RL16(p->buf + 6) <= 1 && // Version
71 AV_RL32(p->buf + 20) && // ImageCount
72 AV_RL32(p->buf + 24) >= HeaderSize && // OffImageHeader
73 AV_RL32(p->buf + 28) >= HeaderSize && // OffSetup
74 AV_RL32(p->buf + 32) >= HeaderSize) // OffImageOffsets
75 return AVPROBE_SCORE_MAX;
76 return 0;
77}
78
79static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
80{
81 if (value || allow_zero) {
82 return av_dict_set_int(dict, key, value, 0);
83 }
84 return 0;
85}
86
87static int set_metadata_float(AVDictionary **dict, const char *key, float value, int allow_zero)
88{
89 if (value != 0 || allow_zero) {
90 char tmp[64];
91 snprintf(tmp, sizeof(tmp), "%f", value);
92 return av_dict_set(dict, key, tmp, 0);
93 }
94 return 0;
95}
96
98{
99 AVIOContext *pb = avctx->pb;
100 AVStream *st;
101 unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
102 int vflip;
103 char *description;
104 uint64_t i;
105
106 st = avformat_new_stream(avctx, NULL);
107 if (!st)
108 return AVERROR(ENOMEM);
111 st->codecpar->codec_tag = 0;
112
113 /* CINEFILEHEADER structure */
114 avio_skip(pb, 4); // Type, Headersize
115
116 compression = avio_rl16(pb);
117 version = avio_rl16(pb);
118 if (version != 1) {
119 avpriv_request_sample(avctx, "unknown version %i", version);
120 return AVERROR_INVALIDDATA;
121 }
122
123 avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
124
125 st->duration = avio_rl32(pb);
126 offImageHeader = avio_rl32(pb);
127 offSetup = avio_rl32(pb);
128 offImageOffsets = avio_rl32(pb);
129
130 avio_skip(pb, 8); // TriggerTime
131
132 /* BITMAPINFOHEADER structure */
133 avio_seek(pb, offImageHeader, SEEK_SET);
134 avio_skip(pb, 4); //biSize
135 st->codecpar->width = avio_rl32(pb);
136 st->codecpar->height = avio_rl32(pb);
137
138 if (avio_rl16(pb) != 1) // biPlanes
139 return AVERROR_INVALIDDATA;
140
141 biBitCount = avio_rl16(pb);
142 if (biBitCount != 8 && biBitCount != 16 && biBitCount != 24 && biBitCount != 48) {
143 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
144 return AVERROR_INVALIDDATA;
145 }
146
147 switch (avio_rl32(pb)) {
148 case BMP_RGB:
149 vflip = 0;
150 break;
151 case 0x100: /* BI_PACKED */
152 st->codecpar->codec_tag = MKTAG('B', 'I', 'T', 0);
153 vflip = 1;
154 break;
155 default:
156 avpriv_request_sample(avctx, "unknown bitmap compression");
157 return AVERROR_INVALIDDATA;
158 }
159
160 avio_skip(pb, 4); // biSizeImage
161
162 /* parse SETUP structure */
163 avio_seek(pb, offSetup, SEEK_SET);
164 avio_skip(pb, 140); // FrameRatae16 .. descriptionOld
165 if (avio_rl16(pb) != 0x5453)
166 return AVERROR_INVALIDDATA;
167 length = avio_rl16(pb);
168 if (length < 0x163C) {
169 avpriv_request_sample(avctx, "short SETUP header");
170 return AVERROR_INVALIDDATA;
171 }
172
173 avio_skip(pb, 616); // Binning .. bFlipH
174 if (!avio_rl32(pb) ^ vflip) {
175 st->codecpar->extradata = av_strdup("BottomUp");
176 if (!st->codecpar->extradata) {
177 st->codecpar->extradata_size = 0;
178 return AVERROR(ENOMEM);
179 }
180 st->codecpar->extradata_size = 9;
181 }
182
183 avio_skip(pb, 4); // Grid
184
185 avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
186
187 avio_skip(pb, 20); // Shutter .. bEnableColor
188
189 set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb), 0);
190 set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb), 0);
191 set_metadata_int(&st->metadata, "software_version", avio_rl32(pb), 0);
192 set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb), 0);
193
194 CFA = avio_rl32(pb);
195
196 set_metadata_int(&st->metadata, "brightness", avio_rl32(pb), 1);
197 set_metadata_int(&st->metadata, "contrast", avio_rl32(pb), 1);
198 set_metadata_int(&st->metadata, "gamma", avio_rl32(pb), 1);
199
200 avio_skip(pb, 12 + 16); // Reserved1 .. AutoExpRect
201 set_metadata_float(&st->metadata, "wbgain[0].r", av_int2float(avio_rl32(pb)), 1);
202 set_metadata_float(&st->metadata, "wbgain[0].b", av_int2float(avio_rl32(pb)), 1);
203 avio_skip(pb, 36); // WBGain[1].. WBView
204
206
207 if (compression == CC_RGB) {
208 if (biBitCount == 8) {
210 } else if (biBitCount == 16) {
212 } else if (biBitCount == 24) {
214 } else if (biBitCount == 48) {
216 } else {
217 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
218 return AVERROR_INVALIDDATA;
219 }
220 } else if (compression == CC_UNINT) {
221 switch (CFA & 0xFFFFFF) {
222 case CFA_BAYER:
223 if (biBitCount == 8) {
225 } else if (biBitCount == 16) {
227 } else {
228 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
229 return AVERROR_INVALIDDATA;
230 }
231 break;
232 case CFA_BAYERFLIP:
233 if (biBitCount == 8) {
235 } else if (biBitCount == 16) {
237 } else {
238 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
239 return AVERROR_INVALIDDATA;
240 }
241 break;
242 case CFA_BAYERFLIPB:
243 if (biBitCount == 8) {
245 } else if (biBitCount == 16) {
247 } else {
248 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
249 return AVERROR_INVALIDDATA;
250 }
251 break;
252 case CFA_BAYERFLIPH:
253 if (biBitCount == 8) {
255 } else if (biBitCount == 16) {
257 } else {
258 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
259 return AVERROR_INVALIDDATA;
260 }
261 break;
262 default:
263 avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
264 return AVERROR_INVALIDDATA;
265 }
266 } else { //CC_LEAD
267 avpriv_request_sample(avctx, "unsupported compression %i", compression);
268 return AVERROR_INVALIDDATA;
269 }
270
271 avio_skip(pb, 668); // Conv8Min ... Sensor
272
273 set_metadata_int(&st->metadata, "shutter_ns", avio_rl32(pb), 0);
274
275 avio_skip(pb, 24); // EDRShutterNs ... ImHeightAcq
276
277#define DESCRIPTION_SIZE 4096
278 description = av_malloc(DESCRIPTION_SIZE + 1);
279 if (!description)
280 return AVERROR(ENOMEM);
281 i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
282 if (i < DESCRIPTION_SIZE)
284 if (description[0])
285 av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
286 else
287 av_free(description);
288
289 avio_skip(pb, 1176); // RisingEdge ... cmUser
290
291 set_metadata_int(&st->metadata, "enable_crop", avio_rl32(pb), 1);
292 set_metadata_int(&st->metadata, "crop_left", avio_rl32(pb), 1);
293 set_metadata_int(&st->metadata, "crop_top", avio_rl32(pb), 1);
294 set_metadata_int(&st->metadata, "crop_right", avio_rl32(pb), 1);
295 set_metadata_int(&st->metadata, "crop_bottom", avio_rl32(pb), 1);
296
297 /* parse image offsets */
298 avio_seek(pb, offImageOffsets, SEEK_SET);
299 for (i = 0; i < st->duration; i++) {
300 int64_t pos = avio_rl64(pb);
301 if (avio_feof(pb) || pos < 0)
302 return AVERROR_INVALIDDATA;
303
305 }
306
307 return 0;
308}
309
311{
312 CineDemuxContext *cine = avctx->priv_data;
313 AVStream *st = avctx->streams[0];
314 FFStream *const sti = ffstream(st);
315 AVIOContext *pb = avctx->pb;
316 int n, size, ret;
317 int64_t ret64;
318
319 if (cine->pts >= sti->nb_index_entries)
320 return AVERROR_EOF;
321
322 ret64 = avio_seek(pb, sti->index_entries[cine->pts].pos, SEEK_SET);
323 if (ret64 < 0)
324 return ret64;
325 n = avio_rl32(pb);
326 if (n < 8)
327 return AVERROR_INVALIDDATA;
328 avio_skip(pb, n - 8);
329 size = avio_rl32(pb);
330 if (avio_feof(pb) || size < 0)
331 return AVERROR_INVALIDDATA;
332
333 if (cine->maxsize && (uint64_t)sti->index_entries[cine->pts].pos + size + n > cine->maxsize)
334 size = cine->maxsize - sti->index_entries[cine->pts].pos - n;
335
336 ret = av_get_packet(pb, pkt, size);
337 if (ret < 0)
338 return ret;
339
340 if (ret != size)
341 cine->maxsize = (uint64_t)sti->index_entries[cine->pts].pos + n + ret;
342
343 pkt->pts = cine->pts++;
344 pkt->stream_index = 0;
345 pkt->flags |= AV_PKT_FLAG_KEY;
346 return 0;
347}
348
349static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
350{
351 CineDemuxContext *cine = avctx->priv_data;
352
354 return AVERROR(ENOSYS);
355
356 if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
357 return AVERROR(ENOSYS);
358
359 cine->pts = timestamp;
360 return 0;
361}
362
364 .p.name = "cine",
365 .p.long_name = NULL_IF_CONFIG_SMALL("Phantom Cine"),
366 .priv_data_size = sizeof(CineDemuxContext),
371};
const FFInputFormat ff_cine_demuxer
Definition cinedec.c:363
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition avformat.h:2617
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition avformat.h:2619
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition aviobuf.c:869
uint64_t avio_rl64(AVIOContext *s)
Definition aviobuf.c:741
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
@ BMP_RGB
Definition bmp.h:26
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
@ CFA_VRI
GBRG/RGGB.
Definition cinedec.c:51
@ CFA_BAYERFLIP
RG/GB.
Definition cinedec.c:54
@ CFA_NONE
GRAY.
Definition cinedec.c:50
@ CFA_VRIV6
BGGR/GRBG.
Definition cinedec.c:52
@ CFA_BAYERFLIPH
BG/GR.
Definition cinedec.c:56
@ CFA_BAYERFLIPB
GR/BG.
Definition cinedec.c:55
@ CFA_BAYER
GB/RG.
Definition cinedec.c:53
static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
Definition cinedec.c:349
static int set_metadata_float(AVDictionary **dict, const char *key, float value, int allow_zero)
Definition cinedec.c:87
static int cine_read_probe(const AVProbeData *p)
Definition cinedec.c:64
@ CC_RGB
Gray.
Definition cinedec.c:43
@ CC_LEAD
LEAD (M)JPEG.
Definition cinedec.c:44
@ CC_UNINT
Uninterpolated color image (CFA field indicates color ordering)
Definition cinedec.c:45
static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
Definition cinedec.c:79
#define DESCRIPTION_SIZE
static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition cinedec.c:310
static int cine_read_header(AVFormatContext *avctx)
Definition cinedec.c:97
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
double value
Definition eval.c:102
const char * key
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:177
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition intfloat.h:40
#define AV_RL32(p)
#define AV_RL16(p)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
version
Definition libkvazaar.c:313
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_BAYER_GBRG8
bayer, GBGB..(odd line), RGRG..(even line), 8-bit samples
Definition pixfmt.h:287
@ AV_PIX_FMT_BAYER_GRBG16LE
bayer, GRGR..(odd line), BGBG..(even line), 16-bit samples, little-endian
Definition pixfmt.h:295
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_BAYER_RGGB16LE
bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, little-endian
Definition pixfmt.h:291
@ AV_PIX_FMT_BAYER_GRBG8
bayer, GRGR..(odd line), BGBG..(even line), 8-bit samples
Definition pixfmt.h:288
@ AV_PIX_FMT_BGR48LE
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as lit...
Definition pixfmt.h:146
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition pixfmt.h:105
@ AV_PIX_FMT_BAYER_GBRG16LE
bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, little-endian
Definition pixfmt.h:293
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_BAYER_BGGR16LE
bayer, BGBG..(odd line), GRGR..(even line), 16-bit samples, little-endian
Definition pixfmt.h:289
@ AV_PIX_FMT_BAYER_RGGB8
bayer, RGRG..(odd line), GBGB..(even line), 8-bit samples
Definition pixfmt.h:286
@ AV_PIX_FMT_BAYER_BGGR8
bayer, BGBG..(odd line), GRGR..(even line), 8-bit samples
Definition pixfmt.h:285
#define snprintf
Definition snprintf.h:34
unsigned int pos
Definition spdifenc.c:431
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
AVIOContext * pb
I/O context.
Definition avformat.h:1375
void * priv_data
Format private data.
Definition avformat.h:1361
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
int64_t pos
Definition avformat.h:621
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
AVDictionary * metadata
Definition avformat.h:846
uint64_t pts
Definition cinedec.c:37
uint64_t maxsize
Definition cinedec.c:38
int nb_index_entries
Definition internal.h:193
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
#define av_free(p)
#define avpriv_request_sample(...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int size