Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ubitux committed Nov 24, 2012
1 parent 807fa71 commit 13bf4d6
Show file tree
Hide file tree
Showing 7 changed files with 715 additions and 24 deletions.
66 changes: 63 additions & 3 deletions libavcodec/assenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

#include "avcodec.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/internal.h"
#include "libavutil/mem.h"
#include "libavutil/subtitles.h"

static av_cold int ass_encode_init(AVCodecContext *avctx)
{
Expand All @@ -36,18 +38,76 @@ static av_cold int ass_encode_init(AVCodecContext *avctx)
return 0;
}

static void encode_ast_to_ass(AVBPrint *buf, const AVSubtitle *sub)
{
int i;
const AVSubtitleAST *ast = sub->rects[0]->ast;

if (ast->g_settings && ast->g_settings->name) av_bprintf(buf, "%s,", ast->g_settings->name);
else av_bprintf(buf, "Default,");

for (i = 0; i < ast->nb_chunks; i++) {
const AVSubtitleASTChunk *c = &ast->chunks[i];
int *p = c->p;

if (c->reset) {
switch (c->type) {
case AVSUBTITLE_AST_CHUNK_FONTNAME: av_bprintf(buf, "{\\fn}"); break;
case AVSUBTITLE_AST_CHUNK_FONTSIZE: av_bprintf(buf, "{\\fs}"); break;
case AVSUBTITLE_AST_CHUNK_COLOR: av_bprintf(buf, "{\\c}"); break;
case AVSUBTITLE_AST_CHUNK_COLOR_2: av_bprintf(buf, "{\\2c}"); break;
case AVSUBTITLE_AST_CHUNK_COLOR_OUTLINE: av_bprintf(buf, "{\\3c}"); break;
case AVSUBTITLE_AST_CHUNK_COLOR_BACK: av_bprintf(buf, "{\\4c}"); break;
case AVSUBTITLE_AST_CHUNK_BOLD: av_bprintf(buf, "{\\b}"); break;
case AVSUBTITLE_AST_CHUNK_ITALIC: av_bprintf(buf, "{\\i}"); break;
case AVSUBTITLE_AST_CHUNK_STRIKEOUT: av_bprintf(buf, "{\\s}"); break;
case AVSUBTITLE_AST_CHUNK_UNDERLINE: av_bprintf(buf, "{\\u}"); break;
case AVSUBTITLE_AST_CHUNK_ALIGNMENT: av_bprintf(buf, "{\\an}"); break;
}
} else {
switch (c->type) {
case AVSUBTITLE_AST_CHUNK_RAW_TEXT: av_bprintf(buf, "%s", c->s); break;
case AVSUBTITLE_AST_CHUNK_COMMENT: av_bprintf(buf, "{%s}", c->s); break;
case AVSUBTITLE_AST_CHUNK_TIMING: av_bprintf(buf, "{\\k%d}", c->i); break;
case AVSUBTITLE_AST_CHUNK_FONTNAME: av_bprintf(buf, "{\\fn%s}", c->s); break;
case AVSUBTITLE_AST_CHUNK_FONTSIZE: av_bprintf(buf, "{\\fs%d}", c->i); break;
case AVSUBTITLE_AST_CHUNK_COLOR: av_bprintf(buf, "{\\c&H%06X&}", c->u32 & 0xffffff); break;
case AVSUBTITLE_AST_CHUNK_COLOR_2: av_bprintf(buf, "{\\2c&H%06X&}", c->u32 & 0xffffff); break;
case AVSUBTITLE_AST_CHUNK_COLOR_OUTLINE: av_bprintf(buf, "{\\3c&H%06X&}", c->u32 & 0xffffff); break;
case AVSUBTITLE_AST_CHUNK_COLOR_BACK: av_bprintf(buf, "{\\4c&H%06X&}", c->u32 & 0xffffff); break;
case AVSUBTITLE_AST_CHUNK_BOLD: av_bprintf(buf, "{\\b%d}", c->i); break;
case AVSUBTITLE_AST_CHUNK_ITALIC: av_bprintf(buf, "{\\i%d}", c->i); break;
case AVSUBTITLE_AST_CHUNK_STRIKEOUT: av_bprintf(buf, "{\\s%d}", c->i); break;
case AVSUBTITLE_AST_CHUNK_UNDERLINE: av_bprintf(buf, "{\\u%d}", c->i); break;
case AVSUBTITLE_AST_CHUNK_ALIGNMENT: av_bprintf(buf, "{\\an%d}", c->i); break;
case AVSUBTITLE_AST_CHUNK_POSITION: av_bprintf(buf, "{\\pos(%d,%d)", p[0], p[1]); break;
case AVSUBTITLE_AST_CHUNK_MOVE: av_bprintf(buf, "{\\move(%d,%d,%d,%d)", p[0], p[1], p[2], p[3]); break;
case AVSUBTITLE_AST_CHUNK_LINEBREAK: av_bprintf(buf, "\\N"); break;
}
}
}
}

static int ass_encode_frame(AVCodecContext *avctx,
unsigned char *buf, int bufsize,
const AVSubtitle *sub)
{
int i, len, total_len = 0;

for (i=0; i<sub->num_rects; i++) {
if (sub->rects[i]->type != SUBTITLE_ASS) {

if (sub->rects[i]->type == SUBTITLE_AST) {
AVBPrint assbuf;

av_log(0,0,"assenc: encode AST\n");
av_bprint_init(&assbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
encode_ast_to_ass(&assbuf, sub);
len = av_strlcpy(buf+total_len, assbuf.str, bufsize-total_len);
av_bprint_finalize(&assbuf, NULL);
} else if (sub->rects[i]->type != SUBTITLE_ASS) {
av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
return -1;
}

} else
len = av_strlcpy(buf+total_len, sub->rects[i]->ass, bufsize-total_len);

if (len > bufsize-total_len-1) {
Expand Down
8 changes: 8 additions & 0 deletions libavcodec/avcodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "libavutil/log.h"
#include "libavutil/pixfmt.h"
#include "libavutil/rational.h"
#include "libavutil/subtitles.h"

#include "libavcodec/version.h"
/**
Expand Down Expand Up @@ -3393,6 +3394,11 @@ enum AVSubtitleType {
* authoritative. pict and text fields may contain approximations.
*/
SUBTITLE_ASS,

/**
* Abstract textual representation.
*/
SUBTITLE_AST = 0x1000,
};

#define AV_SUBTITLE_FLAG_FORCED 0x00000001
Expand Down Expand Up @@ -3421,6 +3427,8 @@ typedef struct AVSubtitleRect {
char *ass;

int flags;

AVSubtitleAST *ast;
} AVSubtitleRect;

typedef struct AVSubtitle {
Expand Down
Loading

0 comments on commit 13bf4d6

Please sign in to comment.