31 uint32_t global_palette[16];
36 #define PUTNIBBLE(val)\
39 *q++ = bitbuf | ((val) & 0x0f);\
45 const uint8_t *bitmap,
int linesize,
50 unsigned int bitbuf = 0;
56 for (y = 0; y <
h; ++
y) {
58 for(x = 0; x < w; x +=
len) {
60 for (len=1; x+len < w; ++
len)
61 if (bitmap[x+len] != color)
67 }
else if (len < 0x10) {
70 }
else if (len < 0x40) {
74 }
else if (x+len == w) {
100 int alpha_a = 8, alpha_b = 8;
102 for (i = 24; i >= 0; i -= 8) {
103 d = alpha_a * (int)((a >> i) & 0xFF) -
104 alpha_b * (
int)((b >> i) & 0xFF);
120 unsigned count[256] = { 0 };
123 int x,
y, i, j, match, d, best_d,
av_uninit(best_j);
126 for (
y = 0;
y < r->
h;
y++) {
127 for (x = 0; x < r->
w; x++)
131 for (i = 0; i < 256; i++) {
136 match =
color < 0x33000000 ? 0 :
color < 0xCC000000 ? 1 : 17;
139 for (j = 0; j < 16; j++) {
149 hits[match] += count[i];
154 int out_alpha[4],
unsigned hits[33])
157 int i, j, bright,
mult;
159 int selected[4] = { 0 };
160 uint32_t pseudopal[33] = { 0 };
161 uint32_t refcolor[3] = { 0x00000000, 0xFFFFFFFF, 0xFF000000 };
167 for (i = 0; i < 16; i++) {
168 if (!(hits[1 + i] + hits[17 + i]))
172 for (j = 0; j < 3; j++, color >>= 8)
173 bright += (color & 0xFF) < 0x40 || (color & 0xFF) >= 0xC0;
174 mult = 2 +
FFMIN(bright, 2);
175 hits[ 1 + i] *=
mult;
176 hits[17 + i] *=
mult;
180 for (i = 0; i < 4; i++) {
181 for (j = 0; j < 33; j++)
182 if (hits[j] > hits[selected[i]])
184 hits[selected[i]] = 0;
189 for (i = 0; i < 16; i++) {
193 for (i = 0; i < 3; i++) {
195 for (j = i + 1; j < 4; j++) {
198 FFSWAP(
int, selected[i], selected[j]);
205 for (i = 0; i < 4; i++) {
206 out_palette[i] = selected[i] ? (selected[i] - 1) & 0xF : 0;
207 out_alpha [i] = !selected[i] ? 0 : selected[i] < 17 ? 0x80 : 0xFF;
213 const int out_palette[],
unsigned int const out_alpha[])
217 uint32_t pseudopal[4];
219 for (i = 0; i < 4; i++)
220 pseudopal[i] = (out_alpha[i] << 24) |
222 for (i = 0; i < 256; i++) {
224 for (j = 0; j < 4; j++) {
242 for (y = 0; y < src->
h; y++) {
243 for (x = 0; x < src->
w; x++)
244 *(q++) = cmap[*(p++)];
251 uint8_t *outbuf,
int outbuf_size,
256 int offset1, offset2;
258 unsigned global_palette_hits[33] = { 0 };
267 if (rects == 0 || !h->
rects)
269 for (i = 0; i < rects; i++)
275 for (i = 0; i < rects; i++)
280 vrect = *h->
rects[0];
287 int xmin = h->
rects[0]->
x, xmax = xmin + h->
rects[0]->
w;
288 int ymin = h->
rects[0]->
y, ymax = ymin + h->
rects[0]->
h;
289 for (i = 1; i < rects; i++) {
297 vrect.
w = xmax - xmin;
298 vrect.
h = ymax - ymin;
303 global_palette_hits[0] = vrect.
w * vrect.
h;
304 for (i = 0; i < rects; i++)
305 global_palette_hits[0] -= h->
rects[i]->
w * h->
rects[i]->
h;
308 for (i = 0; i < rects; i++)
310 select_palette(avctx, out_palette, out_alpha, global_palette_hits);
317 for (i = 0; i < rects; i++) {
319 out_palette, out_alpha);
322 for (i = 0; i < 4; i++)
326 out_palette, out_alpha);
330 for (i = 0; i < 4; i++)
333 out_palette[i], out_alpha[i] >> 4);
338 offset1 = q - outbuf;
340 if ((q - outbuf) + vrect.
w * vrect.
h / 2 + 17 + 21 > outbuf_size) {
346 vrect.
w, (vrect.
h + 1) >> 1, cmap);
347 offset2 = q - outbuf;
349 vrect.
w, vrect.
h >> 1, cmap);
360 bytestream_put_be16(&qq, q - outbuf);
364 bytestream_put_be16(&q, (q - outbuf) + 8 + 12 + 2);
366 *q++ = (out_palette[3] << 4) | out_palette[2];
367 *q++ = (out_palette[1] << 4) | out_palette[0];
369 *q++ = (out_alpha[3] & 0xF0) | (out_alpha[2] >> 4);
370 *q++ = (out_alpha[1] & 0xF0) | (out_alpha[0] >> 4);
373 x2 = vrect.
x + vrect.
w - 1;
374 y2 = vrect.
y + vrect.
h - 1;
379 *q++ = (vrect.
x << 4) | ((x2 >> 8) & 0xf);
383 *q++ = (vrect.
y << 4) | ((y2 >> 8) & 0xf);
388 bytestream_put_be16(&q, offset1);
389 bytestream_put_be16(&q, offset2);
391 *q++ = forced ? 0x00 : 0x01;
396 bytestream_put_be16(&q, (q - outbuf) - 2 );
401 bytestream_put_be16(&qq, q - outbuf);
414 static const uint32_t default_palette[16] = {
415 0x000000, 0x0000FF, 0x00FF00, 0xFF0000,
416 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF,
417 0x808000, 0x8080FF, 0x800080, 0x80FF80,
418 0x008080, 0xFF8080, 0x555555, 0xAAAAAA,
430 for (i = 0; i < 16; i++)
442 unsigned char *
buf,
int buf_size,
452 #define OFFSET(x) offsetof(DVDSubtitleContext, x)
453 #define SE AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM
455 {
"even_rows_fix",
"Make number of rows even (workaround for some players)",
OFFSET(even_rows_fix),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
SE},
void av_bprintf(AVBPrint *buf, const char *fmt,...)
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
int x
top left corner of pict, undefined when pict is not set
#define LIBAVUTIL_VERSION_INT
static int color_distance(uint32_t a, uint32_t b)
static const AVClass dvdsubenc_class
static void copy_rectangle(AVSubtitleRect *dst, AVSubtitleRect *src, int cmap[])
int w
width of pict, undefined when pict is not set
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
static void dvd_encode_rle(uint8_t **pq, const uint8_t *bitmap, int linesize, int w, int h, const int cmap[256])
int h
height of pict, undefined when pict is not set
int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
Finalize buf into extradata and set its size appropriately.
static int dvdsub_encode(AVCodecContext *avctx, unsigned char *buf, int buf_size, const AVSubtitle *sub)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define PTRDIFF_SPECIFIER
int y
top left corner of pict, undefined when pict is not set
AVCodec ff_dvdsub_encoder
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Libavcodec external API header.
uint32_t end_display_time
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
A bitmap, pict will be set.
#define AV_SUBTITLE_FLAG_FORCED
AVPicture pict
data+linesize for the bitmap of this subtitle.
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
int width
picture width / height.
static void build_color_map(AVCodecContext *avctx, int cmap[], const uint32_t palette[], const int out_palette[], unsigned int const out_alpha[])
main external API structure.
static const AVOption options[]
static int16_t mult(Float11 *f1, Float11 *f2)
Describe the class of an AVClass context structure.
static void select_palette(AVCodecContext *avctx, int out_palette[4], int out_alpha[4], unsigned hits[33])
static void count_colors(AVCodecContext *avctx, unsigned hits[33], const AVSubtitleRect *r)
Count colors used in a rectangle, quantizing alpha and grouping by nearest global palette entry...
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
static int encode_dvd_subtitles(AVCodecContext *avctx, uint8_t *outbuf, int outbuf_size, const AVSubtitle *h)
common internal api header.
uint32_t start_display_time
#define FFSWAP(type, a, b)
uint32_t global_palette[16]
static int dvdsub_init(AVCodecContext *avctx)