[FFmpeg-devel] Decoding slice based frames

d.gordenin at ngslab.ru d.gordenin at ngslab.ru
Wed Mar 5 14:40:59 EET 2025


Hi

My decoder receives slice based frame, and it cannot decode them (the 
result is a greased picture). How should I decode them and which are 
differences from decoding a usual frame? My code of dedoding is canonical:

   if (size >= sizeof(kh264Prefix) && memcmp(data, kh264Prefix, 
sizeof(kh264Prefix)) != 0)
   {
     if (size > prefix_buffer_.size() - sizeof(kh264Prefix))
     {
       logger_->Error("Too large data packet for decoding");
       return false;
     }

     memcpy(prefix_buffer_.data() + sizeof(kh264Prefix), data, size);

     data = prefix_buffer_.data();
     size += sizeof(kh264Prefix);
   }

   AVPacket avpkt;
   av_init_packet(&avpkt);
   avpkt.pts = 0;
   avpkt.dts = 0;
   avpkt.data = data;
   avpkt.size = size;

   int gotFrame = false;

   while (avpkt.size > 0)
   {
     // We must use positive result to move through the data buffer
     auto len = avcodec_decode_video2(av_context_, av_frame_, &gotFrame, 
&avpkt);
     if (len < 0)
     {
       // In case of an error/warning FFmpeg will call
       // decoder::LogCallback(...) to inform about the issue
       break;
     }

     avpkt.data += len;
     avpkt.size -= len;
   }

   av_free_packet(&avpkt);

I tried the AV_CODEC_CAP_SLICE_THREADS flag, not sure is it for decoding 
or not.



More information about the ffmpeg-devel mailing list