<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div>I use custom IO to read is ok, but got a endless loop on call avformat_write_header().<br><br>int ffmpegConverter::open_output_file(IStream *fileStreamDataOut)<br>{<br> AVStream *out_stream;<br> AVStream *in_stream;<br> AVCodecContext *dec_ctx, *enc_ctx;<br> AVCodec *encoder;<br> int ret;<br> unsigned int i;<br><br> ofmt_ctx = NULL;<br> //avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);<br> fileStreamBufferOut = (unsigned char*)av_malloc(FILESTREAMBUFFERSZ);<br> avIOCtx2 = avio_alloc_context(fileStreamBufferOut, FILESTREAMBUFFERSZ, 0, fileStreamDataOut, FileStreamRead, FileStreamWrite, FileStreamSeek);<br> ofmt_ctx = avformat_alloc_context();<br> ofmt_ctx->pb = avIOCtx2;<br> ofmt_ctx->flags |= AVFMT_FLAG_CUSTOM_IO;<br> ofmt_ctx->oformat = av_guess_format("wav", NULL, NULL);<br><br> if (!ofmt_ctx) {<br> OutputDebugString(L"Could not create output context\n");<br> return AVERROR_UNKNOWN;<br> }<br><br><br> for (i = 0; i < ifmt_ctx->nb_streams; i++) {<br> out_stream = avformat_new_stream(ofmt_ctx, NULL);<br> if (!out_stream) {<br> OutputDebugString(L"Failed allocating output stream\n");<br> return AVERROR_UNKNOWN;<br> }<br><br> in_stream = ifmt_ctx->streams[i];<br> dec_ctx = in_stream->codec;<br> enc_ctx = out_stream->codec;<br><br> if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO<br> || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {<br> /* in this example, we choose transcoding to same codec */<br> //encoder = avcodec_find_encoder(dec_ctx->codec_id);<br> encoder = avcodec_find_encoder(AV_CODEC_ID_PCM_MULAW);<br> if (!encoder) {<br> OutputDebugString(L"Necessary encoder not found\n");<br> return AVERROR_INVALIDDATA;<br> }<br><br> /* In this example, we transcode to same properties (picture size,<br> * sample rate etc.). These properties can be changed for output<br> * streams easily using filters */<br> if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {<br> enc_ctx->height = dec_ctx->height;<br> enc_ctx->width = dec_ctx->width;<br> enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;<br> /* take first format from list of supported formats */<br> enc_ctx->pix_fmt = encoder->pix_fmts[0];<br> /* video time_base can be set to whatever is handy and supported by encoder */<br> enc_ctx->time_base = dec_ctx->time_base;<br> }<br> else {<br> enc_ctx->sample_rate = dec_ctx->sample_rate;<br> enc_ctx->channel_layout = dec_ctx->channel_layout;<br> enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);<br> /* take first format from list of supported formats */<br> enc_ctx->sample_fmt = encoder->sample_fmts[0];<br> enc_ctx->time_base = { 1, enc_ctx->sample_rate };<br> }<br><br> /* Third parameter can be used to pass settings to encoder */<br> ret = avcodec_open2(enc_ctx, encoder, NULL);<br> if (ret < 0) {<br> av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);<br> return ret;<br> }<br> }<br> else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {<br> av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);<br> return AVERROR_INVALIDDATA;<br> }<br> else {<br> /* if this stream must be remuxed */<br> ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,<br> ifmt_ctx->streams[i]->codec);<br> if (ret < 0) {<br> OutputDebugString(L"Copying stream context failed\n");<br> return ret;<br> }<br> }<br><br> if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)<br> enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;<br><br> }<br> av_dump_format(ofmt_ctx, 0, "", 1);<br> /* init muxer, write output file header */<br> ret = avformat_write_header(ofmt_ctx, NULL); //got dead <br> if (ret < 0) {<br> OutputDebugString(L"Error occurred when opening output file\n");<br> return ret;<br> }<br><br> return 0;<br>}<br><br></div></div><br><br><span title="neteasefooter"><p> </p></span>