Mercurial > libavcodec.hg
annotate pgssubdec.c @ 12404:86f5aff0f8e8 libavcodec
removed an unnecessary blank line
author | bindhammer |
---|---|
date | Mon, 23 Aug 2010 13:19:48 +0000 |
parents | d46c4c3204b8 |
children | ffb3668ff7af |
rev | line source |
---|---|
10083 | 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 /** | |
11644
7dd2a45249a9
Remove explicit filename from Doxygen @file commands.
diego
parents:
11560
diff
changeset
|
23 * @file |
10083 | 24 * PGS subtitle decoder |
25 */ | |
26 | |
27 #include "avcodec.h" | |
28 #include "dsputil.h" | |
29 #include "bytestream.h" | |
12039 | 30 #include "libavutil/colorspace.h" |
12394
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
31 #include "libavcore/imgutils.h" |
10083 | 32 |
33 //#define DEBUG_PACKET_CONTENTS | |
34 | |
35 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)) | |
36 | |
37 enum SegmentType { | |
38 PALETTE_SEGMENT = 0x14, | |
39 PICTURE_SEGMENT = 0x15, | |
40 PRESENTATION_SEGMENT = 0x16, | |
41 WINDOW_SEGMENT = 0x17, | |
42 DISPLAY_SEGMENT = 0x80, | |
43 }; | |
44 | |
45 typedef struct PGSSubPresentation { | |
46 int x; | |
47 int y; | |
48 int id_number; | |
49 } PGSSubPresentation; | |
50 | |
51 typedef struct PGSSubPicture { | |
52 int w; | |
53 int h; | |
54 uint8_t *rle; | |
55 unsigned int rle_buffer_size, rle_data_len; | |
56 } PGSSubPicture; | |
57 | |
58 typedef struct PGSSubContext { | |
59 PGSSubPresentation presentation; | |
60 uint32_t clut[256]; | |
61 PGSSubPicture picture; | |
62 } PGSSubContext; | |
63 | |
64 static av_cold int init_decoder(AVCodecContext *avctx) | |
65 { | |
12136
31bca176d2d9
Set pix_fmt to the correct value for the format the PGS decoder actually uses.
reimar
parents:
12039
diff
changeset
|
66 avctx->pix_fmt = PIX_FMT_PAL8; |
10083 | 67 |
68 return 0; | |
69 } | |
70 | |
71 static av_cold int close_decoder(AVCodecContext *avctx) | |
72 { | |
73 PGSSubContext *ctx = avctx->priv_data; | |
74 | |
75 av_freep(&ctx->picture.rle); | |
76 ctx->picture.rle_buffer_size = 0; | |
77 | |
78 return 0; | |
79 } | |
80 | |
81 /** | |
12024 | 82 * Decode the RLE data. |
10083 | 83 * |
84 * The subtitle is stored as an Run Length Encoded image. | |
85 * | |
86 * @param avctx contains the current codec context | |
87 * @param sub pointer to the processed subtitle data | |
88 * @param buf pointer to the RLE data to process | |
89 * @param buf_size size of the RLE data to process | |
90 */ | |
91 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub, | |
92 const uint8_t *buf, unsigned int buf_size) | |
93 { | |
94 const uint8_t *rle_bitmap_end; | |
95 int pixel_count, line_count; | |
96 | |
97 rle_bitmap_end = buf + buf_size; | |
98 | |
99 sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h); | |
100 | |
101 if (!sub->rects[0]->pict.data[0]) | |
102 return -1; | |
103 | |
104 pixel_count = 0; | |
105 line_count = 0; | |
106 | |
107 while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) { | |
108 uint8_t flags, color; | |
109 int run; | |
110 | |
111 color = bytestream_get_byte(&buf); | |
112 run = 1; | |
113 | |
114 if (color == 0x00) { | |
115 flags = bytestream_get_byte(&buf); | |
116 run = flags & 0x3f; | |
117 if (flags & 0x40) | |
118 run = (run << 8) + bytestream_get_byte(&buf); | |
119 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0; | |
120 } | |
121 | |
122 if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) { | |
123 memset(sub->rects[0]->pict.data[0] + pixel_count, color, run); | |
124 pixel_count += run; | |
125 } else if (!run) { | |
126 /* | |
127 * New Line. Check if correct pixels decoded, if not display warning | |
128 * and adjust bitmap pointer to correct new line position. | |
129 */ | |
130 if (pixel_count % sub->rects[0]->w > 0) | |
131 av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n", | |
132 pixel_count % sub->rects[0]->w, sub->rects[0]->w); | |
133 line_count++; | |
134 } | |
135 } | |
136 | |
137 dprintf(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h); | |
138 | |
139 return 0; | |
140 } | |
141 | |
142 /** | |
12024 | 143 * Parse the picture segment packet. |
10083 | 144 * |
145 * The picture segment contains details on the sequence id, | |
146 * width, height and Run Length Encoded (RLE) bitmap data. | |
147 * | |
148 * @param avctx contains the current codec context | |
149 * @param buf pointer to the packet to process | |
150 * @param buf_size size of packet to process | |
151 * @todo TODO: Enable support for RLE data over multiple packets | |
152 */ | |
153 static int parse_picture_segment(AVCodecContext *avctx, | |
154 const uint8_t *buf, int buf_size) | |
155 { | |
156 PGSSubContext *ctx = avctx->priv_data; | |
157 | |
158 uint8_t sequence_desc; | |
159 unsigned int rle_bitmap_len, width, height; | |
160 | |
161 /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */ | |
162 buf += 3; | |
163 | |
164 /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */ | |
165 sequence_desc = bytestream_get_byte(&buf); | |
166 | |
167 if (!(sequence_desc & 0x80)) { | |
168 av_log(avctx, AV_LOG_ERROR, "Decoder does not support object data over multiple packets.\n"); | |
169 return -1; | |
170 } | |
171 | |
172 /* Decode rle bitmap length */ | |
173 rle_bitmap_len = bytestream_get_be24(&buf); | |
174 | |
175 /* Check to ensure we have enough data for rle_bitmap_length if just a single packet */ | |
176 if (rle_bitmap_len > buf_size - 7) { | |
177 av_log(avctx, AV_LOG_ERROR, "Not enough RLE data for specified length of %d.\n", rle_bitmap_len); | |
178 return -1; | |
179 } | |
180 | |
181 ctx->picture.rle_data_len = rle_bitmap_len; | |
182 | |
183 /* Get bitmap dimensions from data */ | |
184 width = bytestream_get_be16(&buf); | |
185 height = bytestream_get_be16(&buf); | |
186 | |
187 /* Make sure the bitmap is not too large */ | |
12394
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
188 if (avctx->width < width || avctx->height < height) { |
10083 | 189 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger then video.\n"); |
190 return -1; | |
191 } | |
192 | |
193 ctx->picture.w = width; | |
194 ctx->picture.h = height; | |
195 | |
196 av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len); | |
197 | |
198 if (!ctx->picture.rle) | |
199 return -1; | |
200 | |
201 memcpy(ctx->picture.rle, buf, rle_bitmap_len); | |
202 | |
203 return 0; | |
204 } | |
205 | |
206 /** | |
12024 | 207 * Parse the palette segment packet. |
10083 | 208 * |
209 * The palette segment contains details of the palette, | |
210 * a maximum of 256 colors can be defined. | |
211 * | |
212 * @param avctx contains the current codec context | |
213 * @param buf pointer to the packet to process | |
214 * @param buf_size size of packet to process | |
215 */ | |
216 static void parse_palette_segment(AVCodecContext *avctx, | |
217 const uint8_t *buf, int buf_size) | |
218 { | |
219 PGSSubContext *ctx = avctx->priv_data; | |
220 | |
221 const uint8_t *buf_end = buf + buf_size; | |
222 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; | |
223 int color_id; | |
224 int y, cb, cr, alpha; | |
225 int r, g, b, r_add, g_add, b_add; | |
226 | |
227 /* Skip two null bytes */ | |
228 buf += 2; | |
229 | |
230 while (buf < buf_end) { | |
231 color_id = bytestream_get_byte(&buf); | |
232 y = bytestream_get_byte(&buf); | |
233 cb = bytestream_get_byte(&buf); | |
234 cr = bytestream_get_byte(&buf); | |
235 alpha = bytestream_get_byte(&buf); | |
236 | |
237 YUV_TO_RGB1(cb, cr); | |
238 YUV_TO_RGB2(r, g, b, y); | |
239 | |
240 dprintf(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha); | |
241 | |
242 /* Store color in palette */ | |
243 ctx->clut[color_id] = RGBA(r,g,b,alpha); | |
244 } | |
245 } | |
246 | |
247 /** | |
12024 | 248 * Parse the presentation segment packet. |
10083 | 249 * |
250 * The presentation segment contains details on the video | |
251 * width, video height, x & y subtitle position. | |
252 * | |
253 * @param avctx contains the current codec context | |
254 * @param buf pointer to the packet to process | |
255 * @param buf_size size of packet to process | |
256 * @todo TODO: Implement cropping | |
257 * @todo TODO: Implement forcing of subtitles | |
258 * @todo TODO: Blanking of subtitle | |
259 */ | |
260 static void parse_presentation_segment(AVCodecContext *avctx, | |
261 const uint8_t *buf, int buf_size) | |
262 { | |
263 PGSSubContext *ctx = avctx->priv_data; | |
264 | |
265 int x, y; | |
266 uint8_t block; | |
267 | |
12394
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
268 int w = bytestream_get_be16(&buf); |
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
269 int h = bytestream_get_be16(&buf); |
10083 | 270 |
271 dprintf(avctx, "Video Dimensions %dx%d\n", | |
12394
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
272 w, h); |
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
273 if (av_check_image_size(w, h, 0, avctx) >= 0) |
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
274 avcodec_set_dimensions(avctx, w, h); |
10083 | 275 |
276 /* Skip 1 bytes of unknown, frame rate? */ | |
277 buf++; | |
278 | |
279 ctx->presentation.id_number = bytestream_get_be16(&buf); | |
280 | |
281 /* Next byte is the state. */ | |
282 block = bytestream_get_byte(&buf);; | |
283 if (block == 0x80) { | |
284 /* | |
285 * Skip 7 bytes of unknown: | |
286 * palette_update_flag (0x80), | |
287 * palette_id_to_use, | |
288 * Object Number (if > 0 determines if more data to process), | |
289 * object_id_ref (2 bytes), | |
290 * window_id_ref, | |
291 * composition_flag (0x80 - object cropped, 0x40 - object forced) | |
292 */ | |
293 buf += 7; | |
294 | |
295 x = bytestream_get_be16(&buf); | |
296 y = bytestream_get_be16(&buf); | |
297 | |
298 /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/ | |
299 | |
300 dprintf(avctx, "Subtitle Placement x=%d, y=%d\n", x, y); | |
301 | |
12394
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
302 if (x > avctx->width || y > avctx->height) { |
10083 | 303 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n", |
12394
d46c4c3204b8
Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents:
12136
diff
changeset
|
304 x, y, avctx->width, avctx->height); |
10083 | 305 x = 0; y = 0; |
306 } | |
307 | |
308 /* Fill in dimensions */ | |
309 ctx->presentation.x = x; | |
310 ctx->presentation.y = y; | |
311 } else if (block == 0x00) { | |
312 /* TODO: Blank context as subtitle should not be displayed. | |
313 * If the subtitle is blanked now the subtitle is not | |
314 * on screen long enough to read, due to a delay in | |
315 * initial display timing. | |
316 */ | |
317 } | |
318 } | |
319 | |
320 /** | |
12024 | 321 * Parse the display segment packet. |
10083 | 322 * |
323 * The display segment controls the updating of the display. | |
324 * | |
325 * @param avctx contains the current codec context | |
326 * @param data pointer to the data pertaining the subtitle to display | |
327 * @param buf pointer to the packet to process | |
328 * @param buf_size size of packet to process | |
329 * @todo TODO: Fix start time, relies on correct PTS, currently too late | |
330 * | |
331 * @todo TODO: Fix end time, normally cleared by a second display | |
332 * @todo segment, which is currently ignored as it clears | |
333 * @todo the subtitle too early. | |
334 */ | |
335 static int display_end_segment(AVCodecContext *avctx, void *data, | |
336 const uint8_t *buf, int buf_size) | |
337 { | |
338 AVSubtitle *sub = data; | |
339 PGSSubContext *ctx = avctx->priv_data; | |
340 | |
341 /* | |
342 * The end display time is a timeout value and is only reached | |
343 * if the next subtitle is later then timeout or subtitle has | |
344 * not been cleared by a subsequent empty display command. | |
345 */ | |
346 | |
10087 | 347 memset(sub, 0, sizeof(*sub)); |
10083 | 348 sub->start_display_time = 0; |
349 sub->end_display_time = 20000; | |
350 sub->format = 0; | |
351 | |
10089 | 352 sub->rects = av_mallocz(sizeof(*sub->rects)); |
353 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0])); | |
354 sub->num_rects = 1; | |
10083 | 355 |
356 sub->rects[0]->x = ctx->presentation.x; | |
357 sub->rects[0]->y = ctx->presentation.y; | |
358 sub->rects[0]->w = ctx->picture.w; | |
359 sub->rects[0]->h = ctx->picture.h; | |
360 sub->rects[0]->type = SUBTITLE_BITMAP; | |
361 | |
362 /* Process bitmap */ | |
363 sub->rects[0]->pict.linesize[0] = ctx->picture.w; | |
364 | |
365 if (ctx->picture.rle) | |
366 if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0) | |
367 return 0; | |
368 | |
369 /* Allocate memory for colors */ | |
370 sub->rects[0]->nb_colors = 256; | |
10092
e85fbfe709d8
Always allocate a buffer of AVPALETTE_SIZE for palette in the
cehoyos
parents:
10089
diff
changeset
|
371 sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE); |
10083 | 372 |
373 memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t)); | |
374 | |
375 return 1; | |
376 } | |
377 | |
378 static int decode(AVCodecContext *avctx, void *data, int *data_size, | |
379 AVPacket *avpkt) | |
380 { | |
381 const uint8_t *buf = avpkt->data; | |
382 int buf_size = avpkt->size; | |
383 | |
384 const uint8_t *buf_end; | |
385 uint8_t segment_type; | |
386 int segment_length; | |
387 | |
388 #ifdef DEBUG_PACKET_CONTENTS | |
389 int i; | |
390 | |
391 av_log(avctx, AV_LOG_INFO, "PGS sub packet:\n"); | |
392 | |
393 for (i = 0; i < buf_size; i++) { | |
394 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]); | |
395 if (i % 16 == 15) | |
396 av_log(avctx, AV_LOG_INFO, "\n"); | |
397 } | |
398 | |
399 if (i & 15) | |
400 av_log(avctx, AV_LOG_INFO, "\n"); | |
401 #endif | |
402 | |
403 *data_size = 0; | |
404 | |
405 /* Ensure that we have received at a least a segment code and segment length */ | |
406 if (buf_size < 3) | |
407 return -1; | |
408 | |
409 buf_end = buf + buf_size; | |
410 | |
411 /* Step through buffer to identify segments */ | |
412 while (buf < buf_end) { | |
413 segment_type = bytestream_get_byte(&buf); | |
414 segment_length = bytestream_get_be16(&buf); | |
415 | |
416 dprintf(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type); | |
417 | |
418 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf) | |
419 break; | |
420 | |
421 switch (segment_type) { | |
422 case PALETTE_SEGMENT: | |
423 parse_palette_segment(avctx, buf, segment_length); | |
424 break; | |
425 case PICTURE_SEGMENT: | |
426 parse_picture_segment(avctx, buf, segment_length); | |
427 break; | |
428 case PRESENTATION_SEGMENT: | |
429 parse_presentation_segment(avctx, buf, segment_length); | |
430 break; | |
431 case WINDOW_SEGMENT: | |
432 /* | |
433 * Window Segment Structure (No new information provided): | |
434 * 2 bytes: Unkown, | |
435 * 2 bytes: X position of subtitle, | |
436 * 2 bytes: Y position of subtitle, | |
437 * 2 bytes: Width of subtitle, | |
438 * 2 bytes: Height of subtitle. | |
439 */ | |
440 break; | |
441 case DISPLAY_SEGMENT: | |
442 *data_size = display_end_segment(avctx, data, buf, segment_length); | |
443 break; | |
444 default: | |
445 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n", | |
446 segment_type, segment_length); | |
447 break; | |
448 } | |
449 | |
450 buf += segment_length; | |
451 } | |
452 | |
453 return buf_size; | |
454 } | |
455 | |
456 AVCodec pgssub_decoder = { | |
457 "pgssub", | |
11560
8a4984c5cacc
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
10092
diff
changeset
|
458 AVMEDIA_TYPE_SUBTITLE, |
10083 | 459 CODEC_ID_HDMV_PGS_SUBTITLE, |
460 sizeof(PGSSubContext), | |
461 init_decoder, | |
462 NULL, | |
463 close_decoder, | |
464 decode, | |
465 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"), | |
466 }; |