1 /*
2 * This file is part of mpv.
3 *
4 * mpv is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * mpv is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 #include <libavcodec/avcodec.h>
26 #include <libavutil/common.h>
27
28 #include "common/av_common.h"
29
30 #include "options/m_config.h"
31 #include "options/m_option.h"
32
33 #include "stream/stream.h"
34 #include "demux.h"
35 #include "stheader.h"
36 #include "codec_tags.h"
37
38 #include "video/fmt-conversion.h"
39 #include "video/img_format.h"
40
41 #include "osdep/endian.h"
42
43 struct demux_rawaudio_opts {
44 struct m_channels channels;
45 int samplerate;
46 int aformat;
47 };
48
49 // Ad-hoc schema to systematically encode the format as int
50 #define PCM(sign, is_float, bits, is_be) \
51 ((sign) | ((is_float) << 1) | ((is_be) << 2) | ((bits) << 3))
52 #define NE (BYTE_ORDER == BIG_ENDIAN)
53
54 #define OPT_BASE_STRUCT struct demux_rawaudio_opts
55 const struct m_sub_options demux_rawaudio_conf = {
56 .opts = (const m_option_t[]) {
57 {"channels", OPT_CHANNELS(channels), .flags = M_OPT_CHANNELS_LIMITED},
58 {"rate", OPT_INT(samplerate), M_RANGE(1000, 8 * 48000)},
59 {"format", OPT_CHOICE(aformat,
60 {"u8", PCM(0, 0, 8, 0)},
61 {"s8", PCM(1, 0, 8, 0)},
62 {"u16le", PCM(0, 0, 16, 0)}, {"u16be", PCM(0, 0, 16, 1)},
63 {"s16le", PCM(1, 0, 16, 0)}, {"s16be", PCM(1, 0, 16, 1)},
64 {"u24le", PCM(0, 0, 24, 0)}, {"u24be", PCM(0, 0, 24, 1)},
65 {"s24le", PCM(1, 0, 24, 0)}, {"s24be", PCM(1, 0, 24, 1)},
66 {"u32le", PCM(0, 0, 32, 0)}, {"u32be", PCM(0, 0, 32, 1)},
67 {"s32le", PCM(1, 0, 32, 0)}, {"s32be", PCM(1, 0, 32, 1)},
68 {"floatle", PCM(0, 1, 32, 0)}, {"floatbe", PCM(0, 1, 32, 1)},
69 {"doublele",PCM(0, 1, 64, 0)}, {"doublebe", PCM(0, 1, 64, 1)},
70 {"u16", PCM(0, 0, 16, NE)},
71 {"s16", PCM(1, 0, 16, NE)},
72 {"u24", PCM(0, 0, 24, NE)},
73 {"s24", PCM(1, 0, 24, NE)},
74 {"u32", PCM(0, 0, 32, NE)},
75 {"s32", PCM(1, 0, 32, NE)},
76 {"float", PCM(0, 1, 32, NE)},
77 {"double", PCM(0, 1, 64, NE)})},
78 {0}
79 },
80 .size = sizeof(struct demux_rawaudio_opts),
81 .defaults = &(const struct demux_rawaudio_opts){
82 // Note that currently, stream_cdda expects exactly these parameters!
83 .channels = {
84 .set = 1,
85 .chmaps = (struct mp_chmap[]){ MP_CHMAP_INIT_STEREO, },
86 .num_chmaps = 1,
87 },
88 .samplerate = 44100,
89 .aformat = PCM(1, 0, 16, 0), // s16le
90 },
91 };
92
93 #undef PCM
94 #undef NE
95
96 struct demux_rawvideo_opts {
97 int vformat;
98 int mp_format;
99 char *codec;
100 int width;
101 int height;
102 float fps;
103 int imgsize;
104 };
105
106 #undef OPT_BASE_STRUCT
107 #define OPT_BASE_STRUCT struct demux_rawvideo_opts
108 const struct m_sub_options demux_rawvideo_conf = {
109 .opts = (const m_option_t[]) {
110 {"w", OPT_INT(width), M_RANGE(1, 8192)},
111 {"h", OPT_INT(height), M_RANGE(1, 8192)},
112 {"format", OPT_FOURCC(vformat)},
113 {"mp-format", OPT_IMAGEFORMAT(mp_format)},
114 {"codec", OPT_STRING(codec)},
115 {"fps", OPT_FLOAT(fps), M_RANGE(0.001, 1000)},
116 {"size", OPT_INT(imgsize), M_RANGE(1, 8192 * 8192 * 4)},
117 {0}
118 },
119 .size = sizeof(struct demux_rawvideo_opts),
120 .defaults = &(const struct demux_rawvideo_opts){
121 .vformat = MKTAG('I', '4', '2', '0'),
122 .width = 1280,
123 .height = 720,
124 .fps = 25,
125 },
126 };
127
128 struct priv {
129 struct sh_stream *sh;
130 int frame_size;
131 int read_frames;
132 double frame_rate;
133 };
134
generic_open(struct demuxer * demuxer)135 static int generic_open(struct demuxer *demuxer)
136 {
137 struct stream *s = demuxer->stream;
138 struct priv *p = demuxer->priv;
139
140 int64_t end = stream_get_size(s);
141 if (end >= 0)
142 demuxer->duration = (end / p->frame_size) / p->frame_rate;
143
144 return 0;
145 }
146
demux_rawaudio_open(demuxer_t * demuxer,enum demux_check check)147 static int demux_rawaudio_open(demuxer_t *demuxer, enum demux_check check)
148 {
149 struct demux_rawaudio_opts *opts =
150 mp_get_config_group(demuxer, demuxer->global, &demux_rawaudio_conf);
151
152 if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE)
153 return -1;
154
155 if (opts->channels.num_chmaps != 1) {
156 MP_ERR(demuxer, "Invalid channels option given.\n");
157 return -1;
158 }
159
160 struct sh_stream *sh = demux_alloc_sh_stream(STREAM_AUDIO);
161 struct mp_codec_params *c = sh->codec;
162 c->channels = opts->channels.chmaps[0];
163 c->force_channels = true;
164 c->samplerate = opts->samplerate;
165
166 c->native_tb_num = 1;
167 c->native_tb_den = c->samplerate;
168
169 int f = opts->aformat;
170 // See PCM(): sign float bits endian
171 mp_set_pcm_codec(sh->codec, f & 1, f & 2, f >> 3, f & 4);
172 int samplesize = ((f >> 3) + 7) / 8;
173
174 demux_add_sh_stream(demuxer, sh);
175
176 struct priv *p = talloc_ptrtype(demuxer, p);
177 demuxer->priv = p;
178 *p = (struct priv) {
179 .sh = sh,
180 .frame_size = samplesize * c->channels.num,
181 .frame_rate = c->samplerate,
182 .read_frames = c->samplerate / 8,
183 };
184
185 return generic_open(demuxer);
186 }
187
demux_rawvideo_open(demuxer_t * demuxer,enum demux_check check)188 static int demux_rawvideo_open(demuxer_t *demuxer, enum demux_check check)
189 {
190 struct demux_rawvideo_opts *opts =
191 mp_get_config_group(demuxer, demuxer->global, &demux_rawvideo_conf);
192
193 if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE)
194 return -1;
195
196 int width = opts->width;
197 int height = opts->height;
198
199 if (!width || !height) {
200 MP_ERR(demuxer, "rawvideo: width or height not specified!\n");
201 return -1;
202 }
203
204 const char *decoder = "rawvideo";
205 int imgfmt = opts->vformat;
206 int imgsize = opts->imgsize;
207 int mp_imgfmt = 0;
208 if (opts->mp_format && !IMGFMT_IS_HWACCEL(opts->mp_format)) {
209 mp_imgfmt = opts->mp_format;
210 if (!imgsize) {
211 struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(opts->mp_format);
212 for (int p = 0; p < desc.num_planes; p++) {
213 imgsize += ((width >> desc.xs[p]) * (height >> desc.ys[p]) *
214 desc.bpp[p] + 7) / 8;
215 }
216 }
217 } else if (opts->codec && opts->codec[0])
218 decoder = talloc_strdup(demuxer, opts->codec);
219
220 if (!imgsize) {
221 int bpp = 0;
222 switch (imgfmt) {
223 case MKTAG('Y', 'V', '1', '2'):
224 case MKTAG('I', '4', '2', '0'):
225 case MKTAG('I', 'Y', 'U', 'V'):
226 bpp = 12;
227 break;
228 case MKTAG('U', 'Y', 'V', 'Y'):
229 case MKTAG('Y', 'U', 'Y', '2'):
230 bpp = 16;
231 break;
232 }
233 if (!bpp) {
234 MP_ERR(demuxer, "rawvideo: img size not specified and unknown format!\n");
235 return -1;
236 }
237 imgsize = width * height * bpp / 8;
238 }
239
240 struct sh_stream *sh = demux_alloc_sh_stream(STREAM_VIDEO);
241 struct mp_codec_params *c = sh->codec;
242 c->codec = decoder;
243 c->codec_tag = imgfmt;
244 c->fps = opts->fps;
245 c->reliable_fps = true;
246 c->disp_w = width;
247 c->disp_h = height;
248 if (mp_imgfmt) {
249 c->lav_codecpar = avcodec_parameters_alloc();
250 if (!c->lav_codecpar)
251 abort();
252 c->lav_codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
253 c->lav_codecpar->codec_id = mp_codec_to_av_codec_id(decoder);
254 c->lav_codecpar->format = imgfmt2pixfmt(mp_imgfmt);
255 c->lav_codecpar->width = width;
256 c->lav_codecpar->height = height;
257 }
258 demux_add_sh_stream(demuxer, sh);
259
260 struct priv *p = talloc_ptrtype(demuxer, p);
261 demuxer->priv = p;
262 *p = (struct priv) {
263 .sh = sh,
264 .frame_size = imgsize,
265 .frame_rate = c->fps,
266 .read_frames = 1,
267 };
268
269 return generic_open(demuxer);
270 }
271
raw_read_packet(struct demuxer * demuxer,struct demux_packet ** pkt)272 static bool raw_read_packet(struct demuxer *demuxer, struct demux_packet **pkt)
273 {
274 struct priv *p = demuxer->priv;
275
276 if (demuxer->stream->eof)
277 return false;
278
279 struct demux_packet *dp = new_demux_packet(p->frame_size * p->read_frames);
280 if (!dp) {
281 MP_ERR(demuxer, "Can't read packet.\n");
282 return true;
283 }
284
285 dp->keyframe = true;
286 dp->pos = stream_tell(demuxer->stream);
287 dp->pts = (dp->pos / p->frame_size) / p->frame_rate;
288
289 int len = stream_read(demuxer->stream, dp->buffer, dp->len);
290 demux_packet_shorten(dp, len);
291
292 dp->stream = p->sh->index;
293 *pkt = dp;
294
295 return true;
296 }
297
raw_seek(demuxer_t * demuxer,double seek_pts,int flags)298 static void raw_seek(demuxer_t *demuxer, double seek_pts, int flags)
299 {
300 struct priv *p = demuxer->priv;
301 stream_t *s = demuxer->stream;
302 int64_t end = stream_get_size(s);
303 int64_t frame_nr = seek_pts * p->frame_rate;
304 frame_nr = frame_nr - (frame_nr % p->read_frames);
305 int64_t pos = frame_nr * p->frame_size;
306 if (flags & SEEK_FACTOR)
307 pos = end * seek_pts;
308 if (pos < 0)
309 pos = 0;
310 if (end > 0 && pos > end)
311 pos = end;
312 stream_seek(s, (pos / p->frame_size) * p->frame_size);
313 }
314
315 const demuxer_desc_t demuxer_desc_rawaudio = {
316 .name = "rawaudio",
317 .desc = "Uncompressed audio",
318 .open = demux_rawaudio_open,
319 .read_packet = raw_read_packet,
320 .seek = raw_seek,
321 };
322
323 const demuxer_desc_t demuxer_desc_rawvideo = {
324 .name = "rawvideo",
325 .desc = "Uncompressed video",
326 .open = demux_rawvideo_open,
327 .read_packet = raw_read_packet,
328 .seek = raw_seek,
329 };
330