OSDN Git Service

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