Mercurial > mplayer.hg
annotate av_sub.c @ 31630:78260a246393
10l, also assign the original width in spudec_new_scaled.
author | reimar |
---|---|
date | Sun, 11 Jul 2010 09:39:06 +0000 |
parents | f15df2e3081b |
children | 67f2fb3ff4c7 |
rev | line source |
---|---|
31599 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
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. | |
8 * | |
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. | |
13 * | |
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. | |
17 */ | |
18 | |
19 #include "libavcodec/avcodec.h" | |
20 #include "libmpdemux/stheader.h" | |
21 #include "libvo/sub.h" | |
22 #include "spudec.h" | |
23 #include "av_sub.h" | |
24 | |
25 void reset_avsub(struct sh_sub *sh) | |
26 { | |
27 if (sh->context) { | |
28 avcodec_close(sh->context); | |
29 av_freep(&sh->context); | |
30 } | |
31 } | |
32 | |
33 /** | |
34 * Decode a subtitle packet via libavcodec. | |
35 * \return < 0 on error, > 0 if further processing is needed | |
36 */ | |
37 int decode_avsub(struct sh_sub *sh, uint8_t **data, int *size, double *pts, double *endpts) | |
38 { | |
39 AVCodecContext *ctx = sh->context; | |
40 int new_type = 0; | |
41 int res; | |
42 int got_sub; | |
43 AVSubtitle sub; | |
44 AVPacket pkt; | |
45 av_init_packet(&pkt); | |
46 pkt.data = *data; | |
47 pkt.size = *size; | |
48 pkt.pts = *pts * 1000; | |
49 if (*pts != MP_NOPTS_VALUE && *endpts != MP_NOPTS_VALUE) | |
50 pkt.convergence_duration = (*endpts - *pts) * 1000; | |
51 if (!ctx) { | |
31628
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
52 enum CodecID cid = CODEC_ID_NONE; |
31599 | 53 AVCodec *sub_codec; |
54 avcodec_init(); | |
55 avcodec_register_all(); | |
56 ctx = avcodec_alloc_context(); | |
31628
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
57 switch (sh->type) { |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
58 case 'b': |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
59 cid = CODEC_ID_DVB_SUBTITLE; break; |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
60 case 'p': |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
61 cid = CODEC_ID_HDMV_PGS_SUBTITLE; break; |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
62 case 'x': |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
63 cid = CODEC_ID_XSUB; break; |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
64 } |
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
65 sub_codec = avcodec_find_decoder(cid); |
31599 | 66 if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) { |
67 mp_msg(MSGT_SUBREADER, MSGL_FATAL, "Could not open subtitle decoder\n"); | |
68 av_freep(&ctx); | |
69 return -1; | |
70 } | |
71 sh->context = ctx; | |
72 } | |
73 res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt); | |
74 if (res < 0) | |
75 return res; | |
76 if (*pts != MP_NOPTS_VALUE) { | |
77 if (sub.end_display_time > sub.start_display_time) | |
78 *endpts = *pts + sub.end_display_time / 1000.0; | |
79 *pts += sub.start_display_time / 1000.0; | |
80 } | |
81 if (got_sub && sub.num_rects > 0) { | |
82 switch (sub.rects[0]->type) { | |
83 case SUBTITLE_BITMAP: | |
84 if (!vo_spudec) | |
85 vo_spudec = spudec_new(NULL); | |
86 spudec_set_paletted(vo_spudec, | |
87 sub.rects[0]->pict.data[0], | |
88 sub.rects[0]->pict.linesize[0], | |
89 sub.rects[0]->pict.data[1], | |
90 sub.rects[0]->x, | |
91 sub.rects[0]->y, | |
92 sub.rects[0]->w, | |
93 sub.rects[0]->h, | |
94 *pts, | |
95 *endpts); | |
96 vo_osd_changed(OSDTYPE_SPU); | |
97 break; | |
98 case SUBTITLE_TEXT: | |
99 *data = strdup(sub.rects[0]->text); | |
100 new_type = 't'; | |
101 break; | |
102 case SUBTITLE_ASS: | |
103 *data = strdup(sub.rects[0]->ass); | |
104 new_type = 'a'; | |
105 break; | |
106 } | |
107 } | |
108 if (got_sub) | |
31624
40c30c70ead0
Fix memory leak for subtitles decoded by libavcodec.
reimar
parents:
31599
diff
changeset
|
109 avsubtitle_free(&sub); |
31599 | 110 return new_type; |
111 } |