/* dts of the last packet read for this stream */
int64_t dts;
- int64_t next_pts; /* synthetic pts for cases where pkt.pts
- is not defined */
- int64_t pts; /* current pts */
+ int64_t next_pts; ///< synthetic pts for the next decode frame
+ int64_t pts; ///< current pts of the decoded frame
double ts_scale;
int is_start; /* is 1 at the start and after a discontinuity */
int showed_multi_packet_warning;
}
}
-static double get_sync_ipts(const OutputStream *ost)
+static double
+get_sync_ipts(const OutputStream *ost, int64_t pts)
{
- const InputStream *ist = ost->sync_ist;
OutputFile *of = &output_files[ost->file_index];
- return (double)(ist->pts - of->start_time) / AV_TIME_BASE;
+ return (double)(pts - of->start_time) / AV_TIME_BASE;
}
static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
exit_program(1);
}
+
+ ost->sync_opts += frame->nb_samples;
}
got_packet = 0;
av_free_packet(&pkt);
}
- if (frame)
- ost->sync_opts += frame->nb_samples;
-
return ret;
}
av_assert0(ost->audio_resample || dec->sample_fmt==enc->sample_fmt);
if (audio_sync_method) {
- double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts -
+ double delta = get_sync_ipts(ost, ist->pts) * enc->sample_rate - ost->sync_opts -
av_fifo_size(ost->fifo) / (enc->channels * osize);
int idelta = delta * dec->sample_rate / enc->sample_rate;
int byte_delta = idelta * isize * dec->channels;
}
}
} else
- ost->sync_opts = lrintf(get_sync_ipts(ost) * enc->sample_rate) -
+ ost->sync_opts = lrintf(get_sync_ipts(ost, ist->pts) * enc->sample_rate) -
av_fifo_size(ost->fifo) / (enc->channels * osize); // FIXME wrong
if (ost->audio_resample || ost->audio_channels_mapped) {
nb = 1;
for (i = 0; i < nb; i++) {
+ ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
+
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);
}
}
-static int bit_buffer_size = 1024 * 256;
-static uint8_t *bit_buffer = NULL;
-
static void do_video_resample(OutputStream *ost,
InputStream *ist,
AVFrame *in_picture,
duration /= av_q2d(enc->time_base);
}
- sync_ipts = get_sync_ipts(ost) / av_q2d(enc->time_base);
+ sync_ipts = get_sync_ipts(ost, in_picture->pts) / av_q2d(enc->time_base);
/* by default, we output a single frame */
nb_frames = 1;
for (i = 0; i < nb_frames; i++) {
AVPacket pkt;
av_init_packet(&pkt);
+ pkt.data = NULL;
+ pkt.size = 0;
if (s->oformat->flags & AVFMT_RAWPICTURE &&
enc->codec->id == CODEC_ID_RAWVIDEO) {
write_frame(s, &pkt, ost);
} else {
+ int got_packet;
AVFrame big_picture;
big_picture = *final_picture;
big_picture.pict_type = AV_PICTURE_TYPE_I;
ost->forced_kf_index++;
}
- ret = avcodec_encode_video(enc,
- bit_buffer, bit_buffer_size,
- &big_picture);
+ ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);
if (ret < 0) {
av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
exit_program(1);
}
- if (ret > 0) {
- pkt.data = bit_buffer;
- pkt.size = ret;
- if (!(enc->codec->capabilities & CODEC_CAP_DELAY))
- pkt.pts = av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base);
- if (enc->coded_frame->pts != AV_NOPTS_VALUE)
- pkt.pts = av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
+ if (got_packet) {
+ if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & CODEC_CAP_DELAY))
+ pkt.pts = ost->sync_opts;
+
+ if (pkt.pts != AV_NOPTS_VALUE)
+ pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
+ if (pkt.dts != AV_NOPTS_VALUE)
+ pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
- if (enc->coded_frame->key_frame)
- pkt.flags |= AV_PKT_FLAG_KEY;
if (format_video_sync == VSYNC_DROP)
pkt.pts = pkt.dts = AV_NOPTS_VALUE;
+
write_frame(s, &pkt, ost);
- *frame_size = ret;
- video_size += ret;
+ *frame_size = pkt.size;
+ video_size += pkt.size;
/* if two pass, output log */
if (ost->logfile && enc->stats_out) {
for (;;) {
AVPacket pkt;
- int fifo_bytes;
+ int fifo_bytes, got_packet;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
}
break;
case AVMEDIA_TYPE_VIDEO:
- ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
+ ret = avcodec_encode_video2(enc, &pkt, NULL, &got_packet);
if (ret < 0) {
av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
exit_program(1);
}
- video_size += ret;
- if (enc->coded_frame && enc->coded_frame->key_frame)
- pkt.flags |= AV_PKT_FLAG_KEY;
+ video_size += pkt.size;
if (ost->logfile && enc->stats_out) {
fprintf(ost->logfile, "%s", enc->stats_out);
}
- if (ret <= 0) {
+ if (!got_packet) {
stop_encoding = 1;
break;
}
- pkt.data = bit_buffer;
- pkt.size = ret;
- if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
- pkt.pts = av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
+ if (pkt.pts != AV_NOPTS_VALUE)
+ pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
+ if (pkt.dts != AV_NOPTS_VALUE)
+ pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
write_frame(os, &pkt, ost);
break;
default:
opkt.pts = AV_NOPTS_VALUE;
if (pkt->dts == AV_NOPTS_VALUE)
- opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base);
+ opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->st->time_base);
else
opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
opkt.dts -= ost_tb_start_time;
static void rate_emu_sleep(InputStream *ist)
{
if (input_files[ist->file_index].rate_emu) {
- int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE);
+ int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
int64_t now = av_gettime() - ist->start;
if (pts > now)
usleep(pts - now);
/* if the decoder provides a pts, use it instead of the last packet pts.
the decoder could be delaying output by a packet or more. */
if (decoded_frame->pts != AV_NOPTS_VALUE)
- ist->next_pts = decoded_frame->pts;
+ ist->dts = ist->next_dts = ist->pts = ist->next_pts = decoded_frame->pts;
- /* increment next_pts to use for the case where the input stream does not
+ /* increment next_dts to use for the case where the input stream does not
have timestamps or there are multiple frames in the packet */
ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
avctx->sample_rate;
+ ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
+ avctx->sample_rate;
+
// preprocess audio (volume)
if (audio_volume != 256) {
#if CONFIG_AVFILTER
int frame_available = 1;
#endif
- int duration=0;
int64_t *best_effort_timestamp;
AVRational *frame_sample_aspect;
pkt->dts = ist->dts;
*pkt_pts = AV_NOPTS_VALUE;
- if (pkt->duration) {
- duration = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
- } else if(ist->st->codec->time_base.num != 0) {
- int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
- duration = ((int64_t)AV_TIME_BASE *
- ist->st->codec->time_base.num * ticks) /
- ist->st->codec->time_base.den;
- }
-
- if(ist->dts != AV_NOPTS_VALUE && duration) {
- ist->next_dts += duration;
- }else
- ist->next_dts = AV_NOPTS_VALUE;
-
ret = avcodec_decode_video2(ist->st->codec,
decoded_frame, got_output, pkt);
if (ret < 0)
best_effort_timestamp= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "best_effort_timestamp");
if(*best_effort_timestamp != AV_NOPTS_VALUE)
- ist->next_pts = ist->pts = *best_effort_timestamp;
+ ist->next_pts = ist->pts = decoded_frame->pts = *best_effort_timestamp;
- ist->next_pts += duration;
pkt->size = 0;
pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
ist->st->codec->pix_fmt);
avfilter_copy_frame_props(fb, decoded_frame);
- fb->pts = ist->pts;
fb->buf->priv = buf;
fb->buf->free = filter_release_buffer;
*filtered_frame= *decoded_frame; //for me_threshold
if (ost->picref) {
avfilter_fill_frame_from_video_buffer_ref(filtered_frame, ost->picref);
- ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
+ filtered_frame->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
}
}
if (ost->picref->video && !ost->frame_aspect_ratio)
// while we have more to decode or while the decoder did output something on EOF
while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
+ int duration;
handle_eof:
ist->pts = ist->next_pts;
break;
case AVMEDIA_TYPE_VIDEO:
ret = transcode_video (ist, &avpkt, &got_output, &pkt_pts);
+ if (avpkt.duration) {
+ duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);
+ } else if(ist->st->codec->time_base.num != 0) {
+ int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
+ duration = ((int64_t)AV_TIME_BASE *
+ ist->st->codec->time_base.num * ticks) /
+ ist->st->codec->time_base.den;
+ } else
+ duration = 0;
+
+ if(ist->dts != AV_NOPTS_VALUE && duration) {
+ ist->next_dts += duration;
+ }else
+ ist->next_dts = AV_NOPTS_VALUE;
+
+ if (got_output)
+ ist->next_pts += duration; //FIXME the duration is not correct in some cases
break;
case AVMEDIA_TYPE_SUBTITLE:
ret = transcode_subtitles(ist, &avpkt, &got_output);
/* handle stream copy */
if (!ist->decoding_needed) {
rate_emu_sleep(ist);
- ist->pts = ist->next_pts;
+ ist->dts = ist->next_dts;
switch (ist->st->codec->codec_type) {
case AVMEDIA_TYPE_AUDIO:
- ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
+ ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
ist->st->codec->sample_rate;
break;
case AVMEDIA_TYPE_VIDEO:
if (pkt->duration) {
- ist->next_pts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
+ ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
} else if(ist->st->codec->time_base.num != 0) {
int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
- ist->next_pts += ((int64_t)AV_TIME_BASE *
+ ist->next_dts += ((int64_t)AV_TIME_BASE *
ist->st->codec->time_base.num * ticks) /
ist->st->codec->time_base.den;
}
break;
}
+ ist->pts = ist->dts;
+ ist->next_pts = ist->next_dts;
}
for (i = 0; pkt && i < nb_ostreams; i++) {
OutputStream *ost = &ost_table[i];
assert_avoptions(ist->opts);
}
- ist->pts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
+ ist->dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
ist->next_pts = AV_NOPTS_VALUE;
+ ist->next_dts = AV_NOPTS_VALUE;
ist->is_start = 1;
return 0;
}
}
}
- if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
- /* maximum video buffer size is 8-bytes per pixel, plus DPX header size (1664)*/
- int size = codec->width * codec->height;
- bit_buffer_size = FFMAX(bit_buffer_size, 9*size + 10000);
- }
- }
-
- if (!bit_buffer)
- bit_buffer = av_malloc(bit_buffer_size);
- if (!bit_buffer) {
- av_log(NULL, AV_LOG_ERROR, "Cannot allocate %d bytes output buffer\n",
- bit_buffer_size);
- return AVERROR(ENOMEM);
}
/* open each encoder */
if (pkt.dts != AV_NOPTS_VALUE)
pkt.dts *= ist->ts_scale;
- //fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n",
- // ist->next_pts,
- // pkt.dts, input_files[ist->file_index].ts_offset,
+ //fprintf(stderr, "next:%"PRId64" dts:%"PRId64"/%"PRId64" off:%"PRId64" %d\n",
+ // ist->next_dts,
+ // ist->dts, av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q), input_files[ist->file_index].ts_offset,
// ist->st->codec->codec_type);
- if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE
+ if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE
&& (is->iformat->flags & AVFMT_TS_DISCONT)) {
int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
- int64_t delta = pkt_dts - ist->next_pts;
+ int64_t delta = pkt_dts - ist->next_dts;
if((delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
(delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
ret = 0;
fail:
- av_freep(&bit_buffer);
av_freep(&no_packet);
if (output_streams) {