Home > Back-end >  Help: FFmpeg transfer YUV H264 write files show abnormal
Help: FFmpeg transfer YUV H264 write files show abnormal

Time:12-30

 static int encode2 (AVFormatContext * fmtCtx, AVCodecContext * enc_ctx, AVFrame * frame, AVPacket * PKT, FILE * outfile) 
{
Int ret=0;
If (0 & gt; Avcodec_send_frame (enc_ctx, frame))
return -1;

While (true)
{
Ret=avcodec_receive_packet (enc_ctx PKT);
If (ret==AVERROR (EAGAIN) | | ret==AVERROR_EOF)
break;
Else if (ret & lt; 0)
return -1;
PKT - & gt; DTS=PKT - & gt; PTS;
Ret=av_write_frame (fmtCtx PKT);
Av_packet_unref (PKT);
//fwrite (PKT - & gt; Data, 1 PKT - & gt; The size, outfile);
}
return 0;
}

Bool yuv_to_h264_2 (const char * szInput, const char * szOutput)
{
Int ret=0;
The FILE * infile=nullptr, * outfile=nullptr;

AVCodecContext * pCodecCtx=avcodec_alloc_context3 (nullptr);

AVFormatContext * pOutputFmtCtx=nullptr;
Ret=avformat_alloc_output_context2 (& amp; POutputFmtCtx nullptr, nullptr szOutput);
AVOutputFormat * pFmt=pOutputFmtCtx - & gt; Oformat;

Ret=avio_open2 (& amp; POutputFmtCtx - & gt; Pb, szOutput AVIO_FLAG_READ_WRITE nullptr, nullptr);

AVStream * pVideoStream=avformat_new_stream (pOutputFmtCtx nullptr);
PVideoStream - & gt; Engl j med time_base={1};
Avcodec_parameters_to_context (pCodecCtx pVideoStream - & gt; Codecpar);

//set the encoding parameter
PCodecCtx - & gt; Codec_id=pFmt - & gt; Video_codec;
PCodecCtx - & gt; Codec_type=AVMEDIA_TYPE_VIDEO;
PCodecCtx - & gt; Width=360;
PCodecCtx - & gt; Height=270;
PCodecCtx - & gt; Engl j med time_base={1};
PCodecCtx - & gt; Framerate={25, 1};
PCodecCtx - & gt; Gop_size=250;
PCodecCtx - & gt; Max_b_frames=3;
PCodecCtx - & gt; Pix_fmt=AV_PIX_FMT_YUV420P;
PCodecCtx - & gt; Qmin=10;
PCodecCtx - & gt; Qmax=51;
PCodecCtx - & gt; Bit_rate=400000;

AVDictionary * param=0;
If (pCodecCtx - & gt; Codec_id==AV_CODEC_ID_H264)
{
Ret=av_dict_set (& amp; Param, "preset", "missile", 0);
Ret=av_dict_set (& amp; Param, "most cerebral sci-film", "zerolatency", 0);
}

Av_dump_format (szOutput pOutputFmtCtx, 0, 1);

Const AVCodec * pCodec=avcodec_find_encoder (pCodecCtx - & gt; Codec_id);
if (! PCodec)
return false;

Ret=avcodec_open2 (pCodecCtx pCodec, & amp; Param);
If (ret & lt; 0)
return false;

AVPacket * PKT=av_packet_alloc ();
AVFrame * frame=av_frame_alloc ();

Frame - & gt; Format=pCodecCtx - & gt; Pix_fmt;
Frame - & gt; Width=pCodecCtx - & gt; Width;
Frame - & gt; Height=pCodecCtx - & gt; Height;
If (0 & gt; Av_frame_get_buffer (frame, 0))
return false;

Int frameBytes=av_image_get_buffer_size (pCodecCtx - & gt; Pix_fmt, frame - & gt; Width, frame - & gt; Height, 1);
Uint8_t * yuvBuf=(uint8_t *) av_malloc (frameBytes);

Int64_t beginTime=get_time ();
Int64_t endTime=beginTime;
Int64_t allBeginTime=get_time ();
Int64_t allEndTime=allBeginTime;
Int64_t PTS=0;

Avformat_write_header (pOutputFmtCtx nullptr);

Fopen_s (& amp; Infile szInput, "rb");
if (! Infile)
return false;

for (int i=0; ; I++)
{
Memset (yuvBuf, 0, frameBytes);
If (0 & gt; Fread (yuvBuf, 1, frameBytes, infile))
break;
Else if (feof (infile))
break;

Ret=av_frame_make_writable (frame);

If (ret!=0) {
: : printf (" av_frame_make_writable failed, ret=% d \ n ", ret);
break;
}
Int needSize=av_image_fill_arrays (frame - & gt; The data frame - & gt; Linesize yuvBuf, pCodecCtx - & gt; Pix_fmt, frame - & gt; Width, frame - & gt; Height, 1);
If (needSize!=frameBytes)
{
break;
}

PTS +=40;
Frame - & gt; PTS=PTS;
BeginTime +=get_time ();
Ret=encode2 (pOutputFmtCtx pCodecCtx, frame, PKT, outfile);
EndTime=get_time ();
: : printf (" encode time: % I64dms \ r \ n ", the endTime - beginTime);
If (ret & lt; 0)
break;
}

Encode2 (pOutputFmtCtx pCodecCtx, nullptr PKT, outfile);
AllEndTime=get_time ();
Printf (" all encode time: % I64dms \ r \ n ", allEndTime - allBeginTime);

Av_write_trailer (pOutputFmtCtx);

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The fclose (infile);
If (yuvBuf)
Av_free (yuvBuf);
Avcodec_close (pCodecCtx);
Av_frame_free (& amp; Frame);
Av_packet_free (& amp; PKT);
Avio_close (pOutputFmtCtx - & gt; Pb);
Avcodec_free_context (& amp; PCodecCtx);

Printf (" main finish, both please enter enter to exit. \ r \ n ");
return true;
}

Seek advice me this directly with the new API in C write after the file is no problem for use av_write_frame () implementation will collapse and give some directions where is the problem
  • Related