[Libav-user] Assertion while calling avcodec_send_frame

Udo Pokojski ffmpeg at upokojski.de
Thu Dec 7 22:18:36 EET 2023


Hello,

I am trying to encode one single video frame as a jpg image.

This my code:
--------------------------------------------------
void saveFrame(AVFrame *pFrame,char *filename, int i)
{
     // pFrame has pix_fmt  AV_PIX_FMT_YUV420P
     int ret;
     FILE *JPEGFile;
     AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
     if (!jpegCodec) {
         return;
     }
     AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
     if (!jpegContext) {
fprintf(stderr,"Error getting encoder context\n");
         return;
     }
     AVPacket* packet = av_packet_alloc();
  jpegContext->height = pFrame->height;
  jpegContext->width = pFrame->width;
  jpegContext->pix_fmt = AV_PIX_FMT_YUVJ420P;
  jpegContext->time_base = (AVRational){1,25};
  jpegContext->framerate = (AVRational){25,1};
  jpegContext->qcompress = 0.5;
     if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
fprintf(stderr,"Error opening encoder\n");
         return;
     }
     ret = avcodec_send_frame(jpegContext,pFrame);
     if (ret < 0)
     {
write_avErrorMsg(ret,"saveFrame");
         return;
     }
     ret = avcodec_receive_packet(jpegContext,packet);
         if (ret < 0)
     {
write_avErrorMsg(ret,"saveFrame");
          return;
     }
     JPEGFile = fopen(filename, "wb");
  fwrite(packet->data, 1, packet->size, JPEGFile);
     fclose(JPEGFile);
  av_packet_unref(packet);
  avcodec_close(jpegContext);
     return;
-----------------------------------------

Calling avcodec_send_frame results in this error:
Assertion ((src_linesize) >= 0 ? (src_linesize) : (-(src_linesize))) >= 
bytewidth failed at src/libavutil/imgutils.c:349

Something seems to be wrong with the source (pFrame in my example). I 
assume an issue with the bytewidth, because the values of 
pFrame->linesize are correct.
How can I fix this problem?

Thanks in advance,
Udo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20231207/d1bfe683/attachment.htm>


More information about the Libav-user mailing list