ffmpeg.c

来自「FFmpeg是用于录制、转换和流化音频和视频的完整解决方案」· C语言 代码 · 共 1,804 行 · 第 1/5 页

C
1,804
字号
        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);        }    }        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;        }    }    /* 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;            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->time_base = icodec->time_base;                codec->width = icodec->width;                codec->height = icodec->height;                codec->has_b_frames = icodec->has_b_frames;                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)))                {                    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;                }            }        }        if(codec->codec_type == CODEC_TYPE_VIDEO){            int size= codec->width * codec->height;            bit_buffer_size= FFMAX(bit_buffer_size, 4*size);        }    }    if (!bit_buffer)        bit_buffer = av_malloc(bit_buffer_size);    if (!bit_buffer)        goto fail;    /* 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 output 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 = av_rescale_q(ist->st->start_time, ist->st->time_base, AV_TIME_BASE_Q);        if(ist->next_pts == AV_NOPTS_VALUE)             ist->next_pts=0;        if(input_files_ts_offset[ist->file_index])            ist->next_pts= AV_NOPTS_VALUE;        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;    }    /* set meta data information from input file if required */    for (i=0;i<nb_meta_data_maps;i++) {        AVFormatContext *out_file;        AVFormatContext *in_file;        int out_file_index = meta_data_maps[i].out_file;        int in_file_index = meta_data_maps[i].in_file;        if ( out

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?