annotate dvdsub.c @ 3947:c8c591fe26f8 libavcodec

Change license headers to say 'FFmpeg' instead of 'this program/this library' and fix GPL/LGPL version mismatches.
author diego
date Sat, 07 Oct 2006 15:30:46 +0000
parents 961af1358c7f
children 6d4ac21853d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
1 /*
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
2 * DVD subtitle decoding for ffmpeg
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
3 * Copyright (c) 2005 Fabrice Bellard.
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3075
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3075
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3075
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3075
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3075
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
15 * Lesser General Public License for more details.
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
16 *
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3075
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
20 */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
21 #include "avcodec.h"
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
22
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
23 //#define DEBUG
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
24
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
25 typedef struct DVDSubContext {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
26 } DVDSubContext;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
27
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
28 static int dvdsub_init_decoder(AVCodecContext *avctx)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
29 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
30 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
31 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
32
3075
961af1358c7f add static keyword to some functions
mru
parents: 3036
diff changeset
33 static uint16_t getbe16(const uint8_t *p)
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
34 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
35 return (p[0] << 8) | p[1];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
36 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
37
3075
961af1358c7f add static keyword to some functions
mru
parents: 3036
diff changeset
38 static int get_nibble(const uint8_t *buf, int nibble_offset)
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
39 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
40 return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
41 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
42
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
43 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
44 const uint8_t *buf, int nibble_offset, int buf_size)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
45 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
46 unsigned int v;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
47 int x, y, len, color, nibble_end;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
48 uint8_t *d;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
49
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
50 nibble_end = buf_size * 2;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
51 x = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
52 y = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
53 d = bitmap;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
54 for(;;) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
55 if (nibble_offset >= nibble_end)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
56 return -1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
57 v = get_nibble(buf, nibble_offset++);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
58 if (v < 0x4) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
59 v = (v << 4) | get_nibble(buf, nibble_offset++);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
60 if (v < 0x10) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
61 v = (v << 4) | get_nibble(buf, nibble_offset++);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
62 if (v < 0x040) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
63 v = (v << 4) | get_nibble(buf, nibble_offset++);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
64 if (v < 4) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
65 v |= (w - x) << 2;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
66 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
67 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
68 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
69 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
70 len = v >> 2;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
71 if (len > (w - x))
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
72 len = (w - x);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
73 color = v & 0x03;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
74 memset(d + x, color, len);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
75 x += len;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
76 if (x >= w) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
77 y++;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
78 if (y >= h)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
79 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
80 d += linesize;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
81 x = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
82 /* byte align */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
83 nibble_offset += (nibble_offset & 1);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
84 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
85 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
86 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
87 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
88
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
89 static void guess_palette(uint32_t *rgba_palette,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
90 uint8_t *palette,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
91 uint8_t *alpha,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
92 uint32_t subtitle_color)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
93 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
94 uint8_t color_used[16];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
95 int nb_opaque_colors, i, level, j, r, g, b;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
96
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
97 for(i = 0; i < 4; i++)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
98 rgba_palette[i] = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
99
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
100 memset(color_used, 0, 16);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
101 nb_opaque_colors = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
102 for(i = 0; i < 4; i++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
103 if (alpha[i] != 0 && !color_used[palette[i]]) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
104 color_used[palette[i]] = 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
105 nb_opaque_colors++;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
106 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
107 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
108
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
109 if (nb_opaque_colors == 0)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
110 return;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
111
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
112 j = nb_opaque_colors;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
113 memset(color_used, 0, 16);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
114 for(i = 0; i < 4; i++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
115 if (alpha[i] != 0) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
116 if (!color_used[palette[i]]) {
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
117 level = (0xff * j) / nb_opaque_colors;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
118 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
119 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
120 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
121 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
122 color_used[palette[i]] = (i + 1);
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
123 j--;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
124 } else {
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
125 rgba_palette[i] = (rgba_palette[color_used[palette[i]] - 1] & 0x00ffffff) |
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
126 ((alpha[i] * 17) << 24);
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
127 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
128 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
129 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
130 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
131
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
132 static int decode_dvd_subtitles(AVSubtitle *sub_header,
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
133 const uint8_t *buf, int buf_size)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
134 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
135 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
136 uint8_t palette[4], alpha[4];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
137 int date;
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
138 int i;
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
139 int is_menu = 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
140
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
141 if (buf_size < 4)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
142 return -1;
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
143 sub_header->rects = NULL;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
144 sub_header->num_rects = 0;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
145 sub_header->start_display_time = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
146 sub_header->end_display_time = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
147
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
148 cmd_pos = getbe16(buf + 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
149 while ((cmd_pos + 4) < buf_size) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
150 date = getbe16(buf + cmd_pos);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
151 next_cmd_pos = getbe16(buf + cmd_pos + 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
152 #ifdef DEBUG
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
153 av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
154 cmd_pos, next_cmd_pos, date);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
155 #endif
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
156 pos = cmd_pos + 4;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
157 offset1 = -1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
158 offset2 = -1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
159 x1 = y1 = x2 = y2 = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
160 while (pos < buf_size) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
161 cmd = buf[pos++];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
162 #ifdef DEBUG
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
163 av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
164 #endif
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
165 switch(cmd) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
166 case 0x00:
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
167 /* menu subpicture */
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
168 is_menu = 1;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
169 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
170 case 0x01:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
171 /* set start date */
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
172 sub_header->start_display_time = (date << 10) / 90;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
173 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
174 case 0x02:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
175 /* set end date */
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
176 sub_header->end_display_time = (date << 10) / 90;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
177 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
178 case 0x03:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
179 /* set palette */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
180 if ((buf_size - pos) < 2)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
181 goto fail;
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
182 palette[3] = buf[pos] >> 4;
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
183 palette[2] = buf[pos] & 0x0f;
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
184 palette[1] = buf[pos + 1] >> 4;
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
185 palette[0] = buf[pos + 1] & 0x0f;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
186 pos += 2;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
187 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
188 case 0x04:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
189 /* set alpha */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
190 if ((buf_size - pos) < 2)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
191 goto fail;
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
192 alpha[3] = buf[pos] >> 4;
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
193 alpha[2] = buf[pos] & 0x0f;
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
194 alpha[1] = buf[pos + 1] >> 4;
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
195 alpha[0] = buf[pos + 1] & 0x0f;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
196 pos += 2;
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
197 #ifdef DEBUG
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
198 av_log(NULL, AV_LOG_INFO, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
199 #endif
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
200 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
201 case 0x05:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
202 if ((buf_size - pos) < 6)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
203 goto fail;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
204 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
205 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
206 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
207 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
208 #ifdef DEBUG
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
209 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
210 x1, x2, y1, y2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
211 #endif
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
212 pos += 6;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
213 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
214 case 0x06:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
215 if ((buf_size - pos) < 4)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
216 goto fail;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
217 offset1 = getbe16(buf + pos);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
218 offset2 = getbe16(buf + pos + 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
219 #ifdef DEBUG
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
220 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
221 #endif
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
222 pos += 4;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
223 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
224 case 0xff:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
225 default:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
226 goto the_end;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
227 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
228 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
229 the_end:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
230 if (offset1 >= 0) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
231 int w, h;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
232 uint8_t *bitmap;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
233
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
234 /* decode the bitmap */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
235 w = x2 - x1 + 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
236 if (w < 0)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
237 w = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
238 h = y2 - y1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
239 if (h < 0)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
240 h = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
241 if (w > 0 && h > 0) {
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
242 if (sub_header->rects != NULL) {
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
243 for (i = 0; i < sub_header->num_rects; i++) {
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
244 av_free(sub_header->rects[i].bitmap);
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
245 av_free(sub_header->rects[i].rgba_palette);
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
246 }
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
247 av_freep(&sub_header->rects);
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
248 sub_header->num_rects = 0;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
249 }
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
250
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
251 bitmap = av_malloc(w * h);
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
252 sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
253 sub_header->num_rects = 1;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
254 sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
255 decode_rle(bitmap, w * 2, w, h / 2,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
256 buf, offset1 * 2, buf_size);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
257 decode_rle(bitmap + w, w * 2, w, h / 2,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
258 buf, offset2 * 2, buf_size);
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
259 guess_palette(sub_header->rects[0].rgba_palette,
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
260 palette, alpha, 0xffff00);
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
261 sub_header->rects[0].x = x1;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
262 sub_header->rects[0].y = y1;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
263 sub_header->rects[0].w = w;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
264 sub_header->rects[0].h = h;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
265 sub_header->rects[0].nb_colors = 4;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
266 sub_header->rects[0].linesize = w;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
267 sub_header->rects[0].bitmap = bitmap;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
268 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
269 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
270 if (next_cmd_pos == cmd_pos)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
271 break;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
272 cmd_pos = next_cmd_pos;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
273 }
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
274 if (sub_header->num_rects > 0)
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
275 return is_menu;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
276 fail:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
277 return -1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
278 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
279
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
280 static int is_transp(const uint8_t *buf, int pitch, int n,
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
281 const uint8_t *transp_color)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
282 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
283 int i;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
284 for(i = 0; i < n; i++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
285 if (!transp_color[*buf])
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
286 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
287 buf += pitch;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
288 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
289 return 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
290 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
291
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
292 /* return 0 if empty rectangle, 1 if non empty */
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
293 static int find_smallest_bounding_rectangle(AVSubtitle *s)
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
294 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
295 uint8_t transp_color[256];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
296 int y1, y2, x1, x2, y, w, h, i;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
297 uint8_t *bitmap;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
298
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
299 if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
300 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
301
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
302 memset(transp_color, 0, 256);
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
303 for(i = 0; i < s->rects[0].nb_colors; i++) {
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
304 if ((s->rects[0].rgba_palette[i] >> 24) == 0)
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
305 transp_color[i] = 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
306 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
307 y1 = 0;
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
308 while (y1 < s->rects[0].h && is_transp(s->rects[0].bitmap + y1 * s->rects[0].linesize,
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
309 1, s->rects[0].w, transp_color))
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
310 y1++;
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
311 if (y1 == s->rects[0].h) {
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
312 av_freep(&s->rects[0].bitmap);
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
313 s->rects[0].w = s->rects[0].h = 0;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
314 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
315 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
316
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
317 y2 = s->rects[0].h - 1;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
318 while (y2 > 0 && is_transp(s->rects[0].bitmap + y2 * s->rects[0].linesize, 1,
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
319 s->rects[0].w, transp_color))
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
320 y2--;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
321 x1 = 0;
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
322 while (x1 < (s->rects[0].w - 1) && is_transp(s->rects[0].bitmap + x1, s->rects[0].linesize,
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
323 s->rects[0].h, transp_color))
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
324 x1++;
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
325 x2 = s->rects[0].w - 1;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
326 while (x2 > 0 && is_transp(s->rects[0].bitmap + x2, s->rects[0].linesize, s->rects[0].h,
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
327 transp_color))
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
328 x2--;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
329 w = x2 - x1 + 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
330 h = y2 - y1 + 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
331 bitmap = av_malloc(w * h);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
332 if (!bitmap)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
333 return 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
334 for(y = 0; y < h; y++) {
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
335 memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
336 }
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
337 av_freep(&s->rects[0].bitmap);
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
338 s->rects[0].bitmap = bitmap;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
339 s->rects[0].linesize = w;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
340 s->rects[0].w = w;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
341 s->rects[0].h = h;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
342 s->rects[0].x += x1;
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
343 s->rects[0].y += y1;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
344 return 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
345 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
346
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
347 static int dvdsub_close_decoder(AVCodecContext *avctx)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
348 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
349 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
350 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
351
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
352 #ifdef DEBUG
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
353 #undef fprintf
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
354 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
355 uint32_t *rgba_palette)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
356 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
357 int x, y, v;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
358 FILE *f;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
359
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
360 f = fopen(filename, "w");
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
361 if (!f) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
362 perror(filename);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
363 exit(1);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
364 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
365 fprintf(f, "P6\n"
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
366 "%d %d\n"
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
367 "%d\n",
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
368 w, h, 255);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
369 for(y = 0; y < h; y++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
370 for(x = 0; x < w; x++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
371 v = rgba_palette[bitmap[y * w + x]];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
372 putc((v >> 16) & 0xff, f);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
373 putc((v >> 8) & 0xff, f);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
374 putc((v >> 0) & 0xff, f);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
375 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
376 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
377 fclose(f);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
378 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
379 #endif
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
380
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
381 static int dvdsub_decode(AVCodecContext *avctx,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
382 void *data, int *data_size,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
383 uint8_t *buf, int buf_size)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
384 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
385 AVSubtitle *sub = (void *)data;
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
386 int is_menu;
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
387
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
388 is_menu = decode_dvd_subtitles(sub, buf, buf_size);
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
389
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
390 if (is_menu < 0) {
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
391 no_subtitle:
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
392 *data_size = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
393
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
394 return buf_size;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
395 }
2833
1f117208d20f subs.diff fixes a couple of minor bugs in my DVB subtitle decoder, and also fixes a few
michael
parents: 2797
diff changeset
396 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
397 goto no_subtitle;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
398
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
399 #if defined(DEBUG)
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
400 av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
401 sub->start_display_time,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
402 sub->end_display_time);
2797
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
403 ppm_save("/tmp/a.ppm", sub->rects[0].bitmap,
217844bd1fa1 10l (forgot to commit this yesterday)
michael
parents: 2756
diff changeset
404 sub->rects[0].w, sub->rects[0].h, sub->rects[0].rgba_palette);
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
405 #endif
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
406
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
407 *data_size = 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
408 return buf_size;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
409 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
410
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
411 AVCodec dvdsub_decoder = {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
412 "dvdsub",
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
413 CODEC_TYPE_SUBTITLE,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
414 CODEC_ID_DVD_SUBTITLE,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
415 sizeof(DVDSubContext),
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
416 dvdsub_init_decoder,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
417 NULL,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
418 dvdsub_close_decoder,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
419 dvdsub_decode,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
420 };
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
421
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
422 /* parser definition */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
423 typedef struct DVDSubParseContext {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
424 uint8_t *packet;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
425 int packet_len;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
426 int packet_index;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
427 } DVDSubParseContext;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
428
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
429 static int dvdsub_parse_init(AVCodecParserContext *s)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
430 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
431 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
432 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
433
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
434 static int dvdsub_parse(AVCodecParserContext *s,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
435 AVCodecContext *avctx,
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
436 uint8_t **poutbuf, int *poutbuf_size,
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
437 const uint8_t *buf, int buf_size)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
438 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
439 DVDSubParseContext *pc = s->priv_data;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2833
diff changeset
440
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
441 if (pc->packet_index == 0) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
442 if (buf_size < 2)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
443 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
444 pc->packet_len = (buf[0] << 8) | buf[1];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
445 av_freep(&pc->packet);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
446 pc->packet = av_malloc(pc->packet_len);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
447 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
448 if (pc->packet) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
449 if (pc->packet_index + buf_size <= pc->packet_len) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
450 memcpy(pc->packet + pc->packet_index, buf, buf_size);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
451 pc->packet_index += buf_size;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
452 if (pc->packet_index >= pc->packet_len) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
453 *poutbuf = pc->packet;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
454 *poutbuf_size = pc->packet_len;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
455 pc->packet_index = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
456 return buf_size;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
457 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
458 } else {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
459 /* erroneous size */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
460 pc->packet_index = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
461 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
462 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
463 *poutbuf = NULL;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
464 *poutbuf_size = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
465 return buf_size;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
466 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
467
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
468 static void dvdsub_parse_close(AVCodecParserContext *s)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
469 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
470 DVDSubParseContext *pc = s->priv_data;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
471 av_freep(&pc->packet);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
472 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
473
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
474 AVCodecParser dvdsub_parser = {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
475 { CODEC_ID_DVD_SUBTITLE },
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
476 sizeof(DVDSubParseContext),
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
477 dvdsub_parse_init,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
478 dvdsub_parse,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
479 dvdsub_parse_close,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
480 };