Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / libavformat / cinedec.c
1 /*
2  * Phanton 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 "avformat.h"
31 #include "internal.h"
32
33 typedef struct {
34     uint64_t pts;
35 } CineDemuxContext;
36
37 /** Compression */
38 enum {
39     CC_RGB   = 0,  /**< Gray */
40     CC_LEAD  = 1,  /**< LEAD (M)JPEG */
41     CC_UNINT = 2   /**< Uninterpolated color image (CFA field indicates color ordering)  */
42 };
43
44 /** Color Filter Array */
45 enum {
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 */
51
52     CFA_TLGRAY    = 0x80000000,
53     CFA_TRGRAY    = 0x40000000,
54     CFA_BLGRAY    = 0x20000000,
55     CFA_BRGRAY    = 0x10000000
56 };
57
58 static int cine_read_probe(AVProbeData *p)
59 {
60     int HeaderSize;
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;
70     return 0;
71 }
72
73 static int set_metadata_int(AVDictionary **dict, const char *key, int value, int allow_zero)
74 {
75     if (value || allow_zero) {
76         return av_dict_set_int(dict, key, value, 0);
77     }
78     return 0;
79 }
80
81 static int cine_read_header(AVFormatContext *avctx)
82 {
83     AVIOContext *pb = avctx->pb;
84     AVStream *st;
85     unsigned int version, compression, offImageHeader, offSetup, offImageOffsets, biBitCount, length, CFA;
86     int vflip;
87     char *description;
88     uint64_t i;
89
90     st = avformat_new_stream(avctx, NULL);
91     if (!st)
92         return AVERROR(ENOMEM);
93     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
94     st->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
95     st->codec->codec_tag  = 0;
96
97     /* CINEFILEHEADER structure */
98     avio_skip(pb, 4); // Type, Headersize
99
100     compression = avio_rl16(pb);
101     version     = avio_rl16(pb);
102     if (version != 1) {
103         avpriv_request_sample(avctx, "uknown version %i", version);
104         return AVERROR_INVALIDDATA;
105     }
106
107     avio_skip(pb, 12); // FirstMovieImage, TotalImageCount, FirstImageNumber
108
109     st->duration    = avio_rl32(pb);
110     offImageHeader  = avio_rl32(pb);
111     offSetup        = avio_rl32(pb);
112     offImageOffsets = avio_rl32(pb);
113
114     avio_skip(pb, 8); // TriggerTime
115
116     /* BITMAPINFOHEADER structure */
117     avio_seek(pb, offImageHeader, SEEK_SET);
118     avio_skip(pb, 4); //biSize
119     st->codec->width      = avio_rl32(pb);
120     st->codec->height     = avio_rl32(pb);
121
122     if (avio_rl16(pb) != 1) // biPlanes
123         return AVERROR_INVALIDDATA;
124
125     biBitCount = avio_rl16(pb);
126     if (biBitCount != 8 && biBitCount != 16 && biBitCount != 24 && biBitCount != 48) {
127         avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
128         return AVERROR_INVALIDDATA;
129     }
130
131     switch (avio_rl32(pb)) {
132     case BMP_RGB:
133         vflip = 0;
134         break;
135     case 0x100: /* BI_PACKED */
136         st->codec->codec_tag = MKTAG('B', 'I', 'T', 0);
137         vflip = 1;
138         break;
139     default:
140         avpriv_request_sample(avctx, "unknown bitmap compression");
141         return AVERROR_INVALIDDATA;
142     }
143
144     avio_skip(pb, 4); // biSizeImage
145
146     /* parse SETUP structure */
147     avio_seek(pb, offSetup, SEEK_SET);
148     avio_skip(pb, 140); // FrameRatae16 .. descriptionOld
149     if (avio_rl16(pb) != 0x5453)
150         return AVERROR_INVALIDDATA;
151     length = avio_rl16(pb);
152     if (length < 0x163C) {
153         avpriv_request_sample(avctx, "short SETUP header");
154         return AVERROR_INVALIDDATA;
155     }
156
157     avio_skip(pb, 616); // Binning .. bFlipH
158     if (!avio_rl32(pb) ^ vflip) {
159         st->codec->extradata  = av_strdup("BottomUp");
160         st->codec->extradata_size  = 9;
161     }
162
163     avio_skip(pb, 4); // Grid
164
165     avpriv_set_pts_info(st, 64, 1, avio_rl32(pb));
166
167     avio_skip(pb, 20); // Shutter .. bEnableColor
168
169     set_metadata_int(&st->metadata, "camera_version", avio_rl32(pb), 0);
170     set_metadata_int(&st->metadata, "firmware_version", avio_rl32(pb), 0);
171     set_metadata_int(&st->metadata, "software_version", avio_rl32(pb), 0);
172     set_metadata_int(&st->metadata, "recording_timezone", avio_rl32(pb), 0);
173
174     CFA = avio_rl32(pb);
175
176     set_metadata_int(&st->metadata, "brightness", avio_rl32(pb), 1);
177     set_metadata_int(&st->metadata, "contrast", avio_rl32(pb), 1);
178     set_metadata_int(&st->metadata, "gamma", avio_rl32(pb), 1);
179
180     avio_skip(pb, 72); // Reserved1 .. WBView
181
182     st->codec->bits_per_coded_sample = avio_rl32(pb);
183
184     if (compression == CC_RGB) {
185         if (biBitCount == 8) {
186             st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
187         } else if (biBitCount == 16) {
188             st->codec->pix_fmt = AV_PIX_FMT_GRAY16LE;
189         } else if (biBitCount == 24) {
190             st->codec->pix_fmt = AV_PIX_FMT_BGR24;
191         } else if (biBitCount == 48) {
192             st->codec->pix_fmt = AV_PIX_FMT_BGR48LE;
193         } else {
194             avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
195             return AVERROR_INVALIDDATA;
196         }
197     } else if (compression == CC_UNINT) {
198         switch (CFA & 0xFFFFFF) {
199         case CFA_BAYER:
200             if (biBitCount == 8) {
201                 st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
202             } else if (biBitCount == 16) {
203                 st->codec->pix_fmt = AV_PIX_FMT_BAYER_GBRG16LE;
204             } else {
205                 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
206                 return AVERROR_INVALIDDATA;
207             }
208             break;
209         case CFA_BAYERFLIP:
210             if (biBitCount == 8) {
211                 st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
212             } else if (biBitCount == 16) {
213                 st->codec->pix_fmt = AV_PIX_FMT_BAYER_RGGB16LE;
214             } else {
215                 avpriv_request_sample(avctx, "unsupported biBitCount %i", biBitCount);
216                 return AVERROR_INVALIDDATA;
217             }
218             break;
219         default:
220            avpriv_request_sample(avctx, "unsupported Color Field Array (CFA) %i", CFA & 0xFFFFFF);
221             return AVERROR_INVALIDDATA;
222         }
223     } else { //CC_LEAD
224         avpriv_request_sample(avctx, "unsupported compression %i", compression);
225         return AVERROR_INVALIDDATA;
226     }
227
228     avio_skip(pb, 668); // Conv8Min ... Sensor
229
230     set_metadata_int(&st->metadata, "shutter_ns", avio_rl32(pb), 0);
231
232     avio_skip(pb, 24); // EDRShutterNs ... ImHeightAcq
233
234 #define DESCRIPTION_SIZE 4096
235     description = av_malloc(DESCRIPTION_SIZE + 1);
236     if (!description)
237         return AVERROR(ENOMEM);
238     i = avio_get_str(pb, DESCRIPTION_SIZE, description, DESCRIPTION_SIZE + 1);
239     if (i < DESCRIPTION_SIZE)
240         avio_skip(pb, DESCRIPTION_SIZE - i);
241     if (description[0])
242         av_dict_set(&st->metadata, "description", description, AV_DICT_DONT_STRDUP_VAL);
243     else
244         av_free(description);
245
246     avio_skip(pb, 1176); // RisingEdge ... cmUser
247
248     set_metadata_int(&st->metadata, "enable_crop", avio_rl32(pb), 1);
249     set_metadata_int(&st->metadata, "crop_left", avio_rl32(pb), 1);
250     set_metadata_int(&st->metadata, "crop_top", avio_rl32(pb), 1);
251     set_metadata_int(&st->metadata, "crop_right", avio_rl32(pb), 1);
252     set_metadata_int(&st->metadata, "crop_bottom", avio_rl32(pb), 1);
253
254     /* parse image offsets */
255     avio_seek(pb, offImageOffsets, SEEK_SET);
256     for (i = 0; i < st->duration; i++)
257         av_add_index_entry(st, avio_rl64(pb), i, 0, 0, AVINDEX_KEYFRAME);
258
259     return 0;
260 }
261
262 static int cine_read_packet(AVFormatContext *avctx, AVPacket *pkt)
263 {
264     CineDemuxContext *cine = avctx->priv_data;
265     AVStream *st = avctx->streams[0];
266     AVIOContext *pb = avctx->pb;
267     int n, size, ret;
268
269     if (cine->pts >= st->duration)
270         return AVERROR_EOF;
271
272     avio_seek(pb, st->index_entries[cine->pts].pos, SEEK_SET);
273     n = avio_rl32(pb);
274     if (n < 8)
275         return AVERROR_INVALIDDATA;
276     avio_skip(pb, n - 8);
277     size = avio_rl32(pb);
278
279     ret = av_get_packet(pb, pkt, size);
280     if (ret < 0)
281         return ret;
282
283     pkt->pts = cine->pts++;
284     pkt->stream_index = 0;
285     pkt->flags |= AV_PKT_FLAG_KEY;
286     return 0;
287 }
288
289 static int cine_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
290 {
291     CineDemuxContext *cine = avctx->priv_data;
292
293     if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
294         return AVERROR(ENOSYS);
295
296     if (!avctx->pb->seekable)
297         return AVERROR(EIO);
298
299     cine->pts = timestamp;
300     return 0;
301 }
302
303 AVInputFormat ff_cine_demuxer = {
304     .name           = "cine",
305     .long_name      = NULL_IF_CONFIG_SMALL("Phantom Cine"),
306     .priv_data_size = sizeof(CineDemuxContext),
307     .read_probe     = cine_read_probe,
308     .read_header    = cine_read_header,
309     .read_packet    = cine_read_packet,
310     .read_seek      = cine_read_seek,
311 };