mpv/filters/frame.c

191 lines
4.2 KiB
C
Raw Normal View History

video: rewrite filtering glue code Get rid of the old vf.c code. Replace it with a generic filtering framework, which can potentially handle more than just --vf. At least reimplementing --af with this code is planned. This changes some --vf semantics (including runtime behavior and the "vf" command). The most important ones are listed in interface-changes. vf_convert.c is renamed to f_swscale.c. It is now an internal filter that can not be inserted by the user manually. f_lavfi.c is a refactor of player/lavfi.c. The latter will be removed once --lavfi-complex is reimplemented on top of f_lavfi.c. (which is conceptually easy, but a big mess due to the data flow changes). The existing filters are all changed heavily. The data flow of the new filter framework is different. Especially EOF handling changes - EOF is now a "frame" rather than a state, and must be passed through exactly once. Another major thing is that all filters must support dynamic format changes. The filter reconfig() function goes away. (This sounds complex, but since all filters need to handle EOF draining anyway, they can use the same code, and it removes the mess with reconfig() having to predict the output format, which completely breaks with libavfilter anyway.) In addition, there is no automatic format negotiation or conversion. libavfilter's primitive and insufficient API simply doesn't allow us to do this in a reasonable way. Instead, filters can use f_autoconvert as sub-filter, and tell it which formats they support. This filter will in turn add actual conversion filters, such as f_swscale, to perform necessary format changes. vf_vapoursynth.c uses the same basic principle of operation as before, but with worryingly different details in data flow. Still appears to work. The hardware deint filters (vf_vavpp.c, vf_d3d11vpp.c, vf_vdpaupp.c) are heavily changed. Fortunately, they all used refqueue.c, which is for sharing the data flow logic (especially for managing future/past surfaces and such). It turns out it can be used to factor out most of the data flow. Some of these filters accepted software input. Instead of having ad-hoc upload code in each filter, surface upload is now delegated to f_autoconvert, which can use f_hwupload to perform this. Exporting VO capabilities is still a big mess (mp_stream_info stuff). The D3D11 code drops the redundant image formats, and all code uses the hw_subfmt (sw_format in FFmpeg) instead. Although that too seems to be a big mess for now. f_async_queue is unused.
2018-01-16 10:53:44 +00:00
#include <libavutil/frame.h>
#include "audio/aframe.h"
#include "common/av_common.h"
#include "demux/packet.h"
video: rewrite filtering glue code Get rid of the old vf.c code. Replace it with a generic filtering framework, which can potentially handle more than just --vf. At least reimplementing --af with this code is planned. This changes some --vf semantics (including runtime behavior and the "vf" command). The most important ones are listed in interface-changes. vf_convert.c is renamed to f_swscale.c. It is now an internal filter that can not be inserted by the user manually. f_lavfi.c is a refactor of player/lavfi.c. The latter will be removed once --lavfi-complex is reimplemented on top of f_lavfi.c. (which is conceptually easy, but a big mess due to the data flow changes). The existing filters are all changed heavily. The data flow of the new filter framework is different. Especially EOF handling changes - EOF is now a "frame" rather than a state, and must be passed through exactly once. Another major thing is that all filters must support dynamic format changes. The filter reconfig() function goes away. (This sounds complex, but since all filters need to handle EOF draining anyway, they can use the same code, and it removes the mess with reconfig() having to predict the output format, which completely breaks with libavfilter anyway.) In addition, there is no automatic format negotiation or conversion. libavfilter's primitive and insufficient API simply doesn't allow us to do this in a reasonable way. Instead, filters can use f_autoconvert as sub-filter, and tell it which formats they support. This filter will in turn add actual conversion filters, such as f_swscale, to perform necessary format changes. vf_vapoursynth.c uses the same basic principle of operation as before, but with worryingly different details in data flow. Still appears to work. The hardware deint filters (vf_vavpp.c, vf_d3d11vpp.c, vf_vdpaupp.c) are heavily changed. Fortunately, they all used refqueue.c, which is for sharing the data flow logic (especially for managing future/past surfaces and such). It turns out it can be used to factor out most of the data flow. Some of these filters accepted software input. Instead of having ad-hoc upload code in each filter, surface upload is now delegated to f_autoconvert, which can use f_hwupload to perform this. Exporting VO capabilities is still a big mess (mp_stream_info stuff). The D3D11 code drops the redundant image formats, and all code uses the hw_subfmt (sw_format in FFmpeg) instead. Although that too seems to be a big mess for now. f_async_queue is unused.
2018-01-16 10:53:44 +00:00
#include "video/mp_image.h"
#include "frame.h"
struct frame_handler {
const char *name;
bool is_data;
bool is_signaling;
void *(*new_ref)(void *data);
double (*get_pts)(void *data);
void (*set_pts)(void *data, double pts);
AVFrame *(*new_av_ref)(void *data);
void *(*from_av_ref)(AVFrame *data);
void (*free)(void *data);
};
static void *video_ref(void *data)
{
return mp_image_new_ref(data);
}
static double video_get_pts(void *data)
{
return ((struct mp_image *)data)->pts;
}
static void video_set_pts(void *data, double pts)
{
((struct mp_image *)data)->pts = pts;
}
static AVFrame *video_new_av_ref(void *data)
{
return mp_image_to_av_frame(data);
}
static void *video_from_av_ref(AVFrame *data)
{
return mp_image_from_av_frame(data);
}
static void *audio_ref(void *data)
{
return mp_aframe_new_ref(data);
}
static double audio_get_pts(void *data)
{
return mp_aframe_get_pts(data);
}
static void audio_set_pts(void *data, double pts)
{
mp_aframe_set_pts(data, pts);
}
static AVFrame *audio_new_av_ref(void *data)
{
return mp_aframe_to_avframe(data);
}
static void *audio_from_av_ref(AVFrame *data)
{
return mp_aframe_from_avframe(data);
}
static void *packet_ref(void *data)
{
return demux_copy_packet(data);
}
video: rewrite filtering glue code Get rid of the old vf.c code. Replace it with a generic filtering framework, which can potentially handle more than just --vf. At least reimplementing --af with this code is planned. This changes some --vf semantics (including runtime behavior and the "vf" command). The most important ones are listed in interface-changes. vf_convert.c is renamed to f_swscale.c. It is now an internal filter that can not be inserted by the user manually. f_lavfi.c is a refactor of player/lavfi.c. The latter will be removed once --lavfi-complex is reimplemented on top of f_lavfi.c. (which is conceptually easy, but a big mess due to the data flow changes). The existing filters are all changed heavily. The data flow of the new filter framework is different. Especially EOF handling changes - EOF is now a "frame" rather than a state, and must be passed through exactly once. Another major thing is that all filters must support dynamic format changes. The filter reconfig() function goes away. (This sounds complex, but since all filters need to handle EOF draining anyway, they can use the same code, and it removes the mess with reconfig() having to predict the output format, which completely breaks with libavfilter anyway.) In addition, there is no automatic format negotiation or conversion. libavfilter's primitive and insufficient API simply doesn't allow us to do this in a reasonable way. Instead, filters can use f_autoconvert as sub-filter, and tell it which formats they support. This filter will in turn add actual conversion filters, such as f_swscale, to perform necessary format changes. vf_vapoursynth.c uses the same basic principle of operation as before, but with worryingly different details in data flow. Still appears to work. The hardware deint filters (vf_vavpp.c, vf_d3d11vpp.c, vf_vdpaupp.c) are heavily changed. Fortunately, they all used refqueue.c, which is for sharing the data flow logic (especially for managing future/past surfaces and such). It turns out it can be used to factor out most of the data flow. Some of these filters accepted software input. Instead of having ad-hoc upload code in each filter, surface upload is now delegated to f_autoconvert, which can use f_hwupload to perform this. Exporting VO capabilities is still a big mess (mp_stream_info stuff). The D3D11 code drops the redundant image formats, and all code uses the hw_subfmt (sw_format in FFmpeg) instead. Although that too seems to be a big mess for now. f_async_queue is unused.
2018-01-16 10:53:44 +00:00
static const struct frame_handler frame_handlers[] = {
[MP_FRAME_NONE] = {
.name = "none",
},
[MP_FRAME_EOF] = {
.name = "eof",
.is_signaling = true,
},
[MP_FRAME_VIDEO] = {
.name = "video",
.is_data = true,
.new_ref = video_ref,
.get_pts = video_get_pts,
.set_pts = video_set_pts,
.new_av_ref = video_new_av_ref,
.from_av_ref = video_from_av_ref,
.free = talloc_free,
},
[MP_FRAME_AUDIO] = {
.name = "audio",
.is_data = true,
.new_ref = audio_ref,
.get_pts = audio_get_pts,
.set_pts = audio_set_pts,
.new_av_ref = audio_new_av_ref,
.from_av_ref = audio_from_av_ref,
.free = talloc_free,
},
[MP_FRAME_PACKET] = {
.name = "packet",
.is_data = true,
.new_ref = packet_ref,
.free = talloc_free,
},
video: rewrite filtering glue code Get rid of the old vf.c code. Replace it with a generic filtering framework, which can potentially handle more than just --vf. At least reimplementing --af with this code is planned. This changes some --vf semantics (including runtime behavior and the "vf" command). The most important ones are listed in interface-changes. vf_convert.c is renamed to f_swscale.c. It is now an internal filter that can not be inserted by the user manually. f_lavfi.c is a refactor of player/lavfi.c. The latter will be removed once --lavfi-complex is reimplemented on top of f_lavfi.c. (which is conceptually easy, but a big mess due to the data flow changes). The existing filters are all changed heavily. The data flow of the new filter framework is different. Especially EOF handling changes - EOF is now a "frame" rather than a state, and must be passed through exactly once. Another major thing is that all filters must support dynamic format changes. The filter reconfig() function goes away. (This sounds complex, but since all filters need to handle EOF draining anyway, they can use the same code, and it removes the mess with reconfig() having to predict the output format, which completely breaks with libavfilter anyway.) In addition, there is no automatic format negotiation or conversion. libavfilter's primitive and insufficient API simply doesn't allow us to do this in a reasonable way. Instead, filters can use f_autoconvert as sub-filter, and tell it which formats they support. This filter will in turn add actual conversion filters, such as f_swscale, to perform necessary format changes. vf_vapoursynth.c uses the same basic principle of operation as before, but with worryingly different details in data flow. Still appears to work. The hardware deint filters (vf_vavpp.c, vf_d3d11vpp.c, vf_vdpaupp.c) are heavily changed. Fortunately, they all used refqueue.c, which is for sharing the data flow logic (especially for managing future/past surfaces and such). It turns out it can be used to factor out most of the data flow. Some of these filters accepted software input. Instead of having ad-hoc upload code in each filter, surface upload is now delegated to f_autoconvert, which can use f_hwupload to perform this. Exporting VO capabilities is still a big mess (mp_stream_info stuff). The D3D11 code drops the redundant image formats, and all code uses the hw_subfmt (sw_format in FFmpeg) instead. Although that too seems to be a big mess for now. f_async_queue is unused.
2018-01-16 10:53:44 +00:00
};
const char *mp_frame_type_str(enum mp_frame_type t)
{
return frame_handlers[t].name;
}
bool mp_frame_is_data(struct mp_frame frame)
{
return frame_handlers[frame.type].is_data;
}
bool mp_frame_is_signaling(struct mp_frame frame)
{
return frame_handlers[frame.type].is_signaling;
}
void mp_frame_unref(struct mp_frame *frame)
{
if (!frame)
return;
if (frame_handlers[frame->type].free)
frame_handlers[frame->type].free(frame->data);
*frame = (struct mp_frame){0};
}
struct mp_frame mp_frame_ref(struct mp_frame frame)
{
if (frame_handlers[frame.type].new_ref) {
assert(frame.data);
frame.data = frame_handlers[frame.type].new_ref(frame.data);
if (!frame.data)
frame.type = MP_FRAME_NONE;
}
return frame;
}
double mp_frame_get_pts(struct mp_frame frame)
{
if (frame_handlers[frame.type].get_pts)
return frame_handlers[frame.type].get_pts(frame.data);
return MP_NOPTS_VALUE;
}
void mp_frame_set_pts(struct mp_frame frame, double pts)
{
if (frame_handlers[frame.type].get_pts)
frame_handlers[frame.type].set_pts(frame.data, pts);
}
AVFrame *mp_frame_to_av(struct mp_frame frame, struct AVRational *tb)
{
if (!frame_handlers[frame.type].new_av_ref)
return NULL;
AVFrame *res = frame_handlers[frame.type].new_av_ref(frame.data);
if (!res)
return NULL;
res->pts = mp_pts_to_av(mp_frame_get_pts(frame), tb);
return res;
}
struct mp_frame mp_frame_from_av(enum mp_frame_type type, struct AVFrame *frame,
struct AVRational *tb)
{
struct mp_frame res = {type};
if (!frame_handlers[res.type].from_av_ref)
return MP_NO_FRAME;
res.data = frame_handlers[res.type].from_av_ref(frame);
if (!res.data)
return MP_NO_FRAME;
mp_frame_set_pts(res, mp_pts_from_av(frame->pts, tb));
return res;
}