video-processingvideo-capturelive555

Can't parse a h265 stream


I'm trying to parse a stream from a h265 camera. The live555 library give me frames which I get in afterGettingFrame(). There I parse them to get NALUs by their header "0x00 0x00 0x00 0x01". After I parse the next byte of NALU and get is it "I" frame or not:

  auto t = (buf[0] >> 1) & 0x3f;
  return t == 19 || t == 20;

And I get an example stream of NALUs:

log info Frame: time=1691746190593, key=true, duration=25230, size=50338
log info Frame: time=1691746190593, key=true, duration=3163, size=3464
log info Frame: time=1691746190713, key=false, duration=37765, size=480
log info Frame: time=1691746190713, key=false, duration=998, size=3274
log info Frame: time=1691746190713, key=false, duration=711, size=420
log info Frame: time=1691746190793, key=false, duration=115679, size=322
log info Frame: time=1691746190793, key=false, duration=1533, size=2437
log info Frame: time=1691746190793, key=false, duration=842, size=286
log info Frame: time=1691746190913, key=false, duration=77556, size=287
log info Frame: time=1691746190913, key=false, duration=1322, size=2693
log info Frame: time=1691746190913, key=false, duration=2059, size=216
log info Frame: time=1691746190993, key=false, duration=117100, size=258
log info Frame: time=1691746190993, key=false, duration=3076, size=2486
log info Frame: time=1691746190993, key=false, duration=396, size=211
log info Frame: time=1691746191113, key=false, duration=81118, size=23
log info Frame: time=1691746191113, key=false, duration=377, size=64
log info Frame: time=1691746191113, key=false, duration=3962, size=10
log info Frame: time=1691746191113, key=true, duration=5140, size=5069
log info Frame: time=1691746191113, key=true, duration=2797, size=49916
log info Frame: time=1691746191113, key=true, duration=1949, size=3493
log info Frame: time=1691746191193, key=false, duration=101536, size=429
log info Frame: time=1691746191193, key=false, duration=1158, size=3186
log info Frame: time=1691746191193, key=false, duration=1146, size=350
log info Frame: time=1691746191313, key=false, duration=77701, size=332

Here there are the frames with the same timestamp, but as far as I know, it's OK. Futher, what should I do with them (frames with the same timestamp)? There are some ways: to concate them by timestamp or to parse by one. I tried both ways without success. Decode as usual (which works for h264):

  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)
    {
      break;
    }

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

So, this code returns gotFrame=true, I make a jpeg and get a picture half green! What is the right way to parse such a stream?


Solution

  • Finally got it. When I parsed the frames from live555, I removed the first header "0001", so result concataned frame didn't have them, only from the first NALU. But decoder demands to have all the NALU's headers, including the intermediate ones.