stream_file: Simplify and document MinGW stdin hack
[mplayer/glamo.git] / av_sub.c
blobbada534024024d4abcfbed4914b35496cf891bf4
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 enum CodecID cid = CODEC_ID_NONE;
42 int srcw = 0, srch = 0;
43 int new_type = 0;
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 srcw = 1920; srch = 1080;
54 cid = CODEC_ID_HDMV_PGS_SUBTITLE; break;
55 case 'x':
56 cid = CODEC_ID_XSUB; break;
59 av_init_packet(&pkt);
60 pkt.data = *data;
61 pkt.size = *size;
62 pkt.pts = *pts * 1000;
63 if (*pts != MP_NOPTS_VALUE && *endpts != MP_NOPTS_VALUE)
64 pkt.convergence_duration = (*endpts - *pts) * 1000;
65 if (!ctx) {
66 AVCodec *sub_codec;
67 avcodec_init();
68 avcodec_register_all();
69 ctx = avcodec_alloc_context();
70 sub_codec = avcodec_find_decoder(cid);
71 if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) {
72 mp_msg(MSGT_SUBREADER, MSGL_FATAL, "Could not open subtitle decoder\n");
73 av_freep(&ctx);
74 return -1;
76 sh->context = ctx;
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;
86 if (got_sub && sub.num_rects > 0) {
87 switch (sub.rects[0]->type) {
88 case SUBTITLE_BITMAP:
89 if (!vo_spudec)
90 vo_spudec = spudec_new_scaled(NULL, srcw, srch, NULL, 0);
91 spudec_set_paletted(vo_spudec,
92 sub.rects[0]->pict.data[0],
93 sub.rects[0]->pict.linesize[0],
94 sub.rects[0]->pict.data[1],
95 sub.rects[0]->x,
96 sub.rects[0]->y,
97 sub.rects[0]->w,
98 sub.rects[0]->h,
99 *pts,
100 *endpts);
101 vo_osd_changed(OSDTYPE_SPU);
102 break;
103 case SUBTITLE_TEXT:
104 *data = strdup(sub.rects[0]->text);
105 *size = strlen(*data);
106 new_type = 't';
107 break;
108 case SUBTITLE_ASS:
109 *data = strdup(sub.rects[0]->ass);
110 *size = strlen(*data);
111 new_type = 'a';
112 break;
115 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 82, 0)
116 if (got_sub)
117 avsubtitle_free(&sub);
118 #endif
119 return new_type;