2009-05-08 21:51:13 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-05-08 21:51:13 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2009-05-08 21:51:13 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-05-08 21:51:13 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-05-08 21:51:13 +00:00
|
|
|
*/
|
2003-01-22 23:51:04 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2014-06-10 21:06:42 +00:00
|
|
|
#include "options/options.h"
|
2003-01-22 23:51:04 +00:00
|
|
|
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "demux.h"
|
2003-01-22 23:51:04 +00:00
|
|
|
#include "stheader.h"
|
2014-09-24 20:55:50 +00:00
|
|
|
#include "codec_tags.h"
|
2003-01-22 23:51:04 +00:00
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/img_format.h"
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
#include "video/img_fourcc.h"
|
2003-01-22 23:51:04 +00:00
|
|
|
|
2014-09-23 19:04:37 +00:00
|
|
|
#include "osdep/endian.h"
|
|
|
|
|
2014-06-10 21:06:42 +00:00
|
|
|
struct demux_rawaudio_opts {
|
|
|
|
struct mp_chmap channels;
|
|
|
|
int samplerate;
|
|
|
|
int aformat;
|
|
|
|
};
|
|
|
|
|
2014-09-24 20:55:50 +00:00
|
|
|
// Ad-hoc schema to systematically encode the format as int
|
|
|
|
#define PCM(sign, is_float, bits, is_be) \
|
|
|
|
((sign) | ((is_float) << 1) | ((is_be) << 2) | ((bits) << 3))
|
|
|
|
#define NE (BYTE_ORDER == BIG_ENDIAN)
|
|
|
|
|
2014-06-10 21:06:42 +00:00
|
|
|
#define OPT_BASE_STRUCT struct demux_rawaudio_opts
|
|
|
|
const struct m_sub_options demux_rawaudio_conf = {
|
|
|
|
.opts = (const m_option_t[]) {
|
|
|
|
OPT_CHMAP("channels", channels, CONF_MIN, .min = 1),
|
|
|
|
OPT_INTRANGE("rate", samplerate, 0, 1000, 8 * 48000),
|
2014-09-24 20:55:50 +00:00
|
|
|
OPT_CHOICE("format", aformat, 0,
|
|
|
|
({"u8", PCM(0, 0, 8, 0)},
|
|
|
|
{"s8", PCM(1, 0, 8, 0)},
|
|
|
|
{"u16le", PCM(0, 0, 16, 0)}, {"u16be", PCM(0, 0, 16, 1)},
|
|
|
|
{"s16le", PCM(1, 0, 16, 0)}, {"u16be", PCM(1, 0, 16, 1)},
|
|
|
|
{"u24le", PCM(0, 0, 24, 0)}, {"u24be", PCM(0, 0, 24, 1)},
|
|
|
|
{"s24le", PCM(1, 0, 24, 0)}, {"s24be", PCM(1, 0, 24, 1)},
|
|
|
|
{"u32le", PCM(0, 0, 32, 0)}, {"u32be", PCM(0, 0, 32, 1)},
|
|
|
|
{"s32le", PCM(1, 0, 32, 0)}, {"s32be", PCM(1, 0, 32, 1)},
|
|
|
|
{"floatle", PCM(0, 1, 32, 0)}, {"floatbe", PCM(0, 1, 32, 1)},
|
|
|
|
{"doublele",PCM(0, 1, 64, 0)}, {"doublebe", PCM(0, 1, 64, 1)},
|
|
|
|
{"u16", PCM(0, 0, 16, NE)},
|
|
|
|
{"s16", PCM(1, 0, 16, NE)},
|
|
|
|
{"u24", PCM(0, 0, 24, NE)},
|
|
|
|
{"s24", PCM(1, 0, 24, NE)},
|
|
|
|
{"u32", PCM(0, 0, 32, NE)},
|
|
|
|
{"s32", PCM(1, 0, 32, NE)},
|
|
|
|
{"float", PCM(0, 1, 32, NE)},
|
|
|
|
{"double", PCM(0, 1, 64, NE)})),
|
2014-06-10 21:06:42 +00:00
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct demux_rawaudio_opts),
|
|
|
|
.defaults = &(const struct demux_rawaudio_opts){
|
2014-09-23 19:04:37 +00:00
|
|
|
// Note that currently, stream_cdda expects exactly these parameters!
|
2014-06-10 21:06:42 +00:00
|
|
|
.channels = MP_CHMAP_INIT_STEREO,
|
|
|
|
.samplerate = 44100,
|
2014-09-25 17:52:17 +00:00
|
|
|
.aformat = PCM(1, 0, 16, 0), // s16le
|
2014-06-10 21:06:42 +00:00
|
|
|
},
|
2013-07-12 20:52:10 +00:00
|
|
|
};
|
|
|
|
|
2014-09-24 20:55:50 +00:00
|
|
|
#undef PCM
|
|
|
|
#undef NE
|
|
|
|
|
2014-06-10 21:06:42 +00:00
|
|
|
struct demux_rawvideo_opts {
|
|
|
|
int vformat;
|
|
|
|
int mp_format;
|
|
|
|
char *codec;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
float fps;
|
|
|
|
int imgsize;
|
|
|
|
};
|
2013-07-12 20:52:10 +00:00
|
|
|
|
2014-06-10 21:06:42 +00:00
|
|
|
#undef OPT_BASE_STRUCT
|
|
|
|
#define OPT_BASE_STRUCT struct demux_rawvideo_opts
|
|
|
|
const struct m_sub_options demux_rawvideo_conf = {
|
|
|
|
.opts = (const m_option_t[]) {
|
|
|
|
OPT_INTRANGE("w", width, 0, 1, 8192),
|
|
|
|
OPT_INTRANGE("h", height, 0, 1, 8192),
|
|
|
|
OPT_GENERAL(int, "format", vformat, 0, .type = &m_option_type_fourcc),
|
|
|
|
OPT_IMAGEFORMAT("mp-format", mp_format, 0),
|
|
|
|
OPT_STRING("codec", codec, 0),
|
|
|
|
OPT_FLOATRANGE("fps", fps, 0, 0.001, 1000),
|
|
|
|
OPT_INTRANGE("size", imgsize, 0, 1, 8192 * 8192 * 4),
|
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct demux_rawvideo_opts),
|
|
|
|
.defaults = &(const struct demux_rawvideo_opts){
|
|
|
|
.vformat = MP_FOURCC_I420,
|
|
|
|
.width = 1280,
|
|
|
|
.height = 720,
|
|
|
|
.fps = 25,
|
|
|
|
},
|
2013-07-12 20:52:10 +00:00
|
|
|
};
|
|
|
|
|
2014-06-10 21:06:42 +00:00
|
|
|
struct priv {
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
struct sh_stream *sh;
|
2014-06-10 21:06:42 +00:00
|
|
|
int frame_size;
|
|
|
|
int read_frames;
|
|
|
|
double frame_rate;
|
2003-01-22 23:51:04 +00:00
|
|
|
};
|
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
static int demux_rawaudio_open(demuxer_t *demuxer, enum demux_check check)
|
2013-07-12 20:52:10 +00:00
|
|
|
{
|
2014-06-10 21:06:42 +00:00
|
|
|
struct demux_rawaudio_opts *opts = demuxer->opts->demux_rawaudio;
|
2013-07-14 15:55:54 +00:00
|
|
|
|
|
|
|
if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE)
|
|
|
|
return -1;
|
|
|
|
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
struct sh_stream *sh = demux_alloc_sh_stream(STREAM_AUDIO);
|
2016-01-12 22:48:19 +00:00
|
|
|
struct mp_codec_params *c = sh->codec;
|
|
|
|
c->channels = opts->channels;
|
|
|
|
c->force_channels = true;
|
|
|
|
c->samplerate = opts->samplerate;
|
2014-09-24 20:55:50 +00:00
|
|
|
|
|
|
|
int f = opts->aformat;
|
2016-01-12 22:48:19 +00:00
|
|
|
// See PCM(): sign float bits endian
|
|
|
|
mp_set_pcm_codec(sh->codec, f & 1, f & 2, f >> 3, f & 4);
|
2014-09-24 20:55:50 +00:00
|
|
|
int samplesize = ((f >> 3) + 7) / 8;
|
2013-07-14 15:55:54 +00:00
|
|
|
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
demux_add_sh_stream(demuxer, sh);
|
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
struct priv *p = talloc_ptrtype(demuxer, p);
|
|
|
|
demuxer->priv = p;
|
|
|
|
*p = (struct priv) {
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
.sh = sh,
|
2016-01-12 22:48:19 +00:00
|
|
|
.frame_size = samplesize * c->channels.num,
|
|
|
|
.frame_rate = c->samplerate,
|
|
|
|
.read_frames = c->samplerate / 8,
|
2013-07-14 15:55:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return 0;
|
2013-07-12 20:52:10 +00:00
|
|
|
}
|
2003-01-22 23:51:04 +00:00
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
static int demux_rawvideo_open(demuxer_t *demuxer, enum demux_check check)
|
2013-07-12 19:58:11 +00:00
|
|
|
{
|
2014-06-10 21:06:42 +00:00
|
|
|
struct demux_rawvideo_opts *opts = demuxer->opts->demux_rawvideo;
|
2013-07-14 15:55:54 +00:00
|
|
|
|
|
|
|
if (check != DEMUX_CHECK_REQUEST && check != DEMUX_CHECK_FORCE)
|
|
|
|
return -1;
|
|
|
|
|
2014-06-10 21:06:42 +00:00
|
|
|
int width = opts->width;
|
|
|
|
int height = opts->height;
|
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
if (!width || !height) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_ERR(demuxer, "rawvideo: width or height not specified!\n");
|
2013-07-14 15:55:54 +00:00
|
|
|
return -1;
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
}
|
2013-07-14 15:55:54 +00:00
|
|
|
|
|
|
|
const char *decoder = "rawvideo";
|
2014-06-10 21:06:42 +00:00
|
|
|
int imgfmt = opts->vformat;
|
|
|
|
int imgsize = opts->imgsize;
|
|
|
|
if (opts->mp_format && !IMGFMT_IS_HWACCEL(opts->mp_format)) {
|
2013-07-14 15:55:54 +00:00
|
|
|
decoder = "mp-rawvideo";
|
2014-06-10 21:06:42 +00:00
|
|
|
imgfmt = opts->mp_format;
|
2013-07-14 15:55:54 +00:00
|
|
|
if (!imgsize) {
|
2014-06-10 21:06:42 +00:00
|
|
|
struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(opts->mp_format);
|
2013-07-14 15:55:54 +00:00
|
|
|
for (int p = 0; p < desc.num_planes; p++) {
|
|
|
|
imgsize += ((width >> desc.xs[p]) * (height >> desc.ys[p]) *
|
|
|
|
desc.bpp[p] + 7) / 8;
|
|
|
|
}
|
|
|
|
}
|
2014-06-10 21:06:42 +00:00
|
|
|
} else if (opts->codec && opts->codec[0])
|
|
|
|
decoder = talloc_strdup(demuxer, opts->codec);
|
2013-07-14 15:55:54 +00:00
|
|
|
|
|
|
|
if (!imgsize) {
|
|
|
|
int bpp = 0;
|
2014-06-10 21:06:42 +00:00
|
|
|
switch (imgfmt) {
|
2013-07-14 15:55:54 +00:00
|
|
|
case MP_FOURCC_I420: case MP_FOURCC_IYUV:
|
|
|
|
case MP_FOURCC_NV12: case MP_FOURCC_NV21:
|
|
|
|
case MP_FOURCC_HM12:
|
|
|
|
case MP_FOURCC_YV12:
|
|
|
|
bpp = 12;
|
|
|
|
break;
|
|
|
|
case MP_FOURCC_RGB12: case MP_FOURCC_BGR12:
|
|
|
|
case MP_FOURCC_RGB15: case MP_FOURCC_BGR15:
|
|
|
|
case MP_FOURCC_RGB16: case MP_FOURCC_BGR16:
|
|
|
|
case MP_FOURCC_YUY2: case MP_FOURCC_UYVY:
|
|
|
|
bpp = 16;
|
|
|
|
break;
|
|
|
|
case MP_FOURCC_RGB8: case MP_FOURCC_BGR8:
|
|
|
|
case MP_FOURCC_Y800: case MP_FOURCC_Y8:
|
|
|
|
bpp = 8;
|
|
|
|
break;
|
|
|
|
case MP_FOURCC_RGB24: case MP_FOURCC_BGR24:
|
|
|
|
bpp = 24;
|
|
|
|
break;
|
|
|
|
case MP_FOURCC_RGB32: case MP_FOURCC_BGR32:
|
|
|
|
bpp = 32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!bpp) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_ERR(demuxer, "rawvideo: img size not specified and unknown format!\n");
|
2013-07-14 15:55:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
imgsize = width * height * bpp / 8;
|
video: decouple internal pixel formats from FourCCs
mplayer's video chain traditionally used FourCCs for pixel formats. For
example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the
string 'YV12' interpreted as unsigned int. Additionally, it used to
encode information into the numeric values of some formats. The RGB
formats had their bit depth and endian encoded into the least
significant byte. Extended planar formats (420P10 etc.) had chroma
shift, endian, and component bit depth encoded. (This has been removed
in recent commits.)
Replace the FourCC mess with a simple enum. Remove all the redundant
formats like YV12/I420/IYUV. Replace some image format names by
something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P.
Add img_fourcc.h, which contains the old IDs for code that actually uses
FourCCs. Change the way demuxers, that output raw video, identify the
video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to
request the rawvideo decoder, and sh_video->imgfmt specifies the pixel
format. Like the previous hack, this is supposed to avoid the need for
a complete codecs.cfg entry per format, or other lookup tables. (Note
that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT
raw video, but this is still considered better than adding a raw video
decoder - even if trivial, it would be full of annoying lookup tables.)
The TV code has not been tested.
Some corrective changes regarding endian and other image format flags
creep in.
2012-12-23 19:03:30 +00:00
|
|
|
}
|
2013-07-14 15:55:54 +00:00
|
|
|
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
struct sh_stream *sh = demux_alloc_sh_stream(STREAM_VIDEO);
|
2016-01-12 22:48:19 +00:00
|
|
|
struct mp_codec_params *c = sh->codec;
|
|
|
|
c->codec = decoder;
|
|
|
|
c->codec_tag = imgfmt;
|
|
|
|
c->fps = opts->fps;
|
|
|
|
c->disp_w = width;
|
|
|
|
c->disp_h = height;
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
demux_add_sh_stream(demuxer, sh);
|
2013-07-14 15:55:54 +00:00
|
|
|
|
|
|
|
struct priv *p = talloc_ptrtype(demuxer, p);
|
|
|
|
demuxer->priv = p;
|
|
|
|
*p = (struct priv) {
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
.sh = sh,
|
2013-07-14 15:55:54 +00:00
|
|
|
.frame_size = imgsize,
|
2016-01-12 22:48:19 +00:00
|
|
|
.frame_rate = c->fps,
|
2013-08-22 16:56:19 +00:00
|
|
|
.read_frames = 1,
|
2013-07-14 15:55:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return 0;
|
2003-01-22 23:51:04 +00:00
|
|
|
}
|
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
static int raw_fill_buffer(demuxer_t *demuxer)
|
2013-07-11 17:17:51 +00:00
|
|
|
{
|
2013-07-14 15:55:54 +00:00
|
|
|
struct priv *p = demuxer->priv;
|
2013-07-08 22:03:14 +00:00
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
if (demuxer->stream->eof)
|
|
|
|
return 0;
|
2013-07-08 22:03:14 +00:00
|
|
|
|
2013-08-22 16:56:19 +00:00
|
|
|
struct demux_packet *dp = new_demux_packet(p->frame_size * p->read_frames);
|
2014-09-16 16:11:00 +00:00
|
|
|
if (!dp) {
|
|
|
|
MP_ERR(demuxer, "Can't read packet.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:03:07 +00:00
|
|
|
dp->pos = stream_tell(demuxer->stream);
|
2013-07-14 15:55:54 +00:00
|
|
|
dp->pts = (dp->pos / p->frame_size) / p->frame_rate;
|
2013-07-08 22:03:14 +00:00
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
int len = stream_read(demuxer->stream, dp->buffer, dp->len);
|
2014-06-13 00:02:30 +00:00
|
|
|
demux_packet_shorten(dp, len);
|
demux: remove weird tripple-buffering for the sh_stream list
The demuxer infrastructure was originally single-threaded. To make it
suitable for multithreading (specifically, demuxing and decoding on
separate threads), some sort of tripple-buffering was introduced. There
are separate "struct demuxer" allocations. The demuxer thread sets the
state on d_thread. If anything changes, the state is copied to d_buffer
(the copy is protected by a lock), and the decoder thread is notified.
Then the decoder thread copies the state from d_buffer to d_user (again
while holding a lock). This avoids the need for locking in the
demuxer/decoder code itself (only demux.c needs an internal, "invisible"
lock.)
Remove the streams/num_streams fields from this tripple-buffering
schema. Move them to the internal struct, and protect them with the
internal lock. Use accessors for read access outside of demux.c.
Other than replacing all field accesses with accessors, this separates
allocating and adding sh_streams. This is needed to avoid race
conditions. Before this change, this was awkwardly handled by first
initializing the sh_stream, and then sending a stream change event. Now
the stream is allocated, then initialized, and then declared as
immutable and added (at which point it becomes visible to the decoder
thread immediately).
This change is useful for PR #2626. And eventually, we should probably
get entirely of the tripple buffering, and this makes a nice first step.
2015-12-23 20:44:53 +00:00
|
|
|
demux_add_packet(p->sh, dp);
|
2013-07-08 22:03:14 +00:00
|
|
|
|
2013-07-14 15:55:54 +00:00
|
|
|
return 1;
|
2003-01-22 23:51:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 18:14:23 +00:00
|
|
|
static void raw_seek(demuxer_t *demuxer, double seek_pts, int flags)
|
2013-07-14 15:55:54 +00:00
|
|
|
{
|
|
|
|
struct priv *p = demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
2014-05-24 12:04:09 +00:00
|
|
|
int64_t end = 0;
|
|
|
|
stream_control(s, STREAM_CTRL_GET_SIZE, &end);
|
2016-02-28 18:14:23 +00:00
|
|
|
int64_t pos = seek_pts * p->frame_rate * p->frame_size;
|
2013-07-14 15:55:54 +00:00
|
|
|
if (flags & SEEK_FACTOR)
|
2016-02-28 18:14:23 +00:00
|
|
|
pos = end * seek_pts;
|
2013-07-14 15:55:54 +00:00
|
|
|
if (pos < 0)
|
|
|
|
pos = 0;
|
|
|
|
if (end && pos > end)
|
|
|
|
pos = end;
|
|
|
|
stream_seek(s, (pos / p->frame_size) * p->frame_size);
|
2003-01-22 23:51:04 +00:00
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2013-07-12 21:09:45 +00:00
|
|
|
static int raw_control(demuxer_t *demuxer, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct priv *p = demuxer->priv;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH: {
|
|
|
|
stream_t *s = demuxer->stream;
|
2014-05-24 12:04:09 +00:00
|
|
|
int64_t end = 0;
|
|
|
|
if (stream_control(s, STREAM_CTRL_GET_SIZE, &end) != STREAM_OK)
|
2013-07-12 21:09:45 +00:00
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
|
2014-05-24 12:03:07 +00:00
|
|
|
*((double *) arg) = (end / p->frame_size) / p->frame_rate;
|
2013-07-12 21:09:45 +00:00
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 20:52:10 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_rawaudio = {
|
|
|
|
.name = "rawaudio",
|
|
|
|
.desc = "Uncompressed audio",
|
|
|
|
.open = demux_rawaudio_open,
|
|
|
|
.fill_buffer = raw_fill_buffer,
|
|
|
|
.seek = raw_seek,
|
2013-07-12 21:09:45 +00:00
|
|
|
.control = raw_control,
|
2013-07-12 20:52:10 +00:00
|
|
|
};
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_rawvideo = {
|
2013-07-11 18:08:12 +00:00
|
|
|
.name = "rawvideo",
|
2013-07-12 20:12:02 +00:00
|
|
|
.desc = "Uncompressed video",
|
2013-07-11 18:08:12 +00:00
|
|
|
.open = demux_rawvideo_open,
|
2013-07-12 20:52:10 +00:00
|
|
|
.fill_buffer = raw_fill_buffer,
|
|
|
|
.seek = raw_seek,
|
2013-07-12 21:09:45 +00:00
|
|
|
.control = raw_control,
|
2005-08-05 19:57:47 +00:00
|
|
|
};
|