FFmpeg
Loading...
Searching...
No Matches
encode.c
Go to the documentation of this file.
1/*
2 * generic encoding-related code
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/avassert.h"
23#include "libavutil/emms.h"
24#include "libavutil/frame.h"
25#include "libavutil/hwcontext.h"
26#include "libavutil/internal.h"
28#include "libavutil/mem.h"
29#include "libavutil/opt.h"
30#include "libavutil/pixdesc.h"
31#include "libavutil/samplefmt.h"
32
33#include "avcodec.h"
34#include "avcodec_internal.h"
35#include "codec_desc.h"
36#include "codec_internal.h"
37#include "encode.h"
39#include "internal.h"
40
41typedef struct EncodeContext {
43
44 /**
45 * This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only
46 * formats (i.e. whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set).
47 * This is used to set said flag generically for said encoders.
48 */
50
51 /**
52 * An audio frame with less than required samples has been submitted (and
53 * potentially padded with silence). Reject all subsequent frames.
54 */
57
59{
60 return (EncodeContext*)avci;
61}
62
64{
66 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
68 return AVERROR(EINVAL);
69 }
70
71 av_assert0(!avpkt->data);
72
75 avpkt->data = avctx->internal->byte_buffer;
76 if (!avpkt->data) {
77 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
78 return AVERROR(ENOMEM);
79 }
80 avpkt->size = size;
81
82 return 0;
83}
84
86{
87 int ret;
88
89 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
90 return AVERROR(EINVAL);
91
92 if (avpkt->data || avpkt->buf) {
93 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
94 return AVERROR(EINVAL);
95 }
96
98 if (ret < 0) {
99 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
100 return ret;
101 }
102 avpkt->data = avpkt->buf->data;
103
104 return 0;
105}
106
108{
109 int ret;
110
112 return AVERROR(EINVAL);
113
114 av_assert0(!avpkt->data && !avpkt->buf);
115
116 avpkt->size = size;
117 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
118 if (ret < 0)
119 goto fail;
120
121 if (!avpkt->data || !avpkt->buf) {
122 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
123 ret = AVERROR(EINVAL);
124 goto fail;
125 }
126 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
127
128 ret = 0;
129fail:
130 if (ret < 0) {
131 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
132 av_packet_unref(avpkt);
133 }
134
135 return ret;
136}
137
139{
140 uint8_t *data = avpkt->data;
141 int ret;
142
143 if (avpkt->buf)
144 return 0;
145
146 avpkt->data = NULL;
147 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
148 if (ret < 0)
149 return ret;
150 memcpy(avpkt->data, data, avpkt->size);
151
152 return 0;
153}
154
155/**
156 * Pad last frame with silence.
157 */
158static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
159{
160 AVFrameSideData *sd;
161 int discard_padding;
162 int ret;
163
164 frame->format = src->format;
165 frame->nb_samples = out_samples;
166 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
167 if (ret < 0)
168 goto fail;
170 if (ret < 0)
171 goto fail;
172
174 if (ret < 0)
175 goto fail;
176
177 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
178 src->nb_samples, s->ch_layout.nb_channels,
179 s->sample_fmt)) < 0)
180 goto fail;
181 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
182 frame->nb_samples - src->nb_samples,
183 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
184 goto fail;
185
186 discard_padding = frame->nb_samples - src->nb_samples;
187 av_assert1(discard_padding > 0);
189 if (!sd) {
190 ret = AVERROR(ENOMEM);
191 goto fail;
192 }
193 AV_WL32A(sd->data, 0);
194 AV_WL32A(sd->data + 4, discard_padding);
195 AV_WL16A(sd->data + 8, 0);
196
197 return 0;
198
199fail:
201 encode_ctx(s->internal)->last_audio_frame = 0;
202 return ret;
203}
204
205int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
206 const AVSubtitle *sub)
207{
208 int ret;
209 if (sub->start_display_time) {
210 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
211 return -1;
212 }
213 av_assert0(buf_size >= 0);
214
215 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
216 avctx->frame_num++;
217 return ret;
218}
219
221{
222 AVCodecInternal *avci = avctx->internal;
223
224 if (avci->draining)
225 return AVERROR_EOF;
226
227 if (!avci->buffer_frame->buf[0])
228 return AVERROR(EAGAIN);
229
231
232 return 0;
233}
234
236{
237 AVCodecInternal *avci = avctx->internal;
238 EncodeContext *ec = encode_ctx(avci);
239
240 if (avpkt->pts == AV_NOPTS_VALUE) {
241 avpkt->pts = frame->pts;
242 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && avpkt->pts != AV_NOPTS_VALUE)
243 avpkt->pts -= ff_samples_to_time_base(avctx, avctx->initial_padding);
244 }
245
246 if (!avpkt->duration) {
247 if (frame->duration)
248 avpkt->duration = frame->duration;
249 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
250 avpkt->duration = ff_samples_to_time_base(avctx,
251 frame->nb_samples);
252 }
253 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
255
256 if (frame_sd && frame_sd->size >= 10) {
257 int skip_samples = AV_RL32(frame_sd->data + 0);
258 int discard_padding = AV_RL32(frame_sd->data + 4);
259
260 if (discard_padding > 0 && avctx->frame_size && ec->last_audio_frame) {
262 avpkt->duration = FFMIN(avpkt->duration, ff_samples_to_time_base(avctx, avctx->frame_size));
263 discard_padding = avctx->frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
264 }
265
266 if (skip_samples > 0 || discard_padding > 0) {
267 uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
268 if (!packet_sd)
269 return AVERROR(ENOMEM);
270 AV_WL32A(packet_sd + 0, skip_samples);
271 AV_WL32A(packet_sd + 4, discard_padding);
272 AV_WL8 (packet_sd + 8, AV_RB8(frame_sd->data + 8));
273 AV_WL8 (packet_sd + 9, AV_RB8(frame_sd->data + 9));
274 }
275 }
276 }
277 }
278
279 return 0;
280}
281
283 AVPacket *pkt, const AVFrame *frame)
284{
285 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
286 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
287 if (ret < 0)
288 return ret;
289 pkt->opaque = frame->opaque;
290 }
291
292 return 0;
293}
294
296 AVFrame *frame, int *got_packet)
297{
298 const FFCodec *const codec = ffcodec(avctx->codec);
299 int ret;
300
301 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
303 av_assert0(ret <= 0);
304
305 if (!ret && *got_packet) {
306 if (avpkt->data) {
307 ret = encode_make_refcounted(avctx, avpkt);
308 if (ret < 0)
309 goto unref;
310 // Date returned by encoders must always be ref-counted
311 av_assert0(avpkt->buf);
312 }
313
314 // set the timestamps for the simple no-delay case
315 // encoders with delay have to set the timestamps themselves
316 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
318 ret = encode_set_packet_props(avctx, avpkt, frame);
319 if (ret < 0)
320 goto unref;
321
322 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
323 if (ret < 0)
324 goto unref;
325 }
326
327 // dts equals pts unless there is reordering
328 // there can be no reordering if there is no encoder delay
332 avpkt->dts = avpkt->pts;
333 } else {
334unref:
335 av_packet_unref(avpkt);
336 }
337
338 if (frame)
340
341 return ret;
342}
343
345{
346 AVCodecInternal *avci = avctx->internal;
347 AVFrame *frame = avci->in_frame;
348 const FFCodec *const codec = ffcodec(avctx->codec);
349 int got_packet;
350 int ret;
351
352 if (avci->draining_done)
353 return AVERROR_EOF;
354
355 if (!frame->buf[0] && !avci->draining) {
357 ret = ff_encode_get_frame(avctx, frame);
358 if (ret < 0 && ret != AVERROR_EOF)
359 return ret;
360 }
361
362 if (!frame->buf[0]) {
363 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
365 return AVERROR_EOF;
366
367 // Flushing is signaled with a NULL frame
368 frame = NULL;
369 }
370
371 got_packet = 0;
372
374
375#if CONFIG_FRAME_THREAD_ENCODER
376 if (avci->frame_thread_encoder)
377 /* This will unref frame. */
378 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
379 else
380#endif
381 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
382
383 if (avci->draining && !got_packet)
384 avci->draining_done = 1;
385
386 return ret;
387}
388
390{
391 int ret;
392
393 while (!avpkt->data && !avpkt->side_data) {
394 ret = encode_simple_internal(avctx, avpkt);
395 if (ret < 0)
396 return ret;
397 }
398
399 return 0;
400}
401
403{
404 AVCodecInternal *avci = avctx->internal;
405 int ret;
406
407 if (avci->draining_done)
408 return AVERROR_EOF;
409
410 av_assert0(!avpkt->data && !avpkt->side_data);
411
412 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
413 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
414 avctx->stats_out[0] = '\0';
415 }
416
418 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
419 if (ret < 0)
420 av_packet_unref(avpkt);
421 else
422 // Encoders must always return ref-counted buffers.
423 // Side-data only packets have no data and can be not ref-counted.
424 av_assert0(!avpkt->data || avpkt->buf);
425 } else
426 ret = encode_simple_receive_packet(avctx, avpkt);
427 if (ret >= 0)
428 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
429
430 if (ret == AVERROR_EOF)
431 avci->draining_done = 1;
432
433 return ret;
434}
435
436#if CONFIG_LCMS2
438{
439 enum AVColorTransferCharacteristic trc = frame->color_trc;
440 enum AVColorPrimaries prim = frame->color_primaries;
441 const FFCodec *const codec = ffcodec(avctx->codec);
442 AVCodecInternal *avci = avctx->internal;
443 cmsHPROFILE profile;
444 int ret;
445
446 /* don't generate ICC profiles if disabled or unsupported */
447 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
448 return 0;
450 return 0;
451
452 if (trc == AVCOL_TRC_UNSPECIFIED)
453 trc = avctx->color_trc;
454 if (prim == AVCOL_PRI_UNSPECIFIED)
455 prim = avctx->color_primaries;
456 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
457 return 0; /* can't generate ICC profile with missing csp tags */
458
460 return 0; /* don't overwrite existing ICC profile */
461
462 if (!avci->icc.avctx) {
463 ret = ff_icc_context_init(&avci->icc, avctx);
464 if (ret < 0)
465 return ret;
466 }
467
468 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
469 if (ret < 0)
470 return ret;
471
472 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
473 cmsCloseProfile(profile);
474 return ret;
475}
476#else /* !CONFIG_LCMS2 */
481#endif
482
484{
485 AVCodecInternal *avci = avctx->internal;
486 EncodeContext *ec = encode_ctx(avci);
487 AVFrame *dst = avci->buffer_frame;
488 int ret;
489
490 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
491 /* extract audio service type metadata */
493 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
494 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
495
496 /* check for valid frame size */
497 if (avctx->frame_size) {
498 /* if we already got an undersized frame, that must have been the last */
499 if (ec->last_audio_frame) {
500 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
501 return AVERROR(EINVAL);
502 }
503 if (src->nb_samples > avctx->frame_size) {
504 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
505 return AVERROR(EINVAL);
506 }
507 if (src->nb_samples < avctx->frame_size) {
508 ec->last_audio_frame = 1;
511 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
512 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
513
514 if (out_samples != src->nb_samples) {
515 ret = pad_last_frame(avctx, dst, src, out_samples);
516 if (ret < 0)
517 return ret;
518 goto finish;
519 }
520 }
521 }
522 }
523 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
525
526 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
527 if (src->hw_frames_ctx) {
528 const AVHWFramesContext *frames_ctx = (AVHWFramesContext *)src->hw_frames_ctx->data;
529
530 if (frames_ctx->width < avctx->width || frames_ctx->height < avctx->height) {
531 av_log(avctx, AV_LOG_ERROR, "Hardware frames are smaller than the encoder\n");
532 return AVERROR(EINVAL);
533 }
534 }
535 } else if (src->format != avctx->pix_fmt ||
536 src->width != avctx->width || src->height != avctx->height) {
537 av_log(avctx, AV_LOG_ERROR, "Frame size or pixel format does not match the encoder\n");
538 return AVERROR(EINVAL);
539 }
540 }
541
543 if (ret < 0)
544 return ret;
545
546finish:
547
548 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
550 if (ret < 0)
551 return ret;
552 }
553
554 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
555 // since otherwise we cannot be sure that whatever value it has is in the
556 // right timebase, so we would produce an incorrect value, which is worse
557 // than none at all
558 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
559 dst->duration = 0;
560
561 return 0;
562}
563
565{
566 AVCodecInternal *avci = avctx->internal;
567 int ret;
568
569 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
570 return AVERROR(EINVAL);
571
572 if (avci->draining)
573 return AVERROR_EOF;
574
575 if (avci->buffer_frame->buf[0])
576 return AVERROR(EAGAIN);
577
578 if (!frame) {
579 avci->draining = 1;
580 } else {
582 if (ret < 0)
583 return ret;
584 }
585
586 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
588 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
589 return ret;
590 }
591
592 avctx->frame_num++;
593
594 return 0;
595}
596
598{
599 AVCodecInternal *avci = avctx->internal;
600 int ret;
601
602 av_packet_unref(avpkt);
603
604 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
605 return AVERROR(EINVAL);
606
607 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
608 av_packet_move_ref(avpkt, avci->buffer_pkt);
609 } else {
610 ret = encode_receive_packet_internal(avctx, avpkt);
611 if (ret < 0)
612 return ret;
613 }
614
615 return 0;
616}
617
619{
620 const AVCodec *codec = avctx->codec;
621 const FFCodec *codec2 = ffcodec(codec);
622 const AVDictionaryEntry *t = NULL;
624 int ret;
625
627
628 ret = av_dict_copy(&copy, *dict, 0);
629 if (ret < 0)
630 goto end;
631
632 // Remove the dictionary entries that would be applied to the private codec context
633 if (codec->priv_class) {
634 while ((t = av_dict_iterate(*dict, t))) {
635 if (av_opt_find(avctx->priv_data, t->key, NULL,
637 av_dict_set(dict, t->key, NULL, 0);
638 }
639 }
640
641 // Ditto for global options
642 if (codec2->defaults) {
643 const FFCodecDefault *d = codec2->defaults;
644 while (d->key) {
646 av_dict_set(dict, d->key, NULL, 0);
647 d++;
648 }
649 }
650
651 // If any entry remains, then the requested option/s don't exist or are not settable.
652 if (av_dict_count(*dict)) {
654 goto end;
655 }
656
657 ret = av_dict_copy(dict, copy, 0);
658 if (ret < 0)
659 goto end;
660
661 // Do a dry run of applying the options, to ensure the encoder is unchanged in case
662 // one of them has an invalid value.
663 // This is done twice, once for avctx and once for the AVCodec, because using the
664 // search children flag in combination with the fake obj flag will iterate through
665 // the options from all compiled in codecs if you pass the avctx class.
666 if (codec->priv_class) {
668 if (ret < 0)
669 goto end;
670 }
672 if (ret < 0)
673 goto end;
674
675 // The dictionary should be empty.
677
679 if (ret < 0)
680 goto end;
681
682 // The dictionary should be empty.
683 av_assert0(!av_dict_count(*dict));
684
685 ret = 0;
686end:
688
689 return ret;
690}
691
693{
694 const FFCodec *codec = ffcodec(avctx->codec);
695 int ret = AVERROR_BUG;
696
697 if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
698 return AVERROR(EINVAL);
699
701 av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n");
702 return AVERROR(ENOSYS);
703 }
704
705 if (codec->reconf)
706 ret = codec->reconf(avctx, dict);
707 else
708 ret = ff_encode_reconf_parse_dict(avctx, dict);
709 if (ret < 0)
710 return ret;
711
712 return 0;
713}
714
716{
717 const AVCodec *c = avctx->codec;
718 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
719 const enum AVPixelFormat *pix_fmts;
720 int ret, i, num_pix_fmts;
721
722 if (!pixdesc) {
723 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
724 avctx->pix_fmt);
725 return AVERROR(EINVAL);
726 }
727
729 0, (const void **) &pix_fmts, &num_pix_fmts);
730 if (ret < 0)
731 return ret;
732
733 if (pix_fmts) {
734 for (i = 0; i < num_pix_fmts; i++)
735 if (avctx->pix_fmt == pix_fmts[i])
736 break;
737 if (i == num_pix_fmts) {
738 av_log(avctx, AV_LOG_ERROR,
739 "Specified pixel format %s is not supported by the %s encoder.\n",
740 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
741
742 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
743 for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
744 av_log(avctx, AV_LOG_ERROR, " %s\n",
746 }
747
748 return AVERROR(EINVAL);
749 }
756 }
757
758 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
759 const enum AVAlphaMode *alpha_modes;
760 int num_alpha_modes;
762 0, (const void **) &alpha_modes, &num_alpha_modes);
763 if (ret < 0)
764 return ret;
765
766 if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
767 for (i = 0; i < num_alpha_modes; i++) {
768 if (avctx->alpha_mode == alpha_modes[i])
769 break;
770 }
771 if (i == num_alpha_modes) {
772 av_log(avctx, AV_LOG_ERROR,
773 "Specified alpha mode '%s' is not supported by the %s encoder.\n",
774 av_alpha_mode_name(avctx->alpha_mode), c->name);
775 av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
776 for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
777 av_log(avctx, AV_LOG_ERROR, " %s\n",
778 av_alpha_mode_name(alpha_modes[p]));
779 }
780 return AVERROR(EINVAL);
781 }
782 }
783 }
784
785 if ( avctx->bits_per_raw_sample < 0
786 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
787 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
788 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
789 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
790 }
791 if (avctx->width <= 0 || avctx->height <= 0) {
792 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
793 return AVERROR(EINVAL);
794 }
795
796 if (avctx->hw_frames_ctx) {
798 if (frames_ctx->format != avctx->pix_fmt) {
799 av_log(avctx, AV_LOG_ERROR,
800 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
801 return AVERROR(EINVAL);
802 }
803 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
804 avctx->sw_pix_fmt != frames_ctx->sw_format) {
805 av_log(avctx, AV_LOG_ERROR,
806 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
807 "and AVHWFramesContext.sw_format (%s)\n",
809 av_get_pix_fmt_name(frames_ctx->sw_format));
810 return AVERROR(EINVAL);
811 }
812 avctx->sw_pix_fmt = frames_ctx->sw_format;
813 }
814
815 return 0;
816}
817
819{
820 const AVCodec *c = avctx->codec;
821 const enum AVSampleFormat *sample_fmts;
822 const int *supported_samplerates;
823 const AVChannelLayout *ch_layouts;
824 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
825
826 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
827 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
828 avctx->sample_fmt);
829 return AVERROR(EINVAL);
830 }
831
833 0, (const void **) &sample_fmts,
834 &num_sample_fmts);
835 if (ret < 0)
836 return ret;
837 if (sample_fmts) {
838 for (i = 0; i < num_sample_fmts; i++) {
839 if (avctx->sample_fmt == sample_fmts[i])
840 break;
841 if (avctx->ch_layout.nb_channels == 1 &&
844 avctx->sample_fmt = sample_fmts[i];
845 break;
846 }
847 }
848 if (i == num_sample_fmts) {
849 av_log(avctx, AV_LOG_ERROR,
850 "Specified sample format %s is not supported by the %s encoder\n",
851 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
852
853 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
854 for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
855 av_log(avctx, AV_LOG_ERROR, " %s\n",
857 }
858
859 return AVERROR(EINVAL);
860 }
861 }
862
864 0, (const void **) &supported_samplerates,
865 &num_samplerates);
866 if (ret < 0)
867 return ret;
868 if (supported_samplerates) {
869 for (i = 0; i < num_samplerates; i++)
870 if (avctx->sample_rate == supported_samplerates[i])
871 break;
872 if (i == num_samplerates) {
873 av_log(avctx, AV_LOG_ERROR,
874 "Specified sample rate %d is not supported by the %s encoder\n",
875 avctx->sample_rate, c->name);
876
877 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
878 for (int p = 0; supported_samplerates[p]; p++)
879 av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
880
881 return AVERROR(EINVAL);
882 }
883 }
885 0, (const void **) &ch_layouts, &num_ch_layouts);
886 if (ret < 0)
887 return ret;
888 if (ch_layouts) {
889 for (i = 0; i < num_ch_layouts; i++) {
890 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
891 break;
892 }
893 if (i == num_ch_layouts) {
894 char buf[512];
895 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
896 av_log(avctx, AV_LOG_ERROR,
897 "Specified channel layout '%s' is not supported by the %s encoder\n",
898 ret > 0 ? buf : "?", c->name);
899
900 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
901 for (int p = 0; ch_layouts[p].nb_channels; p++) {
902 ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
903 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
904 }
905 return AVERROR(EINVAL);
906 }
907 }
908
909 if (!avctx->bits_per_raw_sample)
911 if (!avctx->bits_per_raw_sample)
913
914 return 0;
915}
916
918{
919 AVCodecInternal *avci = avctx->internal;
920 EncodeContext *ec = encode_ctx(avci);
921 int ret = 0;
922
923 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
924 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
925 return AVERROR(EINVAL);
926 }
927
928 if (avctx->bit_rate < 0) {
929 av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
930 return AVERROR(EINVAL);
931 }
932
933 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
935 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
936 "encoder does not support it.\n");
937 return AVERROR(EINVAL);
938 }
939
940 switch (avctx->codec_type) {
941 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
942 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
943 }
944 if (ret < 0)
945 return ret;
946
947 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
948 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
949 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
950 }
951
952 if (!avctx->rc_initial_buffer_occupancy)
953 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
954
957
959 avci->in_frame = av_frame_alloc();
960 if (!avci->in_frame)
961 return AVERROR(ENOMEM);
962 }
963
964 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
966 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
967 "from an encoder not supporting it\n");
968 return AVERROR(ENOSYS);
969 }
970
971 avci->recon_frame = av_frame_alloc();
972 if (!avci->recon_frame)
973 return AVERROR(ENOMEM);
974 }
975
976 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
977 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
978 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
979 const AVFrameSideData *sd_frame;
980 AVPacketSideData *sd_packet;
981
984 type_frame);
985 if (!sd_frame ||
987 type_packet))
988
989 continue;
990
992 type_packet, sd_frame->size, 0);
993 if (!sd_packet)
994 return AVERROR(ENOMEM);
995
996 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
997 }
998
999#if CONFIG_FRAME_THREAD_ENCODER
1001 if (ret < 0)
1002 return ret;
1003#endif
1004
1005 return 0;
1006}
1007
1009{
1010 int ret;
1011
1013
1014 frame->format = avctx->pix_fmt;
1015 if (frame->width <= 0 || frame->height <= 0) {
1016 frame->width = avctx->width;
1017 frame->height = avctx->height;
1018 }
1019
1021 if (ret < 0) {
1022 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1024 return ret;
1025 }
1026
1027 return 0;
1028}
1029
1031{
1032 AVCodecInternal *avci = avctx->internal;
1033
1034 if (!avci->recon_frame)
1035 return AVERROR(EINVAL);
1036 if (!avci->recon_frame->buf[0])
1037 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
1038
1040 return 0;
1041}
1042
1044{
1045 AVCodecInternal *avci = avctx->internal;
1046
1047 if (avci->in_frame)
1048 av_frame_unref(avci->in_frame);
1049 if (avci->recon_frame)
1051}
1052
1054{
1055 return av_mallocz(sizeof(EncodeContext));
1056}
1057
1059{
1061 AVCPBProperties *props;
1062 size_t size;
1063 int i;
1064
1065 for (i = 0; i < avctx->nb_coded_side_data; i++)
1067 return (AVCPBProperties *)avctx->coded_side_data[i].data;
1068
1069 props = av_cpb_properties_alloc(&size);
1070 if (!props)
1071 return NULL;
1072
1073 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1074 if (!tmp) {
1075 av_freep(&props);
1076 return NULL;
1077 }
1078
1079 avctx->coded_side_data = tmp;
1080 avctx->nb_coded_side_data++;
1081
1083 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1084 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1085
1086 return props;
1087}
1088
1090 int error_count, enum AVPictureType pict_type)
1091{
1092 uint8_t *side_data;
1093 size_t side_data_size;
1094
1095 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1096 if (!side_data) {
1097 side_data_size = 4+4+8*error_count;
1099 side_data_size);
1100 }
1101
1102 if (!side_data || side_data_size < 4+4+8*error_count)
1103 return AVERROR(ENOMEM);
1104
1105 AV_WL32(side_data, quality);
1106 side_data[4] = pict_type;
1107 side_data[5] = error_count;
1108 for (int i = 0; i < error_count; ++i)
1109 AV_WL64(side_data+8 + 8*i , error[i]);
1110
1111 return 0;
1112}
1113
1114int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
1115{
1116 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
1117 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
1118 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
1119 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
1120 uint16_t *matrix = matrices[m];
1121 if (matrix && (types & (1U << m))) {
1122 for (int i = 0; i < 64; i++) {
1123 if (matrix[i] < min || matrix[i] > max) {
1124 av_log(avctx, AV_LOG_ERROR, "%s matrix[%d] is %d which is out of the allowed range [%"PRIu16"-%"PRIu16"].\n", names[m], i, matrix[i], min, max);
1125 return AVERROR(EINVAL);
1126 }
1127 }
1128 }
1129 }
1130 return 0;
1131}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static void finish(void)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
const SideDataMap ff_sd_global_map[]
A map between packet and frame side data types.
Definition avcodec.c:56
Libavcodec external API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FF_CODEC_CAP_EOF_FLUSH
The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it only wants to be flushe...
@ FF_CODEC_CB_TYPE_ENCODE
@ FF_CODEC_CB_TYPE_RECEIVE_PACKET
static av_always_inline const FFCodec * ffcodec(const AVCodec *codec)
#define FF_CODEC_CAP_ICC_PROFILES
Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
#define av_sat_add64
Definition common.h:139
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define min(a, b)
#define max(a, b)
AVCPBProperties * av_cpb_properties_alloc(size_t *size)
Allocate a CPB properties structure and initialize its fields to default values.
Definition utils.c:986
AVAudioServiceType
Definition defs.h:251
static AVPacket * pkt
static AVFrame * frame
#define ff_assert1_fpu()
Definition emms.h:98
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:63
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:107
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition encode.c:220
static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:138
static int encode_preinit_audio(AVCodecContext *avctx)
Definition encode.c:818
static int encode_preinit_video(AVCodecContext *avctx)
Definition encode.c:715
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition encode.c:1058
AVCodecInternal * ff_encode_internal_alloc(void)
Definition encode.c:1053
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
Definition encode.c:483
static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
Definition encode.c:477
int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, AVFrame *frame, int *got_packet)
Definition encode.c:295
static EncodeContext * encode_ctx(AVCodecInternal *avci)
Definition encode.c:58
static int encode_set_packet_props(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame)
Definition encode.c:235
int ff_encode_preinit(AVCodecContext *avctx)
Definition encode.c:917
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
Definition encode.c:1089
static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
Pad last frame with silence.
Definition encode.c:158
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:402
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:389
int ff_encode_reordered_opaque(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Propagate user opaque values from the frame to avctx/pkt as needed.
Definition encode.c:282
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:344
int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
avcodec_receive_frame() implementation for encoders.
Definition encode.c:1030
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
Definition encode.c:1008
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
Definition encode.c:1114
av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
Definition encode.c:618
void ff_encode_flush_buffers(AVCodecContext *avctx)
Definition encode.c:1043
static av_always_inline int64_t ff_samples_from_time_base(const AVCodecContext *avctx, int64_t duration)
Rescale from time base to AVCodecContext.sample_rate.
Definition encode.h:108
static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition encode.h:96
int ret
Definition ffplay.c:391
reference-counted frame API
av_cold int ff_frame_thread_encoder_init(AVCodecContext *avctx)
Initialize frame thread encoder.
int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, AVFrame *frame, int *got_packet_ptr)
#define fail
Definition test.h:479
#define AV_OPT_FLAG_RUNTIME_PARAM
A generic parameter which can be set by the user at runtime.
Definition opt.h:376
av_cold int avcodec_encode_reconfigure(AVCodecContext *avctx, AVDictionary **dict)
Try to reconfigure the encoder with the provided dictionary.
Definition encode.c:692
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_ENCODER_RECONF
Encoder can be reconfigured by passing new initialization parameters.
Definition codec.h:54
#define AV_CODEC_FLAG2_ICC_PROFILES
Generate/parse ICC profiles on encode/decode, as appropriate for the type of file.
Definition avcodec.h:382
int av_codec_is_encoder(const AVCodec *codec)
Definition utils.c:79
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
Definition codec_desc.h:72
#define AV_CODEC_FLAG_FRAME_DURATION
Signal to the encoder that the values of AVFrame.duration are valid and should be used (typically for...
Definition avcodec.h:286
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition avcodec.h:290
#define AV_CODEC_FLAG2_FIXED_FRAME_SIZE
Force audio encoders to use a fixed frame size.
Definition avcodec.h:359
#define AV_CODEC_CAP_PARAM_CHANGE
Codec supports changed parameters at any point.
Definition codec.h:106
int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:461
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
#define AV_CODEC_CAP_ENCODER_RECON_FRAME
The encoder is able to output reconstructed frame data, i.e.
Definition codec.h:162
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition avcodec.h:279
#define AV_CODEC_PROP_REORDER
Codec supports frame reordering.
Definition codec_desc.h:92
#define AV_CODEC_FLAG_RECON_FRAME
Request the encoder to output reconstructed frames, i.e. frames that would be produced by decoding th...
Definition avcodec.h:244
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition get_buffer.c:253
int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition encode.c:597
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition encode.c:564
int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
The default callback for AVCodecContext.get_encode_buffer().
Definition encode.c:85
int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec, enum AVCodecConfig config, unsigned flags, const void **out, int *out_num)
Retrieve a list of all supported values for a given configuration type.
Definition avcodec.c:820
@ AV_CODEC_CONFIG_PIX_FORMAT
AVPixelFormat, terminated by AV_PIX_FMT_NONE.
Definition avcodec.h:2593
@ AV_CODEC_CONFIG_SAMPLE_FORMAT
AVSampleFormat, terminated by AV_SAMPLE_FMT_NONE.
Definition avcodec.h:2596
@ AV_CODEC_CONFIG_ALPHA_MODE
AVAlphaMode, terminated by AVALPHA_MODE_UNSPECIFIED.
Definition avcodec.h:2600
@ AV_CODEC_CONFIG_SAMPLE_RATE
int, terminated by 0
Definition avcodec.h:2595
@ AV_CODEC_CONFIG_CHANNEL_LAYOUT
AVChannelLayout, terminated by {0}.
Definition avcodec.h:2597
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition encode.c:205
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition utils.c:53
int avcodec_is_open(AVCodecContext *s)
Definition avcodec.c:700
AVPacketSideData * av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, size_t size, int flags)
Allocate a new packet side data.
Definition packet.c:621
AVPacketSideDataType
Definition packet.h:41
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition packet.c:571
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_QUALITY_STATS
This side data contains quality related information from the encoder.
Definition packet.h:129
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition packet.h:142
@ AV_PKT_DATA_NB
The number of side data types.
Definition packet.h:401
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
int av_buffer_realloc(AVBufferRef **pbuf, size_t size)
Reallocate a given buffer.
Definition buffer.c:184
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition buffer.c:234
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:43
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:250
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:38
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition error.h:63
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:499
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition frame.c:662
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:209
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition frame.c:526
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:281
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, size_t size)
Add a new side data to a frame.
Definition frame.c:650
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
AVFrameSideDataType
Definition frame.h:49
static const AVFrameSideData * av_frame_side_data_get(AVFrameSideData *const *sd, const int nb_sd, enum AVFrameSideDataType type)
Wrapper around av_frame_side_data_get_c() to workaround the limitation that for any type T the conver...
Definition frame.h:1206
@ AV_FRAME_DATA_AUDIO_SERVICE_TYPE
This side data must be associated with an audio frame and corresponds to enum AVAudioServiceType defi...
Definition frame.h:114
@ AV_FRAME_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition frame.h:109
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition frame.h:144
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
AVPictureType
Definition avutil.h:276
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:109
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
Definition samplefmt.c:87
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition samplefmt.c:52
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:247
int av_samples_copy(uint8_t *const *dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition samplefmt.c:223
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
Definition opt.c:2084
#define AV_OPT_SEARCH_FAKE_OBJ
The obj passed to av_opt_find() or av_opt_set() is fake – only a double pointer to AVClass instead of...
Definition opt.h:612
int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
Set all the options from a given dictionary on an object.
Definition opt.c:2055
#define AV_WL8(p, d)
#define AV_RB8(x)
#define AV_WL64(p, v)
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_WL32A(p, v)
#define AV_WL16A(p, v)
int ff_icc_profile_attach(FFIccContext *s, cmsHPROFILE profile, AVFrame *frame)
Attach an ICC profile to a frame.
Definition fflcms2.c:171
int ff_icc_profile_generate(FFIccContext *s, enum AVColorPrimaries color_prim, enum AVColorTransferCharacteristic color_trc, cmsHPROFILE *out_profile)
Generate an ICC profile for a given combination of color primaries and transfer function.
Definition fflcms2.c:144
int ff_icc_context_init(FFIccContext *s, void *avctx)
Initializes an FFIccContext.
Definition fflcms2.c:30
common internal api header.
#define av_unused
Definition attributes.h:164
#define av_cold
Definition attributes.h:117
common internal API header
#define attribute_align_arg
Definition internal.h:51
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
const char data[16]
Definition mxf.c:149
int profile
Definition mxfenc.c:2300
AVOptions.
const char * av_alpha_mode_name(enum AVAlphaMode mode)
Definition pixdesc.c:3949
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
AVAlphaMode
Correlation between the alpha channel and color values.
Definition pixfmt.h:825
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition pixfmt.h:826
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:647
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:650
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:677
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:680
#define FF_ARRAY_ELEMS(a)
uint8_t * data
The data buffer.
Definition buffer.h:90
This structure describes the bitrate properties of an encoded bitstream.
Definition defs.h:298
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
uint16_t * chroma_intra_matrix
custom intra quantization matrix
Definition avcodec.h:980
int width
picture width / height.
Definition avcodec.h:605
AVPacketSideData * coded_side_data
Additional data associated with the entire coded stream.
Definition avcodec.h:1778
char * stats_out
pass1 encoding statistics output buffer
Definition avcodec.h:1335
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition avcodec.h:1719
int rc_buffer_size
decoder bitstream buffer size
Definition avcodec.h:1278
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1059
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:651
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:685
int nb_coded_side_data
Definition avcodec.h:1779
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition avcodec.h:1093
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:658
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition avcodec.h:1477
enum AVMediaType codec_type
Definition avcodec.h:451
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1893
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition avcodec.h:973
int(* get_encode_buffer)(struct AVCodecContext *s, AVPacket *pkt, int flags)
This callback is called at the beginning of each packet to get a data buffer for it.
Definition avcodec.h:1882
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
const struct AVCodec * codec
Definition avcodec.h:452
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition avcodec.h:1321
int nb_decoded_side_data
Definition avcodec.h:1940
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1581
int initial_padding
Audio only.
Definition avcodec.h:1118
int sample_rate
samples per second
Definition avcodec.h:1044
const AVClass * av_class
information on struct for av_log
Definition avcodec.h:448
uint16_t * intra_matrix
custom intra quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition avcodec.h:964
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:665
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
AVFrameSideData ** decoded_side_data
Array containing static side data, such as HDR10 CLL / MDCV structures.
Definition avcodec.h:1939
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
Definition avcodec.h:1947
enum AVCodecID codec_id
Definition avcodec.h:453
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1072
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition codec_desc.h:54
AVFrame * recon_frame
When the AV_CODEC_FLAG_RECON_FRAME flag is used.
Definition internal.h:114
int pad_samples
Audio encoders can set this flag during init to indicate that they want the small last frame to be pa...
Definition internal.h:67
unsigned int byte_buffer_size
Definition internal.h:96
uint8_t * byte_buffer
temporary buffer used for encoders to store their bitstream
Definition internal.h:95
AVFrame * buffer_frame
Definition internal.h:145
AVPacket * buffer_pkt
Temporary buffers for newly received or not yet output packets/frames.
Definition internal.h:144
AVFrame * in_frame
The input frame is stored here for encoders implementing the simple encode API.
Definition internal.h:106
int draining
decoding: AVERROR_EOF has been returned from ff_decode_get_packet(); must not be used by decoders tha...
Definition internal.h:139
void * frame_thread_encoder
Definition internal.h:98
AVCodec.
Definition codec.h:175
const AVClass * priv_class
AVClass for the private context.
Definition codec.h:197
enum AVMediaType type
Definition codec.h:188
int capabilities
Codec capabilities.
Definition codec.h:194
int depth
Number of bits in the component.
Definition pixdesc.h:57
char * key
Definition dict.h:91
Structure to hold side data for an AVFrame.
Definition frame.h:334
size_t size
Definition frame.h:337
uint8_t * data
Definition frame.h:336
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition frame.h:656
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:431
uint8_t * data
Definition packet.h:432
enum AVPacketSideDataType type
Definition packet.h:434
This structure stores compressed data.
Definition packet.h:587
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:593
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:616
int size
Definition packet.h:611
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:628
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:609
uint8_t * data
Definition packet.h:610
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition packet.h:621
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint64_t flags
Combination of AV_PIX_FMT_FLAG_... flags.
Definition pixdesc.h:94
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
uint32_t start_display_time
Definition avcodec.h:2109
int intra_only_flag
This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only formats (i.e.
Definition encode.c:49
AVCodecInternal avci
Definition encode.c:42
int last_audio_frame
An audio frame with less than required samples has been submitted (and potentially padded with silenc...
Definition encode.c:55
const char * key
unsigned cb_type
This field determines the type of the codec (decoder/encoder) and also the exact callback cb implemen...
const FFCodecDefault * defaults
Private codec-specific defaults.
int(* receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt)
Encode API with decoupled frame/packet dataflow.
int(* reconf)(struct AVCodecContext *avctx, struct AVDictionary **dict)
Encoding only.
int(* encode_sub)(struct AVCodecContext *avctx, uint8_t *buf, int buf_size, const struct AVSubtitle *sub)
Encode subtitles to a raw buffer.
int(* encode)(struct AVCodecContext *avctx, struct AVPacket *avpkt, const struct AVFrame *frame, int *got_packet_ptr)
Encode data to an AVPacket.
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
union FFCodec::@344166142000117327246261075357045351044045072174 cb
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
int size
static void copy(const float *p1, float *p2, const int length)
static const uint8_t quality[]
Definition vmixdec.c:58
static double c[64]