Mercurial > mplayer.hg
annotate sub/av_sub.c @ 34845:f56b927a9e3e
SDL OpenGL: add support for swapinterval option.
author | reimar |
---|---|
date | Sun, 20 May 2012 17:57:39 +0000 |
parents | d180a74b1a89 |
children | 97470e8b352f |
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" | |
34174
a93891202051
Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents:
32564
diff
changeset
|
21 #include "mp_msg.h" |
32467 | 22 #include "sub.h" |
32464
22888a8cb312
Do not use a path for including files in the same directory.
reimar
parents:
32457
diff
changeset
|
23 #include "spudec.h" |
34528 | 24 #include "av_helpers.h" |
32464
22888a8cb312
Do not use a path for including files in the same directory.
reimar
parents:
32457
diff
changeset
|
25 #include "av_sub.h" |
31599 | 26 |
27 void reset_avsub(struct sh_sub *sh) | |
28 { | |
29 if (sh->context) { | |
30 avcodec_close(sh->context); | |
31 av_freep(&sh->context); | |
32 } | |
33 } | |
34 | |
34784 | 35 static void avsub_to_spudec(AVSubtitleRect **rects, int num_rects, |
36 double pts, double endpts) | |
37 { | |
38 int i, xmin = INT_MAX, ymin = INT_MAX, xmax = INT_MIN, ymax = INT_MIN; | |
39 struct spu_packet_t *packet; | |
40 | |
41 if (num_rects == 1) { | |
42 spudec_set_paletted(vo_spudec, | |
43 rects[0]->pict.data[0], | |
44 rects[0]->pict.linesize[0], | |
45 rects[0]->pict.data[1], | |
46 rects[0]->x, | |
47 rects[0]->y, | |
48 rects[0]->w, | |
49 rects[0]->h, | |
50 pts, | |
51 endpts); | |
52 return; | |
53 } | |
54 for (i = 0; i < num_rects; i++) { | |
55 xmin = FFMIN(xmin, rects[i]->x); | |
56 ymin = FFMIN(ymin, rects[i]->y); | |
57 xmax = FFMAX(xmax, rects[i]->x + rects[i]->w); | |
58 ymax = FFMAX(ymax, rects[i]->y + rects[i]->h); | |
59 } | |
60 packet = spudec_packet_create(xmin, ymin, xmax - xmin, ymax - ymin); | |
61 if (!packet) | |
62 return; | |
63 spudec_packet_clear(packet); | |
64 for (i = 0; i < num_rects; i++) | |
65 spudec_packet_fill(packet, | |
66 rects[i]->pict.data[0], | |
67 rects[i]->pict.linesize[0], | |
68 rects[i]->pict.data[1], | |
69 rects[i]->x - xmin, | |
70 rects[i]->y - ymin, | |
71 rects[i]->w, | |
72 rects[i]->h); | |
73 spudec_packet_send(vo_spudec, packet, pts, endpts); | |
74 } | |
75 | |
31599 | 76 /** |
77 * Decode a subtitle packet via libavcodec. | |
78 * \return < 0 on error, > 0 if further processing is needed | |
79 */ | |
32080 | 80 int decode_avsub(struct sh_sub *sh, uint8_t **data, int *size, |
81 double *pts, double *endpts) | |
31599 | 82 { |
83 AVCodecContext *ctx = sh->context; | |
31631
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
84 enum CodecID cid = CODEC_ID_NONE; |
31599 | 85 int new_type = 0; |
86 int res; | |
87 int got_sub; | |
88 AVSubtitle sub; | |
89 AVPacket pkt; | |
31631
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
90 |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
91 switch (sh->type) { |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
92 case 'b': |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
93 cid = CODEC_ID_DVB_SUBTITLE; break; |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
94 case 'p': |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
95 cid = CODEC_ID_HDMV_PGS_SUBTITLE; break; |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
96 case 'x': |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
97 cid = CODEC_ID_XSUB; break; |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
98 } |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
99 |
31599 | 100 av_init_packet(&pkt); |
101 pkt.data = *data; | |
102 pkt.size = *size; | |
103 pkt.pts = *pts * 1000; | |
104 if (*pts != MP_NOPTS_VALUE && *endpts != MP_NOPTS_VALUE) | |
105 pkt.convergence_duration = (*endpts - *pts) * 1000; | |
106 if (!ctx) { | |
107 AVCodec *sub_codec; | |
34528 | 108 init_avcodec(); |
34566
f3d53cd55376
Update deprecated avcodec_alloc_context()/avcodec_open() API calls
siretart
parents:
34528
diff
changeset
|
109 ctx = avcodec_alloc_context3(NULL); |
31628
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
110 sub_codec = avcodec_find_decoder(cid); |
34566
f3d53cd55376
Update deprecated avcodec_alloc_context()/avcodec_open() API calls
siretart
parents:
34528
diff
changeset
|
111 if (!ctx || !sub_codec || avcodec_open2(ctx, sub_codec, NULL) < 0) { |
32080 | 112 mp_msg(MSGT_SUBREADER, MSGL_FATAL, |
113 "Could not open subtitle decoder\n"); | |
31599 | 114 av_freep(&ctx); |
115 return -1; | |
116 } | |
117 sh->context = ctx; | |
118 } | |
119 res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt); | |
120 if (res < 0) | |
121 return res; | |
122 if (*pts != MP_NOPTS_VALUE) { | |
123 if (sub.end_display_time > sub.start_display_time) | |
124 *endpts = *pts + sub.end_display_time / 1000.0; | |
125 *pts += sub.start_display_time / 1000.0; | |
126 } | |
32564
61ac00c3c51c
Support clearing subtitles. Makes PGS subtitles disappear at the appropriate time.
reimar
parents:
32467
diff
changeset
|
127 if (got_sub && vo_spudec && sub.num_rects == 0) |
61ac00c3c51c
Support clearing subtitles. Makes PGS subtitles disappear at the appropriate time.
reimar
parents:
32467
diff
changeset
|
128 spudec_set_paletted(vo_spudec, NULL, 0, NULL, 0, 0, 0, 0, *pts, *endpts); |
31599 | 129 if (got_sub && sub.num_rects > 0) { |
130 switch (sub.rects[0]->type) { | |
131 case SUBTITLE_BITMAP: | |
132 if (!vo_spudec) | |
31924
8d7f15885b64
Pass the video dimensions specified in the subtitle to spudec.
reimar
parents:
31642
diff
changeset
|
133 vo_spudec = spudec_new_scaled(NULL, ctx->width, ctx->height, NULL, 0); |
34784 | 134 avsub_to_spudec(sub.rects, sub.num_rects, *pts, *endpts); |
31599 | 135 vo_osd_changed(OSDTYPE_SPU); |
136 break; | |
137 case SUBTITLE_TEXT: | |
138 *data = strdup(sub.rects[0]->text); | |
31642 | 139 *size = strlen(*data); |
31599 | 140 new_type = 't'; |
141 break; | |
142 case SUBTITLE_ASS: | |
143 *data = strdup(sub.rects[0]->ass); | |
31642 | 144 *size = strlen(*data); |
31599 | 145 new_type = 'a'; |
146 break; | |
147 } | |
148 } | |
149 if (got_sub) | |
31624
40c30c70ead0
Fix memory leak for subtitles decoded by libavcodec.
reimar
parents:
31599
diff
changeset
|
150 avsubtitle_free(&sub); |
31599 | 151 return new_type; |
152 } |