You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video.c 7.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. // Video deocode demo using OpenMAX IL though the ilcient helper library
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "bcm_host.h"
  30. #include "ilclient.h"
  31. static int video_decode_test(char *filename, int loop)
  32. {
  33. OMX_VIDEO_PARAM_PORTFORMATTYPE format;
  34. OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
  35. COMPONENT_T *video_decode = NULL, *video_scheduler = NULL, *video_render = NULL, *clock = NULL;
  36. COMPONENT_T *list[5];
  37. TUNNEL_T tunnel[4];
  38. ILCLIENT_T *client;
  39. FILE *in;
  40. int status = 0;
  41. unsigned int data_len = 0;
  42. memset(list, 0, sizeof(list));
  43. memset(tunnel, 0, sizeof(tunnel));
  44. if((in = fopen(filename, "rb")) == NULL)
  45. return -2;
  46. if((client = ilclient_init()) == NULL)
  47. {
  48. fclose(in);
  49. return -3;
  50. }
  51. if(OMX_Init() != OMX_ErrorNone)
  52. {
  53. ilclient_destroy(client);
  54. fclose(in);
  55. return -4;
  56. }
  57. // create video_decode
  58. if(ilclient_create_component(client, &video_decode, "video_decode", ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS) != 0)
  59. status = -14;
  60. list[0] = video_decode;
  61. // create video_render
  62. if(status == 0 && ilclient_create_component(client, &video_render, "video_render", ILCLIENT_DISABLE_ALL_PORTS) != 0)
  63. status = -14;
  64. list[1] = video_render;
  65. // create clock
  66. if(status == 0 && ilclient_create_component(client, &clock, "clock", ILCLIENT_DISABLE_ALL_PORTS) != 0)
  67. status = -14;
  68. list[2] = clock;
  69. memset(&cstate, 0, sizeof(cstate));
  70. cstate.nSize = sizeof(cstate);
  71. cstate.nVersion.nVersion = OMX_VERSION;
  72. cstate.eState = OMX_TIME_ClockStateWaitingForStartTime;
  73. cstate.nWaitMask = 1;
  74. if(clock != NULL && OMX_SetParameter(ILC_GET_HANDLE(clock), OMX_IndexConfigTimeClockState, &cstate) != OMX_ErrorNone)
  75. status = -13;
  76. // create video_scheduler
  77. if(status == 0 && ilclient_create_component(client, &video_scheduler, "video_scheduler", ILCLIENT_DISABLE_ALL_PORTS) != 0)
  78. status = -14;
  79. list[3] = video_scheduler;
  80. set_tunnel(tunnel, video_decode, 131, video_scheduler, 10);
  81. set_tunnel(tunnel+1, video_scheduler, 11, video_render, 90);
  82. set_tunnel(tunnel+2, clock, 80, video_scheduler, 12);
  83. // setup clock tunnel first
  84. if(status == 0 && ilclient_setup_tunnel(tunnel+2, 0, 0) != 0)
  85. status = -15;
  86. else
  87. ilclient_change_component_state(clock, OMX_StateExecuting);
  88. if(status == 0)
  89. ilclient_change_component_state(video_decode, OMX_StateIdle);
  90. memset(&format, 0, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
  91. format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
  92. format.nVersion.nVersion = OMX_VERSION;
  93. format.nPortIndex = 130;
  94. format.eCompressionFormat = OMX_VIDEO_CodingAVC;
  95. if(status == 0 &&
  96. OMX_SetParameter(ILC_GET_HANDLE(video_decode), OMX_IndexParamVideoPortFormat, &format) == OMX_ErrorNone &&
  97. ilclient_enable_port_buffers(video_decode, 130, NULL, NULL, NULL) == 0)
  98. {
  99. OMX_BUFFERHEADERTYPE *buf;
  100. int port_settings_changed = 0;
  101. int first_packet = 1;
  102. ilclient_change_component_state(video_decode, OMX_StateExecuting);
  103. while((buf = ilclient_get_input_buffer(video_decode, 130, 1)) != NULL)
  104. {
  105. // feed data and wait until we get port settings changed
  106. unsigned char *dest = buf->pBuffer;
  107. data_len += fread(dest, 1, buf->nAllocLen-data_len, in);
  108. if(port_settings_changed == 0 &&
  109. ((data_len > 0 && ilclient_remove_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1) == 0) ||
  110. (data_len == 0 && ilclient_wait_for_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1,
  111. ILCLIENT_EVENT_ERROR | ILCLIENT_PARAMETER_CHANGED, 10000) == 0)))
  112. {
  113. port_settings_changed = 1;
  114. if(ilclient_setup_tunnel(tunnel, 0, 0) != 0)
  115. {
  116. status = -7;
  117. break;
  118. }
  119. ilclient_change_component_state(video_scheduler, OMX_StateExecuting);
  120. // now setup tunnel to video_render
  121. if(ilclient_setup_tunnel(tunnel+1, 0, 1000) != 0)
  122. {
  123. status = -12;
  124. break;
  125. }
  126. ilclient_change_component_state(video_render, OMX_StateExecuting);
  127. }
  128. if(!data_len) {
  129. // Finished reading the file, either loop or exit.
  130. if (loop) {
  131. fseek(in, 0, SEEK_SET);
  132. }
  133. else {
  134. break;
  135. }
  136. }
  137. buf->nFilledLen = data_len;
  138. data_len = 0;
  139. buf->nOffset = 0;
  140. if(first_packet)
  141. {
  142. buf->nFlags = OMX_BUFFERFLAG_STARTTIME;
  143. first_packet = 0;
  144. }
  145. else
  146. buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
  147. if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
  148. {
  149. status = -6;
  150. break;
  151. }
  152. }
  153. buf->nFilledLen = 0;
  154. buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN | OMX_BUFFERFLAG_EOS;
  155. if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
  156. status = -20;
  157. // wait for EOS from render
  158. ilclient_wait_for_event(video_render, OMX_EventBufferFlag, 90, 0, OMX_BUFFERFLAG_EOS, 0,
  159. ILCLIENT_BUFFER_FLAG_EOS, 10000);
  160. // need to flush the renderer to allow video_decode to disable its input port
  161. ilclient_flush_tunnels(tunnel, 0);
  162. ilclient_disable_port_buffers(video_decode, 130, NULL, NULL, NULL);
  163. }
  164. fclose(in);
  165. ilclient_disable_tunnel(tunnel);
  166. ilclient_disable_tunnel(tunnel+1);
  167. ilclient_disable_tunnel(tunnel+2);
  168. ilclient_teardown_tunnels(tunnel);
  169. ilclient_state_transition(list, OMX_StateIdle);
  170. ilclient_state_transition(list, OMX_StateLoaded);
  171. ilclient_cleanup_components(list);
  172. OMX_Deinit();
  173. ilclient_destroy(client);
  174. return status;
  175. }
  176. void error_usage(char* name) {
  177. printf("Usage: %s [--loop] <filename>\n", name);
  178. exit(1);
  179. }
  180. int main (int argc, char **argv)
  181. {
  182. int loop = 0;
  183. if (argc < 2 || argc > 3) {
  184. error_usage(argv[0]);
  185. }
  186. // Check optional parameter.
  187. if (argc == 3) {
  188. // Check for loop parameter.
  189. if (strcmp(argv[1], "--loop") == 0) {
  190. loop = 1;
  191. }
  192. // Error unknown parameter.
  193. else {
  194. error_usage(argv[0]);
  195. }
  196. }
  197. bcm_host_init();
  198. return video_decode_test(argv[argc-1], loop);
  199. }