FFmpeg
Loading...
Searching...
No Matches
gdv.c
Go to the documentation of this file.
1/*
2 * Gremlin Digital Video demuxer
3 * Copyright (c) 2017 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
23
24#include "avformat.h"
25#include "avio.h"
26#include "demux.h"
27#include "internal.h"
28
29typedef struct GDVContext {
35 unsigned pal[256];
37
38static int gdv_read_probe(const AVProbeData *p)
39{
40 if (AV_RL32(p->buf) == 0x29111994)
41 return AVPROBE_SCORE_MAX;
42
43 return 0;
44}
45
46static struct {
47 uint16_t id;
48 uint16_t width;
49 uint16_t height;
50} FixedSize[] = {
51 { 0, 320, 200},
52 { 1, 640, 200},
53 { 2, 320, 167},
54 { 3, 320, 180},
55 { 4, 320, 400},
56 { 5, 320, 170},
57 { 6, 160, 85},
58 { 7, 160, 83},
59 { 8, 160, 90},
60 { 9, 280, 128},
61 {10, 320, 240},
62 {11, 320, 201},
63 {16, 640, 400},
64 {17, 640, 200},
65 {18, 640, 180},
66 {19, 640, 167},
67 {20, 640, 170},
68 {21, 320, 240}
69};
70
72{
73 GDVContext *gdv = ctx->priv_data;
74 AVIOContext *pb = ctx->pb;
75 AVStream *vst, *ast;
76 unsigned fps, snd_flags, vid_depth, size_id;
77
78 avio_skip(pb, 4);
79 size_id = avio_rl16(pb);
80
81 vst = avformat_new_stream(ctx, 0);
82 if (!vst)
83 return AVERROR(ENOMEM);
84
85 vst->start_time = 0;
86 vst->duration =
87 vst->nb_frames = avio_rl16(pb);
88
89 fps = avio_rl16(pb);
90 if (!fps)
92
93 snd_flags = avio_rl16(pb);
94 if (snd_flags & 1) {
95 ast = avformat_new_stream(ctx, 0);
96 if (!ast)
97 return AVERROR(ENOMEM);
98
99 ast->start_time = 0;
101 ast->codecpar->codec_tag = 0;
102 ast->codecpar->sample_rate = avio_rl16(pb);
103 ast->codecpar->ch_layout.nb_channels = 1 + !!(snd_flags & 2);
104 if (snd_flags & 8) {
106 } else {
107 ast->codecpar->codec_id = (snd_flags & 4) ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_U8;
108 }
109
110 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
111 gdv->audio_size = (ast->codecpar->sample_rate / fps) *
113 (1 + !!(snd_flags & 4)) / (1 + !!(snd_flags & 8));
114 gdv->is_audio = 1;
115 } else {
116 avio_skip(pb, 2);
117 }
118 vid_depth = avio_rl16(pb);
119 avio_skip(pb, 4);
120
123 vst->codecpar->codec_tag = 0;
124 vst->codecpar->width = avio_rl16(pb);
125 vst->codecpar->height = avio_rl16(pb);
126
127 if (vst->codecpar->width == 0 || vst->codecpar->height == 0) {
128 int i;
129
130 for (i = 0; i < FF_ARRAY_ELEMS(FixedSize) - 1; i++) {
131 if (FixedSize[i].id == size_id)
132 break;
133 }
134
135 vst->codecpar->width = FixedSize[i].width;
136 vst->codecpar->height = FixedSize[i].height;
137 }
138
139 avpriv_set_pts_info(vst, 64, 1, fps);
140
141 if (vid_depth & 1) {
142 int i;
143
144 for (i = 0; i < 256; i++) {
145 unsigned r = avio_r8(pb);
146 unsigned g = avio_r8(pb);
147 unsigned b = avio_r8(pb);
148 gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
149 }
150 }
151
152 gdv->is_first_video = 1;
153
154 return 0;
155}
156
158{
159 GDVContext *gdv = ctx->priv_data;
160 AVIOContext *pb = ctx->pb;
161 int ret;
162
163 if (avio_feof(pb))
164 return pb->error ? pb->error : AVERROR_EOF;
165
166 if (gdv->audio_size && gdv->is_audio) {
167 ret = av_get_packet(pb, pkt, gdv->audio_size);
168 if (ret < 0)
169 return ret;
170 pkt->stream_index = 1;
171 gdv->is_audio = 0;
172 } else {
173 uint8_t *pal;
174
175 if (avio_rl16(pb) != 0x1305)
176 return AVERROR_INVALIDDATA;
177 ret = av_get_packet(pb, pkt, 4 + avio_rl16(pb));
178 if (ret < 0)
179 return ret;
180 pkt->stream_index = 0;
181 gdv->is_audio = 1;
182
183 if (gdv->is_first_video) {
186 if (!pal) {
187 return AVERROR(ENOMEM);
188 }
189 memcpy(pal, gdv->pal, AVPALETTE_SIZE);
190 pkt->flags |= AV_PKT_FLAG_KEY;
191 gdv->is_first_video = 0;
192 }
193 }
194
195 return 0;
196}
197
199 .p.name = "gdv",
200 .p.long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
201 .priv_data_size = sizeof(GDVContext),
205};
const FFInputFormat ff_gdv_demuxer
Definition gdv.c:198
static AVFormatContext * ctx
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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:98
Buffered I/O operations.
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
static AVPacket * pkt
enum AVCodecID id
Definition dts2pts.c:607
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_GDV
Definition codec_id.h:280
@ AV_CODEC_ID_GREMLIN_DPCM
Definition codec_id.h:447
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RL32(p)
static int gdv_read_probe(const AVProbeData *p)
Definition gdv.c:38
static int gdv_read_header(AVFormatContext *ctx)
Definition gdv.c:71
static struct @114121243206025044303163346146170263153276173313 FixedSize[]
static int gdv_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition gdv.c:157
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define AVPALETTE_SIZE
Definition pixfmt.h:32
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int error
contains the error code or 0 if no error happened
Definition avio.h:239
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int is_first_video
Definition gdv.c:30
int audio_stream_index
Definition gdv.c:33
int video_stream_index
Definition gdv.c:34
int audio_size
Definition gdv.c:32
int is_audio
Definition gdv.c:31
uint32_t pal[256]
Definition gdv.c:38
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
const char * g
Definition vf_curves.c:128