視訊轉碼 via FFmpeg
- FFmpeg 簡介
- FFmpeg 指令行轉碼
- FFmpeg API 轉碼
-
- Transcoding 流程圖
- Transcoding 代碼
-
- open_input_file 函數
- open_output_video_file 函數
- HW_dec_helper::init 函數
- init_cvt_frame_and_sws 函數
- video_transcode 函數
-
- decode_av_frame 函數
-
- HW_dec_helper::convert_frame 函數
- encode_av_frame 函數
- flush_encoder 函數
- 其他架構的轉碼
轉碼(transcoding)其實就是把音頻從一種編碼轉換成另一種編碼的過程,如 mpg2 → h.264。基本流程如下圖:
FFmpeg 簡介
FFmpeg 是一套可以用來記錄、轉換數字音頻、視訊,并能将其轉化為流的開源計算機程式。采用 LGPL 或 GPL 許可證。它提供了錄制、轉換以及流化音視訊的完整解決方案。它包含了非常先進的音頻 / 視訊編解碼庫 libavcodec,為了保證高可移植性和編解碼品質,libavcodec 裡很多 code 都是從頭開發的。
FFmpeg 在 Linux 平台下開發,但它同樣也可以在其它作業系統環境中編譯運作,包括 Windows、Mac OS X 等。這個項目最早由 Fabrice Bellard 發起,2004 年至 2015 年間由 Michael Niedermayer 主要負責維護。許多 FFmpeg 的開發人員都來自 MPlayer 項目,而且目前 FFmpeg 也是放在 MPlayer 項目組的伺服器上。項目的名稱來自 MPEG 視訊編碼标準,前面的 “FF” 代表 “Fast Forward”。
FFmpeg 指令行轉碼
FFmpeg 提供了指令行的方式對視訊(含音頻)進行轉碼:
>ffmpeg.exe -i test.mp4 -s 640*360 test.mpg
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
Metadata:
Duration: 00:00:01.90, start: 0.000000, bitrate: 14050 kb/s
Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 13912 kb/s, 30 fps, 30 tbr, 30k tbn, 60 tbc (default)
Metadata:
encoder : AVC Coding
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 32000 Hz, stereo, fltp, 130 kb/s (default)
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mpeg1video (native))
Stream #0:1 -> #0:1 (aac (native) -> mp2 (native))
Output #0, mpeg, to 'test.mpg':
Metadata:
encoder : Lavf58.20.100
Stream #0:0(eng): Video: mpeg1video, yuv420p(progressive), 640x360 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 30 fps, 90k tbn, 30 tbc (default)
Metadata:
encoder : Lavc58.35.100 mpeg1video
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
Stream #0:1(eng): Audio: mp2, 32000 Hz, stereo, s16, 384 kb/s (default)
Metadata:
encoder : Lavc58.35.100 mp2
frame=56 fps=0.0 q=31.0 Lsize=230kB time=00:00:01.89 bitrate=993.2kbits/s speed=3.79x
video:134kB audio:91kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.332372%
FFmpeg API 轉碼
Transcoding 流程圖
Transcoding 代碼
以下是整個轉碼過程的概要代碼,略去各個函數的具體實作和資源釋放:
本文中的代碼基于 FFmpeg 4.1。
int video_index = open_input_file(in_file, AVMEDIA_TYPE_VIDEO, &in_fmt_ctx, &dec_ctx);
double frame_rate = av_q2d(av_guess_frame_rate(in_fmt_ctx, in_fmt_ctx->streams[video_index], NULL));
hr = open_output_video_file(out_file, dec_ctx, 400 * 1000, (int)ceil(frame_rate), &out_fmt_ctx, &enc_ctx);
if (is_hw_dec)
hr = hw_decoder.init(hw_type_name, dec_ctx);
hr = init_cvt_frame_and_sws(AV_PIX_FMT_YUV420P, dec_ctx, &yuv420p_frame, &yuv420p_buffer, &sws_ctx);
hr = avformat_write_header(out_fmt_ctx, NULL);
while (_kbhit() == 0) {
int finished = 0;
hr = video_transcode(
in_fmt_ctx, dec_ctx,
out_fmt_ctx, enc_ctx,
video_index, yuv420p_frame,
sws_ctx, &finished );
if (finished)
break;
}
flush_encoder(out_fmt_ctx, enc_ctx, false, false);
hr = av_write_trailer(out_fmt_ctx);
open_input_file 函數
請看 這裡,一模一樣。
open_output_video_file 函數
請看 這裡,一模一樣。
HW_dec_helper::init 函數
硬解碼器的初始化。
int HW_dec_helper::init(char* hw_type_name, AVCodecContext *decoder_ctx, AVCodec *decoder = NULL)
{
AVHWDeviceType hw_type = av_hwdevice_find_type_by_name(hw_type_name);
RETURN_IF_FALSE(hw_type != AV_HWDEVICE_TYPE_NONE);
if (decoder == NULL)
decoder = (AVCodec*)decoder_ctx->codec;
for (int i = 0; ; i++) {
const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
RETURN_IF_NULL(config);
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
config->device_type == hw_type) {
m_hw_pix_fmt = config->pix_fmt;
break;
}
}
decoder_ctx->get_format = get_hw_format;
int hr = av_hwdevice_ctx_create(&m_hw_device_ctx, hw_type, NULL, NULL, 0);
RETURN_IF_FAILED(hr);
decoder_ctx->hw_device_ctx = av_buffer_ref(m_hw_device_ctx);
return 0;
}
init_cvt_frame_and_sws 函數
請看 這裡,一模一樣。
video_transcode 函數
解碼 → 色彩空間轉換 → 編碼 的過程。
const int output_frame_size = enc_ctx->frame_size;
std::vector<AVFrame*> decoded_frames;
while (true) {
hr = decode_av_frame(in_fmt_ctx, dec_ctx, video_index, decoded_frames, finished);
for (size_t dec_frame_idx = 0; dec_frame_idx < decoded_frames.size(); ++dec_frame_idx) {
AVFrame* frame = decoded_frames[dec_frame_idx];
// convert to YUV420P format
int height = sws_scale(sws_ctx, (const uint8_t* const*)frame->data, frame->linesize, 0,
dec_ctx->height, yuv420p_frame->data, yuv420p_frame->linesize);
av_frame_copy_props(yuv420p_frame, frame);
yuv420p_frame->pict_type = AV_PICTURE_TYPE_NONE;
int data_written = 0;
hr = encode_av_frame(yuv420p_frame, out_fmt_ctx, enc_ctx, &data_written, interleaved, init_pts);
if (hr == AVERROR(EAGAIN))
continue;
GOTO_IF_FAILED(hr);
}
if (SUCCEEDED(hr))
break;
}
if (*finished)
flush_encoder(out_fmt_ctx, enc_ctx, interleaved, init_pts);
decode_av_frame 函數
這裡和之前的解碼函數不一樣的地方就是增加了硬解碼的處理。
init_packet(&input_packet);
while (true) {
hr = av_read_frame(in_fmt_ctx, &input_packet);
if (hr == AVERROR_EOF)
*finished = 1;
else
av_packet_rescale_ts(&input_packet, in_fmt_ctx->streams[input_packet.stream_index]->time_base, in_codec_ctx->time_base);
hr = avcodec_send_packet(in_codec_ctx, *finished ? NULL : &input_packet);
if (SUCCEEDED(hr) || (hr == AVERROR(EAGAIN))) {
while (true) {
frame = av_frame_alloc();
hr = avcodec_receive_frame(in_codec_ctx, frame);
if (SUCCEEDED(hr)) {
// decoded by hardware
AVFrame* sw_frame = HW_dec_helper::convert_frame(frame);
frames.push_back(sw_frame);
av_frame_free(&frame);
}
else if (hr == AVERROR_EOF) {
*finished = 1;
break;
}
else if (hr == AVERROR(EAGAIN)) // need more packets
break;
}
}
else if (hr == AVERROR_EOF)
*finished = 1;
if (*finished || !frames.empty())
break;
}
HW_dec_helper::convert_frame 函數
從顯存中拷貝 frame 到記憶體中。
AVFrame* HW_dec_helper::convert_frame(AVFrame* frame)
{
if (frame->format == m_hw_pix_fmt) {
AVFrame* sw_frame = av_frame_alloc();
int hr = av_hwframe_transfer_data(sw_frame, frame, 0);
GOTO_LABEL_IF_FAILED(hr, OnErr);
av_frame_copy_props(sw_frame, frame);
return sw_frame;
OnErr:
if (NULL != sw_frame)
av_frame_free(&sw_frame);
return NULL;
}
else
return frame;
}
encode_av_frame 函數
請看 這裡,一模一樣。
flush_encoder 函數
請看 這裡,一模一樣。
其他架構的轉碼
關于 Media Foundation 的視訊轉碼請參考 這裡。
– EOF –