2 * Gremlin Digital Video demuxer
3 * Copyright (c) 2017 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/intreadwrite.h"
28 typedef struct GDVContext {
32 int audio_stream_index;
33 int video_stream_index;
37 static int gdv_read_probe(AVProbeData *p)
39 if (AV_RL32(p->buf) == 0x29111994)
40 return AVPROBE_SCORE_MAX;
70 static int gdv_read_header(AVFormatContext *ctx)
72 GDVContext *gdv = ctx->priv_data;
73 AVIOContext *pb = ctx->pb;
75 unsigned fps, snd_flags, vid_depth, size_id;
78 size_id = avio_rl16(pb);
80 vst = avformat_new_stream(ctx, 0);
82 return AVERROR(ENOMEM);
86 vst->nb_frames = avio_rl16(pb);
89 snd_flags = avio_rl16(pb);
91 ast = avformat_new_stream(ctx, 0);
93 return AVERROR(ENOMEM);
96 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
97 ast->codecpar->codec_tag = 0;
98 ast->codecpar->sample_rate = avio_rl16(pb);
99 ast->codecpar->channels = 1 + !!(snd_flags & 2);
101 ast->codecpar->codec_id = AV_CODEC_ID_GREMLIN_DPCM;
103 ast->codecpar->codec_id = (snd_flags & 4) ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_U8;
106 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
107 gdv->audio_size = (ast->codecpar->sample_rate / fps) *
108 ast->codecpar->channels * (1 + !!(snd_flags & 4)) / (1 + !!(snd_flags & 8));
113 vid_depth = avio_rl16(pb);
116 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
117 vst->codecpar->codec_id = AV_CODEC_ID_GDV;
118 vst->codecpar->codec_tag = 0;
119 vst->codecpar->width = avio_rl16(pb);
120 vst->codecpar->height = avio_rl16(pb);
122 if (vst->codecpar->width == 0 || vst->codecpar->height == 0) {
125 for (i = 0; i < FF_ARRAY_ELEMS(FixedSize) - 1; i++) {
126 if (FixedSize[i].id == size_id)
130 vst->codecpar->width = FixedSize[i].width;
131 vst->codecpar->height = FixedSize[i].height;
134 avpriv_set_pts_info(vst, 64, 1, fps);
139 for (i = 0; i < 256; i++) {
140 unsigned r = avio_r8(pb);
141 unsigned g = avio_r8(pb);
142 unsigned b = avio_r8(pb);
143 gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
147 gdv->is_first_video = 1;
152 static int gdv_read_packet(AVFormatContext *ctx, AVPacket *pkt)
154 GDVContext *gdv = ctx->priv_data;
155 AVIOContext *pb = ctx->pb;
159 return pb->error ? pb->error : AVERROR_EOF;
161 if (gdv->audio_size && gdv->is_audio) {
162 ret = av_get_packet(pb, pkt, gdv->audio_size);
165 pkt->stream_index = 1;
170 if (avio_rl16(pb) != 0x1305)
171 return AVERROR_INVALIDDATA;
172 ret = av_get_packet(pb, pkt, 4 + avio_rl16(pb));
175 pkt->stream_index = 0;
178 if (gdv->is_first_video) {
179 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
182 av_packet_unref(pkt);
183 return AVERROR(ENOMEM);
185 memcpy(pal, gdv->pal, AVPALETTE_SIZE);
186 pkt->flags |= AV_PKT_FLAG_KEY;
187 gdv->is_first_video = 0;
194 AVInputFormat ff_gdv_demuxer = {
196 .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
197 .priv_data_size = sizeof(GDVContext),
198 .read_probe = gdv_read_probe,
199 .read_header = gdv_read_header,
200 .read_packet = gdv_read_packet,