]> git.sesse.net Git - ffmpeg/blob - libavformat/gdv.c
87bba2fdbd62e30a1e899ccf79d548a651c4ccb8
[ffmpeg] / libavformat / gdv.c
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
22 #include "libavutil/intreadwrite.h"
23
24 #include "avformat.h"
25 #include "avio.h"
26 #include "internal.h"
27
28 typedef struct GDVContext {
29     int is_first_video;
30     int is_audio;
31     int audio_size;
32     int audio_stream_index;
33     int video_stream_index;
34     unsigned pal[256];
35 } GDVContext;
36
37 static int gdv_read_probe(AVProbeData *p)
38 {
39     if (AV_RL32(p->buf) == 0x29111994)
40         return AVPROBE_SCORE_MAX;
41
42     return 0;
43 }
44
45 static int gdv_read_header(AVFormatContext *ctx)
46 {
47     GDVContext *gdv = ctx->priv_data;
48     AVIOContext *pb = ctx->pb;
49     AVStream *vst, *ast;
50     unsigned fps, snd_flags, vid_depth;
51
52     avio_skip(pb, 6);
53
54     vst = avformat_new_stream(ctx, 0);
55     if (!vst)
56         return AVERROR(ENOMEM);
57
58     vst->start_time        = 0;
59     vst->duration          =
60     vst->nb_frames         = avio_rl16(pb);
61
62     fps = avio_rl16(pb);
63     snd_flags = avio_rl16(pb);
64     if (snd_flags & 1) {
65         ast = avformat_new_stream(ctx, 0);
66         if (!ast)
67             return AVERROR(ENOMEM);
68
69         ast->start_time = 0;
70         ast->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
71         ast->codecpar->codec_tag   = 0;
72         ast->codecpar->sample_rate = avio_rl16(pb);
73         ast->codecpar->channels    = 1 + !!(snd_flags & 2);
74         if (snd_flags & 8) {
75             ast->codecpar->codec_id = AV_CODEC_ID_GREMLIN_DPCM;
76         } else {
77             ast->codecpar->codec_id = (snd_flags & 4) ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_U8;
78         }
79
80         avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
81         gdv->audio_size = (ast->codecpar->sample_rate / fps) *
82                            ast->codecpar->channels * (1 + !!(snd_flags & 4)) / (1 + !!(snd_flags & 8));
83         gdv->is_audio = 1;
84     }
85     vid_depth = avio_rl16(pb);
86     avio_skip(pb, 4);
87
88     vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
89     vst->codecpar->codec_id   = AV_CODEC_ID_GDV;
90     vst->codecpar->codec_tag  = 0;
91     vst->codecpar->width      = avio_rl16(pb);
92     vst->codecpar->height     = avio_rl16(pb);
93
94     avpriv_set_pts_info(vst, 64, 1, fps);
95
96     if (vid_depth & 1) {
97         int i;
98
99         for (i = 0; i < 256; i++) {
100             unsigned r = avio_r8(pb);
101             unsigned g = avio_r8(pb);
102             unsigned b = avio_r8(pb);
103             gdv->pal[i] = 0xFF << 24 | r << 18 | g << 10 | b << 2;
104         }
105     }
106
107     gdv->is_first_video = 1;
108
109     return 0;
110 }
111
112 static int gdv_read_packet(AVFormatContext *ctx, AVPacket *pkt)
113 {
114     GDVContext *gdv = ctx->priv_data;
115     AVIOContext *pb = ctx->pb;
116     int ret;
117
118     if (avio_feof(pb))
119         return pb->error ? pb->error : AVERROR_EOF;
120
121     if (gdv->audio_size && gdv->is_audio) {
122         ret = av_get_packet(pb, pkt, gdv->audio_size);
123         if (ret < 0)
124             return ret;
125         pkt->stream_index = 1;
126         gdv->is_audio = 0;
127     } else {
128         uint8_t *pal;
129
130         if (avio_rl16(pb) != 0x1305)
131             return AVERROR_INVALIDDATA;
132         ret = av_get_packet(pb, pkt, 4 + avio_rl16(pb));
133         if (ret < 0)
134             return ret;
135         pkt->stream_index = 0;
136         gdv->is_audio = 1;
137
138         if (gdv->is_first_video) {
139             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
140                                           AVPALETTE_SIZE);
141             if (!pal) {
142                 av_packet_unref(pkt);
143                 return AVERROR(ENOMEM);
144             }
145             memcpy(pal, gdv->pal, AVPALETTE_SIZE);
146             pkt->flags |= AV_PKT_FLAG_KEY;
147             gdv->is_first_video = 0;
148         }
149     }
150
151     return 0;
152 }
153
154 AVInputFormat ff_gdv_demuxer = {
155     .name           = "gdv",
156     .long_name      = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
157     .priv_data_size = sizeof(GDVContext),
158     .read_probe     = gdv_read_probe,
159     .read_header    = gdv_read_header,
160     .read_packet    = gdv_read_packet,
161 };