[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mplayer-users
Subject:    [MPlayer-users] Fw: [PATCH] Bluray Subtitle Support,
From:       Dâniel Fraga <fragabr () gmail ! com>
Date:       2009-08-10 16:40:03
Message-ID: 84v5l6-ctl.ln1 () tux ! abusar ! org ! br
[Download RAW message or body]

Is there any MPlayer developer interested in porting this
ffmpeg blu-ray subtitle support to MPlayer?

On Mon, 2009-08-10 at 11:31 +0200, Reimar Döffinger wrote:
> On Mon, Aug 03, 2009 at 07:37:11PM +1000, stev391@exemail.com.au wrote:
> > > Also you should maybe use av_fast_malloc
> > Not sure if this is a good idea, as the subtitle size is constantly
> > fluctuating, what happens if I specify a smaller buffer then already
> > provided and then the next round increase it to larger then ever
> > provided? Will the the little bit that was not used be released properly
> > (i.e not leak) along with the specified buffer length (old width * old
> > height)?
> 
> Not sure what exactly you are asking.
> It will keep allocated the largest size it ever had, though if really
> important to someone this behaviour could be changed in one single
> place.
> Also it might not be that important since on the other hand it reduces
> memory wasted due to heap fragmentation.
> Even realloc might be better than free+malloc, except that it might
> uselessly copy around the old content in some cases...

I agreed with the idea, but did not understand the implementation,
however after a little poking around in the code, I think I have worked
it out.

My question was: The 'size' variable I have to pass av_fast_malloc, is
this the previous utilised size of buffer (subtitles width * height), or
is this the maximum size of the allocated memory (for all occurrences in
the past since I initialise the decoder)? And do I have to update the
'size' variable myself?

However the answer seems to be:
Add a variable to the context to keep track of the size of buffer and
initialise to 0:

unsigned int bitmap_size;

bitmap_size = 0;

Then when allocating just call:
av_fast_malloc(&ctx->picture->bitmap, &ctx->picture->bitmap_size, width
* height);

and it will update the bitmap_size variable with the actual size of the
buffer which is always equal or exceeding width * height, unless it
fails:

if (!ctx->picture->bitmap)
    return AVERROR(ENOMEM);


Is this summary correct?
(Patch attached with these changes, also cosmetics changes to mpegts.c
after subtitle patch applied [no change to previous cosmetics])

Sorry for the slight confusion.

Thanks,
Stephen.





-- 
Linux 2.6.31-rc5: Man-Eating Seals of Antiquity

[Attachment #3 (text/x-patch)]

Index: Changelog
===================================================================
--- Changelog	(revision 19613)
+++ Changelog	(working copy)
@@ -31,6 +31,7 @@
 - RTP depacketization of ASF and RTSP from WMS servers
 - RTMP support in libavformat
 - noX handling for OPT_BOOL X options
+- Bluray (PGS) subtitle decoder
 
 
 
Index: libavcodec/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c	(revision 19613)
+++ libavcodec/allcodecs.c	(working copy)
@@ -300,6 +300,7 @@
     /* subtitles */
     REGISTER_ENCDEC  (DVBSUB, dvbsub);
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
+    REGISTER_DECODER (PGSSUB, pgssub);
     REGISTER_ENCDEC  (XSUB, xsub);
 
     /* external libraries */
Index: libavcodec/avcodec.h
===================================================================
--- libavcodec/avcodec.h	(revision 19613)
+++ libavcodec/avcodec.h	(working copy)
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVCODEC_VERSION_MAJOR 52
-#define LIBAVCODEC_VERSION_MINOR 32
+#define LIBAVCODEC_VERSION_MINOR 33
 #define LIBAVCODEC_VERSION_MICRO  0
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
@@ -327,6 +327,7 @@
     CODEC_ID_XSUB,
     CODEC_ID_SSA,
     CODEC_ID_MOV_TEXT,
+    CODEC_ID_HDMV_PGS_SUBTITLE,
 
     /* other specific kind of codecs (generally used for attachments) */
     CODEC_ID_TTF= 0x18000,
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile	(revision 19613)
+++ libavcodec/Makefile	(working copy)
@@ -169,6 +169,7 @@
 OBJS-$(CONFIG_PGM_ENCODER)             += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)          += pnmenc.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)          += pnmenc.o
+OBJS-$(CONFIG_PGSSUB_DECODER)          += pgssubdec.o
 OBJS-$(CONFIG_PNG_DECODER)             += png.o pngdec.o
 OBJS-$(CONFIG_PNG_ENCODER)             += png.o pngenc.o
 OBJS-$(CONFIG_PPM_DECODER)             += pnmenc.o pnm.o
Index: libavcodec/pgssubdec.c
===================================================================
--- libavcodec/pgssubdec.c	(revision 0)
+++ libavcodec/pgssubdec.c	(revision 0)
@@ -0,0 +1,401 @@
+/*
+ * PGS subtitle decoder
+ * Copyright (c) 2009 Stephen Backway
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file libavcodec/pgssubdec.c
+ * PGS subtitle decoder
+ */
+
+#include "avcodec.h"
+#include "dsputil.h"
+#include "colorspace.h"
+#include "bytestream.h"
+
+//#define DEBUG_PACKET_CONTENTS
+
+#define cm  (ff_cropTbl + MAX_NEG_CROP)
+#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
+
+enum SegmentType {
+    PALETTE_SEGMENT      = 0x14,
+    PICTURE_SEGMENT      = 0x15,
+    PRESENTATION_SEGMENT = 0x16,
+    WINDOW_SEGMENT       = 0x17,
+    DISPLAY_SEGMENT      = 0x80,
+};
+
+typedef struct PGSSubPresentation {
+    int x;
+    int y;
+    int video_w;
+    int video_h;
+    int id_number;
+} PGSSubPresentation;
+
+typedef struct PGSSubPicture {
+    int          w;
+    int          h;
+    uint8_t      *bitmap;
+    unsigned int bitmap_size;
+} PGSSubPicture;
+
+typedef struct PGSSubContext {
+    PGSSubPresentation presentation;
+    uint32_t           clut[256];
+    PGSSubPicture      *picture;
+} PGSSubContext;
+
+static av_cold int init_decoder(AVCodecContext *avctx)
+{
+    avctx->pix_fmt     = PIX_FMT_RGB32;
+    PGSSubContext *ctx = avctx->priv_data;
+
+    ctx->picture              = av_mallocz(sizeof(PGSSubPicture));
+
+    ctx->presentation.x       = 0;
+    ctx->presentation.y       = 0;
+    ctx->presentation.video_w = 0;
+    ctx->presentation.video_h = 0;
+
+    return 0;
+}
+
+static av_cold int close_decoder(AVCodecContext *avctx)
+{
+    PGSSubContext *ctx = avctx->priv_data;
+
+    av_freep(&ctx->picture->bitmap);
+    av_freep(&ctx->picture);
+
+    return 0;
+}
+
+static void parse_picture_segment(AVCodecContext *avctx,
+                                  const uint8_t *buf, int buf_size)
+{
+    PGSSubContext *ctx = avctx->priv_data;
+
+    uint8_t block, flags, color, *rle_bitmap_end, sequence_desc;
+    int rle_bitmap_len, pixel_count, line_count, run, width, height;
+
+    /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
+    buf += 3;
+
+    /* Read the Sequence Description to determine if start of RLE data or appended \
to previous RLE */ +    sequence_desc = bytestream_get_byte(&buf);
+
+    if (!(sequence_desc & 0x80)) {
+        av_log(avctx, AV_LOG_ERROR, "Decoder does not support object data over \
multiple packets.\n"); +        return;
+    }
+
+    /* Decode rle bitmap length */
+    rle_bitmap_len = bytestream_get_be24(&buf);
+
+    /* Check to ensure we have enough data for rle_bitmap_length if just a single \
packet */ +    if (rle_bitmap_len > buf_size - 7) {
+        av_log(avctx, AV_LOG_ERROR, "Not enough RLE data for specified length of \
%d.\n", rle_bitmap_len); +        return;
+    }
+
+    rle_bitmap_end = buf + rle_bitmap_len;
+
+    /* Get bitmap dimensions from data */
+    width  = bytestream_get_be16(&buf);
+    height = bytestream_get_be16(&buf);
+
+    /* Make sure the bitmap is not too large */
+    if (ctx->presentation.video_w < width || ctx->presentation.video_h < height) {
+        av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger then video.\n");
+        return;
+    }
+
+    ctx->picture->w = width;
+    ctx->picture->h = height;
+
+    av_fast_malloc(&ctx->picture->bitmap, &ctx->picture->bitmap_size, width * \
height); +
+    if (!ctx->picture->bitmap)
+        return AVERROR(ENOMEM);
+
+    pixel_count = 0;
+    line_count  = 0;
+
+    while (buf < rle_bitmap_end && line_count < height) {
+        block = bytestream_get_byte(&buf);
+        if (block == 0x00) {
+            flags = bytestream_get_byte(&buf);
+            run   = flags & 0x3f;
+            if (flags & 0x40)
+                run = (run << 8) + bytestream_get_byte(&buf);
+            color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
+        } else {
+            run   = 1;
+            color = block;
+        }
+
+        if (run > 0 && pixel_count + run <= width * height) {
+            memset(ctx->picture->bitmap + pixel_count, color, run);
+            pixel_count += run;
+        } else if (run == 0) {
+            /**
+             * New Line. Check if correct pixels decoded,
+             * if not display warning and adjust bitmap
+             * pointer to correct new line position.
+             */
+            if (pixel_count % width > 0)
+                av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be \
%d pixels\n", +                       pixel_count % width, width);
+            line_count++;
+        }
+    }
+
+    dprintf(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, width * height);
+}
+
+static void parse_palette_segment(AVCodecContext *avctx,
+                                  const uint8_t *buf, int buf_size)
+{
+    PGSSubContext *ctx = avctx->priv_data;
+
+    const uint8_t *buf_end = buf + buf_size;
+    int color_id;
+    int y, cb, cr, alpha;
+    int r, g, b, r_add, g_add, b_add;
+
+    /* Skip two null bytes */
+    buf += 2;
+
+    while (buf < buf_end) {
+        color_id  = bytestream_get_byte(&buf);
+        y         = bytestream_get_byte(&buf);
+        cb        = bytestream_get_byte(&buf);
+        cr        = bytestream_get_byte(&buf);
+        alpha     = bytestream_get_byte(&buf);
+
+        YUV_TO_RGB1(cb, cr);
+        YUV_TO_RGB2(r, g, b, y);
+
+        dprintf(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
+
+        /* Store color in palette */
+        if (color_id < 256)
+            ctx->clut[color_id] = RGBA(r,g,b,alpha);
+        else
+            av_log(avctx, AV_LOG_ERROR, "Invalid color defined with index %d\n", \
color_id); +    }
+}
+
+static void parse_presentation_segment(AVCodecContext *avctx,
+                                       const uint8_t *buf, int buf_size)
+{
+    PGSSubContext *ctx = avctx->priv_data;
+
+    int x, y;
+    uint8_t block;
+
+    ctx->presentation.video_w = bytestream_get_be16(&buf);
+    ctx->presentation.video_h = bytestream_get_be16(&buf);
+
+    dprintf(avctx, "Video Dimensions %dx%d\n",
+            ctx->presentation.video_w, ctx->presentation.video_h);
+
+    /* Skip 1 bytes of unknown, frame rate? */
+    buf++;
+
+    ctx->presentation.id_number = bytestream_get_be16(&buf);
+
+    /* Next byte is the state. */
+    block = bytestream_get_byte(&buf);;
+    if (block == 0x80) {
+        /**
+         * Skip 7 bytes of unknown:
+         *     palette_update_flag (0x80),
+         *     palette_id_to_use,
+         *     Object Number (if > 0 determines if more data to process),
+         *     object_id_ref (2 bytes),
+         *     window_id_ref,
+         *     composition_flag (0x80 - object cropped, 0x40 - object forced)
+         */
+        buf += 7;
+
+        x = bytestream_get_be16(&buf);
+        y = bytestream_get_be16(&buf);
+
+        /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height \
(all 2 bytes).*/ +
+        dprintf(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
+
+        if (x > ctx->presentation.video_w || y > ctx->presentation.video_h) {
+            av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = \
%d, video width = %d, video height = %d.\n", +                   x, y, \
ctx->presentation.video_w, ctx->presentation.video_h); +            x = 0; y = 0;
+        }
+
+        /* Fill in dimensions */
+        ctx->presentation.x = x;
+        ctx->presentation.y = y;
+    } else if (block == 0x00) {
+        /* TODO: Blank context as subtitle should not be displayed.
+         *       If the subtitle is blanked now the subtitle is not
+         *       on screen long enough to read, due to a delay in
+         *       initial display timing.
+         */
+    }
+}
+
+static int display_end_segment(AVCodecContext *avctx, void *data,
+                               const uint8_t *buf, int buf_size)
+{
+    AVSubtitle    *sub = data;
+    PGSSubContext *ctx = avctx->priv_data;
+
+    /**
+     * TODO Fix start and end time.
+     *      Currently the subtitle is displayed too late.
+     *      The end display time is a timeout value and is only reached
+     *      if the next subtitle is later then timeout or subtitle has
+     *      not been cleared by a subsequent empty display command.
+     *      Empty subtitle command is currently ignored as it clears
+     *      the subtitle too early.
+     */
+
+    sub->start_display_time = 0;
+    sub->end_display_time   = 20000;
+    sub->format             = 0;
+
+    if (!sub->rects) {
+        sub->rects     = av_mallocz(sizeof(*sub->rects));
+        sub->rects[0]  = av_mallocz(sizeof(*sub->rects[0]));
+        sub->num_rects = 1;
+    }
+
+    sub->rects[0]->x    = ctx->presentation.x;
+    sub->rects[0]->y    = ctx->presentation.y;
+    sub->rects[0]->w    = ctx->picture->w;
+    sub->rects[0]->h    = ctx->picture->h;
+    sub->rects[0]->type = SUBTITLE_BITMAP;
+
+    /* Allocate memory for bitmap */
+    sub->rects[0]->pict.data[0]     = av_malloc(ctx->picture->w * ctx->picture->h);
+    sub->rects[0]->pict.linesize[0] = ctx->picture->w;
+
+    if (ctx->picture->bitmap)
+        memcpy(sub->rects[0]->pict.data[0], ctx->picture->bitmap, ctx->picture->w * \
ctx->picture->h); +
+    /* Allocate memory for colors */
+    sub->rects[0]->nb_colors    = 256;
+    sub->rects[0]->pict.data[1] = av_malloc(sub->rects[0]->nb_colors * \
sizeof(uint32_t)); +
+    memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * \
sizeof(uint32_t)); +
+    return 1;
+}
+
+static int decode(AVCodecContext *avctx, void *data, int *data_size,
+                  AVPacket *avpkt)
+{
+    const uint8_t *buf = avpkt->data;
+    int buf_size       = avpkt->size;
+
+    const uint8_t *buf_end;
+    uint8_t       segment_type;
+    int           segment_length;
+
+#ifdef DEBUG_PACKET_CONTENTS
+    int i;
+
+    av_log(avctx, AV_LOG_INFO, "PGS sub packet:\n");
+
+    for (i = 0; i < buf_size; i++) {
+        av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
+        if (i % 16 == 15)
+            av_log(avctx, AV_LOG_INFO, "\n");
+    }
+
+    if (i % 16)
+        av_log(avctx, AV_LOG_INFO, "\n");
+#endif
+
+    *data_size = 0;
+
+    /* Ensure that we have received at a least a segment code and segment length */
+    if (buf_size < 3)
+        return -1;
+
+    buf_end = buf + buf_size;
+
+    /* Step through buffer to identify segments */
+    while (buf < buf_end) {
+        segment_type   = bytestream_get_byte(&buf);
+        segment_length = bytestream_get_be16(&buf);
+
+        dprintf(avctx, "Segment Length %d, Segment Type %x\n", segment_length, \
segment_type); +
+        if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
+            break;
+
+        switch (segment_type) {
+        case PALETTE_SEGMENT:
+            parse_palette_segment(avctx, buf, segment_length);
+            break;
+        case PICTURE_SEGMENT:
+            parse_picture_segment(avctx, buf, segment_length);
+            break;
+        case PRESENTATION_SEGMENT:
+            parse_presentation_segment(avctx, buf, segment_length);
+            break;
+        case WINDOW_SEGMENT:
+            /**
+             * Window Segment Structure (No new information provided):
+             *     2 bytes: Unkown,
+             *     2 bytes: X position of subtitle,
+             *     2 bytes: Y position of subtitle,
+             *     2 bytes: Width of subtitle,
+             *     2 bytes: Height of subtitle.
+             */
+            break;
+        case DISPLAY_SEGMENT:
+            *data_size = display_end_segment(avctx, data, buf, segment_length);
+            break;
+        default:
+            av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length \
%d\n", +                   segment_type, segment_length);
+            break;
+        }
+
+        buf += segment_length;
+    }
+
+    return buf_size;
+}
+
+AVCodec pgssub_decoder = {
+    "pgssub",
+    CODEC_TYPE_SUBTITLE,
+    CODEC_ID_HDMV_PGS_SUBTITLE,
+    sizeof(PGSSubContext),
+    init_decoder,
+    NULL,
+    close_decoder,
+    decode,
+    .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
+};
Index: doc/general.texi
===================================================================
--- doc/general.texi	(revision 19613)
+++ doc/general.texi	(working copy)
@@ -633,6 +633,7 @@
 @item SSA/ASS      @tab X @tab X
 @item DVB          @tab X @tab X @tab X @tab X
 @item DVD          @tab X @tab X @tab X @tab X
+@item PGS          @tab   @tab   @tab   @tab X
 @item XSUB         @tab   @tab   @tab X @tab X
 @end multitable
 
Index: libavformat/mpegts.c
===================================================================
--- libavformat/mpegts.c	(revision 19613)
+++ libavformat/mpegts.c	(working copy)
@@ -506,6 +506,7 @@
 static const StreamType HDMV_types[] = {
     { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
     { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
+    { 0x90, CODEC_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
     { 0 },
 };
 


[Attachment #4 (text/x-patch)]

Index: libavformat/mpegts.c
===================================================================
--- libavformat/mpegts.c	2009-08-02 10:19:12.000000000 +1000
+++ libavformat/mpegts.c	2009-08-10 17:59:53.000000000 +1000
@@ -504,8 +504,8 @@
 };
 
 static const StreamType HDMV_types[] = {
-    { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
-    { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
+    { 0x81, CODEC_TYPE_AUDIO,                  CODEC_ID_AC3 },
+    { 0x82, CODEC_TYPE_AUDIO,                  CODEC_ID_DTS },
     { 0x90, CODEC_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
     { 0 },
 };


_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@mplayerhq.hu
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-devel

_______________________________________________
MPlayer-users mailing list
MPlayer-users@mplayerhq.hu
https://lists.mplayerhq.hu/mailman/listinfo/mplayer-users

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic