• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/pgssubdec.c

Go to the documentation of this file.
00001 /*
00002  * PGS subtitle decoder
00003  * Copyright (c) 2009 Stephen Backway
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "bytestream.h"
00030 #include "libavutil/colorspace.h"
00031 #include "libavutil/imgutils.h"
00032 
00033 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
00034 
00035 enum SegmentType {
00036     PALETTE_SEGMENT      = 0x14,
00037     PICTURE_SEGMENT      = 0x15,
00038     PRESENTATION_SEGMENT = 0x16,
00039     WINDOW_SEGMENT       = 0x17,
00040     DISPLAY_SEGMENT      = 0x80,
00041 };
00042 
00043 typedef struct PGSSubPresentation {
00044     int x;
00045     int y;
00046     int id_number;
00047     int object_number;
00048 } PGSSubPresentation;
00049 
00050 typedef struct PGSSubPicture {
00051     int          w;
00052     int          h;
00053     uint8_t      *rle;
00054     unsigned int rle_buffer_size, rle_data_len;
00055     unsigned int rle_remaining_len;
00056 } PGSSubPicture;
00057 
00058 typedef struct PGSSubContext {
00059     PGSSubPresentation presentation;
00060     uint32_t           clut[256];
00061     PGSSubPicture      picture;
00062 } PGSSubContext;
00063 
00064 static av_cold int init_decoder(AVCodecContext *avctx)
00065 {
00066     avctx->pix_fmt = PIX_FMT_PAL8;
00067 
00068     return 0;
00069 }
00070 
00071 static av_cold int close_decoder(AVCodecContext *avctx)
00072 {
00073     PGSSubContext *ctx = avctx->priv_data;
00074 
00075     av_freep(&ctx->picture.rle);
00076     ctx->picture.rle_buffer_size  = 0;
00077 
00078     return 0;
00079 }
00080 
00091 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
00092                       const uint8_t *buf, unsigned int buf_size)
00093 {
00094     const uint8_t *rle_bitmap_end;
00095     int pixel_count, line_count;
00096 
00097     rle_bitmap_end = buf + buf_size;
00098 
00099     sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
00100 
00101     if (!sub->rects[0]->pict.data[0])
00102         return -1;
00103 
00104     pixel_count = 0;
00105     line_count  = 0;
00106 
00107     while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
00108         uint8_t flags, color;
00109         int run;
00110 
00111         color = bytestream_get_byte(&buf);
00112         run   = 1;
00113 
00114         if (color == 0x00) {
00115             flags = bytestream_get_byte(&buf);
00116             run   = flags & 0x3f;
00117             if (flags & 0x40)
00118                 run = (run << 8) + bytestream_get_byte(&buf);
00119             color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
00120         }
00121 
00122         if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
00123             memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
00124             pixel_count += run;
00125         } else if (!run) {
00126             /*
00127              * New Line. Check if correct pixels decoded, if not display warning
00128              * and adjust bitmap pointer to correct new line position.
00129              */
00130             if (pixel_count % sub->rects[0]->w > 0)
00131                 av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
00132                        pixel_count % sub->rects[0]->w, sub->rects[0]->w);
00133             line_count++;
00134         }
00135     }
00136 
00137     if (pixel_count < sub->rects[0]->w * sub->rects[0]->h) {
00138         av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
00139         return -1;
00140     }
00141 
00142     av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
00143 
00144     return 0;
00145 }
00146 
00158 static int parse_picture_segment(AVCodecContext *avctx,
00159                                   const uint8_t *buf, int buf_size)
00160 {
00161     PGSSubContext *ctx = avctx->priv_data;
00162 
00163     uint8_t sequence_desc;
00164     unsigned int rle_bitmap_len, width, height;
00165 
00166     if (buf_size <= 4)
00167         return -1;
00168     buf_size -= 4;
00169 
00170     /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
00171     buf += 3;
00172 
00173     /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
00174     sequence_desc = bytestream_get_byte(&buf);
00175 
00176     if (!(sequence_desc & 0x80)) {
00177         /* Additional RLE data */
00178         if (buf_size > ctx->picture.rle_remaining_len)
00179             return -1;
00180 
00181         memcpy(ctx->picture.rle + ctx->picture.rle_data_len, buf, buf_size);
00182         ctx->picture.rle_data_len += buf_size;
00183         ctx->picture.rle_remaining_len -= buf_size;
00184 
00185         return 0;
00186     }
00187 
00188     if (buf_size <= 7)
00189         return -1;
00190     buf_size -= 7;
00191 
00192     /* Decode rle bitmap length, stored size includes width/height data */
00193     rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
00194 
00195     /* Get bitmap dimensions from data */
00196     width  = bytestream_get_be16(&buf);
00197     height = bytestream_get_be16(&buf);
00198 
00199     /* Make sure the bitmap is not too large */
00200     if (avctx->width < width || avctx->height < height) {
00201         av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
00202         return -1;
00203     }
00204 
00205     ctx->picture.w = width;
00206     ctx->picture.h = height;
00207 
00208     av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
00209 
00210     if (!ctx->picture.rle)
00211         return -1;
00212 
00213     memcpy(ctx->picture.rle, buf, buf_size);
00214     ctx->picture.rle_data_len = buf_size;
00215     ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
00216 
00217     return 0;
00218 }
00219 
00230 static void parse_palette_segment(AVCodecContext *avctx,
00231                                   const uint8_t *buf, int buf_size)
00232 {
00233     PGSSubContext *ctx = avctx->priv_data;
00234 
00235     const uint8_t *buf_end = buf + buf_size;
00236     const uint8_t *cm      = ff_cropTbl + MAX_NEG_CROP;
00237     int color_id;
00238     int y, cb, cr, alpha;
00239     int r, g, b, r_add, g_add, b_add;
00240 
00241     /* Skip two null bytes */
00242     buf += 2;
00243 
00244     while (buf < buf_end) {
00245         color_id  = bytestream_get_byte(&buf);
00246         y         = bytestream_get_byte(&buf);
00247         cr        = bytestream_get_byte(&buf);
00248         cb        = bytestream_get_byte(&buf);
00249         alpha     = bytestream_get_byte(&buf);
00250 
00251         YUV_TO_RGB1(cb, cr);
00252         YUV_TO_RGB2(r, g, b, y);
00253 
00254         av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
00255 
00256         /* Store color in palette */
00257         ctx->clut[color_id] = RGBA(r,g,b,alpha);
00258     }
00259 }
00260 
00273 static void parse_presentation_segment(AVCodecContext *avctx,
00274                                        const uint8_t *buf, int buf_size)
00275 {
00276     PGSSubContext *ctx = avctx->priv_data;
00277 
00278     int x, y;
00279 
00280     int w = bytestream_get_be16(&buf);
00281     int h = bytestream_get_be16(&buf);
00282 
00283     av_dlog(avctx, "Video Dimensions %dx%d\n",
00284             w, h);
00285     if (av_image_check_size(w, h, 0, avctx) >= 0)
00286         avcodec_set_dimensions(avctx, w, h);
00287 
00288     /* Skip 1 bytes of unknown, frame rate? */
00289     buf++;
00290 
00291     ctx->presentation.id_number = bytestream_get_be16(&buf);
00292 
00293     /*
00294      * Skip 3 bytes of unknown:
00295      *     state
00296      *     palette_update_flag (0x80),
00297      *     palette_id_to_use,
00298      */
00299     buf += 3;
00300 
00301     ctx->presentation.object_number = bytestream_get_byte(&buf);
00302     if (!ctx->presentation.object_number)
00303         return;
00304 
00305     /*
00306      * Skip 4 bytes of unknown:
00307      *     object_id_ref (2 bytes),
00308      *     window_id_ref,
00309      *     composition_flag (0x80 - object cropped, 0x40 - object forced)
00310      */
00311     buf += 4;
00312 
00313     x = bytestream_get_be16(&buf);
00314     y = bytestream_get_be16(&buf);
00315 
00316     /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
00317 
00318     av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
00319 
00320     if (x > avctx->width || y > avctx->height) {
00321         av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
00322                x, y, avctx->width, avctx->height);
00323         x = 0; y = 0;
00324     }
00325 
00326     /* Fill in dimensions */
00327     ctx->presentation.x = x;
00328     ctx->presentation.y = y;
00329 }
00330 
00346 static int display_end_segment(AVCodecContext *avctx, void *data,
00347                                const uint8_t *buf, int buf_size)
00348 {
00349     AVSubtitle    *sub = data;
00350     PGSSubContext *ctx = avctx->priv_data;
00351 
00352     /*
00353      *      The end display time is a timeout value and is only reached
00354      *      if the next subtitle is later then timeout or subtitle has
00355      *      not been cleared by a subsequent empty display command.
00356      */
00357 
00358     memset(sub, 0, sizeof(*sub));
00359     // Blank if last object_number was 0.
00360     // Note that this may be wrong for more complex subtitles.
00361     if (!ctx->presentation.object_number)
00362         return 1;
00363     sub->start_display_time = 0;
00364     sub->end_display_time   = 20000;
00365     sub->format             = 0;
00366 
00367     sub->rects     = av_mallocz(sizeof(*sub->rects));
00368     sub->rects[0]  = av_mallocz(sizeof(*sub->rects[0]));
00369     sub->num_rects = 1;
00370 
00371     sub->rects[0]->x    = ctx->presentation.x;
00372     sub->rects[0]->y    = ctx->presentation.y;
00373     sub->rects[0]->w    = ctx->picture.w;
00374     sub->rects[0]->h    = ctx->picture.h;
00375     sub->rects[0]->type = SUBTITLE_BITMAP;
00376 
00377     /* Process bitmap */
00378     sub->rects[0]->pict.linesize[0] = ctx->picture.w;
00379 
00380     if (ctx->picture.rle) {
00381         if (ctx->picture.rle_remaining_len)
00382             av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
00383                    ctx->picture.rle_data_len, ctx->picture.rle_remaining_len);
00384         if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
00385             return 0;
00386     }
00387     /* Allocate memory for colors */
00388     sub->rects[0]->nb_colors    = 256;
00389     sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00390 
00391     memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
00392 
00393     return 1;
00394 }
00395 
00396 static int decode(AVCodecContext *avctx, void *data, int *data_size,
00397                   AVPacket *avpkt)
00398 {
00399     const uint8_t *buf = avpkt->data;
00400     int buf_size       = avpkt->size;
00401 
00402     const uint8_t *buf_end;
00403     uint8_t       segment_type;
00404     int           segment_length;
00405     int i;
00406 
00407     av_dlog(avctx, "PGS sub packet:\n");
00408 
00409     for (i = 0; i < buf_size; i++) {
00410         av_dlog(avctx, "%02x ", buf[i]);
00411         if (i % 16 == 15)
00412             av_dlog(avctx, "\n");
00413     }
00414 
00415     if (i & 15)
00416         av_dlog(avctx, "\n");
00417 
00418     *data_size = 0;
00419 
00420     /* Ensure that we have received at a least a segment code and segment length */
00421     if (buf_size < 3)
00422         return -1;
00423 
00424     buf_end = buf + buf_size;
00425 
00426     /* Step through buffer to identify segments */
00427     while (buf < buf_end) {
00428         segment_type   = bytestream_get_byte(&buf);
00429         segment_length = bytestream_get_be16(&buf);
00430 
00431         av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
00432 
00433         if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
00434             break;
00435 
00436         switch (segment_type) {
00437         case PALETTE_SEGMENT:
00438             parse_palette_segment(avctx, buf, segment_length);
00439             break;
00440         case PICTURE_SEGMENT:
00441             parse_picture_segment(avctx, buf, segment_length);
00442             break;
00443         case PRESENTATION_SEGMENT:
00444             parse_presentation_segment(avctx, buf, segment_length);
00445             break;
00446         case WINDOW_SEGMENT:
00447             /*
00448              * Window Segment Structure (No new information provided):
00449              *     2 bytes: Unkown,
00450              *     2 bytes: X position of subtitle,
00451              *     2 bytes: Y position of subtitle,
00452              *     2 bytes: Width of subtitle,
00453              *     2 bytes: Height of subtitle.
00454              */
00455             break;
00456         case DISPLAY_SEGMENT:
00457             *data_size = display_end_segment(avctx, data, buf, segment_length);
00458             break;
00459         default:
00460             av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
00461                    segment_type, segment_length);
00462             break;
00463         }
00464 
00465         buf += segment_length;
00466     }
00467 
00468     return buf_size;
00469 }
00470 
00471 AVCodec ff_pgssub_decoder = {
00472     .name           = "pgssub",
00473     .type           = AVMEDIA_TYPE_SUBTITLE,
00474     .id             = CODEC_ID_HDMV_PGS_SUBTITLE,
00475     .priv_data_size = sizeof(PGSSubContext),
00476     .init           = init_decoder,
00477     .close          = close_decoder,
00478     .decode         = decode,
00479     .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
00480 };
Generated on Thu Jan 24 2013 17:08:53 for Libav by doxygen 1.7.1