📄 ffmpeg.c
字号:
break; case CODEC_TYPE_VIDEO: ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL); video_size += ret; if(enc->coded_frame && enc->coded_frame->key_frame) pkt.flags |= PKT_FLAG_KEY; if (ost->logfile && enc->stats_out) { fprintf(ost->logfile, "%s", enc->stats_out); } break; default: ret=-1; } if(ret<=0) break; pkt.data= bit_buffer; pkt.size= ret; if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE) pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); av_interleaved_write_frame(os, &pkt); } } } } } return 0; fail_decode: return -1;}/* * The following code is the main loop of the file converter */static int av_encode(AVFormatContext **output_files, int nb_output_files, AVFormatContext **input_files, int nb_input_files, AVStreamMap *stream_maps, int nb_stream_maps){ int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0; AVFormatContext *is, *os; AVCodecContext *codec, *icodec; AVOutputStream *ost, **ost_table = NULL; AVInputStream *ist, **ist_table = NULL; AVInputFile *file_table; AVFormatContext *stream_no_data; int key; file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile)); if (!file_table) goto fail; /* input stream init */ j = 0; for(i=0;i<nb_input_files;i++) { is = input_files[i]; file_table[i].ist_index = j; file_table[i].nb_streams = is->nb_streams; j += is->nb_streams; } nb_istreams = j; ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *)); if (!ist_table) goto fail; for(i=0;i<nb_istreams;i++) { ist = av_mallocz(sizeof(AVInputStream)); if (!ist) goto fail; ist_table[i] = ist; } j = 0; for(i=0;i<nb_input_files;i++) { is = input_files[i]; for(k=0;k<is->nb_streams;k++) { ist = ist_table[j++]; ist->st = is->streams[k]; ist->file_index = i; ist->index = k; ist->discard = 1; /* the stream is discarded by default (changed later) */ if (ist->st->codec->rate_emu) { ist->start = av_gettime(); ist->frame = 0; } } } /* output stream init */ nb_ostreams = 0; for(i=0;i<nb_output_files;i++) { os = output_files[i]; nb_ostreams += os->nb_streams; } if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) { fprintf(stderr, "Number of stream maps must match number of output streams\n"); exit(1); } /* Sanity check the mapping args -- do the input files & streams exist? */ for(i=0;i<nb_stream_maps;i++) { int fi = stream_maps[i].file_index; int si = stream_maps[i].stream_index; if (fi < 0 || fi > nb_input_files - 1 || si < 0 || si > file_table[fi].nb_streams - 1) { fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si); exit(1); } fi = stream_maps[i].sync_file_index; si = stream_maps[i].sync_stream_index; if (fi < 0 || fi > nb_input_files - 1 || si < 0 || si > file_table[fi].nb_streams - 1) { fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si); exit(1); } } ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams); if (!ost_table) goto fail; for(i=0;i<nb_ostreams;i++) { ost = av_mallocz(sizeof(AVOutputStream)); if (!ost) goto fail; ost_table[i] = ost; } n = 0; for(k=0;k<nb_output_files;k++) { os = output_files[k]; for(i=0;i<os->nb_streams;i++) { int found; ost = ost_table[n++]; ost->file_index = k; ost->index = i; ost->st = os->streams[i]; if (nb_stream_maps > 0) { ost->source_index = file_table[stream_maps[n-1].file_index].ist_index + stream_maps[n-1].stream_index; /* Sanity check that the stream types match */ if (ist_table[ost->source_index]->st->codec->codec_type != ost->st->codec->codec_type) { fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n", stream_maps[n-1].file_index, stream_maps[n-1].stream_index, ost->file_index, ost->index); exit(1); } } else { /* get corresponding input stream index : we select the first one with the right type */ found = 0; for(j=0;j<nb_istreams;j++) { ist = ist_table[j]; if (ist->discard && ist->st->codec->codec_type == ost->st->codec->codec_type) { ost->source_index = j; found = 1; break; } } if (!found) { /* try again and reuse existing stream */ for(j=0;j<nb_istreams;j++) { ist = ist_table[j]; if (ist->st->codec->codec_type == ost->st->codec->codec_type) { ost->source_index = j; found = 1; } } if (!found) { fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n", ost->file_index, ost->index); exit(1); } } } ist = ist_table[ost->source_index]; ist->discard = 0; ost->sync_ist = (nb_stream_maps > 0) ? ist_table[file_table[stream_maps[n-1].sync_file_index].ist_index + stream_maps[n-1].sync_stream_index] : ist; } } /* 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; if(!codec->codec_tag) codec->codec_tag = icodec->codec_tag; codec->bit_rate = icodec->bit_rate; codec->extradata= icodec->extradata; codec->extradata_size= icodec->extradata_size; codec->time_base = icodec->time_base; switch(codec->codec_type) { case CODEC_TYPE_AUDIO: codec->sample_rate = icodec->sample_rate; codec->channels = icodec->channels; codec->frame_size = icodec->frame_size; codec->block_align= icodec->block_align; break; case CODEC_TYPE_VIDEO: codec->pix_fmt = icodec->pix_fmt; codec->width = icodec->width; codec->height = icodec->height; codec->has_b_frames = icodec->has_b_frames; break; case CODEC_TYPE_SUBTITLE: 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 || icodec->codec_id == CODEC_ID_DTS)) { /* Special case for 5:1 AC3 and DTS 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)) && (frame_rightBand + frame_leftBand + 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)) && (frame_padright + frame_padleft + 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( codec->width, codec->height, icodec->width, icodec->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; case CODEC_TYPE_SUBTITLE: ost->encoding_needed = 1; ist->decoding_needed = 1; break; default: av_abort(); break; } /* 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -