FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/cinedec.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 125 188 66.5%
Functions: 5 6 83.3%
Branches: 45 110 40.9%

Line Branch Exec Source
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;
36 uint64_t maxsize;
37 } CineDemuxContext;
38
39 /** Compression */
40 enum {
41 CC_RGB = 0, /**< Gray */
42 CC_LEAD = 1, /**< LEAD (M)JPEG */
43 CC_UNINT = 2 /**< Uninterpolated color image (CFA field indicates color ordering) */
44 };
45
46 /** Color Filter Array */
47 enum {
48 CFA_NONE = 0, /**< GRAY */
49 CFA_VRI = 1, /**< GBRG/RGGB */
50 CFA_VRIV6 = 2, /**< BGGR/GRBG */
51 CFA_BAYER = 3, /**< GB/RG */
52 CFA_BAYERFLIP = 4, /**< RG/GB */
53 };
54
55 #define CFA_TLGRAY 0x80000000U
56 #define CFA_TRGRAY 0x40000000U
57 #define CFA_BLGRAY 0x20000000U
58 #define CFA_BRGRAY 0x10000000U
59
60 6782 static int cine_read_probe(const AVProbeData *p)
61 {
62 int HeaderSize;
63
4/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6773 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8 times.
6782 if (p->buf[0] == 'C' && p->buf[1] == 'I' && // Type
64
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (HeaderSize = AV_RL16(p->buf + 2)) >= 0x2C && // HeaderSize
65
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL16(p->buf + 4) <= CC_UNINT && // Compression
66
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL16(p->buf + 6) <= 1 && // Version
67
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(p->buf + 20) && // ImageCount
68
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(p->buf + 24) >= HeaderSize && // OffImageHeader
69
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(p->buf + 28) >= HeaderSize && // OffSetup
70
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(p->buf + 32) >= HeaderSize) // OffImageOffsets
71 1 return AVPROBE_SCORE_MAX;
72 6781 return 0;
73 }
74
75 13 static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
76 {
77
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
13 if (value || allow_zero) {
78 13 return av_dict_set_int(dict, key, value, 0);
79 }
80 return 0;
81 }
82
83 2 static int set_metadata_float(AVDictionary **dict, const char *key, float value, int allow_zero)
84 {
85
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (value != 0 || allow_zero) {
86 char tmp[64];
87 2 snprintf(tmp, sizeof(tmp), "%f", value);
88 2 return av_dict_set(dict, key, tmp, 0);
89 }
90 return 0;
91 }
92
93 1 static int cine_read_header(AVFormatContext *avctx)
94 {
95 1 AVIOContext *pb = avctx->pb;
96 AVStream *st;
97 unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
98 int vflip;
99 char *description;
100 uint64_t i;
101
102 1 st = avformat_new_stream(avctx, NULL);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
104 return AVERROR(ENOMEM);
105 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
106 1 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
107 1 st->codecpar->codec_tag = 0;
108
109 /* CINEFILEHEADER structure */
110 1 avio_skip(pb, 4); // Type, Headersize
111
112 1 compression = avio_rl16(pb);
113 1 version = avio_rl16(pb);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (version != 1) {
115 avpriv_request_sample(avctx, "unknown version %i", version);
116 return AVERROR_INVALIDDATA;
117 }
118
119 1 avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
120
121 1 st->duration = avio_rl32(pb);
122 1 offImageHeader = avio_rl32(pb);
123 1 offSetup = avio_rl32(pb);
124 1 offImageOffsets = avio_rl32(pb);
125
126 1 avio_skip(pb, 8); // TriggerTime
127
128 /* BITMAPINFOHEADER structure */
129 1 avio_seek(pb, offImageHeader, SEEK_SET);
130 1 avio_skip(pb, 4); //biSize
131 1 st->codecpar->width = avio_rl32(pb);
132 1 st->codecpar->height = avio_rl32(pb);
133
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_rl16(pb) != 1) // biPlanes
135 return AVERROR_INVALIDDATA;
136
137 1 biBitCount = avio_rl16(pb);
138
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 if (biBitCount != 8 && biBitCount != 16 && biBitCount != 24 && biBitCount != 48) {
139 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
140 return AVERROR_INVALIDDATA;
141 }
142
143
1/3
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 switch (avio_rl32(pb)) {
144 1 case BMP_RGB:
145 1 vflip = 0;
146 1 break;
147 case 0x100: /* BI_PACKED */
148 st->codecpar->codec_tag = MKTAG('B', 'I', 'T', 0);
149 vflip = 1;
150 break;
151 default:
152 avpriv_request_sample(avctx, "unknown bitmap compression");
153 return AVERROR_INVALIDDATA;
154 }
155
156 1 avio_skip(pb, 4); // biSizeImage
157
158 /* parse SETUP structure */
159 1 avio_seek(pb, offSetup, SEEK_SET);
160 1 avio_skip(pb, 140); // FrameRatae16 .. descriptionOld
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_rl16(pb) != 0x5453)
162 return AVERROR_INVALIDDATA;
163 1 length = avio_rl16(pb);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (length < 0x163C) {
165 avpriv_request_sample(avctx, "short SETUP header");
166 return AVERROR_INVALIDDATA;
167 }
168
169 1 avio_skip(pb, 616); // Binning .. bFlipH
170
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!avio_rl32(pb) ^ vflip) {
171 1 st->codecpar->extradata = av_strdup("BottomUp");
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st->codecpar->extradata) {
173 st->codecpar->extradata_size = 0;
174 return AVERROR(ENOMEM);
175 }
176 1 st->codecpar->extradata_size = 9;
177 }
178
179 1 avio_skip(pb, 4); // Grid
180
181 1 avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
182
183 1 avio_skip(pb, 20); // Shutter .. bEnableColor
184
185 1 set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb), 0);
186 1 set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb), 0);
187 1 set_metadata_int(&st->metadata, "software_version", avio_rl32(pb), 0);
188 1 set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb), 0);
189
190 1 CFA = avio_rl32(pb);
191
192 1 set_metadata_int(&st->metadata, "brightness", avio_rl32(pb), 1);
193 1 set_metadata_int(&st->metadata, "contrast", avio_rl32(pb), 1);
194 1 set_metadata_int(&st->metadata, "gamma", avio_rl32(pb), 1);
195
196 1 avio_skip(pb, 12 + 16); // Reserved1 .. AutoExpRect
197 1 set_metadata_float(&st->metadata, "wbgain[0].r", av_int2float(avio_rl32(pb)), 1);
198 1 set_metadata_float(&st->metadata, "wbgain[0].b", av_int2float(avio_rl32(pb)), 1);
199 1 avio_skip(pb, 36); // WBGain[1].. WBView
200
201 1 st->codecpar->bits_per_coded_sample = avio_rl32(pb);
202
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (compression == CC_RGB) {
204 if (biBitCount == 8) {
205 st->codecpar->format = AV_PIX_FMT_GRAY8;
206 } else if (biBitCount == 16) {
207 st->codecpar->format = AV_PIX_FMT_GRAY16LE;
208 } else if (biBitCount == 24) {
209 st->codecpar->format = AV_PIX_FMT_BGR24;
210 } else if (biBitCount == 48) {
211 st->codecpar->format = AV_PIX_FMT_BGR48LE;
212 } else {
213 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
214 return AVERROR_INVALIDDATA;
215 }
216
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (compression == CC_UNINT) {
217
1/3
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
1 switch (CFA & 0xFFFFFF) {
218 1 case CFA_BAYER:
219
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (biBitCount == 8) {
220 1 st->codecpar->format = AV_PIX_FMT_BAYER_GBRG8;
221 } else if (biBitCount == 16) {
222 st->codecpar->format = AV_PIX_FMT_BAYER_GBRG16LE;
223 } else {
224 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
225 return AVERROR_INVALIDDATA;
226 }
227 1 break;
228 case CFA_BAYERFLIP:
229 if (biBitCount == 8) {
230 st->codecpar->format = AV_PIX_FMT_BAYER_RGGB8;
231 } else if (biBitCount == 16) {
232 st->codecpar->format = AV_PIX_FMT_BAYER_RGGB16LE;
233 } else {
234 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
235 return AVERROR_INVALIDDATA;
236 }
237 break;
238 default:
239 avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
240 return AVERROR_INVALIDDATA;
241 }
242 } else { //CC_LEAD
243 avpriv_request_sample(avctx, "unsupported compression %i", compression);
244 return AVERROR_INVALIDDATA;
245 }
246
247 1 avio_skip(pb, 668); // Conv8Min ... Sensor
248
249 1 set_metadata_int(&st->metadata, "shutter_ns", avio_rl32(pb), 0);
250
251 1 avio_skip(pb, 24); // EDRShutterNs ... ImHeightAcq
252
253 #define DESCRIPTION_SIZE 4096
254 1 description = av_malloc(DESCRIPTION_SIZE + 1);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!description)
256 return AVERROR(ENOMEM);
257 1 i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
258
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (i < DESCRIPTION_SIZE)
259 1 avio_skip(pb, DESCRIPTION_SIZE - i);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (description[0])
261 av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
262 else
263 1 av_free(description);
264
265 1 avio_skip(pb, 1176); // RisingEdge ... cmUser
266
267 1 set_metadata_int(&st->metadata, "enable_crop", avio_rl32(pb), 1);
268 1 set_metadata_int(&st->metadata, "crop_left", avio_rl32(pb), 1);
269 1 set_metadata_int(&st->metadata, "crop_top", avio_rl32(pb), 1);
270 1 set_metadata_int(&st->metadata, "crop_right", avio_rl32(pb), 1);
271 1 set_metadata_int(&st->metadata, "crop_bottom", avio_rl32(pb), 1);
272
273 /* parse image offsets */
274 1 avio_seek(pb, offImageOffsets, SEEK_SET);
275
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1 times.
101 for (i = 0; i < st->duration; i++) {
276 100 int64_t pos = avio_rl64(pb);
277
2/4
✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 100 times.
100 if (avio_feof(pb) || pos < 0)
278 return AVERROR_INVALIDDATA;
279
280 100 av_add_index_entry(st, pos, i, 0, 0, AVINDEX_KEYFRAME);
281 }
282
283 1 return 0;
284 }
285
286 4 static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
287 {
288 4 CineDemuxContext *cine = avctx->priv_data;
289 4 AVStream *st = avctx->streams[0];
290 4 FFStream *const sti = ffstream(st);
291 4 AVIOContext *pb = avctx->pb;
292 int n, size, ret;
293 int64_t ret64;
294
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (cine->pts >= sti->nb_index_entries)
296 return AVERROR_EOF;
297
298 4 ret64 = avio_seek(pb, sti->index_entries[cine->pts].pos, SEEK_SET);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret64 < 0)
300 return ret64;
301 4 n = avio_rl32(pb);
302
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (n < 8)
303 2 return AVERROR_INVALIDDATA;
304 2 avio_skip(pb, n - 8);
305 2 size = avio_rl32(pb);
306
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (avio_feof(pb) || size < 0)
307 return AVERROR_INVALIDDATA;
308
309
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (cine->maxsize && (uint64_t)sti->index_entries[cine->pts].pos + size + n > cine->maxsize)
310 size = cine->maxsize - sti->index_entries[cine->pts].pos - n;
311
312 2 ret = av_get_packet(pb, pkt, size);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
314 return ret;
315
316
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ret != size)
317 1 cine->maxsize = (uint64_t)sti->index_entries[cine->pts].pos + n + ret;
318
319 2 pkt->pts = cine->pts++;
320 2 pkt->stream_index = 0;
321 2 pkt->flags |= AV_PKT_FLAG_KEY;
322 2 return 0;
323 }
324
325 static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
326 {
327 CineDemuxContext *cine = avctx->priv_data;
328
329 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
330 return AVERROR(ENOSYS);
331
332 if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
333 return AVERROR(EIO);
334
335 cine->pts = timestamp;
336 return 0;
337 }
338
339 const AVInputFormat ff_cine_demuxer = {
340 .name = "cine",
341 .long_name = NULL_IF_CONFIG_SMALL("Phantom Cine"),
342 .priv_data_size = sizeof(CineDemuxContext),
343 .read_probe = cine_read_probe,
344 .read_header = cine_read_header,
345 .read_packet = cine_read_packet,
346 .read_seek = cine_read_seek,
347 };
348