21 static AVCodecContext* g_pAudio; |
21 static AVCodecContext* g_pAudio; |
22 static AVCodecContext* g_pVideo; |
22 static AVCodecContext* g_pVideo; |
23 |
23 |
24 static int g_Width, g_Height; |
24 static int g_Width, g_Height; |
25 static uint32_t g_Frequency, g_Channels; |
25 static uint32_t g_Frequency, g_Channels; |
26 static int g_VQuality, g_AQuality; |
26 static int g_VQuality; |
27 static AVRational g_Framerate; |
27 static AVRational g_Framerate; |
28 static const char* g_pPreset; |
|
29 |
28 |
30 static FILE* g_pSoundFile; |
29 static FILE* g_pSoundFile; |
31 static int16_t* g_pSamples; |
30 static int16_t* g_pSamples; |
32 static int g_NumSamples; |
31 static int g_NumSamples; |
33 |
32 |
108 g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16; |
107 g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16; |
109 g_pAudio->sample_rate = g_Frequency; |
108 g_pAudio->sample_rate = g_Frequency; |
110 g_pAudio->channels = g_Channels; |
109 g_pAudio->channels = g_Channels; |
111 |
110 |
112 // set quality |
111 // set quality |
113 if (g_AQuality > 100) |
112 g_pAudio->bit_rate = 160000; |
114 g_pAudio->bit_rate = g_AQuality; |
113 |
115 else |
114 // for codecs that support variable bitrate use it, it should be better |
116 { |
115 g_pAudio->flags |= CODEC_FLAG_QSCALE; |
117 g_pAudio->flags |= CODEC_FLAG_QSCALE; |
116 g_pAudio->global_quality = 1*FF_QP2LAMBDA; |
118 g_pAudio->global_quality = g_AQuality*FF_QP2LAMBDA; |
|
119 } |
|
120 |
117 |
121 // some formats want stream headers to be separate |
118 // some formats want stream headers to be separate |
122 if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
119 if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
123 g_pAudio->flags |= CODEC_FLAG_GLOBAL_HEADER; |
120 g_pAudio->flags |= CODEC_FLAG_GLOBAL_HEADER; |
124 |
121 |
238 |
235 |
239 // some formats want stream headers to be separate |
236 // some formats want stream headers to be separate |
240 if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
237 if (g_pFormat->flags & AVFMT_GLOBALHEADER) |
241 g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER; |
238 g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER; |
242 |
239 |
|
240 #if LIBAVCODEC_VERSION_MAJOR < 54 |
|
241 // for some versions of ffmpeg x264 options must be set explicitly |
|
242 if (strcmp(g_pVCodec->name, "libx264") == 0) |
|
243 { |
|
244 g_pVideo->coder_type = FF_CODER_TYPE_AC; |
|
245 g_pVideo->flags |= CODEC_FLAG_LOOP_FILTER; |
|
246 g_pVideo->crf = 23; |
|
247 g_pVideo->thread_count = 3; |
|
248 g_pVideo->me_cmp = FF_CMP_CHROMA; |
|
249 g_pVideo->partitions = X264_PART_I8X8 | X264_PART_I4X4 | X264_PART_P8X8 | X264_PART_B8X8; |
|
250 g_pVideo->me_method = ME_HEX; |
|
251 g_pVideo->me_subpel_quality = 7; |
|
252 g_pVideo->me_range = 16; |
|
253 g_pVideo->gop_size = 250; |
|
254 g_pVideo->keyint_min = 25; |
|
255 g_pVideo->scenechange_threshold = 40; |
|
256 g_pVideo->i_quant_factor = 0.71; |
|
257 g_pVideo->b_frame_strategy = 1; |
|
258 g_pVideo->qcompress = 0.6; |
|
259 g_pVideo->qmin = 10; |
|
260 g_pVideo->qmax = 51; |
|
261 g_pVideo->max_qdiff = 4; |
|
262 g_pVideo->max_b_frames = 3; |
|
263 g_pVideo->refs = 3; |
|
264 g_pVideo->directpred = 1; |
|
265 g_pVideo->trellis = 1; |
|
266 g_pVideo->flags2 = CODEC_FLAG2_BPYRAMID | CODEC_FLAG2_MIXED_REFS | CODEC_FLAG2_WPRED | CODEC_FLAG2_8X8DCT | CODEC_FLAG2_FASTPSKIP; |
|
267 g_pVideo->weighted_p_pred = 2; |
|
268 } |
|
269 #endif |
|
270 |
243 // open the codec |
271 // open the codec |
244 #if LIBAVCODEC_VERSION_MAJOR >= 53 |
272 #if LIBAVCODEC_VERSION_MAJOR >= 53 |
245 AVDictionary* pDict = NULL; |
273 AVDictionary* pDict = NULL; |
246 if (strcmp(g_pVCodec->name, "libx264") == 0) |
274 if (strcmp(g_pVCodec->name, "libx264") == 0) |
247 av_dict_set(&pDict, "preset", g_pPreset, 0); |
275 av_dict_set(&pDict, "preset", "medium", 0); |
248 |
276 |
249 if (avcodec_open2(g_pVideo, g_pVCodec, &pDict) < 0) |
277 if (avcodec_open2(g_pVideo, g_pVCodec, &pDict) < 0) |
250 #else |
278 #else |
251 if (avcodec_open(g_pVideo, g_pVCodec) < 0) |
279 if (avcodec_open(g_pVideo, g_pVCodec) < 0) |
252 #endif |
280 #endif |
346 const char* pDesc, |
374 const char* pDesc, |
347 const char* pSoundFile, |
375 const char* pSoundFile, |
348 const char* pFormatName, |
376 const char* pFormatName, |
349 const char* pVCodecName, |
377 const char* pVCodecName, |
350 const char* pACodecName, |
378 const char* pACodecName, |
351 const char* pVPreset, |
|
352 int Width, int Height, |
379 int Width, int Height, |
353 int FramerateNum, int FramerateDen, |
380 int FramerateNum, int FramerateDen, |
354 int VQuality, int AQuality) |
381 int VQuality) |
355 { |
382 { |
356 AddFileLogRaw = pAddFileLogRaw; |
383 AddFileLogRaw = pAddFileLogRaw; |
357 av_log_set_callback( &LogCallback ); |
384 av_log_set_callback( &LogCallback ); |
358 |
385 |
359 g_Width = Width; |
386 g_Width = Width; |
360 g_Height = Height; |
387 g_Height = Height; |
361 g_Framerate.num = FramerateNum; |
388 g_Framerate.num = FramerateNum; |
362 g_Framerate.den = FramerateDen; |
389 g_Framerate.den = FramerateDen; |
363 g_VQuality = VQuality; |
390 g_VQuality = VQuality; |
364 g_AQuality = AQuality; |
|
365 g_pPreset = pVPreset; |
|
366 |
391 |
367 // initialize libav and register all codecs and formats |
392 // initialize libav and register all codecs and formats |
368 av_register_all(); |
393 av_register_all(); |
369 |
394 |
370 // find format |
395 // find format |