2017-06-21 16:06:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001 Alex Beregszaszi
|
|
|
|
*
|
|
|
|
* Feb 19, 2002: Significant rewrites by Charles R. Henrich (henrich@msu.edu)
|
|
|
|
* to add support for audio, and bktr *BSD support.
|
|
|
|
*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-07-05 15:00:23 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
|
|
|
|
#include "options/m_option.h"
|
|
|
|
#include "options/m_config.h"
|
|
|
|
#include "options/options.h"
|
|
|
|
|
|
|
|
#include "demux.h"
|
2014-09-24 20:55:50 +00:00
|
|
|
#include "codec_tags.h"
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
#include "audio/format.h"
|
2014-09-23 19:04:37 +00:00
|
|
|
#include "osdep/endian.h"
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
#include "stream/stream.h"
|
|
|
|
#include "stream/tv.h"
|
|
|
|
|
|
|
|
static int demux_open_tv(demuxer_t *demuxer, enum demux_check check)
|
|
|
|
{
|
|
|
|
tvi_handle_t *tvh;
|
|
|
|
const tvi_functions_t *funcs;
|
|
|
|
|
2017-02-02 17:24:27 +00:00
|
|
|
if (check > DEMUX_CHECK_REQUEST)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!demuxer->stream->info || strcmp(demuxer->stream->info->name, "tv") != 0)
|
2014-07-05 15:00:23 +00:00
|
|
|
return -1;
|
|
|
|
|
2016-09-06 18:09:44 +00:00
|
|
|
tv_param_t *params = mp_get_config_group(demuxer, demuxer->global,
|
|
|
|
&tv_params_conf);
|
2016-09-09 15:36:01 +00:00
|
|
|
bstr urlparams = bstr0(demuxer->stream->path);
|
|
|
|
bstr channel, input;
|
|
|
|
bstr_split_tok(urlparams, "/", &channel, &input);
|
|
|
|
if (channel.len) {
|
2017-02-02 07:50:18 +00:00
|
|
|
talloc_free(params->channel);
|
2016-09-09 15:36:01 +00:00
|
|
|
params->channel = bstrto0(NULL, channel);
|
|
|
|
}
|
|
|
|
if (input.len) {
|
|
|
|
bstr r;
|
|
|
|
params->input = bstrtoll(input, &r, 0);
|
|
|
|
if (r.len) {
|
|
|
|
MP_ERR(demuxer->stream, "invalid input: '%.*s'\n", BSTR_P(input));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-05 15:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(demuxer->priv==NULL);
|
|
|
|
if(!(tvh=tv_begin(params, demuxer->log))) return -1;
|
|
|
|
if (!tvh->functions->init(tvh->priv)) return -1;
|
|
|
|
|
|
|
|
tvh->demuxer = demuxer;
|
|
|
|
|
|
|
|
if (!open_tv(tvh)){
|
|
|
|
tv_uninit(tvh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
funcs = tvh->functions;
|
|
|
|
demuxer->priv=tvh;
|
|
|
|
|
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_v = demux_alloc_sh_stream(STREAM_VIDEO);
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
/* get IMAGE FORMAT */
|
|
|
|
int fourcc;
|
|
|
|
funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &fourcc);
|
2018-03-03 19:57:41 +00:00
|
|
|
if (fourcc == MP_FOURCC_MJPEG || fourcc == MP_FOURCC_JPEG) {
|
2016-01-12 22:48:19 +00:00
|
|
|
sh_v->codec->codec = "mjpeg";
|
2014-07-05 15:00:23 +00:00
|
|
|
} else {
|
2016-01-12 22:48:19 +00:00
|
|
|
sh_v->codec->codec = "rawvideo";
|
|
|
|
sh_v->codec->codec_tag = fourcc;
|
2014-07-05 15:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set FPS and FRAMETIME */
|
|
|
|
|
2016-01-12 22:48:19 +00:00
|
|
|
if(!sh_v->codec->fps)
|
2014-07-05 15:00:23 +00:00
|
|
|
{
|
|
|
|
float tmp;
|
|
|
|
if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE)
|
2016-01-12 22:48:19 +00:00
|
|
|
sh_v->codec->fps = 25.0f; /* on PAL */
|
|
|
|
else sh_v->codec->fps = tmp;
|
2014-07-05 15:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tvh->tv_param->fps != -1.0f)
|
2016-01-12 22:48:19 +00:00
|
|
|
sh_v->codec->fps = tvh->tv_param->fps;
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
/* If playback only mode, go to immediate mode, fail silently */
|
|
|
|
if(tvh->tv_param->immediate == 1)
|
|
|
|
{
|
|
|
|
funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0);
|
|
|
|
tvh->tv_param->audio = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set width */
|
2016-01-12 22:48:19 +00:00
|
|
|
funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_v->codec->disp_w);
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
/* set height */
|
2016-01-12 22:48:19 +00:00
|
|
|
funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_v->codec->disp_h);
|
2014-07-05 15:00:23 +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_v);
|
|
|
|
|
2014-07-05 15:00:23 +00:00
|
|
|
demuxer->seekable = 0;
|
|
|
|
|
|
|
|
/* here comes audio init */
|
|
|
|
if (tvh->tv_param->audio && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
|
|
|
|
{
|
|
|
|
int audio_format;
|
|
|
|
|
|
|
|
/* yeah, audio is present */
|
|
|
|
|
|
|
|
funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE,
|
|
|
|
&tvh->tv_param->audiorate);
|
|
|
|
|
|
|
|
if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE)
|
|
|
|
goto no_audio;
|
|
|
|
|
|
|
|
switch(audio_format)
|
|
|
|
{
|
2014-09-24 20:55:50 +00:00
|
|
|
// This is the only format any of the current inputs generate.
|
2014-09-23 19:04:37 +00:00
|
|
|
case AF_FORMAT_S16:
|
2014-07-05 15:00:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MP_ERR(tvh, "Audio type '%s' unsupported!\n",
|
|
|
|
af_fmt_to_str(audio_format));
|
|
|
|
goto no_audio;
|
|
|
|
}
|
|
|
|
|
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_a = demux_alloc_sh_stream(STREAM_AUDIO);
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE,
|
2016-01-12 22:48:19 +00:00
|
|
|
&sh_a->codec->samplerate);
|
|
|
|
int nchannels = sh_a->codec->channels.num;
|
2014-07-05 15:00:23 +00:00
|
|
|
funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS,
|
|
|
|
&nchannels);
|
2016-01-12 22:48:19 +00:00
|
|
|
mp_chmap_from_channels(&sh_a->codec->channels, nchannels);
|
2014-07-05 15:00:23 +00:00
|
|
|
|
2014-09-24 20:55:50 +00:00
|
|
|
// s16ne
|
2016-01-12 22:48:19 +00:00
|
|
|
mp_set_pcm_codec(sh_a->codec, true, false, 16, BYTE_ORDER == BIG_ENDIAN);
|
2014-07-05 15:00:23 +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_a);
|
|
|
|
|
2014-07-05 15:00:23 +00:00
|
|
|
MP_VERBOSE(tvh, " TV audio: %d channels, %d bits, %d Hz\n",
|
2016-01-12 22:48:19 +00:00
|
|
|
nchannels, 16, sh_a->codec->samplerate);
|
2014-07-05 15:00:23 +00:00
|
|
|
}
|
|
|
|
no_audio:
|
|
|
|
|
|
|
|
if(!(funcs->start(tvh->priv))){
|
|
|
|
// start failed :(
|
|
|
|
tv_uninit(tvh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set color eq */
|
|
|
|
tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness);
|
|
|
|
tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue);
|
|
|
|
tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation);
|
|
|
|
tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast);
|
|
|
|
|
|
|
|
if(tvh->tv_param->gain!=-1)
|
|
|
|
if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE)
|
|
|
|
MP_WARN(tvh, "Unable to set gain control!\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void demux_close_tv(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
|
|
|
|
if (!tvh) return;
|
|
|
|
tv_uninit(tvh);
|
|
|
|
free(tvh);
|
|
|
|
demuxer->priv=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int demux_tv_fill_buffer(demuxer_t *demux)
|
|
|
|
{
|
|
|
|
tvi_handle_t *tvh=(tvi_handle_t*)(demux->priv);
|
|
|
|
demux_packet_t* dp;
|
|
|
|
unsigned int len=0;
|
|
|
|
struct sh_stream *want_audio = NULL, *want_video = NULL;
|
|
|
|
|
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
|
|
|
int num_streams = demux_get_num_stream(demux);
|
|
|
|
for (int n = 0; n < num_streams; n++) {
|
|
|
|
struct sh_stream *sh = demux_get_stream(demux, n);
|
2014-07-06 17:02:21 +00:00
|
|
|
if (!demux_has_packet(sh) && demux_stream_is_selected(sh)) {
|
2014-07-05 15:00:23 +00:00
|
|
|
if (sh->type == STREAM_AUDIO)
|
|
|
|
want_audio = sh;
|
|
|
|
if (sh->type == STREAM_VIDEO)
|
|
|
|
want_video = sh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================== ADD AUDIO PACKET =================== */
|
|
|
|
|
|
|
|
if (want_audio && tvh->tv_param->audio &&
|
|
|
|
tvh->functions->control(tvh->priv,
|
|
|
|
TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE)
|
2014-09-16 16:11:00 +00:00
|
|
|
{
|
2014-07-05 15:00:23 +00:00
|
|
|
len = tvh->functions->get_audio_framesize(tvh->priv);
|
|
|
|
|
|
|
|
dp=new_demux_packet(len);
|
2014-09-16 16:11:00 +00:00
|
|
|
if (dp) {
|
|
|
|
dp->keyframe = true;
|
|
|
|
dp->pts=tvh->functions->grab_audio_frame(tvh->priv, dp->buffer,len);
|
|
|
|
demux_add_packet(want_audio, dp);
|
2014-07-05 15:00:23 +00:00
|
|
|
}
|
2014-09-16 16:11:00 +00:00
|
|
|
}
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
/* ================== ADD VIDEO PACKET =================== */
|
|
|
|
|
|
|
|
if (want_video && tvh->functions->control(tvh->priv,
|
|
|
|
TVI_CONTROL_IS_VIDEO, 0) == TVI_CONTROL_TRUE)
|
2014-09-16 16:11:00 +00:00
|
|
|
{
|
|
|
|
len = tvh->functions->get_video_framesize(tvh->priv);
|
2014-07-05 15:00:23 +00:00
|
|
|
dp=new_demux_packet(len);
|
2014-09-16 16:11:00 +00:00
|
|
|
if (dp) {
|
|
|
|
dp->keyframe = true;
|
|
|
|
dp->pts=tvh->functions->grab_video_frame(tvh->priv, dp->buffer, len);
|
|
|
|
demux_add_packet(want_video, dp);
|
|
|
|
}
|
|
|
|
}
|
2014-07-05 15:00:23 +00:00
|
|
|
|
|
|
|
if (tvh->tv_param->scan) tv_scan(tvh);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int demux_tv_control(demuxer_t *demuxer, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
tvi_handle_t *tvh=(tvi_handle_t*)(demuxer->priv);
|
|
|
|
if (cmd != DEMUXER_CTRL_STREAM_CTRL)
|
2017-06-19 15:54:02 +00:00
|
|
|
return CONTROL_UNKNOWN;
|
2014-07-05 15:00:23 +00:00
|
|
|
struct demux_ctrl_stream_ctrl *ctrl = arg;
|
|
|
|
ctrl->res = tv_stream_control(tvh, ctrl->ctrl, ctrl->arg);
|
2017-06-19 15:54:02 +00:00
|
|
|
return CONTROL_OK;
|
2014-07-05 15:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const demuxer_desc_t demuxer_desc_tv = {
|
|
|
|
.name = "tv",
|
|
|
|
.desc = "TV card demuxer",
|
|
|
|
.fill_buffer = demux_tv_fill_buffer,
|
|
|
|
.control = demux_tv_control,
|
|
|
|
.open = demux_open_tv,
|
|
|
|
.close = demux_close_tv,
|
|
|
|
};
|