Skip to content

Commit

Permalink
Add support for ASS “Video Colorspace” property
Browse files Browse the repository at this point in the history
This property has been introduced with recent versions of Aegisub and
should be taken into account if possible.
  • Loading branch information
Martin Herkt committed Apr 20, 2012
1 parent b0b86f3 commit c22e1f2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ string srt_font:
Defaults to whatever Fontconfig chooses for “Sans”.
string colorspace:
The color space of your (YUV) video. Possible values:
- auto, guess (guess based on video resolution)
- Rec709, BT.709
- Rec601, BT.601
Default is to use the ASS script’s “Video Colorspace” property, else guess
based on video resolution (width > 1280 or height > 576 → BT.709).
20 changes: 14 additions & 6 deletions src/assrender.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ AVS_Value AVSC_CC assrender_create(AVS_ScriptEnvironment *env, AVS_Value args,
const char *colorspace = avs_as_string(avs_array_elt(args, 16)) ?
avs_as_string(avs_array_elt(args, 16)) : "guess";

char *tmpcsp = calloc(1, BUFSIZ);
memcpy(tmpcsp, colorspace, BUFSIZ - 1);

ASS_Hinting hinting;
udata *data;
ASS_Track *ass;
Expand Down Expand Up @@ -110,8 +113,10 @@ AVS_Value AVSC_CC assrender_create(AVS_ScriptEnvironment *env, AVS_Value args,

if (!strcasecmp(strrchr(f, '.'), ".srt"))
ass = parse_srt(f, data, srt_font);
else
else {
ass = ass_read_file(data->ass_library, (char *) f, (char *) cs);
ass_read_colorspace(f, tmpcsp);
}

if (!ass) {
sprintf(e, "AssRender: unable to parse '%s'", f);
Expand Down Expand Up @@ -169,15 +174,18 @@ AVS_Value AVSC_CC assrender_create(AVS_ScriptEnvironment *env, AVS_Value args,
data->isvfr = 0;
}

if (!strcasecmp(colorspace, "guess") || !strcasecmp(colorspace, "auto")) {
if (!strcasecmp(tmpcsp, "bt.709") || !strcasecmp(tmpcsp,"rec709"))
data->colorspace = BT709;
else if (!strcasecmp(tmpcsp, "bt.601") || !strcasecmp(tmpcsp, "rec601"))
data->colorspace = BT601;
else {
if (fi->vi.width > 1280 || fi->vi.height > 576)
data->colorspace = BT709;
else
data->colorspace = BT601;
} else if (!strcasecmp(colorspace, "bt.709") || !strcasecmp(colorspace,"rec709"))
data->colorspace = BT709;
else if (!strcasecmp(colorspace, "bt.601") || !strcasecmp(colorspace, "rec601"))
data->colorspace = BT601;
}

free(tmpcsp);

data->uv_tmp[0] = malloc(fi->vi.width * fi->vi.height);
data->uv_tmp[1] = malloc(fi->vi.width * fi->vi.height);
Expand Down
19 changes: 19 additions & 0 deletions src/sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

#include "assrender.h"

void ass_read_colorspace(const char *f, char *csp) {
char buf[BUFSIZ];
FILE *fh = fopen(f, "r");

if (!fh)
return;

while (fgets(buf, BUFSIZ - 1, fh) != NULL) {
if (buf[0] == 0 || buf[0] == '\n' || buf[0] == '\r')
continue;

if (sscanf(buf, "Video Colorspace: %s", csp) == 1)
break;

if (!strcmp(buf, "[Events]"))
break;
}
}

ASS_Track *parse_srt(const char *f, udata *ud, const char *srt_font)
{
char l[BUFSIZ], buf[BUFSIZ];
Expand Down
2 changes: 2 additions & 0 deletions src/sub.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
void ass_read_colorspace(const char *f, char *csp);

ASS_Track *parse_srt(const char *f, udata *ud, const char *srt_font);

void msg_callback(int level, const char *fmt, va_list va, void *data);
Expand Down

0 comments on commit c22e1f2

Please sign in to comment.