annotate av_sub.c @ 32315:549b1ad3f777

Deprecate sws_getContext(), use sws_alloc_context() and sws_init_context() instead.
author stefano
date Tue, 28 Sep 2010 22:23:58 +0000
parents 8c29dfec15c2
children 728bd5c2aea7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
1 /*
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
2 * This file is part of MPlayer.
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
3 *
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
7 * (at your option) any later version.
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
8 *
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
12 * GNU General Public License for more details.
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
13 *
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License along
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
17 */
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
18
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
19 #include "libavcodec/avcodec.h"
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
20 #include "libmpdemux/stheader.h"
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
21 #include "libvo/sub.h"
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
22 #include "spudec.h"
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
23 #include "av_sub.h"
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
24
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
25 void reset_avsub(struct sh_sub *sh)
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
26 {
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
27 if (sh->context) {
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
28 avcodec_close(sh->context);
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
29 av_freep(&sh->context);
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
30 }
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
31 }
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
32
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
33 /**
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
34 * Decode a subtitle packet via libavcodec.
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
35 * \return < 0 on error, > 0 if further processing is needed
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
36 */
32080
8c29dfec15c2 cosmetics: Break some long lines.
diego
parents: 31924
diff changeset
37 int decode_avsub(struct sh_sub *sh, uint8_t **data, int *size,
8c29dfec15c2 cosmetics: Break some long lines.
diego
parents: 31924
diff changeset
38 double *pts, double *endpts)
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
39 {
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
40 AVCodecContext *ctx = sh->context;
31631
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
41 enum CodecID cid = CODEC_ID_NONE;
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
42 int new_type = 0;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
43 int res;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
44 int got_sub;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
45 AVSubtitle sub;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
46 AVPacket pkt;
31631
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
47
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
48 switch (sh->type) {
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
49 case 'b':
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
50 cid = CODEC_ID_DVB_SUBTITLE; break;
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
51 case 'p':
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
52 cid = CODEC_ID_HDMV_PGS_SUBTITLE; break;
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
53 case 'x':
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
54 cid = CODEC_ID_XSUB; break;
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
55 }
67f2fb3ff4c7 Try to get subtitle scaling somewhat right with libavcodec decoded
reimar
parents: 31628
diff changeset
56
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
57 av_init_packet(&pkt);
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
58 pkt.data = *data;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
59 pkt.size = *size;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
60 pkt.pts = *pts * 1000;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
61 if (*pts != MP_NOPTS_VALUE && *endpts != MP_NOPTS_VALUE)
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
62 pkt.convergence_duration = (*endpts - *pts) * 1000;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
63 if (!ctx) {
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
64 AVCodec *sub_codec;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
65 avcodec_init();
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
66 avcodec_register_all();
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
67 ctx = avcodec_alloc_context();
31628
f15df2e3081b Add support for DVB and XSUB subtitles, not yet working properly.
reimar
parents: 31624
diff changeset
68 sub_codec = avcodec_find_decoder(cid);
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
69 if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) {
32080
8c29dfec15c2 cosmetics: Break some long lines.
diego
parents: 31924
diff changeset
70 mp_msg(MSGT_SUBREADER, MSGL_FATAL,
8c29dfec15c2 cosmetics: Break some long lines.
diego
parents: 31924
diff changeset
71 "Could not open subtitle decoder\n");
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
72 av_freep(&ctx);
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
73 return -1;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
74 }
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
75 sh->context = ctx;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
76 }
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
77 res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
78 if (res < 0)
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
79 return res;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
80 if (*pts != MP_NOPTS_VALUE) {
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
81 if (sub.end_display_time > sub.start_display_time)
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
82 *endpts = *pts + sub.end_display_time / 1000.0;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
83 *pts += sub.start_display_time / 1000.0;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
84 }
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
85 if (got_sub && sub.num_rects > 0) {
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
86 switch (sub.rects[0]->type) {
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
87 case SUBTITLE_BITMAP:
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
88 if (!vo_spudec)
31924
8d7f15885b64 Pass the video dimensions specified in the subtitle to spudec.
reimar
parents: 31642
diff changeset
89 vo_spudec = spudec_new_scaled(NULL, ctx->width, ctx->height, NULL, 0);
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
90 spudec_set_paletted(vo_spudec,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
91 sub.rects[0]->pict.data[0],
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
92 sub.rects[0]->pict.linesize[0],
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
93 sub.rects[0]->pict.data[1],
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
94 sub.rects[0]->x,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
95 sub.rects[0]->y,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
96 sub.rects[0]->w,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
97 sub.rects[0]->h,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
98 *pts,
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
99 *endpts);
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
100 vo_osd_changed(OSDTYPE_SPU);
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
101 break;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
102 case SUBTITLE_TEXT:
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
103 *data = strdup(sub.rects[0]->text);
31642
af4d89e131b4 Set size correctly for decoded text subs.
reimar
parents: 31631
diff changeset
104 *size = strlen(*data);
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
105 new_type = 't';
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
106 break;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
107 case SUBTITLE_ASS:
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
108 *data = strdup(sub.rects[0]->ass);
31642
af4d89e131b4 Set size correctly for decoded text subs.
reimar
parents: 31631
diff changeset
109 *size = strlen(*data);
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
110 new_type = 'a';
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
111 break;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
112 }
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
113 }
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
114 if (got_sub)
31624
40c30c70ead0 Fix memory leak for subtitles decoded by libavcodec.
reimar
parents: 31599
diff changeset
115 avsubtitle_free(&sub);
31599
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
116 return new_type;
cafeb7863de8 Add support for PGS subtitle decoding via libavcodec.
reimar
parents:
diff changeset
117 }