annotate pgssubdec.c @ 10092:e85fbfe709d8 libavcodec

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