Advertisement
LocutusOfBorg

hedgewars issue 752

Jan 3rd, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. diff --git a/hedgewars/avwrapper/avwrapper.c b/hedgewars/avwrapper/avwrapper.c
  2. index 401f7d0..a1dcaa6 100644
  3. --- a/hedgewars/avwrapper/avwrapper.c
  4. +++ b/hedgewars/avwrapper/avwrapper.c
  5. @@ -65,7 +65,7 @@ static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
  6. // pointer to function from hwengine (uUtils.pas)
  7. static void (*AddFileLogRaw)(const char* pString);
  8.  
  9. -static void FatalError(const char* pFmt, ...)
  10. +static int FatalError(const char* pFmt, ...)
  11. {
  12. char Buffer[1024];
  13. va_list VaArgs;
  14. @@ -77,7 +77,7 @@ static void FatalError(const char* pFmt, ...)
  15. AddFileLogRaw("Error in av-wrapper: ");
  16. AddFileLogRaw(Buffer);
  17. AddFileLogRaw("\n");
  18. - exit(1);
  19. + return(255);
  20. }
  21.  
  22. // Function to be called from libav for logging.
  23. @@ -166,7 +166,7 @@ static void AddAudioStream()
  24. }
  25. }
  26.  
  27. -// returns non-zero if there is more sound
  28. +// returns non-zero if there is more sound, 255 in case of error
  29. static int WriteAudioFrame()
  30. {
  31. if (!g_pAStream)
  32. @@ -189,7 +189,7 @@ static int WriteAudioFrame()
  33. // when NumSamples == 0 we still need to call encode_audio2 to flush
  34. int got_packet;
  35. if (avcodec_encode_audio2(g_pAudio, &Packet, pFrame, &got_packet) != 0)
  36. - FatalError("avcodec_encode_audio2 failed");
  37. + return FatalError("avcodec_encode_audio2 failed");
  38. if (!got_packet)
  39. return 0;
  40. #else
  41. @@ -210,12 +210,12 @@ static int WriteAudioFrame()
  42. // Write the compressed frame to the media file.
  43. Packet.stream_index = g_pAStream->index;
  44. if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
  45. - FatalError("Error while writing audio frame");
  46. + return FatalError("Error while writing audio frame");
  47. return 1;
  48. }
  49.  
  50. // add a video output stream
  51. -static void AddVideoStream()
  52. +static int AddVideoStream()
  53. {
  54. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  55. g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec);
  56. @@ -223,7 +223,7 @@ static void AddVideoStream()
  57. g_pVStream = av_new_stream(g_pContainer, 0);
  58. #endif
  59. if (!g_pVStream)
  60. - FatalError("Could not allocate video stream");
  61. + return FatalError("Could not allocate video stream");
  62.  
  63. g_pVideo = g_pVStream->codec;
  64.  
  65. @@ -297,29 +297,35 @@ static void AddVideoStream()
  66. #else
  67. if (avcodec_open(g_pVideo, g_pVCodec) < 0)
  68. #endif
  69. - FatalError("Could not open video codec %s", g_pVCodec->long_name);
  70. + return FatalError("Could not open video codec %s", g_pVCodec->long_name);
  71.  
  72. g_pVFrame = avcodec_alloc_frame();
  73. if (!g_pVFrame)
  74. - FatalError("Could not allocate frame");
  75. + return FatalError("Could not allocate frame");
  76.  
  77. g_pVFrame->linesize[0] = g_Width;
  78. g_pVFrame->linesize[1] = g_Width/2;
  79. g_pVFrame->linesize[2] = g_Width/2;
  80. g_pVFrame->linesize[3] = 0;
  81. + return 0;
  82. }
  83.  
  84. static int WriteFrame(AVFrame* pFrame)
  85. {
  86. double AudioTime, VideoTime;
  87. -
  88. + int ret;
  89. // write interleaved audio frame
  90. if (g_pAStream)
  91. {
  92. VideoTime = (double)g_pVStream->pts.val*g_pVStream->time_base.num/g_pVStream->time_base.den;
  93. do
  94. + {
  95. AudioTime = (double)g_pAStream->pts.val*g_pAStream->time_base.num/g_pAStream->time_base.den;
  96. - while (AudioTime < VideoTime && WriteAudioFrame());
  97. + ret = WriteAudioFrame();
  98. + }
  99. + while (AudioTime < VideoTime && (ret != 0 || ret != 255));
  100. + if (ret == 255)
  101. + return ret;
  102. }
  103.  
  104. if (!g_pVStream)
  105. @@ -341,7 +347,7 @@ static int WriteFrame(AVFrame* pFrame)
  106. Packet.size = sizeof(AVPicture);
  107.  
  108. if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
  109. - FatalError("Error while writing video frame");
  110. + return FatalError("Error while writing video frame");
  111. return 0;
  112. }
  113. else
  114. @@ -349,7 +355,7 @@ static int WriteFrame(AVFrame* pFrame)
  115. #if LIBAVCODEC_VERSION_MAJOR >= 54
  116. int got_packet;
  117. if (avcodec_encode_video2(g_pVideo, &Packet, pFrame, &got_packet) < 0)
  118. - FatalError("avcodec_encode_video2 failed");
  119. + return FatalError("avcodec_encode_video2 failed");
  120. if (!got_packet)
  121. return 0;
  122.  
  123. @@ -360,7 +366,7 @@ static int WriteFrame(AVFrame* pFrame)
  124. #else
  125. Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame);
  126. if (Packet.size < 0)
  127. - FatalError("avcodec_encode_video failed");
  128. + return FatalError("avcodec_encode_video failed");
  129. if (Packet.size == 0)
  130. return 0;
  131.  
  132. @@ -373,21 +379,21 @@ static int WriteFrame(AVFrame* pFrame)
  133. // write the compressed frame in the media file
  134. Packet.stream_index = g_pVStream->index;
  135. if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
  136. - FatalError("Error while writing video frame");
  137. + return FatalError("Error while writing video frame");
  138.  
  139. return 1;
  140. }
  141. }
  142.  
  143. -AVWRAP_DECL void AVWrapper_WriteFrame(uint8_t* pY, uint8_t* pCb, uint8_t* pCr)
  144. +AVWRAP_DECL int AVWrapper_WriteFrame(uint8_t* pY, uint8_t* pCb, uint8_t* pCr)
  145. {
  146. g_pVFrame->data[0] = pY;
  147. g_pVFrame->data[1] = pCb;
  148. g_pVFrame->data[2] = pCr;
  149. - WriteFrame(g_pVFrame);
  150. + return WriteFrame(g_pVFrame);
  151. }
  152.  
  153. -AVWRAP_DECL void AVWrapper_Init(
  154. +AVWRAP_DECL int AVWrapper_Init(
  155. void (*pAddFileLogRaw)(const char*),
  156. const char* pFilename,
  157. const char* pDesc,
  158. @@ -399,6 +405,7 @@ AVWRAP_DECL void AVWrapper_Init(
  159. int FramerateNum, int FramerateDen,
  160. int VQuality)
  161. {
  162. + int ret;
  163. AddFileLogRaw = pAddFileLogRaw;
  164. av_log_set_callback( &LogCallback );
  165.  
  166. @@ -414,12 +421,12 @@ AVWRAP_DECL void AVWrapper_Init(
  167. // find format
  168. g_pFormat = av_guess_format(pFormatName, NULL, NULL);
  169. if (!g_pFormat)
  170. - FatalError("Format \"%s\" was not found", pFormatName);
  171. + return FatalError("Format \"%s\" was not found", pFormatName);
  172.  
  173. // allocate the output media context
  174. g_pContainer = avformat_alloc_context();
  175. if (!g_pContainer)
  176. - FatalError("Could not allocate output context");
  177. + return FatalError("Could not allocate output context");
  178.  
  179. g_pContainer->oformat = g_pFormat;
  180.  
  181. @@ -442,7 +449,10 @@ AVWRAP_DECL void AVWrapper_Init(
  182. g_pAStream = NULL;
  183.  
  184. if (g_pVCodec)
  185. - AddVideoStream();
  186. + {
  187. + if((ret = AddVideoStream()))
  188. + return ret;
  189. + }
  190. else
  191. Log("Video codec \"%s\" was not found; video will be ignored.\n", pVCodecName);
  192.  
  193. @@ -462,7 +472,7 @@ AVWRAP_DECL void AVWrapper_Init(
  194. Log("Audio codec \"%s\" was not found; audio will be ignored.\n", pACodecName);
  195.  
  196. if (!g_pAStream && !g_pVStream)
  197. - FatalError("No video, no audio, aborting...");
  198. + return FatalError("No video, no audio, aborting...");
  199.  
  200. // write format info to log
  201. av_dump_format(g_pContainer, 0, g_pContainer->filename, 1);
  202. @@ -471,22 +481,36 @@ AVWRAP_DECL void AVWrapper_Init(
  203. if (!(g_pFormat->flags & AVFMT_NOFILE))
  204. {
  205. if (avio_open(&g_pContainer->pb, g_pContainer->filename, AVIO_FLAG_WRITE) < 0)
  206. - FatalError("Could not open output file (%s)", g_pContainer->filename);
  207. + return FatalError("Could not open output file (%s)", g_pContainer->filename);
  208. }
  209.  
  210. // write the stream header, if any
  211. avformat_write_header(g_pContainer, NULL);
  212.  
  213. g_pVFrame->pts = -1;
  214. + return 0;
  215. }
  216.  
  217. -AVWRAP_DECL void AVWrapper_Close()
  218. +AVWRAP_DECL int AVWrapper_Close()
  219. {
  220. + int ret;
  221. // output buffered frames
  222. if (g_pVCodec->capabilities & CODEC_CAP_DELAY)
  223. - while( WriteFrame(NULL) );
  224. + {
  225. + do
  226. + {
  227. + ret = WriteFrame(NULL);
  228. + }
  229. + while (ret != 0 && ret !=255);
  230. + if (ret == 255)
  231. + return ret;
  232. + }
  233. // output any remaining audio
  234. - while( WriteAudioFrame() );
  235. + do
  236. + {
  237. + ret = WriteAudioFrame();
  238. + }
  239. + while(ret != 0 && ret !=255);
  240.  
  241. // write the trailer, if any.
  242. av_write_trailer(g_pContainer);
  243. @@ -514,4 +538,5 @@ AVWRAP_DECL void AVWrapper_Close()
  244. }
  245.  
  246. av_free(g_pContainer);
  247. + return 0;
  248. }
  249. diff --git a/hedgewars/uVideoRec.pas b/hedgewars/uVideoRec.pas
  250. index 309e26d..03f6259 100644
  251. --- a/hedgewars/uVideoRec.pas
  252. +++ b/hedgewars/uVideoRec.pas
  253. @@ -53,12 +53,12 @@ uses uVariables, uUtils, GLunit, SDLh, SysUtils, uIO, uMisc, uTypes;
  254. type TAddFileLogRaw = procedure (s: pchar); cdecl;
  255. const AvwrapperLibName = 'libavwrapper';
  256.  
  257. -procedure AVWrapper_Init(
  258. +function AVWrapper_Init(
  259. AddLog: TAddFileLogRaw;
  260. filename, desc, soundFile, format, vcodec, acodec: PChar;
  261. - width, height, framerateNum, framerateDen, vquality: LongInt); cdecl; external AvwrapperLibName;
  262. -procedure AVWrapper_Close; cdecl; external AvwrapperLibName;
  263. -procedure AVWrapper_WriteFrame( pY, pCb, pCr: PByte ); cdecl; external AvwrapperLibName;
  264. + width, height, framerateNum, framerateDen, vquality: LongInt): integer; cdecl; external AvwrapperLibName;
  265. +function AVWrapper_Close: integer; cdecl; external AvwrapperLibName;
  266. +function AVWrapper_WriteFrame( pY, pCb, pCr: PByte ): integer; cdecl; external AvwrapperLibName;
  267.  
  268. type TFrame = record
  269. realTicks: LongWord;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement