avconv: rework -t handling for encoding.

Current code compares the desired recording time with InputStream.pts,
which has a very unclear meaning. Change the code to use actual
timestamps of the frames passed to the encoder.

In several tests, one less frame is encoded, which is more correct.

In the idroq test one more frame is encoded, which is again more
correct.

Behavior with stream copy should be unchanged.
This commit is contained in:
Anton Khirnov 2012-02-05 14:32:10 +01:00
parent c9af832623
commit 1270e12e49
16 changed files with 82 additions and 50 deletions

View File

@ -204,6 +204,9 @@ typedef struct OutputStream {
// double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
struct InputStream *sync_ist; /* input stream to sync against */
int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
/* pts of the first frame encoded for this stream, used for limiting
* recording time */
int64_t first_pts;
AVBitStreamFilterContext *bitstream_filters;
AVCodec *enc;
int64_t max_frames;
@ -918,6 +921,19 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
}
}
static int check_recording_time(OutputStream *ost)
{
OutputFile *of = &output_files[ost->file_index];
if (of->recording_time != INT64_MAX &&
av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
AV_TIME_BASE_Q) >= 0) {
ost->is_past_recording_time = 1;
return 0;
}
return 1;
}
static void generate_silence(uint8_t* buf, enum AVSampleFormat sample_fmt, size_t size)
{
int fill_char = 0x00;
@ -958,6 +974,11 @@ static int encode_audio_frame(AVFormatContext *s, OutputStream *ost,
av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
exit_program(1);
}
if (!check_recording_time(ost))
return 0;
ost->sync_opts += frame->nb_samples;
}
got_packet = 0;
@ -977,9 +998,6 @@ static int encode_audio_frame(AVFormatContext *s, OutputStream *ost,
audio_size += pkt.size;
}
if (frame)
ost->sync_opts += frame->nb_samples;
return pkt.size;
}
@ -1241,6 +1259,10 @@ static void do_subtitle_out(AVFormatContext *s,
nb = 1;
for (i = 0; i < nb; i++) {
ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
if (!check_recording_time(ost))
return;
sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
// start_display_time is required to be 0
sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
@ -1382,11 +1404,17 @@ static void do_video_out(AVFormatContext *s,
final_picture = in_picture;
#endif
if (!ost->frame_number)
ost->first_pts = ost->sync_opts;
/* duplicates frame if needed */
for (i = 0; i < nb_frames; i++) {
AVPacket pkt;
av_init_packet(&pkt);
if (!check_recording_time(ost))
return;
if (s->oformat->flags & AVFMT_RAWPICTURE &&
enc->codec->id == CODEC_ID_RAWVIDEO) {
/* raw pictures are written as AVPicture structure to
@ -1723,13 +1751,6 @@ static int check_output_constraints(InputStream *ist, OutputStream *ost)
if (of->start_time && ist->pts < of->start_time)
return 0;
if (of->recording_time != INT64_MAX &&
av_compare_ts(ist->pts, AV_TIME_BASE_Q, of->recording_time + of->start_time,
(AVRational){ 1, 1000000 }) >= 0) {
ost->is_past_recording_time = 1;
return 0;
}
return 1;
}
@ -1745,6 +1766,12 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
!ost->copy_initial_nonkeyframes)
return;
if (of->recording_time != INT64_MAX &&
ist->pts >= of->recording_time + of->start_time) {
ost->is_past_recording_time = 1;
return;
}
/* force the input stream PTS */
if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
audio_size += pkt->size;

View File

@ -1 +1 @@
75df9ce475c1b185fc3dbc219596edd3
72e5b060ff0ab8855da22f33a6e04bff

View File

@ -238,4 +238,3 @@
0, 236, 236, 1, 276480, 0x8f316c66
0, 237, 237, 1, 276480, 0x6348ecf5
0, 238, 238, 1, 276480, 0x34b5b78a
0, 239, 239, 1, 276480, 0xcbf66922

View File

@ -1,3 +1,3 @@
c544bb40c2f4c09d44318db5228ee499 *./tests/data/lavf/lavf.asf
98ffddfa94926558ecf20cc6f47236a6 *./tests/data/lavf/lavf.asf
333375 ./tests/data/lavf/lavf.asf
./tests/data/lavf/lavf.asf CRC=0x9f5ab3e6
./tests/data/lavf/lavf.asf CRC=0x51485213

View File

@ -1,3 +1,3 @@
7e5e4db8c04f0acd16cff6b30e60d0e5 *./tests/data/lavf/lavf.avi
331032 ./tests/data/lavf/lavf.avi
./tests/data/lavf/lavf.avi CRC=0x2a83e6b0
e056e1164236b22fafc8325de8221a58 *./tests/data/lavf/lavf.avi
330798 ./tests/data/lavf/lavf.avi
./tests/data/lavf/lavf.avi CRC=0xa79b84dd

View File

@ -1,3 +1,3 @@
4ef091d638bb20b8eaef5b3a0d6f97b7 *./tests/data/lavf/lavf.ffm
c963591a7f9a08d48e0f988640795690 *./tests/data/lavf/lavf.ffm
376832 ./tests/data/lavf/lavf.ffm
./tests/data/lavf/lavf.ffm CRC=0xf361ed74
./tests/data/lavf/lavf.ffm CRC=0x88f58ba1

View File

@ -1,3 +1,3 @@
a36c2d9378b9870880556ced1cb89ecf *./tests/data/lavf/lavf.mkv
320478 ./tests/data/lavf/lavf.mkv
./tests/data/lavf/lavf.mkv CRC=0x2a83e6b0
ed1f083a7d2169c51e5894dabed061df *./tests/data/lavf/lavf.mkv
320262 ./tests/data/lavf/lavf.mkv
./tests/data/lavf/lavf.mkv CRC=0xa79b84dd

View File

@ -1,3 +1,3 @@
dd60652c2193670abffb8c2a123a820e *./tests/data/lavf/lavf.mpg
f0b995c0c10b08133f5138069c3e9786 *./tests/data/lavf/lavf.mpg
372736 ./tests/data/lavf/lavf.mpg
./tests/data/lavf/lavf.mpg CRC=0xf361ed74
./tests/data/lavf/lavf.mpg CRC=0x88f58ba1

View File

@ -1,3 +1,3 @@
16b9d2cf8effb7dae316c6b9248a49b7 *./tests/data/lavf/lavf.nut
319888 ./tests/data/lavf/lavf.nut
./tests/data/lavf/lavf.nut CRC=0x2a83e6b0
7e44a8ed5ff2fe5442f758d48fe1b496 *./tests/data/lavf/lavf.nut
319680 ./tests/data/lavf/lavf.nut
./tests/data/lavf/lavf.nut CRC=0xa79b84dd

View File

@ -1,3 +1,3 @@
b55661ae1a65f99af249d8efc7619a03 *./tests/data/lavf/lavf.ogg
13819 ./tests/data/lavf/lavf.ogg
./tests/data/lavf/lavf.ogg CRC=0xf1ae5536
37147a98d9a484208389efa6a1f8796f *./tests/data/lavf/lavf.ogg
13966 ./tests/data/lavf/lavf.ogg
./tests/data/lavf/lavf.ogg CRC=0x37a143ea

View File

@ -1,2 +1,2 @@
2e3d6b1944c6cd2cf14e13055aecf82a *./tests/data/lavf/lavf.rm
346706 ./tests/data/lavf/lavf.rm
f3ce1f1850655ae43f6184ae436acb70 *./tests/data/lavf/lavf.rm
346414 ./tests/data/lavf/lavf.rm

View File

@ -1,3 +1,3 @@
293142d7286db15e5f4d7d1ca0d9c97c *./tests/data/lavf/lavf.ts
406644 ./tests/data/lavf/lavf.ts
./tests/data/lavf/lavf.ts CRC=0x133216c1
9b59f71822c6dde18fb38b084c150285 *./tests/data/lavf/lavf.ts
406456 ./tests/data/lavf/lavf.ts
./tests/data/lavf/lavf.ts CRC=0x64fab4df

View File

@ -8,7 +8,7 @@ ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 301466 size: 27864
ret:-1 st: 0 flags:1 ts:-0.320000
ret:-1 st: 1 flags:0 ts: 2.586122
ret: 0 st: 1 flags:1 ts: 1.462857
ret: 0 st: 1 flags:1 dts: 1.018776 pts: 1.018776 pos: 329774 size: 209
ret: 0 st: 1 flags:1 dts: 0.992653 pts: 0.992653 pos: 329556 size: 209
ret: 0 st:-1 flags:0 ts: 0.365002
ret: 0 st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 156166 size: 27955
ret:-1 st:-1 flags:1 ts:-0.740831
@ -18,7 +18,7 @@ ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 301466 size: 27864
ret: 0 st: 1 flags:0 ts:-0.052245
ret: 0 st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos: 37784 size: 208
ret: 0 st: 1 flags:1 ts: 2.847347
ret: 0 st: 1 flags:1 dts: 1.018776 pts: 1.018776 pos: 329774 size: 209
ret: 0 st: 1 flags:1 dts: 0.992653 pts: 0.992653 pos: 329556 size: 209
ret:-1 st:-1 flags:0 ts: 1.730004
ret: 0 st:-1 flags:1 ts: 0.624171
ret: 0 st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 156166 size: 27955
@ -38,7 +38,7 @@ ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 301466 size: 27864
ret:-1 st: 0 flags:1 ts:-0.240000
ret:-1 st: 1 flags:0 ts: 2.664490
ret: 0 st: 1 flags:1 ts: 1.567347
ret: 0 st: 1 flags:1 dts: 1.018776 pts: 1.018776 pos: 329774 size: 209
ret: 0 st: 1 flags:1 dts: 0.992653 pts: 0.992653 pos: 329556 size: 209
ret: 0 st:-1 flags:0 ts: 0.460008
ret: 0 st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 156166 size: 27955
ret:-1 st:-1 flags:1 ts:-0.645825

View File

@ -10,7 +10,7 @@ ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 513 size: 27837
ret: 0 st: 1 flags:0 ts: 2.577000
ret:-EOF
ret: 0 st: 1 flags:1 ts: 1.471000
ret: 0 st: 1 flags:1 dts: 1.019000 pts: 1.019000 pos: 320207 size: 209
ret: 0 st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 319991 size: 209
ret: 0 st:-1 flags:0 ts: 0.365002
ret: 0 st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 146703 size: 27925
ret: 0 st:-1 flags:1 ts:-0.740831
@ -22,7 +22,7 @@ ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 291934 size: 27834
ret: 0 st: 1 flags:0 ts:-0.058000
ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 513 size: 27837
ret: 0 st: 1 flags:1 ts: 2.836000
ret: 0 st: 1 flags:1 dts: 1.019000 pts: 1.019000 pos: 320207 size: 209
ret: 0 st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 319991 size: 209
ret: 0 st:-1 flags:0 ts: 1.730004
ret:-EOF
ret: 0 st:-1 flags:1 ts: 0.624171
@ -46,7 +46,7 @@ ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 513 size: 27837
ret: 0 st: 1 flags:0 ts: 2.672000
ret:-EOF
ret: 0 st: 1 flags:1 ts: 1.566000
ret: 0 st: 1 flags:1 dts: 1.019000 pts: 1.019000 pos: 320207 size: 209
ret: 0 st: 1 flags:1 dts: 0.993000 pts: 0.993000 pos: 319991 size: 209
ret: 0 st:-1 flags:0 ts: 0.460008
ret: 0 st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 146703 size: 27925
ret: 0 st:-1 flags:1 ts:-0.645825

View File

@ -8,7 +8,7 @@ ret: 0 st: 1 flags:1 dts: 1.000000 pts: 1.000000 pos: 2048 size: 208
ret: 0 st: 0 flags:1 ts:-0.317500
ret: 0 st: 1 flags:1 dts: 1.000000 pts: 1.000000 pos: 2048 size: 208
ret: 0 st: 1 flags:0 ts: 2.576667
ret: 0 st: 1 flags:1 dts: 2.018778 pts: 2.018778 pos: 370700 size: 235
ret: 0 st: 1 flags:1 dts: 1.783678 pts: 1.783678 pos: 368652 size: 379
ret: 0 st: 1 flags:1 ts: 1.470833
ret: 0 st: 1 flags:1 dts: 1.261222 pts: 1.261222 pos: 145408 size: 261
ret: 0 st:-1 flags:0 ts: 0.365002
@ -22,7 +22,7 @@ ret: 0 st: 0 flags:0 dts: 1.040000 pts: 1.080000 pos: 40960 size: 16073
ret: 0 st: 1 flags:0 ts:-0.058333
ret: 0 st: 1 flags:1 dts: 1.000000 pts: 1.000000 pos: 2048 size: 208
ret: 0 st: 1 flags:1 ts: 2.835833
ret: 0 st: 1 flags:1 dts: 2.018778 pts: 2.018778 pos: 370700 size: 235
ret: 0 st: 1 flags:1 dts: 1.783678 pts: 1.783678 pos: 368652 size: 379
ret: 0 st:-1 flags:0 ts: 1.730004
ret: 0 st: 0 flags:0 dts: 1.760000 pts: 1.800000 pos: 292864 size: 13170
ret: 0 st:-1 flags:1 ts: 0.624171
@ -44,7 +44,7 @@ ret: 0 st: 1 flags:1 dts: 1.000000 pts: 1.000000 pos: 2048 size: 208
ret: 0 st: 0 flags:1 ts:-0.222489
ret: 0 st: 1 flags:1 dts: 1.000000 pts: 1.000000 pos: 2048 size: 208
ret: 0 st: 1 flags:0 ts: 2.671678
ret: 0 st: 1 flags:1 dts: 2.018778 pts: 2.018778 pos: 370700 size: 235
ret: 0 st: 1 flags:1 dts: 1.783678 pts: 1.783678 pos: 368652 size: 379
ret: 0 st: 1 flags:1 ts: 1.565844
ret: 0 st: 1 flags:1 dts: 1.522444 pts: 1.522444 pos: 342028 size: 314
ret: 0 st:-1 flags:0 ts: 0.460008

View File

@ -7,8 +7,10 @@ ret: 0 st: 0 flags:0 ts: 0.788000
ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 314982 size: 31143
ret: 0 st: 0 flags:1 ts:-0.317000
ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 398 size: 31082
ret:-1 st: 1 flags:0 ts: 2.577000
ret:-1 st: 1 flags:1 ts: 1.471000
ret: 0 st: 1 flags:0 ts: 2.577000
ret: 0 st: 1 flags:1 dts: 0.975000 pts: 0.975000 pos: 346128 size: 278
ret: 0 st: 1 flags:1 ts: 1.471000
ret: 0 st: 1 flags:1 dts: 0.975000 pts: 0.975000 pos: 346128 size: 278
ret: 0 st:-1 flags:0 ts: 0.365002
ret: 0 st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 158515 size: 31134
ret: 0 st:-1 flags:1 ts:-0.740831
@ -19,7 +21,8 @@ ret: 0 st: 0 flags:1 ts: 1.048000
ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 314982 size: 31143
ret: 0 st: 1 flags:0 ts:-0.058000
ret: 0 st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos: 31483 size: 278
ret:-1 st: 1 flags:1 ts: 2.836000
ret: 0 st: 1 flags:1 ts: 2.836000
ret: 0 st: 1 flags:1 dts: 0.975000 pts: 0.975000 pos: 346128 size: 278
ret: 0 st:-1 flags:0 ts: 1.730004
ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 314982 size: 31143
ret: 0 st:-1 flags:1 ts: 0.624171
@ -28,7 +31,8 @@ ret: 0 st: 0 flags:0 ts:-0.482000
ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 398 size: 31082
ret: 0 st: 0 flags:1 ts: 2.413000
ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 314982 size: 31143
ret:-1 st: 1 flags:0 ts: 1.307000
ret: 0 st: 1 flags:0 ts: 1.307000
ret: 0 st: 1 flags:1 dts: 0.975000 pts: 0.975000 pos: 346128 size: 278
ret: 0 st: 1 flags:1 ts: 0.201000
ret: 0 st: 1 flags:1 dts: 0.174000 pts: 0.174000 pos: 78969 size: 278
ret: 0 st:-1 flags:0 ts:-0.904994
@ -39,8 +43,10 @@ ret: 0 st: 0 flags:0 ts: 0.883000
ret: 0 st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos: 314982 size: 31143
ret: 0 st: 0 flags:1 ts:-0.222000
ret: 0 st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos: 398 size: 31082
ret:-1 st: 1 flags:0 ts: 2.672000
ret:-1 st: 1 flags:1 ts: 1.566000
ret: 0 st: 1 flags:0 ts: 2.672000
ret: 0 st: 1 flags:1 dts: 0.975000 pts: 0.975000 pos: 346128 size: 278
ret: 0 st: 1 flags:1 ts: 1.566000
ret: 0 st: 1 flags:1 dts: 0.975000 pts: 0.975000 pos: 346128 size: 278
ret: 0 st:-1 flags:0 ts: 0.460008
ret: 0 st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 158515 size: 31134
ret: 0 st:-1 flags:1 ts:-0.645825