|
|
|
@ -19,6 +19,22 @@ static void output_shutdown(output_t *output); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int output_got_packet(encoder_t *enc, void *u1, void *u2) { |
|
|
|
output_t *output = u1; |
|
|
|
|
|
|
|
dbg("{%s} output avpkt size is %i", output->file_name, (int) enc->avpkt.size); |
|
|
|
dbg("{%s} output pkt pts/dts is %li/%li", output->file_name, (long) enc->avpkt.pts, |
|
|
|
(long) enc->avpkt.dts); |
|
|
|
dbg("{%s} output dts %li", output->file_name, (long) output->encoder->mux_dts); |
|
|
|
|
|
|
|
av_write_frame(output->fmtctx, &enc->avpkt); |
|
|
|
|
|
|
|
output->fifo_pts += output->frame->nb_samples; |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static int output_flush(output_t *output) { |
|
|
|
while (av_audio_fifo_size(output->fifo) >= output->frame->nb_samples) { |
|
|
|
|
|
|
|
@ -29,78 +45,7 @@ static int output_flush(output_t *output) { |
|
|
|
dbg("{%s} output fifo pts %lu", output->file_name, (unsigned long) output->fifo_pts); |
|
|
|
output->frame->pts = output->fifo_pts; |
|
|
|
|
|
|
|
int keep_going; |
|
|
|
int have_frame = 1; |
|
|
|
do { |
|
|
|
keep_going = 0; |
|
|
|
int got_packet = 0; |
|
|
|
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 36, 0) |
|
|
|
if (have_frame) { |
|
|
|
int ret = avcodec_send_frame(output->avcctx, output->frame); |
|
|
|
dbg("{%s} send frame ret %i", output->file_name, ret); |
|
|
|
if (ret == 0) { |
|
|
|
// consumed |
|
|
|
have_frame = 0; |
|
|
|
keep_going = 1; |
|
|
|
} |
|
|
|
else { |
|
|
|
if (ret == AVERROR(EAGAIN)) |
|
|
|
; // check output and maybe try again |
|
|
|
else |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int ret = avcodec_receive_packet(output->avcctx, &output->avpkt); |
|
|
|
dbg("{%s} receive packet ret %i", output->file_name, ret); |
|
|
|
if (ret == 0) { |
|
|
|
// got some data |
|
|
|
keep_going = 1; |
|
|
|
got_packet = 1; |
|
|
|
} |
|
|
|
else { |
|
|
|
if (ret == AVERROR(EAGAIN)) |
|
|
|
; // try again if there's still more input |
|
|
|
else |
|
|
|
return -1; |
|
|
|
} |
|
|
|
#else |
|
|
|
if (!have_frame) |
|
|
|
break; |
|
|
|
|
|
|
|
int ret = avcodec_encode_audio2(output->avcctx, &output->avpkt, output->frame, &got_packet); |
|
|
|
dbg("{%s} encode frame ret %i, got packet %i", output->file_name, ret, got_packet); |
|
|
|
if (ret == 0) |
|
|
|
have_frame = 0; // consumed |
|
|
|
else |
|
|
|
return -1; // error |
|
|
|
if (got_packet) |
|
|
|
keep_going = 1; |
|
|
|
#endif |
|
|
|
|
|
|
|
if (!got_packet) |
|
|
|
continue; |
|
|
|
|
|
|
|
dbg("{%s} output avpkt size is %i", output->file_name, (int) output->avpkt.size); |
|
|
|
dbg("{%s} output pkt pts/dts is %li/%li", output->file_name, (long) output->avpkt.pts, |
|
|
|
(long) output->avpkt.dts); |
|
|
|
dbg("{%s} output dts %li", output->file_name, (long) output->mux_dts); |
|
|
|
|
|
|
|
// the encoder may return frames with the same dts multiple consecutive times. |
|
|
|
// the muxer may not like this, so ensure monotonically increasing dts. |
|
|
|
if (output->mux_dts > output->avpkt.dts) |
|
|
|
output->avpkt.dts = output->mux_dts; |
|
|
|
if (output->avpkt.pts < output->avpkt.dts) |
|
|
|
output->avpkt.pts = output->avpkt.dts; |
|
|
|
|
|
|
|
av_write_frame(output->fmtctx, &output->avpkt); |
|
|
|
|
|
|
|
output->fifo_pts += output->frame->nb_samples; |
|
|
|
output->mux_dts = output->avpkt.dts + 1; // min next expected dts |
|
|
|
|
|
|
|
av_packet_unref(&output->avpkt); |
|
|
|
} while (keep_going); |
|
|
|
encoder_input_data(output->encoder, output->frame, output_got_packet, output, NULL); |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
@ -131,9 +76,8 @@ output_t *output_new(const char *path, const char *filename) { |
|
|
|
g_strlcpy(ret->file_path, path, sizeof(ret->file_path)); |
|
|
|
g_strlcpy(ret->file_name, filename, sizeof(ret->file_name)); |
|
|
|
snprintf(ret->full_filename, sizeof(ret->full_filename), "%s/%s", path, filename); |
|
|
|
format_init(&ret->requested_format); |
|
|
|
format_init(&ret->actual_format); |
|
|
|
ret->file_format = output_file_format; |
|
|
|
ret->encoder = encoder_new(); |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
@ -141,13 +85,13 @@ output_t *output_new(const char *path, const char *filename) { |
|
|
|
int output_config(output_t *output, const format_t *requested_format, format_t *actual_format) { |
|
|
|
const char *err; |
|
|
|
|
|
|
|
// anything to do? |
|
|
|
if (G_LIKELY(format_eq(requested_format, &output->requested_format))) |
|
|
|
goto done; |
|
|
|
// // anything to do? |
|
|
|
// if (G_LIKELY(format_eq(requested_format, &output->requested_format))) |
|
|
|
// goto done; |
|
|
|
|
|
|
|
output_shutdown(output); |
|
|
|
|
|
|
|
output->requested_format = *requested_format; |
|
|
|
// output->requested_format = *requested_format; |
|
|
|
|
|
|
|
err = "failed to alloc format context"; |
|
|
|
output->fmtctx = avformat_alloc_context(); |
|
|
|
@ -158,47 +102,50 @@ int output_config(output_t *output, const format_t *requested_format, format_t * |
|
|
|
if (!output->fmtctx->oformat) |
|
|
|
goto err; |
|
|
|
|
|
|
|
err = "output codec not found"; |
|
|
|
AVCodec *codec = avcodec_find_encoder(output_codec_id); |
|
|
|
if (!codec) |
|
|
|
if (encoder_config(output->encoder, output_codec_id, mp3_bitrate, requested_format, actual_format)) |
|
|
|
goto err; |
|
|
|
|
|
|
|
// err = "output codec not found"; |
|
|
|
// AVCodec *codec = avcodec_find_encoder(output_codec_id); |
|
|
|
// if (!codec) |
|
|
|
// goto err; |
|
|
|
err = "failed to alloc output stream"; |
|
|
|
output->avst = avformat_new_stream(output->fmtctx, codec); |
|
|
|
output->avst = avformat_new_stream(output->fmtctx, output->encoder->codec); |
|
|
|
if (!output->avst) |
|
|
|
goto err; |
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 0, 0) |
|
|
|
err = "failed to alloc codec context"; |
|
|
|
output->avcctx = avcodec_alloc_context3(codec); |
|
|
|
if (!output->avcctx) |
|
|
|
goto err; |
|
|
|
#else |
|
|
|
output->avcctx = output->avst->codec; |
|
|
|
#endif |
|
|
|
//#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 0, 0) |
|
|
|
// err = "failed to alloc codec context"; |
|
|
|
// output->avcctx = avcodec_alloc_context3(codec); |
|
|
|
// if (!output->avcctx) |
|
|
|
// goto err; |
|
|
|
//#else |
|
|
|
// output->avcctx = output->avst->codec; |
|
|
|
//#endif |
|
|
|
|
|
|
|
// copy all format params |
|
|
|
output->actual_format = output->requested_format; |
|
|
|
// output->actual_format = output->requested_format; |
|
|
|
|
|
|
|
// determine sample format to use |
|
|
|
output->actual_format.format = -1; |
|
|
|
for (const enum AVSampleFormat *sfmt = codec->sample_fmts; sfmt && *sfmt != -1; sfmt++) { |
|
|
|
dbg("supported sample format for output codec %s: %s", codec->name, av_get_sample_fmt_name(*sfmt)); |
|
|
|
if (*sfmt == requested_format->format) |
|
|
|
output->actual_format.format = *sfmt; |
|
|
|
} |
|
|
|
if (output->actual_format.format == -1 && codec->sample_fmts) |
|
|
|
output->actual_format.format = codec->sample_fmts[0]; |
|
|
|
dbg("using output sample format %s for codec %s", av_get_sample_fmt_name(output->actual_format.format), codec->name); |
|
|
|
|
|
|
|
output->avcctx->channels = output->actual_format.channels; |
|
|
|
output->avcctx->channel_layout = av_get_default_channel_layout(output->actual_format.channels); |
|
|
|
output->avcctx->sample_rate = output->actual_format.clockrate; |
|
|
|
output->avcctx->sample_fmt = output->actual_format.format; |
|
|
|
output->avcctx->time_base = (AVRational){1,output->actual_format.clockrate}; |
|
|
|
output->avcctx->bit_rate = mp3_bitrate; |
|
|
|
output->avst->time_base = output->avcctx->time_base; |
|
|
|
// output->actual_format.format = -1; |
|
|
|
// for (const enum AVSampleFormat *sfmt = codec->sample_fmts; sfmt && *sfmt != -1; sfmt++) { |
|
|
|
// dbg("supported sample format for output codec %s: %s", codec->name, av_get_sample_fmt_name(*sfmt)); |
|
|
|
// if (*sfmt == requested_format->format) |
|
|
|
// output->actual_format.format = *sfmt; |
|
|
|
// } |
|
|
|
// if (output->actual_format.format == -1 && codec->sample_fmts) |
|
|
|
// output->actual_format.format = codec->sample_fmts[0]; |
|
|
|
// dbg("using output sample format %s for codec %s", av_get_sample_fmt_name(output->actual_format.format), codec->name); |
|
|
|
// |
|
|
|
// output->avcctx->channels = output->actual_format.channels; |
|
|
|
// output->avcctx->channel_layout = av_get_default_channel_layout(output->actual_format.channels); |
|
|
|
// output->avcctx->sample_rate = output->actual_format.clockrate; |
|
|
|
// output->avcctx->sample_fmt = output->actual_format.format; |
|
|
|
// output->avcctx->time_base = (AVRational){1,output->actual_format.clockrate}; |
|
|
|
// output->avcctx->bit_rate = mp3_bitrate; |
|
|
|
output->avst->time_base = output->encoder->avcctx->time_base; |
|
|
|
|
|
|
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 26, 0) // exact version? present in 57.56 |
|
|
|
avcodec_parameters_from_context(output->avst->codecpar, output->avcctx); |
|
|
|
avcodec_parameters_from_context(output->avst->codecpar, output->encoder->avcctx); |
|
|
|
#endif |
|
|
|
|
|
|
|
char full_fn[PATH_MAX*2]; |
|
|
|
@ -214,11 +161,12 @@ int output_config(output_t *output, const format_t *requested_format, format_t * |
|
|
|
goto err; |
|
|
|
|
|
|
|
got_fn: |
|
|
|
err = "failed to open output context"; |
|
|
|
int i = avcodec_open2(output->avcctx, codec, NULL); |
|
|
|
if (i) |
|
|
|
goto err; |
|
|
|
// err = "failed to open output context"; |
|
|
|
// int i = avcodec_open2(output->avcctx, codec, NULL); |
|
|
|
// if (i) |
|
|
|
// goto err; |
|
|
|
err = "failed to open avio"; |
|
|
|
int i; |
|
|
|
i = avio_open(&output->fmtctx->pb, full_fn, AVIO_FLAG_WRITE); |
|
|
|
if (i < 0) |
|
|
|
goto err; |
|
|
|
@ -227,24 +175,22 @@ got_fn: |
|
|
|
if (i) |
|
|
|
goto err; |
|
|
|
|
|
|
|
av_init_packet(&output->avpkt); |
|
|
|
// av_init_packet(&output->avpkt); |
|
|
|
|
|
|
|
// output frame and fifo |
|
|
|
output->frame = av_frame_alloc(); |
|
|
|
output->frame->nb_samples = output->avcctx->frame_size ? : 256; |
|
|
|
output->frame->format = output->avcctx->sample_fmt; |
|
|
|
output->frame->sample_rate = output->avcctx->sample_rate; |
|
|
|
output->frame->channel_layout = output->avcctx->channel_layout; |
|
|
|
output->frame->nb_samples = output->encoder->avcctx->frame_size ? : 256; |
|
|
|
output->frame->format = output->encoder->avcctx->sample_fmt; |
|
|
|
output->frame->sample_rate = output->encoder->avcctx->sample_rate; |
|
|
|
output->frame->channel_layout = output->encoder->avcctx->channel_layout; |
|
|
|
if (!output->frame->channel_layout) |
|
|
|
output->frame->channel_layout = av_get_default_channel_layout(output->avcctx->channels); |
|
|
|
output->frame->channel_layout = av_get_default_channel_layout(output->encoder->avcctx->channels); |
|
|
|
if (av_frame_get_buffer(output->frame, 0) < 0) |
|
|
|
abort(); |
|
|
|
|
|
|
|
output->fifo = av_audio_fifo_alloc(output->avcctx->sample_fmt, output->avcctx->channels, |
|
|
|
output->fifo = av_audio_fifo_alloc(output->encoder->avcctx->sample_fmt, output->encoder->avcctx->channels, |
|
|
|
output->frame->nb_samples); |
|
|
|
|
|
|
|
done: |
|
|
|
*actual_format = output->actual_format; |
|
|
|
db_config_stream(output); |
|
|
|
return 0; |
|
|
|
|
|
|
|
@ -265,24 +211,25 @@ static void output_shutdown(output_t *output) { |
|
|
|
av_write_trailer(output->fmtctx); |
|
|
|
avio_closep(&output->fmtctx->pb); |
|
|
|
} |
|
|
|
avcodec_close(output->avcctx); |
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 0, 0) |
|
|
|
avcodec_free_context(&output->avcctx); |
|
|
|
#endif |
|
|
|
// avcodec_close(output->avcctx); |
|
|
|
//#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 0, 0) |
|
|
|
// avcodec_free_context(&output->avcctx); |
|
|
|
//#endif |
|
|
|
avformat_free_context(output->fmtctx); |
|
|
|
av_audio_fifo_free(output->fifo); |
|
|
|
av_frame_free(&output->frame); |
|
|
|
|
|
|
|
output->avcctx = NULL; |
|
|
|
encoder_close(output->encoder); |
|
|
|
|
|
|
|
// output->avcctx = NULL; |
|
|
|
output->fmtctx = NULL; |
|
|
|
output->avst = NULL; |
|
|
|
output->fifo = NULL; |
|
|
|
|
|
|
|
output->fifo_pts = 0; |
|
|
|
output->mux_dts = 0; |
|
|
|
|
|
|
|
format_init(&output->requested_format); |
|
|
|
format_init(&output->actual_format); |
|
|
|
// format_init(&output->requested_format); |
|
|
|
// format_init(&output->actual_format); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -291,6 +238,7 @@ void output_close(output_t *output) { |
|
|
|
return; |
|
|
|
output_shutdown(output); |
|
|
|
db_close_stream(output); |
|
|
|
encoder_free(output->encoder); |
|
|
|
g_slice_free1(sizeof(*output), output); |
|
|
|
} |
|
|
|
|
|
|
|
|