From 20a1f250c676e54c164745b1f5c4fc8feda8f306 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 19 Apr 2018 20:13:28 +0200 Subject: encode: cosmetics Mostly whitespace changes; some semantic preserving transformations. --- audio/out/ao_lavc.c | 54 +++++++++++++++++++---------------- common/encode_lavc.c | 81 +++++++++++++++++++++++++++++++--------------------- video/out/vo_lavc.c | 65 +++++++++++++++++++---------------------- 3 files changed, 107 insertions(+), 93 deletions(-) diff --git a/audio/out/ao_lavc.c b/audio/out/ao_lavc.c index 830fbf9b72..aaa9e6422c 100644 --- a/audio/out/ao_lavc.c +++ b/audio/out/ao_lavc.c @@ -58,11 +58,13 @@ struct priv { bool shutdown; }; +static void encode(struct ao *ao, double apts, void **data); + static bool supports_format(AVCodec *codec, int format) { for (const enum AVSampleFormat *sampleformat = codec->sample_fmts; sampleformat && *sampleformat != AV_SAMPLE_FMT_NONE; - ++sampleformat) + sampleformat++) { if (af_from_avformat(*sampleformat) == format) return true; @@ -100,9 +102,10 @@ static int init(struct ao *ao) if (encode_lavc_alloc_stream(ao->encode_lavc_ctx, AVMEDIA_TYPE_AUDIO, - &ac->stream, &ac->codec) < 0) { - MP_ERR(ao, "could not get a new audio stream\n"); - goto fail; + &ac->stream, &ac->codec) < 0) + { + MP_ERR(ao, "could not get a new audio stream\n"); + goto fail; } codec = ao->encode_lavc_ctx->ac; @@ -114,7 +117,7 @@ static int init(struct ao *ao) // TODO: Remove this redundancy with encode_lavc_alloc_stream also // setting the time base. - // Using codec->time_bvase is deprecated, but needed for older lavf. + // Using codec->time_base is deprecated, but needed for older lavf. ac->stream->time_base.num = 1; ac->stream->time_base.den = ao->samplerate; ac->codec->time_base.num = 1; @@ -145,10 +148,11 @@ static int init(struct ao *ao) if (ac->codec->frame_size <= 1) ac->pcmhack = av_get_bits_per_sample(ac->codec->codec_id) / 8; - if (ac->pcmhack) + if (ac->pcmhack) { ac->aframesize = 16384; // "enough" - else + } else { ac->aframesize = ac->codec->frame_size; + } // enough frames for at least 0.25 seconds ac->framecount = ceil(ao->samplerate * 0.25 / ac->aframesize); @@ -175,7 +179,6 @@ fail: } // close audio device -static void encode(struct ao *ao, double apts, void **data); static void uninit(struct ao *ao) { struct priv *ac = ao->priv; @@ -245,8 +248,7 @@ static void write_packet(struct ao *ao, AVPacket *packet) ac->savepts = AV_NOPTS_VALUE; - if (encode_lavc_write_frame(ao->encode_lavc_ctx, - ac->stream, packet) < 0) { + if (encode_lavc_write_frame(ao->encode_lavc_ctx, ac->stream, packet) < 0) { MP_ERR(ao, "error writing at %d %d/%d\n", (int) packet->pts, ac->stream->time_base.num, @@ -269,6 +271,7 @@ static void encode_audio_and_write(struct ao *ao, AVFrame *frame) ac->codec->time_base.den); return; } + for (;;) { av_init_packet(&packet); status = avcodec_receive_packet(ac->codec, &packet); @@ -328,29 +331,33 @@ static void encode(struct ao *ao, double apts, void **data) if (ectx->options->rawts || ectx->options->copyts) { // real audio pts - frame->pts = floor(apts * ac->codec->time_base.den / ac->codec->time_base.num + 0.5); + frame->pts = floor(apts * ac->codec->time_base.den / + ac->codec->time_base.num + 0.5); } else { // audio playback time - frame->pts = floor(realapts * ac->codec->time_base.den / ac->codec->time_base.num + 0.5); + frame->pts = floor(realapts * ac->codec->time_base.den / + ac->codec->time_base.num + 0.5); } - int64_t frame_pts = av_rescale_q(frame->pts, ac->codec->time_base, ac->worst_time_base); + int64_t frame_pts = av_rescale_q(frame->pts, ac->codec->time_base, + ac->worst_time_base); if (ac->lastpts != AV_NOPTS_VALUE && frame_pts <= ac->lastpts) { // this indicates broken video // (video pts failing to increase fast enough to match audio) MP_WARN(ao, "audio frame pts went backwards (%d <- %d), autofixed\n", (int)frame->pts, (int)ac->lastpts); frame_pts = ac->lastpts + 1; - frame->pts = av_rescale_q(frame_pts, ac->worst_time_base, ac->codec->time_base); + frame->pts = av_rescale_q(frame_pts, ac->worst_time_base, + ac->codec->time_base); } ac->lastpts = frame_pts; frame->quality = ac->codec->global_quality; encode_audio_and_write(ao, frame); av_frame_free(&frame); - } - else + } else { encode_audio_and_write(ao, NULL); + } } // this should round samples down to frame sizes @@ -400,7 +407,6 @@ static int play(struct ao *ao, void **data, int samples, int flags) } if (ac->worst_time_base.den == 0) { - //if (ac->codec->time_base.num / ac->codec->time_base.den >= ac->stream->time_base.num / ac->stream->time_base.den) if (ac->codec->time_base.num * (double) ac->stream->time_base.den >= ac->stream->time_base.num * (double) ac->codec->time_base.den) { MP_VERBOSE(ao, "NOTE: using codec time base (%d/%d) for pts " @@ -445,8 +451,9 @@ static int play(struct ao *ao, void **data, int samples, int flags) nextpts = pts; if (ectx->discontinuity_pts_offset == MP_NOPTS_VALUE) { ectx->discontinuity_pts_offset = ectx->next_in_pts - nextpts; - } - else if (fabs(nextpts + ectx->discontinuity_pts_offset - ectx->next_in_pts) > 30) { + } else if (fabs(nextpts + ectx->discontinuity_pts_offset - + ectx->next_in_pts) > 30) + { MP_WARN(ao, "detected an unexpected discontinuity (pts jumped by " "%f seconds)\n", nextpts + ectx->discontinuity_pts_offset - ectx->next_in_pts); @@ -454,8 +461,7 @@ static int play(struct ao *ao, void **data, int samples, int flags) } outpts = pts + ectx->discontinuity_pts_offset; - } - else { + } else { outpts = pts; } @@ -488,13 +494,11 @@ static int play(struct ao *ao, void **data, int samples, int flags) pthread_mutex_unlock(&ectx->lock); if (flags & AOPLAY_FINAL_CHUNK) { - if (bufpos < orig_samples) { + if (bufpos < orig_samples) MP_ERR(ao, "did not write enough data at the end\n"); - } } else { - if (bufpos > orig_samples) { + if (bufpos > orig_samples) MP_ERR(ao, "audio buffer overflow (should never happen)\n"); - } } return taken; diff --git a/common/encode_lavc.c b/common/encode_lavc.c index bed4a3b980..950e4559c9 100644 --- a/common/encode_lavc.c +++ b/common/encode_lavc.c @@ -233,10 +233,11 @@ struct encode_lavc_context *encode_lavc_init(struct encode_opts *options, if (*in) ++in; } - } else + } else { ctx->vc = avcodec_find_encoder(av_guess_codec(ctx->avc->oformat, NULL, ctx->avc->url, NULL, AVMEDIA_TYPE_VIDEO)); + } if (ctx->options->acodec) { char *tok; @@ -252,14 +253,14 @@ struct encode_lavc_context *encode_lavc_init(struct encode_opts *options, if (*in) ++in; } - } else + } else { ctx->ac = avcodec_find_encoder(av_guess_codec(ctx->avc->oformat, NULL, ctx->avc->url, NULL, AVMEDIA_TYPE_AUDIO)); + } if (!ctx->vc && !ctx->ac) { - encode_lavc_fail( - ctx, "neither audio nor video codec was found\n"); + encode_lavc_fail(ctx, "neither audio nor video codec was found\n"); return NULL; } @@ -322,17 +323,18 @@ int encode_lavc_start(struct encode_lavc_context *ctx) if (ctx->expect_video && ctx->vcc == NULL) { if (ctx->avc->oformat->video_codec != AV_CODEC_ID_NONE || - ctx->options->vcodec) { - encode_lavc_fail(ctx, - "no video stream succeeded - invalid codec?\n"); + ctx->options->vcodec) + { + encode_lavc_fail(ctx,"no video stream succeeded - invalid codec?\n"); return 0; } } + if (ctx->expect_audio && ctx->acc == NULL) { if (ctx->avc->oformat->audio_codec != AV_CODEC_ID_NONE || - ctx->options->acodec) { - encode_lavc_fail(ctx, - "no audio stream succeeded - invalid codec?\n"); + ctx->options->acodec) + { + encode_lavc_fail(ctx, "no audio stream succeeded - invalid codec?\n"); return 0; } } @@ -340,11 +342,9 @@ int encode_lavc_start(struct encode_lavc_context *ctx) ctx->header_written = -1; if (!(ctx->avc->oformat->flags & AVFMT_NOFILE)) { - MP_INFO(ctx, "Opening output file: %s\n", - ctx->avc->url); + MP_INFO(ctx, "Opening output file: %s\n", ctx->avc->url); - if (avio_open(&ctx->avc->pb, ctx->avc->url, - AVIO_FLAG_WRITE) < 0) { + if (avio_open(&ctx->avc->pb, ctx->avc->url, AVIO_FLAG_WRITE) < 0) { encode_lavc_fail(ctx, "could not open '%s'\n", ctx->avc->url); return 0; @@ -370,7 +370,9 @@ int encode_lavc_start(struct encode_lavc_context *ctx) for (de = NULL; (de = av_dict_get(ctx->foptions, "", de, AV_DICT_IGNORE_SUFFIX));) + { MP_WARN(ctx, "ofopts: key '%s' not found.\n", de->key); + } av_dict_free(&ctx->foptions); ctx->header_written = 1; @@ -382,9 +384,10 @@ void encode_lavc_free(struct encode_lavc_context *ctx) if (!ctx) return; - if (!ctx->finished) + if (!ctx->finished) { encode_lavc_fail(ctx, "called encode_lavc_free without encode_lavc_finish\n"); + } pthread_mutex_destroy(&ctx->lock); talloc_free(ctx); @@ -623,10 +626,12 @@ int encode_lavc_alloc_stream(struct encode_lavc_context *ctx, ctx->voptions = NULL; - if (ctx->options->vopts) - for (p = ctx->options->vopts; *p; ++p) + if (ctx->options->vopts) { + for (p = ctx->options->vopts; *p; ++p) { if (!set_to_avdictionary(ctx, &ctx->voptions, NULL, *p)) MP_WARN(ctx, "vo-lavc: could not set option %s\n", *p); + } + } de = av_dict_get(ctx->voptions, "global_quality", NULL, 0); if (de) @@ -658,10 +663,12 @@ int encode_lavc_alloc_stream(struct encode_lavc_context *ctx, ctx->aoptions = 0; - if (ctx->options->aopts) - for (p = ctx->options->aopts; *p; ++p) + if (ctx->options->aopts) { + for (p = ctx->options->aopts; *p; ++p) { if (!set_to_avdictionary(ctx, &ctx->aoptions, NULL, *p)) MP_WARN(ctx, "ao-lavc: could not set option %s\n", *p); + } + } de = av_dict_get(ctx->aoptions, "global_quality", NULL, 0); if (de) @@ -785,18 +792,22 @@ void encode_lavc_write_stats(struct encode_lavc_context *ctx, switch (codec->codec_type) { case AVMEDIA_TYPE_VIDEO: - if (ctx->twopass_bytebuffer_v) - if (codec->stats_out) + if (ctx->twopass_bytebuffer_v) { + if (codec->stats_out) { stream_write_buffer(ctx->twopass_bytebuffer_v, codec->stats_out, strlen(codec->stats_out)); + } + } break; case AVMEDIA_TYPE_AUDIO: - if (ctx->twopass_bytebuffer_a) - if (codec->stats_out) + if (ctx->twopass_bytebuffer_a) { + if (codec->stats_out) { stream_write_buffer(ctx->twopass_bytebuffer_a, codec->stats_out, strlen(codec->stats_out)); + } + } break; default: break; @@ -835,7 +846,7 @@ int encode_lavc_write_frame(struct encode_lavc_context *ctx, AVStream *stream, switch (stream->codecpar->codec_type) { case AVMEDIA_TYPE_VIDEO: ctx->vbytes += packet->size; - ++ctx->frames; + ctx->frames += 1; break; case AVMEDIA_TYPE_AUDIO: ctx->abytes += packet->size; @@ -907,17 +918,19 @@ static void encode_lavc_printoptions(struct mp_log *log, const void *obj, * Don't print anything but CONST's on level two. * Only print items from the requested unit. */ - if (!unit && opt->type == AV_OPT_TYPE_CONST) + if (!unit && opt->type == AV_OPT_TYPE_CONST) { continue; - else if (unit && opt->type != AV_OPT_TYPE_CONST) + } else if (unit && opt->type != AV_OPT_TYPE_CONST) { continue; - else if (unit && opt->type == AV_OPT_TYPE_CONST + } else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit)) + { continue; - else if (unit && opt->type == AV_OPT_TYPE_CONST) + } else if (unit && opt->type == AV_OPT_TYPE_CONST) { mp_info(log, "%s", subindent); - else + } else { mp_info(log, "%s", indent); + } switch (opt->type) { case AV_OPT_TYPE_FLAGS: @@ -973,9 +986,10 @@ bool encode_lavc_showhelp(struct mp_log *log, struct encode_opts *opts) const AVOutputFormat *c = NULL; void *iter = NULL; mp_info(log, "Available output formats:\n"); - while ((c = av_muxer_iterate(&iter))) + while ((c = av_muxer_iterate(&iter))) { mp_info(log, " --of=%-13s %s\n", c->name, c->long_name ? c->long_name : ""); + } } if (CHECKV(opts->fopts)) { AVFormatContext *c = avformat_alloc_context(); @@ -1126,15 +1140,16 @@ int encode_lavc_getstatus(struct encode_lavc_context *ctx, megabytes = ctx->avc->pb ? (avio_size(ctx->avc->pb) / 1048576.0 / f) : 0; fps = ctx->frames / (now - ctx->t0); x = ctx->audioseconds / (now - ctx->t0); - if (ctx->frames) + if (ctx->frames) { snprintf(buf, bufsize, "{%.1fmin %.1ffps %.1fMB}", minutes, fps, megabytes); - else if (ctx->audioseconds) + } else if (ctx->audioseconds) { snprintf(buf, bufsize, "{%.1fmin %.2fx %.1fMB}", minutes, x, megabytes); - else + } else { snprintf(buf, bufsize, "{%.1fmin %.1fMB}", minutes, megabytes); + } buf[bufsize - 1] = 0; pthread_mutex_unlock(&ctx->lock); diff --git a/video/out/vo_lavc.c b/video/out/vo_lavc.c index b32e405747..e59ff79791 100644 --- a/video/out/vo_lavc.c +++ b/video/out/vo_lavc.c @@ -57,6 +57,8 @@ struct priv { bool shutdown; }; +static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi); + static int preinit(struct vo *vo) { struct priv *vc; @@ -70,7 +72,6 @@ static int preinit(struct vo *vo) return 0; } -static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi); static void uninit(struct vo *vo) { struct priv *vc = vo->priv; @@ -103,15 +104,15 @@ static int reconfig(struct vo *vo, struct mp_image_params *params) pthread_mutex_lock(&vo->encode_lavc_ctx->lock); if (vc->stream) { - if (width == vc->codec->width && - height == vc->codec->height) { + if (width == vc->codec->width && height == vc->codec->height) { if (aspect.num != vc->codec->sample_aspect_ratio.num || - aspect.den != vc->codec->sample_aspect_ratio.den) { + aspect.den != vc->codec->sample_aspect_ratio.den) + { /* aspect-only changes are not critical */ MP_WARN(vo, "unsupported pixel aspect ratio change from %d:%d to %d:%d\n", - vc->codec->sample_aspect_ratio.num, - vc->codec->sample_aspect_ratio.den, - aspect.num, aspect.den); + vc->codec->sample_aspect_ratio.num, + vc->codec->sample_aspect_ratio.den, + aspect.num, aspect.den); } goto done; } @@ -143,8 +144,7 @@ static int reconfig(struct vo *vo, struct mp_image_params *params) AVMEDIA_TYPE_VIDEO, &vc->stream, &vc->codec) < 0) goto error; - vc->stream->sample_aspect_ratio = vc->codec->sample_aspect_ratio = - aspect; + vc->stream->sample_aspect_ratio = vc->codec->sample_aspect_ratio = aspect; vc->codec->width = width; vc->codec->height = height; vc->codec->pix_fmt = pix_fmt; @@ -208,14 +208,13 @@ static void write_packet(struct vo *vo, AVPacket *packet) // HACK: libavformat calculates dts wrong if the initial packet // duration is not set, but ONLY if the time base is "high" and if we // have b-frames! - if (!packet->duration) - if (!vc->have_first_packet) - if (vc->codec->has_b_frames - || vc->codec->max_b_frames) - if (vc->stream->time_base.num * 1000LL <= - vc->stream->time_base.den) - packet->duration = FFMAX(1, av_rescale_q(1, - vc->codec->time_base, vc->stream->time_base)); + if (!packet->duration && !vc->have_first_packet && + (vc->codec->has_b_frames || vc->codec->max_b_frames) && + (vc->stream->time_base.num * 1000LL <= vc->stream->time_base.den)) + { + packet->duration = FFMAX(1, av_rescale_q(1, + vc->codec->time_base, vc->stream->time_base)); + } } if (encode_lavc_write_frame(vo->encode_lavc_ctx, @@ -247,15 +246,13 @@ static void encode_video_and_write(struct vo *vo, AVFrame *frame) av_init_packet(&packet); status = avcodec_receive_packet(vc->codec, &packet); if (status == AVERROR(EAGAIN)) { // No more packets for now. - if (frame == NULL) { + if (frame == NULL) MP_ERR(vo, "sent flush frame, got EAGAIN"); - } break; } if (status == AVERROR_EOF) { // No more packets, ever. - if (frame != NULL) { + if (frame != NULL) MP_ERR(vo, "sent image frame, got EOF"); - } break; } if (status < 0) { @@ -304,7 +301,6 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) avc = vc->codec; if (vc->worst_time_base.den == 0) { - //if (avc->time_base.num / avc->time_base.den >= vc->stream->time_base.num / vc->stream->time_base.den) if (avc->time_base.num * (double) vc->stream->time_base.den >= vc->stream->time_base.num * (double) avc->time_base.den) { MP_VERBOSE(vo, "NOTE: using codec time base " @@ -322,11 +318,12 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) vc->worst_time_base = vc->stream->time_base; vc->worst_time_base_is_stream = 1; } - if (ectx->options->maxfps) + if (ectx->options->maxfps) { vc->mindeltapts = ceil(vc->worst_time_base.den / (vc->worst_time_base.num * ectx->options->maxfps)); - else + } else { vc->mindeltapts = 0; + } // NOTE: we use the following "axiom" of av_rescale_q: // if time base A is worse than time base B, then @@ -355,8 +352,9 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) nextpts = pts; if (ectx->discontinuity_pts_offset == MP_NOPTS_VALUE) { ectx->discontinuity_pts_offset = ectx->next_in_pts - nextpts; - } - else if (fabs(nextpts + ectx->discontinuity_pts_offset - ectx->next_in_pts) > 30) { + } else if (fabs(nextpts + ectx->discontinuity_pts_offset - + ectx->next_in_pts) > 30) + { MP_WARN(vo, "detected an unexpected discontinuity (pts jumped by " "%f seconds)\n", nextpts + ectx->discontinuity_pts_offset - ectx->next_in_pts); @@ -364,8 +362,7 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) } outpts = pts + ectx->discontinuity_pts_offset; - } - else { + } else { // adjust pts by knowledge of audio pts vs audio playback time double duration = 0; if (ectx->last_video_in_pts != MP_NOPTS_VALUE) @@ -407,17 +404,14 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) } if (vc->lastipts != AV_NOPTS_VALUE) { - // we have a valid image in lastimg while (vc->lastimg && vc->lastipts < frameipts) { int64_t thisduration = vc->harddup ? 1 : (frameipts - vc->lastipts); // we will ONLY encode this frame if it can be encoded at at least // vc->mindeltapts after the last encoded frame! - int64_t skipframes = - (vc->lastencodedipts == AV_NOPTS_VALUE) - ? 0 - : vc->lastencodedipts + vc->mindeltapts - vc->lastipts; + int64_t skipframes = (vc->lastencodedipts == AV_NOPTS_VALUE) + ? 0 : vc->lastencodedipts + vc->mindeltapts - vc->lastipts; if (skipframes < 0) skipframes = 0; @@ -434,7 +428,7 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) encode_video_and_write(vo, frame); av_frame_free(&frame); - ++vc->lastdisplaycount; + vc->lastdisplaycount += 1; vc->lastencodedipts = vc->lastipts + skipframes; } @@ -456,7 +450,8 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) vc->lastframeipts = vc->lastipts = frameipts; if (ectx->options->rawts && vc->lastipts < 0) { - MP_ERR(vo, "why does this happen? DEBUG THIS! vc->lastipts = %lld\n", (long long) vc->lastipts); + MP_ERR(vo, "why does this happen? DEBUG THIS! vc->lastipts = %lld\n", + (long long) vc->lastipts); vc->lastipts = -1; } vc->lastdisplaycount = 0; -- cgit v1.2.3