Skip to content

Commit 9e4245a

Browse files
committed
PTS fixes
1 parent fbba7b8 commit 9e4245a

3 files changed

Lines changed: 30 additions & 19 deletions

File tree

event.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,7 @@ static void event_new_video(struct context *cnt,
584584

585585
cnt->movie_fps = cnt->lastrate;
586586

587-
MOTION_LOG(NTC, TYPE_EVENTS, NO_ERRNO, "%s FPS %d",
588-
cnt->movie_fps);
587+
MOTION_LOG(NTC, TYPE_EVENTS, NO_ERRNO, "%s Source FPS %d", cnt->movie_fps);
589588

590589
if (cnt->movie_fps < 2) cnt->movie_fps = 2;
591590
}
@@ -785,11 +784,12 @@ static void event_ffmpeg_put(struct context *cnt,
785784
unsigned char *u, *v;
786785

787786
if (cnt->imgs.type == VIDEO_PALETTE_GREY)
788-
u = cnt->ffmpeg_timelapse->udata;
787+
u = cnt->ffmpeg_output->udata;
789788
else
790789
u = y + (width * height);
791790

792791
v = u + (width * height) / 4;
792+
793793
if (ffmpeg_put_other_image(cnt->ffmpeg_output, y, u, v) == -1) {
794794
cnt->finish = 1;
795795
cnt->restart = 0;

ffmpeg.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ struct ffmpeg *ffmpeg_open(const char *ffmpeg_video_codec, char *filename,
290290
*/
291291
ffmpeg = mymalloc(sizeof(struct ffmpeg));
292292

293-
ffmpeg->tlapse = tlapse;
293+
ffmpeg->tlapse = tlapse;
294294

295295
/* Store codec name in ffmpeg->codec, with buffer overflow check. */
296296
snprintf(ffmpeg->codec, sizeof(ffmpeg->codec), "%s", ffmpeg_video_codec);
@@ -358,6 +358,8 @@ struct ffmpeg *ffmpeg_open(const char *ffmpeg_video_codec, char *filename,
358358
if (c->codec_id == MY_CODEC_ID_H264 ||
359359
c->codec_id == MY_CODEC_ID_HEVC){
360360
ffmpeg->vbr = (int)(( (100-vbr) * 51)/100);
361+
} else if (strcmp(ffmpeg_video_codec, "ogg") == 0){
362+
ffmpeg->vbr = (int)(( (100-vbr) * 10)/100);
361363
} else {
362364
ffmpeg->vbr =(int)(((100-vbr)*(100-vbr)*(100-vbr) * 8000) / 1000000) + 1;
363365
}
@@ -380,11 +382,7 @@ struct ffmpeg *ffmpeg_open(const char *ffmpeg_video_codec, char *filename,
380382
}
381383

382384
if (strcmp(ffmpeg_video_codec, "ffv1") == 0) c->strict_std_compliance = -2;
383-
if (!strcmp(ffmpeg->oc->oformat->name, "mp4") ||
384-
!strcmp(ffmpeg->oc->oformat->name, "mov") ||
385-
!strcmp(ffmpeg->oc->oformat->name, "3gp")) {
386-
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
387-
}
385+
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
388386

389387
pthread_mutex_lock(&global_lock);
390388
retcd = avcodec_open2(c, codec, &opts);
@@ -415,7 +413,16 @@ struct ffmpeg *ffmpeg_open(const char *ffmpeg_video_codec, char *filename,
415413

416414
}
417415
av_dict_free(&opts);
418-
MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO, "%s Selected Output FPS %d", c->time_base.den);
416+
MOTION_LOG(NTC, TYPE_ENCODER, NO_ERRNO, "%s Selected Output FPS %d", c->time_base.den);
417+
418+
if ((strcmp(ffmpeg_video_codec, "ffv1") == 0) ||
419+
(strcmp(ffmpeg_video_codec, "ogg") == 0) ){
420+
ffmpeg->video_st->time_base.num = 1;
421+
ffmpeg->video_st->time_base.den = 1000;
422+
} else {
423+
ffmpeg->video_st->time_base.num = 1;
424+
ffmpeg->video_st->time_base.den = rate;
425+
}
419426

420427
ffmpeg->video_outbuf = NULL;
421428
if (!(ffmpeg->oc->oformat->flags & AVFMT_RAWPICTURE)) {
@@ -439,7 +446,6 @@ struct ffmpeg *ffmpeg_open(const char *ffmpeg_video_codec, char *filename,
439446
ffmpeg->picture->linesize[1] = ffmpeg->c->width / 2;
440447
ffmpeg->picture->linesize[2] = ffmpeg->c->width / 2;
441448

442-
443449
/* Open the output file, if needed. */
444450
if ((timelapse_exists(filename) == 0) || (ffmpeg->tlapse != TIMELAPSE_APPEND)) {
445451
if (!(ffmpeg->oc->oformat->flags & AVFMT_NOFILE)) {
@@ -466,6 +472,8 @@ struct ffmpeg *ffmpeg_open(const char *ffmpeg_video_codec, char *filename,
466472
}
467473
}
468474
}
475+
gettimeofday(&ffmpeg->start_time, NULL);
476+
469477
/* Write the stream header, For the TIMELAPSE_APPEND
470478
* we write the data via standard file I/O so we close the
471479
* items here
@@ -481,6 +489,7 @@ struct ffmpeg *ffmpeg_open(const char *ffmpeg_video_codec, char *filename,
481489
av_write_trailer(ffmpeg->oc);
482490
avio_close(ffmpeg->oc->pb);
483491
}
492+
484493
}
485494
return ffmpeg;
486495
}
@@ -630,6 +639,9 @@ int ffmpeg_put_frame(struct ffmpeg *ffmpeg, AVFrame *pic){
630639
int got_packet_ptr;
631640
AVPacket pkt;
632641
char errstr[128];
642+
struct timeval tv1;
643+
644+
gettimeofday(&tv1, NULL);
633645

634646
av_init_packet(&pkt); /* Init static structure. */
635647
if (ffmpeg->oc->oformat->flags & AVFMT_RAWPICTURE) {
@@ -653,14 +665,12 @@ int ffmpeg_put_frame(struct ffmpeg *ffmpeg, AVFrame *pic){
653665
my_packet_unref(pkt);
654666
return -2;
655667
}
656-
if (pkt.pts != AV_NOPTS_VALUE)
657-
pkt.pts = av_rescale_q(pkt.pts,
658-
ffmpeg->video_st->codec->time_base,
659-
ffmpeg->video_st->time_base);
660-
if (pkt.dts != AV_NOPTS_VALUE)
661-
pkt.dts = av_rescale_q(pkt.dts,
662-
ffmpeg->video_st->codec->time_base,
663-
ffmpeg->video_st->time_base);
668+
pkt.pts = ((1000000L * (tv1.tv_sec - ffmpeg->start_time.tv_sec)) + tv1.tv_usec - ffmpeg->start_time.tv_usec)/10;
669+
pkt.pts = av_rescale_q(pkt.pts,(AVRational){1, 100000},ffmpeg->video_st->time_base);
670+
if (pkt.pts < 1) pkt.pts = 1;
671+
if (pkt.pts < pkt.dts) pkt.pts=pkt.dts;
672+
673+
// MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "%s: pts:%d dts:%d stream:%d ",pkt.pts,pkt.dts,ffmpeg->video_st->time_base.den);
664674
}
665675
if (ffmpeg->tlapse == TIMELAPSE_APPEND) {
666676
retcd = timelapse_append(ffmpeg, pkt);

ffmpeg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct ffmpeg {
4646
int vbr; /* variable bitrate setting */
4747
char codec[20]; /* codec name */
4848
int tlapse;
49+
struct timeval start_time;
4950
#else
5051
int dummy;
5152
#endif

0 commit comments

Comments
 (0)