/src/ffmpeg/libavformat/gdv.c
Line | Count | Source |
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 "demux.h" |
27 | | #include "internal.h" |
28 | | |
29 | | typedef struct GDVContext { |
30 | | int is_first_video; |
31 | | int is_audio; |
32 | | int audio_size; |
33 | | int audio_stream_index; |
34 | | int video_stream_index; |
35 | | unsigned pal[256]; |
36 | | } GDVContext; |
37 | | |
38 | | static int gdv_read_probe(const AVProbeData *p) |
39 | 936k | { |
40 | 936k | if (AV_RL32(p->buf) == 0x29111994) |
41 | 146 | return AVPROBE_SCORE_MAX; |
42 | | |
43 | 936k | return 0; |
44 | 936k | } |
45 | | |
46 | | static 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 | | |
71 | | static int gdv_read_header(AVFormatContext *ctx) |
72 | 1.69k | { |
73 | 1.69k | GDVContext *gdv = ctx->priv_data; |
74 | 1.69k | AVIOContext *pb = ctx->pb; |
75 | 1.69k | AVStream *vst, *ast; |
76 | 1.69k | unsigned fps, snd_flags, vid_depth, size_id; |
77 | | |
78 | 1.69k | avio_skip(pb, 4); |
79 | 1.69k | size_id = avio_rl16(pb); |
80 | | |
81 | 1.69k | vst = avformat_new_stream(ctx, 0); |
82 | 1.69k | if (!vst) |
83 | 0 | return AVERROR(ENOMEM); |
84 | | |
85 | 1.69k | vst->start_time = 0; |
86 | 1.69k | vst->duration = |
87 | 1.69k | vst->nb_frames = avio_rl16(pb); |
88 | | |
89 | 1.69k | fps = avio_rl16(pb); |
90 | 1.69k | if (!fps) |
91 | 128 | return AVERROR_INVALIDDATA; |
92 | | |
93 | 1.56k | snd_flags = avio_rl16(pb); |
94 | 1.56k | if (snd_flags & 1) { |
95 | 1.21k | ast = avformat_new_stream(ctx, 0); |
96 | 1.21k | if (!ast) |
97 | 0 | return AVERROR(ENOMEM); |
98 | | |
99 | 1.21k | ast->start_time = 0; |
100 | 1.21k | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
101 | 1.21k | ast->codecpar->codec_tag = 0; |
102 | 1.21k | ast->codecpar->sample_rate = avio_rl16(pb); |
103 | 1.21k | ast->codecpar->ch_layout.nb_channels = 1 + !!(snd_flags & 2); |
104 | 1.21k | if (snd_flags & 8) { |
105 | 431 | ast->codecpar->codec_id = AV_CODEC_ID_GREMLIN_DPCM; |
106 | 784 | } else { |
107 | 784 | ast->codecpar->codec_id = (snd_flags & 4) ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_U8; |
108 | 784 | } |
109 | | |
110 | 1.21k | avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate); |
111 | 1.21k | gdv->audio_size = (ast->codecpar->sample_rate / fps) * |
112 | 1.21k | ast->codecpar->ch_layout.nb_channels * |
113 | 1.21k | (1 + !!(snd_flags & 4)) / (1 + !!(snd_flags & 8)); |
114 | 1.21k | gdv->is_audio = 1; |
115 | 1.21k | } else { |
116 | 350 | avio_skip(pb, 2); |
117 | 350 | } |
118 | 1.56k | vid_depth = avio_rl16(pb); |
119 | 1.56k | avio_skip(pb, 4); |
120 | | |
121 | 1.56k | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
122 | 1.56k | vst->codecpar->codec_id = AV_CODEC_ID_GDV; |
123 | 1.56k | vst->codecpar->codec_tag = 0; |
124 | 1.56k | vst->codecpar->width = avio_rl16(pb); |
125 | 1.56k | vst->codecpar->height = avio_rl16(pb); |
126 | | |
127 | 1.56k | if (vst->codecpar->width == 0 || vst->codecpar->height == 0) { |
128 | 337 | int i; |
129 | | |
130 | 4.72k | for (i = 0; i < FF_ARRAY_ELEMS(FixedSize) - 1; i++) { |
131 | 4.47k | if (FixedSize[i].id == size_id) |
132 | 90 | break; |
133 | 4.47k | } |
134 | | |
135 | 337 | vst->codecpar->width = FixedSize[i].width; |
136 | 337 | vst->codecpar->height = FixedSize[i].height; |
137 | 337 | } |
138 | | |
139 | 1.56k | avpriv_set_pts_info(vst, 64, 1, fps); |
140 | | |
141 | 1.56k | if (vid_depth & 1) { |
142 | 135 | int i; |
143 | | |
144 | 34.6k | for (i = 0; i < 256; i++) { |
145 | 34.5k | unsigned r = avio_r8(pb); |
146 | 34.5k | unsigned g = avio_r8(pb); |
147 | 34.5k | unsigned b = avio_r8(pb); |
148 | 34.5k | gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2; |
149 | 34.5k | } |
150 | 135 | } |
151 | | |
152 | 1.56k | gdv->is_first_video = 1; |
153 | | |
154 | 1.56k | return 0; |
155 | 1.56k | } |
156 | | |
157 | | static int gdv_read_packet(AVFormatContext *ctx, AVPacket *pkt) |
158 | 774k | { |
159 | 774k | GDVContext *gdv = ctx->priv_data; |
160 | 774k | AVIOContext *pb = ctx->pb; |
161 | 774k | int ret; |
162 | | |
163 | 774k | if (avio_feof(pb)) |
164 | 2.13k | return pb->error ? pb->error : AVERROR_EOF; |
165 | | |
166 | 772k | if (gdv->audio_size && gdv->is_audio) { |
167 | 311k | ret = av_get_packet(pb, pkt, gdv->audio_size); |
168 | 311k | if (ret < 0) |
169 | 59 | return ret; |
170 | 311k | pkt->stream_index = 1; |
171 | 311k | gdv->is_audio = 0; |
172 | 460k | } else { |
173 | 460k | uint8_t *pal; |
174 | | |
175 | 460k | if (avio_rl16(pb) != 0x1305) |
176 | 761 | return AVERROR_INVALIDDATA; |
177 | 459k | ret = av_get_packet(pb, pkt, 4 + avio_rl16(pb)); |
178 | 459k | if (ret < 0) |
179 | 57 | return ret; |
180 | 459k | pkt->stream_index = 0; |
181 | 459k | gdv->is_audio = 1; |
182 | | |
183 | 459k | if (gdv->is_first_video) { |
184 | 931 | pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, |
185 | 931 | AVPALETTE_SIZE); |
186 | 931 | if (!pal) { |
187 | 0 | return AVERROR(ENOMEM); |
188 | 0 | } |
189 | 931 | memcpy(pal, gdv->pal, AVPALETTE_SIZE); |
190 | 931 | pkt->flags |= AV_PKT_FLAG_KEY; |
191 | 931 | gdv->is_first_video = 0; |
192 | 931 | } |
193 | 459k | } |
194 | | |
195 | 771k | return 0; |
196 | 772k | } |
197 | | |
198 | | const FFInputFormat ff_gdv_demuxer = { |
199 | | .p.name = "gdv", |
200 | | .p.long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"), |
201 | | .priv_data_size = sizeof(GDVContext), |
202 | | .read_probe = gdv_read_probe, |
203 | | .read_header = gdv_read_header, |
204 | | .read_packet = gdv_read_packet, |
205 | | }; |