Skip to content

Commit

Permalink
libhb: refactor how extradata is stored, use a dynamic heap allocated…
Browse files Browse the repository at this point in the history
… buffer instead of a static one.
  • Loading branch information
galad87 committed Mar 13, 2024
1 parent 84cb918 commit 0c6793a
Show file tree
Hide file tree
Showing 25 changed files with 435 additions and 541 deletions.
64 changes: 8 additions & 56 deletions libhb/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "handbrake/handbrake.h"
#include "handbrake/hbffmpeg.h"
#include "handbrake/extradata.h"
#include "x264.h"
#include "handbrake/lang.h"
#include "handbrake/common.h"
Expand Down Expand Up @@ -4374,6 +4375,8 @@ static void job_clean( hb_job_t * job )
free(job->file);
job->file = NULL;

hb_buffer_close(&job->extradata);

// clean up chapter list
while( ( chapter = hb_list_item( job->list_chapter, 0 ) ) )
{
Expand Down Expand Up @@ -5227,6 +5230,7 @@ hb_audio_t *hb_audio_copy(const hb_audio_t *src)
{
audio->config.in.name = strdup(src->config.in.name);
}
audio->priv.extradata = hb_buffer_dup(src->priv.extradata);
}
return audio;
}
Expand Down Expand Up @@ -5264,6 +5268,7 @@ void hb_audio_close( hb_audio_t **audio )
{
if ( audio && *audio )
{
hb_buffer_close(&(*audio)->priv.extradata);
free((char*)(*audio)->config.in.name);
free((char*)(*audio)->config.out.name);
free(*audio);
Expand Down Expand Up @@ -5383,10 +5388,9 @@ hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src)
{
subtitle = calloc(1, sizeof(*subtitle));
memcpy(subtitle, src, sizeof(*subtitle));
if ( src->extradata )
if (src->extradata)
{
subtitle->extradata = malloc( src->extradata_size );
memcpy( subtitle->extradata, src->extradata, src->extradata_size );
hb_buffer_dup(subtitle->extradata);
}
if (src->name != NULL)
{
Expand Down Expand Up @@ -5441,7 +5445,7 @@ void hb_subtitle_close( hb_subtitle_t **_sub )
free((char*)sub->name);
free((char*)sub->config.name);
free((char*)sub->config.src_filename);
free(sub->extradata);
hb_buffer_close(&sub->extradata);
free(sub);
*_sub = NULL;
}
Expand All @@ -5452,58 +5456,6 @@ void hb_subtitle_close( hb_subtitle_t **_sub )
**********************************************************************
*
*********************************************************************/
int hb_subtitle_add_ssa_header(hb_subtitle_t *subtitle, const char *font,
int fs, int w, int h)
{
// Free any pre-existing extradata
free(subtitle->extradata);

float shadow_size = fs / 36.0;
float outline_size = fs / 30.0;

char *shadow_size_string = hb_strdup_printf("%.2f", shadow_size);
hb_str_from_locale(shadow_size_string);

char *outline_size_string = hb_strdup_printf("%.2f", outline_size);
hb_str_from_locale(outline_size_string);

if (shadow_size_string == NULL || outline_size_string == NULL)
{
hb_error("hb_subtitle_add_ssa_header: malloc failed");
return 0;
}

// SRT subtitles are represented internally as SSA
// Create an SSA header
const char * ssa_header =
"[Script Info]\r\n"
"ScriptType: v4.00+\r\n"
"Collisions: Normal\r\n"
"PlayResX: %d\r\n"
"PlayResY: %d\r\n"
"Timer: 100.0\r\n"
"WrapStyle: 0\r\n"
"ScaledBorderAndShadow: yes\r\n"
"\r\n"
"[V4+ Styles]\r\n"
"Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\r\n"
"Style: Default,%s,%d,&H00FFFFFF,&H00FFFFFF,&H000F0F0F,&H000F0F0F,0,0,0,0,100,100,0,0.00,1,%s,%s,2,20,20,20,0\r\n";

subtitle->extradata = (uint8_t *)hb_strdup_printf(ssa_header, w, h, font, fs, outline_size_string, shadow_size_string);

free(shadow_size_string);
free(outline_size_string);

if (subtitle->extradata == NULL)
{
hb_error("hb_subtitle_add_ssa_header: malloc failed");
return 0;
}
subtitle->extradata_size = strlen((char*)subtitle->extradata) + 1;

return 1;
}

int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track)
{
hb_title_t *title = job->title;
Expand Down
8 changes: 3 additions & 5 deletions libhb/decavcodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "handbrake/hwaccel.h"
#include "handbrake/lang.h"
#include "handbrake/audio_resample.h"
#include "handbrake/extradata.h"

#if HB_PROJECT_FEATURE_QSV
#include "libavutil/hwcontext_qsv.h"
Expand Down Expand Up @@ -822,7 +823,7 @@ static int parse_adts_extradata( hb_audio_t * audio, AVCodecContext * context,
return ret;
}

if (audio->priv.config.extradata.length == 0)
if (audio->priv.extradata)
{
const uint8_t * extradata;
size_t size;
Expand All @@ -831,10 +832,7 @@ static int parse_adts_extradata( hb_audio_t * audio, AVCodecContext * context,
&size);
if (extradata != NULL && size > 0)
{
int len;
len = MIN(size, HB_CONFIG_MAX_SIZE);
memcpy(audio->priv.config.extradata.bytes, extradata, len);
audio->priv.config.extradata.length = len;
hb_set_extradata(&audio->priv.extradata, extradata, size);
}
}

Expand Down
9 changes: 5 additions & 4 deletions libhb/decavsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "handbrake/handbrake.h"
#include "handbrake/hbffmpeg.h"
#include "handbrake/decavsub.h"
#include "handbrake/extradata.h"

struct hb_avsub_context_s
{
Expand Down Expand Up @@ -129,14 +130,14 @@ hb_avsub_context_t * decavsubInit( hb_work_object_t * w, hb_job_t * job )
case AV_CODEC_ID_EIA_608:
{
// Mono font for CC
hb_subtitle_add_ssa_header(ctx->subtitle, HB_FONT_MONO,
20, 384, 288);
hb_set_ssa_extradata(&ctx->subtitle->extradata,
HB_FONT_MONO, 20, 384, 288);
} break;

default:
{
hb_subtitle_add_ssa_header(ctx->subtitle, HB_FONT_SANS,
.066 * job->title->geometry.height, width, height);
hb_set_ssa_extradata(&ctx->subtitle->extradata, HB_FONT_SANS,
.066 * job->title->geometry.height, width, height);
} break;
}
}
Expand Down
7 changes: 4 additions & 3 deletions libhb/decsrtsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "handbrake/handbrake.h"
#include "handbrake/colormap.h"
#include "handbrake/decavsub.h"
#include "handbrake/extradata.h"

struct start_and_end {
unsigned long start, end;
Expand Down Expand Up @@ -566,9 +567,9 @@ static int decsrtInit( hb_work_object_t * w, hb_job_t * job )
// Generate generic SSA Script Info.
int height = job->title->geometry.height - job->crop[0] - job->crop[1];
int width = job->title->geometry.width - job->crop[2] - job->crop[3];
hb_subtitle_add_ssa_header(w->subtitle, HB_FONT_SANS,
.066 * job->title->geometry.height,
width, height);
hb_set_ssa_extradata(&w->subtitle->extradata, HB_FONT_SANS,
.066 * job->title->geometry.height,
width, height);
return 0;

fail:
Expand Down
7 changes: 2 additions & 5 deletions libhb/decssasub.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "handbrake/handbrake.h"
#include "handbrake/decavsub.h"
#include "handbrake/extradata.h"
#include "libavformat/avformat.h"

struct hb_work_private_s
Expand Down Expand Up @@ -57,11 +58,7 @@ static int extradataInit( hb_work_private_t * pv )
}
if (st->codecpar->extradata != NULL)
{
pv->subtitle->extradata = malloc(st->codecpar->extradata_size + 1);
memcpy(pv->subtitle->extradata,
st->codecpar->extradata, st->codecpar->extradata_size);
pv->subtitle->extradata[st->codecpar->extradata_size] = 0;
pv->subtitle->extradata_size = st->codecpar->extradata_size + 1;
hb_set_text_extradata(&pv->subtitle->extradata, st->codecpar->extradata, st->codecpar->extradata_size);
}
return 0;
}
Expand Down
7 changes: 4 additions & 3 deletions libhb/dectx3gsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdio.h>
#include "handbrake/handbrake.h"
#include "handbrake/colormap.h"
#include "handbrake/extradata.h"

struct hb_work_private_s
{
Expand Down Expand Up @@ -260,9 +261,9 @@ static int dectx3gInit( hb_work_object_t * w, hb_job_t * job )
// For now we just create a generic SSA Script Info.
int height = job->title->geometry.height - job->crop[0] - job->crop[1];
int width = job->title->geometry.width - job->crop[2] - job->crop[3];
hb_subtitle_add_ssa_header(w->subtitle, HB_FONT_SANS,
.066 * job->title->geometry.height,
width, height);
hb_set_ssa_extradata(&w->subtitle->extradata, HB_FONT_SANS,
.066 * job->title->geometry.height,
width, height);

return 0;
}
Expand Down
39 changes: 22 additions & 17 deletions libhb/enc_qsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "handbrake/qsv_memory.h"
#include "handbrake/h264_common.h"
#include "handbrake/h265_common.h"
#include "handbrake/extradata.h"

#include "libavutil/hwcontext_qsv.h"
#include "libavutil/hwcontext.h"
Expand Down Expand Up @@ -643,7 +644,9 @@ static int qsv_hevc_make_header(hb_work_object_t *w, mfxSession session, const m
len = bitstream.DataLength;
buf = bitstream.Data + bitstream.DataOffset;
end = bitstream.Data + bitstream.DataOffset + bitstream.DataLength;
w->config->h265.headers_length = 0;

size_t extradata_size = 0;
uint8_t extradata[HB_CONFIG_MAX_SIZE];

while ((buf = hb_annexb_find_next_nalu(buf, &len)) != NULL)
{
Expand All @@ -660,19 +663,20 @@ static int qsv_hevc_make_header(hb_work_object_t *w, mfxSession session, const m
continue;
}

size_t size = hb_nal_unit_write_annexb(NULL, buf, len) + w->config->h265.headers_length;
if (sizeof(w->config->h265.headers) < size)
size_t size = hb_nal_unit_write_annexb(NULL, buf, len) + extradata_size;
if (sizeof(extradata) < size)
{
/* Will never happen in practice */
hb_log("qsv_hevc_make_header: header too large (size: %lu, max: %lu)",
size, sizeof(w->config->h265.headers));
size, sizeof(extradata));
}

w->config->h265.headers_length += hb_nal_unit_write_annexb(w->config->h265.headers +
w->config->h265.headers_length, buf, len);
extradata_size += hb_nal_unit_write_annexb(extradata + extradata_size, buf, len);
len = end - buf;
}

hb_set_extradata(w->extradata, extradata, extradata_size);

end:
if (bitstream.Data)
av_free(bitstream.Data);
Expand Down Expand Up @@ -1709,16 +1713,20 @@ int encqsvInit(hb_work_object_t *w, hb_job_t *job)
memset(&videoParam, 0, sizeof(mfxVideoParam));
videoParam.ExtParam = extParamArray;
videoParam.NumExtParam = 0;

uint8_t sps[HB_CONFIG_MAX_SIZE];
uint8_t pps[HB_CONFIG_MAX_SIZE];

// introduced in API 1.3
memset(sps_pps, 0, sizeof(mfxExtCodingOptionSPSPPS));
sps_pps->Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS;
sps_pps->Header.BufferSz = sizeof(mfxExtCodingOptionSPSPPS);
sps_pps->SPSId = 0;
sps_pps->SPSBuffer = w->config->h264.sps;
sps_pps->SPSBufSize = sizeof(w->config->h264.sps);
sps_pps->SPSBuffer = sps;
sps_pps->SPSBufSize = sizeof(sps);
sps_pps->PPSId = 0;
sps_pps->PPSBuffer = w->config->h264.pps;
sps_pps->PPSBufSize = sizeof(w->config->h264.pps);
sps_pps->PPSBuffer = pps;
sps_pps->PPSBufSize = sizeof(pps);
if (pv->param.videoParam->mfx.CodecId == MFX_CODEC_AVC)
{
videoParam.ExtParam[videoParam.NumExtParam++] = (mfxExtBuffer*)sps_pps;
Expand Down Expand Up @@ -1788,12 +1796,9 @@ int encqsvInit(hb_work_object_t *w, hb_job_t *job)
if (videoParam.mfx.CodecId == MFX_CODEC_AVC)
{
// remove 4-byte Annex B NAL unit prefix (0x00 0x00 0x00 0x01)
w->config->h264.sps_length = sps_pps->SPSBufSize - 4;
memmove(w->config->h264.sps, w->config->h264.sps + 4,
w->config->h264.sps_length);
w->config->h264.pps_length = sps_pps->PPSBufSize - 4;
memmove(w->config->h264.pps, w->config->h264.pps + 4,
w->config->h264.pps_length);
hb_set_h264_extradata(w->extradata,
sps + 4, sps_pps->SPSBufSize - 4,
pps + 4, sps_pps->PPSBufSize - 4);
}
else if (videoParam.mfx.CodecId == MFX_CODEC_HEVC)
{
Expand Down Expand Up @@ -1827,7 +1832,7 @@ int encqsvInit(hb_work_object_t *w, hb_job_t *job)
{
case MFX_CODEC_AVC:
case MFX_CODEC_HEVC:
pv->init_delay = &w->config->init_delay;
pv->init_delay = &w->init_delay;
break;
default:
break;
Expand Down
7 changes: 3 additions & 4 deletions libhb/encavcodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "handbrake/av1_common.h"
#include "handbrake/nal_units.h"
#include "handbrake/nvenc_common.h"
#include "handbrake/extradata.h"

/*
* The frame info struct remembers information about each frame across calls
Expand Down Expand Up @@ -852,9 +853,7 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )

if (context->extradata != NULL)
{
memcpy(w->config->extradata.bytes, context->extradata,
context->extradata_size);
w->config->extradata.length = context->extradata_size;
hb_set_extradata(w->extradata, context->extradata, context->extradata_size);
}

done:
Expand Down Expand Up @@ -923,7 +922,7 @@ static void compute_dts_offset( hb_work_private_t * pv, hb_buffer_t * buf )
if ( ( pv->frameno_in ) == pv->job->areBframes )
{
pv->dts_delay = buf->s.start;
pv->job->config.init_delay = pv->dts_delay;
pv->job->init_delay = pv->dts_delay;
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions libhb/encavcodecaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "handbrake/handbrake.h"
#include "handbrake/hbffmpeg.h"
#include "handbrake/extradata.h"

struct hb_work_private_s
{
Expand Down Expand Up @@ -224,8 +225,7 @@ static int encavcodecaInit(hb_work_object_t *w, hb_job_t *job)
hb_error("encavcodecaInit: hb_avcodec_open() failed");
return 1;
}
w->config->init_delay = av_rescale(context->initial_padding,
90000, context->sample_rate);
*w->init_delay = av_rescale(context->initial_padding, 90000, context->sample_rate);

// avcodec_open populates the opts dictionary with the
// things it didn't recognize.
Expand Down Expand Up @@ -291,9 +291,7 @@ static int encavcodecaInit(hb_work_object_t *w, hb_job_t *job)

if (context->extradata != NULL)
{
memcpy(w->config->extradata.bytes, context->extradata,
context->extradata_size);
w->config->extradata.length = context->extradata_size;
hb_set_extradata(w->extradata, context->extradata, context->extradata_size);
}

return 0;
Expand All @@ -313,9 +311,8 @@ static void Finalize(hb_work_object_t *w)
// Then we need to recopy the header since it was modified
if (pv->context->extradata != NULL)
{
memcpy(w->config->extradata.bytes, pv->context->extradata,
pv->context->extradata_size);
w->config->extradata.length = pv->context->extradata_size;
hb_set_extradata(w->extradata, pv->context->extradata,
pv->context->extradata_size);
}
}

Expand Down
Loading

0 comments on commit 0c6793a

Please sign in to comment.