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.

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