2010-07-10 13:45:09 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
|
2011-08-19 02:17:02 +00:00
|
|
|
#include "mp_msg.h"
|
2010-07-10 13:45:09 +00:00
|
|
|
#include "libmpdemux/stheader.h"
|
2011-01-26 17:40:52 +00:00
|
|
|
#include "sub.h"
|
2010-07-10 13:45:09 +00:00
|
|
|
#include "spudec.h"
|
|
|
|
#include "av_sub.h"
|
|
|
|
|
|
|
|
void reset_avsub(struct sh_sub *sh)
|
|
|
|
{
|
|
|
|
if (sh->context) {
|
|
|
|
avcodec_close(sh->context);
|
|
|
|
av_freep(&sh->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a subtitle packet via libavcodec.
|
|
|
|
* \return < 0 on error, > 0 if further processing is needed
|
|
|
|
*/
|
2011-01-14 12:05:22 +00:00
|
|
|
int decode_avsub(struct sh_sub *sh, uint8_t *data, int size,
|
2011-01-16 18:51:48 +00:00
|
|
|
double pts, double duration)
|
2010-07-10 13:45:09 +00:00
|
|
|
{
|
|
|
|
AVCodecContext *ctx = sh->context;
|
2010-07-11 09:40:46 +00:00
|
|
|
enum CodecID cid = CODEC_ID_NONE;
|
2010-07-10 13:45:09 +00:00
|
|
|
int res;
|
|
|
|
int got_sub;
|
|
|
|
AVSubtitle sub;
|
|
|
|
AVPacket pkt;
|
2010-07-11 09:40:46 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-10 13:45:09 +00:00
|
|
|
av_init_packet(&pkt);
|
2011-01-14 12:05:22 +00:00
|
|
|
pkt.data = data;
|
|
|
|
pkt.size = size;
|
|
|
|
pkt.pts = pts * 1000;
|
2011-01-16 18:51:48 +00:00
|
|
|
if (duration >= 0)
|
|
|
|
pkt.convergence_duration = duration * 1000;
|
2010-07-10 13:45:09 +00:00
|
|
|
if (!ctx) {
|
|
|
|
AVCodec *sub_codec;
|
|
|
|
ctx = avcodec_alloc_context();
|
2010-07-11 09:04:29 +00:00
|
|
|
sub_codec = avcodec_find_decoder(cid);
|
2010-07-10 13:45:09 +00:00
|
|
|
if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) {
|
2010-09-11 22:13:45 +00:00
|
|
|
mp_msg(MSGT_SUBREADER, MSGL_FATAL,
|
|
|
|
"Could not open subtitle decoder\n");
|
2010-07-10 13:45:09 +00:00
|
|
|
av_freep(&ctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sh->context = ctx;
|
|
|
|
}
|
|
|
|
res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
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;
|
2010-11-27 10:01:11 +00:00
|
|
|
if (got_sub && vo_spudec && sub.num_rects == 0)
|
2011-01-14 12:05:22 +00:00
|
|
|
spudec_set_paletted(vo_spudec, NULL, 0, NULL, 0, 0, 0, 0, pts, endpts);
|
2010-07-10 13:45:09 +00:00
|
|
|
if (got_sub && sub.num_rects > 0) {
|
|
|
|
switch (sub.rects[0]->type) {
|
|
|
|
case SUBTITLE_BITMAP:
|
|
|
|
if (!vo_spudec)
|
2010-08-21 11:54:04 +00:00
|
|
|
vo_spudec = spudec_new_scaled(NULL, ctx->width, ctx->height, NULL, 0);
|
2010-07-10 13:45:09 +00:00
|
|
|
spudec_set_paletted(vo_spudec,
|
|
|
|
sub.rects[0]->pict.data[0],
|
|
|
|
sub.rects[0]->pict.linesize[0],
|
|
|
|
sub.rects[0]->pict.data[1],
|
|
|
|
sub.rects[0]->x,
|
|
|
|
sub.rects[0]->y,
|
|
|
|
sub.rects[0]->w,
|
|
|
|
sub.rects[0]->h,
|
2011-01-14 12:05:22 +00:00
|
|
|
pts,
|
|
|
|
endpts);
|
2010-07-10 13:45:09 +00:00
|
|
|
vo_osd_changed(OSDTYPE_SPU);
|
|
|
|
break;
|
2011-01-14 12:05:22 +00:00
|
|
|
default:
|
|
|
|
mp_msg(MSGT_SUBREADER, MSGL_ERR, "sd_avsub: unsupported subtitle "
|
|
|
|
"type from libavcodec\n");
|
|
|
|
res = -1;
|
2010-07-10 13:45:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-11 07:37:02 +00:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 82, 0)
|
2010-07-10 13:45:09 +00:00
|
|
|
if (got_sub)
|
2010-07-11 07:37:02 +00:00
|
|
|
avsubtitle_free(&sub);
|
|
|
|
#endif
|
2011-01-14 12:05:22 +00:00
|
|
|
return res;
|
2010-07-10 13:45:09 +00:00
|
|
|
}
|