Commit fdc9f791 authored by yang.jie's avatar yang.jie

add flush encoder

parent cdf5808f
......@@ -10,7 +10,7 @@ echo "WORKPATH"=$WORKPATH
rm -rf ${WORKPATH}/demo/mp4encoder.js ${WORKPATH}/demo/mp4encoder.wasm
FFMPEG_ST=yes
FFMPEG_ST=no
EMSDK=/emsdk
......
......@@ -2,6 +2,14 @@ cmake_minimum_required(VERSION 3.10.0)
project(transcode_mp3)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "\
-pthread \
-Wno-deprecated-declarations \
-Wno-pointer-sign \
-Wno-implicit-int-float-conversion \
-Wno-switch -Wno-parentheses\
-Qunused-arguments \
")
# set(CMAKE_EXECUTABLE_SUFFIX ".html") # 编译生成.html
set( CMAKE_VERBOSE_MAKEFILE on )
......@@ -24,17 +32,11 @@ add_subdirectory(audio)
add_subdirectory(ffmbase)
set_target_properties(transcode_mp3 PROPERTIES LINK_FLAGS "\
-Wno-deprecated-declarations \
-Wno-pointer-sign \
-Wno-implicit-int-float-conversion \
-Wno-switch -Wno-parentheses\
-Qunused-arguments \
-s FORCE_FILESYSTEM=1 \
-s WASM=1 \
-s USE_SDL=2 \
-s INVOKE_RUN=0 \
-s EXIT_RUNTIME=1 \
-s MODULARIZE=1 \
-s EXPORT_NAME=\"createTrancodeMp3\" \
-s EXPORTED_FUNCTIONS=\"[_main,_malloc,_free]\" \
-s EXPORTED_RUNTIME_METHODS=\"[FS, cwrap, ccall, setValue, writeAsciiToMemory]\" \
......@@ -42,6 +44,10 @@ set_target_properties(transcode_mp3 PROPERTIES LINK_FLAGS "\
-s ALLOW_MEMORY_GROWTH=1 \
--pre-js ${PRE_PATH}/pre.js \
--post-js ${POST_PATH}/post.js \
-pthread \
-s USE_PTHREADS=1 \
-s PROXY_TO_PTHREAD=1 \
-s PTHREAD_POOL_SIZE=8\
")
target_link_libraries(${PROJECT_NAME} PRIVATE audio)
......
......@@ -56,11 +56,8 @@ namespace FFM {
void FFAudioDecoder::startDecoder()
{
// std::thread t([this]() {
// decodeAudio();
// });
// t.join();
decodeAudio();
}
int FFAudioDecoder::decode(int *gotframe)
......@@ -168,10 +165,14 @@ namespace FFM {
//}
}
}
av_packet_unref(m_pPkt);
}
if (m_audioEncoder) {
m_audioEncoder->flush_encoder();
}
printf("------------------count=%d\n", count);
/*if (m_audioEncoder) {
m_audioEncoder->closeEncoder();
......
......@@ -834,12 +834,47 @@ namespace FFM {
printf("File duration: %02d:%02d:%02d\n", thh, tmm, tss);
}
int FFAudioEncoder::writePacket(AVPacket *pkt)
{
int ret = -1;
av_packet_rescale_ts(pkt, m_pCodecCtx->time_base, m_pStream->time_base);
pkt->stream_index = m_pStream->index;
//AVStream *in_stream = m_pInStream;
////pkt.pts = av_rescale_q(m_swr->getLastTime(), m_swr->getTimeBase(), m_pFormatCtx->streams[pkt.stream_index]->time_base);
////
////pkt.pts = av_frame_get_best_effort_timestamp(filter_frame);
////pkt.dts = pkt.pts;
////pkt.pts = pkt.dts = m_frameIndex++ *( m_pCodecCtx->frame_size * 1000 / m_pCodecCtx->sample_rate);
pkt->pts = av_rescale_q_rnd(pkt->pts, m_pInCodeCtx->time_base, m_pStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt->dts = av_rescale_q_rnd(pkt->dts, m_pInCodeCtx->time_base, m_pStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt->duration = av_rescale_q(pkt->duration, m_pInCodeCtx->time_base, m_pStream->time_base);
//pkt.pos = -1;
printf("timestamp: %lf\n", pkt->pts*av_q2d(m_pStream->time_base));
printf("pkt.size: %d, frame.nb_samples: %d\npts: %ld, dts: %ld, duration: %ld\n", pkt->size,\
m_pStream->codec->frame_size,\
pkt->pts, pkt->dts, pkt->duration);
ret = av_interleaved_write_frame(m_pFormatCtx, pkt);
return ret;
}
int FFAudioEncoder::encodeAudio(AVCodecContext *inCtx, AVFrame *frame, uint64_t starttime)
{
uint64_t time = av_gettime() - starttime;
int ret = -1;
if (!m_pFormatCtx) return -1;
if (!m_pFormatCtx || !inCtx) return -1;
m_pInCodeCtx = inCtx;
AVFrame *outframe = nullptr;
......@@ -886,39 +921,15 @@ namespace FFM {
}
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = nullptr;
pkt.size = 0;
av_init_packet(&pkt);
while (avcodec_receive_packet(m_pCodecCtx, &pkt) >= 0)
{
printf("encoding.\n");
av_packet_rescale_ts(&pkt, m_pCodecCtx->time_base, m_pStream->time_base);
pkt.stream_index = m_pStream->index;
//AVStream *in_stream = m_pInStream;
////pkt.pts = av_rescale_q(m_swr->getLastTime(), m_swr->getTimeBase(), m_pFormatCtx->streams[pkt.stream_index]->time_base);
////
////pkt.pts = av_frame_get_best_effort_timestamp(filter_frame);
////pkt.dts = pkt.pts;
////pkt.pts = pkt.dts = m_frameIndex++ *( m_pCodecCtx->frame_size * 1000 / m_pCodecCtx->sample_rate);
pkt.pts = av_rescale_q_rnd(pkt.pts, inCtx->time_base, m_pStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, inCtx->time_base, m_pStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, inCtx->time_base, m_pStream->time_base);
pkt.pos = -1;
printf("timestamp: %lf\n", pkt.pts*av_q2d(m_pStream->time_base));
printf("pkt.size: %d, frame.nb_samples: %d\npts: %ld, dts: %ld, duration: %ld\n", pkt.size, m_pStream->codec->frame_size, pkt.pts, pkt.dts, pkt.duration);
m_pStream->duration = m_pInStream->duration;
av_interleaved_write_frame(m_pFormatCtx, &pkt);
writePacket(&pkt);
av_packet_unref(&pkt);
}
......@@ -937,4 +948,38 @@ namespace FFM {
} while (r);
return ret;
}
int FFAudioEncoder::flush_encoder()
{
int ret = -1;
for (;;) {
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = nullptr;
pkt.size = 0;
while ((ret = avcodec_receive_packet(m_pCodecCtx, &pkt)) == AVERROR(EAGAIN)) {
ret = avcodec_send_frame(m_pCodecCtx, NULL);
if (ret < 0) {
printf("encoding failed!\n");
exit(1);
}
printf(">>> have encode pkt\n");
}
if (ret == AVERROR_EOF || ret < 0) {
//av_packet_unref(&pkt);
break;
}
writePacket(&pkt);
av_packet_unref(&pkt);
}
printf(">>>> exit flush\n");
return ret;
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
/*
.ogg DE y
.opus E n
.opus E y
.aac DE y
.mp3 DE y
.flac DE y
......@@ -14,9 +14,9 @@
.m4a D y
.oga E y
.mid n
.webm DE n
.webm DE y
.weba n
.amr DE n
.amr DE y
.au DE y
.wma D y
.aiff DE y
......@@ -180,12 +180,17 @@ namespace FFM
m_filter = fileter;
}
int flush_encoder();
private:
int openFile(std::string &fileName);
int openCodec();
int writePacket(AVPacket *pkt);
private:
AVStream *m_pInStream = nullptr;
AVCodecContext *m_pInCodeCtx = nullptr;
AVFormatContext *m_pFormatCtx = nullptr;
AVOutputFormat* m_pOutFmt = nullptr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment