Mercurial > mplayer.hg
annotate sub/av_sub.c @ 34177:fadc00cc31d3
Optimize TranslateFilename().
Replace for loop with index by while loop with pointer.
Unite the if conditions.
author | ib |
---|---|
date | Wed, 26 Oct 2011 15:52:06 +0000 |
parents | a93891202051 |
children | cd1c0283e645 |
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" |
22888a8cb312
Do not use a path for including files in the same directory.
reimar
parents:
32457
diff
changeset
|
24 #include "av_sub.h" |
31599 | 25 |
26 void reset_avsub(struct sh_sub *sh) | |
27 { | |
28 if (sh->context) { | |
29 avcodec_close(sh->context); | |
30 av_freep(&sh->context); | |
31 } | |
32 } | |
33 | |
34 /** | |
35 * Decode a subtitle packet via libavcodec. | |
36 * \return < 0 on error, > 0 if further processing is needed | |
37 */ | |
32080 | 38 int decode_avsub(struct sh_sub *sh, uint8_t **data, int *size, |
39 double *pts, double *endpts) | |
31599 | 40 { |
41 AVCodecContext *ctx = sh->context; | |
31631
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
42 enum CodecID cid = CODEC_ID_NONE; |
31599 | 43 int new_type = 0; |
44 int res; | |
45 int got_sub; | |
46 AVSubtitle sub; | |
47 AVPacket pkt; | |
31631
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
48 |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
49 switch (sh->type) { |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
50 case 'b': |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
51 cid = CODEC_ID_DVB_SUBTITLE; break; |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
52 case 'p': |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
53 cid = CODEC_ID_HDMV_PGS_SUBTITLE; break; |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
54 case 'x': |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
55 cid = CODEC_ID_XSUB; break; |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
56 } |
67f2fb3ff4c7
Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents:
31628
diff
changeset
|
57 |
31599 | 58 av_init_packet(&pkt); |
59 pkt.data = *data; | |
60 pkt.size = *size; | |
61 pkt.pts = *pts * 1000; | |
62 if (*pts != MP_NOPTS_VALUE && *endpts != MP_NOPTS_VALUE) | |
63 pkt.convergence_duration = (*endpts - *pts) * 1000; | |
64 if (!ctx) { | |
65 AVCodec *sub_codec; | |
66 avcodec_init(); | |
67 avcodec_register_all(); | |
68 ctx = avcodec_alloc_context(); | |
31628
f15df2e3081b
Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents:
31624
diff
changeset
|
69 sub_codec = avcodec_find_decoder(cid); |
31599 | 70 if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) { |
32080 | 71 mp_msg(MSGT_SUBREADER, MSGL_FATAL, |
72 "Could not open subtitle decoder\n"); | |
31599 | 73 av_freep(&ctx); |
74 return -1; | |
75 } | |
76 sh->context = ctx; | |
77 } | |
78 res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt); | |
79 if (res < 0) | |
80 return res; | |
81 if (*pts != MP_NOPTS_VALUE) { | |
82 if (sub.end_display_time > sub.start_display_time) | |
83 *endpts = *pts + sub.end_display_time / 1000.0; | |
84 *pts += sub.start_display_time / 1000.0; | |
85 } | |
32564
61ac00c3c51c
Support clearing subtitles. Makes PGS subtitles disappear at the appropriate time.
reimar
parents:
32467
diff
changeset
|
86 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
|
87 spudec_set_paletted(vo_spudec, NULL, 0, NULL, 0, 0, 0, 0, *pts, *endpts); |
31599 | 88 if (got_sub && sub.num_rects > 0) { |
89 switch (sub.rects[0]->type) { | |
90 case SUBTITLE_BITMAP: | |
91 if (!vo_spudec) | |
31924
8d7f15885b64
Pass the video dimensions specified in the subtitle to spudec.
reimar
parents:
31642
diff
changeset
|
92 vo_spudec = spudec_new_scaled(NULL, ctx->width, ctx->height, NULL, 0); |
31599 | 93 spudec_set_paletted(vo_spudec, |
94 sub.rects[0]->pict.data[0], | |
95 sub.rects[0]->pict.linesize[0], | |
96 sub.rects[0]->pict.data[1], | |
97 sub.rects[0]->x, | |
98 sub.rects[0]->y, | |
99 sub.rects[0]->w, | |
100 sub.rects[0]->h, | |
101 *pts, | |
102 *endpts); | |
103 vo_osd_changed(OSDTYPE_SPU); | |
104 break; | |
105 case SUBTITLE_TEXT: | |
106 *data = strdup(sub.rects[0]->text); | |
31642 | 107 *size = strlen(*data); |
31599 | 108 new_type = 't'; |
109 break; | |
110 case SUBTITLE_ASS: | |
111 *data = strdup(sub.rects[0]->ass); | |
31642 | 112 *size = strlen(*data); |
31599 | 113 new_type = 'a'; |
114 break; | |
115 } | |
116 } | |
117 if (got_sub) | |
31624
40c30c70ead0
Fix memory leak for subtitles decoded by libavcodec.
reimar
parents:
31599
diff
changeset
|
118 avsubtitle_free(&sub); |
31599 | 119 return new_type; |
120 } |