1 |
|
|
/* |
2 |
|
|
* PGS subtitle decoder |
3 |
|
|
* Copyright (c) 2009 Stephen Backway |
4 |
|
|
* |
5 |
|
|
* This file is part of FFmpeg. |
6 |
|
|
* |
7 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU Lesser General Public |
9 |
|
|
* License as published by the Free Software Foundation; either |
10 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* Lesser General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU Lesser General Public |
18 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
* @file |
24 |
|
|
* PGS subtitle decoder |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include "avcodec.h" |
28 |
|
|
#include "bytestream.h" |
29 |
|
|
#include "internal.h" |
30 |
|
|
#include "mathops.h" |
31 |
|
|
|
32 |
|
|
#include "libavutil/colorspace.h" |
33 |
|
|
#include "libavutil/imgutils.h" |
34 |
|
|
#include "libavutil/opt.h" |
35 |
|
|
|
36 |
|
|
#define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) |
37 |
|
|
#define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch |
38 |
|
|
#define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch |
39 |
|
|
#define MAX_OBJECT_REFS 2 // Max objects per display set |
40 |
|
|
|
41 |
|
|
enum SegmentType { |
42 |
|
|
PALETTE_SEGMENT = 0x14, |
43 |
|
|
OBJECT_SEGMENT = 0x15, |
44 |
|
|
PRESENTATION_SEGMENT = 0x16, |
45 |
|
|
WINDOW_SEGMENT = 0x17, |
46 |
|
|
DISPLAY_SEGMENT = 0x80, |
47 |
|
|
}; |
48 |
|
|
|
49 |
|
|
typedef struct PGSSubObjectRef { |
50 |
|
|
int id; |
51 |
|
|
int window_id; |
52 |
|
|
uint8_t composition_flag; |
53 |
|
|
int x; |
54 |
|
|
int y; |
55 |
|
|
int crop_x; |
56 |
|
|
int crop_y; |
57 |
|
|
int crop_w; |
58 |
|
|
int crop_h; |
59 |
|
|
} PGSSubObjectRef; |
60 |
|
|
|
61 |
|
|
typedef struct PGSSubPresentation { |
62 |
|
|
int id_number; |
63 |
|
|
int palette_id; |
64 |
|
|
int object_count; |
65 |
|
|
PGSSubObjectRef objects[MAX_OBJECT_REFS]; |
66 |
|
|
int64_t pts; |
67 |
|
|
} PGSSubPresentation; |
68 |
|
|
|
69 |
|
|
typedef struct PGSSubObject { |
70 |
|
|
int id; |
71 |
|
|
int w; |
72 |
|
|
int h; |
73 |
|
|
uint8_t *rle; |
74 |
|
|
unsigned int rle_buffer_size, rle_data_len; |
75 |
|
|
unsigned int rle_remaining_len; |
76 |
|
|
} PGSSubObject; |
77 |
|
|
|
78 |
|
|
typedef struct PGSSubObjects { |
79 |
|
|
int count; |
80 |
|
|
PGSSubObject object[MAX_EPOCH_OBJECTS]; |
81 |
|
|
} PGSSubObjects; |
82 |
|
|
|
83 |
|
|
typedef struct PGSSubPalette { |
84 |
|
|
int id; |
85 |
|
|
uint32_t clut[256]; |
86 |
|
|
} PGSSubPalette; |
87 |
|
|
|
88 |
|
|
typedef struct PGSSubPalettes { |
89 |
|
|
int count; |
90 |
|
|
PGSSubPalette palette[MAX_EPOCH_PALETTES]; |
91 |
|
|
} PGSSubPalettes; |
92 |
|
|
|
93 |
|
|
typedef struct PGSSubContext { |
94 |
|
|
AVClass *class; |
95 |
|
|
PGSSubPresentation presentation; |
96 |
|
|
PGSSubPalettes palettes; |
97 |
|
|
PGSSubObjects objects; |
98 |
|
|
int forced_subs_only; |
99 |
|
|
} PGSSubContext; |
100 |
|
|
|
101 |
|
5 |
static void flush_cache(AVCodecContext *avctx) |
102 |
|
|
{ |
103 |
|
5 |
PGSSubContext *ctx = avctx->priv_data; |
104 |
|
|
int i; |
105 |
|
|
|
106 |
✓✓ |
8 |
for (i = 0; i < ctx->objects.count; i++) { |
107 |
|
3 |
av_freep(&ctx->objects.object[i].rle); |
108 |
|
3 |
ctx->objects.object[i].rle_buffer_size = 0; |
109 |
|
3 |
ctx->objects.object[i].rle_remaining_len = 0; |
110 |
|
|
} |
111 |
|
5 |
ctx->objects.count = 0; |
112 |
|
5 |
ctx->palettes.count = 0; |
113 |
|
5 |
} |
114 |
|
|
|
115 |
|
5 |
static PGSSubObject * find_object(int id, PGSSubObjects *objects) |
116 |
|
|
{ |
117 |
|
|
int i; |
118 |
|
|
|
119 |
✓✓ |
7 |
for (i = 0; i < objects->count; i++) { |
120 |
✓✓ |
4 |
if (objects->object[i].id == id) |
121 |
|
2 |
return &objects->object[i]; |
122 |
|
|
} |
123 |
|
3 |
return NULL; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
3 |
static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes) |
127 |
|
|
{ |
128 |
|
|
int i; |
129 |
|
|
|
130 |
✓✓ |
3 |
for (i = 0; i < palettes->count; i++) { |
131 |
✓✗ |
1 |
if (palettes->palette[i].id == id) |
132 |
|
1 |
return &palettes->palette[i]; |
133 |
|
|
} |
134 |
|
2 |
return NULL; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
2 |
static av_cold int init_decoder(AVCodecContext *avctx) |
138 |
|
|
{ |
139 |
|
2 |
avctx->pix_fmt = AV_PIX_FMT_PAL8; |
140 |
|
|
|
141 |
|
2 |
return 0; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
2 |
static av_cold int close_decoder(AVCodecContext *avctx) |
145 |
|
|
{ |
146 |
|
2 |
flush_cache(avctx); |
147 |
|
|
|
148 |
|
2 |
return 0; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
/** |
152 |
|
|
* Decode the RLE data. |
153 |
|
|
* |
154 |
|
|
* The subtitle is stored as a Run Length Encoded image. |
155 |
|
|
* |
156 |
|
|
* @param avctx contains the current codec context |
157 |
|
|
* @param sub pointer to the processed subtitle data |
158 |
|
|
* @param buf pointer to the RLE data to process |
159 |
|
|
* @param buf_size size of the RLE data to process |
160 |
|
|
*/ |
161 |
|
2 |
static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect, |
162 |
|
|
const uint8_t *buf, unsigned int buf_size) |
163 |
|
|
{ |
164 |
|
|
const uint8_t *rle_bitmap_end; |
165 |
|
|
int pixel_count, line_count; |
166 |
|
|
|
167 |
|
2 |
rle_bitmap_end = buf + buf_size; |
168 |
|
|
|
169 |
|
2 |
rect->data[0] = av_malloc_array(rect->w, rect->h); |
170 |
|
|
|
171 |
✗✓ |
2 |
if (!rect->data[0]) |
172 |
|
|
return AVERROR(ENOMEM); |
173 |
|
|
|
174 |
|
2 |
pixel_count = 0; |
175 |
|
2 |
line_count = 0; |
176 |
|
|
|
177 |
✓✓✓✗
|
15729 |
while (buf < rle_bitmap_end && line_count < rect->h) { |
178 |
|
|
uint8_t flags, color; |
179 |
|
|
int run; |
180 |
|
|
|
181 |
|
15727 |
color = bytestream_get_byte(&buf); |
182 |
|
15727 |
run = 1; |
183 |
|
|
|
184 |
✓✓ |
15727 |
if (color == 0x00) { |
185 |
|
5718 |
flags = bytestream_get_byte(&buf); |
186 |
|
5718 |
run = flags & 0x3f; |
187 |
✓✓ |
5718 |
if (flags & 0x40) |
188 |
|
254 |
run = (run << 8) + bytestream_get_byte(&buf); |
189 |
✓✓ |
5718 |
color = flags & 0x80 ? bytestream_get_byte(&buf) : 0; |
190 |
|
|
} |
191 |
|
|
|
192 |
✓✓✓✗
|
15727 |
if (run > 0 && pixel_count + run <= rect->w * rect->h) { |
193 |
|
15597 |
memset(rect->data[0] + pixel_count, color, run); |
194 |
|
15597 |
pixel_count += run; |
195 |
✓✗ |
130 |
} else if (!run) { |
196 |
|
|
/* |
197 |
|
|
* New Line. Check if correct pixels decoded, if not display warning |
198 |
|
|
* and adjust bitmap pointer to correct new line position. |
199 |
|
|
*/ |
200 |
✗✓ |
130 |
if (pixel_count % rect->w > 0) { |
201 |
|
|
av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n", |
202 |
|
|
pixel_count % rect->w, rect->w); |
203 |
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) { |
204 |
|
|
return AVERROR_INVALIDDATA; |
205 |
|
|
} |
206 |
|
|
} |
207 |
|
130 |
line_count++; |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
|
211 |
✗✓ |
2 |
if (pixel_count < rect->w * rect->h) { |
212 |
|
|
av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n"); |
213 |
|
|
return AVERROR_INVALIDDATA; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
ff_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h); |
217 |
|
|
|
218 |
|
2 |
return 0; |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
/** |
222 |
|
|
* Parse the picture segment packet. |
223 |
|
|
* |
224 |
|
|
* The picture segment contains details on the sequence id, |
225 |
|
|
* width, height and Run Length Encoded (RLE) bitmap data. |
226 |
|
|
* |
227 |
|
|
* @param avctx contains the current codec context |
228 |
|
|
* @param buf pointer to the packet to process |
229 |
|
|
* @param buf_size size of packet to process |
230 |
|
|
*/ |
231 |
|
3 |
static int parse_object_segment(AVCodecContext *avctx, |
232 |
|
|
const uint8_t *buf, int buf_size) |
233 |
|
|
{ |
234 |
|
3 |
PGSSubContext *ctx = avctx->priv_data; |
235 |
|
|
PGSSubObject *object; |
236 |
|
|
|
237 |
|
|
uint8_t sequence_desc; |
238 |
|
|
unsigned int rle_bitmap_len, width, height; |
239 |
|
|
int id; |
240 |
|
|
|
241 |
✗✓ |
3 |
if (buf_size <= 4) |
242 |
|
|
return AVERROR_INVALIDDATA; |
243 |
|
3 |
buf_size -= 4; |
244 |
|
|
|
245 |
|
3 |
id = bytestream_get_be16(&buf); |
246 |
|
3 |
object = find_object(id, &ctx->objects); |
247 |
✓✗ |
3 |
if (!object) { |
248 |
✗✓ |
3 |
if (ctx->objects.count >= MAX_EPOCH_OBJECTS) { |
249 |
|
|
av_log(avctx, AV_LOG_ERROR, "Too many objects in epoch\n"); |
250 |
|
|
return AVERROR_INVALIDDATA; |
251 |
|
|
} |
252 |
|
3 |
object = &ctx->objects.object[ctx->objects.count++]; |
253 |
|
3 |
object->id = id; |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
/* skip object version number */ |
257 |
|
3 |
buf += 1; |
258 |
|
|
|
259 |
|
|
/* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */ |
260 |
|
3 |
sequence_desc = bytestream_get_byte(&buf); |
261 |
|
|
|
262 |
✗✓ |
3 |
if (!(sequence_desc & 0x80)) { |
263 |
|
|
/* Additional RLE data */ |
264 |
|
|
if (buf_size > object->rle_remaining_len) |
265 |
|
|
return AVERROR_INVALIDDATA; |
266 |
|
|
|
267 |
|
|
memcpy(object->rle + object->rle_data_len, buf, buf_size); |
268 |
|
|
object->rle_data_len += buf_size; |
269 |
|
|
object->rle_remaining_len -= buf_size; |
270 |
|
|
|
271 |
|
|
return 0; |
272 |
|
|
} |
273 |
|
|
|
274 |
✗✓ |
3 |
if (buf_size <= 7) |
275 |
|
|
return AVERROR_INVALIDDATA; |
276 |
|
3 |
buf_size -= 7; |
277 |
|
|
|
278 |
|
|
/* Decode rle bitmap length, stored size includes width/height data */ |
279 |
|
3 |
rle_bitmap_len = bytestream_get_be24(&buf) - 2*2; |
280 |
|
|
|
281 |
✗✓ |
3 |
if (buf_size > rle_bitmap_len) { |
282 |
|
|
av_log(avctx, AV_LOG_ERROR, |
283 |
|
|
"Buffer dimension %d larger than the expected RLE data %d\n", |
284 |
|
|
buf_size, rle_bitmap_len); |
285 |
|
|
return AVERROR_INVALIDDATA; |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
/* Get bitmap dimensions from data */ |
289 |
|
3 |
width = bytestream_get_be16(&buf); |
290 |
|
3 |
height = bytestream_get_be16(&buf); |
291 |
|
|
|
292 |
|
|
/* Make sure the bitmap is not too large */ |
293 |
✓✗✓✗ ✓✗✗✓
|
3 |
if (avctx->width < width || avctx->height < height || !width || !height) { |
294 |
|
|
av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) invalid.\n", width, height); |
295 |
|
|
return AVERROR_INVALIDDATA; |
296 |
|
|
} |
297 |
|
|
|
298 |
|
3 |
object->w = width; |
299 |
|
3 |
object->h = height; |
300 |
|
|
|
301 |
|
3 |
av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len); |
302 |
|
|
|
303 |
✗✓ |
3 |
if (!object->rle) { |
304 |
|
|
object->rle_data_len = 0; |
305 |
|
|
object->rle_remaining_len = 0; |
306 |
|
|
return AVERROR(ENOMEM); |
307 |
|
|
} |
308 |
|
|
|
309 |
|
3 |
memcpy(object->rle, buf, buf_size); |
310 |
|
3 |
object->rle_data_len = buf_size; |
311 |
|
3 |
object->rle_remaining_len = rle_bitmap_len - buf_size; |
312 |
|
|
|
313 |
|
3 |
return 0; |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
/** |
317 |
|
|
* Parse the palette segment packet. |
318 |
|
|
* |
319 |
|
|
* The palette segment contains details of the palette, |
320 |
|
|
* a maximum of 256 colors can be defined. |
321 |
|
|
* |
322 |
|
|
* @param avctx contains the current codec context |
323 |
|
|
* @param buf pointer to the packet to process |
324 |
|
|
* @param buf_size size of packet to process |
325 |
|
|
*/ |
326 |
|
2 |
static int parse_palette_segment(AVCodecContext *avctx, |
327 |
|
|
const uint8_t *buf, int buf_size) |
328 |
|
|
{ |
329 |
|
2 |
PGSSubContext *ctx = avctx->priv_data; |
330 |
|
|
PGSSubPalette *palette; |
331 |
|
|
|
332 |
|
2 |
const uint8_t *buf_end = buf + buf_size; |
333 |
|
2 |
const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; |
334 |
|
|
int color_id; |
335 |
|
|
int y, cb, cr, alpha; |
336 |
|
|
int r, g, b, r_add, g_add, b_add; |
337 |
|
|
int id; |
338 |
|
|
|
339 |
|
2 |
id = bytestream_get_byte(&buf); |
340 |
|
2 |
palette = find_palette(id, &ctx->palettes); |
341 |
✓✗ |
2 |
if (!palette) { |
342 |
✗✓ |
2 |
if (ctx->palettes.count >= MAX_EPOCH_PALETTES) { |
343 |
|
|
av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n"); |
344 |
|
|
return AVERROR_INVALIDDATA; |
345 |
|
|
} |
346 |
|
2 |
palette = &ctx->palettes.palette[ctx->palettes.count++]; |
347 |
|
2 |
palette->id = id; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
/* Skip palette version */ |
351 |
|
2 |
buf += 1; |
352 |
|
|
|
353 |
✓✓ |
64 |
while (buf < buf_end) { |
354 |
|
62 |
color_id = bytestream_get_byte(&buf); |
355 |
|
62 |
y = bytestream_get_byte(&buf); |
356 |
|
62 |
cr = bytestream_get_byte(&buf); |
357 |
|
62 |
cb = bytestream_get_byte(&buf); |
358 |
|
62 |
alpha = bytestream_get_byte(&buf); |
359 |
|
|
|
360 |
|
|
/* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */ |
361 |
✓✗✓✗
|
62 |
if (avctx->height <= 0 || avctx->height > 576) { |
362 |
|
62 |
YUV_TO_RGB1_CCIR_BT709(cb, cr); |
363 |
|
|
} else { |
364 |
|
|
YUV_TO_RGB1_CCIR(cb, cr); |
365 |
|
|
} |
366 |
|
|
|
367 |
|
62 |
YUV_TO_RGB2_CCIR(r, g, b, y); |
368 |
|
|
|
369 |
|
|
ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha); |
370 |
|
|
|
371 |
|
|
/* Store color in palette */ |
372 |
|
62 |
palette->clut[color_id] = RGBA(r,g,b,alpha); |
373 |
|
|
} |
374 |
|
2 |
return 0; |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
/** |
378 |
|
|
* Parse the presentation segment packet. |
379 |
|
|
* |
380 |
|
|
* The presentation segment contains details on the video |
381 |
|
|
* width, video height, x & y subtitle position. |
382 |
|
|
* |
383 |
|
|
* @param avctx contains the current codec context |
384 |
|
|
* @param buf pointer to the packet to process |
385 |
|
|
* @param buf_size size of packet to process |
386 |
|
|
* @todo TODO: Implement cropping |
387 |
|
|
*/ |
388 |
|
3 |
static int parse_presentation_segment(AVCodecContext *avctx, |
389 |
|
|
const uint8_t *buf, int buf_size, |
390 |
|
|
int64_t pts) |
391 |
|
|
{ |
392 |
|
3 |
PGSSubContext *ctx = avctx->priv_data; |
393 |
|
|
int i, state, ret; |
394 |
|
3 |
const uint8_t *buf_end = buf + buf_size; |
395 |
|
|
|
396 |
|
|
// Video descriptor |
397 |
|
3 |
int w = bytestream_get_be16(&buf); |
398 |
|
3 |
int h = bytestream_get_be16(&buf); |
399 |
|
|
|
400 |
|
3 |
ctx->presentation.pts = pts; |
401 |
|
|
|
402 |
|
|
ff_dlog(avctx, "Video Dimensions %dx%d\n", |
403 |
|
|
w, h); |
404 |
|
3 |
ret = ff_set_dimensions(avctx, w, h); |
405 |
✗✓ |
3 |
if (ret < 0) |
406 |
|
|
return ret; |
407 |
|
|
|
408 |
|
|
/* Skip 1 bytes of unknown, frame rate */ |
409 |
|
3 |
buf++; |
410 |
|
|
|
411 |
|
|
// Composition descriptor |
412 |
|
3 |
ctx->presentation.id_number = bytestream_get_be16(&buf); |
413 |
|
|
/* |
414 |
|
|
* state is a 2 bit field that defines pgs epoch boundaries |
415 |
|
|
* 00 - Normal, previously defined objects and palettes are still valid |
416 |
|
|
* 01 - Acquisition point, previous objects and palettes can be released |
417 |
|
|
* 10 - Epoch start, previous objects and palettes can be released |
418 |
|
|
* 11 - Epoch continue, previous objects and palettes can be released |
419 |
|
|
* |
420 |
|
|
* reserved 6 bits discarded |
421 |
|
|
*/ |
422 |
|
3 |
state = bytestream_get_byte(&buf) >> 6; |
423 |
✓✗ |
3 |
if (state != 0) { |
424 |
|
3 |
flush_cache(avctx); |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
/* |
428 |
|
|
* skip palette_update_flag (0x80), |
429 |
|
|
*/ |
430 |
|
3 |
buf += 1; |
431 |
|
3 |
ctx->presentation.palette_id = bytestream_get_byte(&buf); |
432 |
|
3 |
ctx->presentation.object_count = bytestream_get_byte(&buf); |
433 |
✗✓ |
3 |
if (ctx->presentation.object_count > MAX_OBJECT_REFS) { |
434 |
|
|
av_log(avctx, AV_LOG_ERROR, |
435 |
|
|
"Invalid number of presentation objects %d\n", |
436 |
|
|
ctx->presentation.object_count); |
437 |
|
|
ctx->presentation.object_count = 2; |
438 |
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) { |
439 |
|
|
return AVERROR_INVALIDDATA; |
440 |
|
|
} |
441 |
|
|
} |
442 |
|
|
|
443 |
|
|
|
444 |
✓✓ |
9 |
for (i = 0; i < ctx->presentation.object_count; i++) |
445 |
|
|
{ |
446 |
|
|
|
447 |
✗✓ |
6 |
if (buf_end - buf < 8) { |
448 |
|
|
av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n"); |
449 |
|
|
ctx->presentation.object_count = i; |
450 |
|
|
return AVERROR_INVALIDDATA; |
451 |
|
|
} |
452 |
|
|
|
453 |
|
6 |
ctx->presentation.objects[i].id = bytestream_get_be16(&buf); |
454 |
|
6 |
ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf); |
455 |
|
6 |
ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf); |
456 |
|
|
|
457 |
|
6 |
ctx->presentation.objects[i].x = bytestream_get_be16(&buf); |
458 |
|
6 |
ctx->presentation.objects[i].y = bytestream_get_be16(&buf); |
459 |
|
|
|
460 |
|
|
// If cropping |
461 |
✗✓ |
6 |
if (ctx->presentation.objects[i].composition_flag & 0x80) { |
462 |
|
|
ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf); |
463 |
|
|
ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf); |
464 |
|
|
ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf); |
465 |
|
|
ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf); |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", |
469 |
|
|
ctx->presentation.objects[i].x, ctx->presentation.objects[i].y); |
470 |
|
|
|
471 |
✓✗ |
6 |
if (ctx->presentation.objects[i].x > avctx->width || |
472 |
✗✓ |
6 |
ctx->presentation.objects[i].y > avctx->height) { |
473 |
|
|
av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n", |
474 |
|
|
ctx->presentation.objects[i].x, |
475 |
|
|
ctx->presentation.objects[i].y, |
476 |
|
|
avctx->width, avctx->height); |
477 |
|
|
ctx->presentation.objects[i].x = 0; |
478 |
|
|
ctx->presentation.objects[i].y = 0; |
479 |
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) { |
480 |
|
|
return AVERROR_INVALIDDATA; |
481 |
|
|
} |
482 |
|
|
} |
483 |
|
|
} |
484 |
|
|
|
485 |
|
3 |
return 0; |
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
/** |
489 |
|
|
* Parse the display segment packet. |
490 |
|
|
* |
491 |
|
|
* The display segment controls the updating of the display. |
492 |
|
|
* |
493 |
|
|
* @param avctx contains the current codec context |
494 |
|
|
* @param data pointer to the data pertaining the subtitle to display |
495 |
|
|
* @param buf pointer to the packet to process |
496 |
|
|
* @param buf_size size of packet to process |
497 |
|
|
*/ |
498 |
|
1 |
static int display_end_segment(AVCodecContext *avctx, void *data, |
499 |
|
|
const uint8_t *buf, int buf_size) |
500 |
|
|
{ |
501 |
|
1 |
AVSubtitle *sub = data; |
502 |
|
1 |
PGSSubContext *ctx = avctx->priv_data; |
503 |
|
|
int64_t pts; |
504 |
|
|
PGSSubPalette *palette; |
505 |
|
|
int i, ret; |
506 |
|
|
|
507 |
✓✗ |
1 |
pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts; |
508 |
|
1 |
memset(sub, 0, sizeof(*sub)); |
509 |
|
1 |
sub->pts = pts; |
510 |
|
1 |
ctx->presentation.pts = AV_NOPTS_VALUE; |
511 |
|
1 |
sub->start_display_time = 0; |
512 |
|
|
// There is no explicit end time for PGS subtitles. The end time |
513 |
|
|
// is defined by the start of the next sub which may contain no |
514 |
|
|
// objects (i.e. clears the previous sub) |
515 |
|
1 |
sub->end_display_time = UINT32_MAX; |
516 |
|
1 |
sub->format = 0; |
517 |
|
|
|
518 |
|
|
// Blank if last object_count was 0. |
519 |
✗✓ |
1 |
if (!ctx->presentation.object_count) |
520 |
|
|
return 1; |
521 |
|
1 |
sub->rects = av_mallocz_array(ctx->presentation.object_count, sizeof(*sub->rects)); |
522 |
✗✓ |
1 |
if (!sub->rects) { |
523 |
|
|
return AVERROR(ENOMEM); |
524 |
|
|
} |
525 |
|
1 |
palette = find_palette(ctx->presentation.palette_id, &ctx->palettes); |
526 |
✗✓ |
1 |
if (!palette) { |
527 |
|
|
// Missing palette. Should only happen with damaged streams. |
528 |
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n", |
529 |
|
|
ctx->presentation.palette_id); |
530 |
|
|
avsubtitle_free(sub); |
531 |
|
|
return AVERROR_INVALIDDATA; |
532 |
|
|
} |
533 |
✓✓ |
3 |
for (i = 0; i < ctx->presentation.object_count; i++) { |
534 |
|
|
PGSSubObject *object; |
535 |
|
|
|
536 |
|
2 |
sub->rects[i] = av_mallocz(sizeof(*sub->rects[0])); |
537 |
✗✓ |
2 |
if (!sub->rects[i]) { |
538 |
|
|
avsubtitle_free(sub); |
539 |
|
|
return AVERROR(ENOMEM); |
540 |
|
|
} |
541 |
|
2 |
sub->num_rects++; |
542 |
|
2 |
sub->rects[i]->type = SUBTITLE_BITMAP; |
543 |
|
|
|
544 |
|
|
/* Process bitmap */ |
545 |
|
2 |
object = find_object(ctx->presentation.objects[i].id, &ctx->objects); |
546 |
✗✓ |
2 |
if (!object) { |
547 |
|
|
// Missing object. Should only happen with damaged streams. |
548 |
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n", |
549 |
|
|
ctx->presentation.objects[i].id); |
550 |
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) { |
551 |
|
|
avsubtitle_free(sub); |
552 |
|
|
return AVERROR_INVALIDDATA; |
553 |
|
|
} |
554 |
|
|
// Leaves rect empty with 0 width and height. |
555 |
|
|
continue; |
556 |
|
|
} |
557 |
✗✓ |
2 |
if (ctx->presentation.objects[i].composition_flag & 0x40) |
558 |
|
|
sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED; |
559 |
|
|
|
560 |
|
2 |
sub->rects[i]->x = ctx->presentation.objects[i].x; |
561 |
|
2 |
sub->rects[i]->y = ctx->presentation.objects[i].y; |
562 |
|
|
|
563 |
✓✗ |
2 |
if (object->rle) { |
564 |
|
2 |
sub->rects[i]->w = object->w; |
565 |
|
2 |
sub->rects[i]->h = object->h; |
566 |
|
|
|
567 |
|
2 |
sub->rects[i]->linesize[0] = object->w; |
568 |
|
|
|
569 |
✗✓ |
2 |
if (object->rle_remaining_len) { |
570 |
|
|
av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n", |
571 |
|
|
object->rle_data_len, object->rle_remaining_len); |
572 |
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) { |
573 |
|
|
avsubtitle_free(sub); |
574 |
|
|
return AVERROR_INVALIDDATA; |
575 |
|
|
} |
576 |
|
|
} |
577 |
|
2 |
ret = decode_rle(avctx, sub->rects[i], object->rle, object->rle_data_len); |
578 |
✗✓ |
2 |
if (ret < 0) { |
579 |
|
|
if ((avctx->err_recognition & AV_EF_EXPLODE) || |
580 |
|
|
ret == AVERROR(ENOMEM)) { |
581 |
|
|
avsubtitle_free(sub); |
582 |
|
|
return ret; |
583 |
|
|
} |
584 |
|
|
sub->rects[i]->w = 0; |
585 |
|
|
sub->rects[i]->h = 0; |
586 |
|
|
continue; |
587 |
|
|
} |
588 |
|
|
} |
589 |
|
|
/* Allocate memory for colors */ |
590 |
|
2 |
sub->rects[i]->nb_colors = 256; |
591 |
|
2 |
sub->rects[i]->data[1] = av_mallocz(AVPALETTE_SIZE); |
592 |
✗✓ |
2 |
if (!sub->rects[i]->data[1]) { |
593 |
|
|
avsubtitle_free(sub); |
594 |
|
|
return AVERROR(ENOMEM); |
595 |
|
|
} |
596 |
|
|
|
597 |
✗✓✗✗
|
2 |
if (!ctx->forced_subs_only || ctx->presentation.objects[i].composition_flag & 0x40) |
598 |
|
2 |
memcpy(sub->rects[i]->data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t)); |
599 |
|
|
|
600 |
|
|
#if FF_API_AVPICTURE |
601 |
|
|
FF_DISABLE_DEPRECATION_WARNINGS |
602 |
|
|
{ |
603 |
|
|
AVSubtitleRect *rect; |
604 |
|
|
int j; |
605 |
|
2 |
rect = sub->rects[i]; |
606 |
✓✓ |
10 |
for (j = 0; j < 4; j++) { |
607 |
|
8 |
rect->pict.data[j] = rect->data[j]; |
608 |
|
8 |
rect->pict.linesize[j] = rect->linesize[j]; |
609 |
|
|
} |
610 |
|
|
} |
611 |
|
|
FF_ENABLE_DEPRECATION_WARNINGS |
612 |
|
|
#endif |
613 |
|
|
} |
614 |
|
1 |
return 1; |
615 |
|
|
} |
616 |
|
|
|
617 |
|
11 |
static int decode(AVCodecContext *avctx, void *data, int *got_sub_ptr, |
618 |
|
|
AVPacket *avpkt) |
619 |
|
|
{ |
620 |
|
11 |
const uint8_t *buf = avpkt->data; |
621 |
|
11 |
int buf_size = avpkt->size; |
622 |
|
|
|
623 |
|
|
const uint8_t *buf_end; |
624 |
|
|
uint8_t segment_type; |
625 |
|
|
int segment_length; |
626 |
|
|
int i, ret; |
627 |
|
|
|
628 |
|
|
ff_dlog(avctx, "PGS sub packet:\n"); |
629 |
|
|
|
630 |
✓✓ |
49323 |
for (i = 0; i < buf_size; i++) { |
631 |
|
|
ff_dlog(avctx, "%02x ", buf[i]); |
632 |
|
49312 |
if (i % 16 == 15) |
633 |
|
|
ff_dlog(avctx, "\n"); |
634 |
|
|
} |
635 |
|
|
|
636 |
|
11 |
if (i & 15) |
637 |
|
|
ff_dlog(avctx, "\n"); |
638 |
|
|
|
639 |
|
11 |
*got_sub_ptr = 0; |
640 |
|
|
|
641 |
|
|
/* Ensure that we have received at a least a segment code and segment length */ |
642 |
✗✓ |
11 |
if (buf_size < 3) |
643 |
|
|
return -1; |
644 |
|
|
|
645 |
|
11 |
buf_end = buf + buf_size; |
646 |
|
|
|
647 |
|
|
/* Step through buffer to identify segments */ |
648 |
✓✓ |
22 |
while (buf < buf_end) { |
649 |
|
11 |
segment_type = bytestream_get_byte(&buf); |
650 |
|
11 |
segment_length = bytestream_get_be16(&buf); |
651 |
|
|
|
652 |
|
|
ff_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type); |
653 |
|
|
|
654 |
✓✓✗✓
|
11 |
if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf) |
655 |
|
|
break; |
656 |
|
|
|
657 |
|
11 |
ret = 0; |
658 |
✓✓✓✓ ✓✗ |
11 |
switch (segment_type) { |
659 |
|
2 |
case PALETTE_SEGMENT: |
660 |
|
2 |
ret = parse_palette_segment(avctx, buf, segment_length); |
661 |
|
2 |
break; |
662 |
|
3 |
case OBJECT_SEGMENT: |
663 |
|
3 |
ret = parse_object_segment(avctx, buf, segment_length); |
664 |
|
3 |
break; |
665 |
|
3 |
case PRESENTATION_SEGMENT: |
666 |
|
3 |
ret = parse_presentation_segment(avctx, buf, segment_length, ((AVSubtitle*)(data))->pts); |
667 |
|
3 |
break; |
668 |
|
2 |
case WINDOW_SEGMENT: |
669 |
|
|
/* |
670 |
|
|
* Window Segment Structure (No new information provided): |
671 |
|
|
* 2 bytes: Unknown, |
672 |
|
|
* 2 bytes: X position of subtitle, |
673 |
|
|
* 2 bytes: Y position of subtitle, |
674 |
|
|
* 2 bytes: Width of subtitle, |
675 |
|
|
* 2 bytes: Height of subtitle. |
676 |
|
|
*/ |
677 |
|
2 |
break; |
678 |
|
1 |
case DISPLAY_SEGMENT: |
679 |
✗✓ |
1 |
if (*got_sub_ptr) { |
680 |
|
|
av_log(avctx, AV_LOG_ERROR, "Duplicate display segment\n"); |
681 |
|
|
ret = AVERROR_INVALIDDATA; |
682 |
|
|
break; |
683 |
|
|
} |
684 |
|
1 |
ret = display_end_segment(avctx, data, buf, segment_length); |
685 |
✓✗ |
1 |
if (ret >= 0) |
686 |
|
1 |
*got_sub_ptr = ret; |
687 |
|
1 |
break; |
688 |
|
|
default: |
689 |
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n", |
690 |
|
|
segment_type, segment_length); |
691 |
|
|
ret = AVERROR_INVALIDDATA; |
692 |
|
|
break; |
693 |
|
|
} |
694 |
✗✓✗✗
|
11 |
if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) { |
695 |
|
|
avsubtitle_free(data); |
696 |
|
|
*got_sub_ptr = 0; |
697 |
|
|
return ret; |
698 |
|
|
} |
699 |
|
|
|
700 |
|
11 |
buf += segment_length; |
701 |
|
|
} |
702 |
|
|
|
703 |
|
11 |
return buf_size; |
704 |
|
|
} |
705 |
|
|
|
706 |
|
|
#define OFFSET(x) offsetof(PGSSubContext, x) |
707 |
|
|
#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM |
708 |
|
|
static const AVOption options[] = { |
709 |
|
|
{"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD}, |
710 |
|
|
{ NULL }, |
711 |
|
|
}; |
712 |
|
|
|
713 |
|
|
static const AVClass pgsdec_class = { |
714 |
|
|
.class_name = "PGS subtitle decoder", |
715 |
|
|
.item_name = av_default_item_name, |
716 |
|
|
.option = options, |
717 |
|
|
.version = LIBAVUTIL_VERSION_INT, |
718 |
|
|
}; |
719 |
|
|
|
720 |
|
|
AVCodec ff_pgssub_decoder = { |
721 |
|
|
.name = "pgssub", |
722 |
|
|
.long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"), |
723 |
|
|
.type = AVMEDIA_TYPE_SUBTITLE, |
724 |
|
|
.id = AV_CODEC_ID_HDMV_PGS_SUBTITLE, |
725 |
|
|
.priv_data_size = sizeof(PGSSubContext), |
726 |
|
|
.init = init_decoder, |
727 |
|
|
.close = close_decoder, |
728 |
|
|
.decode = decode, |
729 |
|
|
.priv_class = &pgsdec_class, |
730 |
|
|
}; |