X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffmpeg.c;h=ba34f7c5a9562cc9ed9ddf82d30a7c40b80acca6;hb=f8e98061ecc7301c8e1f5b7b83344fa64b483e5c;hp=6da97d24ae1bbc1b17a773844c0cdfb25043ce19;hpb=a7758884db7eb3041b73d673c1ac3897609556b9;p=ffmpeg diff --git a/ffmpeg.c b/ffmpeg.c index 6da97d24ae1..ba34f7c5a95 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -145,9 +145,8 @@ static int audio_volume = 256; static int exit_on_error = 0; static int using_stdin = 0; -static int verbose = 1; static int run_as_daemon = 0; -static int q_pressed = 0; +static volatile int received_nb_signals = 0; static int64_t video_size = 0; static int64_t audio_size = 0; static int64_t extra_size = 0; @@ -157,11 +156,13 @@ static int input_sync; static float dts_delta_threshold = 10; +static int print_stats = 1; + static uint8_t *audio_buf; static uint8_t *audio_out; static unsigned int allocated_audio_out_size, allocated_audio_buf_size; -static short *samples; +static void *samples; static uint8_t *input_tmp= NULL; #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass" @@ -186,10 +187,10 @@ typedef struct InputStream { typedef struct InputFile { AVFormatContext *ctx; int eof_reached; /* true if eof reached */ - int ist_index; /* index of first stream in ist_table */ + int ist_index; /* index of first stream in input_streams */ int buffer_size; /* current total buffer size */ int64_t ts_offset; - int nb_streams; /* number of stream that avconv is aware of; may be different + int nb_streams; /* number of stream that ffmpeg is aware of; may be different from ctx.nb_streams if new streams appear during av_read_frame() */ int rate_emu; } InputFile; @@ -521,7 +522,7 @@ static volatile int received_sigterm = 0; static void sigterm_handler(int sig) { received_sigterm = sig; - q_pressed++; + received_nb_signals++; term_exit(); } @@ -565,9 +566,6 @@ static int read_key(void) struct timeval tv; fd_set rfds; - if(run_as_daemon) - return -1; - FD_ZERO(&rfds); FD_SET(0, &rfds); tv.tv_sec = 0; @@ -615,8 +613,7 @@ static int read_key(void) static int decode_interrupt_cb(void) { - q_pressed += read_key() == 'q'; - return q_pressed > 1; + return received_nb_signals > 1; } void exit_program(int ret) @@ -659,9 +656,8 @@ void exit_program(int ret) av_freep(&input_tmp); if (received_sigterm) { - fprintf(stderr, - "Received signal %d: terminating.\n", - (int) received_sigterm); + av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n", + (int) received_sigterm); exit (255); } @@ -672,7 +668,7 @@ static void assert_avoptions(AVDictionary *m) { AVDictionaryEntry *t; if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) { - av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key); + av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key); exit_program(1); } } @@ -683,12 +679,12 @@ static void assert_codec_experimental(AVCodecContext *c, int encoder) AVCodec *codec; if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL && c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { - av_log(NULL, AV_LOG_ERROR, "%s '%s' is experimental and might produce bad " + av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad " "results.\nAdd '-strict experimental' if you want to use it.\n", codec_string, c->codec->name); codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id); if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL)) - av_log(NULL, AV_LOG_ERROR, "Or use the non experimental %s '%s'.\n", + av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n", codec_string, codec->name); exit_program(1); } @@ -784,9 +780,9 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx av_free_packet(pkt); new_pkt.destruct= av_destruct_packet; } else if(a<0){ - fprintf(stderr, "%s failed for stream %d, codec %s", - bsfc->filter->name, pkt->stream_index, - avctx->codec ? avctx->codec->name : "copy"); + av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s", + bsfc->filter->name, pkt->stream_index, + avctx->codec ? avctx->codec->name : "copy"); print_error("", a); if (exit_on_error) exit_program(1); @@ -832,14 +828,14 @@ need_realloc: audio_out_size += FF_MIN_BUFFER_SIZE; if(audio_out_size > INT_MAX || audio_buf_size > INT_MAX){ - fprintf(stderr, "Buffer sizes too large\n"); + av_log(NULL, AV_LOG_FATAL, "Buffer sizes too large\n"); exit_program(1); } av_fast_malloc(&audio_buf, &allocated_audio_buf_size, audio_buf_size); av_fast_malloc(&audio_out, &allocated_audio_out_size, audio_out_size); if (!audio_buf || !audio_out){ - fprintf(stderr, "Out of memory in do_audio_out\n"); + av_log(NULL, AV_LOG_FATAL, "Out of memory in do_audio_out\n"); exit_program(1); } @@ -878,12 +874,12 @@ need_realloc: av_set_int(ost->swr, "och", enc->channels); if(audio_sync_method>1) av_set_int(ost->swr, "flags", SWR_FLAG_RESAMPLE); if(ost->swr && swr_init(ost->swr) < 0){ - fprintf(stderr, "swr_init() failed\n"); + av_log(NULL, AV_LOG_FATAL, "swr_init() failed\n"); swr_free(&ost->swr); } if (!ost->swr) { - fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n", + av_log(NULL, AV_LOG_FATAL, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n", dec->channels, dec->sample_rate, enc->channels, enc->sample_rate); exit_program(1); @@ -906,8 +902,7 @@ need_realloc: byte_delta= FFMAX(byte_delta, -size); size += byte_delta; buf -= byte_delta; - if(verbose > 2) - fprintf(stderr, "discarding %d audio samples\n", (int)-delta); + av_log(NULL, AV_LOG_VERBOSE, "discarding %d audio samples\n", (int)-delta); if(!size) return; ist->is_start=0; @@ -924,14 +919,13 @@ need_realloc: memcpy(input_tmp + byte_delta, buf, size); buf= input_tmp; size += byte_delta; - if(verbose > 2) - fprintf(stderr, "adding %d audio samples of silence\n", (int)delta); + av_log(NULL, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", (int)delta); } }else if(audio_sync_method>1){ int comp= av_clip(delta, -audio_sync_method, audio_sync_method); av_assert0(ost->audio_resample); - if(verbose > 2) - fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate); + av_log(NULL, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", + delta, comp, enc->sample_rate); // fprintf(stderr, "drift:%f len:%d opts:%"PRId64" ipts:%"PRId64" fifo:%d\n", delta, -1, ost->sync_opts, (int64_t)(get_sync_ipts(ost) * enc->sample_rate), av_fifo_size(ost->fifo)/(ost->st->codec->channels * 2)); swr_compensate(ost->swr, comp, enc->sample_rate); } @@ -956,7 +950,7 @@ need_realloc: if (enc->frame_size > 1) { /* output resampled raw samples */ if (av_fifo_realloc2(ost->fifo, av_fifo_size(ost->fifo) + size_out) < 0) { - fprintf(stderr, "av_fifo_realloc2() failed\n"); + av_log(NULL, AV_LOG_FATAL, "av_fifo_realloc2() failed\n"); exit_program(1); } av_fifo_generic_write(ost->fifo, buftmp, size_out, NULL); @@ -974,7 +968,7 @@ need_realloc: ret = avcodec_encode_audio(enc, audio_out, audio_out_size, (short *)audio_buf); if (ret < 0) { - fprintf(stderr, "Audio encoding failed\n"); + av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); exit_program(1); } audio_size += ret; @@ -1001,7 +995,7 @@ need_realloc: size_out = size_out*coded_bps/8; if(size_out > audio_out_size){ - fprintf(stderr, "Internal error, buffer size too small\n"); + av_log(NULL, AV_LOG_FATAL, "Internal error, buffer size too small\n"); exit_program(1); } @@ -1009,7 +1003,7 @@ need_realloc: ret = avcodec_encode_audio(enc, audio_out, size_out, (short *)buftmp); if (ret < 0) { - fprintf(stderr, "Audio encoding failed\n"); + av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); exit_program(1); } audio_size += ret; @@ -1048,7 +1042,7 @@ static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void * if(avpicture_deinterlace(picture2, picture, dec->pix_fmt, dec->width, dec->height) < 0) { /* if error, do not deinterlace */ - fprintf(stderr, "Deinterlacing failed\n"); + av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n"); av_free(buf); buf = NULL; picture2 = picture; @@ -1075,7 +1069,7 @@ static void do_subtitle_out(AVFormatContext *s, AVPacket pkt; if (pts == AV_NOPTS_VALUE) { - fprintf(stderr, "Subtitle packets must have a pts\n"); + av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n"); if (exit_on_error) exit_program(1); return; @@ -1104,7 +1098,7 @@ static void do_subtitle_out(AVFormatContext *s, subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out, subtitle_out_max_size, sub); if (subtitle_out_size < 0) { - fprintf(stderr, "Subtitle encoding failed\n"); + av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n"); exit_program(1); } @@ -1166,7 +1160,7 @@ static void do_video_resample(OutputStream *ost, avcodec_get_frame_defaults(&ost->resample_frame); if (avpicture_alloc((AVPicture *)&ost->resample_frame, enc->pix_fmt, enc->width, enc->height)) { - fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n"); + av_log(NULL, AV_LOG_FATAL, "Cannot allocate temp picture, check pix fmt\n"); exit_program(1); } } @@ -1176,7 +1170,7 @@ static void do_video_resample(OutputStream *ost, enc->width, enc->height, enc->pix_fmt, ost->sws_flags, NULL, NULL, NULL); if (ost->img_resample_ctx == NULL) { - fprintf(stderr, "Cannot get resampling context\n"); + av_log(NULL, AV_LOG_FATAL, "Cannot get resampling context\n"); exit_program(1); } } @@ -1235,12 +1229,10 @@ static void do_video_out(AVFormatContext *s, //fprintf(stderr, "vdelta:%f, ost->sync_opts:%"PRId64", ost->sync_ipts:%f nb_frames:%d\n", vdelta, ost->sync_opts, get_sync_ipts(ost), nb_frames); if (nb_frames == 0){ ++nb_frames_drop; - if (verbose>2) - fprintf(stderr, "*** drop!\n"); + av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n"); }else if (nb_frames > 1) { nb_frames_dup += nb_frames - 1; - if (verbose>2) - fprintf(stderr, "*** %d dup!\n", nb_frames-1); + av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames-1); } }else ost->sync_opts= lrintf(sync_ipts); @@ -1301,7 +1293,7 @@ static void do_video_out(AVFormatContext *s, bit_buffer, bit_buffer_size, &big_picture); if (ret < 0) { - fprintf(stderr, "Video encoding failed\n"); + av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); exit_program(1); } @@ -1376,7 +1368,7 @@ static void do_video_stats(AVFormatContext *os, OutputStream *ost, static void print_report(OutputFile *output_files, OutputStream *ost_table, int nb_ostreams, - int is_last_report, int64_t timer_start) + int is_last_report, int64_t timer_start, int64_t cur_time) { char buf[1024]; OutputStream *ost; @@ -1388,11 +1380,12 @@ static void print_report(OutputFile *output_files, int64_t pts = INT64_MAX; static int64_t last_time = -1; static int qp_histogram[52]; + int hours, mins, secs, us; + + if (!print_stats && !is_last_report) + return; if (!is_last_report) { - int64_t cur_time; - /* display the report every 0.5 seconds */ - cur_time = av_gettime(); if (last_time == -1) { last_time = cur_time; return; @@ -1421,7 +1414,7 @@ static void print_report(OutputFile *output_files, snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q); } if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) { - float t = (av_gettime()-timer_start) / 1000000.0; + float t = (cur_time-timer_start) / 1000000.0; frame_number = ost->frame_number; snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ", @@ -1464,43 +1457,39 @@ static void print_report(OutputFile *output_files, ost->st->time_base, AV_TIME_BASE_Q)); } - if (verbose > 0 || is_last_report) { - int hours, mins, secs, us; - secs = pts / AV_TIME_BASE; - us = pts % AV_TIME_BASE; - mins = secs / 60; - secs %= 60; - hours = mins / 60; - mins %= 60; - - bitrate = pts ? total_size * 8 / (pts / 1000.0) : 0; - - snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), - "size=%8.0fkB time=", total_size / 1024.0); - snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), - "%02d:%02d:%02d.%02d ", hours, mins, secs, - (100 * us) / AV_TIME_BASE); - snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), - "bitrate=%6.1fkbits/s", bitrate); - - if (nb_frames_dup || nb_frames_drop) - snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d", - nb_frames_dup, nb_frames_drop); - - if (verbose >= 0) - fprintf(stderr, "%s \r", buf); - - fflush(stderr); - } + secs = pts / AV_TIME_BASE; + us = pts % AV_TIME_BASE; + mins = secs / 60; + secs %= 60; + hours = mins / 60; + mins %= 60; + + bitrate = pts ? total_size * 8 / (pts / 1000.0) : 0; + + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "size=%8.0fkB time=", total_size / 1024.0); + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "%02d:%02d:%02d.%02d ", hours, mins, secs, + (100 * us) / AV_TIME_BASE); + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "bitrate=%6.1fkbits/s", bitrate); + + if (nb_frames_dup || nb_frames_drop) + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d", + nb_frames_dup, nb_frames_drop); + + av_log(NULL, AV_LOG_INFO, "%s \r", buf); - if (is_last_report && verbose >= 0){ + fflush(stderr); + + if (is_last_report) { int64_t raw= audio_size + video_size + extra_size; - fprintf(stderr, "\n"); - fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n", - video_size/1024.0, - audio_size/1024.0, - extra_size/1024.0, - 100.0*(total_size - raw)/raw + av_log(NULL, AV_LOG_INFO, "\n"); + av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n", + video_size/1024.0, + audio_size/1024.0, + extra_size/1024.0, + 100.0*(total_size - raw)/raw ); } } @@ -1564,7 +1553,7 @@ static void flush_encoders(OutputStream *ost_table, int nb_ostreams) ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL); } if (ret < 0) { - fprintf(stderr, "Audio encoding failed\n"); + av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); exit_program(1); } audio_size += ret; @@ -1573,7 +1562,7 @@ static void flush_encoders(OutputStream *ost_table, int nb_ostreams) case AVMEDIA_TYPE_VIDEO: ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL); if (ret < 0) { - fprintf(stderr, "Video encoding failed\n"); + av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); exit_program(1); } video_size += ret; @@ -1605,9 +1594,8 @@ static int output_packet(InputStream *ist, int ist_index, { AVFormatContext *os; OutputStream *ost; - int ret, i; + int ret = 0, i; int got_output; - AVFrame picture; void *buffer_to_free = NULL; static unsigned int samples_size= 0; AVSubtitle subtitle, *subtitle_to_free; @@ -1642,16 +1630,17 @@ static int output_packet(InputStream *ist, int ist_index, while (avpkt.size > 0 || (!pkt && got_output)) { uint8_t *data_buf, *decoded_data_buf; int data_size, decoded_data_size; + AVFrame *decoded_frame, *filtered_frame; handle_eof: ist->pts= ist->next_pts; - if(avpkt.size && avpkt.size != pkt->size && - ((!ist->showed_multi_packet_warning && verbose>0) || verbose>1)){ - fprintf(stderr, "Multiple frames in a packet from stream %d\n", pkt->stream_index); + if(avpkt.size && avpkt.size != pkt->size) + av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING, + "Multiple frames in a packet from stream %d\n", pkt->stream_index); ist->showed_multi_packet_warning=1; - } /* decode the packet if needed */ + decoded_frame = filtered_frame = NULL; decoded_data_buf = NULL; /* fail safe */ decoded_data_size= 0; data_buf = avpkt.data; @@ -1660,8 +1649,8 @@ static int output_packet(InputStream *ist, int ist_index, if (ist->decoding_needed) { switch(ist->st->codec->codec_type) { case AVMEDIA_TYPE_AUDIO:{ - if(pkt && samples_size < FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE)) { - samples_size = FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE); + if(pkt && samples_size < FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE)) { + samples_size = FFMAX(pkt->size * bps, AVCODEC_MAX_AUDIO_FRAME_SIZE); av_free(samples); samples= av_malloc(samples_size); } @@ -1687,23 +1676,23 @@ static int output_packet(InputStream *ist, int ist_index, (ist->st->codec->sample_rate * ist->st->codec->channels); break;} case AVMEDIA_TYPE_VIDEO: - decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2; - /* XXX: allocate picture correctly */ - avcodec_get_frame_defaults(&picture); + if (!(decoded_frame = avcodec_alloc_frame())) + return AVERROR(ENOMEM); avpkt.pts = pkt_pts; avpkt.dts = ist->pts; pkt_pts = AV_NOPTS_VALUE; ret = avcodec_decode_video2(ist->st->codec, - &picture, &got_output, &avpkt); - quality = same_quant ? picture.quality : 0; + decoded_frame, &got_output, &avpkt); + quality = same_quant ? decoded_frame->quality : 0; if (ret < 0) - return ret; + goto fail; if (!got_output) { /* no picture yet */ + av_freep(&decoded_frame); goto discard_packet; } - ist->next_pts = ist->pts = picture.best_effort_timestamp; + ist->next_pts = ist->pts = decoded_frame->best_effort_timestamp; 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 * @@ -1712,7 +1701,7 @@ static int output_packet(InputStream *ist, int ist_index, } avpkt.size = 0; buffer_to_free = NULL; - pre_process_video_frame(ist, (AVPicture *)&picture, &buffer_to_free); + pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free); break; case AVMEDIA_TYPE_SUBTITLE: ret = avcodec_decode_subtitle2(ist->st->codec, @@ -1753,11 +1742,11 @@ static int output_packet(InputStream *ist, int ist_index, if (of->start_time == 0 || ist->pts >= of->start_time) { ost = &ost_table[i]; if (ost->input_video_filter && ost->source_index == ist_index) { - if (!picture.sample_aspect_ratio.num) - picture.sample_aspect_ratio = ist->st->sample_aspect_ratio; - picture.pts = ist->pts; + if (!decoded_frame->sample_aspect_ratio.num) + decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio; + decoded_frame->pts = ist->pts; - av_vsrc_buffer_add_frame(ost->input_video_filter, &picture, AV_VSRC_BUF_FLAG_OVERWRITE); + av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE); } } } @@ -1766,11 +1755,57 @@ static int output_packet(InputStream *ist, int ist_index, // preprocess audio (volume) if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { if (audio_volume != 256) { - short *volp; - volp = samples; - for(i=0;i<(decoded_data_size / sizeof(short));i++) { - int v = ((*volp) * audio_volume + 128) >> 8; - *volp++ = av_clip_int16(v); + switch (ist->st->codec->sample_fmt) { + case AV_SAMPLE_FMT_U8: + { + uint8_t *volp = samples; + for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) { + int v = (((*volp - 128) * audio_volume + 128) >> 8) + 128; + *volp++ = av_clip_uint8(v); + } + break; + } + case AV_SAMPLE_FMT_S16: + { + int16_t *volp = samples; + for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) { + int v = ((*volp) * audio_volume + 128) >> 8; + *volp++ = av_clip_int16(v); + } + break; + } + case AV_SAMPLE_FMT_S32: + { + int32_t *volp = samples; + for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) { + int64_t v = (((int64_t)*volp * audio_volume + 128) >> 8); + *volp++ = av_clipl_int32(v); + } + break; + } + case AV_SAMPLE_FMT_FLT: + { + float *volp = samples; + float scale = audio_volume / 256.f; + for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) { + *volp++ *= scale; + } + break; + } + case AV_SAMPLE_FMT_DBL: + { + double *volp = samples; + double scale = audio_volume / 256.; + for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) { + *volp++ *= scale; + } + break; + } + default: + av_log(NULL, AV_LOG_FATAL, + "Audio volume adjustment on sample format %s is not supported.\n", + av_get_sample_fmt_name(ist->st->codec->sample_fmt)); + exit_program(1); } } } @@ -1810,11 +1845,18 @@ static int output_packet(InputStream *ist, int ist_index, AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base; if (av_buffersink_get_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0) goto cont; + if (!filtered_frame && !(filtered_frame = avcodec_alloc_frame())) { + ret = AVERROR(ENOMEM); + goto fail; + } + *filtered_frame= *decoded_frame; //for me_threshold if (ost->picref) { - avfilter_fill_frame_from_video_buffer_ref(&picture, 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); } } +#else + filtered_frame = decoded_frame; #endif os = output_files[ost->file_index].ctx; @@ -1832,8 +1874,8 @@ static int output_packet(InputStream *ist, int ist_index, if (ost->picref->video && !ost->frame_aspect_ratio) ost->st->codec->sample_aspect_ratio = ost->picref->video->sample_aspect_ratio; #endif - do_video_out(os, ost, ist, &picture, &frame_size, - same_quant ? quality : ost->st->codec->global_quality); + do_video_out(os, ost, ist, filtered_frame, &frame_size, + same_quant ? quality : ost->st->codec->global_quality); if (vstats_filename && frame_size) do_video_stats(os, ost, frame_size); break; @@ -1845,7 +1887,6 @@ static int output_packet(InputStream *ist, int ist_index, abort(); } } else { - AVFrame avframe; //FIXME/XXX remove this AVPicture pict; AVPacket opkt; int64_t ost_tb_start_time= av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base); @@ -1861,10 +1902,6 @@ static int output_packet(InputStream *ist, int ist_index, /* no reencoding needed : output the packet directly */ /* force the input stream PTS */ - avcodec_get_frame_defaults(&avframe); - ost->st->codec->coded_frame= &avframe; - avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY; - if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) audio_size += data_size; else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { @@ -1917,15 +1954,20 @@ static int output_packet(InputStream *ist, int ist_index, ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]); avfilter_unref_buffer(ost->picref); } + av_freep(&filtered_frame); #endif } +fail: av_free(buffer_to_free); /* XXX: allocate the subtitles in the codec ? */ if (subtitle_to_free) { avsubtitle_free(subtitle_to_free); subtitle_to_free = NULL; } + av_freep(&decoded_frame); + if (ret < 0) + return ret; } discard_packet: @@ -2000,7 +2042,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, os = output_files[i].ctx; if (!os->nb_streams && !(os->oformat->flags & AVFMT_NOSTREAMS)) { av_dump_format(os, i, os->filename, 1); - fprintf(stderr, "Output file #%d does not contain any stream\n", i); + av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i); return AVERROR(EINVAL); } } @@ -2065,7 +2107,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, switch(codec->codec_type) { case AVMEDIA_TYPE_AUDIO: if(audio_volume != 256) { - fprintf(stderr,"-acodec copy and -vol are incompatible (frames are not decoded)\n"); + av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n"); exit_program(1); } codec->channel_layout = icodec->channel_layout; @@ -2097,6 +2139,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, codec->height = icodec->height; break; case AVMEDIA_TYPE_DATA: + case AVMEDIA_TYPE_ATTACHMENT: break; default: abort(); @@ -2104,6 +2147,8 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, } else { if (!ost->enc) ost->enc = avcodec_find_encoder(ost->st->codec->codec_id); + ist->decoding_needed = 1; + ost->encoding_needed = 1; switch(codec->codec_type) { case AVMEDIA_TYPE_AUDIO: ost->fifo= av_fifo_alloc(1024); @@ -2128,8 +2173,6 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, ost->audio_resample |= codec->sample_fmt != icodec->sample_fmt || codec->channel_layout != icodec->channel_layout; icodec->request_channels = codec->channels; - ist->decoding_needed = 1; - ost->encoding_needed = 1; ost->resample_sample_fmt = icodec->sample_fmt; ost->resample_sample_rate = icodec->sample_rate; ost->resample_channels = icodec->channels; @@ -2140,7 +2183,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, choose_pixel_fmt(ost->st, ost->enc); if (ost->st->codec->pix_fmt == PIX_FMT_NONE) { - fprintf(stderr, "Video pixel format is unknown, stream cannot be encoded\n"); + av_log(NULL, AV_LOG_FATAL, "Video pixel format is unknown, stream cannot be encoded\n"); exit_program(1); } @@ -2159,8 +2202,6 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, ost->resample_height = icodec->height; ost->resample_width = icodec->width; ost->resample_pix_fmt= icodec->pix_fmt; - ost->encoding_needed = 1; - ist->decoding_needed = 1; if (!ost->frame_rate.num) ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25,1}; @@ -2174,24 +2215,26 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, av_log(os, AV_LOG_WARNING, "Frame rate very high for a muxer not effciciently supporting it.\n" "Please consider specifiying a lower framerate, a different muxer or -vsync 2\n"); } + for (j = 0; j < ost->forced_kf_count; j++) + ost->forced_kf_pts[j] = av_rescale_q(ost->forced_kf_pts[j], + AV_TIME_BASE_Q, + codec->time_base); #if CONFIG_AVFILTER if (configure_video_filters(ist, ost)) { - fprintf(stderr, "Error opening filters!\n"); + av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n"); exit(1); } #endif break; case AVMEDIA_TYPE_SUBTITLE: - ost->encoding_needed = 1; - ist->decoding_needed = 1; break; default: abort(); break; } /* two pass mode */ - if (ost->encoding_needed && codec->codec_id != CODEC_ID_H264 && + if (codec->codec_id != CODEC_ID_H264 && (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) { char logfilename[1024]; FILE *f; @@ -2202,7 +2245,8 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, if (codec->flags & CODEC_FLAG_PASS1) { f = fopen(logfilename, "wb"); if (!f) { - fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno)); + av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n", + logfilename, strerror(errno)); exit_program(1); } ost->logfile = f; @@ -2210,7 +2254,8 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, char *logbuffer; size_t logbuffer_size; if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) { - fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename); + av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n", + logfilename); exit_program(1); } codec->stats_in = logbuffer; @@ -2218,17 +2263,17 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, } } if(codec->codec_type == AVMEDIA_TYPE_VIDEO){ - /* maximum video buffer size is 6-bytes per pixel, plus DPX header size */ + /* maximum video buffer size is 6-bytes per pixel, plus DPX header size (1664)*/ int size= codec->width * codec->height; - bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 1664); + bit_buffer_size= FFMAX(bit_buffer_size, 7*size + 10000); } } if (!bit_buffer) bit_buffer = av_malloc(bit_buffer_size); if (!bit_buffer) { - fprintf(stderr, "Cannot allocate %d bytes output buffer\n", - bit_buffer_size); + av_log(NULL, AV_LOG_ERROR, "Cannot allocate %d bytes output buffer\n", + bit_buffer_size); return AVERROR(ENOMEM); } @@ -2263,7 +2308,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, assert_avoptions(ost->opts); if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000) av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low." - "It takes bits/s as argument, not kbits/s\n"); + " It takes bits/s as argument, not kbits/s\n"); extra_size += ost->st->codec->extradata_size; if (ost->st->codec->me_threshold) @@ -2314,31 +2359,29 @@ static int transcode_init(OutputFile *output_files, int nb_output_files, } /* dump the stream mapping */ - if (verbose >= 0) { - fprintf(stderr, "Stream mapping:\n"); - for (i = 0; i < nb_output_streams;i ++) { - ost = &output_streams[i]; - fprintf(stderr, " Stream #%d.%d -> #%d.%d", - input_streams[ost->source_index].file_index, - input_streams[ost->source_index].st->index, - ost->file_index, - ost->index); - if (ost->sync_ist != &input_streams[ost->source_index]) - fprintf(stderr, " [sync #%d.%d]", - ost->sync_ist->file_index, - ost->sync_ist->st->index); - if (ost->st->stream_copy) - fprintf(stderr, " (copy)"); - else - fprintf(stderr, " (%s -> %s)", input_streams[ost->source_index].dec ? - input_streams[ost->source_index].dec->name : "?", - ost->enc ? ost->enc->name : "?"); - fprintf(stderr, "\n"); - } + av_log(NULL, AV_LOG_INFO, "Stream mapping:\n"); + for (i = 0; i < nb_output_streams; i++) { + ost = &output_streams[i]; + av_log(NULL, AV_LOG_INFO, " Stream #%d.%d -> #%d.%d", + input_streams[ost->source_index].file_index, + input_streams[ost->source_index].st->index, + ost->file_index, + ost->index); + if (ost->sync_ist != &input_streams[ost->source_index]) + av_log(NULL, AV_LOG_INFO, " [sync #%d.%d]", + ost->sync_ist->file_index, + ost->sync_ist->st->index); + if (ost->st->stream_copy) + av_log(NULL, AV_LOG_INFO, " (copy)"); + else + av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index].dec ? + input_streams[ost->source_index].dec->name : "?", + ost->enc ? ost->enc->name : "?"); + av_log(NULL, AV_LOG_INFO, "\n"); } if (ret) { - fprintf(stderr, "%s\n", error); + av_log(NULL, AV_LOG_ERROR, "%s\n", error); return ret; } @@ -2372,11 +2415,9 @@ static int transcode(OutputFile *output_files, int nb_output_files, goto fail; if (!using_stdin) { - if(verbose >= 0) - fprintf(stderr, "Press [q] to stop, [?] for help\n"); + av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n"); avio_set_interrupt_cb(decode_interrupt_cb); } - term_init(); timer_start = av_gettime(); @@ -2385,19 +2426,25 @@ static int transcode(OutputFile *output_files, int nb_output_files, AVPacket pkt; int64_t ipts_min; double opts_min; + int64_t cur_time= av_gettime(); ipts_min= INT64_MAX; opts_min= 1e100; /* if 'q' pressed, exits */ if (!using_stdin) { - if (q_pressed) + static int64_t last_time; + if (received_nb_signals) break; /* read_key() returns 0 on EOF */ - key = read_key(); + if(cur_time - last_time >= 100000 && !run_as_daemon){ + key = read_key(); + last_time = cur_time; + }else + key = -1; if (key == 'q') break; - if (key == '+') verbose++; - if (key == '-') verbose--; + if (key == '+') av_log_set_level(av_log_get_level()+10); + if (key == '-') av_log_set_level(av_log_get_level()-10); if (key == 's') qp_hist ^= 1; if (key == 'h'){ if (do_hex_dump){ @@ -2410,29 +2457,35 @@ static int transcode(OutputFile *output_files, int nb_output_files, } #if CONFIG_AVFILTER if (key == 'c' || key == 'C'){ - char ret[4096], target[64], cmd[256], arg[256]={0}; - double ts; - int k; + char buf[4096], target[64], command[256], arg[256] = {0}; + double time; + int k, n = 0; fprintf(stderr, "\nEnter command: