[Libav-user] custom IO write header error

yzf.zisun yzf.zisun at 163.com
Fri Nov 20 07:57:43 CET 2015


I use custom IO to read  is ok, but got a endless loop on call  avformat_write_header().

int ffmpegConverter::open_output_file(IStream *fileStreamDataOut)
{
    AVStream *out_stream;
    AVStream *in_stream;
    AVCodecContext *dec_ctx, *enc_ctx;
    AVCodec *encoder;
    int ret;
    unsigned int i;

    ofmt_ctx = NULL;
    //avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
    fileStreamBufferOut = (unsigned char*)av_malloc(FILESTREAMBUFFERSZ);
    avIOCtx2 = avio_alloc_context(fileStreamBufferOut, FILESTREAMBUFFERSZ, 0, fileStreamDataOut, FileStreamRead, FileStreamWrite, FileStreamSeek);
    ofmt_ctx = avformat_alloc_context();
    ofmt_ctx->pb = avIOCtx2;
    ofmt_ctx->flags |= AVFMT_FLAG_CUSTOM_IO;
    ofmt_ctx->oformat = av_guess_format("wav", NULL, NULL);

    if (!ofmt_ctx) {
        OutputDebugString(L"Could not create output context\n");
        return AVERROR_UNKNOWN;
    }


    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            OutputDebugString(L"Failed allocating output stream\n");
            return AVERROR_UNKNOWN;
        }

        in_stream = ifmt_ctx->streams[i];
        dec_ctx = in_stream->codec;
        enc_ctx = out_stream->codec;

        if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
            || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
            /* in this example, we choose transcoding to same codec */
            //encoder = avcodec_find_encoder(dec_ctx->codec_id);
            encoder = avcodec_find_encoder(AV_CODEC_ID_PCM_MULAW);
            if (!encoder) {
                OutputDebugString(L"Necessary encoder not found\n");
                return AVERROR_INVALIDDATA;
            }

            /* In this example, we transcode to same properties (picture size,
            * sample rate etc.). These properties can be changed for output
            * streams easily using filters */
            if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
                enc_ctx->height = dec_ctx->height;
                enc_ctx->width = dec_ctx->width;
                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
                /* take first format from list of supported formats */
                enc_ctx->pix_fmt = encoder->pix_fmts[0];
                /* video time_base can be set to whatever is handy and supported by encoder */
                enc_ctx->time_base = dec_ctx->time_base;
            }
            else {
                enc_ctx->sample_rate = dec_ctx->sample_rate;
                enc_ctx->channel_layout = dec_ctx->channel_layout;
                enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
                /* take first format from list of supported formats */
                enc_ctx->sample_fmt = encoder->sample_fmts[0];
                enc_ctx->time_base =  { 1, enc_ctx->sample_rate };
            }

            /* Third parameter can be used to pass settings to encoder */
            ret = avcodec_open2(enc_ctx, encoder, NULL);
            if (ret < 0) {
                av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
                return ret;
            }
        }
        else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
            av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
            return AVERROR_INVALIDDATA;
        }
        else {
            /* if this stream must be remuxed */
            ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
                ifmt_ctx->streams[i]->codec);
            if (ret < 0) {
                OutputDebugString(L"Copying stream context failed\n");
                return ret;
            }
        }

        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

    }
    av_dump_format(ofmt_ctx, 0, "", 1);
    /* init muxer, write output file header */
    ret = avformat_write_header(ofmt_ctx, NULL);  //got dead  
    if (ret < 0) {
        OutputDebugString(L"Error occurred when opening output file\n");
        return ret;
    }

    return 0;
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20151120/e422a80d/attachment.html>


More information about the Libav-user mailing list