av_sub: Fix memory leak for subtitles decoded by libavcodec
[mplayer/glamo.git] / av_sub.c
blob9a7c832594f1e1f18444b6713b31a0471cd84541
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, double *pts, double *endpts)
40 AVCodecContext *ctx = sh->context;
41 int new_type = 0;
42 int res;
43 int got_sub;
44 AVSubtitle sub;
45 AVPacket pkt;
46 av_init_packet(&pkt);
47 pkt.data = *data;
48 pkt.size = *size;
49 pkt.pts = *pts * 1000;
50 if (*pts != MP_NOPTS_VALUE && *endpts != MP_NOPTS_VALUE)
51 pkt.convergence_duration = (*endpts - *pts) * 1000;
52 if (!ctx) {
53 AVCodec *sub_codec;
54 avcodec_init();
55 avcodec_register_all();
56 ctx = avcodec_alloc_context();
57 sub_codec = avcodec_find_decoder(CODEC_ID_HDMV_PGS_SUBTITLE);
58 if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) {
59 mp_msg(MSGT_SUBREADER, MSGL_FATAL, "Could not open subtitle decoder\n");
60 av_freep(&ctx);
61 return -1;
63 sh->context = ctx;
65 res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
66 if (res < 0)
67 return res;
68 if (*pts != MP_NOPTS_VALUE) {
69 if (sub.end_display_time > sub.start_display_time)
70 *endpts = *pts + sub.end_display_time / 1000.0;
71 *pts += sub.start_display_time / 1000.0;
73 if (got_sub && sub.num_rects > 0) {
74 switch (sub.rects[0]->type) {
75 case SUBTITLE_BITMAP:
76 if (!vo_spudec)
77 vo_spudec = spudec_new(NULL);
78 spudec_set_paletted(vo_spudec,
79 sub.rects[0]->pict.data[0],
80 sub.rects[0]->pict.linesize[0],
81 sub.rects[0]->pict.data[1],
82 sub.rects[0]->x,
83 sub.rects[0]->y,
84 sub.rects[0]->w,
85 sub.rects[0]->h,
86 *pts,
87 *endpts);
88 vo_osd_changed(OSDTYPE_SPU);
89 break;
90 case SUBTITLE_TEXT:
91 *data = strdup(sub.rects[0]->text);
92 new_type = 't';
93 break;
94 case SUBTITLE_ASS:
95 *data = strdup(sub.rects[0]->ass);
96 new_type = 'a';
97 break;
100 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 82, 0)
101 if (got_sub)
102 avsubtitle_free(&sub);
103 #endif
104 return new_type;