PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Codec/Avidemux-EditorPlugin/avidemux-2.4.4/avidemux/ADM_outputs/oplug_ogm/op_ogsaveprocess.cpp

https://bitbucket.org/gianni/vaet
C++ | 264 lines | 181 code | 49 blank | 34 comment | 23 complexity | e179f4787f3703d6156310ee1355534e MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, GPL-3.0
  1. //
  2. // C++ Implementation: op_ogsaveprocess
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author: mean <fixounet@free.fr>, (C) 2004
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. //
  12. //
  13. // C++ Implementation: op_ogsavecopy
  14. //
  15. // Description:
  16. //
  17. //
  18. // Author: mean <fixounet@free.fr>, (C) 2004
  19. //
  20. // Copyright: See COPYING file that comes with this distribution
  21. //
  22. //
  23. #define __STDC_LIMIT_MACROS
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <sys/time.h>
  30. #include "config.h"
  31. #include "fourcc.h"
  32. #include "avi_vars.h"
  33. #include "ADM_toolkit/toolkit.hxx"
  34. #include "subchunk.h"
  35. //#include "avilist.h"
  36. #include "ADM_video/ADM_genvideo.hxx"
  37. #include "ADM_filter/video_filters.h"
  38. #include "ADM_encoder/ADM_vidEncode.hxx"
  39. #include "ADM_audio/aviaudio.hxx"
  40. #include "ADM_audiofilter/audioprocess.hxx"
  41. #include "default.h"
  42. #include "oplug_ogm/op_ogsave.h"
  43. #define aprintf printf
  44. //________________________________________________
  45. uint8_t ADM_ogmWriteProcess::initVideo(const char *name)
  46. {
  47. uint32_t w,h,fps1000,fcc;
  48. _prestore=0;
  49. _prestoring=1;
  50. _incoming = getLastVideoFilter (frameStart,frameEnd-frameStart);
  51. _togo=_incoming->getInfo()->nb_frames;
  52. _encode = getVideoEncoder (_incoming->getInfo()->width,_incoming->getInfo()->height);
  53. if (!_encode)
  54. return 0;
  55. TwoPassLogFile=new char[strlen(name)+6];
  56. strcpy(TwoPassLogFile,name);
  57. strcat(TwoPassLogFile,".stat");
  58. _encode->setLogFile(TwoPassLogFile,_togo);
  59. if (!_encode->configure (_incoming))
  60. {
  61. delete _encode;
  62. _encode = NULL;
  63. GUI_Error_HIG (QT_TR_NOOP("Filter init failed"), NULL);
  64. return 0;
  65. };
  66. w= _incoming->getInfo ()->width;
  67. h=_incoming->getInfo ()->height;
  68. fps1000=_incoming->getInfo ()->fps1000;
  69. _fps1000=fps1000;
  70. fcc= fourCC::get((uint8_t *)_encode->getCodecName());
  71. _videoBuffer=new uint8_t[w*h*3];
  72. encoding_gui->setCodec(_encode->getDisplayName());
  73. //-----------------------VBR--------------------------------------
  74. if (_encode->isDualPass ())
  75. {
  76. uint8_t *buffer;
  77. uint32_t len, flag;
  78. FILE *tmp;
  79. uint8_t reuse=0;
  80. int frameDelay = 0, r;
  81. aprintf("\n** Dual pass encoding**\n");
  82. if((tmp=fopen(TwoPassLogFile,"rt")))
  83. {
  84. fclose(tmp);
  85. if(GUI_Question(QT_TR_NOOP("\n Reuse the existing log-file ?")))
  86. {
  87. reuse=1;
  88. }
  89. }
  90. if(!reuse)
  91. {
  92. aprintf("**Pass 1:%lu\n",_togo);
  93. _encode->startPass1 ();
  94. encoding_gui->setCodec(_encode->getCodecName());
  95. encoding_gui->setPhasis(QT_TR_NOOP("Pass one"));
  96. ADMBitstream bitstream(w*h*3);
  97. bitstream.data=_videoBuffer;
  98. for (uint32_t cf = 0; cf < _togo; cf++)
  99. {
  100. if (!encoding_gui->isAlive())
  101. return 0;
  102. for (;;)
  103. {
  104. bitstream.cleanup(cf);
  105. if (cf + frameDelay >= _togo)
  106. {
  107. if (_encode->getRequirements() & ADM_ENC_REQ_NULL_FLUSH)
  108. r = _encode->encode(UINT32_MAX, &bitstream);
  109. else
  110. r = 0;
  111. }
  112. else
  113. r = _encode->encode(cf + frameDelay, &bitstream);
  114. if (!r)
  115. {
  116. printf("Encoding of frame %lu failed!\n", cf);
  117. return 0;
  118. }
  119. if (bitstream.len == 0 && (_encode->getRequirements() & ADM_ENC_REQ_NULL_FLUSH))
  120. {
  121. printf("skipping frame: %u size: %i\n", cf + frameDelay, bitstream.len);
  122. frameDelay++;
  123. }
  124. else
  125. break;
  126. }
  127. encoding_gui->setFrame(cf,bitstream.len,bitstream.out_quantizer,_togo);
  128. }
  129. aprintf("**Pass 1:done\n");
  130. }// End of reuse
  131. if(!_encode->startPass2 ())
  132. {
  133. printf("Pass2 ignition failed\n");
  134. return 0;
  135. }
  136. encoding_gui->setPhasis(QT_TR_NOOP("Pass 2"));
  137. } //-------------------------/VBR-----------------------------------
  138. else
  139. {
  140. encoding_gui->setPhasis(QT_TR_NOOP("Encoding"));
  141. }
  142. // init save avi
  143. // now we build the new stream !
  144. aprintf("**main pass:\n");
  145. stream_header header;
  146. int64_t dur64;
  147. uint32_t dur32;
  148. uint16_t dur16;
  149. memset(&header,0,sizeof(header));
  150. memcpy(&(header.streamtype),"video\0\0\0",8);
  151. MEMCPY(&(header.subtype),&fcc,4);
  152. //header.size=sizeof(header);
  153. dur32=sizeof(header);
  154. MEMCPY(&header.size,&dur32,4);
  155. MEMCPY(&(header.video.width),&w,4);
  156. MEMCPY(&(header.video.height),&h,4);
  157. // Timing ..
  158. double duration; // duration in 10us
  159. duration=fps1000;
  160. duration=1000./duration;
  161. duration*=1000*1000;
  162. duration*=10;
  163. dur64=(int64_t)duration;
  164. MEMCPY(&header.time_unit,&dur64,8);
  165. dur64=1;
  166. MEMCPY(&header.samples_per_unit,&dur64,8);
  167. dur32=0x10000;
  168. MEMCPY(&header.buffersize,&dur32,4);
  169. dur16=24;
  170. MEMCPY(&header.bits_per_sample,&dur16,2);
  171. //header.default_len=1;
  172. dur32=1;
  173. MEMCPY(&header.default_len,&dur32,4);
  174. return videoStream->writeHeaders(sizeof(header),(uint8_t *)&header); // +4 ?
  175. }
  176. //___________________________________________________
  177. int ADM_ogmWriteProcess::writeVideo(uint32_t frame)
  178. {
  179. uint32_t len, flags;
  180. uint8_t ret;
  181. ADMBitstream bitstream(_incoming->getInfo()->width * _incoming->getInfo()->height * 3);
  182. bitstream.data = _videoBuffer;
  183. bitstream.cleanup(frame);
  184. if (frame >= _togo)
  185. {
  186. if (_encode->getRequirements() & ADM_ENC_REQ_NULL_FLUSH)
  187. ret = _encode->encode(UINT32_MAX, &bitstream);
  188. }
  189. else
  190. ret = _encode->encode(frame, &bitstream);
  191. if (!ret)
  192. {
  193. printf("OgmWrite: Error encoding frame %d\n",frame);
  194. return -1;
  195. }
  196. if (bitstream.len > 0)
  197. {
  198. encoding_gui->setFrame(frame >= _togo ? _togo - 1 : frame, bitstream.len, bitstream.out_quantizer, _togo);
  199. if (!videoStream->write(bitstream.len, _videoBuffer, bitstream.flags, bitstream.dtsFrame))
  200. return -1;
  201. }
  202. return bitstream.len;
  203. }
  204. //___________________________________________________
  205. ADM_ogmWriteProcess::ADM_ogmWriteProcess( void)
  206. {
  207. _incoming=NULL;
  208. }
  209. //___________________________________________________
  210. ADM_ogmWriteProcess::~ADM_ogmWriteProcess()
  211. {
  212. if(_incoming) delete _incoming;
  213. if(_encode) delete _encode;
  214. }
  215. //EOF