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) {
261 if (!frame->duration)
262 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples - discard_padding);
264 avpkt->duration = FFMIN(avpkt->duration, ff_samples_to_time_base(avctx, frame->nb_samples));
265 discard_padding = FFMAX(0, discard_padding - avctx->initial_padding);
266 }
267
268 if (skip_samples > 0 || discard_padding > 0) {
269 uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
270 if (!packet_sd)
271 return AVERROR(ENOMEM);
272 AV_WL32A(packet_sd + 0, skip_samples);
273 AV_WL32A(packet_sd + 4, discard_padding);
274 AV_WL8 (packet_sd + 8, AV_RB8(frame_sd->data + 8));
275 AV_WL8 (packet_sd + 9, AV_RB8(frame_sd->data + 9));
276 }
277 }
278 }
279 }
280
281 return 0;
282}
283
285 AVPacket *pkt, const AVFrame *frame)
286{
287 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
288 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
289 if (ret < 0)
290 return ret;
291 pkt->opaque = frame->opaque;
292 }
293
294 return 0;
295}
296
298 AVFrame *frame, int *got_packet)
299{
300 const FFCodec *const codec = ffcodec(avctx->codec);
301 int ret;
302
303 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
305 av_assert0(ret <= 0);
306
307 if (!ret && *got_packet) {
308 if (avpkt->data) {
309 ret = encode_make_refcounted(avctx, avpkt);
310 if (ret < 0)
311 goto unref;
312 // Date returned by encoders must always be ref-counted
313 av_assert0(avpkt->buf);
314 }
315
316 // set the timestamps for the simple no-delay case
317 // encoders with delay have to set the timestamps themselves
318 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
320 ret = encode_set_packet_props(avctx, avpkt, frame);
321 if (ret < 0)
322 goto unref;
323
324 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
325 if (ret < 0)
326 goto unref;
327 }
328
329 // dts equals pts unless there is reordering
330 // there can be no reordering if there is no encoder delay
334 avpkt->dts = avpkt->pts;
335 } else {
336unref:
337 av_packet_unref(avpkt);
338 }
339
340 if (frame)
342
343 return ret;
344}
345
347{
348 AVCodecInternal *avci = avctx->internal;
349 AVFrame *frame = avci->in_frame;
350 const FFCodec *const codec = ffcodec(avctx->codec);
351 int got_packet;
352 int ret;
353
354 if (avci->draining_done)
355 return AVERROR_EOF;
356
357 if (!frame->buf[0] && !avci->draining) {
359 ret = ff_encode_get_frame(avctx, frame);
360 if (ret < 0 && ret != AVERROR_EOF)
361 return ret;
362 }
363
364 if (!frame->buf[0]) {
365 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
367 return AVERROR_EOF;
368
369 // Flushing is signaled with a NULL frame
370 frame = NULL;
371 }
372
373 got_packet = 0;
374
376
377#if CONFIG_FRAME_THREAD_ENCODER
378 if (avci->frame_thread_encoder)
379 /* This will unref frame. */
380 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
381 else
382#endif
383 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
384
385 if (avci->draining && !got_packet)
386 avci->draining_done = 1;
387
388 return ret;
389}
390
392{
393 int ret;
394
395 while (!avpkt->data && !avpkt->side_data) {
396 ret = encode_simple_internal(avctx, avpkt);
397 if (ret < 0)
398 return ret;
399 }
400
401 return 0;
402}
403
405{
406 AVCodecInternal *avci = avctx->internal;
407 int ret;
408
409 if (avci->draining_done)
410 return AVERROR_EOF;
411
412 av_assert0(!avpkt->data && !avpkt->side_data);
413
414 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
415 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
416 avctx->stats_out[0] = '\0';
417 }
418
420 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
421 if (ret < 0)
422 av_packet_unref(avpkt);
423 else
424 // Encoders must always return ref-counted buffers.
425 // Side-data only packets have no data and can be not ref-counted.
426 av_assert0(!avpkt->data || avpkt->buf);
427 } else
428 ret = encode_simple_receive_packet(avctx, avpkt);
429 if (ret >= 0)
430 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
431
432 if (ret == AVERROR_EOF)
433 avci->draining_done = 1;
434
435 return ret;
436}
437
438#if CONFIG_LCMS2
440{
441 enum AVColorTransferCharacteristic trc = frame->color_trc;
442 enum AVColorPrimaries prim = frame->color_primaries;
443 const FFCodec *const codec = ffcodec(avctx->codec);
444 AVCodecInternal *avci = avctx->internal;
445 cmsHPROFILE profile;
446 int ret;
447
448 /* don't generate ICC profiles if disabled or unsupported */
449 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
450 return 0;
452 return 0;
453
454 if (trc == AVCOL_TRC_UNSPECIFIED)
455 trc = avctx->color_trc;
456 if (prim == AVCOL_PRI_UNSPECIFIED)
457 prim = avctx->color_primaries;
458 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
459 return 0; /* can't generate ICC profile with missing csp tags */
460
462 return 0; /* don't overwrite existing ICC profile */
463
464 if (!avci->icc.avctx) {
465 ret = ff_icc_context_init(&avci->icc, avctx);
466 if (ret < 0)
467 return ret;
468 }
469
470 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
471 if (ret < 0)
472 return ret;
473
474 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
475 cmsCloseProfile(profile);
476 return ret;
477}
478#else /* !CONFIG_LCMS2 */
483#endif
484
486{
487 AVCodecInternal *avci = avctx->internal;
488 EncodeContext *ec = encode_ctx(avci);
489 AVFrame *dst = avci->buffer_frame;
490 int ret;
491
492 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
493 /* extract audio service type metadata */
495 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
496 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
497
498 /* check for valid frame size */
499 if (avctx->frame_size) {
500 /* if we already got an undersized frame, that must have been the last */
501 if (ec->last_audio_frame) {
502 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
503 return AVERROR(EINVAL);
504 }
505 if (src->nb_samples > avctx->frame_size) {
506 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
507 return AVERROR(EINVAL);
508 }
509 if (src->nb_samples < avctx->frame_size) {
510 ec->last_audio_frame = 1;
513 int pad_samples = avci->pad_samples && !(avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE) ?
514 avci->pad_samples : avctx->frame_size;
515 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
516
517 if (out_samples != src->nb_samples) {
518 ret = pad_last_frame(avctx, dst, src, out_samples);
519 if (ret < 0)
520 return ret;
521 goto finish;
522 }
523 }
524 }
525 }
526 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
528
529 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
530 if (src->hw_frames_ctx) {
531 const AVHWFramesContext *frames_ctx = (AVHWFramesContext *)src->hw_frames_ctx->data;
532
533 if (frames_ctx->width < avctx->width || frames_ctx->height < avctx->height) {
534 av_log(avctx, AV_LOG_ERROR, "Hardware frames are smaller than the encoder\n");
535 return AVERROR(EINVAL);
536 }
537 }
538 } else if (src->format != avctx->pix_fmt ||
539 src->width != avctx->width || src->height != avctx->height) {
540 av_log(avctx, AV_LOG_ERROR, "Frame size or pixel format does not match the encoder\n");
541 return AVERROR(EINVAL);
542 }
543 }
544
546 if (ret < 0)
547 return ret;
548
549finish:
550
551 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
553 if (ret < 0)
554 return ret;
555 }
556
557 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
558 // since otherwise we cannot be sure that whatever value it has is in the
559 // right timebase, so we would produce an incorrect value, which is worse
560 // than none at all
561 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
562 dst->duration = 0;
563
564 return 0;
565}
566
568{
569 AVCodecInternal *avci = avctx->internal;
570 int ret;
571
572 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
573 return AVERROR(EINVAL);
574
575 if (avci->draining)
576 return AVERROR_EOF;
577
578 if (avci->buffer_frame->buf[0])
579 return AVERROR(EAGAIN);
580
581 if (!frame) {
582 avci->draining = 1;
583 } else {
585 if (ret < 0)
586 return ret;
587 }
588
589 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
591 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
592 return ret;
593 }
594
595 avctx->frame_num++;
596
597 return 0;
598}
599
601{
602 AVCodecInternal *avci = avctx->internal;
603 int ret;
604
605 av_packet_unref(avpkt);
606
607 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
608 return AVERROR(EINVAL);
609
610 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
611 av_packet_move_ref(avpkt, avci->buffer_pkt);
612 } else {
613 ret = encode_receive_packet_internal(avctx, avpkt);
614 if (ret < 0)
615 return ret;
616 }
617
618 return 0;
619}
620
622{
623 const AVCodec *codec = avctx->codec;
624 const FFCodec *codec2 = ffcodec(codec);
625 const AVDictionaryEntry *t = NULL;
627 int ret;
628
630
631 ret = av_dict_copy(&copy, *dict, 0);
632 if (ret < 0)
633 goto end;
634
635 // Remove the dictionary entries that would be applied to the private codec context
636 if (codec->priv_class) {
637 while ((t = av_dict_iterate(*dict, t))) {
638 if (av_opt_find(avctx->priv_data, t->key, NULL,
640 av_dict_set(dict, t->key, NULL, 0);
641 }
642 }
643
644 // Ditto for global options
645 if (codec2->defaults) {
646 const FFCodecDefault *d = codec2->defaults;
647 while (d->key) {
649 av_dict_set(dict, d->key, NULL, 0);
650 d++;
651 }
652 }
653
654 // If any entry remains, then the requested option/s don't exist or are not settable.
655 if (av_dict_count(*dict)) {
657 goto end;
658 }
659
660 ret = av_dict_copy(dict, copy, 0);
661 if (ret < 0)
662 goto end;
663
664 // Do a dry run of applying the options, to ensure the encoder is unchanged in case
665 // one of them has an invalid value.
666 // This is done twice, once for avctx and once for the AVCodec, because using the
667 // search children flag in combination with the fake obj flag will iterate through
668 // the options from all compiled in codecs if you pass the avctx class.
669 if (codec->priv_class) {
671 if (ret < 0)
672 goto end;
673 }
675 if (ret < 0)
676 goto end;
677
678 // The dictionary should be empty.
680
682 if (ret < 0)
683 goto end;
684
685 // The dictionary should be empty.
686 av_assert0(!av_dict_count(*dict));
687
688 ret = 0;
689end:
691
692 return ret;
693}
694
696{
697 const FFCodec *codec = ffcodec(avctx->codec);
698 int ret = AVERROR_BUG;
699
700 if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
701 return AVERROR(EINVAL);
702
704 av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n");
705 return AVERROR(ENOSYS);
706 }
707
708 if (codec->reconf)
709 ret = codec->reconf(avctx, dict);
710 else
711 ret = ff_encode_reconf_parse_dict(avctx, dict);
712 if (ret < 0)
713 return ret;
714
715 return 0;
716}
717
719{
720 const AVCodec *c = avctx->codec;
721 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
722 const enum AVPixelFormat *pix_fmts;
723 int ret, i, num_pix_fmts;
724
725 if (!pixdesc) {
726 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
727 avctx->pix_fmt);
728 return AVERROR(EINVAL);
729 }
730
732 0, (const void **) &pix_fmts, &num_pix_fmts);
733 if (ret < 0)
734 return ret;
735
736 if (pix_fmts) {
737 for (i = 0; i < num_pix_fmts; i++)
738 if (avctx->pix_fmt == pix_fmts[i])
739 break;
740 if (i == num_pix_fmts) {
741 av_log(avctx, AV_LOG_ERROR,
742 "Specified pixel format %s is not supported by the %s encoder.\n",
743 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
744
745 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
746 for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
747 av_log(avctx, AV_LOG_ERROR, " %s\n",
749 }
750
751 return AVERROR(EINVAL);
752 }
759 }
760
761 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
762 const enum AVAlphaMode *alpha_modes;
763 int num_alpha_modes;
765 0, (const void **) &alpha_modes, &num_alpha_modes);
766 if (ret < 0)
767 return ret;
768
769 if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
770 for (i = 0; i < num_alpha_modes; i++) {
771 if (avctx->alpha_mode == alpha_modes[i])
772 break;
773 }
774 if (i == num_alpha_modes) {
775 av_log(avctx, AV_LOG_ERROR,
776 "Specified alpha mode '%s' is not supported by the %s encoder.\n",
777 av_alpha_mode_name(avctx->alpha_mode), c->name);
778 av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
779 for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
780 av_log(avctx, AV_LOG_ERROR, " %s\n",
781 av_alpha_mode_name(alpha_modes[p]));
782 }
783 return AVERROR(EINVAL);
784 }
785 }
786 }
787
788 if ( avctx->bits_per_raw_sample < 0
789 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
790 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
791 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
792 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
793 }
794 if (avctx->width <= 0 || avctx->height <= 0) {
795 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
796 return AVERROR(EINVAL);
797 }
798
799 if (avctx->hw_frames_ctx) {
801 if (frames_ctx->format != avctx->pix_fmt) {
802 av_log(avctx, AV_LOG_ERROR,
803 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
804 return AVERROR(EINVAL);
805 }
806 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
807 avctx->sw_pix_fmt != frames_ctx->sw_format) {
808 av_log(avctx, AV_LOG_ERROR,
809 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
810 "and AVHWFramesContext.sw_format (%s)\n",
812 av_get_pix_fmt_name(frames_ctx->sw_format));
813 return AVERROR(EINVAL);
814 }
815 avctx->sw_pix_fmt = frames_ctx->sw_format;
816 }
817
818 return 0;
819}
820
822{
823 const AVCodec *c = avctx->codec;
824 const enum AVSampleFormat *sample_fmts;
825 const int *supported_samplerates;
826 const AVChannelLayout *ch_layouts;
827 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
828
829 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
830 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
831 avctx->sample_fmt);
832 return AVERROR(EINVAL);
833 }
834
836 0, (const void **) &sample_fmts,
837 &num_sample_fmts);
838 if (ret < 0)
839 return ret;
840 if (sample_fmts) {
841 for (i = 0; i < num_sample_fmts; i++) {
842 if (avctx->sample_fmt == sample_fmts[i])
843 break;
844 if (avctx->ch_layout.nb_channels == 1 &&
847 avctx->sample_fmt = sample_fmts[i];
848 break;
849 }
850 }
851 if (i == num_sample_fmts) {
852 av_log(avctx, AV_LOG_ERROR,
853 "Specified sample format %s is not supported by the %s encoder\n",
854 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
855
856 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
857 for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
858 av_log(avctx, AV_LOG_ERROR, " %s\n",
860 }
861
862 return AVERROR(EINVAL);
863 }
864 }
865
867 0, (const void **) &supported_samplerates,
868 &num_samplerates);
869 if (ret < 0)
870 return ret;
871 if (supported_samplerates) {
872 for (i = 0; i < num_samplerates; i++)
873 if (avctx->sample_rate == supported_samplerates[i])
874 break;
875 if (i == num_samplerates) {
876 av_log(avctx, AV_LOG_ERROR,
877 "Specified sample rate %d is not supported by the %s encoder\n",
878 avctx->sample_rate, c->name);
879
880 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
881 for (int p = 0; supported_samplerates[p]; p++)
882 av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
883
884 return AVERROR(EINVAL);
885 }
886 }
888 0, (const void **) &ch_layouts, &num_ch_layouts);
889 if (ret < 0)
890 return ret;
891 if (ch_layouts) {
892 for (i = 0; i < num_ch_layouts; i++) {
893 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
894 break;
895 }
896 if (i == num_ch_layouts) {
897 char buf[512];
898 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
899 av_log(avctx, AV_LOG_ERROR,
900 "Specified channel layout '%s' is not supported by the %s encoder\n",
901 ret > 0 ? buf : "?", c->name);
902
903 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
904 for (int p = 0; ch_layouts[p].nb_channels; p++) {
905 ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
906 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
907 }
908 return AVERROR(EINVAL);
909 }
910 }
911
912 if (!avctx->bits_per_raw_sample)
914 if (!avctx->bits_per_raw_sample)
916
917 return 0;
918}
919
921{
922 AVCodecInternal *avci = avctx->internal;
923 EncodeContext *ec = encode_ctx(avci);
924 int ret = 0;
925
926 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
927 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
928 return AVERROR(EINVAL);
929 }
930
931 if (avctx->bit_rate < 0) {
932 av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
933 return AVERROR(EINVAL);
934 }
935
936 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
938 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
939 "encoder does not support it.\n");
940 return AVERROR(EINVAL);
941 }
942
943 switch (avctx->codec_type) {
944 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
945 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
946 }
947 if (ret < 0)
948 return ret;
949
950 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
951 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
952 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
953 }
954
955 if (!avctx->rc_initial_buffer_occupancy)
956 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
957
960
962 avci->in_frame = av_frame_alloc();
963 if (!avci->in_frame)
964 return AVERROR(ENOMEM);
965 }
966
967 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
969 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
970 "from an encoder not supporting it\n");
971 return AVERROR(ENOSYS);
972 }
973
974 avci->recon_frame = av_frame_alloc();
975 if (!avci->recon_frame)
976 return AVERROR(ENOMEM);
977 }
978
979 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
980 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
981 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
982 const AVFrameSideData *sd_frame;
983 AVPacketSideData *sd_packet;
984
987 type_frame);
988 if (!sd_frame ||
990 type_packet))
991
992 continue;
993
995 type_packet, sd_frame->size, 0);
996 if (!sd_packet)
997 return AVERROR(ENOMEM);
998
999 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
1000 }
1001
1002#if CONFIG_FRAME_THREAD_ENCODER
1004 if (ret < 0)
1005 return ret;
1006#endif
1007
1008 return 0;
1009}
1010
1012{
1013 int ret;
1014
1016
1017 frame->format = avctx->pix_fmt;
1018 if (frame->width <= 0 || frame->height <= 0) {
1019 frame->width = avctx->width;
1020 frame->height = avctx->height;
1021 }
1022
1024 if (ret < 0) {
1025 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1027 return ret;
1028 }
1029
1030 return 0;
1031}
1032
1034{
1035 AVCodecInternal *avci = avctx->internal;
1036
1037 if (!avci->recon_frame)
1038 return AVERROR(EINVAL);
1039 if (!avci->recon_frame->buf[0])
1040 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
1041
1043 return 0;
1044}
1045
1047{
1048 AVCodecInternal *avci = avctx->internal;
1049
1050 if (avci->in_frame)
1051 av_frame_unref(avci->in_frame);
1052 if (avci->recon_frame)
1054}
1055
1057{
1058 return av_mallocz(sizeof(EncodeContext));
1059}
1060
1062{
1064 AVCPBProperties *props;
1065 size_t size;
1066 int i;
1067
1068 for (i = 0; i < avctx->nb_coded_side_data; i++)
1070 return (AVCPBProperties *)avctx->coded_side_data[i].data;
1071
1072 props = av_cpb_properties_alloc(&size);
1073 if (!props)
1074 return NULL;
1075
1076 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1077 if (!tmp) {
1078 av_freep(&props);
1079 return NULL;
1080 }
1081
1082 avctx->coded_side_data = tmp;
1083 avctx->nb_coded_side_data++;
1084
1086 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1087 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1088
1089 return props;
1090}
1091
1093 int error_count, enum AVPictureType pict_type)
1094{
1095 uint8_t *side_data;
1096 size_t side_data_size;
1097
1098 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1099 if (!side_data) {
1100 side_data_size = 4+4+8*error_count;
1102 side_data_size);
1103 }
1104
1105 if (!side_data || side_data_size < 4+4+8*error_count)
1106 return AVERROR(ENOMEM);
1107
1108 AV_WL32(side_data, quality);
1109 side_data[4] = pict_type;
1110 side_data[5] = error_count;
1111 for (int i = 0; i < error_count; ++i)
1112 AV_WL64(side_data+8 + 8*i , error[i]);
1113
1114 return 0;
1115}
1116
1117int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
1118{
1119 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
1120 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
1121 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
1122 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
1123 uint16_t *matrix = matrices[m];
1124 if (matrix && (types & (1U << m))) {
1125 for (int i = 0; i < 64; i++) {
1126 if (matrix[i] < min || matrix[i] > max) {
1127 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);
1128 return AVERROR(EINVAL);
1129 }
1130 }
1131 }
1132 }
1133 return 0;
1134}
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:821
static int encode_preinit_video(AVCodecContext *avctx)
Definition encode.c:718
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition encode.c:1061
AVCodecInternal * ff_encode_internal_alloc(void)
Definition encode.c:1056
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
Definition encode.c:485
static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
Definition encode.c:479
int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, AVFrame *frame, int *got_packet)
Definition encode.c:297
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:920
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:1092
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:404
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:391
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:284
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:346
int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
avcodec_receive_frame() implementation for encoders.
Definition encode.c:1033
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
Definition encode.c:1011
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
Definition encode.c:1117
av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
Definition encode.c:621
void ff_encode_flush_buffers(AVCodecContext *avctx)
Definition encode.c:1046
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:695
#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:600
#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:567
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:110
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
const char data[16]
Definition mxf.c:155
int profile
Definition mxfenc.c:2393
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]