VBR for ffmpeg mpegts, user mount for udf images, widget fixes
authorGood Guy <good1.2guy@gmail.com>
Thu, 2 Feb 2017 21:05:56 +0000 (14:05 -0700)
committerGood Guy <good1.2guy@gmail.com>
Thu, 2 Feb 2017 21:05:56 +0000 (14:05 -0700)
cinelerra-5.1/cinelerra/batchrender.C
cinelerra-5.1/cinelerra/bdcreate.C
cinelerra-5.1/cinelerra/bdcreate.h
cinelerra-5.1/cinelerra/zoombar.C
cinelerra-5.1/cinelerra/zoompanel.C
cinelerra-5.1/cinelerra/zoompanel.h
cinelerra-5.1/ffmpeg/format/bluray
cinelerra-5.1/guicast/bcpopupmenu.C
cinelerra-5.1/thirdparty/Makefile
cinelerra-5.1/thirdparty/src/ffmpeg.patch1
cinelerra-5.1/thirdparty/src/ffmpeg.patch4

index bea451cef030c9d5816beac9a81caac5f4345e59..799796ff94eab561b3ab324cb2b345144be96b79 100644 (file)
@@ -782,7 +782,6 @@ void BatchRenderGUI::create_objects()
        x += new_batch->get_w() + mwindow->theme->widget_border;
        add_subwindow(delete_batch = new BatchRenderDelete(thread, x, y));
        x = x2;  y += delete_batch->get_h() + mwindow->theme->widget_border;
-       y += mwindow->theme->widget_border;
        add_subwindow(savelist_batch = new BatchRenderSaveList(thread, x, y));
        x += savelist_batch->get_w() + mwindow->theme->widget_border;
        add_subwindow(loadlist_batch = new BatchRenderLoadList(thread, x, y));
@@ -868,7 +867,7 @@ int BatchRenderGUI::resize_event(int w, int h)
        x += savelist_batch->get_w() + mwindow->theme->widget_border;
        loadlist_batch->reposition_window(x, y);
        y += loadlist_batch->get_h() + mwindow->theme->widget_border;
-       warning->reposition_window(x, y);
+       warning->reposition_window(x2, y);
 
        y1 = 15 + BC_GenericButton::calculate_h() + mwindow->theme->widget_border;
        y2 = get_h() - y1 - batch_list->get_h();
index b784d0ed218c7689ad5e8fca8282144219e58cfe..02386bd05ca28da2950689d2af8c78b999fca0a5 100644 (file)
@@ -121,6 +121,49 @@ CreateBD_Thread::~CreateBD_Thread()
        close_window();
 }
 
+void CreateBD_Thread::get_udfs_mount(char *udfs, char *mopts, char *mntpt)
+{
+// default: mount -t udf -o loop $1/bd.udfs $1/udfs
+       strcpy(udfs,"$1/bd.udfs");
+       strcpy(mopts,"-t udf -o loop $1/bd.udfs ");
+       strcpy(mntpt,"$1/udfs");
+       const char *home = getenv("HOME");
+       if( !home ) return;
+       FILE *fp = fopen("/etc/fstab","r");
+       if( !fp ) return;
+       int len = strlen(home);
+       char line[BCTEXTLEN], typ[BCTEXTLEN], file[BCTEXTLEN];
+       char mpnt[BCTEXTLEN], opts[BCTEXTLEN];
+       while( fgets(line,sizeof(line),fp) ) {
+// search "/etc/fstab" for: $HOME/img_file $HOME/bluray udf noauto,loop,rw,user
+               if( line[0] == '#' || line[0] == ';' ) continue;
+               if( sscanf(line,"%s %s %s %s ", &file[0], &mpnt[0], &typ[0], &opts[0]) != 4 )
+                       continue;
+               if( strcmp("udf", typ) ) continue;
+               if( strncmp(home, file, len) || file[len] != '/' ) continue;
+               if( strncmp(home, mpnt, len) ) continue;
+               if( strcmp(&mpnt[len], "/bluray") ) continue;
+               int loop = 0, user = 0, rw = 0, noauto = 0;
+               char *op = opts, *ep = op + sizeof(opts)-1;
+               while( op < ep && *op ) {
+                       char opt[BCTEXTLEN], *cp = opt;
+                       while( op < ep && *op && (*cp=*op++)!=',' ) ++cp;
+                       *cp = 0;
+                       if( !strcmp("loop", opt) ) loop = 1;
+                       else if( !strcmp("user", opt) ) user = 1;
+                       else if( !strcmp("noauto", opt) ) noauto = 1;
+                       else if( !strcmp("rw", opt) ) rw = 1;
+               }
+               if( loop && user && rw && noauto ) {
+                       strcpy(udfs, file);
+                       strcpy(mopts, "");
+                       strcpy(mntpt, mpnt);
+                       break;
+               }
+       }
+       fclose(fp);
+}
+
 int CreateBD_Thread::create_bd_jobs(ArrayList<BatchRenderJob*> *jobs, const char *asset_dir)
 {
        EDL *edl = mwindow->edl;
@@ -174,19 +217,22 @@ int CreateBD_Thread::create_bd_jobs(ArrayList<BatchRenderJob*> *jobs, const char
                MainError::show_error(msg);
                return 1;
        }
+       char udfs[BCTEXTLEN], mopts[BCTEXTLEN], mntpt[BCTEXTLEN];
+       get_udfs_mount(udfs, mopts, mntpt);
        const char *exec_path = File::get_cinlib_path();
        fprintf(fp,"#!/bin/bash -ex\n");
        fprintf(fp,"PATH=$PATH:%s\n",exec_path);
        fprintf(fp,"mkdir -p $1/udfs\n");
        fprintf(fp,"sz=`du -sb $1/bd.m2ts | sed -e 's/[ \t].*//'`\n");
        fprintf(fp,"blks=$((sz/2048 + 4096))\n");
-       fprintf(fp,"mkudffs $1/bd.udfs $blks\n");
-       fprintf(fp,"mount -o loop $1/bd.udfs $1/udfs\n");
-       fprintf(fp,"bdwrite $1/udfs $1/bd.m2ts\n");
-       fprintf(fp,"umount $1/udfs\n");
+       fprintf(fp,"rm -f %s\n", udfs);
+       fprintf(fp,"mkudffs %s $blks\n", udfs);
+       fprintf(fp,"mount %s%s\n", mopts, mntpt);
+       fprintf(fp,"bdwrite %s $1/bd.m2ts\n",mntpt);
+       fprintf(fp,"umount %s\n",mntpt);
        fprintf(fp,"echo To burn bluray, load writable media and run:\n");
-       fprintf(fp,"echo for WORM: growisofs -dvd-compat -Z /dev/bd=$1/bd.udfs\n");
-       fprintf(fp,"echo for RW:   dd if=$1/bd.udfs of=/dev/bd bs=2048000\n");
+       fprintf(fp,"echo for WORM: growisofs -dvd-compat -Z /dev/bd=%s\n", udfs);
+       fprintf(fp,"echo for RW:   dd if=%s of=/dev/bd bs=2048000\n",udfs);
        fprintf(fp,"\n");
        fclose(fp);
 
index d04e5a09b42ffd269032b06684699f68a88d24d0..6bcb8e8d36485aced86bd462671458979b43e6f3 100644 (file)
@@ -34,6 +34,7 @@ class CreateBD_Thread : public BC_DialogThread
        static const int BD_MAX_BITRATE, BD_CHANNELS, BD_WIDE_CHANNELS;
        static const double BD_FRAMERATE, BD_SAMPLERATE, BD_KAUDIO_RATE;
        static const int BD_INTERLACE_MODE;
+       static void get_udfs_mount(char *udfs, char *mopts, char *mntpt);
 public:
        CreateBD_Thread(MWindow *mwindow);
        ~CreateBD_Thread();
index 401898c7122645c675cbf1924b3f730f025d4193..8c9c8a4964aaa699aa7bc749582048803093cb31 100644 (file)
@@ -67,25 +67,16 @@ void ZoomBar::create_objects()
 
        draw_top_background(get_parent(), 0, 0, get_w(), get_h());
        sample_zoom = new SampleZoomPanel(mwindow, this, x, y);
-       sample_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
-       sample_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
        sample_zoom->create_objects();
-       sample_zoom->zoom_text->set_tooltip(_("Duration visible in the timeline"));
-       sample_zoom->zoom_tumbler->set_tooltip(_("Duration visible in the timeline"));
+       sample_zoom->set_tooltip(_("Duration visible in the timeline"));
        x += sample_zoom->get_w();
        amp_zoom = new AmpZoomPanel(mwindow, this, x, y);
-       amp_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
-       amp_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
        amp_zoom->create_objects();
-       amp_zoom->zoom_text->set_tooltip(_("Audio waveform scale"));
-       amp_zoom->zoom_tumbler->set_tooltip(_("Audio waveform scale"));
+       amp_zoom->set_tooltip(_("Audio waveform scale"));
        x += amp_zoom->get_w();
        track_zoom = new TrackZoomPanel(mwindow, this, x, y);
-       track_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
-       track_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
        track_zoom->create_objects();
-       track_zoom->zoom_text->set_tooltip(_("Height of tracks in the timeline"));
-       track_zoom->zoom_tumbler->set_tooltip(_("Height of tracks in the timeline"));
+       track_zoom->set_tooltip(_("Height of tracks in the timeline"));
        x += track_zoom->get_w() + 10;
 
        int wid = 120;
@@ -136,23 +127,8 @@ void ZoomBar::update_formatting(BC_TextBox *dst)
 
 void ZoomBar::resize_event()
 {
-       reposition_window(mwindow->theme->mzoom_x,
-               mwindow->theme->mzoom_y,
-               mwindow->theme->mzoom_w,
-               mwindow->theme->mzoom_h);
-
-       draw_top_background(get_parent(), 0, 0, get_w(), get_h());
-       int x = 3, y = 1;
-       if (sample_zoom) delete sample_zoom;
-       sample_zoom = new SampleZoomPanel(mwindow, this, x, y);
-       sample_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
-       sample_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
-       sample_zoom->create_objects();
-//     x += sample_zoom->get_w();
-//     amp_zoom->reposition_window(x, y);
-//     x += amp_zoom->get_w();
-//     track_zoom->reposition_window(x, y);
-       flash();
+       reposition_window(mwindow->theme->mzoom_x, mwindow->theme->mzoom_y,
+               mwindow->theme->mzoom_w, mwindow->theme->mzoom_h);
 }
 
 void ZoomBar::redraw_time_dependancies()
@@ -350,19 +326,9 @@ int ZoomBar::set_selection(int which_one)
 
 
 
-SampleZoomPanel::SampleZoomPanel(MWindow *mwindow,
-       ZoomBar *zoombar,
-       int x,
-       int y)
- : ZoomPanel(mwindow,
-       zoombar,
-       mwindow->edl->local_session->zoom_sample,
-       x,
-       y,
-       110,
-       MIN_ZOOM_TIME,
-       MAX_ZOOM_TIME,
-       ZOOM_TIME)
+SampleZoomPanel::SampleZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
+ : ZoomPanel(mwindow, zoombar, mwindow->edl->local_session->zoom_sample,
+               x, y, 110, MIN_ZOOM_TIME, MAX_ZOOM_TIME, ZOOM_TIME)
 {
        this->mwindow = mwindow;
        this->zoombar = zoombar;
@@ -375,15 +341,8 @@ int SampleZoomPanel::handle_event()
 
 
 AmpZoomPanel::AmpZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
- : ZoomPanel(mwindow,
-       zoombar,
-       mwindow->edl->local_session->zoom_y,
-       x,
-       y,
-       80,
-       MIN_AMP_ZOOM,
-       MAX_AMP_ZOOM,
-       ZOOM_LONG)
+ : ZoomPanel(mwindow, zoombar, mwindow->edl->local_session->zoom_y,
+               x, y, 80, MIN_AMP_ZOOM, MAX_AMP_ZOOM, ZOOM_LONG)
 {
        this->mwindow = mwindow;
        this->zoombar = zoombar;
@@ -395,15 +354,8 @@ int AmpZoomPanel::handle_event()
 }
 
 TrackZoomPanel::TrackZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
- : ZoomPanel(mwindow,
-       zoombar,
-       mwindow->edl->local_session->zoom_track,
-       x,
-       y,
-       70,
-       MIN_TRACK_ZOOM,
-       MAX_TRACK_ZOOM,
-       ZOOM_LONG)
+ : ZoomPanel(mwindow, zoombar, mwindow->edl->local_session->zoom_track,
+               x, y, 70, MIN_TRACK_ZOOM, MAX_TRACK_ZOOM, ZOOM_LONG)
 {
        this->mwindow = mwindow;
        this->zoombar = zoombar;
@@ -419,9 +371,7 @@ int TrackZoomPanel::handle_event()
 
 
 AutoZoom::AutoZoom(MWindow *mwindow, ZoomBar *zoombar, int x, int y, int changemax)
- : BC_Tumbler(x,
-       y,
-       mwindow->theme->get_image_set("zoombar_tumbler"))
+ : BC_Tumbler(x, y, mwindow->theme->get_image_set("zoombar_tumbler"))
 {
        this->mwindow = mwindow;
        this->zoombar = zoombar;
index a719cc00278d58aa498417a44519d4ce43437f32..8b58f1a651b2e091514b59082d84a5530df5edde 100644 (file)
@@ -79,8 +79,6 @@ ZoomPanel::ZoomPanel(MWindow *mwindow,
        this->min = min;
        this->max = max;
        this->zoom_type = zoom_type;
-       this->menu_images = 0;
-       this->tumbler_images = 0;
        this->user_table = 0;
        this->user_size = 0;
 }
@@ -104,8 +102,6 @@ ZoomPanel::ZoomPanel(MWindow *mwindow,
        this->min = min;
        this->max = max;
        this->zoom_type = zoom_type;
-       this->menu_images = 0;
-       this->tumbler_images = 0;
        this->user_table = user_table;
        this->user_size = user_size;
 }
@@ -150,27 +146,11 @@ void ZoomPanel::update_menu()
        calculate_menu();
 }
 
-void ZoomPanel::set_menu_images(VFrame **data)
-{
-       this->menu_images = data;
-}
-
-void ZoomPanel::set_tumbler_images(VFrame **data)
-{
-       this->tumbler_images = data;
-}
-
 void ZoomPanel::create_objects()
 {
-       subwindow->add_subwindow(zoom_text = new ZoomPopup(mwindow,
-               this,
-               x,
-               y));
+       subwindow->add_subwindow(zoom_text = new ZoomPopup(mwindow, this, x, y));
        x += zoom_text->get_w();
-       subwindow->add_subwindow(zoom_tumbler = new ZoomTumbler(mwindow,
-               this,
-               x,
-               y));
+       subwindow->add_subwindow(zoom_tumbler = new ZoomTumbler(mwindow, this, x, y));
        calculate_menu();
 }
 
@@ -202,6 +182,12 @@ void ZoomPanel::set_text(const char *text)
        zoom_text->set_text(text);
 }
 
+void ZoomPanel::set_tooltip(const char *text)
+{
+       zoom_text->set_tooltip(text);
+       zoom_tumbler->set_tooltip(text);
+}
+
 void ZoomPanel::update(double value)
 {
        this->value = value;
@@ -309,12 +295,8 @@ double ZoomPanel::text_to_zoom(char *text, int use_table)
 
 
 ZoomPopup::ZoomPopup(MWindow *mwindow, ZoomPanel *panel, int x, int y)
- : BC_PopupMenu(x,
-               y,
-               panel->w,
-               panel->value_to_text(panel->value, 0),
-               1,
-               panel->menu_images)
+ : BC_PopupMenu(x, y, panel->w, panel->value_to_text(panel->value, 0),
+       1, mwindow->theme->get_image_set("zoombar_menu", 0))
 {
        this->mwindow = mwindow;
        this->panel = panel;
@@ -334,9 +316,7 @@ int ZoomPopup::handle_event()
 
 
 ZoomTumbler::ZoomTumbler(MWindow *mwindow, ZoomPanel *panel, int x, int y)
- : BC_Tumbler(x,
-       y,
-       panel->tumbler_images)
+ : BC_Tumbler(x, y, mwindow->theme->get_image_set("zoombar_tumbler", 0))
 {
        this->mwindow = mwindow;
        this->panel = panel;
index e1fa5c5cc292dcef2e4eba2caed2ab1da7146cd9..98318dcb6a669c8592e7c800aedbe4cf8cb4c73f 100644 (file)
@@ -78,12 +78,10 @@ public:
        void set_text(const char *text);
        char* value_to_text(double value, int use_table = 1);
        double text_to_zoom(char *text, int use_table = 1);
+       void set_tooltip(const char *text);
        void update(double value);
        void update(const char *value);
        void reposition_window(int x, int y);
-// Set images to be used
-       void set_menu_images(VFrame **data);
-       void set_tumbler_images(VFrame **data);
 
        MWindow *mwindow;
        BC_WindowBase *subwindow;
@@ -100,8 +98,6 @@ public:
        int user_size;
        int zoom_type;
        ArrayList<ZoomHash*> zoom_table;
-       VFrame **menu_images;
-       VFrame **tumbler_images;
 };
 
 class ZoomPopup : public BC_PopupMenu
index 10cc4cd1285b41c0734ce234fb37b880aaf98357..abb1589b160dabb14e054860a46c4420653f8cea 100644 (file)
@@ -2,7 +2,7 @@ mpegts
 mpegts_m2ts_mode=2
 mpegts_start_pid=1024
 mpegts_pmt_start_pid=256
+muxrate 1
 sdt_period=-1
 packetsize 2048
-muxrate 10080000
 preload 500000
index 0c8281469610f702cd44029b841630e3fad1960f..71a172e43b62acbc9bf5edfe63a3c9565740b57c 100644 (file)
@@ -419,8 +419,6 @@ int BC_PopupMenu::button_release_event()
 {
 // try the title
        int result = 0;
-       if( pending )
-               return menu_activate();
 
        if(is_event_win() && use_title)
        {
@@ -432,6 +430,9 @@ int BC_PopupMenu::button_release_event()
                }
        }
 
+       if( pending )
+               return menu_activate();
+
        if( !use_title && status == BUTTON_DN ) {
                result = 1;
        }
index aae252b0616990f739c013ebee3281803e7bf98b..09e872cc550db4f92e9a09869529d9c56249008c 100644 (file)
@@ -42,7 +42,7 @@ pkg-config=$(BLD)/$(1).configure
 pkg-built=$(BLD)/$(1).built
 
 ext=$(lastword $(subst ., ,$(1)))
-tarball=$(lastword $(wildcard src/$(1)*.tar.*))
+tarball=$(lastword $(sort $(wildcard src/$(1)*.tar.*)))
 unpack=$(call unpack_$(call ext,$(1)),$(1))
 unpack_gz=tar -xzf $(1)
 unpack_bz2=tar -xjf $(1)
@@ -71,7 +71,7 @@ $(1)-clean:
 
 $(call pkg-source,$(1)):
        $(call unpack,$(call tarball,$(1))) \
-       $(foreach pch,$(wildcard src/$(1).patch*),
+       $(foreach pch,$(sort $(wildcard src/$(1).patch*)),
                patch -d $(1)* -p1 < $(pch))
        touch $$@
 
index 5f9971e1145bb61724b1e5ec64aae9846c75b34c..6f0f0d48718d6b6071274dd34669c234567b1a55 100644 (file)
@@ -1,6 +1,6 @@
 --- a/configure        2015-09-10 13:36:03.763986195 -0600
 +++ b/configure        2015-09-10 13:37:25.190550438 -0600
-@@ -5556,7 +5556,9 @@
+@@ -5735,7 +5735,9 @@
                                 die "ERROR: libx264 must be installed and version must be >= 0.118."; } &&
                               { check_cpp_condition x264.h "X264_MPEG2" &&
                                 enable libx262; }
index cd7c4252e4b52c4462db12e9af16cade75ddb9ae..0793e47afda329dd9f8c124e0555e6dd22498724 100644 (file)
@@ -1,26 +1,74 @@
---- a/libavformat/mpegtsenc.c  2016-09-30 19:12:42.000000000 -0600
-+++ b/libavformat/mpegtsenc.c  2017-01-25 17:25:58.720017593 -0700
-@@ -55,7 +55,7 @@
+--- a/libavformat/mpegtsenc.c  2017-02-02 10:53:52.235702393 -0700
++++ b/libavformat/mpegtsenc.c  2017-02-02 11:53:38.863560974 -0700
+@@ -55,9 +55,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;
-     int pcr_packet_count;
-     int pcr_packet_period;
      AVProgram *program;
-@@ -94,8 +94,10 @@
+ } MpegTSService;
+@@ -77,14 +76,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;
+@@ -94,12 +91,14 @@
      int service_type;
  
      int pmt_start_pid;
 +    int pcr_start_pid;
      int start_pid;
      int m2ts_mode;
-+    int64_t pcr_offset;
++    int64_t ts_offset;
  
      int reemit_pat_pmt; // backward compatibility
  
-@@ -704,6 +706,7 @@
+-    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
+@@ -109,8 +108,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;
+@@ -214,10 +211,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;
+@@ -704,6 +701,7 @@
      service->pmt.pid       = ts->pmt_start_pid + ts->nb_services;
      service->sid           = sid;
      service->pcr_pid       = 0x1fff;
      service->provider_name = av_strdup(provider_name);
      service->name          = av_strdup(name);
      if (!service->provider_name || !service->name)
-@@ -722,7 +725,7 @@
- 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;
-+           ts->first_pcr + ts->pcr_offset;
+@@ -719,18 +717,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)
-@@ -760,6 +763,14 @@
+ {
+     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));
+@@ -751,6 +742,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;
+@@ -759,6 +751,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->filename, "m2ts")) {
 +            ts->m2ts_mode = 1;
 +            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;
-@@ -803,6 +814,8 @@
+@@ -803,6 +804,8 @@
              service->program          = program;
          }
      }
  
      ts->pat.pid          = PAT_PID;
      /* Initialize at 15 so that it wraps and is equal to 0 for the
-@@ -885,10 +898,9 @@
+@@ -885,10 +888,9 @@
          ts_st->cc              = 15;
          /* update PCR pid by using the first video stream */
          if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
          if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
              st->codecpar->extradata_size > 0) {
              AVStream *ast;
-@@ -924,12 +936,24 @@
+@@ -924,78 +926,47 @@
      av_freep(&pids);
  
      /* if no video stream, use the first stream as PCR */
 -        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;
 +        av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", service->pcr_pid);
 +        ret = AVERROR(EINVAL);
 +        goto fail;
-+    }
+     }
  
-     if (ts->mux_rate > 1) {
-         service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
-@@ -989,14 +1013,6 @@
-            service->pcr_packet_period,
-            ts->sdt_packet_period, ts->pat_packet_period);
++    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->filename, "m2ts")) {
 -            ts->m2ts_mode = 1;
 -            ts->m2ts_mode = 0;
 -        }
 -    }
--
      return 0;
  
- fail:
-@@ -1010,9 +1026,9 @@
+@@ -1010,22 +981,12 @@
      MpegTSWrite *ts = s->priv_data;
      int i;
  
 -    if (++ts->sdt_packet_count == ts->sdt_packet_period ||
-+    if ( ts->sdt_period >= 0 && (++ts->sdt_packet_count == ts->sdt_packet_period ||
-         (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
+-        (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)
-+        (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0))
-     ) {
-         ts->sdt_packet_count = 0;
-         if (dts != AV_NOPTS_VALUE)
-@@ -1067,13 +1083,14 @@
+-    ) {
+-        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]);
+@@ -1067,20 +1028,21 @@
  {
      MpegTSWrite *ts = s->priv_data;
      MpegTSWriteStream *ts_st = st->priv_data;
      *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 */
-@@ -1148,7 +1165,7 @@
-     uint8_t buf[TS_PACKET_SIZE];
+     *q++ = 0x10;               /* Adaptation flags: PCR present */
+     /* 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));
+@@ -1149,8 +1111,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;
-+    int afc_len, stuffing_len, write_null;
-     int64_t pcr = -1; /* avoid warning */
-     int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
+     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;
-@@ -1163,8 +1180,8 @@
+     av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
+@@ -1160,28 +1120,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;
+         write_pcr = 0;
 -        if (ts_st->pid == ts_st->service->pcr_pid) {
-+        write_pcr = write_null = 0;
+-            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->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 >=
-@@ -1173,15 +1190,17 @@
++            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);
--            else
++               (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 */
-+               (dts - get_pcr(ts, s->pb) / 300) > delay) {
-+            write_null = 1;
-+        }
-+        /* pcr insert gets priority over null packet insert */
-+        if (write_pcr && ts_st->service->pcr_sid != ts_st->service->pcr_pid) {
-+            mpegts_insert_pcr_only(s, st);
-+            continue;
-+        }
-+        if (write_null) {
-+            mpegts_insert_null_packet(s);
++               mpegts_insert_null_packet(s);
++            /* recalculate write_pcr and possibly retransimit si_info */
              continue;
          }
  
-@@ -1191,13 +1210,17 @@
+@@ -1191,13 +1156,17 @@
          val  = ts_st->pid >> 8;
          if (is_start)
              val |= 0x40;
                  write_pcr = 1;
              set_af_flag(buf, 0x40);
              q = get_ts_payload_start(buf);
-@@ -1310,11 +1333,13 @@
+@@ -1205,14 +1174,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) {
+@@ -1310,11 +1275,13 @@
              *q++ = flags;
              *q++ = header_len;
              if (pts != AV_NOPTS_VALUE) {
 -                write_pts(q, flags >> 6, pts);
-+                int64_t ts_pts = pts + ts->pcr_offset;
++                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->pcr_offset;
++                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) {
-@@ -1838,12 +1863,18 @@
+@@ -1483,7 +1450,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;
+@@ -1504,16 +1470,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) {
+@@ -1671,7 +1636,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);
+@@ -1838,12 +1803,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 },
 -      { .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, pcr_offset), AV_OPT_TYPE_BOOL,
++      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 },
-@@ -1886,7 +1917,7 @@
-       { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-     { "sdt_period", "SDT retransmission time limit in seconds",
+@@ -1878,15 +1849,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",
+-      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 = INT_MAX }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
++      { .dbl = SDT_RETRANS_TIME }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
      { NULL },
  };