static int64_t extra_size = 0;
static int nb_frames_dup = 0;
static int nb_frames_drop = 0;
+static int input_sync;
#define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
}
}
+static uint8_t *video_buffer= NULL; //FIXME rename, its used for audio too at the end
static void do_video_out(AVFormatContext *s,
AVOutputStream *ost,
int nb_frames, i, ret;
AVFrame *final_picture, *formatted_picture;
AVFrame picture_format_temp, picture_crop_temp;
- static uint8_t *video_buffer= NULL;
uint8_t *buf = NULL, *buf1 = NULL;
AVCodecContext *enc, *dec;
enum PixelFormat target_pixfmt;
else if (vdelta > 1.1)
nb_frames = 2;
//fprintf(stderr, "vdelta:%f, ost->sync_opts:%lld, ost->sync_ipts:%f nb_frames:%d\n", vdelta, ost->sync_opts, ost->sync_ipts, nb_frames);
- }
- if (nb_frames == 0){
- ++nb_frames_drop;
- if (verbose>2)
- fprintf(stderr, "*** drop!\n");
- }else if (nb_frames == 2) {
- ++nb_frames_dup;
- if (verbose>2)
- fprintf(stderr, "*** dup!\n");
- }
- ost->sync_opts+= nb_frames;
+ if (nb_frames == 0){
+ ++nb_frames_drop;
+ if (verbose>2)
+ fprintf(stderr, "*** drop!\n");
+ }else if (nb_frames == 2) {
+ ++nb_frames_dup;
+ if (verbose>2)
+ fprintf(stderr, "*** dup!\n");
+ }
+ }else
+ ost->sync_opts= lrintf(ost->sync_ipts * enc->frame_rate / enc->frame_rate_base);
if (nb_frames <= 0)
return;
big_picture.quality = ost->st->quality;
if(!me_threshold)
big_picture.pict_type = 0;
- big_picture.pts = AV_NOPTS_VALUE; //FIXME
+// big_picture.pts = AV_NOPTS_VALUE;
+ big_picture.pts= av_rescale(ost->sync_opts, AV_TIME_BASE*(int64_t)enc->frame_rate_base, enc->frame_rate);
+//av_log(NULL, AV_LOG_DEBUG, "%lld -> encoder\n", ost->sync_opts);
ret = avcodec_encode_video(enc,
video_buffer, VIDEO_BUFFER_SIZE,
&big_picture);
pkt.size= ret;
if(enc->coded_frame)
pkt.pts= enc->coded_frame->pts;
+/*av_log(NULL, AV_LOG_DEBUG, "encoder -> %lld/%lld\n",
+ pkt.pts != AV_NOPTS_VALUE ? av_rescale(pkt.pts, enc->frame_rate, AV_TIME_BASE*(int64_t)enc->frame_rate_base) : -1,
+ pkt.dts != AV_NOPTS_VALUE ? av_rescale(pkt.dts, enc->frame_rate, AV_TIME_BASE*(int64_t)enc->frame_rate_base) : -1);*/
+
if(enc->coded_frame && enc->coded_frame->key_frame)
pkt.flags |= PKT_FLAG_KEY;
av_interleaved_write_frame(s, &pkt);
}
}
}
+ ost->sync_opts++;
ost->frame_number++;
}
the_end:
av_free(buffer_to_free);
}
discard_packet:
+ if (pkt == NULL) {
+ /* EOF handling */
+
+ for(i=0;i<nb_ostreams;i++) {
+ ost = ost_table[i];
+ if (ost->source_index == ist_index) {
+ AVCodecContext *enc= &ost->st->codec;
+ os = output_files[ost->file_index];
+
+ if(ost->st->codec.codec_type == CODEC_TYPE_AUDIO && enc->frame_size <=1)
+ continue;
+ if(ost->st->codec.codec_type == CODEC_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
+ continue;
+
+ if (ost->encoding_needed) {
+ for(;;) {
+ AVPacket pkt;
+ av_init_packet(&pkt);
+ pkt.stream_index= ost->index;
+
+ switch(ost->st->codec.codec_type) {
+ case CODEC_TYPE_AUDIO:
+ ret = avcodec_encode_audio(enc, video_buffer, VIDEO_BUFFER_SIZE, NULL);
+ audio_size += ret;
+ pkt.flags |= PKT_FLAG_KEY;
+ break;
+ case CODEC_TYPE_VIDEO:
+ ret = avcodec_encode_video(enc, video_buffer, VIDEO_BUFFER_SIZE, NULL);
+ video_size += ret;
+ if(enc->coded_frame && enc->coded_frame->key_frame)
+ pkt.flags |= PKT_FLAG_KEY;
+ if (ost->logfile && enc->stats_out) {
+ fprintf(ost->logfile, "%s", enc->stats_out);
+ }
+ break;
+ default:
+ ret=-1;
+ }
+
+ if(ret<=0)
+ break;
+ pkt.data= video_buffer;
+ pkt.size= ret;
+ if(enc->coded_frame)
+ pkt.pts= enc->coded_frame->pts;
+ av_interleaved_write_frame(os, &pkt);
+ }
+ }
+ }
+ }
+ }
+
return 0;
fail_decode:
return -1;
/* select the stream that we must read now by looking at the
smallest output pts */
file_index = -1;
- pts_min = 1e10;
+ pts_min = 1e100;
for(i=0;i<nb_ostreams;i++) {
double pts;
ost = ost_table[i];
os = output_files[ost->file_index];
ist = ist_table[ost->source_index];
- if(ost->st->codec.codec_type == CODEC_TYPE_VIDEO)
- pts = (double)ost->sync_opts * ost->st->codec.frame_rate_base / ost->st->codec.frame_rate;
- else
- pts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
+ if (input_sync == 0) {
+ if(ost->st->codec.codec_type == CODEC_TYPE_VIDEO)
+ pts = (double)ost->sync_opts * ost->st->codec.frame_rate_base / ost->st->codec.frame_rate;
+ else
+ pts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
+ } else {
+ pts = (double)ist->pts;
+ }
if (!file_table[ist->file_index].eof_reached &&
pts < pts_min) {
pts_min = pts;
audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
if (audio_stream_copy) {
st->stream_copy = 1;
+ audio_enc->channels = audio_channels;
} else {
codec_id = file_oformat->audio_codec;
if (audio_codec_id != CODEC_ID_NONE)
audio_enc->codec_id = codec_id;
audio_enc->bit_rate = audio_bit_rate;
- audio_enc->sample_rate = audio_sample_rate;
audio_enc->strict_std_compliance = strict;
audio_enc->thread_count = thread_count;
/* For audio codecs other than AC3 we limit */
} else
audio_enc->channels = audio_channels;
}
+ audio_enc->sample_rate = audio_sample_rate;
}
oc->nb_streams = nb_streams;
}
if (nb_input_files == 0) {
+ input_sync = 1;
prepare_grab();
}