Advertisement
ldmnyblzs

OpenMAX JPEG decoder

Jul 16th, 2015
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.99 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3.  
  4. #include <bcm_host.h>
  5. #include <ilclient.h>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     FILE *input, *output;
  10.     long length;
  11.     ILCLIENT_T *ILClient;
  12.     TUNNEL_T *tunnel;
  13.     COMPONENT_T *decoder, *resizer;
  14.     OMX_PARAM_PORTDEFINITIONTYPE definition;
  15.     OMX_IMAGE_PARAM_PORTFORMATTYPE format;
  16.     OMX_BUFFERHEADERTYPE *bufferHeader;
  17.     int width, height, stride;
  18.     int i;
  19.    
  20.     assert(argc == 3);
  21.    
  22.     // Open files
  23.     assert((input = fopen(argv[1], "r")) != NULL);
  24.     assert((output = fopen(argv[2], "w")) != NULL);
  25.     // Get the input file's length
  26.     fseek(input, 0, SEEK_END);
  27.     length = ftell(input);
  28.     rewind(input);
  29.    
  30.     // Init OMX
  31.     bcm_host_init();
  32.     assert((ILClient = ilclient_init()) != NULL);
  33.     assert(OMX_Init() == OMX_ErrorNone);
  34.    
  35.     // Create components
  36.     assert(
  37.         ilclient_create_component(ILClient,
  38.                                   &decoder,
  39.                                   "image_decode",
  40.                                   ILCLIENT_DISABLE_ALL_PORTS
  41.                                   |
  42.                                   ILCLIENT_ENABLE_INPUT_BUFFERS)
  43.         == 0);
  44.     assert(ilclient_change_component_state(decoder, OMX_StateIdle) == 0);
  45.    
  46.     assert(
  47.         ilclient_create_component(ILClient,
  48.                                   &resizer,
  49.                                   "resize",
  50.                                   ILCLIENT_DISABLE_ALL_PORTS
  51.                                   |
  52.                                   ILCLIENT_ENABLE_OUTPUT_BUFFERS)
  53.           == 0);
  54.     assert(ilclient_change_component_state(resizer, OMX_StateIdle) == 0);
  55.    
  56.     // Define input as JPEG and make sure the image fits in a single buffer
  57.     // then enable the port
  58.     memset(&definition, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
  59.     definition.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
  60.     definition.nVersion.nVersion = OMX_VERSION;
  61.     definition.nPortIndex = 320;
  62.     assert(
  63.         OMX_GetParameter(ilclient_get_handle(decoder),
  64.                          OMX_IndexParamPortDefinition,
  65.                          &definition)
  66.         == OMX_ErrorNone);
  67.     definition.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
  68.     definition.nBufferSize = definition.nBufferSize > length ? definition.nBufferSize: length;
  69.     definition.nBufferCountActual = 2;
  70.     assert(
  71.         OMX_SetParameter(ilclient_get_handle(decoder),
  72.                          OMX_IndexParamPortDefinition,
  73.                          &definition)
  74.         == OMX_ErrorNone);
  75.     assert(ilclient_enable_port_buffers(decoder, 320, NULL, NULL, NULL) == 0);
  76.    
  77.     // Execute decoder
  78.     assert(ilclient_change_component_state(decoder, OMX_StateExecuting) == 0);
  79.    
  80.     // Fill the first buffer with the image
  81.     assert(
  82.         (bufferHeader = ilclient_get_input_buffer(decoder, 320, 1))
  83.         != NULL);
  84.     fread(bufferHeader->pBuffer, 1, length, input);
  85.     bufferHeader->nFilledLen = length;
  86.     bufferHeader->nFlags = OMX_BUFFERFLAG_EOS | OMX_BUFFERFLAG_ENDOFFRAME;
  87.     assert(
  88.         OMX_EmptyThisBuffer(ilclient_get_handle(decoder),
  89.                             bufferHeader)
  90.         == OMX_ErrorNone);
  91.    
  92.     // Fill the second buffer with zeros
  93.     assert(
  94.         (bufferHeader = ilclient_get_input_buffer(decoder, 320, 1))
  95.         != NULL);
  96.     memset(bufferHeader->pBuffer, 0xFF, bufferHeader->nAllocLen);
  97.     bufferHeader->nFilledLen = bufferHeader->nAllocLen;
  98.     assert(
  99.         OMX_EmptyThisBuffer(ilclient_get_handle(decoder),
  100.                             bufferHeader)
  101.         == OMX_ErrorNone);
  102.    
  103.     // Create tunnel
  104.     tunnel = calloc(2, sizeof(TUNNEL_T));
  105.     set_tunnel(tunnel, decoder, 321, resizer, 60);
  106.     assert(ilclient_setup_tunnel(tunnel, 0, 10000) == 0);
  107.    
  108.     // Define output as RGBA32 and enable the port
  109.     memset(&definition, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
  110.     definition.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
  111.     definition.nVersion.nVersion = OMX_VERSION;
  112.     definition.nPortIndex = 61;
  113.     assert(
  114.         OMX_GetParameter(ilclient_get_handle(resizer),
  115.                          OMX_IndexParamPortDefinition,
  116.                          &definition)
  117.         == OMX_ErrorNone);
  118.     definition.format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
  119.     definition.format.image.eColorFormat = OMX_COLOR_Format32bitABGR8888;
  120.     definition.format.image.nStride = 0;
  121.     definition.format.image.nSliceHeight = 0;
  122.     assert(
  123.         OMX_SetParameter(ilclient_get_handle(resizer),
  124.                          OMX_IndexParamPortDefinition,
  125.                          &definition)
  126.         == OMX_ErrorNone);
  127.     assert(ilclient_enable_port_buffers(resizer, 61, NULL, NULL, NULL) == 0);
  128.    
  129.     // Get the actual port definition
  130.     memset(&definition, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
  131.     definition.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
  132.     definition.nVersion.nVersion = OMX_VERSION;
  133.     definition.nPortIndex = 61;
  134.     assert(
  135.         OMX_GetParameter(ilclient_get_handle(resizer),
  136.                          OMX_IndexParamPortDefinition,
  137.                          &definition)
  138.         == OMX_ErrorNone);
  139.     width = definition.format.image.nFrameWidth;
  140.     height = definition.format.image.nFrameHeight;
  141.     stride = definition.format.image.nStride;
  142.    
  143.     // Execute resizer
  144.     assert(ilclient_change_component_state(resizer, OMX_StateExecuting) == 0);
  145.    
  146.     // Get the only output buffer and tell OpenMAX to fill it
  147.     assert(
  148.         (bufferHeader = ilclient_get_output_buffer(resizer, 61, 1))
  149.         != NULL);
  150.     assert(
  151.         OMX_FillThisBuffer(ilclient_get_handle(resizer),
  152.                            bufferHeader)
  153.         == OMX_ErrorNone);
  154.  
  155.     // Get the filled output buffer
  156.     // The DATACORRUPT flag is set on this buffer, but the content is valid
  157.     // OpenMAX bug???
  158.     assert(
  159.         (bufferHeader = ilclient_get_output_buffer(resizer, 61, 1))
  160.         != NULL);
  161.     // Crop the padding from the image
  162.     for(i = 0; i < height; i++)
  163.     {
  164.         fwrite(bufferHeader->pBuffer + i * stride,
  165.                1,
  166.                width * 4,
  167.                output);
  168.     }
  169.     assert(
  170.         OMX_FillThisBuffer(ilclient_get_handle(resizer),
  171.                         bufferHeader)
  172.         == OMX_ErrorNone);
  173.    
  174.     // Cleanup: do everything backwards
  175.     ilclient_flush_tunnels(tunnel, 1);
  176.     ilclient_disable_tunnel(tunnel);
  177.     ilclient_teardown_tunnels(tunnel);
  178.     free(tunnel);
  179.     ilclient_disable_port_buffers(resizer, 61, NULL, NULL, NULL);
  180.     ilclient_disable_port_buffers(decoder, 320, NULL, NULL, NULL);
  181.     ilclient_state_transition((COMPONENT_T *[]){decoder, resizer, NULL}, OMX_StateIdle);
  182.     ilclient_state_transition((COMPONENT_T *[]){decoder, resizer, NULL}, OMX_StateLoaded);
  183.     ilclient_cleanup_components((COMPONENT_T *[]){decoder, resizer, NULL});
  184.     OMX_Deinit();
  185.     ilclient_destroy(ILClient);
  186.     bcm_host_deinit();
  187.     fclose(output);
  188.     fclose(input);
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement