3

I am currently using nvenc to decode a H264 file and I require random access to the file. So, what I did for test purposes was make every frame a IDR frame. Now, if I know the file offset of the frame in the H264 file. Is it enough to just seek to that position, read the amount of bytes that was written for that frame and pass that to the decoder. My understanding is that the decoder should be able to handle just a single IDR frame and be able to decode it. Is that correct?

Luca
  • 10,458
  • 24
  • 107
  • 234
  • I have to say, I would never have thought of using H.264 in that way - we also need random access to our video files, but just use FFMPEG to transcode our H.264 files to MJPEG (Motion JPEG). But I'm not saying that your approach wouldn't work! However: you've tagged your question with `nvenc` - that's NVidia's *en*coder hardware, it does not *de*code H.264. Did you set a wrong tag, or did I misunderstand the question? – JPNotADragon Jan 11 '17 at 23:06
  • To answer your question though (assuming I didn't misunderstand): yes, in my opinion, it should work. It should be easy to test, too: just open your file with VideoLAN and jump around randomly. If you can do that indefinitely without seeing any distortions, you're good. – JPNotADragon Jan 11 '17 at 23:28
  • I made a small hack by basically jumping to the closest IDR frame and decoding from there which seems to work fine as well. – Luca Jan 12 '17 at 08:52
  • @JPNotADragon I did not see a nvdec tag option unfortunately :( – Luca Jan 12 '17 at 08:54
  • I wasn't even aware that a thing called NVDEC actually exists - of course NVidia has a hardware decoder, I just never knew what it was called. It's probably best to wait for a moderator to create the new tag and edit your question. – JPNotADragon Jan 12 '17 at 11:33
  • So, you no longer use a file consisting of IDR frames only? (I'd be interested to know what your hack consists of then.) – JPNotADragon Jan 12 '17 at 11:36
  • @JPNotADragon So, basically given a frame number, I find the closest IDR frame preceding it. I then start decoding from it till I have decoded the frame I need. Supposedly, I have an IDR frame every 15 frames then in the worst case, I would need to decode 14 extra frames but this seemed like a good compromise between compression and being able to do random access. – Luca Jan 12 '17 at 11:54
  • Thanks! So you have IDR frames at fixed intervals, meaning that finding the preceding IDR frame for any given frame does not involve scanning? – JPNotADragon Jan 12 '17 at 12:11
  • Yes, that is correct. So, I basically force an IDR frame every n number of frames rather than every frame. I also keep track of the byte offset for every frame (during the encoding process). So, I know where to jump in the file. This is the bit where I have a luxury as the encoding and decoding would need to be done with my application (which is the case for me). However, if you want to use a random H264 file, this byte offset map will need to be rebuilt once (as each frame can have variable length) – Luca Jan 12 '17 at 13:31
  • I did not realize in my first comments that you were using specific hardware to do the decoding, so I didn't quite understand what the difficulty was - of course you need the byte position if you feed the decoder yourself rather than relying on something like FFMPEG to seek by frame index, as we do. (I've never tried to seek to specific frames in a simple H.264 dump - that would probably not work even with FFMPEG). Thanks! Out of interest, why use NVDEC directly - doesn't FFMPEG use it, or is that lib too big for your needs? – JPNotADragon Jan 12 '17 at 13:56
  • @JPNotADragon FFMPEG would work and that was my first choice but the client did not want to have that dependency. So I wrote a simple wrapper around nvenc and nvdec. – Luca Jan 12 '17 at 13:59
  • Thanks! (Have to say though - I didn't think NVENC was all that simple :-) ) – JPNotADragon Jan 12 '17 at 15:26
  • @JPNotADragon Well, if they ever allow me, I can try and make it open source. NVidia's example code was a bit helpful. – Luca Jan 12 '17 at 15:46

0 Answers0