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.

484 lines
20 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaMediaEncoder.h"
  2. #include "servaKernel.h"
  3. #define LOG( X ) m_pLog->Log( "MEN", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG234[256];\
  7. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "MEN", this, _LOG_ERROR, _tBfrErrMSG234 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "MEN", this, _LOG_ERROR, X );
  12. #endif
  13. servaMediaEncoder::servaMediaEncoder( uint64_t Ref ):basicThread(){
  14. m_pLog = basicLogCollection::getInstance();
  15. m_pMaster = servaKernel::getInstance();
  16. m_nRefID = Ref;
  17. m_nInFileSize = 0;
  18. m_nOutFileSize = 0;
  19. m_bStatus = ENCODER_NONE;
  20. DBC_SERVA_MEDIA_ENCODER_SETTINGS *tData = NULL;//m_pDBC->getMediaEncoderSettings();
  21. if( tData ){
  22. memcpy( &m_sEncoderSettings, tData, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ) );
  23. } else {
  24. memset( &m_sEncoderSettings, 0, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ));
  25. m_sEncoderSettings.CRF = 20;
  26. strncpy( m_sEncoderSettings.preset, "medium", 15 );
  27. strncpy( m_sEncoderSettings.tune, "fastdecode", 15 );
  28. strncpy( m_sEncoderSettings.profile, "high422", 15 );
  29. m_sEncoderSettings.TH_MODE = FF_THREAD_FRAME;
  30. m_sEncoderSettings.bitrate = 400000;
  31. strncpy( m_sEncoderSettings.CONTAINER, "mp4", 4 );
  32. strncpy( m_sEncoderSettings.CODEC, "h264", 4 );
  33. }
  34. memset( &m_sDecoderVS, 0, sizeof(LIBAV_VS));
  35. memset( &m_sEncoderVS, 0, sizeof(LIBAV_VS));
  36. };
  37. CSTR servaMediaEncoder::getDstFormat(){ return m_sEncoderSettings.CONTAINER; };
  38. servaMediaEncoder::~servaMediaEncoder(){
  39. if (m_sEncoderVS.m_pCodecCtx) {
  40. avcodec_close( m_sEncoderVS.m_pCodecCtx );
  41. avcodec_free_context( &m_sEncoderVS.m_pCodecCtx );
  42. };
  43. if ( m_sEncoderVS.m_pFormatCtx ) {
  44. /* Free the streams. */
  45. for (int i = 0; i < m_sEncoderVS.m_pFormatCtx->nb_streams; i++) {
  46. //av_freep(&m_sEncoderVS.m_pFormatCtx->streams[i]->codec);
  47. av_freep(&m_sEncoderVS.m_pFormatCtx->streams[i]);
  48. };
  49. av_free( m_sEncoderVS.m_pFormatCtx );
  50. };
  51. if (m_sEncoderVS.m_pFormatOpts) av_free( m_sEncoderVS.m_pFormatOpts );
  52. };
  53. #include <sys/stat.h>
  54. bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int height, CSTR custom ){
  55. char Buffer[1024] = {0};
  56. int RetVal;
  57. if ( !initLibAV() ) return false;
  58. strncpy( m_sDecoderVS.m_pFileName, src, MAX_PATH_LEN);
  59. strncpy( m_sEncoderVS.m_pFileName, dst, MAX_PATH_LEN);
  60. struct stat st;
  61. stat( m_sDecoderVS.m_pFileName, &st );
  62. m_nInFileSize = st.st_size;
  63. //LOG("Before Open1");
  64. if( ( RetVal = avformat_open_input( &m_sDecoderVS.m_pFormatCtx, m_sDecoderVS.m_pFileName, NULL, &m_sDecoderVS.m_pFormatOpts) ) != 0 ){
  65. //av_dict_free(&options);
  66. char tBuffer[1024];
  67. av_strerror( RetVal, tBuffer, 1024 );
  68. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_sDecoderVS.m_pFileName );
  69. LOG_ERROR(Buffer);
  70. return false;
  71. };
  72. //LOG("After Open1");
  73. AVDictionaryEntry *e;
  74. if( e = av_dict_get( m_sDecoderVS.m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  75. sprintf( Buffer, "Option %s not recognized by the demuxer", e->key );
  76. LOG_ERROR(Buffer);
  77. };
  78. #ifdef DEBUG_EXTRA
  79. LOG("DataStream Opened");
  80. #endif
  81. m_sDecoderVS.m_bStatus = MS_OPENED;
  82. if( avformat_find_stream_info( m_sDecoderVS.m_pFormatCtx, NULL ) < 0 ){
  83. sprintf(Buffer, "Couldn't find stream information: %s", m_sDecoderVS.m_pFileName );
  84. LOG_ERROR( Buffer );
  85. return false;
  86. };
  87. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  88. for( unsigned int i=0; i < m_sDecoderVS.m_pFormatCtx->nb_streams; i++ ){
  89. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  90. if( m_sDecoderVS.m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  91. #ifdef DEBUG_EXTRA
  92. LOG( "VIDEO stream detected." );
  93. #endif
  94. if( m_sDecoderVS.m_iVideoStreamID <0 ){
  95. #ifdef DEBUG_EXTRA
  96. LOG( "VIDEO stream assigned." );
  97. #endif
  98. m_pVideoState->m_iVideoStreamID=i;
  99. };
  100. };
  101. };
  102. if( m_sDecoderVS.m_iVideoStreamID == -1 ){
  103. LOG_ERROR( "Didn't find a video stream.");
  104. return false;
  105. };
  106. // Stream found
  107. m_sDecoderVS.m_pCodecCtx = m_sDecoderVS.m_pFormatCtx->streams[m_sDecoderVS.m_iVideoStreamID]->codec;
  108. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  109. m_sDecoderVS.m_pVideoCodec = avcodec_find_decoder( m_sDecoderVS.m_pCodecCtx->codec_id );
  110. #else
  111. # ifdef DEBUG_EXTRA
  112. LOG("YABABA-2!");
  113. # endif
  114. /* find the video stream information */
  115. m_sDecoderVS.m_iVideoStreamID = av_find_best_stream( m_sDecoderVS.m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_sDecoderVS.m_pVideoCodec, 0);
  116. if (m_sDecoderVS.m_iVideoStreamID == -1){
  117. LOG_ERROR("Didn't find a video stream.");
  118. return false;
  119. };
  120. if (!(m_sDecoderVS.m_pCodecCtx = avcodec_alloc_context3(m_sDecoderVS.m_pVideoCodec))){
  121. LOG_ERROR("Can not create decoder context!");
  122. return false;
  123. };
  124. m_sDecoderVS.m_pVideoStr = m_sDecoderVS.m_pFormatCtx->streams[m_sDecoderVS.m_iVideoStreamID];
  125. if ( avcodec_parameters_to_context( m_sDecoderVS.m_pCodecCtx, m_sDecoderVS.m_pVideoStr->codecpar ) < 0 ){
  126. LOG_ERROR("Can not set Context parameters!");
  127. return false;
  128. };
  129. #endif
  130. if( m_sDecoderVS.m_pVideoCodec == NULL ){
  131. sprintf(Buffer, "Codec [%d] not found for %s", m_sDecoderVS.m_pCodecCtx->codec_id, m_sDecoderVS.m_pFileName );
  132. LOG_ERROR( Buffer );
  133. return false;
  134. };
  135. /*
  136. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  137. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  138. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  139. */
  140. //m_sDecoderVS.m_pCodecCtx->thread_count = 4;
  141. m_sDecoderVS.m_pCodecCtx->thread_type = m_sEncoderSettings.TH_MODE;//FF_THREAD_FRAME;
  142. //if (is_live)
  143. //m_sDecoderVS.m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  144. //else
  145. //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  146. #ifdef DEBUG_EXTRA
  147. LOG("Before Open2");
  148. #endif
  149. if( avcodec_open2( m_sDecoderVS.m_pCodecCtx, m_sDecoderVS.m_pVideoCodec, NULL ) < 0 ){
  150. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_sDecoderVS.m_pCodecCtx->codec_id, m_sDecoderVS.m_pFileName );
  151. LOG_ERROR( Buffer );
  152. return false;
  153. };
  154. /* we do not send complete frames */
  155. if( m_sDecoderVS.m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_sDecoderVS.m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  156. #ifdef DEBUG_EXTRA
  157. char tB[256];
  158. sprintf( tB, "Setting Frame [%dx%d]", width, height );
  159. LOG( tB );
  160. sprintf( tB, "Source Frame [%dx%d]", m_sDecoderVS.m_pCodecCtx->width, m_sDecoderVS.m_pCodecCtx->height );
  161. LOG( tB );
  162. #endif
  163. /*
  164. if( ( width > 0 ) && ( height > 0 ) ){
  165. m_nOutWidth = width;
  166. m_nOutHeight = height;
  167. } else {
  168. if( m_bSizeIsAutomatic ){
  169. #ifdef DEBUG_EXTRA
  170. LOG("Size is Automatic");
  171. #endif
  172. if(( m_nOutWidth == -1 )&&( m_nOutHeight == -1 )){
  173. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  174. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  175. } else {
  176. // Proportional
  177. if( m_nOutHeight == -1 ){
  178. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  179. m_nOutHeight = (int)(((float)m_nOutWidth ) * k );
  180. } else {
  181. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  182. m_nOutWidth = (int)(((float)m_nOutHeight ) * k );
  183. };
  184. };
  185. } else {
  186. #ifdef DEBUG_EXTRA
  187. LOG("Size is Manual");
  188. #endif
  189. if( ( width > 0 ) && ( height > 0 ) ){
  190. m_nOutWidth = width;
  191. m_nOutHeight = height;
  192. } else {
  193. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  194. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  195. };
  196. };
  197. };
  198. #ifdef DEBUG_EXTRA
  199. sprintf( tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight );
  200. LOG( tB );
  201. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  202. m_pVideoState->m_pCodecCtx->height,
  203. m_pVideoState->m_pCodecCtx->pix_fmt,
  204. m_nOutWidth,
  205. m_nOutHeight,
  206. AV_PIX_FMT_BGR24,
  207. SWS_BICUBIC );
  208. LOG( tB );
  209. #endif
  210. m_pVideoState->m_pSwsCtx = sws_getContext(
  211. m_pVideoState->m_pCodecCtx->width,
  212. m_pVideoState->m_pCodecCtx->height,
  213. m_pVideoState->m_pCodecCtx->pix_fmt,
  214. m_nOutWidth,
  215. m_nOutHeight,
  216. AV_PIX_FMT_BGR24,
  217. SWS_BICUBIC, //SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  218. NULL,
  219. NULL,
  220. NULL
  221. );
  222. if ( m_pVideoState->m_pSwsCtx == NULL ){
  223. LOG_ERROR("Error Allocating SwsCtx!");
  224. return false;
  225. };
  226. */
  227. m_bStatus = DECODER_INIT;
  228. /* Encoder */
  229. m_sEncoderVS.m_pFormatCtx = NULL;
  230. m_sEncoderVS.m_pFormatOpts = NULL;
  231. int ret = avformat_alloc_output_context2( &m_sEncoderVS.m_pFormatCtx, NULL, NULL, m_sEncoderVS.m_pFileName );
  232. if (ret < 0) {
  233. LOG_ERROR("VideoFormat Context Memory Allocation Error");
  234. return false;
  235. };
  236. LOG(m_sEncoderVS.m_pFileName);
  237. m_sEncoderVS.m_pFormatCtx->oformat = av_guess_format( NULL, m_sEncoderVS.m_pFileName, NULL);
  238. if (m_sEncoderVS.m_pFormatCtx->oformat == NULL){
  239. LOG_ERROR("VideoFormat Format Not Guessed!");
  240. return false;
  241. #ifdef DEBUG_EXTRA
  242. } else {
  243. LOG("Video Container Format Guessed");
  244. #endif
  245. };
  246. //In Case mp4
  247. if ( strcasestr( m_sEncoderSettings.CONTAINER, "mp4" ) ) {
  248. if (av_dict_set(&m_sEncoderVS.m_pFormatOpts, "brand", "mp42", 0) < 0) {
  249. LOG_ERROR( "av_dict_set brand!" );
  250. };
  251. };
  252. m_sEncoderVS.m_pVideoStr = avformat_new_stream( m_sEncoderVS.m_pFormatCtx, m_sEncoderVS.m_pVideoCodec );
  253. if( m_sEncoderVS.m_pVideoStr == NULL ){
  254. LOG_ERROR("VideoStream Not Created!");
  255. return false;
  256. #ifdef DEBUG_EXTRA
  257. }else{
  258. LOG("VideoStream is Created");
  259. #endif
  260. };
  261. //m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder(av_guess_codec(m_sEncoderVS.m_pFormatCtx->oformat, NULL, m_sEncoderVS.m_pFileName, NULL, AVMEDIA_TYPE_VIDEO));
  262. if ( strcasestr( m_sEncoderSettings.CODEC, "h264" ) ) {
  263. m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder( AV_CODEC_ID_H264 );
  264. } else {
  265. m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder( AV_CODEC_ID_H265 );
  266. };
  267. if( m_sEncoderVS.m_pVideoCodec == NULL ){
  268. LOG_ERROR("VideoCodec Not Found!");
  269. return false;
  270. #ifdef DEBUG_EXTRA
  271. } else {
  272. LOG("Video Codec Found");
  273. #endif
  274. };
  275. m_sEncoderVS.m_pCodecCtx = avcodec_alloc_context3( m_sEncoderVS.m_pVideoCodec );
  276. if (m_sEncoderVS.m_pCodecCtx == NULL){
  277. LOG_ERROR("VideoCodec Context Memory Allocation Error");
  278. return false;
  279. #ifdef DEBUG_EXTRA
  280. } else {
  281. LOG("Codec Context3 Allocated");
  282. #endif
  283. };
  284. m_sEncoderVS.m_pCodecCtx->width = m_sDecoderVS.m_pCodecCtx->width;
  285. m_sEncoderVS.m_pCodecCtx->height = m_sDecoderVS.m_pCodecCtx->height;
  286. //const AVRational dst_fps = { fps, 1 };
  287. m_sEncoderVS.m_pVideoStr->time_base = av_make_q( 1, 10000 );// .num = 1;//av_inv_q(dst_fps);
  288. //m_sEncoderVS.m_pVideoStr->time_base.den = fps;
  289. m_sEncoderVS.m_pVideoStr->r_frame_rate = av_make_q( fps, 1);//.num = fps;
  290. //m_sEncoderVS.m_pVideoStr->r_frame_rate.den = 1;
  291. m_sEncoderVS.m_pVideoStr->avg_frame_rate = av_make_q( fps, 1); //.num = fps;
  292. //m_sEncoderVS.m_pVideoStr->avg_frame_rate.den = 1;
  293. m_sEncoderVS.m_pCodecCtx->time_base = av_make_q( 1, 10000 );//.num = 1;
  294. //m_sEncoderVS.m_pCodecCtx->time_base.den = fps;
  295. m_sEncoderVS.m_pCodecCtx->gop_size = fps << 1; // emit one intra frame every fps*2 frames
  296. m_sEncoderVS.m_pCodecCtx->bit_rate = m_sEncoderSettings.bitrate; //400000;
  297. m_sEncoderVS.m_pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
  298. //if (codec_id == AV_CODEC_ID_H264)
  299. //av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "slow", 0);
  300. av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", m_sEncoderSettings.preset, 0 );
  301. av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "profile", m_sEncoderSettings.profile, 0 );
  302. //m_pCodecCtx->max_b_frames = 1;
  303. //m_pCodecCtx->level = 31;
  304. /// P-frames, generated by referencing data from prev and future frames.
  305. /// [Compression up, CPU usage up]
  306. /// [use 3/gop]
  307. m_sEncoderVS.m_pCodecCtx->max_b_frames = 5;
  308. /// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
  309. /// [use 3�5 ref per P]
  310. m_sEncoderVS.m_pCodecCtx->refs = 3;
  311. /// Compression efficiency (slower -> better quality + higher cpu%)
  312. /// [ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow]
  313. /// Set this option to "ultrafast" is critical for realtime encoding
  314. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "ultrafast", 0);
  315. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "fast", 0 );
  316. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "slow", 0 );
  317. /// Compression rate (lower -> higher compression) compress to lower size, makes decoded image more noisy
  318. /// Range: [0; 51], sane range: [18; 26]. I used 35 as good compression/quality compromise. This option also critical for realtime encoding
  319. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "crf", "20", 0);
  320. char tBuf[16]; sprintf( tBuf, "%d", m_sEncoderSettings.CRF );
  321. av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "crf", tBuf, 0 );
  322. /// Change settings based upon the specifics of input
  323. /// [psnr, ssim, grain, zerolatency, fastdecode, animation]
  324. /// This option is most critical for realtime encoding, because it removes delay between 1th input frame and 1th output packet.
  325. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "tune", "zerolatency", 0);
  326. av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "tune", m_sEncoderSettings.tune, 0);
  327. /// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
  328. /// Because Quicktime (ie. iOS) doesn't support this option
  329. av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "b-pyramid", "0", 0 );
  330. if( avcodec_is_open( m_sEncoderVS.m_pCodecCtx ) == 0 ){
  331. #ifdef DEBUG_EXTRA
  332. LOG("output codec context is closed, opening");
  333. #endif
  334. int ret = avcodec_open2( m_sEncoderVS.m_pCodecCtx, m_sEncoderVS.m_pVideoCodec, NULL );
  335. if( ret < 0 ){
  336. LOG_ERROR("Error opening2 encoder");
  337. char buffer[30];
  338. sprintf(buffer, "Error code: %i", ret );
  339. LOG_ERROR(buffer);
  340. return false;
  341. };
  342. #ifdef DEBUG_EXTRA
  343. } else {
  344. LOG("output codec context is opened");
  345. #endif
  346. };
  347. m_sEncoderVS.m_pVideoStr->codec = m_sEncoderVS.m_pCodecCtx; //---->see next page
  348. if (avio_open(&m_sEncoderVS.m_pFormatCtx->pb, m_sEncoderVS.m_pFileName, AVIO_FLAG_WRITE ) < 0){
  349. LOG_ERROR("Could not open output file.");
  350. return false;
  351. };
  352. if (avformat_write_header(m_sEncoderVS.m_pFormatCtx, &m_sEncoderVS.m_pFormatOpts ) < 0){
  353. LOG_ERROR("Error occurred when writing header to output file.");
  354. return false;
  355. };
  356. m_sEncoderVS.m_iVideoStreamID = -1;
  357. #ifdef DEBUG_EXTRA
  358. LOG("Allocation Done");
  359. #endif
  360. m_bStatus = ENCODER_INIT;
  361. return init();
  362. };
  363. void servaMediaEncoder::m_fThreadProc( void ){
  364. AVPacket read_packet;
  365. AVPacket encoded_packet;
  366. uint32_t current_frame_num = 0;
  367. uint32_t frame_count = 0;
  368. uint8_t operc = 0;
  369. uint64_t pack_duration = m_sEncoderVS.m_pCodecCtx->time_base.den / m_sEncoderVS.m_pCodecCtx->time_base.num / m_sEncoderVS.m_pVideoStr->avg_frame_rate.num * m_sEncoderVS.m_pVideoStr->avg_frame_rate.den;
  370. int got_frame;
  371. int test;
  372. m_bStatus = THREAD_INIT;
  373. // Allocate video frame
  374. AVFrame *decoded_frame = av_frame_alloc();
  375. m_bThreadStatus = TH_WORKING;
  376. m_bStatus = ENCODER_ENCODING;
  377. m_nReadSize = 0;
  378. while( av_read_frame( m_sDecoderVS.m_pFormatCtx, &read_packet ) >= 0 && m_bThreadStatus < TH_ENDED ){
  379. m_nReadSize += read_packet.size;
  380. // Is this a packet from the video stream?
  381. if( read_packet.stream_index == m_sDecoderVS.m_iVideoStreamID ){
  382. // Decode video frame
  383. if( avcodec_decode_video2( m_sDecoderVS.m_pCodecCtx, decoded_frame, &got_frame, &read_packet ) < 0 ){
  384. m_bThreadStatus = TH_ENDED;
  385. continue;
  386. };
  387. // Did we get a video frame?
  388. if( got_frame ){
  389. av_init_packet( &encoded_packet );
  390. encoded_packet.data = NULL;
  391. encoded_packet.size = 0;
  392. decoded_frame->pts = (int64_t)current_frame_num * pack_duration;
  393. uint8_t perc = ( 100 * m_nReadSize ) / m_nInFileSize;
  394. if ( operc!= perc ) {
  395. operc == perc;
  396. m_pMaster->updateEncodeStatus(m_nRefID, MEDIA_ENCODE_PROGRESS, perc);
  397. };
  398. if( avcodec_encode_video2( m_sEncoderVS.m_pCodecCtx, &encoded_packet, decoded_frame, &got_frame) < 0 ){
  399. m_bThreadStatus = TH_ENDED;
  400. continue;
  401. };
  402. if( got_frame ){
  403. //if( m_sEncoderVS.m_pCodecCtx->coded_frame->pts != AV_NOPTS_VALUE ) encoded_packet.pts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
  404. if( m_sEncoderVS.m_pCodecCtx->coded_frame->key_frame ) encoded_packet.flags |= AV_PKT_FLAG_KEY;
  405. encoded_packet.stream_index = m_sEncoderVS.m_pVideoStr->index;
  406. //encoded_packet.dts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
  407. encoded_packet.duration = pack_duration;
  408. if( av_interleaved_write_frame( m_sEncoderVS.m_pFormatCtx, &encoded_packet ) < 0 ){
  409. m_bThreadStatus = TH_ENDED;
  410. continue;
  411. } else {
  412. m_nOutFileSize += encoded_packet.size;
  413. current_frame_num++;
  414. };
  415. };
  416. frame_count++;
  417. av_free_packet( &encoded_packet );
  418. };
  419. };
  420. // Free the packet that was allocated by av_read_frame
  421. av_free_packet( &read_packet );
  422. };
  423. // Close the video file
  424. avformat_close_input( &m_sDecoderVS.m_pFormatCtx );
  425. // Free the YUV frame
  426. av_frame_free( &decoded_frame );
  427. av_write_trailer( m_sEncoderVS.m_pFormatCtx );
  428. avio_close( m_sEncoderVS.m_pFormatCtx->pb );
  429. m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_ENCODE_PROGRESS, 100 );
  430. m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_READY, 100 );
  431. unlink( m_sDecoderVS.m_pFileName );
  432. #ifdef DEBUG_EXTRA
  433. LOG("Encoding Done!");
  434. #endif
  435. m_bStatus = ENCODER_FINISHED;
  436. };
  437. uint8_t servaMediaEncoder::getStatus(){
  438. return m_bStatus;
  439. };
  440. #undef LOG
  441. #undef LOG_ERROR