]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/pgssubdec.c
rtpdec: Cosmetic cleanup of the header
[ffmpeg] / libavcodec / pgssubdec.c
index c283b9c0c37cb4bd74b33a8b76496b5f040580a0..f22088a747281c283b33c2a98a6c38d680f58c15 100644 (file)
@@ -2,20 +2,20 @@
  * PGS subtitle decoder
  * Copyright (c) 2009 Stephen Backway
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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,
+ * Libav 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
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -28,9 +28,7 @@
 #include "dsputil.h"
 #include "bytestream.h"
 #include "libavutil/colorspace.h"
-#include "libavcore/imgutils.h"
-
-//#define DEBUG_PACKET_CONTENTS
+#include "libavutil/imgutils.h"
 
 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
 
@@ -47,6 +45,8 @@ typedef struct PGSSubPresentation {
     int y;
     int id_number;
     int object_number;
+    uint8_t composition_flag;
+    int64_t pts;
 } PGSSubPresentation;
 
 typedef struct PGSSubPicture {
@@ -65,7 +65,7 @@ typedef struct PGSSubContext {
 
 static av_cold int init_decoder(AVCodecContext *avctx)
 {
-    avctx->pix_fmt = PIX_FMT_PAL8;
+    avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
     return 0;
 }
@@ -141,7 +141,7 @@ static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
         return -1;
     }
 
-    dprintf(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
+    av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
 
     return 0;
 }
@@ -182,6 +182,7 @@ static int parse_picture_segment(AVCodecContext *avctx,
 
         memcpy(ctx->picture.rle + ctx->picture.rle_data_len, buf, buf_size);
         ctx->picture.rle_data_len += buf_size;
+        ctx->picture.rle_remaining_len -= buf_size;
 
         return 0;
     }
@@ -199,7 +200,7 @@ static int parse_picture_segment(AVCodecContext *avctx,
 
     /* Make sure the bitmap is not too large */
     if (avctx->width < width || avctx->height < height) {
-        av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger then video.\n");
+        av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
         return -1;
     }
 
@@ -245,14 +246,14 @@ static void parse_palette_segment(AVCodecContext *avctx,
     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);
+        cb        = 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);
+        av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
 
         /* Store color in palette */
         ctx->clut[color_id] = RGBA(r,g,b,alpha);
@@ -272,7 +273,8 @@ static void parse_palette_segment(AVCodecContext *avctx,
  * @todo TODO: Implement forcing of subtitles
  */
 static void parse_presentation_segment(AVCodecContext *avctx,
-                                       const uint8_t *buf, int buf_size)
+                                       const uint8_t *buf, int buf_size,
+                                       int64_t pts)
 {
     PGSSubContext *ctx = avctx->priv_data;
 
@@ -281,7 +283,9 @@ static void parse_presentation_segment(AVCodecContext *avctx,
     int w = bytestream_get_be16(&buf);
     int h = bytestream_get_be16(&buf);
 
-    dprintf(avctx, "Video Dimensions %dx%d\n",
+    ctx->presentation.pts = pts;
+
+    av_dlog(avctx, "Video Dimensions %dx%d\n",
             w, h);
     if (av_image_check_size(w, h, 0, avctx) >= 0)
         avcodec_set_dimensions(avctx, w, h);
@@ -300,23 +304,24 @@ static void parse_presentation_segment(AVCodecContext *avctx,
     buf += 3;
 
     ctx->presentation.object_number = bytestream_get_byte(&buf);
+    ctx->presentation.composition_flag = 0;
     if (!ctx->presentation.object_number)
         return;
 
     /*
-     * Skip 4 bytes of unknown:
+     * Skip 3 bytes of unknown:
      *     object_id_ref (2 bytes),
      *     window_id_ref,
-     *     composition_flag (0x80 - object cropped, 0x40 - object forced)
      */
-    buf += 4;
+    buf += 3;
+    ctx->presentation.composition_flag = bytestream_get_byte(&buf);
 
     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);
+    av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
 
     if (x > avctx->width || y > avctx->height) {
         av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
@@ -357,6 +362,8 @@ static int display_end_segment(AVCodecContext *avctx, void *data,
      */
 
     memset(sub, 0, sizeof(*sub));
+    sub->pts = ctx->presentation.pts;
+
     // Blank if last object_number was 0.
     // Note that this may be wrong for more complex subtitles.
     if (!ctx->presentation.object_number)
@@ -369,6 +376,9 @@ static int display_end_segment(AVCodecContext *avctx, void *data,
     sub->rects[0]  = av_mallocz(sizeof(*sub->rects[0]));
     sub->num_rects = 1;
 
+    if (ctx->presentation.composition_flag & 0x40)
+        sub->rects[0]->flags |= AV_SUBTITLE_FLAG_FORCED;
+
     sub->rects[0]->x    = ctx->presentation.x;
     sub->rects[0]->y    = ctx->presentation.y;
     sub->rects[0]->w    = ctx->picture.w;
@@ -403,21 +413,18 @@ static int decode(AVCodecContext *avctx, void *data, int *data_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");
+    av_dlog(avctx, "PGS sub packet:\n");
 
     for (i = 0; i < buf_size; i++) {
-        av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
+        av_dlog(avctx, "%02x ", buf[i]);
         if (i % 16 == 15)
-            av_log(avctx, AV_LOG_INFO, "\n");
+            av_dlog(avctx, "\n");
     }
 
     if (i & 15)
-        av_log(avctx, AV_LOG_INFO, "\n");
-#endif
+        av_dlog(avctx, "\n");
 
     *data_size = 0;
 
@@ -432,7 +439,7 @@ static int decode(AVCodecContext *avctx, void *data, int *data_size,
         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);
+        av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
 
         if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
             break;
@@ -445,12 +452,12 @@ static int decode(AVCodecContext *avctx, void *data, int *data_size,
             parse_picture_segment(avctx, buf, segment_length);
             break;
         case PRESENTATION_SEGMENT:
-            parse_presentation_segment(avctx, buf, segment_length);
+            parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);
             break;
         case WINDOW_SEGMENT:
             /*
              * Window Segment Structure (No new information provided):
-             *     2 bytes: Unkown,
+             *     2 bytes: Unknown,
              *     2 bytes: X position of subtitle,
              *     2 bytes: Y position of subtitle,
              *     2 bytes: Width of subtitle,
@@ -472,14 +479,13 @@ static int decode(AVCodecContext *avctx, void *data, int *data_size,
     return buf_size;
 }
 
-AVCodec pgssub_decoder = {
-    "pgssub",
-    AVMEDIA_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"),
+AVCodec ff_pgssub_decoder = {
+    .name           = "pgssub",
+    .type           = AVMEDIA_TYPE_SUBTITLE,
+    .id             = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
+    .priv_data_size = sizeof(PGSSubContext),
+    .init           = init_decoder,
+    .close          = close_decoder,
+    .decode         = decode,
+    .long_name      = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
 };