3 * Copyright (c) 2018 Paul B Mahol
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
22 #include "libavutil/imgutils.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
29 #define SER_MAGIC "LUCAM-RECORDER"
31 typedef struct SERDemuxerContext {
38 static int ser_probe(const AVProbeData *pd)
40 if (memcmp(pd->buf, SER_MAGIC, 14) == 0)
41 return AVPROBE_SCORE_MAX;
46 static int ser_read_header(AVFormatContext *s)
48 SERDemuxerContext *ser = s->priv_data;
49 enum AVPixelFormat pix_fmt;
50 int depth, color_id, endian;
54 st = avformat_new_stream(s, NULL);
56 return AVERROR(ENOMEM);
60 color_id = avio_rl32(s->pb);
61 endian = avio_rl32(s->pb);
62 ser->width = avio_rl32(s->pb);
63 ser->height = avio_rl32(s->pb);
64 depth = avio_rl32(s->pb);
65 st->nb_frames = st->duration = avio_rl32(s->pb);
66 avio_skip(s->pb, 120);
71 case 0: pix_fmt = depth <= 8 ? AV_PIX_FMT_GRAY8 : endian ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE; break;
72 case 8: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_RGGB8 : endian ? AV_PIX_FMT_BAYER_RGGB16LE : AV_PIX_FMT_BAYER_RGGB16BE; break;
73 case 9: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_GRBG8 : endian ? AV_PIX_FMT_BAYER_GRBG16LE : AV_PIX_FMT_BAYER_GRBG16BE; break;
74 case 10: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_GBRG8 : endian ? AV_PIX_FMT_BAYER_GBRG16LE : AV_PIX_FMT_BAYER_GBRG16BE; break;
75 case 11: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_BGGR8 : endian ? AV_PIX_FMT_BAYER_BGGR16LE : AV_PIX_FMT_BAYER_BGGR16BE; break;
76 case 100: pix_fmt = depth <= 8 ? AV_PIX_FMT_RGB24 : endian ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE; break;
77 case 101: pix_fmt = depth <= 8 ? AV_PIX_FMT_BGR24 : endian ? AV_PIX_FMT_BGR48LE : AV_PIX_FMT_BGR48BE; break;
79 return AVERROR_PATCHWELCOME;
82 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
83 st->codecpar->codec_id = s->iformat->raw_codec_id;
85 avpriv_set_pts_info(st, 64, ser->framerate.den, ser->framerate.num);
87 st->codecpar->width = ser->width;
88 st->codecpar->height = ser->height;
89 st->codecpar->format = pix_fmt;
90 packet_size = av_image_get_buffer_size(st->codecpar->format, ser->width, ser->height, 1);
93 ser->end = 178 + st->nb_frames * packet_size;
94 s->packet_size = packet_size;
95 st->codecpar->bit_rate = av_rescale_q(s->packet_size,
96 (AVRational){8,1}, st->time_base);
102 static int ser_read_packet(AVFormatContext *s, AVPacket *pkt)
104 SERDemuxerContext *ser = s->priv_data;
108 pos = avio_tell(s->pb);
112 ret = av_get_packet(s->pb, pkt, s->packet_size);
113 pkt->pts = pkt->dts = (pkt->pos - s->internal->data_offset) / s->packet_size;
115 pkt->stream_index = 0;
121 #define OFFSET(x) offsetof(SERDemuxerContext, x)
122 #define DEC AV_OPT_FLAG_DECODING_PARAM
123 static const AVOption ser_options[] = {
124 { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
128 static const AVClass ser_demuxer_class = {
129 .class_name = "ser demuxer",
130 .item_name = av_default_item_name,
131 .option = ser_options,
132 .version = LIBAVUTIL_VERSION_INT,
135 AVInputFormat ff_ser_demuxer = {
137 .long_name = NULL_IF_CONFIG_SMALL("SER (Simple uncompressed video format for astronomical capturing)"),
138 .priv_data_size = sizeof(SERDemuxerContext),
139 .read_probe = ser_probe,
140 .read_header = ser_read_header,
141 .read_packet = ser_read_packet,
142 .flags = AVFMT_GENERIC_INDEX,
144 .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
145 .priv_class = &ser_demuxer_class,