subtitles/demux: store duration instead of endpts in demux packets
[mplayer/glamo.git] / av_sub.c
blob49e5d5baafec1787e5c723494ffa9b731bb89e0f
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer 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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <libavcodec/avcodec.h>
21 #include "libmpdemux/stheader.h"
22 #include "libvo/sub.h"
23 #include "spudec.h"
24 #include "av_sub.h"
26 void reset_avsub(struct sh_sub *sh)
28 if (sh->context) {
29 avcodec_close(sh->context);
30 av_freep(&sh->context);
34 /**
35 * Decode a subtitle packet via libavcodec.
36 * \return < 0 on error, > 0 if further processing is needed
38 int decode_avsub(struct sh_sub *sh, uint8_t *data, int size,
39 double pts, double duration)
41 AVCodecContext *ctx = sh->context;
42 enum CodecID cid = CODEC_ID_NONE;
43 int res;
44 int got_sub;
45 AVSubtitle sub;
46 AVPacket pkt;
48 switch (sh->type) {
49 case 'b':
50 cid = CODEC_ID_DVB_SUBTITLE; break;
51 case 'p':
52 cid = CODEC_ID_HDMV_PGS_SUBTITLE; break;
53 case 'x':
54 cid = CODEC_ID_XSUB; break;
57 av_init_packet(&pkt);
58 pkt.data = data;
59 pkt.size = size;
60 pkt.pts = pts * 1000;
61 if (duration >= 0)
62 pkt.convergence_duration = duration * 1000;
63 if (!ctx) {
64 AVCodec *sub_codec;
65 avcodec_init();
66 avcodec_register_all();
67 ctx = avcodec_alloc_context();
68 sub_codec = avcodec_find_decoder(cid);
69 if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) {
70 mp_msg(MSGT_SUBREADER, MSGL_FATAL,
71 "Could not open subtitle decoder\n");
72 av_freep(&ctx);
73 return -1;
75 sh->context = ctx;
77 res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
78 if (res < 0)
79 return res;
80 if (pts != MP_NOPTS_VALUE) {
81 if (sub.end_display_time > sub.start_display_time)
82 duration = (sub.end_display_time - sub.start_display_time) / 1000.0;
83 pts += sub.start_display_time / 1000.0;
85 double endpts = MP_NOPTS_VALUE;
86 if (pts != MP_NOPTS_VALUE && duration >= 0)
87 endpts = pts + duration;
88 if (got_sub && vo_spudec && sub.num_rects == 0)
89 spudec_set_paletted(vo_spudec, NULL, 0, NULL, 0, 0, 0, 0, pts, endpts);
90 if (got_sub && sub.num_rects > 0) {
91 switch (sub.rects[0]->type) {
92 case SUBTITLE_BITMAP:
93 if (!vo_spudec)
94 vo_spudec = spudec_new_scaled(NULL, ctx->width, ctx->height, NULL, 0);
95 spudec_set_paletted(vo_spudec,
96 sub.rects[0]->pict.data[0],
97 sub.rects[0]->pict.linesize[0],
98 sub.rects[0]->pict.data[1],
99 sub.rects[0]->x,
100 sub.rects[0]->y,
101 sub.rects[0]->w,
102 sub.rects[0]->h,
103 pts,
104 endpts);
105 vo_osd_changed(OSDTYPE_SPU);
106 break;
107 default:
108 mp_msg(MSGT_SUBREADER, MSGL_ERR, "sd_avsub: unsupported subtitle "
109 "type from libavcodec\n");
110 res = -1;
111 break;
114 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 82, 0)
115 if (got_sub)
116 avsubtitle_free(&sub);
117 #endif
118 return res;