I encode a video stream using ffmeg. QT Creator Shell. OC Windows 7. C ++ language. Compiler MinGW. H264 codec.

There is a function to which I give a frame to the input, and I must return the encoded byte array. Inside this function, there is a hangup when calling avcodec_encode_video2, but not on the first call, but on arbitrary. That is, on some (random) coding of the frame hangs (from 100 to 10000).

QByteArray VideoEncoder::createFrameVideoFromImage(QImage picture) { //создаСм Π½ΠΎΠ²Ρ‹ΠΉ пустой ΠΊΠ°Π΄Ρ€ ΠΈ выдСляСм ΠΏΠΎΠ΄ Π½Π΅Π³ΠΎ ΠΏΠ°ΠΌΡΡ‚ΡŒ... AVFrame* frame_source = av_frame_alloc(); //ЗаписываСм Π² этот ΠΊΠ°Π΄Ρ€ Π±ΠΈΡ‚Ρ‹ рисунка... avpicture_fill((AVPicture*)frame_source, picture.bits(), AV_PIX_FMT_RGB24, this->width_frame, this->height_frame); //Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ пустой ΠΊΠ°Π΄Ρ€, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Π±ΡƒΠ΄Π΅Ρ‚ Ρ…Ρ€Π°Π½ΠΈΡ‚ΡŒΡΡ ΠΊΠ°Π΄Ρ€ Π² YUV AVFrame* frame_dst = av_frame_alloc(); //ВыдСляСм ΠΏΠΎΠ΄ Π½Π΅Π³ΠΎ Π½ΡƒΠΆΠ½ΠΎΠ΅ количСство памяти avpicture_fill((AVPicture*)frame_dst, (uint8_t*)this->inbuffer, AV_PIX_FMT_YUV420P, this->width_frame, this->height_frame); //ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΊΠ°Π΄Ρ€Π° Π² RGB Π² ΠΊΠ°Π΄Ρ€ YUV sws_scale(this->convert_rgb_yuv, frame_source->data, frame_source->linesize, 0, this->ctx_codec_in->height, frame_dst->data, frame_dst->linesize); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΠ°ΠΊΠ΅Ρ‚, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Π±ΡƒΠ΄Π΅Ρ‚ записан Π²ΠΈΠ΄Π΅ΠΎΠΊΠ°Π΄Ρ€ AVPacket packet; av_init_packet(&packet); packet.data = NULL; packet.size = 0; packet.pts = packet.dts = AV_NOPTS_VALUE; // ΠŸΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΊΠ°Π΄Ρ€Π° Π² Π²ΠΈΠ΄Π΅ΠΎΠΊΠ°Π΄Ρ€... int nOutputSize = 0; if (avcodec_encode_video2(this->ctx_codec_in, &packet, frame_dst, &nOutputSize) < 0) { qDebug() << "VideoEncoder error"; } this->traffic += packet.size; //ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅ΠΌ ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹ΠΉ Π²ΠΈΠ΄Π΅ΠΎΠΊΠ°Π΄Ρ€ Π² массив Π±Π°ΠΉΡ‚ QByteArray data_frame = QByteArray((char*)packet.data, packet.size); //ΠžΡ‡ΠΈΡΡ‚ΠΊΠ° памяти av_frame_free(&frame_source); av_frame_free(&frame_dst); av_free_packet(&packet); return data_frame; } 

Who can help? Thank you in advance!

  • Explain how the frame from the picture gets into frame_dst ? - Cerbo
  • First, avpicture_fill puts the picture in the frame_source , then sws_scale puts the frame in the frame_dst of the frame_sourse . - Ivan Chvanov
  • Do you make a big frame conversion (will the scale change a lot)? Show the sws_getContext() call - Yaroslav
  • this->convert_rgb_yuv = sws_getContext(this->ctx_codec_in->width, this->ctx_codec_in->height, AV_PIX_FMT_RGB24, this->ctx_codec_in->width, this->ctx_codec_in->height, this->ctx_codec_in->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL); - Ivan Chvanov

0