240c1657 |
/* |
124e2884 |
* DVD subtitle decoding |
406792e7 |
* Copyright (c) 2005 Fabrice Bellard |
240c1657 |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
240c1657 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
240c1657 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
240c1657 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
240c1657 |
*/ |
3dc6272b |
|
240c1657 |
#include "avcodec.h" |
9106a698 |
#include "get_bits.h" |
babbec08 |
#include "internal.h"
|
6fee1b90 |
#include "libavutil/attributes.h" |
2b4abbd6 |
#include "libavutil/colorspace.h" |
217b10de |
#include "libavutil/opt.h" |
3b6e9cd7 |
#include "libavutil/imgutils.h" |
88d55b82 |
#include "libavutil/avstring.h" |
12630fa8 |
#include "libavutil/bswap.h" |
240c1657 |
|
7009b965 |
typedef struct DVDSubContext
{ |
217b10de |
AVClass *class; |
7009b965 |
uint32_t palette[16]; |
217b10de |
char *palette_str; |
12630fa8 |
char *ifo_str; |
7009b965 |
int has_palette; |
32c0c562 |
uint8_t colormap[4];
uint8_t alpha[256]; |
81657771 |
uint8_t buf[0x10000]; |
95cafeb6 |
int buf_size; |
c7d21dee |
int forced_subs_only; |
71e0ae22 |
#ifdef DEBUG
int sub_id;
#endif |
7009b965 |
} DVDSubContext;
|
1aadf63f |
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
{ |
05563cca |
const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; |
1aadf63f |
uint8_t r, g, b;
int i, y, cb, cr;
int r_add, g_add, b_add;
for (i = num_values; i > 0; i--) {
y = *ycbcr++;
cr = *ycbcr++; |
9743e909 |
cb = *ycbcr++; |
1aadf63f |
YUV_TO_RGB1_CCIR(cb, cr);
YUV_TO_RGB2_CCIR(r, g, b, y);
*rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
}
}
static int decode_run_2bit(GetBitContext *gb, int *color)
{
unsigned int v, t;
v = 0;
for (t = 1; v < t && t <= 0x40; t <<= 2)
v = (v << 4) | get_bits(gb, 4);
*color = v & 3;
if (v < 4) { /* Code for fill rest of line */
return INT_MAX;
}
return v >> 2;
}
static int decode_run_8bit(GetBitContext *gb, int *color) |
240c1657 |
{ |
1aadf63f |
int len;
int has_run = get_bits1(gb);
if (get_bits1(gb))
*color = get_bits(gb, 8);
else
*color = get_bits(gb, 2);
if (has_run) {
if (get_bits1(gb)) {
len = get_bits(gb, 7);
if (len == 0)
len = INT_MAX;
else
len += 9;
} else
len = get_bits(gb, 3) + 2;
} else
len = 1;
return len; |
240c1657 |
}
|
115329f1 |
static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, |
1aadf63f |
const uint8_t *buf, int start, int buf_size, int is_8bit) |
240c1657 |
{ |
1aadf63f |
GetBitContext gb;
int bit_len;
int x, y, len, color; |
240c1657 |
uint8_t *d;
|
c9151de7 |
if (start >= buf_size)
return -1;
|
bcaa9099 |
if (w <= 0 || h <= 0)
return -1;
|
1aadf63f |
bit_len = (buf_size - start) * 8;
init_get_bits(&gb, buf + start, bit_len);
|
240c1657 |
x = 0;
y = 0;
d = bitmap;
for(;;) { |
1aadf63f |
if (get_bits_count(&gb) > bit_len) |
240c1657 |
return -1; |
1aadf63f |
if (is_8bit)
len = decode_run_8bit(&gb, &color);
else
len = decode_run_2bit(&gb, &color);
len = FFMIN(len, w - x); |
240c1657 |
memset(d + x, color, len);
x += len;
if (x >= w) {
y++;
if (y >= h)
break;
d += linesize;
x = 0;
/* byte align */ |
1aadf63f |
align_get_bits(&gb); |
240c1657 |
}
}
return 0;
}
|
f924d529 |
static void guess_palette(DVDSubContext* ctx,
uint32_t *rgba_palette, |
240c1657 |
uint32_t subtitle_color)
{ |
cf16104a |
static const uint8_t level_map[4][4] = {
// this configuration (full range, lowest to highest) in tests
// seemed most common, so assume this
{0xff},
{0x00, 0xff},
{0x00, 0x80, 0xff},
{0x00, 0x55, 0xaa, 0xff},
}; |
a92be9b8 |
uint8_t color_used[16] = { 0 }; |
240c1657 |
int nb_opaque_colors, i, level, j, r, g, b; |
32c0c562 |
uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha; |
115329f1 |
|
7009b965 |
if(ctx->has_palette) {
for(i = 0; i < 4; i++)
rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff) |
754dd7e8 |
| ((alpha[i] * 17U) << 24); |
7009b965 |
return;
}
|
240c1657 |
for(i = 0; i < 4; i++)
rgba_palette[i] = 0;
nb_opaque_colors = 0;
for(i = 0; i < 4; i++) { |
a363effb |
if (alpha[i] != 0 && !color_used[colormap[i]]) {
color_used[colormap[i]] = 1; |
240c1657 |
nb_opaque_colors++;
}
} |
115329f1 |
|
240c1657 |
if (nb_opaque_colors == 0)
return; |
115329f1 |
|
cf16104a |
j = 0; |
240c1657 |
memset(color_used, 0, 16);
for(i = 0; i < 4; i++) {
if (alpha[i] != 0) { |
a363effb |
if (!color_used[colormap[i]]) { |
cf16104a |
level = level_map[nb_opaque_colors][j]; |
240c1657 |
r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
b = (((subtitle_color >> 0) & 0xff) * level) >> 8; |
bf01fb69 |
rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24); |
a363effb |
color_used[colormap[i]] = (i + 1); |
cf16104a |
j++; |
240c1657 |
} else { |
a363effb |
rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) | |
bf01fb69 |
((alpha[i] * 17) << 24); |
240c1657 |
}
}
}
}
|
41fd6f07 |
static void reset_rects(AVSubtitle *sub_header)
{
int i;
|
fb33bff9 |
if (sub_header->rects) { |
41fd6f07 |
for (i = 0; i < sub_header->num_rects; i++) {
av_freep(&sub_header->rects[i]->pict.data[0]);
av_freep(&sub_header->rects[i]->pict.data[1]);
av_freep(&sub_header->rects[i]);
}
av_freep(&sub_header->rects);
sub_header->num_rects = 0;
}
}
|
1aadf63f |
#define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
|
7009b965 |
static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header, |
240c1657 |
const uint8_t *buf, int buf_size)
{
int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos; |
1aadf63f |
int big_offsets, offset_size, is_8bit = 0; |
2d7cec14 |
const uint8_t *yuv_palette = NULL; |
32c0c562 |
uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha; |
240c1657 |
int date; |
50e2450b |
int i; |
bf01fb69 |
int is_menu = 0; |
115329f1 |
|
1aadf63f |
if (buf_size < 10) |
240c1657 |
return -1;
|
1aadf63f |
if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
big_offsets = 1;
offset_size = 4;
cmd_pos = 6;
} else {
big_offsets = 0;
offset_size = 2;
cmd_pos = 2;
}
cmd_pos = READ_OFFSET(buf + cmd_pos);
|
95cafeb6 |
if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
return AVERROR(EAGAIN);
|
ffbe087b |
while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) { |
eabf6560 |
date = AV_RB16(buf + cmd_pos); |
1aadf63f |
next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2); |
9ef5a9de |
av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n", |
8b44de14 |
cmd_pos, next_cmd_pos, date); |
1aadf63f |
pos = cmd_pos + 2 + offset_size; |
240c1657 |
offset1 = -1;
offset2 = -1;
x1 = y1 = x2 = y2 = 0;
while (pos < buf_size) {
cmd = buf[pos++]; |
9ef5a9de |
av_dlog(NULL, "cmd=%02x\n", cmd); |
240c1657 |
switch(cmd) {
case 0x00: |
bf01fb69 |
/* menu subpicture */
is_menu = 1; |
240c1657 |
break;
case 0x01:
/* set start date */ |
bf01fb69 |
sub_header->start_display_time = (date << 10) / 90; |
240c1657 |
break;
case 0x02:
/* set end date */ |
bf01fb69 |
sub_header->end_display_time = (date << 10) / 90; |
240c1657 |
break;
case 0x03: |
a363effb |
/* set colormap */ |
240c1657 |
if ((buf_size - pos) < 2)
goto fail; |
a363effb |
colormap[3] = buf[pos] >> 4;
colormap[2] = buf[pos] & 0x0f;
colormap[1] = buf[pos + 1] >> 4;
colormap[0] = buf[pos + 1] & 0x0f; |
240c1657 |
pos += 2;
break;
case 0x04:
/* set alpha */
if ((buf_size - pos) < 2)
goto fail; |
bf01fb69 |
alpha[3] = buf[pos] >> 4;
alpha[2] = buf[pos] & 0x0f;
alpha[1] = buf[pos + 1] >> 4;
alpha[0] = buf[pos + 1] & 0x0f; |
240c1657 |
pos += 2; |
9ef5a9de |
av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]); |
240c1657 |
break;
case 0x05: |
1aadf63f |
case 0x85: |
240c1657 |
if ((buf_size - pos) < 6)
goto fail;
x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5]; |
1aadf63f |
if (cmd & 0x80)
is_8bit = 1; |
9ef5a9de |
av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2); |
240c1657 |
pos += 6;
break;
case 0x06:
if ((buf_size - pos) < 4)
goto fail; |
eabf6560 |
offset1 = AV_RB16(buf + pos);
offset2 = AV_RB16(buf + pos + 2); |
9ef5a9de |
av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2); |
240c1657 |
pos += 4;
break; |
1aadf63f |
case 0x86:
if ((buf_size - pos) < 8)
goto fail;
offset1 = AV_RB32(buf + pos);
offset2 = AV_RB32(buf + pos + 4); |
9ef5a9de |
av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2); |
1aadf63f |
pos += 8;
break;
case 0x83:
/* HD set palette */
if ((buf_size - pos) < 768)
goto fail;
yuv_palette = buf + pos;
pos += 768;
break;
case 0x84:
/* HD set contrast (alpha) */
if ((buf_size - pos) < 256)
goto fail;
for (i = 0; i < 256; i++)
alpha[i] = 0xFF - buf[pos+i];
pos += 256;
break;
|
240c1657 |
case 0xff: |
1aadf63f |
goto the_end; |
240c1657 |
default: |
9ef5a9de |
av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd); |
240c1657 |
goto the_end;
}
}
the_end:
if (offset1 >= 0) {
int w, h;
uint8_t *bitmap; |
115329f1 |
|
240c1657 |
/* decode the bitmap */
w = x2 - x1 + 1;
if (w < 0)
w = 0; |
3f0a3e9e |
h = y2 - y1 + 1; |
240c1657 |
if (h < 0)
h = 0;
if (w > 0 && h > 0) { |
41fd6f07 |
reset_rects(sub_header); |
50e2450b |
|
052571e7 |
sub_header->rects = av_mallocz(sizeof(*sub_header->rects)); |
ec17782e |
if (!sub_header->rects)
goto fail; |
052571e7 |
sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect)); |
ec17782e |
if (!sub_header->rects[0])
goto fail; |
50e2450b |
sub_header->num_rects = 1; |
ec17782e |
bitmap = sub_header->rects[0]->pict.data[0] = av_malloc(w * h);
if (!bitmap)
goto fail; |
c9151de7 |
if (decode_rle(bitmap, w * 2, w, (h + 1) / 2,
buf, offset1, buf_size, is_8bit) < 0)
goto fail;
if (decode_rle(bitmap + w, w * 2, w, h / 2,
buf, offset2, buf_size, is_8bit) < 0)
goto fail; |
29d7eef7 |
sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE); |
ec17782e |
if (!sub_header->rects[0]->pict.data[1])
goto fail; |
1aadf63f |
if (is_8bit) { |
2d7cec14 |
if (!yuv_palette) |
1aadf63f |
goto fail; |
052571e7 |
sub_header->rects[0]->nb_colors = 256; |
25b4c651 |
yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256); |
1aadf63f |
} else { |
052571e7 |
sub_header->rects[0]->nb_colors = 4; |
06af724c |
guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1], |
32c0c562 |
0xffff00); |
1aadf63f |
} |
052571e7 |
sub_header->rects[0]->x = x1;
sub_header->rects[0]->y = y1;
sub_header->rects[0]->w = w;
sub_header->rects[0]->h = h; |
6dc13ccb |
sub_header->rects[0]->type = SUBTITLE_BITMAP; |
25b4c651 |
sub_header->rects[0]->pict.linesize[0] = w; |
1885ffb0 |
sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0; |
240c1657 |
}
} |
99c7b516 |
if (next_cmd_pos < cmd_pos) {
av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
break;
} |
240c1657 |
if (next_cmd_pos == cmd_pos)
break;
cmd_pos = next_cmd_pos;
} |
50e2450b |
if (sub_header->num_rects > 0) |
bf01fb69 |
return is_menu; |
240c1657 |
fail: |
41fd6f07 |
reset_rects(sub_header); |
240c1657 |
return -1;
}
|
115329f1 |
static int is_transp(const uint8_t *buf, int pitch, int n, |
240c1657 |
const uint8_t *transp_color)
{
int i;
for(i = 0; i < n; i++) {
if (!transp_color[*buf])
return 0;
buf += pitch;
}
return 1;
}
/* return 0 if empty rectangle, 1 if non empty */ |
bf01fb69 |
static int find_smallest_bounding_rectangle(AVSubtitle *s) |
240c1657 |
{ |
a92be9b8 |
uint8_t transp_color[256] = { 0 }; |
240c1657 |
int y1, y2, x1, x2, y, w, h, i;
uint8_t *bitmap;
|
f929ab05 |
if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0) |
240c1657 |
return 0;
|
052571e7 |
for(i = 0; i < s->rects[0]->nb_colors; i++) { |
25b4c651 |
if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0) |
240c1657 |
transp_color[i] = 1;
}
y1 = 0; |
25b4c651 |
while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0], |
052571e7 |
1, s->rects[0]->w, transp_color)) |
240c1657 |
y1++; |
052571e7 |
if (y1 == s->rects[0]->h) { |
25b4c651 |
av_freep(&s->rects[0]->pict.data[0]); |
052571e7 |
s->rects[0]->w = s->rects[0]->h = 0; |
240c1657 |
return 0;
}
|
052571e7 |
y2 = s->rects[0]->h - 1; |
25b4c651 |
while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1, |
052571e7 |
s->rects[0]->w, transp_color)) |
240c1657 |
y2--;
x1 = 0; |
25b4c651 |
while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0], |
052571e7 |
s->rects[0]->h, transp_color)) |
240c1657 |
x1++; |
052571e7 |
x2 = s->rects[0]->w - 1; |
25b4c651 |
while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h, |
240c1657 |
transp_color))
x2--;
w = x2 - x1 + 1;
h = y2 - y1 + 1;
bitmap = av_malloc(w * h);
if (!bitmap)
return 1;
for(y = 0; y < h; y++) { |
25b4c651 |
memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w); |
240c1657 |
} |
25b4c651 |
av_freep(&s->rects[0]->pict.data[0]);
s->rects[0]->pict.data[0] = bitmap;
s->rects[0]->pict.linesize[0] = w; |
052571e7 |
s->rects[0]->w = w;
s->rects[0]->h = h;
s->rects[0]->x += x1;
s->rects[0]->y += y1; |
240c1657 |
return 1;
}
#ifdef DEBUG |
d7382328 |
#define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255) |
240c1657 |
static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
uint32_t *rgba_palette)
{ |
d7382328 |
int x, y, alpha;
uint32_t v;
int back[3] = {0, 255, 0}; /* green background */ |
240c1657 |
FILE *f;
f = fopen(filename, "w");
if (!f) {
perror(filename); |
b7fea3da |
return; |
240c1657 |
}
fprintf(f, "P6\n"
"%d %d\n"
"%d\n",
w, h, 255);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
v = rgba_palette[bitmap[y * w + x]]; |
d7382328 |
alpha = v >> 24;
putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f); |
240c1657 |
}
}
fclose(f);
}
#endif
|
95cafeb6 |
static int append_to_cached_buf(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
DVDSubContext *ctx = avctx->priv_data;
|
81657771 |
if (ctx->buf_size >= sizeof(ctx->buf) - buf_size) { |
95cafeb6 |
av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
"too large SPU packets aborted.\n");
return AVERROR_INVALIDDATA;
}
memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
ctx->buf_size += buf_size;
return 0;
}
|
240c1657 |
static int dvdsub_decode(AVCodecContext *avctx,
void *data, int *data_size, |
7a00bbad |
AVPacket *avpkt) |
240c1657 |
{ |
f924d529 |
DVDSubContext *ctx = avctx->priv_data; |
7a00bbad |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
af0554e5 |
AVSubtitle *sub = data; |
bf01fb69 |
int is_menu;
|
81657771 |
if (ctx->buf_size) { |
95cafeb6 |
int ret = append_to_cached_buf(avctx, buf, buf_size);
if (ret < 0) {
*data_size = 0;
return ret;
}
buf = ctx->buf;
buf_size = ctx->buf_size;
}
|
7009b965 |
is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size); |
95cafeb6 |
if (is_menu == AVERROR(EAGAIN)) {
*data_size = 0;
return append_to_cached_buf(avctx, buf, buf_size);
} |
240c1657 |
|
bf01fb69 |
if (is_menu < 0) { |
240c1657 |
no_subtitle: |
dd200c56 |
reset_rects(sub); |
240c1657 |
*data_size = 0;
return buf_size;
} |
bf01fb69 |
if (!is_menu && find_smallest_bounding_rectangle(sub) == 0) |
240c1657 |
goto no_subtitle;
|
c7d21dee |
if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
goto no_subtitle;
|
240c1657 |
#if defined(DEBUG) |
71e0ae22 |
{
char ppm_name[32];
snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++); |
9ef5a9de |
av_dlog(NULL, "start=%d ms end =%d ms\n", |
8b44de14 |
sub->start_display_time,
sub->end_display_time); |
71e0ae22 |
ppm_save(ppm_name, sub->rects[0]->pict.data[0], |
d7382328 |
sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->pict.data[1]); |
71e0ae22 |
} |
240c1657 |
#endif
|
95cafeb6 |
ctx->buf_size = 0; |
240c1657 |
*data_size = 1;
return buf_size;
}
|
217b10de |
static void parse_palette(DVDSubContext *ctx, char *p)
{
int i;
ctx->has_palette = 1;
for(i=0;i<16;i++) {
ctx->palette[i] = strtoul(p, &p, 16); |
88d55b82 |
while(*p == ',' || av_isspace(*p)) |
217b10de |
p++;
}
}
|
12630fa8 |
static int parse_ifo_palette(DVDSubContext *ctx, char *p)
{
FILE *ifo;
char ifostr[12];
uint32_t sp_pgci, pgci, off_pgc, pgc;
uint8_t r, g, b, yuv[65], *buf;
int i, y, cb, cr, r_add, g_add, b_add;
int ret = 0;
const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
ctx->has_palette = 0;
if ((ifo = fopen(p, "r")) == NULL) { |
5d0cfb58 |
av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno))); |
12630fa8 |
return AVERROR_EOF;
}
if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
ret = AVERROR_INVALIDDATA;
goto end;
} |
1de78677 |
if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
ret = AVERROR(errno);
goto end;
} |
12630fa8 |
if (fread(&sp_pgci, 4, 1, ifo) == 1) {
pgci = av_be2ne32(sp_pgci) * 2048; |
1de78677 |
if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
ret = AVERROR(errno);
goto end;
} |
12630fa8 |
if (fread(&off_pgc, 4, 1, ifo) == 1) {
pgc = pgci + av_be2ne32(off_pgc); |
1de78677 |
if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
ret = AVERROR(errno);
goto end;
} |
12630fa8 |
if (fread(yuv, 64, 1, ifo) == 1) {
buf = yuv;
for(i=0; i<16; i++) {
y = *++buf;
cr = *++buf;
cb = *++buf;
YUV_TO_RGB1_CCIR(cb, cr);
YUV_TO_RGB2_CCIR(r, g, b, y);
ctx->palette[i] = (r << 16) + (g << 8) + b;
buf++;
}
ctx->has_palette = 1;
}
}
}
if (ctx->has_palette == 0) {
av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
ret = AVERROR_INVALIDDATA;
}
end:
fclose(ifo);
return ret;
}
|
217b10de |
static int dvdsub_parse_extradata(AVCodecContext *avctx) |
7009b965 |
{
DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data; |
872655fe |
char *dataorig, *data; |
ac967ad8 |
int ret = 1; |
7009b965 |
if (!avctx->extradata || !avctx->extradata_size)
return 1;
|
872655fe |
dataorig = data = av_malloc(avctx->extradata_size+1); |
7009b965 |
if (!data)
return AVERROR(ENOMEM);
memcpy(data, avctx->extradata, avctx->extradata_size);
data[avctx->extradata_size] = '\0';
for(;;) {
int pos = strcspn(data, "\n\r");
if (pos==0 && *data==0)
break;
if (strncmp("palette:", data, 8) == 0) { |
217b10de |
parse_palette(ctx, data + 8); |
3b6e9cd7 |
} else if (strncmp("size:", data, 5) == 0) {
int w, h; |
4a356512 |
if (sscanf(data + 5, "%dx%d", &w, &h) == 2) { |
d466d82f |
ret = ff_set_dimensions(avctx, w, h); |
babbec08 |
if (ret < 0) |
d466d82f |
goto fail; |
babbec08 |
} |
7009b965 |
}
data += pos;
data += strspn(data, "\n\r");
}
|
d466d82f |
fail: |
217b10de |
av_free(dataorig); |
d466d82f |
return ret; |
217b10de |
}
|
a1f234be |
static av_cold int dvdsub_init(AVCodecContext *avctx) |
217b10de |
{ |
06af724c |
DVDSubContext *ctx = avctx->priv_data; |
217b10de |
int ret;
if ((ret = dvdsub_parse_extradata(avctx)) < 0)
return ret;
|
12630fa8 |
if (ctx->ifo_str)
parse_ifo_palette(ctx, ctx->ifo_str); |
217b10de |
if (ctx->palette_str)
parse_palette(ctx, ctx->palette_str); |
7009b965 |
if (ctx->has_palette) {
int i;
av_log(avctx, AV_LOG_DEBUG, "palette:");
for(i=0;i<16;i++)
av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
av_log(avctx, AV_LOG_DEBUG, "\n");
}
return 1;
}
|
95cafeb6 |
static av_cold int dvdsub_close(AVCodecContext *avctx)
{
DVDSubContext *ctx = avctx->priv_data;
ctx->buf_size = 0;
return 0;
}
|
217b10de |
#define OFFSET(field) offsetof(DVDSubContext, field) |
5c073bbb |
#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM |
217b10de |
static const AVOption options[] = { |
5c073bbb |
{ "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD }, |
12630fa8 |
{ "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD }, |
c7d21dee |
{ "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD}, |
217b10de |
{ NULL }
}; |
0915b531 |
static const AVClass dvdsub_class = { |
217b10de |
.class_name = "dvdsubdec",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
|
e7e2df27 |
AVCodec ff_dvdsub_decoder = { |
ec6402b7 |
.name = "dvdsub", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"), |
ec6402b7 |
.type = AVMEDIA_TYPE_SUBTITLE, |
36ef5369 |
.id = AV_CODEC_ID_DVD_SUBTITLE, |
7009b965 |
.priv_data_size = sizeof(DVDSubContext),
.init = dvdsub_init, |
ec6402b7 |
.decode = dvdsub_decode, |
95cafeb6 |
.close = dvdsub_close, |
0915b531 |
.priv_class = &dvdsub_class, |
240c1657 |
}; |