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/internal.h"
27#include "libavutil/mem.h"
28#include "libavutil/opt.h"
29#include "libavutil/pixdesc.h"
30#include "libavutil/samplefmt.h"
31
32#include "avcodec.h"
33#include "avcodec_internal.h"
34#include "codec_desc.h"
35#include "codec_internal.h"
36#include "encode.h"
38#include "internal.h"
39
40typedef struct EncodeContext {
42
43 /**
44 * This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only
45 * formats (i.e. whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set).
46 * This is used to set said flag generically for said encoders.
47 */
49
50 /**
51 * An audio frame with less than required samples has been submitted (and
52 * potentially padded with silence). Reject all subsequent frames.
53 */
56
58{
59 return (EncodeContext*)avci;
60}
61
63{
65 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
67 return AVERROR(EINVAL);
68 }
69
70 av_assert0(!avpkt->data);
71
74 avpkt->data = avctx->internal->byte_buffer;
75 if (!avpkt->data) {
76 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
77 return AVERROR(ENOMEM);
78 }
79 avpkt->size = size;
80
81 return 0;
82}
83
85{
86 int ret;
87
88 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89 return AVERROR(EINVAL);
90
91 if (avpkt->data || avpkt->buf) {
92 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
93 return AVERROR(EINVAL);
94 }
95
97 if (ret < 0) {
98 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
99 return ret;
100 }
101 avpkt->data = avpkt->buf->data;
102
103 return 0;
104}
105
107{
108 int ret;
109
111 return AVERROR(EINVAL);
112
113 av_assert0(!avpkt->data && !avpkt->buf);
114
115 avpkt->size = size;
116 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117 if (ret < 0)
118 goto fail;
119
120 if (!avpkt->data || !avpkt->buf) {
121 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
122 ret = AVERROR(EINVAL);
123 goto fail;
124 }
125 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127 ret = 0;
128fail:
129 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131 av_packet_unref(avpkt);
132 }
133
134 return ret;
135}
136
138{
139 uint8_t *data = avpkt->data;
140 int ret;
141
142 if (avpkt->buf)
143 return 0;
144
145 avpkt->data = NULL;
146 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147 if (ret < 0)
148 return ret;
149 memcpy(avpkt->data, data, avpkt->size);
150
151 return 0;
152}
153
154/**
155 * Pad last frame with silence.
156 */
157static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
158{
159 AVFrameSideData *sd;
160 int discard_padding;
161 int ret;
162
163 frame->format = src->format;
164 frame->nb_samples = out_samples;
165 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
166 if (ret < 0)
167 goto fail;
169 if (ret < 0)
170 goto fail;
171
173 if (ret < 0)
174 goto fail;
175
176 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
177 src->nb_samples, s->ch_layout.nb_channels,
178 s->sample_fmt)) < 0)
179 goto fail;
180 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
181 frame->nb_samples - src->nb_samples,
182 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
183 goto fail;
184
185 discard_padding = frame->nb_samples - src->nb_samples;
186 av_assert1(discard_padding > 0);
188 if (!sd) {
189 ret = AVERROR(ENOMEM);
190 goto fail;
191 }
192 AV_WL32A(sd->data, 0);
193 AV_WL32A(sd->data + 4, discard_padding);
194 AV_WL16A(sd->data + 8, 0);
195
196 return 0;
197
198fail:
200 encode_ctx(s->internal)->last_audio_frame = 0;
201 return ret;
202}
203
204int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
205 const AVSubtitle *sub)
206{
207 int ret;
208 if (sub->start_display_time) {
209 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
210 return -1;
211 }
212
213 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
214 avctx->frame_num++;
215 return ret;
216}
217
219{
220 AVCodecInternal *avci = avctx->internal;
221
222 if (avci->draining)
223 return AVERROR_EOF;
224
225 if (!avci->buffer_frame->buf[0])
226 return AVERROR(EAGAIN);
227
229
230 return 0;
231}
232
234{
235 AVCodecInternal *avci = avctx->internal;
236 EncodeContext *ec = encode_ctx(avci);
237
238 if (avpkt->pts == AV_NOPTS_VALUE) {
239 avpkt->pts = frame->pts;
240 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && avpkt->pts != AV_NOPTS_VALUE)
241 avpkt->pts -= ff_samples_to_time_base(avctx, avctx->initial_padding);
242 }
243
244 if (!avpkt->duration) {
245 if (frame->duration)
246 avpkt->duration = frame->duration;
247 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
248 avpkt->duration = ff_samples_to_time_base(avctx,
249 frame->nb_samples);
250 }
251 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
253
254 if (frame_sd && frame_sd->size >= 10) {
255 int skip_samples = AV_RL32(frame_sd->data + 0);
256 int discard_padding = AV_RL32(frame_sd->data + 4);
257
258 if (discard_padding > 0 && avctx->frame_size && ec->last_audio_frame) {
260 avpkt->duration = FFMIN(avpkt->duration, ff_samples_to_time_base(avctx, avctx->frame_size));
261 discard_padding = avctx->frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
262 }
263
264 if (skip_samples > 0 || discard_padding > 0) {
265 uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
266 if (!packet_sd)
267 return AVERROR(ENOMEM);
268 AV_WL32A(packet_sd + 0, skip_samples);
269 AV_WL32A(packet_sd + 4, discard_padding);
270 AV_WL8 (packet_sd + 8, AV_RB8(frame_sd->data + 8));
271 AV_WL8 (packet_sd + 9, AV_RB8(frame_sd->data + 9));
272 }
273 }
274 }
275 }
276
277 return 0;
278}
279
281 AVPacket *pkt, const AVFrame *frame)
282{
283 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
284 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
285 if (ret < 0)
286 return ret;
287 pkt->opaque = frame->opaque;
288 }
289
290 return 0;
291}
292
294 AVFrame *frame, int *got_packet)
295{
296 const FFCodec *const codec = ffcodec(avctx->codec);
297 int ret;
298
299 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
301 av_assert0(ret <= 0);
302
303 if (!ret && *got_packet) {
304 if (avpkt->data) {
305 ret = encode_make_refcounted(avctx, avpkt);
306 if (ret < 0)
307 goto unref;
308 // Date returned by encoders must always be ref-counted
309 av_assert0(avpkt->buf);
310 }
311
312 // set the timestamps for the simple no-delay case
313 // encoders with delay have to set the timestamps themselves
314 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
316 ret = encode_set_packet_props(avctx, avpkt, frame);
317 if (ret < 0)
318 goto unref;
319
320 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
321 if (ret < 0)
322 goto unref;
323 }
324
325 // dts equals pts unless there is reordering
326 // there can be no reordering if there is no encoder delay
330 avpkt->dts = avpkt->pts;
331 } else {
332unref:
333 av_packet_unref(avpkt);
334 }
335
336 if (frame)
338
339 return ret;
340}
341
343{
344 AVCodecInternal *avci = avctx->internal;
345 AVFrame *frame = avci->in_frame;
346 const FFCodec *const codec = ffcodec(avctx->codec);
347 int got_packet;
348 int ret;
349
350 if (avci->draining_done)
351 return AVERROR_EOF;
352
353 if (!frame->buf[0] && !avci->draining) {
355 ret = ff_encode_get_frame(avctx, frame);
356 if (ret < 0 && ret != AVERROR_EOF)
357 return ret;
358 }
359
360 if (!frame->buf[0]) {
361 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
363 return AVERROR_EOF;
364
365 // Flushing is signaled with a NULL frame
366 frame = NULL;
367 }
368
369 got_packet = 0;
370
372
373#if CONFIG_FRAME_THREAD_ENCODER
374 if (avci->frame_thread_encoder)
375 /* This will unref frame. */
376 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
377 else
378#endif
379 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
380
381 if (avci->draining && !got_packet)
382 avci->draining_done = 1;
383
384 return ret;
385}
386
388{
389 int ret;
390
391 while (!avpkt->data && !avpkt->side_data) {
392 ret = encode_simple_internal(avctx, avpkt);
393 if (ret < 0)
394 return ret;
395 }
396
397 return 0;
398}
399
401{
402 AVCodecInternal *avci = avctx->internal;
403 int ret;
404
405 if (avci->draining_done)
406 return AVERROR_EOF;
407
408 av_assert0(!avpkt->data && !avpkt->side_data);
409
410 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
411 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
412 avctx->stats_out[0] = '\0';
413 }
414
416 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
417 if (ret < 0)
418 av_packet_unref(avpkt);
419 else
420 // Encoders must always return ref-counted buffers.
421 // Side-data only packets have no data and can be not ref-counted.
422 av_assert0(!avpkt->data || avpkt->buf);
423 } else
424 ret = encode_simple_receive_packet(avctx, avpkt);
425 if (ret >= 0)
426 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
427
428 if (ret == AVERROR_EOF)
429 avci->draining_done = 1;
430
431 return ret;
432}
433
434#if CONFIG_LCMS2
436{
437 enum AVColorTransferCharacteristic trc = frame->color_trc;
438 enum AVColorPrimaries prim = frame->color_primaries;
439 const FFCodec *const codec = ffcodec(avctx->codec);
440 AVCodecInternal *avci = avctx->internal;
441 cmsHPROFILE profile;
442 int ret;
443
444 /* don't generate ICC profiles if disabled or unsupported */
445 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
446 return 0;
448 return 0;
449
450 if (trc == AVCOL_TRC_UNSPECIFIED)
451 trc = avctx->color_trc;
452 if (prim == AVCOL_PRI_UNSPECIFIED)
453 prim = avctx->color_primaries;
454 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
455 return 0; /* can't generate ICC profile with missing csp tags */
456
458 return 0; /* don't overwrite existing ICC profile */
459
460 if (!avci->icc.avctx) {
461 ret = ff_icc_context_init(&avci->icc, avctx);
462 if (ret < 0)
463 return ret;
464 }
465
466 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
467 if (ret < 0)
468 return ret;
469
470 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
471 cmsCloseProfile(profile);
472 return ret;
473}
474#else /* !CONFIG_LCMS2 */
479#endif
480
482{
483 AVCodecInternal *avci = avctx->internal;
484 EncodeContext *ec = encode_ctx(avci);
485 AVFrame *dst = avci->buffer_frame;
486 int ret;
487
488 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
489 /* extract audio service type metadata */
491 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
492 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
493
494 /* check for valid frame size */
495 if (avctx->frame_size) {
496 /* if we already got an undersized frame, that must have been the last */
497 if (ec->last_audio_frame) {
498 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
499 return AVERROR(EINVAL);
500 }
501 if (src->nb_samples > avctx->frame_size) {
502 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
503 return AVERROR(EINVAL);
504 }
505 if (src->nb_samples < avctx->frame_size) {
506 ec->last_audio_frame = 1;
509 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
510 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
511
512 if (out_samples != src->nb_samples) {
513 ret = pad_last_frame(avctx, dst, src, out_samples);
514 if (ret < 0)
515 return ret;
516 goto finish;
517 }
518 }
519 }
520 }
521 } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
523
524 if (!(desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
525 (src->format != avctx->pix_fmt ||
526 src->width < avctx->width || src->height < avctx->height)) {
527 av_log(avctx, AV_LOG_ERROR, "Frame size or pixel format does not match the encoder\n");
528 return AVERROR(EINVAL);
529 }
530 }
531
533 if (ret < 0)
534 return ret;
535
536finish:
537
538 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
540 if (ret < 0)
541 return ret;
542 }
543
544 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
545 // since otherwise we cannot be sure that whatever value it has is in the
546 // right timebase, so we would produce an incorrect value, which is worse
547 // than none at all
548 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
549 dst->duration = 0;
550
551 return 0;
552}
553
555{
556 AVCodecInternal *avci = avctx->internal;
557 int ret;
558
559 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
560 return AVERROR(EINVAL);
561
562 if (avci->draining)
563 return AVERROR_EOF;
564
565 if (avci->buffer_frame->buf[0])
566 return AVERROR(EAGAIN);
567
568 if (!frame) {
569 avci->draining = 1;
570 } else {
572 if (ret < 0)
573 return ret;
574 }
575
576 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
578 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
579 return ret;
580 }
581
582 avctx->frame_num++;
583
584 return 0;
585}
586
588{
589 AVCodecInternal *avci = avctx->internal;
590 int ret;
591
592 av_packet_unref(avpkt);
593
594 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
595 return AVERROR(EINVAL);
596
597 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
598 av_packet_move_ref(avpkt, avci->buffer_pkt);
599 } else {
600 ret = encode_receive_packet_internal(avctx, avpkt);
601 if (ret < 0)
602 return ret;
603 }
604
605 return 0;
606}
607
609{
610 const AVCodec *codec = avctx->codec;
611 const FFCodec *codec2 = ffcodec(codec);
612 const AVDictionaryEntry *t = NULL;
614 int ret;
615
617
618 ret = av_dict_copy(&copy, *dict, 0);
619 if (ret < 0)
620 goto end;
621
622 // Remove the dictionary entries that would be applied to the private codec context
623 if (codec->priv_class) {
624 while ((t = av_dict_iterate(*dict, t))) {
625 if (av_opt_find(avctx->priv_data, t->key, NULL,
627 av_dict_set(dict, t->key, NULL, 0);
628 }
629 }
630
631 // Ditto for global options
632 if (codec2->defaults) {
633 const FFCodecDefault *d = codec2->defaults;
634 while (d->key) {
636 av_dict_set(dict, d->key, NULL, 0);
637 d++;
638 }
639 }
640
641 // If any entry remains, then the requested option/s don't exist or are not settable.
642 if (av_dict_count(*dict)) {
644 goto end;
645 }
646
647 ret = av_dict_copy(dict, copy, 0);
648 if (ret < 0)
649 goto end;
650
651 // Do a dry run of applying the options, to ensure the encoder is unchanged in case
652 // one of them has an invalid value.
653 // This is done twice, once for avctx and once for the AVCodec, because using the
654 // search children flag in combination with the fake obj flag will iterate through
655 // the options from all compiled in codecs if you pass the avctx class.
656 if (codec->priv_class) {
658 if (ret < 0)
659 goto end;
660 }
662 if (ret < 0)
663 goto end;
664
665 // The dictionary should be empty.
667
669 if (ret < 0)
670 goto end;
671
672 // The dictionary should be empty.
673 av_assert0(!av_dict_count(*dict));
674
675 ret = 0;
676end:
678
679 return ret;
680}
681
683{
684 const FFCodec *codec = ffcodec(avctx->codec);
685 int ret = AVERROR_BUG;
686
687 if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
688 return AVERROR(EINVAL);
689
691 av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n");
692 return AVERROR(ENOSYS);
693 }
694
695 if (codec->reconf)
696 ret = codec->reconf(avctx, dict);
697 else
698 ret = ff_encode_reconf_parse_dict(avctx, dict);
699 if (ret < 0)
700 return ret;
701
702 return 0;
703}
704
706{
707 const AVCodec *c = avctx->codec;
708 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
709 const enum AVPixelFormat *pix_fmts;
710 int ret, i, num_pix_fmts;
711
712 if (!pixdesc) {
713 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
714 avctx->pix_fmt);
715 return AVERROR(EINVAL);
716 }
717
719 0, (const void **) &pix_fmts, &num_pix_fmts);
720 if (ret < 0)
721 return ret;
722
723 if (pix_fmts) {
724 for (i = 0; i < num_pix_fmts; i++)
725 if (avctx->pix_fmt == pix_fmts[i])
726 break;
727 if (i == num_pix_fmts) {
728 av_log(avctx, AV_LOG_ERROR,
729 "Specified pixel format %s is not supported by the %s encoder.\n",
730 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
731
732 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
733 for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
734 av_log(avctx, AV_LOG_ERROR, " %s\n",
736 }
737
738 return AVERROR(EINVAL);
739 }
746 }
747
748 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
749 const enum AVAlphaMode *alpha_modes;
750 int num_alpha_modes;
752 0, (const void **) &alpha_modes, &num_alpha_modes);
753 if (ret < 0)
754 return ret;
755
756 if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
757 for (i = 0; i < num_alpha_modes; i++) {
758 if (avctx->alpha_mode == alpha_modes[i])
759 break;
760 }
761 if (i == num_alpha_modes) {
762 av_log(avctx, AV_LOG_ERROR,
763 "Specified alpha mode '%s' is not supported by the %s encoder.\n",
764 av_alpha_mode_name(avctx->alpha_mode), c->name);
765 av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
766 for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
767 av_log(avctx, AV_LOG_ERROR, " %s\n",
768 av_alpha_mode_name(alpha_modes[p]));
769 }
770 return AVERROR(EINVAL);
771 }
772 }
773 }
774
775 if ( avctx->bits_per_raw_sample < 0
776 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
777 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
778 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
779 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
780 }
781 if (avctx->width <= 0 || avctx->height <= 0) {
782 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
783 return AVERROR(EINVAL);
784 }
785
786 if (avctx->hw_frames_ctx) {
788 if (frames_ctx->format != avctx->pix_fmt) {
789 av_log(avctx, AV_LOG_ERROR,
790 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
791 return AVERROR(EINVAL);
792 }
793 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
794 avctx->sw_pix_fmt != frames_ctx->sw_format) {
795 av_log(avctx, AV_LOG_ERROR,
796 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
797 "and AVHWFramesContext.sw_format (%s)\n",
799 av_get_pix_fmt_name(frames_ctx->sw_format));
800 return AVERROR(EINVAL);
801 }
802 avctx->sw_pix_fmt = frames_ctx->sw_format;
803 }
804
805 return 0;
806}
807
809{
810 const AVCodec *c = avctx->codec;
811 const enum AVSampleFormat *sample_fmts;
812 const int *supported_samplerates;
813 const AVChannelLayout *ch_layouts;
814 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
815
816 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
817 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
818 avctx->sample_fmt);
819 return AVERROR(EINVAL);
820 }
821
823 0, (const void **) &sample_fmts,
824 &num_sample_fmts);
825 if (ret < 0)
826 return ret;
827 if (sample_fmts) {
828 for (i = 0; i < num_sample_fmts; i++) {
829 if (avctx->sample_fmt == sample_fmts[i])
830 break;
831 if (avctx->ch_layout.nb_channels == 1 &&
834 avctx->sample_fmt = sample_fmts[i];
835 break;
836 }
837 }
838 if (i == num_sample_fmts) {
839 av_log(avctx, AV_LOG_ERROR,
840 "Specified sample format %s is not supported by the %s encoder\n",
841 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
842
843 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
844 for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
845 av_log(avctx, AV_LOG_ERROR, " %s\n",
847 }
848
849 return AVERROR(EINVAL);
850 }
851 }
852
854 0, (const void **) &supported_samplerates,
855 &num_samplerates);
856 if (ret < 0)
857 return ret;
858 if (supported_samplerates) {
859 for (i = 0; i < num_samplerates; i++)
860 if (avctx->sample_rate == supported_samplerates[i])
861 break;
862 if (i == num_samplerates) {
863 av_log(avctx, AV_LOG_ERROR,
864 "Specified sample rate %d is not supported by the %s encoder\n",
865 avctx->sample_rate, c->name);
866
867 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
868 for (int p = 0; supported_samplerates[p]; p++)
869 av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
870
871 return AVERROR(EINVAL);
872 }
873 }
875 0, (const void **) &ch_layouts, &num_ch_layouts);
876 if (ret < 0)
877 return ret;
878 if (ch_layouts) {
879 for (i = 0; i < num_ch_layouts; i++) {
880 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
881 break;
882 }
883 if (i == num_ch_layouts) {
884 char buf[512];
885 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
886 av_log(avctx, AV_LOG_ERROR,
887 "Specified channel layout '%s' is not supported by the %s encoder\n",
888 ret > 0 ? buf : "?", c->name);
889
890 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
891 for (int p = 0; ch_layouts[p].nb_channels; p++) {
892 ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
893 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
894 }
895 return AVERROR(EINVAL);
896 }
897 }
898
899 if (!avctx->bits_per_raw_sample)
901 if (!avctx->bits_per_raw_sample)
903
904 return 0;
905}
906
908{
909 AVCodecInternal *avci = avctx->internal;
910 EncodeContext *ec = encode_ctx(avci);
911 int ret = 0;
912
913 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
914 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
915 return AVERROR(EINVAL);
916 }
917
918 if (avctx->bit_rate < 0) {
919 av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
920 return AVERROR(EINVAL);
921 }
922
923 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
925 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
926 "encoder does not support it.\n");
927 return AVERROR(EINVAL);
928 }
929
930 switch (avctx->codec_type) {
931 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
932 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
933 }
934 if (ret < 0)
935 return ret;
936
937 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
938 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
939 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
940 }
941
942 if (!avctx->rc_initial_buffer_occupancy)
943 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
944
947
949 avci->in_frame = av_frame_alloc();
950 if (!avci->in_frame)
951 return AVERROR(ENOMEM);
952 }
953
954 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
956 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
957 "from an encoder not supporting it\n");
958 return AVERROR(ENOSYS);
959 }
960
961 avci->recon_frame = av_frame_alloc();
962 if (!avci->recon_frame)
963 return AVERROR(ENOMEM);
964 }
965
966 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
967 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
968 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
969 const AVFrameSideData *sd_frame;
970 AVPacketSideData *sd_packet;
971
974 type_frame);
975 if (!sd_frame ||
977 type_packet))
978
979 continue;
980
982 type_packet, sd_frame->size, 0);
983 if (!sd_packet)
984 return AVERROR(ENOMEM);
985
986 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
987 }
988
989#if CONFIG_FRAME_THREAD_ENCODER
991 if (ret < 0)
992 return ret;
993#endif
994
995 return 0;
996}
997
999{
1000 int ret;
1001
1003
1004 frame->format = avctx->pix_fmt;
1005 if (frame->width <= 0 || frame->height <= 0) {
1006 frame->width = avctx->width;
1007 frame->height = avctx->height;
1008 }
1009
1011 if (ret < 0) {
1012 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1014 return ret;
1015 }
1016
1017 return 0;
1018}
1019
1021{
1022 AVCodecInternal *avci = avctx->internal;
1023
1024 if (!avci->recon_frame)
1025 return AVERROR(EINVAL);
1026 if (!avci->recon_frame->buf[0])
1027 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
1028
1030 return 0;
1031}
1032
1034{
1035 AVCodecInternal *avci = avctx->internal;
1036
1037 if (avci->in_frame)
1038 av_frame_unref(avci->in_frame);
1039 if (avci->recon_frame)
1041}
1042
1044{
1045 return av_mallocz(sizeof(EncodeContext));
1046}
1047
1049{
1051 AVCPBProperties *props;
1052 size_t size;
1053 int i;
1054
1055 for (i = 0; i < avctx->nb_coded_side_data; i++)
1057 return (AVCPBProperties *)avctx->coded_side_data[i].data;
1058
1059 props = av_cpb_properties_alloc(&size);
1060 if (!props)
1061 return NULL;
1062
1063 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1064 if (!tmp) {
1065 av_freep(&props);
1066 return NULL;
1067 }
1068
1069 avctx->coded_side_data = tmp;
1070 avctx->nb_coded_side_data++;
1071
1073 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1074 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1075
1076 return props;
1077}
1078
1080 int error_count, enum AVPictureType pict_type)
1081{
1082 uint8_t *side_data;
1083 size_t side_data_size;
1084
1085 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1086 if (!side_data) {
1087 side_data_size = 4+4+8*error_count;
1089 side_data_size);
1090 }
1091
1092 if (!side_data || side_data_size < 4+4+8*error_count)
1093 return AVERROR(ENOMEM);
1094
1095 AV_WL32(side_data, quality);
1096 side_data[4] = pict_type;
1097 side_data[5] = error_count;
1098 for (int i = 0; i < error_count; ++i)
1099 AV_WL64(side_data+8 + 8*i , error[i]);
1100
1101 return 0;
1102}
1103
1104int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
1105{
1106 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
1107 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
1108 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
1109 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
1110 uint16_t *matrix = matrices[m];
1111 if (matrix && (types & (1U << m))) {
1112 for (int i = 0; i < 64; i++) {
1113 if (matrix[i] < min || matrix[i] > max) {
1114 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);
1115 return AVERROR(EINVAL);
1116 }
1117 }
1118 }
1119 }
1120 return 0;
1121}
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:57
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:982
AVAudioServiceType
Definition defs.h:249
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:62
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition encode.c:218
static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:137
static int encode_preinit_audio(AVCodecContext *avctx)
Definition encode.c:808
static int encode_preinit_video(AVCodecContext *avctx)
Definition encode.c:705
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition encode.c:1048
AVCodecInternal * ff_encode_internal_alloc(void)
Definition encode.c:1043
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
Definition encode.c:481
static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
Definition encode.c:475
int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt, AVFrame *frame, int *got_packet)
Definition encode.c:293
static EncodeContext * encode_ctx(AVCodecInternal *avci)
Definition encode.c:57
static int encode_set_packet_props(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame)
Definition encode.c:233
int ff_encode_preinit(AVCodecContext *avctx)
Definition encode.c:907
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:1079
static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
Pad last frame with silence.
Definition encode.c:157
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:400
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:387
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:280
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
Definition encode.c:342
int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
avcodec_receive_frame() implementation for encoders.
Definition encode.c:1020
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
Definition encode.c:998
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
Definition encode.c:1104
av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
Definition encode.c:608
void ff_encode_flush_buffers(AVCodecContext *avctx)
Definition encode.c:1033
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:682
#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:587
#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:554
int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
The default callback for AVCodecContext.get_encode_buffer().
Definition encode.c:84
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:823
@ AV_CODEC_CONFIG_PIX_FORMAT
AVPixelFormat, terminated by AV_PIX_FMT_NONE.
Definition avcodec.h:2588
@ AV_CODEC_CONFIG_SAMPLE_FORMAT
AVSampleFormat, terminated by AV_SAMPLE_FMT_NONE.
Definition avcodec.h:2591
@ AV_CODEC_CONFIG_ALPHA_MODE
AVAlphaMode, terminated by AVALPHA_MODE_UNSPECIFIED.
Definition avcodec.h:2595
@ AV_CODEC_CONFIG_SAMPLE_RATE
int, terminated by 0
Definition avcodec.h:2590
@ AV_CODEC_CONFIG_CHANNEL_LAYOUT
AVChannelLayout, terminated by {0}.
Definition avcodec.h:2592
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub)
Definition encode.c:204
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:703
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:496
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition frame.c:659
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:206
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition frame.c:523
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:278
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:647
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
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:1203
@ 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:2075
#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:2046
#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:788
AVAlphaMode
Correlation between the alpha channel and color values.
Definition pixfmt.h:821
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition pixfmt.h:822
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:296
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:643
uint16_t * chroma_intra_matrix
custom intra quantization matrix
Definition avcodec.h:976
int width
picture width / height.
Definition avcodec.h:604
AVPacketSideData * coded_side_data
Additional data associated with the entire coded stream.
Definition avcodec.h:1773
char * stats_out
pass1 encoding statistics output buffer
Definition avcodec.h:1330
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition avcodec.h:1714
int rc_buffer_size
decoder bitstream buffer size
Definition avcodec.h:1273
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:650
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
int nb_coded_side_data
Definition avcodec.h:1774
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition avcodec.h:1089
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition avcodec.h:1472
enum AVMediaType codec_type
Definition avcodec.h:451
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1888
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition avcodec.h:969
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:1877
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:1316
int nb_decoded_side_data
Definition avcodec.h:1935
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1576
int initial_padding
Audio only.
Definition avcodec.h:1114
int sample_rate
samples per second
Definition avcodec.h:1040
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:960
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
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:1934
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
Definition avcodec.h:1942
enum AVCodecID codec_id
Definition avcodec.h:453
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
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
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:2104
int intra_only_flag
This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only formats (i.e.
Definition encode.c:48
AVCodecInternal avci
Definition encode.c:41
int last_audio_frame
An audio frame with less than required samples has been submitted (and potentially padded with silenc...
Definition encode.c:54
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]