/src/ffmpeg/libavformat/serdec.c
Line | Count | Source (jump to first uncovered line) |
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 |
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 | | #include "libavutil/imgutils.h" |
23 | | #include "libavutil/parseutils.h" |
24 | | #include "libavutil/pixdesc.h" |
25 | | #include "libavutil/opt.h" |
26 | | #include "demux.h" |
27 | | #include "internal.h" |
28 | | #include "avformat.h" |
29 | | |
30 | 924k | #define SER_MAGIC "LUCAM-RECORDER" |
31 | | |
32 | | typedef struct SERDemuxerContext { |
33 | | const AVClass *class; |
34 | | int width, height; |
35 | | AVRational framerate; |
36 | | int64_t end; |
37 | | } SERDemuxerContext; |
38 | | |
39 | | static int ser_probe(const AVProbeData *pd) |
40 | 924k | { |
41 | 924k | if (memcmp(pd->buf, SER_MAGIC, 14) == 0) |
42 | 174 | return AVPROBE_SCORE_MAX; |
43 | 924k | else |
44 | 924k | return 0; |
45 | 924k | } |
46 | | |
47 | | static int ser_read_header(AVFormatContext *s) |
48 | 6.71k | { |
49 | 6.71k | SERDemuxerContext *ser = s->priv_data; |
50 | 6.71k | enum AVPixelFormat pix_fmt; |
51 | 6.71k | int depth, color_id, endian; |
52 | 6.71k | int packet_size; |
53 | 6.71k | AVStream *st; |
54 | | |
55 | 6.71k | st = avformat_new_stream(s, NULL); |
56 | 6.71k | if (!st) |
57 | 0 | return AVERROR(ENOMEM); |
58 | | |
59 | 6.71k | avio_skip(s->pb, 14); |
60 | 6.71k | avio_skip(s->pb, 4); |
61 | 6.71k | color_id = avio_rl32(s->pb); |
62 | 6.71k | endian = avio_rl32(s->pb); |
63 | 6.71k | ser->width = avio_rl32(s->pb); |
64 | 6.71k | ser->height = avio_rl32(s->pb); |
65 | 6.71k | depth = avio_rl32(s->pb); |
66 | 6.71k | st->nb_frames = st->duration = avio_rl32(s->pb); |
67 | 6.71k | avio_skip(s->pb, 120); |
68 | 6.71k | avio_skip(s->pb, 8); |
69 | 6.71k | avio_skip(s->pb, 8); |
70 | | |
71 | 6.71k | switch (color_id) { |
72 | 5.60k | case 0: pix_fmt = depth <= 8 ? AV_PIX_FMT_GRAY8 : endian ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY16LE; break; |
73 | 125 | case 8: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_RGGB8 : endian ? AV_PIX_FMT_BAYER_RGGB16BE : AV_PIX_FMT_BAYER_RGGB16LE; break; |
74 | 127 | case 9: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_GRBG8 : endian ? AV_PIX_FMT_BAYER_GRBG16BE : AV_PIX_FMT_BAYER_GRBG16LE; break; |
75 | 194 | case 10: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_GBRG8 : endian ? AV_PIX_FMT_BAYER_GBRG16BE : AV_PIX_FMT_BAYER_GBRG16LE; break; |
76 | 88 | case 11: pix_fmt = depth <= 8 ? AV_PIX_FMT_BAYER_BGGR8 : endian ? AV_PIX_FMT_BAYER_BGGR16BE : AV_PIX_FMT_BAYER_BGGR16LE; break; |
77 | 119 | case 100: pix_fmt = depth <= 8 ? AV_PIX_FMT_RGB24 : endian ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB48LE; break; |
78 | 172 | case 101: pix_fmt = depth <= 8 ? AV_PIX_FMT_BGR24 : endian ? AV_PIX_FMT_BGR48BE : AV_PIX_FMT_BGR48LE; break; |
79 | 287 | default: |
80 | 287 | return AVERROR_PATCHWELCOME; |
81 | 6.71k | } |
82 | | |
83 | 6.42k | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
84 | 6.42k | st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; |
85 | | |
86 | 6.42k | avpriv_set_pts_info(st, 64, ser->framerate.den, ser->framerate.num); |
87 | | |
88 | 6.42k | st->codecpar->width = ser->width; |
89 | 6.42k | st->codecpar->height = ser->height; |
90 | 6.42k | st->codecpar->format = pix_fmt; |
91 | 6.42k | packet_size = av_image_get_buffer_size(st->codecpar->format, ser->width, ser->height, 1); |
92 | 6.42k | if (packet_size < 0) |
93 | 5.63k | return packet_size; |
94 | 797 | ser->end = 178 + st->nb_frames * packet_size; |
95 | 797 | s->packet_size = packet_size; |
96 | 797 | st->codecpar->bit_rate = av_rescale_q(s->packet_size, |
97 | 797 | (AVRational){8,1}, st->time_base); |
98 | | |
99 | 797 | return 0; |
100 | 6.42k | } |
101 | | |
102 | | |
103 | | static int ser_read_packet(AVFormatContext *s, AVPacket *pkt) |
104 | 95.9k | { |
105 | 95.9k | SERDemuxerContext *ser = s->priv_data; |
106 | 95.9k | int64_t pos; |
107 | 95.9k | int ret; |
108 | | |
109 | 95.9k | pos = avio_tell(s->pb); |
110 | 95.9k | if (pos >= ser->end) |
111 | 93 | return AVERROR_EOF; |
112 | | |
113 | 95.8k | ret = av_get_packet(s->pb, pkt, s->packet_size); |
114 | 95.8k | pkt->pts = pkt->dts = (pkt->pos - ffformatcontext(s)->data_offset) / s->packet_size; |
115 | | |
116 | 95.8k | pkt->stream_index = 0; |
117 | 95.8k | if (ret < 0) |
118 | 1.03k | return ret; |
119 | 94.7k | return 0; |
120 | 95.8k | } |
121 | | |
122 | | #define OFFSET(x) offsetof(SERDemuxerContext, x) |
123 | | #define DEC AV_OPT_FLAG_DECODING_PARAM |
124 | | static const AVOption ser_options[] = { |
125 | | { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC }, |
126 | | { NULL }, |
127 | | }; |
128 | | |
129 | | static const AVClass ser_demuxer_class = { |
130 | | .class_name = "ser demuxer", |
131 | | .item_name = av_default_item_name, |
132 | | .option = ser_options, |
133 | | .version = LIBAVUTIL_VERSION_INT, |
134 | | }; |
135 | | |
136 | | const FFInputFormat ff_ser_demuxer = { |
137 | | .p.name = "ser", |
138 | | .p.long_name = NULL_IF_CONFIG_SMALL("SER (Simple uncompressed video format for astronomical capturing)"), |
139 | | .p.flags = AVFMT_GENERIC_INDEX, |
140 | | .p.extensions = "ser", |
141 | | .p.priv_class = &ser_demuxer_class, |
142 | | .priv_data_size = sizeof(SERDemuxerContext), |
143 | | .read_probe = ser_probe, |
144 | | .read_header = ser_read_header, |
145 | | .read_packet = ser_read_packet, |
146 | | .raw_codec_id = AV_CODEC_ID_RAWVIDEO, |
147 | | }; |