yading@10: /* yading@10: * Copyright (c) 2000-2003 Fabrice Bellard yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * multimedia converter based on the FFmpeg libraries yading@10: */ yading@10: yading@10: #include "config.h" yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: #if HAVE_ISATTY yading@10: #if HAVE_IO_H yading@10: #include yading@10: #endif yading@10: #if HAVE_UNISTD_H yading@10: #include yading@10: #endif yading@10: #endif yading@10: #include "libavformat/avformat.h" yading@10: #include "libavdevice/avdevice.h" yading@10: #include "libswscale/swscale.h" yading@10: #include "libswresample/swresample.h" yading@10: #include "libavutil/opt.h" yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/parseutils.h" yading@10: #include "libavutil/samplefmt.h" yading@10: #include "libavutil/colorspace.h" yading@10: #include "libavutil/fifo.h" yading@10: #include "libavutil/intreadwrite.h" yading@10: #include "libavutil/dict.h" yading@10: #include "libavutil/mathematics.h" yading@10: #include "libavutil/pixdesc.h" yading@10: #include "libavutil/avstring.h" yading@10: #include "libavutil/libm.h" yading@10: #include "libavutil/imgutils.h" yading@10: #include "libavutil/timestamp.h" yading@10: #include "libavutil/bprint.h" yading@10: #include "libavutil/time.h" yading@10: #include "libavformat/os_support.h" yading@10: yading@10: #include "libavformat/ffm.h" // not public API yading@10: yading@10: # include "libavfilter/avcodec.h" yading@10: # include "libavfilter/avfilter.h" yading@10: # include "libavfilter/buffersrc.h" yading@10: # include "libavfilter/buffersink.h" yading@10: yading@10: #if HAVE_SYS_RESOURCE_H yading@10: #include yading@10: #include yading@10: #include yading@10: #elif HAVE_GETPROCESSTIMES yading@10: #include yading@10: #endif yading@10: #if HAVE_GETPROCESSMEMORYINFO yading@10: #include yading@10: #include yading@10: #endif yading@10: yading@10: #if HAVE_SYS_SELECT_H yading@10: #include yading@10: #endif yading@10: yading@10: #if HAVE_TERMIOS_H yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: #elif HAVE_KBHIT yading@10: #include yading@10: #endif yading@10: yading@10: #if HAVE_PTHREADS yading@10: #include yading@10: #endif yading@10: yading@10: #include yading@10: yading@10: #include "ffmpeg.h" yading@10: #include "cmdutils.h" yading@10: yading@10: #include "libavutil/avassert.h" yading@10: yading@10: const char program_name[] = "ffmpeg"; yading@10: const int program_birth_year = 2000; yading@10: yading@10: static FILE *vstats_file; yading@10: yading@10: const char *const forced_keyframes_const_names[] = { yading@10: "n", yading@10: "n_forced", yading@10: "prev_forced_n", yading@10: "prev_forced_t", yading@10: "t", yading@10: NULL yading@10: }; yading@10: yading@10: static void do_video_stats(OutputStream *ost, int frame_size); yading@10: static int64_t getutime(void); yading@10: static int64_t getmaxrss(void); yading@10: yading@10: static int run_as_daemon = 0; yading@10: static int64_t video_size = 0; yading@10: static int64_t audio_size = 0; yading@10: static int64_t subtitle_size = 0; yading@10: static int64_t extra_size = 0; yading@10: static int nb_frames_dup = 0; yading@10: static int nb_frames_drop = 0; yading@10: static int64_t decode_error_stat[2]; yading@10: yading@10: static int current_time; yading@10: AVIOContext *progress_avio = NULL; yading@10: yading@10: static uint8_t *subtitle_out; yading@10: yading@10: #if HAVE_PTHREADS yading@10: /* signal to input threads that they should exit; set by the main thread */ yading@10: static int transcoding_finished; yading@10: #endif yading@10: yading@10: #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass" yading@10: yading@10: InputStream **input_streams = NULL; yading@10: int nb_input_streams = 0; yading@10: InputFile **input_files = NULL; yading@10: int nb_input_files = 0; yading@10: yading@10: OutputStream **output_streams = NULL; yading@10: int nb_output_streams = 0; yading@10: OutputFile **output_files = NULL; yading@10: int nb_output_files = 0; yading@10: yading@10: FilterGraph **filtergraphs; yading@10: int nb_filtergraphs; yading@10: yading@10: #if HAVE_TERMIOS_H yading@10: yading@10: /* init terminal so that we can grab keys */ yading@10: static struct termios oldtty; yading@10: static int restore_tty; yading@10: #endif yading@10: yading@10: yading@10: /* sub2video hack: yading@10: Convert subtitles to video with alpha to insert them in filter graphs. yading@10: This is a temporary solution until libavfilter gets real subtitles support. yading@10: */ yading@10: yading@10: static int sub2video_get_blank_frame(InputStream *ist) yading@10: { yading@10: int ret; yading@10: AVFrame *frame = ist->sub2video.frame; yading@10: yading@10: av_frame_unref(frame); yading@10: ist->sub2video.frame->width = ist->sub2video.w; yading@10: ist->sub2video.frame->height = ist->sub2video.h; yading@10: ist->sub2video.frame->format = AV_PIX_FMT_RGB32; yading@10: if ((ret = av_frame_get_buffer(frame, 32)) < 0) yading@10: return ret; yading@10: memset(frame->data[0], 0, frame->height * frame->linesize[0]); yading@10: return 0; yading@10: } yading@10: yading@10: static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h, yading@10: AVSubtitleRect *r) yading@10: { yading@10: uint32_t *pal, *dst2; yading@10: uint8_t *src, *src2; yading@10: int x, y; yading@10: yading@10: if (r->type != SUBTITLE_BITMAP) { yading@10: av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n"); yading@10: return; yading@10: } yading@10: if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) { yading@10: av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle overflowing\n"); yading@10: return; yading@10: } yading@10: yading@10: dst += r->y * dst_linesize + r->x * 4; yading@10: src = r->pict.data[0]; yading@10: pal = (uint32_t *)r->pict.data[1]; yading@10: for (y = 0; y < r->h; y++) { yading@10: dst2 = (uint32_t *)dst; yading@10: src2 = src; yading@10: for (x = 0; x < r->w; x++) yading@10: *(dst2++) = pal[*(src2++)]; yading@10: dst += dst_linesize; yading@10: src += r->pict.linesize[0]; yading@10: } yading@10: } yading@10: yading@10: static void sub2video_push_ref(InputStream *ist, int64_t pts) yading@10: { yading@10: AVFrame *frame = ist->sub2video.frame; yading@10: int i; yading@10: yading@10: av_assert1(frame->data[0]); yading@10: ist->sub2video.last_pts = frame->pts = pts; yading@10: for (i = 0; i < ist->nb_filters; i++) yading@10: av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame, yading@10: AV_BUFFERSRC_FLAG_KEEP_REF | yading@10: AV_BUFFERSRC_FLAG_PUSH); yading@10: } yading@10: yading@10: static void sub2video_update(InputStream *ist, AVSubtitle *sub) yading@10: { yading@10: int w = ist->sub2video.w, h = ist->sub2video.h; yading@10: AVFrame *frame = ist->sub2video.frame; yading@10: int8_t *dst; yading@10: int dst_linesize; yading@10: int num_rects, i; yading@10: int64_t pts, end_pts; yading@10: yading@10: if (!frame) yading@10: return; yading@10: if (sub) { yading@10: pts = av_rescale_q(sub->pts + sub->start_display_time * 1000, yading@10: AV_TIME_BASE_Q, ist->st->time_base); yading@10: end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000, yading@10: AV_TIME_BASE_Q, ist->st->time_base); yading@10: num_rects = sub->num_rects; yading@10: } else { yading@10: pts = ist->sub2video.end_pts; yading@10: end_pts = INT64_MAX; yading@10: num_rects = 0; yading@10: } yading@10: if (sub2video_get_blank_frame(ist) < 0) { yading@10: av_log(ist->st->codec, AV_LOG_ERROR, yading@10: "Impossible to get a blank canvas.\n"); yading@10: return; yading@10: } yading@10: dst = frame->data [0]; yading@10: dst_linesize = frame->linesize[0]; yading@10: for (i = 0; i < num_rects; i++) yading@10: sub2video_copy_rect(dst, dst_linesize, w, h, sub->rects[i]); yading@10: sub2video_push_ref(ist, pts); yading@10: ist->sub2video.end_pts = end_pts; yading@10: } yading@10: yading@10: static void sub2video_heartbeat(InputStream *ist, int64_t pts) yading@10: { yading@10: InputFile *infile = input_files[ist->file_index]; yading@10: int i, j, nb_reqs; yading@10: int64_t pts2; yading@10: yading@10: /* When a frame is read from a file, examine all sub2video streams in yading@10: the same file and send the sub2video frame again. Otherwise, decoded yading@10: video frames could be accumulating in the filter graph while a filter yading@10: (possibly overlay) is desperately waiting for a subtitle frame. */ yading@10: for (i = 0; i < infile->nb_streams; i++) { yading@10: InputStream *ist2 = input_streams[infile->ist_index + i]; yading@10: if (!ist2->sub2video.frame) yading@10: continue; yading@10: /* subtitles seem to be usually muxed ahead of other streams; yading@10: if not, substracting a larger time here is necessary */ yading@10: pts2 = av_rescale_q(pts, ist->st->time_base, ist2->st->time_base) - 1; yading@10: /* do not send the heartbeat frame if the subtitle is already ahead */ yading@10: if (pts2 <= ist2->sub2video.last_pts) yading@10: continue; yading@10: if (pts2 >= ist2->sub2video.end_pts || !ist2->sub2video.frame->data[0]) yading@10: sub2video_update(ist2, NULL); yading@10: for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++) yading@10: nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter); yading@10: if (nb_reqs) yading@10: sub2video_push_ref(ist2, pts2); yading@10: } yading@10: } yading@10: yading@10: static void sub2video_flush(InputStream *ist) yading@10: { yading@10: int i; yading@10: yading@10: for (i = 0; i < ist->nb_filters; i++) yading@10: av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0); yading@10: } yading@10: yading@10: /* end of sub2video hack */ yading@10: yading@10: void term_exit(void) yading@10: { yading@10: av_log(NULL, AV_LOG_QUIET, "%s", ""); yading@10: #if HAVE_TERMIOS_H yading@10: if(restore_tty) yading@10: tcsetattr (0, TCSANOW, &oldtty); yading@10: #endif yading@10: } yading@10: yading@10: static volatile int received_sigterm = 0; yading@10: static volatile int received_nb_signals = 0; yading@10: yading@10: static void yading@10: sigterm_handler(int sig) yading@10: { yading@10: received_sigterm = sig; yading@10: received_nb_signals++; yading@10: term_exit(); yading@10: if(received_nb_signals > 3) yading@10: exit(123); yading@10: } yading@10: yading@10: void term_init(void) yading@10: { yading@10: #if HAVE_TERMIOS_H yading@10: if(!run_as_daemon){ yading@10: struct termios tty; yading@10: int istty = 1; yading@10: #if HAVE_ISATTY yading@10: istty = isatty(0) && isatty(2); yading@10: #endif yading@10: if (istty && tcgetattr (0, &tty) == 0) { yading@10: oldtty = tty; yading@10: restore_tty = 1; yading@10: atexit(term_exit); yading@10: yading@10: tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP yading@10: |INLCR|IGNCR|ICRNL|IXON); yading@10: tty.c_oflag |= OPOST; yading@10: tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); yading@10: tty.c_cflag &= ~(CSIZE|PARENB); yading@10: tty.c_cflag |= CS8; yading@10: tty.c_cc[VMIN] = 1; yading@10: tty.c_cc[VTIME] = 0; yading@10: yading@10: tcsetattr (0, TCSANOW, &tty); yading@10: } yading@10: signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */ yading@10: } yading@10: #endif yading@10: avformat_network_deinit(); yading@10: yading@10: signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */ yading@10: signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */ yading@10: #ifdef SIGXCPU yading@10: signal(SIGXCPU, sigterm_handler); yading@10: #endif yading@10: } yading@10: yading@10: /* read a key without blocking */ yading@10: static int read_key(void) yading@10: { yading@10: unsigned char ch; yading@10: #if HAVE_TERMIOS_H yading@10: int n = 1; yading@10: struct timeval tv; yading@10: fd_set rfds; yading@10: yading@10: FD_ZERO(&rfds); yading@10: FD_SET(0, &rfds); yading@10: tv.tv_sec = 0; yading@10: tv.tv_usec = 0; yading@10: n = select(1, &rfds, NULL, NULL, &tv); yading@10: if (n > 0) { yading@10: n = read(0, &ch, 1); yading@10: if (n == 1) yading@10: return ch; yading@10: yading@10: return n; yading@10: } yading@10: #elif HAVE_KBHIT yading@10: # if HAVE_PEEKNAMEDPIPE yading@10: static int is_pipe; yading@10: static HANDLE input_handle; yading@10: DWORD dw, nchars; yading@10: if(!input_handle){ yading@10: input_handle = GetStdHandle(STD_INPUT_HANDLE); yading@10: is_pipe = !GetConsoleMode(input_handle, &dw); yading@10: } yading@10: yading@10: if (stdin->_cnt > 0) { yading@10: read(0, &ch, 1); yading@10: return ch; yading@10: } yading@10: if (is_pipe) { yading@10: /* When running under a GUI, you will end here. */ yading@10: if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) { yading@10: // input pipe may have been closed by the program that ran ffmpeg yading@10: return -1; yading@10: } yading@10: //Read it yading@10: if(nchars != 0) { yading@10: read(0, &ch, 1); yading@10: return ch; yading@10: }else{ yading@10: return -1; yading@10: } yading@10: } yading@10: # endif yading@10: if(kbhit()) yading@10: return(getch()); yading@10: #endif yading@10: return -1; yading@10: } yading@10: yading@10: static int decode_interrupt_cb(void *ctx) yading@10: { yading@10: return received_nb_signals > 1; yading@10: } yading@10: yading@10: const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL }; yading@10: yading@10: static void exit_program(void) yading@10: { yading@10: int i, j; yading@10: yading@10: if (do_benchmark) { yading@10: int maxrss = getmaxrss() / 1024; yading@10: printf("bench: maxrss=%ikB\n", maxrss); yading@10: } yading@10: yading@10: for (i = 0; i < nb_filtergraphs; i++) { yading@10: avfilter_graph_free(&filtergraphs[i]->graph); yading@10: for (j = 0; j < filtergraphs[i]->nb_inputs; j++) { yading@10: av_freep(&filtergraphs[i]->inputs[j]->name); yading@10: av_freep(&filtergraphs[i]->inputs[j]); yading@10: } yading@10: av_freep(&filtergraphs[i]->inputs); yading@10: for (j = 0; j < filtergraphs[i]->nb_outputs; j++) { yading@10: av_freep(&filtergraphs[i]->outputs[j]->name); yading@10: av_freep(&filtergraphs[i]->outputs[j]); yading@10: } yading@10: av_freep(&filtergraphs[i]->outputs); yading@10: av_freep(&filtergraphs[i]->graph_desc); yading@10: av_freep(&filtergraphs[i]); yading@10: } yading@10: av_freep(&filtergraphs); yading@10: yading@10: av_freep(&subtitle_out); yading@10: yading@10: /* close files */ yading@10: for (i = 0; i < nb_output_files; i++) { yading@10: AVFormatContext *s = output_files[i]->ctx; yading@10: if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb) yading@10: avio_close(s->pb); yading@10: avformat_free_context(s); yading@10: av_dict_free(&output_files[i]->opts); yading@10: av_freep(&output_files[i]); yading@10: } yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters; yading@10: while (bsfc) { yading@10: AVBitStreamFilterContext *next = bsfc->next; yading@10: av_bitstream_filter_close(bsfc); yading@10: bsfc = next; yading@10: } yading@10: output_streams[i]->bitstream_filters = NULL; yading@10: avcodec_free_frame(&output_streams[i]->filtered_frame); yading@10: yading@10: av_freep(&output_streams[i]->forced_keyframes); yading@10: av_expr_free(output_streams[i]->forced_keyframes_pexpr); yading@10: av_freep(&output_streams[i]->avfilter); yading@10: av_freep(&output_streams[i]->logfile_prefix); yading@10: av_freep(&output_streams[i]); yading@10: } yading@10: for (i = 0; i < nb_input_files; i++) { yading@10: avformat_close_input(&input_files[i]->ctx); yading@10: av_freep(&input_files[i]); yading@10: } yading@10: for (i = 0; i < nb_input_streams; i++) { yading@10: av_frame_free(&input_streams[i]->decoded_frame); yading@10: av_frame_free(&input_streams[i]->filter_frame); yading@10: av_dict_free(&input_streams[i]->opts); yading@10: avsubtitle_free(&input_streams[i]->prev_sub.subtitle); yading@10: av_frame_free(&input_streams[i]->sub2video.frame); yading@10: av_freep(&input_streams[i]->filters); yading@10: av_freep(&input_streams[i]); yading@10: } yading@10: yading@10: if (vstats_file) yading@10: fclose(vstats_file); yading@10: av_free(vstats_filename); yading@10: yading@10: av_freep(&input_streams); yading@10: av_freep(&input_files); yading@10: av_freep(&output_streams); yading@10: av_freep(&output_files); yading@10: yading@10: uninit_opts(); yading@10: yading@10: avformat_network_deinit(); yading@10: yading@10: if (received_sigterm) { yading@10: av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n", yading@10: (int) received_sigterm); yading@10: } yading@10: } yading@10: yading@10: void assert_avoptions(AVDictionary *m) yading@10: { yading@10: AVDictionaryEntry *t; yading@10: if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) { yading@10: av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key); yading@10: exit(1); yading@10: } yading@10: } yading@10: yading@10: static void abort_codec_experimental(AVCodec *c, int encoder) yading@10: { yading@10: exit(1); yading@10: } yading@10: yading@10: static void update_benchmark(const char *fmt, ...) yading@10: { yading@10: if (do_benchmark_all) { yading@10: int64_t t = getutime(); yading@10: va_list va; yading@10: char buf[1024]; yading@10: yading@10: if (fmt) { yading@10: va_start(va, fmt); yading@10: vsnprintf(buf, sizeof(buf), fmt, va); yading@10: va_end(va); yading@10: printf("bench: %8"PRIu64" %s \n", t - current_time, buf); yading@10: } yading@10: current_time = t; yading@10: } yading@10: } yading@10: yading@10: static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) yading@10: { yading@10: AVBitStreamFilterContext *bsfc = ost->bitstream_filters; yading@10: AVCodecContext *avctx = ost->st->codec; yading@10: int ret; yading@10: yading@10: if ((avctx->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || yading@10: (avctx->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) yading@10: pkt->pts = pkt->dts = AV_NOPTS_VALUE; yading@10: yading@10: if ((avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_VIDEO) && pkt->dts != AV_NOPTS_VALUE) { yading@10: int64_t max = ost->st->cur_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); yading@10: if (ost->st->cur_dts && ost->st->cur_dts != AV_NOPTS_VALUE && max > pkt->dts) { yading@10: av_log(s, max - pkt->dts > 2 || avctx->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG, yading@10: "st:%d PTS: %"PRId64" DTS: %"PRId64" < %"PRId64" invalid, clipping\n", pkt->stream_index, pkt->pts, pkt->dts, max); yading@10: if(pkt->pts >= pkt->dts) yading@10: pkt->pts = FFMAX(pkt->pts, max); yading@10: pkt->dts = max; yading@10: } yading@10: } yading@10: yading@10: /* yading@10: * Audio encoders may split the packets -- #frames in != #packets out. yading@10: * But there is no reordering, so we can limit the number of output packets yading@10: * by simply dropping them here. yading@10: * Counting encoded video frames needs to be done separately because of yading@10: * reordering, see do_video_out() yading@10: */ yading@10: if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) { yading@10: if (ost->frame_number >= ost->max_frames) { yading@10: av_free_packet(pkt); yading@10: return; yading@10: } yading@10: ost->frame_number++; yading@10: } yading@10: yading@10: while (bsfc) { yading@10: AVPacket new_pkt = *pkt; yading@10: int a = av_bitstream_filter_filter(bsfc, avctx, NULL, yading@10: &new_pkt.data, &new_pkt.size, yading@10: pkt->data, pkt->size, yading@10: pkt->flags & AV_PKT_FLAG_KEY); yading@10: if(a == 0 && new_pkt.data != pkt->data && new_pkt.destruct) { yading@10: uint8_t *t = av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE); //the new should be a subset of the old so cannot overflow yading@10: if(t) { yading@10: memcpy(t, new_pkt.data, new_pkt.size); yading@10: memset(t + new_pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE); yading@10: new_pkt.data = t; yading@10: new_pkt.buf = NULL; yading@10: a = 1; yading@10: } else yading@10: a = AVERROR(ENOMEM); yading@10: } yading@10: if (a > 0) { yading@10: av_free_packet(pkt); yading@10: new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, yading@10: av_buffer_default_free, NULL, 0); yading@10: if (!new_pkt.buf) yading@10: exit(1); yading@10: } else if (a < 0) { yading@10: av_log(NULL, AV_LOG_ERROR, "Failed to open bitstream filter %s for stream %d with codec %s", yading@10: bsfc->filter->name, pkt->stream_index, yading@10: avctx->codec ? avctx->codec->name : "copy"); yading@10: print_error("", a); yading@10: if (exit_on_error) yading@10: exit(1); yading@10: } yading@10: *pkt = new_pkt; yading@10: yading@10: bsfc = bsfc->next; yading@10: } yading@10: yading@10: pkt->stream_index = ost->index; yading@10: yading@10: if (debug_ts) { yading@10: av_log(NULL, AV_LOG_INFO, "muxer <- type:%s " yading@10: "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n", yading@10: av_get_media_type_string(ost->st->codec->codec_type), yading@10: av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base), yading@10: av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base), yading@10: pkt->size yading@10: ); yading@10: } yading@10: yading@10: ret = av_interleaved_write_frame(s, pkt); yading@10: if (ret < 0) { yading@10: print_error("av_interleaved_write_frame()", ret); yading@10: exit(1); yading@10: } yading@10: } yading@10: yading@10: static void close_output_stream(OutputStream *ost) yading@10: { yading@10: OutputFile *of = output_files[ost->file_index]; yading@10: yading@10: ost->finished = 1; yading@10: if (of->shortest) { yading@10: int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, AV_TIME_BASE_Q); yading@10: of->recording_time = FFMIN(of->recording_time, end); yading@10: } yading@10: } yading@10: yading@10: static int check_recording_time(OutputStream *ost) yading@10: { yading@10: OutputFile *of = output_files[ost->file_index]; yading@10: yading@10: if (of->recording_time != INT64_MAX && yading@10: av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time, yading@10: AV_TIME_BASE_Q) >= 0) { yading@10: close_output_stream(ost); yading@10: return 0; yading@10: } yading@10: return 1; yading@10: } yading@10: yading@10: static void do_audio_out(AVFormatContext *s, OutputStream *ost, yading@10: AVFrame *frame) yading@10: { yading@10: AVCodecContext *enc = ost->st->codec; yading@10: AVPacket pkt; yading@10: int got_packet = 0; yading@10: yading@10: av_init_packet(&pkt); yading@10: pkt.data = NULL; yading@10: pkt.size = 0; yading@10: yading@10: if (!check_recording_time(ost)) yading@10: return; yading@10: yading@10: if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0) yading@10: frame->pts = ost->sync_opts; yading@10: ost->sync_opts = frame->pts + frame->nb_samples; yading@10: yading@10: av_assert0(pkt.size || !pkt.data); yading@10: update_benchmark(NULL); yading@10: if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, "Audio encoding failed (avcodec_encode_audio2)\n"); yading@10: exit(1); yading@10: } yading@10: update_benchmark("encode_audio %d.%d", ost->file_index, ost->index); yading@10: yading@10: if (got_packet) { yading@10: if (pkt.pts != AV_NOPTS_VALUE) yading@10: pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base); yading@10: if (pkt.dts != AV_NOPTS_VALUE) yading@10: pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base); yading@10: if (pkt.duration > 0) yading@10: pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base); yading@10: yading@10: if (debug_ts) { yading@10: av_log(NULL, AV_LOG_INFO, "encoder -> type:audio " yading@10: "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n", yading@10: av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base), yading@10: av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base)); yading@10: } yading@10: yading@10: audio_size += pkt.size; yading@10: write_frame(s, &pkt, ost); yading@10: yading@10: av_free_packet(&pkt); yading@10: } yading@10: } yading@10: yading@10: static void do_subtitle_out(AVFormatContext *s, yading@10: OutputStream *ost, yading@10: InputStream *ist, yading@10: AVSubtitle *sub) yading@10: { yading@10: int subtitle_out_max_size = 1024 * 1024; yading@10: int subtitle_out_size, nb, i; yading@10: AVCodecContext *enc; yading@10: AVPacket pkt; yading@10: int64_t pts; yading@10: yading@10: if (sub->pts == AV_NOPTS_VALUE) { yading@10: av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n"); yading@10: if (exit_on_error) yading@10: exit(1); yading@10: return; yading@10: } yading@10: yading@10: enc = ost->st->codec; yading@10: yading@10: if (!subtitle_out) { yading@10: subtitle_out = av_malloc(subtitle_out_max_size); yading@10: } yading@10: yading@10: /* Note: DVB subtitle need one packet to draw them and one other yading@10: packet to clear them */ yading@10: /* XXX: signal it in the codec context ? */ yading@10: if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) yading@10: nb = 2; yading@10: else yading@10: nb = 1; yading@10: yading@10: /* shift timestamp to honor -ss and make check_recording_time() work with -t */ yading@10: pts = sub->pts - output_files[ost->file_index]->start_time; yading@10: for (i = 0; i < nb; i++) { yading@10: ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base); yading@10: if (!check_recording_time(ost)) yading@10: return; yading@10: yading@10: sub->pts = pts; yading@10: // start_display_time is required to be 0 yading@10: sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q); yading@10: sub->end_display_time -= sub->start_display_time; yading@10: sub->start_display_time = 0; yading@10: if (i == 1) yading@10: sub->num_rects = 0; yading@10: subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out, yading@10: subtitle_out_max_size, sub); yading@10: if (subtitle_out_size < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n"); yading@10: exit(1); yading@10: } yading@10: yading@10: av_init_packet(&pkt); yading@10: pkt.data = subtitle_out; yading@10: pkt.size = subtitle_out_size; yading@10: pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); yading@10: pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base); yading@10: if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) { yading@10: /* XXX: the pts correction is handled here. Maybe handling yading@10: it in the codec would be better */ yading@10: if (i == 0) yading@10: pkt.pts += 90 * sub->start_display_time; yading@10: else yading@10: pkt.pts += 90 * sub->end_display_time; yading@10: } yading@10: subtitle_size += pkt.size; yading@10: write_frame(s, &pkt, ost); yading@10: } yading@10: } yading@10: yading@10: static void do_video_out(AVFormatContext *s, yading@10: OutputStream *ost, yading@10: AVFrame *in_picture) yading@10: { yading@10: int ret, format_video_sync; yading@10: AVPacket pkt; yading@10: AVCodecContext *enc = ost->st->codec; yading@10: int nb_frames, i; yading@10: double sync_ipts, delta; yading@10: double duration = 0; yading@10: int frame_size = 0; yading@10: InputStream *ist = NULL; yading@10: yading@10: if (ost->source_index >= 0) yading@10: ist = input_streams[ost->source_index]; yading@10: yading@10: if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num) yading@10: duration = 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)); yading@10: yading@10: sync_ipts = in_picture->pts; yading@10: delta = sync_ipts - ost->sync_opts + duration; yading@10: yading@10: /* by default, we output a single frame */ yading@10: nb_frames = 1; yading@10: yading@10: format_video_sync = video_sync_method; yading@10: if (format_video_sync == VSYNC_AUTO) yading@10: format_video_sync = (s->oformat->flags & AVFMT_VARIABLE_FPS) ? ((s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR; yading@10: yading@10: switch (format_video_sync) { yading@10: case VSYNC_CFR: yading@10: // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c yading@10: if (delta < -1.1) yading@10: nb_frames = 0; yading@10: else if (delta > 1.1) yading@10: nb_frames = lrintf(delta); yading@10: break; yading@10: case VSYNC_VFR: yading@10: if (delta <= -0.6) yading@10: nb_frames = 0; yading@10: else if (delta > 0.6) yading@10: ost->sync_opts = lrint(sync_ipts); yading@10: break; yading@10: case VSYNC_DROP: yading@10: case VSYNC_PASSTHROUGH: yading@10: ost->sync_opts = lrint(sync_ipts); yading@10: break; yading@10: default: yading@10: av_assert0(0); yading@10: } yading@10: yading@10: nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); yading@10: if (nb_frames == 0) { yading@10: nb_frames_drop++; yading@10: av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n"); yading@10: return; yading@10: } else if (nb_frames > 1) { yading@10: if (nb_frames > dts_error_threshold * 30) { yading@10: av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1); yading@10: nb_frames_drop++; yading@10: return; yading@10: } yading@10: nb_frames_dup += nb_frames - 1; yading@10: av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1); yading@10: } yading@10: yading@10: /* duplicates frame if needed */ yading@10: for (i = 0; i < nb_frames; i++) { yading@10: av_init_packet(&pkt); yading@10: pkt.data = NULL; yading@10: pkt.size = 0; yading@10: yading@10: in_picture->pts = ost->sync_opts; yading@10: yading@10: if (!check_recording_time(ost)) yading@10: return; yading@10: yading@10: if (s->oformat->flags & AVFMT_RAWPICTURE && yading@10: enc->codec->id == AV_CODEC_ID_RAWVIDEO) { yading@10: /* raw pictures are written as AVPicture structure to yading@10: avoid any copies. We support temporarily the older yading@10: method. */ yading@10: enc->coded_frame->interlaced_frame = in_picture->interlaced_frame; yading@10: enc->coded_frame->top_field_first = in_picture->top_field_first; yading@10: if (enc->coded_frame->interlaced_frame) yading@10: enc->field_order = enc->coded_frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT; yading@10: else yading@10: enc->field_order = AV_FIELD_PROGRESSIVE; yading@10: pkt.data = (uint8_t *)in_picture; yading@10: pkt.size = sizeof(AVPicture); yading@10: pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base); yading@10: pkt.flags |= AV_PKT_FLAG_KEY; yading@10: yading@10: video_size += pkt.size; yading@10: write_frame(s, &pkt, ost); yading@10: } else { yading@10: int got_packet, forced_keyframe = 0; yading@10: double pts_time; yading@10: yading@10: if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME) && yading@10: ost->top_field_first >= 0) yading@10: in_picture->top_field_first = !!ost->top_field_first; yading@10: yading@10: if (in_picture->interlaced_frame) { yading@10: if (enc->codec->id == AV_CODEC_ID_MJPEG) yading@10: enc->field_order = in_picture->top_field_first ? AV_FIELD_TT:AV_FIELD_BB; yading@10: else yading@10: enc->field_order = in_picture->top_field_first ? AV_FIELD_TB:AV_FIELD_BT; yading@10: } else yading@10: enc->field_order = AV_FIELD_PROGRESSIVE; yading@10: yading@10: in_picture->quality = ost->st->codec->global_quality; yading@10: if (!enc->me_threshold) yading@10: in_picture->pict_type = 0; yading@10: yading@10: pts_time = in_picture->pts != AV_NOPTS_VALUE ? yading@10: in_picture->pts * av_q2d(enc->time_base) : NAN; yading@10: if (ost->forced_kf_index < ost->forced_kf_count && yading@10: in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) { yading@10: ost->forced_kf_index++; yading@10: forced_keyframe = 1; yading@10: } else if (ost->forced_keyframes_pexpr) { yading@10: double res; yading@10: ost->forced_keyframes_expr_const_values[FKF_T] = pts_time; yading@10: res = av_expr_eval(ost->forced_keyframes_pexpr, yading@10: ost->forced_keyframes_expr_const_values, NULL); yading@10: av_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n", yading@10: ost->forced_keyframes_expr_const_values[FKF_N], yading@10: ost->forced_keyframes_expr_const_values[FKF_N_FORCED], yading@10: ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N], yading@10: ost->forced_keyframes_expr_const_values[FKF_T], yading@10: ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T], yading@10: res); yading@10: if (res) { yading@10: forced_keyframe = 1; yading@10: ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = yading@10: ost->forced_keyframes_expr_const_values[FKF_N]; yading@10: ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = yading@10: ost->forced_keyframes_expr_const_values[FKF_T]; yading@10: ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1; yading@10: } yading@10: yading@10: ost->forced_keyframes_expr_const_values[FKF_N] += 1; yading@10: } yading@10: if (forced_keyframe) { yading@10: in_picture->pict_type = AV_PICTURE_TYPE_I; yading@10: av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time); yading@10: } yading@10: yading@10: update_benchmark(NULL); yading@10: ret = avcodec_encode_video2(enc, &pkt, in_picture, &got_packet); yading@10: update_benchmark("encode_video %d.%d", ost->file_index, ost->index); yading@10: if (ret < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); yading@10: exit(1); yading@10: } yading@10: yading@10: if (got_packet) { yading@10: if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & CODEC_CAP_DELAY)) yading@10: pkt.pts = ost->sync_opts; yading@10: yading@10: if (pkt.pts != AV_NOPTS_VALUE) yading@10: pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base); yading@10: if (pkt.dts != AV_NOPTS_VALUE) yading@10: pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base); yading@10: yading@10: if (debug_ts) { yading@10: av_log(NULL, AV_LOG_INFO, "encoder -> type:video " yading@10: "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n", yading@10: av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base), yading@10: av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base)); yading@10: } yading@10: yading@10: frame_size = pkt.size; yading@10: video_size += pkt.size; yading@10: write_frame(s, &pkt, ost); yading@10: av_free_packet(&pkt); yading@10: yading@10: /* if two pass, output log */ yading@10: if (ost->logfile && enc->stats_out) { yading@10: fprintf(ost->logfile, "%s", enc->stats_out); yading@10: } yading@10: } yading@10: } yading@10: ost->sync_opts++; yading@10: /* yading@10: * For video, number of frames in == number of packets out. yading@10: * But there may be reordering, so we can't throw away frames on encoder yading@10: * flush, we need to limit them here, before they go into encoder. yading@10: */ yading@10: ost->frame_number++; yading@10: yading@10: if (vstats_filename && frame_size) yading@10: do_video_stats(ost, frame_size); yading@10: } yading@10: } yading@10: yading@10: static double psnr(double d) yading@10: { yading@10: return -10.0 * log(d) / log(10.0); yading@10: } yading@10: yading@10: static void do_video_stats(OutputStream *ost, int frame_size) yading@10: { yading@10: AVCodecContext *enc; yading@10: int frame_number; yading@10: double ti1, bitrate, avg_bitrate; yading@10: yading@10: /* this is executed just the first time do_video_stats is called */ yading@10: if (!vstats_file) { yading@10: vstats_file = fopen(vstats_filename, "w"); yading@10: if (!vstats_file) { yading@10: perror("fopen"); yading@10: exit(1); yading@10: } yading@10: } yading@10: yading@10: enc = ost->st->codec; yading@10: if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { yading@10: frame_number = ost->st->nb_frames; yading@10: fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA); yading@10: if (enc->flags&CODEC_FLAG_PSNR) yading@10: fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0))); yading@10: yading@10: fprintf(vstats_file,"f_size= %6d ", frame_size); yading@10: /* compute pts value */ yading@10: ti1 = ost->st->pts.val * av_q2d(enc->time_base); yading@10: if (ti1 < 0.01) yading@10: ti1 = 0.01; yading@10: yading@10: bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; yading@10: avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0; yading@10: fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", yading@10: (double)video_size / 1024, ti1, bitrate, avg_bitrate); yading@10: fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type)); yading@10: } yading@10: } yading@10: yading@10: /** yading@10: * Get and encode new output from any of the filtergraphs, without causing yading@10: * activity. yading@10: * yading@10: * @return 0 for success, <0 for severe errors yading@10: */ yading@10: static int reap_filters(void) yading@10: { yading@10: AVFrame *filtered_frame = NULL; yading@10: int i; yading@10: int64_t frame_pts; yading@10: yading@10: /* Reap all buffers present in the buffer sinks */ yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: OutputStream *ost = output_streams[i]; yading@10: OutputFile *of = output_files[ost->file_index]; yading@10: int ret = 0; yading@10: yading@10: if (!ost->filter) yading@10: continue; yading@10: yading@10: if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) { yading@10: return AVERROR(ENOMEM); yading@10: } else yading@10: avcodec_get_frame_defaults(ost->filtered_frame); yading@10: filtered_frame = ost->filtered_frame; yading@10: yading@10: while (1) { yading@10: ret = av_buffersink_get_frame_flags(ost->filter->filter, filtered_frame, yading@10: AV_BUFFERSINK_FLAG_NO_REQUEST); yading@10: if (ret < 0) { yading@10: if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) { yading@10: av_log(NULL, AV_LOG_WARNING, yading@10: "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret)); yading@10: } yading@10: break; yading@10: } yading@10: frame_pts = AV_NOPTS_VALUE; yading@10: if (filtered_frame->pts != AV_NOPTS_VALUE) { yading@10: filtered_frame->pts = frame_pts = av_rescale_q(filtered_frame->pts, yading@10: ost->filter->filter->inputs[0]->time_base, yading@10: ost->st->codec->time_base) - yading@10: av_rescale_q(of->start_time, yading@10: AV_TIME_BASE_Q, yading@10: ost->st->codec->time_base); yading@10: yading@10: if (of->start_time && filtered_frame->pts < 0) { yading@10: av_frame_unref(filtered_frame); yading@10: continue; yading@10: } yading@10: } yading@10: //if (ost->source_index >= 0) yading@10: // *filtered_frame= *input_streams[ost->source_index]->decoded_frame; //for me_threshold yading@10: yading@10: yading@10: switch (ost->filter->filter->inputs[0]->type) { yading@10: case AVMEDIA_TYPE_VIDEO: yading@10: filtered_frame->pts = frame_pts; yading@10: if (!ost->frame_aspect_ratio.num) yading@10: ost->st->codec->sample_aspect_ratio = filtered_frame->sample_aspect_ratio; yading@10: yading@10: do_video_out(of->ctx, ost, filtered_frame); yading@10: break; yading@10: case AVMEDIA_TYPE_AUDIO: yading@10: filtered_frame->pts = frame_pts; yading@10: if (!(ost->st->codec->codec->capabilities & CODEC_CAP_PARAM_CHANGE) && yading@10: ost->st->codec->channels != av_frame_get_channels(filtered_frame)) { yading@10: av_log(NULL, AV_LOG_ERROR, yading@10: "Audio filter graph output is not normalized and encoder does not support parameter changes\n"); yading@10: break; yading@10: } yading@10: do_audio_out(of->ctx, ost, filtered_frame); yading@10: break; yading@10: default: yading@10: // TODO support subtitle filters yading@10: av_assert0(0); yading@10: } yading@10: yading@10: av_frame_unref(filtered_frame); yading@10: } yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time) yading@10: { yading@10: char buf[1024]; yading@10: AVBPrint buf_script; yading@10: OutputStream *ost; yading@10: AVFormatContext *oc; yading@10: int64_t total_size; yading@10: AVCodecContext *enc; yading@10: int frame_number, vid, i; yading@10: double bitrate; yading@10: int64_t pts = INT64_MIN; yading@10: static int64_t last_time = -1; yading@10: static int qp_histogram[52]; yading@10: int hours, mins, secs, us; yading@10: yading@10: if (!print_stats && !is_last_report && !progress_avio) yading@10: return; yading@10: yading@10: if (!is_last_report) { yading@10: if (last_time == -1) { yading@10: last_time = cur_time; yading@10: return; yading@10: } yading@10: if ((cur_time - last_time) < 500000) yading@10: return; yading@10: last_time = cur_time; yading@10: } yading@10: yading@10: yading@10: oc = output_files[0]->ctx; yading@10: yading@10: total_size = avio_size(oc->pb); yading@10: if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too yading@10: total_size = avio_tell(oc->pb); yading@10: yading@10: buf[0] = '\0'; yading@10: vid = 0; yading@10: av_bprint_init(&buf_script, 0, 1); yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: float q = -1; yading@10: ost = output_streams[i]; yading@10: enc = ost->st->codec; yading@10: if (!ost->stream_copy && enc->coded_frame) yading@10: q = enc->coded_frame->quality / (float)FF_QP2LAMBDA; yading@10: if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) { yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q); yading@10: av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n", yading@10: ost->file_index, ost->index, q); yading@10: } yading@10: if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) { yading@10: float fps, t = (cur_time-timer_start) / 1000000.0; yading@10: yading@10: frame_number = ost->frame_number; yading@10: fps = t > 1 ? frame_number / t : 0; yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3.*f q=%3.1f ", yading@10: frame_number, fps < 9.95, fps, q); yading@10: av_bprintf(&buf_script, "frame=%d\n", frame_number); yading@10: av_bprintf(&buf_script, "fps=%.1f\n", fps); yading@10: av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n", yading@10: ost->file_index, ost->index, q); yading@10: if (is_last_report) yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L"); yading@10: if (qp_hist) { yading@10: int j; yading@10: int qp = lrintf(q); yading@10: if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram)) yading@10: qp_histogram[qp]++; yading@10: for (j = 0; j < 32; j++) yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1))); yading@10: } yading@10: if ((enc->flags&CODEC_FLAG_PSNR) && (enc->coded_frame || is_last_report)) { yading@10: int j; yading@10: double error, error_sum = 0; yading@10: double scale, scale_sum = 0; yading@10: double p; yading@10: char type[3] = { 'Y','U','V' }; yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR="); yading@10: for (j = 0; j < 3; j++) { yading@10: if (is_last_report) { yading@10: error = enc->error[j]; yading@10: scale = enc->width * enc->height * 255.0 * 255.0 * frame_number; yading@10: } else { yading@10: error = enc->coded_frame->error[j]; yading@10: scale = enc->width * enc->height * 255.0 * 255.0; yading@10: } yading@10: if (j) yading@10: scale /= 4; yading@10: error_sum += error; yading@10: scale_sum += scale; yading@10: p = psnr(error / scale); yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], p); yading@10: av_bprintf(&buf_script, "stream_%d_%d_psnr_%c=%2.2f\n", yading@10: ost->file_index, ost->index, type[j] | 32, p); yading@10: } yading@10: p = psnr(error_sum / scale_sum); yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum)); yading@10: av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n", yading@10: ost->file_index, ost->index, p); yading@10: } yading@10: vid = 1; yading@10: } yading@10: /* compute min output value */ yading@10: if ((is_last_report || !ost->finished) && ost->st->pts.val != AV_NOPTS_VALUE) yading@10: pts = FFMAX(pts, av_rescale_q(ost->st->pts.val, yading@10: ost->st->time_base, AV_TIME_BASE_Q)); yading@10: } yading@10: yading@10: secs = pts / AV_TIME_BASE; yading@10: us = pts % AV_TIME_BASE; yading@10: mins = secs / 60; yading@10: secs %= 60; yading@10: hours = mins / 60; yading@10: mins %= 60; yading@10: yading@10: bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1; yading@10: yading@10: if (total_size < 0) snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), yading@10: "size=N/A time="); yading@10: else snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), yading@10: "size=%8.0fkB time=", total_size / 1024.0); yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), yading@10: "%02d:%02d:%02d.%02d ", hours, mins, secs, yading@10: (100 * us) / AV_TIME_BASE); yading@10: if (bitrate < 0) snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), yading@10: "bitrate=N/A"); yading@10: else snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), yading@10: "bitrate=%6.1fkbits/s", bitrate); yading@10: if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n"); yading@10: else av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size); yading@10: av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts); yading@10: av_bprintf(&buf_script, "out_time=%02d:%02d:%02d.%06d\n", yading@10: hours, mins, secs, us); yading@10: yading@10: if (nb_frames_dup || nb_frames_drop) yading@10: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d", yading@10: nb_frames_dup, nb_frames_drop); yading@10: av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup); yading@10: av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop); yading@10: yading@10: if (print_stats || is_last_report) { yading@10: if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) { yading@10: fprintf(stderr, "%s \r", buf); yading@10: } else yading@10: av_log(NULL, AV_LOG_INFO, "%s \r", buf); yading@10: yading@10: fflush(stderr); yading@10: } yading@10: yading@10: if (progress_avio) { yading@10: av_bprintf(&buf_script, "progress=%s\n", yading@10: is_last_report ? "end" : "continue"); yading@10: avio_write(progress_avio, buf_script.str, yading@10: FFMIN(buf_script.len, buf_script.size - 1)); yading@10: avio_flush(progress_avio); yading@10: av_bprint_finalize(&buf_script, NULL); yading@10: if (is_last_report) { yading@10: avio_close(progress_avio); yading@10: progress_avio = NULL; yading@10: } yading@10: } yading@10: yading@10: if (is_last_report) { yading@10: int64_t raw= audio_size + video_size + subtitle_size + extra_size; yading@10: av_log(NULL, AV_LOG_INFO, "\n"); yading@10: av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n", yading@10: video_size / 1024.0, yading@10: audio_size / 1024.0, yading@10: subtitle_size / 1024.0, yading@10: extra_size / 1024.0, yading@10: 100.0 * (total_size - raw) / raw yading@10: ); yading@10: if(video_size + audio_size + subtitle_size + extra_size == 0){ yading@10: av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n"); yading@10: } yading@10: } yading@10: } yading@10: yading@10: static void flush_encoders(void) yading@10: { yading@10: int i, ret; yading@10: yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: OutputStream *ost = output_streams[i]; yading@10: AVCodecContext *enc = ost->st->codec; yading@10: AVFormatContext *os = output_files[ost->file_index]->ctx; yading@10: int stop_encoding = 0; yading@10: yading@10: if (!ost->encoding_needed) yading@10: continue; yading@10: yading@10: if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1) yading@10: continue; yading@10: if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO) yading@10: continue; yading@10: yading@10: for (;;) { yading@10: int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL; yading@10: const char *desc; yading@10: int64_t *size; yading@10: yading@10: switch (ost->st->codec->codec_type) { yading@10: case AVMEDIA_TYPE_AUDIO: yading@10: encode = avcodec_encode_audio2; yading@10: desc = "Audio"; yading@10: size = &audio_size; yading@10: break; yading@10: case AVMEDIA_TYPE_VIDEO: yading@10: encode = avcodec_encode_video2; yading@10: desc = "Video"; yading@10: size = &video_size; yading@10: break; yading@10: default: yading@10: stop_encoding = 1; yading@10: } yading@10: yading@10: if (encode) { yading@10: AVPacket pkt; yading@10: int got_packet; yading@10: av_init_packet(&pkt); yading@10: pkt.data = NULL; yading@10: pkt.size = 0; yading@10: yading@10: update_benchmark(NULL); yading@10: ret = encode(enc, &pkt, NULL, &got_packet); yading@10: update_benchmark("flush %s %d.%d", desc, ost->file_index, ost->index); yading@10: if (ret < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc); yading@10: exit(1); yading@10: } yading@10: *size += pkt.size; yading@10: if (ost->logfile && enc->stats_out) { yading@10: fprintf(ost->logfile, "%s", enc->stats_out); yading@10: } yading@10: if (!got_packet) { yading@10: stop_encoding = 1; yading@10: break; yading@10: } yading@10: if (pkt.pts != AV_NOPTS_VALUE) yading@10: pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base); yading@10: if (pkt.dts != AV_NOPTS_VALUE) yading@10: pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base); yading@10: if (pkt.duration > 0) yading@10: pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base); yading@10: write_frame(os, &pkt, ost); yading@10: if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) { yading@10: do_video_stats(ost, pkt.size); yading@10: } yading@10: } yading@10: yading@10: if (stop_encoding) yading@10: break; yading@10: } yading@10: } yading@10: } yading@10: yading@10: /* yading@10: * Check whether a packet from ist should be written into ost at this time yading@10: */ yading@10: static int check_output_constraints(InputStream *ist, OutputStream *ost) yading@10: { yading@10: OutputFile *of = output_files[ost->file_index]; yading@10: int ist_index = input_files[ist->file_index]->ist_index + ist->st->index; yading@10: yading@10: if (ost->source_index != ist_index) yading@10: return 0; yading@10: yading@10: if (of->start_time && ist->pts < of->start_time) yading@10: return 0; yading@10: yading@10: return 1; yading@10: } yading@10: yading@10: static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt) yading@10: { yading@10: OutputFile *of = output_files[ost->file_index]; yading@10: int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base); yading@10: AVPicture pict; yading@10: AVPacket opkt; yading@10: yading@10: av_init_packet(&opkt); yading@10: yading@10: if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && yading@10: !ost->copy_initial_nonkeyframes) yading@10: return; yading@10: yading@10: if (!ost->frame_number && ist->pts < of->start_time && yading@10: !ost->copy_prior_start) yading@10: return; yading@10: yading@10: if (of->recording_time != INT64_MAX && yading@10: ist->pts >= of->recording_time + of->start_time) { yading@10: close_output_stream(ost); yading@10: return; yading@10: } yading@10: yading@10: /* force the input stream PTS */ yading@10: if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) yading@10: audio_size += pkt->size; yading@10: else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { yading@10: video_size += pkt->size; yading@10: ost->sync_opts++; yading@10: } else if (ost->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) { yading@10: subtitle_size += pkt->size; yading@10: } yading@10: yading@10: if (pkt->pts != AV_NOPTS_VALUE) yading@10: opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time; yading@10: else yading@10: opkt.pts = AV_NOPTS_VALUE; yading@10: yading@10: if (pkt->dts == AV_NOPTS_VALUE) yading@10: opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->st->time_base); yading@10: else yading@10: opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base); yading@10: opkt.dts -= ost_tb_start_time; yading@10: yading@10: if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->dts != AV_NOPTS_VALUE) { yading@10: int duration = av_get_audio_frame_duration(ist->st->codec, pkt->size); yading@10: if(!duration) yading@10: duration = ist->st->codec->frame_size; yading@10: opkt.dts = opkt.pts = av_rescale_delta(ist->st->time_base, pkt->dts, yading@10: (AVRational){1, ist->st->codec->sample_rate}, duration, &ist->filter_in_rescale_delta_last, yading@10: ost->st->time_base) - ost_tb_start_time; yading@10: } yading@10: yading@10: opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base); yading@10: opkt.flags = pkt->flags; yading@10: yading@10: // FIXME remove the following 2 lines they shall be replaced by the bitstream filters yading@10: if ( ost->st->codec->codec_id != AV_CODEC_ID_H264 yading@10: && ost->st->codec->codec_id != AV_CODEC_ID_MPEG1VIDEO yading@10: && ost->st->codec->codec_id != AV_CODEC_ID_MPEG2VIDEO yading@10: && ost->st->codec->codec_id != AV_CODEC_ID_VC1 yading@10: ) { yading@10: if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY)) { yading@10: opkt.buf = av_buffer_create(opkt.data, opkt.size, av_buffer_default_free, NULL, 0); yading@10: if (!opkt.buf) yading@10: exit(1); yading@10: } yading@10: } else { yading@10: opkt.data = pkt->data; yading@10: opkt.size = pkt->size; yading@10: } yading@10: yading@10: if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (of->ctx->oformat->flags & AVFMT_RAWPICTURE)) { yading@10: /* store AVPicture in AVPacket, as expected by the output format */ yading@10: avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height); yading@10: opkt.data = (uint8_t *)&pict; yading@10: opkt.size = sizeof(AVPicture); yading@10: opkt.flags |= AV_PKT_FLAG_KEY; yading@10: } yading@10: yading@10: write_frame(of->ctx, &opkt, ost); yading@10: ost->st->codec->frame_number++; yading@10: } yading@10: yading@10: static void rate_emu_sleep(InputStream *ist) yading@10: { yading@10: if (input_files[ist->file_index]->rate_emu) { yading@10: int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE); yading@10: int64_t now = av_gettime() - ist->start; yading@10: if (pts > now) yading@10: av_usleep(pts - now); yading@10: } yading@10: } yading@10: yading@10: int guess_input_channel_layout(InputStream *ist) yading@10: { yading@10: AVCodecContext *dec = ist->st->codec; yading@10: yading@10: if (!dec->channel_layout) { yading@10: char layout_name[256]; yading@10: yading@10: if (dec->channels > ist->guess_layout_max) yading@10: return 0; yading@10: dec->channel_layout = av_get_default_channel_layout(dec->channels); yading@10: if (!dec->channel_layout) yading@10: return 0; yading@10: av_get_channel_layout_string(layout_name, sizeof(layout_name), yading@10: dec->channels, dec->channel_layout); yading@10: av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream " yading@10: "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name); yading@10: } yading@10: return 1; yading@10: } yading@10: yading@10: static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output) yading@10: { yading@10: AVFrame *decoded_frame, *f; yading@10: AVCodecContext *avctx = ist->st->codec; yading@10: int i, ret, err = 0, resample_changed; yading@10: AVRational decoded_frame_tb; yading@10: yading@10: if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame())) yading@10: return AVERROR(ENOMEM); yading@10: if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc())) yading@10: return AVERROR(ENOMEM); yading@10: decoded_frame = ist->decoded_frame; yading@10: yading@10: update_benchmark(NULL); yading@10: ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt); yading@10: update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index); yading@10: yading@10: if (ret >= 0 && avctx->sample_rate <= 0) { yading@10: av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate); yading@10: ret = AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: if (*got_output || ret<0 || pkt->size) yading@10: decode_error_stat[ret<0] ++; yading@10: yading@10: if (!*got_output || ret < 0) { yading@10: if (!pkt->size) { yading@10: for (i = 0; i < ist->nb_filters; i++) yading@10: #if 1 yading@10: av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0); yading@10: #else yading@10: av_buffersrc_add_frame(ist->filters[i]->filter, NULL); yading@10: #endif yading@10: } yading@10: return ret; yading@10: } yading@10: yading@10: #if 1 yading@10: /* increment next_dts to use for the case where the input stream does not yading@10: have timestamps or there are multiple frames in the packet */ yading@10: ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) / yading@10: avctx->sample_rate; yading@10: ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) / yading@10: avctx->sample_rate; yading@10: #endif yading@10: yading@10: rate_emu_sleep(ist); yading@10: yading@10: resample_changed = ist->resample_sample_fmt != decoded_frame->format || yading@10: ist->resample_channels != avctx->channels || yading@10: ist->resample_channel_layout != decoded_frame->channel_layout || yading@10: ist->resample_sample_rate != decoded_frame->sample_rate; yading@10: if (resample_changed) { yading@10: char layout1[64], layout2[64]; yading@10: yading@10: if (!guess_input_channel_layout(ist)) { yading@10: av_log(NULL, AV_LOG_FATAL, "Unable to find default channel " yading@10: "layout for Input Stream #%d.%d\n", ist->file_index, yading@10: ist->st->index); yading@10: exit(1); yading@10: } yading@10: decoded_frame->channel_layout = avctx->channel_layout; yading@10: yading@10: av_get_channel_layout_string(layout1, sizeof(layout1), ist->resample_channels, yading@10: ist->resample_channel_layout); yading@10: av_get_channel_layout_string(layout2, sizeof(layout2), avctx->channels, yading@10: decoded_frame->channel_layout); yading@10: yading@10: av_log(NULL, AV_LOG_INFO, yading@10: "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d chl:%s to rate:%d fmt:%s ch:%d chl:%s\n", yading@10: ist->file_index, ist->st->index, yading@10: ist->resample_sample_rate, av_get_sample_fmt_name(ist->resample_sample_fmt), yading@10: ist->resample_channels, layout1, yading@10: decoded_frame->sample_rate, av_get_sample_fmt_name(decoded_frame->format), yading@10: avctx->channels, layout2); yading@10: yading@10: ist->resample_sample_fmt = decoded_frame->format; yading@10: ist->resample_sample_rate = decoded_frame->sample_rate; yading@10: ist->resample_channel_layout = decoded_frame->channel_layout; yading@10: ist->resample_channels = avctx->channels; yading@10: yading@10: for (i = 0; i < nb_filtergraphs; i++) yading@10: if (ist_in_filtergraph(filtergraphs[i], ist)) { yading@10: FilterGraph *fg = filtergraphs[i]; yading@10: int j; yading@10: if (configure_filtergraph(fg) < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n"); yading@10: exit(1); yading@10: } yading@10: for (j = 0; j < fg->nb_outputs; j++) { yading@10: OutputStream *ost = fg->outputs[j]->ost; yading@10: if (ost->enc->type == AVMEDIA_TYPE_AUDIO && yading@10: !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) yading@10: av_buffersink_set_frame_size(ost->filter->filter, yading@10: ost->st->codec->frame_size); yading@10: } yading@10: } yading@10: } yading@10: yading@10: /* if the decoder provides a pts, use it instead of the last packet pts. yading@10: the decoder could be delaying output by a packet or more. */ yading@10: if (decoded_frame->pts != AV_NOPTS_VALUE) { yading@10: ist->dts = ist->next_dts = ist->pts = ist->next_pts = av_rescale_q(decoded_frame->pts, avctx->time_base, AV_TIME_BASE_Q); yading@10: decoded_frame_tb = avctx->time_base; yading@10: } else if (decoded_frame->pkt_pts != AV_NOPTS_VALUE) { yading@10: decoded_frame->pts = decoded_frame->pkt_pts; yading@10: pkt->pts = AV_NOPTS_VALUE; yading@10: decoded_frame_tb = ist->st->time_base; yading@10: } else if (pkt->pts != AV_NOPTS_VALUE) { yading@10: decoded_frame->pts = pkt->pts; yading@10: pkt->pts = AV_NOPTS_VALUE; yading@10: decoded_frame_tb = ist->st->time_base; yading@10: }else { yading@10: decoded_frame->pts = ist->dts; yading@10: decoded_frame_tb = AV_TIME_BASE_Q; yading@10: } yading@10: if (decoded_frame->pts != AV_NOPTS_VALUE) yading@10: decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts, yading@10: (AVRational){1, ist->st->codec->sample_rate}, decoded_frame->nb_samples, &ist->filter_in_rescale_delta_last, yading@10: (AVRational){1, ist->st->codec->sample_rate}); yading@10: for (i = 0; i < ist->nb_filters; i++) { yading@10: if (i < ist->nb_filters - 1) { yading@10: f = ist->filter_frame; yading@10: err = av_frame_ref(f, decoded_frame); yading@10: if (err < 0) yading@10: break; yading@10: } else yading@10: f = decoded_frame; yading@10: err = av_buffersrc_add_frame_flags(ist->filters[i]->filter, f, yading@10: AV_BUFFERSRC_FLAG_PUSH); yading@10: if (err < 0) yading@10: break; yading@10: } yading@10: decoded_frame->pts = AV_NOPTS_VALUE; yading@10: yading@10: av_frame_unref(ist->filter_frame); yading@10: av_frame_unref(decoded_frame); yading@10: return err < 0 ? err : ret; yading@10: } yading@10: yading@10: static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output) yading@10: { yading@10: AVFrame *decoded_frame, *f; yading@10: void *buffer_to_free = NULL; yading@10: int i, ret = 0, err = 0, resample_changed; yading@10: int64_t best_effort_timestamp; yading@10: AVRational *frame_sample_aspect; yading@10: yading@10: if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc())) yading@10: return AVERROR(ENOMEM); yading@10: if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc())) yading@10: return AVERROR(ENOMEM); yading@10: decoded_frame = ist->decoded_frame; yading@10: pkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base); yading@10: yading@10: update_benchmark(NULL); yading@10: ret = avcodec_decode_video2(ist->st->codec, yading@10: decoded_frame, got_output, pkt); yading@10: update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index); yading@10: yading@10: if (*got_output || ret<0 || pkt->size) yading@10: decode_error_stat[ret<0] ++; yading@10: yading@10: if (!*got_output || ret < 0) { yading@10: if (!pkt->size) { yading@10: for (i = 0; i < ist->nb_filters; i++) yading@10: #if 1 yading@10: av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0); yading@10: #else yading@10: av_buffersrc_add_frame(ist->filters[i]->filter, NULL); yading@10: #endif yading@10: } yading@10: return ret; yading@10: } yading@10: yading@10: if(ist->top_field_first>=0) yading@10: decoded_frame->top_field_first = ist->top_field_first; yading@10: yading@10: best_effort_timestamp= av_frame_get_best_effort_timestamp(decoded_frame); yading@10: if(best_effort_timestamp != AV_NOPTS_VALUE) yading@10: ist->next_pts = ist->pts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q); yading@10: yading@10: if (debug_ts) { yading@10: av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video " yading@10: "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64" best_effort_ts_time:%s keyframe:%d frame_type:%d \n", yading@10: ist->st->index, av_ts2str(decoded_frame->pts), yading@10: av_ts2timestr(decoded_frame->pts, &ist->st->time_base), yading@10: best_effort_timestamp, yading@10: av_ts2timestr(best_effort_timestamp, &ist->st->time_base), yading@10: decoded_frame->key_frame, decoded_frame->pict_type); yading@10: } yading@10: yading@10: pkt->size = 0; yading@10: yading@10: rate_emu_sleep(ist); yading@10: yading@10: if (ist->st->sample_aspect_ratio.num) yading@10: decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio; yading@10: yading@10: resample_changed = ist->resample_width != decoded_frame->width || yading@10: ist->resample_height != decoded_frame->height || yading@10: ist->resample_pix_fmt != decoded_frame->format; yading@10: if (resample_changed) { yading@10: av_log(NULL, AV_LOG_INFO, yading@10: "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n", yading@10: ist->file_index, ist->st->index, yading@10: ist->resample_width, ist->resample_height, av_get_pix_fmt_name(ist->resample_pix_fmt), yading@10: decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format)); yading@10: yading@10: ist->resample_width = decoded_frame->width; yading@10: ist->resample_height = decoded_frame->height; yading@10: ist->resample_pix_fmt = decoded_frame->format; yading@10: yading@10: for (i = 0; i < nb_filtergraphs; i++) { yading@10: if (ist_in_filtergraph(filtergraphs[i], ist) && ist->reinit_filters && yading@10: configure_filtergraph(filtergraphs[i]) < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n"); yading@10: exit(1); yading@10: } yading@10: } yading@10: } yading@10: yading@10: frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio"); yading@10: for (i = 0; i < ist->nb_filters; i++) { yading@10: if (!frame_sample_aspect->num) yading@10: *frame_sample_aspect = ist->st->sample_aspect_ratio; yading@10: yading@10: if (i < ist->nb_filters - 1) { yading@10: f = ist->filter_frame; yading@10: err = av_frame_ref(f, decoded_frame); yading@10: if (err < 0) yading@10: break; yading@10: } else yading@10: f = decoded_frame; yading@10: ret = av_buffersrc_add_frame_flags(ist->filters[i]->filter, f, AV_BUFFERSRC_FLAG_PUSH); yading@10: if (ret < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, yading@10: "Failed to inject frame into filter network: %s\n", av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: } yading@10: yading@10: av_frame_unref(ist->filter_frame); yading@10: av_frame_unref(decoded_frame); yading@10: av_free(buffer_to_free); yading@10: return err < 0 ? err : ret; yading@10: } yading@10: yading@10: static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output) yading@10: { yading@10: AVSubtitle subtitle; yading@10: int i, ret = avcodec_decode_subtitle2(ist->st->codec, yading@10: &subtitle, got_output, pkt); yading@10: yading@10: if (*got_output || ret<0 || pkt->size) yading@10: decode_error_stat[ret<0] ++; yading@10: yading@10: if (ret < 0 || !*got_output) { yading@10: if (!pkt->size) yading@10: sub2video_flush(ist); yading@10: return ret; yading@10: } yading@10: yading@10: if (ist->fix_sub_duration) { yading@10: if (ist->prev_sub.got_output) { yading@10: int end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts, yading@10: 1000, AV_TIME_BASE); yading@10: if (end < ist->prev_sub.subtitle.end_display_time) { yading@10: av_log(ist->st->codec, AV_LOG_DEBUG, yading@10: "Subtitle duration reduced from %d to %d\n", yading@10: ist->prev_sub.subtitle.end_display_time, end); yading@10: ist->prev_sub.subtitle.end_display_time = end; yading@10: } yading@10: } yading@10: FFSWAP(int, *got_output, ist->prev_sub.got_output); yading@10: FFSWAP(int, ret, ist->prev_sub.ret); yading@10: FFSWAP(AVSubtitle, subtitle, ist->prev_sub.subtitle); yading@10: } yading@10: yading@10: sub2video_update(ist, &subtitle); yading@10: yading@10: if (!*got_output || !subtitle.num_rects) yading@10: return ret; yading@10: yading@10: rate_emu_sleep(ist); yading@10: yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: OutputStream *ost = output_streams[i]; yading@10: yading@10: if (!check_output_constraints(ist, ost) || !ost->encoding_needed) yading@10: continue; yading@10: yading@10: do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle); yading@10: } yading@10: yading@10: avsubtitle_free(&subtitle); yading@10: return ret; yading@10: } yading@10: yading@10: /* pkt = NULL means EOF (needed to flush decoder buffers) */ yading@10: static int output_packet(InputStream *ist, const AVPacket *pkt) yading@10: { yading@10: int ret = 0, i; yading@10: int got_output = 0; yading@10: yading@10: AVPacket avpkt; yading@10: if (!ist->saw_first_ts) { yading@10: 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; yading@10: ist->pts = 0; yading@10: if (pkt != NULL && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) { yading@10: ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q); yading@10: ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong yading@10: } yading@10: ist->saw_first_ts = 1; yading@10: } yading@10: yading@10: if (ist->next_dts == AV_NOPTS_VALUE) yading@10: ist->next_dts = ist->dts; yading@10: if (ist->next_pts == AV_NOPTS_VALUE) yading@10: ist->next_pts = ist->pts; yading@10: yading@10: if (pkt == NULL) { yading@10: /* EOF handling */ yading@10: av_init_packet(&avpkt); yading@10: avpkt.data = NULL; yading@10: avpkt.size = 0; yading@10: goto handle_eof; yading@10: } else { yading@10: avpkt = *pkt; yading@10: } yading@10: yading@10: if (pkt->dts != AV_NOPTS_VALUE) { yading@10: ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q); yading@10: if (ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed) yading@10: ist->next_pts = ist->pts = ist->dts; yading@10: } yading@10: yading@10: // while we have more to decode or while the decoder did output something on EOF yading@10: while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) { yading@10: int duration; yading@10: handle_eof: yading@10: yading@10: ist->pts = ist->next_pts; yading@10: ist->dts = ist->next_dts; yading@10: yading@10: if (avpkt.size && avpkt.size != pkt->size) { yading@10: av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING, yading@10: "Multiple frames in a packet from stream %d\n", pkt->stream_index); yading@10: ist->showed_multi_packet_warning = 1; yading@10: } yading@10: yading@10: switch (ist->st->codec->codec_type) { yading@10: case AVMEDIA_TYPE_AUDIO: yading@10: ret = decode_audio (ist, &avpkt, &got_output); yading@10: break; yading@10: case AVMEDIA_TYPE_VIDEO: yading@10: ret = decode_video (ist, &avpkt, &got_output); yading@10: if (avpkt.duration) { yading@10: duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q); yading@10: } else if(ist->st->codec->time_base.num != 0 && ist->st->codec->time_base.den != 0) { yading@10: int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame; yading@10: duration = ((int64_t)AV_TIME_BASE * yading@10: ist->st->codec->time_base.num * ticks) / yading@10: ist->st->codec->time_base.den; yading@10: } else yading@10: duration = 0; yading@10: yading@10: if(ist->dts != AV_NOPTS_VALUE && duration) { yading@10: ist->next_dts += duration; yading@10: }else yading@10: ist->next_dts = AV_NOPTS_VALUE; yading@10: yading@10: if (got_output) yading@10: ist->next_pts += duration; //FIXME the duration is not correct in some cases yading@10: break; yading@10: case AVMEDIA_TYPE_SUBTITLE: yading@10: ret = transcode_subtitles(ist, &avpkt, &got_output); yading@10: break; yading@10: default: yading@10: return -1; yading@10: } yading@10: yading@10: if (ret < 0) yading@10: return ret; yading@10: yading@10: avpkt.dts= yading@10: avpkt.pts= AV_NOPTS_VALUE; yading@10: yading@10: // touch data and size only if not EOF yading@10: if (pkt) { yading@10: if(ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO) yading@10: ret = avpkt.size; yading@10: avpkt.data += ret; yading@10: avpkt.size -= ret; yading@10: } yading@10: if (!got_output) { yading@10: continue; yading@10: } yading@10: } yading@10: yading@10: /* handle stream copy */ yading@10: if (!ist->decoding_needed) { yading@10: rate_emu_sleep(ist); yading@10: ist->dts = ist->next_dts; yading@10: switch (ist->st->codec->codec_type) { yading@10: case AVMEDIA_TYPE_AUDIO: yading@10: ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) / yading@10: ist->st->codec->sample_rate; yading@10: break; yading@10: case AVMEDIA_TYPE_VIDEO: yading@10: if (ist->framerate.num) { yading@10: // TODO: Remove work-around for c99-to-c89 issue 7 yading@10: AVRational time_base_q = AV_TIME_BASE_Q; yading@10: int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate)); yading@10: ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q); yading@10: } else if (pkt->duration) { yading@10: ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q); yading@10: } else if(ist->st->codec->time_base.num != 0) { yading@10: int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame; yading@10: ist->next_dts += ((int64_t)AV_TIME_BASE * yading@10: ist->st->codec->time_base.num * ticks) / yading@10: ist->st->codec->time_base.den; yading@10: } yading@10: break; yading@10: } yading@10: ist->pts = ist->dts; yading@10: ist->next_pts = ist->next_dts; yading@10: } yading@10: for (i = 0; pkt && i < nb_output_streams; i++) { yading@10: OutputStream *ost = output_streams[i]; yading@10: yading@10: if (!check_output_constraints(ist, ost) || ost->encoding_needed) yading@10: continue; yading@10: yading@10: do_streamcopy(ist, ost, pkt); yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static void print_sdp(void) yading@10: { yading@10: char sdp[16384]; yading@10: int i; yading@10: AVFormatContext **avc = av_malloc(sizeof(*avc) * nb_output_files); yading@10: yading@10: if (!avc) yading@10: exit(1); yading@10: for (i = 0; i < nb_output_files; i++) yading@10: avc[i] = output_files[i]->ctx; yading@10: yading@10: av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp)); yading@10: printf("SDP:\n%s\n", sdp); yading@10: fflush(stdout); yading@10: av_freep(&avc); yading@10: } yading@10: yading@10: static int init_input_stream(int ist_index, char *error, int error_len) yading@10: { yading@10: int ret; yading@10: InputStream *ist = input_streams[ist_index]; yading@10: yading@10: if (ist->decoding_needed) { yading@10: AVCodec *codec = ist->dec; yading@10: if (!codec) { yading@10: snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d", yading@10: avcodec_get_name(ist->st->codec->codec_id), ist->file_index, ist->st->index); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: av_opt_set_int(ist->st->codec, "refcounted_frames", 1, 0); yading@10: yading@10: if (!av_dict_get(ist->opts, "threads", NULL, 0)) yading@10: av_dict_set(&ist->opts, "threads", "auto", 0); yading@10: if ((ret = avcodec_open2(ist->st->codec, codec, &ist->opts)) < 0) { yading@10: if (ret == AVERROR_EXPERIMENTAL) yading@10: abort_codec_experimental(codec, 0); yading@10: snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d", yading@10: ist->file_index, ist->st->index); yading@10: return ret; yading@10: } yading@10: assert_avoptions(ist->opts); yading@10: } yading@10: yading@10: ist->next_pts = AV_NOPTS_VALUE; yading@10: ist->next_dts = AV_NOPTS_VALUE; yading@10: ist->is_start = 1; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static InputStream *get_input_stream(OutputStream *ost) yading@10: { yading@10: if (ost->source_index >= 0) yading@10: return input_streams[ost->source_index]; yading@10: return NULL; yading@10: } yading@10: yading@10: static int compare_int64(const void *a, const void *b) yading@10: { yading@10: int64_t va = *(int64_t *)a, vb = *(int64_t *)b; yading@10: return va < vb ? -1 : va > vb ? +1 : 0; yading@10: } yading@10: yading@10: static void parse_forced_key_frames(char *kf, OutputStream *ost, yading@10: AVCodecContext *avctx) yading@10: { yading@10: char *p; yading@10: int n = 1, i, size, index = 0; yading@10: int64_t t, *pts; yading@10: yading@10: for (p = kf; *p; p++) yading@10: if (*p == ',') yading@10: n++; yading@10: size = n; yading@10: pts = av_malloc(sizeof(*pts) * size); yading@10: if (!pts) { yading@10: av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n"); yading@10: exit(1); yading@10: } yading@10: yading@10: p = kf; yading@10: for (i = 0; i < n; i++) { yading@10: char *next = strchr(p, ','); yading@10: yading@10: if (next) yading@10: *next++ = 0; yading@10: yading@10: if (!memcmp(p, "chapters", 8)) { yading@10: yading@10: AVFormatContext *avf = output_files[ost->file_index]->ctx; yading@10: int j; yading@10: yading@10: if (avf->nb_chapters > INT_MAX - size || yading@10: !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1, yading@10: sizeof(*pts)))) { yading@10: av_log(NULL, AV_LOG_FATAL, yading@10: "Could not allocate forced key frames array.\n"); yading@10: exit(1); yading@10: } yading@10: t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0; yading@10: t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base); yading@10: yading@10: for (j = 0; j < avf->nb_chapters; j++) { yading@10: AVChapter *c = avf->chapters[j]; yading@10: av_assert1(index < size); yading@10: pts[index++] = av_rescale_q(c->start, c->time_base, yading@10: avctx->time_base) + t; yading@10: } yading@10: yading@10: } else { yading@10: yading@10: t = parse_time_or_die("force_key_frames", p, 1); yading@10: av_assert1(index < size); yading@10: pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base); yading@10: yading@10: } yading@10: yading@10: p = next; yading@10: } yading@10: yading@10: av_assert0(index == size); yading@10: qsort(pts, size, sizeof(*pts), compare_int64); yading@10: ost->forced_kf_count = size; yading@10: ost->forced_kf_pts = pts; yading@10: } yading@10: yading@10: static void report_new_stream(int input_index, AVPacket *pkt) yading@10: { yading@10: InputFile *file = input_files[input_index]; yading@10: AVStream *st = file->ctx->streams[pkt->stream_index]; yading@10: yading@10: if (pkt->stream_index < file->nb_streams_warn) yading@10: return; yading@10: av_log(file->ctx, AV_LOG_WARNING, yading@10: "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n", yading@10: av_get_media_type_string(st->codec->codec_type), yading@10: input_index, pkt->stream_index, yading@10: pkt->pos, av_ts2timestr(pkt->dts, &st->time_base)); yading@10: file->nb_streams_warn = pkt->stream_index + 1; yading@10: } yading@10: yading@10: static int transcode_init(void) yading@10: { yading@10: int ret = 0, i, j, k; yading@10: AVFormatContext *oc; yading@10: AVCodecContext *codec; yading@10: OutputStream *ost; yading@10: InputStream *ist; yading@10: char error[1024]; yading@10: int want_sdp = 1; yading@10: yading@10: /* init framerate emulation */ yading@10: for (i = 0; i < nb_input_files; i++) { yading@10: InputFile *ifile = input_files[i]; yading@10: if (ifile->rate_emu) yading@10: for (j = 0; j < ifile->nb_streams; j++) yading@10: input_streams[j + ifile->ist_index]->start = av_gettime(); yading@10: } yading@10: yading@10: /* output stream init */ yading@10: for (i = 0; i < nb_output_files; i++) { yading@10: oc = output_files[i]->ctx; yading@10: if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) { yading@10: av_dump_format(oc, i, oc->filename, 1); yading@10: av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: } yading@10: yading@10: /* init complex filtergraphs */ yading@10: for (i = 0; i < nb_filtergraphs; i++) yading@10: if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0) yading@10: return ret; yading@10: yading@10: /* for each output stream, we compute the right encoding parameters */ yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: AVCodecContext *icodec = NULL; yading@10: ost = output_streams[i]; yading@10: oc = output_files[ost->file_index]->ctx; yading@10: ist = get_input_stream(ost); yading@10: yading@10: if (ost->attachment_filename) yading@10: continue; yading@10: yading@10: codec = ost->st->codec; yading@10: yading@10: if (ist) { yading@10: icodec = ist->st->codec; yading@10: yading@10: ost->st->disposition = ist->st->disposition; yading@10: codec->bits_per_raw_sample = icodec->bits_per_raw_sample; yading@10: codec->chroma_sample_location = icodec->chroma_sample_location; yading@10: } yading@10: yading@10: if (ost->stream_copy) { yading@10: uint64_t extra_size; yading@10: yading@10: av_assert0(ist && !ost->filter); yading@10: yading@10: extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE; yading@10: yading@10: if (extra_size > INT_MAX) { yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: /* if stream_copy is selected, no need to decode or encode */ yading@10: codec->codec_id = icodec->codec_id; yading@10: codec->codec_type = icodec->codec_type; yading@10: yading@10: if (!codec->codec_tag) { yading@10: unsigned int codec_tag; yading@10: if (!oc->oformat->codec_tag || yading@10: av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id || yading@10: !av_codec_get_tag2(oc->oformat->codec_tag, icodec->codec_id, &codec_tag)) yading@10: codec->codec_tag = icodec->codec_tag; yading@10: } yading@10: yading@10: codec->bit_rate = icodec->bit_rate; yading@10: codec->rc_max_rate = icodec->rc_max_rate; yading@10: codec->rc_buffer_size = icodec->rc_buffer_size; yading@10: codec->field_order = icodec->field_order; yading@10: codec->extradata = av_mallocz(extra_size); yading@10: if (!codec->extradata) { yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: memcpy(codec->extradata, icodec->extradata, icodec->extradata_size); yading@10: codec->extradata_size= icodec->extradata_size; yading@10: codec->bits_per_coded_sample = icodec->bits_per_coded_sample; yading@10: yading@10: codec->time_base = ist->st->time_base; yading@10: /* yading@10: * Avi is a special case here because it supports variable fps but yading@10: * having the fps and timebase differe significantly adds quite some yading@10: * overhead yading@10: */ yading@10: if(!strcmp(oc->oformat->name, "avi")) { yading@10: if ( copy_tb<0 && av_q2d(ist->st->r_frame_rate) >= av_q2d(ist->st->avg_frame_rate) yading@10: && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(ist->st->time_base) yading@10: && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(icodec->time_base) yading@10: && av_q2d(ist->st->time_base) < 1.0/500 && av_q2d(icodec->time_base) < 1.0/500 yading@10: || copy_tb==2){ yading@10: codec->time_base.num = ist->st->r_frame_rate.den; yading@10: codec->time_base.den = 2*ist->st->r_frame_rate.num; yading@10: codec->ticks_per_frame = 2; yading@10: } else if ( copy_tb<0 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base) yading@10: && av_q2d(ist->st->time_base) < 1.0/500 yading@10: || copy_tb==0){ yading@10: codec->time_base = icodec->time_base; yading@10: codec->time_base.num *= icodec->ticks_per_frame; yading@10: codec->time_base.den *= 2; yading@10: codec->ticks_per_frame = 2; yading@10: } yading@10: } else if(!(oc->oformat->flags & AVFMT_VARIABLE_FPS) yading@10: && strcmp(oc->oformat->name, "mov") && strcmp(oc->oformat->name, "mp4") && strcmp(oc->oformat->name, "3gp") yading@10: && strcmp(oc->oformat->name, "3g2") && strcmp(oc->oformat->name, "psp") && strcmp(oc->oformat->name, "ipod") yading@10: && strcmp(oc->oformat->name, "f4v") yading@10: ) { yading@10: if( copy_tb<0 && icodec->time_base.den yading@10: && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) yading@10: && av_q2d(ist->st->time_base) < 1.0/500 yading@10: || copy_tb==0){ yading@10: codec->time_base = icodec->time_base; yading@10: codec->time_base.num *= icodec->ticks_per_frame; yading@10: } yading@10: } yading@10: if ( codec->codec_tag == AV_RL32("tmcd") yading@10: && icodec->time_base.num < icodec->time_base.den yading@10: && icodec->time_base.num > 0 yading@10: && 121LL*icodec->time_base.num > icodec->time_base.den) { yading@10: codec->time_base = icodec->time_base; yading@10: } yading@10: yading@10: if (ist && !ost->frame_rate.num) yading@10: ost->frame_rate = ist->framerate; yading@10: if(ost->frame_rate.num) yading@10: codec->time_base = av_inv_q(ost->frame_rate); yading@10: yading@10: av_reduce(&codec->time_base.num, &codec->time_base.den, yading@10: codec->time_base.num, codec->time_base.den, INT_MAX); yading@10: yading@10: switch (codec->codec_type) { yading@10: case AVMEDIA_TYPE_AUDIO: yading@10: if (audio_volume != 256) { yading@10: av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n"); yading@10: exit(1); yading@10: } yading@10: codec->channel_layout = icodec->channel_layout; yading@10: codec->sample_rate = icodec->sample_rate; yading@10: codec->channels = icodec->channels; yading@10: codec->frame_size = icodec->frame_size; yading@10: codec->audio_service_type = icodec->audio_service_type; yading@10: codec->block_align = icodec->block_align; yading@10: if((codec->block_align == 1 || codec->block_align == 1152 || codec->block_align == 576) && codec->codec_id == AV_CODEC_ID_MP3) yading@10: codec->block_align= 0; yading@10: if(codec->codec_id == AV_CODEC_ID_AC3) yading@10: codec->block_align= 0; yading@10: break; yading@10: case AVMEDIA_TYPE_VIDEO: yading@10: codec->pix_fmt = icodec->pix_fmt; yading@10: codec->width = icodec->width; yading@10: codec->height = icodec->height; yading@10: codec->has_b_frames = icodec->has_b_frames; yading@10: if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option yading@10: codec->sample_aspect_ratio = yading@10: ost->st->sample_aspect_ratio = yading@10: av_mul_q(ost->frame_aspect_ratio, yading@10: (AVRational){ codec->height, codec->width }); yading@10: av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio " yading@10: "with stream copy may produce invalid files\n"); yading@10: } else if (!codec->sample_aspect_ratio.num) { yading@10: codec->sample_aspect_ratio = yading@10: ost->st->sample_aspect_ratio = yading@10: ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio : yading@10: ist->st->codec->sample_aspect_ratio.num ? yading@10: ist->st->codec->sample_aspect_ratio : (AVRational){0, 1}; yading@10: } yading@10: ost->st->avg_frame_rate = ist->st->avg_frame_rate; yading@10: break; yading@10: case AVMEDIA_TYPE_SUBTITLE: yading@10: codec->width = icodec->width; yading@10: codec->height = icodec->height; yading@10: break; yading@10: case AVMEDIA_TYPE_DATA: yading@10: case AVMEDIA_TYPE_ATTACHMENT: yading@10: break; yading@10: default: yading@10: abort(); yading@10: } yading@10: } else { yading@10: if (!ost->enc) yading@10: ost->enc = avcodec_find_encoder(codec->codec_id); yading@10: if (!ost->enc) { yading@10: /* should only happen when a default codec is not present. */ yading@10: snprintf(error, sizeof(error), "Encoder (codec %s) not found for output stream #%d:%d", yading@10: avcodec_get_name(ost->st->codec->codec_id), ost->file_index, ost->index); yading@10: ret = AVERROR(EINVAL); yading@10: goto dump_format; yading@10: } yading@10: yading@10: if (ist) yading@10: ist->decoding_needed++; yading@10: ost->encoding_needed = 1; yading@10: yading@10: if (!ost->filter && yading@10: (codec->codec_type == AVMEDIA_TYPE_VIDEO || yading@10: codec->codec_type == AVMEDIA_TYPE_AUDIO)) { yading@10: FilterGraph *fg; yading@10: fg = init_simple_filtergraph(ist, ost); yading@10: if (configure_filtergraph(fg)) { yading@10: av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n"); yading@10: exit(1); yading@10: } yading@10: } yading@10: yading@10: if (codec->codec_type == AVMEDIA_TYPE_VIDEO) { yading@10: if (ost->filter && !ost->frame_rate.num) yading@10: ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter); yading@10: if (ist && !ost->frame_rate.num) yading@10: ost->frame_rate = ist->framerate; yading@10: if (ist && !ost->frame_rate.num) yading@10: ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1}; yading@10: // ost->frame_rate = ist->st->avg_frame_rate.num ? ist->st->avg_frame_rate : (AVRational){25, 1}; yading@10: if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) { yading@10: int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates); yading@10: ost->frame_rate = ost->enc->supported_framerates[idx]; yading@10: } yading@10: } yading@10: yading@10: switch (codec->codec_type) { yading@10: case AVMEDIA_TYPE_AUDIO: yading@10: codec->sample_fmt = ost->filter->filter->inputs[0]->format; yading@10: codec->sample_rate = ost->filter->filter->inputs[0]->sample_rate; yading@10: codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout; yading@10: codec->channels = avfilter_link_get_channels(ost->filter->filter->inputs[0]); yading@10: codec->time_base = (AVRational){ 1, codec->sample_rate }; yading@10: break; yading@10: case AVMEDIA_TYPE_VIDEO: yading@10: codec->time_base = av_inv_q(ost->frame_rate); yading@10: if (ost->filter && !(codec->time_base.num && codec->time_base.den)) yading@10: codec->time_base = ost->filter->filter->inputs[0]->time_base; yading@10: if ( av_q2d(codec->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH yading@10: && (video_sync_method == VSYNC_CFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){ yading@10: av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n" yading@10: "Please consider specifying a lower framerate, a different muxer or -vsync 2\n"); yading@10: } yading@10: for (j = 0; j < ost->forced_kf_count; j++) yading@10: ost->forced_kf_pts[j] = av_rescale_q(ost->forced_kf_pts[j], yading@10: AV_TIME_BASE_Q, yading@10: codec->time_base); yading@10: yading@10: codec->width = ost->filter->filter->inputs[0]->w; yading@10: codec->height = ost->filter->filter->inputs[0]->h; yading@10: codec->sample_aspect_ratio = ost->st->sample_aspect_ratio = yading@10: ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option yading@10: av_mul_q(ost->frame_aspect_ratio, (AVRational){ codec->height, codec->width }) : yading@10: ost->filter->filter->inputs[0]->sample_aspect_ratio; yading@10: if (!strncmp(ost->enc->name, "libx264", 7) && yading@10: codec->pix_fmt == AV_PIX_FMT_NONE && yading@10: ost->filter->filter->inputs[0]->format != AV_PIX_FMT_YUV420P) yading@10: av_log(NULL, AV_LOG_INFO, yading@10: "No pixel format specified, %s for H.264 encoding chosen.\n" yading@10: "Use -pix_fmt yuv420p for compatibility with outdated media players.\n", yading@10: av_get_pix_fmt_name(ost->filter->filter->inputs[0]->format)); yading@10: codec->pix_fmt = ost->filter->filter->inputs[0]->format; yading@10: yading@10: if (!icodec || yading@10: codec->width != icodec->width || yading@10: codec->height != icodec->height || yading@10: codec->pix_fmt != icodec->pix_fmt) { yading@10: codec->bits_per_raw_sample = frame_bits_per_raw_sample; yading@10: } yading@10: yading@10: if (ost->forced_keyframes) { yading@10: if (!strncmp(ost->forced_keyframes, "expr:", 5)) { yading@10: ret = av_expr_parse(&ost->forced_keyframes_pexpr, ost->forced_keyframes+5, yading@10: forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL); yading@10: if (ret < 0) { yading@10: av_log(NULL, AV_LOG_ERROR, yading@10: "Invalid force_key_frames expression '%s'\n", ost->forced_keyframes+5); yading@10: return ret; yading@10: } yading@10: ost->forced_keyframes_expr_const_values[FKF_N] = 0; yading@10: ost->forced_keyframes_expr_const_values[FKF_N_FORCED] = 0; yading@10: ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = NAN; yading@10: ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = NAN; yading@10: } else { yading@10: parse_forced_key_frames(ost->forced_keyframes, ost, ost->st->codec); yading@10: } yading@10: } yading@10: break; yading@10: case AVMEDIA_TYPE_SUBTITLE: yading@10: codec->time_base = (AVRational){1, 1000}; yading@10: if (!codec->width) { yading@10: codec->width = input_streams[ost->source_index]->st->codec->width; yading@10: codec->height = input_streams[ost->source_index]->st->codec->height; yading@10: } yading@10: break; yading@10: default: yading@10: abort(); yading@10: break; yading@10: } yading@10: /* two pass mode */ yading@10: if (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) { yading@10: char logfilename[1024]; yading@10: FILE *f; yading@10: yading@10: snprintf(logfilename, sizeof(logfilename), "%s-%d.log", yading@10: ost->logfile_prefix ? ost->logfile_prefix : yading@10: DEFAULT_PASS_LOGFILENAME_PREFIX, yading@10: i); yading@10: if (!strcmp(ost->enc->name, "libx264")) { yading@10: av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE); yading@10: } else { yading@10: if (codec->flags & CODEC_FLAG_PASS2) { yading@10: char *logbuffer; yading@10: size_t logbuffer_size; yading@10: if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) { yading@10: av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n", yading@10: logfilename); yading@10: exit(1); yading@10: } yading@10: codec->stats_in = logbuffer; yading@10: } yading@10: if (codec->flags & CODEC_FLAG_PASS1) { yading@10: f = fopen(logfilename, "wb"); yading@10: if (!f) { yading@10: av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n", yading@10: logfilename, strerror(errno)); yading@10: exit(1); yading@10: } yading@10: ost->logfile = f; yading@10: } yading@10: } yading@10: } yading@10: } yading@10: } yading@10: yading@10: /* open each encoder */ yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: ost = output_streams[i]; yading@10: if (ost->encoding_needed) { yading@10: AVCodec *codec = ost->enc; yading@10: AVCodecContext *dec = NULL; yading@10: yading@10: if ((ist = get_input_stream(ost))) yading@10: dec = ist->st->codec; yading@10: if (dec && dec->subtitle_header) { yading@10: /* ASS code assumes this buffer is null terminated so add extra byte. */ yading@10: ost->st->codec->subtitle_header = av_mallocz(dec->subtitle_header_size + 1); yading@10: if (!ost->st->codec->subtitle_header) { yading@10: ret = AVERROR(ENOMEM); yading@10: goto dump_format; yading@10: } yading@10: memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size); yading@10: ost->st->codec->subtitle_header_size = dec->subtitle_header_size; yading@10: } yading@10: if (!av_dict_get(ost->opts, "threads", NULL, 0)) yading@10: av_dict_set(&ost->opts, "threads", "auto", 0); yading@10: if ((ret = avcodec_open2(ost->st->codec, codec, &ost->opts)) < 0) { yading@10: if (ret == AVERROR_EXPERIMENTAL) yading@10: abort_codec_experimental(codec, 1); yading@10: snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height", yading@10: ost->file_index, ost->index); yading@10: goto dump_format; yading@10: } yading@10: if (ost->enc->type == AVMEDIA_TYPE_AUDIO && yading@10: !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) yading@10: av_buffersink_set_frame_size(ost->filter->filter, yading@10: ost->st->codec->frame_size); yading@10: assert_avoptions(ost->opts); yading@10: if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000) yading@10: av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low." yading@10: " It takes bits/s as argument, not kbits/s\n"); yading@10: extra_size += ost->st->codec->extradata_size; yading@10: yading@10: if (ost->st->codec->me_threshold) yading@10: input_streams[ost->source_index]->st->codec->debug |= FF_DEBUG_MV; yading@10: } else { yading@10: av_opt_set_dict(ost->st->codec, &ost->opts); yading@10: } yading@10: } yading@10: yading@10: /* init input streams */ yading@10: for (i = 0; i < nb_input_streams; i++) yading@10: if ((ret = init_input_stream(i, error, sizeof(error))) < 0) { yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: ost = output_streams[i]; yading@10: avcodec_close(ost->st->codec); yading@10: } yading@10: goto dump_format; yading@10: } yading@10: yading@10: /* discard unused programs */ yading@10: for (i = 0; i < nb_input_files; i++) { yading@10: InputFile *ifile = input_files[i]; yading@10: for (j = 0; j < ifile->ctx->nb_programs; j++) { yading@10: AVProgram *p = ifile->ctx->programs[j]; yading@10: int discard = AVDISCARD_ALL; yading@10: yading@10: for (k = 0; k < p->nb_stream_indexes; k++) yading@10: if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) { yading@10: discard = AVDISCARD_DEFAULT; yading@10: break; yading@10: } yading@10: p->discard = discard; yading@10: } yading@10: } yading@10: yading@10: /* open files and write file headers */ yading@10: for (i = 0; i < nb_output_files; i++) { yading@10: oc = output_files[i]->ctx; yading@10: oc->interrupt_callback = int_cb; yading@10: if ((ret = avformat_write_header(oc, &output_files[i]->opts)) < 0) { yading@10: char errbuf[128]; yading@10: const char *errbuf_ptr = errbuf; yading@10: if (av_strerror(ret, errbuf, sizeof(errbuf)) < 0) yading@10: errbuf_ptr = strerror(AVUNERROR(ret)); yading@10: snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?): %s", i, errbuf_ptr); yading@10: ret = AVERROR(EINVAL); yading@10: goto dump_format; yading@10: } yading@10: // assert_avoptions(output_files[i]->opts); yading@10: if (strcmp(oc->oformat->name, "rtp")) { yading@10: want_sdp = 0; yading@10: } yading@10: } yading@10: yading@10: dump_format: yading@10: /* dump the file output parameters - cannot be done before in case yading@10: of stream copy */ yading@10: for (i = 0; i < nb_output_files; i++) { yading@10: av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1); yading@10: } yading@10: yading@10: /* dump the stream mapping */ yading@10: av_log(NULL, AV_LOG_INFO, "Stream mapping:\n"); yading@10: for (i = 0; i < nb_input_streams; i++) { yading@10: ist = input_streams[i]; yading@10: yading@10: for (j = 0; j < ist->nb_filters; j++) { yading@10: if (ist->filters[j]->graph->graph_desc) { yading@10: av_log(NULL, AV_LOG_INFO, " Stream #%d:%d (%s) -> %s", yading@10: ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?", yading@10: ist->filters[j]->name); yading@10: if (nb_filtergraphs > 1) yading@10: av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index); yading@10: av_log(NULL, AV_LOG_INFO, "\n"); yading@10: } yading@10: } yading@10: } yading@10: yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: ost = output_streams[i]; yading@10: yading@10: if (ost->attachment_filename) { yading@10: /* an attached file */ yading@10: av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n", yading@10: ost->attachment_filename, ost->file_index, ost->index); yading@10: continue; yading@10: } yading@10: yading@10: if (ost->filter && ost->filter->graph->graph_desc) { yading@10: /* output from a complex graph */ yading@10: av_log(NULL, AV_LOG_INFO, " %s", ost->filter->name); yading@10: if (nb_filtergraphs > 1) yading@10: av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index); yading@10: yading@10: av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index, yading@10: ost->index, ost->enc ? ost->enc->name : "?"); yading@10: continue; yading@10: } yading@10: yading@10: av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d", yading@10: input_streams[ost->source_index]->file_index, yading@10: input_streams[ost->source_index]->st->index, yading@10: ost->file_index, yading@10: ost->index); yading@10: if (ost->sync_ist != input_streams[ost->source_index]) yading@10: av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]", yading@10: ost->sync_ist->file_index, yading@10: ost->sync_ist->st->index); yading@10: if (ost->stream_copy) yading@10: av_log(NULL, AV_LOG_INFO, " (copy)"); yading@10: else yading@10: av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ? yading@10: input_streams[ost->source_index]->dec->name : "?", yading@10: ost->enc ? ost->enc->name : "?"); yading@10: av_log(NULL, AV_LOG_INFO, "\n"); yading@10: } yading@10: yading@10: if (ret) { yading@10: av_log(NULL, AV_LOG_ERROR, "%s\n", error); yading@10: return ret; yading@10: } yading@10: yading@10: if (want_sdp) { yading@10: print_sdp(); yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */ yading@10: static int need_output(void) yading@10: { yading@10: int i; yading@10: yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: OutputStream *ost = output_streams[i]; yading@10: OutputFile *of = output_files[ost->file_index]; yading@10: AVFormatContext *os = output_files[ost->file_index]->ctx; yading@10: yading@10: if (ost->finished || yading@10: (os->pb && avio_tell(os->pb) >= of->limit_filesize)) yading@10: continue; yading@10: if (ost->frame_number >= ost->max_frames) { yading@10: int j; yading@10: for (j = 0; j < of->ctx->nb_streams; j++) yading@10: close_output_stream(output_streams[of->ost_index + j]); yading@10: continue; yading@10: } yading@10: yading@10: return 1; yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: /** yading@10: * Select the output stream to process. yading@10: * yading@10: * @return selected output stream, or NULL if none available yading@10: */ yading@10: static OutputStream *choose_output(void) yading@10: { yading@10: int i; yading@10: int64_t opts_min = INT64_MAX; yading@10: OutputStream *ost_min = NULL; yading@10: yading@10: for (i = 0; i < nb_output_streams; i++) { yading@10: OutputStream *ost = output_streams[i]; yading@10: int64_t opts = av_rescale_q(ost->st->cur_dts, ost->st->time_base, yading@10: AV_TIME_BASE_Q); yading@10: if (!ost->unavailable && !ost->finished && opts < opts_min) { yading@10: opts_min = opts; yading@10: ost_min = ost; yading@10: } yading@10: } yading@10: return ost_min; yading@10: } yading@10: yading@10: static int check_keyboard_interaction(int64_t cur_time) yading@10: { yading@10: int i, ret, key; yading@10: static int64_t last_time; yading@10: if (received_nb_signals) yading@10: return AVERROR_EXIT; yading@10: /* read_key() returns 0 on EOF */ yading@10: if(cur_time - last_time >= 100000 && !run_as_daemon){ yading@10: key = read_key(); yading@10: last_time = cur_time; yading@10: }else yading@10: key = -1; yading@10: if (key == 'q') yading@10: return AVERROR_EXIT; yading@10: if (key == '+') av_log_set_level(av_log_get_level()+10); yading@10: if (key == '-') av_log_set_level(av_log_get_level()-10); yading@10: if (key == 's') qp_hist ^= 1; yading@10: if (key == 'h'){ yading@10: if (do_hex_dump){ yading@10: do_hex_dump = do_pkt_dump = 0; yading@10: } else if(do_pkt_dump){ yading@10: do_hex_dump = 1; yading@10: } else yading@10: do_pkt_dump = 1; yading@10: av_log_set_level(AV_LOG_DEBUG); yading@10: } yading@10: if (key == 'c' || key == 'C'){ yading@10: char buf[4096], target[64], command[256], arg[256] = {0}; yading@10: double time; yading@10: int k, n = 0; yading@10: fprintf(stderr, "\nEnter command: