2005-11-15 00:17:29 +02:00
|
|
|
/*
|
2011-10-30 19:56:57 +03:00
|
|
|
* DVD subtitle encoding
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2005 Wolfram Gloger
|
2005-11-15 00:17:29 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2005-11-15 00:17:29 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2005-11-15 00:17:29 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2005-11-15 00:17:29 +02:00
|
|
|
* 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
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-11-15 00:17:29 +02:00
|
|
|
*/
|
|
|
|
#include "avcodec.h"
|
2007-05-23 17:55:13 +03:00
|
|
|
#include "bytestream.h"
|
2012-12-30 00:09:59 +03:00
|
|
|
#include "internal.h"
|
2012-07-28 02:18:04 +03:00
|
|
|
#include "libavutil/avassert.h"
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
#include "libavutil/bprint.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
avcodec/dvdsubenc: Add dvdsub workaround for some players
The issue affects dvdsub subtitles (a.k.a. VOBSUB).
Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.
The patch below implements a work-around for that. If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix. The default is disabled, so there
is no change of behaviour for users who don't care about it.
The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00. However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead. Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-09 19:43:58 +03:00
|
|
|
#include "libavutil/opt.h"
|
2005-11-15 00:17:29 +02:00
|
|
|
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
typedef struct {
|
avcodec/dvdsubenc: Add dvdsub workaround for some players
The issue affects dvdsub subtitles (a.k.a. VOBSUB).
Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.
The patch below implements a work-around for that. If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix. The default is disabled, so there
is no change of behaviour for users who don't care about it.
The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00. However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead. Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-09 19:43:58 +03:00
|
|
|
AVClass *class;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
uint32_t global_palette[16];
|
avcodec/dvdsubenc: Add dvdsub workaround for some players
The issue affects dvdsub subtitles (a.k.a. VOBSUB).
Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.
The patch below implements a work-around for that. If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix. The default is disabled, so there
is no change of behaviour for users who don't care about it.
The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00. However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead. Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-09 19:43:58 +03:00
|
|
|
int even_rows_fix;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
} DVDSubtitleContext;
|
2005-11-15 00:17:29 +02:00
|
|
|
|
|
|
|
// ncnt is the nibble counter
|
|
|
|
#define PUTNIBBLE(val)\
|
|
|
|
do {\
|
|
|
|
if (ncnt++ & 1)\
|
|
|
|
*q++ = bitbuf | ((val) & 0x0f);\
|
|
|
|
else\
|
|
|
|
bitbuf = (val) << 4;\
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
static void dvd_encode_rle(uint8_t **pq,
|
|
|
|
const uint8_t *bitmap, int linesize,
|
|
|
|
int w, int h,
|
|
|
|
const int cmap[256])
|
|
|
|
{
|
|
|
|
uint8_t *q;
|
|
|
|
unsigned int bitbuf = 0;
|
|
|
|
int ncnt;
|
|
|
|
int x, y, len, color;
|
|
|
|
|
|
|
|
q = *pq;
|
|
|
|
|
|
|
|
for (y = 0; y < h; ++y) {
|
|
|
|
ncnt = 0;
|
|
|
|
for(x = 0; x < w; x += len) {
|
|
|
|
color = bitmap[x];
|
|
|
|
for (len=1; x+len < w; ++len)
|
|
|
|
if (bitmap[x+len] != color)
|
|
|
|
break;
|
|
|
|
color = cmap[color];
|
2012-07-28 02:18:04 +03:00
|
|
|
av_assert0(color < 4);
|
2005-11-15 00:17:29 +02:00
|
|
|
if (len < 0x04) {
|
|
|
|
PUTNIBBLE((len << 2)|color);
|
|
|
|
} else if (len < 0x10) {
|
|
|
|
PUTNIBBLE(len >> 2);
|
|
|
|
PUTNIBBLE((len << 2)|color);
|
|
|
|
} else if (len < 0x40) {
|
|
|
|
PUTNIBBLE(0);
|
|
|
|
PUTNIBBLE(len >> 2);
|
|
|
|
PUTNIBBLE((len << 2)|color);
|
|
|
|
} else if (x+len == w) {
|
|
|
|
PUTNIBBLE(0);
|
|
|
|
PUTNIBBLE(0);
|
|
|
|
PUTNIBBLE(0);
|
|
|
|
PUTNIBBLE(color);
|
|
|
|
} else {
|
|
|
|
if (len > 0xff)
|
|
|
|
len = 0xff;
|
|
|
|
PUTNIBBLE(0);
|
|
|
|
PUTNIBBLE(len >> 6);
|
|
|
|
PUTNIBBLE(len >> 2);
|
|
|
|
PUTNIBBLE((len << 2)|color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* end of line */
|
|
|
|
if (ncnt & 1)
|
|
|
|
PUTNIBBLE(0);
|
|
|
|
bitmap += linesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pq = q;
|
|
|
|
}
|
|
|
|
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
static int color_distance(uint32_t a, uint32_t b)
|
|
|
|
{
|
|
|
|
int r = 0, d, i;
|
2013-01-01 19:06:04 +03:00
|
|
|
int alpha_a = 8, alpha_b = 8;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
|
2013-01-01 19:06:04 +03:00
|
|
|
for (i = 24; i >= 0; i -= 8) {
|
|
|
|
d = alpha_a * (int)((a >> i) & 0xFF) -
|
|
|
|
alpha_b * (int)((b >> i) & 0xFF);
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
r += d * d;
|
2013-01-01 19:06:04 +03:00
|
|
|
alpha_a = a >> 28;
|
|
|
|
alpha_b = b >> 28;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count colors used in a rectangle, quantizing alpha and grouping by
|
|
|
|
* nearest global palette entry.
|
|
|
|
*/
|
|
|
|
static void count_colors(AVCodecContext *avctx, unsigned hits[33],
|
|
|
|
const AVSubtitleRect *r)
|
|
|
|
{
|
|
|
|
DVDSubtitleContext *dvdc = avctx->priv_data;
|
|
|
|
unsigned count[256] = { 0 };
|
2015-10-22 21:18:03 +02:00
|
|
|
uint32_t *palette = (uint32_t *)r->data[1];
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
uint32_t color;
|
|
|
|
int x, y, i, j, match, d, best_d, av_uninit(best_j);
|
2015-10-22 21:18:03 +02:00
|
|
|
uint8_t *p = r->data[0];
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
|
|
|
|
for (y = 0; y < r->h; y++) {
|
|
|
|
for (x = 0; x < r->w; x++)
|
|
|
|
count[*(p++)]++;
|
2015-10-22 21:18:03 +02:00
|
|
|
p += r->linesize[0] - r->w;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
}
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (!count[i]) /* avoid useless search */
|
|
|
|
continue;
|
|
|
|
color = palette[i];
|
|
|
|
/* 0: transparent, 1-16: semi-transparent, 17-33 opaque */
|
|
|
|
match = color < 0x33000000 ? 0 : color < 0xCC000000 ? 1 : 17;
|
|
|
|
if (match) {
|
|
|
|
best_d = INT_MAX;
|
|
|
|
for (j = 0; j < 16; j++) {
|
2013-01-01 19:06:04 +03:00
|
|
|
d = color_distance(0xFF000000 | color,
|
|
|
|
0xFF000000 | dvdc->global_palette[j]);
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
if (d < best_d) {
|
|
|
|
best_d = d;
|
|
|
|
best_j = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match += best_j;
|
|
|
|
}
|
|
|
|
hits[match] += count[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void select_palette(AVCodecContext *avctx, int out_palette[4],
|
|
|
|
int out_alpha[4], unsigned hits[33])
|
|
|
|
{
|
|
|
|
DVDSubtitleContext *dvdc = avctx->priv_data;
|
|
|
|
int i, j, bright, mult;
|
|
|
|
uint32_t color;
|
|
|
|
int selected[4] = { 0 };
|
|
|
|
uint32_t pseudopal[33] = { 0 };
|
|
|
|
uint32_t refcolor[3] = { 0x00000000, 0xFFFFFFFF, 0xFF000000 };
|
|
|
|
|
|
|
|
/* Bonus for transparent: if the rectangle fits tightly the text, the
|
|
|
|
background color can be quite rare, but it would be ugly without it */
|
|
|
|
hits[0] *= 16;
|
|
|
|
/* Bonus for bright colors */
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (!(hits[1 + i] + hits[17 + i]))
|
|
|
|
continue; /* skip unused colors to gain time */
|
|
|
|
color = dvdc->global_palette[i];
|
|
|
|
bright = 0;
|
|
|
|
for (j = 0; j < 3; j++, color >>= 8)
|
|
|
|
bright += (color & 0xFF) < 0x40 || (color & 0xFF) >= 0xC0;
|
|
|
|
mult = 2 + FFMIN(bright, 2);
|
|
|
|
hits[ 1 + i] *= mult;
|
|
|
|
hits[17 + i] *= mult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select four most frequent colors */
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
for (j = 0; j < 33; j++)
|
|
|
|
if (hits[j] > hits[selected[i]])
|
|
|
|
selected[i] = j;
|
|
|
|
hits[selected[i]] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Order the colors like in most DVDs:
|
|
|
|
0: background, 1: foreground, 2: outline */
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
pseudopal[ 1 + i] = 0x80000000 | dvdc->global_palette[i];
|
|
|
|
pseudopal[17 + i] = 0xFF000000 | dvdc->global_palette[i];
|
|
|
|
}
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
int best_d = color_distance(refcolor[i], pseudopal[selected[i]]);
|
|
|
|
for (j = i + 1; j < 4; j++) {
|
|
|
|
int d = color_distance(refcolor[i], pseudopal[selected[j]]);
|
|
|
|
if (d < best_d) {
|
|
|
|
FFSWAP(int, selected[i], selected[j]);
|
|
|
|
best_d = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
out_palette[i] = selected[i] ? (selected[i] - 1) & 0xF : 0;
|
|
|
|
out_alpha [i] = !selected[i] ? 0 : selected[i] < 17 ? 0x80 : 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void build_color_map(AVCodecContext *avctx, int cmap[],
|
|
|
|
const uint32_t palette[],
|
2013-01-12 23:40:41 +03:00
|
|
|
const int out_palette[], unsigned int const out_alpha[])
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
{
|
|
|
|
DVDSubtitleContext *dvdc = avctx->priv_data;
|
|
|
|
int i, j, d, best_d;
|
|
|
|
uint32_t pseudopal[4];
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
pseudopal[i] = (out_alpha[i] << 24) |
|
|
|
|
dvdc->global_palette[out_palette[i]];
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
best_d = INT_MAX;
|
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
d = color_distance(pseudopal[j], palette[i]);
|
|
|
|
if (d < best_d) {
|
|
|
|
cmap[i] = j;
|
|
|
|
best_d = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_rectangle(AVSubtitleRect *dst, AVSubtitleRect *src, int cmap[])
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
uint8_t *p, *q;
|
|
|
|
|
2015-10-22 21:18:03 +02:00
|
|
|
p = src->data[0];
|
|
|
|
q = dst->data[0] + (src->x - dst->x) +
|
|
|
|
(src->y - dst->y) * dst->linesize[0];
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
for (y = 0; y < src->h; y++) {
|
|
|
|
for (x = 0; x < src->w; x++)
|
|
|
|
*(q++) = cmap[*(p++)];
|
2015-10-22 21:18:03 +02:00
|
|
|
p += src->linesize[0] - src->w;
|
|
|
|
q += dst->linesize[0] - src->w;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int encode_dvd_subtitles(AVCodecContext *avctx,
|
|
|
|
uint8_t *outbuf, int outbuf_size,
|
2005-11-15 00:17:29 +02:00
|
|
|
const AVSubtitle *h)
|
|
|
|
{
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
DVDSubtitleContext *dvdc = avctx->priv_data;
|
2005-11-15 00:17:29 +02:00
|
|
|
uint8_t *q, *qq;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
int offset1, offset2;
|
|
|
|
int i, rects = h->num_rects, ret;
|
|
|
|
unsigned global_palette_hits[33] = { 0 };
|
|
|
|
int cmap[256];
|
|
|
|
int out_palette[4];
|
|
|
|
int out_alpha[4];
|
|
|
|
AVSubtitleRect vrect;
|
|
|
|
uint8_t *vrect_data = NULL;
|
|
|
|
int x2, y2;
|
2014-05-18 16:29:28 +03:00
|
|
|
int forced = 0;
|
2005-11-15 00:17:29 +02:00
|
|
|
|
2014-08-14 23:31:24 +03:00
|
|
|
if (rects == 0 || !h->rects)
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
return AVERROR(EINVAL);
|
2012-08-19 20:10:48 +03:00
|
|
|
for (i = 0; i < rects; i++)
|
|
|
|
if (h->rects[i]->type != SUBTITLE_BITMAP) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Bitmap subtitle required\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2014-05-18 16:29:28 +03:00
|
|
|
/* Mark this subtitle forced if any of the rectangles is forced. */
|
|
|
|
for (i = 0; i < rects; i++)
|
|
|
|
if ((h->rects[i]->flags & AV_SUBTITLE_FLAG_FORCED) != 0) {
|
|
|
|
forced = 1;
|
|
|
|
break;
|
|
|
|
}
|
2015-10-22 21:18:03 +02:00
|
|
|
|
2015-10-14 11:33:25 +02:00
|
|
|
#if FF_API_AVPICTURE
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2015-10-22 21:18:03 +02:00
|
|
|
for (i = 0; i < rects; i++)
|
|
|
|
if (!h->rects[i]->data[0]) {
|
|
|
|
AVSubtitleRect *rect = h->rects[i];
|
2015-10-14 11:33:25 +02:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
rect->data[j] = rect->pict.data[j];
|
|
|
|
rect->linesize[j] = rect->pict.linesize[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
|
|
|
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
vrect = *h->rects[0];
|
|
|
|
|
|
|
|
if (rects > 1) {
|
|
|
|
/* DVD subtitles can have only one rectangle: build a virtual
|
|
|
|
rectangle containing all actual rectangles.
|
|
|
|
The data of the rectangles will be copied later, when the palette
|
|
|
|
is decided, because the rectangles may have different palettes. */
|
|
|
|
int xmin = h->rects[0]->x, xmax = xmin + h->rects[0]->w;
|
|
|
|
int ymin = h->rects[0]->y, ymax = ymin + h->rects[0]->h;
|
|
|
|
for (i = 1; i < rects; i++) {
|
|
|
|
xmin = FFMIN(xmin, h->rects[i]->x);
|
|
|
|
ymin = FFMIN(ymin, h->rects[i]->y);
|
|
|
|
xmax = FFMAX(xmax, h->rects[i]->x + h->rects[i]->w);
|
|
|
|
ymax = FFMAX(ymax, h->rects[i]->y + h->rects[i]->h);
|
|
|
|
}
|
|
|
|
vrect.x = xmin;
|
|
|
|
vrect.y = ymin;
|
|
|
|
vrect.w = xmax - xmin;
|
|
|
|
vrect.h = ymax - ymin;
|
|
|
|
if ((ret = av_image_check_size(vrect.w, vrect.h, 0, avctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Count pixels outside the virtual rectangle as transparent */
|
|
|
|
global_palette_hits[0] = vrect.w * vrect.h;
|
|
|
|
for (i = 0; i < rects; i++)
|
|
|
|
global_palette_hits[0] -= h->rects[i]->w * h->rects[i]->h;
|
2005-11-15 00:17:29 +02:00
|
|
|
}
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
|
|
|
|
for (i = 0; i < rects; i++)
|
|
|
|
count_colors(avctx, global_palette_hits, h->rects[i]);
|
|
|
|
select_palette(avctx, out_palette, out_alpha, global_palette_hits);
|
|
|
|
|
|
|
|
if (rects > 1) {
|
|
|
|
if (!(vrect_data = av_calloc(vrect.w, vrect.h)))
|
|
|
|
return AVERROR(ENOMEM);
|
2015-10-22 21:18:03 +02:00
|
|
|
vrect.data [0] = vrect_data;
|
|
|
|
vrect.linesize[0] = vrect.w;
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
for (i = 0; i < rects; i++) {
|
2015-10-22 21:18:03 +02:00
|
|
|
build_color_map(avctx, cmap, (uint32_t *)h->rects[i]->data[1],
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
out_palette, out_alpha);
|
|
|
|
copy_rectangle(&vrect, h->rects[i], cmap);
|
2005-11-15 00:17:29 +02:00
|
|
|
}
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
cmap[i] = i;
|
|
|
|
} else {
|
2015-10-22 21:18:03 +02:00
|
|
|
build_color_map(avctx, cmap, (uint32_t *)h->rects[0]->data[1],
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
out_palette, out_alpha);
|
2005-11-15 00:17:29 +02:00
|
|
|
}
|
|
|
|
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Selected palette:");
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, " 0x%06x@@%02x (0x%x,0x%x)",
|
|
|
|
dvdc->global_palette[out_palette[i]], out_alpha[i],
|
|
|
|
out_palette[i], out_alpha[i] >> 4);
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "\n");
|
2005-11-15 00:17:29 +02:00
|
|
|
|
|
|
|
// encode data block
|
|
|
|
q = outbuf + 4;
|
2012-08-14 12:58:32 +03:00
|
|
|
offset1 = q - outbuf;
|
|
|
|
// worst case memory requirement: 1 nibble per pixel..
|
|
|
|
if ((q - outbuf) + vrect.w * vrect.h / 2 + 17 + 21 > outbuf_size) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
|
|
|
|
ret = AVERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-10-22 21:18:03 +02:00
|
|
|
dvd_encode_rle(&q, vrect.data[0], vrect.w * 2,
|
2012-08-14 12:58:32 +03:00
|
|
|
vrect.w, (vrect.h + 1) >> 1, cmap);
|
|
|
|
offset2 = q - outbuf;
|
2015-10-22 21:18:03 +02:00
|
|
|
dvd_encode_rle(&q, vrect.data[0] + vrect.w, vrect.w * 2,
|
2012-08-14 12:58:32 +03:00
|
|
|
vrect.w, vrect.h >> 1, cmap);
|
2005-11-15 00:17:29 +02:00
|
|
|
|
avcodec/dvdsubenc: Add dvdsub workaround for some players
The issue affects dvdsub subtitles (a.k.a. VOBSUB).
Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.
The patch below implements a work-around for that. If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix. The default is disabled, so there
is no change of behaviour for users who don't care about it.
The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00. However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead. Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-09 19:43:58 +03:00
|
|
|
if (dvdc->even_rows_fix && (vrect.h & 1)) {
|
|
|
|
// Work-around for some players that want the height to be even.
|
|
|
|
vrect.h++;
|
|
|
|
*q++ = 0x00; // 0x00 0x00 == empty row, i.e. fully transparent
|
|
|
|
*q++ = 0x00;
|
|
|
|
}
|
|
|
|
|
2005-11-15 00:17:29 +02:00
|
|
|
// set data packet size
|
|
|
|
qq = outbuf + 2;
|
2007-05-23 17:55:13 +03:00
|
|
|
bytestream_put_be16(&qq, q - outbuf);
|
2005-11-15 00:17:29 +02:00
|
|
|
|
|
|
|
// send start display command
|
2007-05-23 17:55:13 +03:00
|
|
|
bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12 + 2);
|
2005-11-15 00:17:29 +02:00
|
|
|
*q++ = 0x03; // palette - 4 nibbles
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
*q++ = (out_palette[3] << 4) | out_palette[2];
|
|
|
|
*q++ = (out_palette[1] << 4) | out_palette[0];
|
2005-11-15 00:17:29 +02:00
|
|
|
*q++ = 0x04; // alpha - 4 nibbles
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
*q++ = (out_alpha[3] & 0xF0) | (out_alpha[2] >> 4);
|
|
|
|
*q++ = (out_alpha[1] & 0xF0) | (out_alpha[0] >> 4);
|
2005-11-15 00:17:29 +02:00
|
|
|
|
|
|
|
// 12 bytes per rect
|
2012-08-14 12:58:32 +03:00
|
|
|
x2 = vrect.x + vrect.w - 1;
|
|
|
|
y2 = vrect.y + vrect.h - 1;
|
|
|
|
|
|
|
|
*q++ = 0x05;
|
|
|
|
// x1 x2 -> 6 nibbles
|
|
|
|
*q++ = vrect.x >> 4;
|
|
|
|
*q++ = (vrect.x << 4) | ((x2 >> 8) & 0xf);
|
|
|
|
*q++ = x2;
|
|
|
|
// y1 y2 -> 6 nibbles
|
|
|
|
*q++ = vrect.y >> 4;
|
|
|
|
*q++ = (vrect.y << 4) | ((y2 >> 8) & 0xf);
|
|
|
|
*q++ = y2;
|
|
|
|
|
|
|
|
*q++ = 0x06;
|
|
|
|
// offset1, offset2
|
|
|
|
bytestream_put_be16(&q, offset1);
|
|
|
|
bytestream_put_be16(&q, offset2);
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
|
2014-05-18 16:29:28 +03:00
|
|
|
*q++ = forced ? 0x00 : 0x01; // start command
|
2005-11-15 00:17:29 +02:00
|
|
|
*q++ = 0xff; // terminating command
|
|
|
|
|
|
|
|
// send stop display command last
|
2007-05-23 17:55:13 +03:00
|
|
|
bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
|
|
|
|
bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
|
2005-11-15 00:17:29 +02:00
|
|
|
*q++ = 0x02; // set end
|
|
|
|
*q++ = 0xff; // terminating command
|
|
|
|
|
|
|
|
qq = outbuf;
|
2007-05-23 17:55:13 +03:00
|
|
|
bytestream_put_be16(&qq, q - outbuf);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2014-04-24 19:01:30 +03:00
|
|
|
av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%"PTRDIFF_SPECIFIER"\n", q - outbuf);
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
ret = q - outbuf;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_free(vrect_data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dvdsub_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
DVDSubtitleContext *dvdc = avctx->priv_data;
|
|
|
|
static const uint32_t default_palette[16] = {
|
|
|
|
0x000000, 0x0000FF, 0x00FF00, 0xFF0000,
|
|
|
|
0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF,
|
|
|
|
0x808000, 0x8080FF, 0x800080, 0x80FF80,
|
|
|
|
0x008080, 0xFF8080, 0x555555, 0xAAAAAA,
|
|
|
|
};
|
|
|
|
AVBPrint extradata;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
av_assert0(sizeof(dvdc->global_palette) == sizeof(default_palette));
|
|
|
|
memcpy(dvdc->global_palette, default_palette, sizeof(dvdc->global_palette));
|
|
|
|
|
|
|
|
av_bprint_init(&extradata, 0, 1);
|
2012-08-07 20:05:43 +03:00
|
|
|
if (avctx->width && avctx->height)
|
|
|
|
av_bprintf(&extradata, "size: %dx%d\n", avctx->width, avctx->height);
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
av_bprintf(&extradata, "palette:");
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
av_bprintf(&extradata, " %06"PRIx32"%c",
|
|
|
|
dvdc->global_palette[i] & 0xFFFFFF, i < 15 ? ',' : '\n');
|
|
|
|
|
2012-12-31 00:54:53 +03:00
|
|
|
ret = avpriv_bprint_to_extradata(avctx, &extradata);
|
2012-12-30 00:09:59 +03:00
|
|
|
if (ret < 0)
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2005-11-15 00:17:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dvdsub_encode(AVCodecContext *avctx,
|
2012-08-18 17:41:24 +03:00
|
|
|
unsigned char *buf, int buf_size,
|
|
|
|
const AVSubtitle *sub)
|
2005-11-15 00:17:29 +02:00
|
|
|
{
|
|
|
|
//DVDSubtitleContext *s = avctx->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
ret = encode_dvd_subtitles(avctx, buf, buf_size, sub);
|
2005-11-15 00:17:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
avcodec/dvdsubenc: Add dvdsub workaround for some players
The issue affects dvdsub subtitles (a.k.a. VOBSUB).
Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.
The patch below implements a work-around for that. If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix. The default is disabled, so there
is no change of behaviour for users who don't care about it.
The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00. However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead. Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-09 19:43:58 +03:00
|
|
|
#define OFFSET(x) offsetof(DVDSubtitleContext, x)
|
|
|
|
#define SE AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2015-11-21 23:04:39 +02:00
|
|
|
{"even_rows_fix", "Make number of rows even (workaround for some players)", OFFSET(even_rows_fix), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SE},
|
avcodec/dvdsubenc: Add dvdsub workaround for some players
The issue affects dvdsub subtitles (a.k.a. VOBSUB).
Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.
The patch below implements a work-around for that. If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix. The default is disabled, so there
is no change of behaviour for users who don't care about it.
The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00. However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead. Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-09 19:43:58 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass dvdsubenc_class = {
|
|
|
|
.class_name = "VOBSUB subtitle encoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_dvdsub_encoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "dvdsub",
|
2013-10-03 23:57:53 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
|
2011-07-17 13:54:31 +03:00
|
|
|
.type = AVMEDIA_TYPE_SUBTITLE,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_DVD_SUBTITLE,
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
.init = dvdsub_init,
|
2012-08-18 17:41:24 +03:00
|
|
|
.encode_sub = dvdsub_encode,
|
avcodec/dvdsubenc: Add dvdsub workaround for some players
The issue affects dvdsub subtitles (a.k.a. VOBSUB).
Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.
The patch below implements a work-around for that. If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix. The default is disabled, so there
is no change of behaviour for users who don't care about it.
The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00. However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead. Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-07-09 19:43:58 +03:00
|
|
|
.priv_class = &dvdsubenc_class,
|
dvdsubenc: make it usable for transcoding.
DVD subtitles packets can only encode a single rectangle:
if there are several, copy them into a big transparent one.
DVD subtitles rely on an external 16-colors palette:
use a reasonable default one, stored in the private context,
and encode it into the extradata, as specified by Matroska.
TODO: allow to change the palette with an option.
Each packet can use four colors out of the global palette.
The old logic was to map transparent colors to the color 0
and all other colors to 3, 2, 1, cyclically in descending
frequency order, completely disregarding the original color.
Select the "best" four colors from the global palette, according
to heuristics based on frequency, opacity and brightness, and
arrange them in standard DVD order: background, foreground,
outline, other.
TODO: select the alpha value more finely; see if CHG_COLCON can
allow more than 4 colors per packet.
Reference:
http://dvd.sourceforge.net/dvdinfo/spu.html
With these changes, dvdsubenc can be used to transcode DVB subtitles
and get a very decent result.
2012-08-06 15:14:27 +03:00
|
|
|
.priv_data_size = sizeof(DVDSubtitleContext),
|
2005-11-15 00:17:29 +02:00
|
|
|
};
|