/src/mpv/common/encode_lavc.c
Line | Count | Source |
1 | | /* |
2 | | * muxing using libavformat |
3 | | * |
4 | | * Copyright (C) 2010 Nicolas George <george@nsup.org> |
5 | | * Copyright (C) 2011-2012 Rudolf Polzer <divVerent@xonotic.org> |
6 | | * |
7 | | * This file is part of mpv. |
8 | | * |
9 | | * mpv is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public |
11 | | * License as published by the Free Software Foundation; either |
12 | | * version 2.1 of the License, or (at your option) any later version. |
13 | | * |
14 | | * mpv is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | #include <libavutil/avutil.h> |
24 | | #include <libavutil/timestamp.h> |
25 | | |
26 | | #include "encode_lavc.h" |
27 | | #include "common/av_common.h" |
28 | | #include "common/global.h" |
29 | | #include "common/msg.h" |
30 | | #include "common/msg_control.h" |
31 | | #include "options/m_config.h" |
32 | | #include "options/m_option.h" |
33 | | #include "options/options.h" |
34 | | #include "options/path.h" |
35 | | #include "osdep/timer.h" |
36 | | #include "video/out/vo.h" |
37 | | #include "mpv_talloc.h" |
38 | | #include "stream/stream.h" |
39 | | |
40 | | struct encode_priv { |
41 | | struct mp_log *log; |
42 | | |
43 | | // --- All fields are protected by encode_lavc_context.lock |
44 | | |
45 | | bool failed; |
46 | | |
47 | | struct mp_tags *metadata; |
48 | | |
49 | | AVFormatContext *muxer; |
50 | | |
51 | | bool header_written; // muxer was initialized |
52 | | |
53 | | struct mux_stream **streams; |
54 | | int num_streams; |
55 | | |
56 | | // Statistics |
57 | | double t0; |
58 | | |
59 | | long long abytes; |
60 | | long long vbytes; |
61 | | |
62 | | unsigned int frames; |
63 | | double audioseconds; |
64 | | }; |
65 | | |
66 | | struct mux_stream { |
67 | | int index; // index of this into p->streams[] |
68 | | char name[80]; |
69 | | struct encode_lavc_context *ctx; |
70 | | enum AVMediaType codec_type; |
71 | | AVRational encoder_timebase; // packet timestamps from encoder |
72 | | AVStream *st; |
73 | | }; |
74 | | |
75 | | #define OPT_BASE_STRUCT struct encode_opts |
76 | | const struct m_sub_options encode_config = { |
77 | | .opts = (const m_option_t[]) { |
78 | | {"o", OPT_STRING(file), .flags = M_OPT_NOCFG | M_OPT_PRE_PARSE | M_OPT_FILE}, |
79 | | {"of", OPT_STRING(format)}, |
80 | | {"ofopts", OPT_KEYVALUELIST(fopts), .flags = M_OPT_HAVE_HELP}, |
81 | | {"ovc", OPT_STRING(vcodec)}, |
82 | | {"ovcopts", OPT_KEYVALUELIST(vopts), .flags = M_OPT_HAVE_HELP}, |
83 | | {"oac", OPT_STRING(acodec)}, |
84 | | {"oacopts", OPT_KEYVALUELIST(aopts), .flags = M_OPT_HAVE_HELP}, |
85 | | {"orawts", OPT_BOOL(rawts)}, |
86 | | {"ocopy-metadata", OPT_BOOL(copy_metadata)}, |
87 | | {"oset-metadata", OPT_KEYVALUELIST(set_metadata)}, |
88 | | {"oremove-metadata", OPT_STRINGLIST(remove_metadata)}, |
89 | | {0} |
90 | | }, |
91 | | .size = sizeof(struct encode_opts), |
92 | | .defaults = &(const struct encode_opts){ |
93 | | .copy_metadata = true, |
94 | | }, |
95 | | }; |
96 | | |
97 | | struct encode_lavc_context *encode_lavc_init(struct mpv_global *global) |
98 | 279 | { |
99 | 279 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
100 | 279 | return NULL; |
101 | 0 | #endif |
102 | 0 | struct encode_lavc_context *ctx = talloc_ptrtype(NULL, ctx); |
103 | 0 | *ctx = (struct encode_lavc_context){ |
104 | 0 | .global = global, |
105 | 0 | .options = mp_get_config_group(ctx, global, &encode_config), |
106 | 0 | .priv = talloc_zero(ctx, struct encode_priv), |
107 | 0 | .log = mp_log_new(ctx, global->log, "encode"), |
108 | 0 | }; |
109 | 0 | mp_mutex_init(&ctx->lock); |
110 | |
|
111 | 0 | struct encode_priv *p = ctx->priv; |
112 | 0 | p->log = ctx->log; |
113 | |
|
114 | 0 | const char *filename = ctx->options->file; |
115 | | |
116 | | // STUPID STUPID STUPID STUPID avio |
117 | | // does not support "-" as file name to mean stdin/stdout |
118 | | // ffmpeg.c works around this too, the same way |
119 | 0 | if (!strcmp(filename, "-")) |
120 | 0 | filename = "pipe:1"; |
121 | |
|
122 | 0 | encode_lavc_discontinuity(ctx); |
123 | |
|
124 | 0 | p->muxer = avformat_alloc_context(); |
125 | 0 | MP_HANDLE_OOM(p->muxer); |
126 | | |
127 | 0 | if (ctx->options->format && ctx->options->format[0]) { |
128 | 0 | ctx->oformat = av_guess_format(ctx->options->format, filename, NULL); |
129 | 0 | } else { |
130 | 0 | ctx->oformat = av_guess_format(NULL, filename, NULL); |
131 | 0 | } |
132 | |
|
133 | 0 | if (!ctx->oformat) { |
134 | 0 | MP_FATAL(ctx, "format not found\n"); |
135 | 0 | goto fail; |
136 | 0 | } |
137 | | |
138 | 0 | p->muxer->oformat = ctx->oformat; |
139 | |
|
140 | 0 | char *path = mp_get_user_path(NULL, global, filename); |
141 | 0 | p->muxer->url = av_strdup(path); |
142 | 0 | talloc_free(path); |
143 | 0 | MP_HANDLE_OOM(p->muxer->url); |
144 | | |
145 | 0 | return ctx; |
146 | | |
147 | 0 | fail: |
148 | 0 | p->failed = true; |
149 | 0 | encode_lavc_free(ctx); |
150 | 0 | return NULL; |
151 | 0 | } |
152 | | |
153 | | void encode_lavc_set_metadata(struct encode_lavc_context *ctx, |
154 | | struct mp_tags *metadata) |
155 | 0 | { |
156 | 0 | struct encode_priv *p = ctx->priv; |
157 | |
|
158 | 0 | mp_mutex_lock(&ctx->lock); |
159 | |
|
160 | 0 | if (ctx->options->copy_metadata) { |
161 | 0 | p->metadata = mp_tags_dup(ctx, metadata); |
162 | 0 | } else { |
163 | 0 | p->metadata = talloc_zero(ctx, struct mp_tags); |
164 | 0 | } |
165 | |
|
166 | 0 | if (ctx->options->set_metadata) { |
167 | 0 | char **kv = ctx->options->set_metadata; |
168 | | // Set all user-provided metadata tags |
169 | 0 | for (int n = 0; kv[n * 2]; n++) { |
170 | 0 | MP_VERBOSE(ctx, "setting metadata value '%s' for key '%s'\n", |
171 | 0 | kv[n*2 + 0], kv[n*2 +1]); |
172 | 0 | mp_tags_set_str(p->metadata, kv[n*2 + 0], kv[n*2 +1]); |
173 | 0 | } |
174 | 0 | } |
175 | |
|
176 | 0 | if (ctx->options->remove_metadata) { |
177 | 0 | char **k = ctx->options->remove_metadata; |
178 | | // Remove all user-provided metadata tags |
179 | 0 | for (int n = 0; k[n]; n++) { |
180 | 0 | MP_VERBOSE(ctx, "removing metadata key '%s'\n", k[n]); |
181 | 0 | mp_tags_remove_str(p->metadata, k[n]); |
182 | 0 | } |
183 | 0 | } |
184 | |
|
185 | 0 | mp_mutex_unlock(&ctx->lock); |
186 | 0 | } |
187 | | |
188 | | bool encode_lavc_free(struct encode_lavc_context *ctx) |
189 | 16.4k | { |
190 | 16.4k | bool res = true; |
191 | 16.4k | if (!ctx) |
192 | 16.4k | return res; |
193 | | |
194 | 0 | struct encode_priv *p = ctx->priv; |
195 | |
|
196 | 0 | if (!p->failed && !p->header_written) { |
197 | 0 | MP_FATAL(p, "no data written to target file\n"); |
198 | 0 | p->failed = true; |
199 | 0 | } |
200 | |
|
201 | 0 | if (!p->failed && p->header_written) { |
202 | 0 | if (av_write_trailer(p->muxer) < 0) |
203 | 0 | MP_ERR(p, "error writing trailer\n"); |
204 | |
|
205 | 0 | MP_INFO(p, "video: encoded %lld bytes\n", p->vbytes); |
206 | 0 | MP_INFO(p, "audio: encoded %lld bytes\n", p->abytes); |
207 | |
|
208 | 0 | MP_INFO(p, "muxing overhead %lld bytes\n", |
209 | 0 | (long long)(avio_size(p->muxer->pb) - p->vbytes - p->abytes)); |
210 | 0 | } |
211 | |
|
212 | 0 | if (avio_closep(&p->muxer->pb) < 0 && !p->failed) { |
213 | 0 | MP_ERR(p, "Closing file failed\n"); |
214 | 0 | p->failed = true; |
215 | 0 | } |
216 | |
|
217 | 0 | avformat_free_context(p->muxer); |
218 | |
|
219 | 0 | res = !p->failed; |
220 | |
|
221 | 0 | mp_mutex_destroy(&ctx->lock); |
222 | 0 | talloc_free(ctx); |
223 | |
|
224 | 0 | return res; |
225 | 16.4k | } |
226 | | |
227 | | // called locked |
228 | | static void maybe_init_muxer(struct encode_lavc_context *ctx) |
229 | 0 | { |
230 | 0 | struct encode_priv *p = ctx->priv; |
231 | |
|
232 | 0 | if (p->header_written || p->failed) |
233 | 0 | return; |
234 | | |
235 | | // Check if all streams were initialized yet. We need data to know the |
236 | | // AVStream parameters, so we wait for data from _all_ streams before |
237 | | // starting. |
238 | 0 | for (int n = 0; n < p->num_streams; n++) { |
239 | 0 | if (!p->streams[n]->st) |
240 | 0 | return; |
241 | 0 | } |
242 | | |
243 | 0 | if (!(p->muxer->oformat->flags & AVFMT_NOFILE)) { |
244 | 0 | MP_INFO(p, "Opening output file: %s\n", p->muxer->url); |
245 | |
|
246 | 0 | if (avio_open(&p->muxer->pb, p->muxer->url, AVIO_FLAG_WRITE) < 0) { |
247 | 0 | MP_FATAL(p, "could not open '%s'\n", p->muxer->url); |
248 | 0 | goto failed; |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | 0 | p->t0 = mp_time_sec(); |
253 | |
|
254 | 0 | MP_INFO(p, "Opening muxer: %s [%s]\n", |
255 | 0 | p->muxer->oformat->long_name, p->muxer->oformat->name); |
256 | |
|
257 | 0 | if (p->metadata) { |
258 | 0 | for (int i = 0; i < p->metadata->num_keys; i++) { |
259 | 0 | av_dict_set(&p->muxer->metadata, |
260 | 0 | p->metadata->keys[i], p->metadata->values[i], 0); |
261 | 0 | } |
262 | 0 | } |
263 | |
|
264 | 0 | AVDictionary *opts = NULL; |
265 | 0 | mp_set_avdict(&opts, ctx->options->fopts); |
266 | |
|
267 | 0 | if (avformat_write_header(p->muxer, &opts) < 0) { |
268 | 0 | MP_FATAL(p, "Failed to initialize muxer.\n"); |
269 | 0 | p->failed = true; |
270 | 0 | } else { |
271 | 0 | mp_avdict_print_unset(p->log, MSGL_WARN, opts); |
272 | 0 | } |
273 | |
|
274 | 0 | av_dict_free(&opts); |
275 | |
|
276 | 0 | if (p->failed) |
277 | 0 | goto failed; |
278 | | |
279 | 0 | p->header_written = true; |
280 | |
|
281 | 0 | return; |
282 | | |
283 | 0 | failed: |
284 | 0 | p->failed = true; |
285 | 0 | } |
286 | | |
287 | | // called locked |
288 | | static struct mux_stream *find_mux_stream(struct encode_lavc_context *ctx, |
289 | | enum AVMediaType codec_type) |
290 | 0 | { |
291 | 0 | struct encode_priv *p = ctx->priv; |
292 | |
|
293 | 0 | for (int n = 0; n < p->num_streams; n++) { |
294 | 0 | struct mux_stream *s = p->streams[n]; |
295 | 0 | if (s->codec_type == codec_type) |
296 | 0 | return s; |
297 | 0 | } |
298 | | |
299 | 0 | return NULL; |
300 | 0 | } |
301 | | |
302 | | void encode_lavc_expect_stream(struct encode_lavc_context *ctx, |
303 | | enum stream_type type) |
304 | 0 | { |
305 | 0 | struct encode_priv *p = ctx->priv; |
306 | |
|
307 | 0 | mp_mutex_lock(&ctx->lock); |
308 | |
|
309 | 0 | enum AVMediaType codec_type = mp_to_av_stream_type(type); |
310 | | |
311 | | // These calls are idempotent. |
312 | 0 | if (find_mux_stream(ctx, codec_type)) |
313 | 0 | goto done; |
314 | | |
315 | 0 | if (p->header_written) { |
316 | 0 | MP_ERR(p, "Cannot add a stream during encoding.\n"); |
317 | 0 | p->failed = true; |
318 | 0 | goto done; |
319 | 0 | } |
320 | | |
321 | 0 | struct mux_stream *dst = talloc_ptrtype(p, dst); |
322 | 0 | *dst = (struct mux_stream){ |
323 | 0 | .index = p->num_streams, |
324 | 0 | .ctx = ctx, |
325 | 0 | .codec_type = mp_to_av_stream_type(type), |
326 | 0 | }; |
327 | 0 | snprintf(dst->name, sizeof(dst->name), "%s", stream_type_name(type)); |
328 | 0 | MP_TARRAY_APPEND(p, p->streams, p->num_streams, dst); |
329 | |
|
330 | 0 | done: |
331 | 0 | mp_mutex_unlock(&ctx->lock); |
332 | 0 | } |
333 | | |
334 | | // Signal that you are ready to encode (you provide the codec params etc. too). |
335 | | // This returns a muxing handle which you can use to add encodec packets. |
336 | | // Can be called only once per stream. info is copied by callee as needed. |
337 | | static void encode_lavc_add_stream(struct encoder_context *enc, |
338 | | struct encode_lavc_context *ctx, |
339 | | struct encoder_stream_info *info) |
340 | 0 | { |
341 | 0 | struct encode_priv *p = ctx->priv; |
342 | |
|
343 | 0 | mp_mutex_lock(&ctx->lock); |
344 | |
|
345 | 0 | struct mux_stream *dst = find_mux_stream(ctx, info->codecpar->codec_type); |
346 | 0 | if (!dst) { |
347 | 0 | MP_ERR(p, "Cannot add a stream at runtime.\n"); |
348 | 0 | p->failed = true; |
349 | 0 | goto done; |
350 | 0 | } |
351 | 0 | if (dst->st) { |
352 | | // Possibly via --gapless-audio, or explicitly recreating AO/VO. |
353 | 0 | MP_ERR(p, "Encoder was reinitialized; this is not allowed.\n"); |
354 | 0 | p->failed = true; |
355 | 0 | dst = NULL; |
356 | 0 | goto done; |
357 | 0 | } |
358 | | |
359 | 0 | dst->st = avformat_new_stream(p->muxer, NULL); |
360 | 0 | MP_HANDLE_OOM(dst->st); |
361 | | |
362 | 0 | dst->encoder_timebase = info->timebase; |
363 | 0 | dst->st->time_base = info->timebase; // lavf will change this on muxer init |
364 | | // Some muxers (e.g. Matroska one) expect the sample_aspect_ratio to be |
365 | | // set on the AVStream. |
366 | 0 | if (info->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) |
367 | 0 | dst->st->sample_aspect_ratio = info->codecpar->sample_aspect_ratio; |
368 | |
|
369 | 0 | if (avcodec_parameters_copy(dst->st->codecpar, info->codecpar) < 0) |
370 | 0 | MP_HANDLE_OOM(NULL); |
371 | | |
372 | 0 | enc->mux_stream = dst; |
373 | |
|
374 | 0 | maybe_init_muxer(ctx); |
375 | |
|
376 | 0 | done: |
377 | 0 | mp_mutex_unlock(&ctx->lock); |
378 | 0 | } |
379 | | |
380 | | // Write a packet. This will take over ownership of `pkt` |
381 | | static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt) |
382 | 0 | { |
383 | 0 | struct encode_lavc_context *ctx = dst->ctx; |
384 | 0 | struct encode_priv *p = ctx->priv; |
385 | |
|
386 | 0 | mp_assert(dst->st); |
387 | | |
388 | 0 | mp_mutex_lock(&ctx->lock); |
389 | |
|
390 | 0 | if (p->failed) |
391 | 0 | goto done; |
392 | | |
393 | 0 | if (!p->header_written) { |
394 | 0 | MP_ERR(p, "Encoder trying to write packet before muxer was initialized.\n"); |
395 | 0 | p->failed = true; |
396 | 0 | goto done; |
397 | 0 | } |
398 | | |
399 | 0 | pkt->stream_index = dst->st->index; |
400 | 0 | mp_assert(dst->st == p->muxer->streams[pkt->stream_index]); |
401 | | |
402 | 0 | av_packet_rescale_ts(pkt, dst->encoder_timebase, dst->st->time_base); |
403 | |
|
404 | 0 | switch (dst->st->codecpar->codec_type) { |
405 | 0 | case AVMEDIA_TYPE_VIDEO: |
406 | 0 | p->vbytes += pkt->size; |
407 | 0 | p->frames += 1; |
408 | 0 | break; |
409 | 0 | case AVMEDIA_TYPE_AUDIO: |
410 | 0 | p->abytes += pkt->size; |
411 | 0 | p->audioseconds += pkt->duration |
412 | 0 | * (double)dst->st->time_base.num |
413 | 0 | / (double)dst->st->time_base.den; |
414 | 0 | break; |
415 | 0 | } |
416 | | |
417 | 0 | if (av_interleaved_write_frame(p->muxer, pkt) < 0) { |
418 | 0 | MP_ERR(p, "Writing packet failed.\n"); |
419 | 0 | p->failed = true; |
420 | 0 | } |
421 | |
|
422 | 0 | pkt = NULL; |
423 | |
|
424 | 0 | done: |
425 | 0 | mp_mutex_unlock(&ctx->lock); |
426 | 0 | if (pkt) |
427 | 0 | av_packet_unref(pkt); |
428 | 0 | } |
429 | | |
430 | | AVRational mp_encoder_get_mux_timebase_unlocked(struct encoder_context *p) |
431 | 0 | { |
432 | 0 | return p->mux_stream->st->time_base; |
433 | 0 | } |
434 | | |
435 | | void encode_lavc_discontinuity(struct encode_lavc_context *ctx) |
436 | 0 | { |
437 | 0 | if (!ctx) |
438 | 0 | return; |
439 | | |
440 | 0 | mp_mutex_lock(&ctx->lock); |
441 | 0 | ctx->discontinuity_pts_offset = MP_NOPTS_VALUE; |
442 | 0 | mp_mutex_unlock(&ctx->lock); |
443 | 0 | } |
444 | | |
445 | | static void encode_lavc_printoptions(struct mp_log *log, const void *obj, |
446 | | const char *indent, const char *subindent, |
447 | | const char *unit, int filter_and, |
448 | | int filter_eq) |
449 | 745 | { |
450 | 745 | const AVOption *opt = NULL; |
451 | 745 | char optbuf[32]; |
452 | 240k | while ((opt = av_opt_next(obj, opt))) { |
453 | | // if flags are 0, it simply hasn't been filled in yet and may be |
454 | | // potentially useful |
455 | 240k | if (opt->flags) |
456 | 229k | if ((opt->flags & filter_and) != filter_eq) |
457 | 156k | continue; |
458 | | /* Don't print CONST's on level one. |
459 | | * Don't print anything but CONST's on level two. |
460 | | * Only print items from the requested unit. |
461 | | */ |
462 | 84.1k | if (!unit && opt->type == AV_OPT_TYPE_CONST) { |
463 | 2.58k | continue; |
464 | 81.5k | } else if (unit && opt->type != AV_OPT_TYPE_CONST) { |
465 | 32.1k | continue; |
466 | 49.4k | } else if (unit && opt->type == AV_OPT_TYPE_CONST |
467 | 47.2k | && strcmp(unit, opt->unit)) |
468 | 44.1k | { |
469 | 44.1k | continue; |
470 | 44.1k | } else if (unit && opt->type == AV_OPT_TYPE_CONST) { |
471 | 3.11k | mp_info(log, "%s", subindent); |
472 | 3.11k | } else { |
473 | 2.12k | mp_info(log, "%s", indent); |
474 | 2.12k | } |
475 | | |
476 | 5.24k | switch (opt->type) { |
477 | 270 | case AV_OPT_TYPE_FLAGS: |
478 | 270 | snprintf(optbuf, sizeof(optbuf), "%s=<flags>", opt->name); |
479 | 270 | break; |
480 | 1.21k | case AV_OPT_TYPE_INT: |
481 | 1.21k | snprintf(optbuf, sizeof(optbuf), "%s=<int>", opt->name); |
482 | 1.21k | break; |
483 | 275 | case AV_OPT_TYPE_INT64: |
484 | 275 | snprintf(optbuf, sizeof(optbuf), "%s=<int64>", opt->name); |
485 | 275 | break; |
486 | 0 | case AV_OPT_TYPE_DOUBLE: |
487 | 0 | snprintf(optbuf, sizeof(optbuf), "%s=<double>", opt->name); |
488 | 0 | break; |
489 | 104 | case AV_OPT_TYPE_FLOAT: |
490 | 104 | snprintf(optbuf, sizeof(optbuf), "%s=<float>", opt->name); |
491 | 104 | break; |
492 | 53 | case AV_OPT_TYPE_STRING: |
493 | 53 | snprintf(optbuf, sizeof(optbuf), "%s=<string>", opt->name); |
494 | 53 | break; |
495 | 90 | case AV_OPT_TYPE_RATIONAL: |
496 | 90 | snprintf(optbuf, sizeof(optbuf), "%s=<rational>", opt->name); |
497 | 90 | break; |
498 | 0 | case AV_OPT_TYPE_BINARY: |
499 | 0 | snprintf(optbuf, sizeof(optbuf), "%s=<binary>", opt->name); |
500 | 0 | break; |
501 | 3.11k | case AV_OPT_TYPE_CONST: |
502 | 3.11k | snprintf(optbuf, sizeof(optbuf), " [+-]%s", opt->name); |
503 | 3.11k | break; |
504 | 119 | default: |
505 | 119 | snprintf(optbuf, sizeof(optbuf), "%s", opt->name); |
506 | 119 | break; |
507 | 5.24k | } |
508 | 5.24k | optbuf[sizeof(optbuf) - 1] = 0; |
509 | 5.24k | mp_info(log, "%-32s ", optbuf); |
510 | 5.24k | if (opt->help) |
511 | 4.12k | mp_info(log, " %s", opt->help); |
512 | 5.24k | mp_info(log, "\n"); |
513 | 5.24k | if (opt->unit && opt->type != AV_OPT_TYPE_CONST) |
514 | 644 | encode_lavc_printoptions(log, obj, indent, subindent, opt->unit, |
515 | 644 | filter_and, filter_eq); |
516 | 5.24k | } |
517 | 745 | } |
518 | | |
519 | | bool encode_lavc_showhelp(struct mp_log *log, struct encode_opts *opts) |
520 | 11.3k | { |
521 | 11.3k | bool help_output = false; |
522 | 34.1k | #define CHECKS(str) ((str) && \ |
523 | 34.1k | strcmp((str), "help") == 0 ? (help_output |= 1) : 0) |
524 | 34.1k | #define CHECKV(strv) ((strv) && (strv)[0] && \ |
525 | 34.1k | strcmp((strv)[0], "help") == 0 ? (help_output |= 1) : 0) |
526 | 11.3k | if (CHECKS(opts->format)) { |
527 | 19 | const AVOutputFormat *c = NULL; |
528 | 19 | void *iter = NULL; |
529 | 19 | mp_info(log, "Available output formats:\n"); |
530 | 76 | while ((c = av_muxer_iterate(&iter))) { |
531 | 57 | mp_info(log, " --of=%-13s %s\n", c->name, |
532 | 57 | c->long_name ? c->long_name : ""); |
533 | 57 | } |
534 | 19 | } |
535 | 11.3k | if (CHECKV(opts->fopts)) { |
536 | 16 | AVFormatContext *c = avformat_alloc_context(); |
537 | 16 | const AVOutputFormat *format = NULL; |
538 | 16 | mp_info(log, "Available output format ctx->options:\n"); |
539 | 16 | encode_lavc_printoptions(log, c, " --ofopts=", " ", NULL, |
540 | 16 | AV_OPT_FLAG_ENCODING_PARAM, |
541 | 16 | AV_OPT_FLAG_ENCODING_PARAM); |
542 | 16 | avformat_free_context(c); |
543 | 16 | void *iter = NULL; |
544 | 64 | while ((format = av_muxer_iterate(&iter))) { |
545 | 48 | if (format->priv_class) { |
546 | 48 | mp_info(log, "Additionally, for --of=%s:\n", |
547 | 48 | format->name); |
548 | 48 | encode_lavc_printoptions(log, &format->priv_class, " --ofopts=", |
549 | 48 | " ", NULL, |
550 | 48 | AV_OPT_FLAG_ENCODING_PARAM, |
551 | 48 | AV_OPT_FLAG_ENCODING_PARAM); |
552 | 48 | } |
553 | 48 | } |
554 | 16 | } |
555 | 11.3k | if (CHECKV(opts->vopts)) { |
556 | 8 | AVCodecContext *c = avcodec_alloc_context3(NULL); |
557 | 8 | const AVCodec *codec = NULL; |
558 | 8 | mp_info(log, "Available output video codec ctx->options:\n"); |
559 | 8 | encode_lavc_printoptions(log, |
560 | 8 | c, " --ovcopts=", " ", NULL, |
561 | 8 | AV_OPT_FLAG_ENCODING_PARAM | |
562 | 8 | AV_OPT_FLAG_VIDEO_PARAM, |
563 | 8 | AV_OPT_FLAG_ENCODING_PARAM | |
564 | 8 | AV_OPT_FLAG_VIDEO_PARAM); |
565 | 8 | av_free(c); |
566 | 8 | void *iter = NULL; |
567 | 4.01k | while ((codec = av_codec_iterate(&iter))) { |
568 | 4.00k | if (!av_codec_is_encoder(codec)) |
569 | 4.00k | continue; |
570 | 0 | if (codec->type != AVMEDIA_TYPE_VIDEO) |
571 | 0 | continue; |
572 | 0 | if (opts->vcodec && opts->vcodec[0] && |
573 | 0 | strcmp(opts->vcodec, codec->name) != 0) |
574 | 0 | continue; |
575 | 0 | if (codec->priv_class) { |
576 | 0 | mp_info(log, "Additionally, for --ovc=%s:\n", |
577 | 0 | codec->name); |
578 | 0 | encode_lavc_printoptions(log, |
579 | 0 | &codec->priv_class, " --ovcopts=", |
580 | 0 | " ", NULL, |
581 | 0 | AV_OPT_FLAG_ENCODING_PARAM | |
582 | 0 | AV_OPT_FLAG_VIDEO_PARAM, |
583 | 0 | AV_OPT_FLAG_ENCODING_PARAM | |
584 | 0 | AV_OPT_FLAG_VIDEO_PARAM); |
585 | 0 | } |
586 | 0 | } |
587 | 8 | } |
588 | 11.3k | if (CHECKV(opts->aopts)) { |
589 | 29 | AVCodecContext *c = avcodec_alloc_context3(NULL); |
590 | 29 | const AVCodec *codec = NULL; |
591 | 29 | mp_info(log, "Available output audio codec ctx->options:\n"); |
592 | 29 | encode_lavc_printoptions(log, |
593 | 29 | c, " --oacopts=", " ", NULL, |
594 | 29 | AV_OPT_FLAG_ENCODING_PARAM | |
595 | 29 | AV_OPT_FLAG_AUDIO_PARAM, |
596 | 29 | AV_OPT_FLAG_ENCODING_PARAM | |
597 | 29 | AV_OPT_FLAG_AUDIO_PARAM); |
598 | 29 | av_free(c); |
599 | 29 | void *iter = NULL; |
600 | 14.5k | while ((codec = av_codec_iterate(&iter))) { |
601 | 14.5k | if (!av_codec_is_encoder(codec)) |
602 | 14.5k | continue; |
603 | 0 | if (codec->type != AVMEDIA_TYPE_AUDIO) |
604 | 0 | continue; |
605 | 0 | if (opts->acodec && opts->acodec[0] && |
606 | 0 | strcmp(opts->acodec, codec->name) != 0) |
607 | 0 | continue; |
608 | 0 | if (codec->priv_class) { |
609 | 0 | mp_info(log, "Additionally, for --oac=%s:\n", |
610 | 0 | codec->name); |
611 | 0 | encode_lavc_printoptions(log, |
612 | 0 | &codec->priv_class, " --oacopts=", |
613 | 0 | " ", NULL, |
614 | 0 | AV_OPT_FLAG_ENCODING_PARAM | |
615 | 0 | AV_OPT_FLAG_AUDIO_PARAM, |
616 | 0 | AV_OPT_FLAG_ENCODING_PARAM | |
617 | 0 | AV_OPT_FLAG_AUDIO_PARAM); |
618 | 0 | } |
619 | 0 | } |
620 | 29 | } |
621 | 11.3k | if (CHECKS(opts->vcodec)) { |
622 | 2 | const AVCodec *c = NULL; |
623 | 2 | void *iter = NULL; |
624 | 2 | mp_info(log, "Available output video codecs:\n"); |
625 | 1.00k | while ((c = av_codec_iterate(&iter))) { |
626 | 1.00k | if (!av_codec_is_encoder(c)) |
627 | 1.00k | continue; |
628 | 0 | if (c->type != AVMEDIA_TYPE_VIDEO) |
629 | 0 | continue; |
630 | 0 | mp_info(log, " --ovc=%-12s %s\n", c->name, |
631 | 0 | c->long_name ? c->long_name : ""); |
632 | 0 | } |
633 | 2 | } |
634 | 11.3k | if (CHECKS(opts->acodec)) { |
635 | 1 | const AVCodec *c = NULL; |
636 | 1 | void *iter = NULL; |
637 | 1 | mp_info(log, "Available output audio codecs:\n"); |
638 | 502 | while ((c = av_codec_iterate(&iter))) { |
639 | 501 | if (!av_codec_is_encoder(c)) |
640 | 501 | continue; |
641 | 0 | if (c->type != AVMEDIA_TYPE_AUDIO) |
642 | 0 | continue; |
643 | 0 | mp_info(log, " --oac=%-12s %s\n", c->name, |
644 | 0 | c->long_name ? c->long_name : ""); |
645 | 0 | } |
646 | 1 | } |
647 | 11.3k | return help_output; |
648 | 11.3k | } |
649 | | |
650 | | int encode_lavc_getstatus(struct encode_lavc_context *ctx, |
651 | | char *buf, int bufsize, |
652 | | float relative_position) |
653 | 0 | { |
654 | 0 | if (!ctx) |
655 | 0 | return -1; |
656 | | |
657 | 0 | struct encode_priv *p = ctx->priv; |
658 | |
|
659 | 0 | double now = mp_time_sec(); |
660 | 0 | float minutes, megabytes, fps, x; |
661 | 0 | float f = MPMAX(0.0001, relative_position); |
662 | |
|
663 | 0 | mp_mutex_lock(&ctx->lock); |
664 | |
|
665 | 0 | if (p->failed) { |
666 | 0 | snprintf(buf, bufsize, "(failed)\n"); |
667 | 0 | goto done; |
668 | 0 | } |
669 | | |
670 | 0 | minutes = (now - p->t0) / 60.0 * (1 - f) / f; |
671 | 0 | megabytes = p->muxer->pb ? (avio_size(p->muxer->pb) / 1048576.0 / f) : 0; |
672 | 0 | fps = p->frames / (now - p->t0); |
673 | 0 | x = p->audioseconds / (now - p->t0); |
674 | 0 | if (p->frames) { |
675 | 0 | snprintf(buf, bufsize, "{%.1fmin %.1ffps %.1fMB}", |
676 | 0 | minutes, fps, megabytes); |
677 | 0 | } else if (p->audioseconds) { |
678 | 0 | snprintf(buf, bufsize, "{%.1fmin %.2fx %.1fMB}", |
679 | 0 | minutes, x, megabytes); |
680 | 0 | } else { |
681 | 0 | snprintf(buf, bufsize, "{%.1fmin %.1fMB}", |
682 | 0 | minutes, megabytes); |
683 | 0 | } |
684 | 0 | buf[bufsize - 1] = 0; |
685 | |
|
686 | 0 | done: |
687 | 0 | mp_mutex_unlock(&ctx->lock); |
688 | 0 | return 0; |
689 | 0 | } |
690 | | |
691 | | bool encode_lavc_didfail(struct encode_lavc_context *ctx) |
692 | 0 | { |
693 | 0 | if (!ctx) |
694 | 0 | return false; |
695 | 0 | mp_mutex_lock(&ctx->lock); |
696 | 0 | bool fail = ctx->priv->failed; |
697 | 0 | mp_mutex_unlock(&ctx->lock); |
698 | 0 | return fail; |
699 | 0 | } |
700 | | |
701 | | static void encoder_destroy(void *ptr) |
702 | 0 | { |
703 | 0 | struct encoder_context *p = ptr; |
704 | |
|
705 | 0 | av_packet_free(&p->pkt); |
706 | 0 | avcodec_parameters_free(&p->info.codecpar); |
707 | 0 | avcodec_free_context(&p->encoder); |
708 | 0 | free_stream(p->twopass_bytebuffer); |
709 | 0 | } |
710 | | |
711 | | static const AVCodec *find_codec_for(struct encode_lavc_context *ctx, |
712 | | enum stream_type type, bool *used_auto) |
713 | 0 | { |
714 | 0 | char *codec_name = type == STREAM_VIDEO |
715 | 0 | ? ctx->options->vcodec |
716 | 0 | : ctx->options->acodec; |
717 | 0 | enum AVMediaType codec_type = mp_to_av_stream_type(type); |
718 | 0 | const char *tname = stream_type_name(type); |
719 | |
|
720 | 0 | *used_auto = !(codec_name && codec_name[0]); |
721 | |
|
722 | 0 | const AVCodec *codec; |
723 | 0 | if (*used_auto) { |
724 | 0 | codec = avcodec_find_encoder(av_guess_codec(ctx->oformat, NULL, |
725 | 0 | ctx->options->file, NULL, |
726 | 0 | codec_type)); |
727 | 0 | } else { |
728 | 0 | codec = avcodec_find_encoder_by_name(codec_name); |
729 | 0 | if (!codec) |
730 | 0 | MP_FATAL(ctx, "codec '%s' not found.\n", codec_name); |
731 | 0 | } |
732 | |
|
733 | 0 | if (codec && codec->type != codec_type) { |
734 | 0 | MP_FATAL(ctx, "codec for %s has wrong media type\n", tname); |
735 | 0 | codec = NULL; |
736 | 0 | } |
737 | |
|
738 | 0 | return codec; |
739 | 0 | } |
740 | | |
741 | | // Return whether the stream type is "supposed" to work. |
742 | | bool encode_lavc_stream_type_ok(struct encode_lavc_context *ctx, |
743 | | enum stream_type type) |
744 | 0 | { |
745 | | // If a codec was forced, let it proceed to actual encoding, and then error |
746 | | // if it doesn't work. (Worried that av_guess_codec() may return NULL for |
747 | | // some formats where a specific codec works anyway.) |
748 | 0 | bool auto_codec; |
749 | 0 | return !!find_codec_for(ctx, type, &auto_codec) || !auto_codec; |
750 | 0 | } |
751 | | |
752 | | struct encoder_context *mp_encoder_context_alloc(struct encode_lavc_context *ctx, |
753 | | enum stream_type type, |
754 | | struct mp_log *log) |
755 | 0 | { |
756 | 0 | if (!ctx) { |
757 | 0 | mp_err(log, "the option --o (output file) must be specified\n"); |
758 | 0 | return NULL; |
759 | 0 | } |
760 | | |
761 | 0 | struct encoder_context *p = talloc_ptrtype(NULL, p); |
762 | 0 | talloc_set_destructor(p, encoder_destroy); |
763 | 0 | *p = (struct encoder_context){ |
764 | 0 | .global = ctx->global, |
765 | 0 | .options = ctx->options, |
766 | 0 | .oformat = ctx->oformat, |
767 | 0 | .type = type, |
768 | 0 | .log = log, |
769 | 0 | .encode_lavc_ctx = ctx, |
770 | 0 | }; |
771 | |
|
772 | 0 | bool auto_codec; |
773 | 0 | const AVCodec *codec = find_codec_for(ctx, type, &auto_codec); |
774 | 0 | const char *tname = stream_type_name(type); |
775 | |
|
776 | 0 | if (!codec) { |
777 | 0 | if (auto_codec) |
778 | 0 | MP_FATAL(p, "codec for %s not found\n", tname); |
779 | 0 | goto fail; |
780 | 0 | } |
781 | | |
782 | 0 | p->encoder = avcodec_alloc_context3(codec); |
783 | 0 | MP_HANDLE_OOM(p->encoder); |
784 | | |
785 | 0 | return p; |
786 | | |
787 | 0 | fail: |
788 | 0 | talloc_free(p); |
789 | 0 | return NULL; |
790 | 0 | } |
791 | | |
792 | | static void encoder_2pass_prepare(struct encoder_context *p) |
793 | 0 | { |
794 | 0 | char *filename = talloc_asprintf(NULL, "%s-%s-pass1.log", |
795 | 0 | p->options->file, |
796 | 0 | stream_type_name(p->type)); |
797 | |
|
798 | 0 | if (p->encoder->flags & AV_CODEC_FLAG_PASS2) { |
799 | 0 | MP_INFO(p, "Reading 2-pass log: %s\n", filename); |
800 | 0 | struct stream *s = stream_create(filename, |
801 | 0 | STREAM_ORIGIN_DIRECT | STREAM_READ, |
802 | 0 | NULL, p->global); |
803 | 0 | if (s) { |
804 | 0 | struct bstr content = stream_read_complete(s, p, 1000000000); |
805 | 0 | if (content.start) { |
806 | 0 | p->encoder->stats_in = content.start; |
807 | 0 | } else { |
808 | 0 | MP_WARN(p, "could not read '%s', " |
809 | 0 | "disabling 2-pass encoding at pass 1\n", filename); |
810 | 0 | } |
811 | 0 | free_stream(s); |
812 | 0 | } else { |
813 | 0 | MP_WARN(p, "could not open '%s', " |
814 | 0 | "disabling 2-pass encoding at pass 2\n", filename); |
815 | 0 | p->encoder->flags &= ~(unsigned)AV_CODEC_FLAG_PASS2; |
816 | 0 | } |
817 | 0 | } |
818 | |
|
819 | 0 | if (p->encoder->flags & AV_CODEC_FLAG_PASS1) { |
820 | 0 | MP_INFO(p, "Writing to 2-pass log: %s\n", filename); |
821 | 0 | p->twopass_bytebuffer = open_output_stream(filename, p->global); |
822 | 0 | if (!p->twopass_bytebuffer) { |
823 | 0 | MP_WARN(p, "could not open '%s', " |
824 | 0 | "disabling 2-pass encoding at pass 1\n", filename); |
825 | 0 | p->encoder->flags &= ~(unsigned)AV_CODEC_FLAG_PASS1; |
826 | 0 | } |
827 | 0 | } |
828 | |
|
829 | 0 | talloc_free(filename); |
830 | 0 | } |
831 | | |
832 | | bool mp_encoder_init_codec_and_muxer(struct encoder_context *p) |
833 | 0 | { |
834 | 0 | mp_assert(!avcodec_is_open(p->encoder)); |
835 | | |
836 | 0 | char **copts = p->type == STREAM_VIDEO |
837 | 0 | ? p->options->vopts |
838 | 0 | : p->options->aopts; |
839 | | |
840 | | // Set these now, so the code below can read back parsed settings from it. |
841 | 0 | mp_set_avopts(p->log, p->encoder, copts); |
842 | |
|
843 | 0 | encoder_2pass_prepare(p); |
844 | |
|
845 | 0 | if (p->oformat->flags & AVFMT_GLOBALHEADER) |
846 | 0 | p->encoder->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
847 | |
|
848 | 0 | MP_INFO(p, "Opening encoder: %s [%s]\n", |
849 | 0 | p->encoder->codec->long_name, p->encoder->codec->name); |
850 | |
|
851 | 0 | if (p->encoder->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) { |
852 | 0 | p->encoder->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; |
853 | 0 | MP_WARN(p, "\n\n" |
854 | 0 | " ********************************************\n" |
855 | 0 | " **** Experimental codec selected! ****\n" |
856 | 0 | " ********************************************\n\n" |
857 | 0 | "This means the output file may be broken or bad.\n" |
858 | 0 | "Possible reasons, problems, workarounds:\n" |
859 | 0 | "- Codec implementation in ffmpeg is not finished yet.\n" |
860 | 0 | " Try updating ffmpeg.\n" |
861 | 0 | "- Bad picture quality, blocks, blurriness.\n" |
862 | 0 | " Experiment with codec settings to maybe still get the\n" |
863 | 0 | " desired quality output at the expense of bitrate.\n" |
864 | 0 | "- Broken files.\n" |
865 | 0 | " May not work at all, or break with other software.\n" |
866 | 0 | "- Slow compression.\n" |
867 | 0 | " Bear with it.\n" |
868 | 0 | "- Crashes.\n" |
869 | 0 | " Happens. Try varying options to work around.\n" |
870 | 0 | "If none of this helps you, try another codec in place of %s.\n\n", |
871 | 0 | p->encoder->codec->name); |
872 | 0 | } |
873 | |
|
874 | 0 | if (avcodec_open2(p->encoder, p->encoder->codec, NULL) < 0) { |
875 | 0 | MP_FATAL(p, "Could not initialize encoder.\n"); |
876 | 0 | goto fail; |
877 | 0 | } |
878 | | |
879 | 0 | p->info.timebase = p->encoder->time_base; // (_not_ changed by enc. init) |
880 | 0 | p->info.codecpar = avcodec_parameters_alloc(); |
881 | 0 | MP_HANDLE_OOM(p->info.codecpar); |
882 | 0 | if (avcodec_parameters_from_context(p->info.codecpar, p->encoder) < 0) |
883 | 0 | goto fail; |
884 | | |
885 | 0 | p->pkt = av_packet_alloc(); |
886 | 0 | MP_HANDLE_OOM(p->pkt); |
887 | | |
888 | 0 | encode_lavc_add_stream(p, p->encode_lavc_ctx, &p->info); |
889 | 0 | if (!p->mux_stream) |
890 | 0 | goto fail; |
891 | | |
892 | 0 | return true; |
893 | | |
894 | 0 | fail: |
895 | 0 | avcodec_free_context(&p->encoder); |
896 | 0 | return false; |
897 | 0 | } |
898 | | |
899 | | bool mp_encoder_encode(struct encoder_context *p, AVFrame *frame) |
900 | 0 | { |
901 | 0 | int status = avcodec_send_frame(p->encoder, frame); |
902 | 0 | if (status < 0) { |
903 | 0 | if (frame && status == AVERROR_EOF) |
904 | 0 | MP_ERR(p, "new data after sending EOF to encoder\n"); |
905 | 0 | goto fail; |
906 | 0 | } |
907 | | |
908 | 0 | AVPacket *packet = p->pkt; |
909 | 0 | for (;;) { |
910 | 0 | status = avcodec_receive_packet(p->encoder, packet); |
911 | 0 | if (status == AVERROR(EAGAIN)) |
912 | 0 | break; |
913 | 0 | if (status < 0 && status != AVERROR_EOF) |
914 | 0 | goto fail; |
915 | | |
916 | 0 | if (p->twopass_bytebuffer && p->encoder->stats_out) { |
917 | 0 | stream_write_buffer(p->twopass_bytebuffer, p->encoder->stats_out, |
918 | 0 | strlen(p->encoder->stats_out)); |
919 | 0 | } |
920 | |
|
921 | 0 | if (status == AVERROR_EOF) |
922 | 0 | break; |
923 | | |
924 | 0 | encode_lavc_add_packet(p->mux_stream, packet); |
925 | 0 | } |
926 | | |
927 | 0 | return true; |
928 | | |
929 | 0 | fail: |
930 | 0 | MP_ERR(p, "error encoding at %s\n", |
931 | 0 | frame ? av_ts2timestr(frame->pts, &p->encoder->time_base) : "EOF"); |
932 | 0 | return false; |
933 | 0 | } |
934 | | |
935 | | void mp_encoder_update_log(struct mpv_global *global) |
936 | 1.00k | { |
937 | 1.00k | struct encode_opts *options = mp_get_config_group(NULL, global, &encode_config); |
938 | 1.00k | if (options->file && (!strcmp(options->file, "-") || |
939 | 270 | !strcmp(options->file, "/dev/stdout") || |
940 | 267 | !strcmp(options->file, "pipe:") || |
941 | 264 | !strcmp(options->file, "pipe:1"))) |
942 | 32 | { |
943 | 32 | mp_msg_force_stderr(global, true); |
944 | 32 | } |
945 | 1.00k | talloc_free(options); |
946 | 1.00k | } |
947 | | |
948 | | // vim: ts=4 sw=4 et |