ao_pulse: support native mute control
[mplayer.git] / sub / av_sub.c
blob3a9e1b4f26704ab26c8214fbd9d9f56e68d2ed1a
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 "mp_msg.h"
22 #include "libmpdemux/stheader.h"
23 #include "sub.h"
24 #include "spudec.h"
25 #include "av_sub.h"
27 void reset_avsub(struct sh_sub *sh)
29 if (sh->context) {
30 avcodec_close(sh->context);
31 av_freep(&sh->context);
35 /**
36 * Decode a subtitle packet via libavcodec.
37 * \return < 0 on error, > 0 if further processing is needed
39 int decode_avsub(struct sh_sub *sh, uint8_t *data, int size,
40 double pts, double duration)
42 AVCodecContext *ctx = sh->context;
43 enum CodecID cid = CODEC_ID_NONE;
44 int res;
45 int got_sub;
46 AVSubtitle sub;
47 AVPacket pkt;
49 switch (sh->type) {
50 case 'b':
51 cid = CODEC_ID_DVB_SUBTITLE; break;
52 case 'p':
53 cid = CODEC_ID_HDMV_PGS_SUBTITLE; break;
54 case 'x':
55 cid = CODEC_ID_XSUB; break;
58 av_init_packet(&pkt);
59 pkt.data = data;
60 pkt.size = size;
61 pkt.pts = pts * 1000;
62 if (duration >= 0)
63 pkt.convergence_duration = duration * 1000;
64 if (!ctx) {
65 AVCodec *sub_codec;
66 sub_codec = avcodec_find_decoder(cid);
67 if (!sub_codec)
68 goto error;
69 ctx = avcodec_alloc_context3(sub_codec);
70 if (!ctx)
71 goto error;
72 if (avcodec_open2(ctx, sub_codec, NULL) < 0) {
73 error:
74 mp_msg(MSGT_SUBREADER, MSGL_FATAL,
75 "Could not open subtitle decoder\n");
76 av_freep(&ctx);
77 return -1;
79 sh->context = ctx;
81 res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
82 if (res < 0)
83 return res;
84 if (pts != MP_NOPTS_VALUE) {
85 if (sub.end_display_time > sub.start_display_time)
86 duration = (sub.end_display_time - sub.start_display_time) / 1000.0;
87 pts += sub.start_display_time / 1000.0;
89 double endpts = MP_NOPTS_VALUE;
90 if (pts != MP_NOPTS_VALUE && duration >= 0)
91 endpts = pts + duration;
92 if (got_sub && vo_spudec && sub.num_rects == 0)
93 spudec_set_paletted(vo_spudec, NULL, 0, NULL, 0, 0, 0, 0, pts, endpts);
94 if (got_sub && sub.num_rects > 0) {
95 switch (sub.rects[0]->type) {
96 case SUBTITLE_BITMAP:
97 if (!vo_spudec)
98 vo_spudec = spudec_new_scaled(NULL, ctx->width, ctx->height, NULL, 0);
99 spudec_set_paletted(vo_spudec,
100 sub.rects[0]->pict.data[0],
101 sub.rects[0]->pict.linesize[0],
102 sub.rects[0]->pict.data[1],
103 sub.rects[0]->x,
104 sub.rects[0]->y,
105 sub.rects[0]->w,
106 sub.rects[0]->h,
107 pts,
108 endpts);
109 vo_osd_changed(OSDTYPE_SPU);
110 break;
111 default:
112 mp_msg(MSGT_SUBREADER, MSGL_ERR, "sd_avsub: unsupported subtitle "
113 "type from libavcodec\n");
114 res = -1;
115 break;
118 if (got_sub)
119 avsubtitle_free(&sub);
120 return res;