Go to the documentation of this file.
1 /*
2  * SER demuxer
3  * Copyright (c) 2018 Paul B Mahol
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
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  */
22 #include "libavutil/imgutils.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
26 #include "internal.h"
27 #include "avformat.h"
31 typedef struct SERDemuxerContext {
32  const AVClass *class;
33  int width, height;
35  int64_t end;
38 static int ser_probe(const AVProbeData *pd)
39 {
40  if (memcmp(pd->buf, SER_MAGIC, 14) == 0)
42  else
43  return 0;
44 }
47 {
48  SERDemuxerContext *ser = s->priv_data;
50  int depth, color_id, endian;
51  int packet_size;
52  AVStream *st;
54  st = avformat_new_stream(s, NULL);
55  if (!st)
56  return AVERROR(ENOMEM);
58  avio_skip(s->pb, 14);
59  avio_skip(s->pb, 4);
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);
67  avio_skip(s->pb, 8);
68  avio_skip(s->pb, 8);
70  switch (color_id) {
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;
78  default:
80  }
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);
91  if (packet_size < 0)
92  return packet_size;
93  ser->end = 178 + st->nb_frames * packet_size;
94  s->packet_size = packet_size;
96  (AVRational){8,1}, st->time_base);
98  return 0;
99 }
103 {
104  SERDemuxerContext *ser = s->priv_data;
105  int64_t pos;
106  int ret;
108  pos = avio_tell(s->pb);
109  if (pos >= ser->end)
110  return AVERROR_EOF;
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;
116  if (ret < 0)
117  return ret;
118  return 0;
119 }
121 #define OFFSET(x) offsetof(SERDemuxerContext, x)
123 static const AVOption ser_options[] = {
124  { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
125  { NULL },
126 };
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,
133 };
136  .name = "ser",
137  .long_name = NULL_IF_CONFIG_SMALL("SER (Simple uncompressed video format for astronomical capturing)"),
138  .priv_data_size = sizeof(SERDemuxerContext),
143  .extensions = "ser",
144  .raw_codec_id = AV_CODEC_ID_RAWVIDEO,
145  .priv_class = &ser_demuxer_class,
146 };
unsigned int packet_size
Definition: avformat.h:1367
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
Definition: opt.h:248
misc image utilities
Definition: version.h:85
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:383
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:4905
int64_t data_offset
offset of the first packet
Definition: internal.h:80
bayer, GBGB..(odd line), RGRG..(even line), 8-bit samples
Definition: pixfmt.h:262
bayer, GRGR..(odd line), BGBG..(even line), 8-bit samples
Definition: pixfmt.h:263
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int num
Definition: rational.h:59
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
bayer, GRGR..(odd line), BGBG..(even line), 16-bit samples, little-endian
Definition: pixfmt.h:270
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1708
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:341
bayer, BGBG..(odd line), GRGR..(even line), 8-bit samples
Definition: pixfmt.h:260
static AVPacket pkt
static int ser_read_header(AVFormatContext *s)
Definition: serdec.c:46
Format I/O context.
Definition: avformat.h:1243
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int width
Video only.
Definition: codec_par.h:126
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:103
bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, little-endian
Definition: pixfmt.h:268
bayer, GBGB..(odd line), RGRG..(even line), 16-bit samples, big-endian
Definition: pixfmt.h:269
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4481
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:310
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
bayer, BGBG..(odd line), GRGR..(even line), 16-bit samples, little-endian
Definition: pixfmt.h:264
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:466
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:754
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
ff_const59 struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1255
unsigned int pos
Definition: spdifenc.c:412
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
static const AVClass ser_demuxer_class
Definition: serdec.c:128
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:149
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
static int ser_probe(const AVProbeData *pd)
Definition: serdec.c:38
#define s(width, name)
Definition: cbs_vp9.c:257
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:148
#define SER_MAGIC
Definition: serdec.c:29
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
AVRational framerate
Definition: serdec.c:34
Stream structure.
Definition: avformat.h:884
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static const AVOption ser_options[]
Definition: serdec.c:123
int64_t end
Definition: serdec.c:35
#define DEC
Definition: serdec.c:122
AVIOContext * pb
I/O context.
Definition: avformat.h:1285
bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, big-endian
Definition: pixfmt.h:267
bayer, GRGR..(odd line), BGBG..(even line), 16-bit samples, big-endian
Definition: pixfmt.h:271
AVInputFormat ff_ser_demuxer
Definition: serdec.c:135
Describe the class of an AVClass context structure.
Definition: log.h:67
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
Use generic index building code.
Definition: avformat.h:463
Rational number (pair of numerator and denominator).
Definition: rational.h:58
offset must point to AVRational
Definition: opt.h:238
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
bayer, BGBG..(odd line), GRGR..(even line), 16-bit samples, big-endian
Definition: pixfmt.h:265
misc parsing utilities
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:702
#define flags(name, subs,...)
Definition: cbs_av1.c:561
bayer, RGRG..(odd line), GBGB..(even line), 16-bit samples, little-endian
Definition: pixfmt.h:266
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:933
maximum score
Definition: avformat.h:453
Main libavformat public API header.
Y , 8bpp.
Definition: pixfmt.h:74
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:102
bayer, RGRG..(odd line), GBGB..(even line), 8-bit samples
Definition: pixfmt.h:261
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:935
int den
Definition: rational.h:60
void * priv_data
Format private data.
Definition: avformat.h:1271
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
static int ser_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: serdec.c:102
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:362
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1049
int stream_index
Definition: packet.h:365
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:913
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: packet.h:340
#define OFFSET(x)
Definition: serdec.c:121
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356