annotate pgssubdec.c @ 12530:63edd10ad4bc libavcodec tip

Try to fix crashes introduced by r25218 r25218 made assumptions about the existence of past reference frames that weren't necessarily true.
author darkshikari
date Tue, 28 Sep 2010 09:06:22 +0000
parents ffb3668ff7af
children
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 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
23 * @file
10083
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 "bytestream.h"
12039
d5705b52b76e Move colorspace.h from libavcodec to libavutil.
stefano
parents: 12024
diff changeset
30 #include "libavutil/colorspace.h"
12394
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
31 #include "libavcore/imgutils.h"
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
32
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
33 //#define DEBUG_PACKET_CONTENTS
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
34
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
35 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
36
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
37 enum SegmentType {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
38 PALETTE_SEGMENT = 0x14,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
39 PICTURE_SEGMENT = 0x15,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
40 PRESENTATION_SEGMENT = 0x16,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
41 WINDOW_SEGMENT = 0x17,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
42 DISPLAY_SEGMENT = 0x80,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
43 };
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
44
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
45 typedef struct PGSSubPresentation {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
46 int x;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
47 int y;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
48 int id_number;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
49 } PGSSubPresentation;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
50
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
51 typedef struct PGSSubPicture {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
52 int w;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
53 int h;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
54 uint8_t *rle;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
55 unsigned int rle_buffer_size, rle_data_len;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
56 } PGSSubPicture;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
57
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
58 typedef struct PGSSubContext {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
59 PGSSubPresentation presentation;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
60 uint32_t clut[256];
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
61 PGSSubPicture picture;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
62 } PGSSubContext;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
63
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
64 static av_cold int init_decoder(AVCodecContext *avctx)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
65 {
12136
31bca176d2d9 Set pix_fmt to the correct value for the format the PGS decoder actually uses.
reimar
parents: 12039
diff changeset
66 avctx->pix_fmt = PIX_FMT_PAL8;
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
67
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
68 return 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
69 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
70
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
71 static av_cold int close_decoder(AVCodecContext *avctx)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
72 {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
73 PGSSubContext *ctx = avctx->priv_data;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
74
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
75 av_freep(&ctx->picture.rle);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
76 ctx->picture.rle_buffer_size = 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
77
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
78 return 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
79 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
80
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
81 /**
12024
fdafbcef52f5 Fix grammar errors in documentation
mru
parents: 11644
diff changeset
82 * Decode the RLE data.
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
83 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
84 * The subtitle is stored as an Run Length Encoded image.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
85 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
86 * @param avctx contains the current codec context
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
87 * @param sub pointer to the processed subtitle data
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
88 * @param buf pointer to the RLE data to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
89 * @param buf_size size of the RLE data to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
90 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
91 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
92 const uint8_t *buf, unsigned int buf_size)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
93 {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
94 const uint8_t *rle_bitmap_end;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
95 int pixel_count, line_count;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
96
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
97 rle_bitmap_end = buf + buf_size;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
98
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
99 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
100
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
101 if (!sub->rects[0]->pict.data[0])
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
102 return -1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
103
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
104 pixel_count = 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
105 line_count = 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
106
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
107 while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
108 uint8_t flags, color;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
109 int run;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
110
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
111 color = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
112 run = 1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
113
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
114 if (color == 0x00) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
115 flags = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
116 run = flags & 0x3f;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
117 if (flags & 0x40)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
118 run = (run << 8) + bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
119 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
120 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
121
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
122 if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
123 memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
124 pixel_count += run;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
125 } else if (!run) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
126 /*
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
127 * New Line. Check if correct pixels decoded, if not display warning
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
128 * and adjust bitmap pointer to correct new line position.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
129 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
130 if (pixel_count % sub->rects[0]->w > 0)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
131 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
132 pixel_count % sub->rects[0]->w, sub->rects[0]->w);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
133 line_count++;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
134 }
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 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
138
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
139 return 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
140 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
141
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
142 /**
12024
fdafbcef52f5 Fix grammar errors in documentation
mru
parents: 11644
diff changeset
143 * Parse the picture segment packet.
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
144 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
145 * The picture segment contains details on the sequence id,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
146 * width, height and Run Length Encoded (RLE) bitmap data.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
147 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
148 * @param avctx contains the current codec context
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
149 * @param buf pointer to the packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
150 * @param buf_size size of packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
151 * @todo TODO: Enable support for RLE data over multiple packets
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
152 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
153 static int parse_picture_segment(AVCodecContext *avctx,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
154 const uint8_t *buf, int buf_size)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
155 {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
156 PGSSubContext *ctx = avctx->priv_data;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
157
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
158 uint8_t sequence_desc;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
159 unsigned int rle_bitmap_len, width, height;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
160
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
161 /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
162 buf += 3;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
163
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
164 /* 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
165 sequence_desc = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
166
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
167 if (!(sequence_desc & 0x80)) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
168 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
169 return -1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
170 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
171
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
172 /* Decode rle bitmap length */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
173 rle_bitmap_len = bytestream_get_be24(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
174
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
175 /* 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
176 if (rle_bitmap_len > buf_size - 7) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
177 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
178 return -1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
179 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
180
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
181 ctx->picture.rle_data_len = rle_bitmap_len;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
182
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
183 /* Get bitmap dimensions from data */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
184 width = bytestream_get_be16(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
185 height = bytestream_get_be16(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
186
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
187 /* Make sure the bitmap is not too large */
12394
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
188 if (avctx->width < width || avctx->height < height) {
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
189 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger then video.\n");
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
190 return -1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
191 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
192
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
193 ctx->picture.w = width;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
194 ctx->picture.h = height;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
195
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
196 av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
197
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
198 if (!ctx->picture.rle)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
199 return -1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
200
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
201 memcpy(ctx->picture.rle, buf, rle_bitmap_len);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
202
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
203 return 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
204 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
205
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
206 /**
12024
fdafbcef52f5 Fix grammar errors in documentation
mru
parents: 11644
diff changeset
207 * Parse the palette segment packet.
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
208 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
209 * The palette segment contains details of the palette,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
210 * a maximum of 256 colors can be defined.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
211 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
212 * @param avctx contains the current codec context
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
213 * @param buf pointer to the packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
214 * @param buf_size size of packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
215 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
216 static void parse_palette_segment(AVCodecContext *avctx,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
217 const uint8_t *buf, int buf_size)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
218 {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
219 PGSSubContext *ctx = avctx->priv_data;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
220
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
221 const uint8_t *buf_end = buf + buf_size;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
222 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
223 int color_id;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
224 int y, cb, cr, alpha;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
225 int r, g, b, r_add, g_add, b_add;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
226
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
227 /* Skip two null bytes */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
228 buf += 2;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
229
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
230 while (buf < buf_end) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
231 color_id = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
232 y = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
233 cb = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
234 cr = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
235 alpha = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
236
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
237 YUV_TO_RGB1(cb, cr);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
238 YUV_TO_RGB2(r, g, b, y);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
239
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
240 dprintf(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
241
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
242 /* Store color in palette */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
243 ctx->clut[color_id] = RGBA(r,g,b,alpha);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
244 }
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 /**
12024
fdafbcef52f5 Fix grammar errors in documentation
mru
parents: 11644
diff changeset
248 * Parse the presentation segment packet.
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
249 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
250 * The presentation segment contains details on the video
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
251 * width, video height, x & y subtitle position.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
252 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
253 * @param avctx contains the current codec context
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
254 * @param buf pointer to the packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
255 * @param buf_size size of packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
256 * @todo TODO: Implement cropping
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
257 * @todo TODO: Implement forcing of subtitles
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
258 * @todo TODO: Blanking of subtitle
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
259 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
260 static void parse_presentation_segment(AVCodecContext *avctx,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
261 const uint8_t *buf, int buf_size)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
262 {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
263 PGSSubContext *ctx = avctx->priv_data;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
264
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
265 int x, y;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
266 uint8_t block;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
267
12394
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
268 int w = bytestream_get_be16(&buf);
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
269 int h = bytestream_get_be16(&buf);
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
270
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
271 dprintf(avctx, "Video Dimensions %dx%d\n",
12394
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
272 w, h);
12462
ffb3668ff7af Use new imgutils.h API names, fix deprecation warnings.
stefano
parents: 12394
diff changeset
273 if (av_image_check_size(w, h, 0, avctx) >= 0)
12394
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
274 avcodec_set_dimensions(avctx, w, h);
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
275
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
276 /* Skip 1 bytes of unknown, frame rate? */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
277 buf++;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
278
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
279 ctx->presentation.id_number = bytestream_get_be16(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
280
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
281 /* Next byte is the state. */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
282 block = bytestream_get_byte(&buf);;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
283 if (block == 0x80) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
284 /*
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
285 * Skip 7 bytes of unknown:
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
286 * palette_update_flag (0x80),
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
287 * palette_id_to_use,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
288 * Object Number (if > 0 determines if more data to process),
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
289 * object_id_ref (2 bytes),
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
290 * window_id_ref,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
291 * composition_flag (0x80 - object cropped, 0x40 - object forced)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
292 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
293 buf += 7;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
294
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
295 x = bytestream_get_be16(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
296 y = bytestream_get_be16(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
297
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
298 /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
299
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
300 dprintf(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
301
12394
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
302 if (x > avctx->width || y > avctx->height) {
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
303 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
12394
d46c4c3204b8 Export the presentation video dimensions as avctx->width/avctx->height.
reimar
parents: 12136
diff changeset
304 x, y, avctx->width, avctx->height);
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
305 x = 0; y = 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
306 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
307
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
308 /* Fill in dimensions */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
309 ctx->presentation.x = x;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
310 ctx->presentation.y = y;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
311 } else if (block == 0x00) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
312 /* TODO: Blank context as subtitle should not be displayed.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
313 * If the subtitle is blanked now the subtitle is not
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
314 * on screen long enough to read, due to a delay in
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
315 * initial display timing.
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 /**
12024
fdafbcef52f5 Fix grammar errors in documentation
mru
parents: 11644
diff changeset
321 * Parse the display segment packet.
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
322 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
323 * The display segment controls the updating of the display.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
324 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
325 * @param avctx contains the current codec context
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
326 * @param data pointer to the data pertaining the subtitle to display
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
327 * @param buf pointer to the packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
328 * @param buf_size size of packet to process
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
329 * @todo TODO: Fix start time, relies on correct PTS, currently too late
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
330 *
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
331 * @todo TODO: Fix end time, normally cleared by a second display
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
332 * @todo segment, which is currently ignored as it clears
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
333 * @todo the subtitle too early.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
334 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
335 static int display_end_segment(AVCodecContext *avctx, void *data,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
336 const uint8_t *buf, int buf_size)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
337 {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
338 AVSubtitle *sub = data;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
339 PGSSubContext *ctx = avctx->priv_data;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
340
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
341 /*
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
342 * The end display time is a timeout value and is only reached
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
343 * if the next subtitle is later then timeout or subtitle has
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
344 * not been cleared by a subsequent empty display command.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
345 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
346
10087
8f1e09db0905 Apply r19642 also pgssubdec:
reimar
parents: 10083
diff changeset
347 memset(sub, 0, sizeof(*sub));
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
348 sub->start_display_time = 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
349 sub->end_display_time = 20000;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
350 sub->format = 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
351
10089
b9247f1debb2 Reindent.
reimar
parents: 10088
diff changeset
352 sub->rects = av_mallocz(sizeof(*sub->rects));
b9247f1debb2 Reindent.
reimar
parents: 10088
diff changeset
353 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
b9247f1debb2 Reindent.
reimar
parents: 10088
diff changeset
354 sub->num_rects = 1;
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
355
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
356 sub->rects[0]->x = ctx->presentation.x;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
357 sub->rects[0]->y = ctx->presentation.y;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
358 sub->rects[0]->w = ctx->picture.w;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
359 sub->rects[0]->h = ctx->picture.h;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
360 sub->rects[0]->type = SUBTITLE_BITMAP;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
361
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
362 /* Process bitmap */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
363 sub->rects[0]->pict.linesize[0] = ctx->picture.w;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
364
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
365 if (ctx->picture.rle)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
366 if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
367 return 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
368
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
369 /* Allocate memory for colors */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
370 sub->rects[0]->nb_colors = 256;
10092
e85fbfe709d8 Always allocate a buffer of AVPALETTE_SIZE for palette in the
cehoyos
parents: 10089
diff changeset
371 sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
372
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
373 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
374
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
375 return 1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
376 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
377
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
378 static int decode(AVCodecContext *avctx, void *data, int *data_size,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
379 AVPacket *avpkt)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
380 {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
381 const uint8_t *buf = avpkt->data;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
382 int buf_size = avpkt->size;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
383
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
384 const uint8_t *buf_end;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
385 uint8_t segment_type;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
386 int segment_length;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
387
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
388 #ifdef DEBUG_PACKET_CONTENTS
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
389 int i;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
390
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
391 av_log(avctx, AV_LOG_INFO, "PGS sub packet:\n");
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
392
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
393 for (i = 0; i < buf_size; i++) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
394 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
395 if (i % 16 == 15)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
396 av_log(avctx, AV_LOG_INFO, "\n");
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
397 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
398
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
399 if (i & 15)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
400 av_log(avctx, AV_LOG_INFO, "\n");
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
401 #endif
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
402
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
403 *data_size = 0;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
404
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
405 /* Ensure that we have received at a least a segment code and segment length */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
406 if (buf_size < 3)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
407 return -1;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
408
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
409 buf_end = buf + buf_size;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
410
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
411 /* Step through buffer to identify segments */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
412 while (buf < buf_end) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
413 segment_type = bytestream_get_byte(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
414 segment_length = bytestream_get_be16(&buf);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
415
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
416 dprintf(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
417
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
418 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
419 break;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
420
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
421 switch (segment_type) {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
422 case PALETTE_SEGMENT:
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
423 parse_palette_segment(avctx, buf, segment_length);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
424 break;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
425 case PICTURE_SEGMENT:
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
426 parse_picture_segment(avctx, buf, segment_length);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
427 break;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
428 case PRESENTATION_SEGMENT:
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
429 parse_presentation_segment(avctx, buf, segment_length);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
430 break;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
431 case WINDOW_SEGMENT:
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
432 /*
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
433 * Window Segment Structure (No new information provided):
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
434 * 2 bytes: Unkown,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
435 * 2 bytes: X position of subtitle,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
436 * 2 bytes: Y position of subtitle,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
437 * 2 bytes: Width of subtitle,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
438 * 2 bytes: Height of subtitle.
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
439 */
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
440 break;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
441 case DISPLAY_SEGMENT:
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
442 *data_size = display_end_segment(avctx, data, buf, segment_length);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
443 break;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
444 default:
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
445 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
446 segment_type, segment_length);
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
447 break;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
448 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
449
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
450 buf += segment_length;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
451 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
452
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
453 return buf_size;
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
454 }
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
455
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
456 AVCodec pgssub_decoder = {
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
457 "pgssub",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 10092
diff changeset
458 AVMEDIA_TYPE_SUBTITLE,
10083
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
459 CODEC_ID_HDMV_PGS_SUBTITLE,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
460 sizeof(PGSSubContext),
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
461 init_decoder,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
462 NULL,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
463 close_decoder,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
464 decode,
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
465 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
8071f8b68d05 Add Bluray Subtitle Support
cehoyos
parents:
diff changeset
466 };