2010-07-10 13:45:09 +00:00
|
|
|
/*
|
2012-08-16 15:21:21 +00:00
|
|
|
* This file is part of mplayer2.
|
2010-07-10 13:45:09 +00:00
|
|
|
*
|
2012-08-16 15:21:21 +00:00
|
|
|
* mplayer2 is free software; you can redistribute it and/or modify
|
2010-07-10 13:45:09 +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.
|
|
|
|
*
|
2012-08-16 15:21:21 +00:00
|
|
|
* mplayer2 is distributed in the hope that it will be useful,
|
2010-07-10 13:45:09 +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
|
2012-08-16 15:21:21 +00:00
|
|
|
* with mplayer2. If not, see <http://www.gnu.org/licenses/>.
|
2010-07-10 13:45:09 +00:00
|
|
|
*/
|
|
|
|
|
2012-08-16 15:21:21 +00:00
|
|
|
#include <stdlib.h>
|
2012-10-04 15:16:47 +00:00
|
|
|
#include <assert.h>
|
2012-08-16 15:21:21 +00:00
|
|
|
|
2010-07-10 13:45:09 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
|
2012-08-31 12:42:30 +00:00
|
|
|
#include "talloc.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/mp_msg.h"
|
|
|
|
#include "demux/stheader.h"
|
2012-08-16 15:21:21 +00:00
|
|
|
#include "sd.h"
|
2012-10-04 15:16:40 +00:00
|
|
|
#include "dec_sub.h"
|
2012-08-16 15:21:21 +00:00
|
|
|
#include "sub.h"
|
2010-07-10 13:45:09 +00:00
|
|
|
|
2012-08-31 12:42:30 +00:00
|
|
|
struct sd_lavc_priv {
|
|
|
|
AVCodecContext *avctx;
|
2012-10-04 15:16:47 +00:00
|
|
|
AVSubtitle sub;
|
|
|
|
bool have_sub;
|
2012-08-31 12:42:30 +00:00
|
|
|
int count;
|
|
|
|
struct sub_bitmap *inbitmaps;
|
|
|
|
struct sub_bitmap *outbitmaps;
|
2012-10-04 15:16:47 +00:00
|
|
|
struct osd_bmp_indexed *imgs;
|
2012-08-31 12:42:30 +00:00
|
|
|
bool bitmaps_changed;
|
2012-12-12 21:56:41 +00:00
|
|
|
double pts;
|
2012-08-31 12:42:30 +00:00
|
|
|
double endpts;
|
|
|
|
};
|
|
|
|
|
2012-10-02 21:28:18 +00:00
|
|
|
static void guess_resolution(char type, int *w, int *h)
|
|
|
|
{
|
|
|
|
if (type == 'v') {
|
|
|
|
/* XXX Although the video frame is some size, the SPU frame is
|
|
|
|
always maximum size i.e. 720 wide and 576 or 480 high */
|
|
|
|
// For HD files in MKV the VobSub resolution can be higher though,
|
|
|
|
// see largeres_vobsub.mkv
|
|
|
|
if (*w <= 720 && *h <= 576) {
|
|
|
|
*w = 720;
|
|
|
|
*h = (*h == 480 || *h == 240) ? 480 : 576;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Hope that PGS subs set these and 720/576 works for dvb subs
|
|
|
|
if (!*w)
|
|
|
|
*w = 720;
|
|
|
|
if (!*h)
|
|
|
|
*h = 576;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 15:21:21 +00:00
|
|
|
static int init(struct sh_sub *sh, struct osd_state *osd)
|
2010-07-10 13:45:09 +00:00
|
|
|
{
|
2012-08-31 12:42:30 +00:00
|
|
|
if (sh->initialized)
|
|
|
|
return 0;
|
|
|
|
struct sd_lavc_priv *priv = talloc_zero(NULL, struct sd_lavc_priv);
|
2010-07-11 09:40:46 +00:00
|
|
|
enum CodecID cid = CODEC_ID_NONE;
|
|
|
|
switch (sh->type) {
|
|
|
|
case 'b':
|
|
|
|
cid = CODEC_ID_DVB_SUBTITLE; break;
|
|
|
|
case 'p':
|
|
|
|
cid = CODEC_ID_HDMV_PGS_SUBTITLE; break;
|
|
|
|
case 'x':
|
|
|
|
cid = CODEC_ID_XSUB; break;
|
2012-08-31 12:42:30 +00:00
|
|
|
case 'v':
|
|
|
|
cid = CODEC_ID_DVD_SUBTITLE; break;
|
2010-07-11 09:40:46 +00:00
|
|
|
}
|
2012-08-16 15:21:21 +00:00
|
|
|
AVCodecContext *ctx = NULL;
|
|
|
|
AVCodec *sub_codec = avcodec_find_decoder(cid);
|
|
|
|
if (!sub_codec)
|
|
|
|
goto error;
|
|
|
|
ctx = avcodec_alloc_context3(sub_codec);
|
|
|
|
if (!ctx)
|
|
|
|
goto error;
|
2012-08-31 12:42:30 +00:00
|
|
|
ctx->extradata_size = sh->extradata_len;
|
|
|
|
ctx->extradata = sh->extradata;
|
2012-08-16 15:21:21 +00:00
|
|
|
if (avcodec_open2(ctx, sub_codec, NULL) < 0)
|
|
|
|
goto error;
|
2012-08-31 12:42:30 +00:00
|
|
|
priv->avctx = ctx;
|
|
|
|
sh->context = priv;
|
2012-08-16 15:21:21 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
mp_msg(MSGT_SUBREADER, MSGL_ERR,
|
|
|
|
"Could not open libavcodec subtitle decoder\n");
|
|
|
|
av_free(ctx);
|
2012-08-31 12:42:30 +00:00
|
|
|
talloc_free(priv);
|
2012-08-16 15:21:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-31 12:42:30 +00:00
|
|
|
static void clear(struct sd_lavc_priv *priv)
|
|
|
|
{
|
|
|
|
priv->count = 0;
|
|
|
|
talloc_free(priv->inbitmaps);
|
|
|
|
talloc_free(priv->outbitmaps);
|
|
|
|
priv->inbitmaps = priv->outbitmaps = NULL;
|
2012-10-04 15:16:47 +00:00
|
|
|
talloc_free(priv->imgs);
|
|
|
|
priv->imgs = NULL;
|
2012-08-31 12:42:30 +00:00
|
|
|
priv->bitmaps_changed = true;
|
2012-12-12 21:56:41 +00:00
|
|
|
priv->pts = MP_NOPTS_VALUE;
|
2012-08-31 12:42:30 +00:00
|
|
|
priv->endpts = MP_NOPTS_VALUE;
|
2012-10-04 15:16:47 +00:00
|
|
|
if (priv->have_sub)
|
|
|
|
avsubtitle_free(&priv->sub);
|
|
|
|
priv->have_sub = false;
|
2012-08-31 12:42:30 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 15:21:21 +00:00
|
|
|
static void decode(struct sh_sub *sh, struct osd_state *osd, void *data,
|
|
|
|
int data_len, double pts, double duration)
|
|
|
|
{
|
2012-08-31 12:42:30 +00:00
|
|
|
struct sd_lavc_priv *priv = sh->context;
|
|
|
|
AVCodecContext *ctx = priv->avctx;
|
2012-08-16 15:21:21 +00:00
|
|
|
AVSubtitle sub;
|
|
|
|
AVPacket pkt;
|
2010-07-11 09:40:46 +00:00
|
|
|
|
2012-08-31 12:42:30 +00:00
|
|
|
clear(priv);
|
2010-07-10 13:45:09 +00:00
|
|
|
av_init_packet(&pkt);
|
2011-01-14 12:05:22 +00:00
|
|
|
pkt.data = data;
|
2012-08-16 15:21:21 +00:00
|
|
|
pkt.size = data_len;
|
2011-01-14 12:05:22 +00:00
|
|
|
pkt.pts = pts * 1000;
|
2011-01-16 18:51:48 +00:00
|
|
|
if (duration >= 0)
|
|
|
|
pkt.convergence_duration = duration * 1000;
|
2012-08-16 15:21:21 +00:00
|
|
|
int got_sub;
|
|
|
|
int res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
|
|
|
|
if (res < 0 || !got_sub)
|
|
|
|
return;
|
2012-10-04 15:16:47 +00:00
|
|
|
priv->sub = sub;
|
|
|
|
priv->have_sub = true;
|
2011-01-14 12:05:22 +00:00
|
|
|
if (pts != MP_NOPTS_VALUE) {
|
2010-07-10 13:45:09 +00:00
|
|
|
if (sub.end_display_time > sub.start_display_time)
|
2011-01-16 18:51:48 +00:00
|
|
|
duration = (sub.end_display_time - sub.start_display_time) / 1000.0;
|
2011-01-14 12:05:22 +00:00
|
|
|
pts += sub.start_display_time / 1000.0;
|
2010-07-10 13:45:09 +00:00
|
|
|
}
|
2011-01-16 18:51:48 +00:00
|
|
|
double endpts = MP_NOPTS_VALUE;
|
|
|
|
if (pts != MP_NOPTS_VALUE && duration >= 0)
|
|
|
|
endpts = pts + duration;
|
2012-08-16 15:21:21 +00:00
|
|
|
if (sub.num_rects > 0) {
|
2010-07-10 13:45:09 +00:00
|
|
|
switch (sub.rects[0]->type) {
|
|
|
|
case SUBTITLE_BITMAP:
|
2012-08-31 12:42:30 +00:00
|
|
|
priv->inbitmaps = talloc_array(priv, struct sub_bitmap,
|
|
|
|
sub.num_rects);
|
2012-10-04 15:16:47 +00:00
|
|
|
priv->imgs = talloc_array(priv, struct osd_bmp_indexed,
|
|
|
|
sub.num_rects);
|
2012-08-31 12:42:30 +00:00
|
|
|
for (int i = 0; i < sub.num_rects; i++) {
|
|
|
|
struct AVSubtitleRect *r = sub.rects[i];
|
|
|
|
struct sub_bitmap *b = &priv->inbitmaps[i];
|
2012-10-04 15:16:47 +00:00
|
|
|
struct osd_bmp_indexed *img = &priv->imgs[i];
|
|
|
|
img->bitmap = r->pict.data[0];
|
|
|
|
assert(r->nb_colors > 0);
|
|
|
|
assert(r->nb_colors * 4 <= sizeof(img->palette));
|
|
|
|
memcpy(img->palette, r->pict.data[1], r->nb_colors * 4);
|
|
|
|
b->bitmap = img;
|
|
|
|
b->stride = r->pict.linesize[0];
|
2012-08-31 12:42:30 +00:00
|
|
|
b->w = r->w;
|
|
|
|
b->h = r->h;
|
|
|
|
b->x = r->x;
|
|
|
|
b->y = r->y;
|
|
|
|
}
|
|
|
|
priv->count = sub.num_rects;
|
2012-12-12 21:56:41 +00:00
|
|
|
priv->pts = pts;
|
2012-08-31 12:42:30 +00:00
|
|
|
priv->endpts = endpts;
|
2010-07-10 13:45:09 +00:00
|
|
|
break;
|
2011-01-14 12:05:22 +00:00
|
|
|
default:
|
2012-08-16 15:21:21 +00:00
|
|
|
mp_msg(MSGT_SUBREADER, MSGL_ERR, "sd_lavc: unsupported subtitle "
|
2011-01-14 12:05:22 +00:00
|
|
|
"type from libavcodec\n");
|
2010-07-10 13:45:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-16 15:21:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 12:42:30 +00:00
|
|
|
static void get_bitmaps(struct sh_sub *sh, struct osd_state *osd,
|
VO, sub: refactor
Remove VFCTRL_DRAW_OSD, VFCAP_EOSD_FILTER, VFCAP_EOSD_RGBA, VFCAP_EOSD,
VOCTRL_DRAW_EOSD, VOCTRL_GET_EOSD_RES, VOCTRL_QUERY_EOSD_FORMAT.
Remove draw_osd_with_eosd(), which rendered the OSD by calling
VOCTRL_DRAW_EOSD. Change VOs to call osd_draw() directly, which takes
a callback as argument. (This basically works like the old OSD API,
except multiple OSD bitmap formats are supported and caching is
possible.)
Remove all mentions of "eosd". It's simply "osd" now.
Make OSD size per-OSD-object, as they can be different when using
vf_sub. Include display_par/video_par in resolution change detection.
Fix the issue with margin borders in vo_corevideo.
2012-10-19 17:25:18 +00:00
|
|
|
struct mp_osd_res d, double pts,
|
2012-08-31 12:42:30 +00:00
|
|
|
struct sub_bitmaps *res)
|
|
|
|
{
|
|
|
|
struct sd_lavc_priv *priv = sh->context;
|
|
|
|
|
2012-12-12 21:56:41 +00:00
|
|
|
if (priv->pts != MP_NOPTS_VALUE && pts < priv->pts)
|
|
|
|
return;
|
VO, sub: refactor
Remove VFCTRL_DRAW_OSD, VFCAP_EOSD_FILTER, VFCAP_EOSD_RGBA, VFCAP_EOSD,
VOCTRL_DRAW_EOSD, VOCTRL_GET_EOSD_RES, VOCTRL_QUERY_EOSD_FORMAT.
Remove draw_osd_with_eosd(), which rendered the OSD by calling
VOCTRL_DRAW_EOSD. Change VOs to call osd_draw() directly, which takes
a callback as argument. (This basically works like the old OSD API,
except multiple OSD bitmap formats are supported and caching is
possible.)
Remove all mentions of "eosd". It's simply "osd" now.
Make OSD size per-OSD-object, as they can be different when using
vf_sub. Include display_par/video_par in resolution change detection.
Fix the issue with margin borders in vo_corevideo.
2012-10-19 17:25:18 +00:00
|
|
|
if (priv->endpts != MP_NOPTS_VALUE && (pts >= priv->endpts ||
|
|
|
|
pts < priv->endpts - 300))
|
2012-08-31 12:42:30 +00:00
|
|
|
clear(priv);
|
|
|
|
if (priv->bitmaps_changed && priv->count > 0)
|
|
|
|
priv->outbitmaps = talloc_memdup(priv, priv->inbitmaps,
|
|
|
|
talloc_get_size(priv->inbitmaps));
|
|
|
|
int inw = priv->avctx->width;
|
|
|
|
int inh = priv->avctx->height;
|
2012-10-02 21:28:18 +00:00
|
|
|
guess_resolution(sh->type, &inw, &inh);
|
VO, sub: refactor
Remove VFCTRL_DRAW_OSD, VFCAP_EOSD_FILTER, VFCAP_EOSD_RGBA, VFCAP_EOSD,
VOCTRL_DRAW_EOSD, VOCTRL_GET_EOSD_RES, VOCTRL_QUERY_EOSD_FORMAT.
Remove draw_osd_with_eosd(), which rendered the OSD by calling
VOCTRL_DRAW_EOSD. Change VOs to call osd_draw() directly, which takes
a callback as argument. (This basically works like the old OSD API,
except multiple OSD bitmap formats are supported and caching is
possible.)
Remove all mentions of "eosd". It's simply "osd" now.
Make OSD size per-OSD-object, as they can be different when using
vf_sub. Include display_par/video_par in resolution change detection.
Fix the issue with margin borders in vo_corevideo.
2012-10-19 17:25:18 +00:00
|
|
|
double xscale = (double) (d.w - d.ml - d.mr) / inw;
|
|
|
|
double yscale = (double) (d.h - d.mt - d.mb) / inh;
|
2012-08-31 12:42:30 +00:00
|
|
|
for (int i = 0; i < priv->count; i++) {
|
|
|
|
struct sub_bitmap *bi = &priv->inbitmaps[i];
|
|
|
|
struct sub_bitmap *bo = &priv->outbitmaps[i];
|
VO, sub: refactor
Remove VFCTRL_DRAW_OSD, VFCAP_EOSD_FILTER, VFCAP_EOSD_RGBA, VFCAP_EOSD,
VOCTRL_DRAW_EOSD, VOCTRL_GET_EOSD_RES, VOCTRL_QUERY_EOSD_FORMAT.
Remove draw_osd_with_eosd(), which rendered the OSD by calling
VOCTRL_DRAW_EOSD. Change VOs to call osd_draw() directly, which takes
a callback as argument. (This basically works like the old OSD API,
except multiple OSD bitmap formats are supported and caching is
possible.)
Remove all mentions of "eosd". It's simply "osd" now.
Make OSD size per-OSD-object, as they can be different when using
vf_sub. Include display_par/video_par in resolution change detection.
Fix the issue with margin borders in vo_corevideo.
2012-10-19 17:25:18 +00:00
|
|
|
bo->x = bi->x * xscale + d.ml;
|
|
|
|
bo->y = bi->y * yscale + d.mt;
|
2012-10-16 05:26:45 +00:00
|
|
|
bo->dw = bi->w * xscale;
|
|
|
|
bo->dh = bi->h * yscale;
|
2012-08-31 12:42:30 +00:00
|
|
|
}
|
|
|
|
res->parts = priv->outbitmaps;
|
2012-09-28 19:19:36 +00:00
|
|
|
res->num_parts = priv->count;
|
2012-08-31 12:42:30 +00:00
|
|
|
if (priv->bitmaps_changed)
|
|
|
|
res->bitmap_id = ++res->bitmap_pos_id;
|
|
|
|
priv->bitmaps_changed = false;
|
2012-10-04 15:16:47 +00:00
|
|
|
res->format = SUBBITMAP_INDEXED;
|
2012-08-31 12:42:30 +00:00
|
|
|
res->scaled = xscale != 1 || yscale != 1;
|
|
|
|
}
|
|
|
|
|
2012-08-16 15:21:21 +00:00
|
|
|
static void reset(struct sh_sub *sh, struct osd_state *osd)
|
|
|
|
{
|
2012-08-31 12:42:30 +00:00
|
|
|
struct sd_lavc_priv *priv = sh->context;
|
|
|
|
|
2012-12-12 21:56:41 +00:00
|
|
|
if (priv->pts == MP_NOPTS_VALUE)
|
|
|
|
clear(priv);
|
2012-08-16 15:21:21 +00:00
|
|
|
// lavc might not do this right for all codecs; may need close+reopen
|
2012-08-31 12:42:30 +00:00
|
|
|
avcodec_flush_buffers(priv->avctx);
|
2010-07-10 13:45:09 +00:00
|
|
|
}
|
2012-08-16 15:21:21 +00:00
|
|
|
|
|
|
|
static void uninit(struct sh_sub *sh)
|
|
|
|
{
|
2012-08-31 12:42:30 +00:00
|
|
|
struct sd_lavc_priv *priv = sh->context;
|
|
|
|
|
2012-10-04 15:16:47 +00:00
|
|
|
clear(priv);
|
2012-08-31 12:42:30 +00:00
|
|
|
avcodec_close(priv->avctx);
|
|
|
|
av_free(priv->avctx);
|
|
|
|
talloc_free(priv);
|
2012-08-16 15:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct sd_functions sd_lavc = {
|
|
|
|
.init = init,
|
|
|
|
.decode = decode,
|
2012-08-31 12:42:30 +00:00
|
|
|
.get_bitmaps = get_bitmaps,
|
2012-08-16 15:21:21 +00:00
|
|
|
.reset = reset,
|
|
|
|
.switch_off = reset,
|
|
|
|
.uninit = uninit,
|
|
|
|
};
|