📄 ffmpeg.c
字号:
/* for each output stream, we compute the right encoding parameters */ for(i=0;i<nb_ostreams;i++) { ost = ost_table[i]; ist = ist_table[ost->source_index]; codec = &ost->st->codec; icodec = &ist->st->codec; if (ost->st->stream_copy) { /* if stream_copy is selected, no need to decode or encode */ codec->codec_id = icodec->codec_id; codec->codec_type = icodec->codec_type; codec->codec_tag = icodec->codec_tag; codec->bit_rate = icodec->bit_rate; switch(codec->codec_type) { case CODEC_TYPE_AUDIO: codec->sample_rate = icodec->sample_rate; codec->channels = icodec->channels; codec->frame_size = icodec->frame_size; break; case CODEC_TYPE_VIDEO: codec->frame_rate = icodec->frame_rate; codec->frame_rate_base = icodec->frame_rate_base; codec->width = icodec->width; codec->height = icodec->height; break; default: av_abort(); } } else { switch(codec->codec_type) { case CODEC_TYPE_AUDIO: if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE)) goto fail; if (codec->channels == icodec->channels && codec->sample_rate == icodec->sample_rate) { ost->audio_resample = 0; } else { if (codec->channels != icodec->channels && icodec->codec_id == CODEC_ID_AC3) { /* Special case for 5:1 AC3 input */ /* and mono or stereo output */ /* Request specific number of channels */ icodec->channels = codec->channels; if (codec->sample_rate == icodec->sample_rate) ost->audio_resample = 0; else { ost->audio_resample = 1; } } else { ost->audio_resample = 1; } } if(audio_sync_method>1) ost->audio_resample = 1; if(ost->audio_resample){ ost->resample = audio_resample_init(codec->channels, icodec->channels, codec->sample_rate, icodec->sample_rate); if(!ost->resample){ printf("Can't resample. Aborting.\n"); av_abort(); } } ist->decoding_needed = 1; ost->encoding_needed = 1; break; case CODEC_TYPE_VIDEO: if (codec->width == icodec->width && codec->height == icodec->height && frame_topBand == 0 && frame_bottomBand == 0 && frame_leftBand == 0 && frame_rightBand == 0 && frame_padtop == 0 && frame_padbottom == 0 && frame_padleft == 0 && frame_padright == 0) { ost->video_resample = 0; ost->video_crop = 0; ost->video_pad = 0; } else if ((codec->width == icodec->width - (frame_leftBand + frame_rightBand)) && (codec->height == icodec->height - (frame_topBand + frame_bottomBand))) { ost->video_resample = 0; ost->video_crop = 1; ost->topBand = frame_topBand; ost->leftBand = frame_leftBand; } else if ((codec->width == icodec->width + (frame_padleft + frame_padright)) && (codec->height == icodec->height + (frame_padtop + frame_padbottom))) { ost->video_resample = 0; ost->video_crop = 0; ost->video_pad = 1; ost->padtop = frame_padtop; ost->padleft = frame_padleft; ost->padbottom = frame_padbottom; ost->padright = frame_padright; avcodec_get_frame_defaults(&ost->pict_tmp); if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P, codec->width, codec->height ) ) goto fail; } else { ost->video_resample = 1; ost->video_crop = 0; // cropping is handled as part of resample avcodec_get_frame_defaults(&ost->pict_tmp); if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P, codec->width, codec->height ) ) goto fail; ost->img_resample_ctx = img_resample_full_init( ost->st->codec.width, ost->st->codec.height, ist->st->codec.width, ist->st->codec.height, frame_topBand, frame_bottomBand, frame_leftBand, frame_rightBand, frame_padtop, frame_padbottom, frame_padleft, frame_padright); ost->padtop = frame_padtop; ost->padleft = frame_padleft; ost->padbottom = frame_padbottom; ost->padright = frame_padright; } ost->encoding_needed = 1; ist->decoding_needed = 1; break; default: av_abort(); } /* two pass mode */ if (ost->encoding_needed && (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) { char logfilename[1024]; FILE *f; int size; char *logbuffer; snprintf(logfilename, sizeof(logfilename), "%s-%d.log", pass_logfilename ? pass_logfilename : DEFAULT_PASS_LOGFILENAME, i); if (codec->flags & CODEC_FLAG_PASS1) { f = fopen(logfilename, "w"); if (!f) { perror(logfilename); exit(1); } ost->logfile = f; } else { /* read the log file */ f = fopen(logfilename, "r"); if (!f) { perror(logfilename); exit(1); } fseek(f, 0, SEEK_END); size = ftell(f); fseek(f, 0, SEEK_SET); logbuffer = av_malloc(size + 1); if (!logbuffer) { fprintf(stderr, "Could not allocate log buffer\n"); exit(1); } size = fread(logbuffer, 1, size, f); fclose(f); logbuffer[size] = '\0'; codec->stats_in = logbuffer; } } } } /* dump the file output parameters - cannot be done before in case of stream copy */ for(i=0;i<nb_output_files;i++) { dump_format(output_files[i], i, output_files[i]->filename, 1); } /* dump the stream mapping */ if (verbose >= 0) { fprintf(stderr, "Stream mapping:\n"); for(i=0;i<nb_ostreams;i++) { ost = ost_table[i]; fprintf(stderr, " Stream #%d.%d -> #%d.%d\n", ist_table[ost->source_index]->file_index, ist_table[ost->source_index]->index, ost->file_index, ost->index); } } /* open each encoder */ for(i=0;i<nb_ostreams;i++) { ost = ost_table[i]; if (ost->encoding_needed) { AVCodec *codec; codec = avcodec_find_encoder(ost->st->codec.codec_id); if (!codec) { fprintf(stderr, "Unsupported codec for output stream #%d.%d\n", ost->file_index, ost->index); exit(1); } if (avcodec_open(&ost->st->codec, codec) < 0) { fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n", ost->file_index, ost->index); exit(1); } extra_size += ost->st->codec.extradata_size; } } /* open each decoder */ for(i=0;i<nb_istreams;i++) { ist = ist_table[i]; if (ist->decoding_needed) { AVCodec *codec; codec = avcodec_find_decoder(ist->st->codec.codec_id); if (!codec) { fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n", ist->st->codec.codec_id, ist->file_index, ist->index); exit(1); } if (avcodec_open(&ist->st->codec, codec) < 0) { fprintf(stderr, "Error while opening codec for input stream #%d.%d\n", ist->file_index, ist->index); exit(1); } //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD; } } /* init pts */ for(i=0;i<nb_istreams;i++) { ist = ist_table[i]; is = input_files[ist->file_index]; ist->pts = 0; ist->next_pts = 0; ist->is_start = 1; } /* compute buffer size max (should use a complete heuristic) */ for(i=0;i<nb_input_files;i++) { file_table[i].buffer_size_max = 2048; } /* open files and write file headers */ for(i=0;i<nb_output_files;i++) { os = output_files[i]; if (av_write_header(os) < 0) { fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i); ret = -EINVAL; goto fail; } }#ifndef CONFIG_WIN32 if ( !using_stdin && verbose >= 0) { fprintf(stderr, "Press [q] to stop encoding\n"); url_set_interrupt_cb(decode_interrupt_cb); }#endif term_init(); stream_no_data = 0; key = -1; for(; received_sigterm == 0;) { int file_index, ist_index; AVPacket pkt; double ipts_min= 1e100; double opts_min= 1e100; redo: /* if 'q' pressed, exits */ if (!using_stdin) { if (q_pressed) break; /* read_key() returns 0 on EOF */ key = read_key(); if (key == 'q') break; } /* select the stream that we must read now by looking at the smallest output pts */ file_index = -1; for(i=0;i<nb_ostreams;i++) { double ipts, opts; ost = ost_table[i]; os = output_files[ost->file_index]; ist = ist_table[ost->source_index]; if(ost->st->codec.codec_type == CODEC_TYPE_VIDEO) opts = (double)ost->sync_opts * ost->st->codec.frame_rate_base / ost->st->codec.frame_rate; else opts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den; ipts = (double)ist->pts; if (!file_table[ist->file_index].eof_reached){ if(ipts < ipts_min) { ipts_min = ipts; if(input_sync ) file_index = ist->file_index; } if(opts < opts_min) { opts_min = opts; if(!input_sync) file_index = ist->file_index; } } } /* if none, if is finished */ if (file_index < 0) { break; } /* finish if recording time exhausted */ if (recording_time > 0 && opts_min >= (recording_time / 1000000.0)) break; /* read a frame from it and output it in the fifo */ is = input_files[file_index]; if (av_read_frame(is, &pkt) < 0) { file_table[file_index].eof_reached = 1; continue; } if (!pkt.size) { stream_no_data = is; } else { stream_no_data = 0; } if (do_pkt_dump) { av_pkt_dump(stdout, &pkt, do_hex_dump); } /* the following test is needed in case new streams appear dynamically in stream : we ignore them */ if (pkt.stream_index >= file_table[file_index].nb_streams) goto discard_packet; ist_index = file_table[file_index].ist_index + pkt.stream_index; ist = ist_table[ist_index]; if (ist->discard) goto discard_packet;// fprintf(stderr, "next:%lld dts:%lld off:%lld %d\n", ist->next_pts, pkt.dts, input_files_ts_offset[ist->file_index], ist->st->codec.codec_type); if (pkt.dts != AV_NOPTS_VALUE) { int64_t delta= pkt.dts - ist->next_pts; if(ABS(delta) > 10LL*AV_TIME_BASE && !copy_ts){ input_files_ts_offset[ist->file_index]-= delta; if (verbose > 2) fprintf(stderr, "timestamp discontinuity %lld, new offset= %lld\n", delta, input_files_ts_offset[ist->file_index]); for(i=0; i<file_table[file_index].nb_streams; i++){ int index= file_table[file_index].ist_index + i; ist_table[index]->next_pts += delta; ist_table[index]->is_start=1; } } } //fprintf(stderr,"read #%d.%d size=%d\n", ist->fi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -