FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
28 #include "libavutil/intreadwrite.h"
29 #include "libavcodec/bmp.h"
30 #include "libavutil/intfloat.h"
31 #include "avformat.h"
32 #include "internal.h"
33 
34 typedef struct {
35  uint64_t pts;
37 
38 /** Compression */
39 enum {
40  CC_RGB = 0, /**< Gray */
41  CC_LEAD = 1, /**< LEAD (M)JPEG */
42  CC_UNINT = 2 /**< Uninterpolated color image (CFA field indicates color ordering) */
43 };
44 
45 /** Color Filter Array */
46 enum {
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 */
52 
53  CFA_TLGRAY = 0x80000000,
54  CFA_TRGRAY = 0x40000000,
55  CFA_BLGRAY = 0x20000000,
56  CFA_BRGRAY = 0x10000000
57 };
58 
60 {
61  int HeaderSize;
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;
71  return 0;
72 }
73 
74 static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
75 {
76  if (value || allow_zero) {
77  return av_dict_set_int(dict, key, value, 0);
78  }
79  return 0;
80 }
81 
82 static int set_metadata_float(AVDictionary **dict, const char *key, float value, int allow_zero)
83 {
84  if (value != 0 || allow_zero) {
85  char tmp[64];
86  snprintf(tmp, sizeof(tmp), "%f", value);
87  return av_dict_set(dict, key, tmp, 0);
88  }
89  return 0;
90 }
91 
93 {
94  AVIOContext *pb = avctx->pb;
95  AVStream *st;
96  unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
97  int vflip;
98  char *description;
99  uint64_t i;
100 
101  st = avformat_new_stream(avctx, NULL);
102  if (!st)
103  return AVERROR(ENOMEM);
106  st->codec->codec_tag = 0;
107 
108  /* CINEFILEHEADER structure */
109  avio_skip(pb, 4); // Type, Headersize
110 
111  compression = avio_rl16(pb);
112  version = avio_rl16(pb);
113  if (version != 1) {
114  avpriv_request_sample(avctx, "uknown version %i", version);
115  return AVERROR_INVALIDDATA;
116  }
117 
118  avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
119 
120  st->duration = avio_rl32(pb);
121  offImageHeader = avio_rl32(pb);
122  offSetup = avio_rl32(pb);
123  offImageOffsets = avio_rl32(pb);
124 
125  avio_skip(pb, 8); // TriggerTime
126 
127  /* BITMAPINFOHEADER structure */
128  avio_seek(pb, offImageHeader, SEEK_SET);
129  avio_skip(pb, 4); //biSize
130  st->codec->width = avio_rl32(pb);
131  st->codec->height = avio_rl32(pb);
132 
133  if (avio_rl16(pb) != 1) // biPlanes
134  return AVERROR_INVALIDDATA;
135 
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;
140  }
141 
142  switch (avio_rl32(pb)) {
143  case BMP_RGB:
144  vflip = 0;
145  break;
146  case 0x100: /* BI_PACKED */
147  st->codec->codec_tag = MKTAG('B', 'I', 'T', 0);
148  vflip = 1;
149  break;
150  default:
151  avpriv_request_sample(avctx, "unknown bitmap compression");
152  return AVERROR_INVALIDDATA;
153  }
154 
155  avio_skip(pb, 4); // biSizeImage
156 
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;
166  }
167 
168  avio_skip(pb, 616); // Binning .. bFlipH
169  if (!avio_rl32(pb) ^ vflip) {
170  st->codec->extradata = av_strdup("BottomUp");
171  st->codec->extradata_size = 9;
172  }
173 
174  avio_skip(pb, 4); // Grid
175 
176  avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
177 
178  avio_skip(pb, 20); // Shutter .. bEnableColor
179 
180  set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb), 0);
181  set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb), 0);
182  set_metadata_int(&st->metadata, "software_version", avio_rl32(pb), 0);
183  set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb), 0);
184 
185  CFA = avio_rl32(pb);
186 
187  set_metadata_int(&st->metadata, "brightness", avio_rl32(pb), 1);
188  set_metadata_int(&st->metadata, "contrast", avio_rl32(pb), 1);
189  set_metadata_int(&st->metadata, "gamma", avio_rl32(pb), 1);
190 
191  avio_skip(pb, 12 + 16); // Reserved1 .. AutoExpRect
192  set_metadata_float(&st->metadata, "wbgain[0].r", av_int2float(avio_rl32(pb)), 1);
193  set_metadata_float(&st->metadata, "wbgain[0].b", av_int2float(avio_rl32(pb)), 1);
194  avio_skip(pb, 36); // WBGain[1].. WBView
195 
197 
198  if (compression == CC_RGB) {
199  if (biBitCount == 8) {
201  } else if (biBitCount == 16) {
203  } else if (biBitCount == 24) {
205  } else if (biBitCount == 48) {
207  } else {
208  avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
209  return AVERROR_INVALIDDATA;
210  }
211  } else if (compression == CC_UNINT) {
212  switch (CFA & 0xFFFFFF) {
213  case CFA_BAYER:
214  if (biBitCount == 8) {
216  } else if (biBitCount == 16) {
218  } else {
219  avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
220  return AVERROR_INVALIDDATA;
221  }
222  break;
223  case CFA_BAYERFLIP:
224  if (biBitCount == 8) {
226  } else if (biBitCount == 16) {
228  } else {
229  avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
230  return AVERROR_INVALIDDATA;
231  }
232  break;
233  default:
234  avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
235  return AVERROR_INVALIDDATA;
236  }
237  } else { //CC_LEAD
238  avpriv_request_sample(avctx, "unsupported compression %i", compression);
239  return AVERROR_INVALIDDATA;
240  }
241 
242  avio_skip(pb, 668); // Conv8Min ... Sensor
243 
244  set_metadata_int(&st->metadata, "shutter_ns", avio_rl32(pb), 0);
245 
246  avio_skip(pb, 24); // EDRShutterNs ... ImHeightAcq
247 
248 #define DESCRIPTION_SIZE 4096
249  description = av_malloc(DESCRIPTION_SIZE + 1);
250  if (!description)
251  return AVERROR(ENOMEM);
252  i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
253  if (i < DESCRIPTION_SIZE)
254  avio_skip(pb, DESCRIPTION_SIZE - i);
255  if (description[0])
256  av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
257  else
258  av_free(description);
259 
260  avio_skip(pb, 1176); // RisingEdge ... cmUser
261 
262  set_metadata_int(&st->metadata, "enable_crop", avio_rl32(pb), 1);
263  set_metadata_int(&st->metadata, "crop_left", avio_rl32(pb), 1);
264  set_metadata_int(&st->metadata, "crop_top", avio_rl32(pb), 1);
265  set_metadata_int(&st->metadata, "crop_right", avio_rl32(pb), 1);
266  set_metadata_int(&st->metadata, "crop_bottom", avio_rl32(pb), 1);
267 
268  /* parse image offsets */
269  avio_seek(pb, offImageOffsets, SEEK_SET);
270  for (i = 0; i < st->duration; i++)
271  av_add_index_entry(st, avio_rl64(pb), i, 0, 0, AVINDEX_KEYFRAME);
272 
273  return 0;
274 }
275 
277 {
278  CineDemuxContext *cine = avctx->priv_data;
279  AVStream *st = avctx->streams[0];
280  AVIOContext *pb = avctx->pb;
281  int n, size, ret;
282 
283  if (cine->pts >= st->duration)
284  return AVERROR_EOF;
285 
286  avio_seek(pb, st->index_entries[cine->pts].pos, SEEK_SET);
287  n = avio_rl32(pb);
288  if (n < 8)
289  return AVERROR_INVALIDDATA;
290  avio_skip(pb, n - 8);
291  size = avio_rl32(pb);
292 
293  ret = av_get_packet(pb, pkt, size);
294  if (ret < 0)
295  return ret;
296 
297  pkt->pts = cine->pts++;
298  pkt->stream_index = 0;
299  pkt->flags |= AV_PKT_FLAG_KEY;
300  return 0;
301 }
302 
303 static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
304 {
305  CineDemuxContext *cine = avctx->priv_data;
306 
307  if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
308  return AVERROR(ENOSYS);
309 
310  if (!avctx->pb->seekable)
311  return AVERROR(EIO);
312 
313  cine->pts = timestamp;
314  return 0;
315 }
316 
318  .name = "cine",
319  .long_name = NULL_IF_CONFIG_SMALL("Phantom Cine"),
320  .priv_data_size = sizeof(CineDemuxContext),
325 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
Definition: cinedec.c:74
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: utils.c:1745
#define DESCRIPTION_SIZE
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4083
LEAD (M)JPEG.
Definition: cinedec.c:41
int64_t pos
Definition: avformat.h:784
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
bayer, GBGB..(odd line), RGRG..(even line), 8-bit samples */
Definition: pixfmt.h:306
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1045
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:277
int version
Definition: avisynth_c.h:629
static AVPacket pkt
Gray.
Definition: cinedec.c:40
GBRG/RGGB.
Definition: cinedec.c:48
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
Format I/O context.
Definition: avformat.h:1273
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define av_malloc(s)
bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, little-endian */
Definition: pixfmt.h:312
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1617
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:244
ptrdiff_t size
Definition: opengl_enc.c:101
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2996
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
static int cine_read_probe(AVProbeData *p)
Definition: cinedec.c:59
static int cine_read_header(AVFormatContext *avctx)
Definition: cinedec.c:92
#define AVINDEX_KEYFRAME
Definition: avformat.h:791
AVInputFormat ff_cine_demuxer
Definition: cinedec.c:317
GRAY.
Definition: cinedec.c:47
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:659
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
GLsizei GLsizei * length
Definition: opengl_enc.c:115
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:161
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
static int set_metadata_float(AVDictionary **dict, const char *key, float value, int allow_zero)
Definition: cinedec.c:82
#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:78
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1681
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition: cinedec.c:276
int n
Definition: avisynth_c.h:547
AVDictionary * metadata
Definition: avformat.h:916
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
GB/RG.
Definition: cinedec.c:50
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:623
Stream structure.
Definition: avformat.h:842
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
BGGR/GRBG.
Definition: cinedec.c:49
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1534
int extradata_size
Definition: avcodec.h:1618
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:69
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2278
#define snprintf
Definition: snprintf.h:34
uint64_t pts
Definition: cinedec.c:35
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
RG/GB.
Definition: cinedec.c:51
Definition: bmp.h:28
static int flags
Definition: cpu.c:47
bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, little-endian */
Definition: pixfmt.h:310
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:901
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:643
Main libavformat public API header.
Y , 8bpp.
Definition: pixfmt.h:71
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:143
bayer, RGRG..(odd line), GBGB..(even line), 8-bit samples */
Definition: pixfmt.h:305
#define av_free(p)
Uninterpolated color image (CFA field indicates color ordering)
Definition: cinedec.c:42
void * priv_data
Format private data.
Definition: avformat.h:1301
Y , 16bpp, little-endian.
Definition: pixfmt.h:100
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:2280
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
Definition: cinedec.c:303
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:715
int stream_index
Definition: avcodec.h:1425
#define MKTAG(a, b, c, d)
Definition: common.h:330
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1400
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:667
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416