- av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
- video_size / 1024.0,
- audio_size / 1024.0,
- subtitle_size / 1024.0,
- extra_size / 1024.0,
- 100.0 * (total_size - raw) / raw
- );
- if(video_size + audio_size + subtitle_size + extra_size == 0){
- av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
- }
- }
-}
-
-static void flush_encoders(void)
-{
- int i, ret;
-
- for (i = 0; i < nb_output_streams; i++) {
- OutputStream *ost = output_streams[i];
- AVCodecContext *enc = ost->st->codec;
- AVFormatContext *os = output_files[ost->file_index]->ctx;
- int stop_encoding = 0;
-
- if (!ost->encoding_needed)
- continue;
-
- if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
- continue;
- if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == CODEC_ID_RAWVIDEO)
- continue;
-
- for (;;) {
- int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
- const char *desc;
- int64_t *size;
-
- switch (ost->st->codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- encode = avcodec_encode_audio2;
- desc = "Audio";
- size = &audio_size;
- break;
- case AVMEDIA_TYPE_VIDEO:
- encode = avcodec_encode_video2;
- desc = "Video";
- size = &video_size;
- break;
- default:
- stop_encoding = 1;
- }
-
- if (encode) {
- AVPacket pkt;
- int got_packet;
- av_init_packet(&pkt);
- pkt.data = NULL;
- pkt.size = 0;
-
- update_benchmark(NULL);
- ret = encode(enc, &pkt, NULL, &got_packet);
- update_benchmark("flush %s %d.%d", desc, ost->file_index, ost->index);
- if (ret < 0) {
- av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
- exit_program(1);
- }
- *size += pkt.size;
- if (ost->logfile && enc->stats_out) {
- fprintf(ost->logfile, "%s", enc->stats_out);
- }
- if (!got_packet) {
- stop_encoding = 1;
- break;
- }
- if (pkt.pts != AV_NOPTS_VALUE)
- pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
- if (pkt.dts != AV_NOPTS_VALUE)
- pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
- write_frame(os, &pkt, ost);
- }
-
- if (stop_encoding)
- break;
- }
- }
-}
-
-/*
- * Check whether a packet from ist should be written into ost at this time
- */
-static int check_output_constraints(InputStream *ist, OutputStream *ost)
-{
- OutputFile *of = output_files[ost->file_index];
- int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
-
- if (ost->source_index != ist_index)
- return 0;
-
- if (of->start_time && ist->pts < of->start_time)
- return 0;
-
- return 1;
-}
-
-static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
-{
- OutputFile *of = output_files[ost->file_index];
- int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
- AVPicture pict;
- AVPacket opkt;
-
- av_init_packet(&opkt);
-
- if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
- !ost->copy_initial_nonkeyframes)
- return;
-
- if (of->recording_time != INT64_MAX &&
- ist->pts >= of->recording_time + of->start_time) {
- ost->is_past_recording_time = 1;
- return;
- }
-
- /* force the input stream PTS */
- if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
- audio_size += pkt->size;
- else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
- video_size += pkt->size;
- ost->sync_opts++;
- } else if (ost->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
- subtitle_size += pkt->size;
- }
-
- if (pkt->pts != AV_NOPTS_VALUE)
- opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
- else
- opkt.pts = AV_NOPTS_VALUE;
-
- if (pkt->dts == AV_NOPTS_VALUE)
- opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->st->time_base);
- else
- opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
- opkt.dts -= ost_tb_start_time;
-
- opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
- opkt.flags = pkt->flags;
-
- // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
- if ( ost->st->codec->codec_id != CODEC_ID_H264
- && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
- && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
- && ost->st->codec->codec_id != CODEC_ID_VC1
- ) {
- if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
- opkt.destruct = av_destruct_packet;
- } else {
- opkt.data = pkt->data;
- opkt.size = pkt->size;
- }
-
- if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (of->ctx->oformat->flags & AVFMT_RAWPICTURE)) {
- /* store AVPicture in AVPacket, as expected by the output format */
- avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
- opkt.data = (uint8_t *)&pict;
- opkt.size = sizeof(AVPicture);
- opkt.flags |= AV_PKT_FLAG_KEY;
- }
-
- write_frame(of->ctx, &opkt, ost);
- ost->st->codec->frame_number++;
- av_free_packet(&opkt);
-}
-
-static void rate_emu_sleep(InputStream *ist)
-{
- if (input_files[ist->file_index]->rate_emu) {
- int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
- int64_t now = av_gettime() - ist->start;
- if (pts > now)
- av_usleep(pts - now);
- }
-}
-
-static int guess_input_channel_layout(InputStream *ist)
-{
- AVCodecContext *dec = ist->st->codec;
-
- if (!dec->channel_layout) {
- char layout_name[256];
-
- dec->channel_layout = av_get_default_channel_layout(dec->channels);
- if (!dec->channel_layout)
- return 0;
- av_get_channel_layout_string(layout_name, sizeof(layout_name),
- dec->channels, dec->channel_layout);
- av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
- "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
- }
- return 1;
-}
-
-static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
-{
- AVFrame *decoded_frame;
- AVCodecContext *avctx = ist->st->codec;
- int i, ret, resample_changed;
- AVRational decoded_frame_tb;
-
- if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
- return AVERROR(ENOMEM);
- else
- avcodec_get_frame_defaults(ist->decoded_frame);
- decoded_frame = ist->decoded_frame;
-
- update_benchmark(NULL);
- ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
- update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
- if (ret < 0) {
- return ret;
- }
- if (avctx->sample_rate <= 0) {
- av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
- return AVERROR_INVALIDDATA;
- }
-
- if (!*got_output) {
- /* no audio frame */
- if (!pkt->size)
- for (i = 0; i < ist->nb_filters; i++)
- av_buffersrc_add_ref(ist->filters[i]->filter, NULL,
- AV_BUFFERSRC_FLAG_NO_COPY);
- return ret;
- }
-
-#if 1
- /* increment next_dts to use for the case where the input stream does not
- have timestamps or there are multiple frames in the packet */
- ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
- avctx->sample_rate;
- ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
- avctx->sample_rate;
-#endif
-
- rate_emu_sleep(ist);
-
- resample_changed = ist->resample_sample_fmt != decoded_frame->format ||
- ist->resample_channels != avctx->channels ||
- ist->resample_channel_layout != decoded_frame->channel_layout ||
- ist->resample_sample_rate != decoded_frame->sample_rate;
- if (resample_changed) {
- char layout1[64], layout2[64];
-
- if (!guess_input_channel_layout(ist)) {
- av_log(NULL, AV_LOG_FATAL, "Unable to find default channel "
- "layout for Input Stream #%d.%d\n", ist->file_index,
- ist->st->index);
- exit_program(1);
- }
- decoded_frame->channel_layout = avctx->channel_layout;
-
- av_get_channel_layout_string(layout1, sizeof(layout1), ist->resample_channels,
- ist->resample_channel_layout);
- av_get_channel_layout_string(layout2, sizeof(layout2), avctx->channels,
- decoded_frame->channel_layout);
-
- av_log(NULL, AV_LOG_INFO,
- "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",
- ist->file_index, ist->st->index,
- ist->resample_sample_rate, av_get_sample_fmt_name(ist->resample_sample_fmt),
- ist->resample_channels, layout1,
- decoded_frame->sample_rate, av_get_sample_fmt_name(decoded_frame->format),
- avctx->channels, layout2);
-
- ist->resample_sample_fmt = decoded_frame->format;
- ist->resample_sample_rate = decoded_frame->sample_rate;
- ist->resample_channel_layout = decoded_frame->channel_layout;
- ist->resample_channels = avctx->channels;
-
- for (i = 0; i < nb_filtergraphs; i++)
- if (ist_in_filtergraph(filtergraphs[i], ist)) {
- FilterGraph *fg = filtergraphs[i];
- int j;
- if (configure_filtergraph(fg) < 0) {
- av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
- exit_program(1);
- }
- for (j = 0; j < fg->nb_outputs; j++) {
- OutputStream *ost = fg->outputs[j]->ost;
- if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
- !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
- av_buffersink_set_frame_size(ost->filter->filter,
- ost->st->codec->frame_size);
- }
- }
- }
-
- /* if the decoder provides a pts, use it instead of the last packet pts.
- the decoder could be delaying output by a packet or more. */
- if (decoded_frame->pts != AV_NOPTS_VALUE) {
- ist->dts = ist->next_dts = ist->pts = ist->next_pts = av_rescale_q(decoded_frame->pts, avctx->time_base, AV_TIME_BASE_Q);
- decoded_frame_tb = avctx->time_base;
- } else if (decoded_frame->pkt_pts != AV_NOPTS_VALUE) {
- decoded_frame->pts = decoded_frame->pkt_pts;
- pkt->pts = AV_NOPTS_VALUE;
- decoded_frame_tb = ist->st->time_base;
- } else if (pkt->pts != AV_NOPTS_VALUE) {
- decoded_frame->pts = pkt->pts;
- pkt->pts = AV_NOPTS_VALUE;
- decoded_frame_tb = ist->st->time_base;
- }else {
- decoded_frame->pts = ist->dts;
- decoded_frame_tb = AV_TIME_BASE_Q;
- }
- if (decoded_frame->pts != AV_NOPTS_VALUE)
- decoded_frame->pts = av_rescale_q(decoded_frame->pts,
- decoded_frame_tb,
- (AVRational){1, ist->st->codec->sample_rate});
- for (i = 0; i < ist->nb_filters; i++)
- av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame, 0);
-
- decoded_frame->pts = AV_NOPTS_VALUE;
-
- return ret;
-}
-
-static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
-{
- AVFrame *decoded_frame;
- void *buffer_to_free = NULL;
- int i, ret = 0, resample_changed;
- int64_t best_effort_timestamp;
- AVRational *frame_sample_aspect;
- float quality;
-
- if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
- return AVERROR(ENOMEM);
- else
- avcodec_get_frame_defaults(ist->decoded_frame);
- decoded_frame = ist->decoded_frame;
- pkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
-
- update_benchmark(NULL);
- ret = avcodec_decode_video2(ist->st->codec,
- decoded_frame, got_output, pkt);
- update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
- if (ret < 0)
- return ret;
-
- quality = same_quant ? decoded_frame->quality : 0;
- if (!*got_output) {
- /* no picture yet */
- if (!pkt->size)
- for (i = 0; i < ist->nb_filters; i++)
- av_buffersrc_add_ref(ist->filters[i]->filter, NULL, AV_BUFFERSRC_FLAG_NO_COPY);
- return ret;
- }
-
- if(ist->top_field_first>=0)
- decoded_frame->top_field_first = ist->top_field_first;
-
- best_effort_timestamp= av_frame_get_best_effort_timestamp(decoded_frame);
- if(best_effort_timestamp != AV_NOPTS_VALUE)
- ist->next_pts = ist->pts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q);
-
- if (debug_ts) {
- av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video "
- "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64" best_effort_ts_time:%s keyframe:%d frame_type:%d \n",
- ist->st->index, av_ts2str(decoded_frame->pts),
- av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
- best_effort_timestamp,
- av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
- decoded_frame->key_frame, decoded_frame->pict_type);
- }
-
- pkt->size = 0;
- pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
-
- rate_emu_sleep(ist);
-
- if (ist->st->sample_aspect_ratio.num)
- decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
-
- resample_changed = ist->resample_width != decoded_frame->width ||
- ist->resample_height != decoded_frame->height ||
- ist->resample_pix_fmt != decoded_frame->format;
- if (resample_changed) {
- av_log(NULL, AV_LOG_INFO,
- "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
- ist->file_index, ist->st->index,
- ist->resample_width, ist->resample_height, av_get_pix_fmt_name(ist->resample_pix_fmt),
- decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format));
-
- ist->resample_width = decoded_frame->width;
- ist->resample_height = decoded_frame->height;
- ist->resample_pix_fmt = decoded_frame->format;
-
- for (i = 0; i < nb_filtergraphs; i++)
- if (ist_in_filtergraph(filtergraphs[i], ist) &&
- configure_filtergraph(filtergraphs[i]) < 0) {
- av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
- exit_program(1);
- }
- }
-
- frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
- for (i = 0; i < ist->nb_filters; i++) {
- int changed = ist->st->codec->width != ist->filters[i]->filter->outputs[0]->w
- || ist->st->codec->height != ist->filters[i]->filter->outputs[0]->h
- || ist->st->codec->pix_fmt != ist->filters[i]->filter->outputs[0]->format;
- // XXX what an ugly hack
- if (ist->filters[i]->graph->nb_outputs == 1)
- ist->filters[i]->graph->outputs[0]->ost->last_quality = quality;
-
- if (!frame_sample_aspect->num)
- *frame_sample_aspect = ist->st->sample_aspect_ratio;
- if (ist->dr1 && decoded_frame->type==FF_BUFFER_TYPE_USER && !changed) {
- FrameBuffer *buf = decoded_frame->opaque;
- AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
- decoded_frame->data, decoded_frame->linesize,
- AV_PERM_READ | AV_PERM_PRESERVE,
- ist->st->codec->width, ist->st->codec->height,
- ist->st->codec->pix_fmt);
-
- avfilter_copy_frame_props(fb, decoded_frame);
- fb->buf->priv = buf;
- fb->buf->free = filter_release_buffer;
-
- av_assert0(buf->refcount>0);
- buf->refcount++;
- av_buffersrc_add_ref(ist->filters[i]->filter, fb,
- AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
- AV_BUFFERSRC_FLAG_NO_COPY);
- } else
- if(av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame, 0)<0) {
- av_log(NULL, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
- exit_program(1);
- }
-
- }
-
- av_free(buffer_to_free);
- return ret;
-}
-
-static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
-{
- AVSubtitle subtitle;
- int i, ret = avcodec_decode_subtitle2(ist->st->codec,
- &subtitle, got_output, pkt);
- if (ret < 0 || !*got_output) {
- if (!pkt->size)
- sub2video_flush(ist);
- return ret;
- }
-
- rate_emu_sleep(ist);
-
- sub2video_update(ist, &subtitle, pkt->pts);
-
- for (i = 0; i < nb_output_streams; i++) {
- OutputStream *ost = output_streams[i];
-
- if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
- continue;
-
- do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle, pkt->pts);
- }
-
- avsubtitle_free(&subtitle);
- return ret;
-}
-
-/* pkt = NULL means EOF (needed to flush decoder buffers) */
-static int output_packet(InputStream *ist, const AVPacket *pkt)
-{
- int ret = 0, i;
- int got_output;
-
- AVPacket avpkt;
- if (!ist->saw_first_ts) {
- ist->dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
- ist->pts = 0;
- if (pkt != NULL && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
- ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
- ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong
- }
- ist->saw_first_ts = 1;
- }
-
- if (ist->next_dts == AV_NOPTS_VALUE)
- ist->next_dts = ist->dts;
- if (ist->next_pts == AV_NOPTS_VALUE)
- ist->next_pts = ist->pts;
-
- if (pkt == NULL) {
- /* EOF handling */
- av_init_packet(&avpkt);
- avpkt.data = NULL;
- avpkt.size = 0;
- goto handle_eof;
- } else {
- avpkt = *pkt;
- }
-
- if (pkt->dts != AV_NOPTS_VALUE) {
- ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
- if (ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
- ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
- }
-
- // while we have more to decode or while the decoder did output something on EOF
- while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
- int duration;
- handle_eof:
-
- ist->pts = ist->next_pts;
- ist->dts = ist->next_dts;
-
- if (avpkt.size && avpkt.size != pkt->size) {
- av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
- "Multiple frames in a packet from stream %d\n", pkt->stream_index);
- ist->showed_multi_packet_warning = 1;
- }
-
- switch (ist->st->codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- ret = decode_audio (ist, &avpkt, &got_output);
- break;
- case AVMEDIA_TYPE_VIDEO:
- ret = decode_video (ist, &avpkt, &got_output);
- if (avpkt.duration) {
- duration = av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);
- } else if(ist->st->codec->time_base.num != 0 && ist->st->codec->time_base.den != 0) {
- int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
- duration = ((int64_t)AV_TIME_BASE *
- ist->st->codec->time_base.num * ticks) /
- ist->st->codec->time_base.den;
- } else
- duration = 0;
-
- if(ist->dts != AV_NOPTS_VALUE && duration) {
- ist->next_dts += duration;
- }else
- ist->next_dts = AV_NOPTS_VALUE;
-
- if (got_output)
- ist->next_pts += duration; //FIXME the duration is not correct in some cases
- break;
- case AVMEDIA_TYPE_SUBTITLE:
- ret = transcode_subtitles(ist, &avpkt, &got_output);
- break;
- default:
- return -1;
- }
-
- if (ret < 0)
- return ret;
-
- avpkt.dts=
- avpkt.pts= AV_NOPTS_VALUE;
-
- // touch data and size only if not EOF
- if (pkt) {
- if(ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
- ret = avpkt.size;
- avpkt.data += ret;
- avpkt.size -= ret;
- }
- if (!got_output) {
- continue;
- }
- }
-
- /* handle stream copy */
- if (!ist->decoding_needed) {
- rate_emu_sleep(ist);
- ist->dts = ist->next_dts;
- switch (ist->st->codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
- ist->st->codec->sample_rate;
- break;
- case AVMEDIA_TYPE_VIDEO:
- if (pkt->duration) {
- ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
- } else if(ist->st->codec->time_base.num != 0) {
- int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
- ist->next_dts += ((int64_t)AV_TIME_BASE *
- ist->st->codec->time_base.num * ticks) /
- ist->st->codec->time_base.den;
- }
- break;
- }
- ist->pts = ist->dts;
- ist->next_pts = ist->next_dts;
- }
- for (i = 0; pkt && i < nb_output_streams; i++) {
- OutputStream *ost = output_streams[i];
-
- if (!check_output_constraints(ist, ost) || ost->encoding_needed)
- continue;
-
- do_streamcopy(ist, ost, pkt);
- }
-
- return 0;
-}
-
-static void print_sdp(void)
-{
- char sdp[2048];
- int i;
- AVFormatContext **avc = av_malloc(sizeof(*avc) * nb_output_files);
-
- if (!avc)
- exit_program(1);
- for (i = 0; i < nb_output_files; i++)
- avc[i] = output_files[i]->ctx;
-
- av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
- printf("SDP:\n%s\n", sdp);
- fflush(stdout);
- av_freep(&avc);
-}
-
-static int init_input_stream(int ist_index, char *error, int error_len)
-{
- InputStream *ist = input_streams[ist_index];
-
- if (ist->decoding_needed) {
- AVCodec *codec = ist->dec;
- if (!codec) {
- snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
- avcodec_get_name(ist->st->codec->codec_id), ist->file_index, ist->st->index);
- return AVERROR(EINVAL);
- }
-
- ist->dr1 = (codec->capabilities & CODEC_CAP_DR1) && !do_deinterlace;
- if (codec->type == AVMEDIA_TYPE_VIDEO && ist->dr1) {
- ist->st->codec->get_buffer = codec_get_buffer;
- ist->st->codec->release_buffer = codec_release_buffer;
- ist->st->codec->opaque = &ist->buffer_pool;
- }
-
- if (!av_dict_get(ist->opts, "threads", NULL, 0))
- av_dict_set(&ist->opts, "threads", "auto", 0);
- if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
- snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
- ist->file_index, ist->st->index);
- return AVERROR(EINVAL);
- }
- assert_codec_experimental(ist->st->codec, 0);
- assert_avoptions(ist->opts);
- }
-
- ist->next_pts = AV_NOPTS_VALUE;
- ist->next_dts = AV_NOPTS_VALUE;
- ist->is_start = 1;
-
- return 0;
-}
-
-static InputStream *get_input_stream(OutputStream *ost)
-{
- if (ost->source_index >= 0)
- return input_streams[ost->source_index];
- return NULL;
-}
-
-static void parse_forced_key_frames(char *kf, OutputStream *ost,
- AVCodecContext *avctx)
-{
- char *p;
- int n = 1, i;
- int64_t t;
-
- for (p = kf; *p; p++)
- if (*p == ',')
- n++;
- ost->forced_kf_count = n;
- ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
- if (!ost->forced_kf_pts) {
- av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
- exit_program(1);
- }
-
- p = kf;
- for (i = 0; i < n; i++) {
- char *next = strchr(p, ',');
-
- if (next)
- *next++ = 0;
-
- t = parse_time_or_die("force_key_frames", p, 1);
- ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
-
- p = next;
- }
-}
-
-static void report_new_stream(int input_index, AVPacket *pkt)
-{
- InputFile *file = input_files[input_index];
- AVStream *st = file->ctx->streams[pkt->stream_index];
-
- if (pkt->stream_index < file->nb_streams_warn)
- return;
- av_log(file->ctx, AV_LOG_WARNING,
- "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
- av_get_media_type_string(st->codec->codec_type),
- input_index, pkt->stream_index,
- pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
- file->nb_streams_warn = pkt->stream_index + 1;
-}
-
-static int transcode_init(void)
-{
- int ret = 0, i, j, k;
- AVFormatContext *oc;
- AVCodecContext *codec, *icodec = NULL;
- OutputStream *ost;
- InputStream *ist;
- char error[1024];
- int want_sdp = 1;
-
- /* init framerate emulation */
- for (i = 0; i < nb_input_files; i++) {
- InputFile *ifile = input_files[i];
- if (ifile->rate_emu)
- for (j = 0; j < ifile->nb_streams; j++)
- input_streams[j + ifile->ist_index]->start = av_gettime();
- }
-
- /* output stream init */
- for (i = 0; i < nb_output_files; i++) {
- oc = output_files[i]->ctx;
- if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
- av_dump_format(oc, i, oc->filename, 1);
- av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
- return AVERROR(EINVAL);
- }
- }
-
- /* init complex filtergraphs */
- for (i = 0; i < nb_filtergraphs; i++)
- if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0)
- return ret;
-
- /* for each output stream, we compute the right encoding parameters */
- for (i = 0; i < nb_output_streams; i++) {
- ost = output_streams[i];
- oc = output_files[ost->file_index]->ctx;
- ist = get_input_stream(ost);
-
- if (ost->attachment_filename)
- continue;
-
- codec = ost->st->codec;
-
- if (ist) {
- icodec = ist->st->codec;
-
- ost->st->disposition = ist->st->disposition;
- codec->bits_per_raw_sample = icodec->bits_per_raw_sample;
- codec->chroma_sample_location = icodec->chroma_sample_location;
- }
-
- if (ost->stream_copy) {
- uint64_t extra_size;
-
- av_assert0(ist && !ost->filter);
-
- extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
-
- if (extra_size > INT_MAX) {
- return AVERROR(EINVAL);
- }
-
- /* if stream_copy is selected, no need to decode or encode */
- codec->codec_id = icodec->codec_id;
- codec->codec_type = icodec->codec_type;
-
- if (!codec->codec_tag) {
- if (!oc->oformat->codec_tag ||
- av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
- av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
- codec->codec_tag = icodec->codec_tag;
- }
-
- codec->bit_rate = icodec->bit_rate;
- codec->rc_max_rate = icodec->rc_max_rate;
- codec->rc_buffer_size = icodec->rc_buffer_size;
- codec->field_order = icodec->field_order;
- codec->extradata = av_mallocz(extra_size);
- if (!codec->extradata) {
- return AVERROR(ENOMEM);
- }
- memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
- codec->extradata_size= icodec->extradata_size;
- codec->bits_per_coded_sample = icodec->bits_per_coded_sample;
-
- codec->time_base = ist->st->time_base;
- /*
- * Avi is a special case here because it supports variable fps but
- * having the fps and timebase differe significantly adds quite some
- * overhead
- */
- if(!strcmp(oc->oformat->name, "avi")) {
- if ( copy_tb<0 && av_q2d(ist->st->r_frame_rate) >= av_q2d(ist->st->avg_frame_rate)
- && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(ist->st->time_base)
- && 0.5/av_q2d(ist->st->r_frame_rate) > av_q2d(icodec->time_base)
- && av_q2d(ist->st->time_base) < 1.0/500 && av_q2d(icodec->time_base) < 1.0/500
- || copy_tb==2){
- codec->time_base.num = ist->st->r_frame_rate.den;
- codec->time_base.den = 2*ist->st->r_frame_rate.num;
- codec->ticks_per_frame = 2;
- } else if ( copy_tb<0 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > 2*av_q2d(ist->st->time_base)
- && av_q2d(ist->st->time_base) < 1.0/500
- || copy_tb==0){
- codec->time_base = icodec->time_base;
- codec->time_base.num *= icodec->ticks_per_frame;
- codec->time_base.den *= 2;
- codec->ticks_per_frame = 2;
- }
- } else if(!(oc->oformat->flags & AVFMT_VARIABLE_FPS)
- && strcmp(oc->oformat->name, "mov") && strcmp(oc->oformat->name, "mp4") && strcmp(oc->oformat->name, "3gp")
- && strcmp(oc->oformat->name, "3g2") && strcmp(oc->oformat->name, "psp") && strcmp(oc->oformat->name, "ipod")
- ) {
- if( copy_tb<0 && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base)
- && av_q2d(ist->st->time_base) < 1.0/500
- || copy_tb==0){
- codec->time_base = icodec->time_base;
- codec->time_base.num *= icodec->ticks_per_frame;
- }
- }
-
- if(ost->frame_rate.num)
- codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
-
- av_reduce(&codec->time_base.num, &codec->time_base.den,
- codec->time_base.num, codec->time_base.den, INT_MAX);
-
- switch (codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- if (audio_volume != 256) {
- av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
- exit_program(1);
- }
- codec->channel_layout = icodec->channel_layout;
- codec->sample_rate = icodec->sample_rate;
- codec->channels = icodec->channels;
- codec->frame_size = icodec->frame_size;
- codec->audio_service_type = icodec->audio_service_type;
- codec->block_align = icodec->block_align;
- if((codec->block_align == 1 || codec->block_align == 1152) && codec->codec_id == CODEC_ID_MP3)
- codec->block_align= 0;
- if(codec->codec_id == CODEC_ID_AC3)
- codec->block_align= 0;
- break;
- case AVMEDIA_TYPE_VIDEO:
- codec->pix_fmt = icodec->pix_fmt;
- codec->width = icodec->width;
- codec->height = icodec->height;
- codec->has_b_frames = icodec->has_b_frames;
- if (!codec->sample_aspect_ratio.num) {
- codec->sample_aspect_ratio =
- ost->st->sample_aspect_ratio =
- ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
- ist->st->codec->sample_aspect_ratio.num ?
- ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};
- }
- ost->st->avg_frame_rate = ist->st->avg_frame_rate;
- break;
- case AVMEDIA_TYPE_SUBTITLE:
- codec->width = icodec->width;
- codec->height = icodec->height;
- break;
- case AVMEDIA_TYPE_DATA:
- case AVMEDIA_TYPE_ATTACHMENT:
- break;
- default:
- abort();
- }
- } else {
- if (!ost->enc)
- ost->enc = avcodec_find_encoder(codec->codec_id);
- if (!ost->enc) {
- /* should only happen when a default codec is not present. */
- snprintf(error, sizeof(error), "Encoder (codec %s) not found for output stream #%d:%d",
- avcodec_get_name(ost->st->codec->codec_id), ost->file_index, ost->index);
- ret = AVERROR(EINVAL);
- goto dump_format;
- }
-
- if (ist)
- ist->decoding_needed = 1;
- ost->encoding_needed = 1;
-
- if (!ost->filter &&
- (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
- codec->codec_type == AVMEDIA_TYPE_AUDIO)) {
- FilterGraph *fg;
- fg = init_simple_filtergraph(ist, ost);
- if (configure_filtergraph(fg)) {
- av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
- exit(1);
- }
- }
-
- if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
- if (ost->filter && !ost->frame_rate.num)
- ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
- if (ist && !ost->frame_rate.num)
- ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};
-// ost->frame_rate = ist->st->avg_frame_rate.num ? ist->st->avg_frame_rate : (AVRational){25, 1};
- if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
- int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
- ost->frame_rate = ost->enc->supported_framerates[idx];
- }
- }
-
- switch (codec->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- codec->sample_fmt = ost->filter->filter->inputs[0]->format;
- codec->sample_rate = ost->filter->filter->inputs[0]->sample_rate;
- codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
- codec->channels = av_get_channel_layout_nb_channels(codec->channel_layout);
- codec->time_base = (AVRational){ 1, codec->sample_rate };
- break;
- case AVMEDIA_TYPE_VIDEO:
- codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
- if (ost->filter && !(codec->time_base.num && codec->time_base.den))
- codec->time_base = ost->filter->filter->inputs[0]->time_base;
- if ( av_q2d(codec->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH
- && (video_sync_method == VSYNC_CFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
- av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
- "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
- }
- for (j = 0; j < ost->forced_kf_count; j++)
- ost->forced_kf_pts[j] = av_rescale_q(ost->forced_kf_pts[j],
- AV_TIME_BASE_Q,
- codec->time_base);
-
- codec->width = ost->filter->filter->inputs[0]->w;
- codec->height = ost->filter->filter->inputs[0]->h;
- codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
- ost->frame_aspect_ratio ? // overridden by the -aspect cli option
- av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
- ost->filter->filter->inputs[0]->sample_aspect_ratio;
- codec->pix_fmt = ost->filter->filter->inputs[0]->format;
-
- if (!icodec ||
- codec->width != icodec->width ||
- codec->height != icodec->height ||
- codec->pix_fmt != icodec->pix_fmt) {
- codec->bits_per_raw_sample = frame_bits_per_raw_sample;
- }
-
- if (ost->forced_keyframes)
- parse_forced_key_frames(ost->forced_keyframes, ost,
- ost->st->codec);
- break;
- case AVMEDIA_TYPE_SUBTITLE:
- codec->time_base = (AVRational){1, 1000};
- break;
- default:
- abort();
- break;
- }
- /* two pass mode */
- if (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
- char logfilename[1024];
- FILE *f;
-
- snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
- pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
- i);
- if (!strcmp(ost->enc->name, "libx264")) {
- av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
- } else {
- if (codec->flags & CODEC_FLAG_PASS2) {
- char *logbuffer;
- size_t logbuffer_size;
- if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
- av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
- logfilename);
- exit_program(1);
- }
- codec->stats_in = logbuffer;
- }
- if (codec->flags & CODEC_FLAG_PASS1) {
- f = fopen(logfilename, "wb");
- if (!f) {
- av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
- logfilename, strerror(errno));
- exit_program(1);
- }
- ost->logfile = f;
- }
- }
- }
- }
- }
-
- /* open each encoder */
- for (i = 0; i < nb_output_streams; i++) {
- ost = output_streams[i];
- if (ost->encoding_needed) {
- AVCodec *codec = ost->enc;
- AVCodecContext *dec = NULL;
-
- if ((ist = get_input_stream(ost)))
- dec = ist->st->codec;
- if (dec && dec->subtitle_header) {
- /* ASS code assumes this buffer is null terminated so add extra byte. */
- ost->st->codec->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
- if (!ost->st->codec->subtitle_header) {
- ret = AVERROR(ENOMEM);
- goto dump_format;
- }
- memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
- ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
- }
- if (!av_dict_get(ost->opts, "threads", NULL, 0))
- av_dict_set(&ost->opts, "threads", "auto", 0);
- if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
- snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
- ost->file_index, ost->index);
- ret = AVERROR(EINVAL);
- goto dump_format;
- }
- if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
- !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
- av_buffersink_set_frame_size(ost->filter->filter,
- ost->st->codec->frame_size);
- assert_codec_experimental(ost->st->codec, 1);
- assert_avoptions(ost->opts);
- if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
- av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
- " It takes bits/s as argument, not kbits/s\n");
- extra_size += ost->st->codec->extradata_size;
-
- if (ost->st->codec->me_threshold)
- input_streams[ost->source_index]->st->codec->debug |= FF_DEBUG_MV;