Auto *prev_auto = 0;
mask_autos->get_prev_auto(position, PLAY_FORWARD, (Auto *&)prev_auto, 1);
MaskAuto *prev_mask = (MaskAuto *)prev_auto;
- ArrayList<MaskPoint*> points;
+ MaskPoints points;
int update_points = 1;
// Determine the points based on whether
// new keyframes will be generated or drawing is performed.
if(button_press && result) {
#ifdef USE_KEYFRAME_SPANNING
- ArrayList<MaskPoint*> &mask_points = points;
+ MaskPoints &mask_points = points;
#else
SubMask *mask = gui->mask_keyframe->get_submask(mwindow->edl->session->cwindow_mask);
- ArrayList<MaskPoint*> &mask_points = mask->points;
+ MaskPoints &mask_points = mask->points;
#endif
int k = gui->affected_point;
if( k >= 0 && k < mask_points.size() ) {
gui->current_operation != CWINDOW_NONE ) {
// mwindow->undo->update_undo_before(_("mask point"), this);
#ifdef USE_KEYFRAME_SPANNING
- ArrayList<MaskPoint*> &mask_points = points;
+ MaskPoints &mask_points = points;
#else
- ArrayList<MaskPoint*> &mask_points = mask->points;
+ MaskPoints &mask_points = mask->points;
#endif
MaskPoint *point = mask_points.get(gui->affected_point);
// canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
if( !mask_gui->mask_enables[j]->get_value() ) continue;
SubMask *sub_mask = keyframe->get_submask(j);
if( !sub_mask ) continue;
- ArrayList<MaskPoint*> &points = sub_mask->points;
+ MaskPoints &points = sub_mask->points;
for( int i=0; i<points.size(); ++i ) {
MaskPoint *point = points[i];
point->x += dx;
int button_no = get_buttonpress();
double ds = accel/64., dt = accel*M_PI/360.;
double scale = button_no == WHEEL_UP ? 1.+ds : 1.-ds;
+ int mode = mask_gui->scale_mode;
+ double xscale = !rotate && (mode == 0 || mode == 2 ) ? scale : 1.;
+ double yscale = !rotate && (mode == 1 || mode == 2 ) ? scale : 1.;
double theta = button_no == WHEEL_UP ? dt : -dt;
if( rotate ? theta==0 : scale==1 ) break;
float st = sin(theta), ct = cos(theta);
if( !mask_gui->mask_enables[j]->get_value() ) continue;
SubMask *sub_mask = keyframe->get_submask(j);
if( !sub_mask ) continue;
- ArrayList<MaskPoint*> &points = sub_mask->points;
+ MaskPoints &points = sub_mask->points;
for( int i=0; i<points.size(); ++i ) {
MaskPoint *point = points[i];
float px = point->x - gui->x_origin;
float py = point->y - gui->y_origin;
- float nx = !rotate ? px*scale : px*ct + py*st;
- float ny = !rotate ? py*scale : py*ct - px*st;
+ float nx = !rotate ? px*xscale : px*ct + py*st;
+ float ny = !rotate ? py*yscale : py*ct - px*st;
point->x = nx + gui->x_origin;
point->y = ny + gui->y_origin;
px = point->control_x1; py = point->control_y1;
- point->control_x1 = !rotate ? px*scale : px*ct + py*st;
- point->control_y1 = !rotate ? py*scale : py*ct - px*st;
+ point->control_x1 = !rotate ? px*xscale : px*ct + py*st;
+ point->control_y1 = !rotate ? py*yscale : py*ct - px*st;
px = point->control_x2; py = point->control_y2;
- point->control_x2 = !rotate ? px*scale : px*ct + py*st;
- point->control_y2 = !rotate ? py*scale : py*ct - px*st;
+ point->control_x2 = !rotate ? px*xscale : px*ct + py*st;
+ point->control_y2 = !rotate ? py*yscale : py*ct - px*st;
}
}
rerender = 1;
mask_autos->update_parameter(&temp_keyframe);
}
#endif
-
- points.remove_all_objects();
//printf("CWindowCanvas::do_mask 20\n");
+
+ if( draw && draw_boundary && !draw_markers ) {
+ BC_WindowBase *cvs_win = get_canvas();
+ cvs_win->set_inverse();
+ cvs_win->set_color(RED+GREEN);
+ for( int k=0; k<SUBMASKS; ++k ) {
+ if( k == mwindow->edl->session->cwindow_mask ) continue;
+ points.remove_all_objects();
+ if( use_interpolated )
+ mask_autos->get_points(&points, k, position, PLAY_FORWARD);
+ else
+ prev_mask->get_points(&points, k);
+ MaskEdge edge;
+ edge.load(points, 0);
+ for( int i=0; i<edge.size(); ++i ) {
+ MaskCoord a = edge[i];
+ MaskCoord b = i<edge.size()-1 ? edge[i+1] : edge[0];
+ float ax = a.x, ay = a.y;
+ float bx = b.x, by = b.y;
+ output_to_canvas(mwindow->edl, 0, ax, ay);
+ output_to_canvas(mwindow->edl, 0, bx, by);
+ cvs_win->draw_line(ax,ay, bx,by);
+ }
+ }
+ }
+
return result;
}
for(MaskAuto *current = (MaskAuto*)autos->default_auto; current; ) {
SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
memset(submask->name, 0, sizeof(submask->name));
- strncpy(submask->name, get_text(), sizeof(submask->name));
+ strncpy(submask->name, get_text(), sizeof(submask->name)-1);
current = current == (MaskAuto*)autos->default_auto ?
(MaskAuto*)autos->first : (MaskAuto*)NEXT;
}
mask_items.remove_all_objects();
int sz = !keyframe ? 0 : keyframe->masks.size();
for( int i=0; i<SUBMASKS; ++i ) {
- char text[BCSTRLEN];
+ char text[BCSTRLEN]; memset(text, 0, sizeof(text));
if( i < sz ) {
SubMask *sub_mask = keyframe->masks.get(i);
- strncpy(text, sub_mask->name, sizeof(text));
+ strncpy(text, sub_mask->name, sizeof(text)-1);
}
else
sprintf(text, "%d", i);
return 1;
}
+int CWindowMaskFocus::calculate_w(CWindowMaskGUI *gui)
+{
+ int w, h;
+ calculate_extents(gui, &w, &h, _("Focus"));
+ return w;
+}
+
+CWindowMaskScaleXY::CWindowMaskScaleXY(MWindow *mwindow, CWindowMaskGUI *gui,
+ int x, int y, VFrame **data, int v, int id, const char *tip)
+ : BC_Toggle(x, y, data, v)
+{
+ this->id = id;
+ this->mwindow = mwindow;
+ this->gui = gui;
+ set_tooltip(tip);
+}
+
+CWindowMaskScaleXY::~CWindowMaskScaleXY()
+{
+}
+
+int CWindowMaskScaleXY::handle_event()
+{
+ gui->scale_mode = id;
+ gui->mask_scale_x->update(id == 0);
+ gui->mask_scale_y->update(id == 1);
+ gui->mask_scale_xy->update(id == 2);
+ return 1;
+}
+
CWindowMaskHelp::CWindowMaskHelp(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
: BC_CheckBox(x, y, 0, _("Help"))
{
CWindowMaskGUI::CWindowMaskGUI(MWindow *mwindow, CWindowTool *thread)
: CWindowToolGUI(mwindow, thread,
- _(PROGRAM_NAME ": Mask"), 430, 700)
+ _(PROGRAM_NAME ": Mask"), 460, 700)
{
this->mwindow = mwindow;
this->thread = thread;
fade = 0;
feather = 0;
focused = 0;
+ scale_mode = 2;
markers = 1;
boundary = 1;
preset_dialog = 0;
void CWindowMaskGUI::create_objects()
{
- int x = 10, y = 10, margin = mwindow->theme->widget_border;
+ Theme *theme = mwindow->theme;
+ int x = 10, y = 10, margin = theme->widget_border;
int clr_w = CWindowMaskClrMask::calculate_w(mwindow);
int clr_x = get_w()-x - clr_w;
int del_w = CWindowMaskDelMask::calculate_w(this,_("Delete"));
add_subwindow(mask_delete = new CWindowMaskDelete(mwindow, this, x2, y, 80));
y += mask_load->get_h() + 2*margin;
BC_Bar *bar;
- add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
- y += bar->get_h() + 2*margin;
+// add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
+// y += bar->get_h() + 2*margin;
add_subwindow(title = new BC_Title(x, y, _("Select:")));
int bw = 0, bh = 0;
mask_blabels[i] = new BC_Title(x2+tx, y, text);
add_subwindow(mask_blabels[i]);
}
+ x2 += margin;
+ add_subwindow(mask_center = new CWindowMaskCenter(mwindow, this, x2, y, 80));
y += mask_blabels[0]->get_h() + margin;
add_subwindow(mask_unclr = new CWindowMaskUnclear(mwindow, this, x, y, x1-x-2*margin));
x2 = x1;
mask_enables[i] = new CWindowMaskEnable(mwindow, this, x2, y, i, 1);
add_subwindow(mask_enables[i]);
}
+ x2 += margin;
+ add_subwindow(mask_normal = new CWindowMaskNormal(mwindow, this, x2, y, 80));
y += mask_enables[0]->get_h() + 2*margin;
add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Fade & Feather")));
y += title_bar->get_h() + margin;
float cx = mwindow->edl->session->output_w / 2.f;
focus_x = new CWindowCoord(this, x1, y, cx);
focus_x->create_objects();
- add_subwindow(focus = new CWindowMaskFocus(mwindow, this, del_x, y));
+ x2 = x1 + focus_x->get_w() + 3*margin;
+ add_subwindow(title = new BC_Title(x2, y, _("Scaling Mode:")));
+ x2 = clr_x - 2*margin - CWindowMaskFocus::calculate_w(this);
+ add_subwindow(focus = new CWindowMaskFocus(mwindow, this, x2, y));
add_subwindow(gang_focus = new CWindowMaskGangFocus(mwindow, this, clr_x, y));
y += focus_x->get_h() + margin;
add_subwindow(title = new BC_Title(x, y, "Y:"));
float cy = mwindow->edl->session->output_h / 2.f;
focus_y = new CWindowCoord(this, x1, y, cy);
focus_y->create_objects();
- add_subwindow(mask_center = new CWindowMaskCenter(mwindow, this, x2=x4, y, 80));
- x2 += mask_center->get_w() + 2*margin;
- add_subwindow(mask_normal = new CWindowMaskNormal(mwindow, this, x2, y, 80));
+ x2 = x1 + focus_y->get_w() + 3*margin;
+ add_subwindow(mask_scale_x = new CWindowMaskScaleXY(mwindow, this,
+ x2, y, theme->get_image_set("mask_scale_x"), 0, 0, _("scale x")));
+ x2 += mask_scale_x->get_w() + margin;
+ add_subwindow(mask_scale_y = new CWindowMaskScaleXY(mwindow, this,
+ x2, y, theme->get_image_set("mask_scale_y"), 0, 1, _("scale y")));
+ x2 += mask_scale_y->get_w() + margin;
+ add_subwindow(mask_scale_xy = new CWindowMaskScaleXY(mwindow, this,
+ x2, y, theme->get_image_set("mask_scale_xy"), 1, 2, _("scale xy")));
y += focus_x->get_h() + 2*margin;
add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
y += bar->get_h() + margin;
for( int j=typ<0? 0 : k; j<n; ++j ) {
if( !mask_enables[j]->get_value() ) continue;
SubMask *sub_mask = keyframe->get_submask(j);
- ArrayList<MaskPoint*> &points = sub_mask->points;
+ MaskPoints &points = sub_mask->points;
int psz = points.size();
if( psz < 3 ) continue;
int l = mwindow->cwindow->gui->affected_point;
keyframe = &temp_keyframe;
#endif
SubMask *sub_mask = keyframe->get_submask(k);
- ArrayList<MaskPoint*> &points = sub_mask->points;
+ MaskPoints &points = sub_mask->points;
int psz = points.size();
if( psz > 0 ) {
float cx = 0, cy = 0;
keyframe = &temp_keyframe;
#endif
SubMask *sub_mask = keyframe->get_submask(k);
- ArrayList<MaskPoint*> &points = sub_mask->points;
+ MaskPoints &points = sub_mask->points;
int psz = points.size();
float cx = 0, cy = 0;
double dr = 0;
pgui->preset_dialog->gui->load_masks(masks);
for( int i=0; i<masks.size(); ++i ) {
char text[BCSTRLEN]; memset(text, 0, sizeof(text));
- strncpy(text, masks[i]->name, sizeof(text-1));
+ strncpy(text, masks[i]->name, sizeof(text)-1);
mask_items.append(new CWindowMaskItem(text));
}
masks.remove_all_objects();
double c = 4*(sqrt(2.)-1)/3; // bezier aprox circle
float r2 = r / 2.f, rc = r*c, r4 = r / 4.f;
MaskPoint *pt = 0;
- ArrayList<MaskPoint*> &points = sub_mask->points;
+ MaskPoints &points = sub_mask->points;
points.remove_all_objects();
switch( i ) {
- case 0: // square
+ case MASK_SHAPE_SQUARE:
points.append(pt = new MaskPoint());
pt->x = cx - r; pt->y = cy - r;
points.append(pt = new MaskPoint());
points.append(pt = new MaskPoint());
pt->x = cx - r; pt->y = cy + r;
break;
- case 1: // circle
+ case MASK_SHAPE_CIRCLE:
points.append(pt = new MaskPoint());
pt->x = cx - r; pt->y = cy - r;
pt->control_x1 = -rc; pt->control_y1 = rc;
pt->control_x1 = rc; pt->control_y1 = rc;
pt->control_x2 = -rc; pt->control_y2 = -rc;
break;
- case 2: // triangle
+ case MASK_SHAPE_TRIANGLE:
points.append(pt = new MaskPoint());
pt->x = cx + 0; pt->y = cy - r*(sqrt(3.)-1.);
points.append(pt = new MaskPoint());
points.append(pt = new MaskPoint());
pt->x = cx - r; pt->y = cy + r;
break;
- case 3: // oval
+ case MASK_SHAPE_OVAL:
points.append(pt = new MaskPoint());
pt->x = cx - r; pt->y = cy - r2;
pt->control_x1 = -r2; pt->control_y1 = r4;
// Get existing keyframe
gui->get_keyframe(track, autos, keyframe,
mask, point, create_it);
- int k = get_selection_number(0, 0);
- if( track && k >= 0 ) {
+ CWindowMaskItem *item = (CWindowMaskItem *) get_selection(0, 0);
+ if( track && item ) {
#ifdef USE_KEYFRAME_SPANNING
MaskAuto temp_keyframe(mwindow->edl, autos);
temp_keyframe.copy_data(keyframe);
keyframe = &temp_keyframe;
mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
#endif
+ int k = item->id;
if( mask ) {
- if( k < 4 )
+ if( k < MASK_SHAPE_BUILTIN )
builtin_shape(k, mask);
else
- load_shape(k-4, mask);
+ load_shape(k-MASK_SHAPE_BUILTIN, mask);
#ifdef USE_KEYFRAME_SPANNING
autos->update_parameter(keyframe);
#endif
void CWindowMaskShape::create_objects()
{
shape_items.remove_all_objects();
- shape_items.append(new BC_ListBoxItem(_("square")));
- shape_items.append(new BC_ListBoxItem(_("circle")));
- shape_items.append(new BC_ListBoxItem(_("triangle")));
- shape_items.append(new BC_ListBoxItem(_("oval")));
+ shape_items.append(new CWindowMaskItem(_("square"), MASK_SHAPE_SQUARE));
+ shape_items.append(new CWindowMaskItem(_("circle"), MASK_SHAPE_CIRCLE));
+ shape_items.append(new CWindowMaskItem(_("triangle"), MASK_SHAPE_TRIANGLE));
+ shape_items.append(new CWindowMaskItem(_("oval"), MASK_SHAPE_OVAL));
ArrayList<SubMask *> masks;
gui->load_masks(masks);
+ int id = MASK_SHAPE_BUILTIN;
for( int i=0; i<masks.size(); ++i )
- shape_items.append(new BC_ListBoxItem(masks[i]->name));
+ shape_items.append(new CWindowMaskItem(masks[i]->name, id++));
masks.remove_all_objects();
update(&shape_items, 0, 0, 1);
}
int handle_event();
MWindow *mwindow;
CWindowMaskGUI *gui;
+ static int calculate_w(CWindowMaskGUI *gui);
+};
+
+class CWindowMaskScaleXY : public BC_Toggle
+{
+public:
+ CWindowMaskScaleXY(MWindow *mwindow, CWindowMaskGUI *gui,
+ int x, int y, VFrame **data, int v,
+ int id, const char *tip);
+ ~CWindowMaskScaleXY();
+ int handle_event();
+ MWindow *mwindow;
+ CWindowMaskGUI *gui;
+ int id;
};
class CWindowMaskHelp : public BC_CheckBox
class CWindowMaskShape : public BC_ListBox
{
public:
+ enum { MASK_SHAPE_SQUARE, MASK_SHAPE_CIRCLE,
+ MASK_SHAPE_TRIANGLE, MASK_SHAPE_OVAL,
+ MASK_SHAPE_BUILTIN };
CWindowMaskShape(MWindow *mwindow, CWindowMaskGUI *gui);
~CWindowMaskShape();
void create_objects();
CWindowMaskSmoothButton *mask_crv_linear, *mask_crv_smooth;
CWindowMaskSmoothButton *mask_all_linear, *mask_all_smooth;
CWindowCoord *x, *y;
+ CWindowMaskScaleXY *mask_scale_x, *mask_scale_y, *mask_scale_xy;
+ int scale_mode;
CWindowMaskFocus *focus;
int focused;
CWindowMaskGangFocus *gang_focus;
class CWindowMaskSmoothButton;
class CWindowMaskAffectedPoint;
class CWindowMaskFocus;
+class CWindowMaskScaleXY;
class CWindowMaskHelp;
class CWindowMaskDrawMarkers;
class CWindowMaskDrawBoundary;
return masks.values[number];
}
-void MaskAuto::get_points(ArrayList<MaskPoint*> *points,
+void MaskAuto::get_points(MaskPoints *points,
int submask)
{
points->remove_all_objects();
}
}
-void MaskAuto::set_points(ArrayList<MaskPoint*> *points,
+void MaskAuto::set_points(MaskPoints *points,
int submask)
{
SubMask *submask_ptr = get_submask(submask);
return min_fader >= 0 && total_points < 2 ? 0 : 1;
}
+static inline double line_dist(float cx,float cy, float tx,float ty)
+{
+ double dx = tx-cx, dy = ty-cy;
+ return sqrt(dx*dx + dy*dy);
+}
+
+void MaskEdge::load(MaskPoints &points, float ofs)
+{
+ remove_all();
+ int first_point = 1;
+// Need to tabulate every vertex in persistent memory because
+// gluTessVertex doesn't copy them.
+ for( int i=0; i<points.total; ++i ) {
+ MaskPoint *point1 = points.values[i];
+ MaskPoint *point2 = (i >= points.total-1) ?
+ points.values[0] : points.values[i+1];
+
+ int segments = 0;
+ if( !point1->control_x2 && !point1->control_y2 &&
+ !point2->control_x1 && !point2->control_y1 )
+ segments = 1;
+
+ float x0 = point1->x, y0 = point1->y;
+ float x1 = point1->x + point1->control_x2;
+ float y1 = point1->y + point1->control_y2;
+ float x2 = point2->x + point2->control_x1;
+ float y2 = point2->y + point2->control_y1;
+ float x3 = point2->x, y3 = point2->y;
+
+ // forward differencing bezier curves implementation taken from GPL code at
+ // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
+
+ float cx3, cx2, cx1, cx0;
+ float cy3, cy2, cy1, cy0;
+
+ // [-1 +3 -3 +1]
+ // [+3 -6 +3 0]
+ // [-3 +3 0 0]
+ // [+1 0 0 0]
+
+ cx3 = - x0 + 3*x1 - 3*x2 + x3;
+ cx2 = 3*x0 - 6*x1 + 3*x2;
+ cx1 = -3*x0 + 3*x1;
+ cx0 = x0;
+
+ cy3 = - y0 + 3*y1 - 3*y2 + y3;
+ cy2 = 3*y0 - 6*y1 + 3*y2;
+ cy1 = -3*y0 + 3*y1;
+ cy0 = y0;
+
+ // This equation is from Graphics Gems I.
+ //
+ // The idea is that since we're approximating a cubic curve with lines,
+ // any error we incur is due to the curvature of the line, which we can
+ // estimate by calculating the maximum acceleration of the curve. For
+ // a cubic, the acceleration (second derivative) is a line, meaning that
+ // the absolute maximum acceleration must occur at either the beginning
+ // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
+ // conservative than that, but that's okay.
+ if( !segments ) {
+ float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
+ float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
+ float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
+ segments = maxaccel > 1.0 ? sqrt(maxaccel) :
+ 1 + line_dist(point1->x,point1->y, point2->x,point2->y);
+ }
+
+ for( int j=0; j<=segments; ++j ) {
+ float t = (float)j / segments;
+ float x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
+ float y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
+
+ if( j > 0 || first_point ) {
+ append(x, y-ofs);
+ first_point = 0;
+ }
+ }
+ }
+}
+
float control_x2, control_y2;
};
+class MaskCoord { public: double x, y, z; };
+
+class MaskEdge : public ArrayList<MaskCoord>
+{
+public:
+ MaskCoord &append() { return ArrayList<MaskCoord>::append(); }
+ MaskCoord &append(double x, double y, double z=0) {
+ MaskCoord &c = append();
+ c.x = x; c.y = y; c.z = z;
+ return c;
+ }
+ void load(MaskPoints &points, float ofs);
+};
+
+class MaskEdges : public ArrayList<MaskEdge*> {
+public:
+ MaskEdges() {}
+ ~MaskEdges() { remove_all_objects(); }
+};
+
+class MaskPoints : public ArrayList<MaskPoint *>
+{
+public:
+ void clear() { remove_all_objects(); }
+ MaskPoints() {}
+ ~MaskPoints() { clear(); }
+};
+
+class MaskPointSets : public ArrayList<MaskPoints*>
+{
+public:
+ void clear() { remove_all_objects(); }
+ MaskPointSets() {}
+ ~MaskPointSets() { clear(); }
+};
+
#define FEATHER_MAX 100
class SubMask
char name[BCSTRLEN];
float fader; // -100 - 100
float feather; // -100 - 100
- ArrayList<MaskPoint*> points;
+ MaskPoints points;
MaskAuto *keyframe;
};
void copy_from(MaskAuto *src);
// Copy data but not position
void copy_data(MaskAuto *src);
- void get_points(ArrayList<MaskPoint*> *points,
+ void get_points(MaskPoints *points,
int submask);
- void set_points(ArrayList<MaskPoint*> *points,
+ void set_points(MaskPoints *points,
int submask);
// Copy parameters to this which differ between ref & src
int disable_opengl_masking;
};
-class MaskCoord { public: double x, y, z; };
-
-class MaskEdge : public ArrayList<MaskCoord>
-{
-public:
- MaskCoord &append() { return ArrayList<MaskCoord>::append(); }
- MaskCoord &append(double x, double y, double z=0) {
- MaskCoord &c = append();
- c.x = x; c.y = y; c.z = z;
- return c;
- }
-};
-
// shader buffer unsized array vec only seems to work for dvec (05/2019)
class MaskSpot { public: double x, y; };
}
};
-class MaskEdges : public ArrayList<MaskEdge*> {
-public:
- MaskEdges() {}
- ~MaskEdges() { remove_all_objects(); }
-};
-
-class MaskPointSet : public ArrayList<MaskPoint*>
-{
-public:
- void clear() { remove_all_objects(); }
- MaskPointSet() {}
- ~MaskPointSet() { clear(); }
-};
-class MaskPointSets : public ArrayList<MaskPointSet*>
-{
-public:
- void clear() { remove_all_objects(); }
- MaskPointSets() {}
- ~MaskPointSets() { clear(); }
-};
-
#endif
#define MASKAUTO_INC
-
-class MaskAuto;
class MaskPoint;
class SubMask;
+class MaskAuto;
+class MaskCoord;
+class MaskEdge;
+class MaskSpot;
+class MaskSpots;
+class MaskEdges;
+class MaskPoints;
+class MaskPointSets;
enum { // no longer used, legacy value
MASK_MULTIPLY_ALPHA,
#define SUBMASKS 8
-
-
#endif
-void MaskAutos::get_points(ArrayList<MaskPoint*> *points,
+void MaskAutos::get_points(MaskPoints *points,
int submask,
int64_t position,
int direction)
int mask_exists(int64_t position, int direction);
// Perform interpolation
- void get_points(ArrayList<MaskPoint*> *points, int submask, int64_t position, int direction);
+ void get_points(MaskPoints *points, int submask, int64_t position, int direction);
double get_feather(int64_t position, int i, int direction);
double get_fader(int64_t position, int i, int direction);
int total_submasks(int64_t position, int direction);
point_sets.remove_all_objects();
}
-int MaskEngine::points_equivalent(ArrayList<MaskPoint*> *new_points,
- ArrayList<MaskPoint*> *points)
+int MaskEngine::points_equivalent(MaskPoints *new_points,
+ MaskPoints *points)
{
//printf("MaskEngine::points_equivalent %d %d\n", new_points->total, points->total);
if( new_points->total != points->total ) return 0;
return 1;
}
-void MaskEngine::draw_edge(MaskEdge &edge, MaskPointSet &points)
-{
- if( points.size() < 2 ) return;
- edge.remove_all();
- for( int i=0; i<points.size(); ++i ) {
- MaskPoint *ap = points[i];
- MaskPoint *bp = (i>=points.size()-1) ?
- points[0] : points[i+1];
- int segments = 0;
- if( ap->control_x2 == 0 && ap->control_y2 == 0 &&
- bp->control_x1 == 0 && bp->control_y1 == 0 )
- segments = 1;
- float x0 = ap->x, y0 = ap->y;
- float x1 = ap->x + ap->control_x2;
- float y1 = ap->y + ap->control_y2;
- float x2 = bp->x + bp->control_x1;
- float y2 = bp->y + bp->control_y1;
- float x3 = bp->x, y3 = bp->y;
-
-// from Playback3D::do_mask_sync
- float cx3 = - x0 + 3*x1 - 3*x2 + x3;
- float cx2 = 3*x0 - 6*x1 + 3*x2;
- float cx1 = -3*x0 + 3*x1;
- float cx0 = x0;
-
- float cy3 = - y0 + 3*y1 - 3*y2 + y3;
- float cy2 = 3*y0 - 6*y1 + 3*y2;
- float cy1 = -3*y0 + 3*y1;
- float cy0 = y0;
-
- if( segments == 0 ) {
- float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
- float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
- float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
- float h = 1.0;
- if( maxaccel > 8.0 ) h = sqrt((8.0) / maxaccel);
- segments = int(1/h);
- }
-
- for( int j = 0; j <= segments; ++j ) {
- float t = (float)j / segments;
- float x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
- float y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
- edge.append(x, y);
- }
- }
-}
-
void MaskEngine::do_mask(VFrame *output,
int64_t start_position_project,
MaskAutos *keyframe_set,
if( new_fader != faders[i] ) { recalculate = 1; break; }
float new_feather = keyframe_set->get_feather(start_position_project, i, PLAY_FORWARD);
if( new_feather != feathers[i] ) { recalculate = 1; break; }
- ArrayList<MaskPoint*> new_points;
+ MaskPoints new_points;
keyframe_set->get_points(&new_points, i,
start_position_project, PLAY_FORWARD);
if( !points_equivalent(&new_points, point_sets[i]) )
recalculate = 1;
- new_points.remove_all_objects();
}
if( recalculate ) {
for( int i = 0; i < point_sets.total; i++ ) {
- ArrayList<MaskPoint*> *points = point_sets[i];
+ MaskPoints *points = point_sets[i];
points->remove_all_objects();
}
point_sets.remove_all_objects();
fade[i+1] = t;
float feather = keyframe_set->get_feather(start_position_project, i, PLAY_FORWARD);
feathers.append(feather);
- MaskPointSet *new_points = new MaskPointSet();
+ MaskPoints *new_points = new MaskPoints();
keyframe_set->get_points(new_points, i, start_position_project, PLAY_FORWARD);
point_sets.append(new_points);
MaskEdge *edge = edges.append(new MaskEdge());
if( !((show_mask>>i) & 1) ) continue;
- draw_edge(*edge, *new_points);
+ edge->load(*new_points, 0);
}
// draw mask
if( !mask ) mask = new VFrame(mask_w, mask_h, mask_model, 0);
MaskAutos *keyframe_set,
MaskAuto *keyframe,
MaskAuto *default_auto);
- int points_equivalent(ArrayList<MaskPoint*> *new_points,
- ArrayList<MaskPoint*> *points);
+ int points_equivalent(MaskPoints *new_points,
+ MaskPoints *points);
void delete_packages();
void init_packages();
LoadClient* new_client();
LoadPackage* new_package();
- void draw_edge(MaskEdge &edge, MaskPointSet &points);
VFrame *output;
VFrame *mask, *temp;
int h = command->frame->get_h();
MaskEdges edges;
float faders[SUBMASKS], feathers[SUBMASKS], bg = 1;
- MaskPointSet point_set[SUBMASKS];
+ MaskPoints point_set[SUBMASKS];
// Draw every submask as a new polygon
int total_submasks = command->keyframe_set->total_submasks(
command->start_position_project, PLAY_FORWARD);
for(int k = 0; k < total_submasks; k++) {
- MaskPointSet &points = point_set[k];
+ MaskPoints &points = point_set[k];
command->keyframe_set->get_points(&points,
k, command->start_position_project, PLAY_FORWARD);
float fader = command->keyframe_set->get_fader(
int show_mask = command->keyframe_set->track->masks;
for(int k = 0; k < total_submasks; k++) {
- MaskPointSet &points = point_set[k];
MaskEdge &edge = *edges.append(new MaskEdge());
if( !((show_mask>>k) & 1) ) continue;
- int first_point = 0;
-// Need to tabulate every vertex in persistent memory because
-// gluTessVertex doesn't copy them.
- for(int i = 0; i < points.total; i++) {
- MaskPoint *point1 = points.values[i];
- MaskPoint *point2 = (i >= points.total - 1) ?
- points.values[0] : points.values[i + 1];
-
- float x, y;
- int segments = 0;
- if( point1->control_x2 == 0 && point1->control_y2 == 0 &&
- point2->control_x1 == 0 && point2->control_y1 == 0 )
- segments = 1;
-
- float x0 = point1->x, y0 = point1->y;
- float x1 = point1->x + point1->control_x2;
- float y1 = point1->y + point1->control_y2;
- float x2 = point2->x + point2->control_x1;
- float y2 = point2->y + point2->control_y1;
- float x3 = point2->x, y3 = point2->y;
-
- // forward differencing bezier curves implementation taken from GPL code at
- // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
-
- float cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
-
- // [-1 +3 -3 +1]
- // [+3 -6 +3 0]
- // [-3 +3 0 0]
- // [+1 0 0 0]
-
- cx3 = - x0 + 3*x1 - 3*x2 + x3;
- cx2 = 3*x0 - 6*x1 + 3*x2;
- cx1 = -3*x0 + 3*x1;
- cx0 = x0;
-
- cy3 = - y0 + 3*y1 - 3*y2 + y3;
- cy2 = 3*y0 - 6*y1 + 3*y2;
- cy1 = -3*y0 + 3*y1;
- cy0 = y0;
-
- // This equation is from Graphics Gems I.
- //
- // The idea is that since we're approximating a cubic curve with lines,
- // any error we incur is due to the curvature of the line, which we can
- // estimate by calculating the maximum acceleration of the curve. For
- // a cubic, the acceleration (second derivative) is a line, meaning that
- // the absolute maximum acceleration must occur at either the beginning
- // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
- // conservative than that, but that's okay.
- if (segments == 0) {
- float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
- float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
-
- float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
- float h = 1.0;
-
- if(maxaccel > 8.0) h = sqrt((8.0) / maxaccel);
- segments = int(1/h);
- }
-
- for(int j = 0; j <= segments; j++) {
- float t = (float)j / segments;
- x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
- y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
-
- if(j > 0 || first_point) {
- edge.append(x, y - h);
- first_point = 0;
- }
- }
- }
+ edge.load(point_set[k], h);
if( edge.size() > 0 ) {
// draw polygon
float fader = faders[k];
[ . ])
PKG_3RD([ffmpeg],[yes],
- [ffmpeg-4.1],
+ [ffmpeg-4.1.4],
[ libavutil/libavutil.a \
libavcodec/libavcodec.a \
libpostproc/libpostproc.a \
int BC_PopupTextBoxText::handle_event()
{
+ popup->list_item = -1;
popup->handle_event();
return 1;
}
}
int BC_PopupTextBoxList::handle_event()
{
- BC_ListBoxItem *item = get_selection(0, 0);
- if(item)
- {
- popup->textbox->update(item->get_text());
+ int k = get_selection_number(0, 0);
+ popup->list_item = k;
+ if( k >= 0 && k < popup->list_items->size() ) {
+ popup->textbox->update(popup->list_items->get(k)->get_text());
popup->textbox->set_text_row(0);
popup->handle_event();
}
void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
{
+ list_items = data;
listbox->update(data, 0, 0, 1);
}
const char *BC_PopupTextBox::get_text() { return textbox->get_text(); }
const wchar_t *BC_PopupTextBox::get_wtext() { return textbox->get_wtext(); }
-int BC_PopupTextBox::get_number() { return listbox->get_selection_number(0, 0); }
+int BC_PopupTextBox::get_number() { return list_item; }
int BC_PopupTextBox::get_x() { return x; }
int BC_PopupTextBox::get_y() { return y; }
int BC_PopupTextBox::get_w() { return textbox->get_w() + listbox->get_w(); }
private:
int x, y, text_w, list_h;
- int list_format;
+ int list_format, list_item;
const char *default_text;
const wchar_t *default_wtext;
ArrayList<BC_ListBoxItem*> *list_items;
}
DeScratchMode::DeScratchMode(DeScratchWindow *win, int x, int y, int *value)
- : BC_PopupMenu(x, y, 64, "", 1)
+ : BC_PopupMenu(x, y, 100, "", 1)
{
this->win = win;
this->value = value;
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
+
flush_images();
new_toggle("blank30x30.png",
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
editpanel_up, editpanel_hi, editpanel_checked,
editpanel_dn, editpanel_checkedhi,
"tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
}
void CAKEWALKTHEME::get_vwindow_sizes(VWindowGUI *gui)
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
editpanel_dn, editpanel_checkedhi,
"tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
+
flush_images();
}
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
new_toggle("tan_smooth.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_smooth");
new_toggle("tan_linear.png", editpanel_up, editpanel_hi, editpanel_checked, editpanel_dn, editpanel_checkedhi, "tan_linear");
+ new_toggle("mask_scale_x.png", "mask_scale_xup.png", "mask_scale_xuphi.png",
+ "mask_scale_xchkd.png", "mask_scale_xdown.png", "mask_scale_xchkdhi.png",
+ "mask_scale_x");
+ new_toggle("mask_scale_y.png", "mask_scale_yup.png", "mask_scale_yuphi.png",
+ "mask_scale_ychkd.png", "mask_scale_ydown.png", "mask_scale_ychkdhi.png",
+ "mask_scale_y");
+ new_toggle("mask_scale_xy.png", "mask_scale_xyup.png", "mask_scale_xyuphi.png",
+ "mask_scale_xychkd.png", "mask_scale_xydown.png", "mask_scale_xychkdhi.png",
+ "mask_scale_xy");
flush_images();
$(call if_pkg,libvpx,--enable-libvpx) \
$(call if_pkg,x264,--enable-libx264) \
$(call if_pkg,x265,--enable-libx265) \
- --extra-cflags="\
+ --extra-cflags="-Wno-attributes \
$(if $(WANT_NV), $(inc_ffnvcodec)) \
$(call inc_path,twolame,libtwolame) \
$(call inc_path,lame,include) \
--- /dev/null
+diff -urN a/fftools/cmdutils.c b/fftools/cmdutils.c
+--- a/fftools/cmdutils.c 2018-10-01 10:52:48.866784675 -0600
++++ b/fftools/cmdutils.c 2018-10-01 10:52:55.550799827 -0600
+@@ -1179,6 +1179,7 @@
+
+ void show_banner(int argc, char **argv, const OptionDef *options)
+ {
++ return;
+ int idx = locate_option(argc, argv, options, "version");
+ if (hide_banner || idx)
+ return;
--- /dev/null
+diff -urN a/libavformat/bluray.c b/libavformat/bluray.c
+--- a/libavformat/bluray.c 2018-04-13 17:34:28.000000000 -0600
++++ b/libavformat/bluray.c 2018-04-24 11:02:19.724232178 -0600
+@@ -28,7 +28,7 @@
+ #include "libavutil/opt.h"
+
+ #define BLURAY_PROTO_PREFIX "bluray:"
+-#define MIN_PLAYLIST_LENGTH 180 /* 3 min */
++#define MIN_PLAYLIST_LENGTH 0
+
+ typedef struct {
+ const AVClass *class;
--- /dev/null
+diff -urN a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
+--- a/libavformat/mpegtsenc.c 2018-04-20 04:02:57.000000000 -0600
++++ b/libavformat/mpegtsenc.c 2018-04-24 10:27:57.193689213 -0600
+@@ -56,9 +56,8 @@
+ int sid; /* service ID */
+ char *name;
+ char *provider_name;
+- int pcr_pid;
+- int pcr_packet_count;
+- int pcr_packet_period;
++ int64_t pcr, pcr_packet_timer, pcr_packet_period;
++ int pcr_sid, pcr_pid;
+ AVProgram *program;
+ } MpegTSService;
+
+@@ -78,14 +77,12 @@
+ MpegTSSection pat; /* MPEG-2 PAT table */
+ MpegTSSection sdt; /* MPEG-2 SDT table context */
+ MpegTSService **services;
+- int sdt_packet_count;
+- int sdt_packet_period;
+- int pat_packet_count;
+- int pat_packet_period;
++ int64_t sdt_packet_timer, sdt_packet_period;
++ int64_t pat_packet_timer, pat_packet_period;
+ int nb_services;
+ int onid;
+ int tsid;
+- int64_t first_pcr;
++ int64_t pcr, first_pcr, delay;
+ int mux_rate; ///< set to 1 when VBR
+ int pes_payload_size;
+
+@@ -95,12 +92,14 @@
+ int service_type;
+
+ int pmt_start_pid;
++ int pcr_start_pid;
+ int start_pid;
+ int m2ts_mode;
++ int64_t ts_offset;
+
+ int reemit_pat_pmt; // backward compatibility
+
+- int pcr_period;
++ double pcr_period;
+ #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
+ #define MPEGTS_FLAG_AAC_LATM 0x02
+ #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
+@@ -111,8 +110,6 @@
+ int tables_version;
+ double pat_period;
+ double sdt_period;
+- int64_t last_pat_ts;
+- int64_t last_sdt_ts;
+
+ int omit_video_pes_length;
+ } MpegTSWrite;
+@@ -222,10 +219,10 @@
+ #define DEFAULT_PROVIDER_NAME "FFmpeg"
+ #define DEFAULT_SERVICE_NAME "Service01"
+
+-/* we retransmit the SI info at this rate */
++/* we retransmit the SI info at this rate (ms) */
+ #define SDT_RETRANS_TIME 500
+ #define PAT_RETRANS_TIME 100
+-#define PCR_RETRANS_TIME 20
++#define PCR_RETRANS_TIME 50
+
+ typedef struct MpegTSWriteStream {
+ struct MpegTSService *service;
+@@ -721,6 +718,7 @@
+ service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
+ service->sid = sid;
+ service->pcr_pid = 0x1fff;
++ service->pcr_sid = 0x1fff;
+ service->provider_name = av_strdup(provider_name);
+ service->name = av_strdup(name);
+ if (!service->provider_name || !service->name)
+@@ -736,18 +734,11 @@
+ return NULL;
+ }
+
+-static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
+-{
+- return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
+- ts->first_pcr;
+-}
+-
+ static void mpegts_prefix_m2ts_header(AVFormatContext *s)
+ {
+ MpegTSWrite *ts = s->priv_data;
+ if (ts->m2ts_mode) {
+- int64_t pcr = get_pcr(s->priv_data, s->pb);
+- uint32_t tp_extra_header = pcr % 0x3fffffff;
++ uint32_t tp_extra_header = ts->pcr % 0x3fffffff;
+ tp_extra_header = AV_RB32(&tp_extra_header);
+ avio_write(s->pb, (unsigned char *) &tp_extra_header,
+ sizeof(tp_extra_header));
+@@ -768,6 +759,7 @@
+ MpegTSService *service;
+ AVStream *st, *pcr_st = NULL;
+ AVDictionaryEntry *title, *provider;
++ double clk_rate;
+ int i, j;
+ const char *service_name;
+ const char *provider_name;
+@@ -776,6 +768,15 @@
+
+ if (s->max_delay < 0) /* Not set by the caller */
+ s->max_delay = 0;
++ ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
++
++ if (ts->m2ts_mode == -1) {
++ if (av_match_ext(s->url, "m2ts")) {
++ ts->m2ts_mode = 1;
++ } else {
++ ts->m2ts_mode = 0;
++ }
++ }
+
+ // round up to a whole number of TS packets
+ ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
+@@ -822,6 +823,8 @@
+ service->program = program;
+ }
+ }
++ if (ts->m2ts_mode > 1)
++ service->pmt.pid = 0x00ff + ts->service_id;
+
+ ts->pat.pid = PAT_PID;
+ /* Initialize at 15 so that it wraps and is equal to 0 for the
+@@ -907,10 +910,9 @@
+ ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
+ /* update PCR pid by using the first video stream */
+ if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
+- service->pcr_pid == 0x1fff) {
+- service->pcr_pid = ts_st->pid;
++ service->pcr_sid == 0x1fff)
+ pcr_st = st;
+- }
++
+ if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
+ st->codecpar->extradata_size > 0) {
+ AVStream *ast;
+@@ -946,78 +948,47 @@
+ av_freep(&pids);
+
+ /* if no video stream, use the first stream as PCR */
+- if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
+- pcr_st = s->streams[0];
+- ts_st = pcr_st->priv_data;
+- service->pcr_pid = ts_st->pid;
+- } else
+- ts_st = pcr_st->priv_data;
+-
+- if (ts->mux_rate > 1) {
+- service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
+- (TS_PACKET_SIZE * 8 * 1000);
+- ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
+- (TS_PACKET_SIZE * 8 * 1000);
+- ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
+- (TS_PACKET_SIZE * 8 * 1000);
+-
+- if (ts->copyts < 1)
+- ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
+- } else {
+- /* Arbitrary values, PAT/PMT will also be written on video key frames */
+- ts->sdt_packet_period = 200;
+- ts->pat_packet_period = 40;
+- if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+- int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
+- if (!frame_size) {
+- av_log(s, AV_LOG_WARNING, "frame size not set\n");
+- service->pcr_packet_period =
+- pcr_st->codecpar->sample_rate / (10 * 512);
+- } else {
+- service->pcr_packet_period =
+- pcr_st->codecpar->sample_rate / (10 * frame_size);
+- }
+- } else {
+- // max delta PCR 0.1s
+- // TODO: should be avg_frame_rate
+- service->pcr_packet_period =
+- ts_st->user_tb.den / (10 * ts_st->user_tb.num);
+- }
+- if (!service->pcr_packet_period)
+- service->pcr_packet_period = 1;
+- }
+-
+- ts->last_pat_ts = AV_NOPTS_VALUE;
+- ts->last_sdt_ts = AV_NOPTS_VALUE;
+- // The user specified a period, use only it
+- if (ts->pat_period < INT_MAX/2) {
+- ts->pat_packet_period = INT_MAX;
++ if (!pcr_st && s->nb_streams > 0)
++ pcr_st = s->streams[0];
++ if (!pcr_st) {
++ av_log(s, AV_LOG_ERROR, "no streams\n");
++ ret = AVERROR(EINVAL);
++ goto fail;
+ }
+- if (ts->sdt_period < INT_MAX/2) {
+- ts->sdt_packet_period = INT_MAX;
++ ts_st = pcr_st->priv_data;
++ if (service->pcr_sid == 0x1fff)
++ service->pcr_sid = ts_st->pid;
++ if (service->pcr_pid == 0x1fff)
++ service->pcr_pid = ts->m2ts_mode > 1 ?
++ 0x1000 + ts->service_id : service->pcr_sid ;
++ if (service->pmt.pid == service->pcr_pid) {
++ av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", service->pcr_pid);
++ ret = AVERROR(EINVAL);
++ goto fail;
+ }
+
++ clk_rate = ts->mux_rate > 1 ? ts->mux_rate : PCR_TIME_BASE;
++ ts->sdt_packet_period = ts->sdt_period < 0 ? -1 : ts->sdt_period/1000 * clk_rate;
++ ts->pat_packet_period = ts->pat_period/1000 * clk_rate;
++ service->pcr_packet_period = ts->pcr_period/1000 * clk_rate;
++ if (service->pcr_packet_period < (TS_PACKET_SIZE*8*10))
++ service->pcr_packet_period = (TS_PACKET_SIZE*8*10);
++ av_log(s, AV_LOG_VERBOSE, "clk_rate %f: ticks/pkt %d pcr, %d sdt, %d pmt\n", clk_rate,
++ (int)service->pcr_packet_period, (int)ts->sdt_packet_period, (int)ts->pat_packet_period);
++
++ if (ts->copyts < 1)
++ ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
++
+ // output a PCR as soon as possible
+- service->pcr_packet_count = service->pcr_packet_period;
+- ts->pat_packet_count = ts->pat_packet_period - 1;
+- ts->sdt_packet_count = ts->sdt_packet_period - 1;
++ ts->pcr = 0;
++ service->pcr_packet_timer = 0;
++ ts->pat_packet_timer = 0;
++ ts->sdt_packet_timer = 0;
+
+ if (ts->mux_rate == 1)
+ av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
+ else
+ av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
+- av_log(s, AV_LOG_VERBOSE,
+- "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
+- service->pcr_packet_period,
+- ts->sdt_packet_period, ts->pat_packet_period);
+-
+- if (ts->m2ts_mode == -1) {
+- if (av_match_ext(s->url, "m2ts")) {
+- ts->m2ts_mode = 1;
+- } else {
+- ts->m2ts_mode = 0;
+- }
+- }
+
+ return 0;
+
+@@ -1032,22 +1003,12 @@
+ MpegTSWrite *ts = s->priv_data;
+ int i;
+
+- if (++ts->sdt_packet_count == ts->sdt_packet_period ||
+- (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
+- (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
+- ) {
+- ts->sdt_packet_count = 0;
+- if (dts != AV_NOPTS_VALUE)
+- ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
++ if ( ts->sdt_packet_period >= 0 && ts->pcr >= ts->sdt_packet_timer ) {
++ ts->sdt_packet_timer = ts->pcr + ts->sdt_packet_period;
+ mpegts_write_sdt(s);
+ }
+- if (++ts->pat_packet_count == ts->pat_packet_period ||
+- (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
+- (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
+- force_pat) {
+- ts->pat_packet_count = 0;
+- if (dts != AV_NOPTS_VALUE)
+- ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
++ if (ts->pcr >= ts->pat_packet_timer || force_pat) {
++ ts->pat_packet_timer = ts->pcr + ts->pat_packet_period;
+ mpegts_write_pat(s);
+ for (i = 0; i < ts->nb_services; i++)
+ mpegts_write_pmt(s, ts->services[i]);
+@@ -1089,13 +1050,14 @@
+ {
+ MpegTSWrite *ts = s->priv_data;
+ MpegTSWriteStream *ts_st = st->priv_data;
++ uint32_t pcr_pid = ts_st->service->pcr_pid;
+ uint8_t *q;
+ uint8_t buf[TS_PACKET_SIZE];
+
+ q = buf;
+ *q++ = 0x47;
+- *q++ = ts_st->pid >> 8;
+- *q++ = ts_st->pid;
++ *q++ = pcr_pid >> 8;
++ *q++ = pcr_pid;
+ *q++ = 0x20 | ts_st->cc; /* Adaptation only */
+ /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
+ *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
+@@ -1106,7 +1068,7 @@
+ }
+
+ /* PCR coded into 6 bytes */
+- q += write_pcr_bits(q, get_pcr(ts, s->pb));
++ q += write_pcr_bits(q, ts->pcr);
+
+ /* stuffing bytes */
+ memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
+@@ -1175,8 +1137,6 @@
+ uint8_t *q;
+ int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
+ int afc_len, stuffing_len;
+- int64_t pcr = -1; /* avoid warning */
+- int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
+ int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
+
+ av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
+@@ -1186,28 +1146,33 @@
+
+ is_start = 1;
+ while (payload_size > 0) {
++ ts->pcr = ts->first_pcr + (ts->mux_rate == 1 ?
++ (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300) :
++ // add 11, pcr references the last byte of program clock reference base
++ av_rescale(avio_tell(s->pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate));
++
+ retransmit_si_info(s, force_pat, dts);
+ force_pat = 0;
+
+ write_pcr = 0;
+- if (ts_st->pid == ts_st->service->pcr_pid) {
+- if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
+- ts_st->service->pcr_packet_count++;
+- if (ts_st->service->pcr_packet_count >=
+- ts_st->service->pcr_packet_period) {
+- ts_st->service->pcr_packet_count = 0;
++ if (ts_st->pid == ts_st->service->pcr_sid) {
++ if( ts->pcr >= ts_st->service->pcr_packet_timer ) {
++ ts_st->service->pcr_packet_timer = ts->pcr + ts_st->service->pcr_packet_period;
+ write_pcr = 1;
+ }
+ }
+-
++ if (write_pcr && ts_st->service->pcr_sid != ts_st->service->pcr_pid) {
++ mpegts_insert_pcr_only(s, st);
++ continue;
++ }
+ if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
+- (dts - get_pcr(ts, s->pb) / 300) > delay) {
+- /* pcr insert gets priority over null packet insert */
+- if (write_pcr)
+- mpegts_insert_pcr_only(s, st);
++ (dts - ts->pcr / 300) > ts->delay) {
++ /* pcr insert gets priority over null packet insert */
++ if (write_pcr)
++ mpegts_insert_pcr_only(s, st);
+ else
+- mpegts_insert_null_packet(s);
+- /* recalculate write_pcr and possibly retransmit si_info */
++ mpegts_insert_null_packet(s);
++ /* recalculate write_pcr and possibly retransimit si_info */
+ continue;
+ }
+
+@@ -1217,6 +1182,10 @@
+ val = ts_st->pid >> 8;
+ if (is_start)
+ val |= 0x40;
++ if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
++ st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
++ ts->m2ts_mode > 1)
++ val |= 0x20;
+ *q++ = val;
+ *q++ = ts_st->pid;
+ ts_st->cc = ts_st->cc + 1 & 0xf;
+@@ -1228,7 +1197,7 @@
+ }
+ if (key && is_start && pts != AV_NOPTS_VALUE) {
+ // set Random Access for key frames
+- if (ts_st->pid == ts_st->service->pcr_pid)
++ if (ts_st->pid == ts_st->service->pcr_sid)
+ write_pcr = 1;
+ set_af_flag(buf, 0x40);
+ q = get_ts_payload_start(buf);
+@@ -1236,14 +1205,10 @@
+ if (write_pcr) {
+ set_af_flag(buf, 0x10);
+ q = get_ts_payload_start(buf);
+- // add 11, pcr references the last byte of program clock reference base
+ if (ts->mux_rate > 1)
+- pcr = get_pcr(ts, s->pb);
+- else
+- pcr = (dts - delay) * 300;
+- if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
++ if (dts != AV_NOPTS_VALUE && dts < ts->pcr / 300)
+ av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
+- extend_af(buf, write_pcr_bits(q, pcr));
++ extend_af(buf, write_pcr_bits(q, ts->pcr));
+ q = get_ts_payload_start(buf);
+ }
+ if (is_start) {
+@@ -1344,11 +1309,13 @@
+ *q++ = flags;
+ *q++ = header_len;
+ if (pts != AV_NOPTS_VALUE) {
+- write_pts(q, flags >> 6, pts);
++ int64_t ts_pts = pts + ts->ts_offset;
++ write_pts(q, flags >> 6, ts_pts);
+ q += 5;
+ }
+ if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
+- write_pts(q, 1, dts);
++ int64_t ts_dts = dts + ts->ts_offset;
++ write_pts(q, 1, ts_dts);
+ q += 5;
+ }
+ if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
+@@ -1519,7 +1486,6 @@
+ uint8_t *data = NULL;
+ MpegTSWrite *ts = s->priv_data;
+ MpegTSWriteStream *ts_st = st->priv_data;
+- const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
+ int64_t dts = pkt->dts, pts = pkt->pts;
+ int opus_samples = 0;
+ int side_data_size;
+@@ -1540,16 +1506,15 @@
+ }
+
+ if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
+- ts->pat_packet_count = ts->pat_packet_period - 1;
+- ts->sdt_packet_count = ts->sdt_packet_period - 1;
++ ts->pat_packet_timer = ts->sdt_packet_timer = 0;
+ ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
+ }
+
+ if (ts->copyts < 1) {
+ if (pts != AV_NOPTS_VALUE)
+- pts += delay;
++ pts += 2*ts->delay;
+ if (dts != AV_NOPTS_VALUE)
+- dts += delay;
++ dts += 2*ts->delay;
+ }
+
+ if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
+@@ -1737,7 +1702,7 @@
+ AVStream *st2 = s->streams[i];
+ MpegTSWriteStream *ts_st2 = st2->priv_data;
+ if ( ts_st2->payload_size
+- && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
++ && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > ts->delay)) {
+ mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
+ ts_st2->payload_pts, ts_st2->payload_dts,
+ ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
+@@ -1908,12 +1873,18 @@
+ { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
+ offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
+ { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
++ { "mpegts_pcr_start_pid", "Set the first pid of the PCR.",
++ offsetof(MpegTSWrite, pcr_start_pid), AV_OPT_TYPE_INT,
++ { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
+ { "mpegts_start_pid", "Set the first pid.",
+ offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
+ { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
+ { "mpegts_m2ts_mode", "Enable m2ts mode.",
+ offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
+- { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
++ { .i64 = -1 }, -1, 2, AV_OPT_FLAG_ENCODING_PARAM },
++ { "mpegts_pcr_offset", "clock offset.",
++ offsetof(MpegTSWrite, ts_offset), AV_OPT_TYPE_BOOL,
++ { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+ { "muxrate", NULL,
+ offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
+ { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+@@ -1951,15 +1922,15 @@
+ { "omit_video_pes_length", "Omit the PES packet length for video packets",
+ offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
+ { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
+- { "pcr_period", "PCR retransmission time in milliseconds",
+- offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
+- { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+- { "pat_period", "PAT/PMT retransmission time limit in seconds",
++ { "pcr_period", "PCR retransmission time limit in msecs",
++ offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_DOUBLE,
++ { .dbl = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
++ { "pat_period", "PAT/PMT retransmission time limit in msecs",
+ offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
+- { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+- { "sdt_period", "SDT retransmission time limit in seconds",
++ { .dbl = PAT_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
++ { "sdt_period", "SDT retransmission time limit in msecs",
+ offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
+- { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
++ { .dbl = SDT_RETRANS_TIME }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+ { NULL },
+ };
+
--- /dev/null
+diff -urN a/libavformat/avformat.h b/libavformat/avformat.h
+--- a/libavformat/avformat.h 2018-11-05 16:22:26.000000000 -0700
++++ b/libavformat/avformat.h 2018-11-08 07:25:17.066799941 -0700
+@@ -487,6 +487,9 @@
+ The user or muxer can override this through
+ AVFormatContext.avoid_negative_ts
+ */
++#define AVFMT_SEEK_NOSTREAMS 0x80000 /**< Stream index ignored by seek,
++ or some streams fail to seek
++ */
+
+ #define AVFMT_SEEK_TO_PTS 0x4000000 /**< Seeking is based on PTS */
+
+@@ -647,7 +650,8 @@
+ /**
+ * Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS,
+ * AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH,
+- * AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK, AVFMT_SEEK_TO_PTS.
++ * AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK, AVFMT_SEEK_TO_PTS,
++ * AVFMT_SEEK_NOSTREAMS
+ */
+ int flags;
+
+diff -urN a/libavformat/dv.c b/libavformat/dv.c
+--- a/libavformat/dv.c 2018-11-01 12:34:26.000000000 -0600
++++ b/libavformat/dv.c 2018-11-08 07:25:17.066799941 -0700
+@@ -632,6 +632,7 @@
+ AVInputFormat ff_dv_demuxer = {
+ .name = "dv",
+ .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
++ .flags = AVFMT_SEEK_NOSTREAMS,
+ .priv_data_size = sizeof(RawDVContext),
+ .read_probe = dv_probe,
+ .read_header = dv_read_header,
+diff -urN a/libavformat/matroskadec.c b/libavformat/matroskadec.c
+--- a/libavformat/matroskadec.c 2018-11-05 16:22:26.000000000 -0700
++++ b/libavformat/matroskadec.c 2018-11-08 07:25:17.067799930 -0700
+@@ -4030,6 +4030,7 @@
+ AVInputFormat ff_matroska_demuxer = {
+ .name = "matroska,webm",
+ .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
++ .flags = AVFMT_SEEK_NOSTREAMS,
+ .extensions = "mkv,mk3d,mka,mks",
+ .priv_data_size = sizeof(MatroskaDemuxContext),
+ .read_probe = matroska_probe,
+@@ -4043,6 +4044,7 @@
+ AVInputFormat ff_webm_dash_manifest_demuxer = {
+ .name = "webm_dash_manifest",
+ .long_name = NULL_IF_CONFIG_SMALL("WebM DASH Manifest"),
++ .flags = AVFMT_SEEK_NOSTREAMS,
+ .priv_data_size = sizeof(MatroskaDemuxContext),
+ .read_header = webm_dash_manifest_read_header,
+ .read_packet = webm_dash_manifest_read_packet,
+diff -urN a/libavformat/utils.c b/libavformat/utils.c
+--- a/libavformat/utils.c 2018-11-05 16:22:26.000000000 -0700
++++ b/libavformat/utils.c 2018-11-08 07:25:17.069799908 -0700
+@@ -2472,6 +2472,13 @@
+ return seek_frame_byte(s, stream_index, timestamp, flags);
+ }
+
++ if (stream_index != -1 && (s->iformat->flags & AVFMT_SEEK_NOSTREAMS)) {
++ timestamp = av_rescale_q(timestamp,
++ s->streams[stream_index]->time_base,
++ AV_TIME_BASE_Q);
++ stream_index = -1;
++ }
++
+ if (stream_index < 0) {
+ stream_index = av_find_default_stream_index(s);
+ if (stream_index < 0)
--- /dev/null
+--- a/libavfilter/af_aformat.c 2018-07-17 03:27:41.000000000 -0600
++++ b/libavfilter/af_aformat.c 2019-03-16 17:55:28.449442750 -0600
+@@ -109,6 +109,16 @@
+ return 0;
+ }
+
++#define DEL_FIELD(p,mem,fld) if( p->mem ) { av_freep(&p->mem->fld); av_freep(&p->mem); }
++
++static av_cold void uninit(AVFilterContext *ctx)
++{
++ AFormatContext *s = ctx->priv;
++ DEL_FIELD(s, formats, formats);
++ DEL_FIELD(s, sample_rates, formats);
++ DEL_FIELD(s, channel_layouts, channel_layouts);
++}
++
+ static int query_formats(AVFilterContext *ctx)
+ {
+ AFormatContext *s = ctx->priv;
+@@ -146,6 +156,7 @@
+ .name = "aformat",
+ .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
+ .init = init,
++ .uninit = uninit,
+ .query_formats = query_formats,
+ .priv_size = sizeof(AFormatContext),
+ .priv_class = &aformat_class,
--- /dev/null
+diff -u a/libavfilter/formats.c b/libavfilter/formats.c
+--- a/libavfilter/formats.c 2018-11-02 18:17:29.000000000 -0600
++++ b/libavfilter/formats.c 2019-04-09 14:12:01.659501027 -0600
+@@ -107,11 +107,13 @@
+ possibly causing a lossy conversion elsewhere in the graph.
+ To avoid that, pretend that there are no common formats to force the
+ insertion of a conversion filter. */
+- if (type == AVMEDIA_TYPE_VIDEO)
+- for (i = 0; i < a->nb_formats; i++)
++ if (type == AVMEDIA_TYPE_VIDEO) {
++ for (i = 0; i < a->nb_formats; i++) {
++ const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
++ if( !adesc ) continue;
+ for (j = 0; j < b->nb_formats; j++) {
+- const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
+ const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
++ if( !bdesc ) continue;
+ alpha2 |= adesc->flags & bdesc->flags & AV_PIX_FMT_FLAG_ALPHA;
+ chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
+ if (a->formats[i] == b->formats[j]) {
+@@ -119,6 +121,8 @@
+ chroma1|= adesc->nb_components > 1;
+ }
+ }
++ }
++ }
+
+ // If chroma or alpha can be lost through merging then do not merge
+ if (alpha2 > alpha1 || chroma2 > chroma1)
--- /dev/null
+diff -u a/libavcodec/vdpau_mpeg12.c b/libavcodec/vdpau_mpeg12.c
+--- a/libavcodec/vdpau_mpeg12.c 2018-11-02 18:17:29.000000000 -0600
++++ b/libavcodec/vdpau_mpeg12.c 2019-04-22 10:28:41.762275864 -0600
+@@ -114,6 +114,7 @@
+ .frame_priv_data_size = sizeof(struct vdpau_picture_context),
+ .init = vdpau_mpeg1_init,
+ .uninit = ff_vdpau_common_uninit,
++ .frame_params = ff_vdpau_common_frame_params,
+ .priv_data_size = sizeof(VDPAUContext),
+ .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
+ };
--- /dev/null
+diff -urN a/libavcodec/h263dec.c b/libavcodec/h263dec.c
+--- a/libavcodec/h263dec.c
++++ b/libavcodec/h263dec.c
+@@ -684,7 +684,7 @@ frame_end:
+ if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
+ ff_mpeg4_frame_end(avctx, buf, buf_size);
+
+- if (!s->divx_packed && avctx->hwaccel)
++ if (s->divx_packed && avctx->hwaccel)
+ ff_thread_finish_setup(avctx);
+
+ av_assert1(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);
+++ /dev/null
-diff -urN a/fftools/cmdutils.c b/fftools/cmdutils.c
---- a/fftools/cmdutils.c 2018-10-01 10:52:48.866784675 -0600
-+++ b/fftools/cmdutils.c 2018-10-01 10:52:55.550799827 -0600
-@@ -1179,6 +1179,7 @@
-
- void show_banner(int argc, char **argv, const OptionDef *options)
- {
-+ return;
- int idx = locate_option(argc, argv, options, "version");
- if (hide_banner || idx)
- return;
+++ /dev/null
-diff -urN a/libavformat/bluray.c b/libavformat/bluray.c
---- a/libavformat/bluray.c 2018-04-13 17:34:28.000000000 -0600
-+++ b/libavformat/bluray.c 2018-04-24 11:02:19.724232178 -0600
-@@ -28,7 +28,7 @@
- #include "libavutil/opt.h"
-
- #define BLURAY_PROTO_PREFIX "bluray:"
--#define MIN_PLAYLIST_LENGTH 180 /* 3 min */
-+#define MIN_PLAYLIST_LENGTH 0
-
- typedef struct {
- const AVClass *class;
+++ /dev/null
-diff -urN a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
---- a/libavformat/mpegtsenc.c 2018-04-20 04:02:57.000000000 -0600
-+++ b/libavformat/mpegtsenc.c 2018-04-24 10:27:57.193689213 -0600
-@@ -56,9 +56,8 @@
- int sid; /* service ID */
- char *name;
- char *provider_name;
-- int pcr_pid;
-- int pcr_packet_count;
-- int pcr_packet_period;
-+ int64_t pcr, pcr_packet_timer, pcr_packet_period;
-+ int pcr_sid, pcr_pid;
- AVProgram *program;
- } MpegTSService;
-
-@@ -78,14 +77,12 @@
- MpegTSSection pat; /* MPEG-2 PAT table */
- MpegTSSection sdt; /* MPEG-2 SDT table context */
- MpegTSService **services;
-- int sdt_packet_count;
-- int sdt_packet_period;
-- int pat_packet_count;
-- int pat_packet_period;
-+ int64_t sdt_packet_timer, sdt_packet_period;
-+ int64_t pat_packet_timer, pat_packet_period;
- int nb_services;
- int onid;
- int tsid;
-- int64_t first_pcr;
-+ int64_t pcr, first_pcr, delay;
- int mux_rate; ///< set to 1 when VBR
- int pes_payload_size;
-
-@@ -95,12 +92,14 @@
- int service_type;
-
- int pmt_start_pid;
-+ int pcr_start_pid;
- int start_pid;
- int m2ts_mode;
-+ int64_t ts_offset;
-
- int reemit_pat_pmt; // backward compatibility
-
-- int pcr_period;
-+ double pcr_period;
- #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
- #define MPEGTS_FLAG_AAC_LATM 0x02
- #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
-@@ -111,8 +110,6 @@
- int tables_version;
- double pat_period;
- double sdt_period;
-- int64_t last_pat_ts;
-- int64_t last_sdt_ts;
-
- int omit_video_pes_length;
- } MpegTSWrite;
-@@ -222,10 +219,10 @@
- #define DEFAULT_PROVIDER_NAME "FFmpeg"
- #define DEFAULT_SERVICE_NAME "Service01"
-
--/* we retransmit the SI info at this rate */
-+/* we retransmit the SI info at this rate (ms) */
- #define SDT_RETRANS_TIME 500
- #define PAT_RETRANS_TIME 100
--#define PCR_RETRANS_TIME 20
-+#define PCR_RETRANS_TIME 50
-
- typedef struct MpegTSWriteStream {
- struct MpegTSService *service;
-@@ -721,6 +718,7 @@
- service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
- service->sid = sid;
- service->pcr_pid = 0x1fff;
-+ service->pcr_sid = 0x1fff;
- service->provider_name = av_strdup(provider_name);
- service->name = av_strdup(name);
- if (!service->provider_name || !service->name)
-@@ -736,18 +734,11 @@
- return NULL;
- }
-
--static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
--{
-- return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
-- ts->first_pcr;
--}
--
- static void mpegts_prefix_m2ts_header(AVFormatContext *s)
- {
- MpegTSWrite *ts = s->priv_data;
- if (ts->m2ts_mode) {
-- int64_t pcr = get_pcr(s->priv_data, s->pb);
-- uint32_t tp_extra_header = pcr % 0x3fffffff;
-+ uint32_t tp_extra_header = ts->pcr % 0x3fffffff;
- tp_extra_header = AV_RB32(&tp_extra_header);
- avio_write(s->pb, (unsigned char *) &tp_extra_header,
- sizeof(tp_extra_header));
-@@ -768,6 +759,7 @@
- MpegTSService *service;
- AVStream *st, *pcr_st = NULL;
- AVDictionaryEntry *title, *provider;
-+ double clk_rate;
- int i, j;
- const char *service_name;
- const char *provider_name;
-@@ -776,6 +768,15 @@
-
- if (s->max_delay < 0) /* Not set by the caller */
- s->max_delay = 0;
-+ ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
-+
-+ if (ts->m2ts_mode == -1) {
-+ if (av_match_ext(s->url, "m2ts")) {
-+ ts->m2ts_mode = 1;
-+ } else {
-+ ts->m2ts_mode = 0;
-+ }
-+ }
-
- // round up to a whole number of TS packets
- ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
-@@ -822,6 +823,8 @@
- service->program = program;
- }
- }
-+ if (ts->m2ts_mode > 1)
-+ service->pmt.pid = 0x00ff + ts->service_id;
-
- ts->pat.pid = PAT_PID;
- /* Initialize at 15 so that it wraps and is equal to 0 for the
-@@ -907,10 +910,9 @@
- ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
- /* update PCR pid by using the first video stream */
- if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
-- service->pcr_pid == 0x1fff) {
-- service->pcr_pid = ts_st->pid;
-+ service->pcr_sid == 0x1fff)
- pcr_st = st;
-- }
-+
- if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
- st->codecpar->extradata_size > 0) {
- AVStream *ast;
-@@ -946,78 +948,47 @@
- av_freep(&pids);
-
- /* if no video stream, use the first stream as PCR */
-- if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
-- pcr_st = s->streams[0];
-- ts_st = pcr_st->priv_data;
-- service->pcr_pid = ts_st->pid;
-- } else
-- ts_st = pcr_st->priv_data;
--
-- if (ts->mux_rate > 1) {
-- service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
-- (TS_PACKET_SIZE * 8 * 1000);
-- ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
-- (TS_PACKET_SIZE * 8 * 1000);
-- ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
-- (TS_PACKET_SIZE * 8 * 1000);
--
-- if (ts->copyts < 1)
-- ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
-- } else {
-- /* Arbitrary values, PAT/PMT will also be written on video key frames */
-- ts->sdt_packet_period = 200;
-- ts->pat_packet_period = 40;
-- if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
-- int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
-- if (!frame_size) {
-- av_log(s, AV_LOG_WARNING, "frame size not set\n");
-- service->pcr_packet_period =
-- pcr_st->codecpar->sample_rate / (10 * 512);
-- } else {
-- service->pcr_packet_period =
-- pcr_st->codecpar->sample_rate / (10 * frame_size);
-- }
-- } else {
-- // max delta PCR 0.1s
-- // TODO: should be avg_frame_rate
-- service->pcr_packet_period =
-- ts_st->user_tb.den / (10 * ts_st->user_tb.num);
-- }
-- if (!service->pcr_packet_period)
-- service->pcr_packet_period = 1;
-- }
--
-- ts->last_pat_ts = AV_NOPTS_VALUE;
-- ts->last_sdt_ts = AV_NOPTS_VALUE;
-- // The user specified a period, use only it
-- if (ts->pat_period < INT_MAX/2) {
-- ts->pat_packet_period = INT_MAX;
-+ if (!pcr_st && s->nb_streams > 0)
-+ pcr_st = s->streams[0];
-+ if (!pcr_st) {
-+ av_log(s, AV_LOG_ERROR, "no streams\n");
-+ ret = AVERROR(EINVAL);
-+ goto fail;
- }
-- if (ts->sdt_period < INT_MAX/2) {
-- ts->sdt_packet_period = INT_MAX;
-+ ts_st = pcr_st->priv_data;
-+ if (service->pcr_sid == 0x1fff)
-+ service->pcr_sid = ts_st->pid;
-+ if (service->pcr_pid == 0x1fff)
-+ service->pcr_pid = ts->m2ts_mode > 1 ?
-+ 0x1000 + ts->service_id : service->pcr_sid ;
-+ if (service->pmt.pid == service->pcr_pid) {
-+ av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", service->pcr_pid);
-+ ret = AVERROR(EINVAL);
-+ goto fail;
- }
-
-+ clk_rate = ts->mux_rate > 1 ? ts->mux_rate : PCR_TIME_BASE;
-+ ts->sdt_packet_period = ts->sdt_period < 0 ? -1 : ts->sdt_period/1000 * clk_rate;
-+ ts->pat_packet_period = ts->pat_period/1000 * clk_rate;
-+ service->pcr_packet_period = ts->pcr_period/1000 * clk_rate;
-+ if (service->pcr_packet_period < (TS_PACKET_SIZE*8*10))
-+ service->pcr_packet_period = (TS_PACKET_SIZE*8*10);
-+ av_log(s, AV_LOG_VERBOSE, "clk_rate %f: ticks/pkt %d pcr, %d sdt, %d pmt\n", clk_rate,
-+ (int)service->pcr_packet_period, (int)ts->sdt_packet_period, (int)ts->pat_packet_period);
-+
-+ if (ts->copyts < 1)
-+ ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
-+
- // output a PCR as soon as possible
-- service->pcr_packet_count = service->pcr_packet_period;
-- ts->pat_packet_count = ts->pat_packet_period - 1;
-- ts->sdt_packet_count = ts->sdt_packet_period - 1;
-+ ts->pcr = 0;
-+ service->pcr_packet_timer = 0;
-+ ts->pat_packet_timer = 0;
-+ ts->sdt_packet_timer = 0;
-
- if (ts->mux_rate == 1)
- av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
- else
- av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
-- av_log(s, AV_LOG_VERBOSE,
-- "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
-- service->pcr_packet_period,
-- ts->sdt_packet_period, ts->pat_packet_period);
--
-- if (ts->m2ts_mode == -1) {
-- if (av_match_ext(s->url, "m2ts")) {
-- ts->m2ts_mode = 1;
-- } else {
-- ts->m2ts_mode = 0;
-- }
-- }
-
- return 0;
-
-@@ -1032,22 +1003,12 @@
- MpegTSWrite *ts = s->priv_data;
- int i;
-
-- if (++ts->sdt_packet_count == ts->sdt_packet_period ||
-- (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
-- (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
-- ) {
-- ts->sdt_packet_count = 0;
-- if (dts != AV_NOPTS_VALUE)
-- ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
-+ if ( ts->sdt_packet_period >= 0 && ts->pcr >= ts->sdt_packet_timer ) {
-+ ts->sdt_packet_timer = ts->pcr + ts->sdt_packet_period;
- mpegts_write_sdt(s);
- }
-- if (++ts->pat_packet_count == ts->pat_packet_period ||
-- (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
-- (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
-- force_pat) {
-- ts->pat_packet_count = 0;
-- if (dts != AV_NOPTS_VALUE)
-- ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
-+ if (ts->pcr >= ts->pat_packet_timer || force_pat) {
-+ ts->pat_packet_timer = ts->pcr + ts->pat_packet_period;
- mpegts_write_pat(s);
- for (i = 0; i < ts->nb_services; i++)
- mpegts_write_pmt(s, ts->services[i]);
-@@ -1089,13 +1050,14 @@
- {
- MpegTSWrite *ts = s->priv_data;
- MpegTSWriteStream *ts_st = st->priv_data;
-+ uint32_t pcr_pid = ts_st->service->pcr_pid;
- uint8_t *q;
- uint8_t buf[TS_PACKET_SIZE];
-
- q = buf;
- *q++ = 0x47;
-- *q++ = ts_st->pid >> 8;
-- *q++ = ts_st->pid;
-+ *q++ = pcr_pid >> 8;
-+ *q++ = pcr_pid;
- *q++ = 0x20 | ts_st->cc; /* Adaptation only */
- /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
- *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
-@@ -1106,7 +1068,7 @@
- }
-
- /* PCR coded into 6 bytes */
-- q += write_pcr_bits(q, get_pcr(ts, s->pb));
-+ q += write_pcr_bits(q, ts->pcr);
-
- /* stuffing bytes */
- memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
-@@ -1175,8 +1137,6 @@
- uint8_t *q;
- int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
- int afc_len, stuffing_len;
-- int64_t pcr = -1; /* avoid warning */
-- int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
- int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
-
- av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
-@@ -1186,28 +1146,33 @@
-
- is_start = 1;
- while (payload_size > 0) {
-+ ts->pcr = ts->first_pcr + (ts->mux_rate == 1 ?
-+ (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300) :
-+ // add 11, pcr references the last byte of program clock reference base
-+ av_rescale(avio_tell(s->pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate));
-+
- retransmit_si_info(s, force_pat, dts);
- force_pat = 0;
-
- write_pcr = 0;
-- if (ts_st->pid == ts_st->service->pcr_pid) {
-- if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
-- ts_st->service->pcr_packet_count++;
-- if (ts_st->service->pcr_packet_count >=
-- ts_st->service->pcr_packet_period) {
-- ts_st->service->pcr_packet_count = 0;
-+ if (ts_st->pid == ts_st->service->pcr_sid) {
-+ if( ts->pcr >= ts_st->service->pcr_packet_timer ) {
-+ ts_st->service->pcr_packet_timer = ts->pcr + ts_st->service->pcr_packet_period;
- write_pcr = 1;
- }
- }
--
-+ if (write_pcr && ts_st->service->pcr_sid != ts_st->service->pcr_pid) {
-+ mpegts_insert_pcr_only(s, st);
-+ continue;
-+ }
- if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
-- (dts - get_pcr(ts, s->pb) / 300) > delay) {
-- /* pcr insert gets priority over null packet insert */
-- if (write_pcr)
-- mpegts_insert_pcr_only(s, st);
-+ (dts - ts->pcr / 300) > ts->delay) {
-+ /* pcr insert gets priority over null packet insert */
-+ if (write_pcr)
-+ mpegts_insert_pcr_only(s, st);
- else
-- mpegts_insert_null_packet(s);
-- /* recalculate write_pcr and possibly retransmit si_info */
-+ mpegts_insert_null_packet(s);
-+ /* recalculate write_pcr and possibly retransimit si_info */
- continue;
- }
-
-@@ -1217,6 +1182,10 @@
- val = ts_st->pid >> 8;
- if (is_start)
- val |= 0x40;
-+ if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
-+ st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
-+ ts->m2ts_mode > 1)
-+ val |= 0x20;
- *q++ = val;
- *q++ = ts_st->pid;
- ts_st->cc = ts_st->cc + 1 & 0xf;
-@@ -1228,7 +1197,7 @@
- }
- if (key && is_start && pts != AV_NOPTS_VALUE) {
- // set Random Access for key frames
-- if (ts_st->pid == ts_st->service->pcr_pid)
-+ if (ts_st->pid == ts_st->service->pcr_sid)
- write_pcr = 1;
- set_af_flag(buf, 0x40);
- q = get_ts_payload_start(buf);
-@@ -1236,14 +1205,10 @@
- if (write_pcr) {
- set_af_flag(buf, 0x10);
- q = get_ts_payload_start(buf);
-- // add 11, pcr references the last byte of program clock reference base
- if (ts->mux_rate > 1)
-- pcr = get_pcr(ts, s->pb);
-- else
-- pcr = (dts - delay) * 300;
-- if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
-+ if (dts != AV_NOPTS_VALUE && dts < ts->pcr / 300)
- av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
-- extend_af(buf, write_pcr_bits(q, pcr));
-+ extend_af(buf, write_pcr_bits(q, ts->pcr));
- q = get_ts_payload_start(buf);
- }
- if (is_start) {
-@@ -1344,11 +1309,13 @@
- *q++ = flags;
- *q++ = header_len;
- if (pts != AV_NOPTS_VALUE) {
-- write_pts(q, flags >> 6, pts);
-+ int64_t ts_pts = pts + ts->ts_offset;
-+ write_pts(q, flags >> 6, ts_pts);
- q += 5;
- }
- if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
-- write_pts(q, 1, dts);
-+ int64_t ts_dts = dts + ts->ts_offset;
-+ write_pts(q, 1, ts_dts);
- q += 5;
- }
- if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
-@@ -1519,7 +1486,6 @@
- uint8_t *data = NULL;
- MpegTSWrite *ts = s->priv_data;
- MpegTSWriteStream *ts_st = st->priv_data;
-- const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
- int64_t dts = pkt->dts, pts = pkt->pts;
- int opus_samples = 0;
- int side_data_size;
-@@ -1540,16 +1506,15 @@
- }
-
- if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
-- ts->pat_packet_count = ts->pat_packet_period - 1;
-- ts->sdt_packet_count = ts->sdt_packet_period - 1;
-+ ts->pat_packet_timer = ts->sdt_packet_timer = 0;
- ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
- }
-
- if (ts->copyts < 1) {
- if (pts != AV_NOPTS_VALUE)
-- pts += delay;
-+ pts += 2*ts->delay;
- if (dts != AV_NOPTS_VALUE)
-- dts += delay;
-+ dts += 2*ts->delay;
- }
-
- if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
-@@ -1737,7 +1702,7 @@
- AVStream *st2 = s->streams[i];
- MpegTSWriteStream *ts_st2 = st2->priv_data;
- if ( ts_st2->payload_size
-- && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
-+ && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > ts->delay)) {
- mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
- ts_st2->payload_pts, ts_st2->payload_dts,
- ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
-@@ -1908,12 +1873,18 @@
- { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
- offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
- { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
-+ { "mpegts_pcr_start_pid", "Set the first pid of the PCR.",
-+ offsetof(MpegTSWrite, pcr_start_pid), AV_OPT_TYPE_INT,
-+ { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
- { "mpegts_start_pid", "Set the first pid.",
- offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
- { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
- { "mpegts_m2ts_mode", "Enable m2ts mode.",
- offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
-- { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
-+ { .i64 = -1 }, -1, 2, AV_OPT_FLAG_ENCODING_PARAM },
-+ { "mpegts_pcr_offset", "clock offset.",
-+ offsetof(MpegTSWrite, ts_offset), AV_OPT_TYPE_BOOL,
-+ { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
- { "muxrate", NULL,
- offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
- { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-@@ -1951,15 +1922,15 @@
- { "omit_video_pes_length", "Omit the PES packet length for video packets",
- offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
- { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
-- { "pcr_period", "PCR retransmission time in milliseconds",
-- offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
-- { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-- { "pat_period", "PAT/PMT retransmission time limit in seconds",
-+ { "pcr_period", "PCR retransmission time limit in msecs",
-+ offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_DOUBLE,
-+ { .dbl = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-+ { "pat_period", "PAT/PMT retransmission time limit in msecs",
- offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
-- { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-- { "sdt_period", "SDT retransmission time limit in seconds",
-+ { .dbl = PAT_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-+ { "sdt_period", "SDT retransmission time limit in msecs",
- offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
-- { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-+ { .dbl = SDT_RETRANS_TIME }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
- { NULL },
- };
-
+++ /dev/null
-diff -urN a/libavformat/avformat.h b/libavformat/avformat.h
---- a/libavformat/avformat.h 2018-11-05 16:22:26.000000000 -0700
-+++ b/libavformat/avformat.h 2018-11-08 07:25:17.066799941 -0700
-@@ -487,6 +487,9 @@
- The user or muxer can override this through
- AVFormatContext.avoid_negative_ts
- */
-+#define AVFMT_SEEK_NOSTREAMS 0x80000 /**< Stream index ignored by seek,
-+ or some streams fail to seek
-+ */
-
- #define AVFMT_SEEK_TO_PTS 0x4000000 /**< Seeking is based on PTS */
-
-@@ -647,7 +650,8 @@
- /**
- * Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS,
- * AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH,
-- * AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK, AVFMT_SEEK_TO_PTS.
-+ * AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK, AVFMT_SEEK_TO_PTS,
-+ * AVFMT_SEEK_NOSTREAMS
- */
- int flags;
-
-diff -urN a/libavformat/dv.c b/libavformat/dv.c
---- a/libavformat/dv.c 2018-11-01 12:34:26.000000000 -0600
-+++ b/libavformat/dv.c 2018-11-08 07:25:17.066799941 -0700
-@@ -632,6 +632,7 @@
- AVInputFormat ff_dv_demuxer = {
- .name = "dv",
- .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
-+ .flags = AVFMT_SEEK_NOSTREAMS,
- .priv_data_size = sizeof(RawDVContext),
- .read_probe = dv_probe,
- .read_header = dv_read_header,
-diff -urN a/libavformat/matroskadec.c b/libavformat/matroskadec.c
---- a/libavformat/matroskadec.c 2018-11-05 16:22:26.000000000 -0700
-+++ b/libavformat/matroskadec.c 2018-11-08 07:25:17.067799930 -0700
-@@ -4030,6 +4030,7 @@
- AVInputFormat ff_matroska_demuxer = {
- .name = "matroska,webm",
- .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
-+ .flags = AVFMT_SEEK_NOSTREAMS,
- .extensions = "mkv,mk3d,mka,mks",
- .priv_data_size = sizeof(MatroskaDemuxContext),
- .read_probe = matroska_probe,
-@@ -4043,6 +4044,7 @@
- AVInputFormat ff_webm_dash_manifest_demuxer = {
- .name = "webm_dash_manifest",
- .long_name = NULL_IF_CONFIG_SMALL("WebM DASH Manifest"),
-+ .flags = AVFMT_SEEK_NOSTREAMS,
- .priv_data_size = sizeof(MatroskaDemuxContext),
- .read_header = webm_dash_manifest_read_header,
- .read_packet = webm_dash_manifest_read_packet,
-diff -urN a/libavformat/utils.c b/libavformat/utils.c
---- a/libavformat/utils.c 2018-11-05 16:22:26.000000000 -0700
-+++ b/libavformat/utils.c 2018-11-08 07:25:17.069799908 -0700
-@@ -2472,6 +2472,13 @@
- return seek_frame_byte(s, stream_index, timestamp, flags);
- }
-
-+ if (stream_index != -1 && (s->iformat->flags & AVFMT_SEEK_NOSTREAMS)) {
-+ timestamp = av_rescale_q(timestamp,
-+ s->streams[stream_index]->time_base,
-+ AV_TIME_BASE_Q);
-+ stream_index = -1;
-+ }
-+
- if (stream_index < 0) {
- stream_index = av_find_default_stream_index(s);
- if (stream_index < 0)
+++ /dev/null
---- a/libavfilter/af_aformat.c 2018-07-17 03:27:41.000000000 -0600
-+++ b/libavfilter/af_aformat.c 2019-03-16 17:55:28.449442750 -0600
-@@ -109,6 +109,16 @@
- return 0;
- }
-
-+#define DEL_FIELD(p,mem,fld) if( p->mem ) { av_freep(&p->mem->fld); av_freep(&p->mem); }
-+
-+static av_cold void uninit(AVFilterContext *ctx)
-+{
-+ AFormatContext *s = ctx->priv;
-+ DEL_FIELD(s, formats, formats);
-+ DEL_FIELD(s, sample_rates, formats);
-+ DEL_FIELD(s, channel_layouts, channel_layouts);
-+}
-+
- static int query_formats(AVFilterContext *ctx)
- {
- AFormatContext *s = ctx->priv;
-@@ -146,6 +156,7 @@
- .name = "aformat",
- .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
- .init = init,
-+ .uninit = uninit,
- .query_formats = query_formats,
- .priv_size = sizeof(AFormatContext),
- .priv_class = &aformat_class,
+++ /dev/null
-diff -u a/libavfilter/formats.c b/libavfilter/formats.c
---- a/libavfilter/formats.c 2018-11-02 18:17:29.000000000 -0600
-+++ b/libavfilter/formats.c 2019-04-09 14:12:01.659501027 -0600
-@@ -107,11 +107,13 @@
- possibly causing a lossy conversion elsewhere in the graph.
- To avoid that, pretend that there are no common formats to force the
- insertion of a conversion filter. */
-- if (type == AVMEDIA_TYPE_VIDEO)
-- for (i = 0; i < a->nb_formats; i++)
-+ if (type == AVMEDIA_TYPE_VIDEO) {
-+ for (i = 0; i < a->nb_formats; i++) {
-+ const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
-+ if( !adesc ) continue;
- for (j = 0; j < b->nb_formats; j++) {
-- const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
- const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
-+ if( !bdesc ) continue;
- alpha2 |= adesc->flags & bdesc->flags & AV_PIX_FMT_FLAG_ALPHA;
- chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
- if (a->formats[i] == b->formats[j]) {
-@@ -119,6 +121,8 @@
- chroma1|= adesc->nb_components > 1;
- }
- }
-+ }
-+ }
-
- // If chroma or alpha can be lost through merging then do not merge
- if (alpha2 > alpha1 || chroma2 > chroma1)
+++ /dev/null
-diff -u a/libavcodec/vdpau_mpeg12.c b/libavcodec/vdpau_mpeg12.c
---- a/libavcodec/vdpau_mpeg12.c 2018-11-02 18:17:29.000000000 -0600
-+++ b/libavcodec/vdpau_mpeg12.c 2019-04-22 10:28:41.762275864 -0600
-@@ -114,6 +114,7 @@
- .frame_priv_data_size = sizeof(struct vdpau_picture_context),
- .init = vdpau_mpeg1_init,
- .uninit = ff_vdpau_common_uninit,
-+ .frame_params = ff_vdpau_common_frame_params,
- .priv_data_size = sizeof(VDPAUContext),
- .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
- };
+++ /dev/null
-diff -urN a/libavcodec/h263dec.c b/libavcodec/h263dec.c
---- a/libavcodec/h263dec.c
-+++ b/libavcodec/h263dec.c
-@@ -684,7 +684,7 @@ frame_end:
- if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
- ff_mpeg4_frame_end(avctx, buf, buf_size);
-
-- if (!s->divx_packed && avctx->hwaccel)
-+ if (s->divx_packed && avctx->hwaccel)
- ff_thread_finish_setup(avctx);
-
- av_assert1(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type);