atiti
Posts: 6
Joined: Wed Aug 14, 2013 5:34 pm

Order of operations for video encoding

Mon Mar 25, 2019 1:28 pm

Hi all,

I'm in a bit of a pickle to try to understand how the video encoding process should work. Sorry if this question has already been covered before, I just couldn't find any reasonable thread here about it.

What I'm doing is:

Code: Select all

//... All the init & enable code...
//then a loop which does:
while (1) {
      buf = ilclient_get_input_buffer(private_data->video_encode, 200, 1);
      // check if buf is not null...
      memcpy(buf->pBuffer, my_raw_frame, my_raw_frame_size);
      buf->nFilledLen = my_raw_frame_size;
      if (OMX_EmptyThisBuffer(ILC_GET_HANDLE(private_data->video_encode), buf) != OMX_ErrorNone) {
          // Handle error
       }
       
       out = ilclient_get_output_buffer(private_data->video_encode, 201, 1);
       r = OMX_FillThisBuffer(ILC_GET_HANDLE(private_data->video_encode), out);
       // Check if r != OMX_ErrorNone
       // Check if out == NULL
       // Check if out->nFilledLen == 0
       
       memcpy(my_nal_output, out->pBuffer, out->nFilledLen);
       out->nFilledLen = 0;
       
       // do whatever with my_nal_output   
}
However, it seems that out->nFilledLen is always 0?
When I looked at the example of hello_encode.c, this is the order of operations, however when I looked some other examples/code online, I can see that there are cases where the buffer is handled "between" the ilclient_get_output_buffer and the OMX_FillThisBuffer.

I also saw that there are some callbacks in order to know if the component has processed the input/output buffer, however, when I looked at the ilclient code, I saw that it already handles that callback and when block=1 then it should only return data that's available.
Is this assumption completely wrong?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7131
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Order of operations for video encoding

Mon Mar 25, 2019 3:10 pm

OMX_FillThisBuffer submits a buffer for filling. You must wait for the OMX_FillThisBufferDone callback (potentially handled by ilclient on your behalf) before looking at the buffer header again. Dcoumented in section 3.2.2.18 of the IL spec.
Likewise OMX_EmptyThisBuffer submits a buffer for emptying, and you must wait for OMX_EmptyThisBufferDone before putting more data into the buffer. Documented in section 3.2.2.17 of the spec.

hello_encode follows the correct pattern.
https://github.com/raspberrypi/userland ... ode.c#L258

Code: Select all

         out = ilclient_get_output_buffer(video_encode, 201, 1);

         if (out != NULL) {
            if (out->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
               int i;
               for (i = 0; i < out->nFilledLen; i++)
                  printf("%x ", out->pBuffer[i]);
               printf("\n");
            }

            r = fwrite(out->pBuffer, 1, out->nFilledLen, outf);
            if (r != out->nFilledLen) {
               printf("fwrite: Error emptying buffer: %d!\n", r);
            } else {
               printf("Writing frame %d/%d, len %u\n", framenumber, NUMFRAMES, out->nFilledLen);
            }
            out->nFilledLen = 0;
         } else {
            printf("Not getting it :(\n");
         }

         r = OMX_FillThisBuffer(ILC_GET_HANDLE(video_encode), out);
         if (r != OMX_ErrorNone) {
            printf("Error sending buffer for filling: %x\n", r);
}
It is true that it did things in the wrong, but that was fixed July 2018 with https://github.com/raspberrypi/userland ... d7b85664c5
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

atiti
Posts: 6
Joined: Wed Aug 14, 2013 5:34 pm

Re: Order of operations for video encoding

Mon Mar 25, 2019 8:10 pm

Thanks, appreciate the reply 6by9! Indeed, flipping around the order has solved the issue for me!

Return to “OpenMAX”