ffmpeg change h264 to jpeg ok.

This commit is contained in:
Fancy code 2024-07-20 00:33:26 +08:00
parent a4bd40a847
commit 1b7ff22112
2 changed files with 31 additions and 19 deletions

View File

@ -118,22 +118,22 @@ bool FfmpegDecoder::Init(void)
bool FfmpegDecoder::UnInit(void)
{
LogInfo("uninit %s\n", avcodec_get_name(mCodecId));
if (mFrame) {
av_frame_free(&mFrame);
mFrame = nullptr;
}
if (mCodecCtx) {
if (mCodecId != AV_CODEC_ID_H264) {
avcodec_free_context(&mCodecCtx);
mCodecCtx = nullptr;
}
}
av_packet_free(&mPacket);
mPacket = nullptr;
if (mParser) {
av_parser_close(mParser);
mParser = nullptr;
}
// if (mCodecCtx) {
// // if (mCodecId != AV_CODEC_ID_H264) {
// // }
// }
avcodec_free_context(&mCodecCtx);
mCodecCtx = nullptr;
if (mFrame) {
av_frame_free(&mFrame);
mFrame = nullptr;
}
return true;
}
void FfmpegDecoder::DecodeData(const void *data, const size_t &size, const unsigned long long &pts,

View File

@ -68,6 +68,10 @@ void FfmpegThumbnail::UnInit(void)
}
avformat_free_context(mOutputFormat);
fx_system_v2("sync");
if (sws_ctx) {
sws_freeContext(sws_ctx);
sws_ctx = nullptr;
}
}
bool FfmpegThumbnail::CreateThumbnail(const std::string &outputFile, const void *data, const size_t &size)
{
@ -112,13 +116,13 @@ bool FfmpegThumbnail::CreateThumbnail(const std::string &outputFile, const void
av_dump_format(mOutputFormat, 0, outputFile.c_str(), 1);
/* open the output file, if needed */
// if (!(mOutputFormat->oformat->flags & AVFMT_NOFILE)) {
ret = avio_open(&mOutputFormat->pb, outputFile.c_str(), AVIO_FLAG_WRITE);
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
LogError("Could not open '%s': %s\n",
outputFile.c_str(),
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
}
// ret = avio_open(&mOutputFormat->pb, outputFile.c_str(), AVIO_FLAG_WRITE);
// if (ret < 0) {
// char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
// LogError("Could not open '%s': %s\n",
// outputFile.c_str(),
// av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
// }
LogInfo("Open output file\n");
// }
av_dict_set_int(&opt, "use_editlist", 0, 0);
@ -132,6 +136,7 @@ bool FfmpegThumbnail::CreateThumbnail(const std::string &outputFile, const void
return false;
}
av_dict_free(&opt);
// return true;
mDecoder->Init();
mEncoder->Init(mOutputFormat->flags);
mStream->time_base = mEncoder->GetTimeBase();
@ -148,7 +153,11 @@ void FfmpegThumbnail::GetDecodeDataCallback(AVFrame *frame)
output_frame->format = AV_PIX_FMT_YUV420P;
output_frame->width = 640;
output_frame->height = 480;
int jpeg_buf_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 640, 480, 1);
// output_frame->linesize[0] = 640; // Y plane 的跨度
// output_frame->linesize[1] = 320; // U/V planes 的跨度
// output_frame->linesize[2] = 320; // U/V planes 的跨度
int jpeg_buf_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 1920, 2160, 1);
LogInfo("jpeg_buf_size: %d\n", jpeg_buf_size);
uint8_t *jpeg_buf = (uint8_t *)av_malloc(jpeg_buf_size);
av_image_fill_arrays(
@ -169,8 +178,11 @@ void FfmpegThumbnail::GetDecodeDataCallback(AVFrame *frame)
if (mEncoder) {
mEncoder->EncodeData(output_frame, mStream, mEncodeCallback);
return;
// return;
}
av_frame_free(&output_frame);
av_free(jpeg_buf);
return;
}
void FfmpegThumbnail::GetEncodeDataCallback(AVPacket *pkt)
{