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.

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