PageRenderTime 97ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/audio_video_lib/video_av_files_lib/video_reader.e

http://github.com/tioui/eiffel_game_lib
Specman e | 255 lines | 210 code | 44 blank | 1 comment | 8 complexity | 705324645aaf0556cc2bf63a350bae4d MD5 | raw file
Possible License(s): GPL-3.0
  1. note
  2. description: "Summary description for {VIDEO_READER}."
  3. author: ""
  4. date: "$Date$"
  5. revision: "$Revision$"
  6. class
  7. VIDEO_READER
  8. inherit
  9. AV_ANY
  10. DISPOSABLE
  11. create
  12. make
  13. feature {NONE} -- Initialization
  14. make(a_core_lib_ctrl:GAME_LIB_CONTROLLER)
  15. -- Initialization for `Current'.
  16. Require
  17. Video_Make_Screen_Must_Exist:a_core_lib_ctrl.screen_is_create
  18. do
  19. core_lib_ctrl:=a_core_lib_ctrl
  20. dst_rect:= dst_rect.memory_alloc ({GAME_SDL_EXTERNAL}.c_sizeof_sdl_rect)
  21. set_x (0)
  22. set_y (0)
  23. set_width (a_core_lib_ctrl.screen_surface.width)
  24. set_height (a_core_lib_ctrl.screen_surface.height)
  25. create video_queued.make
  26. is_auto_resize:=true
  27. end
  28. feature -- Access
  29. x:INTEGER assign set_x
  30. do
  31. Result:={GAME_SDL_EXTERNAL}.get_rect_struct_x(dst_rect).to_integer
  32. end
  33. y:INTEGER assign set_y
  34. do
  35. Result:={GAME_SDL_EXTERNAL}.get_rect_struct_y(dst_rect).to_integer
  36. end
  37. width:INTEGER assign set_width
  38. do
  39. Result:={GAME_SDL_EXTERNAL}.get_rect_struct_w(dst_rect).to_integer_32
  40. end
  41. height:INTEGER assign set_height
  42. do
  43. Result:={GAME_SDL_EXTERNAL}.get_rect_struct_h(dst_rect).to_integer_32
  44. end
  45. set_x(a_x:INTEGER)
  46. do
  47. {GAME_SDL_EXTERNAL}.set_rect_struct_x(dst_rect,a_x.to_integer_16)
  48. end
  49. set_y(a_y:INTEGER)
  50. do
  51. {GAME_SDL_EXTERNAL}.set_rect_struct_y(dst_rect,a_y.to_integer_16)
  52. end
  53. set_width(a_width:INTEGER)
  54. do
  55. {GAME_SDL_EXTERNAL}.set_rect_struct_w(dst_rect,a_width.to_natural_16)
  56. end
  57. set_height(a_height:INTEGER)
  58. do
  59. {GAME_SDL_EXTERNAL}.set_rect_struct_h(dst_rect,a_height.to_natural_16)
  60. end
  61. play
  62. require
  63. Play_Has_Video_Queud:video_queued_count>0
  64. do
  65. if not is_on_pause then
  66. load_video
  67. end
  68. is_playing:=true
  69. is_on_pause:=false
  70. next_frame_ticks:=core_lib_ctrl.get_ticks
  71. end
  72. pause
  73. require
  74. Pause_Is_Playing: is_playing
  75. do
  76. is_playing:=false
  77. is_on_pause:=true
  78. end
  79. stop
  80. do
  81. is_playing:=false
  82. is_on_pause:=false
  83. video_queued.wipe_out
  84. end
  85. is_playing:BOOLEAN
  86. is_on_pause:BOOLEAN
  87. is_stop:BOOLEAN
  88. do
  89. Result:=not is_playing and not is_on_pause
  90. end
  91. is_auto_resize:BOOLEAN
  92. enable_auto_resize
  93. do
  94. is_auto_resize:=true
  95. end
  96. disable_auto_resize
  97. do
  98. is_auto_resize:=true
  99. end
  100. queue_video_loop(a_video:VIDEO_MEDIA;a_nb_loop:INTEGER)
  101. do
  102. video_queued.put ([a_video,a_nb_loop])
  103. end
  104. queue_video_infinite_loop(a_video:VIDEO_MEDIA)
  105. do
  106. queue_video_loop (a_video,-1)
  107. end
  108. queue_video(a_video:VIDEO_MEDIA)
  109. do
  110. queue_video_loop (a_video,0)
  111. end
  112. video_queued_count:INTEGER
  113. do
  114. Result:=video_queued.count
  115. end
  116. update_video_to_screen
  117. local
  118. error:INTEGER
  119. temp_delay:REAL_64
  120. do
  121. if is_playing and then core_lib_ctrl.get_ticks>=next_frame_ticks.floor.to_natural_32 then
  122. temp_delay:=(video_queued.item.video.time_base_num.to_double/video_queued.item.video.time_base_denum.to_double)
  123. video_queued.item.video.decode_frame
  124. if not video_queued.item.video.end_of_file then
  125. error:={GAME_SDL_EXTERNAL}.SDL_LockYUVOverlay(overlay)
  126. check error=0 end
  127. error:={AV_VIDEO_EXTERNAL}.sws_scale(converter, {AV_EXTERNAL}.get_av_frame_struct_data(video_queued.item.video.last_decoded_frame),
  128. {AV_EXTERNAL}.get_av_frame_struct_linesize(video_queued.item.video.last_decoded_frame),0,
  129. video_queued.item.video.height, {AV_EXTERNAL}.get_av_frame_struct_data(pictureYUV),
  130. {AV_EXTERNAL}.get_av_frame_struct_linesize(pictureYUV));
  131. {GAME_SDL_EXTERNAL}.SDL_UnlockYUVOverlay(overlay)
  132. {GAME_SDL_EXTERNAL}.SDL_DisplayYUVOverlay(overlay, dst_rect)
  133. temp_delay:=temp_delay+({AV_EXTERNAL}.get_av_frame_struct_repeat_pict(video_queued.item.video.last_decoded_frame)*(temp_delay*0.5))
  134. next_frame_ticks:=next_frame_ticks+(temp_delay*1000.0)
  135. else
  136. video_queued.item.video.restart
  137. if video_queued.item.nb_loop>0 then
  138. video_queued.item.nb_loop:=video_queued.item.nb_loop-1
  139. elseif video_queued.item.nb_loop=0 then
  140. video_queued.remove
  141. if video_queued.is_empty then
  142. stop
  143. else
  144. load_video
  145. end
  146. end
  147. next_frame_ticks:=core_lib_ctrl.get_ticks
  148. end
  149. elseif not is_stop then
  150. {GAME_SDL_EXTERNAL}.SDL_DisplayYUVOverlay(overlay, dst_rect)
  151. end
  152. end
  153. feature {NONE} -- Implementation - Routines
  154. load_video
  155. local
  156. size,error:INTEGER
  157. do
  158. if is_auto_resize then
  159. set_width (video_queued.item.video.width)
  160. set_height (video_queued.item.video.height)
  161. end
  162. free_converter
  163. overlay:={GAME_SDL_EXTERNAL}.SDL_CreateYUVOverlay(video_queued.item.video.width,video_queued.item.video.height,{GAME_SDL_EXTERNAL}.SDL_YV12_OVERLAY,{GAME_SDL_EXTERNAL}.SDL_GetVideoSurface)
  164. converter:={AV_VIDEO_EXTERNAL}.sws_getContext(video_queued.item.video.width,video_queued.item.video.height,
  165. video_queued.item.video.pix_fmt,video_queued.item.video.width,video_queued.item.video.height,
  166. {AV_EXTERNAL}.PIX_FMT_YUV420P,{AV_VIDEO_EXTERNAL}.SWS_BICUBIC,void_ptr,void_ptr,void_ptr)
  167. check not converter.is_default_pointer end
  168. pictureYUV:={AV_EXTERNAL}.avcodec_alloc_frame
  169. check not pictureYUV.is_default_pointer end
  170. size:={AV_EXTERNAL}.avpicture_get_size(video_queued.item.video.pix_fmt,video_queued.item.video.width,video_queued.item.video.height)
  171. pictureYUV_buffer:={AV_EXTERNAL}.av_malloc(size)
  172. error:={AV_EXTERNAL}.avpicture_fill(pictureYUV,pictureYUV_buffer,video_queued.item.video.pix_fmt,video_queued.item.video.width,video_queued.item.video.height)
  173. check error>=0 end
  174. {AV_EXTERNAL}.set_av_frame_struct_data_i(pictureYUV,0,
  175. {GAME_SDL_EXTERNAL}.get_sdl_overlay_struct_pixels_i(overlay,0))
  176. {AV_EXTERNAL}.set_av_frame_struct_data_i(pictureYUV,1,
  177. {GAME_SDL_EXTERNAL}.get_sdl_overlay_struct_pixels_i(overlay,2))
  178. {AV_EXTERNAL}.set_av_frame_struct_data_i(pictureYUV,2,
  179. {GAME_SDL_EXTERNAL}.get_sdl_overlay_struct_pixels_i(overlay,1))
  180. {AV_EXTERNAL}.set_av_frame_struct_linesize_i(pictureYUV,0,
  181. {GAME_SDL_EXTERNAL}.get_sdl_overlay_struct_pitches_i(overlay,0))
  182. {AV_EXTERNAL}.set_av_frame_struct_linesize_i(pictureYUV,1,
  183. {GAME_SDL_EXTERNAL}.get_sdl_overlay_struct_pitches_i(overlay,2))
  184. {AV_EXTERNAL}.set_av_frame_struct_linesize_i(pictureYUV,2,
  185. {GAME_SDL_EXTERNAL}.get_sdl_overlay_struct_pitches_i(overlay,1))
  186. end
  187. free_converter
  188. do
  189. if overlay/=Void then
  190. {GAME_SDL_EXTERNAL}.SDL_FreeYUVOverlay(overlay)
  191. {AV_EXTERNAL}.av_free(converter)
  192. {AV_EXTERNAL}.av_free(pictureYUV)
  193. {AV_EXTERNAL}.av_free(pictureYUV_buffer)
  194. end
  195. end
  196. dispose
  197. do
  198. video_queued.wipe_out
  199. dst_rect.memory_free
  200. free_converter
  201. end
  202. feature {NONE} -- Implementation - Variables
  203. core_lib_ctrl:GAME_LIB_CONTROLLER
  204. overlay:POINTER
  205. dst_rect:POINTER
  206. video_queued:LINKED_QUEUE[TUPLE[video:VIDEO_MEDIA;nb_loop:INTEGER]]
  207. converter:POINTER
  208. pictureYUV:POINTER
  209. pictureYUV_buffer:POINTER
  210. next_frame_ticks:REAL_64
  211. end