|
| 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 <assert.h> |
| 19 | +#include <math.h> |
| 20 | +#include <limits.h> |
| 21 | + |
| 22 | +#include <libavutil/common.h> |
| 23 | +#include <subrandr/subrandr.h> |
| 24 | + |
| 25 | +#include "mpv_talloc.h" |
| 26 | + |
| 27 | +#include "options/m_config.h" |
| 28 | +#include "options/options.h" |
| 29 | +#include "common/common.h" |
| 30 | +#include "demux/packet_pool.h" |
| 31 | +#include "demux/stheader.h" |
| 32 | +#include "sub/osd.h" |
| 33 | +#include "video/mp_image.h" |
| 34 | +#include "sd.h" |
| 35 | + |
| 36 | +struct sd_sbr_priv { |
| 37 | + struct sbr_library *sbr_library; |
| 38 | + struct sbr_renderer *sbr_renderer; |
| 39 | + struct sbr_subtitles *sbr_subtitles; |
| 40 | + struct mp_image_params video_params; |
| 41 | + struct mp_osd_res osd; |
| 42 | + struct sub_bitmaps *bitmaps; |
| 43 | +}; |
| 44 | + |
| 45 | +static void enable_output(struct sd *sd, bool enable) |
| 46 | +{ |
| 47 | + struct sd_sbr_priv *ctx = sd->priv; |
| 48 | + if (enable == !!ctx->sbr_renderer) |
| 49 | + return; |
| 50 | + if (ctx->sbr_renderer) { |
| 51 | + sbr_renderer_destroy(ctx->sbr_renderer); |
| 52 | + ctx->sbr_renderer = NULL; |
| 53 | + } else { |
| 54 | + ctx->sbr_renderer = sbr_renderer_create(ctx->sbr_library); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +static int init(struct sd *sd) |
| 59 | +{ |
| 60 | + if(strcmp(sd->codec->codec, "textsub/srv3") && strcmp(sd->codec->codec, "textsub/vtt")) |
| 61 | + return -1; |
| 62 | + |
| 63 | + struct sd_sbr_priv *ctx = talloc_zero(sd, struct sd_sbr_priv); |
| 64 | + sd->priv = ctx; |
| 65 | + |
| 66 | + ctx->sbr_library = sbr_library_init(); |
| 67 | + ctx->bitmaps = talloc_zero(ctx, struct sub_bitmaps); |
| 68 | + ctx->bitmaps->format = SUBBITMAP_BGRA; |
| 69 | + ctx->bitmaps->num_parts = 1; |
| 70 | + ctx->bitmaps->parts = talloc_zero(ctx->bitmaps, struct sub_bitmap); |
| 71 | + |
| 72 | + enable_output(sd, true); |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static void decode(struct sd *sd, struct demux_packet *packet) |
| 78 | +{ |
| 79 | + struct sd_sbr_priv *ctx = sd->priv; |
| 80 | + |
| 81 | + if(ctx->sbr_subtitles) |
| 82 | + sbr_subtitles_destroy(ctx->sbr_subtitles); |
| 83 | + |
| 84 | + sbr_subtitle_format fmt = SBR_SUBTITLE_FORMAT_UNKOWN; |
| 85 | + if(!strcmp(sd->codec->codec, "textsub/srv3") ) |
| 86 | + fmt = SBR_SUBTITLE_FORMAT_SRV3; |
| 87 | + else if(!strcmp(sd->codec->codec, "textsub/vtt")) |
| 88 | + fmt = SBR_SUBTITLE_FORMAT_WEBVTT; |
| 89 | + |
| 90 | + ctx->sbr_subtitles = sbr_load_text( |
| 91 | + ctx->sbr_library, |
| 92 | + packet->buffer, |
| 93 | + packet->len, |
| 94 | + fmt, |
| 95 | + NULL |
| 96 | + ); |
| 97 | + packet->sub_duration = packet->duration; |
| 98 | +} |
| 99 | + |
| 100 | +static struct sub_bitmaps *get_bitmaps(struct sd *sd, struct mp_osd_res dim, |
| 101 | + int format, double pts) |
| 102 | +{ |
| 103 | + struct sd_sbr_priv *ctx = sd->priv; |
| 104 | + struct mp_subtitle_opts *opts = sd->opts; |
| 105 | + |
| 106 | + ctx->osd = dim; |
| 107 | + |
| 108 | + if (pts == MP_NOPTS_VALUE || !ctx->sbr_renderer || !ctx->sbr_subtitles) |
| 109 | + return NULL; |
| 110 | + |
| 111 | + if (opts->sub_forced_events_only) |
| 112 | + return NULL; |
| 113 | + |
| 114 | + struct sbr_subtitle_context context = (sbr_subtitle_context) { |
| 115 | + .dpi = 72, |
| 116 | + .padding_top = (int32_t)dim.mt << 6, |
| 117 | + .padding_bottom = (int32_t)dim.mb << 6, |
| 118 | + .padding_left = (int32_t)dim.ml << 6, |
| 119 | + .padding_right = (int32_t)dim.mr << 6, |
| 120 | + .video_height = (int32_t)(dim.h - dim.mt - dim.mb) << 6, |
| 121 | + .video_width = (int32_t)(dim.w - dim.ml - dim.mr) << 6, |
| 122 | + }; |
| 123 | + |
| 124 | + unsigned t = lrint(pts * 1000); |
| 125 | + |
| 126 | + struct sub_bitmaps *bitmaps = ctx->bitmaps; |
| 127 | + struct sub_bitmap *bitmap = bitmaps->parts; |
| 128 | + |
| 129 | + bool size_did_change = bitmap->w != dim.w || bitmap->h != dim.h; |
| 130 | + if (size_did_change || sbr_renderer_did_change(ctx->sbr_renderer, &context, t)) { |
| 131 | + if(bitmaps->packed) |
| 132 | + talloc_free(bitmaps->packed); |
| 133 | + |
| 134 | + bitmaps->packed = mp_image_alloc(IMGFMT_BGRA, dim.w, dim.h); |
| 135 | + bitmaps->packed_h = dim.h; |
| 136 | + bitmaps->packed_w = dim.w; |
| 137 | + bitmaps->packed->params.repr.alpha = PL_ALPHA_PREMULTIPLIED; |
| 138 | + ++bitmaps->change_id; |
| 139 | + |
| 140 | + bitmap->bitmap = bitmaps->packed->planes[0]; |
| 141 | + bitmap->w = dim.w; |
| 142 | + bitmap->h = dim.h; |
| 143 | + bitmap->dw = dim.w; |
| 144 | + bitmap->dh = dim.h; |
| 145 | + // HACK: subrandr doesn't support stride, probably could add that though |
| 146 | + bitmaps->packed->stride[0] = dim.w * 4; |
| 147 | + bitmap->stride = (*bitmaps->packed).stride[0]; |
| 148 | + |
| 149 | + sbr_renderer_render(ctx->sbr_renderer, &context, ctx->sbr_subtitles, t, bitmap->bitmap, dim.w, dim.h); |
| 150 | + } |
| 151 | + |
| 152 | + return sub_bitmaps_copy(NULL, bitmaps); |
| 153 | +} |
| 154 | + |
| 155 | +static void uninit(struct sd *sd) |
| 156 | +{ |
| 157 | + struct sd_sbr_priv *ctx = sd->priv; |
| 158 | + |
| 159 | + if(ctx->sbr_renderer) |
| 160 | + sbr_renderer_destroy(ctx->sbr_renderer); |
| 161 | + if(ctx->sbr_subtitles) |
| 162 | + sbr_subtitles_destroy(ctx->sbr_subtitles); |
| 163 | + sbr_library_fini(ctx->sbr_library); |
| 164 | +} |
| 165 | + |
| 166 | +static int control(struct sd *sd, enum sd_ctrl cmd, void *arg) |
| 167 | +{ |
| 168 | + struct sd_sbr_priv *ctx = sd->priv; |
| 169 | + switch (cmd) { |
| 170 | + case SD_CTRL_SET_VIDEO_PARAMS: |
| 171 | + ctx->video_params = *(struct mp_image_params *)arg; |
| 172 | + return CONTROL_OK; |
| 173 | + default: |
| 174 | + return CONTROL_UNKNOWN; |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +const struct sd_functions sd_sbr = { |
| 179 | + .name = "subrandr", |
| 180 | + .accept_packets_in_advance = true, |
| 181 | + .init = init, |
| 182 | + .decode = decode, |
| 183 | + .get_bitmaps = get_bitmaps, |
| 184 | + .control = control, |
| 185 | + .select = enable_output, |
| 186 | + .uninit = uninit, |
| 187 | +}; |
0 commit comments