Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <stdio.h>
- #include <bcm_host.h>
- #include <ilclient.h>
- int main(int argc, char *argv[])
- {
- FILE *input, *output;
- long length;
- ILCLIENT_T *ILClient;
- TUNNEL_T *tunnel;
- COMPONENT_T *decoder, *resizer;
- OMX_PARAM_PORTDEFINITIONTYPE definition;
- OMX_IMAGE_PARAM_PORTFORMATTYPE format;
- OMX_BUFFERHEADERTYPE *bufferHeader;
- int width, height, stride;
- int i;
- assert(argc == 3);
- // Open files
- assert((input = fopen(argv[1], "r")) != NULL);
- assert((output = fopen(argv[2], "w")) != NULL);
- // Get the input file's length
- fseek(input, 0, SEEK_END);
- length = ftell(input);
- rewind(input);
- // Init OMX
- bcm_host_init();
- assert((ILClient = ilclient_init()) != NULL);
- assert(OMX_Init() == OMX_ErrorNone);
- // Create components
- assert(
- ilclient_create_component(ILClient,
- &decoder,
- "image_decode",
- ILCLIENT_DISABLE_ALL_PORTS
- |
- ILCLIENT_ENABLE_INPUT_BUFFERS)
- == 0);
- assert(ilclient_change_component_state(decoder, OMX_StateIdle) == 0);
- assert(
- ilclient_create_component(ILClient,
- &resizer,
- "resize",
- ILCLIENT_DISABLE_ALL_PORTS
- |
- ILCLIENT_ENABLE_OUTPUT_BUFFERS)
- == 0);
- assert(ilclient_change_component_state(resizer, OMX_StateIdle) == 0);
- // Define input as JPEG and make sure the image fits in a single buffer
- // then enable the port
- memset(&definition, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
- definition.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
- definition.nVersion.nVersion = OMX_VERSION;
- definition.nPortIndex = 320;
- assert(
- OMX_GetParameter(ilclient_get_handle(decoder),
- OMX_IndexParamPortDefinition,
- &definition)
- == OMX_ErrorNone);
- definition.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
- definition.nBufferSize = definition.nBufferSize > length ? definition.nBufferSize: length;
- definition.nBufferCountActual = 2;
- assert(
- OMX_SetParameter(ilclient_get_handle(decoder),
- OMX_IndexParamPortDefinition,
- &definition)
- == OMX_ErrorNone);
- assert(ilclient_enable_port_buffers(decoder, 320, NULL, NULL, NULL) == 0);
- // Execute decoder
- assert(ilclient_change_component_state(decoder, OMX_StateExecuting) == 0);
- // Fill the first buffer with the image
- assert(
- (bufferHeader = ilclient_get_input_buffer(decoder, 320, 1))
- != NULL);
- fread(bufferHeader->pBuffer, 1, length, input);
- bufferHeader->nFilledLen = length;
- bufferHeader->nFlags = OMX_BUFFERFLAG_EOS | OMX_BUFFERFLAG_ENDOFFRAME;
- assert(
- OMX_EmptyThisBuffer(ilclient_get_handle(decoder),
- bufferHeader)
- == OMX_ErrorNone);
- // Fill the second buffer with zeros
- assert(
- (bufferHeader = ilclient_get_input_buffer(decoder, 320, 1))
- != NULL);
- memset(bufferHeader->pBuffer, 0xFF, bufferHeader->nAllocLen);
- bufferHeader->nFilledLen = bufferHeader->nAllocLen;
- assert(
- OMX_EmptyThisBuffer(ilclient_get_handle(decoder),
- bufferHeader)
- == OMX_ErrorNone);
- // Create tunnel
- tunnel = calloc(2, sizeof(TUNNEL_T));
- set_tunnel(tunnel, decoder, 321, resizer, 60);
- assert(ilclient_setup_tunnel(tunnel, 0, 10000) == 0);
- // Define output as RGBA32 and enable the port
- memset(&definition, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
- definition.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
- definition.nVersion.nVersion = OMX_VERSION;
- definition.nPortIndex = 61;
- assert(
- OMX_GetParameter(ilclient_get_handle(resizer),
- OMX_IndexParamPortDefinition,
- &definition)
- == OMX_ErrorNone);
- definition.format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
- definition.format.image.eColorFormat = OMX_COLOR_Format32bitABGR8888;
- definition.format.image.nStride = 0;
- definition.format.image.nSliceHeight = 0;
- assert(
- OMX_SetParameter(ilclient_get_handle(resizer),
- OMX_IndexParamPortDefinition,
- &definition)
- == OMX_ErrorNone);
- assert(ilclient_enable_port_buffers(resizer, 61, NULL, NULL, NULL) == 0);
- // Get the actual port definition
- memset(&definition, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
- definition.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
- definition.nVersion.nVersion = OMX_VERSION;
- definition.nPortIndex = 61;
- assert(
- OMX_GetParameter(ilclient_get_handle(resizer),
- OMX_IndexParamPortDefinition,
- &definition)
- == OMX_ErrorNone);
- width = definition.format.image.nFrameWidth;
- height = definition.format.image.nFrameHeight;
- stride = definition.format.image.nStride;
- // Execute resizer
- assert(ilclient_change_component_state(resizer, OMX_StateExecuting) == 0);
- // Get the only output buffer and tell OpenMAX to fill it
- assert(
- (bufferHeader = ilclient_get_output_buffer(resizer, 61, 1))
- != NULL);
- assert(
- OMX_FillThisBuffer(ilclient_get_handle(resizer),
- bufferHeader)
- == OMX_ErrorNone);
- // Get the filled output buffer
- // The DATACORRUPT flag is set on this buffer, but the content is valid
- // OpenMAX bug???
- assert(
- (bufferHeader = ilclient_get_output_buffer(resizer, 61, 1))
- != NULL);
- // Crop the padding from the image
- for(i = 0; i < height; i++)
- {
- fwrite(bufferHeader->pBuffer + i * stride,
- 1,
- width * 4,
- output);
- }
- assert(
- OMX_FillThisBuffer(ilclient_get_handle(resizer),
- bufferHeader)
- == OMX_ErrorNone);
- // Cleanup: do everything backwards
- ilclient_flush_tunnels(tunnel, 1);
- ilclient_disable_tunnel(tunnel);
- ilclient_teardown_tunnels(tunnel);
- free(tunnel);
- ilclient_disable_port_buffers(resizer, 61, NULL, NULL, NULL);
- ilclient_disable_port_buffers(decoder, 320, NULL, NULL, NULL);
- ilclient_state_transition((COMPONENT_T *[]){decoder, resizer, NULL}, OMX_StateIdle);
- ilclient_state_transition((COMPONENT_T *[]){decoder, resizer, NULL}, OMX_StateLoaded);
- ilclient_cleanup_components((COMPONENT_T *[]){decoder, resizer, NULL});
- OMX_Deinit();
- ilclient_destroy(ILClient);
- bcm_host_deinit();
- fclose(output);
- fclose(input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement