summaryrefslogtreecommitdiffstats
path: root/libhb
diff options
context:
space:
mode:
Diffstat (limited to 'libhb')
-rw-r--r--libhb/common.c78
-rw-r--r--libhb/common.h35
-rw-r--r--libhb/decssasub.c381
-rw-r--r--libhb/decvobsub.c8
-rw-r--r--libhb/encavcodec.c23
-rw-r--r--libhb/hb.c13
-rw-r--r--libhb/internal.h5
-rw-r--r--libhb/module.defs8
-rw-r--r--libhb/render.c19
-rw-r--r--libhb/stream.c50
-rw-r--r--libhb/sync.c56
-rw-r--r--libhb/work.c8
12 files changed, 594 insertions, 90 deletions
diff --git a/libhb/common.c b/libhb/common.c
index f7b9ee384..35417cb6c 100644
--- a/libhb/common.c
+++ b/libhb/common.c
@@ -730,6 +730,7 @@ hb_title_t * hb_title_init( char * path, int index )
t->list_audio = hb_list_init();
t->list_chapter = hb_list_init();
t->list_subtitle = hb_list_init();
+ t->list_attachment = hb_list_init();
strcat( t->path, path );
// default to decoding mpeg2
t->video_id = 0xE0;
@@ -749,6 +750,7 @@ void hb_title_close( hb_title_t ** _t )
hb_audio_t * audio;
hb_chapter_t * chapter;
hb_subtitle_t * subtitle;
+ hb_attachment_t * attachment;
while( ( audio = hb_list_item( t->list_audio, 0 ) ) )
{
@@ -767,9 +769,31 @@ void hb_title_close( hb_title_t ** _t )
while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) )
{
hb_list_rem( t->list_subtitle, subtitle );
+ if ( subtitle->extradata )
+ {
+ free( subtitle->extradata );
+ subtitle->extradata = NULL;
+ }
free( subtitle );
}
hb_list_close( &t->list_subtitle );
+
+ while( ( attachment = hb_list_item( t->list_attachment, 0 ) ) )
+ {
+ hb_list_rem( t->list_attachment, attachment );
+ if ( attachment->name )
+ {
+ free( attachment->name );
+ attachment->name = NULL;
+ }
+ if ( attachment->data )
+ {
+ free( attachment->data );
+ attachment->data = NULL;
+ }
+ free( attachment );
+ }
+ hb_list_close( &t->list_attachment );
if( t->metadata )
{
@@ -924,6 +948,11 @@ hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
{
subtitle = calloc(1, sizeof(*subtitle));
memcpy(subtitle, src, sizeof(*subtitle));
+ if ( src->extradata )
+ {
+ subtitle->extradata = malloc( src->extradata_size );
+ memcpy( subtitle->extradata, src->extradata, src->extradata_size );
+ }
}
return subtitle;
}
@@ -1022,6 +1051,32 @@ char * hb_strdup_printf( char * fmt, ... )
}
/**********************************************************************
+ * hb_attachment_copy
+ **********************************************************************
+ *
+ *********************************************************************/
+hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src)
+{
+ hb_attachment_t *attachment = NULL;
+
+ if( src )
+ {
+ attachment = calloc(1, sizeof(*attachment));
+ memcpy(attachment, src, sizeof(*attachment));
+ if ( src->name )
+ {
+ attachment->name = strdup( src->name );
+ }
+ if ( src->data )
+ {
+ attachment->data = malloc( src->size );
+ memcpy( attachment->data, src->data, src->size );
+ }
+ }
+ return attachment;
+}
+
+/**********************************************************************
* hb_yuv2rgb
**********************************************************************
* Converts a YCbCr pixel to an RGB pixel.
@@ -1089,3 +1144,26 @@ int hb_rgb2yuv(int rgb)
return (y << 16) | (Cb << 8) | Cr;
}
+const char * hb_subsource_name( int source )
+{
+ switch (source)
+ {
+ case VOBSUB:
+ return "VOBSUB";
+ case SRTSUB:
+ return "SRT";
+ case CC608SUB:
+ return "CC";
+ case CC708SUB:
+ return "CC";
+ case UTF8SUB:
+ return "UTF-8";
+ case TX3GSUB:
+ return "TX3G";
+ case SSASUB:
+ return "SSA";
+ default:
+ return "Unknown";
+ }
+}
+
diff --git a/libhb/common.h b/libhb/common.h
index b59cba515..bba48350c 100644
--- a/libhb/common.h
+++ b/libhb/common.h
@@ -63,6 +63,7 @@ typedef struct hb_audio_s hb_audio_t;
typedef struct hb_audio_config_s hb_audio_config_t;
typedef struct hb_subtitle_s hb_subtitle_t;
typedef struct hb_subtitle_config_s hb_subtitle_config_t;
+typedef struct hb_attachment_s hb_attachment_t;
typedef struct hb_metadata_s hb_metadata_t;
typedef struct hb_state_s hb_state_t;
typedef union hb_esconfig_u hb_esconfig_t;
@@ -102,10 +103,12 @@ void hb_audio_config_init(hb_audio_config_t * audiocfg);
int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg);
hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i);
+hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src);
int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track);
int hb_srt_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg,
const char *lang);
+hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src);
struct hb_rate_s
{
@@ -194,8 +197,8 @@ struct hb_job_s
int itu_par;
int par_width;
int par_height;
- int dar_width;
- int dar_height;
+ int dar_width; // 0 if normal
+ int dar_height; // 0 if normal
int keep_display_aspect;
} anamorphic;
@@ -237,8 +240,7 @@ struct hb_job_s
/* List of audio settings. */
hb_list_t * list_audio;
- /* Subtitles
- */
+ /* Subtitles */
hb_list_t * list_subtitle;
/* Muxer settings
@@ -488,9 +490,8 @@ struct hb_chapter_s
* > config.dest
* - whether to render the subtitle on the video track (RENDERSUB) or
* to pass it through its own subtitle track in the output container (PASSTHRUSUB)
- * - for legacy compatibility, all newly created VOBSUB tracks should default to RENDERSUB
- * - since only VOBSUBs are renderable (as of 2010-04-25), all other newly created
- * subtitle track types should default to PASSTHRUSUB
+ * - all newly created non-VOBSUB tracks should default to PASSTHRUSUB
+ * - all newly created VOBSUB tracks should default to RENDERSUB, for legacy compatibility
* > lang
* - user-readable description of the subtitle track
* - may correspond to the language of the track (see the 'iso639_2' field)
@@ -516,6 +517,10 @@ struct hb_subtitle_s
uint32_t palette[16];
int width;
int height;
+
+ // Codec private data for subtitles originating from FFMPEG sources
+ uint8_t * extradata;
+ int extradata_size;
int hits; /* How many hits/occurrences of this subtitle */
int forced_hits; /* How many forced hits in this subtitle */
@@ -530,6 +535,19 @@ struct hb_subtitle_s
#endif
};
+/*
+ * An attachment.
+ *
+ * These are usually used for attaching embedded fonts to movies containing SSA subtitles.
+ */
+struct hb_attachment_s
+{
+ enum attachtype { FONT_TTF_ATTACH } type;
+ char * name;
+ char * data;
+ int size;
+};
+
struct hb_metadata_s
{
char name[255];
@@ -593,6 +611,7 @@ struct hb_title_s
hb_list_t * list_chapter;
hb_list_t * list_audio;
hb_list_t * list_subtitle;
+ hb_list_t * list_attachment;
/* Job template for this title */
hb_job_t * job;
@@ -800,4 +819,6 @@ char * hb_strdup_printf( char * fmt, ... );
int hb_yuv2rgb(int yuv);
int hb_rgb2yuv(int rgb);
+const char * hb_subsource_name( int source );
+
#endif
diff --git a/libhb/decssasub.c b/libhb/decssasub.c
index 2b350dc47..99e4259a0 100644
--- a/libhb/decssasub.c
+++ b/libhb/decssasub.c
@@ -4,13 +4,19 @@
It may be used under the terms of the GNU General Public License. */
/*
- * Converts SSA subtitles to UTF-8 subtitles with limited HTML-style markup (<b>, <i>, <u>).
+ * Converts SSA subtitles to either:
+ * (1) TEXTSUB format: UTF-8 subtitles with limited HTML-style markup (<b>, <i>, <u>), or
+ * (2) PICTURESUB format, using libass.
*
* SSA format references:
* http://www.matroska.org/technical/specs/subtitles/ssa.html
* http://moodub.free.fr/video/ass-specs.doc
* vlc-1.0.4/modules/codec/subtitles/subsass.c:ParseSSAString
*
+ * libass references:
+ * libass-0.9.9/ass.h
+ * vlc-1.0.4/modules/codec/libass.c
+ *
* @author David Foster (davidfstr)
*/
@@ -18,6 +24,17 @@
#include <stdio.h>
#include "hb.h"
+#include <ass/ass.h>
+
+struct hb_work_private_s
+{
+ // If decoding to PICTURESUB format:
+ ASS_Library *ssa;
+ ASS_Renderer *renderer;
+ ASS_Track *ssaTrack;
+ int readOrder;
+};
+
typedef enum {
BOLD = 0x01,
ITALIC = 0x02,
@@ -92,14 +109,17 @@ static void ssa_append_html_tags_for_style_change(
#undef APPEND
}
-static hb_buffer_t *ssa_decode_to_utf8_line( uint8_t *in_data, int in_size );
+static hb_buffer_t *ssa_decode_line_to_utf8( uint8_t *in_data, int in_size, int in_sequence );
+static hb_buffer_t *ssa_decode_line_to_picture( hb_work_object_t * w, uint8_t *in_data, int in_size, int in_sequence );
/*
+ * Decodes a single SSA packet to one or more TEXTSUB or PICTURESUB subtitle packets.
+ *
* SSA packet format:
* ( Dialogue: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text CR LF ) +
* 1 2 3 4 5 6 7 8 9 10
*/
-static hb_buffer_t *ssa_decode_to_utf8( hb_buffer_t *in )
+static hb_buffer_t *ssa_decode_packet( hb_work_object_t * w, hb_buffer_t *in )
{
// Store NULL after the end of the buffer to make using string processing safe
hb_buffer_realloc( in, in->size + 1 );
@@ -119,22 +139,31 @@ static hb_buffer_t *ssa_decode_to_utf8( hb_buffer_t *in )
continue;
// Decode an individual SSA line
- hb_buffer_t *out = ssa_decode_to_utf8_line( (uint8_t*)curLine, strlen( curLine ) );
-
- // We shouldn't be storing the extra NULL character,
- // but the MP4 muxer expects this, unfortunately.
- if ( out->size > 0 && out->data[out->size - 1] != '\0' ) {
- // NOTE: out->size remains unchanged
- hb_buffer_realloc( out, out->size + 1 );
- out->data[out->size] = '\0';
- }
-
- // If the input packet was non-empty, do not pass through
- // an empty output packet (even if the subtitle was empty),
- // as this would be interpreted as an end-of-stream
- if ( in->size > 0 && out->size == 0 ) {
- hb_buffer_close(&out);
- continue;
+ hb_buffer_t *out;
+ if ( w->subtitle->config.dest == PASSTHRUSUB ) {
+ out = ssa_decode_line_to_utf8( (uint8_t *) curLine, strlen( curLine ), in->sequence );
+ if ( out == NULL )
+ continue;
+
+ // We shouldn't be storing the extra NULL character,
+ // but the MP4 muxer expects this, unfortunately.
+ if ( out->size > 0 && out->data[out->size - 1] != '\0' ) {
+ // NOTE: out->size remains unchanged
+ hb_buffer_realloc( out, out->size + 1 );
+ out->data[out->size] = '\0';
+ }
+
+ // If the input packet was non-empty, do not pass through
+ // an empty output packet (even if the subtitle was empty),
+ // as this would be interpreted as an end-of-stream
+ if ( in->size > 0 && out->size == 0 ) {
+ hb_buffer_close(&out);
+ continue;
+ }
+ } else if ( w->subtitle->config.dest == RENDERSUB ) {
+ out = ssa_decode_line_to_picture( w, (uint8_t *) curLine, strlen( curLine ), in->sequence );
+ if ( out == NULL )
+ continue;
}
// Append 'out' to 'out_list'
@@ -146,50 +175,68 @@ static hb_buffer_t *ssa_decode_to_utf8( hb_buffer_t *in )
}
/*
- * SSA line format:
- * Dialogue: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text '\0'
- * 1 2 3 4 5 6 7 8 9 10
+ * Parses the start and stop time from the specified SSA packet.
+ *
+ * Returns true if parsing failed; false otherwise.
*/
-static hb_buffer_t *ssa_decode_to_utf8_line( uint8_t *in_data, int in_size )
+static int parse_timing_from_ssa_packet( char *in_data, int64_t *in_start, int64_t *in_stop )
{
- uint8_t *pos = in_data;
- uint8_t *end = in_data + in_size;
-
/*
* Parse Start and End fields for timing information
*/
int start_hr, start_min, start_sec, start_centi;
int end_hr, end_min, end_sec, end_centi;
- int numPartsRead = sscanf( (char *) in_data, "%*128[^,],"
+ int numPartsRead = sscanf( (char *) in_data, "Dialogue: %*128[^,],"
"%d:%d:%d.%d," // Start
"%d:%d:%d.%d,", // End
&start_hr, &start_min, &start_sec, &start_centi,
&end_hr, &end_min, &end_sec, &end_centi );
if ( numPartsRead != 8 )
- goto fail;
+ return 1;
- int64_t in_start = SSA_2_HB_TIME(start_hr, start_min, start_sec, start_centi);
- int64_t in_stop = SSA_2_HB_TIME( end_hr, end_min, end_sec, end_centi);
+ *in_start = SSA_2_HB_TIME(start_hr, start_min, start_sec, start_centi);
+ *in_stop = SSA_2_HB_TIME( end_hr, end_min, end_sec, end_centi);
- /*
- * Advance 'pos' to the beginning of the Text field
- */
+ return 0;
+}
+
+static uint8_t *find_field( uint8_t *pos, uint8_t *end, int fieldNum )
+{
int curFieldID = 1;
while (pos < end)
{
if ( *pos++ == ',' )
{
curFieldID++;
- if ( curFieldID == 10 ) // Text
- break;
+ if ( curFieldID == fieldNum )
+ return pos;
}
}
- if ( curFieldID != 10 )
+ return NULL;
+}
+
+/*
+ * SSA line format:
+ * Dialogue: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text '\0'
+ * 1 2 3 4 5 6 7 8 9 10
+ */
+static hb_buffer_t *ssa_decode_line_to_utf8( uint8_t *in_data, int in_size, int in_sequence )
+{
+ uint8_t *pos = in_data;
+ uint8_t *end = in_data + in_size;
+
+ // Parse values for in->start and in->stop
+ int64_t in_start, in_stop;
+ if ( parse_timing_from_ssa_packet( (char *) in_data, &in_start, &in_stop ) )
+ goto fail;
+
+ uint8_t *textFieldPos = find_field( pos, end, 10 );
+ if ( textFieldPos == NULL )
goto fail;
- uint8_t *textFieldPos = pos;
// Count the number of style overrides in the Text field
int numStyleOverrides = 0;
+ pos = textFieldPos;
while ( pos < end )
{
if (*pos++ == '{')
@@ -207,7 +254,7 @@ static hb_buffer_t *ssa_decode_to_utf8_line( uint8_t *in_data, int in_size )
* The Text field contains plain text marked up with:
* (1) '\n' -> space
* (2) '\N' -> newline
- * (3) curly-brace control codes like '{\k44}' -> empty (strip them)
+ * (3) curly-brace control codes like '{\k44}' -> HTML tags / strip
*
* Perform the above conversions and copy it to the output packet
*/
@@ -253,6 +300,7 @@ static hb_buffer_t *ssa_decode_to_utf8_line( uint8_t *in_data, int in_size )
// Copy metadata from the input packet to the output packet
out->start = in_start;
out->stop = in_stop;
+ out->sequence = in_sequence;
return out;
@@ -261,8 +309,252 @@ fail:
return NULL;
}
+/*
+ * SSA line format:
+ * Dialogue: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text '\0'
+ * 1 2 3 4 5 6 7 8 9 10
+ *
+ * MKV-SSA packet format:
+ * ReadOrder,Marked, Style,Name,MarginL,MarginR,MarginV,Effect,Text '\0'
+ * 1 2 3 4 5 6 7 8 9
+ */
+static hb_buffer_t *ssa_decode_line_to_picture( hb_work_object_t * w, uint8_t *in_data, int in_size, int in_sequence )
+{
+ hb_work_private_t * pv = w->private_data;
+
+ // Parse values for in->start and in->stop
+ int64_t in_start, in_stop;
+ if ( parse_timing_from_ssa_packet( (char *) in_data, &in_start, &in_stop ) )
+ goto fail;
+
+ // Convert the SSA packet to MKV-SSA format, which is what libass expects
+ char *mkvIn;
+ int mkvInSize;
+ {
+ char *layerField = malloc( in_size );
+ int numPartsRead = sscanf( (char *) in_data, "Dialogue: %128[^,],", layerField );
+ if ( numPartsRead != 1 )
+ goto fail;
+
+ char *styleToTextFields = (char *) find_field( in_data, in_data + in_size, 4 );
+ if ( styleToTextFields == NULL ) {
+ free( layerField );
+ goto fail;
+ }
+
+ mkvIn = malloc( in_size + 1 );
+ mkvIn[0] = '\0';
+ sprintf(mkvIn, "%d", pv->readOrder++); // ReadOrder: make this up
+ strcat( mkvIn, "," );
+ strcat( mkvIn, layerField );
+ strcat( mkvIn, "," );
+ strcat( mkvIn, (char *) styleToTextFields );
+
+ mkvInSize = strlen(mkvIn);
+
+ free( layerField );
+ }
+
+ // Parse MKV-SSA packet
+ ass_process_chunk( pv->ssaTrack, mkvIn, mkvInSize, in_start / 90, (in_stop - in_start) / 90 );
+
+ free( mkvIn );
+
+ // TODO: To support things like karaoke, it won't be sufficient to only generate
+ // new subtitle pictures when there are subtitle packets. Rather, pictures will
+ // need to be generated potentially continuously.
+ //
+ // Until "karaoke support" is implemented, make an educated guess about the
+ // timepoint within the subtitle that should be rendered. I guess the midpoint.
+ int64_t renderTime = ( in_start + in_stop ) / 2;
+
+ int changed;
+ ASS_Image *frameList = ass_render_frame( pv->renderer, pv->ssaTrack, renderTime / 90, &changed );
+ if ( !changed && !frameList )
+ return NULL;
+
+ int numFrames = 0;
+ ASS_Image *curFrame;
+ for (curFrame = frameList; curFrame; curFrame = curFrame->next)
+ numFrames++;
+
+ hb_buffer_t *outSubpictureList = NULL;
+ hb_buffer_t **outSubpictureListTailPtr = &outSubpictureList;
+
+ // Generate a PICTURESUB packet from the frames
+ ASS_Image *frame;
+ for (frame = frameList; frame; frame = frame->next) {
+ // Allocate pixmap where drawing will be done
+ uint8_t *rgba = calloc(frame->w * frame->h * 4, 1);
+
+ unsigned r = (frame->color >> 24) & 0xff;
+ unsigned g = (frame->color >> 16) & 0xff;
+ unsigned b = (frame->color >> 8) & 0xff;
+ unsigned a = (frame->color ) & 0xff;
+
+ int x, y;
+ for (y = 0; y < frame->h; y++) {
+ for (x = 0; x < frame->w; x++) {
+ unsigned srcAlphaPrenormalized = frame->bitmap[y*frame->stride + x];
+ unsigned srcAlpha = (255 - a) * srcAlphaPrenormalized / 255;
+
+ uint8_t *dst = &rgba[(y*frame->w + x) * 4];
+ unsigned oldDstAlpha = dst[3];
+
+ if (oldDstAlpha == 0) {
+ // Optimized version
+ dst[0] = r;
+ dst[1] = g;
+ dst[2] = b;
+ dst[3] = srcAlpha;
+ } else {
+ dst[3] = 255 - ( 255 - dst[3] ) * ( 255 - srcAlpha ) / 255;
+ if (dst[3] != 0) {
+ dst[0] = ( dst[0] * oldDstAlpha * (255-srcAlpha) / 255 + r * srcAlpha ) / dst[3];
+ dst[1] = ( dst[1] * oldDstAlpha * (255-srcAlpha) / 255 + g * srcAlpha ) / dst[3];
+ dst[2] = ( dst[2] * oldDstAlpha * (255-srcAlpha) / 255 + b * srcAlpha ) / dst[3];
+ }
+ }
+ }
+ }
+
+ // Generate output subpicture (in PICTURESUB format)
+ hb_buffer_t *out = hb_buffer_init(frame->w * frame->h * 4);
+ out->x = frame->dst_x;
+ out->y = frame->dst_y;
+ out->width = frame->w;
+ out->height = frame->h;
+
+ int i;
+ int numPixels = frame->w * frame->h;
+ for (i = 0; i < numPixels; i++) {
+ uint8_t *srcRgba = &rgba[i * 4];
+
+ uint8_t *dstY = &out->data[(numPixels * 0) + i];
+ uint8_t *dstA = &out->data[(numPixels * 1) + i];
+ uint8_t *dstU = &out->data[(numPixels * 2) + i];
+ uint8_t *dstV = &out->data[(numPixels * 3) + i];
+
+ int srcYuv = hb_rgb2yuv((srcRgba[0] << 16) | (srcRgba[1] << 8) | (srcRgba[2] << 0));
+ int srcA = srcRgba[3];
+
+ *dstY = (srcYuv >> 16) & 0xff;
+ *dstU = (srcYuv >> 8 ) & 0xff;
+ *dstV = (srcYuv >> 0 ) & 0xff;
+ *dstA = srcA / 16; // HB's max alpha value is 16
+ }
+
+ free(rgba);
+
+ *outSubpictureListTailPtr = out;
+ outSubpictureListTailPtr = &out->next_subpicture;
+ }
+
+ // NOTE: The subpicture list is actually considered a single packet by most other code
+ hb_buffer_t *out = outSubpictureList;
+
+ // Copy metadata from the input packet to the output packet
+ out->start = in_start;
+ out->stop = in_stop;
+ out->sequence = in_sequence;
+
+ return out;
+
+fail:
+ hb_log( "decssasub: malformed SSA subtitle packet: %.*s\n", in_size, in_data );
+ return NULL;
+}
+
+static void ssa_log(int level, const char *fmt, va_list args, void *data)
+{
+ if ( level < 5 ) // same as default verbosity when no callback is set
+ {
+ char *msg;
+ if ( vasprintf( &msg, fmt, args ) < 0 )
+ {
+ hb_log( "decssasub: could not report libass message\n" );
+ return;
+ }
+ hb_log( "[ass] %s", msg ); // no need for extra '\n' because libass sends it
+
+ free( msg );
+ }
+}
+
static int decssaInit( hb_work_object_t * w, hb_job_t * job )
{
+ hb_work_private_t * pv;
+
+ pv = calloc( 1, sizeof( hb_work_private_t ) );
+ w->private_data = pv;
+
+ if ( w->subtitle->config.dest == RENDERSUB ) {
+ pv->ssa = ass_library_init();
+ if ( !pv->ssa ) {
+ hb_log( "decssasub: libass initialization failed\n" );
+ return 1;
+ }
+
+ // Redirect libass output to hb_log
+ ass_set_message_cb( pv->ssa, ssa_log, NULL );
+
+ // Load embedded fonts
+ hb_list_t * list_attachment = job->title->list_attachment;
+ int i;
+ for ( i = 0; i < hb_list_count(list_attachment); i++ )
+ {
+ hb_attachment_t * attachment = hb_list_item( list_attachment, i );
+
+ if ( attachment->type == FONT_TTF_ATTACH )
+ {
+ ass_add_font(
+ pv->ssa,
+ attachment->name,
+ attachment->data,
+ attachment->size );
+ }
+ }
+
+ ass_set_extract_fonts( pv->ssa, 1 );
+ ass_set_style_overrides( pv->ssa, NULL );
+
+ pv->renderer = ass_renderer_init( pv->ssa );
+ if ( !pv->renderer ) {
+ hb_log( "decssasub: renderer initialization failed\n" );
+ return 1;
+ }
+
+ ass_set_use_margins( pv->renderer, 0 );
+ ass_set_hinting( pv->renderer, ASS_HINTING_LIGHT ); // VLC 1.0.4 uses this
+ ass_set_font_scale( pv->renderer, 1.0 );
+ ass_set_line_spacing( pv->renderer, 1.0 );
+
+ // Setup default font family
+ //
+ // SSA v4.00 requires that "Arial" be the default font
+ const char *font = NULL;
+ const char *family = "Arial";
+ // NOTE: This can sometimes block for several *seconds*.
+ // It seems that process_fontdata() for some embedded fonts is slow.
+ ass_set_fonts( pv->renderer, font, family, /*haveFontConfig=*/1, NULL, 1 );
+
+ // Setup track state
+ pv->ssaTrack = ass_new_track( pv->ssa );
+ if ( !pv->ssaTrack ) {
+ hb_log( "decssasub: ssa track initialization failed\n" );
+ return 1;
+ }
+
+ // NOTE: The codec extradata is expected to be in MKV format
+ ass_process_codec_private( pv->ssaTrack,
+ (char *) w->subtitle->extradata, w->subtitle->extradata_size );
+
+ int originalWidth = job->title->width;
+ int originalHeight = job->title->height;
+ ass_set_frame_size( pv->renderer, originalWidth, originalHeight);
+ ass_set_aspect_ratio( pv->renderer, /*dar=*/1.0, /*sar=*/1.0 );
+ }
+
return 0;
}
@@ -273,7 +565,7 @@ static int decssaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_buffer_t * out_list = NULL;
if ( in->size > 0 ) {
- out_list = ssa_decode_to_utf8(in);
+ out_list = ssa_decode_packet(w, in);
} else {
out_list = hb_buffer_init( 0 );
}
@@ -288,7 +580,16 @@ static int decssaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
static void decssaClose( hb_work_object_t * w )
{
- // nothing
+ hb_work_private_t * pv = w->private_data;
+
+ if ( pv->ssaTrack )
+ ass_free_track( pv->ssaTrack );
+ if ( pv->renderer )
+ ass_renderer_done( pv->renderer );
+ if ( pv->ssa )
+ ass_library_done( pv->ssa );
+
+ free( w->private_data );
}
hb_work_object_t hb_decssasub =
diff --git a/libhb/decvobsub.c b/libhb/decvobsub.c
index 3b5177ca3..fc4a9e24f 100644
--- a/libhb/decvobsub.c
+++ b/libhb/decvobsub.c
@@ -17,10 +17,10 @@
*
* Output format of this decoder is PICTURESUB, which is:
* struct PictureSubPacket {
- * uint8_t lum[pixelCount];
- * uint8_t alpha[pixelCount];
- * uint8_t chromaU[pixelCount];
- * uint8_t chromaV[pixelCount];
+ * uint8_t lum[pixelCount]; // Y
+ * uint8_t alpha[pixelCount]; // alpha (max = 16)
+ * uint8_t chromaU[pixelCount]; // Cb
+ * uint8_t chromaV[pixelCount]; // Cr
* }
*/
diff --git a/libhb/encavcodec.c b/libhb/encavcodec.c
index 22eb286d5..5ae5a3929 100644
--- a/libhb/encavcodec.c
+++ b/libhb/encavcodec.c
@@ -235,13 +235,22 @@ int encavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
// doesn't do the trick. It must be set in the AVFrame.
frame->quality = pv->context->global_quality;
- /* Should be way too large */
- buf = hb_video_buffer_init( job->width, job->height );
- buf->size = avcodec_encode_video( pv->context, buf->data, buf->alloc,
- frame );
- buf->start = in->start;
- buf->stop = in->stop;
- buf->frametype = pv->context->coded_frame->key_frame ? HB_FRAME_KEY : HB_FRAME_REF;
+ if ( pv->context->codec )
+ {
+ /* Should be way too large */
+ buf = hb_video_buffer_init( job->width, job->height );
+ buf->size = avcodec_encode_video( pv->context, buf->data, buf->alloc,
+ frame );
+ buf->start = in->start;
+ buf->stop = in->stop;
+ buf->frametype = pv->context->coded_frame->key_frame ? HB_FRAME_KEY : HB_FRAME_REF;
+ }
+ else
+ {
+ buf = NULL;
+
+ hb_error( "encavcodec: codec context has uninitialized codec; skipping frame" );
+ }
av_free( frame );
diff --git a/libhb/hb.c b/libhb/hb.c
index 11b80357e..36fcd75c9 100644
--- a/libhb/hb.c
+++ b/libhb/hb.c
@@ -1225,6 +1225,7 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
hb_chapter_t * chapter, * chapter_copy;
hb_audio_t * audio;
hb_subtitle_t * subtitle, * subtitle_copy;
+ hb_attachment_t * attachment;
int i;
char audio_lang[4];
@@ -1272,7 +1273,6 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
/* Copy the audio track(s) we want */
title_copy->list_audio = hb_list_init();
-
for( i = 0; i < hb_list_count(job->list_audio); i++ )
{
if( ( audio = hb_list_item( job->list_audio, i ) ) )
@@ -1281,7 +1281,18 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
}
}
+ /* Initialize subtitle list - filled out further below */
title_copy->list_subtitle = hb_list_init();
+
+ /* Copy all the attachments */
+ title_copy->list_attachment = hb_list_init();
+ for( i = 0; i < hb_list_count(title->list_attachment); i++ )
+ {
+ if( ( attachment = hb_list_item( title->list_attachment, i ) ) )
+ {
+ hb_list_add( title_copy->list_attachment, hb_attachment_copy(attachment) );
+ }
+ }
/*
* The following code is confusing, there are two ways in which
diff --git a/libhb/internal.h b/libhb/internal.h
index fe1cdca26..30bf022d5 100644
--- a/libhb/internal.h
+++ b/libhb/internal.h
@@ -85,15 +85,16 @@ struct hb_buffer_s
/* Holds the output PTS from x264, for use by b-frame offsets in muxmp4.c */
int64_t renderOffset;
- // VOB subtitle packets:
+ // PICTURESUB subtitle packets:
// Location and size of the subpicture.
int x;
int y;
int width;
int height;
+ hb_buffer_t * next_subpicture;
// Video packets (after processing by the hb_sync_video work-object):
- // A (copy of a) VOB subtitle packet that needs to be burned into this video packet by the hb_render work-object.
+ // A (copy of a) PICTURESUB subtitle packet that needs to be burned into this video packet by the hb_render work-object.
// Subtitles that are simply passed thru are NOT attached to the associated video packets.
hb_buffer_t * sub;
diff --git a/libhb/module.defs b/libhb/module.defs
index c6fb9dceb..4759a921a 100644
--- a/libhb/module.defs
+++ b/libhb/module.defs
@@ -1,5 +1,5 @@
-__deps__ := A52DEC BZIP2 FAAC FAAD2 FFMPEG LAME LIBDCA \
- LIBDVDREAD LIBDVDNAV LIBICONV LIBMKV LIBOGG LIBSAMPLERATE LIBTHEORA LIBVORBIS \
+__deps__ := A52DEC BZIP2 FAAC FAAD2 FFMPEG FONTCONFIG FREETYPE LAME LIBASS LIBDCA \
+ LIBDVDREAD LIBDVDNAV LIBICONV LIBMKV LIBOGG LIBSAMPLERATE LIBTHEORA LIBVORBIS LIBXML2 \
MP4V2 MPEG2DEC PTHREADW32 X264 ZLIB LIBBLURAY
$(eval $(call import.MODULE.defs,LIBHB,libhb,$(__deps__)))
@@ -89,8 +89,8 @@ LIBHB.dll = $(LIBHB.build/)hb.dll
LIBHB.lib = $(LIBHB.build/)hb.lib
LIBHB.dll.libs = $(foreach n, \
- a52 avcore avcodec avformat avutil dca dvdnav dvdread faac faad mkv mpeg2 mp3lame mp4v2 \
- ogg samplerate swscale theora vorbis vorbisenc x264 bluray, \
+ a52 ass avcore avcodec avformat avutil dca dvdnav dvdread faac faad fontconfig freetype mkv mpeg2 mp3lame mp4v2 \
+ ogg samplerate swscale theora vorbis vorbisenc x264 xml2 bluray, \
$(CONTRIB.build/)lib/lib$(n).a )
ifneq ($(HAS.iconv),1)
diff --git a/libhb/render.c b/libhb/render.c
index 6888a1e0a..98bc7fea3 100644
--- a/libhb/render.c
+++ b/libhb/render.c
@@ -472,8 +472,14 @@ int renderWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
pv->dropped_frames++;
/* Pop the frame's subtitle and dispose of it. */
- hb_buffer_t * subtitles = hb_fifo_get( pv->subtitle_queue );
- hb_buffer_close( &subtitles );
+ hb_buffer_t * subpicture_list = hb_fifo_get( pv->subtitle_queue );
+ hb_buffer_t * subpicture;
+ hb_buffer_t * subpicture_next;
+ for ( subpicture = subpicture_list; subpicture; subpicture = subpicture_next )
+ {
+ subpicture_next = subpicture->next_subpicture;
+ hb_buffer_close( &subpicture );
+ }
buf_tmp_in = NULL;
break;
}
@@ -500,10 +506,13 @@ int renderWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
/* Apply subtitles */
if( buf_tmp_in )
{
- hb_buffer_t * subtitles = hb_fifo_get( pv->subtitle_queue );
- if( subtitles )
+ hb_buffer_t * subpicture_list = hb_fifo_get( pv->subtitle_queue );
+ hb_buffer_t * subpicture;
+ hb_buffer_t * subpicture_next;
+ for ( subpicture = subpicture_list; subpicture; subpicture = subpicture_next )
{
- ApplySub( job, buf_tmp_in, &subtitles );
+ subpicture_next = subpicture->next_subpicture;
+ ApplySub( job, buf_tmp_in, &subpicture );
}
}
diff --git a/libhb/stream.c b/libhb/stream.c
index 58b2506f0..85a891c7b 100644
--- a/libhb/stream.c
+++ b/libhb/stream.c
@@ -3181,9 +3181,55 @@ static void add_ffmpeg_subtitle( hb_title_t *title, hb_stream_t *stream, int id
strcpy( subtitle->lang, language->eng_name );
strncpy( subtitle->iso639_2, language->iso639_2, 4 );
+ // Copy the extradata for the subtitle track
+ subtitle->extradata = malloc( codec->extradata_size );
+ memcpy( subtitle->extradata, codec->extradata, codec->extradata_size );
+ subtitle->extradata_size = codec->extradata_size;
+
hb_list_add(title->list_subtitle, subtitle);
}
+static char *get_ffmpeg_metadata_value( AVMetadata *m, char *key )
+{
+ AVMetadataTag *tag = NULL;
+ while ( tag = av_metadata_get(m, "", tag, AV_METADATA_IGNORE_SUFFIX) )
+ {
+ if ( !strcmp( key, tag->key ) )
+ {
+ return tag->value;
+ }
+ }
+ return NULL;
+}
+
+static void add_ffmpeg_attachment( hb_title_t *title, hb_stream_t *stream, int id )
+{
+ AVStream *st = stream->ffmpeg_ic->streams[id];
+ AVCodecContext *codec = st->codec;
+
+ enum attachtype type;
+ switch ( codec->codec_id )
+ {
+ case CODEC_ID_TTF:
+ type = FONT_TTF_ATTACH;
+ break;
+ default:
+ // Ignore unrecognized attachment type
+ return;
+ }
+
+ hb_attachment_t *attachment = calloc( 1, sizeof(*attachment) );
+
+ // Copy the attachment name and data
+ attachment->type = type;
+ attachment->name = strdup( get_ffmpeg_metadata_value( st->metadata, "filename" ) );
+ attachment->data = malloc( codec->extradata_size );
+ memcpy( attachment->data, codec->extradata, codec->extradata_size );
+ attachment->size = codec->extradata_size;
+
+ hb_list_add(title->list_attachment, attachment);
+}
+
static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream )
{
AVFormatContext *ic = stream->ffmpeg_ic;
@@ -3244,6 +3290,10 @@ static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream )
{
add_ffmpeg_subtitle( title, stream, i );
}
+ else if ( ic->streams[i]->codec->codec_type == CODEC_TYPE_ATTACHMENT )
+ {
+ add_ffmpeg_attachment( title, stream, i );
+ }
}
title->container_name = strdup( ic->iformat->name );
diff --git a/libhb/sync.c b/libhb/sync.c
index 53b3193a3..58628f316 100644
--- a/libhb/sync.c
+++ b/libhb/sync.c
@@ -247,6 +247,8 @@ void syncVideoClose( hb_work_object_t * w )
***********************************************************************
*
**********************************************************************/
+static hb_buffer_t * copy_subtitle( hb_buffer_t * src );
+
int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_buffer_t ** buf_out )
{
@@ -562,12 +564,10 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
/*
* Rewrite timestamps on subtitles that need it (on raw queue).
*/
- if( subtitle->source == CC608SUB ||
- subtitle->source == CC708SUB ||
- subtitle->source == SRTSUB ||
- subtitle->source == UTF8SUB ||
- subtitle->source == TX3GSUB ||
- subtitle->source == SSASUB)
+ // NOTE: It's probably fine to use this logic for passthru VOBSUBs as well,
+ // but I am currently preserving backwards compatibility with the old
+ // VOBSUB behavior, which uses the more complex logic following this if-statement.
+ if( subtitle->config.dest == PASSTHRUSUB && subtitle->source != VOBSUB )
{
/*
* Rewrite timestamps on subtitles that came from Closed Captions
@@ -611,9 +611,13 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
}
}
}
+
+ continue;
}
- if( subtitle->source == VOBSUB )
+ // For rendered subtitles (and, for backward compatibility, passthru VOBSUBs),
+ // delay pushing subtitle packets through the pipeline until the video catches up
+ if( subtitle->config.dest == RENDERSUB || subtitle->source == VOBSUB )
{
hb_buffer_t * sub2;
while( ( sub = hb_fifo_see( subtitle->fifo_raw ) ) )
@@ -681,7 +685,7 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
* Subtitle is on for less than three
* seconds, extend the time that it is
* displayed to make it easier to read.
- * Make it 3 seconds or until the next
+ * Make it 2 seconds or until the next
* subtitle is displayed.
*
* This is in response to Indochine which
@@ -760,24 +764,23 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
}
/* If we have a subtitle for this picture, copy it */
- /* FIXME: we should avoid this memcpy */
if( sub )
{
if( sub->size > 0 )
{
if( subtitle->config.dest == RENDERSUB )
{
+ // Only allow one subtitle to be showing at once; ignore others
if ( cur->sub == NULL )
{
/*
* Tack onto the video buffer for rendering
*/
- cur->sub = hb_buffer_init( sub->size );
- cur->sub->x = sub->x;
- cur->sub->y = sub->y;
- cur->sub->width = sub->width;
- cur->sub->height = sub->height;
- memcpy( cur->sub->data, sub->data, sub->size );
+ /* FIXME: we should avoid this memcpy */
+ cur->sub = copy_subtitle( sub );
+
+ // Leave the subtitle on the raw queue
+ // (until it no longer needs to be displayed)
}
} else {
/*
@@ -846,6 +849,29 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
return HB_WORK_OK;
}
+static hb_buffer_t * copy_subtitle( hb_buffer_t * src_list )
+{
+ hb_buffer_t * dst_list = NULL;
+
+ hb_buffer_t * src;
+ hb_buffer_t * dst;
+ hb_buffer_t ** dst_ptr = &dst_list;
+ for ( src = src_list, dst_ptr = &dst_list;
+ src;
+ src = src->next_subpicture, dst_ptr = &dst->next_subpicture )
+ {
+ (*dst_ptr) = hb_buffer_init( src->size );
+ dst = (*dst_ptr);
+ dst->x = src->x;
+ dst->y = src->y;
+ dst->width = src->width;
+ dst->height = src->height;
+ memcpy( dst->data, src->data, src->size );
+ }
+
+ return dst_list;
+}
+
// sync*Init does nothing because sync has a special initializer
// that takes care of initializing video and all audio tracks
int syncVideoInit( hb_work_object_t * w, hb_job_t * job)
diff --git a/libhb/work.c b/libhb/work.c
index 68bb6fe2f..3c3346d19 100644
--- a/libhb/work.c
+++ b/libhb/work.c
@@ -317,11 +317,7 @@ void hb_display_job_info( hb_job_t * job )
{
hb_log( " * subtitle track %i, %s (id %x) %s [%s] -> %s%s%s", subtitle->track, subtitle->lang, subtitle->id,
subtitle->format == PICTURESUB ? "Picture" : "Text",
- subtitle->source == VOBSUB ? "VOBSUB" :
- subtitle->source == CC608SUB || subtitle->source == CC708SUB ? "CC" :
- subtitle->source == UTF8SUB ? "UTF-8" :
- subtitle->source == TX3GSUB ? "TX3G" :
- subtitle->source == SSASUB ? "SSA" : "Unknown",
+ hb_subsource_name( subtitle->source ),
job->indepth_scan ? "Foreign Audio Search" :
subtitle->config.dest == RENDERSUB ? "Render/Burn in" : "Pass-Through",
subtitle->config.force ? ", Forced Only" : "",
@@ -422,6 +418,7 @@ static void do_job( hb_job_t * job, int cpu_count )
hb_audio_t * audio;
hb_subtitle_t * subtitle;
+ hb_attachment_t * attachment;
unsigned int subtitle_highest = 0;
unsigned int subtitle_highest_id = 0;
unsigned int subtitle_lowest = -1;
@@ -819,6 +816,7 @@ static void do_job( hb_job_t * job, int cpu_count )
w = hb_get_work( WORK_DECSSASUB );
w->fifo_in = subtitle->fifo_in;
w->fifo_out = subtitle->fifo_raw;
+ w->subtitle = subtitle;
hb_list_add( job->list_work, w );
}